blob: 24992f0a29f22b12c753004c065f23f34d19df36 [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>
13#include <linux/nfs.h>
14#include <linux/nfs3.h>
15#include <linux/nfs_fs.h>
16#include <linux/nfs_page.h>
17#include <linux/lockd/bind.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000018#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Chuck Lever006ea732006-03-20 13:44:14 -050020#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040021#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define NFSDBG_FACILITY NFSDBG_PROC
24
Jeff Laytonb68d69b2010-01-07 09:42:04 -050025/* A wrapper to handle the EJUKEBOX and EKEYEXPIRED error messages */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int
27nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
28{
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 do {
31 res = rpc_call_sync(clnt, msg, flags);
Jeff Laytonb68d69b2010-01-07 09:42:04 -050032 if (res != -EJUKEBOX && res != -EKEYEXPIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 break;
Matthew Wilcox150030b2007-12-06 16:24:39 -050034 schedule_timeout_killable(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 res = -ERESTARTSYS;
Matthew Wilcox150030b2007-12-06 16:24:39 -050036 } while (!fatal_signal_pending(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return res;
38}
39
Chuck Leverdead28d2006-03-20 13:44:23 -050040#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static int
Chuck Lever006ea732006-03-20 13:44:14 -050043nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Jeff Laytonb68d69b2010-01-07 09:42:04 -050045 if (task->tk_status != -EJUKEBOX && task->tk_status != -EKEYEXPIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return 0;
Jeff Laytonb68d69b2010-01-07 09:42:04 -050047 if (task->tk_status == -EJUKEBOX)
48 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 task->tk_status = 0;
50 rpc_restart_call(task);
51 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
52 return 1;
53}
54
J. Bruce Fields03c21732006-01-03 09:55:48 +010055static int
56do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
57 struct nfs_fsinfo *info)
58{
Chuck Leverdead28d2006-03-20 13:44:23 -050059 struct rpc_message msg = {
60 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
61 .rpc_argp = fhandle,
62 .rpc_resp = info,
63 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010064 int status;
65
Harvey Harrison3110ff82008-05-02 13:42:44 -070066 dprintk("%s: call fsinfo\n", __func__);
J. Bruce Fields03c21732006-01-03 09:55:48 +010067 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050068 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070069 dprintk("%s: reply fsinfo: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010070 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050071 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
72 msg.rpc_resp = info->fattr;
73 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 dprintk("%s: reply getattr: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010075 }
76 return status;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
David Howells54ceac42006-08-22 20:06:13 -040080 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82static int
83nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
84 struct nfs_fsinfo *info)
85{
86 int status;
87
J. Bruce Fields03c21732006-01-03 09:55:48 +010088 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040089 if (status && server->nfs_client->cl_rpcclient != server->client)
90 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return status;
92}
93
94/*
95 * One function for each procedure in the NFS protocol.
96 */
97static int
98nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
99 struct nfs_fattr *fattr)
100{
Chuck Leverdead28d2006-03-20 13:44:23 -0500101 struct rpc_message msg = {
102 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
103 .rpc_argp = fhandle,
104 .rpc_resp = fattr,
105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int status;
107
108 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400109 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500110 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dprintk("NFS reply getattr: %d\n", status);
112 return status;
113}
114
115static int
116nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
117 struct iattr *sattr)
118{
119 struct inode *inode = dentry->d_inode;
120 struct nfs3_sattrargs arg = {
121 .fh = NFS_FH(inode),
122 .sattr = sattr,
123 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500124 struct rpc_message msg = {
125 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
126 .rpc_argp = &arg,
127 .rpc_resp = fattr,
128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int status;
130
131 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400132 if (sattr->ia_valid & ATTR_FILE)
133 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400134 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500135 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400136 if (status == 0)
137 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 dprintk("NFS reply setattr: %d\n", status);
139 return status;
140}
141
142static int
143nfs3_proc_lookup(struct inode *dir, struct qstr *name,
144 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
145{
146 struct nfs_fattr dir_attr;
147 struct nfs3_diropargs arg = {
148 .fh = NFS_FH(dir),
149 .name = name->name,
150 .len = name->len
151 };
152 struct nfs3_diropres res = {
153 .dir_attr = &dir_attr,
154 .fh = fhandle,
155 .fattr = fattr
156 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500157 struct rpc_message msg = {
158 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
159 .rpc_argp = &arg,
160 .rpc_resp = &res,
161 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int status;
163
164 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400165 nfs_fattr_init(&dir_attr);
166 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500167 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust81c76882007-10-02 10:30:00 -0400168 nfs_refresh_inode(dir, &dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
170 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
171 msg.rpc_argp = fhandle;
172 msg.rpc_resp = fattr;
173 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return status;
177}
178
179static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
180{
181 struct nfs_fattr fattr;
182 struct nfs3_accessargs arg = {
183 .fh = NFS_FH(inode),
184 };
185 struct nfs3_accessres res = {
186 .fattr = &fattr,
187 };
188 struct rpc_message msg = {
189 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
190 .rpc_argp = &arg,
191 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500192 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 };
194 int mode = entry->mask;
195 int status;
196
197 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (mode & MAY_READ)
200 arg.access |= NFS3_ACCESS_READ;
201 if (S_ISDIR(inode->i_mode)) {
202 if (mode & MAY_WRITE)
203 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
204 if (mode & MAY_EXEC)
205 arg.access |= NFS3_ACCESS_LOOKUP;
206 } else {
207 if (mode & MAY_WRITE)
208 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
209 if (mode & MAY_EXEC)
210 arg.access |= NFS3_ACCESS_EXECUTE;
211 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400212 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
214 nfs_refresh_inode(inode, &fattr);
215 if (status == 0) {
216 entry->mask = 0;
217 if (res.access & NFS3_ACCESS_READ)
218 entry->mask |= MAY_READ;
219 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
220 entry->mask |= MAY_WRITE;
221 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
222 entry->mask |= MAY_EXEC;
223 }
224 dprintk("NFS reply access: %d\n", status);
225 return status;
226}
227
228static int nfs3_proc_readlink(struct inode *inode, struct page *page,
229 unsigned int pgbase, unsigned int pglen)
230{
231 struct nfs_fattr fattr;
232 struct nfs3_readlinkargs args = {
233 .fh = NFS_FH(inode),
234 .pgbase = pgbase,
235 .pglen = pglen,
236 .pages = &page
237 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500238 struct rpc_message msg = {
239 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
240 .rpc_argp = &args,
241 .rpc_resp = &fattr,
242 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int status;
244
245 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400246 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500247 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 nfs_refresh_inode(inode, &fattr);
249 dprintk("NFS reply readlink: %d\n", status);
250 return status;
251}
252
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400253struct nfs3_createdata {
254 struct rpc_message msg;
255 union {
256 struct nfs3_createargs create;
257 struct nfs3_mkdirargs mkdir;
258 struct nfs3_symlinkargs symlink;
259 struct nfs3_mknodargs mknod;
260 } arg;
261 struct nfs3_diropres res;
262 struct nfs_fh fh;
263 struct nfs_fattr fattr;
264 struct nfs_fattr dir_attr;
265};
266
267static struct nfs3_createdata *nfs3_alloc_createdata(void)
268{
269 struct nfs3_createdata *data;
270
271 data = kzalloc(sizeof(*data), GFP_KERNEL);
272 if (data != NULL) {
273 data->msg.rpc_argp = &data->arg;
274 data->msg.rpc_resp = &data->res;
275 data->res.fh = &data->fh;
276 data->res.fattr = &data->fattr;
277 data->res.dir_attr = &data->dir_attr;
278 nfs_fattr_init(data->res.fattr);
279 nfs_fattr_init(data->res.dir_attr);
280 }
281 return data;
282}
283
284static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
285{
286 int status;
287
288 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
289 nfs_post_op_update_inode(dir, data->res.dir_attr);
290 if (status == 0)
291 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
292 return status;
293}
294
295static void nfs3_free_createdata(struct nfs3_createdata *data)
296{
297 kfree(data);
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
301 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303static int
304nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700305 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400307 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000308 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400309 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 dprintk("NFS call create %s\n", dentry->d_name.name);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400312
313 data = nfs3_alloc_createdata();
314 if (data == NULL)
315 goto out;
316
317 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
318 data->arg.create.fh = NFS_FH(dir);
319 data->arg.create.name = dentry->d_name.name;
320 data->arg.create.len = dentry->d_name.len;
321 data->arg.create.sattr = sattr;
322
323 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400325 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
326 data->arg.create.verifier[0] = jiffies;
327 data->arg.create.verifier[1] = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
Al Viroce3b0f82009-03-29 19:08:22 -0400330 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000331
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400332 for (;;) {
333 status = nfs3_do_create(dir, dentry, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400335 if (status != -ENOTSUPP)
336 break;
337 /* If the server doesn't support the exclusive creation
338 * semantics, try again with simple 'guarded' mode. */
339 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400341 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343
344 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400345 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347
348 case NFS3_CREATE_UNCHECKED:
349 goto out;
350 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400351 nfs_fattr_init(data->res.dir_attr);
352 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (status != 0)
356 goto out;
357
358 /* When we created the file with exclusive semantics, make
359 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400360 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dprintk("NFS call setattr (post-create)\n");
362
363 if (!(sattr->ia_valid & ATTR_ATIME_SET))
364 sattr->ia_valid |= ATTR_ATIME;
365 if (!(sattr->ia_valid & ATTR_MTIME_SET))
366 sattr->ia_valid |= ATTR_MTIME;
367
368 /* Note: we could use a guarded setattr here, but I'm
369 * not sure this buys us anything (and I'd have
370 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400371 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
372 nfs_post_op_update_inode(dentry->d_inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400374 if (status != 0)
375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000377 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400379 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dprintk("NFS reply create: %d\n", status);
381 return status;
382}
383
384static int
385nfs3_proc_remove(struct inode *dir, struct qstr *name)
386{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400387 struct nfs_removeargs arg = {
388 .fh = NFS_FH(dir),
389 .name.len = name->len,
390 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400392 struct nfs_removeres res;
393 struct rpc_message msg = {
394 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
395 .rpc_argp = &arg,
396 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 };
398 int status;
399
400 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400401 nfs_fattr_init(&res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400403 nfs_post_op_update_inode(dir, &res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dprintk("NFS reply remove: %d\n", status);
405 return status;
406}
407
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400408static void
409nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400415nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400417 struct nfs_removeres *res;
418 if (nfs3_async_handle_jukebox(task, dir))
419 return 0;
420 res = task->tk_msg.rpc_resp;
421 nfs_post_op_update_inode(dir, &res->dir_attr);
422 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static int
426nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
427 struct inode *new_dir, struct qstr *new_name)
428{
429 struct nfs_fattr old_dir_attr, new_dir_attr;
430 struct nfs3_renameargs arg = {
431 .fromfh = NFS_FH(old_dir),
432 .fromname = old_name->name,
433 .fromlen = old_name->len,
434 .tofh = NFS_FH(new_dir),
435 .toname = new_name->name,
436 .tolen = new_name->len
437 };
438 struct nfs3_renameres res = {
439 .fromattr = &old_dir_attr,
440 .toattr = &new_dir_attr
441 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500442 struct rpc_message msg = {
443 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
444 .rpc_argp = &arg,
445 .rpc_resp = &res,
446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int status;
448
449 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400450 nfs_fattr_init(&old_dir_attr);
451 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500452 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400453 nfs_post_op_update_inode(old_dir, &old_dir_attr);
454 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dprintk("NFS reply rename: %d\n", status);
456 return status;
457}
458
459static int
460nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
461{
462 struct nfs_fattr dir_attr, fattr;
463 struct nfs3_linkargs arg = {
464 .fromfh = NFS_FH(inode),
465 .tofh = NFS_FH(dir),
466 .toname = name->name,
467 .tolen = name->len
468 };
469 struct nfs3_linkres res = {
470 .dir_attr = &dir_attr,
471 .fattr = &fattr
472 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500473 struct rpc_message msg = {
474 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
475 .rpc_argp = &arg,
476 .rpc_resp = &res,
477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int status;
479
480 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400481 nfs_fattr_init(&dir_attr);
482 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500483 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400484 nfs_post_op_update_inode(dir, &dir_attr);
485 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dprintk("NFS reply link: %d\n", status);
487 return status;
488}
489
490static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400491nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
492 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400494 struct nfs3_createdata *data;
495 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Chuck Lever94a6d752006-08-22 20:06:23 -0400497 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400499
Chuck Lever94a6d752006-08-22 20:06:23 -0400500 dprintk("NFS call symlink %s\n", dentry->d_name.name);
501
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400502 data = nfs3_alloc_createdata();
503 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400504 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400505 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
506 data->arg.symlink.fromfh = NFS_FH(dir);
507 data->arg.symlink.fromname = dentry->d_name.name;
508 data->arg.symlink.fromlen = dentry->d_name.len;
509 data->arg.symlink.pages = &page;
510 data->arg.symlink.pathlen = len;
511 data->arg.symlink.sattr = sattr;
512
513 status = nfs3_do_create(dir, dentry, data);
514
515 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400516out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 dprintk("NFS reply symlink: %d\n", status);
518 return status;
519}
520
521static int
522nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
523{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400524 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000525 int mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000529
Al Viroce3b0f82009-03-29 19:08:22 -0400530 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000531
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400532 data = nfs3_alloc_createdata();
533 if (data == NULL)
534 goto out;
535
536 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
537 data->arg.mkdir.fh = NFS_FH(dir);
538 data->arg.mkdir.name = dentry->d_name.name;
539 data->arg.mkdir.len = dentry->d_name.len;
540 data->arg.mkdir.sattr = sattr;
541
542 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000543 if (status != 0)
544 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400545
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000546 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
547out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400548 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dprintk("NFS reply mkdir: %d\n", status);
550 return status;
551}
552
553static int
554nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
555{
556 struct nfs_fattr dir_attr;
557 struct nfs3_diropargs arg = {
558 .fh = NFS_FH(dir),
559 .name = name->name,
560 .len = name->len
561 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500562 struct rpc_message msg = {
563 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
564 .rpc_argp = &arg,
565 .rpc_resp = &dir_attr,
566 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int status;
568
569 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400570 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500571 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400572 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dprintk("NFS reply rmdir: %d\n", status);
574 return status;
575}
576
577/*
578 * The READDIR implementation is somewhat hackish - we pass the user buffer
579 * to the encode function, which installs it in the receive iovec.
580 * The decode function itself doesn't perform any decoding, it just makes
581 * sure the reply is syntactically correct.
582 *
583 * Also note that this implementation handles both plain readdir and
584 * readdirplus.
585 */
586static int
587nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
588 u64 cookie, struct page *page, unsigned int count, int plus)
589{
590 struct inode *dir = dentry->d_inode;
591 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700592 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct nfs3_readdirargs arg = {
594 .fh = NFS_FH(dir),
595 .cookie = cookie,
596 .verf = {verf[0], verf[1]},
597 .plus = plus,
598 .count = count,
599 .pages = &page
600 };
601 struct nfs3_readdirres res = {
602 .dir_attr = &dir_attr,
603 .verf = verf,
604 .plus = plus
605 };
606 struct rpc_message msg = {
607 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
608 .rpc_argp = &arg,
609 .rpc_resp = &res,
610 .rpc_cred = cred
611 };
612 int status;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (plus)
615 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
616
617 dprintk("NFS call readdir%s %d\n",
618 plus? "plus" : "", (unsigned int) cookie);
619
Trond Myklebust0e574af2005-10-27 22:12:38 -0400620 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400622
623 nfs_invalidate_atime(dir);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 nfs_refresh_inode(dir, &dir_attr);
626 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return status;
628}
629
630static int
631nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
632 dev_t rdev)
633{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400634 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000635 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400636 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
639 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000640
Al Viroce3b0f82009-03-29 19:08:22 -0400641 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000642
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400643 data = nfs3_alloc_createdata();
644 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000645 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400646
647 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
648 data->arg.mknod.fh = NFS_FH(dir);
649 data->arg.mknod.name = dentry->d_name.name;
650 data->arg.mknod.len = dentry->d_name.len;
651 data->arg.mknod.sattr = sattr;
652 data->arg.mknod.rdev = rdev;
653
654 switch (sattr->ia_mode & S_IFMT) {
655 case S_IFBLK:
656 data->arg.mknod.type = NF3BLK;
657 break;
658 case S_IFCHR:
659 data->arg.mknod.type = NF3CHR;
660 break;
661 case S_IFIFO:
662 data->arg.mknod.type = NF3FIFO;
663 break;
664 case S_IFSOCK:
665 data->arg.mknod.type = NF3SOCK;
666 break;
667 default:
668 status = -EINVAL;
669 goto out;
670 }
671
672 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000673 if (status != 0)
674 goto out;
675 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
676out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400677 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 dprintk("NFS reply mknod: %d\n", status);
679 return status;
680}
681
682static int
683nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
684 struct nfs_fsstat *stat)
685{
Chuck Leverdead28d2006-03-20 13:44:23 -0500686 struct rpc_message msg = {
687 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
688 .rpc_argp = fhandle,
689 .rpc_resp = stat,
690 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int status;
692
693 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400694 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500695 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 dprintk("NFS reply statfs: %d\n", status);
697 return status;
698}
699
700static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400701do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct nfs_fsinfo *info)
703{
Chuck Leverdead28d2006-03-20 13:44:23 -0500704 struct rpc_message msg = {
705 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
706 .rpc_argp = fhandle,
707 .rpc_resp = info,
708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int status;
710
711 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400712 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400713 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dprintk("NFS reply fsinfo: %d\n", status);
715 return status;
716}
717
EG Keizer37ca8f52008-08-19 16:34:36 -0400718/*
719 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
720 * nfs_create_server
721 */
722static int
723nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
724 struct nfs_fsinfo *info)
725{
726 int status;
727
728 status = do_proc_fsinfo(server->client, fhandle, info);
729 if (status && server->nfs_client->cl_rpcclient != server->client)
730 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
731 return status;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static int
735nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
736 struct nfs_pathconf *info)
737{
Chuck Leverdead28d2006-03-20 13:44:23 -0500738 struct rpc_message msg = {
739 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
740 .rpc_argp = fhandle,
741 .rpc_resp = info,
742 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int status;
744
745 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400746 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500747 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 dprintk("NFS reply pathconf: %d\n", status);
749 return status;
750}
751
Trond Myklebustec06c092006-03-20 13:44:27 -0500752static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Chuck Lever006ea732006-03-20 13:44:14 -0500754 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500755 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400756
757 nfs_invalidate_atime(data->inode);
758 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400762static void nfs3_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400764 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Trond Myklebust788e7a82006-03-20 13:44:27 -0500767static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Chuck Lever006ea732006-03-20 13:44:14 -0500769 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500770 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400772 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400776static void nfs3_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400778 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Trond Myklebust788e7a82006-03-20 13:44:27 -0500781static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Chuck Lever006ea732006-03-20 13:44:14 -0500783 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500784 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400785 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400789static void nfs3_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400791 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794static int
795nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
796{
Chuck Lever1093a602008-01-11 17:09:59 -0500797 struct inode *inode = filp->f_path.dentry->d_inode;
798
799 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
David Howells509de812006-08-22 20:06:11 -0400802const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 .version = 3, /* protocol version */
804 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000805 .dir_inode_ops = &nfs3_dir_inode_operations,
806 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .getroot = nfs3_proc_get_root,
808 .getattr = nfs3_proc_getattr,
809 .setattr = nfs3_proc_setattr,
810 .lookup = nfs3_proc_lookup,
811 .access = nfs3_proc_access,
812 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .create = nfs3_proc_create,
814 .remove = nfs3_proc_remove,
815 .unlink_setup = nfs3_proc_unlink_setup,
816 .unlink_done = nfs3_proc_unlink_done,
817 .rename = nfs3_proc_rename,
818 .link = nfs3_proc_link,
819 .symlink = nfs3_proc_symlink,
820 .mkdir = nfs3_proc_mkdir,
821 .rmdir = nfs3_proc_rmdir,
822 .readdir = nfs3_proc_readdir,
823 .mknod = nfs3_proc_mknod,
824 .statfs = nfs3_proc_statfs,
825 .fsinfo = nfs3_proc_fsinfo,
826 .pathconf = nfs3_proc_pathconf,
827 .decode_dirent = nfs3_decode_dirent,
828 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500829 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500831 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500833 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000835 .clear_acl_cache = nfs3_forget_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400836 .close_context = nfs_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837};