blob: 92c870d19ccdbe61a44d4dcb110c83ef9067e3da [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
71/*
72 * Bare-bones access to getattr: this is for nfs_read_super.
73 */
74static int
75nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
76 struct nfs_fsinfo *info)
77{
78 int status;
79
80 dprintk("%s: call fsinfo\n", __FUNCTION__);
Trond Myklebust0e574af2005-10-27 22:12:38 -040081 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
83 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
84 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
85 status = rpc_call(server->client_sys, NFS3PROC_GETATTR, fhandle, info->fattr, 0);
86 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
87 }
88 return status;
89}
90
91/*
92 * One function for each procedure in the NFS protocol.
93 */
94static int
95nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
96 struct nfs_fattr *fattr)
97{
98 int status;
99
100 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400101 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 status = rpc_call(server->client, NFS3PROC_GETATTR,
103 fhandle, fattr, 0);
104 dprintk("NFS reply getattr: %d\n", status);
105 return status;
106}
107
108static int
109nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
110 struct iattr *sattr)
111{
112 struct inode *inode = dentry->d_inode;
113 struct nfs3_sattrargs arg = {
114 .fh = NFS_FH(inode),
115 .sattr = sattr,
116 };
117 int status;
118
119 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400120 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_SETATTR, &arg, fattr, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400122 if (status == 0)
123 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dprintk("NFS reply setattr: %d\n", status);
125 return status;
126}
127
128static int
129nfs3_proc_lookup(struct inode *dir, struct qstr *name,
130 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
131{
132 struct nfs_fattr dir_attr;
133 struct nfs3_diropargs arg = {
134 .fh = NFS_FH(dir),
135 .name = name->name,
136 .len = name->len
137 };
138 struct nfs3_diropres res = {
139 .dir_attr = &dir_attr,
140 .fh = fhandle,
141 .fattr = fattr
142 };
143 int status;
144
145 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400146 nfs_fattr_init(&dir_attr);
147 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_LOOKUP, &arg, &res, 0);
149 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
150 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_GETATTR,
151 fhandle, fattr, 0);
152 dprintk("NFS reply lookup: %d\n", status);
153 if (status >= 0)
154 status = nfs_refresh_inode(dir, &dir_attr);
155 return status;
156}
157
158static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
159{
160 struct nfs_fattr fattr;
161 struct nfs3_accessargs arg = {
162 .fh = NFS_FH(inode),
163 };
164 struct nfs3_accessres res = {
165 .fattr = &fattr,
166 };
167 struct rpc_message msg = {
168 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
169 .rpc_argp = &arg,
170 .rpc_resp = &res,
171 .rpc_cred = entry->cred
172 };
173 int mode = entry->mask;
174 int status;
175
176 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (mode & MAY_READ)
179 arg.access |= NFS3_ACCESS_READ;
180 if (S_ISDIR(inode->i_mode)) {
181 if (mode & MAY_WRITE)
182 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
183 if (mode & MAY_EXEC)
184 arg.access |= NFS3_ACCESS_LOOKUP;
185 } else {
186 if (mode & MAY_WRITE)
187 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
188 if (mode & MAY_EXEC)
189 arg.access |= NFS3_ACCESS_EXECUTE;
190 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400191 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
193 nfs_refresh_inode(inode, &fattr);
194 if (status == 0) {
195 entry->mask = 0;
196 if (res.access & NFS3_ACCESS_READ)
197 entry->mask |= MAY_READ;
198 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
199 entry->mask |= MAY_WRITE;
200 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
201 entry->mask |= MAY_EXEC;
202 }
203 dprintk("NFS reply access: %d\n", status);
204 return status;
205}
206
207static int nfs3_proc_readlink(struct inode *inode, struct page *page,
208 unsigned int pgbase, unsigned int pglen)
209{
210 struct nfs_fattr fattr;
211 struct nfs3_readlinkargs args = {
212 .fh = NFS_FH(inode),
213 .pgbase = pgbase,
214 .pglen = pglen,
215 .pages = &page
216 };
217 int status;
218
219 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400220 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_READLINK,
222 &args, &fattr, 0);
223 nfs_refresh_inode(inode, &fattr);
224 dprintk("NFS reply readlink: %d\n", status);
225 return status;
226}
227
228static int nfs3_proc_read(struct nfs_read_data *rdata)
229{
230 int flags = rdata->flags;
231 struct inode * inode = rdata->inode;
232 struct nfs_fattr * fattr = rdata->res.fattr;
233 struct rpc_message msg = {
234 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
235 .rpc_argp = &rdata->args,
236 .rpc_resp = &rdata->res,
237 .rpc_cred = rdata->cred,
238 };
239 int status;
240
241 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
242 (long long) rdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400243 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
245 if (status >= 0)
246 nfs_refresh_inode(inode, fattr);
247 dprintk("NFS reply read: %d\n", status);
248 return status;
249}
250
251static int nfs3_proc_write(struct nfs_write_data *wdata)
252{
253 int rpcflags = wdata->flags;
254 struct inode * inode = wdata->inode;
255 struct nfs_fattr * fattr = wdata->res.fattr;
256 struct rpc_message msg = {
257 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
258 .rpc_argp = &wdata->args,
259 .rpc_resp = &wdata->res,
260 .rpc_cred = wdata->cred,
261 };
262 int status;
263
264 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
265 (long long) wdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400266 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
268 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400269 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dprintk("NFS reply write: %d\n", status);
271 return status < 0? status : wdata->res.count;
272}
273
274static int nfs3_proc_commit(struct nfs_write_data *cdata)
275{
276 struct inode * inode = cdata->inode;
277 struct nfs_fattr * fattr = cdata->res.fattr;
278 struct rpc_message msg = {
279 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
280 .rpc_argp = &cdata->args,
281 .rpc_resp = &cdata->res,
282 .rpc_cred = cdata->cred,
283 };
284 int status;
285
286 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
287 (long long) cdata->args.offset);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400288 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
290 if (status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400291 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 dprintk("NFS reply commit: %d\n", status);
293 return status;
294}
295
296/*
297 * Create a regular file.
298 * For now, we don't implement O_EXCL.
299 */
300static int
301nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700302 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct nfs_fh fhandle;
305 struct nfs_fattr fattr;
306 struct nfs_fattr dir_attr;
307 struct nfs3_createargs arg = {
308 .fh = NFS_FH(dir),
309 .name = dentry->d_name.name,
310 .len = dentry->d_name.len,
311 .sattr = sattr,
312 };
313 struct nfs3_diropres res = {
314 .dir_attr = &dir_attr,
315 .fh = &fhandle,
316 .fattr = &fattr
317 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000318 mode_t mode = sattr->ia_mode;
319 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 dprintk("NFS call create %s\n", dentry->d_name.name);
322 arg.createmode = NFS3_CREATE_UNCHECKED;
323 if (flags & O_EXCL) {
324 arg.createmode = NFS3_CREATE_EXCLUSIVE;
325 arg.verifier[0] = jiffies;
326 arg.verifier[1] = current->pid;
327 }
328
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000329 sattr->ia_mode &= ~current->fs->umask;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400332 nfs_fattr_init(&dir_attr);
333 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_CREATE, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400335 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* If the server doesn't support the exclusive creation semantics,
338 * try again with simple 'guarded' mode. */
339 if (status == NFSERR_NOTSUPP) {
340 switch (arg.createmode) {
341 case NFS3_CREATE_EXCLUSIVE:
342 arg.createmode = NFS3_CREATE_GUARDED;
343 break;
344
345 case NFS3_CREATE_GUARDED:
346 arg.createmode = NFS3_CREATE_UNCHECKED;
347 break;
348
349 case NFS3_CREATE_UNCHECKED:
350 goto out;
351 }
352 goto again;
353 }
354
355 if (status == 0)
356 status = nfs_instantiate(dentry, &fhandle, &fattr);
357 if (status != 0)
358 goto out;
359
360 /* When we created the file with exclusive semantics, make
361 * sure we set the attributes afterwards. */
362 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
363 dprintk("NFS call setattr (post-create)\n");
364
365 if (!(sattr->ia_valid & ATTR_ATIME_SET))
366 sattr->ia_valid |= ATTR_ATIME;
367 if (!(sattr->ia_valid & ATTR_MTIME_SET))
368 sattr->ia_valid |= ATTR_MTIME;
369
370 /* Note: we could use a guarded setattr here, but I'm
371 * not sure this buys us anything (and I'd have
372 * to revamp the NFSv3 XDR code) */
373 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebust65e43082005-08-16 11:49:44 -0400374 if (status == 0)
375 nfs_setattr_update_inode(dentry->d_inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 nfs_refresh_inode(dentry->d_inode, &fattr);
377 dprintk("NFS reply setattr (post-create): %d\n", status);
378 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000379 if (status != 0)
380 goto out;
381 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382out:
383 dprintk("NFS reply create: %d\n", status);
384 return status;
385}
386
387static int
388nfs3_proc_remove(struct inode *dir, struct qstr *name)
389{
390 struct nfs_fattr dir_attr;
391 struct nfs3_diropargs arg = {
392 .fh = NFS_FH(dir),
393 .name = name->name,
394 .len = name->len
395 };
396 struct rpc_message msg = {
397 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
398 .rpc_argp = &arg,
399 .rpc_resp = &dir_attr,
400 };
401 int status;
402
403 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400404 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400406 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dprintk("NFS reply remove: %d\n", status);
408 return status;
409}
410
411static int
412nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
413{
414 struct unlinkxdr {
415 struct nfs3_diropargs arg;
416 struct nfs_fattr res;
417 } *ptr;
418
419 ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
420 if (!ptr)
421 return -ENOMEM;
422 ptr->arg.fh = NFS_FH(dir->d_inode);
423 ptr->arg.name = name->name;
424 ptr->arg.len = name->len;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400425 nfs_fattr_init(&ptr->res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
427 msg->rpc_argp = &ptr->arg;
428 msg->rpc_resp = &ptr->res;
429 return 0;
430}
431
432static int
433nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
434{
435 struct rpc_message *msg = &task->tk_msg;
436 struct nfs_fattr *dir_attr;
437
438 if (nfs3_async_handle_jukebox(task))
439 return 1;
440 if (msg->rpc_argp) {
441 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400442 nfs_post_op_update_inode(dir->d_inode, dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 kfree(msg->rpc_argp);
444 }
445 return 0;
446}
447
448static int
449nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
450 struct inode *new_dir, struct qstr *new_name)
451{
452 struct nfs_fattr old_dir_attr, new_dir_attr;
453 struct nfs3_renameargs arg = {
454 .fromfh = NFS_FH(old_dir),
455 .fromname = old_name->name,
456 .fromlen = old_name->len,
457 .tofh = NFS_FH(new_dir),
458 .toname = new_name->name,
459 .tolen = new_name->len
460 };
461 struct nfs3_renameres res = {
462 .fromattr = &old_dir_attr,
463 .toattr = &new_dir_attr
464 };
465 int status;
466
467 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400468 nfs_fattr_init(&old_dir_attr);
469 nfs_fattr_init(&new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 status = rpc_call(NFS_CLIENT(old_dir), NFS3PROC_RENAME, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400471 nfs_post_op_update_inode(old_dir, &old_dir_attr);
472 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dprintk("NFS reply rename: %d\n", status);
474 return status;
475}
476
477static int
478nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
479{
480 struct nfs_fattr dir_attr, fattr;
481 struct nfs3_linkargs arg = {
482 .fromfh = NFS_FH(inode),
483 .tofh = NFS_FH(dir),
484 .toname = name->name,
485 .tolen = name->len
486 };
487 struct nfs3_linkres res = {
488 .dir_attr = &dir_attr,
489 .fattr = &fattr
490 };
491 int status;
492
493 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400494 nfs_fattr_init(&dir_attr);
495 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 status = rpc_call(NFS_CLIENT(inode), NFS3PROC_LINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400497 nfs_post_op_update_inode(dir, &dir_attr);
498 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dprintk("NFS reply link: %d\n", status);
500 return status;
501}
502
503static int
504nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
505 struct iattr *sattr, struct nfs_fh *fhandle,
506 struct nfs_fattr *fattr)
507{
508 struct nfs_fattr dir_attr;
509 struct nfs3_symlinkargs arg = {
510 .fromfh = NFS_FH(dir),
511 .fromname = name->name,
512 .fromlen = name->len,
513 .topath = path->name,
514 .tolen = path->len,
515 .sattr = sattr
516 };
517 struct nfs3_diropres res = {
518 .dir_attr = &dir_attr,
519 .fh = fhandle,
520 .fattr = fattr
521 };
522 int status;
523
524 if (path->len > NFS3_MAXPATHLEN)
525 return -ENAMETOOLONG;
526 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400527 nfs_fattr_init(&dir_attr);
528 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SYMLINK, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400530 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dprintk("NFS reply symlink: %d\n", status);
532 return status;
533}
534
535static int
536nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
537{
538 struct nfs_fh fhandle;
539 struct nfs_fattr fattr, dir_attr;
540 struct nfs3_mkdirargs arg = {
541 .fh = NFS_FH(dir),
542 .name = dentry->d_name.name,
543 .len = dentry->d_name.len,
544 .sattr = sattr
545 };
546 struct nfs3_diropres res = {
547 .dir_attr = &dir_attr,
548 .fh = &fhandle,
549 .fattr = &fattr
550 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000551 int mode = sattr->ia_mode;
552 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000555
556 sattr->ia_mode &= ~current->fs->umask;
557
Trond Myklebust0e574af2005-10-27 22:12:38 -0400558 nfs_fattr_init(&dir_attr);
559 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKDIR, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400561 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000562 if (status != 0)
563 goto out;
564 status = nfs_instantiate(dentry, &fhandle, &fattr);
565 if (status != 0)
566 goto out;
567 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dprintk("NFS reply mkdir: %d\n", status);
570 return status;
571}
572
573static int
574nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
575{
576 struct nfs_fattr dir_attr;
577 struct nfs3_diropargs arg = {
578 .fh = NFS_FH(dir),
579 .name = name->name,
580 .len = name->len
581 };
582 int status;
583
584 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400585 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400587 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 dprintk("NFS reply rmdir: %d\n", status);
589 return status;
590}
591
592/*
593 * The READDIR implementation is somewhat hackish - we pass the user buffer
594 * to the encode function, which installs it in the receive iovec.
595 * The decode function itself doesn't perform any decoding, it just makes
596 * sure the reply is syntactically correct.
597 *
598 * Also note that this implementation handles both plain readdir and
599 * readdirplus.
600 */
601static int
602nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
603 u64 cookie, struct page *page, unsigned int count, int plus)
604{
605 struct inode *dir = dentry->d_inode;
606 struct nfs_fattr dir_attr;
607 u32 *verf = NFS_COOKIEVERF(dir);
608 struct nfs3_readdirargs arg = {
609 .fh = NFS_FH(dir),
610 .cookie = cookie,
611 .verf = {verf[0], verf[1]},
612 .plus = plus,
613 .count = count,
614 .pages = &page
615 };
616 struct nfs3_readdirres res = {
617 .dir_attr = &dir_attr,
618 .verf = verf,
619 .plus = plus
620 };
621 struct rpc_message msg = {
622 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
623 .rpc_argp = &arg,
624 .rpc_resp = &res,
625 .rpc_cred = cred
626 };
627 int status;
628
629 lock_kernel();
630
631 if (plus)
632 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
633
634 dprintk("NFS call readdir%s %d\n",
635 plus? "plus" : "", (unsigned int) cookie);
636
Trond Myklebust0e574af2005-10-27 22:12:38 -0400637 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
639 nfs_refresh_inode(dir, &dir_attr);
640 dprintk("NFS reply readdir: %d\n", status);
641 unlock_kernel();
642 return status;
643}
644
645static int
646nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
647 dev_t rdev)
648{
649 struct nfs_fh fh;
650 struct nfs_fattr fattr, dir_attr;
651 struct nfs3_mknodargs arg = {
652 .fh = NFS_FH(dir),
653 .name = dentry->d_name.name,
654 .len = dentry->d_name.len,
655 .sattr = sattr,
656 .rdev = rdev
657 };
658 struct nfs3_diropres res = {
659 .dir_attr = &dir_attr,
660 .fh = &fh,
661 .fattr = &fattr
662 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000663 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int status;
665
666 switch (sattr->ia_mode & S_IFMT) {
667 case S_IFBLK: arg.type = NF3BLK; break;
668 case S_IFCHR: arg.type = NF3CHR; break;
669 case S_IFIFO: arg.type = NF3FIFO; break;
670 case S_IFSOCK: arg.type = NF3SOCK; break;
671 default: return -EINVAL;
672 }
673
674 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
675 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000676
677 sattr->ia_mode &= ~current->fs->umask;
678
Trond Myklebust0e574af2005-10-27 22:12:38 -0400679 nfs_fattr_init(&dir_attr);
680 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKNOD, &arg, &res, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400682 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000683 if (status != 0)
684 goto out;
685 status = nfs_instantiate(dentry, &fh, &fattr);
686 if (status != 0)
687 goto out;
688 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
689out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dprintk("NFS reply mknod: %d\n", status);
691 return status;
692}
693
694static int
695nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
696 struct nfs_fsstat *stat)
697{
698 int status;
699
700 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400701 nfs_fattr_init(stat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, stat, 0);
703 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{
711 int status;
712
713 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400714 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
716 dprintk("NFS reply fsinfo: %d\n", status);
717 return status;
718}
719
720static int
721nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
722 struct nfs_pathconf *info)
723{
724 int status;
725
726 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400727 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 status = rpc_call(server->client, NFS3PROC_PATHCONF, fhandle, info, 0);
729 dprintk("NFS reply pathconf: %d\n", status);
730 return status;
731}
732
733extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
734
735static void
736nfs3_read_done(struct rpc_task *task)
737{
Trond Myklebust34123da2005-10-27 19:10:09 -0400738 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (nfs3_async_handle_jukebox(task))
741 return;
742 /* Call back common NFS readpage processing */
743 if (task->tk_status >= 0)
744 nfs_refresh_inode(data->inode, &data->fattr);
745 nfs_readpage_result(task);
746}
747
748static void
749nfs3_proc_read_setup(struct nfs_read_data *data)
750{
751 struct rpc_task *task = &data->task;
752 struct inode *inode = data->inode;
753 int flags;
754 struct rpc_message msg = {
755 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
756 .rpc_argp = &data->args,
757 .rpc_resp = &data->res,
758 .rpc_cred = data->cred,
759 };
760
761 /* N.B. Do we need to test? Never called for swapfile inode */
762 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
763
764 /* Finalize the task. */
765 rpc_init_task(task, NFS_CLIENT(inode), nfs3_read_done, flags);
766 rpc_call_setup(task, &msg, 0);
767}
768
769static void
770nfs3_write_done(struct rpc_task *task)
771{
772 struct nfs_write_data *data;
773
774 if (nfs3_async_handle_jukebox(task))
775 return;
776 data = (struct nfs_write_data *)task->tk_calldata;
777 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400778 nfs_post_op_update_inode(data->inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 nfs_writeback_done(task);
780}
781
782static void
783nfs3_proc_write_setup(struct nfs_write_data *data, int how)
784{
785 struct rpc_task *task = &data->task;
786 struct inode *inode = data->inode;
787 int stable;
788 int flags;
789 struct rpc_message msg = {
790 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
791 .rpc_argp = &data->args,
792 .rpc_resp = &data->res,
793 .rpc_cred = data->cred,
794 };
795
796 if (how & FLUSH_STABLE) {
797 if (!NFS_I(inode)->ncommit)
798 stable = NFS_FILE_SYNC;
799 else
800 stable = NFS_DATA_SYNC;
801 } else
802 stable = NFS_UNSTABLE;
803 data->args.stable = stable;
804
805 /* Set the initial flags for the task. */
806 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
807
808 /* Finalize the task. */
809 rpc_init_task(task, NFS_CLIENT(inode), nfs3_write_done, flags);
810 rpc_call_setup(task, &msg, 0);
811}
812
813static void
814nfs3_commit_done(struct rpc_task *task)
815{
816 struct nfs_write_data *data;
817
818 if (nfs3_async_handle_jukebox(task))
819 return;
820 data = (struct nfs_write_data *)task->tk_calldata;
821 if (task->tk_status >= 0)
Trond Myklebustdecf4912005-10-27 22:12:39 -0400822 nfs_post_op_update_inode(data->inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 nfs_commit_done(task);
824}
825
826static void
827nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
828{
829 struct rpc_task *task = &data->task;
830 struct inode *inode = data->inode;
831 int flags;
832 struct rpc_message msg = {
833 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
834 .rpc_argp = &data->args,
835 .rpc_resp = &data->res,
836 .rpc_cred = data->cred,
837 };
838
839 /* Set the initial flags for the task. */
840 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
841
842 /* Finalize the task. */
843 rpc_init_task(task, NFS_CLIENT(inode), nfs3_commit_done, flags);
844 rpc_call_setup(task, &msg, 0);
845}
846
847static int
848nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
849{
850 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
851}
852
853struct nfs_rpc_ops nfs_v3_clientops = {
854 .version = 3, /* protocol version */
855 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000856 .dir_inode_ops = &nfs3_dir_inode_operations,
857 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .getroot = nfs3_proc_get_root,
859 .getattr = nfs3_proc_getattr,
860 .setattr = nfs3_proc_setattr,
861 .lookup = nfs3_proc_lookup,
862 .access = nfs3_proc_access,
863 .readlink = nfs3_proc_readlink,
864 .read = nfs3_proc_read,
865 .write = nfs3_proc_write,
866 .commit = nfs3_proc_commit,
867 .create = nfs3_proc_create,
868 .remove = nfs3_proc_remove,
869 .unlink_setup = nfs3_proc_unlink_setup,
870 .unlink_done = nfs3_proc_unlink_done,
871 .rename = nfs3_proc_rename,
872 .link = nfs3_proc_link,
873 .symlink = nfs3_proc_symlink,
874 .mkdir = nfs3_proc_mkdir,
875 .rmdir = nfs3_proc_rmdir,
876 .readdir = nfs3_proc_readdir,
877 .mknod = nfs3_proc_mknod,
878 .statfs = nfs3_proc_statfs,
879 .fsinfo = nfs3_proc_fsinfo,
880 .pathconf = nfs3_proc_pathconf,
881 .decode_dirent = nfs3_decode_dirent,
882 .read_setup = nfs3_proc_read_setup,
883 .write_setup = nfs3_proc_write_setup,
884 .commit_setup = nfs3_proc_commit_setup,
885 .file_open = nfs_open,
886 .file_release = nfs_release,
887 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000888 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889};