blob: 72334c19d481b21efbfc7dac224c6b4f7e522fb1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/nfs3proc.c
3 *
4 * Client-side NFSv3 procedures stubs.
5 *
6 * Copyright (C) 1997, Olaf Kirch
7 */
8
9#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/string.h>
12#include <linux/sunrpc/clnt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000019#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chuck Lever006ea732006-03-20 13:44:14 -050021#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040022#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NFSDBG_FACILITY NFSDBG_PROC
25
Jeff Laytonb68d69b2010-01-07 09:42:04 -050026/* A wrapper to handle the EJUKEBOX and EKEYEXPIRED error messages */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int
28nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
29{
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 do {
32 res = rpc_call_sync(clnt, msg, flags);
Jeff Laytonb68d69b2010-01-07 09:42:04 -050033 if (res != -EJUKEBOX && res != -EKEYEXPIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 break;
Matthew Wilcox150030b2007-12-06 16:24:39 -050035 schedule_timeout_killable(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 res = -ERESTARTSYS;
Matthew Wilcox150030b2007-12-06 16:24:39 -050037 } while (!fatal_signal_pending(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return res;
39}
40
Chuck Leverdead28d2006-03-20 13:44:23 -050041#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static int
Chuck Lever006ea732006-03-20 13:44:14 -050044nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Jeff Laytonb68d69b2010-01-07 09:42:04 -050046 if (task->tk_status != -EJUKEBOX && task->tk_status != -EKEYEXPIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return 0;
Jeff Laytonb68d69b2010-01-07 09:42:04 -050048 if (task->tk_status == -EJUKEBOX)
49 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 task->tk_status = 0;
51 rpc_restart_call(task);
52 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
53 return 1;
54}
55
J. Bruce Fields03c21732006-01-03 09:55:48 +010056static int
57do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
58 struct nfs_fsinfo *info)
59{
Chuck Leverdead28d2006-03-20 13:44:23 -050060 struct rpc_message msg = {
61 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
62 .rpc_argp = fhandle,
63 .rpc_resp = info,
64 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010065 int status;
66
Harvey Harrison3110ff82008-05-02 13:42:44 -070067 dprintk("%s: call fsinfo\n", __func__);
J. Bruce Fields03c21732006-01-03 09:55:48 +010068 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050069 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070070 dprintk("%s: reply fsinfo: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010071 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050072 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
73 msg.rpc_resp = info->fattr;
74 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070075 dprintk("%s: reply getattr: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010076 }
77 return status;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
David Howells54ceac42006-08-22 20:06:13 -040081 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83static int
84nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
85 struct nfs_fsinfo *info)
86{
87 int status;
88
J. Bruce Fields03c21732006-01-03 09:55:48 +010089 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040090 if (status && server->nfs_client->cl_rpcclient != server->client)
91 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return status;
93}
94
95/*
96 * One function for each procedure in the NFS protocol.
97 */
98static int
99nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
100 struct nfs_fattr *fattr)
101{
Chuck Leverdead28d2006-03-20 13:44:23 -0500102 struct rpc_message msg = {
103 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
104 .rpc_argp = fhandle,
105 .rpc_resp = fattr,
106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int status;
108
109 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400110 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500111 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dprintk("NFS reply getattr: %d\n", status);
113 return status;
114}
115
116static int
117nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
118 struct iattr *sattr)
119{
120 struct inode *inode = dentry->d_inode;
121 struct nfs3_sattrargs arg = {
122 .fh = NFS_FH(inode),
123 .sattr = sattr,
124 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500125 struct rpc_message msg = {
126 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
127 .rpc_argp = &arg,
128 .rpc_resp = fattr,
129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int status;
131
132 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400133 if (sattr->ia_valid & ATTR_FILE)
134 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400135 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500136 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400137 if (status == 0)
138 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dprintk("NFS reply setattr: %d\n", status);
140 return status;
141}
142
143static int
144nfs3_proc_lookup(struct inode *dir, struct qstr *name,
145 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct nfs3_diropargs arg = {
148 .fh = NFS_FH(dir),
149 .name = name->name,
150 .len = name->len
151 };
152 struct nfs3_diropres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .fh = fhandle,
154 .fattr = fattr
155 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500156 struct rpc_message msg = {
157 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
158 .rpc_argp = &arg,
159 .rpc_resp = &res,
160 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int status;
162
163 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400164 res.dir_attr = nfs_alloc_fattr();
165 if (res.dir_attr == NULL)
166 return -ENOMEM;
167
Trond Myklebust0e574af2005-10-27 22:12:38 -0400168 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400170 nfs_refresh_inode(dir, res.dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500171 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
172 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
173 msg.rpc_argp = fhandle;
174 msg.rpc_resp = fattr;
175 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
176 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400177 nfs_free_fattr(res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return status;
180}
181
182static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
183{
184 struct nfs_fattr fattr;
185 struct nfs3_accessargs arg = {
186 .fh = NFS_FH(inode),
187 };
188 struct nfs3_accessres res = {
189 .fattr = &fattr,
190 };
191 struct rpc_message msg = {
192 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
193 .rpc_argp = &arg,
194 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500195 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 };
197 int mode = entry->mask;
198 int status;
199
200 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (mode & MAY_READ)
203 arg.access |= NFS3_ACCESS_READ;
204 if (S_ISDIR(inode->i_mode)) {
205 if (mode & MAY_WRITE)
206 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
207 if (mode & MAY_EXEC)
208 arg.access |= NFS3_ACCESS_LOOKUP;
209 } else {
210 if (mode & MAY_WRITE)
211 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
212 if (mode & MAY_EXEC)
213 arg.access |= NFS3_ACCESS_EXECUTE;
214 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400215 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
217 nfs_refresh_inode(inode, &fattr);
218 if (status == 0) {
219 entry->mask = 0;
220 if (res.access & NFS3_ACCESS_READ)
221 entry->mask |= MAY_READ;
222 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
223 entry->mask |= MAY_WRITE;
224 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
225 entry->mask |= MAY_EXEC;
226 }
227 dprintk("NFS reply access: %d\n", status);
228 return status;
229}
230
231static int nfs3_proc_readlink(struct inode *inode, struct page *page,
232 unsigned int pgbase, unsigned int pglen)
233{
234 struct nfs_fattr fattr;
235 struct nfs3_readlinkargs args = {
236 .fh = NFS_FH(inode),
237 .pgbase = pgbase,
238 .pglen = pglen,
239 .pages = &page
240 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500241 struct rpc_message msg = {
242 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
243 .rpc_argp = &args,
244 .rpc_resp = &fattr,
245 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int status;
247
248 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400249 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500250 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 nfs_refresh_inode(inode, &fattr);
252 dprintk("NFS reply readlink: %d\n", status);
253 return status;
254}
255
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400256struct nfs3_createdata {
257 struct rpc_message msg;
258 union {
259 struct nfs3_createargs create;
260 struct nfs3_mkdirargs mkdir;
261 struct nfs3_symlinkargs symlink;
262 struct nfs3_mknodargs mknod;
263 } arg;
264 struct nfs3_diropres res;
265 struct nfs_fh fh;
266 struct nfs_fattr fattr;
267 struct nfs_fattr dir_attr;
268};
269
270static struct nfs3_createdata *nfs3_alloc_createdata(void)
271{
272 struct nfs3_createdata *data;
273
274 data = kzalloc(sizeof(*data), GFP_KERNEL);
275 if (data != NULL) {
276 data->msg.rpc_argp = &data->arg;
277 data->msg.rpc_resp = &data->res;
278 data->res.fh = &data->fh;
279 data->res.fattr = &data->fattr;
280 data->res.dir_attr = &data->dir_attr;
281 nfs_fattr_init(data->res.fattr);
282 nfs_fattr_init(data->res.dir_attr);
283 }
284 return data;
285}
286
287static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
288{
289 int status;
290
291 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
292 nfs_post_op_update_inode(dir, data->res.dir_attr);
293 if (status == 0)
294 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
295 return status;
296}
297
298static void nfs3_free_createdata(struct nfs3_createdata *data)
299{
300 kfree(data);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
304 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306static int
307nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700308 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400310 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000311 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400312 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 dprintk("NFS call create %s\n", dentry->d_name.name);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400315
316 data = nfs3_alloc_createdata();
317 if (data == NULL)
318 goto out;
319
320 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
321 data->arg.create.fh = NFS_FH(dir);
322 data->arg.create.name = dentry->d_name.name;
323 data->arg.create.len = dentry->d_name.len;
324 data->arg.create.sattr = sattr;
325
326 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400328 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
329 data->arg.create.verifier[0] = jiffies;
330 data->arg.create.verifier[1] = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Al Viroce3b0f82009-03-29 19:08:22 -0400333 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000334
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400335 for (;;) {
336 status = nfs3_do_create(dir, dentry, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400338 if (status != -ENOTSUPP)
339 break;
340 /* If the server doesn't support the exclusive creation
341 * semantics, try again with simple 'guarded' mode. */
342 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400344 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
346
347 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400348 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350
351 case NFS3_CREATE_UNCHECKED:
352 goto out;
353 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400354 nfs_fattr_init(data->res.dir_attr);
355 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (status != 0)
359 goto out;
360
361 /* When we created the file with exclusive semantics, make
362 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400363 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 dprintk("NFS call setattr (post-create)\n");
365
366 if (!(sattr->ia_valid & ATTR_ATIME_SET))
367 sattr->ia_valid |= ATTR_ATIME;
368 if (!(sattr->ia_valid & ATTR_MTIME_SET))
369 sattr->ia_valid |= ATTR_MTIME;
370
371 /* Note: we could use a guarded setattr here, but I'm
372 * not sure this buys us anything (and I'd have
373 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400374 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
375 nfs_post_op_update_inode(dentry->d_inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400377 if (status != 0)
378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000380 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400382 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dprintk("NFS reply create: %d\n", status);
384 return status;
385}
386
387static int
388nfs3_proc_remove(struct inode *dir, struct qstr *name)
389{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400390 struct nfs_removeargs arg = {
391 .fh = NFS_FH(dir),
392 .name.len = name->len,
393 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400395 struct nfs_removeres res;
396 struct rpc_message msg = {
397 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
398 .rpc_argp = &arg,
399 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 };
401 int status;
402
403 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400404 nfs_fattr_init(&res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400406 nfs_post_op_update_inode(dir, &res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dprintk("NFS reply remove: %d\n", status);
408 return status;
409}
410
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400411static void
412nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400418nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400420 struct nfs_removeres *res;
421 if (nfs3_async_handle_jukebox(task, dir))
422 return 0;
423 res = task->tk_msg.rpc_resp;
424 nfs_post_op_update_inode(dir, &res->dir_attr);
425 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static int
429nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
430 struct inode *new_dir, struct qstr *new_name)
431{
432 struct nfs_fattr old_dir_attr, new_dir_attr;
433 struct nfs3_renameargs arg = {
434 .fromfh = NFS_FH(old_dir),
435 .fromname = old_name->name,
436 .fromlen = old_name->len,
437 .tofh = NFS_FH(new_dir),
438 .toname = new_name->name,
439 .tolen = new_name->len
440 };
441 struct nfs3_renameres res = {
442 .fromattr = &old_dir_attr,
443 .toattr = &new_dir_attr
444 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500445 struct rpc_message msg = {
446 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
447 .rpc_argp = &arg,
448 .rpc_resp = &res,
449 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int status;
451
452 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400453 nfs_fattr_init(&old_dir_attr);
454 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500455 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400456 nfs_post_op_update_inode(old_dir, &old_dir_attr);
457 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dprintk("NFS reply rename: %d\n", status);
459 return status;
460}
461
462static int
463nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
464{
465 struct nfs_fattr dir_attr, fattr;
466 struct nfs3_linkargs arg = {
467 .fromfh = NFS_FH(inode),
468 .tofh = NFS_FH(dir),
469 .toname = name->name,
470 .tolen = name->len
471 };
472 struct nfs3_linkres res = {
473 .dir_attr = &dir_attr,
474 .fattr = &fattr
475 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500476 struct rpc_message msg = {
477 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
478 .rpc_argp = &arg,
479 .rpc_resp = &res,
480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int status;
482
483 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400484 nfs_fattr_init(&dir_attr);
485 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500486 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400487 nfs_post_op_update_inode(dir, &dir_attr);
488 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dprintk("NFS reply link: %d\n", status);
490 return status;
491}
492
493static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400494nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
495 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400497 struct nfs3_createdata *data;
498 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Chuck Lever94a6d752006-08-22 20:06:23 -0400500 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400502
Chuck Lever94a6d752006-08-22 20:06:23 -0400503 dprintk("NFS call symlink %s\n", dentry->d_name.name);
504
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400505 data = nfs3_alloc_createdata();
506 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400507 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400508 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
509 data->arg.symlink.fromfh = NFS_FH(dir);
510 data->arg.symlink.fromname = dentry->d_name.name;
511 data->arg.symlink.fromlen = dentry->d_name.len;
512 data->arg.symlink.pages = &page;
513 data->arg.symlink.pathlen = len;
514 data->arg.symlink.sattr = sattr;
515
516 status = nfs3_do_create(dir, dentry, data);
517
518 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dprintk("NFS reply symlink: %d\n", status);
521 return status;
522}
523
524static int
525nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
526{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400527 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000528 int mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400529 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000532
Al Viroce3b0f82009-03-29 19:08:22 -0400533 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000534
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400535 data = nfs3_alloc_createdata();
536 if (data == NULL)
537 goto out;
538
539 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
540 data->arg.mkdir.fh = NFS_FH(dir);
541 data->arg.mkdir.name = dentry->d_name.name;
542 data->arg.mkdir.len = dentry->d_name.len;
543 data->arg.mkdir.sattr = sattr;
544
545 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000546 if (status != 0)
547 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400548
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000549 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
550out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400551 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dprintk("NFS reply mkdir: %d\n", status);
553 return status;
554}
555
556static int
557nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
558{
559 struct nfs_fattr dir_attr;
560 struct nfs3_diropargs arg = {
561 .fh = NFS_FH(dir),
562 .name = name->name,
563 .len = name->len
564 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500565 struct rpc_message msg = {
566 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
567 .rpc_argp = &arg,
568 .rpc_resp = &dir_attr,
569 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int status;
571
572 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400573 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500574 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400575 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 dprintk("NFS reply rmdir: %d\n", status);
577 return status;
578}
579
580/*
581 * The READDIR implementation is somewhat hackish - we pass the user buffer
582 * to the encode function, which installs it in the receive iovec.
583 * The decode function itself doesn't perform any decoding, it just makes
584 * sure the reply is syntactically correct.
585 *
586 * Also note that this implementation handles both plain readdir and
587 * readdirplus.
588 */
589static int
590nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
591 u64 cookie, struct page *page, unsigned int count, int plus)
592{
593 struct inode *dir = dentry->d_inode;
594 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700595 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct nfs3_readdirargs arg = {
597 .fh = NFS_FH(dir),
598 .cookie = cookie,
599 .verf = {verf[0], verf[1]},
600 .plus = plus,
601 .count = count,
602 .pages = &page
603 };
604 struct nfs3_readdirres res = {
605 .dir_attr = &dir_attr,
606 .verf = verf,
607 .plus = plus
608 };
609 struct rpc_message msg = {
610 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
611 .rpc_argp = &arg,
612 .rpc_resp = &res,
613 .rpc_cred = cred
614 };
615 int status;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (plus)
618 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
619
620 dprintk("NFS call readdir%s %d\n",
621 plus? "plus" : "", (unsigned int) cookie);
622
Trond Myklebust0e574af2005-10-27 22:12:38 -0400623 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400625
626 nfs_invalidate_atime(dir);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 nfs_refresh_inode(dir, &dir_attr);
629 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return status;
631}
632
633static int
634nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
635 dev_t rdev)
636{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400637 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000638 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400639 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
642 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000643
Al Viroce3b0f82009-03-29 19:08:22 -0400644 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000645
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400646 data = nfs3_alloc_createdata();
647 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000648 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400649
650 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
651 data->arg.mknod.fh = NFS_FH(dir);
652 data->arg.mknod.name = dentry->d_name.name;
653 data->arg.mknod.len = dentry->d_name.len;
654 data->arg.mknod.sattr = sattr;
655 data->arg.mknod.rdev = rdev;
656
657 switch (sattr->ia_mode & S_IFMT) {
658 case S_IFBLK:
659 data->arg.mknod.type = NF3BLK;
660 break;
661 case S_IFCHR:
662 data->arg.mknod.type = NF3CHR;
663 break;
664 case S_IFIFO:
665 data->arg.mknod.type = NF3FIFO;
666 break;
667 case S_IFSOCK:
668 data->arg.mknod.type = NF3SOCK;
669 break;
670 default:
671 status = -EINVAL;
672 goto out;
673 }
674
675 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000676 if (status != 0)
677 goto out;
678 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
679out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400680 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 dprintk("NFS reply mknod: %d\n", status);
682 return status;
683}
684
685static int
686nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
687 struct nfs_fsstat *stat)
688{
Chuck Leverdead28d2006-03-20 13:44:23 -0500689 struct rpc_message msg = {
690 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
691 .rpc_argp = fhandle,
692 .rpc_resp = stat,
693 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int status;
695
696 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400697 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500698 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dprintk("NFS reply statfs: %d\n", status);
700 return status;
701}
702
703static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400704do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct nfs_fsinfo *info)
706{
Chuck Leverdead28d2006-03-20 13:44:23 -0500707 struct rpc_message msg = {
708 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
709 .rpc_argp = fhandle,
710 .rpc_resp = info,
711 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int status;
713
714 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400715 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400716 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 dprintk("NFS reply fsinfo: %d\n", status);
718 return status;
719}
720
EG Keizer37ca8f52008-08-19 16:34:36 -0400721/*
722 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
723 * nfs_create_server
724 */
725static int
726nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
727 struct nfs_fsinfo *info)
728{
729 int status;
730
731 status = do_proc_fsinfo(server->client, fhandle, info);
732 if (status && server->nfs_client->cl_rpcclient != server->client)
733 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
734 return status;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int
738nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
739 struct nfs_pathconf *info)
740{
Chuck Leverdead28d2006-03-20 13:44:23 -0500741 struct rpc_message msg = {
742 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
743 .rpc_argp = fhandle,
744 .rpc_resp = info,
745 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int status;
747
748 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400749 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500750 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dprintk("NFS reply pathconf: %d\n", status);
752 return status;
753}
754
Trond Myklebustec06c092006-03-20 13:44:27 -0500755static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Chuck Lever006ea732006-03-20 13:44:14 -0500757 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500758 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400759
760 nfs_invalidate_atime(data->inode);
761 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400765static void nfs3_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400767 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Trond Myklebust788e7a82006-03-20 13:44:27 -0500770static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Chuck Lever006ea732006-03-20 13:44:14 -0500772 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500773 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400775 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400779static void nfs3_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400781 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Trond Myklebust788e7a82006-03-20 13:44:27 -0500784static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Chuck Lever006ea732006-03-20 13:44:14 -0500786 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500787 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400788 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400792static void nfs3_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400794 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static int
798nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
799{
Chuck Lever1093a602008-01-11 17:09:59 -0500800 struct inode *inode = filp->f_path.dentry->d_inode;
801
802 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
David Howells509de812006-08-22 20:06:11 -0400805const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .version = 3, /* protocol version */
807 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000808 .dir_inode_ops = &nfs3_dir_inode_operations,
809 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .getroot = nfs3_proc_get_root,
811 .getattr = nfs3_proc_getattr,
812 .setattr = nfs3_proc_setattr,
813 .lookup = nfs3_proc_lookup,
814 .access = nfs3_proc_access,
815 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .create = nfs3_proc_create,
817 .remove = nfs3_proc_remove,
818 .unlink_setup = nfs3_proc_unlink_setup,
819 .unlink_done = nfs3_proc_unlink_done,
820 .rename = nfs3_proc_rename,
821 .link = nfs3_proc_link,
822 .symlink = nfs3_proc_symlink,
823 .mkdir = nfs3_proc_mkdir,
824 .rmdir = nfs3_proc_rmdir,
825 .readdir = nfs3_proc_readdir,
826 .mknod = nfs3_proc_mknod,
827 .statfs = nfs3_proc_statfs,
828 .fsinfo = nfs3_proc_fsinfo,
829 .pathconf = nfs3_proc_pathconf,
830 .decode_dirent = nfs3_decode_dirent,
831 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500832 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500834 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500836 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000838 .clear_acl_cache = nfs3_forget_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400839 .close_context = nfs_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840};