blob: ed67567f0556ab4711213093975deac920e85d09 [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
22#define NFSDBG_FACILITY NFSDBG_PROC
23
24extern struct rpc_procinfo nfs3_procedures[];
25
26/* 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
44static inline int
45nfs3_rpc_call_wrapper(struct rpc_clnt *clnt, u32 proc, void *argp, void *resp, int flags)
46{
47 struct rpc_message msg = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000048 .rpc_proc = &clnt->cl_procinfo[proc],
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .rpc_argp = argp,
50 .rpc_resp = resp,
51 };
52 return nfs3_rpc_wrapper(clnt, &msg, flags);
53}
54
55#define rpc_call(clnt, proc, argp, resp, flags) \
56 nfs3_rpc_call_wrapper(clnt, proc, argp, resp, flags)
57#define rpc_call_sync(clnt, msg, flags) \
58 nfs3_rpc_wrapper(clnt, msg, flags)
59
60static int
61nfs3_async_handle_jukebox(struct rpc_task *task)
62{
63 if (task->tk_status != -EJUKEBOX)
64 return 0;
65 task->tk_status = 0;
66 rpc_restart_call(task);
67 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
68 return 1;
69}
70
J. Bruce Fields03c21732006-01-03 09:55:48 +010071static int
72do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
73 struct nfs_fsinfo *info)
74{
75 int status;
76
77 dprintk("%s: call fsinfo\n", __FUNCTION__);
78 nfs_fattr_init(info->fattr);
79 status = rpc_call(client, NFS3PROC_FSINFO, fhandle, info, 0);
80 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
81 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
82 status = rpc_call(client, NFS3PROC_GETATTR, fhandle, info->fattr, 0);
83 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
84 }
85 return status;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * Bare-bones access to getattr: this is for nfs_read_super.
90 */
91static int
92nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
93 struct nfs_fsinfo *info)
94{
95 int status;
96
J. Bruce Fields03c21732006-01-03 09:55:48 +010097 status = do_proc_get_root(server->client, fhandle, info);
98 if (status && server->client_sys != server->client)
99 status = do_proc_get_root(server->client_sys, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return status;
101}
102
103/*
104 * One function for each procedure in the NFS protocol.
105 */
106static int
107nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
108 struct nfs_fattr *fattr)
109{
110 int status;
111
112 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400113 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 status = rpc_call(server->client, NFS3PROC_GETATTR,
115 fhandle, fattr, 0);
116 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 };
129 int status;
130
131 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400132 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_SETATTR, &arg, fattr, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400134 if (status == 0)
135 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dprintk("NFS reply setattr: %d\n", status);
137 return status;
138}
139
140static int
141nfs3_proc_lookup(struct inode *dir, struct qstr *name,
142 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
143{
144 struct nfs_fattr dir_attr;
145 struct nfs3_diropargs arg = {
146 .fh = NFS_FH(dir),
147 .name = name->name,
148 .len = name->len
149 };
150 struct nfs3_diropres res = {
151 .dir_attr = &dir_attr,
152 .fh = fhandle,
153 .fattr = fattr
154 };
155 int status;
156
157 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400158 nfs_fattr_init(&dir_attr);
159 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_LOOKUP, &arg, &res, 0);
161 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
162 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_GETATTR,
163 fhandle, fattr, 0);
164 dprintk("NFS reply lookup: %d\n", status);
165 if (status >= 0)
166 status = nfs_refresh_inode(dir, &dir_attr);
167 return status;
168}
169
170static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
171{
172 struct nfs_fattr fattr;
173 struct nfs3_accessargs arg = {
174 .fh = NFS_FH(inode),
175 };
176 struct nfs3_accessres res = {
177 .fattr = &fattr,
178 };
179 struct rpc_message msg = {
180 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
181 .rpc_argp = &arg,
182 .rpc_resp = &res,
183 .rpc_cred = entry->cred
184 };
185 int mode = entry->mask;
186 int status;
187
188 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (mode & MAY_READ)
191 arg.access |= NFS3_ACCESS_READ;
192 if (S_ISDIR(inode->i_mode)) {
193 if (mode & MAY_WRITE)
194 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
195 if (mode & MAY_EXEC)
196 arg.access |= NFS3_ACCESS_LOOKUP;
197 } else {
198 if (mode & MAY_WRITE)
199 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
200 if (mode & MAY_EXEC)
201 arg.access |= NFS3_ACCESS_EXECUTE;
202 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400203 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
205 nfs_refresh_inode(inode, &fattr);
206 if (status == 0) {
207 entry->mask = 0;
208 if (res.access & NFS3_ACCESS_READ)
209 entry->mask |= MAY_READ;
210 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
211 entry->mask |= MAY_WRITE;
212 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
213 entry->mask |= MAY_EXEC;
214 }
215 dprintk("NFS reply access: %d\n", status);
216 return status;
217}
218
219static int nfs3_proc_readlink(struct inode *inode, struct page *page,
220 unsigned int pgbase, unsigned int pglen)
221{
222 struct nfs_fattr fattr;
223 struct nfs3_readlinkargs args = {
224 .fh = NFS_FH(inode),
225 .pgbase = pgbase,
226 .pglen = pglen,
227 .pages = &page
228 };
229 int status;
230
231 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400232 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_READLINK,
234 &args, &fattr, 0);
235 nfs_refresh_inode(inode, &fattr);
236 dprintk("NFS reply readlink: %d\n", status);
237 return status;
238}
239
240static int nfs3_proc_read(struct nfs_read_data *rdata)
241{
242 int flags = rdata->flags;
243 struct inode * inode = rdata->inode;
244 struct nfs_fattr * fattr = rdata->res.fattr;
245 struct rpc_message msg = {
246 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
247 .rpc_argp = &rdata->args,
248 .rpc_resp = &rdata->res,
249 .rpc_cred = rdata->cred,
250 };
251 int status;
252
253 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
254 (long long) rdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400255 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
257 if (status >= 0)
258 nfs_refresh_inode(inode, fattr);
259 dprintk("NFS reply read: %d\n", status);
260 return status;
261}
262
263static int nfs3_proc_write(struct nfs_write_data *wdata)
264{
265 int rpcflags = wdata->flags;
266 struct inode * inode = wdata->inode;
267 struct nfs_fattr * fattr = wdata->res.fattr;
268 struct rpc_message msg = {
269 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
270 .rpc_argp = &wdata->args,
271 .rpc_resp = &wdata->res,
272 .rpc_cred = wdata->cred,
273 };
274 int status;
275
276 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
277 (long long) wdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400278 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
280 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400281 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dprintk("NFS reply write: %d\n", status);
283 return status < 0? status : wdata->res.count;
284}
285
286static int nfs3_proc_commit(struct nfs_write_data *cdata)
287{
288 struct inode * inode = cdata->inode;
289 struct nfs_fattr * fattr = cdata->res.fattr;
290 struct rpc_message msg = {
291 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
292 .rpc_argp = &cdata->args,
293 .rpc_resp = &cdata->res,
294 .rpc_cred = cdata->cred,
295 };
296 int status;
297
298 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
299 (long long) cdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400300 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
302 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400303 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dprintk("NFS reply commit: %d\n", status);
305 return status;
306}
307
308/*
309 * Create a regular file.
310 * For now, we don't implement O_EXCL.
311 */
312static int
313nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700314 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct nfs_fh fhandle;
317 struct nfs_fattr fattr;
318 struct nfs_fattr dir_attr;
319 struct nfs3_createargs arg = {
320 .fh = NFS_FH(dir),
321 .name = dentry->d_name.name,
322 .len = dentry->d_name.len,
323 .sattr = sattr,
324 };
325 struct nfs3_diropres res = {
326 .dir_attr = &dir_attr,
327 .fh = &fhandle,
328 .fattr = &fattr
329 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000330 mode_t mode = sattr->ia_mode;
331 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 dprintk("NFS call create %s\n", dentry->d_name.name);
334 arg.createmode = NFS3_CREATE_UNCHECKED;
335 if (flags & O_EXCL) {
336 arg.createmode = NFS3_CREATE_EXCLUSIVE;
337 arg.verifier[0] = jiffies;
338 arg.verifier[1] = current->pid;
339 }
340
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000341 sattr->ia_mode &= ~current->fs->umask;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400344 nfs_fattr_init(&dir_attr);
345 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_CREATE, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400347 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* If the server doesn't support the exclusive creation semantics,
350 * try again with simple 'guarded' mode. */
351 if (status == NFSERR_NOTSUPP) {
352 switch (arg.createmode) {
353 case NFS3_CREATE_EXCLUSIVE:
354 arg.createmode = NFS3_CREATE_GUARDED;
355 break;
356
357 case NFS3_CREATE_GUARDED:
358 arg.createmode = NFS3_CREATE_UNCHECKED;
359 break;
360
361 case NFS3_CREATE_UNCHECKED:
362 goto out;
363 }
364 goto again;
365 }
366
367 if (status == 0)
368 status = nfs_instantiate(dentry, &fhandle, &fattr);
369 if (status != 0)
370 goto out;
371
372 /* When we created the file with exclusive semantics, make
373 * sure we set the attributes afterwards. */
374 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
375 dprintk("NFS call setattr (post-create)\n");
376
377 if (!(sattr->ia_valid & ATTR_ATIME_SET))
378 sattr->ia_valid |= ATTR_ATIME;
379 if (!(sattr->ia_valid & ATTR_MTIME_SET))
380 sattr->ia_valid |= ATTR_MTIME;
381
382 /* Note: we could use a guarded setattr here, but I'm
383 * not sure this buys us anything (and I'd have
384 * to revamp the NFSv3 XDR code) */
385 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400386 if (status == 0)
387 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 nfs_refresh_inode(dentry->d_inode, &fattr);
389 dprintk("NFS reply setattr (post-create): %d\n", status);
390 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000391 if (status != 0)
392 goto out;
393 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394out:
395 dprintk("NFS reply create: %d\n", status);
396 return status;
397}
398
399static int
400nfs3_proc_remove(struct inode *dir, struct qstr *name)
401{
402 struct nfs_fattr dir_attr;
403 struct nfs3_diropargs arg = {
404 .fh = NFS_FH(dir),
405 .name = name->name,
406 .len = name->len
407 };
408 struct rpc_message msg = {
409 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
410 .rpc_argp = &arg,
411 .rpc_resp = &dir_attr,
412 };
413 int status;
414
415 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400416 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400418 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dprintk("NFS reply remove: %d\n", status);
420 return status;
421}
422
423static int
424nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
425{
426 struct unlinkxdr {
427 struct nfs3_diropargs arg;
428 struct nfs_fattr res;
429 } *ptr;
430
431 ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
432 if (!ptr)
433 return -ENOMEM;
434 ptr->arg.fh = NFS_FH(dir->d_inode);
435 ptr->arg.name = name->name;
436 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400437 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
439 msg->rpc_argp = &ptr->arg;
440 msg->rpc_resp = &ptr->res;
441 return 0;
442}
443
444static int
445nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
446{
447 struct rpc_message *msg = &task->tk_msg;
448 struct nfs_fattr *dir_attr;
449
450 if (nfs3_async_handle_jukebox(task))
451 return 1;
452 if (msg->rpc_argp) {
453 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400454 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 kfree(msg->rpc_argp);
456 }
457 return 0;
458}
459
460static int
461nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
462 struct inode *new_dir, struct qstr *new_name)
463{
464 struct nfs_fattr old_dir_attr, new_dir_attr;
465 struct nfs3_renameargs arg = {
466 .fromfh = NFS_FH(old_dir),
467 .fromname = old_name->name,
468 .fromlen = old_name->len,
469 .tofh = NFS_FH(new_dir),
470 .toname = new_name->name,
471 .tolen = new_name->len
472 };
473 struct nfs3_renameres res = {
474 .fromattr = &old_dir_attr,
475 .toattr = &new_dir_attr
476 };
477 int status;
478
479 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400480 nfs_fattr_init(&old_dir_attr);
481 nfs_fattr_init(&new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 status = rpc_call(NFS_CLIENT(old_dir), NFS3PROC_RENAME, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400483 nfs_post_op_update_inode(old_dir, &old_dir_attr);
484 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dprintk("NFS reply rename: %d\n", status);
486 return status;
487}
488
489static int
490nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
491{
492 struct nfs_fattr dir_attr, fattr;
493 struct nfs3_linkargs arg = {
494 .fromfh = NFS_FH(inode),
495 .tofh = NFS_FH(dir),
496 .toname = name->name,
497 .tolen = name->len
498 };
499 struct nfs3_linkres res = {
500 .dir_attr = &dir_attr,
501 .fattr = &fattr
502 };
503 int status;
504
505 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400506 nfs_fattr_init(&dir_attr);
507 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_LINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400509 nfs_post_op_update_inode(dir, &dir_attr);
510 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 dprintk("NFS reply link: %d\n", status);
512 return status;
513}
514
515static int
516nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
517 struct iattr *sattr, struct nfs_fh *fhandle,
518 struct nfs_fattr *fattr)
519{
520 struct nfs_fattr dir_attr;
521 struct nfs3_symlinkargs arg = {
522 .fromfh = NFS_FH(dir),
523 .fromname = name->name,
524 .fromlen = name->len,
525 .topath = path->name,
526 .tolen = path->len,
527 .sattr = sattr
528 };
529 struct nfs3_diropres res = {
530 .dir_attr = &dir_attr,
531 .fh = fhandle,
532 .fattr = fattr
533 };
534 int status;
535
536 if (path->len > NFS3_MAXPATHLEN)
537 return -ENAMETOOLONG;
538 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400539 nfs_fattr_init(&dir_attr);
540 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SYMLINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400542 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dprintk("NFS reply symlink: %d\n", status);
544 return status;
545}
546
547static int
548nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
549{
550 struct nfs_fh fhandle;
551 struct nfs_fattr fattr, dir_attr;
552 struct nfs3_mkdirargs arg = {
553 .fh = NFS_FH(dir),
554 .name = dentry->d_name.name,
555 .len = dentry->d_name.len,
556 .sattr = sattr
557 };
558 struct nfs3_diropres res = {
559 .dir_attr = &dir_attr,
560 .fh = &fhandle,
561 .fattr = &fattr
562 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000563 int mode = sattr->ia_mode;
564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000567
568 sattr->ia_mode &= ~current->fs->umask;
569
Trond Myklebust0e574af2005-10-27 22:12:38 -0400570 nfs_fattr_init(&dir_attr);
571 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKDIR, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400573 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000574 if (status != 0)
575 goto out;
576 status = nfs_instantiate(dentry, &fhandle, &fattr);
577 if (status != 0)
578 goto out;
579 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
580out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dprintk("NFS reply mkdir: %d\n", status);
582 return status;
583}
584
585static int
586nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
587{
588 struct nfs_fattr dir_attr;
589 struct nfs3_diropargs arg = {
590 .fh = NFS_FH(dir),
591 .name = name->name,
592 .len = name->len
593 };
594 int status;
595
596 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400597 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400599 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 dprintk("NFS reply rmdir: %d\n", status);
601 return status;
602}
603
604/*
605 * The READDIR implementation is somewhat hackish - we pass the user buffer
606 * to the encode function, which installs it in the receive iovec.
607 * The decode function itself doesn't perform any decoding, it just makes
608 * sure the reply is syntactically correct.
609 *
610 * Also note that this implementation handles both plain readdir and
611 * readdirplus.
612 */
613static int
614nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
615 u64 cookie, struct page *page, unsigned int count, int plus)
616{
617 struct inode *dir = dentry->d_inode;
618 struct nfs_fattr dir_attr;
619 u32 *verf = NFS_COOKIEVERF(dir);
620 struct nfs3_readdirargs arg = {
621 .fh = NFS_FH(dir),
622 .cookie = cookie,
623 .verf = {verf[0], verf[1]},
624 .plus = plus,
625 .count = count,
626 .pages = &page
627 };
628 struct nfs3_readdirres res = {
629 .dir_attr = &dir_attr,
630 .verf = verf,
631 .plus = plus
632 };
633 struct rpc_message msg = {
634 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
635 .rpc_argp = &arg,
636 .rpc_resp = &res,
637 .rpc_cred = cred
638 };
639 int status;
640
641 lock_kernel();
642
643 if (plus)
644 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
645
646 dprintk("NFS call readdir%s %d\n",
647 plus? "plus" : "", (unsigned int) cookie);
648
Trond Myklebust0e574af2005-10-27 22:12:38 -0400649 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
651 nfs_refresh_inode(dir, &dir_attr);
652 dprintk("NFS reply readdir: %d\n", status);
653 unlock_kernel();
654 return status;
655}
656
657static int
658nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
659 dev_t rdev)
660{
661 struct nfs_fh fh;
662 struct nfs_fattr fattr, dir_attr;
663 struct nfs3_mknodargs arg = {
664 .fh = NFS_FH(dir),
665 .name = dentry->d_name.name,
666 .len = dentry->d_name.len,
667 .sattr = sattr,
668 .rdev = rdev
669 };
670 struct nfs3_diropres res = {
671 .dir_attr = &dir_attr,
672 .fh = &fh,
673 .fattr = &fattr
674 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000675 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int status;
677
678 switch (sattr->ia_mode & S_IFMT) {
679 case S_IFBLK: arg.type = NF3BLK; break;
680 case S_IFCHR: arg.type = NF3CHR; break;
681 case S_IFIFO: arg.type = NF3FIFO; break;
682 case S_IFSOCK: arg.type = NF3SOCK; break;
683 default: return -EINVAL;
684 }
685
686 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
687 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000688
689 sattr->ia_mode &= ~current->fs->umask;
690
Trond Myklebust0e574af2005-10-27 22:12:38 -0400691 nfs_fattr_init(&dir_attr);
692 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKNOD, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400694 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000695 if (status != 0)
696 goto out;
697 status = nfs_instantiate(dentry, &fh, &fattr);
698 if (status != 0)
699 goto out;
700 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
701out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 dprintk("NFS reply mknod: %d\n", status);
703 return status;
704}
705
706static int
707nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
708 struct nfs_fsstat *stat)
709{
710 int status;
711
712 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400713 nfs_fattr_init(stat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, stat, 0);
715 dprintk("NFS reply statfs: %d\n", status);
716 return status;
717}
718
719static int
720nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
721 struct nfs_fsinfo *info)
722{
723 int status;
724
725 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400726 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
728 dprintk("NFS reply fsinfo: %d\n", status);
729 return status;
730}
731
732static int
733nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
734 struct nfs_pathconf *info)
735{
736 int status;
737
738 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400739 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 status = rpc_call(server->client, NFS3PROC_PATHCONF, fhandle, info, 0);
741 dprintk("NFS reply pathconf: %d\n", status);
742 return status;
743}
744
745extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
746
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100747static void nfs3_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100749 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (nfs3_async_handle_jukebox(task))
752 return;
753 /* Call back common NFS readpage processing */
754 if (task->tk_status >= 0)
755 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100756 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100759static const struct rpc_call_ops nfs3_read_ops = {
760 .rpc_call_done = nfs3_read_done,
761 .rpc_release = nfs_readdata_release,
762};
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764static void
765nfs3_proc_read_setup(struct nfs_read_data *data)
766{
767 struct rpc_task *task = &data->task;
768 struct inode *inode = data->inode;
769 int flags;
770 struct rpc_message msg = {
771 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
772 .rpc_argp = &data->args,
773 .rpc_resp = &data->res,
774 .rpc_cred = data->cred,
775 };
776
777 /* N.B. Do we need to test? Never called for swapfile inode */
778 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
779
780 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100781 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 rpc_call_setup(task, &msg, 0);
783}
784
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100785static void nfs3_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100787 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (nfs3_async_handle_jukebox(task))
790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400792 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100793 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100796static const struct rpc_call_ops nfs3_write_ops = {
797 .rpc_call_done = nfs3_write_done,
798 .rpc_release = nfs_writedata_release,
799};
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static void
802nfs3_proc_write_setup(struct nfs_write_data *data, int how)
803{
804 struct rpc_task *task = &data->task;
805 struct inode *inode = data->inode;
806 int stable;
807 int flags;
808 struct rpc_message msg = {
809 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
810 .rpc_argp = &data->args,
811 .rpc_resp = &data->res,
812 .rpc_cred = data->cred,
813 };
814
815 if (how & FLUSH_STABLE) {
816 if (!NFS_I(inode)->ncommit)
817 stable = NFS_FILE_SYNC;
818 else
819 stable = NFS_DATA_SYNC;
820 } else
821 stable = NFS_UNSTABLE;
822 data->args.stable = stable;
823
824 /* Set the initial flags for the task. */
825 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
826
827 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100828 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 rpc_call_setup(task, &msg, 0);
830}
831
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100832static void nfs3_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100834 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (nfs3_async_handle_jukebox(task))
837 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400839 nfs_post_op_update_inode(data->inode, data->res.fattr);
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100840 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100843static const struct rpc_call_ops nfs3_commit_ops = {
844 .rpc_call_done = nfs3_commit_done,
845 .rpc_release = nfs_commit_release,
846};
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static void
849nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
850{
851 struct rpc_task *task = &data->task;
852 struct inode *inode = data->inode;
853 int flags;
854 struct rpc_message msg = {
855 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
856 .rpc_argp = &data->args,
857 .rpc_resp = &data->res,
858 .rpc_cred = data->cred,
859 };
860
861 /* Set the initial flags for the task. */
862 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
863
864 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100865 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 rpc_call_setup(task, &msg, 0);
867}
868
869static int
870nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
871{
872 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
873}
874
875struct nfs_rpc_ops nfs_v3_clientops = {
876 .version = 3, /* protocol version */
877 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000878 .dir_inode_ops = &nfs3_dir_inode_operations,
879 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .getroot = nfs3_proc_get_root,
881 .getattr = nfs3_proc_getattr,
882 .setattr = nfs3_proc_setattr,
883 .lookup = nfs3_proc_lookup,
884 .access = nfs3_proc_access,
885 .readlink = nfs3_proc_readlink,
886 .read = nfs3_proc_read,
887 .write = nfs3_proc_write,
888 .commit = nfs3_proc_commit,
889 .create = nfs3_proc_create,
890 .remove = nfs3_proc_remove,
891 .unlink_setup = nfs3_proc_unlink_setup,
892 .unlink_done = nfs3_proc_unlink_done,
893 .rename = nfs3_proc_rename,
894 .link = nfs3_proc_link,
895 .symlink = nfs3_proc_symlink,
896 .mkdir = nfs3_proc_mkdir,
897 .rmdir = nfs3_proc_rmdir,
898 .readdir = nfs3_proc_readdir,
899 .mknod = nfs3_proc_mknod,
900 .statfs = nfs3_proc_statfs,
901 .fsinfo = nfs3_proc_fsinfo,
902 .pathconf = nfs3_proc_pathconf,
903 .decode_dirent = nfs3_decode_dirent,
904 .read_setup = nfs3_proc_read_setup,
905 .write_setup = nfs3_proc_write_setup,
906 .commit_setup = nfs3_proc_commit_setup,
907 .file_open = nfs_open,
908 .file_release = nfs_release,
909 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000910 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};