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/utsname.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
39 #include <linux/pagemap.h>
40 #include <linux/sunrpc/clnt.h>
41 #include <linux/nfs.h>
42 #include <linux/nfs2.h>
43 #include <linux/nfs_fs.h>
44 #include <linux/nfs_page.h>
45 #include <linux/lockd/bind.h>
46 #include <linux/smp_lock.h>
49 #define NFSDBG_FACILITY NFSDBG_PROC
52 * Bare-bones access to getattr: this is for nfs_read_super.
55 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
56 struct nfs_fsinfo
*info
)
58 struct nfs_fattr
*fattr
= info
->fattr
;
59 struct nfs2_fsstat fsinfo
;
60 struct rpc_message msg
= {
61 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
67 dprintk("%s: call getattr\n", __FUNCTION__
);
68 nfs_fattr_init(fattr
);
69 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
70 dprintk("%s: reply getattr: %d\n", __FUNCTION__
, status
);
73 dprintk("%s: call statfs\n", __FUNCTION__
);
74 msg
.rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
];
75 msg
.rpc_resp
= &fsinfo
;
76 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
77 dprintk("%s: reply statfs: %d\n", __FUNCTION__
, status
);
80 info
->rtmax
= NFS_MAXDATA
;
81 info
->rtpref
= fsinfo
.tsize
;
82 info
->rtmult
= fsinfo
.bsize
;
83 info
->wtmax
= NFS_MAXDATA
;
84 info
->wtpref
= fsinfo
.tsize
;
85 info
->wtmult
= fsinfo
.bsize
;
86 info
->dtpref
= fsinfo
.tsize
;
87 info
->maxfilesize
= 0x7FFFFFFF;
93 * One function for each procedure in the NFS protocol.
96 nfs_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
97 struct nfs_fattr
*fattr
)
99 struct rpc_message msg
= {
100 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
106 dprintk("NFS call getattr\n");
107 nfs_fattr_init(fattr
);
108 status
= rpc_call_sync(server
->client
, &msg
, 0);
109 dprintk("NFS reply getattr: %d\n", status
);
114 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
117 struct inode
*inode
= dentry
->d_inode
;
118 struct nfs_sattrargs arg
= {
122 struct rpc_message msg
= {
123 .rpc_proc
= &nfs_procedures
[NFSPROC_SETATTR
],
129 /* Mask out the non-modebit related stuff from attr->ia_mode */
130 sattr
->ia_mode
&= S_IALLUGO
;
132 dprintk("NFS call setattr\n");
133 nfs_fattr_init(fattr
);
134 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
136 nfs_setattr_update_inode(inode
, sattr
);
137 dprintk("NFS reply setattr: %d\n", status
);
142 nfs_proc_lookup(struct inode
*dir
, struct qstr
*name
,
143 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
145 struct nfs_diropargs arg
= {
150 struct nfs_diropok res
= {
154 struct rpc_message msg
= {
155 .rpc_proc
= &nfs_procedures
[NFSPROC_LOOKUP
],
161 dprintk("NFS call lookup %s\n", name
->name
);
162 nfs_fattr_init(fattr
);
163 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
164 dprintk("NFS reply lookup: %d\n", status
);
168 static int nfs_proc_readlink(struct inode
*inode
, struct page
*page
,
169 unsigned int pgbase
, unsigned int pglen
)
171 struct nfs_readlinkargs args
= {
177 struct rpc_message msg
= {
178 .rpc_proc
= &nfs_procedures
[NFSPROC_READLINK
],
183 dprintk("NFS call readlink\n");
184 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
185 dprintk("NFS reply readlink: %d\n", status
);
189 static int nfs_proc_read(struct nfs_read_data
*rdata
)
191 int flags
= rdata
->flags
;
192 struct inode
* inode
= rdata
->inode
;
193 struct nfs_fattr
* fattr
= rdata
->res
.fattr
;
194 struct rpc_message msg
= {
195 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
196 .rpc_argp
= &rdata
->args
,
197 .rpc_resp
= &rdata
->res
,
198 .rpc_cred
= rdata
->cred
,
202 dprintk("NFS call read %d @ %Ld\n", rdata
->args
.count
,
203 (long long) rdata
->args
.offset
);
204 nfs_fattr_init(fattr
);
205 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
207 nfs_refresh_inode(inode
, fattr
);
208 /* Emulate the eof flag, which isn't normally needed in NFSv2
209 * as it is guaranteed to always return the file attributes
211 if (rdata
->args
.offset
+ rdata
->args
.count
>= fattr
->size
)
214 dprintk("NFS reply read: %d\n", status
);
219 nfs_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
220 int flags
, struct nameidata
*nd
)
222 struct nfs_fh fhandle
;
223 struct nfs_fattr fattr
;
224 struct nfs_createargs arg
= {
226 .name
= dentry
->d_name
.name
,
227 .len
= dentry
->d_name
.len
,
230 struct nfs_diropok res
= {
234 struct rpc_message msg
= {
235 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
241 nfs_fattr_init(&fattr
);
242 dprintk("NFS call create %s\n", dentry
->d_name
.name
);
243 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
245 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
246 dprintk("NFS reply create: %d\n", status
);
251 * In NFSv2, mknod is grafted onto the create call.
254 nfs_proc_mknod(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
257 struct nfs_fh fhandle
;
258 struct nfs_fattr fattr
;
259 struct nfs_createargs arg
= {
261 .name
= dentry
->d_name
.name
,
262 .len
= dentry
->d_name
.len
,
265 struct nfs_diropok res
= {
269 struct rpc_message msg
= {
270 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
276 dprintk("NFS call mknod %s\n", dentry
->d_name
.name
);
278 mode
= sattr
->ia_mode
;
279 if (S_ISFIFO(mode
)) {
280 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
281 sattr
->ia_valid
&= ~ATTR_SIZE
;
282 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
283 sattr
->ia_valid
|= ATTR_SIZE
;
284 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
287 nfs_fattr_init(&fattr
);
288 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
289 nfs_mark_for_revalidate(dir
);
291 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
292 sattr
->ia_mode
= mode
;
293 nfs_fattr_init(&fattr
);
294 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
297 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
298 dprintk("NFS reply mknod: %d\n", status
);
303 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
305 struct nfs_diropargs arg
= {
310 struct rpc_message msg
= {
311 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
316 dprintk("NFS call remove %s\n", name
->name
);
317 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
318 nfs_mark_for_revalidate(dir
);
320 dprintk("NFS reply remove: %d\n", status
);
325 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
, struct qstr
*name
)
327 struct nfs_diropargs
*arg
;
329 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
332 arg
->fh
= NFS_FH(dir
->d_inode
);
333 arg
->name
= name
->name
;
334 arg
->len
= name
->len
;
335 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
341 nfs_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
343 struct rpc_message
*msg
= &task
->tk_msg
;
346 nfs_mark_for_revalidate(dir
->d_inode
);
347 kfree(msg
->rpc_argp
);
353 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
354 struct inode
*new_dir
, struct qstr
*new_name
)
356 struct nfs_renameargs arg
= {
357 .fromfh
= NFS_FH(old_dir
),
358 .fromname
= old_name
->name
,
359 .fromlen
= old_name
->len
,
360 .tofh
= NFS_FH(new_dir
),
361 .toname
= new_name
->name
,
362 .tolen
= new_name
->len
364 struct rpc_message msg
= {
365 .rpc_proc
= &nfs_procedures
[NFSPROC_RENAME
],
370 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
371 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
372 nfs_mark_for_revalidate(old_dir
);
373 nfs_mark_for_revalidate(new_dir
);
374 dprintk("NFS reply rename: %d\n", status
);
379 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
381 struct nfs_linkargs arg
= {
382 .fromfh
= NFS_FH(inode
),
384 .toname
= name
->name
,
387 struct rpc_message msg
= {
388 .rpc_proc
= &nfs_procedures
[NFSPROC_LINK
],
393 dprintk("NFS call link %s\n", name
->name
);
394 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
395 nfs_mark_for_revalidate(inode
);
396 nfs_mark_for_revalidate(dir
);
397 dprintk("NFS reply link: %d\n", status
);
402 nfs_proc_symlink(struct inode
*dir
, struct dentry
*dentry
, struct page
*page
,
403 unsigned int len
, struct iattr
*sattr
)
405 struct nfs_fh fhandle
;
406 struct nfs_fattr fattr
;
407 struct nfs_symlinkargs arg
= {
408 .fromfh
= NFS_FH(dir
),
409 .fromname
= dentry
->d_name
.name
,
410 .fromlen
= dentry
->d_name
.len
,
415 struct rpc_message msg
= {
416 .rpc_proc
= &nfs_procedures
[NFSPROC_SYMLINK
],
421 if (len
> NFS2_MAXPATHLEN
)
422 return -ENAMETOOLONG
;
424 dprintk("NFS call symlink %s\n", dentry
->d_name
.name
);
426 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
427 nfs_mark_for_revalidate(dir
);
430 * V2 SYMLINK requests don't return any attributes. Setting the
431 * filehandle size to zero indicates to nfs_instantiate that it
432 * should fill in the data with a LOOKUP call on the wire.
435 nfs_fattr_init(&fattr
);
437 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
440 dprintk("NFS reply symlink: %d\n", status
);
445 nfs_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
)
447 struct nfs_fh fhandle
;
448 struct nfs_fattr fattr
;
449 struct nfs_createargs arg
= {
451 .name
= dentry
->d_name
.name
,
452 .len
= dentry
->d_name
.len
,
455 struct nfs_diropok res
= {
459 struct rpc_message msg
= {
460 .rpc_proc
= &nfs_procedures
[NFSPROC_MKDIR
],
466 dprintk("NFS call mkdir %s\n", dentry
->d_name
.name
);
467 nfs_fattr_init(&fattr
);
468 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
469 nfs_mark_for_revalidate(dir
);
471 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
472 dprintk("NFS reply mkdir: %d\n", status
);
477 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
479 struct nfs_diropargs arg
= {
484 struct rpc_message msg
= {
485 .rpc_proc
= &nfs_procedures
[NFSPROC_RMDIR
],
490 dprintk("NFS call rmdir %s\n", name
->name
);
491 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
492 nfs_mark_for_revalidate(dir
);
493 dprintk("NFS reply rmdir: %d\n", status
);
498 * The READDIR implementation is somewhat hackish - we pass a temporary
499 * buffer to the encode function, which installs it in the receive
500 * the receive iovec. The decode function just parses the reply to make
501 * sure it is syntactically correct; the entries itself are decoded
502 * from nfs_readdir by calling the decode_entry function directly.
505 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
506 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
508 struct inode
*dir
= dentry
->d_inode
;
509 struct nfs_readdirargs arg
= {
515 struct rpc_message msg
= {
516 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
522 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
523 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
525 dprintk("NFS reply readdir: %d\n", status
);
530 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
531 struct nfs_fsstat
*stat
)
533 struct nfs2_fsstat fsinfo
;
534 struct rpc_message msg
= {
535 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
541 dprintk("NFS call statfs\n");
542 nfs_fattr_init(stat
->fattr
);
543 status
= rpc_call_sync(server
->client
, &msg
, 0);
544 dprintk("NFS reply statfs: %d\n", status
);
547 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
548 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
549 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
558 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
559 struct nfs_fsinfo
*info
)
561 struct nfs2_fsstat fsinfo
;
562 struct rpc_message msg
= {
563 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
569 dprintk("NFS call fsinfo\n");
570 nfs_fattr_init(info
->fattr
);
571 status
= rpc_call_sync(server
->client
, &msg
, 0);
572 dprintk("NFS reply fsinfo: %d\n", status
);
575 info
->rtmax
= NFS_MAXDATA
;
576 info
->rtpref
= fsinfo
.tsize
;
577 info
->rtmult
= fsinfo
.bsize
;
578 info
->wtmax
= NFS_MAXDATA
;
579 info
->wtpref
= fsinfo
.tsize
;
580 info
->wtmult
= fsinfo
.bsize
;
581 info
->dtpref
= fsinfo
.tsize
;
582 info
->maxfilesize
= 0x7FFFFFFF;
583 info
->lease_time
= 0;
589 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
590 struct nfs_pathconf
*info
)
593 info
->max_namelen
= NFS2_MAXNAMLEN
;
597 static int nfs_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
599 if (task
->tk_status
>= 0) {
600 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
601 /* Emulate the eof flag, which isn't normally needed in NFSv2
602 * as it is guaranteed to always return the file attributes
604 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
610 static void nfs_proc_read_setup(struct nfs_read_data
*data
)
612 struct rpc_message msg
= {
613 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
614 .rpc_argp
= &data
->args
,
615 .rpc_resp
= &data
->res
,
616 .rpc_cred
= data
->cred
,
619 rpc_call_setup(&data
->task
, &msg
, 0);
622 static int nfs_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
624 if (task
->tk_status
>= 0)
625 nfs_post_op_update_inode(data
->inode
, data
->res
.fattr
);
629 static void nfs_proc_write_setup(struct nfs_write_data
*data
, int how
)
631 struct rpc_message msg
= {
632 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
633 .rpc_argp
= &data
->args
,
634 .rpc_resp
= &data
->res
,
635 .rpc_cred
= data
->cred
,
638 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
639 data
->args
.stable
= NFS_FILE_SYNC
;
641 /* Finalize the task. */
642 rpc_call_setup(&data
->task
, &msg
, 0);
646 nfs_proc_commit_setup(struct nfs_write_data
*data
, int how
)
652 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
654 return nlmclnt_proc(filp
->f_path
.dentry
->d_inode
, cmd
, fl
);
658 const struct nfs_rpc_ops nfs_v2_clientops
= {
659 .version
= 2, /* protocol version */
660 .dentry_ops
= &nfs_dentry_operations
,
661 .dir_inode_ops
= &nfs_dir_inode_operations
,
662 .file_inode_ops
= &nfs_file_inode_operations
,
663 .getroot
= nfs_proc_get_root
,
664 .getattr
= nfs_proc_getattr
,
665 .setattr
= nfs_proc_setattr
,
666 .lookup
= nfs_proc_lookup
,
667 .access
= NULL
, /* access */
668 .readlink
= nfs_proc_readlink
,
669 .read
= nfs_proc_read
,
670 .create
= nfs_proc_create
,
671 .remove
= nfs_proc_remove
,
672 .unlink_setup
= nfs_proc_unlink_setup
,
673 .unlink_done
= nfs_proc_unlink_done
,
674 .rename
= nfs_proc_rename
,
675 .link
= nfs_proc_link
,
676 .symlink
= nfs_proc_symlink
,
677 .mkdir
= nfs_proc_mkdir
,
678 .rmdir
= nfs_proc_rmdir
,
679 .readdir
= nfs_proc_readdir
,
680 .mknod
= nfs_proc_mknod
,
681 .statfs
= nfs_proc_statfs
,
682 .fsinfo
= nfs_proc_fsinfo
,
683 .pathconf
= nfs_proc_pathconf
,
684 .decode_dirent
= nfs_decode_dirent
,
685 .read_setup
= nfs_proc_read_setup
,
686 .read_done
= nfs_read_done
,
687 .write_setup
= nfs_proc_write_setup
,
688 .write_done
= nfs_write_done
,
689 .commit_setup
= nfs_proc_commit_setup
,
690 .file_open
= nfs_open
,
691 .file_release
= nfs_release
,
692 .lock
= nfs_proc_lock
,