blob: 76d7d0259b6c19a3e7f14c40395c417d45379e7f [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"
54
55#define NFSDBG_FACILITY NFSDBG_PROC
56
57#define NFS4_POLL_RETRY_MIN (1*HZ)
58#define NFS4_POLL_RETRY_MAX (15*HZ)
59
Trond Myklebustcdd4e682006-01-03 09:55:12 +010060struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010061static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070063static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070065static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
67extern struct rpc_procinfo nfs4_procedures[];
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Prevent leaks of NFSv4 errors into userland */
70int nfs4_map_errors(int err)
71{
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
74 __FUNCTION__, -err);
75 return -EIO;
76 }
77 return err;
78}
79
80/*
81 * This is our standard bitmap for GETATTR requests.
82 */
83const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98};
99
100const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107};
108
Trond Myklebust4ce79712005-06-22 17:16:21 +0000109const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120};
121
122static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
123 struct nfs4_readdir_arg *readdir)
124{
125 u32 *start, *p;
126
127 BUG_ON(readdir->count < 80);
128 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000129 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
131 return;
132 }
133
134 readdir->cookie = 0;
135 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
136 if (cookie == 2)
137 return;
138
139 /*
140 * NFSv4 servers do not return entries for '.' and '..'
141 * Therefore, we fake these entries here. We let '.'
142 * have cookie 0 and '..' have cookie 1. Note that
143 * when talking to the server, we always send cookie 0
144 * instead of 1 or 2.
145 */
146 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
147
148 if (cookie == 0) {
149 *p++ = xdr_one; /* next */
150 *p++ = xdr_zero; /* cookie, first word */
151 *p++ = xdr_one; /* cookie, second word */
152 *p++ = xdr_one; /* entry len */
153 memcpy(p, ".\0\0\0", 4); /* entry */
154 p++;
155 *p++ = xdr_one; /* bitmap length */
156 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
157 *p++ = htonl(8); /* attribute buffer length */
158 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
159 }
160
161 *p++ = xdr_one; /* next */
162 *p++ = xdr_zero; /* cookie, first word */
163 *p++ = xdr_two; /* cookie, second word */
164 *p++ = xdr_two; /* entry len */
165 memcpy(p, "..\0\0", 4); /* entry */
166 p++;
167 *p++ = xdr_one; /* bitmap length */
168 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
169 *p++ = htonl(8); /* attribute buffer length */
170 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
171
172 readdir->pgbase = (char *)p - (char *)start;
173 readdir->count -= readdir->pgbase;
174 kunmap_atomic(start, KM_USER0);
175}
176
Trond Myklebust26e976a2006-01-03 09:55:21 +0100177static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct nfs4_client *clp = server->nfs4_state;
180 spin_lock(&clp->cl_lock);
181 if (time_before(clp->cl_last_renewal,timestamp))
182 clp->cl_last_renewal = timestamp;
183 spin_unlock(&clp->cl_lock);
184}
185
186static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
187{
188 struct nfs_inode *nfsi = NFS_I(inode);
189
Trond Myklebustdecf4912005-10-27 22:12:39 -0400190 spin_lock(&inode->i_lock);
191 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400194 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100197struct nfs4_opendata {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100198 atomic_t count;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100199 struct nfs_openargs o_arg;
200 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100201 struct nfs_open_confirmargs c_arg;
202 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100203 struct nfs_fattr f_attr;
204 struct nfs_fattr dir_attr;
205 struct dentry *dentry;
206 struct dentry *dir;
207 struct nfs4_state_owner *owner;
208 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100209 unsigned long timestamp;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100210 int rpc_status;
211 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100212};
213
214static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
215 struct nfs4_state_owner *sp, int flags,
216 const struct iattr *attrs)
217{
218 struct dentry *parent = dget_parent(dentry);
219 struct inode *dir = parent->d_inode;
220 struct nfs_server *server = NFS_SERVER(dir);
221 struct nfs4_opendata *p;
222
223 p = kzalloc(sizeof(*p), GFP_KERNEL);
224 if (p == NULL)
225 goto err;
226 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
227 if (p->o_arg.seqid == NULL)
228 goto err_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100229 atomic_set(&p->count, 1);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100230 p->dentry = dget(dentry);
231 p->dir = parent;
232 p->owner = sp;
233 atomic_inc(&sp->so_count);
234 p->o_arg.fh = NFS_FH(dir);
235 p->o_arg.open_flags = flags,
236 p->o_arg.clientid = server->nfs4_state->cl_clientid;
237 p->o_arg.id = sp->so_id;
238 p->o_arg.name = &dentry->d_name;
239 p->o_arg.server = server;
240 p->o_arg.bitmask = server->attr_bitmask;
241 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
242 p->o_res.f_attr = &p->f_attr;
243 p->o_res.dir_attr = &p->dir_attr;
244 p->o_res.server = server;
245 nfs_fattr_init(&p->f_attr);
246 nfs_fattr_init(&p->dir_attr);
247 if (flags & O_EXCL) {
248 u32 *s = (u32 *) p->o_arg.u.verifier.data;
249 s[0] = jiffies;
250 s[1] = current->pid;
251 } else if (flags & O_CREAT) {
252 p->o_arg.u.attrs = &p->attrs;
253 memcpy(&p->attrs, attrs, sizeof(p->attrs));
254 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100255 p->c_arg.fh = &p->o_res.fh;
256 p->c_arg.stateid = &p->o_res.stateid;
257 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100258 return p;
259err_free:
260 kfree(p);
261err:
262 dput(parent);
263 return NULL;
264}
265
266static void nfs4_opendata_free(struct nfs4_opendata *p)
267{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100268 if (p != NULL && atomic_dec_and_test(&p->count)) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100269 nfs_free_seqid(p->o_arg.seqid);
270 nfs4_put_state_owner(p->owner);
271 dput(p->dir);
272 dput(p->dentry);
273 kfree(p);
274 }
275}
276
Trond Myklebust95121352005-10-18 14:20:12 -0700277/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100278static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100279 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700280{
281 struct rpc_task *task;
282
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100283 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700284 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700285 rpc_execute(task);
286 return 0;
287}
288
Trond Myklebust06f814a2006-01-03 09:55:07 +0100289static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
290{
291 sigset_t oldset;
292 int ret;
293
294 rpc_clnt_sigmask(task->tk_client, &oldset);
295 ret = rpc_wait_for_completion_task(task);
296 rpc_clnt_sigunmask(task->tk_client, &oldset);
297 return ret;
298}
299
Trond Myklebuste7616922006-01-03 09:55:13 +0100300static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
301{
302 switch (open_flags) {
303 case FMODE_WRITE:
304 state->n_wronly++;
305 break;
306 case FMODE_READ:
307 state->n_rdonly++;
308 break;
309 case FMODE_READ|FMODE_WRITE:
310 state->n_rdwr++;
311 }
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
315{
316 struct inode *inode = state->inode;
317
318 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500319 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700320 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500322 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100323 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500324 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700326 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100329static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
330{
331 struct inode *inode;
332 struct nfs4_state *state = NULL;
333
334 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
335 goto out;
336 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
337 if (inode == NULL)
338 goto out;
339 state = nfs4_get_open_state(inode, data->owner);
340 if (state == NULL)
341 goto put_inode;
342 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
343put_inode:
344 iput(inode);
345out:
346 return state;
347}
348
Trond Myklebust864472e2006-01-03 09:55:15 +0100349static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
350{
351 struct nfs_inode *nfsi = NFS_I(state->inode);
352 struct nfs_open_context *ctx;
353
354 spin_lock(&state->inode->i_lock);
355 list_for_each_entry(ctx, &nfsi->open_files, list) {
356 if (ctx->state != state)
357 continue;
358 get_nfs_open_context(ctx);
359 spin_unlock(&state->inode->i_lock);
360 return ctx;
361 }
362 spin_unlock(&state->inode->i_lock);
363 return ERR_PTR(-ENOENT);
364}
365
366static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
367{
368 int ret;
369
370 opendata->o_arg.open_flags = openflags;
371 ret = _nfs4_proc_open(opendata);
372 if (ret != 0)
373 return ret;
374 memcpy(stateid->data, opendata->o_res.stateid.data,
375 sizeof(stateid->data));
376 return 0;
377}
378
379static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
380{
381 nfs4_stateid stateid;
382 struct nfs4_state *newstate;
383 int mode = 0;
384 int delegation = 0;
385 int ret;
386
387 /* memory barrier prior to reading state->n_* */
388 smp_rmb();
389 if (state->n_rdwr != 0) {
390 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
391 if (ret != 0)
392 return ret;
393 mode |= FMODE_READ|FMODE_WRITE;
394 if (opendata->o_res.delegation_type != 0)
395 delegation = opendata->o_res.delegation_type;
396 smp_rmb();
397 }
398 if (state->n_wronly != 0) {
399 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
400 if (ret != 0)
401 return ret;
402 mode |= FMODE_WRITE;
403 if (opendata->o_res.delegation_type != 0)
404 delegation = opendata->o_res.delegation_type;
405 smp_rmb();
406 }
407 if (state->n_rdonly != 0) {
408 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
409 if (ret != 0)
410 return ret;
411 mode |= FMODE_READ;
412 }
413 clear_bit(NFS_DELEGATED_STATE, &state->flags);
414 if (mode == 0)
415 return 0;
416 if (opendata->o_res.delegation_type == 0)
417 opendata->o_res.delegation_type = delegation;
418 opendata->o_arg.open_flags |= mode;
419 newstate = nfs4_opendata_to_nfs4_state(opendata);
420 if (newstate != NULL) {
421 if (opendata->o_res.delegation_type != 0) {
422 struct nfs_inode *nfsi = NFS_I(newstate->inode);
423 int delegation_flags = 0;
424 if (nfsi->delegation)
425 delegation_flags = nfsi->delegation->flags;
426 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
427 nfs_inode_set_delegation(newstate->inode,
428 opendata->owner->so_cred,
429 &opendata->o_res);
430 else
431 nfs_inode_reclaim_delegation(newstate->inode,
432 opendata->owner->so_cred,
433 &opendata->o_res);
434 }
435 nfs4_close_state(newstate, opendata->o_arg.open_flags);
436 }
437 if (newstate != state)
438 return -ESTALE;
439 return 0;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*
443 * OPEN_RECLAIM:
444 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Trond Myklebust864472e2006-01-03 09:55:15 +0100446static int _nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Trond Myklebust864472e2006-01-03 09:55:15 +0100448 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
449 struct nfs4_opendata *opendata;
450 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int status;
452
453 if (delegation != NULL) {
454 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
455 memcpy(&state->stateid, &delegation->stateid,
456 sizeof(state->stateid));
457 set_bit(NFS_DELEGATED_STATE, &state->flags);
458 return 0;
459 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100460 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100462 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
463 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700464 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100465 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
466 opendata->o_arg.fh = NFS_FH(state->inode);
467 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
468 opendata->o_arg.u.delegation_type = delegation_type;
469 status = nfs4_open_recover(opendata, state);
470 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return status;
472}
473
Trond Myklebust864472e2006-01-03 09:55:15 +0100474static int nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct nfs_server *server = NFS_SERVER(state->inode);
477 struct nfs4_exception exception = { };
478 int err;
479 do {
Trond Myklebust864472e2006-01-03 09:55:15 +0100480 err = _nfs4_do_open_reclaim(sp, state, dentry);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000481 if (err != -NFS4ERR_DELAY)
482 break;
483 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } while (exception.retry);
485 return err;
486}
487
Trond Myklebust864472e2006-01-03 09:55:15 +0100488static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
489{
490 struct nfs_open_context *ctx;
491 int ret;
492
493 ctx = nfs4_state_find_open_context(state);
494 if (IS_ERR(ctx))
495 return PTR_ERR(ctx);
496 ret = nfs4_do_open_reclaim(sp, state, ctx->dentry);
497 put_nfs_open_context(ctx);
498 return ret;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
502{
503 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100504 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100505 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust864472e2006-01-03 09:55:15 +0100508 return 0;
509 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100510 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100511 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100512 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100513 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
514 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100515 ret = nfs4_open_recover(opendata, state);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100516 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
521{
522 struct nfs4_exception exception = { };
523 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
524 int err;
525 do {
526 err = _nfs4_open_delegation_recall(dentry, state);
527 switch (err) {
528 case 0:
529 return err;
530 case -NFS4ERR_STALE_CLIENTID:
531 case -NFS4ERR_STALE_STATEID:
532 case -NFS4ERR_EXPIRED:
533 /* Don't recall a delegation if it was lost */
534 nfs4_schedule_state_recovery(server->nfs4_state);
535 return err;
536 }
537 err = nfs4_handle_exception(server, err, &exception);
538 } while (exception.retry);
539 return err;
540}
541
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100542static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100544 struct nfs4_opendata *data = calldata;
545 struct rpc_message msg = {
546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
547 .rpc_argp = &data->c_arg,
548 .rpc_resp = &data->c_res,
549 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100551 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100552 rpc_call_setup(task, &msg, 0);
553}
554
555static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
556{
557 struct nfs4_opendata *data = calldata;
558
559 data->rpc_status = task->tk_status;
560 if (RPC_ASSASSINATED(task))
561 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100562 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100563 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
564 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100565 renew_lease(data->o_res.server, data->timestamp);
566 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100567 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
568 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
569}
570
571static void nfs4_open_confirm_release(void *calldata)
572{
573 struct nfs4_opendata *data = calldata;
574 struct nfs4_state *state = NULL;
575
576 /* If this request hasn't been cancelled, do nothing */
577 if (data->cancelled == 0)
578 goto out_free;
579 /* In case of error, no cleanup! */
580 if (data->rpc_status != 0)
581 goto out_free;
582 nfs_confirm_seqid(&data->owner->so_seqid, 0);
583 state = nfs4_opendata_to_nfs4_state(data);
584 if (state != NULL)
585 nfs4_close_state(state, data->o_arg.open_flags);
586out_free:
587 nfs4_opendata_free(data);
588}
589
590static const struct rpc_call_ops nfs4_open_confirm_ops = {
591 .rpc_call_prepare = nfs4_open_confirm_prepare,
592 .rpc_call_done = nfs4_open_confirm_done,
593 .rpc_release = nfs4_open_confirm_release,
594};
595
596/*
597 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
598 */
599static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
600{
601 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
602 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int status;
604
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100605 atomic_inc(&data->count);
606 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
607 if (IS_ERR(task)) {
608 nfs4_opendata_free(data);
609 return PTR_ERR(task);
610 }
611 status = nfs4_wait_for_completion_rpc_task(task);
612 if (status != 0) {
613 data->cancelled = 1;
614 smp_wmb();
615 } else
616 status = data->rpc_status;
617 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return status;
619}
620
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100621static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100623 struct nfs4_opendata *data = calldata;
624 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct rpc_message msg = {
626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100627 .rpc_argp = &data->o_arg,
628 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 .rpc_cred = sp->so_cred,
630 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100631
632 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
633 return;
634 /* Update sequence id. */
635 data->o_arg.id = sp->so_id;
636 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100637 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
638 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100639 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100640 rpc_call_setup(task, &msg, 0);
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100643static void nfs4_open_done(struct rpc_task *task, void *calldata)
644{
645 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100647 data->rpc_status = task->tk_status;
648 if (RPC_ASSASSINATED(task))
649 return;
650 if (task->tk_status == 0) {
651 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700652 case S_IFREG:
653 break;
654 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100655 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700656 break;
657 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100658 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700659 break;
660 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100661 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700662 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100663 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700664 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100665 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
666}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700667
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100668static void nfs4_open_release(void *calldata)
669{
670 struct nfs4_opendata *data = calldata;
671 struct nfs4_state *state = NULL;
672
673 /* If this request hasn't been cancelled, do nothing */
674 if (data->cancelled == 0)
675 goto out_free;
676 /* In case of error, no cleanup! */
677 if (data->rpc_status != 0)
678 goto out_free;
679 /* In case we need an open_confirm, no cleanup! */
680 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
681 goto out_free;
682 nfs_confirm_seqid(&data->owner->so_seqid, 0);
683 state = nfs4_opendata_to_nfs4_state(data);
684 if (state != NULL)
685 nfs4_close_state(state, data->o_arg.open_flags);
686out_free:
687 nfs4_opendata_free(data);
688}
689
690static const struct rpc_call_ops nfs4_open_ops = {
691 .rpc_call_prepare = nfs4_open_prepare,
692 .rpc_call_done = nfs4_open_done,
693 .rpc_release = nfs4_open_release,
694};
695
696/*
697 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
698 */
699static int _nfs4_proc_open(struct nfs4_opendata *data)
700{
701 struct inode *dir = data->dir->d_inode;
702 struct nfs_server *server = NFS_SERVER(dir);
703 struct nfs_openargs *o_arg = &data->o_arg;
704 struct nfs_openres *o_res = &data->o_res;
705 struct rpc_task *task;
706 int status;
707
708 atomic_inc(&data->count);
709 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
710 if (IS_ERR(task)) {
711 nfs4_opendata_free(data);
712 return PTR_ERR(task);
713 }
714 status = nfs4_wait_for_completion_rpc_task(task);
715 if (status != 0) {
716 data->cancelled = 1;
717 smp_wmb();
718 } else
719 status = data->rpc_status;
720 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100722 return status;
723
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400724 if (o_arg->open_flags & O_CREAT) {
725 update_changeattr(dir, &o_res->cinfo);
726 nfs_post_op_update_inode(dir, o_res->dir_attr);
727 } else
728 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100730 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100732 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100734 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100736 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
741{
742 struct nfs_access_entry cache;
743 int mask = 0;
744 int status;
745
746 if (openflags & FMODE_READ)
747 mask |= MAY_READ;
748 if (openflags & FMODE_WRITE)
749 mask |= MAY_WRITE;
750 status = nfs_access_get_cached(inode, cred, &cache);
751 if (status == 0)
752 goto out;
753
754 /* Be clever: ask server to check for all possible rights */
755 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
756 cache.cred = cred;
757 cache.jiffies = jiffies;
758 status = _nfs4_proc_access(inode, &cache);
759 if (status != 0)
760 return status;
761 nfs_access_add_cache(inode, &cache);
762out:
763 if ((cache.mask & mask) == mask)
764 return 0;
765 return -EACCES;
766}
767
768/*
769 * OPEN_EXPIRED:
770 * reclaim state on the server after a network partition.
771 * Assumes caller holds the appropriate lock
772 */
773static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100778 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100779 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100782 ret = _nfs4_do_access(inode, sp->so_cred, openflags);
783 if (ret < 0)
784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
786 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100789 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100791 return -ENOMEM;
792 ret = nfs4_open_recover(opendata, state);
793 if (ret == -ESTALE) {
794 /* Invalidate the state owner so we don't ever use it again */
795 nfs4_drop_state_owner(sp);
796 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100799 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Trond Myklebust202b50d2005-06-22 17:16:29 +0000802static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
803{
804 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
805 struct nfs4_exception exception = { };
806 int err;
807
808 do {
809 err = _nfs4_open_expired(sp, state, dentry);
810 if (err == -NFS4ERR_DELAY)
811 nfs4_handle_exception(server, err, &exception);
812 } while (exception.retry);
813 return err;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100819 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Trond Myklebust864472e2006-01-03 09:55:15 +0100821 ctx = nfs4_state_find_open_context(state);
822 if (IS_ERR(ctx))
823 return PTR_ERR(ctx);
824 ret = nfs4_do_open_expired(sp, state, ctx->dentry);
825 put_nfs_open_context(ctx);
826 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100830 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
832static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
833{
834 struct nfs_delegation *delegation;
835 struct nfs_server *server = NFS_SERVER(inode);
836 struct nfs4_client *clp = server->nfs4_state;
837 struct nfs_inode *nfsi = NFS_I(inode);
838 struct nfs4_state_owner *sp = NULL;
839 struct nfs4_state *state = NULL;
840 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
841 int err;
842
843 /* Protect against reboot recovery - NOTE ORDER! */
844 down_read(&clp->cl_sem);
845 /* Protect against delegation recall */
846 down_read(&nfsi->rwsem);
847 delegation = NFS_I(inode)->delegation;
848 err = -ENOENT;
849 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
850 goto out_err;
851 err = -ENOMEM;
852 if (!(sp = nfs4_get_state_owner(server, cred))) {
853 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
854 goto out_err;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 state = nfs4_get_open_state(inode, sp);
857 if (state == NULL)
858 goto out_err;
859
860 err = -ENOENT;
861 if ((state->state & open_flags) == open_flags) {
862 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100863 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_unlock(&inode->i_lock);
865 goto out_ok;
866 } else if (state->state != 0)
867 goto out_err;
868
869 lock_kernel();
870 err = _nfs4_do_access(inode, cred, open_flags);
871 unlock_kernel();
872 if (err != 0)
873 goto out_err;
874 set_bit(NFS_DELEGATED_STATE, &state->flags);
875 update_open_stateid(state, &delegation->stateid, open_flags);
876out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 nfs4_put_state_owner(sp);
878 up_read(&nfsi->rwsem);
879 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *res = state;
881 return 0;
882out_err:
883 if (sp != NULL) {
884 if (state != NULL)
885 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 nfs4_put_state_owner(sp);
887 }
888 up_read(&nfsi->rwsem);
889 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700890 if (err != -EACCES)
891 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return err;
893}
894
895static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
896{
897 struct nfs4_exception exception = { };
898 struct nfs4_state *res;
899 int err;
900
901 do {
902 err = _nfs4_open_delegated(inode, flags, cred, &res);
903 if (err == 0)
904 break;
905 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
906 err, &exception));
907 } while (exception.retry);
908 return res;
909}
910
911/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100912 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
915{
916 struct nfs4_state_owner *sp;
917 struct nfs4_state *state = NULL;
918 struct nfs_server *server = NFS_SERVER(dir);
919 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100920 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Protect against reboot recovery conflicts */
924 down_read(&clp->cl_sem);
925 status = -ENOMEM;
926 if (!(sp = nfs4_get_state_owner(server, cred))) {
927 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
928 goto out_err;
929 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100930 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
931 if (opendata == NULL)
932 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100934 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (status != 0)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100936 goto err_opendata_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100939 state = nfs4_opendata_to_nfs4_state(opendata);
940 if (state == NULL)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100941 goto err_opendata_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100943 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100944 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 nfs4_put_state_owner(sp);
946 up_read(&clp->cl_sem);
947 *res = state;
948 return 0;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949err_opendata_free:
950 nfs4_opendata_free(opendata);
951err_put_state_owner:
952 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
955 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *res = NULL;
957 return status;
958}
959
960
961static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
962{
963 struct nfs4_exception exception = { };
964 struct nfs4_state *res;
965 int status;
966
967 do {
968 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
969 if (status == 0)
970 break;
971 /* NOTE: BAD_SEQID means the server and client disagree about the
972 * book-keeping w.r.t. state-changing operations
973 * (OPEN/CLOSE/LOCK/LOCKU...)
974 * It is actually a sign of a bug on the client or on the server.
975 *
976 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700977 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * have unhashed the old state_owner for us, and that we can
979 * therefore safely retry using a new one. We should still warn
980 * the user though...
981 */
982 if (status == -NFS4ERR_BAD_SEQID) {
983 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
984 exception.retry = 1;
985 continue;
986 }
Trond Myklebust550f5742005-10-18 14:20:21 -0700987 /*
988 * BAD_STATEID on OPEN means that the server cancelled our
989 * state before it received the OPEN_CONFIRM.
990 * Recover by retrying the request as per the discussion
991 * on Page 181 of RFC3530.
992 */
993 if (status == -NFS4ERR_BAD_STATEID) {
994 exception.retry = 1;
995 continue;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
998 status, &exception));
999 } while (exception.retry);
1000 return res;
1001}
1002
1003static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1004 struct nfs_fh *fhandle, struct iattr *sattr,
1005 struct nfs4_state *state)
1006{
1007 struct nfs_setattrargs arg = {
1008 .fh = fhandle,
1009 .iap = sattr,
1010 .server = server,
1011 .bitmask = server->attr_bitmask,
1012 };
1013 struct nfs_setattrres res = {
1014 .fattr = fattr,
1015 .server = server,
1016 };
1017 struct rpc_message msg = {
1018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1019 .rpc_argp = &arg,
1020 .rpc_resp = &res,
1021 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001022 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001023 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Trond Myklebust0e574af2005-10-27 22:12:38 -04001025 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001027 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001029 nfs4_copy_stateid(&arg.stateid, state, current->files);
1030 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1032
Trond Myklebust65e43082005-08-16 11:49:44 -04001033 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001034 if (status == 0 && state != NULL)
1035 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001036 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1040 struct nfs_fh *fhandle, struct iattr *sattr,
1041 struct nfs4_state *state)
1042{
1043 struct nfs4_exception exception = { };
1044 int err;
1045 do {
1046 err = nfs4_handle_exception(server,
1047 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1048 state),
1049 &exception);
1050 } while (exception.retry);
1051 return err;
1052}
1053
1054struct nfs4_closedata {
1055 struct inode *inode;
1056 struct nfs4_state *state;
1057 struct nfs_closeargs arg;
1058 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001059 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001060 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061};
1062
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001063static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001064{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001065 struct nfs4_closedata *calldata = data;
1066 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001067
1068 nfs4_put_open_state(calldata->state);
1069 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001070 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001071 kfree(calldata);
1072}
1073
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001074static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001076 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct nfs_server *server = NFS_SERVER(calldata->inode);
1079
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001080 if (RPC_ASSASSINATED(task))
1081 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* hmm. we are done with the inode, and in the process of freeing
1083 * the state_owner. we keep this around to process errors
1084 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001085 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 switch (task->tk_status) {
1087 case 0:
1088 memcpy(&state->stateid, &calldata->res.stateid,
1089 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001090 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092 case -NFS4ERR_STALE_STATEID:
1093 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 nfs4_schedule_state_recovery(server->nfs4_state);
1095 break;
1096 default:
1097 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1098 rpc_restart_call(task);
1099 return;
1100 }
1101 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001102 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001105static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001107 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001108 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct rpc_message msg = {
1110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1111 .rpc_argp = &calldata->arg,
1112 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001113 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001115 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001116
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001117 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001118 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001119 /* Recalculate the new open mode in case someone reopened the file
1120 * while we were waiting in line to be scheduled.
1121 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001122 spin_lock(&state->owner->so_lock);
1123 spin_lock(&calldata->inode->i_lock);
1124 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001125 if (state->n_rdwr == 0) {
1126 if (state->n_rdonly == 0)
1127 mode &= ~FMODE_READ;
1128 if (state->n_wronly == 0)
1129 mode &= ~FMODE_WRITE;
1130 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001131 nfs4_state_set_mode_locked(state, mode);
1132 spin_unlock(&calldata->inode->i_lock);
1133 spin_unlock(&state->owner->so_lock);
1134 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001135 /* Note: exit _without_ calling nfs4_close_done */
1136 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001137 return;
1138 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001139 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001140 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001142 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001143 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001144 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001147static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001148 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001149 .rpc_call_done = nfs4_close_done,
1150 .rpc_release = nfs4_free_closedata,
1151};
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
1154 * It is possible for data to be read/written from a mem-mapped file
1155 * after the sys_close call (which hits the vfs layer as a flush).
1156 * This means that we can't safely call nfsv4 close on a file until
1157 * the inode is cleared. This in turn means that we are not good
1158 * NFSv4 citizens - we do not indicate to the server to update the file's
1159 * share state even when we are done with one of the three share
1160 * stateid's in the inode.
1161 *
1162 * NOTE: Caller must be holding the sp->so_owner semaphore!
1163 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001164int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Trond Myklebust516a6af2005-10-27 22:12:41 -04001166 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -07001168 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Trond Myklebust95121352005-10-18 14:20:12 -07001170 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 calldata->inode = inode;
1174 calldata->state = state;
1175 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001176 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001178 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001179 if (calldata->arg.seqid == NULL)
1180 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001181 calldata->arg.bitmask = server->attr_bitmask;
1182 calldata->res.fattr = &calldata->fattr;
1183 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -07001184
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001185 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -07001186 if (status == 0)
1187 goto out;
1188
1189 nfs_free_seqid(calldata->arg.seqid);
1190out_free_calldata:
1191 kfree(calldata);
1192out:
1193 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Trond Myklebust02a913a2005-10-18 14:20:17 -07001196static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1197{
1198 struct file *filp;
1199
1200 filp = lookup_instantiate_filp(nd, dentry, NULL);
1201 if (!IS_ERR(filp)) {
1202 struct nfs_open_context *ctx;
1203 ctx = (struct nfs_open_context *)filp->private_data;
1204 ctx->state = state;
1205 } else
1206 nfs4_close_state(state, nd->intent.open.flags);
1207}
1208
1209struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1211{
1212 struct iattr attr;
1213 struct rpc_cred *cred;
1214 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001215 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (nd->flags & LOOKUP_CREATE) {
1218 attr.ia_mode = nd->intent.open.create_mode;
1219 attr.ia_valid = ATTR_MODE;
1220 if (!IS_POSIXACL(dir))
1221 attr.ia_mode &= ~current->fs->umask;
1222 } else {
1223 attr.ia_valid = 0;
1224 BUG_ON(nd->intent.open.flags & O_CREAT);
1225 }
1226
1227 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1228 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001229 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1231 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001232 if (IS_ERR(state)) {
1233 if (PTR_ERR(state) == -ENOENT)
1234 d_add(dentry, NULL);
1235 return (struct dentry *)state;
1236 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001237 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001238 if (res != NULL)
1239 dentry = res;
1240 nfs4_intent_set_file(nd, dentry, state);
1241 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001245nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 struct rpc_cred *cred;
1248 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1251 if (IS_ERR(cred))
1252 return PTR_ERR(cred);
1253 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1254 if (IS_ERR(state))
1255 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1256 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001257 if (IS_ERR(state)) {
1258 switch (PTR_ERR(state)) {
1259 case -EPERM:
1260 case -EACCES:
1261 case -EDQUOT:
1262 case -ENOSPC:
1263 case -EROFS:
1264 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1265 return 1;
1266 case -ENOENT:
1267 if (dentry->d_inode == NULL)
1268 return 1;
1269 }
1270 goto out_drop;
1271 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001272 if (state->inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001273 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 1;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001277out_drop:
1278 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return 0;
1280}
1281
1282
1283static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1284{
1285 struct nfs4_server_caps_res res = {};
1286 struct rpc_message msg = {
1287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1288 .rpc_argp = fhandle,
1289 .rpc_resp = &res,
1290 };
1291 int status;
1292
1293 status = rpc_call_sync(server->client, &msg, 0);
1294 if (status == 0) {
1295 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1296 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1297 server->caps |= NFS_CAP_ACLS;
1298 if (res.has_links != 0)
1299 server->caps |= NFS_CAP_HARDLINKS;
1300 if (res.has_symlinks != 0)
1301 server->caps |= NFS_CAP_SYMLINKS;
1302 server->acl_bitmask = res.acl_bitmask;
1303 }
1304 return status;
1305}
1306
1307static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1308{
1309 struct nfs4_exception exception = { };
1310 int err;
1311 do {
1312 err = nfs4_handle_exception(server,
1313 _nfs4_server_capabilities(server, fhandle),
1314 &exception);
1315 } while (exception.retry);
1316 return err;
1317}
1318
1319static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1320 struct nfs_fsinfo *info)
1321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct nfs4_lookup_root_arg args = {
1323 .bitmask = nfs4_fattr_bitmap,
1324 };
1325 struct nfs4_lookup_res res = {
1326 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001327 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 .fh = fhandle,
1329 };
1330 struct rpc_message msg = {
1331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1332 .rpc_argp = &args,
1333 .rpc_resp = &res,
1334 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001335 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return rpc_call_sync(server->client, &msg, 0);
1337}
1338
1339static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1340 struct nfs_fsinfo *info)
1341{
1342 struct nfs4_exception exception = { };
1343 int err;
1344 do {
1345 err = nfs4_handle_exception(server,
1346 _nfs4_lookup_root(server, fhandle, info),
1347 &exception);
1348 } while (exception.retry);
1349 return err;
1350}
1351
1352static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1353 struct nfs_fsinfo *info)
1354{
1355 struct nfs_fattr * fattr = info->fattr;
1356 unsigned char * p;
1357 struct qstr q;
1358 struct nfs4_lookup_arg args = {
1359 .dir_fh = fhandle,
1360 .name = &q,
1361 .bitmask = nfs4_fattr_bitmap,
1362 };
1363 struct nfs4_lookup_res res = {
1364 .server = server,
1365 .fattr = fattr,
1366 .fh = fhandle,
1367 };
1368 struct rpc_message msg = {
1369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1370 .rpc_argp = &args,
1371 .rpc_resp = &res,
1372 };
1373 int status;
1374
1375 /*
1376 * Now we do a separate LOOKUP for each component of the mount path.
1377 * The LOOKUPs are done separately so that we can conveniently
1378 * catch an ERR_WRONGSEC if it occurs along the way...
1379 */
1380 status = nfs4_lookup_root(server, fhandle, info);
1381 if (status)
1382 goto out;
1383
1384 p = server->mnt_path;
1385 for (;;) {
1386 struct nfs4_exception exception = { };
1387
1388 while (*p == '/')
1389 p++;
1390 if (!*p)
1391 break;
1392 q.name = p;
1393 while (*p && (*p != '/'))
1394 p++;
1395 q.len = p - q.name;
1396
1397 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001398 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 status = nfs4_handle_exception(server,
1400 rpc_call_sync(server->client, &msg, 0),
1401 &exception);
1402 } while (exception.retry);
1403 if (status == 0)
1404 continue;
1405 if (status == -ENOENT) {
1406 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1407 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1408 }
1409 break;
1410 }
1411 if (status == 0)
1412 status = nfs4_server_capabilities(server, fhandle);
1413 if (status == 0)
1414 status = nfs4_do_fsinfo(server, fhandle, info);
1415out:
1416 return status;
1417}
1418
1419static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1420{
1421 struct nfs4_getattr_arg args = {
1422 .fh = fhandle,
1423 .bitmask = server->attr_bitmask,
1424 };
1425 struct nfs4_getattr_res res = {
1426 .fattr = fattr,
1427 .server = server,
1428 };
1429 struct rpc_message msg = {
1430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1431 .rpc_argp = &args,
1432 .rpc_resp = &res,
1433 };
1434
Trond Myklebust0e574af2005-10-27 22:12:38 -04001435 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return rpc_call_sync(server->client, &msg, 0);
1437}
1438
1439static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1440{
1441 struct nfs4_exception exception = { };
1442 int err;
1443 do {
1444 err = nfs4_handle_exception(server,
1445 _nfs4_proc_getattr(server, fhandle, fattr),
1446 &exception);
1447 } while (exception.retry);
1448 return err;
1449}
1450
1451/*
1452 * The file is not closed if it is opened due to the a request to change
1453 * the size of the file. The open call will not be needed once the
1454 * VFS layer lookup-intents are implemented.
1455 *
1456 * Close is called when the inode is destroyed.
1457 * If we haven't opened the file for O_WRONLY, we
1458 * need to in the size_change case to obtain a stateid.
1459 *
1460 * Got race?
1461 * Because OPEN is always done by name in nfsv4, it is
1462 * possible that we opened a different file by the same
1463 * name. We can recognize this race condition, but we
1464 * can't do anything about it besides returning an error.
1465 *
1466 * This will be fixed with VFS changes (lookup-intent).
1467 */
1468static int
1469nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1470 struct iattr *sattr)
1471{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001472 struct rpc_cred *cred;
1473 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001474 struct nfs_open_context *ctx;
1475 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int status;
1477
Trond Myklebust0e574af2005-10-27 22:12:38 -04001478 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001480 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1481 if (IS_ERR(cred))
1482 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001483
1484 /* Search for an existing open(O_WRITE) file */
1485 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1486 if (ctx != NULL)
1487 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1490 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001491 if (status == 0)
1492 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001493 if (ctx != NULL)
1494 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001495 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return status;
1497}
1498
1499static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1500 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1501{
1502 int status;
1503 struct nfs_server *server = NFS_SERVER(dir);
1504 struct nfs4_lookup_arg args = {
1505 .bitmask = server->attr_bitmask,
1506 .dir_fh = NFS_FH(dir),
1507 .name = name,
1508 };
1509 struct nfs4_lookup_res res = {
1510 .server = server,
1511 .fattr = fattr,
1512 .fh = fhandle,
1513 };
1514 struct rpc_message msg = {
1515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1516 .rpc_argp = &args,
1517 .rpc_resp = &res,
1518 };
1519
Trond Myklebust0e574af2005-10-27 22:12:38 -04001520 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 dprintk("NFS call lookup %s\n", name->name);
1523 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1524 dprintk("NFS reply lookup: %d\n", status);
1525 return status;
1526}
1527
1528static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1529{
1530 struct nfs4_exception exception = { };
1531 int err;
1532 do {
1533 err = nfs4_handle_exception(NFS_SERVER(dir),
1534 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1535 &exception);
1536 } while (exception.retry);
1537 return err;
1538}
1539
1540static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1541{
1542 struct nfs4_accessargs args = {
1543 .fh = NFS_FH(inode),
1544 };
1545 struct nfs4_accessres res = { 0 };
1546 struct rpc_message msg = {
1547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1548 .rpc_argp = &args,
1549 .rpc_resp = &res,
1550 .rpc_cred = entry->cred,
1551 };
1552 int mode = entry->mask;
1553 int status;
1554
1555 /*
1556 * Determine which access bits we want to ask for...
1557 */
1558 if (mode & MAY_READ)
1559 args.access |= NFS4_ACCESS_READ;
1560 if (S_ISDIR(inode->i_mode)) {
1561 if (mode & MAY_WRITE)
1562 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1563 if (mode & MAY_EXEC)
1564 args.access |= NFS4_ACCESS_LOOKUP;
1565 } else {
1566 if (mode & MAY_WRITE)
1567 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1568 if (mode & MAY_EXEC)
1569 args.access |= NFS4_ACCESS_EXECUTE;
1570 }
1571 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1572 if (!status) {
1573 entry->mask = 0;
1574 if (res.access & NFS4_ACCESS_READ)
1575 entry->mask |= MAY_READ;
1576 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1577 entry->mask |= MAY_WRITE;
1578 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1579 entry->mask |= MAY_EXEC;
1580 }
1581 return status;
1582}
1583
1584static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1585{
1586 struct nfs4_exception exception = { };
1587 int err;
1588 do {
1589 err = nfs4_handle_exception(NFS_SERVER(inode),
1590 _nfs4_proc_access(inode, entry),
1591 &exception);
1592 } while (exception.retry);
1593 return err;
1594}
1595
1596/*
1597 * TODO: For the time being, we don't try to get any attributes
1598 * along with any of the zero-copy operations READ, READDIR,
1599 * READLINK, WRITE.
1600 *
1601 * In the case of the first three, we want to put the GETATTR
1602 * after the read-type operation -- this is because it is hard
1603 * to predict the length of a GETATTR response in v4, and thus
1604 * align the READ data correctly. This means that the GETATTR
1605 * may end up partially falling into the page cache, and we should
1606 * shift it into the 'tail' of the xdr_buf before processing.
1607 * To do this efficiently, we need to know the total length
1608 * of data received, which doesn't seem to be available outside
1609 * of the RPC layer.
1610 *
1611 * In the case of WRITE, we also want to put the GETATTR after
1612 * the operation -- in this case because we want to make sure
1613 * we get the post-operation mtime and size. This means that
1614 * we can't use xdr_encode_pages() as written: we need a variant
1615 * of it which would leave room in the 'tail' iovec.
1616 *
1617 * Both of these changes to the XDR layer would in fact be quite
1618 * minor, but I decided to leave them for a subsequent patch.
1619 */
1620static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1621 unsigned int pgbase, unsigned int pglen)
1622{
1623 struct nfs4_readlink args = {
1624 .fh = NFS_FH(inode),
1625 .pgbase = pgbase,
1626 .pglen = pglen,
1627 .pages = &page,
1628 };
1629 struct rpc_message msg = {
1630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1631 .rpc_argp = &args,
1632 .rpc_resp = NULL,
1633 };
1634
1635 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1636}
1637
1638static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1639 unsigned int pgbase, unsigned int pglen)
1640{
1641 struct nfs4_exception exception = { };
1642 int err;
1643 do {
1644 err = nfs4_handle_exception(NFS_SERVER(inode),
1645 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1646 &exception);
1647 } while (exception.retry);
1648 return err;
1649}
1650
1651static int _nfs4_proc_read(struct nfs_read_data *rdata)
1652{
1653 int flags = rdata->flags;
1654 struct inode *inode = rdata->inode;
1655 struct nfs_fattr *fattr = rdata->res.fattr;
1656 struct nfs_server *server = NFS_SERVER(inode);
1657 struct rpc_message msg = {
1658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1659 .rpc_argp = &rdata->args,
1660 .rpc_resp = &rdata->res,
1661 .rpc_cred = rdata->cred,
1662 };
1663 unsigned long timestamp = jiffies;
1664 int status;
1665
1666 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1667 (long long) rdata->args.offset);
1668
Trond Myklebust0e574af2005-10-27 22:12:38 -04001669 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 status = rpc_call_sync(server->client, &msg, flags);
1671 if (!status)
1672 renew_lease(server, timestamp);
1673 dprintk("NFS reply read: %d\n", status);
1674 return status;
1675}
1676
1677static int nfs4_proc_read(struct nfs_read_data *rdata)
1678{
1679 struct nfs4_exception exception = { };
1680 int err;
1681 do {
1682 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1683 _nfs4_proc_read(rdata),
1684 &exception);
1685 } while (exception.retry);
1686 return err;
1687}
1688
1689static int _nfs4_proc_write(struct nfs_write_data *wdata)
1690{
1691 int rpcflags = wdata->flags;
1692 struct inode *inode = wdata->inode;
1693 struct nfs_fattr *fattr = wdata->res.fattr;
1694 struct nfs_server *server = NFS_SERVER(inode);
1695 struct rpc_message msg = {
1696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1697 .rpc_argp = &wdata->args,
1698 .rpc_resp = &wdata->res,
1699 .rpc_cred = wdata->cred,
1700 };
1701 int status;
1702
1703 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1704 (long long) wdata->args.offset);
1705
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001706 wdata->args.bitmask = server->attr_bitmask;
1707 wdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001708 wdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001709 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 status = rpc_call_sync(server->client, &msg, rpcflags);
1711 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001712 if (status < 0)
1713 return status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001714 renew_lease(server, wdata->timestamp);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001715 nfs_post_op_update_inode(inode, fattr);
1716 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719static int nfs4_proc_write(struct nfs_write_data *wdata)
1720{
1721 struct nfs4_exception exception = { };
1722 int err;
1723 do {
1724 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1725 _nfs4_proc_write(wdata),
1726 &exception);
1727 } while (exception.retry);
1728 return err;
1729}
1730
1731static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1732{
1733 struct inode *inode = cdata->inode;
1734 struct nfs_fattr *fattr = cdata->res.fattr;
1735 struct nfs_server *server = NFS_SERVER(inode);
1736 struct rpc_message msg = {
1737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1738 .rpc_argp = &cdata->args,
1739 .rpc_resp = &cdata->res,
1740 .rpc_cred = cdata->cred,
1741 };
1742 int status;
1743
1744 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1745 (long long) cdata->args.offset);
1746
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001747 cdata->args.bitmask = server->attr_bitmask;
1748 cdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001749 cdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001750 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001752 if (status >= 0)
1753 renew_lease(server, cdata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001755 if (status >= 0)
1756 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return status;
1758}
1759
1760static int nfs4_proc_commit(struct nfs_write_data *cdata)
1761{
1762 struct nfs4_exception exception = { };
1763 int err;
1764 do {
1765 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1766 _nfs4_proc_commit(cdata),
1767 &exception);
1768 } while (exception.retry);
1769 return err;
1770}
1771
1772/*
1773 * Got race?
1774 * We will need to arrange for the VFS layer to provide an atomic open.
1775 * Until then, this create/open method is prone to inefficiency and race
1776 * conditions due to the lookup, create, and open VFS calls from sys_open()
1777 * placed on the wire.
1778 *
1779 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1780 * The file will be opened again in the subsequent VFS open call
1781 * (nfs4_proc_file_open).
1782 *
1783 * The open for read will just hang around to be used by any process that
1784 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1785 */
1786
1787static int
1788nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001789 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct nfs4_state *state;
1792 struct rpc_cred *cred;
1793 int status = 0;
1794
1795 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1796 if (IS_ERR(cred)) {
1797 status = PTR_ERR(cred);
1798 goto out;
1799 }
1800 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1801 put_rpccred(cred);
1802 if (IS_ERR(state)) {
1803 status = PTR_ERR(state);
1804 goto out;
1805 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001806 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (flags & O_EXCL) {
1808 struct nfs_fattr fattr;
1809 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1810 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001811 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001812 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001813 }
1814 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1815 nfs4_intent_set_file(nd, dentry, state);
1816 else
1817 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818out:
1819 return status;
1820}
1821
1822static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1823{
Trond Myklebust16e42952005-10-27 22:12:44 -04001824 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct nfs4_remove_arg args = {
1826 .fh = NFS_FH(dir),
1827 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001828 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001830 struct nfs_fattr dir_attr;
1831 struct nfs4_remove_res res = {
1832 .server = server,
1833 .dir_attr = &dir_attr,
1834 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct rpc_message msg = {
1836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1837 .rpc_argp = &args,
1838 .rpc_resp = &res,
1839 };
1840 int status;
1841
Trond Myklebust16e42952005-10-27 22:12:44 -04001842 nfs_fattr_init(res.dir_attr);
1843 status = rpc_call_sync(server->client, &msg, 0);
1844 if (status == 0) {
1845 update_changeattr(dir, &res.cinfo);
1846 nfs_post_op_update_inode(dir, res.dir_attr);
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return status;
1849}
1850
1851static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1852{
1853 struct nfs4_exception exception = { };
1854 int err;
1855 do {
1856 err = nfs4_handle_exception(NFS_SERVER(dir),
1857 _nfs4_proc_remove(dir, name),
1858 &exception);
1859 } while (exception.retry);
1860 return err;
1861}
1862
1863struct unlink_desc {
1864 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001865 struct nfs4_remove_res res;
1866 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867};
1868
1869static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1870 struct qstr *name)
1871{
Trond Myklebust16e42952005-10-27 22:12:44 -04001872 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct unlink_desc *up;
1874
1875 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1876 if (!up)
1877 return -ENOMEM;
1878
1879 up->args.fh = NFS_FH(dir->d_inode);
1880 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001881 up->args.bitmask = server->attr_bitmask;
1882 up->res.server = server;
1883 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1886 msg->rpc_argp = &up->args;
1887 msg->rpc_resp = &up->res;
1888 return 0;
1889}
1890
1891static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1892{
1893 struct rpc_message *msg = &task->tk_msg;
1894 struct unlink_desc *up;
1895
1896 if (msg->rpc_resp != NULL) {
1897 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001898 update_changeattr(dir->d_inode, &up->res.cinfo);
1899 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 kfree(up);
1901 msg->rpc_resp = NULL;
1902 msg->rpc_argp = NULL;
1903 }
1904 return 0;
1905}
1906
1907static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1908 struct inode *new_dir, struct qstr *new_name)
1909{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001910 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct nfs4_rename_arg arg = {
1912 .old_dir = NFS_FH(old_dir),
1913 .new_dir = NFS_FH(new_dir),
1914 .old_name = old_name,
1915 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001916 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001918 struct nfs_fattr old_fattr, new_fattr;
1919 struct nfs4_rename_res res = {
1920 .server = server,
1921 .old_fattr = &old_fattr,
1922 .new_fattr = &new_fattr,
1923 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct rpc_message msg = {
1925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1926 .rpc_argp = &arg,
1927 .rpc_resp = &res,
1928 };
1929 int status;
1930
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001931 nfs_fattr_init(res.old_fattr);
1932 nfs_fattr_init(res.new_fattr);
1933 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 if (!status) {
1936 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001937 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001939 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 return status;
1942}
1943
1944static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1945 struct inode *new_dir, struct qstr *new_name)
1946{
1947 struct nfs4_exception exception = { };
1948 int err;
1949 do {
1950 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1951 _nfs4_proc_rename(old_dir, old_name,
1952 new_dir, new_name),
1953 &exception);
1954 } while (exception.retry);
1955 return err;
1956}
1957
1958static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1959{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001960 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 struct nfs4_link_arg arg = {
1962 .fh = NFS_FH(inode),
1963 .dir_fh = NFS_FH(dir),
1964 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001965 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001967 struct nfs_fattr fattr, dir_attr;
1968 struct nfs4_link_res res = {
1969 .server = server,
1970 .fattr = &fattr,
1971 .dir_attr = &dir_attr,
1972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct rpc_message msg = {
1974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1975 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001976 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 };
1978 int status;
1979
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001980 nfs_fattr_init(res.fattr);
1981 nfs_fattr_init(res.dir_attr);
1982 status = rpc_call_sync(server->client, &msg, 0);
1983 if (!status) {
1984 update_changeattr(dir, &res.cinfo);
1985 nfs_post_op_update_inode(dir, res.dir_attr);
1986 nfs_refresh_inode(inode, res.fattr);
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 return status;
1990}
1991
1992static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1993{
1994 struct nfs4_exception exception = { };
1995 int err;
1996 do {
1997 err = nfs4_handle_exception(NFS_SERVER(inode),
1998 _nfs4_proc_link(inode, dir, name),
1999 &exception);
2000 } while (exception.retry);
2001 return err;
2002}
2003
2004static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2005 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2006 struct nfs_fattr *fattr)
2007{
2008 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002009 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct nfs4_create_arg arg = {
2011 .dir_fh = NFS_FH(dir),
2012 .server = server,
2013 .name = name,
2014 .attrs = sattr,
2015 .ftype = NF4LNK,
2016 .bitmask = server->attr_bitmask,
2017 };
2018 struct nfs4_create_res res = {
2019 .server = server,
2020 .fh = fhandle,
2021 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002022 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 };
2024 struct rpc_message msg = {
2025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2026 .rpc_argp = &arg,
2027 .rpc_resp = &res,
2028 };
2029 int status;
2030
2031 if (path->len > NFS4_MAXPATHLEN)
2032 return -ENAMETOOLONG;
2033 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04002034 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002035 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2038 if (!status)
2039 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002040 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return status;
2042}
2043
2044static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2045 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2046 struct nfs_fattr *fattr)
2047{
2048 struct nfs4_exception exception = { };
2049 int err;
2050 do {
2051 err = nfs4_handle_exception(NFS_SERVER(dir),
2052 _nfs4_proc_symlink(dir, name, path, sattr,
2053 fhandle, fattr),
2054 &exception);
2055 } while (exception.retry);
2056 return err;
2057}
2058
2059static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2060 struct iattr *sattr)
2061{
2062 struct nfs_server *server = NFS_SERVER(dir);
2063 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002064 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct nfs4_create_arg arg = {
2066 .dir_fh = NFS_FH(dir),
2067 .server = server,
2068 .name = &dentry->d_name,
2069 .attrs = sattr,
2070 .ftype = NF4DIR,
2071 .bitmask = server->attr_bitmask,
2072 };
2073 struct nfs4_create_res res = {
2074 .server = server,
2075 .fh = &fhandle,
2076 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002077 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 };
2079 struct rpc_message msg = {
2080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2081 .rpc_argp = &arg,
2082 .rpc_resp = &res,
2083 };
2084 int status;
2085
Trond Myklebust0e574af2005-10-27 22:12:38 -04002086 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002087 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2090 if (!status) {
2091 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002092 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 status = nfs_instantiate(dentry, &fhandle, &fattr);
2094 }
2095 return status;
2096}
2097
2098static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2099 struct iattr *sattr)
2100{
2101 struct nfs4_exception exception = { };
2102 int err;
2103 do {
2104 err = nfs4_handle_exception(NFS_SERVER(dir),
2105 _nfs4_proc_mkdir(dir, dentry, sattr),
2106 &exception);
2107 } while (exception.retry);
2108 return err;
2109}
2110
2111static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2112 u64 cookie, struct page *page, unsigned int count, int plus)
2113{
2114 struct inode *dir = dentry->d_inode;
2115 struct nfs4_readdir_arg args = {
2116 .fh = NFS_FH(dir),
2117 .pages = &page,
2118 .pgbase = 0,
2119 .count = count,
2120 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2121 };
2122 struct nfs4_readdir_res res;
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2125 .rpc_argp = &args,
2126 .rpc_resp = &res,
2127 .rpc_cred = cred,
2128 };
2129 int status;
2130
Trond Myklebusteadf4592005-06-22 17:16:39 +00002131 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2132 dentry->d_parent->d_name.name,
2133 dentry->d_name.name,
2134 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 lock_kernel();
2136 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2137 res.pgbase = args.pgbase;
2138 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2139 if (status == 0)
2140 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2141 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00002142 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return status;
2144}
2145
2146static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2147 u64 cookie, struct page *page, unsigned int count, int plus)
2148{
2149 struct nfs4_exception exception = { };
2150 int err;
2151 do {
2152 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2153 _nfs4_proc_readdir(dentry, cred, cookie,
2154 page, count, plus),
2155 &exception);
2156 } while (exception.retry);
2157 return err;
2158}
2159
2160static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2161 struct iattr *sattr, dev_t rdev)
2162{
2163 struct nfs_server *server = NFS_SERVER(dir);
2164 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002165 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct nfs4_create_arg arg = {
2167 .dir_fh = NFS_FH(dir),
2168 .server = server,
2169 .name = &dentry->d_name,
2170 .attrs = sattr,
2171 .bitmask = server->attr_bitmask,
2172 };
2173 struct nfs4_create_res res = {
2174 .server = server,
2175 .fh = &fh,
2176 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002177 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 };
2179 struct rpc_message msg = {
2180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2181 .rpc_argp = &arg,
2182 .rpc_resp = &res,
2183 };
2184 int status;
2185 int mode = sattr->ia_mode;
2186
Trond Myklebust0e574af2005-10-27 22:12:38 -04002187 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002188 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2191 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2192 if (S_ISFIFO(mode))
2193 arg.ftype = NF4FIFO;
2194 else if (S_ISBLK(mode)) {
2195 arg.ftype = NF4BLK;
2196 arg.u.device.specdata1 = MAJOR(rdev);
2197 arg.u.device.specdata2 = MINOR(rdev);
2198 }
2199 else if (S_ISCHR(mode)) {
2200 arg.ftype = NF4CHR;
2201 arg.u.device.specdata1 = MAJOR(rdev);
2202 arg.u.device.specdata2 = MINOR(rdev);
2203 }
2204 else
2205 arg.ftype = NF4SOCK;
2206
2207 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2208 if (status == 0) {
2209 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002210 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 status = nfs_instantiate(dentry, &fh, &fattr);
2212 }
2213 return status;
2214}
2215
2216static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2217 struct iattr *sattr, dev_t rdev)
2218{
2219 struct nfs4_exception exception = { };
2220 int err;
2221 do {
2222 err = nfs4_handle_exception(NFS_SERVER(dir),
2223 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2224 &exception);
2225 } while (exception.retry);
2226 return err;
2227}
2228
2229static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2230 struct nfs_fsstat *fsstat)
2231{
2232 struct nfs4_statfs_arg args = {
2233 .fh = fhandle,
2234 .bitmask = server->attr_bitmask,
2235 };
2236 struct rpc_message msg = {
2237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2238 .rpc_argp = &args,
2239 .rpc_resp = fsstat,
2240 };
2241
Trond Myklebust0e574af2005-10-27 22:12:38 -04002242 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return rpc_call_sync(server->client, &msg, 0);
2244}
2245
2246static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2247{
2248 struct nfs4_exception exception = { };
2249 int err;
2250 do {
2251 err = nfs4_handle_exception(server,
2252 _nfs4_proc_statfs(server, fhandle, fsstat),
2253 &exception);
2254 } while (exception.retry);
2255 return err;
2256}
2257
2258static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2259 struct nfs_fsinfo *fsinfo)
2260{
2261 struct nfs4_fsinfo_arg args = {
2262 .fh = fhandle,
2263 .bitmask = server->attr_bitmask,
2264 };
2265 struct rpc_message msg = {
2266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2267 .rpc_argp = &args,
2268 .rpc_resp = fsinfo,
2269 };
2270
2271 return rpc_call_sync(server->client, &msg, 0);
2272}
2273
2274static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2275{
2276 struct nfs4_exception exception = { };
2277 int err;
2278
2279 do {
2280 err = nfs4_handle_exception(server,
2281 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2282 &exception);
2283 } while (exception.retry);
2284 return err;
2285}
2286
2287static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2288{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002289 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2291}
2292
2293static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2294 struct nfs_pathconf *pathconf)
2295{
2296 struct nfs4_pathconf_arg args = {
2297 .fh = fhandle,
2298 .bitmask = server->attr_bitmask,
2299 };
2300 struct rpc_message msg = {
2301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2302 .rpc_argp = &args,
2303 .rpc_resp = pathconf,
2304 };
2305
2306 /* None of the pathconf attributes are mandatory to implement */
2307 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2308 memset(pathconf, 0, sizeof(*pathconf));
2309 return 0;
2310 }
2311
Trond Myklebust0e574af2005-10-27 22:12:38 -04002312 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return rpc_call_sync(server->client, &msg, 0);
2314}
2315
2316static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_pathconf *pathconf)
2318{
2319 struct nfs4_exception exception = { };
2320 int err;
2321
2322 do {
2323 err = nfs4_handle_exception(server,
2324 _nfs4_proc_pathconf(server, fhandle, pathconf),
2325 &exception);
2326 } while (exception.retry);
2327 return err;
2328}
2329
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002330static void nfs4_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002332 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 struct inode *inode = data->inode;
2334
2335 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2336 rpc_restart_call(task);
2337 return;
2338 }
2339 if (task->tk_status > 0)
2340 renew_lease(NFS_SERVER(inode), data->timestamp);
2341 /* Call back common NFS readpage processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002342 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002345static const struct rpc_call_ops nfs4_read_ops = {
2346 .rpc_call_done = nfs4_read_done,
2347 .rpc_release = nfs_readdata_release,
2348};
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350static void
2351nfs4_proc_read_setup(struct nfs_read_data *data)
2352{
2353 struct rpc_task *task = &data->task;
2354 struct rpc_message msg = {
2355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2356 .rpc_argp = &data->args,
2357 .rpc_resp = &data->res,
2358 .rpc_cred = data->cred,
2359 };
2360 struct inode *inode = data->inode;
2361 int flags;
2362
2363 data->timestamp = jiffies;
2364
2365 /* N.B. Do we need to test? Never called for swapfile inode */
2366 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2367
2368 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002369 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 rpc_call_setup(task, &msg, 0);
2371}
2372
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002373static void nfs4_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002375 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 struct inode *inode = data->inode;
2377
2378 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2379 rpc_restart_call(task);
2380 return;
2381 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002382 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002384 nfs_post_op_update_inode(inode, data->res.fattr);
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002387 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002390static const struct rpc_call_ops nfs4_write_ops = {
2391 .rpc_call_done = nfs4_write_done,
2392 .rpc_release = nfs_writedata_release,
2393};
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395static void
2396nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2397{
2398 struct rpc_task *task = &data->task;
2399 struct rpc_message msg = {
2400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2401 .rpc_argp = &data->args,
2402 .rpc_resp = &data->res,
2403 .rpc_cred = data->cred,
2404 };
2405 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002406 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 int stable;
2408 int flags;
2409
2410 if (how & FLUSH_STABLE) {
2411 if (!NFS_I(inode)->ncommit)
2412 stable = NFS_FILE_SYNC;
2413 else
2414 stable = NFS_DATA_SYNC;
2415 } else
2416 stable = NFS_UNSTABLE;
2417 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002418 data->args.bitmask = server->attr_bitmask;
2419 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 data->timestamp = jiffies;
2422
2423 /* Set the initial flags for the task. */
2424 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2425
2426 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002427 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 rpc_call_setup(task, &msg, 0);
2429}
2430
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002431static void nfs4_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002433 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct inode *inode = data->inode;
2435
2436 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2437 rpc_restart_call(task);
2438 return;
2439 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002440 if (task->tk_status >= 0)
2441 nfs_post_op_update_inode(inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002443 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002446static const struct rpc_call_ops nfs4_commit_ops = {
2447 .rpc_call_done = nfs4_commit_done,
2448 .rpc_release = nfs_commit_release,
2449};
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451static void
2452nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2453{
2454 struct rpc_task *task = &data->task;
2455 struct rpc_message msg = {
2456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2457 .rpc_argp = &data->args,
2458 .rpc_resp = &data->res,
2459 .rpc_cred = data->cred,
2460 };
2461 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002462 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 int flags;
2464
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002465 data->args.bitmask = server->attr_bitmask;
2466 data->res.server = server;
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /* Set the initial flags for the task. */
2469 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2470
2471 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002472 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 rpc_call_setup(task, &msg, 0);
2474}
2475
2476/*
2477 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2478 * standalone procedure for queueing an asynchronous RENEW.
2479 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002480static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002483 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (task->tk_status < 0) {
2486 switch (task->tk_status) {
2487 case -NFS4ERR_STALE_CLIENTID:
2488 case -NFS4ERR_EXPIRED:
2489 case -NFS4ERR_CB_PATH_DOWN:
2490 nfs4_schedule_state_recovery(clp);
2491 }
2492 return;
2493 }
2494 spin_lock(&clp->cl_lock);
2495 if (time_before(clp->cl_last_renewal,timestamp))
2496 clp->cl_last_renewal = timestamp;
2497 spin_unlock(&clp->cl_lock);
2498}
2499
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002500static const struct rpc_call_ops nfs4_renew_ops = {
2501 .rpc_call_done = nfs4_renew_done,
2502};
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504int
2505nfs4_proc_async_renew(struct nfs4_client *clp)
2506{
2507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2509 .rpc_argp = clp,
2510 .rpc_cred = clp->cl_cred,
2511 };
2512
2513 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002514 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517int
2518nfs4_proc_renew(struct nfs4_client *clp)
2519{
2520 struct rpc_message msg = {
2521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2522 .rpc_argp = clp,
2523 .rpc_cred = clp->cl_cred,
2524 };
2525 unsigned long now = jiffies;
2526 int status;
2527
2528 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2529 if (status < 0)
2530 return status;
2531 spin_lock(&clp->cl_lock);
2532 if (time_before(clp->cl_last_renewal,now))
2533 clp->cl_last_renewal = now;
2534 spin_unlock(&clp->cl_lock);
2535 return 0;
2536}
2537
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002538static inline int nfs4_server_supports_acls(struct nfs_server *server)
2539{
2540 return (server->caps & NFS_CAP_ACLS)
2541 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2542 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2543}
2544
2545/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2546 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2547 * the stack.
2548 */
2549#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2550
2551static void buf_to_pages(const void *buf, size_t buflen,
2552 struct page **pages, unsigned int *pgbase)
2553{
2554 const void *p = buf;
2555
2556 *pgbase = offset_in_page(buf);
2557 p -= *pgbase;
2558 while (p < buf + buflen) {
2559 *(pages++) = virt_to_page(p);
2560 p += PAGE_CACHE_SIZE;
2561 }
2562}
2563
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002564struct nfs4_cached_acl {
2565 int cached;
2566 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002567 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002568};
2569
2570static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002571{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002572 struct nfs_inode *nfsi = NFS_I(inode);
2573
2574 spin_lock(&inode->i_lock);
2575 kfree(nfsi->nfs4_acl);
2576 nfsi->nfs4_acl = acl;
2577 spin_unlock(&inode->i_lock);
2578}
2579
2580static void nfs4_zap_acl_attr(struct inode *inode)
2581{
2582 nfs4_set_cached_acl(inode, NULL);
2583}
2584
2585static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2586{
2587 struct nfs_inode *nfsi = NFS_I(inode);
2588 struct nfs4_cached_acl *acl;
2589 int ret = -ENOENT;
2590
2591 spin_lock(&inode->i_lock);
2592 acl = nfsi->nfs4_acl;
2593 if (acl == NULL)
2594 goto out;
2595 if (buf == NULL) /* user is just asking for length */
2596 goto out_len;
2597 if (acl->cached == 0)
2598 goto out;
2599 ret = -ERANGE; /* see getxattr(2) man page */
2600 if (acl->len > buflen)
2601 goto out;
2602 memcpy(buf, acl->data, acl->len);
2603out_len:
2604 ret = acl->len;
2605out:
2606 spin_unlock(&inode->i_lock);
2607 return ret;
2608}
2609
2610static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2611{
2612 struct nfs4_cached_acl *acl;
2613
2614 if (buf && acl_len <= PAGE_SIZE) {
2615 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2616 if (acl == NULL)
2617 goto out;
2618 acl->cached = 1;
2619 memcpy(acl->data, buf, acl_len);
2620 } else {
2621 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2622 if (acl == NULL)
2623 goto out;
2624 acl->cached = 0;
2625 }
2626 acl->len = acl_len;
2627out:
2628 nfs4_set_cached_acl(inode, acl);
2629}
2630
2631static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2632{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002633 struct page *pages[NFS4ACL_MAXPAGES];
2634 struct nfs_getaclargs args = {
2635 .fh = NFS_FH(inode),
2636 .acl_pages = pages,
2637 .acl_len = buflen,
2638 };
2639 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002640 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002641 struct rpc_message msg = {
2642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2643 .rpc_argp = &args,
2644 .rpc_resp = &resp_len,
2645 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002646 struct page *localpage = NULL;
2647 int ret;
2648
2649 if (buflen < PAGE_SIZE) {
2650 /* As long as we're doing a round trip to the server anyway,
2651 * let's be prepared for a page of acl data. */
2652 localpage = alloc_page(GFP_KERNEL);
2653 resp_buf = page_address(localpage);
2654 if (localpage == NULL)
2655 return -ENOMEM;
2656 args.acl_pages[0] = localpage;
2657 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002658 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002659 } else {
2660 resp_buf = buf;
2661 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2662 }
2663 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2664 if (ret)
2665 goto out_free;
2666 if (resp_len > args.acl_len)
2667 nfs4_write_cached_acl(inode, NULL, resp_len);
2668 else
2669 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2670 if (buf) {
2671 ret = -ERANGE;
2672 if (resp_len > buflen)
2673 goto out_free;
2674 if (localpage)
2675 memcpy(buf, resp_buf, resp_len);
2676 }
2677 ret = resp_len;
2678out_free:
2679 if (localpage)
2680 __free_page(localpage);
2681 return ret;
2682}
2683
2684static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2685{
2686 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002687 int ret;
2688
2689 if (!nfs4_server_supports_acls(server))
2690 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002691 ret = nfs_revalidate_inode(server, inode);
2692 if (ret < 0)
2693 return ret;
2694 ret = nfs4_read_cached_acl(inode, buf, buflen);
2695 if (ret != -ENOENT)
2696 return ret;
2697 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002698}
2699
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002700static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2701{
2702 struct nfs_server *server = NFS_SERVER(inode);
2703 struct page *pages[NFS4ACL_MAXPAGES];
2704 struct nfs_setaclargs arg = {
2705 .fh = NFS_FH(inode),
2706 .acl_pages = pages,
2707 .acl_len = buflen,
2708 };
2709 struct rpc_message msg = {
2710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2711 .rpc_argp = &arg,
2712 .rpc_resp = NULL,
2713 };
2714 int ret;
2715
2716 if (!nfs4_server_supports_acls(server))
2717 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002718 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002719 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2720 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002721 if (ret == 0)
2722 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002723 return ret;
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002727nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 struct nfs4_client *clp = server->nfs4_state;
2730
2731 if (!clp || task->tk_status >= 0)
2732 return 0;
2733 switch(task->tk_status) {
2734 case -NFS4ERR_STALE_CLIENTID:
2735 case -NFS4ERR_STALE_STATEID:
2736 case -NFS4ERR_EXPIRED:
2737 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2738 nfs4_schedule_state_recovery(clp);
2739 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2740 rpc_wake_up_task(task);
2741 task->tk_status = 0;
2742 return -EAGAIN;
2743 case -NFS4ERR_GRACE:
2744 case -NFS4ERR_DELAY:
2745 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2746 task->tk_status = 0;
2747 return -EAGAIN;
2748 case -NFS4ERR_OLD_STATEID:
2749 task->tk_status = 0;
2750 return -EAGAIN;
2751 }
2752 task->tk_status = nfs4_map_errors(task->tk_status);
2753 return 0;
2754}
2755
2756static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2757{
2758 DEFINE_WAIT(wait);
2759 sigset_t oldset;
2760 int interruptible, res = 0;
2761
2762 might_sleep();
2763
2764 rpc_clnt_sigmask(clnt, &oldset);
2765 interruptible = TASK_UNINTERRUPTIBLE;
2766 if (clnt->cl_intr)
2767 interruptible = TASK_INTERRUPTIBLE;
2768 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2769 nfs4_schedule_state_recovery(clp);
2770 if (clnt->cl_intr && signalled())
2771 res = -ERESTARTSYS;
2772 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2773 schedule();
2774 finish_wait(&clp->cl_waitq, &wait);
2775 rpc_clnt_sigunmask(clnt, &oldset);
2776 return res;
2777}
2778
2779static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2780{
2781 sigset_t oldset;
2782 int res = 0;
2783
2784 might_sleep();
2785
2786 if (*timeout <= 0)
2787 *timeout = NFS4_POLL_RETRY_MIN;
2788 if (*timeout > NFS4_POLL_RETRY_MAX)
2789 *timeout = NFS4_POLL_RETRY_MAX;
2790 rpc_clnt_sigmask(clnt, &oldset);
2791 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002792 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (signalled())
2794 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002795 } else
2796 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 rpc_clnt_sigunmask(clnt, &oldset);
2798 *timeout <<= 1;
2799 return res;
2800}
2801
2802/* This is the error handling routine for processes that are allowed
2803 * to sleep.
2804 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002805int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct nfs4_client *clp = server->nfs4_state;
2808 int ret = errorcode;
2809
2810 exception->retry = 0;
2811 switch(errorcode) {
2812 case 0:
2813 return 0;
2814 case -NFS4ERR_STALE_CLIENTID:
2815 case -NFS4ERR_STALE_STATEID:
2816 case -NFS4ERR_EXPIRED:
2817 ret = nfs4_wait_clnt_recover(server->client, clp);
2818 if (ret == 0)
2819 exception->retry = 1;
2820 break;
2821 case -NFS4ERR_GRACE:
2822 case -NFS4ERR_DELAY:
2823 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002824 if (ret != 0)
2825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002827 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 /* We failed to handle the error */
2830 return nfs4_map_errors(ret);
2831}
2832
2833int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2834{
2835 nfs4_verifier sc_verifier;
2836 struct nfs4_setclientid setclientid = {
2837 .sc_verifier = &sc_verifier,
2838 .sc_prog = program,
2839 };
2840 struct rpc_message msg = {
2841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2842 .rpc_argp = &setclientid,
2843 .rpc_resp = clp,
2844 .rpc_cred = clp->cl_cred,
2845 };
2846 u32 *p;
2847 int loop = 0;
2848 int status;
2849
2850 p = (u32*)sc_verifier.data;
2851 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2852 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2853
2854 for(;;) {
2855 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2856 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2857 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2858 clp->cl_cred->cr_ops->cr_name,
2859 clp->cl_id_uniquifier);
2860 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2861 sizeof(setclientid.sc_netid), "tcp");
2862 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2863 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2864 clp->cl_ipaddr, port >> 8, port & 255);
2865
2866 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2867 if (status != -NFS4ERR_CLID_INUSE)
2868 break;
2869 if (signalled())
2870 break;
2871 if (loop++ & 1)
2872 ssleep(clp->cl_lease_time + 1);
2873 else
2874 if (++clp->cl_id_uniquifier == 0)
2875 break;
2876 }
2877 return status;
2878}
2879
2880int
2881nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2882{
2883 struct nfs_fsinfo fsinfo;
2884 struct rpc_message msg = {
2885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2886 .rpc_argp = clp,
2887 .rpc_resp = &fsinfo,
2888 .rpc_cred = clp->cl_cred,
2889 };
2890 unsigned long now;
2891 int status;
2892
2893 now = jiffies;
2894 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2895 if (status == 0) {
2896 spin_lock(&clp->cl_lock);
2897 clp->cl_lease_time = fsinfo.lease_time * HZ;
2898 clp->cl_last_renewal = now;
2899 spin_unlock(&clp->cl_lock);
2900 }
2901 return status;
2902}
2903
Trond Myklebustfe650402006-01-03 09:55:18 +01002904struct nfs4_delegreturndata {
2905 struct nfs4_delegreturnargs args;
2906 struct nfs_fh fh;
2907 nfs4_stateid stateid;
2908 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002909 unsigned long timestamp;
2910 const struct nfs_server *server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002911 int rpc_status;
2912};
2913
2914static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Trond Myklebustfe650402006-01-03 09:55:18 +01002916 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 struct rpc_message msg = {
2918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002919 .rpc_argp = &data->args,
2920 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 };
Trond Myklebustfe650402006-01-03 09:55:18 +01002922 rpc_call_setup(task, &msg, 0);
2923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebustfe650402006-01-03 09:55:18 +01002925static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2926{
2927 struct nfs4_delegreturndata *data = calldata;
2928 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002929 if (data->rpc_status == 0)
2930 renew_lease(data->server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002931}
2932
2933static void nfs4_delegreturn_release(void *calldata)
2934{
2935 struct nfs4_delegreturndata *data = calldata;
2936
2937 put_rpccred(data->cred);
2938 kfree(calldata);
2939}
2940
2941const static struct rpc_call_ops nfs4_delegreturn_ops = {
2942 .rpc_call_prepare = nfs4_delegreturn_prepare,
2943 .rpc_call_done = nfs4_delegreturn_done,
2944 .rpc_release = nfs4_delegreturn_release,
2945};
2946
2947static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2948{
2949 struct nfs4_delegreturndata *data;
2950 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;
2958 nfs_copy_fh(&data->fh, NFS_FH(inode));
2959 memcpy(&data->stateid, stateid, sizeof(data->stateid));
2960 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002961 data->timestamp = jiffies;
2962 data->server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002963 data->rpc_status = 0;
2964
2965 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2966 if (IS_ERR(task)) {
2967 nfs4_delegreturn_release(data);
2968 return PTR_ERR(task);
2969 }
2970 status = nfs4_wait_for_completion_rpc_task(task);
2971 if (status == 0)
2972 status = data->rpc_status;
2973 rpc_release_task(task);
2974 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2978{
2979 struct nfs_server *server = NFS_SERVER(inode);
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2984 switch (err) {
2985 case -NFS4ERR_STALE_STATEID:
2986 case -NFS4ERR_EXPIRED:
2987 nfs4_schedule_state_recovery(server->nfs4_state);
2988 case 0:
2989 return 0;
2990 }
2991 err = nfs4_handle_exception(server, err, &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2997#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2998
2999/*
3000 * sleep, with exponential backoff, and retry the LOCK operation.
3001 */
3002static unsigned long
3003nfs4_set_lock_task_retry(unsigned long timeout)
3004{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003005 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 timeout <<= 1;
3007 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3008 return NFS4_LOCK_MAXTIMEOUT;
3009 return timeout;
3010}
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3013{
3014 struct inode *inode = state->inode;
3015 struct nfs_server *server = NFS_SERVER(inode);
3016 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003017 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003019 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003021 struct nfs_lockt_res res = {
3022 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 };
3024 struct rpc_message msg = {
3025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3026 .rpc_argp = &arg,
3027 .rpc_resp = &res,
3028 .rpc_cred = state->owner->so_cred,
3029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct nfs4_lock_state *lsp;
3031 int status;
3032
3033 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003034 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003035 status = nfs4_set_lock_state(state, request);
3036 if (status != 0)
3037 goto out;
3038 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003039 arg.lock_owner.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003041 switch (status) {
3042 case 0:
3043 request->fl_type = F_UNLCK;
3044 break;
3045 case -NFS4ERR_DENIED:
3046 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003048out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 up_read(&clp->cl_sem);
3050 return status;
3051}
3052
3053static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3054{
3055 struct nfs4_exception exception = { };
3056 int err;
3057
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3060 _nfs4_proc_getlk(state, cmd, request),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
3066static int do_vfs_lock(struct file *file, struct file_lock *fl)
3067{
3068 int res = 0;
3069 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3070 case FL_POSIX:
3071 res = posix_lock_file_wait(file, fl);
3072 break;
3073 case FL_FLOCK:
3074 res = flock_lock_file_wait(file, fl);
3075 break;
3076 default:
3077 BUG();
3078 }
3079 if (res < 0)
3080 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3081 __FUNCTION__);
3082 return res;
3083}
3084
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003085struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003086 struct nfs_locku_args arg;
3087 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003088 struct nfs4_lock_state *lsp;
3089 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003090 struct file_lock fl;
3091 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003092 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003093};
3094
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003095static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3096 struct nfs_open_context *ctx,
3097 struct nfs4_lock_state *lsp,
3098 struct nfs_seqid *seqid)
3099{
3100 struct nfs4_unlockdata *p;
3101 struct inode *inode = lsp->ls_state->inode;
3102
3103 p = kmalloc(sizeof(*p), GFP_KERNEL);
3104 if (p == NULL)
3105 return NULL;
3106 p->arg.fh = NFS_FH(inode);
3107 p->arg.fl = &p->fl;
3108 p->arg.seqid = seqid;
3109 p->arg.stateid = &lsp->ls_stateid;
3110 p->lsp = lsp;
3111 atomic_inc(&lsp->ls_count);
3112 /* Ensure we don't close file until we're done freeing locks! */
3113 p->ctx = get_nfs_open_context(ctx);
3114 memcpy(&p->fl, fl, sizeof(p->fl));
3115 p->server = NFS_SERVER(inode);
3116 return p;
3117}
3118
Trond Myklebust06f814a2006-01-03 09:55:07 +01003119static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003120{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003121 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003122 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003123 nfs4_put_lock_state(calldata->lsp);
3124 put_nfs_open_context(calldata->ctx);
3125 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003126}
3127
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003128static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003129{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003130 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003131
Trond Myklebust06f814a2006-01-03 09:55:07 +01003132 if (RPC_ASSASSINATED(task))
3133 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003134 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003135 switch (task->tk_status) {
3136 case 0:
3137 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003138 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003139 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003140 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003141 break;
3142 case -NFS4ERR_STALE_STATEID:
3143 case -NFS4ERR_EXPIRED:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003144 nfs4_schedule_state_recovery(calldata->server->nfs4_state);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003145 break;
3146 default:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003147 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) {
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003148 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003149 }
3150 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003151}
3152
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003153static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003154{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003155 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003158 .rpc_argp = &calldata->arg,
3159 .rpc_resp = &calldata->res,
3160 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003163 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003164 return;
3165 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003166 /* Note: exit _without_ running nfs4_locku_done */
3167 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003168 return;
3169 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003170 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003171 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003174static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003175 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003176 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003177 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178};
3179
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003180static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3181 struct nfs_open_context *ctx,
3182 struct nfs4_lock_state *lsp,
3183 struct nfs_seqid *seqid)
3184{
3185 struct nfs4_unlockdata *data;
3186 struct rpc_task *task;
3187
3188 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3189 if (data == NULL) {
3190 nfs_free_seqid(seqid);
3191 return ERR_PTR(-ENOMEM);
3192 }
3193
3194 /* Unlock _before_ we do the RPC call */
3195 do_vfs_lock(fl->fl_file, fl);
3196 task = rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3197 if (IS_ERR(task))
3198 nfs4_locku_release_calldata(data);
3199 return task;
3200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3203{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003204 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003205 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003206 struct rpc_task *task;
3207 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003209 /* Is this a delegated lock? */
3210 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003211 goto out_unlock;
3212 /* Is this open_owner holding any locks on the server? */
3213 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
3214 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003215
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003216 status = nfs4_set_lock_state(state, request);
3217 if (status != 0)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003218 goto out_unlock;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003219 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003220 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003221 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3222 if (seqid == NULL)
3223 goto out_unlock;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003224 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3225 status = PTR_ERR(task);
3226 if (IS_ERR(task))
3227 goto out_unlock;
3228 status = nfs4_wait_for_completion_rpc_task(task);
3229 rpc_release_task(task);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003230 return status;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003231out_unlock:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003232 do_vfs_lock(request->fl_file, request);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003233 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003236struct nfs4_lockdata {
3237 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003238 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003239 struct nfs4_lock_state *lsp;
3240 struct nfs_open_context *ctx;
3241 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003242 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003243 int rpc_status;
3244 int cancelled;
3245};
3246
3247static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3248 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3249{
3250 struct nfs4_lockdata *p;
3251 struct inode *inode = lsp->ls_state->inode;
3252 struct nfs_server *server = NFS_SERVER(inode);
3253
3254 p = kzalloc(sizeof(*p), GFP_KERNEL);
3255 if (p == NULL)
3256 return NULL;
3257
3258 p->arg.fh = NFS_FH(inode);
3259 p->arg.fl = &p->fl;
3260 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3261 if (p->arg.lock_seqid == NULL)
3262 goto out_free;
3263 p->arg.lock_stateid = &lsp->ls_stateid;
3264 p->arg.lock_owner.clientid = server->nfs4_state->cl_clientid;
3265 p->arg.lock_owner.id = lsp->ls_id;
3266 p->lsp = lsp;
3267 atomic_inc(&lsp->ls_count);
3268 p->ctx = get_nfs_open_context(ctx);
3269 memcpy(&p->fl, fl, sizeof(p->fl));
3270 return p;
3271out_free:
3272 kfree(p);
3273 return NULL;
3274}
3275
3276static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3277{
3278 struct nfs4_lockdata *data = calldata;
3279 struct nfs4_state *state = data->lsp->ls_state;
3280 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3283 .rpc_argp = &data->arg,
3284 .rpc_resp = &data->res,
3285 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003288 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3289 return;
3290 dprintk("%s: begin!\n", __FUNCTION__);
3291 /* Do we need to do an open_to_lock_owner? */
3292 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3293 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3294 if (data->arg.open_seqid == NULL) {
3295 data->rpc_status = -ENOMEM;
3296 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003297 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003298 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003299 data->arg.open_stateid = &state->stateid;
3300 data->arg.new_lock_owner = 1;
3301 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003302 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003303 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003304out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003305 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3306}
3307
3308static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3309{
3310 struct nfs4_lockdata *data = calldata;
3311
3312 dprintk("%s: begin!\n", __FUNCTION__);
3313
3314 data->rpc_status = task->tk_status;
3315 if (RPC_ASSASSINATED(task))
3316 goto out;
3317 if (data->arg.new_lock_owner != 0) {
3318 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3319 if (data->rpc_status == 0)
3320 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3321 else
3322 goto out;
3323 }
3324 if (data->rpc_status == 0) {
3325 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3326 sizeof(data->lsp->ls_stateid.data));
3327 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003328 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003329 }
3330 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3331out:
3332 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3333}
3334
3335static void nfs4_lock_release(void *calldata)
3336{
3337 struct nfs4_lockdata *data = calldata;
3338
3339 dprintk("%s: begin!\n", __FUNCTION__);
3340 if (data->arg.open_seqid != NULL)
3341 nfs_free_seqid(data->arg.open_seqid);
3342 if (data->cancelled != 0) {
3343 struct rpc_task *task;
3344 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3345 data->arg.lock_seqid);
3346 if (!IS_ERR(task))
3347 rpc_release_task(task);
3348 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3349 } else
3350 nfs_free_seqid(data->arg.lock_seqid);
3351 nfs4_put_lock_state(data->lsp);
3352 put_nfs_open_context(data->ctx);
3353 kfree(data);
3354 dprintk("%s: done!\n", __FUNCTION__);
3355}
3356
3357static const struct rpc_call_ops nfs4_lock_ops = {
3358 .rpc_call_prepare = nfs4_lock_prepare,
3359 .rpc_call_done = nfs4_lock_done,
3360 .rpc_release = nfs4_lock_release,
3361};
3362
3363static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3364{
3365 struct nfs4_lockdata *data;
3366 struct rpc_task *task;
3367 int ret;
3368
3369 dprintk("%s: begin!\n", __FUNCTION__);
3370 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3371 fl->fl_u.nfs4_fl.owner);
3372 if (data == NULL)
3373 return -ENOMEM;
3374 if (IS_SETLKW(cmd))
3375 data->arg.block = 1;
3376 if (reclaim != 0)
3377 data->arg.reclaim = 1;
3378 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3379 &nfs4_lock_ops, data);
3380 if (IS_ERR(task)) {
3381 nfs4_lock_release(data);
3382 return PTR_ERR(task);
3383 }
3384 ret = nfs4_wait_for_completion_rpc_task(task);
3385 if (ret == 0) {
3386 ret = data->rpc_status;
3387 if (ret == -NFS4ERR_DENIED)
3388 ret = -EAGAIN;
3389 } else
3390 data->cancelled = 1;
3391 rpc_release_task(task);
3392 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3393 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
3396static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3397{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003398 struct nfs_server *server = NFS_SERVER(state->inode);
3399 struct nfs4_exception exception = { };
3400 int err;
3401
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003402 /* Cache the lock if possible... */
3403 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3404 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003405 do {
3406 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3407 if (err != -NFS4ERR_DELAY)
3408 break;
3409 nfs4_handle_exception(server, err, &exception);
3410 } while (exception.retry);
3411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412}
3413
3414static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3415{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003416 struct nfs_server *server = NFS_SERVER(state->inode);
3417 struct nfs4_exception exception = { };
3418 int err;
3419
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003420 err = nfs4_set_lock_state(state, request);
3421 if (err != 0)
3422 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003423 do {
3424 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3425 if (err != -NFS4ERR_DELAY)
3426 break;
3427 nfs4_handle_exception(server, err, &exception);
3428 } while (exception.retry);
3429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3433{
3434 struct nfs4_client *clp = state->owner->so_client;
3435 int status;
3436
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003437 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003438 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003439 /* Yes: cache locks! */
3440 status = do_vfs_lock(request->fl_file, request);
3441 /* ...but avoid races with delegation recall... */
3442 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
Trond Myklebustff604062005-11-25 17:10:01 -05003445 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003446 status = nfs4_set_lock_state(state, request);
3447 if (status != 0)
3448 goto out;
3449 status = _nfs4_do_setlk(state, cmd, request, 0);
3450 if (status != 0)
3451 goto out;
3452 /* Note: we always want to sleep here! */
3453 request->fl_flags |= FL_SLEEP;
3454 if (do_vfs_lock(request->fl_file, request) < 0)
3455 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 up_read(&clp->cl_sem);
3458 return status;
3459}
3460
3461static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3462{
3463 struct nfs4_exception exception = { };
3464 int err;
3465
3466 do {
3467 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3468 _nfs4_proc_setlk(state, cmd, request),
3469 &exception);
3470 } while (exception.retry);
3471 return err;
3472}
3473
3474static int
3475nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3476{
3477 struct nfs_open_context *ctx;
3478 struct nfs4_state *state;
3479 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3480 int status;
3481
3482 /* verify open state */
3483 ctx = (struct nfs_open_context *)filp->private_data;
3484 state = ctx->state;
3485
3486 if (request->fl_start < 0 || request->fl_end < 0)
3487 return -EINVAL;
3488
3489 if (IS_GETLK(cmd))
3490 return nfs4_proc_getlk(state, F_GETLK, request);
3491
3492 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3493 return -EINVAL;
3494
3495 if (request->fl_type == F_UNLCK)
3496 return nfs4_proc_unlck(state, cmd, request);
3497
3498 do {
3499 status = nfs4_proc_setlk(state, cmd, request);
3500 if ((status != -EAGAIN) || IS_SETLK(cmd))
3501 break;
3502 timeout = nfs4_set_lock_task_retry(timeout);
3503 status = -ERESTARTSYS;
3504 if (signalled())
3505 break;
3506 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return status;
3508}
3509
Trond Myklebust888e6942005-11-04 15:38:11 -05003510int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3511{
3512 struct nfs_server *server = NFS_SERVER(state->inode);
3513 struct nfs4_exception exception = { };
3514 int err;
3515
3516 err = nfs4_set_lock_state(state, fl);
3517 if (err != 0)
3518 goto out;
3519 do {
3520 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3521 if (err != -NFS4ERR_DELAY)
3522 break;
3523 err = nfs4_handle_exception(server, err, &exception);
3524 } while (exception.retry);
3525out:
3526 return err;
3527}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003528
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003529#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3530
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003531int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3532 size_t buflen, int flags)
3533{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003534 struct inode *inode = dentry->d_inode;
3535
3536 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3537 return -EOPNOTSUPP;
3538
3539 if (!S_ISREG(inode->i_mode) &&
3540 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3541 return -EPERM;
3542
3543 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003544}
3545
3546/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3547 * and that's what we'll do for e.g. user attributes that haven't been set.
3548 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3549 * attributes in kernel-managed attribute namespaces. */
3550ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3551 size_t buflen)
3552{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003553 struct inode *inode = dentry->d_inode;
3554
3555 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3556 return -EOPNOTSUPP;
3557
3558 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003559}
3560
3561ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3562{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003563 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003564
3565 if (buf && buflen < len)
3566 return -ERANGE;
3567 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003568 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3569 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003570}
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3573 .recover_open = nfs4_open_reclaim,
3574 .recover_lock = nfs4_lock_reclaim,
3575};
3576
3577struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3578 .recover_open = nfs4_open_expired,
3579 .recover_lock = nfs4_lock_expired,
3580};
3581
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003582static struct inode_operations nfs4_file_inode_operations = {
3583 .permission = nfs_permission,
3584 .getattr = nfs_getattr,
3585 .setattr = nfs_setattr,
3586 .getxattr = nfs4_getxattr,
3587 .setxattr = nfs4_setxattr,
3588 .listxattr = nfs4_listxattr,
3589};
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591struct nfs_rpc_ops nfs_v4_clientops = {
3592 .version = 4, /* protocol version */
3593 .dentry_ops = &nfs4_dentry_operations,
3594 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003595 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .getroot = nfs4_proc_get_root,
3597 .getattr = nfs4_proc_getattr,
3598 .setattr = nfs4_proc_setattr,
3599 .lookup = nfs4_proc_lookup,
3600 .access = nfs4_proc_access,
3601 .readlink = nfs4_proc_readlink,
3602 .read = nfs4_proc_read,
3603 .write = nfs4_proc_write,
3604 .commit = nfs4_proc_commit,
3605 .create = nfs4_proc_create,
3606 .remove = nfs4_proc_remove,
3607 .unlink_setup = nfs4_proc_unlink_setup,
3608 .unlink_done = nfs4_proc_unlink_done,
3609 .rename = nfs4_proc_rename,
3610 .link = nfs4_proc_link,
3611 .symlink = nfs4_proc_symlink,
3612 .mkdir = nfs4_proc_mkdir,
3613 .rmdir = nfs4_proc_remove,
3614 .readdir = nfs4_proc_readdir,
3615 .mknod = nfs4_proc_mknod,
3616 .statfs = nfs4_proc_statfs,
3617 .fsinfo = nfs4_proc_fsinfo,
3618 .pathconf = nfs4_proc_pathconf,
3619 .decode_dirent = nfs4_decode_dirent,
3620 .read_setup = nfs4_proc_read_setup,
3621 .write_setup = nfs4_proc_write_setup,
3622 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003623 .file_open = nfs_open,
3624 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003626 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627};
3628
3629/*
3630 * Local variables:
3631 * c-basic-offset: 8
3632 * End:
3633 */