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", __func__);
67 nfs_fattr_init(fattr);
68 status = rpc_call_sync(server->client, &msg, 0);
69 /* Retry with default authentication if different */
70 if (status && server->nfs_client->cl_rpcclient != server->client)
71 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
72 dprintk("%s: reply getattr: %d\n", __func__, status);
75 dprintk("%s: call statfs\n", __func__);
76 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
77 msg.rpc_resp = &fsinfo;
78 status = rpc_call_sync(server->client, &msg, 0);
79 /* Retry with default authentication if different */
80 if (status && server->nfs_client->cl_rpcclient != server->client)
81 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
82 dprintk("%s: reply statfs: %d\n", __func__, status);
85 info->rtmax = NFS_MAXDATA;
86 info->rtpref = fsinfo.tsize;
87 info->rtmult = fsinfo.bsize;
88 info->wtmax = NFS_MAXDATA;
89 info->wtpref = fsinfo.tsize;
90 info->wtmult = fsinfo.bsize;
91 info->dtpref = fsinfo.tsize;
92 info->maxfilesize = 0x7FFFFFFF;
98 * One function for each procedure in the NFS protocol.
101 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
104 struct rpc_message msg = {
105 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
111 dprintk("NFS call getattr\n");
112 nfs_fattr_init(fattr);
113 status = rpc_call_sync(server->client, &msg, 0);
114 dprintk("NFS reply getattr: %d\n", status);
119 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
122 struct inode *inode = dentry->d_inode;
123 struct nfs_sattrargs arg = {
127 struct rpc_message msg = {
128 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
134 /* Mask out the non-modebit related stuff from attr->ia_mode */
135 sattr->ia_mode &= S_IALLUGO;
137 dprintk("NFS call setattr\n");
138 if (sattr->ia_valid & ATTR_FILE)
139 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
140 nfs_fattr_init(fattr);
141 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
143 nfs_setattr_update_inode(inode, sattr);
144 dprintk("NFS reply setattr: %d\n", status);
149 nfs_proc_lookup(struct inode *dir, struct qstr *name,
150 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
152 struct nfs_diropargs arg = {
157 struct nfs_diropok res = {
161 struct rpc_message msg = {
162 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
168 dprintk("NFS call lookup %s\n", name->name);
169 nfs_fattr_init(fattr);
170 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
171 dprintk("NFS reply lookup: %d\n", status);
175 static int nfs_proc_readlink(struct inode *inode, struct page *page,
176 unsigned int pgbase, unsigned int pglen)
178 struct nfs_readlinkargs args = {
184 struct rpc_message msg = {
185 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
190 dprintk("NFS call readlink\n");
191 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
192 dprintk("NFS reply readlink: %d\n", status);
197 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
198 int flags, struct nameidata *nd)
200 struct nfs_fh fhandle;
201 struct nfs_fattr fattr;
202 struct nfs_createargs arg = {
204 .name = dentry->d_name.name,
205 .len = dentry->d_name.len,
208 struct nfs_diropok res = {
212 struct rpc_message msg = {
213 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
219 nfs_fattr_init(&fattr);
220 dprintk("NFS call create %s\n", dentry->d_name.name);
221 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
222 nfs_mark_for_revalidate(dir);
224 status = nfs_instantiate(dentry, &fhandle, &fattr);
225 dprintk("NFS reply create: %d\n", status);
230 * In NFSv2, mknod is grafted onto the create call.
233 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
236 struct nfs_fh fhandle;
237 struct nfs_fattr fattr;
238 struct nfs_createargs arg = {
240 .name = dentry->d_name.name,
241 .len = dentry->d_name.len,
244 struct nfs_diropok res = {
248 struct rpc_message msg = {
249 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
255 dprintk("NFS call mknod %s\n", dentry->d_name.name);
257 mode = sattr->ia_mode;
258 if (S_ISFIFO(mode)) {
259 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
260 sattr->ia_valid &= ~ATTR_SIZE;
261 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
262 sattr->ia_valid |= ATTR_SIZE;
263 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
266 nfs_fattr_init(&fattr);
267 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
268 nfs_mark_for_revalidate(dir);
270 if (status == -EINVAL && S_ISFIFO(mode)) {
271 sattr->ia_mode = mode;
272 nfs_fattr_init(&fattr);
273 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
276 status = nfs_instantiate(dentry, &fhandle, &fattr);
277 dprintk("NFS reply mknod: %d\n", status);
282 nfs_proc_remove(struct inode *dir, struct qstr *name)
284 struct nfs_removeargs arg = {
286 .name.len = name->len,
287 .name.name = name->name,
289 struct rpc_message msg = {
290 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
295 dprintk("NFS call remove %s\n", name->name);
296 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
297 nfs_mark_for_revalidate(dir);
299 dprintk("NFS reply remove: %d\n", status);
304 nfs_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
306 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
309 static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
311 nfs_mark_for_revalidate(dir);
316 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
317 struct inode *new_dir, struct qstr *new_name)
319 struct nfs_renameargs arg = {
320 .fromfh = NFS_FH(old_dir),
321 .fromname = old_name->name,
322 .fromlen = old_name->len,
323 .tofh = NFS_FH(new_dir),
324 .toname = new_name->name,
325 .tolen = new_name->len
327 struct rpc_message msg = {
328 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
333 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
334 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
335 nfs_mark_for_revalidate(old_dir);
336 nfs_mark_for_revalidate(new_dir);
337 dprintk("NFS reply rename: %d\n", status);
342 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
344 struct nfs_linkargs arg = {
345 .fromfh = NFS_FH(inode),
347 .toname = name->name,
350 struct rpc_message msg = {
351 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
356 dprintk("NFS call link %s\n", name->name);
357 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
358 nfs_mark_for_revalidate(inode);
359 nfs_mark_for_revalidate(dir);
360 dprintk("NFS reply link: %d\n", status);
365 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
366 unsigned int len, struct iattr *sattr)
368 struct nfs_fh fhandle;
369 struct nfs_fattr fattr;
370 struct nfs_symlinkargs arg = {
371 .fromfh = NFS_FH(dir),
372 .fromname = dentry->d_name.name,
373 .fromlen = dentry->d_name.len,
378 struct rpc_message msg = {
379 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
384 if (len > NFS2_MAXPATHLEN)
385 return -ENAMETOOLONG;
387 dprintk("NFS call symlink %s\n", dentry->d_name.name);
389 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
390 nfs_mark_for_revalidate(dir);
393 * V2 SYMLINK requests don't return any attributes. Setting the
394 * filehandle size to zero indicates to nfs_instantiate that it
395 * should fill in the data with a LOOKUP call on the wire.
398 nfs_fattr_init(&fattr);
400 status = nfs_instantiate(dentry, &fhandle, &fattr);
403 dprintk("NFS reply symlink: %d\n", status);
408 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
410 struct nfs_fh fhandle;
411 struct nfs_fattr fattr;
412 struct nfs_createargs arg = {
414 .name = dentry->d_name.name,
415 .len = dentry->d_name.len,
418 struct nfs_diropok res = {
422 struct rpc_message msg = {
423 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
429 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
430 nfs_fattr_init(&fattr);
431 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
432 nfs_mark_for_revalidate(dir);
434 status = nfs_instantiate(dentry, &fhandle, &fattr);
435 dprintk("NFS reply mkdir: %d\n", status);
440 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
442 struct nfs_diropargs arg = {
447 struct rpc_message msg = {
448 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
453 dprintk("NFS call rmdir %s\n", name->name);
454 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
455 nfs_mark_for_revalidate(dir);
456 dprintk("NFS reply rmdir: %d\n", status);
461 * The READDIR implementation is somewhat hackish - we pass a temporary
462 * buffer to the encode function, which installs it in the receive
463 * the receive iovec. The decode function just parses the reply to make
464 * sure it is syntactically correct; the entries itself are decoded
465 * from nfs_readdir by calling the decode_entry function directly.
468 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
469 u64 cookie, struct page *page, unsigned int count, int plus)
471 struct inode *dir = dentry->d_inode;
472 struct nfs_readdirargs arg = {
478 struct rpc_message msg = {
479 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
485 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
486 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
488 nfs_invalidate_atime(dir);
490 dprintk("NFS reply readdir: %d\n", status);
495 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
496 struct nfs_fsstat *stat)
498 struct nfs2_fsstat fsinfo;
499 struct rpc_message msg = {
500 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
506 dprintk("NFS call statfs\n");
507 nfs_fattr_init(stat->fattr);
508 status = rpc_call_sync(server->client, &msg, 0);
509 dprintk("NFS reply statfs: %d\n", status);
512 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
513 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
514 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
523 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
524 struct nfs_fsinfo *info)
526 struct nfs2_fsstat fsinfo;
527 struct rpc_message msg = {
528 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
534 dprintk("NFS call fsinfo\n");
535 nfs_fattr_init(info->fattr);
536 status = rpc_call_sync(server->client, &msg, 0);
537 dprintk("NFS reply fsinfo: %d\n", status);
540 info->rtmax = NFS_MAXDATA;
541 info->rtpref = fsinfo.tsize;
542 info->rtmult = fsinfo.bsize;
543 info->wtmax = NFS_MAXDATA;
544 info->wtpref = fsinfo.tsize;
545 info->wtmult = fsinfo.bsize;
546 info->dtpref = fsinfo.tsize;
547 info->maxfilesize = 0x7FFFFFFF;
548 info->lease_time = 0;
554 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
555 struct nfs_pathconf *info)
558 info->max_namelen = NFS2_MAXNAMLEN;
562 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
564 nfs_invalidate_atime(data->inode);
565 if (task->tk_status >= 0) {
566 nfs_refresh_inode(data->inode, data->res.fattr);
567 /* Emulate the eof flag, which isn't normally needed in NFSv2
568 * as it is guaranteed to always return the file attributes
570 if (data->args.offset + data->args.count >= data->res.fattr->size)
576 static void nfs_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
578 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
581 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
583 if (task->tk_status >= 0)
584 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
588 static void nfs_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
590 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
591 data->args.stable = NFS_FILE_SYNC;
592 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
596 nfs_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
602 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
604 struct inode *inode = filp->f_path.dentry->d_inode;
606 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
609 /* Helper functions for NFS lock bounds checking */
610 #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
611 static int nfs_lock_check_bounds(const struct file_lock *fl)
615 start = (__s32)fl->fl_start;
616 if ((loff_t)start != fl->fl_start)
619 if (fl->fl_end != OFFSET_MAX) {
620 end = (__s32)fl->fl_end;
621 if ((loff_t)end != fl->fl_end)
624 end = NFS_LOCK32_OFFSET_MAX;
626 if (start < 0 || start > end)
633 const struct nfs_rpc_ops nfs_v2_clientops = {
634 .version = 2, /* protocol version */
635 .dentry_ops = &nfs_dentry_operations,
636 .dir_inode_ops = &nfs_dir_inode_operations,
637 .file_inode_ops = &nfs_file_inode_operations,
638 .getroot = nfs_proc_get_root,
639 .getattr = nfs_proc_getattr,
640 .setattr = nfs_proc_setattr,
641 .lookup = nfs_proc_lookup,
642 .access = NULL, /* access */
643 .readlink = nfs_proc_readlink,
644 .create = nfs_proc_create,
645 .remove = nfs_proc_remove,
646 .unlink_setup = nfs_proc_unlink_setup,
647 .unlink_done = nfs_proc_unlink_done,
648 .rename = nfs_proc_rename,
649 .link = nfs_proc_link,
650 .symlink = nfs_proc_symlink,
651 .mkdir = nfs_proc_mkdir,
652 .rmdir = nfs_proc_rmdir,
653 .readdir = nfs_proc_readdir,
654 .mknod = nfs_proc_mknod,
655 .statfs = nfs_proc_statfs,
656 .fsinfo = nfs_proc_fsinfo,
657 .pathconf = nfs_proc_pathconf,
658 .decode_dirent = nfs_decode_dirent,
659 .read_setup = nfs_proc_read_setup,
660 .read_done = nfs_read_done,
661 .write_setup = nfs_proc_write_setup,
662 .write_done = nfs_write_done,
663 .commit_setup = nfs_proc_commit_setup,
664 .lock = nfs_proc_lock,
665 .lock_check_bounds = nfs_lock_check_bounds,
666 .close_context = nfs_close_context,