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
);
218 static int nfs_proc_write(struct nfs_write_data
*wdata
)
220 int flags
= wdata
->flags
;
221 struct inode
* inode
= wdata
->inode
;
222 struct nfs_fattr
* fattr
= wdata
->res
.fattr
;
223 struct rpc_message msg
= {
224 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
225 .rpc_argp
= &wdata
->args
,
226 .rpc_resp
= &wdata
->res
,
227 .rpc_cred
= wdata
->cred
,
231 dprintk("NFS call write %d @ %Ld\n", wdata
->args
.count
,
232 (long long) wdata
->args
.offset
);
233 nfs_fattr_init(fattr
);
234 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
236 nfs_post_op_update_inode(inode
, fattr
);
237 wdata
->res
.count
= wdata
->args
.count
;
238 wdata
->verf
.committed
= NFS_FILE_SYNC
;
240 dprintk("NFS reply write: %d\n", status
);
241 return status
< 0? status
: wdata
->res
.count
;
245 nfs_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
246 int flags
, struct nameidata
*nd
)
248 struct nfs_fh fhandle
;
249 struct nfs_fattr fattr
;
250 struct nfs_createargs arg
= {
252 .name
= dentry
->d_name
.name
,
253 .len
= dentry
->d_name
.len
,
256 struct nfs_diropok res
= {
260 struct rpc_message msg
= {
261 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
267 nfs_fattr_init(&fattr
);
268 dprintk("NFS call create %s\n", dentry
->d_name
.name
);
269 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
271 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
272 dprintk("NFS reply create: %d\n", status
);
277 * In NFSv2, mknod is grafted onto the create call.
280 nfs_proc_mknod(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
283 struct nfs_fh fhandle
;
284 struct nfs_fattr fattr
;
285 struct nfs_createargs arg
= {
287 .name
= dentry
->d_name
.name
,
288 .len
= dentry
->d_name
.len
,
291 struct nfs_diropok res
= {
295 struct rpc_message msg
= {
296 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
302 dprintk("NFS call mknod %s\n", dentry
->d_name
.name
);
304 mode
= sattr
->ia_mode
;
305 if (S_ISFIFO(mode
)) {
306 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
307 sattr
->ia_valid
&= ~ATTR_SIZE
;
308 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
309 sattr
->ia_valid
|= ATTR_SIZE
;
310 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
313 nfs_fattr_init(&fattr
);
314 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
315 nfs_mark_for_revalidate(dir
);
317 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
318 sattr
->ia_mode
= mode
;
319 nfs_fattr_init(&fattr
);
320 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
323 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
324 dprintk("NFS reply mknod: %d\n", status
);
329 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
331 struct nfs_diropargs arg
= {
336 struct rpc_message msg
= {
337 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
342 dprintk("NFS call remove %s\n", name
->name
);
343 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
344 nfs_mark_for_revalidate(dir
);
346 dprintk("NFS reply remove: %d\n", status
);
351 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
, struct qstr
*name
)
353 struct nfs_diropargs
*arg
;
355 arg
= kmalloc(sizeof(*arg
), GFP_KERNEL
);
358 arg
->fh
= NFS_FH(dir
->d_inode
);
359 arg
->name
= name
->name
;
360 arg
->len
= name
->len
;
361 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
367 nfs_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
369 struct rpc_message
*msg
= &task
->tk_msg
;
372 nfs_mark_for_revalidate(dir
->d_inode
);
373 kfree(msg
->rpc_argp
);
379 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
380 struct inode
*new_dir
, struct qstr
*new_name
)
382 struct nfs_renameargs arg
= {
383 .fromfh
= NFS_FH(old_dir
),
384 .fromname
= old_name
->name
,
385 .fromlen
= old_name
->len
,
386 .tofh
= NFS_FH(new_dir
),
387 .toname
= new_name
->name
,
388 .tolen
= new_name
->len
390 struct rpc_message msg
= {
391 .rpc_proc
= &nfs_procedures
[NFSPROC_RENAME
],
396 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
397 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
398 nfs_mark_for_revalidate(old_dir
);
399 nfs_mark_for_revalidate(new_dir
);
400 dprintk("NFS reply rename: %d\n", status
);
405 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
407 struct nfs_linkargs arg
= {
408 .fromfh
= NFS_FH(inode
),
410 .toname
= name
->name
,
413 struct rpc_message msg
= {
414 .rpc_proc
= &nfs_procedures
[NFSPROC_LINK
],
419 dprintk("NFS call link %s\n", name
->name
);
420 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
421 nfs_mark_for_revalidate(inode
);
422 nfs_mark_for_revalidate(dir
);
423 dprintk("NFS reply link: %d\n", status
);
428 nfs_proc_symlink(struct inode
*dir
, struct dentry
*dentry
, struct page
*page
,
429 unsigned int len
, struct iattr
*sattr
)
431 struct nfs_fh fhandle
;
432 struct nfs_fattr fattr
;
433 struct nfs_symlinkargs arg
= {
434 .fromfh
= NFS_FH(dir
),
435 .fromname
= dentry
->d_name
.name
,
436 .fromlen
= dentry
->d_name
.len
,
441 struct rpc_message msg
= {
442 .rpc_proc
= &nfs_procedures
[NFSPROC_SYMLINK
],
447 if (len
> NFS2_MAXPATHLEN
)
448 return -ENAMETOOLONG
;
450 dprintk("NFS call symlink %s\n", dentry
->d_name
.name
);
452 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
453 nfs_mark_for_revalidate(dir
);
456 * V2 SYMLINK requests don't return any attributes. Setting the
457 * filehandle size to zero indicates to nfs_instantiate that it
458 * should fill in the data with a LOOKUP call on the wire.
461 nfs_fattr_init(&fattr
);
463 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
466 dprintk("NFS reply symlink: %d\n", status
);
471 nfs_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
)
473 struct nfs_fh fhandle
;
474 struct nfs_fattr fattr
;
475 struct nfs_createargs arg
= {
477 .name
= dentry
->d_name
.name
,
478 .len
= dentry
->d_name
.len
,
481 struct nfs_diropok res
= {
485 struct rpc_message msg
= {
486 .rpc_proc
= &nfs_procedures
[NFSPROC_MKDIR
],
492 dprintk("NFS call mkdir %s\n", dentry
->d_name
.name
);
493 nfs_fattr_init(&fattr
);
494 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
495 nfs_mark_for_revalidate(dir
);
497 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
498 dprintk("NFS reply mkdir: %d\n", status
);
503 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
505 struct nfs_diropargs arg
= {
510 struct rpc_message msg
= {
511 .rpc_proc
= &nfs_procedures
[NFSPROC_RMDIR
],
516 dprintk("NFS call rmdir %s\n", name
->name
);
517 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
518 nfs_mark_for_revalidate(dir
);
519 dprintk("NFS reply rmdir: %d\n", status
);
524 * The READDIR implementation is somewhat hackish - we pass a temporary
525 * buffer to the encode function, which installs it in the receive
526 * the receive iovec. The decode function just parses the reply to make
527 * sure it is syntactically correct; the entries itself are decoded
528 * from nfs_readdir by calling the decode_entry function directly.
531 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
532 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
534 struct inode
*dir
= dentry
->d_inode
;
535 struct nfs_readdirargs arg
= {
541 struct rpc_message msg
= {
542 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
550 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
551 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
553 dprintk("NFS reply readdir: %d\n", status
);
559 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
560 struct nfs_fsstat
*stat
)
562 struct nfs2_fsstat fsinfo
;
563 struct rpc_message msg
= {
564 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
570 dprintk("NFS call statfs\n");
571 nfs_fattr_init(stat
->fattr
);
572 status
= rpc_call_sync(server
->client
, &msg
, 0);
573 dprintk("NFS reply statfs: %d\n", status
);
576 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
577 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
578 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
587 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
588 struct nfs_fsinfo
*info
)
590 struct nfs2_fsstat fsinfo
;
591 struct rpc_message msg
= {
592 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
598 dprintk("NFS call fsinfo\n");
599 nfs_fattr_init(info
->fattr
);
600 status
= rpc_call_sync(server
->client
, &msg
, 0);
601 dprintk("NFS reply fsinfo: %d\n", status
);
604 info
->rtmax
= NFS_MAXDATA
;
605 info
->rtpref
= fsinfo
.tsize
;
606 info
->rtmult
= fsinfo
.bsize
;
607 info
->wtmax
= NFS_MAXDATA
;
608 info
->wtpref
= fsinfo
.tsize
;
609 info
->wtmult
= fsinfo
.bsize
;
610 info
->dtpref
= fsinfo
.tsize
;
611 info
->maxfilesize
= 0x7FFFFFFF;
612 info
->lease_time
= 0;
618 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
619 struct nfs_pathconf
*info
)
622 info
->max_namelen
= NFS2_MAXNAMLEN
;
626 static int nfs_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
628 if (task
->tk_status
>= 0) {
629 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
630 /* Emulate the eof flag, which isn't normally needed in NFSv2
631 * as it is guaranteed to always return the file attributes
633 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
639 static void nfs_proc_read_setup(struct nfs_read_data
*data
)
641 struct rpc_message msg
= {
642 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
643 .rpc_argp
= &data
->args
,
644 .rpc_resp
= &data
->res
,
645 .rpc_cred
= data
->cred
,
648 rpc_call_setup(&data
->task
, &msg
, 0);
651 static int nfs_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
653 if (task
->tk_status
>= 0)
654 nfs_post_op_update_inode(data
->inode
, data
->res
.fattr
);
658 static void nfs_proc_write_setup(struct nfs_write_data
*data
, int how
)
660 struct rpc_message msg
= {
661 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
662 .rpc_argp
= &data
->args
,
663 .rpc_resp
= &data
->res
,
664 .rpc_cred
= data
->cred
,
667 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
668 data
->args
.stable
= NFS_FILE_SYNC
;
670 /* Finalize the task. */
671 rpc_call_setup(&data
->task
, &msg
, 0);
675 nfs_proc_commit_setup(struct nfs_write_data
*data
, int how
)
681 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
683 return nlmclnt_proc(filp
->f_dentry
->d_inode
, cmd
, fl
);
687 const struct nfs_rpc_ops nfs_v2_clientops
= {
688 .version
= 2, /* protocol version */
689 .dentry_ops
= &nfs_dentry_operations
,
690 .dir_inode_ops
= &nfs_dir_inode_operations
,
691 .file_inode_ops
= &nfs_file_inode_operations
,
692 .getroot
= nfs_proc_get_root
,
693 .getattr
= nfs_proc_getattr
,
694 .setattr
= nfs_proc_setattr
,
695 .lookup
= nfs_proc_lookup
,
696 .access
= NULL
, /* access */
697 .readlink
= nfs_proc_readlink
,
698 .read
= nfs_proc_read
,
699 .write
= nfs_proc_write
,
700 .commit
= NULL
, /* commit */
701 .create
= nfs_proc_create
,
702 .remove
= nfs_proc_remove
,
703 .unlink_setup
= nfs_proc_unlink_setup
,
704 .unlink_done
= nfs_proc_unlink_done
,
705 .rename
= nfs_proc_rename
,
706 .link
= nfs_proc_link
,
707 .symlink
= nfs_proc_symlink
,
708 .mkdir
= nfs_proc_mkdir
,
709 .rmdir
= nfs_proc_rmdir
,
710 .readdir
= nfs_proc_readdir
,
711 .mknod
= nfs_proc_mknod
,
712 .statfs
= nfs_proc_statfs
,
713 .fsinfo
= nfs_proc_fsinfo
,
714 .pathconf
= nfs_proc_pathconf
,
715 .decode_dirent
= nfs_decode_dirent
,
716 .read_setup
= nfs_proc_read_setup
,
717 .read_done
= nfs_read_done
,
718 .write_setup
= nfs_proc_write_setup
,
719 .write_done
= nfs_write_done
,
720 .commit_setup
= nfs_proc_commit_setup
,
721 .file_open
= nfs_open
,
722 .file_release
= nfs_release
,
723 .lock
= nfs_proc_lock
,