2 * linux/fs/nfs/nfs3proc.c
4 * Client-side NFSv3 procedures stubs.
6 * Copyright (C) 1997, Olaf Kirch
10 #include <linux/utsname.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/nfs.h>
15 #include <linux/nfs3.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_page.h>
18 #include <linux/lockd/bind.h>
19 #include <linux/nfs_mount.h>
24 #define NFSDBG_FACILITY NFSDBG_PROC
26 /* A wrapper to handle the EJUKEBOX error message */
28 nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
32 rpc_clnt_sigmask(clnt, &oldset);
34 res = rpc_call_sync(clnt, msg, flags);
37 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
39 } while (!signalled());
40 rpc_clnt_sigunmask(clnt, &oldset);
44 #define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
47 nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
49 if (task->tk_status != -EJUKEBOX)
51 nfs_inc_stats(inode, NFSIOS_DELAY);
53 rpc_restart_call(task);
54 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
59 do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
60 struct nfs_fsinfo *info)
62 struct rpc_message msg = {
63 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
69 dprintk("%s: call fsinfo\n", __FUNCTION__);
70 nfs_fattr_init(info->fattr);
71 status = rpc_call_sync(client, &msg, 0);
72 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
73 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
74 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
75 msg.rpc_resp = info->fattr;
76 status = rpc_call_sync(client, &msg, 0);
77 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
83 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
86 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
87 struct nfs_fsinfo *info)
91 status = do_proc_get_root(server->client, fhandle, info);
92 if (status && server->nfs_client->cl_rpcclient != server->client)
93 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
98 * One function for each procedure in the NFS protocol.
101 nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
104 struct rpc_message msg = {
105 .rpc_proc = &nfs3_procedures[NFS3PROC_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 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
122 struct inode *inode = dentry->d_inode;
123 struct nfs3_sattrargs arg = {
127 struct rpc_message msg = {
128 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
134 dprintk("NFS call setattr\n");
135 nfs_fattr_init(fattr);
136 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
138 nfs_setattr_update_inode(inode, sattr);
139 dprintk("NFS reply setattr: %d\n", status);
144 nfs3_proc_lookup(struct inode *dir, struct qstr *name,
145 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
147 struct nfs_fattr dir_attr;
148 struct nfs3_diropargs arg = {
153 struct nfs3_diropres res = {
154 .dir_attr = &dir_attr,
158 struct rpc_message msg = {
159 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
165 dprintk("NFS call lookup %s\n", name->name);
166 nfs_fattr_init(&dir_attr);
167 nfs_fattr_init(fattr);
168 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
169 nfs_refresh_inode(dir, &dir_attr);
170 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
171 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
172 msg.rpc_argp = fhandle;
173 msg.rpc_resp = fattr;
174 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
176 dprintk("NFS reply lookup: %d\n", status);
180 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
182 struct nfs_fattr fattr;
183 struct nfs3_accessargs arg = {
186 struct nfs3_accessres res = {
189 struct rpc_message msg = {
190 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
193 .rpc_cred = entry->cred,
195 int mode = entry->mask;
198 dprintk("NFS call access\n");
201 arg.access |= NFS3_ACCESS_READ;
202 if (S_ISDIR(inode->i_mode)) {
203 if (mode & MAY_WRITE)
204 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
206 arg.access |= NFS3_ACCESS_LOOKUP;
208 if (mode & MAY_WRITE)
209 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
211 arg.access |= NFS3_ACCESS_EXECUTE;
213 nfs_fattr_init(&fattr);
214 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
215 nfs_refresh_inode(inode, &fattr);
218 if (res.access & NFS3_ACCESS_READ)
219 entry->mask |= MAY_READ;
220 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
221 entry->mask |= MAY_WRITE;
222 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
223 entry->mask |= MAY_EXEC;
225 dprintk("NFS reply access: %d\n", status);
229 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
230 unsigned int pgbase, unsigned int pglen)
232 struct nfs_fattr fattr;
233 struct nfs3_readlinkargs args = {
239 struct rpc_message msg = {
240 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
246 dprintk("NFS call readlink\n");
247 nfs_fattr_init(&fattr);
248 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
249 nfs_refresh_inode(inode, &fattr);
250 dprintk("NFS reply readlink: %d\n", status);
255 * Create a regular file.
256 * For now, we don't implement O_EXCL.
259 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
260 int flags, struct nameidata *nd)
262 struct nfs_fh fhandle;
263 struct nfs_fattr fattr;
264 struct nfs_fattr dir_attr;
265 struct nfs3_createargs arg = {
267 .name = dentry->d_name.name,
268 .len = dentry->d_name.len,
271 struct nfs3_diropres res = {
272 .dir_attr = &dir_attr,
276 struct rpc_message msg = {
277 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
281 mode_t mode = sattr->ia_mode;
284 dprintk("NFS call create %s\n", dentry->d_name.name);
285 arg.createmode = NFS3_CREATE_UNCHECKED;
286 if (flags & O_EXCL) {
287 arg.createmode = NFS3_CREATE_EXCLUSIVE;
288 arg.verifier[0] = jiffies;
289 arg.verifier[1] = current->pid;
292 sattr->ia_mode &= ~current->fs->umask;
295 nfs_fattr_init(&dir_attr);
296 nfs_fattr_init(&fattr);
297 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
298 nfs_refresh_inode(dir, &dir_attr);
300 /* If the server doesn't support the exclusive creation semantics,
301 * try again with simple 'guarded' mode. */
302 if (status == -ENOTSUPP) {
303 switch (arg.createmode) {
304 case NFS3_CREATE_EXCLUSIVE:
305 arg.createmode = NFS3_CREATE_GUARDED;
308 case NFS3_CREATE_GUARDED:
309 arg.createmode = NFS3_CREATE_UNCHECKED;
312 case NFS3_CREATE_UNCHECKED:
319 status = nfs_instantiate(dentry, &fhandle, &fattr);
323 /* When we created the file with exclusive semantics, make
324 * sure we set the attributes afterwards. */
325 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
326 dprintk("NFS call setattr (post-create)\n");
328 if (!(sattr->ia_valid & ATTR_ATIME_SET))
329 sattr->ia_valid |= ATTR_ATIME;
330 if (!(sattr->ia_valid & ATTR_MTIME_SET))
331 sattr->ia_valid |= ATTR_MTIME;
333 /* Note: we could use a guarded setattr here, but I'm
334 * not sure this buys us anything (and I'd have
335 * to revamp the NFSv3 XDR code) */
336 status = nfs3_proc_setattr(dentry, &fattr, sattr);
337 nfs_post_op_update_inode(dentry->d_inode, &fattr);
338 dprintk("NFS reply setattr (post-create): %d\n", status);
342 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
344 dprintk("NFS reply create: %d\n", status);
349 nfs3_proc_remove(struct inode *dir, struct qstr *name)
351 struct nfs_removeargs arg = {
353 .name.len = name->len,
354 .name.name = name->name,
356 struct nfs_removeres res;
357 struct rpc_message msg = {
358 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
364 dprintk("NFS call remove %s\n", name->name);
365 nfs_fattr_init(&res.dir_attr);
366 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
367 nfs_post_op_update_inode(dir, &res.dir_attr);
368 dprintk("NFS reply remove: %d\n", status);
373 nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
375 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
379 nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
381 struct nfs_removeres *res;
382 if (nfs3_async_handle_jukebox(task, dir))
384 res = task->tk_msg.rpc_resp;
385 nfs_post_op_update_inode(dir, &res->dir_attr);
390 nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
391 struct inode *new_dir, struct qstr *new_name)
393 struct nfs_fattr old_dir_attr, new_dir_attr;
394 struct nfs3_renameargs arg = {
395 .fromfh = NFS_FH(old_dir),
396 .fromname = old_name->name,
397 .fromlen = old_name->len,
398 .tofh = NFS_FH(new_dir),
399 .toname = new_name->name,
400 .tolen = new_name->len
402 struct nfs3_renameres res = {
403 .fromattr = &old_dir_attr,
404 .toattr = &new_dir_attr
406 struct rpc_message msg = {
407 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
413 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
414 nfs_fattr_init(&old_dir_attr);
415 nfs_fattr_init(&new_dir_attr);
416 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
417 nfs_post_op_update_inode(old_dir, &old_dir_attr);
418 nfs_post_op_update_inode(new_dir, &new_dir_attr);
419 dprintk("NFS reply rename: %d\n", status);
424 nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
426 struct nfs_fattr dir_attr, fattr;
427 struct nfs3_linkargs arg = {
428 .fromfh = NFS_FH(inode),
430 .toname = name->name,
433 struct nfs3_linkres res = {
434 .dir_attr = &dir_attr,
437 struct rpc_message msg = {
438 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
444 dprintk("NFS call link %s\n", name->name);
445 nfs_fattr_init(&dir_attr);
446 nfs_fattr_init(&fattr);
447 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
448 nfs_post_op_update_inode(dir, &dir_attr);
449 nfs_post_op_update_inode(inode, &fattr);
450 dprintk("NFS reply link: %d\n", status);
455 nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
456 unsigned int len, struct iattr *sattr)
458 struct nfs_fh fhandle;
459 struct nfs_fattr fattr, dir_attr;
460 struct nfs3_symlinkargs arg = {
461 .fromfh = NFS_FH(dir),
462 .fromname = dentry->d_name.name,
463 .fromlen = dentry->d_name.len,
468 struct nfs3_diropres res = {
469 .dir_attr = &dir_attr,
473 struct rpc_message msg = {
474 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
480 if (len > NFS3_MAXPATHLEN)
481 return -ENAMETOOLONG;
483 dprintk("NFS call symlink %s\n", dentry->d_name.name);
485 nfs_fattr_init(&dir_attr);
486 nfs_fattr_init(&fattr);
487 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
488 nfs_post_op_update_inode(dir, &dir_attr);
491 status = nfs_instantiate(dentry, &fhandle, &fattr);
493 dprintk("NFS reply symlink: %d\n", status);
498 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
500 struct nfs_fh fhandle;
501 struct nfs_fattr fattr, dir_attr;
502 struct nfs3_mkdirargs arg = {
504 .name = dentry->d_name.name,
505 .len = dentry->d_name.len,
508 struct nfs3_diropres res = {
509 .dir_attr = &dir_attr,
513 struct rpc_message msg = {
514 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
518 int mode = sattr->ia_mode;
521 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
523 sattr->ia_mode &= ~current->fs->umask;
525 nfs_fattr_init(&dir_attr);
526 nfs_fattr_init(&fattr);
527 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
528 nfs_post_op_update_inode(dir, &dir_attr);
531 status = nfs_instantiate(dentry, &fhandle, &fattr);
534 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
536 dprintk("NFS reply mkdir: %d\n", status);
541 nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
543 struct nfs_fattr dir_attr;
544 struct nfs3_diropargs arg = {
549 struct rpc_message msg = {
550 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
552 .rpc_resp = &dir_attr,
556 dprintk("NFS call rmdir %s\n", name->name);
557 nfs_fattr_init(&dir_attr);
558 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
559 nfs_post_op_update_inode(dir, &dir_attr);
560 dprintk("NFS reply rmdir: %d\n", status);
565 * The READDIR implementation is somewhat hackish - we pass the user buffer
566 * to the encode function, which installs it in the receive iovec.
567 * The decode function itself doesn't perform any decoding, it just makes
568 * sure the reply is syntactically correct.
570 * Also note that this implementation handles both plain readdir and
574 nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
575 u64 cookie, struct page *page, unsigned int count, int plus)
577 struct inode *dir = dentry->d_inode;
578 struct nfs_fattr dir_attr;
579 __be32 *verf = NFS_COOKIEVERF(dir);
580 struct nfs3_readdirargs arg = {
583 .verf = {verf[0], verf[1]},
588 struct nfs3_readdirres res = {
589 .dir_attr = &dir_attr,
593 struct rpc_message msg = {
594 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
602 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
604 dprintk("NFS call readdir%s %d\n",
605 plus? "plus" : "", (unsigned int) cookie);
607 nfs_fattr_init(&dir_attr);
608 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
610 nfs_invalidate_atime(dir);
612 nfs_refresh_inode(dir, &dir_attr);
613 dprintk("NFS reply readdir: %d\n", status);
618 nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
622 struct nfs_fattr fattr, dir_attr;
623 struct nfs3_mknodargs arg = {
625 .name = dentry->d_name.name,
626 .len = dentry->d_name.len,
630 struct nfs3_diropres res = {
631 .dir_attr = &dir_attr,
635 struct rpc_message msg = {
636 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
640 mode_t mode = sattr->ia_mode;
643 switch (sattr->ia_mode & S_IFMT) {
644 case S_IFBLK: arg.type = NF3BLK; break;
645 case S_IFCHR: arg.type = NF3CHR; break;
646 case S_IFIFO: arg.type = NF3FIFO; break;
647 case S_IFSOCK: arg.type = NF3SOCK; break;
648 default: return -EINVAL;
651 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
652 MAJOR(rdev), MINOR(rdev));
654 sattr->ia_mode &= ~current->fs->umask;
656 nfs_fattr_init(&dir_attr);
657 nfs_fattr_init(&fattr);
658 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
659 nfs_post_op_update_inode(dir, &dir_attr);
662 status = nfs_instantiate(dentry, &fh, &fattr);
665 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
667 dprintk("NFS reply mknod: %d\n", status);
672 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
673 struct nfs_fsstat *stat)
675 struct rpc_message msg = {
676 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
682 dprintk("NFS call fsstat\n");
683 nfs_fattr_init(stat->fattr);
684 status = rpc_call_sync(server->client, &msg, 0);
685 dprintk("NFS reply statfs: %d\n", status);
690 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
691 struct nfs_fsinfo *info)
693 struct rpc_message msg = {
694 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
700 dprintk("NFS call fsinfo\n");
701 nfs_fattr_init(info->fattr);
702 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
703 dprintk("NFS reply fsinfo: %d\n", status);
708 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
709 struct nfs_pathconf *info)
711 struct rpc_message msg = {
712 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
718 dprintk("NFS call pathconf\n");
719 nfs_fattr_init(info->fattr);
720 status = rpc_call_sync(server->client, &msg, 0);
721 dprintk("NFS reply pathconf: %d\n", status);
725 static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
727 if (nfs3_async_handle_jukebox(task, data->inode))
730 nfs_invalidate_atime(data->inode);
731 nfs_refresh_inode(data->inode, &data->fattr);
735 static void nfs3_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
737 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
740 static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
742 if (nfs3_async_handle_jukebox(task, data->inode))
744 if (task->tk_status >= 0)
745 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
749 static void nfs3_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
751 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
754 static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
756 if (nfs3_async_handle_jukebox(task, data->inode))
758 nfs_refresh_inode(data->inode, data->res.fattr);
762 static void nfs3_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
764 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
768 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
770 struct inode *inode = filp->f_path.dentry->d_inode;
772 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
775 const struct nfs_rpc_ops nfs_v3_clientops = {
776 .version = 3, /* protocol version */
777 .dentry_ops = &nfs_dentry_operations,
778 .dir_inode_ops = &nfs3_dir_inode_operations,
779 .file_inode_ops = &nfs3_file_inode_operations,
780 .getroot = nfs3_proc_get_root,
781 .getattr = nfs3_proc_getattr,
782 .setattr = nfs3_proc_setattr,
783 .lookup = nfs3_proc_lookup,
784 .access = nfs3_proc_access,
785 .readlink = nfs3_proc_readlink,
786 .create = nfs3_proc_create,
787 .remove = nfs3_proc_remove,
788 .unlink_setup = nfs3_proc_unlink_setup,
789 .unlink_done = nfs3_proc_unlink_done,
790 .rename = nfs3_proc_rename,
791 .link = nfs3_proc_link,
792 .symlink = nfs3_proc_symlink,
793 .mkdir = nfs3_proc_mkdir,
794 .rmdir = nfs3_proc_rmdir,
795 .readdir = nfs3_proc_readdir,
796 .mknod = nfs3_proc_mknod,
797 .statfs = nfs3_proc_statfs,
798 .fsinfo = nfs3_proc_fsinfo,
799 .pathconf = nfs3_proc_pathconf,
800 .decode_dirent = nfs3_decode_dirent,
801 .read_setup = nfs3_proc_read_setup,
802 .read_done = nfs3_read_done,
803 .write_setup = nfs3_proc_write_setup,
804 .write_done = nfs3_write_done,
805 .commit_setup = nfs3_proc_commit_setup,
806 .commit_done = nfs3_commit_done,
807 .file_open = nfs_open,
808 .file_release = nfs_release,
809 .lock = nfs3_proc_lock,
810 .clear_acl_cache = nfs3_forget_cached_acls,