blob: 7143b1f82cea40980d9f5d0d63ed1a18db877a6d [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>
10#include <linux/utsname.h>
11#include <linux/errno.h>
12#include <linux/string.h>
13#include <linux/sunrpc/clnt.h>
14#include <linux/nfs.h>
15#include <linux/nfs3.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_page.h>
18#include <linux/lockd/bind.h>
19#include <linux/smp_lock.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000020#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Chuck Lever006ea732006-03-20 13:44:14 -050022#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040023#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define NFSDBG_FACILITY NFSDBG_PROC
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* A wrapper to handle the EJUKEBOX error message */
28static int
29nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
30{
31 sigset_t oldset;
32 int res;
33 rpc_clnt_sigmask(clnt, &oldset);
34 do {
35 res = rpc_call_sync(clnt, msg, flags);
36 if (res != -EJUKEBOX)
37 break;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -070038 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 res = -ERESTARTSYS;
40 } while (!signalled());
41 rpc_clnt_sigunmask(clnt, &oldset);
42 return res;
43}
44
Chuck Leverdead28d2006-03-20 13:44:23 -050045#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int
Chuck Lever006ea732006-03-20 13:44:14 -050048nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 if (task->tk_status != -EJUKEBOX)
51 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050052 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 task->tk_status = 0;
54 rpc_restart_call(task);
55 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
56 return 1;
57}
58
J. Bruce Fields03c21732006-01-03 09:55:48 +010059static int
60do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
61 struct nfs_fsinfo *info)
62{
Chuck Leverdead28d2006-03-20 13:44:23 -050063 struct rpc_message msg = {
64 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
65 .rpc_argp = fhandle,
66 .rpc_resp = info,
67 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010068 int status;
69
70 dprintk("%s: call fsinfo\n", __FUNCTION__);
71 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050072 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010073 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
74 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050075 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
76 msg.rpc_resp = info->fattr;
77 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010078 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
79 }
80 return status;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * Bare-bones access to getattr: this is for nfs_read_super.
85 */
86static int
87nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
88 struct nfs_fsinfo *info)
89{
90 int status;
91
J. Bruce Fields03c21732006-01-03 09:55:48 +010092 status = do_proc_get_root(server->client, fhandle, info);
93 if (status && server->client_sys != server->client)
94 status = do_proc_get_root(server->client_sys, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return status;
96}
97
98/*
99 * One function for each procedure in the NFS protocol.
100 */
101static int
102nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
103 struct nfs_fattr *fattr)
104{
Chuck Leverdead28d2006-03-20 13:44:23 -0500105 struct rpc_message msg = {
106 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
107 .rpc_argp = fhandle,
108 .rpc_resp = fattr,
109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int status;
111
112 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400113 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500114 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dprintk("NFS reply getattr: %d\n", status);
116 return status;
117}
118
119static int
120nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
121 struct iattr *sattr)
122{
123 struct inode *inode = dentry->d_inode;
124 struct nfs3_sattrargs arg = {
125 .fh = NFS_FH(inode),
126 .sattr = sattr,
127 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500128 struct rpc_message msg = {
129 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
130 .rpc_argp = &arg,
131 .rpc_resp = fattr,
132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int status;
134
135 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400136 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500137 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400138 if (status == 0)
139 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dprintk("NFS reply setattr: %d\n", status);
141 return status;
142}
143
144static int
145nfs3_proc_lookup(struct inode *dir, struct qstr *name,
146 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
147{
148 struct nfs_fattr dir_attr;
149 struct nfs3_diropargs arg = {
150 .fh = NFS_FH(dir),
151 .name = name->name,
152 .len = name->len
153 };
154 struct nfs3_diropres res = {
155 .dir_attr = &dir_attr,
156 .fh = fhandle,
157 .fattr = fattr
158 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500159 struct rpc_message msg = {
160 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
161 .rpc_argp = &arg,
162 .rpc_resp = &res,
163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int status;
165
166 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400167 nfs_fattr_init(&dir_attr);
168 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
170 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
171 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
172 msg.rpc_argp = fhandle;
173 msg.rpc_resp = fattr;
174 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dprintk("NFS reply lookup: %d\n", status);
177 if (status >= 0)
178 status = nfs_refresh_inode(dir, &dir_attr);
179 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
256static int nfs3_proc_read(struct nfs_read_data *rdata)
257{
258 int flags = rdata->flags;
259 struct inode * inode = rdata->inode;
260 struct nfs_fattr * fattr = rdata->res.fattr;
261 struct rpc_message msg = {
262 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
263 .rpc_argp = &rdata->args,
264 .rpc_resp = &rdata->res,
265 .rpc_cred = rdata->cred,
266 };
267 int status;
268
269 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
270 (long long) rdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400271 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
273 if (status >= 0)
274 nfs_refresh_inode(inode, fattr);
275 dprintk("NFS reply read: %d\n", status);
276 return status;
277}
278
279static int nfs3_proc_write(struct nfs_write_data *wdata)
280{
281 int rpcflags = wdata->flags;
282 struct inode * inode = wdata->inode;
283 struct nfs_fattr * fattr = wdata->res.fattr;
284 struct rpc_message msg = {
285 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
286 .rpc_argp = &wdata->args,
287 .rpc_resp = &wdata->res,
288 .rpc_cred = wdata->cred,
289 };
290 int status;
291
292 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
293 (long long) wdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400294 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
296 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400297 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 dprintk("NFS reply write: %d\n", status);
299 return status < 0? status : wdata->res.count;
300}
301
302static int nfs3_proc_commit(struct nfs_write_data *cdata)
303{
304 struct inode * inode = cdata->inode;
305 struct nfs_fattr * fattr = cdata->res.fattr;
306 struct rpc_message msg = {
307 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
308 .rpc_argp = &cdata->args,
309 .rpc_resp = &cdata->res,
310 .rpc_cred = cdata->cred,
311 };
312 int status;
313
314 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
315 (long long) cdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400316 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
318 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400319 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 dprintk("NFS reply commit: %d\n", status);
321 return status;
322}
323
324/*
325 * Create a regular file.
326 * For now, we don't implement O_EXCL.
327 */
328static int
329nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700330 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct nfs_fh fhandle;
333 struct nfs_fattr fattr;
334 struct nfs_fattr dir_attr;
335 struct nfs3_createargs arg = {
336 .fh = NFS_FH(dir),
337 .name = dentry->d_name.name,
338 .len = dentry->d_name.len,
339 .sattr = sattr,
340 };
341 struct nfs3_diropres res = {
342 .dir_attr = &dir_attr,
343 .fh = &fhandle,
344 .fattr = &fattr
345 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500346 struct rpc_message msg = {
347 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
348 .rpc_argp = &arg,
349 .rpc_resp = &res,
350 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000351 mode_t mode = sattr->ia_mode;
352 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 dprintk("NFS call create %s\n", dentry->d_name.name);
355 arg.createmode = NFS3_CREATE_UNCHECKED;
356 if (flags & O_EXCL) {
357 arg.createmode = NFS3_CREATE_EXCLUSIVE;
358 arg.verifier[0] = jiffies;
359 arg.verifier[1] = current->pid;
360 }
361
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000362 sattr->ia_mode &= ~current->fs->umask;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400365 nfs_fattr_init(&dir_attr);
366 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500367 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
368 nfs_refresh_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* If the server doesn't support the exclusive creation semantics,
371 * try again with simple 'guarded' mode. */
372 if (status == NFSERR_NOTSUPP) {
373 switch (arg.createmode) {
374 case NFS3_CREATE_EXCLUSIVE:
375 arg.createmode = NFS3_CREATE_GUARDED;
376 break;
377
378 case NFS3_CREATE_GUARDED:
379 arg.createmode = NFS3_CREATE_UNCHECKED;
380 break;
381
382 case NFS3_CREATE_UNCHECKED:
383 goto out;
384 }
385 goto again;
386 }
387
388 if (status == 0)
389 status = nfs_instantiate(dentry, &fhandle, &fattr);
390 if (status != 0)
391 goto out;
392
393 /* When we created the file with exclusive semantics, make
394 * sure we set the attributes afterwards. */
395 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
396 dprintk("NFS call setattr (post-create)\n");
397
398 if (!(sattr->ia_valid & ATTR_ATIME_SET))
399 sattr->ia_valid |= ATTR_ATIME;
400 if (!(sattr->ia_valid & ATTR_MTIME_SET))
401 sattr->ia_valid |= ATTR_MTIME;
402
403 /* Note: we could use a guarded setattr here, but I'm
404 * not sure this buys us anything (and I'd have
405 * to revamp the NFSv3 XDR code) */
406 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400407 if (status == 0)
408 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 nfs_refresh_inode(dentry->d_inode, &fattr);
410 dprintk("NFS reply setattr (post-create): %d\n", status);
411 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000412 if (status != 0)
413 goto out;
414 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415out:
416 dprintk("NFS reply create: %d\n", status);
417 return status;
418}
419
420static int
421nfs3_proc_remove(struct inode *dir, struct qstr *name)
422{
423 struct nfs_fattr dir_attr;
424 struct nfs3_diropargs arg = {
425 .fh = NFS_FH(dir),
426 .name = name->name,
427 .len = name->len
428 };
429 struct rpc_message msg = {
430 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
431 .rpc_argp = &arg,
432 .rpc_resp = &dir_attr,
433 };
434 int status;
435
436 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400437 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400439 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dprintk("NFS reply remove: %d\n", status);
441 return status;
442}
443
444static int
445nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
446{
447 struct unlinkxdr {
448 struct nfs3_diropargs arg;
449 struct nfs_fattr res;
450 } *ptr;
451
452 ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
453 if (!ptr)
454 return -ENOMEM;
455 ptr->arg.fh = NFS_FH(dir->d_inode);
456 ptr->arg.name = name->name;
457 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400458 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
460 msg->rpc_argp = &ptr->arg;
461 msg->rpc_resp = &ptr->res;
462 return 0;
463}
464
465static int
466nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
467{
468 struct rpc_message *msg = &task->tk_msg;
469 struct nfs_fattr *dir_attr;
470
Chuck Lever006ea732006-03-20 13:44:14 -0500471 if (nfs3_async_handle_jukebox(task, dir->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 1;
473 if (msg->rpc_argp) {
474 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400475 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 kfree(msg->rpc_argp);
477 }
478 return 0;
479}
480
481static int
482nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
483 struct inode *new_dir, struct qstr *new_name)
484{
485 struct nfs_fattr old_dir_attr, new_dir_attr;
486 struct nfs3_renameargs arg = {
487 .fromfh = NFS_FH(old_dir),
488 .fromname = old_name->name,
489 .fromlen = old_name->len,
490 .tofh = NFS_FH(new_dir),
491 .toname = new_name->name,
492 .tolen = new_name->len
493 };
494 struct nfs3_renameres res = {
495 .fromattr = &old_dir_attr,
496 .toattr = &new_dir_attr
497 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500498 struct rpc_message msg = {
499 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
500 .rpc_argp = &arg,
501 .rpc_resp = &res,
502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int status;
504
505 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400506 nfs_fattr_init(&old_dir_attr);
507 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500508 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400509 nfs_post_op_update_inode(old_dir, &old_dir_attr);
510 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 dprintk("NFS reply rename: %d\n", status);
512 return status;
513}
514
515static int
516nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
517{
518 struct nfs_fattr dir_attr, fattr;
519 struct nfs3_linkargs arg = {
520 .fromfh = NFS_FH(inode),
521 .tofh = NFS_FH(dir),
522 .toname = name->name,
523 .tolen = name->len
524 };
525 struct nfs3_linkres res = {
526 .dir_attr = &dir_attr,
527 .fattr = &fattr
528 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500529 struct rpc_message msg = {
530 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
531 .rpc_argp = &arg,
532 .rpc_resp = &res,
533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int status;
535
536 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400537 nfs_fattr_init(&dir_attr);
538 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500539 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400540 nfs_post_op_update_inode(dir, &dir_attr);
541 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 dprintk("NFS reply link: %d\n", status);
543 return status;
544}
545
546static int
547nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
548 struct iattr *sattr, struct nfs_fh *fhandle,
549 struct nfs_fattr *fattr)
550{
551 struct nfs_fattr dir_attr;
552 struct nfs3_symlinkargs arg = {
553 .fromfh = NFS_FH(dir),
554 .fromname = name->name,
555 .fromlen = name->len,
556 .topath = path->name,
557 .tolen = path->len,
558 .sattr = sattr
559 };
560 struct nfs3_diropres res = {
561 .dir_attr = &dir_attr,
562 .fh = fhandle,
563 .fattr = fattr
564 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500565 struct rpc_message msg = {
566 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
567 .rpc_argp = &arg,
568 .rpc_resp = &res,
569 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int status;
571
572 if (path->len > NFS3_MAXPATHLEN)
573 return -ENAMETOOLONG;
574 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400575 nfs_fattr_init(&dir_attr);
576 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500577 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400578 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 dprintk("NFS reply symlink: %d\n", status);
580 return status;
581}
582
583static int
584nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
585{
586 struct nfs_fh fhandle;
587 struct nfs_fattr fattr, dir_attr;
588 struct nfs3_mkdirargs arg = {
589 .fh = NFS_FH(dir),
590 .name = dentry->d_name.name,
591 .len = dentry->d_name.len,
592 .sattr = sattr
593 };
594 struct nfs3_diropres res = {
595 .dir_attr = &dir_attr,
596 .fh = &fhandle,
597 .fattr = &fattr
598 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500599 struct rpc_message msg = {
600 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
601 .rpc_argp = &arg,
602 .rpc_resp = &res,
603 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000604 int mode = sattr->ia_mode;
605 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000608
609 sattr->ia_mode &= ~current->fs->umask;
610
Trond Myklebust0e574af2005-10-27 22:12:38 -0400611 nfs_fattr_init(&dir_attr);
612 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500613 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400614 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000615 if (status != 0)
616 goto out;
617 status = nfs_instantiate(dentry, &fhandle, &fattr);
618 if (status != 0)
619 goto out;
620 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
621out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dprintk("NFS reply mkdir: %d\n", status);
623 return status;
624}
625
626static int
627nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
628{
629 struct nfs_fattr dir_attr;
630 struct nfs3_diropargs arg = {
631 .fh = NFS_FH(dir),
632 .name = name->name,
633 .len = name->len
634 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500635 struct rpc_message msg = {
636 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
637 .rpc_argp = &arg,
638 .rpc_resp = &dir_attr,
639 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int status;
641
642 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400643 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500644 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400645 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dprintk("NFS reply rmdir: %d\n", status);
647 return status;
648}
649
650/*
651 * The READDIR implementation is somewhat hackish - we pass the user buffer
652 * to the encode function, which installs it in the receive iovec.
653 * The decode function itself doesn't perform any decoding, it just makes
654 * sure the reply is syntactically correct.
655 *
656 * Also note that this implementation handles both plain readdir and
657 * readdirplus.
658 */
659static int
660nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
661 u64 cookie, struct page *page, unsigned int count, int plus)
662{
663 struct inode *dir = dentry->d_inode;
664 struct nfs_fattr dir_attr;
665 u32 *verf = NFS_COOKIEVERF(dir);
666 struct nfs3_readdirargs arg = {
667 .fh = NFS_FH(dir),
668 .cookie = cookie,
669 .verf = {verf[0], verf[1]},
670 .plus = plus,
671 .count = count,
672 .pages = &page
673 };
674 struct nfs3_readdirres res = {
675 .dir_attr = &dir_attr,
676 .verf = verf,
677 .plus = plus
678 };
679 struct rpc_message msg = {
680 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
681 .rpc_argp = &arg,
682 .rpc_resp = &res,
683 .rpc_cred = cred
684 };
685 int status;
686
687 lock_kernel();
688
689 if (plus)
690 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
691
692 dprintk("NFS call readdir%s %d\n",
693 plus? "plus" : "", (unsigned int) cookie);
694
Trond Myklebust0e574af2005-10-27 22:12:38 -0400695 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
697 nfs_refresh_inode(dir, &dir_attr);
698 dprintk("NFS reply readdir: %d\n", status);
699 unlock_kernel();
700 return status;
701}
702
703static int
704nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
705 dev_t rdev)
706{
707 struct nfs_fh fh;
708 struct nfs_fattr fattr, dir_attr;
709 struct nfs3_mknodargs arg = {
710 .fh = NFS_FH(dir),
711 .name = dentry->d_name.name,
712 .len = dentry->d_name.len,
713 .sattr = sattr,
714 .rdev = rdev
715 };
716 struct nfs3_diropres res = {
717 .dir_attr = &dir_attr,
718 .fh = &fh,
719 .fattr = &fattr
720 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500721 struct rpc_message msg = {
722 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
723 .rpc_argp = &arg,
724 .rpc_resp = &res,
725 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000726 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int status;
728
729 switch (sattr->ia_mode & S_IFMT) {
730 case S_IFBLK: arg.type = NF3BLK; break;
731 case S_IFCHR: arg.type = NF3CHR; break;
732 case S_IFIFO: arg.type = NF3FIFO; break;
733 case S_IFSOCK: arg.type = NF3SOCK; break;
734 default: return -EINVAL;
735 }
736
737 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
738 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000739
740 sattr->ia_mode &= ~current->fs->umask;
741
Trond Myklebust0e574af2005-10-27 22:12:38 -0400742 nfs_fattr_init(&dir_attr);
743 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500744 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400745 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000746 if (status != 0)
747 goto out;
748 status = nfs_instantiate(dentry, &fh, &fattr);
749 if (status != 0)
750 goto out;
751 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
752out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 dprintk("NFS reply mknod: %d\n", status);
754 return status;
755}
756
757static int
758nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
759 struct nfs_fsstat *stat)
760{
Chuck Leverdead28d2006-03-20 13:44:23 -0500761 struct rpc_message msg = {
762 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
763 .rpc_argp = fhandle,
764 .rpc_resp = stat,
765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int status;
767
768 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400769 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500770 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dprintk("NFS reply statfs: %d\n", status);
772 return status;
773}
774
775static int
776nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
777 struct nfs_fsinfo *info)
778{
Chuck Leverdead28d2006-03-20 13:44:23 -0500779 struct rpc_message msg = {
780 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
781 .rpc_argp = fhandle,
782 .rpc_resp = info,
783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int status;
785
786 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400787 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500788 status = rpc_call_sync(server->client_sys, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dprintk("NFS reply fsinfo: %d\n", status);
790 return status;
791}
792
793static int
794nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
795 struct nfs_pathconf *info)
796{
Chuck Leverdead28d2006-03-20 13:44:23 -0500797 struct rpc_message msg = {
798 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
799 .rpc_argp = fhandle,
800 .rpc_resp = info,
801 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int status;
803
804 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400805 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500806 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 dprintk("NFS reply pathconf: %d\n", status);
808 return status;
809}
810
Trond Myklebustec06c092006-03-20 13:44:27 -0500811static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Chuck Lever006ea732006-03-20 13:44:14 -0500813 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500814 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Call back common NFS readpage processing */
816 if (task->tk_status >= 0)
817 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Trond Myklebustec06c092006-03-20 13:44:27 -0500821static void nfs3_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct rpc_message msg = {
824 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
825 .rpc_argp = &data->args,
826 .rpc_resp = &data->res,
827 .rpc_cred = data->cred,
828 };
829
Trond Myklebustec06c092006-03-20 13:44:27 -0500830 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Trond Myklebust788e7a82006-03-20 13:44:27 -0500833static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Chuck Lever006ea732006-03-20 13:44:14 -0500835 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500836 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400838 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Trond Myklebust788e7a82006-03-20 13:44:27 -0500842static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct rpc_message msg = {
845 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
846 .rpc_argp = &data->args,
847 .rpc_resp = &data->res,
848 .rpc_cred = data->cred,
849 };
850
Trond Myklebust788e7a82006-03-20 13:44:27 -0500851 data->args.stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (how & FLUSH_STABLE) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500853 data->args.stable = NFS_FILE_SYNC;
854 if (NFS_I(data->inode)->ncommit)
855 data->args.stable = NFS_DATA_SYNC;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500859 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Trond Myklebust788e7a82006-03-20 13:44:27 -0500862static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Chuck Lever006ea732006-03-20 13:44:14 -0500864 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500865 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400867 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Trond Myklebust788e7a82006-03-20 13:44:27 -0500871static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct rpc_message msg = {
874 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
875 .rpc_argp = &data->args,
876 .rpc_resp = &data->res,
877 .rpc_cred = data->cred,
878 };
879
Trond Myklebust788e7a82006-03-20 13:44:27 -0500880 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static int
884nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
885{
886 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
887}
888
889struct nfs_rpc_ops nfs_v3_clientops = {
890 .version = 3, /* protocol version */
891 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000892 .dir_inode_ops = &nfs3_dir_inode_operations,
893 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .getroot = nfs3_proc_get_root,
895 .getattr = nfs3_proc_getattr,
896 .setattr = nfs3_proc_setattr,
897 .lookup = nfs3_proc_lookup,
898 .access = nfs3_proc_access,
899 .readlink = nfs3_proc_readlink,
900 .read = nfs3_proc_read,
901 .write = nfs3_proc_write,
902 .commit = nfs3_proc_commit,
903 .create = nfs3_proc_create,
904 .remove = nfs3_proc_remove,
905 .unlink_setup = nfs3_proc_unlink_setup,
906 .unlink_done = nfs3_proc_unlink_done,
907 .rename = nfs3_proc_rename,
908 .link = nfs3_proc_link,
909 .symlink = nfs3_proc_symlink,
910 .mkdir = nfs3_proc_mkdir,
911 .rmdir = nfs3_proc_rmdir,
912 .readdir = nfs3_proc_readdir,
913 .mknod = nfs3_proc_mknod,
914 .statfs = nfs3_proc_statfs,
915 .fsinfo = nfs3_proc_fsinfo,
916 .pathconf = nfs3_proc_pathconf,
917 .decode_dirent = nfs3_decode_dirent,
918 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500919 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500921 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500923 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 .file_open = nfs_open,
925 .file_release = nfs_release,
926 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000927 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928};