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>
48 #define NFSDBG_FACILITY NFSDBG_PROC
51 * Bare-bones access to getattr: this is for nfs_read_super.
54 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
55 struct nfs_fsinfo
*info
)
57 struct nfs_fattr
*fattr
= info
->fattr
;
58 struct nfs2_fsstat fsinfo
;
59 struct rpc_message msg
= {
60 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
66 dprintk("%s: call getattr\n", __FUNCTION__
);
67 nfs_fattr_init(fattr
);
68 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
69 dprintk("%s: reply getattr: %d\n", __FUNCTION__
, status
);
72 dprintk("%s: call statfs\n", __FUNCTION__
);
73 msg
.rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
];
74 msg
.rpc_resp
= &fsinfo
;
75 status
= rpc_call_sync(server
->nfs_client
->cl_rpcclient
, &msg
, 0);
76 dprintk("%s: reply statfs: %d\n", __FUNCTION__
, status
);
79 info
->rtmax
= NFS_MAXDATA
;
80 info
->rtpref
= fsinfo
.tsize
;
81 info
->rtmult
= fsinfo
.bsize
;
82 info
->wtmax
= NFS_MAXDATA
;
83 info
->wtpref
= fsinfo
.tsize
;
84 info
->wtmult
= fsinfo
.bsize
;
85 info
->dtpref
= fsinfo
.tsize
;
86 info
->maxfilesize
= 0x7FFFFFFF;
92 * One function for each procedure in the NFS protocol.
95 nfs_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
96 struct nfs_fattr
*fattr
)
98 struct rpc_message msg
= {
99 .rpc_proc
= &nfs_procedures
[NFSPROC_GETATTR
],
105 dprintk("NFS call getattr\n");
106 nfs_fattr_init(fattr
);
107 status
= rpc_call_sync(server
->client
, &msg
, 0);
108 dprintk("NFS reply getattr: %d\n", status
);
113 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
116 struct inode
*inode
= dentry
->d_inode
;
117 struct nfs_sattrargs arg
= {
121 struct rpc_message msg
= {
122 .rpc_proc
= &nfs_procedures
[NFSPROC_SETATTR
],
128 /* Mask out the non-modebit related stuff from attr->ia_mode */
129 sattr
->ia_mode
&= S_IALLUGO
;
131 dprintk("NFS call setattr\n");
132 nfs_fattr_init(fattr
);
133 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
135 nfs_setattr_update_inode(inode
, sattr
);
136 dprintk("NFS reply setattr: %d\n", status
);
141 nfs_proc_lookup(struct inode
*dir
, struct qstr
*name
,
142 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
144 struct nfs_diropargs arg
= {
149 struct nfs_diropok res
= {
153 struct rpc_message msg
= {
154 .rpc_proc
= &nfs_procedures
[NFSPROC_LOOKUP
],
160 dprintk("NFS call lookup %s\n", name
->name
);
161 nfs_fattr_init(fattr
);
162 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
163 dprintk("NFS reply lookup: %d\n", status
);
167 static int nfs_proc_readlink(struct inode
*inode
, struct page
*page
,
168 unsigned int pgbase
, unsigned int pglen
)
170 struct nfs_readlinkargs args
= {
176 struct rpc_message msg
= {
177 .rpc_proc
= &nfs_procedures
[NFSPROC_READLINK
],
182 dprintk("NFS call readlink\n");
183 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
184 dprintk("NFS reply readlink: %d\n", status
);
189 nfs_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
190 int flags
, struct nameidata
*nd
)
192 struct nfs_fh fhandle
;
193 struct nfs_fattr fattr
;
194 struct nfs_createargs arg
= {
196 .name
= dentry
->d_name
.name
,
197 .len
= dentry
->d_name
.len
,
200 struct nfs_diropok res
= {
204 struct rpc_message msg
= {
205 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
211 nfs_fattr_init(&fattr
);
212 dprintk("NFS call create %s\n", dentry
->d_name
.name
);
213 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
215 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
216 dprintk("NFS reply create: %d\n", status
);
221 * In NFSv2, mknod is grafted onto the create call.
224 nfs_proc_mknod(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
227 struct nfs_fh fhandle
;
228 struct nfs_fattr fattr
;
229 struct nfs_createargs arg
= {
231 .name
= dentry
->d_name
.name
,
232 .len
= dentry
->d_name
.len
,
235 struct nfs_diropok res
= {
239 struct rpc_message msg
= {
240 .rpc_proc
= &nfs_procedures
[NFSPROC_CREATE
],
246 dprintk("NFS call mknod %s\n", dentry
->d_name
.name
);
248 mode
= sattr
->ia_mode
;
249 if (S_ISFIFO(mode
)) {
250 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
251 sattr
->ia_valid
&= ~ATTR_SIZE
;
252 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
253 sattr
->ia_valid
|= ATTR_SIZE
;
254 sattr
->ia_size
= new_encode_dev(rdev
);/* get out your barf bag */
257 nfs_fattr_init(&fattr
);
258 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
259 nfs_mark_for_revalidate(dir
);
261 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
262 sattr
->ia_mode
= mode
;
263 nfs_fattr_init(&fattr
);
264 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
267 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
268 dprintk("NFS reply mknod: %d\n", status
);
273 nfs_proc_remove(struct inode
*dir
, struct qstr
*name
)
275 struct nfs_removeargs arg
= {
277 .name
.len
= name
->len
,
278 .name
.name
= name
->name
,
280 struct rpc_message msg
= {
281 .rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
],
286 dprintk("NFS call remove %s\n", name
->name
);
287 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
288 nfs_mark_for_revalidate(dir
);
290 dprintk("NFS reply remove: %d\n", status
);
295 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
297 msg
->rpc_proc
= &nfs_procedures
[NFSPROC_REMOVE
];
300 static int nfs_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
302 nfs_mark_for_revalidate(dir
);
307 nfs_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
308 struct inode
*new_dir
, struct qstr
*new_name
)
310 struct nfs_renameargs arg
= {
311 .fromfh
= NFS_FH(old_dir
),
312 .fromname
= old_name
->name
,
313 .fromlen
= old_name
->len
,
314 .tofh
= NFS_FH(new_dir
),
315 .toname
= new_name
->name
,
316 .tolen
= new_name
->len
318 struct rpc_message msg
= {
319 .rpc_proc
= &nfs_procedures
[NFSPROC_RENAME
],
324 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
325 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
326 nfs_mark_for_revalidate(old_dir
);
327 nfs_mark_for_revalidate(new_dir
);
328 dprintk("NFS reply rename: %d\n", status
);
333 nfs_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
335 struct nfs_linkargs arg
= {
336 .fromfh
= NFS_FH(inode
),
338 .toname
= name
->name
,
341 struct rpc_message msg
= {
342 .rpc_proc
= &nfs_procedures
[NFSPROC_LINK
],
347 dprintk("NFS call link %s\n", name
->name
);
348 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
349 nfs_mark_for_revalidate(inode
);
350 nfs_mark_for_revalidate(dir
);
351 dprintk("NFS reply link: %d\n", status
);
356 nfs_proc_symlink(struct inode
*dir
, struct dentry
*dentry
, struct page
*page
,
357 unsigned int len
, struct iattr
*sattr
)
359 struct nfs_fh fhandle
;
360 struct nfs_fattr fattr
;
361 struct nfs_symlinkargs arg
= {
362 .fromfh
= NFS_FH(dir
),
363 .fromname
= dentry
->d_name
.name
,
364 .fromlen
= dentry
->d_name
.len
,
369 struct rpc_message msg
= {
370 .rpc_proc
= &nfs_procedures
[NFSPROC_SYMLINK
],
375 if (len
> NFS2_MAXPATHLEN
)
376 return -ENAMETOOLONG
;
378 dprintk("NFS call symlink %s\n", dentry
->d_name
.name
);
380 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
381 nfs_mark_for_revalidate(dir
);
384 * V2 SYMLINK requests don't return any attributes. Setting the
385 * filehandle size to zero indicates to nfs_instantiate that it
386 * should fill in the data with a LOOKUP call on the wire.
389 nfs_fattr_init(&fattr
);
391 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
394 dprintk("NFS reply symlink: %d\n", status
);
399 nfs_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
)
401 struct nfs_fh fhandle
;
402 struct nfs_fattr fattr
;
403 struct nfs_createargs arg
= {
405 .name
= dentry
->d_name
.name
,
406 .len
= dentry
->d_name
.len
,
409 struct nfs_diropok res
= {
413 struct rpc_message msg
= {
414 .rpc_proc
= &nfs_procedures
[NFSPROC_MKDIR
],
420 dprintk("NFS call mkdir %s\n", dentry
->d_name
.name
);
421 nfs_fattr_init(&fattr
);
422 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
423 nfs_mark_for_revalidate(dir
);
425 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
426 dprintk("NFS reply mkdir: %d\n", status
);
431 nfs_proc_rmdir(struct inode
*dir
, struct qstr
*name
)
433 struct nfs_diropargs arg
= {
438 struct rpc_message msg
= {
439 .rpc_proc
= &nfs_procedures
[NFSPROC_RMDIR
],
444 dprintk("NFS call rmdir %s\n", name
->name
);
445 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
446 nfs_mark_for_revalidate(dir
);
447 dprintk("NFS reply rmdir: %d\n", status
);
452 * The READDIR implementation is somewhat hackish - we pass a temporary
453 * buffer to the encode function, which installs it in the receive
454 * the receive iovec. The decode function just parses the reply to make
455 * sure it is syntactically correct; the entries itself are decoded
456 * from nfs_readdir by calling the decode_entry function directly.
459 nfs_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
460 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
462 struct inode
*dir
= dentry
->d_inode
;
463 struct nfs_readdirargs arg
= {
469 struct rpc_message msg
= {
470 .rpc_proc
= &nfs_procedures
[NFSPROC_READDIR
],
476 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
477 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
479 nfs_invalidate_atime(dir
);
481 dprintk("NFS reply readdir: %d\n", status
);
486 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
487 struct nfs_fsstat
*stat
)
489 struct nfs2_fsstat fsinfo
;
490 struct rpc_message msg
= {
491 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
497 dprintk("NFS call statfs\n");
498 nfs_fattr_init(stat
->fattr
);
499 status
= rpc_call_sync(server
->client
, &msg
, 0);
500 dprintk("NFS reply statfs: %d\n", status
);
503 stat
->tbytes
= (u64
)fsinfo
.blocks
* fsinfo
.bsize
;
504 stat
->fbytes
= (u64
)fsinfo
.bfree
* fsinfo
.bsize
;
505 stat
->abytes
= (u64
)fsinfo
.bavail
* fsinfo
.bsize
;
514 nfs_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
515 struct nfs_fsinfo
*info
)
517 struct nfs2_fsstat fsinfo
;
518 struct rpc_message msg
= {
519 .rpc_proc
= &nfs_procedures
[NFSPROC_STATFS
],
525 dprintk("NFS call fsinfo\n");
526 nfs_fattr_init(info
->fattr
);
527 status
= rpc_call_sync(server
->client
, &msg
, 0);
528 dprintk("NFS reply fsinfo: %d\n", status
);
531 info
->rtmax
= NFS_MAXDATA
;
532 info
->rtpref
= fsinfo
.tsize
;
533 info
->rtmult
= fsinfo
.bsize
;
534 info
->wtmax
= NFS_MAXDATA
;
535 info
->wtpref
= fsinfo
.tsize
;
536 info
->wtmult
= fsinfo
.bsize
;
537 info
->dtpref
= fsinfo
.tsize
;
538 info
->maxfilesize
= 0x7FFFFFFF;
539 info
->lease_time
= 0;
545 nfs_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
546 struct nfs_pathconf
*info
)
549 info
->max_namelen
= NFS2_MAXNAMLEN
;
553 static int nfs_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
555 nfs_invalidate_atime(data
->inode
);
556 if (task
->tk_status
>= 0) {
557 nfs_refresh_inode(data
->inode
, data
->res
.fattr
);
558 /* Emulate the eof flag, which isn't normally needed in NFSv2
559 * as it is guaranteed to always return the file attributes
561 if (data
->args
.offset
+ data
->args
.count
>= data
->res
.fattr
->size
)
567 static void nfs_proc_read_setup(struct nfs_read_data
*data
)
569 struct rpc_message msg
= {
570 .rpc_proc
= &nfs_procedures
[NFSPROC_READ
],
571 .rpc_argp
= &data
->args
,
572 .rpc_resp
= &data
->res
,
573 .rpc_cred
= data
->cred
,
576 rpc_call_setup(&data
->task
, &msg
, 0);
579 static int nfs_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
581 if (task
->tk_status
>= 0)
582 nfs_post_op_update_inode_force_wcc(data
->inode
, data
->res
.fattr
);
586 static void nfs_proc_write_setup(struct nfs_write_data
*data
, int how
)
588 struct rpc_message msg
= {
589 .rpc_proc
= &nfs_procedures
[NFSPROC_WRITE
],
590 .rpc_argp
= &data
->args
,
591 .rpc_resp
= &data
->res
,
592 .rpc_cred
= data
->cred
,
595 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
596 data
->args
.stable
= NFS_FILE_SYNC
;
598 /* Finalize the task. */
599 rpc_call_setup(&data
->task
, &msg
, 0);
603 nfs_proc_commit_setup(struct nfs_write_data
*data
, int how
)
609 nfs_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
611 return nlmclnt_proc(filp
->f_path
.dentry
->d_inode
, cmd
, fl
);
615 const struct nfs_rpc_ops nfs_v2_clientops
= {
616 .version
= 2, /* protocol version */
617 .dentry_ops
= &nfs_dentry_operations
,
618 .dir_inode_ops
= &nfs_dir_inode_operations
,
619 .file_inode_ops
= &nfs_file_inode_operations
,
620 .getroot
= nfs_proc_get_root
,
621 .getattr
= nfs_proc_getattr
,
622 .setattr
= nfs_proc_setattr
,
623 .lookup
= nfs_proc_lookup
,
624 .access
= NULL
, /* access */
625 .readlink
= nfs_proc_readlink
,
626 .create
= nfs_proc_create
,
627 .remove
= nfs_proc_remove
,
628 .unlink_setup
= nfs_proc_unlink_setup
,
629 .unlink_done
= nfs_proc_unlink_done
,
630 .rename
= nfs_proc_rename
,
631 .link
= nfs_proc_link
,
632 .symlink
= nfs_proc_symlink
,
633 .mkdir
= nfs_proc_mkdir
,
634 .rmdir
= nfs_proc_rmdir
,
635 .readdir
= nfs_proc_readdir
,
636 .mknod
= nfs_proc_mknod
,
637 .statfs
= nfs_proc_statfs
,
638 .fsinfo
= nfs_proc_fsinfo
,
639 .pathconf
= nfs_proc_pathconf
,
640 .decode_dirent
= nfs_decode_dirent
,
641 .read_setup
= nfs_proc_read_setup
,
642 .read_done
= nfs_read_done
,
643 .write_setup
= nfs_proc_write_setup
,
644 .write_done
= nfs_write_done
,
645 .commit_setup
= nfs_proc_commit_setup
,
646 .file_open
= nfs_open
,
647 .file_release
= nfs_release
,
648 .lock
= nfs_proc_lock
,