6826301be48be9b9475de5a4e9cdea0d7c07812a
[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/wait.h>
17 #include <linux/uio.h>
18
19 #include <linux/nfs_fs_sb.h>
20
21 #include <linux/sunrpc/debug.h>
22 #include <linux/sunrpc/auth.h>
23 #include <linux/sunrpc/clnt.h>
24
25 #include <linux/nfs.h>
26 #include <linux/nfs2.h>
27 #include <linux/nfs3.h>
28 #include <linux/nfs4.h>
29 #include <linux/nfs_page.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 nfs4_shareowner   *ro_owner;
161         struct nfs4_shareowner   *wo_owner;
162         struct nfs4_shareowner   *rw_owner;
163 #endif /* CONFIG_NFS_V4*/
164
165         struct inode            vfs_inode;
166 };
167
168 /*
169  * Legal inode flag values
170  */
171 #define NFS_INO_STALE           0x0001          /* possible stale inode */
172 #define NFS_INO_ADVISE_RDPLUS   0x0002          /* advise readdirplus */
173 #define NFS_INO_REVALIDATING    0x0004          /* revalidating attrs */
174 #define NFS_INO_FLUSH           0x0008          /* inode is due for flushing */
175
176 static inline struct nfs_inode *NFS_I(struct inode *inode)
177 {
178         return container_of(inode, struct nfs_inode, vfs_inode);
179 }
180 #define NFS_SB(s)               ((struct nfs_server *)(s->s_fs_info))
181
182 #define NFS_FH(inode)                   (&NFS_I(inode)->fh)
183 #define NFS_SERVER(inode)               (NFS_SB(inode->i_sb))
184 #define NFS_CLIENT(inode)               (NFS_SERVER(inode)->client)
185 #define NFS_PROTO(inode)                (NFS_SERVER(inode)->rpc_ops)
186 #define NFS_ADDR(inode)                 (RPC_PEERADDR(NFS_CLIENT(inode)))
187 #define NFS_COOKIEVERF(inode)           (NFS_I(inode)->cookieverf)
188 #define NFS_READTIME(inode)             (NFS_I(inode)->read_cache_jiffies)
189 #define NFS_MTIME_UPDATE(inode)         (NFS_I(inode)->cache_mtime_jiffies)
190 #define NFS_CACHE_CTIME(inode)          (NFS_I(inode)->read_cache_ctime)
191 #define NFS_CACHE_MTIME(inode)          (NFS_I(inode)->read_cache_mtime)
192 #define NFS_CACHE_ISIZE(inode)          (NFS_I(inode)->read_cache_isize)
193 #define NFS_CHANGE_ATTR(inode)          (NFS_I(inode)->change_attr)
194 #define NFS_CACHEINV(inode) \
195 do { \
196         NFS_READTIME(inode) = jiffies - NFS_MAXATTRTIMEO(inode) - 1; \
197 } while (0)
198 #define NFS_ATTRTIMEO(inode)            (NFS_I(inode)->attrtimeo)
199 #define NFS_MINATTRTIMEO(inode) \
200         (S_ISDIR(inode->i_mode)? NFS_SERVER(inode)->acdirmin \
201                                : NFS_SERVER(inode)->acregmin)
202 #define NFS_MAXATTRTIMEO(inode) \
203         (S_ISDIR(inode->i_mode)? NFS_SERVER(inode)->acdirmax \
204                                : NFS_SERVER(inode)->acregmax)
205 #define NFS_ATTRTIMEO_UPDATE(inode)     (NFS_I(inode)->attrtimeo_timestamp)
206
207 #define NFS_FLAGS(inode)                (NFS_I(inode)->flags)
208 #define NFS_REVALIDATING(inode)         (NFS_FLAGS(inode) & NFS_INO_REVALIDATING)
209 #define NFS_STALE(inode)                (NFS_FLAGS(inode) & NFS_INO_STALE)
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 static inline
230 loff_t req_offset(struct nfs_page *req)
231 {
232         return (((loff_t)req->wb_index) << PAGE_CACHE_SHIFT) + req->wb_offset;
233 }
234
235 /*
236  * linux/fs/nfs/inode.c
237  */
238 extern void nfs_zap_caches(struct inode *);
239 extern struct inode *nfs_fhget(struct dentry *, struct nfs_fh *,
240                                 struct nfs_fattr *);
241 extern int __nfs_refresh_inode(struct inode *, struct nfs_fattr *);
242 extern int nfs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
243 extern int nfs_permission(struct inode *, int);
244 extern int nfs_open(struct inode *, struct file *);
245 extern int nfs_release(struct inode *, struct file *);
246 extern int __nfs_revalidate_inode(struct nfs_server *, struct inode *);
247 extern int nfs_setattr(struct dentry *, struct iattr *);
248
249 /*
250  * linux/fs/nfs/file.c
251  */
252 extern struct inode_operations nfs_file_inode_operations;
253 extern struct file_operations nfs_file_operations;
254 extern struct address_space_operations nfs_file_aops;
255
256 static __inline__ struct rpc_cred *
257 nfs_file_cred(struct file *file)
258 {
259         struct rpc_cred *cred = NULL;
260         if (file)
261                 cred = (struct rpc_cred *)file->private_data;
262 #ifdef RPC_DEBUG
263         if (cred && cred->cr_magic != RPCAUTH_CRED_MAGIC)
264                 BUG();
265 #endif
266         return cred;
267 }
268
269 /*
270  * linux/fs/nfs/direct.c
271  */
272 extern int nfs_direct_IO(int, struct file *, const struct iovec *, loff_t,
273                         unsigned long);
274
275 /*
276  * linux/fs/nfs/dir.c
277  */
278 extern struct inode_operations nfs_dir_inode_operations;
279 extern struct file_operations nfs_dir_operations;
280 extern struct dentry_operations nfs_dentry_operations;
281
282 /*
283  * linux/fs/nfs/symlink.c
284  */
285 extern struct inode_operations nfs_symlink_inode_operations;
286
287 /*
288  * linux/fs/nfs/locks.c
289  */
290 extern int nfs_lock(struct file *, int, struct file_lock *);
291
292 /*
293  * linux/fs/nfs/unlink.c
294  */
295 extern int  nfs_async_unlink(struct dentry *);
296 extern void nfs_complete_unlink(struct dentry *);
297
298 /*
299  * linux/fs/nfs/write.c
300  */
301 extern int  nfs_writepage(struct page *page, struct writeback_control *wbc);
302 extern int  nfs_writepages(struct address_space *, struct writeback_control *);
303 extern int  nfs_flush_incompatible(struct file *file, struct page *page);
304 extern int  nfs_updatepage(struct file *, struct page *, unsigned int, unsigned int);
305 extern void nfs_writeback_done(struct rpc_task *task);
306 extern void nfs_writedata_release(struct rpc_task *task);
307
308 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
309 extern void nfs_commit_release(struct rpc_task *task);
310 extern void nfs_commit_done(struct rpc_task *);
311 #endif
312
313 /*
314  * Try to write back everything synchronously (but check the
315  * return value!)
316  */
317 extern int  nfs_sync_file(struct inode *, struct file *, unsigned long, unsigned int, int);
318 extern int  nfs_flush_file(struct inode *, struct file *, unsigned long, unsigned int, int);
319 extern int  nfs_flush_list(struct list_head *, int, int);
320 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
321 extern int  nfs_commit_file(struct inode *, struct file *, unsigned long, unsigned int, int);
322 extern int  nfs_commit_list(struct list_head *, int);
323 #else
324 static inline int
325 nfs_commit_file(struct inode *inode, struct file *file, unsigned long offset,
326                 unsigned int len, int flags)
327 {
328         return 0;
329 }
330 #endif
331
332 static inline int
333 nfs_have_writebacks(struct inode *inode)
334 {
335         return NFS_I(inode)->npages != 0;
336 }
337
338 static inline int
339 nfs_wb_all(struct inode *inode)
340 {
341         int error = nfs_sync_file(inode, 0, 0, 0, FLUSH_WAIT);
342         return (error < 0) ? error : 0;
343 }
344
345 /*
346  * Write back all requests on one page - we do this before reading it.
347  */
348 static inline int
349 nfs_wb_page(struct inode *inode, struct page* page)
350 {
351         int error = nfs_sync_file(inode, 0, page->index, 1,
352                                                 FLUSH_WAIT | FLUSH_STABLE);
353         return (error < 0) ? error : 0;
354 }
355
356 /*
357  * Write back all pending writes for one user.. 
358  */
359 static inline int
360 nfs_wb_file(struct inode *inode, struct file *file)
361 {
362         int error = nfs_sync_file(inode, file, 0, 0, FLUSH_WAIT);
363         return (error < 0) ? error : 0;
364 }
365
366 /* Hack for future NFS swap support */
367 #ifndef IS_SWAPFILE
368 # define IS_SWAPFILE(inode)     (0)
369 #endif
370
371 /*
372  * linux/fs/nfs/read.c
373  */
374 extern int  nfs_readpage(struct file *, struct page *);
375 extern int  nfs_readpages(struct file *, struct address_space *,
376                 struct list_head *, unsigned);
377 extern int  nfs_pagein_list(struct list_head *, int);
378 extern void nfs_readpage_result(struct rpc_task *);
379 extern void nfs_readdata_release(struct rpc_task *);
380
381 /*
382  * linux/fs/mount_clnt.c
383  * (Used only by nfsroot module)
384  */
385 extern int  nfsroot_mount(struct sockaddr_in *, char *, struct nfs_fh *,
386                 int, int);
387
388 /*
389  * inline functions
390  */
391 static inline int
392 nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
393 {
394         if (time_before(jiffies, NFS_READTIME(inode)+NFS_ATTRTIMEO(inode)))
395                 return NFS_STALE(inode) ? -ESTALE : 0;
396         return __nfs_revalidate_inode(server, inode);
397 }
398
399 static inline int
400 nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
401 {
402         if ((fattr->valid & NFS_ATTR_FATTR) == 0)
403                 return 0;
404         return __nfs_refresh_inode(inode,fattr);
405 }
406
407 static inline loff_t
408 nfs_size_to_loff_t(__u64 size)
409 {
410         loff_t maxsz = (((loff_t) ULONG_MAX) << PAGE_CACHE_SHIFT) + PAGE_CACHE_SIZE - 1;
411         if (size > (__u64) maxsz)
412                 return maxsz;
413         return (loff_t) size;
414 }
415
416 static inline ino_t
417 nfs_fileid_to_ino_t(u64 fileid)
418 {
419         ino_t ino = (ino_t) fileid;
420         if (sizeof(ino_t) < sizeof(u64))
421                 ino ^= fileid >> (sizeof(u64)-sizeof(ino_t)) * 8;
422         return ino;
423 }
424
425 /* NFS root */
426
427 extern void * nfs_root_data(void);
428
429 #define nfs_wait_event(clnt, wq, condition)                             \
430 ({                                                                      \
431         int __retval = 0;                                               \
432         if (clnt->cl_intr) {                                            \
433                 sigset_t oldmask;                                       \
434                 rpc_clnt_sigmask(clnt, &oldmask);                       \
435                 __retval = wait_event_interruptible(wq, condition);     \
436                 rpc_clnt_sigunmask(clnt, &oldmask);                     \
437         } else                                                          \
438                 wait_event(wq, condition);                              \
439         __retval;                                                       \
440 })
441
442 #define NFS_JUKEBOX_RETRY_TIME (5 * HZ)
443
444 #ifdef CONFIG_NFS_V4
445
446 /*
447  * In a seqid-mutating op, this macro controls which error return
448  * values trigger incrementation of the seqid.
449  *
450  * from rfc 3010:
451  * The client MUST monotonically increment the sequence number for the
452  * CLOSE, LOCK, LOCKU, OPEN, OPEN_CONFIRM, and OPEN_DOWNGRADE
453  * operations.  This is true even in the event that the previous
454  * operation that used the sequence number received an error.  The only
455  * exception to this rule is if the previous operation received one of
456  * the following errors: NFSERR_STALE_CLIENTID, NFSERR_STALE_STATEID,
457  * NFSERR_BAD_STATEID, NFSERR_BAD_SEQID, NFSERR_BADXDR,
458  * NFSERR_RESOURCE, NFSERR_NOFILEHANDLE.
459  *
460  */
461 #define seqid_mutating_err(err)       \
462 (((err) != NFSERR_STALE_CLIENTID) &&  \
463  ((err) != NFSERR_STALE_STATEID)  &&  \
464  ((err) != NFSERR_BAD_STATEID)    &&  \
465  ((err) != NFSERR_BAD_SEQID)      &&  \
466  ((err) != NFSERR_BAD_XDR)        &&  \
467  ((err) != NFSERR_RESOURCE)       &&  \
468  ((err) != NFSERR_NOFILEHANDLE))
469
470 struct nfs4_client {
471         u64                     cl_clientid;    /* constant */
472         nfs4_verifier           cl_confirm;     
473
474         u32                     cl_lockowner_id;
475 };
476
477 /*
478 * The ->so_sema is held during all shareowner seqid-mutating operations:
479 * OPEN, OPEN_DOWNGRADE, and CLOSE.
480 * Its purpose is to properly serialize so_seqid, as mandated by
481 * the protocol.
482 */
483 struct nfs4_shareowner {
484         u32                  so_id;      /* 32-bit identifier, unique */
485         struct semaphore     so_sema;
486         u32                  so_seqid;   /* protected by so_sema */
487         nfs4_stateid         so_stateid; /* protected by so_sema */
488         unsigned int         so_flags;   /* protected by so_sema */
489 };
490
491
492 /* nfs4proc.c */
493 extern int nfs4_proc_renew(struct nfs_server *server);
494 extern int nfs4_do_close(struct inode *inode, struct nfs4_shareowner *sp);
495
496 /* nfs4renewd.c */
497 extern int nfs4_init_renewd(struct nfs_server *server);
498
499 /* nfs4state.c */
500 extern struct nfs4_client *nfs4_get_client(void);
501 extern void nfs4_put_client(struct nfs4_client *clp);
502 extern struct nfs4_shareowner * nfs4_get_shareowner(struct inode *inode);
503 void nfs4_put_shareowner(struct inode *inode, struct nfs4_shareowner *sp);
504 extern int nfs4_set_inode_share(struct inode * inode,
505                      struct nfs4_shareowner *sp, unsigned int flags);
506 extern void nfs4_increment_seqid(u32 status, struct nfs4_shareowner *sp);
507 extern int nfs4_test_shareowner(struct inode *inode, unsigned int open_flags);
508 struct nfs4_shareowner * nfs4_get_inode_share(struct inode * inode, unsigned int open_flags);
509
510
511
512
513
514
515 struct nfs4_mount_data;
516 static inline int
517 create_nfsv4_state(struct nfs_server *server, struct nfs4_mount_data *data)
518 {
519         server->nfs4_state = NULL;
520         return 0;
521 }
522
523 static inline void
524 destroy_nfsv4_state(struct nfs_server *server)
525 {
526         if (server->mnt_path) {
527                 kfree(server->mnt_path);
528                 server->mnt_path = NULL;
529         }
530         if (server->nfs4_state) {
531                 nfs4_put_client(server->nfs4_state);
532                 server->nfs4_state = NULL;
533         }
534 }
535 #else
536 #define create_nfsv4_state(server, data)  0
537 #define destroy_nfsv4_state(server)       do { } while (0)
538 #define nfs4_put_shareowner(inode, owner) do { } while (0)
539 #endif
540
541 #endif /* __KERNEL__ */
542
543 /*
544  * NFS debug flags
545  */
546 #define NFSDBG_VFS              0x0001
547 #define NFSDBG_DIRCACHE         0x0002
548 #define NFSDBG_LOOKUPCACHE      0x0004
549 #define NFSDBG_PAGECACHE        0x0008
550 #define NFSDBG_PROC             0x0010
551 #define NFSDBG_XDR              0x0020
552 #define NFSDBG_FILE             0x0040
553 #define NFSDBG_ROOT             0x0080
554 #define NFSDBG_ALL              0xFFFF
555
556 #ifdef __KERNEL__
557 # undef ifdebug
558 # ifdef NFS_DEBUG
559 #  define ifdebug(fac)          if (nfs_debug & NFSDBG_##fac)
560 # else
561 #  define ifdebug(fac)          if (0)
562 # endif
563 #endif /* __KERNEL */
564
565 #endif