- update to 2.6.1-rc2 -- first cut.
[linux-flexiantxendom0-3.2.10.git] / include / linux / nfs_fs.h
1 /*
2  *  linux/include/linux/nfs_fs.h
3  *
4  *  Copyright (C) 1992  Rick Sladkey
5  *
6  *  OS-specific nfs filesystem definitions and declarations
7  */
8
9 #ifndef _LINUX_NFS_FS_H
10 #define _LINUX_NFS_FS_H
11
12 #include <linux/config.h>
13 #include <linux/in.h>
14 #include <linux/mm.h>
15 #include <linux/pagemap.h>
16 #include <linux/rwsem.h>
17 #include <linux/wait.h>
18 #include <linux/uio.h>
19
20 #include <linux/nfs_fs_sb.h>
21
22 #include <linux/sunrpc/debug.h>
23 #include <linux/sunrpc/auth.h>
24 #include <linux/sunrpc/clnt.h>
25
26 #include <linux/nfs.h>
27 #include <linux/nfs2.h>
28 #include <linux/nfs3.h>
29 #include <linux/nfs4.h>
30 #include <linux/nfs_xdr.h>
31
32 /*
33  * Enable debugging support for nfs client.
34  * Requires RPC_DEBUG.
35  */
36 #ifdef RPC_DEBUG
37 # define NFS_DEBUG
38 #endif
39
40 #define NFS_MAX_FILE_IO_BUFFER_SIZE     32768
41 #define NFS_DEF_FILE_IO_BUFFER_SIZE     4096
42
43 /*
44  * The upper limit on timeouts for the exponential backoff algorithm.
45  */
46 #define NFS_WRITEBACK_DELAY             (5*HZ)
47 #define NFS_WRITEBACK_LOCKDELAY         (60*HZ)
48 #define NFS_COMMIT_DELAY                (5*HZ)
49
50 /*
51  * superblock magic number for NFS
52  */
53 #define NFS_SUPER_MAGIC                 0x6969
54
55 /*
56  * These are the default flags for swap requests
57  */
58 #define NFS_RPC_SWAPFLAGS               (RPC_TASK_SWAPPER|RPC_TASK_ROOTCREDS)
59
60 #define NFS_RW_SYNC             0x0001  /* O_SYNC handling */
61 #define NFS_RW_SWAP             0x0002  /* This is a swap request */
62
63 /*
64  * When flushing a cluster of dirty pages, there can be different
65  * strategies:
66  */
67 #define FLUSH_AGING             0       /* only flush old buffers */
68 #define FLUSH_SYNC              1       /* file being synced, or contention */
69 #define FLUSH_WAIT              2       /* wait for completion */
70 #define FLUSH_STABLE            4       /* commit to stable storage */
71
72 #ifdef __KERNEL__
73
74 /*
75  * NFSv3 Access mode cache
76  */
77 struct nfs_access_cache {
78         unsigned long           jiffies;
79         struct rpc_cred *       cred;
80         int                     mask;
81         int                     err;
82 };
83
84 /*
85  * nfs fs inode data in memory
86  */
87 struct nfs_inode {
88         /*
89          * The 64bit 'inode number'
90          */
91         __u64 fileid;
92
93         /*
94          * NFS file handle
95          */
96         struct nfs_fh           fh;
97
98         /*
99          * Various flags
100          */
101         unsigned short          flags;
102
103         /*
104          * read_cache_jiffies is when we started read-caching this inode,
105          * and read_cache_mtime is the mtime of the inode at that time.
106          * attrtimeo is for how long the cached information is assumed
107          * to be valid. A successful attribute revalidation doubles
108          * attrtimeo (up to acregmax/acdirmax), a failure resets it to
109          * acregmin/acdirmin.
110          *
111          * We need to revalidate the cached attrs for this inode if
112          *
113          *      jiffies - read_cache_jiffies > attrtimeo
114          *
115          * and invalidate any cached data/flush out any dirty pages if
116          * we find that
117          *
118          *      mtime != read_cache_mtime
119          */
120         unsigned long           read_cache_jiffies;
121         struct timespec         read_cache_ctime;
122         struct timespec         read_cache_mtime;
123         __u64                   read_cache_isize;
124         unsigned long           attrtimeo;
125         unsigned long           attrtimeo_timestamp;
126         __u64                   change_attr;            /* v4 only */
127
128         /*
129          * Timestamp that dates the change made to read_cache_mtime.
130          * This is of use for dentry revalidation
131          */
132         unsigned long           cache_mtime_jiffies;
133
134         struct nfs_access_cache cache_access;
135
136         /*
137          * This is the cookie verifier used for NFSv3 readdir
138          * operations
139          */
140         __u32                   cookieverf[2];
141
142         /*
143          * This is the list of dirty unwritten pages.
144          */
145         struct list_head        dirty;
146         struct list_head        commit;
147         struct radix_tree_root  nfs_page_tree;
148
149         unsigned int            ndirty,
150                                 ncommit,
151                                 npages;
152
153         /* Credentials for shared mmap */
154         struct rpc_cred         *mm_cred;
155
156         wait_queue_head_t       nfs_i_wait;
157
158 #ifdef CONFIG_NFS_V4
159         /* NFSv4 state */
160         struct list_head        open_states;
161 #endif /* CONFIG_NFS_V4*/
162
163         struct inode            vfs_inode;
164 };
165
166 /*
167  * Legal inode flag values
168  */
169 #define NFS_INO_STALE           0x0001          /* possible stale inode */
170 #define NFS_INO_ADVISE_RDPLUS   0x0002          /* advise readdirplus */
171 #define NFS_INO_REVALIDATING    0x0004          /* revalidating attrs */
172 #define NFS_INO_FLUSH           0x0008          /* inode is due for flushing */
173 #define NFS_INO_FAKE_ROOT       0x0080          /* root inode placeholder */
174
175 static inline struct nfs_inode *NFS_I(struct inode *inode)
176 {
177         return container_of(inode, struct nfs_inode, vfs_inode);
178 }
179 #define NFS_SB(s)               ((struct nfs_server *)(s->s_fs_info))
180
181 #define NFS_FH(inode)                   (&NFS_I(inode)->fh)
182 #define NFS_SERVER(inode)               (NFS_SB(inode->i_sb))
183 #define NFS_CLIENT(inode)               (NFS_SERVER(inode)->client)
184 #define NFS_PROTO(inode)                (NFS_SERVER(inode)->rpc_ops)
185 #define NFS_ADDR(inode)                 (RPC_PEERADDR(NFS_CLIENT(inode)))
186 #define NFS_COOKIEVERF(inode)           (NFS_I(inode)->cookieverf)
187 #define NFS_READTIME(inode)             (NFS_I(inode)->read_cache_jiffies)
188 #define NFS_MTIME_UPDATE(inode)         (NFS_I(inode)->cache_mtime_jiffies)
189 #define NFS_CACHE_CTIME(inode)          (NFS_I(inode)->read_cache_ctime)
190 #define NFS_CACHE_MTIME(inode)          (NFS_I(inode)->read_cache_mtime)
191 #define NFS_CACHE_ISIZE(inode)          (NFS_I(inode)->read_cache_isize)
192 #define NFS_CHANGE_ATTR(inode)          (NFS_I(inode)->change_attr)
193 #define NFS_CACHEINV(inode) \
194 do { \
195         NFS_READTIME(inode) = jiffies - NFS_MAXATTRTIMEO(inode) - 1; \
196 } while (0)
197 #define NFS_ATTRTIMEO(inode)            (NFS_I(inode)->attrtimeo)
198 #define NFS_MINATTRTIMEO(inode) \
199         (S_ISDIR(inode->i_mode)? NFS_SERVER(inode)->acdirmin \
200                                : NFS_SERVER(inode)->acregmin)
201 #define NFS_MAXATTRTIMEO(inode) \
202         (S_ISDIR(inode->i_mode)? NFS_SERVER(inode)->acdirmax \
203                                : NFS_SERVER(inode)->acregmax)
204 #define NFS_ATTRTIMEO_UPDATE(inode)     (NFS_I(inode)->attrtimeo_timestamp)
205
206 #define NFS_FLAGS(inode)                (NFS_I(inode)->flags)
207 #define NFS_REVALIDATING(inode)         (NFS_FLAGS(inode) & NFS_INO_REVALIDATING)
208 #define NFS_STALE(inode)                (NFS_FLAGS(inode) & NFS_INO_STALE)
209 #define NFS_FAKE_ROOT(inode)            (NFS_FLAGS(inode) & NFS_INO_FAKE_ROOT)
210
211 #define NFS_FILEID(inode)               (NFS_I(inode)->fileid)
212
213 static inline int nfs_server_capable(struct inode *inode, int cap)
214 {
215         return NFS_SERVER(inode)->caps & cap;
216 }
217
218 static inline int NFS_USE_READDIRPLUS(struct inode *inode)
219 {
220         return NFS_FLAGS(inode) & NFS_INO_ADVISE_RDPLUS;
221 }
222
223 static inline
224 loff_t page_offset(struct page *page)
225 {
226         return ((loff_t)page->index) << PAGE_CACHE_SHIFT;
227 }
228
229 /*
230  * linux/fs/nfs/inode.c
231  */
232 extern void nfs_zap_caches(struct inode *);
233 extern struct inode *nfs_fhget(struct super_block *, struct nfs_fh *,
234                                 struct nfs_fattr *);
235 extern int __nfs_refresh_inode(struct inode *, struct nfs_fattr *);
236 extern int nfs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
237 extern int nfs_permission(struct inode *, int, struct nameidata *);
238 extern void nfs_set_mmcred(struct inode *, struct rpc_cred *);
239 extern int nfs_open(struct inode *, struct file *);
240 extern int nfs_release(struct inode *, struct file *);
241 extern int __nfs_revalidate_inode(struct nfs_server *, struct inode *);
242 extern int nfs_setattr(struct dentry *, struct iattr *);
243
244 /*
245  * linux/fs/nfs/file.c
246  */
247 extern struct inode_operations nfs_file_inode_operations;
248 extern struct file_operations nfs_file_operations;
249 extern struct address_space_operations nfs_file_aops;
250
251 /*
252  * linux/fs/nfs/xattr.c
253  */
254 #ifdef CONFIG_NFS_ACL
255 extern ssize_t nfs_listxattr(struct dentry *, char *, size_t);
256 extern ssize_t nfs_getxattr(struct dentry *, const char *, void *, size_t);
257 extern int nfs_setxattr(struct dentry *, const char *,
258                         const void *, size_t, int);
259 extern int nfs_removexattr (struct dentry *, const char *name);
260 #else
261 # define nfs_listxattr NULL
262 # define nfs_getxattr NULL
263 # define nfs_setxattr NULL
264 # define nfs_removexattr NULL
265 #endif
266
267 static __inline__ struct rpc_cred *
268 nfs_file_cred(struct file *file)
269 {
270         struct rpc_cred *cred = NULL;
271         if (file)
272                 cred = (struct rpc_cred *)file->private_data;
273 #ifdef RPC_DEBUG
274         if (cred && cred->cr_magic != RPCAUTH_CRED_MAGIC)
275                 BUG();
276 #endif
277         return cred;
278 }
279
280 /*
281  * linux/fs/nfs/direct.c
282  */
283 extern int nfs_direct_IO(int, struct kiocb *, const struct iovec *, loff_t,
284                         unsigned long);
285
286 /*
287  * linux/fs/nfs/dir.c
288  */
289 extern struct inode_operations nfs_dir_inode_operations;
290 extern struct file_operations nfs_dir_operations;
291 extern struct dentry_operations nfs_dentry_operations;
292
293 /*
294  * linux/fs/nfs/symlink.c
295  */
296 extern struct inode_operations nfs_symlink_inode_operations;
297
298 /*
299  * linux/fs/nfs/locks.c
300  */
301 extern int nfs_lock(struct file *, int, struct file_lock *);
302
303 /*
304  * linux/fs/nfs/unlink.c
305  */
306 extern int  nfs_async_unlink(struct dentry *);
307 extern void nfs_complete_unlink(struct dentry *);
308
309 /*
310  * linux/fs/nfs/write.c
311  */
312 extern int  nfs_writepage(struct page *page, struct writeback_control *wbc);
313 extern int  nfs_writepages(struct address_space *, struct writeback_control *);
314 extern int  nfs_flush_incompatible(struct file *file, struct page *page);
315 extern int  nfs_updatepage(struct file *, struct page *, unsigned int, unsigned int);
316 extern void nfs_writeback_done(struct rpc_task *task);
317 extern void nfs_writedata_release(struct rpc_task *task);
318
319 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
320 extern void nfs_commit_release(struct rpc_task *task);
321 extern void nfs_commit_done(struct rpc_task *);
322 #endif
323
324 /*
325  * Try to write back everything synchronously (but check the
326  * return value!)
327  */
328 extern int  nfs_sync_file(struct inode *, struct file *, unsigned long, unsigned int, int);
329 extern int  nfs_flush_file(struct inode *, struct file *, unsigned long, unsigned int, int);
330 extern int  nfs_flush_list(struct list_head *, int, int);
331 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
332 extern int  nfs_commit_file(struct inode *, struct file *, unsigned long, unsigned int, int);
333 extern int  nfs_commit_list(struct list_head *, int);
334 #else
335 static inline int
336 nfs_commit_file(struct inode *inode, struct file *file, unsigned long offset,
337                 unsigned int len, int flags)
338 {
339         return 0;
340 }
341 #endif
342
343 static inline int
344 nfs_have_writebacks(struct inode *inode)
345 {
346         return NFS_I(inode)->npages != 0;
347 }
348
349 static inline int
350 nfs_wb_all(struct inode *inode)
351 {
352         int error = nfs_sync_file(inode, 0, 0, 0, FLUSH_WAIT);
353         return (error < 0) ? error : 0;
354 }
355
356 /*
357  * Write back all requests on one page - we do this before reading it.
358  */
359 static inline int
360 nfs_wb_page(struct inode *inode, struct page* page)
361 {
362         int error = nfs_sync_file(inode, 0, page->index, 1,
363                                                 FLUSH_WAIT | FLUSH_STABLE);
364         return (error < 0) ? error : 0;
365 }
366
367 /*
368  * Write back all pending writes for one user.. 
369  */
370 static inline int
371 nfs_wb_file(struct inode *inode, struct file *file)
372 {
373         int error = nfs_sync_file(inode, file, 0, 0, FLUSH_WAIT);
374         return (error < 0) ? error : 0;
375 }
376
377 /* Hack for future NFS swap support */
378 #ifndef IS_SWAPFILE
379 # define IS_SWAPFILE(inode)     (0)
380 #endif
381
382 /*
383  * linux/fs/nfs/read.c
384  */
385 extern int  nfs_readpage(struct file *, struct page *);
386 extern int  nfs_readpages(struct file *, struct address_space *,
387                 struct list_head *, unsigned);
388 extern int  nfs_pagein_list(struct list_head *, int);
389 extern void nfs_readpage_result(struct rpc_task *);
390 extern void nfs_readdata_release(struct rpc_task *);
391
392 /*
393  * linux/fs/mount_clnt.c
394  * (Used only by nfsroot module)
395  */
396 extern int  nfsroot_mount(struct sockaddr_in *, char *, struct nfs_fh *,
397                 int, int);
398
399 /*
400  * inline functions
401  */
402 static inline int
403 nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
404 {
405         if (time_before(jiffies, NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
406                 return NFS_STALE(inode) ? -ESTALE : 0;
407         return __nfs_revalidate_inode(server, inode);
408 }
409
410 static inline int
411 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
412 {
413         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
414                 return 0;
415         return __nfs_refresh_inode(inode,fattr);
416 }
417
418 static inline loff_t
419 nfs_size_to_loff_t(__u64 size)
420 {
421         loff_t maxsz = (((loff_t) ULONG_MAX) << PAGE_CACHE_SHIFT) + PAGE_CACHE_SIZE - 1;
422         if (size > maxsz)
423                 return maxsz;
424         return (loff_t) size;
425 }
426
427 static inline ino_t
428 nfs_fileid_to_ino_t(u64 fileid)
429 {
430         ino_t ino = (ino_t) fileid;
431         if (sizeof(ino_t) < sizeof(u64))
432                 ino ^= fileid >> (sizeof(u64)-sizeof(ino_t)) * 8;
433         return ino;
434 }
435
436 /* NFS root */
437
438 extern void * nfs_root_data(void);
439
440 #define nfs_wait_event(clnt, wq, condition)                             \
441 ({                                                                      \
442         int __retval = 0;                                               \
443         if (clnt->cl_intr) {                                            \
444                 sigset_t oldmask;                                       \
445                 rpc_clnt_sigmask(clnt, &oldmask);                       \
446                 __retval = wait_event_interruptible(wq, condition);     \
447                 rpc_clnt_sigunmask(clnt, &oldmask);                     \
448         } else                                                          \
449                 wait_event(wq, condition);                              \
450         __retval;                                                       \
451 })
452
453 #define NFS_JUKEBOX_RETRY_TIME (5 * HZ)
454
455 #ifdef CONFIG_NFS_V4
456
457 /*
458  * In a seqid-mutating op, this macro controls which error return
459  * values trigger incrementation of the seqid.
460  *
461  * from rfc 3010:
462  * The client MUST monotonically increment the sequence number for the
463  * CLOSE, LOCK, LOCKU, OPEN, OPEN_CONFIRM, and OPEN_DOWNGRADE
464  * operations.  This is true even in the event that the previous
465  * operation that used the sequence number received an error.  The only
466  * exception to this rule is if the previous operation received one of
467  * the following errors: NFSERR_STALE_CLIENTID, NFSERR_STALE_STATEID,
468  * NFSERR_BAD_STATEID, NFSERR_BAD_SEQID, NFSERR_BADXDR,
469  * NFSERR_RESOURCE, NFSERR_NOFILEHANDLE.
470  *
471  */
472 #define seqid_mutating_err(err)       \
473 (((err) != NFSERR_STALE_CLIENTID) &&  \
474  ((err) != NFSERR_STALE_STATEID)  &&  \
475  ((err) != NFSERR_BAD_STATEID)    &&  \
476  ((err) != NFSERR_BAD_SEQID)      &&  \
477  ((err) != NFSERR_BAD_XDR)        &&  \
478  ((err) != NFSERR_RESOURCE)       &&  \
479  ((err) != NFSERR_NOFILEHANDLE))
480
481 enum nfs4_client_state {
482         NFS4CLNT_OK  = 0,
483         NFS4CLNT_NEW,
484 };
485
486 /*
487  * The nfs4_client identifies our client state to the server.
488  */
489 struct nfs4_client {
490         struct list_head        cl_servers;     /* Global list of servers */
491         struct in_addr          cl_addr;        /* Server identifier */
492         u64                     cl_clientid;    /* constant */
493         nfs4_verifier           cl_confirm;
494         enum nfs4_client_state  cl_state;
495
496         u32                     cl_lockowner_id;
497
498         /*
499          * The following rwsem ensures exclusive access to the server
500          * while we recover the state following a lease expiration.
501          */
502         struct rw_semaphore     cl_sem;
503
504         struct list_head        cl_state_owners;
505         struct list_head        cl_unused;
506         int                     cl_nunused;
507         spinlock_t              cl_lock;
508         atomic_t                cl_count;
509 };
510
511 /*
512  * NFS4 state_owners and lock_owners are simply labels for ordered
513  * sequences of RPC calls. Their sole purpose is to provide once-only
514  * semantics by allowing the server to identify replayed requests.
515  *
516  * The ->so_sema is held during all state_owner seqid-mutating operations:
517  * OPEN, OPEN_DOWNGRADE, and CLOSE. Its purpose is to properly serialize
518  * so_seqid.
519  */
520 struct nfs4_state_owner {
521         struct list_head     so_list;    /* per-clientid list of state_owners */
522         struct nfs4_client   *so_client;
523         u32                  so_id;      /* 32-bit identifier, unique */
524         struct semaphore     so_sema;
525         u32                  so_seqid;   /* protected by so_sema */
526         unsigned int         so_flags;   /* protected by so_sema */
527         atomic_t             so_count;
528
529         struct rpc_cred      *so_cred;   /* Associated cred */
530         struct list_head     so_states;
531 };
532
533 /*
534  * struct nfs4_state maintains the client-side state for a given
535  * (state_owner,inode) tuple.
536  *
537  * In order to know when to OPEN_DOWNGRADE or CLOSE the state on the server,
538  * we need to know how many files are open for reading or writing on a
539  * given inode. This information too is stored here.
540  */
541 struct nfs4_state {
542         struct list_head open_states;   /* List of states for the same state_owner */
543         struct list_head inode_states;  /* List of states for the same inode */
544
545         struct nfs4_state_owner *owner; /* Pointer to the open owner */
546         struct inode *inode;            /* Pointer to the inode */
547         pid_t pid;                      /* Thread that called OPEN */
548
549         nfs4_stateid stateid;
550
551         int state;                      /* State on the server (R,W, or RW) */
552         atomic_t count;
553 };
554
555
556 /* nfs4proc.c */
557 extern int nfs4_proc_renew(struct nfs_server *server);
558 extern int nfs4_do_close(struct inode *, struct nfs4_state *);
559
560 /* nfs4renewd.c */
561 extern int nfs4_init_renewd(struct nfs_server *server);
562
563 /* nfs4state.c */
564 extern struct nfs4_client *nfs4_get_client(struct in_addr *);
565 extern void nfs4_put_client(struct nfs4_client *clp);
566 extern struct nfs4_state_owner * nfs4_get_state_owner(struct nfs_server *, struct rpc_cred *);
567 extern void nfs4_put_state_owner(struct nfs4_state_owner *);
568 extern struct nfs4_state * nfs4_get_open_state(struct inode *, struct nfs4_state_owner *);
569 extern void nfs4_put_open_state(struct nfs4_state *);
570 extern void nfs4_increment_seqid(u32 status, struct nfs4_state_owner *sp);
571
572
573
574
575
576
577 struct nfs4_mount_data;
578 static inline int
579 create_nfsv4_state(struct nfs_server *server, struct nfs4_mount_data *data)
580 {
581         server->nfs4_state = NULL;
582         return 0;
583 }
584
585 static inline void
586 destroy_nfsv4_state(struct nfs_server *server)
587 {
588         if (server->mnt_path) {
589                 kfree(server->mnt_path);
590                 server->mnt_path = NULL;
591         }
592         if (server->nfs4_state) {
593                 nfs4_put_client(server->nfs4_state);
594                 server->nfs4_state = NULL;
595         }
596 }
597 #else
598 #define create_nfsv4_state(server, data)  0
599 #define destroy_nfsv4_state(server)       do { } while (0)
600 #define nfs4_put_state_owner(inode, owner) do { } while (0)
601 #define nfs4_put_open_state(state) do { } while (0)
602 #endif
603
604 #endif /* __KERNEL__ */
605
606 /*
607  * NFS debug flags
608  */
609 #define NFSDBG_VFS              0x0001
610 #define NFSDBG_DIRCACHE         0x0002
611 #define NFSDBG_LOOKUPCACHE      0x0004
612 #define NFSDBG_PAGECACHE        0x0008
613 #define NFSDBG_PROC             0x0010
614 #define NFSDBG_XDR              0x0020
615 #define NFSDBG_FILE             0x0040
616 #define NFSDBG_ROOT             0x0080
617 #define NFSDBG_ALL              0xFFFF
618
619 #ifdef __KERNEL__
620 # undef ifdebug
621 # ifdef NFS_DEBUG
622 #  define ifdebug(fac)          if (nfs_debug & NFSDBG_##fac)
623 # else
624 #  define ifdebug(fac)          if (0)
625 # endif
626 #endif /* __KERNEL */
627
628 #endif