blob: bad1eae5608ad97adfc639d2333ca07dd9aab465 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Chuck Lever006ea732006-03-20 13:44:14 -050054#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_PROC
57
58#define NFS4_POLL_RETRY_MIN (1*HZ)
59#define NFS4_POLL_RETRY_MAX (15*HZ)
60
Trond Myklebustcdd4e682006-01-03 09:55:12 +010061struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010062static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070066static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Trond Myklebust58d97142006-01-03 09:55:24 +010067static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
69extern struct rpc_procinfo nfs4_procedures[];
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Prevent leaks of NFSv4 errors into userland */
72int nfs4_map_errors(int err)
73{
74 if (err < -1000) {
75 dprintk("%s could not handle NFSv4 error %d\n",
76 __FUNCTION__, -err);
77 return -EIO;
78 }
79 return err;
80}
81
82/*
83 * This is our standard bitmap for GETATTR requests.
84 */
85const u32 nfs4_fattr_bitmap[2] = {
86 FATTR4_WORD0_TYPE
87 | FATTR4_WORD0_CHANGE
88 | FATTR4_WORD0_SIZE
89 | FATTR4_WORD0_FSID
90 | FATTR4_WORD0_FILEID,
91 FATTR4_WORD1_MODE
92 | FATTR4_WORD1_NUMLINKS
93 | FATTR4_WORD1_OWNER
94 | FATTR4_WORD1_OWNER_GROUP
95 | FATTR4_WORD1_RAWDEV
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
100};
101
102const u32 nfs4_statfs_bitmap[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
109};
110
Trond Myklebust4ce79712005-06-22 17:16:21 +0000111const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 FATTR4_WORD0_MAXLINK
113 | FATTR4_WORD0_MAXNAME,
114 0
115};
116
117const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME,
121 0
122};
123
124static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
125 struct nfs4_readdir_arg *readdir)
126{
127 u32 *start, *p;
128
129 BUG_ON(readdir->count < 80);
130 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000131 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
133 return;
134 }
135
136 readdir->cookie = 0;
137 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
138 if (cookie == 2)
139 return;
140
141 /*
142 * NFSv4 servers do not return entries for '.' and '..'
143 * Therefore, we fake these entries here. We let '.'
144 * have cookie 0 and '..' have cookie 1. Note that
145 * when talking to the server, we always send cookie 0
146 * instead of 1 or 2.
147 */
148 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
149
150 if (cookie == 0) {
151 *p++ = xdr_one; /* next */
152 *p++ = xdr_zero; /* cookie, first word */
153 *p++ = xdr_one; /* cookie, second word */
154 *p++ = xdr_one; /* entry len */
155 memcpy(p, ".\0\0\0", 4); /* entry */
156 p++;
157 *p++ = xdr_one; /* bitmap length */
158 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
159 *p++ = htonl(8); /* attribute buffer length */
160 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
161 }
162
163 *p++ = xdr_one; /* next */
164 *p++ = xdr_zero; /* cookie, first word */
165 *p++ = xdr_two; /* cookie, second word */
166 *p++ = xdr_two; /* entry len */
167 memcpy(p, "..\0\0", 4); /* entry */
168 p++;
169 *p++ = xdr_one; /* bitmap length */
170 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
171 *p++ = htonl(8); /* attribute buffer length */
172 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
173
174 readdir->pgbase = (char *)p - (char *)start;
175 readdir->count -= readdir->pgbase;
176 kunmap_atomic(start, KM_USER0);
177}
178
Trond Myklebust26e976a2006-01-03 09:55:21 +0100179static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct nfs4_client *clp = server->nfs4_state;
182 spin_lock(&clp->cl_lock);
183 if (time_before(clp->cl_last_renewal,timestamp))
184 clp->cl_last_renewal = timestamp;
185 spin_unlock(&clp->cl_lock);
186}
187
188static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
189{
190 struct nfs_inode *nfsi = NFS_I(inode);
191
Trond Myklebustdecf4912005-10-27 22:12:39 -0400192 spin_lock(&inode->i_lock);
193 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
195 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400196 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100199struct nfs4_opendata {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100200 atomic_t count;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100201 struct nfs_openargs o_arg;
202 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100203 struct nfs_open_confirmargs c_arg;
204 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100205 struct nfs_fattr f_attr;
206 struct nfs_fattr dir_attr;
207 struct dentry *dentry;
208 struct dentry *dir;
209 struct nfs4_state_owner *owner;
210 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100211 unsigned long timestamp;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100212 int rpc_status;
213 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100214};
215
216static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
217 struct nfs4_state_owner *sp, int flags,
218 const struct iattr *attrs)
219{
220 struct dentry *parent = dget_parent(dentry);
221 struct inode *dir = parent->d_inode;
222 struct nfs_server *server = NFS_SERVER(dir);
223 struct nfs4_opendata *p;
224
225 p = kzalloc(sizeof(*p), GFP_KERNEL);
226 if (p == NULL)
227 goto err;
228 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
229 if (p->o_arg.seqid == NULL)
230 goto err_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100231 atomic_set(&p->count, 1);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100232 p->dentry = dget(dentry);
233 p->dir = parent;
234 p->owner = sp;
235 atomic_inc(&sp->so_count);
236 p->o_arg.fh = NFS_FH(dir);
237 p->o_arg.open_flags = flags,
238 p->o_arg.clientid = server->nfs4_state->cl_clientid;
239 p->o_arg.id = sp->so_id;
240 p->o_arg.name = &dentry->d_name;
241 p->o_arg.server = server;
242 p->o_arg.bitmask = server->attr_bitmask;
243 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
244 p->o_res.f_attr = &p->f_attr;
245 p->o_res.dir_attr = &p->dir_attr;
246 p->o_res.server = server;
247 nfs_fattr_init(&p->f_attr);
248 nfs_fattr_init(&p->dir_attr);
249 if (flags & O_EXCL) {
250 u32 *s = (u32 *) p->o_arg.u.verifier.data;
251 s[0] = jiffies;
252 s[1] = current->pid;
253 } else if (flags & O_CREAT) {
254 p->o_arg.u.attrs = &p->attrs;
255 memcpy(&p->attrs, attrs, sizeof(p->attrs));
256 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100257 p->c_arg.fh = &p->o_res.fh;
258 p->c_arg.stateid = &p->o_res.stateid;
259 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100260 return p;
261err_free:
262 kfree(p);
263err:
264 dput(parent);
265 return NULL;
266}
267
268static void nfs4_opendata_free(struct nfs4_opendata *p)
269{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100270 if (p != NULL && atomic_dec_and_test(&p->count)) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100271 nfs_free_seqid(p->o_arg.seqid);
272 nfs4_put_state_owner(p->owner);
273 dput(p->dir);
274 dput(p->dentry);
275 kfree(p);
276 }
277}
278
Trond Myklebust95121352005-10-18 14:20:12 -0700279/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100280static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100281 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700282{
283 struct rpc_task *task;
284
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100285 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700286 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700287 rpc_execute(task);
288 return 0;
289}
290
Trond Myklebust06f814a2006-01-03 09:55:07 +0100291static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
292{
293 sigset_t oldset;
294 int ret;
295
296 rpc_clnt_sigmask(task->tk_client, &oldset);
297 ret = rpc_wait_for_completion_task(task);
298 rpc_clnt_sigunmask(task->tk_client, &oldset);
299 return ret;
300}
301
Trond Myklebuste7616922006-01-03 09:55:13 +0100302static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
303{
304 switch (open_flags) {
305 case FMODE_WRITE:
306 state->n_wronly++;
307 break;
308 case FMODE_READ:
309 state->n_rdonly++;
310 break;
311 case FMODE_READ|FMODE_WRITE:
312 state->n_rdwr++;
313 }
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
317{
318 struct inode *inode = state->inode;
319
320 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500321 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700322 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500324 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100325 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500326 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700328 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100331static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
332{
333 struct inode *inode;
334 struct nfs4_state *state = NULL;
335
336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
337 goto out;
338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
339 if (inode == NULL)
340 goto out;
341 state = nfs4_get_open_state(inode, data->owner);
342 if (state == NULL)
343 goto put_inode;
344 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
345put_inode:
346 iput(inode);
347out:
348 return state;
349}
350
Trond Myklebust864472e2006-01-03 09:55:15 +0100351static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
352{
353 struct nfs_inode *nfsi = NFS_I(state->inode);
354 struct nfs_open_context *ctx;
355
356 spin_lock(&state->inode->i_lock);
357 list_for_each_entry(ctx, &nfsi->open_files, list) {
358 if (ctx->state != state)
359 continue;
360 get_nfs_open_context(ctx);
361 spin_unlock(&state->inode->i_lock);
362 return ctx;
363 }
364 spin_unlock(&state->inode->i_lock);
365 return ERR_PTR(-ENOENT);
366}
367
368static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
369{
370 int ret;
371
372 opendata->o_arg.open_flags = openflags;
373 ret = _nfs4_proc_open(opendata);
374 if (ret != 0)
375 return ret;
376 memcpy(stateid->data, opendata->o_res.stateid.data,
377 sizeof(stateid->data));
378 return 0;
379}
380
381static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
382{
383 nfs4_stateid stateid;
384 struct nfs4_state *newstate;
385 int mode = 0;
386 int delegation = 0;
387 int ret;
388
389 /* memory barrier prior to reading state->n_* */
390 smp_rmb();
391 if (state->n_rdwr != 0) {
392 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
393 if (ret != 0)
394 return ret;
395 mode |= FMODE_READ|FMODE_WRITE;
396 if (opendata->o_res.delegation_type != 0)
397 delegation = opendata->o_res.delegation_type;
398 smp_rmb();
399 }
400 if (state->n_wronly != 0) {
401 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
402 if (ret != 0)
403 return ret;
404 mode |= FMODE_WRITE;
405 if (opendata->o_res.delegation_type != 0)
406 delegation = opendata->o_res.delegation_type;
407 smp_rmb();
408 }
409 if (state->n_rdonly != 0) {
410 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
411 if (ret != 0)
412 return ret;
413 mode |= FMODE_READ;
414 }
415 clear_bit(NFS_DELEGATED_STATE, &state->flags);
416 if (mode == 0)
417 return 0;
418 if (opendata->o_res.delegation_type == 0)
419 opendata->o_res.delegation_type = delegation;
420 opendata->o_arg.open_flags |= mode;
421 newstate = nfs4_opendata_to_nfs4_state(opendata);
422 if (newstate != NULL) {
423 if (opendata->o_res.delegation_type != 0) {
424 struct nfs_inode *nfsi = NFS_I(newstate->inode);
425 int delegation_flags = 0;
426 if (nfsi->delegation)
427 delegation_flags = nfsi->delegation->flags;
428 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
429 nfs_inode_set_delegation(newstate->inode,
430 opendata->owner->so_cred,
431 &opendata->o_res);
432 else
433 nfs_inode_reclaim_delegation(newstate->inode,
434 opendata->owner->so_cred,
435 &opendata->o_res);
436 }
437 nfs4_close_state(newstate, opendata->o_arg.open_flags);
438 }
439 if (newstate != state)
440 return -ESTALE;
441 return 0;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * OPEN_RECLAIM:
446 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Trond Myklebust864472e2006-01-03 09:55:15 +0100448static int _nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Trond Myklebust864472e2006-01-03 09:55:15 +0100450 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
451 struct nfs4_opendata *opendata;
452 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int status;
454
455 if (delegation != NULL) {
456 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
457 memcpy(&state->stateid, &delegation->stateid,
458 sizeof(state->stateid));
459 set_bit(NFS_DELEGATED_STATE, &state->flags);
460 return 0;
461 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100462 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100464 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
465 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700466 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100467 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
468 opendata->o_arg.fh = NFS_FH(state->inode);
469 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
470 opendata->o_arg.u.delegation_type = delegation_type;
471 status = nfs4_open_recover(opendata, state);
472 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return status;
474}
475
Trond Myklebust864472e2006-01-03 09:55:15 +0100476static int nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct nfs_server *server = NFS_SERVER(state->inode);
479 struct nfs4_exception exception = { };
480 int err;
481 do {
Trond Myklebust864472e2006-01-03 09:55:15 +0100482 err = _nfs4_do_open_reclaim(sp, state, dentry);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000483 if (err != -NFS4ERR_DELAY)
484 break;
485 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } while (exception.retry);
487 return err;
488}
489
Trond Myklebust864472e2006-01-03 09:55:15 +0100490static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
491{
492 struct nfs_open_context *ctx;
493 int ret;
494
495 ctx = nfs4_state_find_open_context(state);
496 if (IS_ERR(ctx))
497 return PTR_ERR(ctx);
498 ret = nfs4_do_open_reclaim(sp, state, ctx->dentry);
499 put_nfs_open_context(ctx);
500 return ret;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
504{
505 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100506 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100507 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust864472e2006-01-03 09:55:15 +0100510 return 0;
511 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100512 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100513 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100514 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100515 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
516 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100517 ret = nfs4_open_recover(opendata, state);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100518 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
523{
524 struct nfs4_exception exception = { };
525 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
526 int err;
527 do {
528 err = _nfs4_open_delegation_recall(dentry, state);
529 switch (err) {
530 case 0:
531 return err;
532 case -NFS4ERR_STALE_CLIENTID:
533 case -NFS4ERR_STALE_STATEID:
534 case -NFS4ERR_EXPIRED:
535 /* Don't recall a delegation if it was lost */
536 nfs4_schedule_state_recovery(server->nfs4_state);
537 return err;
538 }
539 err = nfs4_handle_exception(server, err, &exception);
540 } while (exception.retry);
541 return err;
542}
543
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100544static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100546 struct nfs4_opendata *data = calldata;
547 struct rpc_message msg = {
548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
549 .rpc_argp = &data->c_arg,
550 .rpc_resp = &data->c_res,
551 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100553 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100554 rpc_call_setup(task, &msg, 0);
555}
556
557static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
558{
559 struct nfs4_opendata *data = calldata;
560
561 data->rpc_status = task->tk_status;
562 if (RPC_ASSASSINATED(task))
563 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100564 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100565 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
566 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100567 renew_lease(data->o_res.server, data->timestamp);
568 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100569 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
570 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
571}
572
573static void nfs4_open_confirm_release(void *calldata)
574{
575 struct nfs4_opendata *data = calldata;
576 struct nfs4_state *state = NULL;
577
578 /* If this request hasn't been cancelled, do nothing */
579 if (data->cancelled == 0)
580 goto out_free;
581 /* In case of error, no cleanup! */
582 if (data->rpc_status != 0)
583 goto out_free;
584 nfs_confirm_seqid(&data->owner->so_seqid, 0);
585 state = nfs4_opendata_to_nfs4_state(data);
586 if (state != NULL)
587 nfs4_close_state(state, data->o_arg.open_flags);
588out_free:
589 nfs4_opendata_free(data);
590}
591
592static const struct rpc_call_ops nfs4_open_confirm_ops = {
593 .rpc_call_prepare = nfs4_open_confirm_prepare,
594 .rpc_call_done = nfs4_open_confirm_done,
595 .rpc_release = nfs4_open_confirm_release,
596};
597
598/*
599 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
600 */
601static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
602{
603 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
604 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int status;
606
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100607 atomic_inc(&data->count);
608 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
609 if (IS_ERR(task)) {
610 nfs4_opendata_free(data);
611 return PTR_ERR(task);
612 }
613 status = nfs4_wait_for_completion_rpc_task(task);
614 if (status != 0) {
615 data->cancelled = 1;
616 smp_wmb();
617 } else
618 status = data->rpc_status;
619 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return status;
621}
622
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100623static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100625 struct nfs4_opendata *data = calldata;
626 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct rpc_message msg = {
628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100629 .rpc_argp = &data->o_arg,
630 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .rpc_cred = sp->so_cred,
632 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100633
634 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
635 return;
636 /* Update sequence id. */
637 data->o_arg.id = sp->so_id;
638 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100639 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
640 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100641 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100642 rpc_call_setup(task, &msg, 0);
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100645static void nfs4_open_done(struct rpc_task *task, void *calldata)
646{
647 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100649 data->rpc_status = task->tk_status;
650 if (RPC_ASSASSINATED(task))
651 return;
652 if (task->tk_status == 0) {
653 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700654 case S_IFREG:
655 break;
656 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100657 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700658 break;
659 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100660 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700661 break;
662 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100663 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700664 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100665 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700666 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100667 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
668}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700669
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100670static void nfs4_open_release(void *calldata)
671{
672 struct nfs4_opendata *data = calldata;
673 struct nfs4_state *state = NULL;
674
675 /* If this request hasn't been cancelled, do nothing */
676 if (data->cancelled == 0)
677 goto out_free;
678 /* In case of error, no cleanup! */
679 if (data->rpc_status != 0)
680 goto out_free;
681 /* In case we need an open_confirm, no cleanup! */
682 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
683 goto out_free;
684 nfs_confirm_seqid(&data->owner->so_seqid, 0);
685 state = nfs4_opendata_to_nfs4_state(data);
686 if (state != NULL)
687 nfs4_close_state(state, data->o_arg.open_flags);
688out_free:
689 nfs4_opendata_free(data);
690}
691
692static const struct rpc_call_ops nfs4_open_ops = {
693 .rpc_call_prepare = nfs4_open_prepare,
694 .rpc_call_done = nfs4_open_done,
695 .rpc_release = nfs4_open_release,
696};
697
698/*
699 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
700 */
701static int _nfs4_proc_open(struct nfs4_opendata *data)
702{
703 struct inode *dir = data->dir->d_inode;
704 struct nfs_server *server = NFS_SERVER(dir);
705 struct nfs_openargs *o_arg = &data->o_arg;
706 struct nfs_openres *o_res = &data->o_res;
707 struct rpc_task *task;
708 int status;
709
710 atomic_inc(&data->count);
711 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
712 if (IS_ERR(task)) {
713 nfs4_opendata_free(data);
714 return PTR_ERR(task);
715 }
716 status = nfs4_wait_for_completion_rpc_task(task);
717 if (status != 0) {
718 data->cancelled = 1;
719 smp_wmb();
720 } else
721 status = data->rpc_status;
722 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100724 return status;
725
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400726 if (o_arg->open_flags & O_CREAT) {
727 update_changeattr(dir, &o_res->cinfo);
728 nfs_post_op_update_inode(dir, o_res->dir_attr);
729 } else
730 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100732 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100734 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100736 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100738 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
743{
744 struct nfs_access_entry cache;
745 int mask = 0;
746 int status;
747
748 if (openflags & FMODE_READ)
749 mask |= MAY_READ;
750 if (openflags & FMODE_WRITE)
751 mask |= MAY_WRITE;
752 status = nfs_access_get_cached(inode, cred, &cache);
753 if (status == 0)
754 goto out;
755
756 /* Be clever: ask server to check for all possible rights */
757 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
758 cache.cred = cred;
759 cache.jiffies = jiffies;
760 status = _nfs4_proc_access(inode, &cache);
761 if (status != 0)
762 return status;
763 nfs_access_add_cache(inode, &cache);
764out:
765 if ((cache.mask & mask) == mask)
766 return 0;
767 return -EACCES;
768}
769
Trond Myklebust58d97142006-01-03 09:55:24 +0100770int nfs4_recover_expired_lease(struct nfs_server *server)
771{
772 struct nfs4_client *clp = server->nfs4_state;
773
774 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
775 nfs4_schedule_state_recovery(clp);
776 return nfs4_wait_clnt_recover(server->client, clp);
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*
780 * OPEN_EXPIRED:
781 * reclaim state on the server after a network partition.
782 * Assumes caller holds the appropriate lock
783 */
784static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100789 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100790 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100793 ret = _nfs4_do_access(inode, sp->so_cred, openflags);
794 if (ret < 0)
795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
797 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100800 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100802 return -ENOMEM;
803 ret = nfs4_open_recover(opendata, state);
804 if (ret == -ESTALE) {
805 /* Invalidate the state owner so we don't ever use it again */
806 nfs4_drop_state_owner(sp);
807 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100810 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Trond Myklebust202b50d2005-06-22 17:16:29 +0000813static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
814{
815 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
816 struct nfs4_exception exception = { };
817 int err;
818
819 do {
820 err = _nfs4_open_expired(sp, state, dentry);
821 if (err == -NFS4ERR_DELAY)
822 nfs4_handle_exception(server, err, &exception);
823 } while (exception.retry);
824 return err;
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100830 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Trond Myklebust864472e2006-01-03 09:55:15 +0100832 ctx = nfs4_state_find_open_context(state);
833 if (IS_ERR(ctx))
834 return PTR_ERR(ctx);
835 ret = nfs4_do_open_expired(sp, state, ctx->dentry);
836 put_nfs_open_context(ctx);
837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100841 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
843static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
844{
845 struct nfs_delegation *delegation;
846 struct nfs_server *server = NFS_SERVER(inode);
847 struct nfs4_client *clp = server->nfs4_state;
848 struct nfs_inode *nfsi = NFS_I(inode);
849 struct nfs4_state_owner *sp = NULL;
850 struct nfs4_state *state = NULL;
851 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
852 int err;
853
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100854 err = -ENOMEM;
855 if (!(sp = nfs4_get_state_owner(server, cred))) {
856 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
857 return err;
858 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100859 err = nfs4_recover_expired_lease(server);
860 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100861 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* Protect against reboot recovery - NOTE ORDER! */
863 down_read(&clp->cl_sem);
864 /* Protect against delegation recall */
865 down_read(&nfsi->rwsem);
866 delegation = NFS_I(inode)->delegation;
867 err = -ENOENT;
868 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
869 goto out_err;
870 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 state = nfs4_get_open_state(inode, sp);
872 if (state == NULL)
873 goto out_err;
874
875 err = -ENOENT;
876 if ((state->state & open_flags) == open_flags) {
877 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100878 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 spin_unlock(&inode->i_lock);
880 goto out_ok;
881 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100882 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 lock_kernel();
885 err = _nfs4_do_access(inode, cred, open_flags);
886 unlock_kernel();
887 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100888 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 set_bit(NFS_DELEGATED_STATE, &state->flags);
890 update_open_stateid(state, &delegation->stateid, open_flags);
891out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 nfs4_put_state_owner(sp);
893 up_read(&nfsi->rwsem);
894 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100896 return 0;
897out_put_open_state:
898 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 up_read(&nfsi->rwsem);
901 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700902 if (err != -EACCES)
903 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100904out_put_state_owner:
905 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return err;
907}
908
909static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
910{
911 struct nfs4_exception exception = { };
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500912 struct nfs4_state *res = ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int err;
914
915 do {
916 err = _nfs4_open_delegated(inode, flags, cred, &res);
917 if (err == 0)
918 break;
919 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
920 err, &exception));
921 } while (exception.retry);
922 return res;
923}
924
925/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100926 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
928static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
929{
930 struct nfs4_state_owner *sp;
931 struct nfs4_state *state = NULL;
932 struct nfs_server *server = NFS_SERVER(dir);
933 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100934 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 status = -ENOMEM;
939 if (!(sp = nfs4_get_state_owner(server, cred))) {
940 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
941 goto out_err;
942 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100943 status = nfs4_recover_expired_lease(server);
944 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100945 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100946 down_read(&clp->cl_sem);
947 status = -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100948 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
949 if (opendata == NULL)
950 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100952 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (status != 0)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100954 goto err_opendata_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100957 state = nfs4_opendata_to_nfs4_state(opendata);
958 if (state == NULL)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 goto err_opendata_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100961 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100962 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 nfs4_put_state_owner(sp);
964 up_read(&clp->cl_sem);
965 *res = state;
966 return 0;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967err_opendata_free:
968 nfs4_opendata_free(opendata);
969err_put_state_owner:
970 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
973 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 *res = NULL;
975 return status;
976}
977
978
979static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
980{
981 struct nfs4_exception exception = { };
982 struct nfs4_state *res;
983 int status;
984
985 do {
986 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
987 if (status == 0)
988 break;
989 /* NOTE: BAD_SEQID means the server and client disagree about the
990 * book-keeping w.r.t. state-changing operations
991 * (OPEN/CLOSE/LOCK/LOCKU...)
992 * It is actually a sign of a bug on the client or on the server.
993 *
994 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700995 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 * have unhashed the old state_owner for us, and that we can
997 * therefore safely retry using a new one. We should still warn
998 * the user though...
999 */
1000 if (status == -NFS4ERR_BAD_SEQID) {
1001 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1002 exception.retry = 1;
1003 continue;
1004 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001005 /*
1006 * BAD_STATEID on OPEN means that the server cancelled our
1007 * state before it received the OPEN_CONFIRM.
1008 * Recover by retrying the request as per the discussion
1009 * on Page 181 of RFC3530.
1010 */
1011 if (status == -NFS4ERR_BAD_STATEID) {
1012 exception.retry = 1;
1013 continue;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1016 status, &exception));
1017 } while (exception.retry);
1018 return res;
1019}
1020
1021static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1022 struct nfs_fh *fhandle, struct iattr *sattr,
1023 struct nfs4_state *state)
1024{
1025 struct nfs_setattrargs arg = {
1026 .fh = fhandle,
1027 .iap = sattr,
1028 .server = server,
1029 .bitmask = server->attr_bitmask,
1030 };
1031 struct nfs_setattrres res = {
1032 .fattr = fattr,
1033 .server = server,
1034 };
1035 struct rpc_message msg = {
1036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1037 .rpc_argp = &arg,
1038 .rpc_resp = &res,
1039 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001040 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001041 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Trond Myklebust0e574af2005-10-27 22:12:38 -04001043 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001045 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001047 nfs4_copy_stateid(&arg.stateid, state, current->files);
1048 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1050
Trond Myklebust65e43082005-08-16 11:49:44 -04001051 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001052 if (status == 0 && state != NULL)
1053 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001054 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1058 struct nfs_fh *fhandle, struct iattr *sattr,
1059 struct nfs4_state *state)
1060{
1061 struct nfs4_exception exception = { };
1062 int err;
1063 do {
1064 err = nfs4_handle_exception(server,
1065 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1066 state),
1067 &exception);
1068 } while (exception.retry);
1069 return err;
1070}
1071
1072struct nfs4_closedata {
1073 struct inode *inode;
1074 struct nfs4_state *state;
1075 struct nfs_closeargs arg;
1076 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001077 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001078 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079};
1080
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001081static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001082{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001083 struct nfs4_closedata *calldata = data;
1084 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001085
1086 nfs4_put_open_state(calldata->state);
1087 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001088 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001089 kfree(calldata);
1090}
1091
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001092static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001094 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct nfs_server *server = NFS_SERVER(calldata->inode);
1097
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001098 if (RPC_ASSASSINATED(task))
1099 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* hmm. we are done with the inode, and in the process of freeing
1101 * the state_owner. we keep this around to process errors
1102 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001103 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 switch (task->tk_status) {
1105 case 0:
1106 memcpy(&state->stateid, &calldata->res.stateid,
1107 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001108 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 break;
1110 case -NFS4ERR_STALE_STATEID:
1111 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 nfs4_schedule_state_recovery(server->nfs4_state);
1113 break;
1114 default:
1115 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1116 rpc_restart_call(task);
1117 return;
1118 }
1119 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001120 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001123static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001125 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001126 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct rpc_message msg = {
1128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1129 .rpc_argp = &calldata->arg,
1130 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001131 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001133 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001134
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001135 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001136 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001137 /* Recalculate the new open mode in case someone reopened the file
1138 * while we were waiting in line to be scheduled.
1139 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001140 spin_lock(&state->owner->so_lock);
1141 spin_lock(&calldata->inode->i_lock);
1142 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001143 if (state->n_rdwr == 0) {
1144 if (state->n_rdonly == 0)
1145 mode &= ~FMODE_READ;
1146 if (state->n_wronly == 0)
1147 mode &= ~FMODE_WRITE;
1148 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001149 nfs4_state_set_mode_locked(state, mode);
1150 spin_unlock(&calldata->inode->i_lock);
1151 spin_unlock(&state->owner->so_lock);
1152 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001153 /* Note: exit _without_ calling nfs4_close_done */
1154 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001155 return;
1156 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001157 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001158 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001160 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001161 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001162 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001165static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001166 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001167 .rpc_call_done = nfs4_close_done,
1168 .rpc_release = nfs4_free_closedata,
1169};
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/*
1172 * It is possible for data to be read/written from a mem-mapped file
1173 * after the sys_close call (which hits the vfs layer as a flush).
1174 * This means that we can't safely call nfsv4 close on a file until
1175 * the inode is cleared. This in turn means that we are not good
1176 * NFSv4 citizens - we do not indicate to the server to update the file's
1177 * share state even when we are done with one of the three share
1178 * stateid's in the inode.
1179 *
1180 * NOTE: Caller must be holding the sp->so_owner semaphore!
1181 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001182int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Trond Myklebust516a6af2005-10-27 22:12:41 -04001184 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -07001186 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Trond Myklebust95121352005-10-18 14:20:12 -07001188 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 calldata->inode = inode;
1192 calldata->state = state;
1193 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001194 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001196 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001197 if (calldata->arg.seqid == NULL)
1198 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001199 calldata->arg.bitmask = server->attr_bitmask;
1200 calldata->res.fattr = &calldata->fattr;
1201 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -07001202
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001203 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -07001204 if (status == 0)
1205 goto out;
1206
1207 nfs_free_seqid(calldata->arg.seqid);
1208out_free_calldata:
1209 kfree(calldata);
1210out:
1211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Trond Myklebust02a913a2005-10-18 14:20:17 -07001214static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1215{
1216 struct file *filp;
1217
1218 filp = lookup_instantiate_filp(nd, dentry, NULL);
1219 if (!IS_ERR(filp)) {
1220 struct nfs_open_context *ctx;
1221 ctx = (struct nfs_open_context *)filp->private_data;
1222 ctx->state = state;
1223 } else
1224 nfs4_close_state(state, nd->intent.open.flags);
1225}
1226
1227struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1229{
1230 struct iattr attr;
1231 struct rpc_cred *cred;
1232 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001233 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (nd->flags & LOOKUP_CREATE) {
1236 attr.ia_mode = nd->intent.open.create_mode;
1237 attr.ia_valid = ATTR_MODE;
1238 if (!IS_POSIXACL(dir))
1239 attr.ia_mode &= ~current->fs->umask;
1240 } else {
1241 attr.ia_valid = 0;
1242 BUG_ON(nd->intent.open.flags & O_CREAT);
1243 }
1244
1245 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1246 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001247 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1249 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001250 if (IS_ERR(state)) {
1251 if (PTR_ERR(state) == -ENOENT)
1252 d_add(dentry, NULL);
1253 return (struct dentry *)state;
1254 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001255 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001256 if (res != NULL)
1257 dentry = res;
1258 nfs4_intent_set_file(nd, dentry, state);
1259 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001263nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct rpc_cred *cred;
1266 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1269 if (IS_ERR(cred))
1270 return PTR_ERR(cred);
1271 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1272 if (IS_ERR(state))
1273 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1274 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001275 if (IS_ERR(state)) {
1276 switch (PTR_ERR(state)) {
1277 case -EPERM:
1278 case -EACCES:
1279 case -EDQUOT:
1280 case -ENOSPC:
1281 case -EROFS:
1282 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1283 return 1;
1284 case -ENOENT:
1285 if (dentry->d_inode == NULL)
1286 return 1;
1287 }
1288 goto out_drop;
1289 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 if (state->inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001291 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 1;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001295out_drop:
1296 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298}
1299
1300
1301static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1302{
1303 struct nfs4_server_caps_res res = {};
1304 struct rpc_message msg = {
1305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1306 .rpc_argp = fhandle,
1307 .rpc_resp = &res,
1308 };
1309 int status;
1310
1311 status = rpc_call_sync(server->client, &msg, 0);
1312 if (status == 0) {
1313 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1314 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1315 server->caps |= NFS_CAP_ACLS;
1316 if (res.has_links != 0)
1317 server->caps |= NFS_CAP_HARDLINKS;
1318 if (res.has_symlinks != 0)
1319 server->caps |= NFS_CAP_SYMLINKS;
1320 server->acl_bitmask = res.acl_bitmask;
1321 }
1322 return status;
1323}
1324
1325static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1326{
1327 struct nfs4_exception exception = { };
1328 int err;
1329 do {
1330 err = nfs4_handle_exception(server,
1331 _nfs4_server_capabilities(server, fhandle),
1332 &exception);
1333 } while (exception.retry);
1334 return err;
1335}
1336
1337static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1338 struct nfs_fsinfo *info)
1339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct nfs4_lookup_root_arg args = {
1341 .bitmask = nfs4_fattr_bitmap,
1342 };
1343 struct nfs4_lookup_res res = {
1344 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001345 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 .fh = fhandle,
1347 };
1348 struct rpc_message msg = {
1349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1350 .rpc_argp = &args,
1351 .rpc_resp = &res,
1352 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001353 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return rpc_call_sync(server->client, &msg, 0);
1355}
1356
1357static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1358 struct nfs_fsinfo *info)
1359{
1360 struct nfs4_exception exception = { };
1361 int err;
1362 do {
1363 err = nfs4_handle_exception(server,
1364 _nfs4_lookup_root(server, fhandle, info),
1365 &exception);
1366 } while (exception.retry);
1367 return err;
1368}
1369
1370static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1371 struct nfs_fsinfo *info)
1372{
1373 struct nfs_fattr * fattr = info->fattr;
1374 unsigned char * p;
1375 struct qstr q;
1376 struct nfs4_lookup_arg args = {
1377 .dir_fh = fhandle,
1378 .name = &q,
1379 .bitmask = nfs4_fattr_bitmap,
1380 };
1381 struct nfs4_lookup_res res = {
1382 .server = server,
1383 .fattr = fattr,
1384 .fh = fhandle,
1385 };
1386 struct rpc_message msg = {
1387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1388 .rpc_argp = &args,
1389 .rpc_resp = &res,
1390 };
1391 int status;
1392
1393 /*
1394 * Now we do a separate LOOKUP for each component of the mount path.
1395 * The LOOKUPs are done separately so that we can conveniently
1396 * catch an ERR_WRONGSEC if it occurs along the way...
1397 */
1398 status = nfs4_lookup_root(server, fhandle, info);
1399 if (status)
1400 goto out;
1401
1402 p = server->mnt_path;
1403 for (;;) {
1404 struct nfs4_exception exception = { };
1405
1406 while (*p == '/')
1407 p++;
1408 if (!*p)
1409 break;
1410 q.name = p;
1411 while (*p && (*p != '/'))
1412 p++;
1413 q.len = p - q.name;
1414
1415 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001416 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 status = nfs4_handle_exception(server,
1418 rpc_call_sync(server->client, &msg, 0),
1419 &exception);
1420 } while (exception.retry);
1421 if (status == 0)
1422 continue;
1423 if (status == -ENOENT) {
1424 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1425 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1426 }
1427 break;
1428 }
1429 if (status == 0)
1430 status = nfs4_server_capabilities(server, fhandle);
1431 if (status == 0)
1432 status = nfs4_do_fsinfo(server, fhandle, info);
1433out:
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001434 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1438{
1439 struct nfs4_getattr_arg args = {
1440 .fh = fhandle,
1441 .bitmask = server->attr_bitmask,
1442 };
1443 struct nfs4_getattr_res res = {
1444 .fattr = fattr,
1445 .server = server,
1446 };
1447 struct rpc_message msg = {
1448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1449 .rpc_argp = &args,
1450 .rpc_resp = &res,
1451 };
1452
Trond Myklebust0e574af2005-10-27 22:12:38 -04001453 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return rpc_call_sync(server->client, &msg, 0);
1455}
1456
1457static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1458{
1459 struct nfs4_exception exception = { };
1460 int err;
1461 do {
1462 err = nfs4_handle_exception(server,
1463 _nfs4_proc_getattr(server, fhandle, fattr),
1464 &exception);
1465 } while (exception.retry);
1466 return err;
1467}
1468
1469/*
1470 * The file is not closed if it is opened due to the a request to change
1471 * the size of the file. The open call will not be needed once the
1472 * VFS layer lookup-intents are implemented.
1473 *
1474 * Close is called when the inode is destroyed.
1475 * If we haven't opened the file for O_WRONLY, we
1476 * need to in the size_change case to obtain a stateid.
1477 *
1478 * Got race?
1479 * Because OPEN is always done by name in nfsv4, it is
1480 * possible that we opened a different file by the same
1481 * name. We can recognize this race condition, but we
1482 * can't do anything about it besides returning an error.
1483 *
1484 * This will be fixed with VFS changes (lookup-intent).
1485 */
1486static int
1487nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1488 struct iattr *sattr)
1489{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001490 struct rpc_cred *cred;
1491 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001492 struct nfs_open_context *ctx;
1493 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int status;
1495
Trond Myklebust0e574af2005-10-27 22:12:38 -04001496 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001498 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1499 if (IS_ERR(cred))
1500 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001501
1502 /* Search for an existing open(O_WRITE) file */
1503 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1504 if (ctx != NULL)
1505 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1508 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001509 if (status == 0)
1510 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001511 if (ctx != NULL)
1512 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001513 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return status;
1515}
1516
1517static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1518 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1519{
1520 int status;
1521 struct nfs_server *server = NFS_SERVER(dir);
1522 struct nfs4_lookup_arg args = {
1523 .bitmask = server->attr_bitmask,
1524 .dir_fh = NFS_FH(dir),
1525 .name = name,
1526 };
1527 struct nfs4_lookup_res res = {
1528 .server = server,
1529 .fattr = fattr,
1530 .fh = fhandle,
1531 };
1532 struct rpc_message msg = {
1533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1534 .rpc_argp = &args,
1535 .rpc_resp = &res,
1536 };
1537
Trond Myklebust0e574af2005-10-27 22:12:38 -04001538 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 dprintk("NFS call lookup %s\n", name->name);
1541 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1542 dprintk("NFS reply lookup: %d\n", status);
1543 return status;
1544}
1545
1546static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1547{
1548 struct nfs4_exception exception = { };
1549 int err;
1550 do {
1551 err = nfs4_handle_exception(NFS_SERVER(dir),
1552 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1553 &exception);
1554 } while (exception.retry);
1555 return err;
1556}
1557
1558static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1559{
1560 struct nfs4_accessargs args = {
1561 .fh = NFS_FH(inode),
1562 };
1563 struct nfs4_accessres res = { 0 };
1564 struct rpc_message msg = {
1565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1566 .rpc_argp = &args,
1567 .rpc_resp = &res,
1568 .rpc_cred = entry->cred,
1569 };
1570 int mode = entry->mask;
1571 int status;
1572
1573 /*
1574 * Determine which access bits we want to ask for...
1575 */
1576 if (mode & MAY_READ)
1577 args.access |= NFS4_ACCESS_READ;
1578 if (S_ISDIR(inode->i_mode)) {
1579 if (mode & MAY_WRITE)
1580 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1581 if (mode & MAY_EXEC)
1582 args.access |= NFS4_ACCESS_LOOKUP;
1583 } else {
1584 if (mode & MAY_WRITE)
1585 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1586 if (mode & MAY_EXEC)
1587 args.access |= NFS4_ACCESS_EXECUTE;
1588 }
1589 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1590 if (!status) {
1591 entry->mask = 0;
1592 if (res.access & NFS4_ACCESS_READ)
1593 entry->mask |= MAY_READ;
1594 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1595 entry->mask |= MAY_WRITE;
1596 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1597 entry->mask |= MAY_EXEC;
1598 }
1599 return status;
1600}
1601
1602static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1603{
1604 struct nfs4_exception exception = { };
1605 int err;
1606 do {
1607 err = nfs4_handle_exception(NFS_SERVER(inode),
1608 _nfs4_proc_access(inode, entry),
1609 &exception);
1610 } while (exception.retry);
1611 return err;
1612}
1613
1614/*
1615 * TODO: For the time being, we don't try to get any attributes
1616 * along with any of the zero-copy operations READ, READDIR,
1617 * READLINK, WRITE.
1618 *
1619 * In the case of the first three, we want to put the GETATTR
1620 * after the read-type operation -- this is because it is hard
1621 * to predict the length of a GETATTR response in v4, and thus
1622 * align the READ data correctly. This means that the GETATTR
1623 * may end up partially falling into the page cache, and we should
1624 * shift it into the 'tail' of the xdr_buf before processing.
1625 * To do this efficiently, we need to know the total length
1626 * of data received, which doesn't seem to be available outside
1627 * of the RPC layer.
1628 *
1629 * In the case of WRITE, we also want to put the GETATTR after
1630 * the operation -- in this case because we want to make sure
1631 * we get the post-operation mtime and size. This means that
1632 * we can't use xdr_encode_pages() as written: we need a variant
1633 * of it which would leave room in the 'tail' iovec.
1634 *
1635 * Both of these changes to the XDR layer would in fact be quite
1636 * minor, but I decided to leave them for a subsequent patch.
1637 */
1638static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1639 unsigned int pgbase, unsigned int pglen)
1640{
1641 struct nfs4_readlink args = {
1642 .fh = NFS_FH(inode),
1643 .pgbase = pgbase,
1644 .pglen = pglen,
1645 .pages = &page,
1646 };
1647 struct rpc_message msg = {
1648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1649 .rpc_argp = &args,
1650 .rpc_resp = NULL,
1651 };
1652
1653 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1654}
1655
1656static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1657 unsigned int pgbase, unsigned int pglen)
1658{
1659 struct nfs4_exception exception = { };
1660 int err;
1661 do {
1662 err = nfs4_handle_exception(NFS_SERVER(inode),
1663 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1664 &exception);
1665 } while (exception.retry);
1666 return err;
1667}
1668
1669static int _nfs4_proc_read(struct nfs_read_data *rdata)
1670{
1671 int flags = rdata->flags;
1672 struct inode *inode = rdata->inode;
1673 struct nfs_fattr *fattr = rdata->res.fattr;
1674 struct nfs_server *server = NFS_SERVER(inode);
1675 struct rpc_message msg = {
1676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1677 .rpc_argp = &rdata->args,
1678 .rpc_resp = &rdata->res,
1679 .rpc_cred = rdata->cred,
1680 };
1681 unsigned long timestamp = jiffies;
1682 int status;
1683
1684 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1685 (long long) rdata->args.offset);
1686
Trond Myklebust0e574af2005-10-27 22:12:38 -04001687 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 status = rpc_call_sync(server->client, &msg, flags);
1689 if (!status)
1690 renew_lease(server, timestamp);
1691 dprintk("NFS reply read: %d\n", status);
1692 return status;
1693}
1694
1695static int nfs4_proc_read(struct nfs_read_data *rdata)
1696{
1697 struct nfs4_exception exception = { };
1698 int err;
1699 do {
1700 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1701 _nfs4_proc_read(rdata),
1702 &exception);
1703 } while (exception.retry);
1704 return err;
1705}
1706
1707static int _nfs4_proc_write(struct nfs_write_data *wdata)
1708{
1709 int rpcflags = wdata->flags;
1710 struct inode *inode = wdata->inode;
1711 struct nfs_fattr *fattr = wdata->res.fattr;
1712 struct nfs_server *server = NFS_SERVER(inode);
1713 struct rpc_message msg = {
1714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1715 .rpc_argp = &wdata->args,
1716 .rpc_resp = &wdata->res,
1717 .rpc_cred = wdata->cred,
1718 };
1719 int status;
1720
1721 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1722 (long long) wdata->args.offset);
1723
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001724 wdata->args.bitmask = server->attr_bitmask;
1725 wdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001726 wdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001727 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 status = rpc_call_sync(server->client, &msg, rpcflags);
1729 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001730 if (status < 0)
1731 return status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001732 renew_lease(server, wdata->timestamp);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001733 nfs_post_op_update_inode(inode, fattr);
1734 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737static int nfs4_proc_write(struct nfs_write_data *wdata)
1738{
1739 struct nfs4_exception exception = { };
1740 int err;
1741 do {
1742 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1743 _nfs4_proc_write(wdata),
1744 &exception);
1745 } while (exception.retry);
1746 return err;
1747}
1748
1749static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1750{
1751 struct inode *inode = cdata->inode;
1752 struct nfs_fattr *fattr = cdata->res.fattr;
1753 struct nfs_server *server = NFS_SERVER(inode);
1754 struct rpc_message msg = {
1755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1756 .rpc_argp = &cdata->args,
1757 .rpc_resp = &cdata->res,
1758 .rpc_cred = cdata->cred,
1759 };
1760 int status;
1761
1762 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1763 (long long) cdata->args.offset);
1764
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001765 cdata->args.bitmask = server->attr_bitmask;
1766 cdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001767 cdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001768 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001770 if (status >= 0)
1771 renew_lease(server, cdata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001773 if (status >= 0)
1774 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return status;
1776}
1777
1778static int nfs4_proc_commit(struct nfs_write_data *cdata)
1779{
1780 struct nfs4_exception exception = { };
1781 int err;
1782 do {
1783 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1784 _nfs4_proc_commit(cdata),
1785 &exception);
1786 } while (exception.retry);
1787 return err;
1788}
1789
1790/*
1791 * Got race?
1792 * We will need to arrange for the VFS layer to provide an atomic open.
1793 * Until then, this create/open method is prone to inefficiency and race
1794 * conditions due to the lookup, create, and open VFS calls from sys_open()
1795 * placed on the wire.
1796 *
1797 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1798 * The file will be opened again in the subsequent VFS open call
1799 * (nfs4_proc_file_open).
1800 *
1801 * The open for read will just hang around to be used by any process that
1802 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1803 */
1804
1805static int
1806nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001807 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct nfs4_state *state;
1810 struct rpc_cred *cred;
1811 int status = 0;
1812
1813 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1814 if (IS_ERR(cred)) {
1815 status = PTR_ERR(cred);
1816 goto out;
1817 }
1818 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1819 put_rpccred(cred);
1820 if (IS_ERR(state)) {
1821 status = PTR_ERR(state);
1822 goto out;
1823 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001824 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (flags & O_EXCL) {
1826 struct nfs_fattr fattr;
1827 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1828 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001829 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001830 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001831 }
1832 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1833 nfs4_intent_set_file(nd, dentry, state);
1834 else
1835 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836out:
1837 return status;
1838}
1839
1840static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1841{
Trond Myklebust16e42952005-10-27 22:12:44 -04001842 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct nfs4_remove_arg args = {
1844 .fh = NFS_FH(dir),
1845 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001846 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001848 struct nfs_fattr dir_attr;
1849 struct nfs4_remove_res res = {
1850 .server = server,
1851 .dir_attr = &dir_attr,
1852 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct rpc_message msg = {
1854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1855 .rpc_argp = &args,
1856 .rpc_resp = &res,
1857 };
1858 int status;
1859
Trond Myklebust16e42952005-10-27 22:12:44 -04001860 nfs_fattr_init(res.dir_attr);
1861 status = rpc_call_sync(server->client, &msg, 0);
1862 if (status == 0) {
1863 update_changeattr(dir, &res.cinfo);
1864 nfs_post_op_update_inode(dir, res.dir_attr);
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return status;
1867}
1868
1869static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1870{
1871 struct nfs4_exception exception = { };
1872 int err;
1873 do {
1874 err = nfs4_handle_exception(NFS_SERVER(dir),
1875 _nfs4_proc_remove(dir, name),
1876 &exception);
1877 } while (exception.retry);
1878 return err;
1879}
1880
1881struct unlink_desc {
1882 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001883 struct nfs4_remove_res res;
1884 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885};
1886
1887static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1888 struct qstr *name)
1889{
Trond Myklebust16e42952005-10-27 22:12:44 -04001890 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct unlink_desc *up;
1892
1893 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1894 if (!up)
1895 return -ENOMEM;
1896
1897 up->args.fh = NFS_FH(dir->d_inode);
1898 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001899 up->args.bitmask = server->attr_bitmask;
1900 up->res.server = server;
1901 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1904 msg->rpc_argp = &up->args;
1905 msg->rpc_resp = &up->res;
1906 return 0;
1907}
1908
1909static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1910{
1911 struct rpc_message *msg = &task->tk_msg;
1912 struct unlink_desc *up;
1913
1914 if (msg->rpc_resp != NULL) {
1915 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001916 update_changeattr(dir->d_inode, &up->res.cinfo);
1917 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 kfree(up);
1919 msg->rpc_resp = NULL;
1920 msg->rpc_argp = NULL;
1921 }
1922 return 0;
1923}
1924
1925static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1926 struct inode *new_dir, struct qstr *new_name)
1927{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001928 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 struct nfs4_rename_arg arg = {
1930 .old_dir = NFS_FH(old_dir),
1931 .new_dir = NFS_FH(new_dir),
1932 .old_name = old_name,
1933 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001934 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001936 struct nfs_fattr old_fattr, new_fattr;
1937 struct nfs4_rename_res res = {
1938 .server = server,
1939 .old_fattr = &old_fattr,
1940 .new_fattr = &new_fattr,
1941 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct rpc_message msg = {
1943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1944 .rpc_argp = &arg,
1945 .rpc_resp = &res,
1946 };
1947 int status;
1948
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001949 nfs_fattr_init(res.old_fattr);
1950 nfs_fattr_init(res.new_fattr);
1951 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 if (!status) {
1954 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001955 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001957 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 return status;
1960}
1961
1962static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1963 struct inode *new_dir, struct qstr *new_name)
1964{
1965 struct nfs4_exception exception = { };
1966 int err;
1967 do {
1968 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1969 _nfs4_proc_rename(old_dir, old_name,
1970 new_dir, new_name),
1971 &exception);
1972 } while (exception.retry);
1973 return err;
1974}
1975
1976static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1977{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001978 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct nfs4_link_arg arg = {
1980 .fh = NFS_FH(inode),
1981 .dir_fh = NFS_FH(dir),
1982 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001983 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001985 struct nfs_fattr fattr, dir_attr;
1986 struct nfs4_link_res res = {
1987 .server = server,
1988 .fattr = &fattr,
1989 .dir_attr = &dir_attr,
1990 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct rpc_message msg = {
1992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1993 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001994 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 };
1996 int status;
1997
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001998 nfs_fattr_init(res.fattr);
1999 nfs_fattr_init(res.dir_attr);
2000 status = rpc_call_sync(server->client, &msg, 0);
2001 if (!status) {
2002 update_changeattr(dir, &res.cinfo);
2003 nfs_post_op_update_inode(dir, res.dir_attr);
2004 nfs_refresh_inode(inode, res.fattr);
2005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 return status;
2008}
2009
2010static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2011{
2012 struct nfs4_exception exception = { };
2013 int err;
2014 do {
2015 err = nfs4_handle_exception(NFS_SERVER(inode),
2016 _nfs4_proc_link(inode, dir, name),
2017 &exception);
2018 } while (exception.retry);
2019 return err;
2020}
2021
2022static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2023 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2024 struct nfs_fattr *fattr)
2025{
2026 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002027 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 struct nfs4_create_arg arg = {
2029 .dir_fh = NFS_FH(dir),
2030 .server = server,
2031 .name = name,
2032 .attrs = sattr,
2033 .ftype = NF4LNK,
2034 .bitmask = server->attr_bitmask,
2035 };
2036 struct nfs4_create_res res = {
2037 .server = server,
2038 .fh = fhandle,
2039 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002040 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 };
2042 struct rpc_message msg = {
2043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2044 .rpc_argp = &arg,
2045 .rpc_resp = &res,
2046 };
2047 int status;
2048
2049 if (path->len > NFS4_MAXPATHLEN)
2050 return -ENAMETOOLONG;
2051 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04002052 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002053 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2056 if (!status)
2057 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002058 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return status;
2060}
2061
2062static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2063 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2064 struct nfs_fattr *fattr)
2065{
2066 struct nfs4_exception exception = { };
2067 int err;
2068 do {
2069 err = nfs4_handle_exception(NFS_SERVER(dir),
2070 _nfs4_proc_symlink(dir, name, path, sattr,
2071 fhandle, fattr),
2072 &exception);
2073 } while (exception.retry);
2074 return err;
2075}
2076
2077static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2078 struct iattr *sattr)
2079{
2080 struct nfs_server *server = NFS_SERVER(dir);
2081 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002082 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct nfs4_create_arg arg = {
2084 .dir_fh = NFS_FH(dir),
2085 .server = server,
2086 .name = &dentry->d_name,
2087 .attrs = sattr,
2088 .ftype = NF4DIR,
2089 .bitmask = server->attr_bitmask,
2090 };
2091 struct nfs4_create_res res = {
2092 .server = server,
2093 .fh = &fhandle,
2094 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002095 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 };
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2099 .rpc_argp = &arg,
2100 .rpc_resp = &res,
2101 };
2102 int status;
2103
Trond Myklebust0e574af2005-10-27 22:12:38 -04002104 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002105 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2108 if (!status) {
2109 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002110 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 status = nfs_instantiate(dentry, &fhandle, &fattr);
2112 }
2113 return status;
2114}
2115
2116static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2117 struct iattr *sattr)
2118{
2119 struct nfs4_exception exception = { };
2120 int err;
2121 do {
2122 err = nfs4_handle_exception(NFS_SERVER(dir),
2123 _nfs4_proc_mkdir(dir, dentry, sattr),
2124 &exception);
2125 } while (exception.retry);
2126 return err;
2127}
2128
2129static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2130 u64 cookie, struct page *page, unsigned int count, int plus)
2131{
2132 struct inode *dir = dentry->d_inode;
2133 struct nfs4_readdir_arg args = {
2134 .fh = NFS_FH(dir),
2135 .pages = &page,
2136 .pgbase = 0,
2137 .count = count,
2138 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2139 };
2140 struct nfs4_readdir_res res;
2141 struct rpc_message msg = {
2142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2143 .rpc_argp = &args,
2144 .rpc_resp = &res,
2145 .rpc_cred = cred,
2146 };
2147 int status;
2148
Trond Myklebusteadf4592005-06-22 17:16:39 +00002149 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2150 dentry->d_parent->d_name.name,
2151 dentry->d_name.name,
2152 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 lock_kernel();
2154 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2155 res.pgbase = args.pgbase;
2156 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2157 if (status == 0)
2158 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2159 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00002160 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return status;
2162}
2163
2164static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2165 u64 cookie, struct page *page, unsigned int count, int plus)
2166{
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
2170 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2171 _nfs4_proc_readdir(dentry, cred, cookie,
2172 page, count, plus),
2173 &exception);
2174 } while (exception.retry);
2175 return err;
2176}
2177
2178static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2179 struct iattr *sattr, dev_t rdev)
2180{
2181 struct nfs_server *server = NFS_SERVER(dir);
2182 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002183 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct nfs4_create_arg arg = {
2185 .dir_fh = NFS_FH(dir),
2186 .server = server,
2187 .name = &dentry->d_name,
2188 .attrs = sattr,
2189 .bitmask = server->attr_bitmask,
2190 };
2191 struct nfs4_create_res res = {
2192 .server = server,
2193 .fh = &fh,
2194 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002195 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 };
2197 struct rpc_message msg = {
2198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2199 .rpc_argp = &arg,
2200 .rpc_resp = &res,
2201 };
2202 int status;
2203 int mode = sattr->ia_mode;
2204
Trond Myklebust0e574af2005-10-27 22:12:38 -04002205 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002206 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2209 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2210 if (S_ISFIFO(mode))
2211 arg.ftype = NF4FIFO;
2212 else if (S_ISBLK(mode)) {
2213 arg.ftype = NF4BLK;
2214 arg.u.device.specdata1 = MAJOR(rdev);
2215 arg.u.device.specdata2 = MINOR(rdev);
2216 }
2217 else if (S_ISCHR(mode)) {
2218 arg.ftype = NF4CHR;
2219 arg.u.device.specdata1 = MAJOR(rdev);
2220 arg.u.device.specdata2 = MINOR(rdev);
2221 }
2222 else
2223 arg.ftype = NF4SOCK;
2224
2225 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2226 if (status == 0) {
2227 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002228 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 status = nfs_instantiate(dentry, &fh, &fattr);
2230 }
2231 return status;
2232}
2233
2234static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2235 struct iattr *sattr, dev_t rdev)
2236{
2237 struct nfs4_exception exception = { };
2238 int err;
2239 do {
2240 err = nfs4_handle_exception(NFS_SERVER(dir),
2241 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2242 &exception);
2243 } while (exception.retry);
2244 return err;
2245}
2246
2247static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2248 struct nfs_fsstat *fsstat)
2249{
2250 struct nfs4_statfs_arg args = {
2251 .fh = fhandle,
2252 .bitmask = server->attr_bitmask,
2253 };
2254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2256 .rpc_argp = &args,
2257 .rpc_resp = fsstat,
2258 };
2259
Trond Myklebust0e574af2005-10-27 22:12:38 -04002260 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return rpc_call_sync(server->client, &msg, 0);
2262}
2263
2264static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2265{
2266 struct nfs4_exception exception = { };
2267 int err;
2268 do {
2269 err = nfs4_handle_exception(server,
2270 _nfs4_proc_statfs(server, fhandle, fsstat),
2271 &exception);
2272 } while (exception.retry);
2273 return err;
2274}
2275
2276static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2277 struct nfs_fsinfo *fsinfo)
2278{
2279 struct nfs4_fsinfo_arg args = {
2280 .fh = fhandle,
2281 .bitmask = server->attr_bitmask,
2282 };
2283 struct rpc_message msg = {
2284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2285 .rpc_argp = &args,
2286 .rpc_resp = fsinfo,
2287 };
2288
2289 return rpc_call_sync(server->client, &msg, 0);
2290}
2291
2292static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2293{
2294 struct nfs4_exception exception = { };
2295 int err;
2296
2297 do {
2298 err = nfs4_handle_exception(server,
2299 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2300 &exception);
2301 } while (exception.retry);
2302 return err;
2303}
2304
2305static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2306{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002307 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2309}
2310
2311static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2312 struct nfs_pathconf *pathconf)
2313{
2314 struct nfs4_pathconf_arg args = {
2315 .fh = fhandle,
2316 .bitmask = server->attr_bitmask,
2317 };
2318 struct rpc_message msg = {
2319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2320 .rpc_argp = &args,
2321 .rpc_resp = pathconf,
2322 };
2323
2324 /* None of the pathconf attributes are mandatory to implement */
2325 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2326 memset(pathconf, 0, sizeof(*pathconf));
2327 return 0;
2328 }
2329
Trond Myklebust0e574af2005-10-27 22:12:38 -04002330 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return rpc_call_sync(server->client, &msg, 0);
2332}
2333
2334static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2335 struct nfs_pathconf *pathconf)
2336{
2337 struct nfs4_exception exception = { };
2338 int err;
2339
2340 do {
2341 err = nfs4_handle_exception(server,
2342 _nfs4_proc_pathconf(server, fhandle, pathconf),
2343 &exception);
2344 } while (exception.retry);
2345 return err;
2346}
2347
Trond Myklebustec06c092006-03-20 13:44:27 -05002348static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Trond Myklebustec06c092006-03-20 13:44:27 -05002350 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Trond Myklebustec06c092006-03-20 13:44:27 -05002352 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002354 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002357 renew_lease(server, data->timestamp);
2358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Trond Myklebustec06c092006-03-20 13:44:27 -05002361static void nfs4_proc_read_setup(struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 struct rpc_message msg = {
2364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2365 .rpc_argp = &data->args,
2366 .rpc_resp = &data->res,
2367 .rpc_cred = data->cred,
2368 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 data->timestamp = jiffies;
2371
Trond Myklebustec06c092006-03-20 13:44:27 -05002372 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Trond Myklebust788e7a82006-03-20 13:44:27 -05002375static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct inode *inode = data->inode;
2378
2379 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2380 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002381 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002383 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002385 nfs_post_op_update_inode(inode, data->res.fattr);
2386 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
Trond Myklebust788e7a82006-03-20 13:44:27 -05002390static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2394 .rpc_argp = &data->args,
2395 .rpc_resp = &data->res,
2396 .rpc_cred = data->cred,
2397 };
2398 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002399 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 int stable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 if (how & FLUSH_STABLE) {
2403 if (!NFS_I(inode)->ncommit)
2404 stable = NFS_FILE_SYNC;
2405 else
2406 stable = NFS_DATA_SYNC;
2407 } else
2408 stable = NFS_UNSTABLE;
2409 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002410 data->args.bitmask = server->attr_bitmask;
2411 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 data->timestamp = jiffies;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /* Finalize the task. */
Trond Myklebust788e7a82006-03-20 13:44:27 -05002416 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
Trond Myklebust788e7a82006-03-20 13:44:27 -05002419static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct inode *inode = data->inode;
2422
2423 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2424 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002425 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002427 if (task->tk_status >= 0)
2428 nfs_post_op_update_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Trond Myklebust788e7a82006-03-20 13:44:27 -05002432static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2436 .rpc_argp = &data->args,
2437 .rpc_resp = &data->res,
2438 .rpc_cred = data->cred,
2439 };
Trond Myklebust788e7a82006-03-20 13:44:27 -05002440 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002442 data->args.bitmask = server->attr_bitmask;
2443 data->res.server = server;
2444
Trond Myklebust788e7a82006-03-20 13:44:27 -05002445 rpc_call_setup(&data->task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
2448/*
2449 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2450 * standalone procedure for queueing an asynchronous RENEW.
2451 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002452static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002455 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 if (task->tk_status < 0) {
2458 switch (task->tk_status) {
2459 case -NFS4ERR_STALE_CLIENTID:
2460 case -NFS4ERR_EXPIRED:
2461 case -NFS4ERR_CB_PATH_DOWN:
2462 nfs4_schedule_state_recovery(clp);
2463 }
2464 return;
2465 }
2466 spin_lock(&clp->cl_lock);
2467 if (time_before(clp->cl_last_renewal,timestamp))
2468 clp->cl_last_renewal = timestamp;
2469 spin_unlock(&clp->cl_lock);
2470}
2471
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002472static const struct rpc_call_ops nfs4_renew_ops = {
2473 .rpc_call_done = nfs4_renew_done,
2474};
2475
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002476int nfs4_proc_async_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct rpc_message msg = {
2479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2480 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002481 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 };
2483
2484 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002485 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002488int nfs4_proc_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 struct rpc_message msg = {
2491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2492 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002493 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 };
2495 unsigned long now = jiffies;
2496 int status;
2497
2498 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2499 if (status < 0)
2500 return status;
2501 spin_lock(&clp->cl_lock);
2502 if (time_before(clp->cl_last_renewal,now))
2503 clp->cl_last_renewal = now;
2504 spin_unlock(&clp->cl_lock);
2505 return 0;
2506}
2507
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002508static inline int nfs4_server_supports_acls(struct nfs_server *server)
2509{
2510 return (server->caps & NFS_CAP_ACLS)
2511 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2512 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2513}
2514
2515/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2516 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2517 * the stack.
2518 */
2519#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2520
2521static void buf_to_pages(const void *buf, size_t buflen,
2522 struct page **pages, unsigned int *pgbase)
2523{
2524 const void *p = buf;
2525
2526 *pgbase = offset_in_page(buf);
2527 p -= *pgbase;
2528 while (p < buf + buflen) {
2529 *(pages++) = virt_to_page(p);
2530 p += PAGE_CACHE_SIZE;
2531 }
2532}
2533
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002534struct nfs4_cached_acl {
2535 int cached;
2536 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002537 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002538};
2539
2540static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002541{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002542 struct nfs_inode *nfsi = NFS_I(inode);
2543
2544 spin_lock(&inode->i_lock);
2545 kfree(nfsi->nfs4_acl);
2546 nfsi->nfs4_acl = acl;
2547 spin_unlock(&inode->i_lock);
2548}
2549
2550static void nfs4_zap_acl_attr(struct inode *inode)
2551{
2552 nfs4_set_cached_acl(inode, NULL);
2553}
2554
2555static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2556{
2557 struct nfs_inode *nfsi = NFS_I(inode);
2558 struct nfs4_cached_acl *acl;
2559 int ret = -ENOENT;
2560
2561 spin_lock(&inode->i_lock);
2562 acl = nfsi->nfs4_acl;
2563 if (acl == NULL)
2564 goto out;
2565 if (buf == NULL) /* user is just asking for length */
2566 goto out_len;
2567 if (acl->cached == 0)
2568 goto out;
2569 ret = -ERANGE; /* see getxattr(2) man page */
2570 if (acl->len > buflen)
2571 goto out;
2572 memcpy(buf, acl->data, acl->len);
2573out_len:
2574 ret = acl->len;
2575out:
2576 spin_unlock(&inode->i_lock);
2577 return ret;
2578}
2579
2580static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2581{
2582 struct nfs4_cached_acl *acl;
2583
2584 if (buf && acl_len <= PAGE_SIZE) {
2585 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2586 if (acl == NULL)
2587 goto out;
2588 acl->cached = 1;
2589 memcpy(acl->data, buf, acl_len);
2590 } else {
2591 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2592 if (acl == NULL)
2593 goto out;
2594 acl->cached = 0;
2595 }
2596 acl->len = acl_len;
2597out:
2598 nfs4_set_cached_acl(inode, acl);
2599}
2600
2601static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2602{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002603 struct page *pages[NFS4ACL_MAXPAGES];
2604 struct nfs_getaclargs args = {
2605 .fh = NFS_FH(inode),
2606 .acl_pages = pages,
2607 .acl_len = buflen,
2608 };
2609 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002610 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2613 .rpc_argp = &args,
2614 .rpc_resp = &resp_len,
2615 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002616 struct page *localpage = NULL;
2617 int ret;
2618
2619 if (buflen < PAGE_SIZE) {
2620 /* As long as we're doing a round trip to the server anyway,
2621 * let's be prepared for a page of acl data. */
2622 localpage = alloc_page(GFP_KERNEL);
2623 resp_buf = page_address(localpage);
2624 if (localpage == NULL)
2625 return -ENOMEM;
2626 args.acl_pages[0] = localpage;
2627 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002628 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002629 } else {
2630 resp_buf = buf;
2631 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2632 }
2633 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2634 if (ret)
2635 goto out_free;
2636 if (resp_len > args.acl_len)
2637 nfs4_write_cached_acl(inode, NULL, resp_len);
2638 else
2639 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2640 if (buf) {
2641 ret = -ERANGE;
2642 if (resp_len > buflen)
2643 goto out_free;
2644 if (localpage)
2645 memcpy(buf, resp_buf, resp_len);
2646 }
2647 ret = resp_len;
2648out_free:
2649 if (localpage)
2650 __free_page(localpage);
2651 return ret;
2652}
2653
2654static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2655{
2656 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002657 int ret;
2658
2659 if (!nfs4_server_supports_acls(server))
2660 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002661 ret = nfs_revalidate_inode(server, inode);
2662 if (ret < 0)
2663 return ret;
2664 ret = nfs4_read_cached_acl(inode, buf, buflen);
2665 if (ret != -ENOENT)
2666 return ret;
2667 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002668}
2669
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002670static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2671{
2672 struct nfs_server *server = NFS_SERVER(inode);
2673 struct page *pages[NFS4ACL_MAXPAGES];
2674 struct nfs_setaclargs arg = {
2675 .fh = NFS_FH(inode),
2676 .acl_pages = pages,
2677 .acl_len = buflen,
2678 };
2679 struct rpc_message msg = {
2680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2681 .rpc_argp = &arg,
2682 .rpc_resp = NULL,
2683 };
2684 int ret;
2685
2686 if (!nfs4_server_supports_acls(server))
2687 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002688 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002689 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2690 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002691 if (ret == 0)
2692 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002693 return ret;
2694}
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002697nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 struct nfs4_client *clp = server->nfs4_state;
2700
2701 if (!clp || task->tk_status >= 0)
2702 return 0;
2703 switch(task->tk_status) {
2704 case -NFS4ERR_STALE_CLIENTID:
2705 case -NFS4ERR_STALE_STATEID:
2706 case -NFS4ERR_EXPIRED:
2707 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2708 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002709 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 rpc_wake_up_task(task);
2711 task->tk_status = 0;
2712 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 case -NFS4ERR_DELAY:
Chuck Lever006ea732006-03-20 13:44:14 -05002714 nfs_inc_server_stats((struct nfs_server *) server,
2715 NFSIOS_DELAY);
2716 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2718 task->tk_status = 0;
2719 return -EAGAIN;
2720 case -NFS4ERR_OLD_STATEID:
2721 task->tk_status = 0;
2722 return -EAGAIN;
2723 }
2724 task->tk_status = nfs4_map_errors(task->tk_status);
2725 return 0;
2726}
2727
Trond Myklebust433fbe42006-01-03 09:55:22 +01002728static int nfs4_wait_bit_interruptible(void *word)
2729{
2730 if (signal_pending(current))
2731 return -ERESTARTSYS;
2732 schedule();
2733 return 0;
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002739 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 might_sleep();
2742
2743 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002744 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2745 nfs4_wait_bit_interruptible,
2746 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 rpc_clnt_sigunmask(clnt, &oldset);
2748 return res;
2749}
2750
2751static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2752{
2753 sigset_t oldset;
2754 int res = 0;
2755
2756 might_sleep();
2757
2758 if (*timeout <= 0)
2759 *timeout = NFS4_POLL_RETRY_MIN;
2760 if (*timeout > NFS4_POLL_RETRY_MAX)
2761 *timeout = NFS4_POLL_RETRY_MAX;
2762 rpc_clnt_sigmask(clnt, &oldset);
2763 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002764 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (signalled())
2766 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002767 } else
2768 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 rpc_clnt_sigunmask(clnt, &oldset);
2770 *timeout <<= 1;
2771 return res;
2772}
2773
2774/* This is the error handling routine for processes that are allowed
2775 * to sleep.
2776 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002777int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 struct nfs4_client *clp = server->nfs4_state;
2780 int ret = errorcode;
2781
2782 exception->retry = 0;
2783 switch(errorcode) {
2784 case 0:
2785 return 0;
2786 case -NFS4ERR_STALE_CLIENTID:
2787 case -NFS4ERR_STALE_STATEID:
2788 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002789 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 ret = nfs4_wait_clnt_recover(server->client, clp);
2791 if (ret == 0)
2792 exception->retry = 1;
2793 break;
2794 case -NFS4ERR_GRACE:
2795 case -NFS4ERR_DELAY:
2796 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002797 if (ret != 0)
2798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002800 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 /* We failed to handle the error */
2803 return nfs4_map_errors(ret);
2804}
2805
Trond Myklebust286d7d62006-01-03 09:55:26 +01002806int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 nfs4_verifier sc_verifier;
2809 struct nfs4_setclientid setclientid = {
2810 .sc_verifier = &sc_verifier,
2811 .sc_prog = program,
2812 };
2813 struct rpc_message msg = {
2814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2815 .rpc_argp = &setclientid,
2816 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002817 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 };
2819 u32 *p;
2820 int loop = 0;
2821 int status;
2822
2823 p = (u32*)sc_verifier.data;
2824 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2825 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2826
2827 for(;;) {
2828 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2829 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2830 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002831 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 clp->cl_id_uniquifier);
2833 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2834 sizeof(setclientid.sc_netid), "tcp");
2835 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2836 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2837 clp->cl_ipaddr, port >> 8, port & 255);
2838
2839 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2840 if (status != -NFS4ERR_CLID_INUSE)
2841 break;
2842 if (signalled())
2843 break;
2844 if (loop++ & 1)
2845 ssleep(clp->cl_lease_time + 1);
2846 else
2847 if (++clp->cl_id_uniquifier == 0)
2848 break;
2849 }
2850 return status;
2851}
2852
2853int
Trond Myklebust286d7d62006-01-03 09:55:26 +01002854nfs4_proc_setclientid_confirm(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct nfs_fsinfo fsinfo;
2857 struct rpc_message msg = {
2858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2859 .rpc_argp = clp,
2860 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002861 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 };
2863 unsigned long now;
2864 int status;
2865
2866 now = jiffies;
2867 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2868 if (status == 0) {
2869 spin_lock(&clp->cl_lock);
2870 clp->cl_lease_time = fsinfo.lease_time * HZ;
2871 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002872 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 spin_unlock(&clp->cl_lock);
2874 }
2875 return status;
2876}
2877
Trond Myklebustfe650402006-01-03 09:55:18 +01002878struct nfs4_delegreturndata {
2879 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002880 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002881 struct nfs_fh fh;
2882 nfs4_stateid stateid;
2883 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002884 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002885 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002886 int rpc_status;
2887};
2888
2889static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Trond Myklebustfe650402006-01-03 09:55:18 +01002891 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 struct rpc_message msg = {
2893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002894 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002895 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002896 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002898 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002899 rpc_call_setup(task, &msg, 0);
2900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Trond Myklebustfe650402006-01-03 09:55:18 +01002902static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2903{
2904 struct nfs4_delegreturndata *data = calldata;
2905 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002906 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002907 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002908}
2909
2910static void nfs4_delegreturn_release(void *calldata)
2911{
2912 struct nfs4_delegreturndata *data = calldata;
2913
2914 put_rpccred(data->cred);
2915 kfree(calldata);
2916}
2917
Jesper Juhlc8d149f2006-03-20 13:44:07 -05002918static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01002919 .rpc_call_prepare = nfs4_delegreturn_prepare,
2920 .rpc_call_done = nfs4_delegreturn_done,
2921 .rpc_release = nfs4_delegreturn_release,
2922};
2923
2924static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2925{
2926 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002927 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002928 struct rpc_task *task;
2929 int status;
2930
2931 data = kmalloc(sizeof(*data), GFP_KERNEL);
2932 if (data == NULL)
2933 return -ENOMEM;
2934 data->args.fhandle = &data->fh;
2935 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002936 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002937 nfs_copy_fh(&data->fh, NFS_FH(inode));
2938 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002939 data->res.fattr = &data->fattr;
2940 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002941 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002942 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002943 data->rpc_status = 0;
2944
2945 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2946 if (IS_ERR(task)) {
2947 nfs4_delegreturn_release(data);
2948 return PTR_ERR(task);
2949 }
2950 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002951 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002952 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002953 if (status == 0)
2954 nfs_post_op_update_inode(inode, &data->fattr);
2955 }
Trond Myklebustfe650402006-01-03 09:55:18 +01002956 rpc_release_task(task);
2957 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
2960int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2961{
2962 struct nfs_server *server = NFS_SERVER(inode);
2963 struct nfs4_exception exception = { };
2964 int err;
2965 do {
2966 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2967 switch (err) {
2968 case -NFS4ERR_STALE_STATEID:
2969 case -NFS4ERR_EXPIRED:
2970 nfs4_schedule_state_recovery(server->nfs4_state);
2971 case 0:
2972 return 0;
2973 }
2974 err = nfs4_handle_exception(server, err, &exception);
2975 } while (exception.retry);
2976 return err;
2977}
2978
2979#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2980#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2981
2982/*
2983 * sleep, with exponential backoff, and retry the LOCK operation.
2984 */
2985static unsigned long
2986nfs4_set_lock_task_retry(unsigned long timeout)
2987{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002988 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 timeout <<= 1;
2990 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2991 return NFS4_LOCK_MAXTIMEOUT;
2992 return timeout;
2993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2996{
2997 struct inode *inode = state->inode;
2998 struct nfs_server *server = NFS_SERVER(inode);
2999 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003000 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003002 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003004 struct nfs_lockt_res res = {
3005 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
3007 struct rpc_message msg = {
3008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3009 .rpc_argp = &arg,
3010 .rpc_resp = &res,
3011 .rpc_cred = state->owner->so_cred,
3012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct nfs4_lock_state *lsp;
3014 int status;
3015
3016 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003017 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003018 status = nfs4_set_lock_state(state, request);
3019 if (status != 0)
3020 goto out;
3021 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003022 arg.lock_owner.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003024 switch (status) {
3025 case 0:
3026 request->fl_type = F_UNLCK;
3027 break;
3028 case -NFS4ERR_DENIED:
3029 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003031out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 up_read(&clp->cl_sem);
3033 return status;
3034}
3035
3036static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3037{
3038 struct nfs4_exception exception = { };
3039 int err;
3040
3041 do {
3042 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3043 _nfs4_proc_getlk(state, cmd, request),
3044 &exception);
3045 } while (exception.retry);
3046 return err;
3047}
3048
3049static int do_vfs_lock(struct file *file, struct file_lock *fl)
3050{
3051 int res = 0;
3052 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3053 case FL_POSIX:
3054 res = posix_lock_file_wait(file, fl);
3055 break;
3056 case FL_FLOCK:
3057 res = flock_lock_file_wait(file, fl);
3058 break;
3059 default:
3060 BUG();
3061 }
3062 if (res < 0)
3063 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3064 __FUNCTION__);
3065 return res;
3066}
3067
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003068struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003069 struct nfs_locku_args arg;
3070 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003071 struct nfs4_lock_state *lsp;
3072 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003073 struct file_lock fl;
3074 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003075 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003076};
3077
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003078static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3079 struct nfs_open_context *ctx,
3080 struct nfs4_lock_state *lsp,
3081 struct nfs_seqid *seqid)
3082{
3083 struct nfs4_unlockdata *p;
3084 struct inode *inode = lsp->ls_state->inode;
3085
3086 p = kmalloc(sizeof(*p), GFP_KERNEL);
3087 if (p == NULL)
3088 return NULL;
3089 p->arg.fh = NFS_FH(inode);
3090 p->arg.fl = &p->fl;
3091 p->arg.seqid = seqid;
3092 p->arg.stateid = &lsp->ls_stateid;
3093 p->lsp = lsp;
3094 atomic_inc(&lsp->ls_count);
3095 /* Ensure we don't close file until we're done freeing locks! */
3096 p->ctx = get_nfs_open_context(ctx);
3097 memcpy(&p->fl, fl, sizeof(p->fl));
3098 p->server = NFS_SERVER(inode);
3099 return p;
3100}
3101
Trond Myklebust06f814a2006-01-03 09:55:07 +01003102static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003103{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003104 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003105 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003106 nfs4_put_lock_state(calldata->lsp);
3107 put_nfs_open_context(calldata->ctx);
3108 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003109}
3110
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003111static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003112{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003113 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003114
Trond Myklebust06f814a2006-01-03 09:55:07 +01003115 if (RPC_ASSASSINATED(task))
3116 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003117 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003118 switch (task->tk_status) {
3119 case 0:
3120 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003121 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003122 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003123 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003124 break;
3125 case -NFS4ERR_STALE_STATEID:
3126 case -NFS4ERR_EXPIRED:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003127 nfs4_schedule_state_recovery(calldata->server->nfs4_state);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003128 break;
3129 default:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003130 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) {
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003131 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003132 }
3133 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003134}
3135
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003136static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003137{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003138 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct rpc_message msg = {
3140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003141 .rpc_argp = &calldata->arg,
3142 .rpc_resp = &calldata->res,
3143 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003146 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003147 return;
3148 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149 /* Note: exit _without_ running nfs4_locku_done */
3150 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003151 return;
3152 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003153 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003154 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003157static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003158 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003159 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003160 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003161};
3162
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003163static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3164 struct nfs_open_context *ctx,
3165 struct nfs4_lock_state *lsp,
3166 struct nfs_seqid *seqid)
3167{
3168 struct nfs4_unlockdata *data;
3169 struct rpc_task *task;
3170
3171 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3172 if (data == NULL) {
3173 nfs_free_seqid(seqid);
3174 return ERR_PTR(-ENOMEM);
3175 }
3176
3177 /* Unlock _before_ we do the RPC call */
3178 do_vfs_lock(fl->fl_file, fl);
3179 task = rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3180 if (IS_ERR(task))
3181 nfs4_locku_release_calldata(data);
3182 return task;
3183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3186{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003187 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003188 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003189 struct rpc_task *task;
3190 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003192 /* Is this a delegated lock? */
3193 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003194 goto out_unlock;
3195 /* Is this open_owner holding any locks on the server? */
3196 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
3197 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003198
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003199 status = nfs4_set_lock_state(state, request);
3200 if (status != 0)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003201 goto out_unlock;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003202 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003203 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003204 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3205 if (seqid == NULL)
3206 goto out_unlock;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003207 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3208 status = PTR_ERR(task);
3209 if (IS_ERR(task))
3210 goto out_unlock;
3211 status = nfs4_wait_for_completion_rpc_task(task);
3212 rpc_release_task(task);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003213 return status;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003214out_unlock:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003215 do_vfs_lock(request->fl_file, request);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003219struct nfs4_lockdata {
3220 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003221 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003222 struct nfs4_lock_state *lsp;
3223 struct nfs_open_context *ctx;
3224 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003225 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003226 int rpc_status;
3227 int cancelled;
3228};
3229
3230static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3231 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3232{
3233 struct nfs4_lockdata *p;
3234 struct inode *inode = lsp->ls_state->inode;
3235 struct nfs_server *server = NFS_SERVER(inode);
3236
3237 p = kzalloc(sizeof(*p), GFP_KERNEL);
3238 if (p == NULL)
3239 return NULL;
3240
3241 p->arg.fh = NFS_FH(inode);
3242 p->arg.fl = &p->fl;
3243 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3244 if (p->arg.lock_seqid == NULL)
3245 goto out_free;
3246 p->arg.lock_stateid = &lsp->ls_stateid;
3247 p->arg.lock_owner.clientid = server->nfs4_state->cl_clientid;
3248 p->arg.lock_owner.id = lsp->ls_id;
3249 p->lsp = lsp;
3250 atomic_inc(&lsp->ls_count);
3251 p->ctx = get_nfs_open_context(ctx);
3252 memcpy(&p->fl, fl, sizeof(p->fl));
3253 return p;
3254out_free:
3255 kfree(p);
3256 return NULL;
3257}
3258
3259static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3260{
3261 struct nfs4_lockdata *data = calldata;
3262 struct nfs4_state *state = data->lsp->ls_state;
3263 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3266 .rpc_argp = &data->arg,
3267 .rpc_resp = &data->res,
3268 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003271 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3272 return;
3273 dprintk("%s: begin!\n", __FUNCTION__);
3274 /* Do we need to do an open_to_lock_owner? */
3275 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3276 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3277 if (data->arg.open_seqid == NULL) {
3278 data->rpc_status = -ENOMEM;
3279 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003280 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003281 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003282 data->arg.open_stateid = &state->stateid;
3283 data->arg.new_lock_owner = 1;
3284 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003285 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003286 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003287out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003288 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3289}
3290
3291static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3292{
3293 struct nfs4_lockdata *data = calldata;
3294
3295 dprintk("%s: begin!\n", __FUNCTION__);
3296
3297 data->rpc_status = task->tk_status;
3298 if (RPC_ASSASSINATED(task))
3299 goto out;
3300 if (data->arg.new_lock_owner != 0) {
3301 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3302 if (data->rpc_status == 0)
3303 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3304 else
3305 goto out;
3306 }
3307 if (data->rpc_status == 0) {
3308 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3309 sizeof(data->lsp->ls_stateid.data));
3310 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003311 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003312 }
3313 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3314out:
3315 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3316}
3317
3318static void nfs4_lock_release(void *calldata)
3319{
3320 struct nfs4_lockdata *data = calldata;
3321
3322 dprintk("%s: begin!\n", __FUNCTION__);
3323 if (data->arg.open_seqid != NULL)
3324 nfs_free_seqid(data->arg.open_seqid);
3325 if (data->cancelled != 0) {
3326 struct rpc_task *task;
3327 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3328 data->arg.lock_seqid);
3329 if (!IS_ERR(task))
3330 rpc_release_task(task);
3331 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3332 } else
3333 nfs_free_seqid(data->arg.lock_seqid);
3334 nfs4_put_lock_state(data->lsp);
3335 put_nfs_open_context(data->ctx);
3336 kfree(data);
3337 dprintk("%s: done!\n", __FUNCTION__);
3338}
3339
3340static const struct rpc_call_ops nfs4_lock_ops = {
3341 .rpc_call_prepare = nfs4_lock_prepare,
3342 .rpc_call_done = nfs4_lock_done,
3343 .rpc_release = nfs4_lock_release,
3344};
3345
3346static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3347{
3348 struct nfs4_lockdata *data;
3349 struct rpc_task *task;
3350 int ret;
3351
3352 dprintk("%s: begin!\n", __FUNCTION__);
3353 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3354 fl->fl_u.nfs4_fl.owner);
3355 if (data == NULL)
3356 return -ENOMEM;
3357 if (IS_SETLKW(cmd))
3358 data->arg.block = 1;
3359 if (reclaim != 0)
3360 data->arg.reclaim = 1;
3361 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3362 &nfs4_lock_ops, data);
3363 if (IS_ERR(task)) {
3364 nfs4_lock_release(data);
3365 return PTR_ERR(task);
3366 }
3367 ret = nfs4_wait_for_completion_rpc_task(task);
3368 if (ret == 0) {
3369 ret = data->rpc_status;
3370 if (ret == -NFS4ERR_DENIED)
3371 ret = -EAGAIN;
3372 } else
3373 data->cancelled = 1;
3374 rpc_release_task(task);
3375 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
3379static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3380{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003381 struct nfs_server *server = NFS_SERVER(state->inode);
3382 struct nfs4_exception exception = { };
3383 int err;
3384
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003385 /* Cache the lock if possible... */
3386 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3387 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003388 do {
3389 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3390 if (err != -NFS4ERR_DELAY)
3391 break;
3392 nfs4_handle_exception(server, err, &exception);
3393 } while (exception.retry);
3394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3398{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003399 struct nfs_server *server = NFS_SERVER(state->inode);
3400 struct nfs4_exception exception = { };
3401 int err;
3402
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003403 err = nfs4_set_lock_state(state, request);
3404 if (err != 0)
3405 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003406 do {
3407 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3408 if (err != -NFS4ERR_DELAY)
3409 break;
3410 nfs4_handle_exception(server, err, &exception);
3411 } while (exception.retry);
3412 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
3415static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3416{
3417 struct nfs4_client *clp = state->owner->so_client;
3418 int status;
3419
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003420 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003421 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003422 /* Yes: cache locks! */
3423 status = do_vfs_lock(request->fl_file, request);
3424 /* ...but avoid races with delegation recall... */
3425 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003426 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 }
Trond Myklebustff604062005-11-25 17:10:01 -05003428 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003429 status = nfs4_set_lock_state(state, request);
3430 if (status != 0)
3431 goto out;
3432 status = _nfs4_do_setlk(state, cmd, request, 0);
3433 if (status != 0)
3434 goto out;
3435 /* Note: we always want to sleep here! */
3436 request->fl_flags |= FL_SLEEP;
3437 if (do_vfs_lock(request->fl_file, request) < 0)
3438 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3439out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 up_read(&clp->cl_sem);
3441 return status;
3442}
3443
3444static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3445{
3446 struct nfs4_exception exception = { };
3447 int err;
3448
3449 do {
3450 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3451 _nfs4_proc_setlk(state, cmd, request),
3452 &exception);
3453 } while (exception.retry);
3454 return err;
3455}
3456
3457static int
3458nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3459{
3460 struct nfs_open_context *ctx;
3461 struct nfs4_state *state;
3462 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3463 int status;
3464
3465 /* verify open state */
3466 ctx = (struct nfs_open_context *)filp->private_data;
3467 state = ctx->state;
3468
3469 if (request->fl_start < 0 || request->fl_end < 0)
3470 return -EINVAL;
3471
3472 if (IS_GETLK(cmd))
3473 return nfs4_proc_getlk(state, F_GETLK, request);
3474
3475 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3476 return -EINVAL;
3477
3478 if (request->fl_type == F_UNLCK)
3479 return nfs4_proc_unlck(state, cmd, request);
3480
3481 do {
3482 status = nfs4_proc_setlk(state, cmd, request);
3483 if ((status != -EAGAIN) || IS_SETLK(cmd))
3484 break;
3485 timeout = nfs4_set_lock_task_retry(timeout);
3486 status = -ERESTARTSYS;
3487 if (signalled())
3488 break;
3489 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return status;
3491}
3492
Trond Myklebust888e6942005-11-04 15:38:11 -05003493int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3494{
3495 struct nfs_server *server = NFS_SERVER(state->inode);
3496 struct nfs4_exception exception = { };
3497 int err;
3498
3499 err = nfs4_set_lock_state(state, fl);
3500 if (err != 0)
3501 goto out;
3502 do {
3503 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3504 if (err != -NFS4ERR_DELAY)
3505 break;
3506 err = nfs4_handle_exception(server, err, &exception);
3507 } while (exception.retry);
3508out:
3509 return err;
3510}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003511
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003512#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3513
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003514int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3515 size_t buflen, int flags)
3516{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003517 struct inode *inode = dentry->d_inode;
3518
3519 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3520 return -EOPNOTSUPP;
3521
3522 if (!S_ISREG(inode->i_mode) &&
3523 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3524 return -EPERM;
3525
3526 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003527}
3528
3529/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3530 * and that's what we'll do for e.g. user attributes that haven't been set.
3531 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3532 * attributes in kernel-managed attribute namespaces. */
3533ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3534 size_t buflen)
3535{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003536 struct inode *inode = dentry->d_inode;
3537
3538 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3539 return -EOPNOTSUPP;
3540
3541 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003542}
3543
3544ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3545{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003546 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003547
3548 if (buf && buflen < len)
3549 return -ERANGE;
3550 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003551 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3552 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003553}
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3556 .recover_open = nfs4_open_reclaim,
3557 .recover_lock = nfs4_lock_reclaim,
3558};
3559
3560struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3561 .recover_open = nfs4_open_expired,
3562 .recover_lock = nfs4_lock_expired,
3563};
3564
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003565static struct inode_operations nfs4_file_inode_operations = {
3566 .permission = nfs_permission,
3567 .getattr = nfs_getattr,
3568 .setattr = nfs_setattr,
3569 .getxattr = nfs4_getxattr,
3570 .setxattr = nfs4_setxattr,
3571 .listxattr = nfs4_listxattr,
3572};
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574struct nfs_rpc_ops nfs_v4_clientops = {
3575 .version = 4, /* protocol version */
3576 .dentry_ops = &nfs4_dentry_operations,
3577 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003578 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 .getroot = nfs4_proc_get_root,
3580 .getattr = nfs4_proc_getattr,
3581 .setattr = nfs4_proc_setattr,
3582 .lookup = nfs4_proc_lookup,
3583 .access = nfs4_proc_access,
3584 .readlink = nfs4_proc_readlink,
3585 .read = nfs4_proc_read,
3586 .write = nfs4_proc_write,
3587 .commit = nfs4_proc_commit,
3588 .create = nfs4_proc_create,
3589 .remove = nfs4_proc_remove,
3590 .unlink_setup = nfs4_proc_unlink_setup,
3591 .unlink_done = nfs4_proc_unlink_done,
3592 .rename = nfs4_proc_rename,
3593 .link = nfs4_proc_link,
3594 .symlink = nfs4_proc_symlink,
3595 .mkdir = nfs4_proc_mkdir,
3596 .rmdir = nfs4_proc_remove,
3597 .readdir = nfs4_proc_readdir,
3598 .mknod = nfs4_proc_mknod,
3599 .statfs = nfs4_proc_statfs,
3600 .fsinfo = nfs4_proc_fsinfo,
3601 .pathconf = nfs4_proc_pathconf,
3602 .decode_dirent = nfs4_decode_dirent,
3603 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003604 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003606 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003608 .commit_done = nfs4_commit_done,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003609 .file_open = nfs_open,
3610 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003612 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613};
3614
3615/*
3616 * Local variables:
3617 * c-basic-offset: 8
3618 * End:
3619 */