blob: 7d0371e2bad53228b8d2cbee4e95b9bf3fe75b25 [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/*
David Howells54ceac42006-08-22 20:06:13 -040084 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
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);
David Howells5006a762006-08-22 20:06:12 -040093 if (status && server->nfs_client->cl_rpcclient != server->client)
94 status = do_proc_get_root(server->nfs_client->cl_rpcclient, 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
257 * Create a regular file.
258 * For now, we don't implement O_EXCL.
259 */
260static int
261nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700262 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct nfs_fh fhandle;
265 struct nfs_fattr fattr;
266 struct nfs_fattr dir_attr;
267 struct nfs3_createargs arg = {
268 .fh = NFS_FH(dir),
269 .name = dentry->d_name.name,
270 .len = dentry->d_name.len,
271 .sattr = sattr,
272 };
273 struct nfs3_diropres res = {
274 .dir_attr = &dir_attr,
275 .fh = &fhandle,
276 .fattr = &fattr
277 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500278 struct rpc_message msg = {
279 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
280 .rpc_argp = &arg,
281 .rpc_resp = &res,
282 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000283 mode_t mode = sattr->ia_mode;
284 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 dprintk("NFS call create %s\n", dentry->d_name.name);
287 arg.createmode = NFS3_CREATE_UNCHECKED;
288 if (flags & O_EXCL) {
289 arg.createmode = NFS3_CREATE_EXCLUSIVE;
290 arg.verifier[0] = jiffies;
291 arg.verifier[1] = current->pid;
292 }
293
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000294 sattr->ia_mode &= ~current->fs->umask;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400297 nfs_fattr_init(&dir_attr);
298 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500299 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
300 nfs_refresh_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* If the server doesn't support the exclusive creation semantics,
303 * try again with simple 'guarded' mode. */
Andy Ryan46b9f8e2006-11-07 14:36:26 -0600304 if (status == -ENOTSUPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (arg.createmode) {
306 case NFS3_CREATE_EXCLUSIVE:
307 arg.createmode = NFS3_CREATE_GUARDED;
308 break;
309
310 case NFS3_CREATE_GUARDED:
311 arg.createmode = NFS3_CREATE_UNCHECKED;
312 break;
313
314 case NFS3_CREATE_UNCHECKED:
315 goto out;
316 }
317 goto again;
318 }
319
320 if (status == 0)
321 status = nfs_instantiate(dentry, &fhandle, &fattr);
322 if (status != 0)
323 goto out;
324
325 /* When we created the file with exclusive semantics, make
326 * sure we set the attributes afterwards. */
327 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
328 dprintk("NFS call setattr (post-create)\n");
329
330 if (!(sattr->ia_valid & ATTR_ATIME_SET))
331 sattr->ia_valid |= ATTR_ATIME;
332 if (!(sattr->ia_valid & ATTR_MTIME_SET))
333 sattr->ia_valid |= ATTR_MTIME;
334
335 /* Note: we could use a guarded setattr here, but I'm
336 * not sure this buys us anything (and I'd have
337 * to revamp the NFSv3 XDR code) */
338 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400339 if (status == 0)
340 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 nfs_refresh_inode(dentry->d_inode, &fattr);
342 dprintk("NFS reply setattr (post-create): %d\n", status);
343 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000344 if (status != 0)
345 goto out;
346 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347out:
348 dprintk("NFS reply create: %d\n", status);
349 return status;
350}
351
352static int
353nfs3_proc_remove(struct inode *dir, struct qstr *name)
354{
355 struct nfs_fattr dir_attr;
356 struct nfs3_diropargs arg = {
357 .fh = NFS_FH(dir),
358 .name = name->name,
359 .len = name->len
360 };
361 struct rpc_message msg = {
362 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
363 .rpc_argp = &arg,
364 .rpc_resp = &dir_attr,
365 };
366 int status;
367
368 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400369 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400371 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dprintk("NFS reply remove: %d\n", status);
373 return status;
374}
375
376static int
377nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
378{
379 struct unlinkxdr {
380 struct nfs3_diropargs arg;
381 struct nfs_fattr res;
382 } *ptr;
383
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700384 ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!ptr)
386 return -ENOMEM;
387 ptr->arg.fh = NFS_FH(dir->d_inode);
388 ptr->arg.name = name->name;
389 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400390 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
392 msg->rpc_argp = &ptr->arg;
393 msg->rpc_resp = &ptr->res;
394 return 0;
395}
396
397static int
398nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
399{
400 struct rpc_message *msg = &task->tk_msg;
401 struct nfs_fattr *dir_attr;
402
Chuck Lever006ea732006-03-20 13:44:14 -0500403 if (nfs3_async_handle_jukebox(task, dir->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 1;
405 if (msg->rpc_argp) {
406 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400407 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 kfree(msg->rpc_argp);
409 }
410 return 0;
411}
412
413static int
414nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
415 struct inode *new_dir, struct qstr *new_name)
416{
417 struct nfs_fattr old_dir_attr, new_dir_attr;
418 struct nfs3_renameargs arg = {
419 .fromfh = NFS_FH(old_dir),
420 .fromname = old_name->name,
421 .fromlen = old_name->len,
422 .tofh = NFS_FH(new_dir),
423 .toname = new_name->name,
424 .tolen = new_name->len
425 };
426 struct nfs3_renameres res = {
427 .fromattr = &old_dir_attr,
428 .toattr = &new_dir_attr
429 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500430 struct rpc_message msg = {
431 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
432 .rpc_argp = &arg,
433 .rpc_resp = &res,
434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int status;
436
437 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400438 nfs_fattr_init(&old_dir_attr);
439 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500440 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400441 nfs_post_op_update_inode(old_dir, &old_dir_attr);
442 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dprintk("NFS reply rename: %d\n", status);
444 return status;
445}
446
447static int
448nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
449{
450 struct nfs_fattr dir_attr, fattr;
451 struct nfs3_linkargs arg = {
452 .fromfh = NFS_FH(inode),
453 .tofh = NFS_FH(dir),
454 .toname = name->name,
455 .tolen = name->len
456 };
457 struct nfs3_linkres res = {
458 .dir_attr = &dir_attr,
459 .fattr = &fattr
460 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500461 struct rpc_message msg = {
462 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
463 .rpc_argp = &arg,
464 .rpc_resp = &res,
465 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int status;
467
468 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400469 nfs_fattr_init(&dir_attr);
470 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500471 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400472 nfs_post_op_update_inode(dir, &dir_attr);
473 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 dprintk("NFS reply link: %d\n", status);
475 return status;
476}
477
478static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400479nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
480 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Chuck Lever4f390c12006-08-22 20:06:22 -0400482 struct nfs_fh fhandle;
483 struct nfs_fattr fattr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct nfs3_symlinkargs arg = {
485 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400486 .fromname = dentry->d_name.name,
487 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400488 .pages = &page,
489 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 .sattr = sattr
491 };
492 struct nfs3_diropres res = {
493 .dir_attr = &dir_attr,
Chuck Lever4f390c12006-08-22 20:06:22 -0400494 .fh = &fhandle,
495 .fattr = &fattr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500497 struct rpc_message msg = {
498 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
499 .rpc_argp = &arg,
500 .rpc_resp = &res,
501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int status;
503
Chuck Lever94a6d752006-08-22 20:06:23 -0400504 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400506
Chuck Lever94a6d752006-08-22 20:06:23 -0400507 dprintk("NFS call symlink %s\n", dentry->d_name.name);
508
Trond Myklebust0e574af2005-10-27 22:12:38 -0400509 nfs_fattr_init(&dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400510 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500511 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400512 nfs_post_op_update_inode(dir, &dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400513 if (status != 0)
514 goto out;
515 status = nfs_instantiate(dentry, &fhandle, &fattr);
516out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 dprintk("NFS reply symlink: %d\n", status);
518 return status;
519}
520
521static int
522nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
523{
524 struct nfs_fh fhandle;
525 struct nfs_fattr fattr, dir_attr;
526 struct nfs3_mkdirargs arg = {
527 .fh = NFS_FH(dir),
528 .name = dentry->d_name.name,
529 .len = dentry->d_name.len,
530 .sattr = sattr
531 };
532 struct nfs3_diropres res = {
533 .dir_attr = &dir_attr,
534 .fh = &fhandle,
535 .fattr = &fattr
536 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500537 struct rpc_message msg = {
538 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
539 .rpc_argp = &arg,
540 .rpc_resp = &res,
541 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000542 int mode = sattr->ia_mode;
543 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000546
547 sattr->ia_mode &= ~current->fs->umask;
548
Trond Myklebust0e574af2005-10-27 22:12:38 -0400549 nfs_fattr_init(&dir_attr);
550 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500551 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400552 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000553 if (status != 0)
554 goto out;
555 status = nfs_instantiate(dentry, &fhandle, &fattr);
556 if (status != 0)
557 goto out;
558 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
559out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dprintk("NFS reply mkdir: %d\n", status);
561 return status;
562}
563
564static int
565nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
566{
567 struct nfs_fattr dir_attr;
568 struct nfs3_diropargs arg = {
569 .fh = NFS_FH(dir),
570 .name = name->name,
571 .len = name->len
572 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500573 struct rpc_message msg = {
574 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
575 .rpc_argp = &arg,
576 .rpc_resp = &dir_attr,
577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int status;
579
580 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400581 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500582 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400583 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 dprintk("NFS reply rmdir: %d\n", status);
585 return status;
586}
587
588/*
589 * The READDIR implementation is somewhat hackish - we pass the user buffer
590 * to the encode function, which installs it in the receive iovec.
591 * The decode function itself doesn't perform any decoding, it just makes
592 * sure the reply is syntactically correct.
593 *
594 * Also note that this implementation handles both plain readdir and
595 * readdirplus.
596 */
597static int
598nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
599 u64 cookie, struct page *page, unsigned int count, int plus)
600{
601 struct inode *dir = dentry->d_inode;
602 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700603 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct nfs3_readdirargs arg = {
605 .fh = NFS_FH(dir),
606 .cookie = cookie,
607 .verf = {verf[0], verf[1]},
608 .plus = plus,
609 .count = count,
610 .pages = &page
611 };
612 struct nfs3_readdirres res = {
613 .dir_attr = &dir_attr,
614 .verf = verf,
615 .plus = plus
616 };
617 struct rpc_message msg = {
618 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
619 .rpc_argp = &arg,
620 .rpc_resp = &res,
621 .rpc_cred = cred
622 };
623 int status;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (plus)
626 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
627
628 dprintk("NFS call readdir%s %d\n",
629 plus? "plus" : "", (unsigned int) cookie);
630
Trond Myklebust0e574af2005-10-27 22:12:38 -0400631 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
633 nfs_refresh_inode(dir, &dir_attr);
634 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return status;
636}
637
638static int
639nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
640 dev_t rdev)
641{
642 struct nfs_fh fh;
643 struct nfs_fattr fattr, dir_attr;
644 struct nfs3_mknodargs arg = {
645 .fh = NFS_FH(dir),
646 .name = dentry->d_name.name,
647 .len = dentry->d_name.len,
648 .sattr = sattr,
649 .rdev = rdev
650 };
651 struct nfs3_diropres res = {
652 .dir_attr = &dir_attr,
653 .fh = &fh,
654 .fattr = &fattr
655 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500656 struct rpc_message msg = {
657 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
658 .rpc_argp = &arg,
659 .rpc_resp = &res,
660 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000661 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 int status;
663
664 switch (sattr->ia_mode & S_IFMT) {
665 case S_IFBLK: arg.type = NF3BLK; break;
666 case S_IFCHR: arg.type = NF3CHR; break;
667 case S_IFIFO: arg.type = NF3FIFO; break;
668 case S_IFSOCK: arg.type = NF3SOCK; break;
669 default: return -EINVAL;
670 }
671
672 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
673 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000674
675 sattr->ia_mode &= ~current->fs->umask;
676
Trond Myklebust0e574af2005-10-27 22:12:38 -0400677 nfs_fattr_init(&dir_attr);
678 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500679 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400680 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000681 if (status != 0)
682 goto out;
683 status = nfs_instantiate(dentry, &fh, &fattr);
684 if (status != 0)
685 goto out;
686 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
687out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dprintk("NFS reply mknod: %d\n", status);
689 return status;
690}
691
692static int
693nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
694 struct nfs_fsstat *stat)
695{
Chuck Leverdead28d2006-03-20 13:44:23 -0500696 struct rpc_message msg = {
697 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
698 .rpc_argp = fhandle,
699 .rpc_resp = stat,
700 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int status;
702
703 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400704 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500705 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dprintk("NFS reply statfs: %d\n", status);
707 return status;
708}
709
710static int
711nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
712 struct nfs_fsinfo *info)
713{
Chuck Leverdead28d2006-03-20 13:44:23 -0500714 struct rpc_message msg = {
715 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
716 .rpc_argp = fhandle,
717 .rpc_resp = info,
718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int status;
720
721 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400722 nfs_fattr_init(info->fattr);
David Howells5006a762006-08-22 20:06:12 -0400723 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dprintk("NFS reply fsinfo: %d\n", status);
725 return status;
726}
727
728static int
729nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
730 struct nfs_pathconf *info)
731{
Chuck Leverdead28d2006-03-20 13:44:23 -0500732 struct rpc_message msg = {
733 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
734 .rpc_argp = fhandle,
735 .rpc_resp = info,
736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int status;
738
739 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400740 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500741 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dprintk("NFS reply pathconf: %d\n", status);
743 return status;
744}
745
Trond Myklebustec06c092006-03-20 13:44:27 -0500746static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Chuck Lever006ea732006-03-20 13:44:14 -0500748 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500749 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Call back common NFS readpage processing */
751 if (task->tk_status >= 0)
752 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebustec06c092006-03-20 13:44:27 -0500756static void nfs3_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct rpc_message msg = {
759 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
760 .rpc_argp = &data->args,
761 .rpc_resp = &data->res,
762 .rpc_cred = data->cred,
763 };
764
Trond Myklebustec06c092006-03-20 13:44:27 -0500765 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Trond Myklebust788e7a82006-03-20 13:44:27 -0500768static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Chuck Lever006ea732006-03-20 13:44:14 -0500770 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500771 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400773 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Trond Myklebust788e7a82006-03-20 13:44:27 -0500777static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct rpc_message msg = {
780 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
781 .rpc_argp = &data->args,
782 .rpc_resp = &data->res,
783 .rpc_cred = data->cred,
784 };
785
Trond Myklebust788e7a82006-03-20 13:44:27 -0500786 data->args.stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (how & FLUSH_STABLE) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500788 data->args.stable = NFS_FILE_SYNC;
789 if (NFS_I(data->inode)->ncommit)
790 data->args.stable = NFS_DATA_SYNC;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500794 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Trond Myklebust788e7a82006-03-20 13:44:27 -0500797static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Chuck Lever006ea732006-03-20 13:44:14 -0500799 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500800 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400802 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Trond Myklebust788e7a82006-03-20 13:44:27 -0500806static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct rpc_message msg = {
809 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
810 .rpc_argp = &data->args,
811 .rpc_resp = &data->res,
812 .rpc_cred = data->cred,
813 };
814
Trond Myklebust788e7a82006-03-20 13:44:27 -0500815 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818static int
819nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
820{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800821 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
David Howells509de812006-08-22 20:06:11 -0400824const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .version = 3, /* protocol version */
826 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000827 .dir_inode_ops = &nfs3_dir_inode_operations,
828 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .getroot = nfs3_proc_get_root,
830 .getattr = nfs3_proc_getattr,
831 .setattr = nfs3_proc_setattr,
832 .lookup = nfs3_proc_lookup,
833 .access = nfs3_proc_access,
834 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .create = nfs3_proc_create,
836 .remove = nfs3_proc_remove,
837 .unlink_setup = nfs3_proc_unlink_setup,
838 .unlink_done = nfs3_proc_unlink_done,
839 .rename = nfs3_proc_rename,
840 .link = nfs3_proc_link,
841 .symlink = nfs3_proc_symlink,
842 .mkdir = nfs3_proc_mkdir,
843 .rmdir = nfs3_proc_rmdir,
844 .readdir = nfs3_proc_readdir,
845 .mknod = nfs3_proc_mknod,
846 .statfs = nfs3_proc_statfs,
847 .fsinfo = nfs3_proc_fsinfo,
848 .pathconf = nfs3_proc_pathconf,
849 .decode_dirent = nfs3_decode_dirent,
850 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500851 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500853 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500855 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 .file_open = nfs_open,
857 .file_release = nfs_release,
858 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000859 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};