blob: cf186f0d2b3ba7eb89826e51f9a94a3f57c6ba6a [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
Chuck Leverdead28d2006-03-20 13:44:23 -050046#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static int
Chuck Lever006ea732006-03-20 13:44:14 -050049nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 if (task->tk_status != -EJUKEBOX)
52 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050053 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 task->tk_status = 0;
55 rpc_restart_call(task);
56 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
57 return 1;
58}
59
J. Bruce Fields03c21732006-01-03 09:55:48 +010060static int
61do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
62 struct nfs_fsinfo *info)
63{
Chuck Leverdead28d2006-03-20 13:44:23 -050064 struct rpc_message msg = {
65 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
66 .rpc_argp = fhandle,
67 .rpc_resp = info,
68 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010069 int status;
70
71 dprintk("%s: call fsinfo\n", __FUNCTION__);
72 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050073 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010074 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
75 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050076 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
77 msg.rpc_resp = info->fattr;
78 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010079 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
80 }
81 return status;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * Bare-bones access to getattr: this is for nfs_read_super.
86 */
87static int
88nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
89 struct nfs_fsinfo *info)
90{
91 int status;
92
J. Bruce Fields03c21732006-01-03 09:55:48 +010093 status = do_proc_get_root(server->client, fhandle, info);
94 if (status && server->client_sys != server->client)
95 status = do_proc_get_root(server->client_sys, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return status;
97}
98
99/*
100 * One function for each procedure in the NFS protocol.
101 */
102static int
103nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
104 struct nfs_fattr *fattr)
105{
Chuck Leverdead28d2006-03-20 13:44:23 -0500106 struct rpc_message msg = {
107 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
108 .rpc_argp = fhandle,
109 .rpc_resp = fattr,
110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int status;
112
113 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400114 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500115 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 dprintk("NFS reply getattr: %d\n", status);
117 return status;
118}
119
120static int
121nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
122 struct iattr *sattr)
123{
124 struct inode *inode = dentry->d_inode;
125 struct nfs3_sattrargs arg = {
126 .fh = NFS_FH(inode),
127 .sattr = sattr,
128 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500129 struct rpc_message msg = {
130 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
131 .rpc_argp = &arg,
132 .rpc_resp = fattr,
133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int status;
135
136 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400137 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500138 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400139 if (status == 0)
140 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dprintk("NFS reply setattr: %d\n", status);
142 return status;
143}
144
145static int
146nfs3_proc_lookup(struct inode *dir, struct qstr *name,
147 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
148{
149 struct nfs_fattr dir_attr;
150 struct nfs3_diropargs arg = {
151 .fh = NFS_FH(dir),
152 .name = name->name,
153 .len = name->len
154 };
155 struct nfs3_diropres res = {
156 .dir_attr = &dir_attr,
157 .fh = fhandle,
158 .fattr = fattr
159 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500160 struct rpc_message msg = {
161 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
162 .rpc_argp = &arg,
163 .rpc_resp = &res,
164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int status;
166
167 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400168 nfs_fattr_init(&dir_attr);
169 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500170 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
171 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
172 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
173 msg.rpc_argp = fhandle;
174 msg.rpc_resp = fattr;
175 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 dprintk("NFS reply lookup: %d\n", status);
178 if (status >= 0)
179 status = nfs_refresh_inode(dir, &dir_attr);
180 return status;
181}
182
183static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
184{
185 struct nfs_fattr fattr;
186 struct nfs3_accessargs arg = {
187 .fh = NFS_FH(inode),
188 };
189 struct nfs3_accessres res = {
190 .fattr = &fattr,
191 };
192 struct rpc_message msg = {
193 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
194 .rpc_argp = &arg,
195 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500196 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 };
198 int mode = entry->mask;
199 int status;
200
201 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (mode & MAY_READ)
204 arg.access |= NFS3_ACCESS_READ;
205 if (S_ISDIR(inode->i_mode)) {
206 if (mode & MAY_WRITE)
207 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
208 if (mode & MAY_EXEC)
209 arg.access |= NFS3_ACCESS_LOOKUP;
210 } else {
211 if (mode & MAY_WRITE)
212 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
213 if (mode & MAY_EXEC)
214 arg.access |= NFS3_ACCESS_EXECUTE;
215 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400216 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
218 nfs_refresh_inode(inode, &fattr);
219 if (status == 0) {
220 entry->mask = 0;
221 if (res.access & NFS3_ACCESS_READ)
222 entry->mask |= MAY_READ;
223 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
224 entry->mask |= MAY_WRITE;
225 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
226 entry->mask |= MAY_EXEC;
227 }
228 dprintk("NFS reply access: %d\n", status);
229 return status;
230}
231
232static int nfs3_proc_readlink(struct inode *inode, struct page *page,
233 unsigned int pgbase, unsigned int pglen)
234{
235 struct nfs_fattr fattr;
236 struct nfs3_readlinkargs args = {
237 .fh = NFS_FH(inode),
238 .pgbase = pgbase,
239 .pglen = pglen,
240 .pages = &page
241 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500242 struct rpc_message msg = {
243 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
244 .rpc_argp = &args,
245 .rpc_resp = &fattr,
246 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int status;
248
249 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400250 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500251 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 nfs_refresh_inode(inode, &fattr);
253 dprintk("NFS reply readlink: %d\n", status);
254 return status;
255}
256
257static int nfs3_proc_read(struct nfs_read_data *rdata)
258{
259 int flags = rdata->flags;
260 struct inode * inode = rdata->inode;
261 struct nfs_fattr * fattr = rdata->res.fattr;
262 struct rpc_message msg = {
263 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
264 .rpc_argp = &rdata->args,
265 .rpc_resp = &rdata->res,
266 .rpc_cred = rdata->cred,
267 };
268 int status;
269
270 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
271 (long long) rdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400272 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
274 if (status >= 0)
275 nfs_refresh_inode(inode, fattr);
276 dprintk("NFS reply read: %d\n", status);
277 return status;
278}
279
280static int nfs3_proc_write(struct nfs_write_data *wdata)
281{
282 int rpcflags = wdata->flags;
283 struct inode * inode = wdata->inode;
284 struct nfs_fattr * fattr = wdata->res.fattr;
285 struct rpc_message msg = {
286 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
287 .rpc_argp = &wdata->args,
288 .rpc_resp = &wdata->res,
289 .rpc_cred = wdata->cred,
290 };
291 int status;
292
293 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
294 (long long) wdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400295 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
297 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400298 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dprintk("NFS reply write: %d\n", status);
300 return status < 0? status : wdata->res.count;
301}
302
303static int nfs3_proc_commit(struct nfs_write_data *cdata)
304{
305 struct inode * inode = cdata->inode;
306 struct nfs_fattr * fattr = cdata->res.fattr;
307 struct rpc_message msg = {
308 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
309 .rpc_argp = &cdata->args,
310 .rpc_resp = &cdata->res,
311 .rpc_cred = cdata->cred,
312 };
313 int status;
314
315 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
316 (long long) cdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400317 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
319 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400320 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dprintk("NFS reply commit: %d\n", status);
322 return status;
323}
324
325/*
326 * Create a regular file.
327 * For now, we don't implement O_EXCL.
328 */
329static int
330nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700331 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct nfs_fh fhandle;
334 struct nfs_fattr fattr;
335 struct nfs_fattr dir_attr;
336 struct nfs3_createargs arg = {
337 .fh = NFS_FH(dir),
338 .name = dentry->d_name.name,
339 .len = dentry->d_name.len,
340 .sattr = sattr,
341 };
342 struct nfs3_diropres res = {
343 .dir_attr = &dir_attr,
344 .fh = &fhandle,
345 .fattr = &fattr
346 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500347 struct rpc_message msg = {
348 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
349 .rpc_argp = &arg,
350 .rpc_resp = &res,
351 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000352 mode_t mode = sattr->ia_mode;
353 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 dprintk("NFS call create %s\n", dentry->d_name.name);
356 arg.createmode = NFS3_CREATE_UNCHECKED;
357 if (flags & O_EXCL) {
358 arg.createmode = NFS3_CREATE_EXCLUSIVE;
359 arg.verifier[0] = jiffies;
360 arg.verifier[1] = current->pid;
361 }
362
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000363 sattr->ia_mode &= ~current->fs->umask;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400366 nfs_fattr_init(&dir_attr);
367 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500368 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
369 nfs_refresh_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* If the server doesn't support the exclusive creation semantics,
372 * try again with simple 'guarded' mode. */
373 if (status == NFSERR_NOTSUPP) {
374 switch (arg.createmode) {
375 case NFS3_CREATE_EXCLUSIVE:
376 arg.createmode = NFS3_CREATE_GUARDED;
377 break;
378
379 case NFS3_CREATE_GUARDED:
380 arg.createmode = NFS3_CREATE_UNCHECKED;
381 break;
382
383 case NFS3_CREATE_UNCHECKED:
384 goto out;
385 }
386 goto again;
387 }
388
389 if (status == 0)
390 status = nfs_instantiate(dentry, &fhandle, &fattr);
391 if (status != 0)
392 goto out;
393
394 /* When we created the file with exclusive semantics, make
395 * sure we set the attributes afterwards. */
396 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
397 dprintk("NFS call setattr (post-create)\n");
398
399 if (!(sattr->ia_valid & ATTR_ATIME_SET))
400 sattr->ia_valid |= ATTR_ATIME;
401 if (!(sattr->ia_valid & ATTR_MTIME_SET))
402 sattr->ia_valid |= ATTR_MTIME;
403
404 /* Note: we could use a guarded setattr here, but I'm
405 * not sure this buys us anything (and I'd have
406 * to revamp the NFSv3 XDR code) */
407 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400408 if (status == 0)
409 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 nfs_refresh_inode(dentry->d_inode, &fattr);
411 dprintk("NFS reply setattr (post-create): %d\n", status);
412 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000413 if (status != 0)
414 goto out;
415 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416out:
417 dprintk("NFS reply create: %d\n", status);
418 return status;
419}
420
421static int
422nfs3_proc_remove(struct inode *dir, struct qstr *name)
423{
424 struct nfs_fattr dir_attr;
425 struct nfs3_diropargs arg = {
426 .fh = NFS_FH(dir),
427 .name = name->name,
428 .len = name->len
429 };
430 struct rpc_message msg = {
431 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
432 .rpc_argp = &arg,
433 .rpc_resp = &dir_attr,
434 };
435 int status;
436
437 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400438 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400440 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dprintk("NFS reply remove: %d\n", status);
442 return status;
443}
444
445static int
446nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
447{
448 struct unlinkxdr {
449 struct nfs3_diropargs arg;
450 struct nfs_fattr res;
451 } *ptr;
452
453 ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
454 if (!ptr)
455 return -ENOMEM;
456 ptr->arg.fh = NFS_FH(dir->d_inode);
457 ptr->arg.name = name->name;
458 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400459 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
461 msg->rpc_argp = &ptr->arg;
462 msg->rpc_resp = &ptr->res;
463 return 0;
464}
465
466static int
467nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
468{
469 struct rpc_message *msg = &task->tk_msg;
470 struct nfs_fattr *dir_attr;
471
Chuck Lever006ea732006-03-20 13:44:14 -0500472 if (nfs3_async_handle_jukebox(task, dir->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 1;
474 if (msg->rpc_argp) {
475 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400476 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 kfree(msg->rpc_argp);
478 }
479 return 0;
480}
481
482static int
483nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
484 struct inode *new_dir, struct qstr *new_name)
485{
486 struct nfs_fattr old_dir_attr, new_dir_attr;
487 struct nfs3_renameargs arg = {
488 .fromfh = NFS_FH(old_dir),
489 .fromname = old_name->name,
490 .fromlen = old_name->len,
491 .tofh = NFS_FH(new_dir),
492 .toname = new_name->name,
493 .tolen = new_name->len
494 };
495 struct nfs3_renameres res = {
496 .fromattr = &old_dir_attr,
497 .toattr = &new_dir_attr
498 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500499 struct rpc_message msg = {
500 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
501 .rpc_argp = &arg,
502 .rpc_resp = &res,
503 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int status;
505
506 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400507 nfs_fattr_init(&old_dir_attr);
508 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500509 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400510 nfs_post_op_update_inode(old_dir, &old_dir_attr);
511 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dprintk("NFS reply rename: %d\n", status);
513 return status;
514}
515
516static int
517nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
518{
519 struct nfs_fattr dir_attr, fattr;
520 struct nfs3_linkargs arg = {
521 .fromfh = NFS_FH(inode),
522 .tofh = NFS_FH(dir),
523 .toname = name->name,
524 .tolen = name->len
525 };
526 struct nfs3_linkres res = {
527 .dir_attr = &dir_attr,
528 .fattr = &fattr
529 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500530 struct rpc_message msg = {
531 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
532 .rpc_argp = &arg,
533 .rpc_resp = &res,
534 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int status;
536
537 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400538 nfs_fattr_init(&dir_attr);
539 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500540 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400541 nfs_post_op_update_inode(dir, &dir_attr);
542 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dprintk("NFS reply link: %d\n", status);
544 return status;
545}
546
547static int
548nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
549 struct iattr *sattr, struct nfs_fh *fhandle,
550 struct nfs_fattr *fattr)
551{
552 struct nfs_fattr dir_attr;
553 struct nfs3_symlinkargs arg = {
554 .fromfh = NFS_FH(dir),
555 .fromname = name->name,
556 .fromlen = name->len,
557 .topath = path->name,
558 .tolen = path->len,
559 .sattr = sattr
560 };
561 struct nfs3_diropres res = {
562 .dir_attr = &dir_attr,
563 .fh = fhandle,
564 .fattr = fattr
565 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500566 struct rpc_message msg = {
567 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
568 .rpc_argp = &arg,
569 .rpc_resp = &res,
570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int status;
572
573 if (path->len > NFS3_MAXPATHLEN)
574 return -ENAMETOOLONG;
575 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400576 nfs_fattr_init(&dir_attr);
577 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500578 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400579 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 dprintk("NFS reply symlink: %d\n", status);
581 return status;
582}
583
584static int
585nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
586{
587 struct nfs_fh fhandle;
588 struct nfs_fattr fattr, dir_attr;
589 struct nfs3_mkdirargs arg = {
590 .fh = NFS_FH(dir),
591 .name = dentry->d_name.name,
592 .len = dentry->d_name.len,
593 .sattr = sattr
594 };
595 struct nfs3_diropres res = {
596 .dir_attr = &dir_attr,
597 .fh = &fhandle,
598 .fattr = &fattr
599 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500600 struct rpc_message msg = {
601 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
602 .rpc_argp = &arg,
603 .rpc_resp = &res,
604 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000605 int mode = sattr->ia_mode;
606 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000609
610 sattr->ia_mode &= ~current->fs->umask;
611
Trond Myklebust0e574af2005-10-27 22:12:38 -0400612 nfs_fattr_init(&dir_attr);
613 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500614 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400615 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000616 if (status != 0)
617 goto out;
618 status = nfs_instantiate(dentry, &fhandle, &fattr);
619 if (status != 0)
620 goto out;
621 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
622out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dprintk("NFS reply mkdir: %d\n", status);
624 return status;
625}
626
627static int
628nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
629{
630 struct nfs_fattr dir_attr;
631 struct nfs3_diropargs arg = {
632 .fh = NFS_FH(dir),
633 .name = name->name,
634 .len = name->len
635 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500636 struct rpc_message msg = {
637 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
638 .rpc_argp = &arg,
639 .rpc_resp = &dir_attr,
640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int status;
642
643 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400644 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500645 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400646 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dprintk("NFS reply rmdir: %d\n", status);
648 return status;
649}
650
651/*
652 * The READDIR implementation is somewhat hackish - we pass the user buffer
653 * to the encode function, which installs it in the receive iovec.
654 * The decode function itself doesn't perform any decoding, it just makes
655 * sure the reply is syntactically correct.
656 *
657 * Also note that this implementation handles both plain readdir and
658 * readdirplus.
659 */
660static int
661nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
662 u64 cookie, struct page *page, unsigned int count, int plus)
663{
664 struct inode *dir = dentry->d_inode;
665 struct nfs_fattr dir_attr;
666 u32 *verf = NFS_COOKIEVERF(dir);
667 struct nfs3_readdirargs arg = {
668 .fh = NFS_FH(dir),
669 .cookie = cookie,
670 .verf = {verf[0], verf[1]},
671 .plus = plus,
672 .count = count,
673 .pages = &page
674 };
675 struct nfs3_readdirres res = {
676 .dir_attr = &dir_attr,
677 .verf = verf,
678 .plus = plus
679 };
680 struct rpc_message msg = {
681 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
682 .rpc_argp = &arg,
683 .rpc_resp = &res,
684 .rpc_cred = cred
685 };
686 int status;
687
688 lock_kernel();
689
690 if (plus)
691 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
692
693 dprintk("NFS call readdir%s %d\n",
694 plus? "plus" : "", (unsigned int) cookie);
695
Trond Myklebust0e574af2005-10-27 22:12:38 -0400696 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
698 nfs_refresh_inode(dir, &dir_attr);
699 dprintk("NFS reply readdir: %d\n", status);
700 unlock_kernel();
701 return status;
702}
703
704static int
705nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
706 dev_t rdev)
707{
708 struct nfs_fh fh;
709 struct nfs_fattr fattr, dir_attr;
710 struct nfs3_mknodargs arg = {
711 .fh = NFS_FH(dir),
712 .name = dentry->d_name.name,
713 .len = dentry->d_name.len,
714 .sattr = sattr,
715 .rdev = rdev
716 };
717 struct nfs3_diropres res = {
718 .dir_attr = &dir_attr,
719 .fh = &fh,
720 .fattr = &fattr
721 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500722 struct rpc_message msg = {
723 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
724 .rpc_argp = &arg,
725 .rpc_resp = &res,
726 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000727 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int status;
729
730 switch (sattr->ia_mode & S_IFMT) {
731 case S_IFBLK: arg.type = NF3BLK; break;
732 case S_IFCHR: arg.type = NF3CHR; break;
733 case S_IFIFO: arg.type = NF3FIFO; break;
734 case S_IFSOCK: arg.type = NF3SOCK; break;
735 default: return -EINVAL;
736 }
737
738 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
739 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000740
741 sattr->ia_mode &= ~current->fs->umask;
742
Trond Myklebust0e574af2005-10-27 22:12:38 -0400743 nfs_fattr_init(&dir_attr);
744 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500745 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400746 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000747 if (status != 0)
748 goto out;
749 status = nfs_instantiate(dentry, &fh, &fattr);
750 if (status != 0)
751 goto out;
752 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
753out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dprintk("NFS reply mknod: %d\n", status);
755 return status;
756}
757
758static int
759nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
760 struct nfs_fsstat *stat)
761{
Chuck Leverdead28d2006-03-20 13:44:23 -0500762 struct rpc_message msg = {
763 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
764 .rpc_argp = fhandle,
765 .rpc_resp = stat,
766 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int status;
768
769 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400770 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500771 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dprintk("NFS reply statfs: %d\n", status);
773 return status;
774}
775
776static int
777nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
778 struct nfs_fsinfo *info)
779{
Chuck Leverdead28d2006-03-20 13:44:23 -0500780 struct rpc_message msg = {
781 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
782 .rpc_argp = fhandle,
783 .rpc_resp = info,
784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int status;
786
787 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400788 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500789 status = rpc_call_sync(server->client_sys, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 dprintk("NFS reply fsinfo: %d\n", status);
791 return status;
792}
793
794static int
795nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
796 struct nfs_pathconf *info)
797{
Chuck Leverdead28d2006-03-20 13:44:23 -0500798 struct rpc_message msg = {
799 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
800 .rpc_argp = fhandle,
801 .rpc_resp = info,
802 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int status;
804
805 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400806 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500807 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dprintk("NFS reply pathconf: %d\n", status);
809 return status;
810}
811
812extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
813
Trond Myklebustec06c092006-03-20 13:44:27 -0500814static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Chuck Lever006ea732006-03-20 13:44:14 -0500816 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500817 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Call back common NFS readpage processing */
819 if (task->tk_status >= 0)
820 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Trond Myklebustec06c092006-03-20 13:44:27 -0500824static void nfs3_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct rpc_message msg = {
827 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
828 .rpc_argp = &data->args,
829 .rpc_resp = &data->res,
830 .rpc_cred = data->cred,
831 };
832
Trond Myklebustec06c092006-03-20 13:44:27 -0500833 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Trond Myklebust788e7a82006-03-20 13:44:27 -0500836static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Chuck Lever006ea732006-03-20 13:44:14 -0500838 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500839 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400841 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Trond Myklebust788e7a82006-03-20 13:44:27 -0500845static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct rpc_message msg = {
848 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
849 .rpc_argp = &data->args,
850 .rpc_resp = &data->res,
851 .rpc_cred = data->cred,
852 };
853
Trond Myklebust788e7a82006-03-20 13:44:27 -0500854 data->args.stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (how & FLUSH_STABLE) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500856 data->args.stable = NFS_FILE_SYNC;
857 if (NFS_I(data->inode)->ncommit)
858 data->args.stable = NFS_DATA_SYNC;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500862 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Trond Myklebust788e7a82006-03-20 13:44:27 -0500865static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Chuck Lever006ea732006-03-20 13:44:14 -0500867 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500868 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400870 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Trond Myklebust788e7a82006-03-20 13:44:27 -0500874static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct rpc_message msg = {
877 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
878 .rpc_argp = &data->args,
879 .rpc_resp = &data->res,
880 .rpc_cred = data->cred,
881 };
882
Trond Myklebust788e7a82006-03-20 13:44:27 -0500883 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static int
887nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
888{
889 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
890}
891
892struct nfs_rpc_ops nfs_v3_clientops = {
893 .version = 3, /* protocol version */
894 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000895 .dir_inode_ops = &nfs3_dir_inode_operations,
896 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .getroot = nfs3_proc_get_root,
898 .getattr = nfs3_proc_getattr,
899 .setattr = nfs3_proc_setattr,
900 .lookup = nfs3_proc_lookup,
901 .access = nfs3_proc_access,
902 .readlink = nfs3_proc_readlink,
903 .read = nfs3_proc_read,
904 .write = nfs3_proc_write,
905 .commit = nfs3_proc_commit,
906 .create = nfs3_proc_create,
907 .remove = nfs3_proc_remove,
908 .unlink_setup = nfs3_proc_unlink_setup,
909 .unlink_done = nfs3_proc_unlink_done,
910 .rename = nfs3_proc_rename,
911 .link = nfs3_proc_link,
912 .symlink = nfs3_proc_symlink,
913 .mkdir = nfs3_proc_mkdir,
914 .rmdir = nfs3_proc_rmdir,
915 .readdir = nfs3_proc_readdir,
916 .mknod = nfs3_proc_mknod,
917 .statfs = nfs3_proc_statfs,
918 .fsinfo = nfs3_proc_fsinfo,
919 .pathconf = nfs3_proc_pathconf,
920 .decode_dirent = nfs3_decode_dirent,
921 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500922 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500924 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500926 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 .file_open = nfs_open,
928 .file_release = nfs_release,
929 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000930 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931};