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 res = rpc_call_sync(clnt, msg, flags);
35 schedule_timeout_killable(NFS_JUKEBOX_RETRY_TIME);
37 } while (!fatal_signal_pending(current));
41 #define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
44 nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
46 if (task->tk_status != -EJUKEBOX)
48 nfs_inc_stats(inode, NFSIOS_DELAY);
50 rpc_restart_call(task);
51 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
56 do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
57 struct nfs_fsinfo *info)
59 struct rpc_message msg = {
60 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
66 dprintk("%s: call fsinfo\n", __func__);
67 nfs_fattr_init(info->fattr);
68 status = rpc_call_sync(client, &msg, 0);
69 dprintk("%s: reply fsinfo: %d\n", __func__, status);
70 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
71 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
72 msg.rpc_resp = info->fattr;
73 status = rpc_call_sync(client, &msg, 0);
74 dprintk("%s: reply getattr: %d\n", __func__, status);
80 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
83 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
84 struct nfs_fsinfo *info)
88 status = do_proc_get_root(server->client, fhandle, info);
89 if (status && server->nfs_client->cl_rpcclient != server->client)
90 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
95 * One function for each procedure in the NFS protocol.
98 nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
99 struct nfs_fattr *fattr)
101 struct rpc_message msg = {
102 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
108 dprintk("NFS call getattr\n");
109 nfs_fattr_init(fattr);
110 status = rpc_call_sync(server->client, &msg, 0);
111 dprintk("NFS reply getattr: %d\n", status);
116 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
119 struct inode *inode = dentry->d_inode;
120 struct nfs3_sattrargs arg = {
124 struct rpc_message msg = {
125 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
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 nfs3_proc_lookup(struct inode *dir, struct qstr *name,
142 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
144 struct nfs_fattr dir_attr;
145 struct nfs3_diropargs arg = {
150 struct nfs3_diropres res = {
151 .dir_attr = &dir_attr,
155 struct rpc_message msg = {
156 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
162 dprintk("NFS call lookup %s\n", name->name);
163 nfs_fattr_init(&dir_attr);
164 nfs_fattr_init(fattr);
165 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
166 nfs_refresh_inode(dir, &dir_attr);
167 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
168 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
169 msg.rpc_argp = fhandle;
170 msg.rpc_resp = fattr;
171 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
173 dprintk("NFS reply lookup: %d\n", status);
177 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
179 struct nfs_fattr fattr;
180 struct nfs3_accessargs arg = {
183 struct nfs3_accessres res = {
186 struct rpc_message msg = {
187 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
190 .rpc_cred = entry->cred,
192 int mode = entry->mask;
195 dprintk("NFS call access\n");
198 arg.access |= NFS3_ACCESS_READ;
199 if (S_ISDIR(inode->i_mode)) {
200 if (mode & MAY_WRITE)
201 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
203 arg.access |= NFS3_ACCESS_LOOKUP;
205 if (mode & MAY_WRITE)
206 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
208 arg.access |= NFS3_ACCESS_EXECUTE;
210 nfs_fattr_init(&fattr);
211 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
212 nfs_refresh_inode(inode, &fattr);
215 if (res.access & NFS3_ACCESS_READ)
216 entry->mask |= MAY_READ;
217 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
218 entry->mask |= MAY_WRITE;
219 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
220 entry->mask |= MAY_EXEC;
222 dprintk("NFS reply access: %d\n", status);
226 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
227 unsigned int pgbase, unsigned int pglen)
229 struct nfs_fattr fattr;
230 struct nfs3_readlinkargs args = {
236 struct rpc_message msg = {
237 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
243 dprintk("NFS call readlink\n");
244 nfs_fattr_init(&fattr);
245 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
246 nfs_refresh_inode(inode, &fattr);
247 dprintk("NFS reply readlink: %d\n", status);
252 * Create a regular file.
253 * For now, we don't implement O_EXCL.
256 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
257 int flags, struct nameidata *nd)
259 struct nfs_fh fhandle;
260 struct nfs_fattr fattr;
261 struct nfs_fattr dir_attr;
262 struct nfs3_createargs arg = {
264 .name = dentry->d_name.name,
265 .len = dentry->d_name.len,
268 struct nfs3_diropres res = {
269 .dir_attr = &dir_attr,
273 struct rpc_message msg = {
274 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
278 mode_t mode = sattr->ia_mode;
281 dprintk("NFS call create %s\n", dentry->d_name.name);
282 arg.createmode = NFS3_CREATE_UNCHECKED;
283 if (flags & O_EXCL) {
284 arg.createmode = NFS3_CREATE_EXCLUSIVE;
285 arg.verifier[0] = jiffies;
286 arg.verifier[1] = current->pid;
289 sattr->ia_mode &= ~current->fs->umask;
292 nfs_fattr_init(&dir_attr);
293 nfs_fattr_init(&fattr);
294 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
295 nfs_refresh_inode(dir, &dir_attr);
297 /* If the server doesn't support the exclusive creation semantics,
298 * try again with simple 'guarded' mode. */
299 if (status == -ENOTSUPP) {
300 switch (arg.createmode) {
301 case NFS3_CREATE_EXCLUSIVE:
302 arg.createmode = NFS3_CREATE_GUARDED;
305 case NFS3_CREATE_GUARDED:
306 arg.createmode = NFS3_CREATE_UNCHECKED;
309 case NFS3_CREATE_UNCHECKED:
316 status = nfs_instantiate(dentry, &fhandle, &fattr);
320 /* When we created the file with exclusive semantics, make
321 * sure we set the attributes afterwards. */
322 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
323 dprintk("NFS call setattr (post-create)\n");
325 if (!(sattr->ia_valid & ATTR_ATIME_SET))
326 sattr->ia_valid |= ATTR_ATIME;
327 if (!(sattr->ia_valid & ATTR_MTIME_SET))
328 sattr->ia_valid |= ATTR_MTIME;
330 /* Note: we could use a guarded setattr here, but I'm
331 * not sure this buys us anything (and I'd have
332 * to revamp the NFSv3 XDR code) */
333 status = nfs3_proc_setattr(dentry, &fattr, sattr);
334 nfs_post_op_update_inode(dentry->d_inode, &fattr);
335 dprintk("NFS reply setattr (post-create): %d\n", status);
339 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
341 dprintk("NFS reply create: %d\n", status);
346 nfs3_proc_remove(struct inode *dir, struct qstr *name)
348 struct nfs_removeargs arg = {
350 .name.len = name->len,
351 .name.name = name->name,
353 struct nfs_removeres res;
354 struct rpc_message msg = {
355 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
361 dprintk("NFS call remove %s\n", name->name);
362 nfs_fattr_init(&res.dir_attr);
363 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
364 nfs_post_op_update_inode(dir, &res.dir_attr);
365 dprintk("NFS reply remove: %d\n", status);
370 nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
372 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
376 nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
378 struct nfs_removeres *res;
379 if (nfs3_async_handle_jukebox(task, dir))
381 res = task->tk_msg.rpc_resp;
382 nfs_post_op_update_inode(dir, &res->dir_attr);
387 nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
388 struct inode *new_dir, struct qstr *new_name)
390 struct nfs_fattr old_dir_attr, new_dir_attr;
391 struct nfs3_renameargs arg = {
392 .fromfh = NFS_FH(old_dir),
393 .fromname = old_name->name,
394 .fromlen = old_name->len,
395 .tofh = NFS_FH(new_dir),
396 .toname = new_name->name,
397 .tolen = new_name->len
399 struct nfs3_renameres res = {
400 .fromattr = &old_dir_attr,
401 .toattr = &new_dir_attr
403 struct rpc_message msg = {
404 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
410 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
411 nfs_fattr_init(&old_dir_attr);
412 nfs_fattr_init(&new_dir_attr);
413 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
414 nfs_post_op_update_inode(old_dir, &old_dir_attr);
415 nfs_post_op_update_inode(new_dir, &new_dir_attr);
416 dprintk("NFS reply rename: %d\n", status);
421 nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
423 struct nfs_fattr dir_attr, fattr;
424 struct nfs3_linkargs arg = {
425 .fromfh = NFS_FH(inode),
427 .toname = name->name,
430 struct nfs3_linkres res = {
431 .dir_attr = &dir_attr,
434 struct rpc_message msg = {
435 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
441 dprintk("NFS call link %s\n", name->name);
442 nfs_fattr_init(&dir_attr);
443 nfs_fattr_init(&fattr);
444 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
445 nfs_post_op_update_inode(dir, &dir_attr);
446 nfs_post_op_update_inode(inode, &fattr);
447 dprintk("NFS reply link: %d\n", status);
452 nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
453 unsigned int len, struct iattr *sattr)
455 struct nfs_fh fhandle;
456 struct nfs_fattr fattr, dir_attr;
457 struct nfs3_symlinkargs arg = {
458 .fromfh = NFS_FH(dir),
459 .fromname = dentry->d_name.name,
460 .fromlen = dentry->d_name.len,
465 struct nfs3_diropres res = {
466 .dir_attr = &dir_attr,
470 struct rpc_message msg = {
471 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
477 if (len > NFS3_MAXPATHLEN)
478 return -ENAMETOOLONG;
480 dprintk("NFS call symlink %s\n", dentry->d_name.name);
482 nfs_fattr_init(&dir_attr);
483 nfs_fattr_init(&fattr);
484 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
485 nfs_post_op_update_inode(dir, &dir_attr);
488 status = nfs_instantiate(dentry, &fhandle, &fattr);
490 dprintk("NFS reply symlink: %d\n", status);
495 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
497 struct nfs_fh fhandle;
498 struct nfs_fattr fattr, dir_attr;
499 struct nfs3_mkdirargs arg = {
501 .name = dentry->d_name.name,
502 .len = dentry->d_name.len,
505 struct nfs3_diropres res = {
506 .dir_attr = &dir_attr,
510 struct rpc_message msg = {
511 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
515 int mode = sattr->ia_mode;
518 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
520 sattr->ia_mode &= ~current->fs->umask;
522 nfs_fattr_init(&dir_attr);
523 nfs_fattr_init(&fattr);
524 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
525 nfs_post_op_update_inode(dir, &dir_attr);
528 status = nfs_instantiate(dentry, &fhandle, &fattr);
531 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
533 dprintk("NFS reply mkdir: %d\n", status);
538 nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
540 struct nfs_fattr dir_attr;
541 struct nfs3_diropargs arg = {
546 struct rpc_message msg = {
547 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
549 .rpc_resp = &dir_attr,
553 dprintk("NFS call rmdir %s\n", name->name);
554 nfs_fattr_init(&dir_attr);
555 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
556 nfs_post_op_update_inode(dir, &dir_attr);
557 dprintk("NFS reply rmdir: %d\n", status);
562 * The READDIR implementation is somewhat hackish - we pass the user buffer
563 * to the encode function, which installs it in the receive iovec.
564 * The decode function itself doesn't perform any decoding, it just makes
565 * sure the reply is syntactically correct.
567 * Also note that this implementation handles both plain readdir and
571 nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
572 u64 cookie, struct page *page, unsigned int count, int plus)
574 struct inode *dir = dentry->d_inode;
575 struct nfs_fattr dir_attr;
576 __be32 *verf = NFS_COOKIEVERF(dir);
577 struct nfs3_readdirargs arg = {
580 .verf = {verf[0], verf[1]},
585 struct nfs3_readdirres res = {
586 .dir_attr = &dir_attr,
590 struct rpc_message msg = {
591 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
599 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
601 dprintk("NFS call readdir%s %d\n",
602 plus? "plus" : "", (unsigned int) cookie);
604 nfs_fattr_init(&dir_attr);
605 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
607 nfs_invalidate_atime(dir);
609 nfs_refresh_inode(dir, &dir_attr);
610 dprintk("NFS reply readdir: %d\n", status);
615 nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
619 struct nfs_fattr fattr, dir_attr;
620 struct nfs3_mknodargs arg = {
622 .name = dentry->d_name.name,
623 .len = dentry->d_name.len,
627 struct nfs3_diropres res = {
628 .dir_attr = &dir_attr,
632 struct rpc_message msg = {
633 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
637 mode_t mode = sattr->ia_mode;
640 switch (sattr->ia_mode & S_IFMT) {
641 case S_IFBLK: arg.type = NF3BLK; break;
642 case S_IFCHR: arg.type = NF3CHR; break;
643 case S_IFIFO: arg.type = NF3FIFO; break;
644 case S_IFSOCK: arg.type = NF3SOCK; break;
645 default: return -EINVAL;
648 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
649 MAJOR(rdev), MINOR(rdev));
651 sattr->ia_mode &= ~current->fs->umask;
653 nfs_fattr_init(&dir_attr);
654 nfs_fattr_init(&fattr);
655 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
656 nfs_post_op_update_inode(dir, &dir_attr);
659 status = nfs_instantiate(dentry, &fh, &fattr);
662 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
664 dprintk("NFS reply mknod: %d\n", status);
669 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
670 struct nfs_fsstat *stat)
672 struct rpc_message msg = {
673 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
679 dprintk("NFS call fsstat\n");
680 nfs_fattr_init(stat->fattr);
681 status = rpc_call_sync(server->client, &msg, 0);
682 dprintk("NFS reply statfs: %d\n", status);
687 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
688 struct nfs_fsinfo *info)
690 struct rpc_message msg = {
691 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
697 dprintk("NFS call fsinfo\n");
698 nfs_fattr_init(info->fattr);
699 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
700 dprintk("NFS reply fsinfo: %d\n", status);
705 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
706 struct nfs_pathconf *info)
708 struct rpc_message msg = {
709 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
715 dprintk("NFS call pathconf\n");
716 nfs_fattr_init(info->fattr);
717 status = rpc_call_sync(server->client, &msg, 0);
718 dprintk("NFS reply pathconf: %d\n", status);
722 static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
724 if (nfs3_async_handle_jukebox(task, data->inode))
727 nfs_invalidate_atime(data->inode);
728 nfs_refresh_inode(data->inode, &data->fattr);
732 static void nfs3_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
734 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
737 static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
739 if (nfs3_async_handle_jukebox(task, data->inode))
741 if (task->tk_status >= 0)
742 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
746 static void nfs3_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
748 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
751 static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
753 if (nfs3_async_handle_jukebox(task, data->inode))
755 nfs_refresh_inode(data->inode, data->res.fattr);
759 static void nfs3_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
761 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
765 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
767 struct inode *inode = filp->f_path.dentry->d_inode;
769 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
772 const struct nfs_rpc_ops nfs_v3_clientops = {
773 .version = 3, /* protocol version */
774 .dentry_ops = &nfs_dentry_operations,
775 .dir_inode_ops = &nfs3_dir_inode_operations,
776 .file_inode_ops = &nfs3_file_inode_operations,
777 .getroot = nfs3_proc_get_root,
778 .getattr = nfs3_proc_getattr,
779 .setattr = nfs3_proc_setattr,
780 .lookup = nfs3_proc_lookup,
781 .access = nfs3_proc_access,
782 .readlink = nfs3_proc_readlink,
783 .create = nfs3_proc_create,
784 .remove = nfs3_proc_remove,
785 .unlink_setup = nfs3_proc_unlink_setup,
786 .unlink_done = nfs3_proc_unlink_done,
787 .rename = nfs3_proc_rename,
788 .link = nfs3_proc_link,
789 .symlink = nfs3_proc_symlink,
790 .mkdir = nfs3_proc_mkdir,
791 .rmdir = nfs3_proc_rmdir,
792 .readdir = nfs3_proc_readdir,
793 .mknod = nfs3_proc_mknod,
794 .statfs = nfs3_proc_statfs,
795 .fsinfo = nfs3_proc_fsinfo,
796 .pathconf = nfs3_proc_pathconf,
797 .decode_dirent = nfs3_decode_dirent,
798 .read_setup = nfs3_proc_read_setup,
799 .read_done = nfs3_read_done,
800 .write_setup = nfs3_proc_write_setup,
801 .write_done = nfs3_write_done,
802 .commit_setup = nfs3_proc_commit_setup,
803 .commit_done = nfs3_commit_done,
804 .file_open = nfs_open,
805 .file_release = nfs_release,
806 .lock = nfs3_proc_lock,
807 .clear_acl_cache = nfs3_forget_cached_acls,