]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfs/nfs3proc.c
NFS: Cleanup of NFS write code in preparation for asynchronous o_direct
[net-next-2.6.git] / fs / nfs / nfs3proc.c
CommitLineData
1da177e4
LT
1/*
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>
b7fa0554 20#include <linux/nfs_mount.h>
1da177e4 21
006ea73e
CL
22#include "iostat.h"
23
1da177e4
LT
24#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;
041e0e3b 39 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
1da177e4
LT
40 res = -ERESTARTSYS;
41 } while (!signalled());
42 rpc_clnt_sigunmask(clnt, &oldset);
43 return res;
44}
45
dead28da 46#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
1da177e4
LT
47
48static int
006ea73e 49nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
1da177e4
LT
50{
51 if (task->tk_status != -EJUKEBOX)
52 return 0;
006ea73e 53 nfs_inc_stats(inode, NFSIOS_DELAY);
1da177e4
LT
54 task->tk_status = 0;
55 rpc_restart_call(task);
56 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
57 return 1;
58}
59
1da177e4 60static int
03c21733
BF
61do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
62 struct nfs_fsinfo *info)
1da177e4 63{
dead28da
CL
64 struct rpc_message msg = {
65 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
66 .rpc_argp = fhandle,
67 .rpc_resp = info,
68 };
1da177e4
LT
69 int status;
70
71 dprintk("%s: call fsinfo\n", __FUNCTION__);
0e574af1 72 nfs_fattr_init(info->fattr);
dead28da 73 status = rpc_call_sync(client, &msg, 0);
1da177e4
LT
74 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
75 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
dead28da
CL
76 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
77 msg.rpc_resp = info->fattr;
78 status = rpc_call_sync(client, &msg, 0);
1da177e4
LT
79 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
80 }
81 return status;
82}
83
03c21733
BF
84/*
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
93 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);
96 return status;
97}
98
1da177e4
LT
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{
dead28da
CL
106 struct rpc_message msg = {
107 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
108 .rpc_argp = fhandle,
109 .rpc_resp = fattr,
110 };
1da177e4
LT
111 int status;
112
113 dprintk("NFS call getattr\n");
0e574af1 114 nfs_fattr_init(fattr);
dead28da 115 status = rpc_call_sync(server->client, &msg, 0);
1da177e4
LT
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 };
dead28da
CL
129 struct rpc_message msg = {
130 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
131 .rpc_argp = &arg,
132 .rpc_resp = fattr,
133 };
1da177e4
LT
134 int status;
135
136 dprintk("NFS call setattr\n");
0e574af1 137 nfs_fattr_init(fattr);
dead28da 138 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
65e4308d
TM
139 if (status == 0)
140 nfs_setattr_update_inode(inode, sattr);
1da177e4
LT
141 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 };
dead28da
CL
160 struct rpc_message msg = {
161 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
162 .rpc_argp = &arg,
163 .rpc_resp = &res,
164 };
1da177e4
LT
165 int status;
166
167 dprintk("NFS call lookup %s\n", name->name);
0e574af1
TM
168 nfs_fattr_init(&dir_attr);
169 nfs_fattr_init(fattr);
dead28da
CL
170 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 }
1da177e4
LT
177 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,
dead28da 196 .rpc_cred = entry->cred,
1da177e4
LT
197 };
198 int mode = entry->mask;
199 int status;
200
201 dprintk("NFS call access\n");
1da177e4
LT
202
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 }
0e574af1 216 nfs_fattr_init(&fattr);
1da177e4
LT
217 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 };
dead28da
CL
242 struct rpc_message msg = {
243 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
244 .rpc_argp = &args,
245 .rpc_resp = &fattr,
246 };
1da177e4
LT
247 int status;
248
249 dprintk("NFS call readlink\n");
0e574af1 250 nfs_fattr_init(&fattr);
dead28da 251 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1da177e4
LT
252 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);
0e574af1 272 nfs_fattr_init(fattr);
1da177e4
LT
273 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);
0e574af1 295 nfs_fattr_init(fattr);
1da177e4
LT
296 status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
297 if (status >= 0)
decf491f 298 nfs_post_op_update_inode(inode, fattr);
1da177e4
LT
299 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);
0e574af1 317 nfs_fattr_init(fattr);
1da177e4
LT
318 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
319 if (status >= 0)
decf491f 320 nfs_post_op_update_inode(inode, fattr);
1da177e4
LT
321 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,
02a913a7 331 int flags, struct nameidata *nd)
1da177e4
LT
332{
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 };
dead28da
CL
347 struct rpc_message msg = {
348 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
349 .rpc_argp = &arg,
350 .rpc_resp = &res,
351 };
055ffbea
AG
352 mode_t mode = sattr->ia_mode;
353 int status;
1da177e4
LT
354
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
055ffbea
AG
363 sattr->ia_mode &= ~current->fs->umask;
364
1da177e4 365again:
0e574af1
TM
366 nfs_fattr_init(&dir_attr);
367 nfs_fattr_init(&fattr);
dead28da
CL
368 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
369 nfs_refresh_inode(dir, &dir_attr);
1da177e4
LT
370
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);
65e4308d
TM
408 if (status == 0)
409 nfs_setattr_update_inode(dentry->d_inode, sattr);
1da177e4
LT
410 nfs_refresh_inode(dentry->d_inode, &fattr);
411 dprintk("NFS reply setattr (post-create): %d\n", status);
412 }
055ffbea
AG
413 if (status != 0)
414 goto out;
415 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
1da177e4
LT
416out:
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);
0e574af1 438 nfs_fattr_init(&dir_attr);
1da177e4 439 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
decf491f 440 nfs_post_op_update_inode(dir, &dir_attr);
1da177e4
LT
441 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;
0e574af1 459 nfs_fattr_init(&ptr->res);
1da177e4
LT
460 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
006ea73e 472 if (nfs3_async_handle_jukebox(task, dir->d_inode))
1da177e4
LT
473 return 1;
474 if (msg->rpc_argp) {
475 dir_attr = (struct nfs_fattr*)msg->rpc_resp;
decf491f 476 nfs_post_op_update_inode(dir->d_inode, dir_attr);
1da177e4
LT
477 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 };
dead28da
CL
499 struct rpc_message msg = {
500 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
501 .rpc_argp = &arg,
502 .rpc_resp = &res,
503 };
1da177e4
LT
504 int status;
505
506 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
0e574af1
TM
507 nfs_fattr_init(&old_dir_attr);
508 nfs_fattr_init(&new_dir_attr);
dead28da 509 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
decf491f
TM
510 nfs_post_op_update_inode(old_dir, &old_dir_attr);
511 nfs_post_op_update_inode(new_dir, &new_dir_attr);
1da177e4
LT
512 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 };
dead28da
CL
530 struct rpc_message msg = {
531 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
532 .rpc_argp = &arg,
533 .rpc_resp = &res,
534 };
1da177e4
LT
535 int status;
536
537 dprintk("NFS call link %s\n", name->name);
0e574af1
TM
538 nfs_fattr_init(&dir_attr);
539 nfs_fattr_init(&fattr);
dead28da 540 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
decf491f
TM
541 nfs_post_op_update_inode(dir, &dir_attr);
542 nfs_post_op_update_inode(inode, &fattr);
1da177e4
LT
543 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 };
dead28da
CL
566 struct rpc_message msg = {
567 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
568 .rpc_argp = &arg,
569 .rpc_resp = &res,
570 };
1da177e4
LT
571 int status;
572
573 if (path->len > NFS3_MAXPATHLEN)
574 return -ENAMETOOLONG;
575 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
0e574af1
TM
576 nfs_fattr_init(&dir_attr);
577 nfs_fattr_init(fattr);
dead28da 578 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
decf491f 579 nfs_post_op_update_inode(dir, &dir_attr);
1da177e4
LT
580 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 };
dead28da
CL
600 struct rpc_message msg = {
601 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
602 .rpc_argp = &arg,
603 .rpc_resp = &res,
604 };
055ffbea
AG
605 int mode = sattr->ia_mode;
606 int status;
1da177e4
LT
607
608 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
055ffbea
AG
609
610 sattr->ia_mode &= ~current->fs->umask;
611
0e574af1
TM
612 nfs_fattr_init(&dir_attr);
613 nfs_fattr_init(&fattr);
dead28da 614 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
decf491f 615 nfs_post_op_update_inode(dir, &dir_attr);
055ffbea
AG
616 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:
1da177e4
LT
623 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 };
dead28da
CL
636 struct rpc_message msg = {
637 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
638 .rpc_argp = &arg,
639 .rpc_resp = &dir_attr,
640 };
1da177e4
LT
641 int status;
642
643 dprintk("NFS call rmdir %s\n", name->name);
0e574af1 644 nfs_fattr_init(&dir_attr);
dead28da 645 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
decf491f 646 nfs_post_op_update_inode(dir, &dir_attr);
1da177e4
LT
647 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
0e574af1 696 nfs_fattr_init(&dir_attr);
1da177e4
LT
697 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 };
dead28da
CL
722 struct rpc_message msg = {
723 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
724 .rpc_argp = &arg,
725 .rpc_resp = &res,
726 };
055ffbea 727 mode_t mode = sattr->ia_mode;
1da177e4
LT
728 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));
055ffbea
AG
740
741 sattr->ia_mode &= ~current->fs->umask;
742
0e574af1
TM
743 nfs_fattr_init(&dir_attr);
744 nfs_fattr_init(&fattr);
dead28da 745 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
decf491f 746 nfs_post_op_update_inode(dir, &dir_attr);
055ffbea
AG
747 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:
1da177e4
LT
754 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{
dead28da
CL
762 struct rpc_message msg = {
763 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
764 .rpc_argp = fhandle,
765 .rpc_resp = stat,
766 };
1da177e4
LT
767 int status;
768
769 dprintk("NFS call fsstat\n");
0e574af1 770 nfs_fattr_init(stat->fattr);
dead28da 771 status = rpc_call_sync(server->client, &msg, 0);
1da177e4
LT
772 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{
dead28da
CL
780 struct rpc_message msg = {
781 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
782 .rpc_argp = fhandle,
783 .rpc_resp = info,
784 };
1da177e4
LT
785 int status;
786
787 dprintk("NFS call fsinfo\n");
0e574af1 788 nfs_fattr_init(info->fattr);
dead28da 789 status = rpc_call_sync(server->client_sys, &msg, 0);
1da177e4
LT
790 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{
dead28da
CL
798 struct rpc_message msg = {
799 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
800 .rpc_argp = fhandle,
801 .rpc_resp = info,
802 };
1da177e4
LT
803 int status;
804
805 dprintk("NFS call pathconf\n");
0e574af1 806 nfs_fattr_init(info->fattr);
dead28da 807 status = rpc_call_sync(server->client, &msg, 0);
1da177e4
LT
808 dprintk("NFS reply pathconf: %d\n", status);
809 return status;
810}
811
812extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
813
963d8fe5 814static void nfs3_read_done(struct rpc_task *task, void *calldata)
1da177e4 815{
963d8fe5 816 struct nfs_read_data *data = calldata;
1da177e4 817
006ea73e 818 if (nfs3_async_handle_jukebox(task, data->inode))
1da177e4
LT
819 return;
820 /* Call back common NFS readpage processing */
821 if (task->tk_status >= 0)
822 nfs_refresh_inode(data->inode, &data->fattr);
963d8fe5 823 nfs_readpage_result(task, calldata);
1da177e4
LT
824}
825
963d8fe5
TM
826static const struct rpc_call_ops nfs3_read_ops = {
827 .rpc_call_done = nfs3_read_done,
828 .rpc_release = nfs_readdata_release,
829};
830
1da177e4
LT
831static void
832nfs3_proc_read_setup(struct nfs_read_data *data)
833{
834 struct rpc_task *task = &data->task;
835 struct inode *inode = data->inode;
836 int flags;
837 struct rpc_message msg = {
838 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
839 .rpc_argp = &data->args,
840 .rpc_resp = &data->res,
841 .rpc_cred = data->cred,
842 };
843
844 /* N.B. Do we need to test? Never called for swapfile inode */
845 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
846
847 /* Finalize the task. */
963d8fe5 848 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_read_ops, data);
1da177e4
LT
849 rpc_call_setup(task, &msg, 0);
850}
851
788e7a89 852static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 853{
006ea73e 854 if (nfs3_async_handle_jukebox(task, data->inode))
788e7a89 855 return -EAGAIN;
1da177e4 856 if (task->tk_status >= 0)
decf491f 857 nfs_post_op_update_inode(data->inode, data->res.fattr);
788e7a89 858 return 0;
1da177e4
LT
859}
860
788e7a89 861static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
1da177e4 862{
1da177e4
LT
863 struct rpc_message msg = {
864 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
865 .rpc_argp = &data->args,
866 .rpc_resp = &data->res,
867 .rpc_cred = data->cred,
868 };
869
788e7a89 870 data->args.stable = NFS_UNSTABLE;
1da177e4 871 if (how & FLUSH_STABLE) {
788e7a89
TM
872 data->args.stable = NFS_FILE_SYNC;
873 if (NFS_I(data->inode)->ncommit)
874 data->args.stable = NFS_DATA_SYNC;
875 }
1da177e4
LT
876
877 /* Finalize the task. */
788e7a89 878 rpc_call_setup(&data->task, &msg, 0);
1da177e4
LT
879}
880
788e7a89 881static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 882{
006ea73e 883 if (nfs3_async_handle_jukebox(task, data->inode))
788e7a89 884 return -EAGAIN;
1da177e4 885 if (task->tk_status >= 0)
decf491f 886 nfs_post_op_update_inode(data->inode, data->res.fattr);
788e7a89 887 return 0;
1da177e4
LT
888}
889
788e7a89 890static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
1da177e4 891{
1da177e4
LT
892 struct rpc_message msg = {
893 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
894 .rpc_argp = &data->args,
895 .rpc_resp = &data->res,
896 .rpc_cred = data->cred,
897 };
898
788e7a89 899 rpc_call_setup(&data->task, &msg, 0);
1da177e4
LT
900}
901
902static int
903nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
904{
905 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
906}
907
908struct nfs_rpc_ops nfs_v3_clientops = {
909 .version = 3, /* protocol version */
910 .dentry_ops = &nfs_dentry_operations,
b7fa0554
AG
911 .dir_inode_ops = &nfs3_dir_inode_operations,
912 .file_inode_ops = &nfs3_file_inode_operations,
1da177e4
LT
913 .getroot = nfs3_proc_get_root,
914 .getattr = nfs3_proc_getattr,
915 .setattr = nfs3_proc_setattr,
916 .lookup = nfs3_proc_lookup,
917 .access = nfs3_proc_access,
918 .readlink = nfs3_proc_readlink,
919 .read = nfs3_proc_read,
920 .write = nfs3_proc_write,
921 .commit = nfs3_proc_commit,
922 .create = nfs3_proc_create,
923 .remove = nfs3_proc_remove,
924 .unlink_setup = nfs3_proc_unlink_setup,
925 .unlink_done = nfs3_proc_unlink_done,
926 .rename = nfs3_proc_rename,
927 .link = nfs3_proc_link,
928 .symlink = nfs3_proc_symlink,
929 .mkdir = nfs3_proc_mkdir,
930 .rmdir = nfs3_proc_rmdir,
931 .readdir = nfs3_proc_readdir,
932 .mknod = nfs3_proc_mknod,
933 .statfs = nfs3_proc_statfs,
934 .fsinfo = nfs3_proc_fsinfo,
935 .pathconf = nfs3_proc_pathconf,
936 .decode_dirent = nfs3_decode_dirent,
937 .read_setup = nfs3_proc_read_setup,
938 .write_setup = nfs3_proc_write_setup,
788e7a89 939 .write_done = nfs3_write_done,
1da177e4 940 .commit_setup = nfs3_proc_commit_setup,
788e7a89 941 .commit_done = nfs3_commit_done,
1da177e4
LT
942 .file_open = nfs_open,
943 .file_release = nfs_release,
944 .lock = nfs3_proc_lock,
5c6a9f7d 945 .clear_acl_cache = nfs3_forget_cached_acls,
1da177e4 946};