blob: 47ece1dd3c6705fa12282b31354c5d1558228319 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Chuck Lever006ea732006-03-20 13:44:14 -050054#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_PROC
57
58#define NFS4_POLL_RETRY_MIN (1*HZ)
59#define NFS4_POLL_RETRY_MAX (15*HZ)
60
Trond Myklebustcdd4e682006-01-03 09:55:12 +010061struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010062static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070066static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Trond Myklebust58d97142006-01-03 09:55:24 +010067static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
69extern struct rpc_procinfo nfs4_procedures[];
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Prevent leaks of NFSv4 errors into userland */
72int nfs4_map_errors(int err)
73{
74 if (err < -1000) {
75 dprintk("%s could not handle NFSv4 error %d\n",
76 __FUNCTION__, -err);
77 return -EIO;
78 }
79 return err;
80}
81
82/*
83 * This is our standard bitmap for GETATTR requests.
84 */
85const u32 nfs4_fattr_bitmap[2] = {
86 FATTR4_WORD0_TYPE
87 | FATTR4_WORD0_CHANGE
88 | FATTR4_WORD0_SIZE
89 | FATTR4_WORD0_FSID
90 | FATTR4_WORD0_FILEID,
91 FATTR4_WORD1_MODE
92 | FATTR4_WORD1_NUMLINKS
93 | FATTR4_WORD1_OWNER
94 | FATTR4_WORD1_OWNER_GROUP
95 | FATTR4_WORD1_RAWDEV
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
100};
101
102const u32 nfs4_statfs_bitmap[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
109};
110
Trond Myklebust4ce79712005-06-22 17:16:21 +0000111const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 FATTR4_WORD0_MAXLINK
113 | FATTR4_WORD0_MAXNAME,
114 0
115};
116
117const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME,
121 0
122};
123
124static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
125 struct nfs4_readdir_arg *readdir)
126{
127 u32 *start, *p;
128
129 BUG_ON(readdir->count < 80);
130 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000131 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
133 return;
134 }
135
136 readdir->cookie = 0;
137 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
138 if (cookie == 2)
139 return;
140
141 /*
142 * NFSv4 servers do not return entries for '.' and '..'
143 * Therefore, we fake these entries here. We let '.'
144 * have cookie 0 and '..' have cookie 1. Note that
145 * when talking to the server, we always send cookie 0
146 * instead of 1 or 2.
147 */
148 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
149
150 if (cookie == 0) {
151 *p++ = xdr_one; /* next */
152 *p++ = xdr_zero; /* cookie, first word */
153 *p++ = xdr_one; /* cookie, second word */
154 *p++ = xdr_one; /* entry len */
155 memcpy(p, ".\0\0\0", 4); /* entry */
156 p++;
157 *p++ = xdr_one; /* bitmap length */
158 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
159 *p++ = htonl(8); /* attribute buffer length */
160 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
161 }
162
163 *p++ = xdr_one; /* next */
164 *p++ = xdr_zero; /* cookie, first word */
165 *p++ = xdr_two; /* cookie, second word */
166 *p++ = xdr_two; /* entry len */
167 memcpy(p, "..\0\0", 4); /* entry */
168 p++;
169 *p++ = xdr_one; /* bitmap length */
170 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
171 *p++ = htonl(8); /* attribute buffer length */
172 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
173
174 readdir->pgbase = (char *)p - (char *)start;
175 readdir->count -= readdir->pgbase;
176 kunmap_atomic(start, KM_USER0);
177}
178
Trond Myklebust26e976a2006-01-03 09:55:21 +0100179static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct nfs4_client *clp = server->nfs4_state;
182 spin_lock(&clp->cl_lock);
183 if (time_before(clp->cl_last_renewal,timestamp))
184 clp->cl_last_renewal = timestamp;
185 spin_unlock(&clp->cl_lock);
186}
187
188static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
189{
190 struct nfs_inode *nfsi = NFS_I(inode);
191
Trond Myklebustdecf4912005-10-27 22:12:39 -0400192 spin_lock(&inode->i_lock);
193 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
195 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400196 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100199struct nfs4_opendata {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100200 atomic_t count;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100201 struct nfs_openargs o_arg;
202 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100203 struct nfs_open_confirmargs c_arg;
204 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100205 struct nfs_fattr f_attr;
206 struct nfs_fattr dir_attr;
207 struct dentry *dentry;
208 struct dentry *dir;
209 struct nfs4_state_owner *owner;
210 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100211 unsigned long timestamp;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100212 int rpc_status;
213 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100214};
215
216static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
217 struct nfs4_state_owner *sp, int flags,
218 const struct iattr *attrs)
219{
220 struct dentry *parent = dget_parent(dentry);
221 struct inode *dir = parent->d_inode;
222 struct nfs_server *server = NFS_SERVER(dir);
223 struct nfs4_opendata *p;
224
225 p = kzalloc(sizeof(*p), GFP_KERNEL);
226 if (p == NULL)
227 goto err;
228 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
229 if (p->o_arg.seqid == NULL)
230 goto err_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100231 atomic_set(&p->count, 1);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100232 p->dentry = dget(dentry);
233 p->dir = parent;
234 p->owner = sp;
235 atomic_inc(&sp->so_count);
236 p->o_arg.fh = NFS_FH(dir);
237 p->o_arg.open_flags = flags,
238 p->o_arg.clientid = server->nfs4_state->cl_clientid;
239 p->o_arg.id = sp->so_id;
240 p->o_arg.name = &dentry->d_name;
241 p->o_arg.server = server;
242 p->o_arg.bitmask = server->attr_bitmask;
243 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
244 p->o_res.f_attr = &p->f_attr;
245 p->o_res.dir_attr = &p->dir_attr;
246 p->o_res.server = server;
247 nfs_fattr_init(&p->f_attr);
248 nfs_fattr_init(&p->dir_attr);
249 if (flags & O_EXCL) {
250 u32 *s = (u32 *) p->o_arg.u.verifier.data;
251 s[0] = jiffies;
252 s[1] = current->pid;
253 } else if (flags & O_CREAT) {
254 p->o_arg.u.attrs = &p->attrs;
255 memcpy(&p->attrs, attrs, sizeof(p->attrs));
256 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100257 p->c_arg.fh = &p->o_res.fh;
258 p->c_arg.stateid = &p->o_res.stateid;
259 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100260 return p;
261err_free:
262 kfree(p);
263err:
264 dput(parent);
265 return NULL;
266}
267
268static void nfs4_opendata_free(struct nfs4_opendata *p)
269{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100270 if (p != NULL && atomic_dec_and_test(&p->count)) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100271 nfs_free_seqid(p->o_arg.seqid);
272 nfs4_put_state_owner(p->owner);
273 dput(p->dir);
274 dput(p->dentry);
275 kfree(p);
276 }
277}
278
Trond Myklebust95121352005-10-18 14:20:12 -0700279/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100280static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100281 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700282{
283 struct rpc_task *task;
284
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100285 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700286 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700287 rpc_execute(task);
288 return 0;
289}
290
Trond Myklebust06f814a2006-01-03 09:55:07 +0100291static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
292{
293 sigset_t oldset;
294 int ret;
295
296 rpc_clnt_sigmask(task->tk_client, &oldset);
297 ret = rpc_wait_for_completion_task(task);
298 rpc_clnt_sigunmask(task->tk_client, &oldset);
299 return ret;
300}
301
Trond Myklebuste7616922006-01-03 09:55:13 +0100302static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
303{
304 switch (open_flags) {
305 case FMODE_WRITE:
306 state->n_wronly++;
307 break;
308 case FMODE_READ:
309 state->n_rdonly++;
310 break;
311 case FMODE_READ|FMODE_WRITE:
312 state->n_rdwr++;
313 }
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
317{
318 struct inode *inode = state->inode;
319
320 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500321 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700322 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500324 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100325 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500326 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700328 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100331static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
332{
333 struct inode *inode;
334 struct nfs4_state *state = NULL;
335
336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
337 goto out;
338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500339 if (IS_ERR(inode))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100340 goto out;
341 state = nfs4_get_open_state(inode, data->owner);
342 if (state == NULL)
343 goto put_inode;
344 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
345put_inode:
346 iput(inode);
347out:
348 return state;
349}
350
Trond Myklebust864472e2006-01-03 09:55:15 +0100351static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
352{
353 struct nfs_inode *nfsi = NFS_I(state->inode);
354 struct nfs_open_context *ctx;
355
356 spin_lock(&state->inode->i_lock);
357 list_for_each_entry(ctx, &nfsi->open_files, list) {
358 if (ctx->state != state)
359 continue;
360 get_nfs_open_context(ctx);
361 spin_unlock(&state->inode->i_lock);
362 return ctx;
363 }
364 spin_unlock(&state->inode->i_lock);
365 return ERR_PTR(-ENOENT);
366}
367
368static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
369{
370 int ret;
371
372 opendata->o_arg.open_flags = openflags;
373 ret = _nfs4_proc_open(opendata);
374 if (ret != 0)
375 return ret;
376 memcpy(stateid->data, opendata->o_res.stateid.data,
377 sizeof(stateid->data));
378 return 0;
379}
380
381static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
382{
383 nfs4_stateid stateid;
384 struct nfs4_state *newstate;
385 int mode = 0;
386 int delegation = 0;
387 int ret;
388
389 /* memory barrier prior to reading state->n_* */
390 smp_rmb();
391 if (state->n_rdwr != 0) {
392 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
393 if (ret != 0)
394 return ret;
395 mode |= FMODE_READ|FMODE_WRITE;
396 if (opendata->o_res.delegation_type != 0)
397 delegation = opendata->o_res.delegation_type;
398 smp_rmb();
399 }
400 if (state->n_wronly != 0) {
401 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
402 if (ret != 0)
403 return ret;
404 mode |= FMODE_WRITE;
405 if (opendata->o_res.delegation_type != 0)
406 delegation = opendata->o_res.delegation_type;
407 smp_rmb();
408 }
409 if (state->n_rdonly != 0) {
410 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
411 if (ret != 0)
412 return ret;
413 mode |= FMODE_READ;
414 }
415 clear_bit(NFS_DELEGATED_STATE, &state->flags);
416 if (mode == 0)
417 return 0;
418 if (opendata->o_res.delegation_type == 0)
419 opendata->o_res.delegation_type = delegation;
420 opendata->o_arg.open_flags |= mode;
421 newstate = nfs4_opendata_to_nfs4_state(opendata);
422 if (newstate != NULL) {
423 if (opendata->o_res.delegation_type != 0) {
424 struct nfs_inode *nfsi = NFS_I(newstate->inode);
425 int delegation_flags = 0;
426 if (nfsi->delegation)
427 delegation_flags = nfsi->delegation->flags;
428 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
429 nfs_inode_set_delegation(newstate->inode,
430 opendata->owner->so_cred,
431 &opendata->o_res);
432 else
433 nfs_inode_reclaim_delegation(newstate->inode,
434 opendata->owner->so_cred,
435 &opendata->o_res);
436 }
437 nfs4_close_state(newstate, opendata->o_arg.open_flags);
438 }
439 if (newstate != state)
440 return -ESTALE;
441 return 0;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * OPEN_RECLAIM:
446 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Trond Myklebust864472e2006-01-03 09:55:15 +0100448static int _nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Trond Myklebust864472e2006-01-03 09:55:15 +0100450 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
451 struct nfs4_opendata *opendata;
452 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int status;
454
455 if (delegation != NULL) {
456 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
457 memcpy(&state->stateid, &delegation->stateid,
458 sizeof(state->stateid));
459 set_bit(NFS_DELEGATED_STATE, &state->flags);
460 return 0;
461 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100462 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100464 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
465 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700466 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100467 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
468 opendata->o_arg.fh = NFS_FH(state->inode);
469 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
470 opendata->o_arg.u.delegation_type = delegation_type;
471 status = nfs4_open_recover(opendata, state);
472 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return status;
474}
475
Trond Myklebust864472e2006-01-03 09:55:15 +0100476static int nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct nfs_server *server = NFS_SERVER(state->inode);
479 struct nfs4_exception exception = { };
480 int err;
481 do {
Trond Myklebust864472e2006-01-03 09:55:15 +0100482 err = _nfs4_do_open_reclaim(sp, state, dentry);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000483 if (err != -NFS4ERR_DELAY)
484 break;
485 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } while (exception.retry);
487 return err;
488}
489
Trond Myklebust864472e2006-01-03 09:55:15 +0100490static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
491{
492 struct nfs_open_context *ctx;
493 int ret;
494
495 ctx = nfs4_state_find_open_context(state);
496 if (IS_ERR(ctx))
497 return PTR_ERR(ctx);
498 ret = nfs4_do_open_reclaim(sp, state, ctx->dentry);
499 put_nfs_open_context(ctx);
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
504{
505 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100506 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100507 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust864472e2006-01-03 09:55:15 +0100510 return 0;
511 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100512 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100513 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100514 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100515 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
516 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100517 ret = nfs4_open_recover(opendata, state);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100518 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
523{
524 struct nfs4_exception exception = { };
525 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
526 int err;
527 do {
528 err = _nfs4_open_delegation_recall(dentry, state);
529 switch (err) {
530 case 0:
531 return err;
532 case -NFS4ERR_STALE_CLIENTID:
533 case -NFS4ERR_STALE_STATEID:
534 case -NFS4ERR_EXPIRED:
535 /* Don't recall a delegation if it was lost */
536 nfs4_schedule_state_recovery(server->nfs4_state);
537 return err;
538 }
539 err = nfs4_handle_exception(server, err, &exception);
540 } while (exception.retry);
541 return err;
542}
543
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100544static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100546 struct nfs4_opendata *data = calldata;
547 struct rpc_message msg = {
548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
549 .rpc_argp = &data->c_arg,
550 .rpc_resp = &data->c_res,
551 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100553 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100554 rpc_call_setup(task, &msg, 0);
555}
556
557static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
558{
559 struct nfs4_opendata *data = calldata;
560
561 data->rpc_status = task->tk_status;
562 if (RPC_ASSASSINATED(task))
563 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100564 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100565 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
566 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100567 renew_lease(data->o_res.server, data->timestamp);
568 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100569 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
570 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
571}
572
573static void nfs4_open_confirm_release(void *calldata)
574{
575 struct nfs4_opendata *data = calldata;
576 struct nfs4_state *state = NULL;
577
578 /* If this request hasn't been cancelled, do nothing */
579 if (data->cancelled == 0)
580 goto out_free;
581 /* In case of error, no cleanup! */
582 if (data->rpc_status != 0)
583 goto out_free;
584 nfs_confirm_seqid(&data->owner->so_seqid, 0);
585 state = nfs4_opendata_to_nfs4_state(data);
586 if (state != NULL)
587 nfs4_close_state(state, data->o_arg.open_flags);
588out_free:
589 nfs4_opendata_free(data);
590}
591
592static const struct rpc_call_ops nfs4_open_confirm_ops = {
593 .rpc_call_prepare = nfs4_open_confirm_prepare,
594 .rpc_call_done = nfs4_open_confirm_done,
595 .rpc_release = nfs4_open_confirm_release,
596};
597
598/*
599 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
600 */
601static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
602{
603 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
604 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int status;
606
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100607 atomic_inc(&data->count);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500608 /*
609 * If rpc_run_task() ends up calling ->rpc_release(), we
610 * want to ensure that it takes the 'error' code path.
611 */
612 data->rpc_status = -ENOMEM;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100613 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500614 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100615 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100616 status = nfs4_wait_for_completion_rpc_task(task);
617 if (status != 0) {
618 data->cancelled = 1;
619 smp_wmb();
620 } else
621 status = data->rpc_status;
622 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return status;
624}
625
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100626static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100628 struct nfs4_opendata *data = calldata;
629 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct rpc_message msg = {
631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100632 .rpc_argp = &data->o_arg,
633 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .rpc_cred = sp->so_cred,
635 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100636
637 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
638 return;
639 /* Update sequence id. */
640 data->o_arg.id = sp->so_id;
641 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100642 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
643 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100644 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100645 rpc_call_setup(task, &msg, 0);
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100648static void nfs4_open_done(struct rpc_task *task, void *calldata)
649{
650 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100652 data->rpc_status = task->tk_status;
653 if (RPC_ASSASSINATED(task))
654 return;
655 if (task->tk_status == 0) {
656 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700657 case S_IFREG:
658 break;
659 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100660 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700661 break;
662 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100663 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700664 break;
665 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100666 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700667 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100668 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700669 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100670 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
671}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700672
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100673static void nfs4_open_release(void *calldata)
674{
675 struct nfs4_opendata *data = calldata;
676 struct nfs4_state *state = NULL;
677
678 /* If this request hasn't been cancelled, do nothing */
679 if (data->cancelled == 0)
680 goto out_free;
681 /* In case of error, no cleanup! */
682 if (data->rpc_status != 0)
683 goto out_free;
684 /* In case we need an open_confirm, no cleanup! */
685 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
686 goto out_free;
687 nfs_confirm_seqid(&data->owner->so_seqid, 0);
688 state = nfs4_opendata_to_nfs4_state(data);
689 if (state != NULL)
690 nfs4_close_state(state, data->o_arg.open_flags);
691out_free:
692 nfs4_opendata_free(data);
693}
694
695static const struct rpc_call_ops nfs4_open_ops = {
696 .rpc_call_prepare = nfs4_open_prepare,
697 .rpc_call_done = nfs4_open_done,
698 .rpc_release = nfs4_open_release,
699};
700
701/*
702 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
703 */
704static int _nfs4_proc_open(struct nfs4_opendata *data)
705{
706 struct inode *dir = data->dir->d_inode;
707 struct nfs_server *server = NFS_SERVER(dir);
708 struct nfs_openargs *o_arg = &data->o_arg;
709 struct nfs_openres *o_res = &data->o_res;
710 struct rpc_task *task;
711 int status;
712
713 atomic_inc(&data->count);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500714 /*
715 * If rpc_run_task() ends up calling ->rpc_release(), we
716 * want to ensure that it takes the 'error' code path.
717 */
718 data->rpc_status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100719 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500720 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100721 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100722 status = nfs4_wait_for_completion_rpc_task(task);
723 if (status != 0) {
724 data->cancelled = 1;
725 smp_wmb();
726 } else
727 status = data->rpc_status;
728 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100730 return status;
731
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400732 if (o_arg->open_flags & O_CREAT) {
733 update_changeattr(dir, &o_res->cinfo);
734 nfs_post_op_update_inode(dir, o_res->dir_attr);
735 } else
736 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100738 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100742 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100744 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
749{
750 struct nfs_access_entry cache;
751 int mask = 0;
752 int status;
753
754 if (openflags & FMODE_READ)
755 mask |= MAY_READ;
756 if (openflags & FMODE_WRITE)
757 mask |= MAY_WRITE;
758 status = nfs_access_get_cached(inode, cred, &cache);
759 if (status == 0)
760 goto out;
761
762 /* Be clever: ask server to check for all possible rights */
763 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
764 cache.cred = cred;
765 cache.jiffies = jiffies;
766 status = _nfs4_proc_access(inode, &cache);
767 if (status != 0)
768 return status;
769 nfs_access_add_cache(inode, &cache);
770out:
771 if ((cache.mask & mask) == mask)
772 return 0;
773 return -EACCES;
774}
775
Trond Myklebust58d97142006-01-03 09:55:24 +0100776int nfs4_recover_expired_lease(struct nfs_server *server)
777{
778 struct nfs4_client *clp = server->nfs4_state;
779
780 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
781 nfs4_schedule_state_recovery(clp);
782 return nfs4_wait_clnt_recover(server->client, clp);
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
786 * OPEN_EXPIRED:
787 * reclaim state on the server after a network partition.
788 * Assumes caller holds the appropriate lock
789 */
790static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100795 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100796 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100799 ret = _nfs4_do_access(inode, sp->so_cred, openflags);
800 if (ret < 0)
801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
803 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100806 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100808 return -ENOMEM;
809 ret = nfs4_open_recover(opendata, state);
810 if (ret == -ESTALE) {
811 /* Invalidate the state owner so we don't ever use it again */
812 nfs4_drop_state_owner(sp);
813 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Trond Myklebust202b50d2005-06-22 17:16:29 +0000819static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
820{
821 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
822 struct nfs4_exception exception = { };
823 int err;
824
825 do {
826 err = _nfs4_open_expired(sp, state, dentry);
827 if (err == -NFS4ERR_DELAY)
828 nfs4_handle_exception(server, err, &exception);
829 } while (exception.retry);
830 return err;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100836 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Trond Myklebust864472e2006-01-03 09:55:15 +0100838 ctx = nfs4_state_find_open_context(state);
839 if (IS_ERR(ctx))
840 return PTR_ERR(ctx);
841 ret = nfs4_do_open_expired(sp, state, ctx->dentry);
842 put_nfs_open_context(ctx);
843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100847 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
850{
851 struct nfs_delegation *delegation;
852 struct nfs_server *server = NFS_SERVER(inode);
853 struct nfs4_client *clp = server->nfs4_state;
854 struct nfs_inode *nfsi = NFS_I(inode);
855 struct nfs4_state_owner *sp = NULL;
856 struct nfs4_state *state = NULL;
857 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
858 int err;
859
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100860 err = -ENOMEM;
861 if (!(sp = nfs4_get_state_owner(server, cred))) {
862 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
863 return err;
864 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100865 err = nfs4_recover_expired_lease(server);
866 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100867 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Protect against reboot recovery - NOTE ORDER! */
869 down_read(&clp->cl_sem);
870 /* Protect against delegation recall */
871 down_read(&nfsi->rwsem);
872 delegation = NFS_I(inode)->delegation;
873 err = -ENOENT;
874 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
875 goto out_err;
876 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 state = nfs4_get_open_state(inode, sp);
878 if (state == NULL)
879 goto out_err;
880
881 err = -ENOENT;
882 if ((state->state & open_flags) == open_flags) {
883 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100884 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_unlock(&inode->i_lock);
886 goto out_ok;
887 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100888 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 lock_kernel();
891 err = _nfs4_do_access(inode, cred, open_flags);
892 unlock_kernel();
893 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100894 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 set_bit(NFS_DELEGATED_STATE, &state->flags);
896 update_open_stateid(state, &delegation->stateid, open_flags);
897out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 nfs4_put_state_owner(sp);
899 up_read(&nfsi->rwsem);
900 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100902 return 0;
903out_put_open_state:
904 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 up_read(&nfsi->rwsem);
907 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700908 if (err != -EACCES)
909 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100910out_put_state_owner:
911 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return err;
913}
914
915static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
916{
917 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500918 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int err;
920
921 do {
922 err = _nfs4_open_delegated(inode, flags, cred, &res);
923 if (err == 0)
924 break;
925 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
926 err, &exception));
927 } while (exception.retry);
928 return res;
929}
930
931/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100932 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
934static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
935{
936 struct nfs4_state_owner *sp;
937 struct nfs4_state *state = NULL;
938 struct nfs_server *server = NFS_SERVER(dir);
939 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100940 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 status = -ENOMEM;
945 if (!(sp = nfs4_get_state_owner(server, cred))) {
946 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
947 goto out_err;
948 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100949 status = nfs4_recover_expired_lease(server);
950 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100951 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100952 down_read(&clp->cl_sem);
953 status = -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100954 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
955 if (opendata == NULL)
956 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100958 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (status != 0)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 goto err_opendata_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100963 state = nfs4_opendata_to_nfs4_state(opendata);
964 if (state == NULL)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965 goto err_opendata_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100967 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100968 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 nfs4_put_state_owner(sp);
970 up_read(&clp->cl_sem);
971 *res = state;
972 return 0;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973err_opendata_free:
974 nfs4_opendata_free(opendata);
975err_put_state_owner:
976 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
979 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *res = NULL;
981 return status;
982}
983
984
985static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
986{
987 struct nfs4_exception exception = { };
988 struct nfs4_state *res;
989 int status;
990
991 do {
992 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
993 if (status == 0)
994 break;
995 /* NOTE: BAD_SEQID means the server and client disagree about the
996 * book-keeping w.r.t. state-changing operations
997 * (OPEN/CLOSE/LOCK/LOCKU...)
998 * It is actually a sign of a bug on the client or on the server.
999 *
1000 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001001 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * have unhashed the old state_owner for us, and that we can
1003 * therefore safely retry using a new one. We should still warn
1004 * the user though...
1005 */
1006 if (status == -NFS4ERR_BAD_SEQID) {
1007 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1008 exception.retry = 1;
1009 continue;
1010 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001011 /*
1012 * BAD_STATEID on OPEN means that the server cancelled our
1013 * state before it received the OPEN_CONFIRM.
1014 * Recover by retrying the request as per the discussion
1015 * on Page 181 of RFC3530.
1016 */
1017 if (status == -NFS4ERR_BAD_STATEID) {
1018 exception.retry = 1;
1019 continue;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1022 status, &exception));
1023 } while (exception.retry);
1024 return res;
1025}
1026
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001027static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1028 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001030 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001032 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .iap = sattr,
1034 .server = server,
1035 .bitmask = server->attr_bitmask,
1036 };
1037 struct nfs_setattrres res = {
1038 .fattr = fattr,
1039 .server = server,
1040 };
1041 struct rpc_message msg = {
1042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1043 .rpc_argp = &arg,
1044 .rpc_resp = &res,
1045 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001046 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001047 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Trond Myklebust0e574af2005-10-27 22:12:38 -04001049 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001051 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1052 /* Use that stateid */
1053 } else if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001055 nfs4_copy_stateid(&arg.stateid, state, current->files);
1056 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1058
Trond Myklebust65e43082005-08-16 11:49:44 -04001059 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001060 if (status == 0 && state != NULL)
1061 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001062 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001065static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1066 struct iattr *sattr, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001068 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct nfs4_exception exception = { };
1070 int err;
1071 do {
1072 err = nfs4_handle_exception(server,
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001073 _nfs4_do_setattr(inode, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 &exception);
1075 } while (exception.retry);
1076 return err;
1077}
1078
1079struct nfs4_closedata {
1080 struct inode *inode;
1081 struct nfs4_state *state;
1082 struct nfs_closeargs arg;
1083 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001084 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001085 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
1087
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001088static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001089{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001090 struct nfs4_closedata *calldata = data;
1091 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001092
1093 nfs4_put_open_state(calldata->state);
1094 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001095 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001096 kfree(calldata);
1097}
1098
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001099static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001101 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct nfs_server *server = NFS_SERVER(calldata->inode);
1104
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001105 if (RPC_ASSASSINATED(task))
1106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* hmm. we are done with the inode, and in the process of freeing
1108 * the state_owner. we keep this around to process errors
1109 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001110 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 switch (task->tk_status) {
1112 case 0:
1113 memcpy(&state->stateid, &calldata->res.stateid,
1114 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001115 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117 case -NFS4ERR_STALE_STATEID:
1118 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 nfs4_schedule_state_recovery(server->nfs4_state);
1120 break;
1121 default:
1122 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1123 rpc_restart_call(task);
1124 return;
1125 }
1126 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001127 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001130static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001132 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001133 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct rpc_message msg = {
1135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1136 .rpc_argp = &calldata->arg,
1137 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001138 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001140 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001141
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001142 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001143 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001144 /* Recalculate the new open mode in case someone reopened the file
1145 * while we were waiting in line to be scheduled.
1146 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001147 spin_lock(&state->owner->so_lock);
1148 spin_lock(&calldata->inode->i_lock);
1149 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001150 if (state->n_rdwr == 0) {
1151 if (state->n_rdonly == 0)
1152 mode &= ~FMODE_READ;
1153 if (state->n_wronly == 0)
1154 mode &= ~FMODE_WRITE;
1155 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001156 nfs4_state_set_mode_locked(state, mode);
1157 spin_unlock(&calldata->inode->i_lock);
1158 spin_unlock(&state->owner->so_lock);
1159 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001160 /* Note: exit _without_ calling nfs4_close_done */
1161 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001162 return;
1163 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001164 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001165 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001167 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001168 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001169 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001172static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001173 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001174 .rpc_call_done = nfs4_close_done,
1175 .rpc_release = nfs4_free_closedata,
1176};
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178/*
1179 * It is possible for data to be read/written from a mem-mapped file
1180 * after the sys_close call (which hits the vfs layer as a flush).
1181 * This means that we can't safely call nfsv4 close on a file until
1182 * the inode is cleared. This in turn means that we are not good
1183 * NFSv4 citizens - we do not indicate to the server to update the file's
1184 * share state even when we are done with one of the three share
1185 * stateid's in the inode.
1186 *
1187 * NOTE: Caller must be holding the sp->so_owner semaphore!
1188 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001189int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Trond Myklebust516a6af2005-10-27 22:12:41 -04001191 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -07001193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Trond Myklebust95121352005-10-18 14:20:12 -07001195 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 calldata->inode = inode;
1199 calldata->state = state;
1200 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001201 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001203 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001204 if (calldata->arg.seqid == NULL)
1205 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001206 calldata->arg.bitmask = server->attr_bitmask;
1207 calldata->res.fattr = &calldata->fattr;
1208 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -07001209
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001210 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -07001211 if (status == 0)
1212 goto out;
1213
1214 nfs_free_seqid(calldata->arg.seqid);
1215out_free_calldata:
1216 kfree(calldata);
1217out:
1218 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Trond Myklebust02a913a2005-10-18 14:20:17 -07001221static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1222{
1223 struct file *filp;
1224
1225 filp = lookup_instantiate_filp(nd, dentry, NULL);
1226 if (!IS_ERR(filp)) {
1227 struct nfs_open_context *ctx;
1228 ctx = (struct nfs_open_context *)filp->private_data;
1229 ctx->state = state;
1230 } else
1231 nfs4_close_state(state, nd->intent.open.flags);
1232}
1233
1234struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1236{
1237 struct iattr attr;
1238 struct rpc_cred *cred;
1239 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001240 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (nd->flags & LOOKUP_CREATE) {
1243 attr.ia_mode = nd->intent.open.create_mode;
1244 attr.ia_valid = ATTR_MODE;
1245 if (!IS_POSIXACL(dir))
1246 attr.ia_mode &= ~current->fs->umask;
1247 } else {
1248 attr.ia_valid = 0;
1249 BUG_ON(nd->intent.open.flags & O_CREAT);
1250 }
1251
1252 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1253 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001254 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1256 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001257 if (IS_ERR(state)) {
1258 if (PTR_ERR(state) == -ENOENT)
1259 d_add(dentry, NULL);
1260 return (struct dentry *)state;
1261 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001262 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001263 if (res != NULL)
1264 dentry = res;
1265 nfs4_intent_set_file(nd, dentry, state);
1266 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001270nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 struct rpc_cred *cred;
1273 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1276 if (IS_ERR(cred))
1277 return PTR_ERR(cred);
1278 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1279 if (IS_ERR(state))
1280 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1281 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001282 if (IS_ERR(state)) {
1283 switch (PTR_ERR(state)) {
1284 case -EPERM:
1285 case -EACCES:
1286 case -EDQUOT:
1287 case -ENOSPC:
1288 case -EROFS:
1289 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1290 return 1;
1291 case -ENOENT:
1292 if (dentry->d_inode == NULL)
1293 return 1;
1294 }
1295 goto out_drop;
1296 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 if (state->inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001298 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 1;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001302out_drop:
1303 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
1307
1308static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1309{
1310 struct nfs4_server_caps_res res = {};
1311 struct rpc_message msg = {
1312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1313 .rpc_argp = fhandle,
1314 .rpc_resp = &res,
1315 };
1316 int status;
1317
1318 status = rpc_call_sync(server->client, &msg, 0);
1319 if (status == 0) {
1320 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1321 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1322 server->caps |= NFS_CAP_ACLS;
1323 if (res.has_links != 0)
1324 server->caps |= NFS_CAP_HARDLINKS;
1325 if (res.has_symlinks != 0)
1326 server->caps |= NFS_CAP_SYMLINKS;
1327 server->acl_bitmask = res.acl_bitmask;
1328 }
1329 return status;
1330}
1331
1332static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1333{
1334 struct nfs4_exception exception = { };
1335 int err;
1336 do {
1337 err = nfs4_handle_exception(server,
1338 _nfs4_server_capabilities(server, fhandle),
1339 &exception);
1340 } while (exception.retry);
1341 return err;
1342}
1343
1344static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1345 struct nfs_fsinfo *info)
1346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct nfs4_lookup_root_arg args = {
1348 .bitmask = nfs4_fattr_bitmap,
1349 };
1350 struct nfs4_lookup_res res = {
1351 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001352 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .fh = fhandle,
1354 };
1355 struct rpc_message msg = {
1356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1357 .rpc_argp = &args,
1358 .rpc_resp = &res,
1359 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001360 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return rpc_call_sync(server->client, &msg, 0);
1362}
1363
1364static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1365 struct nfs_fsinfo *info)
1366{
1367 struct nfs4_exception exception = { };
1368 int err;
1369 do {
1370 err = nfs4_handle_exception(server,
1371 _nfs4_lookup_root(server, fhandle, info),
1372 &exception);
1373 } while (exception.retry);
1374 return err;
1375}
1376
1377static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1378 struct nfs_fsinfo *info)
1379{
1380 struct nfs_fattr * fattr = info->fattr;
1381 unsigned char * p;
1382 struct qstr q;
1383 struct nfs4_lookup_arg args = {
1384 .dir_fh = fhandle,
1385 .name = &q,
1386 .bitmask = nfs4_fattr_bitmap,
1387 };
1388 struct nfs4_lookup_res res = {
1389 .server = server,
1390 .fattr = fattr,
1391 .fh = fhandle,
1392 };
1393 struct rpc_message msg = {
1394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1395 .rpc_argp = &args,
1396 .rpc_resp = &res,
1397 };
1398 int status;
1399
1400 /*
1401 * Now we do a separate LOOKUP for each component of the mount path.
1402 * The LOOKUPs are done separately so that we can conveniently
1403 * catch an ERR_WRONGSEC if it occurs along the way...
1404 */
1405 status = nfs4_lookup_root(server, fhandle, info);
1406 if (status)
1407 goto out;
1408
1409 p = server->mnt_path;
1410 for (;;) {
1411 struct nfs4_exception exception = { };
1412
1413 while (*p == '/')
1414 p++;
1415 if (!*p)
1416 break;
1417 q.name = p;
1418 while (*p && (*p != '/'))
1419 p++;
1420 q.len = p - q.name;
1421
1422 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001423 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 status = nfs4_handle_exception(server,
1425 rpc_call_sync(server->client, &msg, 0),
1426 &exception);
1427 } while (exception.retry);
1428 if (status == 0)
1429 continue;
1430 if (status == -ENOENT) {
1431 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1432 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1433 }
1434 break;
1435 }
1436 if (status == 0)
1437 status = nfs4_server_capabilities(server, fhandle);
1438 if (status == 0)
1439 status = nfs4_do_fsinfo(server, fhandle, info);
1440out:
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001441 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
1444static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1445{
1446 struct nfs4_getattr_arg args = {
1447 .fh = fhandle,
1448 .bitmask = server->attr_bitmask,
1449 };
1450 struct nfs4_getattr_res res = {
1451 .fattr = fattr,
1452 .server = server,
1453 };
1454 struct rpc_message msg = {
1455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1456 .rpc_argp = &args,
1457 .rpc_resp = &res,
1458 };
1459
Trond Myklebust0e574af2005-10-27 22:12:38 -04001460 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return rpc_call_sync(server->client, &msg, 0);
1462}
1463
1464static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1465{
1466 struct nfs4_exception exception = { };
1467 int err;
1468 do {
1469 err = nfs4_handle_exception(server,
1470 _nfs4_proc_getattr(server, fhandle, fattr),
1471 &exception);
1472 } while (exception.retry);
1473 return err;
1474}
1475
1476/*
1477 * The file is not closed if it is opened due to the a request to change
1478 * the size of the file. The open call will not be needed once the
1479 * VFS layer lookup-intents are implemented.
1480 *
1481 * Close is called when the inode is destroyed.
1482 * If we haven't opened the file for O_WRONLY, we
1483 * need to in the size_change case to obtain a stateid.
1484 *
1485 * Got race?
1486 * Because OPEN is always done by name in nfsv4, it is
1487 * possible that we opened a different file by the same
1488 * name. We can recognize this race condition, but we
1489 * can't do anything about it besides returning an error.
1490 *
1491 * This will be fixed with VFS changes (lookup-intent).
1492 */
1493static int
1494nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1495 struct iattr *sattr)
1496{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001497 struct rpc_cred *cred;
1498 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001499 struct nfs_open_context *ctx;
1500 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int status;
1502
Trond Myklebust0e574af2005-10-27 22:12:38 -04001503 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001505 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1506 if (IS_ERR(cred))
1507 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001508
1509 /* Search for an existing open(O_WRITE) file */
1510 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1511 if (ctx != NULL)
1512 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001513
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001514 status = nfs4_do_setattr(inode, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001515 if (status == 0)
1516 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001517 if (ctx != NULL)
1518 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001519 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return status;
1521}
1522
1523static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1524 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1525{
1526 int status;
1527 struct nfs_server *server = NFS_SERVER(dir);
1528 struct nfs4_lookup_arg args = {
1529 .bitmask = server->attr_bitmask,
1530 .dir_fh = NFS_FH(dir),
1531 .name = name,
1532 };
1533 struct nfs4_lookup_res res = {
1534 .server = server,
1535 .fattr = fattr,
1536 .fh = fhandle,
1537 };
1538 struct rpc_message msg = {
1539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1540 .rpc_argp = &args,
1541 .rpc_resp = &res,
1542 };
1543
Trond Myklebust0e574af2005-10-27 22:12:38 -04001544 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 dprintk("NFS call lookup %s\n", name->name);
1547 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1548 dprintk("NFS reply lookup: %d\n", status);
1549 return status;
1550}
1551
1552static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1553{
1554 struct nfs4_exception exception = { };
1555 int err;
1556 do {
1557 err = nfs4_handle_exception(NFS_SERVER(dir),
1558 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1559 &exception);
1560 } while (exception.retry);
1561 return err;
1562}
1563
1564static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1565{
1566 struct nfs4_accessargs args = {
1567 .fh = NFS_FH(inode),
1568 };
1569 struct nfs4_accessres res = { 0 };
1570 struct rpc_message msg = {
1571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1572 .rpc_argp = &args,
1573 .rpc_resp = &res,
1574 .rpc_cred = entry->cred,
1575 };
1576 int mode = entry->mask;
1577 int status;
1578
1579 /*
1580 * Determine which access bits we want to ask for...
1581 */
1582 if (mode & MAY_READ)
1583 args.access |= NFS4_ACCESS_READ;
1584 if (S_ISDIR(inode->i_mode)) {
1585 if (mode & MAY_WRITE)
1586 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1587 if (mode & MAY_EXEC)
1588 args.access |= NFS4_ACCESS_LOOKUP;
1589 } else {
1590 if (mode & MAY_WRITE)
1591 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1592 if (mode & MAY_EXEC)
1593 args.access |= NFS4_ACCESS_EXECUTE;
1594 }
1595 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1596 if (!status) {
1597 entry->mask = 0;
1598 if (res.access & NFS4_ACCESS_READ)
1599 entry->mask |= MAY_READ;
1600 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1601 entry->mask |= MAY_WRITE;
1602 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1603 entry->mask |= MAY_EXEC;
1604 }
1605 return status;
1606}
1607
1608static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1609{
1610 struct nfs4_exception exception = { };
1611 int err;
1612 do {
1613 err = nfs4_handle_exception(NFS_SERVER(inode),
1614 _nfs4_proc_access(inode, entry),
1615 &exception);
1616 } while (exception.retry);
1617 return err;
1618}
1619
1620/*
1621 * TODO: For the time being, we don't try to get any attributes
1622 * along with any of the zero-copy operations READ, READDIR,
1623 * READLINK, WRITE.
1624 *
1625 * In the case of the first three, we want to put the GETATTR
1626 * after the read-type operation -- this is because it is hard
1627 * to predict the length of a GETATTR response in v4, and thus
1628 * align the READ data correctly. This means that the GETATTR
1629 * may end up partially falling into the page cache, and we should
1630 * shift it into the 'tail' of the xdr_buf before processing.
1631 * To do this efficiently, we need to know the total length
1632 * of data received, which doesn't seem to be available outside
1633 * of the RPC layer.
1634 *
1635 * In the case of WRITE, we also want to put the GETATTR after
1636 * the operation -- in this case because we want to make sure
1637 * we get the post-operation mtime and size. This means that
1638 * we can't use xdr_encode_pages() as written: we need a variant
1639 * of it which would leave room in the 'tail' iovec.
1640 *
1641 * Both of these changes to the XDR layer would in fact be quite
1642 * minor, but I decided to leave them for a subsequent patch.
1643 */
1644static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1645 unsigned int pgbase, unsigned int pglen)
1646{
1647 struct nfs4_readlink args = {
1648 .fh = NFS_FH(inode),
1649 .pgbase = pgbase,
1650 .pglen = pglen,
1651 .pages = &page,
1652 };
1653 struct rpc_message msg = {
1654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1655 .rpc_argp = &args,
1656 .rpc_resp = NULL,
1657 };
1658
1659 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1660}
1661
1662static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1663 unsigned int pgbase, unsigned int pglen)
1664{
1665 struct nfs4_exception exception = { };
1666 int err;
1667 do {
1668 err = nfs4_handle_exception(NFS_SERVER(inode),
1669 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1670 &exception);
1671 } while (exception.retry);
1672 return err;
1673}
1674
1675static int _nfs4_proc_read(struct nfs_read_data *rdata)
1676{
1677 int flags = rdata->flags;
1678 struct inode *inode = rdata->inode;
1679 struct nfs_fattr *fattr = rdata->res.fattr;
1680 struct nfs_server *server = NFS_SERVER(inode);
1681 struct rpc_message msg = {
1682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1683 .rpc_argp = &rdata->args,
1684 .rpc_resp = &rdata->res,
1685 .rpc_cred = rdata->cred,
1686 };
1687 unsigned long timestamp = jiffies;
1688 int status;
1689
1690 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1691 (long long) rdata->args.offset);
1692
Trond Myklebust0e574af2005-10-27 22:12:38 -04001693 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 status = rpc_call_sync(server->client, &msg, flags);
1695 if (!status)
1696 renew_lease(server, timestamp);
1697 dprintk("NFS reply read: %d\n", status);
1698 return status;
1699}
1700
1701static int nfs4_proc_read(struct nfs_read_data *rdata)
1702{
1703 struct nfs4_exception exception = { };
1704 int err;
1705 do {
1706 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1707 _nfs4_proc_read(rdata),
1708 &exception);
1709 } while (exception.retry);
1710 return err;
1711}
1712
1713static int _nfs4_proc_write(struct nfs_write_data *wdata)
1714{
1715 int rpcflags = wdata->flags;
1716 struct inode *inode = wdata->inode;
1717 struct nfs_fattr *fattr = wdata->res.fattr;
1718 struct nfs_server *server = NFS_SERVER(inode);
1719 struct rpc_message msg = {
1720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1721 .rpc_argp = &wdata->args,
1722 .rpc_resp = &wdata->res,
1723 .rpc_cred = wdata->cred,
1724 };
1725 int status;
1726
1727 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1728 (long long) wdata->args.offset);
1729
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001730 wdata->args.bitmask = server->attr_bitmask;
1731 wdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001732 wdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001733 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 status = rpc_call_sync(server->client, &msg, rpcflags);
1735 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001736 if (status < 0)
1737 return status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001738 renew_lease(server, wdata->timestamp);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001739 nfs_post_op_update_inode(inode, fattr);
1740 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743static int nfs4_proc_write(struct nfs_write_data *wdata)
1744{
1745 struct nfs4_exception exception = { };
1746 int err;
1747 do {
1748 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1749 _nfs4_proc_write(wdata),
1750 &exception);
1751 } while (exception.retry);
1752 return err;
1753}
1754
1755static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1756{
1757 struct inode *inode = cdata->inode;
1758 struct nfs_fattr *fattr = cdata->res.fattr;
1759 struct nfs_server *server = NFS_SERVER(inode);
1760 struct rpc_message msg = {
1761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1762 .rpc_argp = &cdata->args,
1763 .rpc_resp = &cdata->res,
1764 .rpc_cred = cdata->cred,
1765 };
1766 int status;
1767
1768 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1769 (long long) cdata->args.offset);
1770
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001771 cdata->args.bitmask = server->attr_bitmask;
1772 cdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001773 cdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001774 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001776 if (status >= 0)
1777 renew_lease(server, cdata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001779 if (status >= 0)
1780 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return status;
1782}
1783
1784static int nfs4_proc_commit(struct nfs_write_data *cdata)
1785{
1786 struct nfs4_exception exception = { };
1787 int err;
1788 do {
1789 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1790 _nfs4_proc_commit(cdata),
1791 &exception);
1792 } while (exception.retry);
1793 return err;
1794}
1795
1796/*
1797 * Got race?
1798 * We will need to arrange for the VFS layer to provide an atomic open.
1799 * Until then, this create/open method is prone to inefficiency and race
1800 * conditions due to the lookup, create, and open VFS calls from sys_open()
1801 * placed on the wire.
1802 *
1803 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1804 * The file will be opened again in the subsequent VFS open call
1805 * (nfs4_proc_file_open).
1806 *
1807 * The open for read will just hang around to be used by any process that
1808 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1809 */
1810
1811static int
1812nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001813 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct nfs4_state *state;
1816 struct rpc_cred *cred;
1817 int status = 0;
1818
1819 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1820 if (IS_ERR(cred)) {
1821 status = PTR_ERR(cred);
1822 goto out;
1823 }
1824 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1825 put_rpccred(cred);
1826 if (IS_ERR(state)) {
1827 status = PTR_ERR(state);
1828 goto out;
1829 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (flags & O_EXCL) {
1832 struct nfs_fattr fattr;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001833 status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001834 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001835 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001836 }
1837 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1838 nfs4_intent_set_file(nd, dentry, state);
1839 else
1840 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841out:
1842 return status;
1843}
1844
1845static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1846{
Trond Myklebust16e42952005-10-27 22:12:44 -04001847 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct nfs4_remove_arg args = {
1849 .fh = NFS_FH(dir),
1850 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001851 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001853 struct nfs_fattr dir_attr;
1854 struct nfs4_remove_res res = {
1855 .server = server,
1856 .dir_attr = &dir_attr,
1857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct rpc_message msg = {
1859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1860 .rpc_argp = &args,
1861 .rpc_resp = &res,
1862 };
1863 int status;
1864
Trond Myklebust16e42952005-10-27 22:12:44 -04001865 nfs_fattr_init(res.dir_attr);
1866 status = rpc_call_sync(server->client, &msg, 0);
1867 if (status == 0) {
1868 update_changeattr(dir, &res.cinfo);
1869 nfs_post_op_update_inode(dir, res.dir_attr);
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return status;
1872}
1873
1874static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1875{
1876 struct nfs4_exception exception = { };
1877 int err;
1878 do {
1879 err = nfs4_handle_exception(NFS_SERVER(dir),
1880 _nfs4_proc_remove(dir, name),
1881 &exception);
1882 } while (exception.retry);
1883 return err;
1884}
1885
1886struct unlink_desc {
1887 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001888 struct nfs4_remove_res res;
1889 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890};
1891
1892static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1893 struct qstr *name)
1894{
Trond Myklebust16e42952005-10-27 22:12:44 -04001895 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct unlink_desc *up;
1897
1898 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1899 if (!up)
1900 return -ENOMEM;
1901
1902 up->args.fh = NFS_FH(dir->d_inode);
1903 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001904 up->args.bitmask = server->attr_bitmask;
1905 up->res.server = server;
1906 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1909 msg->rpc_argp = &up->args;
1910 msg->rpc_resp = &up->res;
1911 return 0;
1912}
1913
1914static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1915{
1916 struct rpc_message *msg = &task->tk_msg;
1917 struct unlink_desc *up;
1918
1919 if (msg->rpc_resp != NULL) {
1920 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001921 update_changeattr(dir->d_inode, &up->res.cinfo);
1922 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 kfree(up);
1924 msg->rpc_resp = NULL;
1925 msg->rpc_argp = NULL;
1926 }
1927 return 0;
1928}
1929
1930static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1931 struct inode *new_dir, struct qstr *new_name)
1932{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001933 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct nfs4_rename_arg arg = {
1935 .old_dir = NFS_FH(old_dir),
1936 .new_dir = NFS_FH(new_dir),
1937 .old_name = old_name,
1938 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001939 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001941 struct nfs_fattr old_fattr, new_fattr;
1942 struct nfs4_rename_res res = {
1943 .server = server,
1944 .old_fattr = &old_fattr,
1945 .new_fattr = &new_fattr,
1946 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct rpc_message msg = {
1948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1949 .rpc_argp = &arg,
1950 .rpc_resp = &res,
1951 };
1952 int status;
1953
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001954 nfs_fattr_init(res.old_fattr);
1955 nfs_fattr_init(res.new_fattr);
1956 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (!status) {
1959 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001960 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001962 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 return status;
1965}
1966
1967static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1968 struct inode *new_dir, struct qstr *new_name)
1969{
1970 struct nfs4_exception exception = { };
1971 int err;
1972 do {
1973 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1974 _nfs4_proc_rename(old_dir, old_name,
1975 new_dir, new_name),
1976 &exception);
1977 } while (exception.retry);
1978 return err;
1979}
1980
1981static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1982{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001983 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct nfs4_link_arg arg = {
1985 .fh = NFS_FH(inode),
1986 .dir_fh = NFS_FH(dir),
1987 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001988 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001990 struct nfs_fattr fattr, dir_attr;
1991 struct nfs4_link_res res = {
1992 .server = server,
1993 .fattr = &fattr,
1994 .dir_attr = &dir_attr,
1995 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct rpc_message msg = {
1997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1998 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001999 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 };
2001 int status;
2002
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002003 nfs_fattr_init(res.fattr);
2004 nfs_fattr_init(res.dir_attr);
2005 status = rpc_call_sync(server->client, &msg, 0);
2006 if (!status) {
2007 update_changeattr(dir, &res.cinfo);
2008 nfs_post_op_update_inode(dir, res.dir_attr);
2009 nfs_refresh_inode(inode, res.fattr);
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 return status;
2013}
2014
2015static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2016{
2017 struct nfs4_exception exception = { };
2018 int err;
2019 do {
2020 err = nfs4_handle_exception(NFS_SERVER(inode),
2021 _nfs4_proc_link(inode, dir, name),
2022 &exception);
2023 } while (exception.retry);
2024 return err;
2025}
2026
2027static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2028 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2029 struct nfs_fattr *fattr)
2030{
2031 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002032 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct nfs4_create_arg arg = {
2034 .dir_fh = NFS_FH(dir),
2035 .server = server,
2036 .name = name,
2037 .attrs = sattr,
2038 .ftype = NF4LNK,
2039 .bitmask = server->attr_bitmask,
2040 };
2041 struct nfs4_create_res res = {
2042 .server = server,
2043 .fh = fhandle,
2044 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002045 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 };
2047 struct rpc_message msg = {
2048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2049 .rpc_argp = &arg,
2050 .rpc_resp = &res,
2051 };
2052 int status;
2053
2054 if (path->len > NFS4_MAXPATHLEN)
2055 return -ENAMETOOLONG;
2056 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04002057 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002058 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2061 if (!status)
2062 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002063 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return status;
2065}
2066
2067static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2068 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2069 struct nfs_fattr *fattr)
2070{
2071 struct nfs4_exception exception = { };
2072 int err;
2073 do {
2074 err = nfs4_handle_exception(NFS_SERVER(dir),
2075 _nfs4_proc_symlink(dir, name, path, sattr,
2076 fhandle, fattr),
2077 &exception);
2078 } while (exception.retry);
2079 return err;
2080}
2081
2082static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2083 struct iattr *sattr)
2084{
2085 struct nfs_server *server = NFS_SERVER(dir);
2086 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002087 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 struct nfs4_create_arg arg = {
2089 .dir_fh = NFS_FH(dir),
2090 .server = server,
2091 .name = &dentry->d_name,
2092 .attrs = sattr,
2093 .ftype = NF4DIR,
2094 .bitmask = server->attr_bitmask,
2095 };
2096 struct nfs4_create_res res = {
2097 .server = server,
2098 .fh = &fhandle,
2099 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002100 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 };
2102 struct rpc_message msg = {
2103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2104 .rpc_argp = &arg,
2105 .rpc_resp = &res,
2106 };
2107 int status;
2108
Trond Myklebust0e574af2005-10-27 22:12:38 -04002109 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002110 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2113 if (!status) {
2114 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002115 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 status = nfs_instantiate(dentry, &fhandle, &fattr);
2117 }
2118 return status;
2119}
2120
2121static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2122 struct iattr *sattr)
2123{
2124 struct nfs4_exception exception = { };
2125 int err;
2126 do {
2127 err = nfs4_handle_exception(NFS_SERVER(dir),
2128 _nfs4_proc_mkdir(dir, dentry, sattr),
2129 &exception);
2130 } while (exception.retry);
2131 return err;
2132}
2133
2134static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2135 u64 cookie, struct page *page, unsigned int count, int plus)
2136{
2137 struct inode *dir = dentry->d_inode;
2138 struct nfs4_readdir_arg args = {
2139 .fh = NFS_FH(dir),
2140 .pages = &page,
2141 .pgbase = 0,
2142 .count = count,
2143 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2144 };
2145 struct nfs4_readdir_res res;
2146 struct rpc_message msg = {
2147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2148 .rpc_argp = &args,
2149 .rpc_resp = &res,
2150 .rpc_cred = cred,
2151 };
2152 int status;
2153
Trond Myklebusteadf4592005-06-22 17:16:39 +00002154 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2155 dentry->d_parent->d_name.name,
2156 dentry->d_name.name,
2157 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 lock_kernel();
2159 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2160 res.pgbase = args.pgbase;
2161 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2162 if (status == 0)
2163 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2164 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00002165 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return status;
2167}
2168
2169static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2170 u64 cookie, struct page *page, unsigned int count, int plus)
2171{
2172 struct nfs4_exception exception = { };
2173 int err;
2174 do {
2175 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2176 _nfs4_proc_readdir(dentry, cred, cookie,
2177 page, count, plus),
2178 &exception);
2179 } while (exception.retry);
2180 return err;
2181}
2182
2183static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2184 struct iattr *sattr, dev_t rdev)
2185{
2186 struct nfs_server *server = NFS_SERVER(dir);
2187 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002188 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct nfs4_create_arg arg = {
2190 .dir_fh = NFS_FH(dir),
2191 .server = server,
2192 .name = &dentry->d_name,
2193 .attrs = sattr,
2194 .bitmask = server->attr_bitmask,
2195 };
2196 struct nfs4_create_res res = {
2197 .server = server,
2198 .fh = &fh,
2199 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002200 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 };
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2204 .rpc_argp = &arg,
2205 .rpc_resp = &res,
2206 };
2207 int status;
2208 int mode = sattr->ia_mode;
2209
Trond Myklebust0e574af2005-10-27 22:12:38 -04002210 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002211 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2214 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2215 if (S_ISFIFO(mode))
2216 arg.ftype = NF4FIFO;
2217 else if (S_ISBLK(mode)) {
2218 arg.ftype = NF4BLK;
2219 arg.u.device.specdata1 = MAJOR(rdev);
2220 arg.u.device.specdata2 = MINOR(rdev);
2221 }
2222 else if (S_ISCHR(mode)) {
2223 arg.ftype = NF4CHR;
2224 arg.u.device.specdata1 = MAJOR(rdev);
2225 arg.u.device.specdata2 = MINOR(rdev);
2226 }
2227 else
2228 arg.ftype = NF4SOCK;
2229
2230 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2231 if (status == 0) {
2232 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002233 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 status = nfs_instantiate(dentry, &fh, &fattr);
2235 }
2236 return status;
2237}
2238
2239static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2240 struct iattr *sattr, dev_t rdev)
2241{
2242 struct nfs4_exception exception = { };
2243 int err;
2244 do {
2245 err = nfs4_handle_exception(NFS_SERVER(dir),
2246 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2247 &exception);
2248 } while (exception.retry);
2249 return err;
2250}
2251
2252static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2253 struct nfs_fsstat *fsstat)
2254{
2255 struct nfs4_statfs_arg args = {
2256 .fh = fhandle,
2257 .bitmask = server->attr_bitmask,
2258 };
2259 struct rpc_message msg = {
2260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2261 .rpc_argp = &args,
2262 .rpc_resp = fsstat,
2263 };
2264
Trond Myklebust0e574af2005-10-27 22:12:38 -04002265 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return rpc_call_sync(server->client, &msg, 0);
2267}
2268
2269static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2270{
2271 struct nfs4_exception exception = { };
2272 int err;
2273 do {
2274 err = nfs4_handle_exception(server,
2275 _nfs4_proc_statfs(server, fhandle, fsstat),
2276 &exception);
2277 } while (exception.retry);
2278 return err;
2279}
2280
2281static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2282 struct nfs_fsinfo *fsinfo)
2283{
2284 struct nfs4_fsinfo_arg args = {
2285 .fh = fhandle,
2286 .bitmask = server->attr_bitmask,
2287 };
2288 struct rpc_message msg = {
2289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2290 .rpc_argp = &args,
2291 .rpc_resp = fsinfo,
2292 };
2293
2294 return rpc_call_sync(server->client, &msg, 0);
2295}
2296
2297static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2298{
2299 struct nfs4_exception exception = { };
2300 int err;
2301
2302 do {
2303 err = nfs4_handle_exception(server,
2304 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2305 &exception);
2306 } while (exception.retry);
2307 return err;
2308}
2309
2310static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2311{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002312 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2314}
2315
2316static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_pathconf *pathconf)
2318{
2319 struct nfs4_pathconf_arg args = {
2320 .fh = fhandle,
2321 .bitmask = server->attr_bitmask,
2322 };
2323 struct rpc_message msg = {
2324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2325 .rpc_argp = &args,
2326 .rpc_resp = pathconf,
2327 };
2328
2329 /* None of the pathconf attributes are mandatory to implement */
2330 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2331 memset(pathconf, 0, sizeof(*pathconf));
2332 return 0;
2333 }
2334
Trond Myklebust0e574af2005-10-27 22:12:38 -04002335 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return rpc_call_sync(server->client, &msg, 0);
2337}
2338
2339static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2340 struct nfs_pathconf *pathconf)
2341{
2342 struct nfs4_exception exception = { };
2343 int err;
2344
2345 do {
2346 err = nfs4_handle_exception(server,
2347 _nfs4_proc_pathconf(server, fhandle, pathconf),
2348 &exception);
2349 } while (exception.retry);
2350 return err;
2351}
2352
Trond Myklebustec06c092006-03-20 13:44:27 -05002353static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Trond Myklebustec06c092006-03-20 13:44:27 -05002355 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Trond Myklebustec06c092006-03-20 13:44:27 -05002357 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002359 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002362 renew_lease(server, data->timestamp);
2363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Trond Myklebustec06c092006-03-20 13:44:27 -05002366static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct rpc_message msg = {
2369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2370 .rpc_argp = &data->args,
2371 .rpc_resp = &data->res,
2372 .rpc_cred = data->cred,
2373 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 data->timestamp = jiffies;
2376
Trond Myklebustec06c092006-03-20 13:44:27 -05002377 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Trond Myklebust788e7a82006-03-20 13:44:27 -05002380static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 struct inode *inode = data->inode;
2383
2384 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2385 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002386 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002388 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002390 nfs_post_op_update_inode(inode, data->res.fattr);
2391 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Trond Myklebust788e7a82006-03-20 13:44:27 -05002395static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2399 .rpc_argp = &data->args,
2400 .rpc_resp = &data->res,
2401 .rpc_cred = data->cred,
2402 };
2403 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002404 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 if (how & FLUSH_STABLE) {
2408 if (!NFS_I(inode)->ncommit)
2409 stable = NFS_FILE_SYNC;
2410 else
2411 stable = NFS_DATA_SYNC;
2412 } else
2413 stable = NFS_UNSTABLE;
2414 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002415 data->args.bitmask = server->attr_bitmask;
2416 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 data->timestamp = jiffies;
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002421 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Trond Myklebust788e7a82006-03-20 13:44:27 -05002424static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct inode *inode = data->inode;
2427
2428 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2429 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002430 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002432 if (task->tk_status >= 0)
2433 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Trond Myklebust788e7a82006-03-20 13:44:27 -05002437static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct rpc_message msg = {
2440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2441 .rpc_argp = &data->args,
2442 .rpc_resp = &data->res,
2443 .rpc_cred = data->cred,
2444 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002445 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002447 data->args.bitmask = server->attr_bitmask;
2448 data->res.server = server;
2449
Trond Myklebust788e7a82006-03-20 13:44:27 -05002450 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453/*
2454 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2455 * standalone procedure for queueing an asynchronous RENEW.
2456 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002457static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002460 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 if (task->tk_status < 0) {
2463 switch (task->tk_status) {
2464 case -NFS4ERR_STALE_CLIENTID:
2465 case -NFS4ERR_EXPIRED:
2466 case -NFS4ERR_CB_PATH_DOWN:
2467 nfs4_schedule_state_recovery(clp);
2468 }
2469 return;
2470 }
2471 spin_lock(&clp->cl_lock);
2472 if (time_before(clp->cl_last_renewal,timestamp))
2473 clp->cl_last_renewal = timestamp;
2474 spin_unlock(&clp->cl_lock);
2475}
2476
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002477static const struct rpc_call_ops nfs4_renew_ops = {
2478 .rpc_call_done = nfs4_renew_done,
2479};
2480
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002481int nfs4_proc_async_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 struct rpc_message msg = {
2484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2485 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002486 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 };
2488
2489 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002490 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002493int nfs4_proc_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
2495 struct rpc_message msg = {
2496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2497 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002498 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 };
2500 unsigned long now = jiffies;
2501 int status;
2502
2503 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2504 if (status < 0)
2505 return status;
2506 spin_lock(&clp->cl_lock);
2507 if (time_before(clp->cl_last_renewal,now))
2508 clp->cl_last_renewal = now;
2509 spin_unlock(&clp->cl_lock);
2510 return 0;
2511}
2512
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002513static inline int nfs4_server_supports_acls(struct nfs_server *server)
2514{
2515 return (server->caps & NFS_CAP_ACLS)
2516 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2517 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2518}
2519
2520/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2521 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2522 * the stack.
2523 */
2524#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2525
2526static void buf_to_pages(const void *buf, size_t buflen,
2527 struct page **pages, unsigned int *pgbase)
2528{
2529 const void *p = buf;
2530
2531 *pgbase = offset_in_page(buf);
2532 p -= *pgbase;
2533 while (p < buf + buflen) {
2534 *(pages++) = virt_to_page(p);
2535 p += PAGE_CACHE_SIZE;
2536 }
2537}
2538
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002539struct nfs4_cached_acl {
2540 int cached;
2541 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002542 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002543};
2544
2545static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002546{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002547 struct nfs_inode *nfsi = NFS_I(inode);
2548
2549 spin_lock(&inode->i_lock);
2550 kfree(nfsi->nfs4_acl);
2551 nfsi->nfs4_acl = acl;
2552 spin_unlock(&inode->i_lock);
2553}
2554
2555static void nfs4_zap_acl_attr(struct inode *inode)
2556{
2557 nfs4_set_cached_acl(inode, NULL);
2558}
2559
2560static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2561{
2562 struct nfs_inode *nfsi = NFS_I(inode);
2563 struct nfs4_cached_acl *acl;
2564 int ret = -ENOENT;
2565
2566 spin_lock(&inode->i_lock);
2567 acl = nfsi->nfs4_acl;
2568 if (acl == NULL)
2569 goto out;
2570 if (buf == NULL) /* user is just asking for length */
2571 goto out_len;
2572 if (acl->cached == 0)
2573 goto out;
2574 ret = -ERANGE; /* see getxattr(2) man page */
2575 if (acl->len > buflen)
2576 goto out;
2577 memcpy(buf, acl->data, acl->len);
2578out_len:
2579 ret = acl->len;
2580out:
2581 spin_unlock(&inode->i_lock);
2582 return ret;
2583}
2584
2585static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2586{
2587 struct nfs4_cached_acl *acl;
2588
2589 if (buf && acl_len <= PAGE_SIZE) {
2590 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2591 if (acl == NULL)
2592 goto out;
2593 acl->cached = 1;
2594 memcpy(acl->data, buf, acl_len);
2595 } else {
2596 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2597 if (acl == NULL)
2598 goto out;
2599 acl->cached = 0;
2600 }
2601 acl->len = acl_len;
2602out:
2603 nfs4_set_cached_acl(inode, acl);
2604}
2605
2606static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2607{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002608 struct page *pages[NFS4ACL_MAXPAGES];
2609 struct nfs_getaclargs args = {
2610 .fh = NFS_FH(inode),
2611 .acl_pages = pages,
2612 .acl_len = buflen,
2613 };
2614 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002615 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2618 .rpc_argp = &args,
2619 .rpc_resp = &resp_len,
2620 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002621 struct page *localpage = NULL;
2622 int ret;
2623
2624 if (buflen < PAGE_SIZE) {
2625 /* As long as we're doing a round trip to the server anyway,
2626 * let's be prepared for a page of acl data. */
2627 localpage = alloc_page(GFP_KERNEL);
2628 resp_buf = page_address(localpage);
2629 if (localpage == NULL)
2630 return -ENOMEM;
2631 args.acl_pages[0] = localpage;
2632 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002633 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002634 } else {
2635 resp_buf = buf;
2636 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2637 }
2638 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2639 if (ret)
2640 goto out_free;
2641 if (resp_len > args.acl_len)
2642 nfs4_write_cached_acl(inode, NULL, resp_len);
2643 else
2644 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2645 if (buf) {
2646 ret = -ERANGE;
2647 if (resp_len > buflen)
2648 goto out_free;
2649 if (localpage)
2650 memcpy(buf, resp_buf, resp_len);
2651 }
2652 ret = resp_len;
2653out_free:
2654 if (localpage)
2655 __free_page(localpage);
2656 return ret;
2657}
2658
2659static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2660{
2661 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002662 int ret;
2663
2664 if (!nfs4_server_supports_acls(server))
2665 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002666 ret = nfs_revalidate_inode(server, inode);
2667 if (ret < 0)
2668 return ret;
2669 ret = nfs4_read_cached_acl(inode, buf, buflen);
2670 if (ret != -ENOENT)
2671 return ret;
2672 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002673}
2674
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002675static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2676{
2677 struct nfs_server *server = NFS_SERVER(inode);
2678 struct page *pages[NFS4ACL_MAXPAGES];
2679 struct nfs_setaclargs arg = {
2680 .fh = NFS_FH(inode),
2681 .acl_pages = pages,
2682 .acl_len = buflen,
2683 };
2684 struct rpc_message msg = {
2685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2686 .rpc_argp = &arg,
2687 .rpc_resp = NULL,
2688 };
2689 int ret;
2690
2691 if (!nfs4_server_supports_acls(server))
2692 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002693 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002694 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2695 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002696 if (ret == 0)
2697 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002698 return ret;
2699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002702nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 struct nfs4_client *clp = server->nfs4_state;
2705
2706 if (!clp || task->tk_status >= 0)
2707 return 0;
2708 switch(task->tk_status) {
2709 case -NFS4ERR_STALE_CLIENTID:
2710 case -NFS4ERR_STALE_STATEID:
2711 case -NFS4ERR_EXPIRED:
2712 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2713 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002714 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 rpc_wake_up_task(task);
2716 task->tk_status = 0;
2717 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002719 nfs_inc_server_stats((struct nfs_server *) server,
2720 NFSIOS_DELAY);
2721 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2723 task->tk_status = 0;
2724 return -EAGAIN;
2725 case -NFS4ERR_OLD_STATEID:
2726 task->tk_status = 0;
2727 return -EAGAIN;
2728 }
2729 task->tk_status = nfs4_map_errors(task->tk_status);
2730 return 0;
2731}
2732
Trond Myklebust433fbe42006-01-03 09:55:22 +01002733static int nfs4_wait_bit_interruptible(void *word)
2734{
2735 if (signal_pending(current))
2736 return -ERESTARTSYS;
2737 schedule();
2738 return 0;
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002744 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 might_sleep();
2747
2748 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002749 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2750 nfs4_wait_bit_interruptible,
2751 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 rpc_clnt_sigunmask(clnt, &oldset);
2753 return res;
2754}
2755
2756static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2757{
2758 sigset_t oldset;
2759 int res = 0;
2760
2761 might_sleep();
2762
2763 if (*timeout <= 0)
2764 *timeout = NFS4_POLL_RETRY_MIN;
2765 if (*timeout > NFS4_POLL_RETRY_MAX)
2766 *timeout = NFS4_POLL_RETRY_MAX;
2767 rpc_clnt_sigmask(clnt, &oldset);
2768 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002769 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (signalled())
2771 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002772 } else
2773 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 rpc_clnt_sigunmask(clnt, &oldset);
2775 *timeout <<= 1;
2776 return res;
2777}
2778
2779/* This is the error handling routine for processes that are allowed
2780 * to sleep.
2781 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002782int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 struct nfs4_client *clp = server->nfs4_state;
2785 int ret = errorcode;
2786
2787 exception->retry = 0;
2788 switch(errorcode) {
2789 case 0:
2790 return 0;
2791 case -NFS4ERR_STALE_CLIENTID:
2792 case -NFS4ERR_STALE_STATEID:
2793 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002794 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ret = nfs4_wait_clnt_recover(server->client, clp);
2796 if (ret == 0)
2797 exception->retry = 1;
2798 break;
2799 case -NFS4ERR_GRACE:
2800 case -NFS4ERR_DELAY:
2801 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002802 if (ret != 0)
2803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002805 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 /* We failed to handle the error */
2808 return nfs4_map_errors(ret);
2809}
2810
Trond Myklebust286d7d62006-01-03 09:55:26 +01002811int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 nfs4_verifier sc_verifier;
2814 struct nfs4_setclientid setclientid = {
2815 .sc_verifier = &sc_verifier,
2816 .sc_prog = program,
2817 };
2818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2820 .rpc_argp = &setclientid,
2821 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002822 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 };
2824 u32 *p;
2825 int loop = 0;
2826 int status;
2827
2828 p = (u32*)sc_verifier.data;
2829 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2830 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2831
2832 for(;;) {
2833 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2834 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2835 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002836 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 clp->cl_id_uniquifier);
2838 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2839 sizeof(setclientid.sc_netid), "tcp");
2840 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2841 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2842 clp->cl_ipaddr, port >> 8, port & 255);
2843
2844 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2845 if (status != -NFS4ERR_CLID_INUSE)
2846 break;
2847 if (signalled())
2848 break;
2849 if (loop++ & 1)
2850 ssleep(clp->cl_lease_time + 1);
2851 else
2852 if (++clp->cl_id_uniquifier == 0)
2853 break;
2854 }
2855 return status;
2856}
2857
Trond Myklebust51581f32006-03-20 13:44:47 -05002858static int _nfs4_proc_setclientid_confirm(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 struct nfs_fsinfo fsinfo;
2861 struct rpc_message msg = {
2862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2863 .rpc_argp = clp,
2864 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002865 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 };
2867 unsigned long now;
2868 int status;
2869
2870 now = jiffies;
2871 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2872 if (status == 0) {
2873 spin_lock(&clp->cl_lock);
2874 clp->cl_lease_time = fsinfo.lease_time * HZ;
2875 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002876 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 spin_unlock(&clp->cl_lock);
2878 }
2879 return status;
2880}
2881
Trond Myklebust51581f32006-03-20 13:44:47 -05002882int nfs4_proc_setclientid_confirm(struct nfs4_client *clp, struct rpc_cred *cred)
2883{
2884 long timeout;
2885 int err;
2886 do {
2887 err = _nfs4_proc_setclientid_confirm(clp, cred);
2888 switch (err) {
2889 case 0:
2890 return err;
2891 case -NFS4ERR_RESOURCE:
2892 /* The IBM lawyers misread another document! */
2893 case -NFS4ERR_DELAY:
2894 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2895 }
2896 } while (err == 0);
2897 return err;
2898}
2899
Trond Myklebustfe650402006-01-03 09:55:18 +01002900struct nfs4_delegreturndata {
2901 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002902 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002903 struct nfs_fh fh;
2904 nfs4_stateid stateid;
2905 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002906 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002907 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002908 int rpc_status;
2909};
2910
2911static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Trond Myklebustfe650402006-01-03 09:55:18 +01002913 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 struct rpc_message msg = {
2915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002916 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002917 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002918 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002920 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002921 rpc_call_setup(task, &msg, 0);
2922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebustfe650402006-01-03 09:55:18 +01002924static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2925{
2926 struct nfs4_delegreturndata *data = calldata;
2927 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002928 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002929 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002930}
2931
2932static void nfs4_delegreturn_release(void *calldata)
2933{
2934 struct nfs4_delegreturndata *data = calldata;
2935
2936 put_rpccred(data->cred);
2937 kfree(calldata);
2938}
2939
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002940static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002941 .rpc_call_prepare = nfs4_delegreturn_prepare,
2942 .rpc_call_done = nfs4_delegreturn_done,
2943 .rpc_release = nfs4_delegreturn_release,
2944};
2945
2946static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2947{
2948 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002949 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002950 struct rpc_task *task;
2951 int status;
2952
2953 data = kmalloc(sizeof(*data), GFP_KERNEL);
2954 if (data == NULL)
2955 return -ENOMEM;
2956 data->args.fhandle = &data->fh;
2957 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002958 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002959 nfs_copy_fh(&data->fh, NFS_FH(inode));
2960 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002961 data->res.fattr = &data->fattr;
2962 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002963 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002964 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002965 data->rpc_status = 0;
2966
2967 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002968 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01002969 return PTR_ERR(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01002970 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002971 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002972 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002973 if (status == 0)
2974 nfs_post_op_update_inode(inode, &data->fattr);
2975 }
Trond Myklebustfe650402006-01-03 09:55:18 +01002976 rpc_release_task(task);
2977 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2981{
2982 struct nfs_server *server = NFS_SERVER(inode);
2983 struct nfs4_exception exception = { };
2984 int err;
2985 do {
2986 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2987 switch (err) {
2988 case -NFS4ERR_STALE_STATEID:
2989 case -NFS4ERR_EXPIRED:
2990 nfs4_schedule_state_recovery(server->nfs4_state);
2991 case 0:
2992 return 0;
2993 }
2994 err = nfs4_handle_exception(server, err, &exception);
2995 } while (exception.retry);
2996 return err;
2997}
2998
2999#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3000#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3001
3002/*
3003 * sleep, with exponential backoff, and retry the LOCK operation.
3004 */
3005static unsigned long
3006nfs4_set_lock_task_retry(unsigned long timeout)
3007{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003008 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 timeout <<= 1;
3010 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3011 return NFS4_LOCK_MAXTIMEOUT;
3012 return timeout;
3013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3016{
3017 struct inode *inode = state->inode;
3018 struct nfs_server *server = NFS_SERVER(inode);
3019 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003020 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003022 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003024 struct nfs_lockt_res res = {
3025 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
3027 struct rpc_message msg = {
3028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3029 .rpc_argp = &arg,
3030 .rpc_resp = &res,
3031 .rpc_cred = state->owner->so_cred,
3032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct nfs4_lock_state *lsp;
3034 int status;
3035
3036 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003037 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003038 status = nfs4_set_lock_state(state, request);
3039 if (status != 0)
3040 goto out;
3041 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003042 arg.lock_owner.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003044 switch (status) {
3045 case 0:
3046 request->fl_type = F_UNLCK;
3047 break;
3048 case -NFS4ERR_DENIED:
3049 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 up_read(&clp->cl_sem);
3053 return status;
3054}
3055
3056static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3057{
3058 struct nfs4_exception exception = { };
3059 int err;
3060
3061 do {
3062 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3063 _nfs4_proc_getlk(state, cmd, request),
3064 &exception);
3065 } while (exception.retry);
3066 return err;
3067}
3068
3069static int do_vfs_lock(struct file *file, struct file_lock *fl)
3070{
3071 int res = 0;
3072 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3073 case FL_POSIX:
3074 res = posix_lock_file_wait(file, fl);
3075 break;
3076 case FL_FLOCK:
3077 res = flock_lock_file_wait(file, fl);
3078 break;
3079 default:
3080 BUG();
3081 }
3082 if (res < 0)
3083 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3084 __FUNCTION__);
3085 return res;
3086}
3087
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003088struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003089 struct nfs_locku_args arg;
3090 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003091 struct nfs4_lock_state *lsp;
3092 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003093 struct file_lock fl;
3094 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003095 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003096};
3097
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003098static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3099 struct nfs_open_context *ctx,
3100 struct nfs4_lock_state *lsp,
3101 struct nfs_seqid *seqid)
3102{
3103 struct nfs4_unlockdata *p;
3104 struct inode *inode = lsp->ls_state->inode;
3105
3106 p = kmalloc(sizeof(*p), GFP_KERNEL);
3107 if (p == NULL)
3108 return NULL;
3109 p->arg.fh = NFS_FH(inode);
3110 p->arg.fl = &p->fl;
3111 p->arg.seqid = seqid;
3112 p->arg.stateid = &lsp->ls_stateid;
3113 p->lsp = lsp;
3114 atomic_inc(&lsp->ls_count);
3115 /* Ensure we don't close file until we're done freeing locks! */
3116 p->ctx = get_nfs_open_context(ctx);
3117 memcpy(&p->fl, fl, sizeof(p->fl));
3118 p->server = NFS_SERVER(inode);
3119 return p;
3120}
3121
Trond Myklebust06f814a2006-01-03 09:55:07 +01003122static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003123{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003124 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003125 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003126 nfs4_put_lock_state(calldata->lsp);
3127 put_nfs_open_context(calldata->ctx);
3128 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003129}
3130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003132{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003133 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003134
Trond Myklebust06f814a2006-01-03 09:55:07 +01003135 if (RPC_ASSASSINATED(task))
3136 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003137 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003138 switch (task->tk_status) {
3139 case 0:
3140 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003141 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003142 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003143 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003144 break;
3145 case -NFS4ERR_STALE_STATEID:
3146 case -NFS4ERR_EXPIRED:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003147 nfs4_schedule_state_recovery(calldata->server->nfs4_state);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003148 break;
3149 default:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003150 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) {
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003151 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003152 }
3153 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003154}
3155
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003156static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003157{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003158 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct rpc_message msg = {
3160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003161 .rpc_argp = &calldata->arg,
3162 .rpc_resp = &calldata->res,
3163 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003166 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003167 return;
3168 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003169 /* Note: exit _without_ running nfs4_locku_done */
3170 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003171 return;
3172 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003173 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003174 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003178 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003179 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003180 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003181};
3182
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003183static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3184 struct nfs_open_context *ctx,
3185 struct nfs4_lock_state *lsp,
3186 struct nfs_seqid *seqid)
3187{
3188 struct nfs4_unlockdata *data;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003189
3190 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3191 if (data == NULL) {
3192 nfs_free_seqid(seqid);
3193 return ERR_PTR(-ENOMEM);
3194 }
3195
3196 /* Unlock _before_ we do the RPC call */
3197 do_vfs_lock(fl->fl_file, fl);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003198 return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003199}
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3202{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003203 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003204 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003205 struct rpc_task *task;
3206 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003208 /* Is this a delegated lock? */
3209 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003210 goto out_unlock;
3211 /* Is this open_owner holding any locks on the server? */
3212 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
3213 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003214
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003215 status = nfs4_set_lock_state(state, request);
3216 if (status != 0)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003217 goto out_unlock;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003218 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003219 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003220 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3221 if (seqid == NULL)
3222 goto out_unlock;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003223 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3224 status = PTR_ERR(task);
3225 if (IS_ERR(task))
3226 goto out_unlock;
3227 status = nfs4_wait_for_completion_rpc_task(task);
3228 rpc_release_task(task);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003229 return status;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003230out_unlock:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003231 do_vfs_lock(request->fl_file, request);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003232 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003235struct nfs4_lockdata {
3236 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003237 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003238 struct nfs4_lock_state *lsp;
3239 struct nfs_open_context *ctx;
3240 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003241 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003242 int rpc_status;
3243 int cancelled;
3244};
3245
3246static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3247 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3248{
3249 struct nfs4_lockdata *p;
3250 struct inode *inode = lsp->ls_state->inode;
3251 struct nfs_server *server = NFS_SERVER(inode);
3252
3253 p = kzalloc(sizeof(*p), GFP_KERNEL);
3254 if (p == NULL)
3255 return NULL;
3256
3257 p->arg.fh = NFS_FH(inode);
3258 p->arg.fl = &p->fl;
3259 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3260 if (p->arg.lock_seqid == NULL)
3261 goto out_free;
3262 p->arg.lock_stateid = &lsp->ls_stateid;
3263 p->arg.lock_owner.clientid = server->nfs4_state->cl_clientid;
3264 p->arg.lock_owner.id = lsp->ls_id;
3265 p->lsp = lsp;
3266 atomic_inc(&lsp->ls_count);
3267 p->ctx = get_nfs_open_context(ctx);
3268 memcpy(&p->fl, fl, sizeof(p->fl));
3269 return p;
3270out_free:
3271 kfree(p);
3272 return NULL;
3273}
3274
3275static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3276{
3277 struct nfs4_lockdata *data = calldata;
3278 struct nfs4_state *state = data->lsp->ls_state;
3279 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3282 .rpc_argp = &data->arg,
3283 .rpc_resp = &data->res,
3284 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003287 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3288 return;
3289 dprintk("%s: begin!\n", __FUNCTION__);
3290 /* Do we need to do an open_to_lock_owner? */
3291 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3292 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3293 if (data->arg.open_seqid == NULL) {
3294 data->rpc_status = -ENOMEM;
3295 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003296 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003297 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003298 data->arg.open_stateid = &state->stateid;
3299 data->arg.new_lock_owner = 1;
3300 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003301 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003302 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003303out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003304 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3305}
3306
3307static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3308{
3309 struct nfs4_lockdata *data = calldata;
3310
3311 dprintk("%s: begin!\n", __FUNCTION__);
3312
3313 data->rpc_status = task->tk_status;
3314 if (RPC_ASSASSINATED(task))
3315 goto out;
3316 if (data->arg.new_lock_owner != 0) {
3317 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3318 if (data->rpc_status == 0)
3319 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3320 else
3321 goto out;
3322 }
3323 if (data->rpc_status == 0) {
3324 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3325 sizeof(data->lsp->ls_stateid.data));
3326 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003327 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003328 }
3329 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3330out:
3331 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3332}
3333
3334static void nfs4_lock_release(void *calldata)
3335{
3336 struct nfs4_lockdata *data = calldata;
3337
3338 dprintk("%s: begin!\n", __FUNCTION__);
3339 if (data->arg.open_seqid != NULL)
3340 nfs_free_seqid(data->arg.open_seqid);
3341 if (data->cancelled != 0) {
3342 struct rpc_task *task;
3343 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3344 data->arg.lock_seqid);
3345 if (!IS_ERR(task))
3346 rpc_release_task(task);
3347 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3348 } else
3349 nfs_free_seqid(data->arg.lock_seqid);
3350 nfs4_put_lock_state(data->lsp);
3351 put_nfs_open_context(data->ctx);
3352 kfree(data);
3353 dprintk("%s: done!\n", __FUNCTION__);
3354}
3355
3356static const struct rpc_call_ops nfs4_lock_ops = {
3357 .rpc_call_prepare = nfs4_lock_prepare,
3358 .rpc_call_done = nfs4_lock_done,
3359 .rpc_release = nfs4_lock_release,
3360};
3361
3362static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3363{
3364 struct nfs4_lockdata *data;
3365 struct rpc_task *task;
3366 int ret;
3367
3368 dprintk("%s: begin!\n", __FUNCTION__);
3369 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3370 fl->fl_u.nfs4_fl.owner);
3371 if (data == NULL)
3372 return -ENOMEM;
3373 if (IS_SETLKW(cmd))
3374 data->arg.block = 1;
3375 if (reclaim != 0)
3376 data->arg.reclaim = 1;
3377 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3378 &nfs4_lock_ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003379 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003380 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003381 ret = nfs4_wait_for_completion_rpc_task(task);
3382 if (ret == 0) {
3383 ret = data->rpc_status;
3384 if (ret == -NFS4ERR_DENIED)
3385 ret = -EAGAIN;
3386 } else
3387 data->cancelled = 1;
3388 rpc_release_task(task);
3389 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3390 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
3392
3393static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3394{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003395 struct nfs_server *server = NFS_SERVER(state->inode);
3396 struct nfs4_exception exception = { };
3397 int err;
3398
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003399 /* Cache the lock if possible... */
3400 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3401 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003402 do {
3403 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3404 if (err != -NFS4ERR_DELAY)
3405 break;
3406 nfs4_handle_exception(server, err, &exception);
3407 } while (exception.retry);
3408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
3411static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3412{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003413 struct nfs_server *server = NFS_SERVER(state->inode);
3414 struct nfs4_exception exception = { };
3415 int err;
3416
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003417 err = nfs4_set_lock_state(state, request);
3418 if (err != 0)
3419 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003420 do {
3421 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3422 if (err != -NFS4ERR_DELAY)
3423 break;
3424 nfs4_handle_exception(server, err, &exception);
3425 } while (exception.retry);
3426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
3429static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3430{
3431 struct nfs4_client *clp = state->owner->so_client;
3432 int status;
3433
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003434 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003435 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003436 /* Yes: cache locks! */
3437 status = do_vfs_lock(request->fl_file, request);
3438 /* ...but avoid races with delegation recall... */
3439 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003440 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 }
Trond Myklebustff604062005-11-25 17:10:01 -05003442 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003443 status = nfs4_set_lock_state(state, request);
3444 if (status != 0)
3445 goto out;
3446 status = _nfs4_do_setlk(state, cmd, request, 0);
3447 if (status != 0)
3448 goto out;
3449 /* Note: we always want to sleep here! */
3450 request->fl_flags |= FL_SLEEP;
3451 if (do_vfs_lock(request->fl_file, request) < 0)
3452 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3453out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 up_read(&clp->cl_sem);
3455 return status;
3456}
3457
3458static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3459{
3460 struct nfs4_exception exception = { };
3461 int err;
3462
3463 do {
3464 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3465 _nfs4_proc_setlk(state, cmd, request),
3466 &exception);
3467 } while (exception.retry);
3468 return err;
3469}
3470
3471static int
3472nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3473{
3474 struct nfs_open_context *ctx;
3475 struct nfs4_state *state;
3476 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3477 int status;
3478
3479 /* verify open state */
3480 ctx = (struct nfs_open_context *)filp->private_data;
3481 state = ctx->state;
3482
3483 if (request->fl_start < 0 || request->fl_end < 0)
3484 return -EINVAL;
3485
3486 if (IS_GETLK(cmd))
3487 return nfs4_proc_getlk(state, F_GETLK, request);
3488
3489 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3490 return -EINVAL;
3491
3492 if (request->fl_type == F_UNLCK)
3493 return nfs4_proc_unlck(state, cmd, request);
3494
3495 do {
3496 status = nfs4_proc_setlk(state, cmd, request);
3497 if ((status != -EAGAIN) || IS_SETLK(cmd))
3498 break;
3499 timeout = nfs4_set_lock_task_retry(timeout);
3500 status = -ERESTARTSYS;
3501 if (signalled())
3502 break;
3503 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 return status;
3505}
3506
Trond Myklebust888e6942005-11-04 15:38:11 -05003507int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3508{
3509 struct nfs_server *server = NFS_SERVER(state->inode);
3510 struct nfs4_exception exception = { };
3511 int err;
3512
3513 err = nfs4_set_lock_state(state, fl);
3514 if (err != 0)
3515 goto out;
3516 do {
3517 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3518 if (err != -NFS4ERR_DELAY)
3519 break;
3520 err = nfs4_handle_exception(server, err, &exception);
3521 } while (exception.retry);
3522out:
3523 return err;
3524}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003525
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003526#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3527
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003528int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3529 size_t buflen, int flags)
3530{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003531 struct inode *inode = dentry->d_inode;
3532
3533 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3534 return -EOPNOTSUPP;
3535
3536 if (!S_ISREG(inode->i_mode) &&
3537 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3538 return -EPERM;
3539
3540 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003541}
3542
3543/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3544 * and that's what we'll do for e.g. user attributes that haven't been set.
3545 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3546 * attributes in kernel-managed attribute namespaces. */
3547ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3548 size_t buflen)
3549{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003550 struct inode *inode = dentry->d_inode;
3551
3552 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3553 return -EOPNOTSUPP;
3554
3555 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003556}
3557
3558ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3559{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003560 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003561
J. Bruce Fields096455a2006-03-20 23:23:42 -05003562 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3563 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003564 if (buf && buflen < len)
3565 return -ERANGE;
3566 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003567 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3568 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003569}
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3572 .recover_open = nfs4_open_reclaim,
3573 .recover_lock = nfs4_lock_reclaim,
3574};
3575
3576struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3577 .recover_open = nfs4_open_expired,
3578 .recover_lock = nfs4_lock_expired,
3579};
3580
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003581static struct inode_operations nfs4_file_inode_operations = {
3582 .permission = nfs_permission,
3583 .getattr = nfs_getattr,
3584 .setattr = nfs_setattr,
3585 .getxattr = nfs4_getxattr,
3586 .setxattr = nfs4_setxattr,
3587 .listxattr = nfs4_listxattr,
3588};
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590struct nfs_rpc_ops nfs_v4_clientops = {
3591 .version = 4, /* protocol version */
3592 .dentry_ops = &nfs4_dentry_operations,
3593 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003594 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 .getroot = nfs4_proc_get_root,
3596 .getattr = nfs4_proc_getattr,
3597 .setattr = nfs4_proc_setattr,
3598 .lookup = nfs4_proc_lookup,
3599 .access = nfs4_proc_access,
3600 .readlink = nfs4_proc_readlink,
3601 .read = nfs4_proc_read,
3602 .write = nfs4_proc_write,
3603 .commit = nfs4_proc_commit,
3604 .create = nfs4_proc_create,
3605 .remove = nfs4_proc_remove,
3606 .unlink_setup = nfs4_proc_unlink_setup,
3607 .unlink_done = nfs4_proc_unlink_done,
3608 .rename = nfs4_proc_rename,
3609 .link = nfs4_proc_link,
3610 .symlink = nfs4_proc_symlink,
3611 .mkdir = nfs4_proc_mkdir,
3612 .rmdir = nfs4_proc_remove,
3613 .readdir = nfs4_proc_readdir,
3614 .mknod = nfs4_proc_mknod,
3615 .statfs = nfs4_proc_statfs,
3616 .fsinfo = nfs4_proc_fsinfo,
3617 .pathconf = nfs4_proc_pathconf,
3618 .decode_dirent = nfs4_decode_dirent,
3619 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003620 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003622 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003624 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003625 .file_open = nfs_open,
3626 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003628 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629};
3630
3631/*
3632 * Local variables:
3633 * c-basic-offset: 8
3634 * End:
3635 */