blob: 7204ba5b2bf84ab38952524eeaa8f4b4e081177b [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"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NFSDBG_FACILITY NFSDBG_PROC
25
26extern struct rpc_procinfo nfs3_procedures[];
27
28/* A wrapper to handle the EJUKEBOX error message */
29static int
30nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
31{
32 sigset_t oldset;
33 int res;
34 rpc_clnt_sigmask(clnt, &oldset);
35 do {
36 res = rpc_call_sync(clnt, msg, flags);
37 if (res != -EJUKEBOX)
38 break;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -070039 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 res = -ERESTARTSYS;
41 } while (!signalled());
42 rpc_clnt_sigunmask(clnt, &oldset);
43 return res;
44}
45
46static inline int
47nfs3_rpc_call_wrapper(struct rpc_clnt *clnt, u32 proc, void *argp, void *resp, int flags)
48{
49 struct rpc_message msg = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000050 .rpc_proc = &clnt->cl_procinfo[proc],
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .rpc_argp = argp,
52 .rpc_resp = resp,
53 };
54 return nfs3_rpc_wrapper(clnt, &msg, flags);
55}
56
57#define rpc_call(clnt, proc, argp, resp, flags) \
58 nfs3_rpc_call_wrapper(clnt, proc, argp, resp, flags)
59#define rpc_call_sync(clnt, msg, flags) \
60 nfs3_rpc_wrapper(clnt, msg, flags)
61
62static int
Chuck Lever006ea732006-03-20 13:44:14 -050063nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 if (task->tk_status != -EJUKEBOX)
66 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050067 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 task->tk_status = 0;
69 rpc_restart_call(task);
70 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
71 return 1;
72}
73
J. Bruce Fields03c21732006-01-03 09:55:48 +010074static int
75do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
76 struct nfs_fsinfo *info)
77{
78 int status;
79
80 dprintk("%s: call fsinfo\n", __FUNCTION__);
81 nfs_fattr_init(info->fattr);
82 status = rpc_call(client, NFS3PROC_FSINFO, fhandle, info, 0);
83 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
84 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
85 status = rpc_call(client, NFS3PROC_GETATTR, fhandle, info->fattr, 0);
86 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
87 }
88 return status;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Bare-bones access to getattr: this is for nfs_read_super.
93 */
94static int
95nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
96 struct nfs_fsinfo *info)
97{
98 int status;
99
J. Bruce Fields03c21732006-01-03 09:55:48 +0100100 status = do_proc_get_root(server->client, fhandle, info);
101 if (status && server->client_sys != server->client)
102 status = do_proc_get_root(server->client_sys, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return status;
104}
105
106/*
107 * One function for each procedure in the NFS protocol.
108 */
109static int
110nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
111 struct nfs_fattr *fattr)
112{
113 int status;
114
115 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400116 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 status = rpc_call(server->client, NFS3PROC_GETATTR,
118 fhandle, fattr, 0);
119 dprintk("NFS reply getattr: %d\n", status);
120 return status;
121}
122
123static int
124nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
125 struct iattr *sattr)
126{
127 struct inode *inode = dentry->d_inode;
128 struct nfs3_sattrargs arg = {
129 .fh = NFS_FH(inode),
130 .sattr = sattr,
131 };
132 int status;
133
134 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400135 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_SETATTR, &arg, fattr, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400137 if (status == 0)
138 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dprintk("NFS reply setattr: %d\n", status);
140 return status;
141}
142
143static int
144nfs3_proc_lookup(struct inode *dir, struct qstr *name,
145 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
146{
147 struct nfs_fattr dir_attr;
148 struct nfs3_diropargs arg = {
149 .fh = NFS_FH(dir),
150 .name = name->name,
151 .len = name->len
152 };
153 struct nfs3_diropres res = {
154 .dir_attr = &dir_attr,
155 .fh = fhandle,
156 .fattr = fattr
157 };
158 int status;
159
160 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400161 nfs_fattr_init(&dir_attr);
162 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_LOOKUP, &arg, &res, 0);
164 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
165 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_GETATTR,
166 fhandle, fattr, 0);
167 dprintk("NFS reply lookup: %d\n", status);
168 if (status >= 0)
169 status = nfs_refresh_inode(dir, &dir_attr);
170 return status;
171}
172
173static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
174{
175 struct nfs_fattr fattr;
176 struct nfs3_accessargs arg = {
177 .fh = NFS_FH(inode),
178 };
179 struct nfs3_accessres res = {
180 .fattr = &fattr,
181 };
182 struct rpc_message msg = {
183 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
184 .rpc_argp = &arg,
185 .rpc_resp = &res,
186 .rpc_cred = entry->cred
187 };
188 int mode = entry->mask;
189 int status;
190
191 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (mode & MAY_READ)
194 arg.access |= NFS3_ACCESS_READ;
195 if (S_ISDIR(inode->i_mode)) {
196 if (mode & MAY_WRITE)
197 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
198 if (mode & MAY_EXEC)
199 arg.access |= NFS3_ACCESS_LOOKUP;
200 } else {
201 if (mode & MAY_WRITE)
202 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
203 if (mode & MAY_EXEC)
204 arg.access |= NFS3_ACCESS_EXECUTE;
205 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400206 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
208 nfs_refresh_inode(inode, &fattr);
209 if (status == 0) {
210 entry->mask = 0;
211 if (res.access & NFS3_ACCESS_READ)
212 entry->mask |= MAY_READ;
213 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
214 entry->mask |= MAY_WRITE;
215 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
216 entry->mask |= MAY_EXEC;
217 }
218 dprintk("NFS reply access: %d\n", status);
219 return status;
220}
221
222static int nfs3_proc_readlink(struct inode *inode, struct page *page,
223 unsigned int pgbase, unsigned int pglen)
224{
225 struct nfs_fattr fattr;
226 struct nfs3_readlinkargs args = {
227 .fh = NFS_FH(inode),
228 .pgbase = pgbase,
229 .pglen = pglen,
230 .pages = &page
231 };
232 int status;
233
234 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400235 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_READLINK,
237 &args, &fattr, 0);
238 nfs_refresh_inode(inode, &fattr);
239 dprintk("NFS reply readlink: %d\n", status);
240 return status;
241}
242
243static int nfs3_proc_read(struct nfs_read_data *rdata)
244{
245 int flags = rdata->flags;
246 struct inode * inode = rdata->inode;
247 struct nfs_fattr * fattr = rdata->res.fattr;
248 struct rpc_message msg = {
249 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
250 .rpc_argp = &rdata->args,
251 .rpc_resp = &rdata->res,
252 .rpc_cred = rdata->cred,
253 };
254 int status;
255
256 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
257 (long long) rdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400258 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
260 if (status >= 0)
261 nfs_refresh_inode(inode, fattr);
262 dprintk("NFS reply read: %d\n", status);
263 return status;
264}
265
266static int nfs3_proc_write(struct nfs_write_data *wdata)
267{
268 int rpcflags = wdata->flags;
269 struct inode * inode = wdata->inode;
270 struct nfs_fattr * fattr = wdata->res.fattr;
271 struct rpc_message msg = {
272 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
273 .rpc_argp = &wdata->args,
274 .rpc_resp = &wdata->res,
275 .rpc_cred = wdata->cred,
276 };
277 int status;
278
279 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
280 (long long) wdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400281 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
283 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400284 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dprintk("NFS reply write: %d\n", status);
286 return status < 0? status : wdata->res.count;
287}
288
289static int nfs3_proc_commit(struct nfs_write_data *cdata)
290{
291 struct inode * inode = cdata->inode;
292 struct nfs_fattr * fattr = cdata->res.fattr;
293 struct rpc_message msg = {
294 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
295 .rpc_argp = &cdata->args,
296 .rpc_resp = &cdata->res,
297 .rpc_cred = cdata->cred,
298 };
299 int status;
300
301 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
302 (long long) cdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400303 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
305 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400306 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 dprintk("NFS reply commit: %d\n", status);
308 return status;
309}
310
311/*
312 * Create a regular file.
313 * For now, we don't implement O_EXCL.
314 */
315static int
316nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700317 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct nfs_fh fhandle;
320 struct nfs_fattr fattr;
321 struct nfs_fattr dir_attr;
322 struct nfs3_createargs arg = {
323 .fh = NFS_FH(dir),
324 .name = dentry->d_name.name,
325 .len = dentry->d_name.len,
326 .sattr = sattr,
327 };
328 struct nfs3_diropres res = {
329 .dir_attr = &dir_attr,
330 .fh = &fhandle,
331 .fattr = &fattr
332 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000333 mode_t mode = sattr->ia_mode;
334 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 dprintk("NFS call create %s\n", dentry->d_name.name);
337 arg.createmode = NFS3_CREATE_UNCHECKED;
338 if (flags & O_EXCL) {
339 arg.createmode = NFS3_CREATE_EXCLUSIVE;
340 arg.verifier[0] = jiffies;
341 arg.verifier[1] = current->pid;
342 }
343
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000344 sattr->ia_mode &= ~current->fs->umask;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400347 nfs_fattr_init(&dir_attr);
348 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_CREATE, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400350 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* If the server doesn't support the exclusive creation semantics,
353 * try again with simple 'guarded' mode. */
354 if (status == NFSERR_NOTSUPP) {
355 switch (arg.createmode) {
356 case NFS3_CREATE_EXCLUSIVE:
357 arg.createmode = NFS3_CREATE_GUARDED;
358 break;
359
360 case NFS3_CREATE_GUARDED:
361 arg.createmode = NFS3_CREATE_UNCHECKED;
362 break;
363
364 case NFS3_CREATE_UNCHECKED:
365 goto out;
366 }
367 goto again;
368 }
369
370 if (status == 0)
371 status = nfs_instantiate(dentry, &fhandle, &fattr);
372 if (status != 0)
373 goto out;
374
375 /* When we created the file with exclusive semantics, make
376 * sure we set the attributes afterwards. */
377 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
378 dprintk("NFS call setattr (post-create)\n");
379
380 if (!(sattr->ia_valid & ATTR_ATIME_SET))
381 sattr->ia_valid |= ATTR_ATIME;
382 if (!(sattr->ia_valid & ATTR_MTIME_SET))
383 sattr->ia_valid |= ATTR_MTIME;
384
385 /* Note: we could use a guarded setattr here, but I'm
386 * not sure this buys us anything (and I'd have
387 * to revamp the NFSv3 XDR code) */
388 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400389 if (status == 0)
390 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 nfs_refresh_inode(dentry->d_inode, &fattr);
392 dprintk("NFS reply setattr (post-create): %d\n", status);
393 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000394 if (status != 0)
395 goto out;
396 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397out:
398 dprintk("NFS reply create: %d\n", status);
399 return status;
400}
401
402static int
403nfs3_proc_remove(struct inode *dir, struct qstr *name)
404{
405 struct nfs_fattr dir_attr;
406 struct nfs3_diropargs arg = {
407 .fh = NFS_FH(dir),
408 .name = name->name,
409 .len = name->len
410 };
411 struct rpc_message msg = {
412 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
413 .rpc_argp = &arg,
414 .rpc_resp = &dir_attr,
415 };
416 int status;
417
418 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400419 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400421 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 dprintk("NFS reply remove: %d\n", status);
423 return status;
424}
425
426static int
427nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
428{
429 struct unlinkxdr {
430 struct nfs3_diropargs arg;
431 struct nfs_fattr res;
432 } *ptr;
433
434 ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
435 if (!ptr)
436 return -ENOMEM;
437 ptr->arg.fh = NFS_FH(dir->d_inode);
438 ptr->arg.name = name->name;
439 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400440 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
442 msg->rpc_argp = &ptr->arg;
443 msg->rpc_resp = &ptr->res;
444 return 0;
445}
446
447static int
448nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
449{
450 struct rpc_message *msg = &task->tk_msg;
451 struct nfs_fattr *dir_attr;
452
Chuck Lever006ea732006-03-20 13:44:14 -0500453 if (nfs3_async_handle_jukebox(task, dir->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 1;
455 if (msg->rpc_argp) {
456 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400457 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 kfree(msg->rpc_argp);
459 }
460 return 0;
461}
462
463static int
464nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
465 struct inode *new_dir, struct qstr *new_name)
466{
467 struct nfs_fattr old_dir_attr, new_dir_attr;
468 struct nfs3_renameargs arg = {
469 .fromfh = NFS_FH(old_dir),
470 .fromname = old_name->name,
471 .fromlen = old_name->len,
472 .tofh = NFS_FH(new_dir),
473 .toname = new_name->name,
474 .tolen = new_name->len
475 };
476 struct nfs3_renameres res = {
477 .fromattr = &old_dir_attr,
478 .toattr = &new_dir_attr
479 };
480 int status;
481
482 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400483 nfs_fattr_init(&old_dir_attr);
484 nfs_fattr_init(&new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 status = rpc_call(NFS_CLIENT(old_dir), NFS3PROC_RENAME, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400486 nfs_post_op_update_inode(old_dir, &old_dir_attr);
487 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 dprintk("NFS reply rename: %d\n", status);
489 return status;
490}
491
492static int
493nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
494{
495 struct nfs_fattr dir_attr, fattr;
496 struct nfs3_linkargs arg = {
497 .fromfh = NFS_FH(inode),
498 .tofh = NFS_FH(dir),
499 .toname = name->name,
500 .tolen = name->len
501 };
502 struct nfs3_linkres res = {
503 .dir_attr = &dir_attr,
504 .fattr = &fattr
505 };
506 int status;
507
508 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400509 nfs_fattr_init(&dir_attr);
510 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_LINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400512 nfs_post_op_update_inode(dir, &dir_attr);
513 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 dprintk("NFS reply link: %d\n", status);
515 return status;
516}
517
518static int
519nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
520 struct iattr *sattr, struct nfs_fh *fhandle,
521 struct nfs_fattr *fattr)
522{
523 struct nfs_fattr dir_attr;
524 struct nfs3_symlinkargs arg = {
525 .fromfh = NFS_FH(dir),
526 .fromname = name->name,
527 .fromlen = name->len,
528 .topath = path->name,
529 .tolen = path->len,
530 .sattr = sattr
531 };
532 struct nfs3_diropres res = {
533 .dir_attr = &dir_attr,
534 .fh = fhandle,
535 .fattr = fattr
536 };
537 int status;
538
539 if (path->len > NFS3_MAXPATHLEN)
540 return -ENAMETOOLONG;
541 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400542 nfs_fattr_init(&dir_attr);
543 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SYMLINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400545 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dprintk("NFS reply symlink: %d\n", status);
547 return status;
548}
549
550static int
551nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
552{
553 struct nfs_fh fhandle;
554 struct nfs_fattr fattr, dir_attr;
555 struct nfs3_mkdirargs arg = {
556 .fh = NFS_FH(dir),
557 .name = dentry->d_name.name,
558 .len = dentry->d_name.len,
559 .sattr = sattr
560 };
561 struct nfs3_diropres res = {
562 .dir_attr = &dir_attr,
563 .fh = &fhandle,
564 .fattr = &fattr
565 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000566 int mode = sattr->ia_mode;
567 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000570
571 sattr->ia_mode &= ~current->fs->umask;
572
Trond Myklebust0e574af2005-10-27 22:12:38 -0400573 nfs_fattr_init(&dir_attr);
574 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKDIR, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400576 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000577 if (status != 0)
578 goto out;
579 status = nfs_instantiate(dentry, &fhandle, &fattr);
580 if (status != 0)
581 goto out;
582 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 dprintk("NFS reply mkdir: %d\n", status);
585 return status;
586}
587
588static int
589nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
590{
591 struct nfs_fattr dir_attr;
592 struct nfs3_diropargs arg = {
593 .fh = NFS_FH(dir),
594 .name = name->name,
595 .len = name->len
596 };
597 int status;
598
599 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400600 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400602 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 dprintk("NFS reply rmdir: %d\n", status);
604 return status;
605}
606
607/*
608 * The READDIR implementation is somewhat hackish - we pass the user buffer
609 * to the encode function, which installs it in the receive iovec.
610 * The decode function itself doesn't perform any decoding, it just makes
611 * sure the reply is syntactically correct.
612 *
613 * Also note that this implementation handles both plain readdir and
614 * readdirplus.
615 */
616static int
617nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
618 u64 cookie, struct page *page, unsigned int count, int plus)
619{
620 struct inode *dir = dentry->d_inode;
621 struct nfs_fattr dir_attr;
622 u32 *verf = NFS_COOKIEVERF(dir);
623 struct nfs3_readdirargs arg = {
624 .fh = NFS_FH(dir),
625 .cookie = cookie,
626 .verf = {verf[0], verf[1]},
627 .plus = plus,
628 .count = count,
629 .pages = &page
630 };
631 struct nfs3_readdirres res = {
632 .dir_attr = &dir_attr,
633 .verf = verf,
634 .plus = plus
635 };
636 struct rpc_message msg = {
637 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
638 .rpc_argp = &arg,
639 .rpc_resp = &res,
640 .rpc_cred = cred
641 };
642 int status;
643
644 lock_kernel();
645
646 if (plus)
647 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
648
649 dprintk("NFS call readdir%s %d\n",
650 plus? "plus" : "", (unsigned int) cookie);
651
Trond Myklebust0e574af2005-10-27 22:12:38 -0400652 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
654 nfs_refresh_inode(dir, &dir_attr);
655 dprintk("NFS reply readdir: %d\n", status);
656 unlock_kernel();
657 return status;
658}
659
660static int
661nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
662 dev_t rdev)
663{
664 struct nfs_fh fh;
665 struct nfs_fattr fattr, dir_attr;
666 struct nfs3_mknodargs arg = {
667 .fh = NFS_FH(dir),
668 .name = dentry->d_name.name,
669 .len = dentry->d_name.len,
670 .sattr = sattr,
671 .rdev = rdev
672 };
673 struct nfs3_diropres res = {
674 .dir_attr = &dir_attr,
675 .fh = &fh,
676 .fattr = &fattr
677 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000678 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int status;
680
681 switch (sattr->ia_mode & S_IFMT) {
682 case S_IFBLK: arg.type = NF3BLK; break;
683 case S_IFCHR: arg.type = NF3CHR; break;
684 case S_IFIFO: arg.type = NF3FIFO; break;
685 case S_IFSOCK: arg.type = NF3SOCK; break;
686 default: return -EINVAL;
687 }
688
689 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
690 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000691
692 sattr->ia_mode &= ~current->fs->umask;
693
Trond Myklebust0e574af2005-10-27 22:12:38 -0400694 nfs_fattr_init(&dir_attr);
695 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKNOD, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400697 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000698 if (status != 0)
699 goto out;
700 status = nfs_instantiate(dentry, &fh, &fattr);
701 if (status != 0)
702 goto out;
703 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
704out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 dprintk("NFS reply mknod: %d\n", status);
706 return status;
707}
708
709static int
710nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
711 struct nfs_fsstat *stat)
712{
713 int status;
714
715 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400716 nfs_fattr_init(stat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, stat, 0);
718 dprintk("NFS reply statfs: %d\n", status);
719 return status;
720}
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 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400729 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
731 dprintk("NFS reply fsinfo: %d\n", status);
732 return status;
733}
734
735static int
736nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
737 struct nfs_pathconf *info)
738{
739 int status;
740
741 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400742 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 status = rpc_call(server->client, NFS3PROC_PATHCONF, fhandle, info, 0);
744 dprintk("NFS reply pathconf: %d\n", status);
745 return status;
746}
747
748extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
749
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100750static void nfs3_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100752 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Chuck Lever006ea732006-03-20 13:44:14 -0500754 if (nfs3_async_handle_jukebox(task, data->inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return;
756 /* Call back common NFS readpage processing */
757 if (task->tk_status >= 0)
758 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100759 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100762static const struct rpc_call_ops nfs3_read_ops = {
763 .rpc_call_done = nfs3_read_done,
764 .rpc_release = nfs_readdata_release,
765};
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static void
768nfs3_proc_read_setup(struct nfs_read_data *data)
769{
770 struct rpc_task *task = &data->task;
771 struct inode *inode = data->inode;
772 int flags;
773 struct rpc_message msg = {
774 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
775 .rpc_argp = &data->args,
776 .rpc_resp = &data->res,
777 .rpc_cred = data->cred,
778 };
779
780 /* N.B. Do we need to test? Never called for swapfile inode */
781 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
782
783 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100784 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 rpc_call_setup(task, &msg, 0);
786}
787
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100788static void nfs3_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100790 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Chuck Lever006ea732006-03-20 13:44:14 -0500792 if (nfs3_async_handle_jukebox(task, data->inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400795 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100796 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100799static const struct rpc_call_ops nfs3_write_ops = {
800 .rpc_call_done = nfs3_write_done,
801 .rpc_release = nfs_writedata_release,
802};
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static void
805nfs3_proc_write_setup(struct nfs_write_data *data, int how)
806{
807 struct rpc_task *task = &data->task;
808 struct inode *inode = data->inode;
809 int stable;
810 int flags;
811 struct rpc_message msg = {
812 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
813 .rpc_argp = &data->args,
814 .rpc_resp = &data->res,
815 .rpc_cred = data->cred,
816 };
817
818 if (how & FLUSH_STABLE) {
819 if (!NFS_I(inode)->ncommit)
820 stable = NFS_FILE_SYNC;
821 else
822 stable = NFS_DATA_SYNC;
823 } else
824 stable = NFS_UNSTABLE;
825 data->args.stable = stable;
826
827 /* Set the initial flags for the task. */
828 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
829
830 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100831 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 rpc_call_setup(task, &msg, 0);
833}
834
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100835static void nfs3_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100837 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Chuck Lever006ea732006-03-20 13:44:14 -0500839 if (nfs3_async_handle_jukebox(task, data->inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400842 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100843 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100846static const struct rpc_call_ops nfs3_commit_ops = {
847 .rpc_call_done = nfs3_commit_done,
848 .rpc_release = nfs_commit_release,
849};
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static void
852nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
853{
854 struct rpc_task *task = &data->task;
855 struct inode *inode = data->inode;
856 int flags;
857 struct rpc_message msg = {
858 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
859 .rpc_argp = &data->args,
860 .rpc_resp = &data->res,
861 .rpc_cred = data->cred,
862 };
863
864 /* Set the initial flags for the task. */
865 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
866
867 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100868 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 rpc_call_setup(task, &msg, 0);
870}
871
872static int
873nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
874{
875 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
876}
877
878struct nfs_rpc_ops nfs_v3_clientops = {
879 .version = 3, /* protocol version */
880 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000881 .dir_inode_ops = &nfs3_dir_inode_operations,
882 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .getroot = nfs3_proc_get_root,
884 .getattr = nfs3_proc_getattr,
885 .setattr = nfs3_proc_setattr,
886 .lookup = nfs3_proc_lookup,
887 .access = nfs3_proc_access,
888 .readlink = nfs3_proc_readlink,
889 .read = nfs3_proc_read,
890 .write = nfs3_proc_write,
891 .commit = nfs3_proc_commit,
892 .create = nfs3_proc_create,
893 .remove = nfs3_proc_remove,
894 .unlink_setup = nfs3_proc_unlink_setup,
895 .unlink_done = nfs3_proc_unlink_done,
896 .rename = nfs3_proc_rename,
897 .link = nfs3_proc_link,
898 .symlink = nfs3_proc_symlink,
899 .mkdir = nfs3_proc_mkdir,
900 .rmdir = nfs3_proc_rmdir,
901 .readdir = nfs3_proc_readdir,
902 .mknod = nfs3_proc_mknod,
903 .statfs = nfs3_proc_statfs,
904 .fsinfo = nfs3_proc_fsinfo,
905 .pathconf = nfs3_proc_pathconf,
906 .decode_dirent = nfs3_decode_dirent,
907 .read_setup = nfs3_proc_read_setup,
908 .write_setup = nfs3_proc_write_setup,
909 .commit_setup = nfs3_proc_commit_setup,
910 .file_open = nfs_open,
911 .file_release = nfs_release,
912 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000913 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914};