blob: 3f8881d1a0504253de64420a9a25f7d7308d73be [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* A wrapper to handle the EJUKEBOX error message */
26static 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);
32 if (res != -EJUKEBOX)
33 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{
45 if (task->tk_status != -EJUKEBOX)
46 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050047 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 task->tk_status = 0;
49 rpc_restart_call(task);
50 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
51 return 1;
52}
53
J. Bruce Fields03c21732006-01-03 09:55:48 +010054static int
55do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
56 struct nfs_fsinfo *info)
57{
Chuck Leverdead28d2006-03-20 13:44:23 -050058 struct rpc_message msg = {
59 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
60 .rpc_argp = fhandle,
61 .rpc_resp = info,
62 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010063 int status;
64
Harvey Harrison3110ff82008-05-02 13:42:44 -070065 dprintk("%s: call fsinfo\n", __func__);
J. Bruce Fields03c21732006-01-03 09:55:48 +010066 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050067 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070068 dprintk("%s: reply fsinfo: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010069 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050070 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
71 msg.rpc_resp = info->fattr;
72 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070073 dprintk("%s: reply getattr: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010074 }
75 return status;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
David Howells54ceac42006-08-22 20:06:13 -040079 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81static int
82nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
83 struct nfs_fsinfo *info)
84{
85 int status;
86
J. Bruce Fields03c21732006-01-03 09:55:48 +010087 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040088 if (status && server->nfs_client->cl_rpcclient != server->client)
89 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return status;
91}
92
93/*
94 * One function for each procedure in the NFS protocol.
95 */
96static int
97nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
98 struct nfs_fattr *fattr)
99{
Chuck Leverdead28d2006-03-20 13:44:23 -0500100 struct rpc_message msg = {
101 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
102 .rpc_argp = fhandle,
103 .rpc_resp = fattr,
104 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int status;
106
107 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400108 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500109 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 dprintk("NFS reply getattr: %d\n", status);
111 return status;
112}
113
114static int
115nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
116 struct iattr *sattr)
117{
118 struct inode *inode = dentry->d_inode;
119 struct nfs3_sattrargs arg = {
120 .fh = NFS_FH(inode),
121 .sattr = sattr,
122 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500123 struct rpc_message msg = {
124 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
125 .rpc_argp = &arg,
126 .rpc_resp = fattr,
127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int status;
129
130 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400131 if (sattr->ia_valid & ATTR_FILE)
132 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400133 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500134 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400135 if (status == 0)
136 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dprintk("NFS reply setattr: %d\n", status);
138 return status;
139}
140
141static int
142nfs3_proc_lookup(struct inode *dir, struct qstr *name,
143 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
144{
145 struct nfs_fattr dir_attr;
146 struct nfs3_diropargs arg = {
147 .fh = NFS_FH(dir),
148 .name = name->name,
149 .len = name->len
150 };
151 struct nfs3_diropres res = {
152 .dir_attr = &dir_attr,
153 .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 Myklebust0e574af2005-10-27 22:12:38 -0400164 nfs_fattr_init(&dir_attr);
165 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500166 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust81c76882007-10-02 10:30:00 -0400167 nfs_refresh_inode(dir, &dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500168 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
169 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
170 msg.rpc_argp = fhandle;
171 msg.rpc_resp = fattr;
172 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return status;
176}
177
178static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
179{
180 struct nfs_fattr fattr;
181 struct nfs3_accessargs arg = {
182 .fh = NFS_FH(inode),
183 };
184 struct nfs3_accessres res = {
185 .fattr = &fattr,
186 };
187 struct rpc_message msg = {
188 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
189 .rpc_argp = &arg,
190 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500191 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 };
193 int mode = entry->mask;
194 int status;
195
196 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (mode & MAY_READ)
199 arg.access |= NFS3_ACCESS_READ;
200 if (S_ISDIR(inode->i_mode)) {
201 if (mode & MAY_WRITE)
202 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
203 if (mode & MAY_EXEC)
204 arg.access |= NFS3_ACCESS_LOOKUP;
205 } else {
206 if (mode & MAY_WRITE)
207 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
208 if (mode & MAY_EXEC)
209 arg.access |= NFS3_ACCESS_EXECUTE;
210 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400211 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
213 nfs_refresh_inode(inode, &fattr);
214 if (status == 0) {
215 entry->mask = 0;
216 if (res.access & NFS3_ACCESS_READ)
217 entry->mask |= MAY_READ;
218 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
219 entry->mask |= MAY_WRITE;
220 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
221 entry->mask |= MAY_EXEC;
222 }
223 dprintk("NFS reply access: %d\n", status);
224 return status;
225}
226
227static int nfs3_proc_readlink(struct inode *inode, struct page *page,
228 unsigned int pgbase, unsigned int pglen)
229{
230 struct nfs_fattr fattr;
231 struct nfs3_readlinkargs args = {
232 .fh = NFS_FH(inode),
233 .pgbase = pgbase,
234 .pglen = pglen,
235 .pages = &page
236 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500237 struct rpc_message msg = {
238 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
239 .rpc_argp = &args,
240 .rpc_resp = &fattr,
241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int status;
243
244 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400245 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500246 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 nfs_refresh_inode(inode, &fattr);
248 dprintk("NFS reply readlink: %d\n", status);
249 return status;
250}
251
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400252struct nfs3_createdata {
253 struct rpc_message msg;
254 union {
255 struct nfs3_createargs create;
256 struct nfs3_mkdirargs mkdir;
257 struct nfs3_symlinkargs symlink;
258 struct nfs3_mknodargs mknod;
259 } arg;
260 struct nfs3_diropres res;
261 struct nfs_fh fh;
262 struct nfs_fattr fattr;
263 struct nfs_fattr dir_attr;
264};
265
266static struct nfs3_createdata *nfs3_alloc_createdata(void)
267{
268 struct nfs3_createdata *data;
269
270 data = kzalloc(sizeof(*data), GFP_KERNEL);
271 if (data != NULL) {
272 data->msg.rpc_argp = &data->arg;
273 data->msg.rpc_resp = &data->res;
274 data->res.fh = &data->fh;
275 data->res.fattr = &data->fattr;
276 data->res.dir_attr = &data->dir_attr;
277 nfs_fattr_init(data->res.fattr);
278 nfs_fattr_init(data->res.dir_attr);
279 }
280 return data;
281}
282
283static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
284{
285 int status;
286
287 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
288 nfs_post_op_update_inode(dir, data->res.dir_attr);
289 if (status == 0)
290 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
291 return status;
292}
293
294static void nfs3_free_createdata(struct nfs3_createdata *data)
295{
296 kfree(data);
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
302static int
303nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700304 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400306 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000307 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400308 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 dprintk("NFS call create %s\n", dentry->d_name.name);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400311
312 data = nfs3_alloc_createdata();
313 if (data == NULL)
314 goto out;
315
316 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
317 data->arg.create.fh = NFS_FH(dir);
318 data->arg.create.name = dentry->d_name.name;
319 data->arg.create.len = dentry->d_name.len;
320 data->arg.create.sattr = sattr;
321
322 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400324 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
325 data->arg.create.verifier[0] = jiffies;
326 data->arg.create.verifier[1] = current->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Al Viroce3b0f82009-03-29 19:08:22 -0400329 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000330
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400331 for (;;) {
332 status = nfs3_do_create(dir, dentry, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400334 if (status != -ENOTSUPP)
335 break;
336 /* If the server doesn't support the exclusive creation
337 * semantics, try again with simple 'guarded' mode. */
338 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400340 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342
343 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400344 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
346
347 case NFS3_CREATE_UNCHECKED:
348 goto out;
349 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400350 nfs_fattr_init(data->res.dir_attr);
351 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (status != 0)
355 goto out;
356
357 /* When we created the file with exclusive semantics, make
358 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400359 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dprintk("NFS call setattr (post-create)\n");
361
362 if (!(sattr->ia_valid & ATTR_ATIME_SET))
363 sattr->ia_valid |= ATTR_ATIME;
364 if (!(sattr->ia_valid & ATTR_MTIME_SET))
365 sattr->ia_valid |= ATTR_MTIME;
366
367 /* Note: we could use a guarded setattr here, but I'm
368 * not sure this buys us anything (and I'd have
369 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400370 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
371 nfs_post_op_update_inode(dentry->d_inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400373 if (status != 0)
374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000376 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400378 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dprintk("NFS reply create: %d\n", status);
380 return status;
381}
382
383static int
384nfs3_proc_remove(struct inode *dir, struct qstr *name)
385{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400386 struct nfs_removeargs arg = {
387 .fh = NFS_FH(dir),
388 .name.len = name->len,
389 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400391 struct nfs_removeres res;
392 struct rpc_message msg = {
393 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
394 .rpc_argp = &arg,
395 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 };
397 int status;
398
399 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400400 nfs_fattr_init(&res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400402 nfs_post_op_update_inode(dir, &res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dprintk("NFS reply remove: %d\n", status);
404 return status;
405}
406
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400407static void
408nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400414nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400416 struct nfs_removeres *res;
417 if (nfs3_async_handle_jukebox(task, dir))
418 return 0;
419 res = task->tk_msg.rpc_resp;
420 nfs_post_op_update_inode(dir, &res->dir_attr);
421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424static int
425nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
426 struct inode *new_dir, struct qstr *new_name)
427{
428 struct nfs_fattr old_dir_attr, new_dir_attr;
429 struct nfs3_renameargs arg = {
430 .fromfh = NFS_FH(old_dir),
431 .fromname = old_name->name,
432 .fromlen = old_name->len,
433 .tofh = NFS_FH(new_dir),
434 .toname = new_name->name,
435 .tolen = new_name->len
436 };
437 struct nfs3_renameres res = {
438 .fromattr = &old_dir_attr,
439 .toattr = &new_dir_attr
440 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500441 struct rpc_message msg = {
442 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
443 .rpc_argp = &arg,
444 .rpc_resp = &res,
445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int status;
447
448 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400449 nfs_fattr_init(&old_dir_attr);
450 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500451 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400452 nfs_post_op_update_inode(old_dir, &old_dir_attr);
453 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 dprintk("NFS reply rename: %d\n", status);
455 return status;
456}
457
458static int
459nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
460{
461 struct nfs_fattr dir_attr, fattr;
462 struct nfs3_linkargs arg = {
463 .fromfh = NFS_FH(inode),
464 .tofh = NFS_FH(dir),
465 .toname = name->name,
466 .tolen = name->len
467 };
468 struct nfs3_linkres res = {
469 .dir_attr = &dir_attr,
470 .fattr = &fattr
471 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500472 struct rpc_message msg = {
473 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
474 .rpc_argp = &arg,
475 .rpc_resp = &res,
476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int status;
478
479 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400480 nfs_fattr_init(&dir_attr);
481 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500482 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400483 nfs_post_op_update_inode(dir, &dir_attr);
484 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dprintk("NFS reply link: %d\n", status);
486 return status;
487}
488
489static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400490nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
491 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400493 struct nfs3_createdata *data;
494 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Chuck Lever94a6d752006-08-22 20:06:23 -0400496 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400498
Chuck Lever94a6d752006-08-22 20:06:23 -0400499 dprintk("NFS call symlink %s\n", dentry->d_name.name);
500
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400501 data = nfs3_alloc_createdata();
502 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400503 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400504 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
505 data->arg.symlink.fromfh = NFS_FH(dir);
506 data->arg.symlink.fromname = dentry->d_name.name;
507 data->arg.symlink.fromlen = dentry->d_name.len;
508 data->arg.symlink.pages = &page;
509 data->arg.symlink.pathlen = len;
510 data->arg.symlink.sattr = sattr;
511
512 status = nfs3_do_create(dir, dentry, data);
513
514 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400515out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 dprintk("NFS reply symlink: %d\n", status);
517 return status;
518}
519
520static int
521nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
522{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400523 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000524 int mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400525 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000528
Al Viroce3b0f82009-03-29 19:08:22 -0400529 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000530
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400531 data = nfs3_alloc_createdata();
532 if (data == NULL)
533 goto out;
534
535 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
536 data->arg.mkdir.fh = NFS_FH(dir);
537 data->arg.mkdir.name = dentry->d_name.name;
538 data->arg.mkdir.len = dentry->d_name.len;
539 data->arg.mkdir.sattr = sattr;
540
541 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000542 if (status != 0)
543 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400544
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000545 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
546out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400547 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dprintk("NFS reply mkdir: %d\n", status);
549 return status;
550}
551
552static int
553nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
554{
555 struct nfs_fattr dir_attr;
556 struct nfs3_diropargs arg = {
557 .fh = NFS_FH(dir),
558 .name = name->name,
559 .len = name->len
560 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500561 struct rpc_message msg = {
562 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
563 .rpc_argp = &arg,
564 .rpc_resp = &dir_attr,
565 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int status;
567
568 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400569 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500570 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400571 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dprintk("NFS reply rmdir: %d\n", status);
573 return status;
574}
575
576/*
577 * The READDIR implementation is somewhat hackish - we pass the user buffer
578 * to the encode function, which installs it in the receive iovec.
579 * The decode function itself doesn't perform any decoding, it just makes
580 * sure the reply is syntactically correct.
581 *
582 * Also note that this implementation handles both plain readdir and
583 * readdirplus.
584 */
585static int
586nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
587 u64 cookie, struct page *page, unsigned int count, int plus)
588{
589 struct inode *dir = dentry->d_inode;
590 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700591 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct nfs3_readdirargs arg = {
593 .fh = NFS_FH(dir),
594 .cookie = cookie,
595 .verf = {verf[0], verf[1]},
596 .plus = plus,
597 .count = count,
598 .pages = &page
599 };
600 struct nfs3_readdirres res = {
601 .dir_attr = &dir_attr,
602 .verf = verf,
603 .plus = plus
604 };
605 struct rpc_message msg = {
606 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
607 .rpc_argp = &arg,
608 .rpc_resp = &res,
609 .rpc_cred = cred
610 };
611 int status;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (plus)
614 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
615
616 dprintk("NFS call readdir%s %d\n",
617 plus? "plus" : "", (unsigned int) cookie);
618
Trond Myklebust0e574af2005-10-27 22:12:38 -0400619 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400621
622 nfs_invalidate_atime(dir);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 nfs_refresh_inode(dir, &dir_attr);
625 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return status;
627}
628
629static int
630nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
631 dev_t rdev)
632{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400633 struct nfs3_createdata *data;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000634 mode_t mode = sattr->ia_mode;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400635 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
638 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000639
Al Viroce3b0f82009-03-29 19:08:22 -0400640 sattr->ia_mode &= ~current_umask();
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000641
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400642 data = nfs3_alloc_createdata();
643 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000644 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400645
646 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
647 data->arg.mknod.fh = NFS_FH(dir);
648 data->arg.mknod.name = dentry->d_name.name;
649 data->arg.mknod.len = dentry->d_name.len;
650 data->arg.mknod.sattr = sattr;
651 data->arg.mknod.rdev = rdev;
652
653 switch (sattr->ia_mode & S_IFMT) {
654 case S_IFBLK:
655 data->arg.mknod.type = NF3BLK;
656 break;
657 case S_IFCHR:
658 data->arg.mknod.type = NF3CHR;
659 break;
660 case S_IFIFO:
661 data->arg.mknod.type = NF3FIFO;
662 break;
663 case S_IFSOCK:
664 data->arg.mknod.type = NF3SOCK;
665 break;
666 default:
667 status = -EINVAL;
668 goto out;
669 }
670
671 status = nfs3_do_create(dir, dentry, data);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000672 if (status != 0)
673 goto out;
674 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
675out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400676 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 dprintk("NFS reply mknod: %d\n", status);
678 return status;
679}
680
681static int
682nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
683 struct nfs_fsstat *stat)
684{
Chuck Leverdead28d2006-03-20 13:44:23 -0500685 struct rpc_message msg = {
686 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
687 .rpc_argp = fhandle,
688 .rpc_resp = stat,
689 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int status;
691
692 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400693 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500694 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 dprintk("NFS reply statfs: %d\n", status);
696 return status;
697}
698
699static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400700do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct nfs_fsinfo *info)
702{
Chuck Leverdead28d2006-03-20 13:44:23 -0500703 struct rpc_message msg = {
704 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
705 .rpc_argp = fhandle,
706 .rpc_resp = info,
707 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int status;
709
710 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400711 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400712 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 dprintk("NFS reply fsinfo: %d\n", status);
714 return status;
715}
716
EG Keizer37ca8f52008-08-19 16:34:36 -0400717/*
718 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
719 * nfs_create_server
720 */
721static int
722nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
723 struct nfs_fsinfo *info)
724{
725 int status;
726
727 status = do_proc_fsinfo(server->client, fhandle, info);
728 if (status && server->nfs_client->cl_rpcclient != server->client)
729 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
730 return status;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int
734nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
735 struct nfs_pathconf *info)
736{
Chuck Leverdead28d2006-03-20 13:44:23 -0500737 struct rpc_message msg = {
738 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
739 .rpc_argp = fhandle,
740 .rpc_resp = info,
741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 int status;
743
744 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400745 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500746 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dprintk("NFS reply pathconf: %d\n", status);
748 return status;
749}
750
Trond Myklebustec06c092006-03-20 13:44:27 -0500751static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Chuck Lever006ea732006-03-20 13:44:14 -0500753 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500754 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400755
756 nfs_invalidate_atime(data->inode);
757 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400761static void nfs3_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400763 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Trond Myklebust788e7a82006-03-20 13:44:27 -0500766static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Chuck Lever006ea732006-03-20 13:44:14 -0500768 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500769 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400771 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400775static void nfs3_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400777 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Trond Myklebust788e7a82006-03-20 13:44:27 -0500780static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Chuck Lever006ea732006-03-20 13:44:14 -0500782 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500783 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400784 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400788static void nfs3_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400790 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static int
794nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
795{
Chuck Lever1093a602008-01-11 17:09:59 -0500796 struct inode *inode = filp->f_path.dentry->d_inode;
797
798 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
David Howells509de812006-08-22 20:06:11 -0400801const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .version = 3, /* protocol version */
803 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000804 .dir_inode_ops = &nfs3_dir_inode_operations,
805 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .getroot = nfs3_proc_get_root,
807 .getattr = nfs3_proc_getattr,
808 .setattr = nfs3_proc_setattr,
809 .lookup = nfs3_proc_lookup,
810 .access = nfs3_proc_access,
811 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .create = nfs3_proc_create,
813 .remove = nfs3_proc_remove,
814 .unlink_setup = nfs3_proc_unlink_setup,
815 .unlink_done = nfs3_proc_unlink_done,
816 .rename = nfs3_proc_rename,
817 .link = nfs3_proc_link,
818 .symlink = nfs3_proc_symlink,
819 .mkdir = nfs3_proc_mkdir,
820 .rmdir = nfs3_proc_rmdir,
821 .readdir = nfs3_proc_readdir,
822 .mknod = nfs3_proc_mknod,
823 .statfs = nfs3_proc_statfs,
824 .fsinfo = nfs3_proc_fsinfo,
825 .pathconf = nfs3_proc_pathconf,
826 .decode_dirent = nfs3_decode_dirent,
827 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500828 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500830 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500832 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000834 .clear_acl_cache = nfs3_forget_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400835 .close_context = nfs_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836};