4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 * OS-independent nfs remote procedure call functions
8 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
9 * so at last we can have decent(ish) throughput off a
12 * Coding optimized and cleaned up by Florian La Roche.
13 * Note: Error returns are optimized for NFS_OK, which isn't translated via
14 * nfs_stat_to_errno(), but happens to be already the right return code.
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
19 * Feel free to fix it and mail me the diffs if it worries you.
21 * Completely rewritten to support the new RPC call interface;
22 * rewrote and moved the entire XDR stuff to xdr.c
23 * --Olaf Kirch June 1996
25 * The code below initializes all auto variables explicitly, otherwise
26 * it will fail to work as a module (gcc generates a memset call for an
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
35 #include <linux/errno.h>
36 #include <linux/string.h>
38 #include <linux/pagemap.h>
39 #include <linux/sunrpc/clnt.h>
40 #include <linux/nfs.h>
41 #include <linux/nfs2.h>
42 #include <linux/nfs_fs.h>
43 #include <linux/nfs_page.h>
44 #include <linux/lockd/bind.h>
47 #define NFSDBG_FACILITY NFSDBG_PROC
50 * Bare-bones access to getattr: this is for nfs_read_super.
53 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
54 struct nfs_fsinfo
*info
)
56 struct nfs_fattr
*fattr
= info
->fattr
;
57 struct nfs2_fsstat fsinfo
;
58 struct rpc_message msg
= {
59 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
65 dprintk("%s: call getattr\n", __func__
);
66 nfs_fattr_init(fattr
);
67 status
= rpc_call_sync(server
->client
, &msg
, 0);
68 /* Retry with default authentication if different */
69 if (status
&& server
->nfs_client
->cl_rpcclient
!= server
->client
)
70 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
71 dprintk("%s: reply getattr: %d\n", __func__
, status
);
74 dprintk("%s: call statfs\n", __func__
);
75 msg
.rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
];
76 msg
.rpc_resp
= &fsinfo
;
77 status
= rpc_call_sync(server
->client
, &msg
, 0);
78 /* Retry with default authentication if different */
79 if (status
&& server
->nfs_client
->cl_rpcclient
!= server
->client
)
80 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
81 dprintk("%s: reply statfs: %d\n", __func__
, status
);
84 info
->rtmax
= NFS_MAXDATA
;
85 info
->rtpref
= fsinfo
.tsize
;
86 info
->rtmult
= fsinfo
.bsize
;
87 info
->wtmax
= NFS_MAXDATA
;
88 info
->wtpref
= fsinfo
.tsize
;
89 info
->wtmult
= fsinfo
.bsize
;
90 info
->dtpref
= fsinfo
.tsize
;
91 info
->maxfilesize
= 0x7FFFFFFF;
97 * One function for each procedure in the NFS protocol.
100 nfs_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
101 struct nfs_fattr
*fattr
)
103 struct rpc_message msg
= {
104 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
110 dprintk("NFS call getattr\n");
111 nfs_fattr_init(fattr
);
112 status
= rpc_call_sync(server
->client
, &msg
, 0);
113 dprintk("NFS reply getattr: %d\n", status
);
118 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
121 struct inode
*inode
= dentry
->d_inode
;
122 struct nfs_sattrargs arg
= {
126 struct rpc_message msg
= {
127 .rpc_proc
= &nfs_procedures
[NFSPROC_SETATTR
],
133 /* Mask out the non-modebit related stuff from attr->ia_mode */
134 sattr
->ia_mode
&= S_IALLUGO
;
136 dprintk("NFS call setattr\n");
137 if (sattr
->ia_valid
& ATTR_FILE
)
138 msg
.rpc_cred
= nfs_file_cred(sattr
->ia_file
);
139 nfs_fattr_init(fattr
);
140 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
142 nfs_setattr_update_inode(inode
, sattr
);
143 dprintk("NFS reply setattr: %d\n", status
);
148 nfs_proc_lookup(struct inode
*dir
, struct qstr
*name
,
149 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
151 struct nfs_diropargs arg
= {
156 struct nfs_diropok res
= {
160 struct rpc_message msg
= {
161 .rpc_proc
= &nfs_procedures
[NFSPROC_LOOKUP
],
167 dprintk("NFS call lookup %s\n", name
->name
);
168 nfs_fattr_init(fattr
);
169 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
170 dprintk("NFS reply lookup: %d\n", status
);
174 static int nfs_proc_readlink(struct inode
*inode
, struct page
*page
,
175 unsigned int pgbase
, unsigned int pglen
)
177 struct nfs_readlinkargs args
= {
183 struct rpc_message msg
= {
184 .rpc_proc
= &nfs_procedures
[NFSPROC_READLINK
],
189 dprintk("NFS call readlink\n");
190 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
191 dprintk("NFS reply readlink: %d\n", status
);
196 nfs_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
197 int flags
, struct nameidata
*nd
)
199 struct nfs_fh fhandle
;
200 struct nfs_fattr fattr
;
201 struct nfs_createargs arg
= {
203 .name
= dentry
->d_name
.name
,
204 .len
= dentry
->d_name
.len
,
207 struct nfs_diropok res
= {
211 struct rpc_message msg
= {
212 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
218 nfs_fattr_init(&fattr
);
219 dprintk("NFS call create %s\n", dentry
->d_name
.name
);
220 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
221 nfs_mark_for_revalidate(dir
);
223 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
224 dprintk("NFS reply create: %d\n", status
);
229 * In NFSv2, mknod is grafted onto the create call.
232 nfs_proc_mknod(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
235 struct nfs_fh fhandle
;
236 struct nfs_fattr fattr
;
237 struct nfs_createargs arg
= {
239 .name
= dentry
->d_name
.name
,
240 .len
= dentry
->d_name
.len
,
243 struct nfs_diropok res
= {
247 struct rpc_message msg
= {
248 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
254 dprintk("NFS call mknod %s\n", dentry
->d_name
.name
);
256 mode
= sattr
->ia_mode
;
257 if (S_ISFIFO(mode
)) {
258 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
259 sattr
->ia_valid
&= ~ATTR_SIZE
;
260 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
261 sattr
->ia_valid
|= ATTR_SIZE
;
262 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
265 nfs_fattr_init(&fattr
);
266 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
267 nfs_mark_for_revalidate(dir
);
269 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
270 sattr
->ia_mode
= mode
;
271 nfs_fattr_init(&fattr
);
272 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
275 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
276 dprintk("NFS reply mknod: %d\n", status
);
281 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
283 struct nfs_removeargs arg
= {
285 .name
.len
= name
->len
,
286 .name
.name
= name
->name
,
288 struct rpc_message msg
= {
289 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
294 dprintk("NFS call remove %s\n", name
->name
);
295 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
296 nfs_mark_for_revalidate(dir
);
298 dprintk("NFS reply remove: %d\n", status
);
303 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
305 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
308 static int nfs_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
310 nfs_mark_for_revalidate(dir
);
315 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
316 struct inode
*new_dir
, struct qstr
*new_name
)
318 struct nfs_renameargs arg
= {
319 .fromfh
= NFS_FH(old_dir
),
320 .fromname
= old_name
->name
,
321 .fromlen
= old_name
->len
,
322 .tofh
= NFS_FH(new_dir
),
323 .toname
= new_name
->name
,
324 .tolen
= new_name
->len
326 struct rpc_message msg
= {
327 .rpc_proc
= &nfs_procedures
[NFSPROC_RENAME
],
332 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
333 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
334 nfs_mark_for_revalidate(old_dir
);
335 nfs_mark_for_revalidate(new_dir
);
336 dprintk("NFS reply rename: %d\n", status
);
341 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
343 struct nfs_linkargs arg
= {
344 .fromfh
= NFS_FH(inode
),
346 .toname
= name
->name
,
349 struct rpc_message msg
= {
350 .rpc_proc
= &nfs_procedures
[NFSPROC_LINK
],
355 dprintk("NFS call link %s\n", name
->name
);
356 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
357 nfs_mark_for_revalidate(inode
);
358 nfs_mark_for_revalidate(dir
);
359 dprintk("NFS reply link: %d\n", status
);
364 nfs_proc_symlink(struct inode
*dir
, struct dentry
*dentry
, struct page
*page
,
365 unsigned int len
, struct iattr
*sattr
)
367 struct nfs_fh fhandle
;
368 struct nfs_fattr fattr
;
369 struct nfs_symlinkargs arg
= {
370 .fromfh
= NFS_FH(dir
),
371 .fromname
= dentry
->d_name
.name
,
372 .fromlen
= dentry
->d_name
.len
,
377 struct rpc_message msg
= {
378 .rpc_proc
= &nfs_procedures
[NFSPROC_SYMLINK
],
383 if (len
> NFS2_MAXPATHLEN
)
384 return -ENAMETOOLONG
;
386 dprintk("NFS call symlink %s\n", dentry
->d_name
.name
);
388 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
389 nfs_mark_for_revalidate(dir
);
392 * V2 SYMLINK requests don't return any attributes. Setting the
393 * filehandle size to zero indicates to nfs_instantiate that it
394 * should fill in the data with a LOOKUP call on the wire.
397 nfs_fattr_init(&fattr
);
399 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
402 dprintk("NFS reply symlink: %d\n", status
);
407 nfs_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
)
409 struct nfs_fh fhandle
;
410 struct nfs_fattr fattr
;
411 struct nfs_createargs arg
= {
413 .name
= dentry
->d_name
.name
,
414 .len
= dentry
->d_name
.len
,
417 struct nfs_diropok res
= {
421 struct rpc_message msg
= {
422 .rpc_proc
= &nfs_procedures
[NFSPROC_MKDIR
],
428 dprintk("NFS call mkdir %s\n", dentry
->d_name
.name
);
429 nfs_fattr_init(&fattr
);
430 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
431 nfs_mark_for_revalidate(dir
);
433 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
434 dprintk("NFS reply mkdir: %d\n", status
);
439 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
441 struct nfs_diropargs arg
= {
446 struct rpc_message msg
= {
447 .rpc_proc
= &nfs_procedures
[NFSPROC_RMDIR
],
452 dprintk("NFS call rmdir %s\n", name
->name
);
453 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
454 nfs_mark_for_revalidate(dir
);
455 dprintk("NFS reply rmdir: %d\n", status
);
460 * The READDIR implementation is somewhat hackish - we pass a temporary
461 * buffer to the encode function, which installs it in the receive
462 * the receive iovec. The decode function just parses the reply to make
463 * sure it is syntactically correct; the entries itself are decoded
464 * from nfs_readdir by calling the decode_entry function directly.
467 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
468 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
470 struct inode
*dir
= dentry
->d_inode
;
471 struct nfs_readdirargs arg
= {
477 struct rpc_message msg
= {
478 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
484 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
485 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
487 nfs_invalidate_atime(dir
);
489 dprintk("NFS reply readdir: %d\n", status
);
494 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
495 struct nfs_fsstat
*stat
)
497 struct nfs2_fsstat fsinfo
;
498 struct rpc_message msg
= {
499 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
505 dprintk("NFS call statfs\n");
506 nfs_fattr_init(stat
->fattr
);
507 status
= rpc_call_sync(server
->client
, &msg
, 0);
508 dprintk("NFS reply statfs: %d\n", status
);
511 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
512 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
513 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
522 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
523 struct nfs_fsinfo
*info
)
525 struct nfs2_fsstat fsinfo
;
526 struct rpc_message msg
= {
527 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
533 dprintk("NFS call fsinfo\n");
534 nfs_fattr_init(info
->fattr
);
535 status
= rpc_call_sync(server
->client
, &msg
, 0);
536 dprintk("NFS reply fsinfo: %d\n", status
);
539 info
->rtmax
= NFS_MAXDATA
;
540 info
->rtpref
= fsinfo
.tsize
;
541 info
->rtmult
= fsinfo
.bsize
;
542 info
->wtmax
= NFS_MAXDATA
;
543 info
->wtpref
= fsinfo
.tsize
;
544 info
->wtmult
= fsinfo
.bsize
;
545 info
->dtpref
= fsinfo
.tsize
;
546 info
->maxfilesize
= 0x7FFFFFFF;
547 info
->lease_time
= 0;
553 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
554 struct nfs_pathconf
*info
)
557 info
->max_namelen
= NFS2_MAXNAMLEN
;
561 static int nfs_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
563 nfs_invalidate_atime(data
->inode
);
564 if (task
->tk_status
>= 0) {
565 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
566 /* Emulate the eof flag, which isn't normally needed in NFSv2
567 * as it is guaranteed to always return the file attributes
569 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
575 static void nfs_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
577 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_READ
];
580 static int nfs_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
582 if (task
->tk_status
>= 0)
583 nfs_post_op_update_inode_force_wcc(data
->inode
, data
->res
.fattr
);
587 static void nfs_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
589 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
590 data
->args
.stable
= NFS_FILE_SYNC
;
591 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
];
595 nfs_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
601 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
603 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
605 return nlmclnt_proc(NFS_SERVER(inode
)->nlm_host
, cmd
, fl
);
608 /* Helper functions for NFS lock bounds checking */
609 #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
610 static int nfs_lock_check_bounds(const struct file_lock
*fl
)
614 start
= (__s32
)fl
->fl_start
;
615 if ((loff_t
)start
!= fl
->fl_start
)
618 if (fl
->fl_end
!= OFFSET_MAX
) {
619 end
= (__s32
)fl
->fl_end
;
620 if ((loff_t
)end
!= fl
->fl_end
)
623 end
= NFS_LOCK32_OFFSET_MAX
;
625 if (start
< 0 || start
> end
)
632 const struct nfs_rpc_ops nfs_v2_clientops
= {
633 .version
= 2, /* protocol version */
634 .dentry_ops
= &nfs_dentry_operations
,
635 .dir_inode_ops
= &nfs_dir_inode_operations
,
636 .file_inode_ops
= &nfs_file_inode_operations
,
637 .getroot
= nfs_proc_get_root
,
638 .getattr
= nfs_proc_getattr
,
639 .setattr
= nfs_proc_setattr
,
640 .lookup
= nfs_proc_lookup
,
641 .access
= NULL
, /* access */
642 .readlink
= nfs_proc_readlink
,
643 .create
= nfs_proc_create
,
644 .remove
= nfs_proc_remove
,
645 .unlink_setup
= nfs_proc_unlink_setup
,
646 .unlink_done
= nfs_proc_unlink_done
,
647 .rename
= nfs_proc_rename
,
648 .link
= nfs_proc_link
,
649 .symlink
= nfs_proc_symlink
,
650 .mkdir
= nfs_proc_mkdir
,
651 .rmdir
= nfs_proc_rmdir
,
652 .readdir
= nfs_proc_readdir
,
653 .mknod
= nfs_proc_mknod
,
654 .statfs
= nfs_proc_statfs
,
655 .fsinfo
= nfs_proc_fsinfo
,
656 .pathconf
= nfs_proc_pathconf
,
657 .decode_dirent
= nfs_decode_dirent
,
658 .read_setup
= nfs_proc_read_setup
,
659 .read_done
= nfs_read_done
,
660 .write_setup
= nfs_proc_write_setup
,
661 .write_done
= nfs_write_done
,
662 .commit_setup
= nfs_proc_commit_setup
,
663 .lock
= nfs_proc_lock
,
664 .lock_check_bounds
= nfs_lock_check_bounds
,
665 .close_context
= nfs_close_context
,