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>
48 #define NFSDBG_FACILITY NFSDBG_PROC
50 extern struct rpc_procinfo nfs_procedures
[];
53 * Bare-bones access to getattr: this is for nfs_read_super.
56 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
57 struct nfs_fsinfo
*info
)
59 struct nfs_fattr
*fattr
= info
->fattr
;
60 struct nfs2_fsstat fsinfo
;
61 struct rpc_message msg
= {
62 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
68 dprintk("%s: call getattr\n", __FUNCTION__
);
69 nfs_fattr_init(fattr
);
70 status
= rpc_call_sync(server
->client_sys
, &msg
, 0);
71 dprintk("%s: reply getattr: %d\n", __FUNCTION__
, status
);
74 dprintk("%s: call statfs\n", __FUNCTION__
);
75 msg
.rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
];
76 msg
.rpc_resp
= &fsinfo
;
77 status
= rpc_call_sync(server
->client_sys
, &msg
, 0);
78 dprintk("%s: reply statfs: %d\n", __FUNCTION__
, status
);
81 info
->rtmax
= NFS_MAXDATA
;
82 info
->rtpref
= fsinfo
.tsize
;
83 info
->rtmult
= fsinfo
.bsize
;
84 info
->wtmax
= NFS_MAXDATA
;
85 info
->wtpref
= fsinfo
.tsize
;
86 info
->wtmult
= fsinfo
.bsize
;
87 info
->dtpref
= fsinfo
.tsize
;
88 info
->maxfilesize
= 0x7FFFFFFF;
94 * One function for each procedure in the NFS protocol.
97 nfs_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
98 struct nfs_fattr
*fattr
)
100 struct rpc_message msg
= {
101 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
107 dprintk("NFS call getattr\n");
108 nfs_fattr_init(fattr
);
109 status
= rpc_call_sync(server
->client
, &msg
, 0);
110 dprintk("NFS reply getattr: %d\n", status
);
115 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
118 struct inode
*inode
= dentry
->d_inode
;
119 struct nfs_sattrargs arg
= {
123 struct rpc_message msg
= {
124 .rpc_proc
= &nfs_procedures
[NFSPROC_SETATTR
],
130 /* Mask out the non-modebit related stuff from attr->ia_mode */
131 sattr
->ia_mode
&= S_IALLUGO
;
133 dprintk("NFS call setattr\n");
134 nfs_fattr_init(fattr
);
135 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
137 nfs_setattr_update_inode(inode
, sattr
);
138 dprintk("NFS reply setattr: %d\n", status
);
143 nfs_proc_lookup(struct inode
*dir
, struct qstr
*name
,
144 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
146 struct nfs_diropargs arg
= {
151 struct nfs_diropok res
= {
155 struct rpc_message msg
= {
156 .rpc_proc
= &nfs_procedures
[NFSPROC_LOOKUP
],
162 dprintk("NFS call lookup %s\n", name
->name
);
163 nfs_fattr_init(fattr
);
164 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
165 dprintk("NFS reply lookup: %d\n", status
);
169 static int nfs_proc_readlink(struct inode
*inode
, struct page
*page
,
170 unsigned int pgbase
, unsigned int pglen
)
172 struct nfs_readlinkargs args
= {
178 struct rpc_message msg
= {
179 .rpc_proc
= &nfs_procedures
[NFSPROC_READLINK
],
184 dprintk("NFS call readlink\n");
185 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
186 dprintk("NFS reply readlink: %d\n", status
);
190 static int nfs_proc_read(struct nfs_read_data
*rdata
)
192 int flags
= rdata
->flags
;
193 struct inode
* inode
= rdata
->inode
;
194 struct nfs_fattr
* fattr
= rdata
->res
.fattr
;
195 struct rpc_message msg
= {
196 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
197 .rpc_argp
= &rdata
->args
,
198 .rpc_resp
= &rdata
->res
,
199 .rpc_cred
= rdata
->cred
,
203 dprintk("NFS call read %d @ %Ld\n", rdata
->args
.count
,
204 (long long) rdata
->args
.offset
);
205 nfs_fattr_init(fattr
);
206 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
208 nfs_refresh_inode(inode
, fattr
);
209 /* Emulate the eof flag, which isn't normally needed in NFSv2
210 * as it is guaranteed to always return the file attributes
212 if (rdata
->args
.offset
+ rdata
->args
.count
>= fattr
->size
)
215 dprintk("NFS reply read: %d\n", status
);
219 static int nfs_proc_write(struct nfs_write_data
*wdata
)
221 int flags
= wdata
->flags
;
222 struct inode
* inode
= wdata
->inode
;
223 struct nfs_fattr
* fattr
= wdata
->res
.fattr
;
224 struct rpc_message msg
= {
225 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
226 .rpc_argp
= &wdata
->args
,
227 .rpc_resp
= &wdata
->res
,
228 .rpc_cred
= wdata
->cred
,
232 dprintk("NFS call write %d @ %Ld\n", wdata
->args
.count
,
233 (long long) wdata
->args
.offset
);
234 nfs_fattr_init(fattr
);
235 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, flags
);
237 nfs_post_op_update_inode(inode
, fattr
);
238 wdata
->res
.count
= wdata
->args
.count
;
239 wdata
->verf
.committed
= NFS_FILE_SYNC
;
241 dprintk("NFS reply write: %d\n", status
);
242 return status
< 0? status
: wdata
->res
.count
;
246 nfs_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
247 int flags
, struct nameidata
*nd
)
249 struct nfs_fh fhandle
;
250 struct nfs_fattr fattr
;
251 struct nfs_createargs arg
= {
253 .name
= dentry
->d_name
.name
,
254 .len
= dentry
->d_name
.len
,
257 struct nfs_diropok res
= {
261 struct rpc_message msg
= {
262 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
268 nfs_fattr_init(&fattr
);
269 dprintk("NFS call create %s\n", dentry
->d_name
.name
);
270 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
272 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
273 dprintk("NFS reply create: %d\n", status
);
278 * In NFSv2, mknod is grafted onto the create call.
281 nfs_proc_mknod(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
284 struct nfs_fh fhandle
;
285 struct nfs_fattr fattr
;
286 struct nfs_createargs arg
= {
288 .name
= dentry
->d_name
.name
,
289 .len
= dentry
->d_name
.len
,
292 struct nfs_diropok res
= {
296 struct rpc_message msg
= {
297 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
303 dprintk("NFS call mknod %s\n", dentry
->d_name
.name
);
305 mode
= sattr
->ia_mode
;
306 if (S_ISFIFO(mode
)) {
307 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
308 sattr
->ia_valid
&= ~ATTR_SIZE
;
309 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
310 sattr
->ia_valid
|= ATTR_SIZE
;
311 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
314 nfs_fattr_init(&fattr
);
315 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
316 nfs_mark_for_revalidate(dir
);
318 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
319 sattr
->ia_mode
= mode
;
320 nfs_fattr_init(&fattr
);
321 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
324 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
325 dprintk("NFS reply mknod: %d\n", status
);
330 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
332 struct nfs_diropargs arg
= {
337 struct rpc_message msg
= {
338 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
343 dprintk("NFS call remove %s\n", name
->name
);
344 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
345 nfs_mark_for_revalidate(dir
);
347 dprintk("NFS reply remove: %d\n", status
);
352 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
, struct qstr
*name
)
354 struct nfs_diropargs
*arg
;
356 arg
= (struct nfs_diropargs
*)kmalloc(sizeof(*arg
), GFP_KERNEL
);
359 arg
->fh
= NFS_FH(dir
->d_inode
);
360 arg
->name
= name
->name
;
361 arg
->len
= name
->len
;
362 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
368 nfs_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
370 struct rpc_message
*msg
= &task
->tk_msg
;
373 nfs_mark_for_revalidate(dir
->d_inode
);
374 kfree(msg
->rpc_argp
);
380 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
381 struct inode
*new_dir
, struct qstr
*new_name
)
383 struct nfs_renameargs arg
= {
384 .fromfh
= NFS_FH(old_dir
),
385 .fromname
= old_name
->name
,
386 .fromlen
= old_name
->len
,
387 .tofh
= NFS_FH(new_dir
),
388 .toname
= new_name
->name
,
389 .tolen
= new_name
->len
391 struct rpc_message msg
= {
392 .rpc_proc
= &nfs_procedures
[NFSPROC_RENAME
],
397 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
398 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
399 nfs_mark_for_revalidate(old_dir
);
400 nfs_mark_for_revalidate(new_dir
);
401 dprintk("NFS reply rename: %d\n", status
);
406 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
408 struct nfs_linkargs arg
= {
409 .fromfh
= NFS_FH(inode
),
411 .toname
= name
->name
,
414 struct rpc_message msg
= {
415 .rpc_proc
= &nfs_procedures
[NFSPROC_LINK
],
420 dprintk("NFS call link %s\n", name
->name
);
421 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
422 nfs_mark_for_revalidate(inode
);
423 nfs_mark_for_revalidate(dir
);
424 dprintk("NFS reply link: %d\n", status
);
429 nfs_proc_symlink(struct inode
*dir
, struct qstr
*name
, struct qstr
*path
,
430 struct iattr
*sattr
, struct nfs_fh
*fhandle
,
431 struct nfs_fattr
*fattr
)
433 struct nfs_symlinkargs arg
= {
434 .fromfh
= NFS_FH(dir
),
435 .fromname
= name
->name
,
436 .fromlen
= name
->len
,
437 .topath
= path
->name
,
441 struct rpc_message msg
= {
442 .rpc_proc
= &nfs_procedures
[NFSPROC_SYMLINK
],
447 if (path
->len
> NFS2_MAXPATHLEN
)
448 return -ENAMETOOLONG
;
449 dprintk("NFS call symlink %s -> %s\n", name
->name
, path
->name
);
450 nfs_fattr_init(fattr
);
452 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
453 nfs_mark_for_revalidate(dir
);
454 dprintk("NFS reply symlink: %d\n", status
);
459 nfs_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
)
461 struct nfs_fh fhandle
;
462 struct nfs_fattr fattr
;
463 struct nfs_createargs arg
= {
465 .name
= dentry
->d_name
.name
,
466 .len
= dentry
->d_name
.len
,
469 struct nfs_diropok res
= {
473 struct rpc_message msg
= {
474 .rpc_proc
= &nfs_procedures
[NFSPROC_MKDIR
],
480 dprintk("NFS call mkdir %s\n", dentry
->d_name
.name
);
481 nfs_fattr_init(&fattr
);
482 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
483 nfs_mark_for_revalidate(dir
);
485 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
486 dprintk("NFS reply mkdir: %d\n", status
);
491 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
493 struct nfs_diropargs arg
= {
498 struct rpc_message msg
= {
499 .rpc_proc
= &nfs_procedures
[NFSPROC_RMDIR
],
504 dprintk("NFS call rmdir %s\n", name
->name
);
505 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
506 nfs_mark_for_revalidate(dir
);
507 dprintk("NFS reply rmdir: %d\n", status
);
512 * The READDIR implementation is somewhat hackish - we pass a temporary
513 * buffer to the encode function, which installs it in the receive
514 * the receive iovec. The decode function just parses the reply to make
515 * sure it is syntactically correct; the entries itself are decoded
516 * from nfs_readdir by calling the decode_entry function directly.
519 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
520 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
522 struct inode
*dir
= dentry
->d_inode
;
523 struct nfs_readdirargs arg
= {
529 struct rpc_message msg
= {
530 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
538 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
539 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
541 dprintk("NFS reply readdir: %d\n", status
);
547 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
548 struct nfs_fsstat
*stat
)
550 struct nfs2_fsstat fsinfo
;
551 struct rpc_message msg
= {
552 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
558 dprintk("NFS call statfs\n");
559 nfs_fattr_init(stat
->fattr
);
560 status
= rpc_call_sync(server
->client
, &msg
, 0);
561 dprintk("NFS reply statfs: %d\n", status
);
564 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
565 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
566 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
575 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
576 struct nfs_fsinfo
*info
)
578 struct nfs2_fsstat fsinfo
;
579 struct rpc_message msg
= {
580 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
586 dprintk("NFS call fsinfo\n");
587 nfs_fattr_init(info
->fattr
);
588 status
= rpc_call_sync(server
->client
, &msg
, 0);
589 dprintk("NFS reply fsinfo: %d\n", status
);
592 info
->rtmax
= NFS_MAXDATA
;
593 info
->rtpref
= fsinfo
.tsize
;
594 info
->rtmult
= fsinfo
.bsize
;
595 info
->wtmax
= NFS_MAXDATA
;
596 info
->wtpref
= fsinfo
.tsize
;
597 info
->wtmult
= fsinfo
.bsize
;
598 info
->dtpref
= fsinfo
.tsize
;
599 info
->maxfilesize
= 0x7FFFFFFF;
600 info
->lease_time
= 0;
606 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
607 struct nfs_pathconf
*info
)
610 info
->max_namelen
= NFS2_MAXNAMLEN
;
614 extern u32
* nfs_decode_dirent(u32
*, struct nfs_entry
*, int);
616 static void nfs_read_done(struct rpc_task
*task
, void *calldata
)
618 struct nfs_read_data
*data
= calldata
;
620 if (task
->tk_status
>= 0) {
621 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
622 /* Emulate the eof flag, which isn't normally needed in NFSv2
623 * as it is guaranteed to always return the file attributes
625 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
628 nfs_readpage_result(task
, calldata
);
631 static const struct rpc_call_ops nfs_read_ops
= {
632 .rpc_call_done
= nfs_read_done
,
633 .rpc_release
= nfs_readdata_release
,
637 nfs_proc_read_setup(struct nfs_read_data
*data
)
639 struct rpc_task
*task
= &data
->task
;
640 struct inode
*inode
= data
->inode
;
642 struct rpc_message msg
= {
643 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
644 .rpc_argp
= &data
->args
,
645 .rpc_resp
= &data
->res
,
646 .rpc_cred
= data
->cred
,
649 /* N.B. Do we need to test? Never called for swapfile inode */
650 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
652 /* Finalize the task. */
653 rpc_init_task(task
, NFS_CLIENT(inode
), flags
, &nfs_read_ops
, data
);
654 rpc_call_setup(task
, &msg
, 0);
657 static int nfs_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
659 if (task
->tk_status
>= 0)
660 nfs_post_op_update_inode(data
->inode
, data
->res
.fattr
);
664 static void nfs_proc_write_setup(struct nfs_write_data
*data
, int how
)
666 struct rpc_message msg
= {
667 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
668 .rpc_argp
= &data
->args
,
669 .rpc_resp
= &data
->res
,
670 .rpc_cred
= data
->cred
,
673 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
674 data
->args
.stable
= NFS_FILE_SYNC
;
676 /* Finalize the task. */
677 rpc_call_setup(&data
->task
, &msg
, 0);
681 nfs_proc_commit_setup(struct nfs_write_data
*data
, int how
)
687 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
689 return nlmclnt_proc(filp
->f_dentry
->d_inode
, cmd
, fl
);
693 struct nfs_rpc_ops nfs_v2_clientops
= {
694 .version
= 2, /* protocol version */
695 .dentry_ops
= &nfs_dentry_operations
,
696 .dir_inode_ops
= &nfs_dir_inode_operations
,
697 .file_inode_ops
= &nfs_file_inode_operations
,
698 .getroot
= nfs_proc_get_root
,
699 .getattr
= nfs_proc_getattr
,
700 .setattr
= nfs_proc_setattr
,
701 .lookup
= nfs_proc_lookup
,
702 .access
= NULL
, /* access */
703 .readlink
= nfs_proc_readlink
,
704 .read
= nfs_proc_read
,
705 .write
= nfs_proc_write
,
706 .commit
= NULL
, /* commit */
707 .create
= nfs_proc_create
,
708 .remove
= nfs_proc_remove
,
709 .unlink_setup
= nfs_proc_unlink_setup
,
710 .unlink_done
= nfs_proc_unlink_done
,
711 .rename
= nfs_proc_rename
,
712 .link
= nfs_proc_link
,
713 .symlink
= nfs_proc_symlink
,
714 .mkdir
= nfs_proc_mkdir
,
715 .rmdir
= nfs_proc_rmdir
,
716 .readdir
= nfs_proc_readdir
,
717 .mknod
= nfs_proc_mknod
,
718 .statfs
= nfs_proc_statfs
,
719 .fsinfo
= nfs_proc_fsinfo
,
720 .pathconf
= nfs_proc_pathconf
,
721 .decode_dirent
= nfs_decode_dirent
,
722 .read_setup
= nfs_proc_read_setup
,
723 .write_setup
= nfs_proc_write_setup
,
724 .write_done
= nfs_write_done
,
725 .commit_setup
= nfs_proc_commit_setup
,
726 .file_open
= nfs_open
,
727 .file_release
= nfs_release
,
728 .lock
= nfs_proc_lock
,