blob: 814d886b6aa4205e8f634451161f291eaa48e10a [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>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000019#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chuck Lever006ea732006-03-20 13:44:14 -050021#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040022#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NFSDBG_FACILITY NFSDBG_PROC
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* A wrapper to handle the EJUKEBOX error message */
27static int
28nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
29{
30 sigset_t oldset;
31 int res;
32 rpc_clnt_sigmask(clnt, &oldset);
33 do {
34 res = rpc_call_sync(clnt, msg, flags);
35 if (res != -EJUKEBOX)
36 break;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -070037 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 res = -ERESTARTSYS;
39 } while (!signalled());
40 rpc_clnt_sigunmask(clnt, &oldset);
41 return res;
42}
43
Chuck Leverdead28d2006-03-20 13:44:23 -050044#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static int
Chuck Lever006ea732006-03-20 13:44:14 -050047nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 if (task->tk_status != -EJUKEBOX)
50 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050051 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 task->tk_status = 0;
53 rpc_restart_call(task);
54 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
55 return 1;
56}
57
J. Bruce Fields03c21732006-01-03 09:55:48 +010058static int
59do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
60 struct nfs_fsinfo *info)
61{
Chuck Leverdead28d2006-03-20 13:44:23 -050062 struct rpc_message msg = {
63 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
64 .rpc_argp = fhandle,
65 .rpc_resp = info,
66 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010067 int status;
68
69 dprintk("%s: call fsinfo\n", __FUNCTION__);
70 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050071 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010072 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
73 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050074 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
75 msg.rpc_resp = info->fattr;
76 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010077 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
78 }
79 return status;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
David Howells54ceac42006-08-22 20:06:13 -040083 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85static int
86nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
87 struct nfs_fsinfo *info)
88{
89 int status;
90
J. Bruce Fields03c21732006-01-03 09:55:48 +010091 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040092 if (status && server->nfs_client->cl_rpcclient != server->client)
93 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return status;
95}
96
97/*
98 * One function for each procedure in the NFS protocol.
99 */
100static int
101nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
103{
Chuck Leverdead28d2006-03-20 13:44:23 -0500104 struct rpc_message msg = {
105 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
106 .rpc_argp = fhandle,
107 .rpc_resp = fattr,
108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int status;
110
111 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400112 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500113 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 dprintk("NFS reply getattr: %d\n", status);
115 return status;
116}
117
118static int
119nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
120 struct iattr *sattr)
121{
122 struct inode *inode = dentry->d_inode;
123 struct nfs3_sattrargs arg = {
124 .fh = NFS_FH(inode),
125 .sattr = sattr,
126 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500127 struct rpc_message msg = {
128 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
129 .rpc_argp = &arg,
130 .rpc_resp = fattr,
131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int status;
133
134 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400135 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500136 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 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 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500158 struct rpc_message msg = {
159 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
160 .rpc_argp = &arg,
161 .rpc_resp = &res,
162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int status;
164
165 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400166 nfs_fattr_init(&dir_attr);
167 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500168 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
169 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
170 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
171 msg.rpc_argp = fhandle;
172 msg.rpc_resp = fattr;
173 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("NFS reply lookup: %d\n", status);
176 if (status >= 0)
177 status = nfs_refresh_inode(dir, &dir_attr);
178 return status;
179}
180
181static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
182{
183 struct nfs_fattr fattr;
184 struct nfs3_accessargs arg = {
185 .fh = NFS_FH(inode),
186 };
187 struct nfs3_accessres res = {
188 .fattr = &fattr,
189 };
190 struct rpc_message msg = {
191 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
192 .rpc_argp = &arg,
193 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500194 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 };
196 int mode = entry->mask;
197 int status;
198
199 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (mode & MAY_READ)
202 arg.access |= NFS3_ACCESS_READ;
203 if (S_ISDIR(inode->i_mode)) {
204 if (mode & MAY_WRITE)
205 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
206 if (mode & MAY_EXEC)
207 arg.access |= NFS3_ACCESS_LOOKUP;
208 } else {
209 if (mode & MAY_WRITE)
210 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
211 if (mode & MAY_EXEC)
212 arg.access |= NFS3_ACCESS_EXECUTE;
213 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400214 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
216 nfs_refresh_inode(inode, &fattr);
217 if (status == 0) {
218 entry->mask = 0;
219 if (res.access & NFS3_ACCESS_READ)
220 entry->mask |= MAY_READ;
221 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
222 entry->mask |= MAY_WRITE;
223 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
224 entry->mask |= MAY_EXEC;
225 }
226 dprintk("NFS reply access: %d\n", status);
227 return status;
228}
229
230static int nfs3_proc_readlink(struct inode *inode, struct page *page,
231 unsigned int pgbase, unsigned int pglen)
232{
233 struct nfs_fattr fattr;
234 struct nfs3_readlinkargs args = {
235 .fh = NFS_FH(inode),
236 .pgbase = pgbase,
237 .pglen = pglen,
238 .pages = &page
239 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500240 struct rpc_message msg = {
241 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
242 .rpc_argp = &args,
243 .rpc_resp = &fattr,
244 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int status;
246
247 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400248 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500249 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 nfs_refresh_inode(inode, &fattr);
251 dprintk("NFS reply readlink: %d\n", status);
252 return status;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * Create a regular file.
257 * For now, we don't implement O_EXCL.
258 */
259static int
260nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700261 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct nfs_fh fhandle;
264 struct nfs_fattr fattr;
265 struct nfs_fattr dir_attr;
266 struct nfs3_createargs arg = {
267 .fh = NFS_FH(dir),
268 .name = dentry->d_name.name,
269 .len = dentry->d_name.len,
270 .sattr = sattr,
271 };
272 struct nfs3_diropres res = {
273 .dir_attr = &dir_attr,
274 .fh = &fhandle,
275 .fattr = &fattr
276 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500277 struct rpc_message msg = {
278 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
279 .rpc_argp = &arg,
280 .rpc_resp = &res,
281 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000282 mode_t mode = sattr->ia_mode;
283 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 dprintk("NFS call create %s\n", dentry->d_name.name);
286 arg.createmode = NFS3_CREATE_UNCHECKED;
287 if (flags & O_EXCL) {
288 arg.createmode = NFS3_CREATE_EXCLUSIVE;
289 arg.verifier[0] = jiffies;
290 arg.verifier[1] = current->pid;
291 }
292
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000293 sattr->ia_mode &= ~current->fs->umask;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400296 nfs_fattr_init(&dir_attr);
297 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500298 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
299 nfs_refresh_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* If the server doesn't support the exclusive creation semantics,
302 * try again with simple 'guarded' mode. */
Andy Ryan46b9f8e2006-11-07 14:36:26 -0600303 if (status == -ENOTSUPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 switch (arg.createmode) {
305 case NFS3_CREATE_EXCLUSIVE:
306 arg.createmode = NFS3_CREATE_GUARDED;
307 break;
308
309 case NFS3_CREATE_GUARDED:
310 arg.createmode = NFS3_CREATE_UNCHECKED;
311 break;
312
313 case NFS3_CREATE_UNCHECKED:
314 goto out;
315 }
316 goto again;
317 }
318
319 if (status == 0)
320 status = nfs_instantiate(dentry, &fhandle, &fattr);
321 if (status != 0)
322 goto out;
323
324 /* When we created the file with exclusive semantics, make
325 * sure we set the attributes afterwards. */
326 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
327 dprintk("NFS call setattr (post-create)\n");
328
329 if (!(sattr->ia_valid & ATTR_ATIME_SET))
330 sattr->ia_valid |= ATTR_ATIME;
331 if (!(sattr->ia_valid & ATTR_MTIME_SET))
332 sattr->ia_valid |= ATTR_MTIME;
333
334 /* Note: we could use a guarded setattr here, but I'm
335 * not sure this buys us anything (and I'd have
336 * to revamp the NFSv3 XDR code) */
337 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebuste2f032e2007-06-05 19:27:00 -0400338 nfs_post_op_update_inode(dentry->d_inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dprintk("NFS reply setattr (post-create): %d\n", status);
340 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000341 if (status != 0)
342 goto out;
343 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344out:
345 dprintk("NFS reply create: %d\n", status);
346 return status;
347}
348
349static int
350nfs3_proc_remove(struct inode *dir, struct qstr *name)
351{
352 struct nfs_fattr dir_attr;
353 struct nfs3_diropargs arg = {
354 .fh = NFS_FH(dir),
355 .name = name->name,
356 .len = name->len
357 };
358 struct rpc_message msg = {
359 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
360 .rpc_argp = &arg,
361 .rpc_resp = &dir_attr,
362 };
363 int status;
364
365 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400366 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400368 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dprintk("NFS reply remove: %d\n", status);
370 return status;
371}
372
373static int
374nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
375{
376 struct unlinkxdr {
377 struct nfs3_diropargs arg;
378 struct nfs_fattr res;
379 } *ptr;
380
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700381 ptr = kmalloc(sizeof(*ptr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!ptr)
383 return -ENOMEM;
384 ptr->arg.fh = NFS_FH(dir->d_inode);
385 ptr->arg.name = name->name;
386 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400387 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
389 msg->rpc_argp = &ptr->arg;
390 msg->rpc_resp = &ptr->res;
391 return 0;
392}
393
394static int
395nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
396{
397 struct rpc_message *msg = &task->tk_msg;
398 struct nfs_fattr *dir_attr;
399
Chuck Lever006ea732006-03-20 13:44:14 -0500400 if (nfs3_async_handle_jukebox(task, dir->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 1;
402 if (msg->rpc_argp) {
403 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400404 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 kfree(msg->rpc_argp);
406 }
407 return 0;
408}
409
410static int
411nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
412 struct inode *new_dir, struct qstr *new_name)
413{
414 struct nfs_fattr old_dir_attr, new_dir_attr;
415 struct nfs3_renameargs arg = {
416 .fromfh = NFS_FH(old_dir),
417 .fromname = old_name->name,
418 .fromlen = old_name->len,
419 .tofh = NFS_FH(new_dir),
420 .toname = new_name->name,
421 .tolen = new_name->len
422 };
423 struct nfs3_renameres res = {
424 .fromattr = &old_dir_attr,
425 .toattr = &new_dir_attr
426 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500427 struct rpc_message msg = {
428 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
429 .rpc_argp = &arg,
430 .rpc_resp = &res,
431 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int status;
433
434 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400435 nfs_fattr_init(&old_dir_attr);
436 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500437 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400438 nfs_post_op_update_inode(old_dir, &old_dir_attr);
439 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dprintk("NFS reply rename: %d\n", status);
441 return status;
442}
443
444static int
445nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
446{
447 struct nfs_fattr dir_attr, fattr;
448 struct nfs3_linkargs arg = {
449 .fromfh = NFS_FH(inode),
450 .tofh = NFS_FH(dir),
451 .toname = name->name,
452 .tolen = name->len
453 };
454 struct nfs3_linkres res = {
455 .dir_attr = &dir_attr,
456 .fattr = &fattr
457 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500458 struct rpc_message msg = {
459 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
460 .rpc_argp = &arg,
461 .rpc_resp = &res,
462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int status;
464
465 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400466 nfs_fattr_init(&dir_attr);
467 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500468 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400469 nfs_post_op_update_inode(dir, &dir_attr);
470 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dprintk("NFS reply link: %d\n", status);
472 return status;
473}
474
475static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400476nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
477 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Chuck Lever4f390c12006-08-22 20:06:22 -0400479 struct nfs_fh fhandle;
480 struct nfs_fattr fattr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct nfs3_symlinkargs arg = {
482 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400483 .fromname = dentry->d_name.name,
484 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400485 .pages = &page,
486 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .sattr = sattr
488 };
489 struct nfs3_diropres res = {
490 .dir_attr = &dir_attr,
Chuck Lever4f390c12006-08-22 20:06:22 -0400491 .fh = &fhandle,
492 .fattr = &fattr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500494 struct rpc_message msg = {
495 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
496 .rpc_argp = &arg,
497 .rpc_resp = &res,
498 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int status;
500
Chuck Lever94a6d752006-08-22 20:06:23 -0400501 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400503
Chuck Lever94a6d752006-08-22 20:06:23 -0400504 dprintk("NFS call symlink %s\n", dentry->d_name.name);
505
Trond Myklebust0e574af2005-10-27 22:12:38 -0400506 nfs_fattr_init(&dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400507 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500508 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400509 nfs_post_op_update_inode(dir, &dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400510 if (status != 0)
511 goto out;
512 status = nfs_instantiate(dentry, &fhandle, &fattr);
513out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 dprintk("NFS reply symlink: %d\n", status);
515 return status;
516}
517
518static int
519nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
520{
521 struct nfs_fh fhandle;
522 struct nfs_fattr fattr, dir_attr;
523 struct nfs3_mkdirargs arg = {
524 .fh = NFS_FH(dir),
525 .name = dentry->d_name.name,
526 .len = dentry->d_name.len,
527 .sattr = sattr
528 };
529 struct nfs3_diropres res = {
530 .dir_attr = &dir_attr,
531 .fh = &fhandle,
532 .fattr = &fattr
533 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500534 struct rpc_message msg = {
535 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
536 .rpc_argp = &arg,
537 .rpc_resp = &res,
538 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000539 int mode = sattr->ia_mode;
540 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000543
544 sattr->ia_mode &= ~current->fs->umask;
545
Trond Myklebust0e574af2005-10-27 22:12:38 -0400546 nfs_fattr_init(&dir_attr);
547 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500548 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400549 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000550 if (status != 0)
551 goto out;
552 status = nfs_instantiate(dentry, &fhandle, &fattr);
553 if (status != 0)
554 goto out;
555 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
556out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 dprintk("NFS reply mkdir: %d\n", status);
558 return status;
559}
560
561static int
562nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
563{
564 struct nfs_fattr dir_attr;
565 struct nfs3_diropargs arg = {
566 .fh = NFS_FH(dir),
567 .name = name->name,
568 .len = name->len
569 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500570 struct rpc_message msg = {
571 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
572 .rpc_argp = &arg,
573 .rpc_resp = &dir_attr,
574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int status;
576
577 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400578 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500579 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400580 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dprintk("NFS reply rmdir: %d\n", status);
582 return status;
583}
584
585/*
586 * The READDIR implementation is somewhat hackish - we pass the user buffer
587 * to the encode function, which installs it in the receive iovec.
588 * The decode function itself doesn't perform any decoding, it just makes
589 * sure the reply is syntactically correct.
590 *
591 * Also note that this implementation handles both plain readdir and
592 * readdirplus.
593 */
594static int
595nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
596 u64 cookie, struct page *page, unsigned int count, int plus)
597{
598 struct inode *dir = dentry->d_inode;
599 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700600 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct nfs3_readdirargs arg = {
602 .fh = NFS_FH(dir),
603 .cookie = cookie,
604 .verf = {verf[0], verf[1]},
605 .plus = plus,
606 .count = count,
607 .pages = &page
608 };
609 struct nfs3_readdirres res = {
610 .dir_attr = &dir_attr,
611 .verf = verf,
612 .plus = plus
613 };
614 struct rpc_message msg = {
615 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
616 .rpc_argp = &arg,
617 .rpc_resp = &res,
618 .rpc_cred = cred
619 };
620 int status;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (plus)
623 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
624
625 dprintk("NFS call readdir%s %d\n",
626 plus? "plus" : "", (unsigned int) cookie);
627
Trond Myklebust0e574af2005-10-27 22:12:38 -0400628 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
630 nfs_refresh_inode(dir, &dir_attr);
631 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return status;
633}
634
635static int
636nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
637 dev_t rdev)
638{
639 struct nfs_fh fh;
640 struct nfs_fattr fattr, dir_attr;
641 struct nfs3_mknodargs arg = {
642 .fh = NFS_FH(dir),
643 .name = dentry->d_name.name,
644 .len = dentry->d_name.len,
645 .sattr = sattr,
646 .rdev = rdev
647 };
648 struct nfs3_diropres res = {
649 .dir_attr = &dir_attr,
650 .fh = &fh,
651 .fattr = &fattr
652 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500653 struct rpc_message msg = {
654 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
655 .rpc_argp = &arg,
656 .rpc_resp = &res,
657 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000658 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int status;
660
661 switch (sattr->ia_mode & S_IFMT) {
662 case S_IFBLK: arg.type = NF3BLK; break;
663 case S_IFCHR: arg.type = NF3CHR; break;
664 case S_IFIFO: arg.type = NF3FIFO; break;
665 case S_IFSOCK: arg.type = NF3SOCK; break;
666 default: return -EINVAL;
667 }
668
669 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
670 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000671
672 sattr->ia_mode &= ~current->fs->umask;
673
Trond Myklebust0e574af2005-10-27 22:12:38 -0400674 nfs_fattr_init(&dir_attr);
675 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500676 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400677 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000678 if (status != 0)
679 goto out;
680 status = nfs_instantiate(dentry, &fh, &fattr);
681 if (status != 0)
682 goto out;
683 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
684out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dprintk("NFS reply mknod: %d\n", status);
686 return status;
687}
688
689static int
690nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
691 struct nfs_fsstat *stat)
692{
Chuck Leverdead28d2006-03-20 13:44:23 -0500693 struct rpc_message msg = {
694 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
695 .rpc_argp = fhandle,
696 .rpc_resp = stat,
697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int status;
699
700 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400701 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500702 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dprintk("NFS reply statfs: %d\n", status);
704 return status;
705}
706
707static int
708nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
709 struct nfs_fsinfo *info)
710{
Chuck Leverdead28d2006-03-20 13:44:23 -0500711 struct rpc_message msg = {
712 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
713 .rpc_argp = fhandle,
714 .rpc_resp = info,
715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int status;
717
718 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400719 nfs_fattr_init(info->fattr);
David Howells5006a762006-08-22 20:06:12 -0400720 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dprintk("NFS reply fsinfo: %d\n", status);
722 return status;
723}
724
725static int
726nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
727 struct nfs_pathconf *info)
728{
Chuck Leverdead28d2006-03-20 13:44:23 -0500729 struct rpc_message msg = {
730 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
731 .rpc_argp = fhandle,
732 .rpc_resp = info,
733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int status;
735
736 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400737 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500738 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 dprintk("NFS reply pathconf: %d\n", status);
740 return status;
741}
742
Trond Myklebustec06c092006-03-20 13:44:27 -0500743static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Chuck Lever006ea732006-03-20 13:44:14 -0500745 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500746 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Call back common NFS readpage processing */
748 if (task->tk_status >= 0)
749 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Trond Myklebustec06c092006-03-20 13:44:27 -0500753static void nfs3_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct rpc_message msg = {
756 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
757 .rpc_argp = &data->args,
758 .rpc_resp = &data->res,
759 .rpc_cred = data->cred,
760 };
761
Trond Myklebustec06c092006-03-20 13:44:27 -0500762 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Trond Myklebust788e7a82006-03-20 13:44:27 -0500765static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Chuck Lever006ea732006-03-20 13:44:14 -0500767 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500768 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400770 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Trond Myklebust788e7a82006-03-20 13:44:27 -0500774static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct rpc_message msg = {
777 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
778 .rpc_argp = &data->args,
779 .rpc_resp = &data->res,
780 .rpc_cred = data->cred,
781 };
782
Trond Myklebust788e7a82006-03-20 13:44:27 -0500783 data->args.stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (how & FLUSH_STABLE) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500785 data->args.stable = NFS_FILE_SYNC;
786 if (NFS_I(data->inode)->ncommit)
787 data->args.stable = NFS_DATA_SYNC;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500791 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Trond Myklebust788e7a82006-03-20 13:44:27 -0500794static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Chuck Lever006ea732006-03-20 13:44:14 -0500796 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500797 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400799 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Trond Myklebust788e7a82006-03-20 13:44:27 -0500803static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct rpc_message msg = {
806 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
807 .rpc_argp = &data->args,
808 .rpc_resp = &data->res,
809 .rpc_cred = data->cred,
810 };
811
Trond Myklebust788e7a82006-03-20 13:44:27 -0500812 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815static int
816nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
817{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800818 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
David Howells509de812006-08-22 20:06:11 -0400821const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .version = 3, /* protocol version */
823 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000824 .dir_inode_ops = &nfs3_dir_inode_operations,
825 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .getroot = nfs3_proc_get_root,
827 .getattr = nfs3_proc_getattr,
828 .setattr = nfs3_proc_setattr,
829 .lookup = nfs3_proc_lookup,
830 .access = nfs3_proc_access,
831 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .create = nfs3_proc_create,
833 .remove = nfs3_proc_remove,
834 .unlink_setup = nfs3_proc_unlink_setup,
835 .unlink_done = nfs3_proc_unlink_done,
836 .rename = nfs3_proc_rename,
837 .link = nfs3_proc_link,
838 .symlink = nfs3_proc_symlink,
839 .mkdir = nfs3_proc_mkdir,
840 .rmdir = nfs3_proc_rmdir,
841 .readdir = nfs3_proc_readdir,
842 .mknod = nfs3_proc_mknod,
843 .statfs = nfs3_proc_statfs,
844 .fsinfo = nfs3_proc_fsinfo,
845 .pathconf = nfs3_proc_pathconf,
846 .decode_dirent = nfs3_decode_dirent,
847 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500848 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500850 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500852 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 .file_open = nfs_open,
854 .file_release = nfs_release,
855 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000856 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};