blob: 4cdc2361a669bed2347d599f027cc10e57c9d7dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/nfs3proc.c
3 *
4 * Client-side NFSv3 procedures stubs.
5 *
6 * Copyright (C) 1997, Olaf Kirch
7 */
8
9#include <linux/mm.h>
10#include <linux/utsname.h>
11#include <linux/errno.h>
12#include <linux/string.h>
13#include <linux/sunrpc/clnt.h>
14#include <linux/nfs.h>
15#include <linux/nfs3.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_page.h>
18#include <linux/lockd/bind.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000019#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chuck Lever006ea732006-03-20 13:44:14 -050021#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040022#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define NFSDBG_FACILITY NFSDBG_PROC
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* A wrapper to handle the EJUKEBOX error message */
27static int
28nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
29{
30 sigset_t oldset;
31 int res;
32 rpc_clnt_sigmask(clnt, &oldset);
33 do {
34 res = rpc_call_sync(clnt, msg, flags);
35 if (res != -EJUKEBOX)
36 break;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -070037 schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 res = -ERESTARTSYS;
39 } while (!signalled());
40 rpc_clnt_sigunmask(clnt, &oldset);
41 return res;
42}
43
Chuck Leverdead28d2006-03-20 13:44:23 -050044#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static int
Chuck Lever006ea732006-03-20 13:44:14 -050047nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 if (task->tk_status != -EJUKEBOX)
50 return 0;
Chuck Lever006ea732006-03-20 13:44:14 -050051 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 task->tk_status = 0;
53 rpc_restart_call(task);
54 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
55 return 1;
56}
57
J. Bruce Fields03c21732006-01-03 09:55:48 +010058static int
59do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
60 struct nfs_fsinfo *info)
61{
Chuck Leverdead28d2006-03-20 13:44:23 -050062 struct rpc_message msg = {
63 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
64 .rpc_argp = fhandle,
65 .rpc_resp = info,
66 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010067 int status;
68
69 dprintk("%s: call fsinfo\n", __FUNCTION__);
70 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050071 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010072 dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
73 if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050074 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
75 msg.rpc_resp = info->fattr;
76 status = rpc_call_sync(client, &msg, 0);
J. Bruce Fields03c21732006-01-03 09:55:48 +010077 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
78 }
79 return status;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
David Howells54ceac42006-08-22 20:06:13 -040083 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85static int
86nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
87 struct nfs_fsinfo *info)
88{
89 int status;
90
J. Bruce Fields03c21732006-01-03 09:55:48 +010091 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040092 if (status && server->nfs_client->cl_rpcclient != server->client)
93 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return status;
95}
96
97/*
98 * One function for each procedure in the NFS protocol.
99 */
100static int
101nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
102 struct nfs_fattr *fattr)
103{
Chuck Leverdead28d2006-03-20 13:44:23 -0500104 struct rpc_message msg = {
105 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
106 .rpc_argp = fhandle,
107 .rpc_resp = fattr,
108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int status;
110
111 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400112 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500113 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 dprintk("NFS reply getattr: %d\n", status);
115 return status;
116}
117
118static int
119nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
120 struct iattr *sattr)
121{
122 struct inode *inode = dentry->d_inode;
123 struct nfs3_sattrargs arg = {
124 .fh = NFS_FH(inode),
125 .sattr = sattr,
126 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500127 struct rpc_message msg = {
128 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
129 .rpc_argp = &arg,
130 .rpc_resp = fattr,
131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int status;
133
134 dprintk("NFS call setattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400135 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500136 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400137 if (status == 0)
138 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dprintk("NFS reply setattr: %d\n", status);
140 return status;
141}
142
143static int
144nfs3_proc_lookup(struct inode *dir, struct qstr *name,
145 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
146{
147 struct nfs_fattr dir_attr;
148 struct nfs3_diropargs arg = {
149 .fh = NFS_FH(dir),
150 .name = name->name,
151 .len = name->len
152 };
153 struct nfs3_diropres res = {
154 .dir_attr = &dir_attr,
155 .fh = fhandle,
156 .fattr = fattr
157 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500158 struct rpc_message msg = {
159 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
160 .rpc_argp = &arg,
161 .rpc_resp = &res,
162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int status;
164
165 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400166 nfs_fattr_init(&dir_attr);
167 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500168 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust81c76882007-10-02 10:30:00 -0400169 nfs_refresh_inode(dir, &dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500170 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
171 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
172 msg.rpc_argp = fhandle;
173 msg.rpc_resp = fattr;
174 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return status;
178}
179
180static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
181{
182 struct nfs_fattr fattr;
183 struct nfs3_accessargs arg = {
184 .fh = NFS_FH(inode),
185 };
186 struct nfs3_accessres res = {
187 .fattr = &fattr,
188 };
189 struct rpc_message msg = {
190 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
191 .rpc_argp = &arg,
192 .rpc_resp = &res,
Chuck Leverdead28d2006-03-20 13:44:23 -0500193 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 };
195 int mode = entry->mask;
196 int status;
197
198 dprintk("NFS call access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (mode & MAY_READ)
201 arg.access |= NFS3_ACCESS_READ;
202 if (S_ISDIR(inode->i_mode)) {
203 if (mode & MAY_WRITE)
204 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
205 if (mode & MAY_EXEC)
206 arg.access |= NFS3_ACCESS_LOOKUP;
207 } else {
208 if (mode & MAY_WRITE)
209 arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
210 if (mode & MAY_EXEC)
211 arg.access |= NFS3_ACCESS_EXECUTE;
212 }
Trond Myklebust0e574af2005-10-27 22:12:38 -0400213 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
215 nfs_refresh_inode(inode, &fattr);
216 if (status == 0) {
217 entry->mask = 0;
218 if (res.access & NFS3_ACCESS_READ)
219 entry->mask |= MAY_READ;
220 if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
221 entry->mask |= MAY_WRITE;
222 if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
223 entry->mask |= MAY_EXEC;
224 }
225 dprintk("NFS reply access: %d\n", status);
226 return status;
227}
228
229static int nfs3_proc_readlink(struct inode *inode, struct page *page,
230 unsigned int pgbase, unsigned int pglen)
231{
232 struct nfs_fattr fattr;
233 struct nfs3_readlinkargs args = {
234 .fh = NFS_FH(inode),
235 .pgbase = pgbase,
236 .pglen = pglen,
237 .pages = &page
238 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500239 struct rpc_message msg = {
240 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
241 .rpc_argp = &args,
242 .rpc_resp = &fattr,
243 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int status;
245
246 dprintk("NFS call readlink\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400247 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500248 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 nfs_refresh_inode(inode, &fattr);
250 dprintk("NFS reply readlink: %d\n", status);
251 return status;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
255 * Create a regular file.
256 * For now, we don't implement O_EXCL.
257 */
258static int
259nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -0700260 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct nfs_fh fhandle;
263 struct nfs_fattr fattr;
264 struct nfs_fattr dir_attr;
265 struct nfs3_createargs arg = {
266 .fh = NFS_FH(dir),
267 .name = dentry->d_name.name,
268 .len = dentry->d_name.len,
269 .sattr = sattr,
270 };
271 struct nfs3_diropres res = {
272 .dir_attr = &dir_attr,
273 .fh = &fhandle,
274 .fattr = &fattr
275 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500276 struct rpc_message msg = {
277 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
278 .rpc_argp = &arg,
279 .rpc_resp = &res,
280 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000281 mode_t mode = sattr->ia_mode;
282 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 dprintk("NFS call create %s\n", dentry->d_name.name);
285 arg.createmode = NFS3_CREATE_UNCHECKED;
286 if (flags & O_EXCL) {
287 arg.createmode = NFS3_CREATE_EXCLUSIVE;
288 arg.verifier[0] = jiffies;
289 arg.verifier[1] = current->pid;
290 }
291
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000292 sattr->ia_mode &= ~current->fs->umask;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294again:
Trond Myklebust0e574af2005-10-27 22:12:38 -0400295 nfs_fattr_init(&dir_attr);
296 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500297 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
298 nfs_refresh_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* If the server doesn't support the exclusive creation semantics,
301 * try again with simple 'guarded' mode. */
Andy Ryan46b9f8e2006-11-07 14:36:26 -0600302 if (status == -ENOTSUPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 switch (arg.createmode) {
304 case NFS3_CREATE_EXCLUSIVE:
305 arg.createmode = NFS3_CREATE_GUARDED;
306 break;
307
308 case NFS3_CREATE_GUARDED:
309 arg.createmode = NFS3_CREATE_UNCHECKED;
310 break;
311
312 case NFS3_CREATE_UNCHECKED:
313 goto out;
314 }
315 goto again;
316 }
317
318 if (status == 0)
319 status = nfs_instantiate(dentry, &fhandle, &fattr);
320 if (status != 0)
321 goto out;
322
323 /* When we created the file with exclusive semantics, make
324 * sure we set the attributes afterwards. */
325 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
326 dprintk("NFS call setattr (post-create)\n");
327
328 if (!(sattr->ia_valid & ATTR_ATIME_SET))
329 sattr->ia_valid |= ATTR_ATIME;
330 if (!(sattr->ia_valid & ATTR_MTIME_SET))
331 sattr->ia_valid |= ATTR_MTIME;
332
333 /* Note: we could use a guarded setattr here, but I'm
334 * not sure this buys us anything (and I'd have
335 * to revamp the NFSv3 XDR code) */
336 status = nfs3_proc_setattr(dentry, &fattr, sattr);
Trond Myklebuste2f032e2007-06-05 19:27:00 -0400337 nfs_post_op_update_inode(dentry->d_inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dprintk("NFS reply setattr (post-create): %d\n", status);
339 }
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000340 if (status != 0)
341 goto out;
342 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343out:
344 dprintk("NFS reply create: %d\n", status);
345 return status;
346}
347
348static int
349nfs3_proc_remove(struct inode *dir, struct qstr *name)
350{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400351 struct nfs_removeargs arg = {
352 .fh = NFS_FH(dir),
353 .name.len = name->len,
354 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400356 struct nfs_removeres res;
357 struct rpc_message msg = {
358 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
359 .rpc_argp = &arg,
360 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 };
362 int status;
363
364 dprintk("NFS call remove %s\n", name->name);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400365 nfs_fattr_init(&res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400367 nfs_post_op_update_inode(dir, &res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 dprintk("NFS reply remove: %d\n", status);
369 return status;
370}
371
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400372static void
373nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400379nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400381 struct nfs_removeres *res;
382 if (nfs3_async_handle_jukebox(task, dir))
383 return 0;
384 res = task->tk_msg.rpc_resp;
385 nfs_post_op_update_inode(dir, &res->dir_attr);
386 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static int
390nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
391 struct inode *new_dir, struct qstr *new_name)
392{
393 struct nfs_fattr old_dir_attr, new_dir_attr;
394 struct nfs3_renameargs arg = {
395 .fromfh = NFS_FH(old_dir),
396 .fromname = old_name->name,
397 .fromlen = old_name->len,
398 .tofh = NFS_FH(new_dir),
399 .toname = new_name->name,
400 .tolen = new_name->len
401 };
402 struct nfs3_renameres res = {
403 .fromattr = &old_dir_attr,
404 .toattr = &new_dir_attr
405 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500406 struct rpc_message msg = {
407 .rpc_proc = &nfs3_procedures[NFS3PROC_RENAME],
408 .rpc_argp = &arg,
409 .rpc_resp = &res,
410 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int status;
412
413 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400414 nfs_fattr_init(&old_dir_attr);
415 nfs_fattr_init(&new_dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500416 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400417 nfs_post_op_update_inode(old_dir, &old_dir_attr);
418 nfs_post_op_update_inode(new_dir, &new_dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dprintk("NFS reply rename: %d\n", status);
420 return status;
421}
422
423static int
424nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
425{
426 struct nfs_fattr dir_attr, fattr;
427 struct nfs3_linkargs arg = {
428 .fromfh = NFS_FH(inode),
429 .tofh = NFS_FH(dir),
430 .toname = name->name,
431 .tolen = name->len
432 };
433 struct nfs3_linkres res = {
434 .dir_attr = &dir_attr,
435 .fattr = &fattr
436 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500437 struct rpc_message msg = {
438 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
439 .rpc_argp = &arg,
440 .rpc_resp = &res,
441 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int status;
443
444 dprintk("NFS call link %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400445 nfs_fattr_init(&dir_attr);
446 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500447 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400448 nfs_post_op_update_inode(dir, &dir_attr);
449 nfs_post_op_update_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 dprintk("NFS reply link: %d\n", status);
451 return status;
452}
453
454static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400455nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
456 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Chuck Lever4f390c12006-08-22 20:06:22 -0400458 struct nfs_fh fhandle;
459 struct nfs_fattr fattr, dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct nfs3_symlinkargs arg = {
461 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400462 .fromname = dentry->d_name.name,
463 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400464 .pages = &page,
465 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .sattr = sattr
467 };
468 struct nfs3_diropres res = {
469 .dir_attr = &dir_attr,
Chuck Lever4f390c12006-08-22 20:06:22 -0400470 .fh = &fhandle,
471 .fattr = &fattr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500473 struct rpc_message msg = {
474 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
475 .rpc_argp = &arg,
476 .rpc_resp = &res,
477 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int status;
479
Chuck Lever94a6d752006-08-22 20:06:23 -0400480 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400482
Chuck Lever94a6d752006-08-22 20:06:23 -0400483 dprintk("NFS call symlink %s\n", dentry->d_name.name);
484
Trond Myklebust0e574af2005-10-27 22:12:38 -0400485 nfs_fattr_init(&dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400486 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500487 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400488 nfs_post_op_update_inode(dir, &dir_attr);
Chuck Lever4f390c12006-08-22 20:06:22 -0400489 if (status != 0)
490 goto out;
491 status = nfs_instantiate(dentry, &fhandle, &fattr);
492out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dprintk("NFS reply symlink: %d\n", status);
494 return status;
495}
496
497static int
498nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
499{
500 struct nfs_fh fhandle;
501 struct nfs_fattr fattr, dir_attr;
502 struct nfs3_mkdirargs arg = {
503 .fh = NFS_FH(dir),
504 .name = dentry->d_name.name,
505 .len = dentry->d_name.len,
506 .sattr = sattr
507 };
508 struct nfs3_diropres res = {
509 .dir_attr = &dir_attr,
510 .fh = &fhandle,
511 .fattr = &fattr
512 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500513 struct rpc_message msg = {
514 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
515 .rpc_argp = &arg,
516 .rpc_resp = &res,
517 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000518 int mode = sattr->ia_mode;
519 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000522
523 sattr->ia_mode &= ~current->fs->umask;
524
Trond Myklebust0e574af2005-10-27 22:12:38 -0400525 nfs_fattr_init(&dir_attr);
526 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500527 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400528 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000529 if (status != 0)
530 goto out;
531 status = nfs_instantiate(dentry, &fhandle, &fattr);
532 if (status != 0)
533 goto out;
534 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
535out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 dprintk("NFS reply mkdir: %d\n", status);
537 return status;
538}
539
540static int
541nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
542{
543 struct nfs_fattr dir_attr;
544 struct nfs3_diropargs arg = {
545 .fh = NFS_FH(dir),
546 .name = name->name,
547 .len = name->len
548 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500549 struct rpc_message msg = {
550 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
551 .rpc_argp = &arg,
552 .rpc_resp = &dir_attr,
553 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int status;
555
556 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400557 nfs_fattr_init(&dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500558 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400559 nfs_post_op_update_inode(dir, &dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dprintk("NFS reply rmdir: %d\n", status);
561 return status;
562}
563
564/*
565 * The READDIR implementation is somewhat hackish - we pass the user buffer
566 * to the encode function, which installs it in the receive iovec.
567 * The decode function itself doesn't perform any decoding, it just makes
568 * sure the reply is syntactically correct.
569 *
570 * Also note that this implementation handles both plain readdir and
571 * readdirplus.
572 */
573static int
574nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
575 u64 cookie, struct page *page, unsigned int count, int plus)
576{
577 struct inode *dir = dentry->d_inode;
578 struct nfs_fattr dir_attr;
Al Virobc4785c2006-10-19 23:28:51 -0700579 __be32 *verf = NFS_COOKIEVERF(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct nfs3_readdirargs arg = {
581 .fh = NFS_FH(dir),
582 .cookie = cookie,
583 .verf = {verf[0], verf[1]},
584 .plus = plus,
585 .count = count,
586 .pages = &page
587 };
588 struct nfs3_readdirres res = {
589 .dir_attr = &dir_attr,
590 .verf = verf,
591 .plus = plus
592 };
593 struct rpc_message msg = {
594 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
595 .rpc_argp = &arg,
596 .rpc_resp = &res,
597 .rpc_cred = cred
598 };
599 int status;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (plus)
602 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
603
604 dprintk("NFS call readdir%s %d\n",
605 plus? "plus" : "", (unsigned int) cookie);
606
Trond Myklebust0e574af2005-10-27 22:12:38 -0400607 nfs_fattr_init(&dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400609
610 nfs_invalidate_atime(dir);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 nfs_refresh_inode(dir, &dir_attr);
613 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return status;
615}
616
617static int
618nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
619 dev_t rdev)
620{
621 struct nfs_fh fh;
622 struct nfs_fattr fattr, dir_attr;
623 struct nfs3_mknodargs arg = {
624 .fh = NFS_FH(dir),
625 .name = dentry->d_name.name,
626 .len = dentry->d_name.len,
627 .sattr = sattr,
628 .rdev = rdev
629 };
630 struct nfs3_diropres res = {
631 .dir_attr = &dir_attr,
632 .fh = &fh,
633 .fattr = &fattr
634 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500635 struct rpc_message msg = {
636 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
637 .rpc_argp = &arg,
638 .rpc_resp = &res,
639 };
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000640 mode_t mode = sattr->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int status;
642
643 switch (sattr->ia_mode & S_IFMT) {
644 case S_IFBLK: arg.type = NF3BLK; break;
645 case S_IFCHR: arg.type = NF3CHR; break;
646 case S_IFIFO: arg.type = NF3FIFO; break;
647 case S_IFSOCK: arg.type = NF3SOCK; break;
648 default: return -EINVAL;
649 }
650
651 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
652 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000653
654 sattr->ia_mode &= ~current->fs->umask;
655
Trond Myklebust0e574af2005-10-27 22:12:38 -0400656 nfs_fattr_init(&dir_attr);
657 nfs_fattr_init(&fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500658 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400659 nfs_post_op_update_inode(dir, &dir_attr);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000660 if (status != 0)
661 goto out;
662 status = nfs_instantiate(dentry, &fh, &fattr);
663 if (status != 0)
664 goto out;
665 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
666out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 dprintk("NFS reply mknod: %d\n", status);
668 return status;
669}
670
671static int
672nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
673 struct nfs_fsstat *stat)
674{
Chuck Leverdead28d2006-03-20 13:44:23 -0500675 struct rpc_message msg = {
676 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
677 .rpc_argp = fhandle,
678 .rpc_resp = stat,
679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int status;
681
682 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400683 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500684 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dprintk("NFS reply statfs: %d\n", status);
686 return status;
687}
688
689static int
690nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
691 struct nfs_fsinfo *info)
692{
Chuck Leverdead28d2006-03-20 13:44:23 -0500693 struct rpc_message msg = {
694 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
695 .rpc_argp = fhandle,
696 .rpc_resp = info,
697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int status;
699
700 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400701 nfs_fattr_init(info->fattr);
David Howells5006a762006-08-22 20:06:12 -0400702 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dprintk("NFS reply fsinfo: %d\n", status);
704 return status;
705}
706
707static int
708nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
709 struct nfs_pathconf *info)
710{
Chuck Leverdead28d2006-03-20 13:44:23 -0500711 struct rpc_message msg = {
712 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
713 .rpc_argp = fhandle,
714 .rpc_resp = info,
715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int status;
717
718 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400719 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500720 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dprintk("NFS reply pathconf: %d\n", status);
722 return status;
723}
724
Trond Myklebustec06c092006-03-20 13:44:27 -0500725static int nfs3_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Chuck Lever006ea732006-03-20 13:44:14 -0500727 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500728 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400729
730 nfs_invalidate_atime(data->inode);
731 nfs_refresh_inode(data->inode, &data->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Trond Myklebustec06c092006-03-20 13:44:27 -0500735static void nfs3_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct rpc_message msg = {
738 .rpc_proc = &nfs3_procedures[NFS3PROC_READ],
739 .rpc_argp = &data->args,
740 .rpc_resp = &data->res,
741 .rpc_cred = data->cred,
742 };
743
Trond Myklebustec06c092006-03-20 13:44:27 -0500744 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Trond Myklebust788e7a82006-03-20 13:44:27 -0500747static int nfs3_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Chuck Lever006ea732006-03-20 13:44:14 -0500749 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500750 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (task->tk_status >= 0)
Trond Myklebust70ca8852007-09-30 15:21:24 -0400752 nfs_post_op_update_inode_force_wcc(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebust788e7a82006-03-20 13:44:27 -0500756static void nfs3_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct rpc_message msg = {
759 .rpc_proc = &nfs3_procedures[NFS3PROC_WRITE],
760 .rpc_argp = &data->args,
761 .rpc_resp = &data->res,
762 .rpc_cred = data->cred,
763 };
764
Trond Myklebust788e7a82006-03-20 13:44:27 -0500765 data->args.stable = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (how & FLUSH_STABLE) {
Trond Myklebust788e7a82006-03-20 13:44:27 -0500767 data->args.stable = NFS_FILE_SYNC;
768 if (NFS_I(data->inode)->ncommit)
769 data->args.stable = NFS_DATA_SYNC;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -0500773 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Trond Myklebust788e7a82006-03-20 13:44:27 -0500776static int nfs3_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Chuck Lever006ea732006-03-20 13:44:14 -0500778 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500779 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400780 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Trond Myklebust788e7a82006-03-20 13:44:27 -0500784static void nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct rpc_message msg = {
787 .rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT],
788 .rpc_argp = &data->args,
789 .rpc_resp = &data->res,
790 .rpc_cred = data->cred,
791 };
792
Trond Myklebust788e7a82006-03-20 13:44:27 -0500793 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796static int
797nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
798{
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800799 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
David Howells509de812006-08-22 20:06:11 -0400802const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 .version = 3, /* protocol version */
804 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000805 .dir_inode_ops = &nfs3_dir_inode_operations,
806 .file_inode_ops = &nfs3_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .getroot = nfs3_proc_get_root,
808 .getattr = nfs3_proc_getattr,
809 .setattr = nfs3_proc_setattr,
810 .lookup = nfs3_proc_lookup,
811 .access = nfs3_proc_access,
812 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .create = nfs3_proc_create,
814 .remove = nfs3_proc_remove,
815 .unlink_setup = nfs3_proc_unlink_setup,
816 .unlink_done = nfs3_proc_unlink_done,
817 .rename = nfs3_proc_rename,
818 .link = nfs3_proc_link,
819 .symlink = nfs3_proc_symlink,
820 .mkdir = nfs3_proc_mkdir,
821 .rmdir = nfs3_proc_rmdir,
822 .readdir = nfs3_proc_readdir,
823 .mknod = nfs3_proc_mknod,
824 .statfs = nfs3_proc_statfs,
825 .fsinfo = nfs3_proc_fsinfo,
826 .pathconf = nfs3_proc_pathconf,
827 .decode_dirent = nfs3_decode_dirent,
828 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500829 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500831 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 .commit_setup = nfs3_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500833 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .file_open = nfs_open,
835 .file_release = nfs_release,
836 .lock = nfs3_proc_lock,
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000837 .clear_acl_cache = nfs3_forget_cached_acls,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838};