blob: 984ca3454d0441de4008948fc0c91f60b42ccbeb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
54
55#define NFSDBG_FACILITY NFSDBG_PROC
56
57#define NFS4_POLL_RETRY_MIN (1*HZ)
58#define NFS4_POLL_RETRY_MAX (15*HZ)
59
Trond Myklebustcdd4e682006-01-03 09:55:12 +010060struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010061static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070063static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070065static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Trond Myklebust58d97142006-01-03 09:55:24 +010066static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
68extern struct rpc_procinfo nfs4_procedures[];
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Prevent leaks of NFSv4 errors into userland */
71int nfs4_map_errors(int err)
72{
73 if (err < -1000) {
74 dprintk("%s could not handle NFSv4 error %d\n",
75 __FUNCTION__, -err);
76 return -EIO;
77 }
78 return err;
79}
80
81/*
82 * This is our standard bitmap for GETATTR requests.
83 */
84const u32 nfs4_fattr_bitmap[2] = {
85 FATTR4_WORD0_TYPE
86 | FATTR4_WORD0_CHANGE
87 | FATTR4_WORD0_SIZE
88 | FATTR4_WORD0_FSID
89 | FATTR4_WORD0_FILEID,
90 FATTR4_WORD1_MODE
91 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER
93 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_RAWDEV
95 | FATTR4_WORD1_SPACE_USED
96 | FATTR4_WORD1_TIME_ACCESS
97 | FATTR4_WORD1_TIME_METADATA
98 | FATTR4_WORD1_TIME_MODIFY
99};
100
101const u32 nfs4_statfs_bitmap[2] = {
102 FATTR4_WORD0_FILES_AVAIL
103 | FATTR4_WORD0_FILES_FREE
104 | FATTR4_WORD0_FILES_TOTAL,
105 FATTR4_WORD1_SPACE_AVAIL
106 | FATTR4_WORD1_SPACE_FREE
107 | FATTR4_WORD1_SPACE_TOTAL
108};
109
Trond Myklebust4ce79712005-06-22 17:16:21 +0000110const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 FATTR4_WORD0_MAXLINK
112 | FATTR4_WORD0_MAXNAME,
113 0
114};
115
116const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
117 | FATTR4_WORD0_MAXREAD
118 | FATTR4_WORD0_MAXWRITE
119 | FATTR4_WORD0_LEASE_TIME,
120 0
121};
122
123static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
124 struct nfs4_readdir_arg *readdir)
125{
126 u32 *start, *p;
127
128 BUG_ON(readdir->count < 80);
129 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000130 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
132 return;
133 }
134
135 readdir->cookie = 0;
136 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
137 if (cookie == 2)
138 return;
139
140 /*
141 * NFSv4 servers do not return entries for '.' and '..'
142 * Therefore, we fake these entries here. We let '.'
143 * have cookie 0 and '..' have cookie 1. Note that
144 * when talking to the server, we always send cookie 0
145 * instead of 1 or 2.
146 */
147 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
148
149 if (cookie == 0) {
150 *p++ = xdr_one; /* next */
151 *p++ = xdr_zero; /* cookie, first word */
152 *p++ = xdr_one; /* cookie, second word */
153 *p++ = xdr_one; /* entry len */
154 memcpy(p, ".\0\0\0", 4); /* entry */
155 p++;
156 *p++ = xdr_one; /* bitmap length */
157 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
158 *p++ = htonl(8); /* attribute buffer length */
159 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
160 }
161
162 *p++ = xdr_one; /* next */
163 *p++ = xdr_zero; /* cookie, first word */
164 *p++ = xdr_two; /* cookie, second word */
165 *p++ = xdr_two; /* entry len */
166 memcpy(p, "..\0\0", 4); /* entry */
167 p++;
168 *p++ = xdr_one; /* bitmap length */
169 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
170 *p++ = htonl(8); /* attribute buffer length */
171 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
172
173 readdir->pgbase = (char *)p - (char *)start;
174 readdir->count -= readdir->pgbase;
175 kunmap_atomic(start, KM_USER0);
176}
177
Trond Myklebust26e976a2006-01-03 09:55:21 +0100178static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct nfs4_client *clp = server->nfs4_state;
181 spin_lock(&clp->cl_lock);
182 if (time_before(clp->cl_last_renewal,timestamp))
183 clp->cl_last_renewal = timestamp;
184 spin_unlock(&clp->cl_lock);
185}
186
187static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
188{
189 struct nfs_inode *nfsi = NFS_I(inode);
190
Trond Myklebustdecf4912005-10-27 22:12:39 -0400191 spin_lock(&inode->i_lock);
192 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
194 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400195 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100198struct nfs4_opendata {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100199 atomic_t count;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100200 struct nfs_openargs o_arg;
201 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100202 struct nfs_open_confirmargs c_arg;
203 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100204 struct nfs_fattr f_attr;
205 struct nfs_fattr dir_attr;
206 struct dentry *dentry;
207 struct dentry *dir;
208 struct nfs4_state_owner *owner;
209 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100210 unsigned long timestamp;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100211 int rpc_status;
212 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100213};
214
215static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
216 struct nfs4_state_owner *sp, int flags,
217 const struct iattr *attrs)
218{
219 struct dentry *parent = dget_parent(dentry);
220 struct inode *dir = parent->d_inode;
221 struct nfs_server *server = NFS_SERVER(dir);
222 struct nfs4_opendata *p;
223
224 p = kzalloc(sizeof(*p), GFP_KERNEL);
225 if (p == NULL)
226 goto err;
227 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
228 if (p->o_arg.seqid == NULL)
229 goto err_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100230 atomic_set(&p->count, 1);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100231 p->dentry = dget(dentry);
232 p->dir = parent;
233 p->owner = sp;
234 atomic_inc(&sp->so_count);
235 p->o_arg.fh = NFS_FH(dir);
236 p->o_arg.open_flags = flags,
237 p->o_arg.clientid = server->nfs4_state->cl_clientid;
238 p->o_arg.id = sp->so_id;
239 p->o_arg.name = &dentry->d_name;
240 p->o_arg.server = server;
241 p->o_arg.bitmask = server->attr_bitmask;
242 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
243 p->o_res.f_attr = &p->f_attr;
244 p->o_res.dir_attr = &p->dir_attr;
245 p->o_res.server = server;
246 nfs_fattr_init(&p->f_attr);
247 nfs_fattr_init(&p->dir_attr);
248 if (flags & O_EXCL) {
249 u32 *s = (u32 *) p->o_arg.u.verifier.data;
250 s[0] = jiffies;
251 s[1] = current->pid;
252 } else if (flags & O_CREAT) {
253 p->o_arg.u.attrs = &p->attrs;
254 memcpy(&p->attrs, attrs, sizeof(p->attrs));
255 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100256 p->c_arg.fh = &p->o_res.fh;
257 p->c_arg.stateid = &p->o_res.stateid;
258 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100259 return p;
260err_free:
261 kfree(p);
262err:
263 dput(parent);
264 return NULL;
265}
266
267static void nfs4_opendata_free(struct nfs4_opendata *p)
268{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100269 if (p != NULL && atomic_dec_and_test(&p->count)) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100270 nfs_free_seqid(p->o_arg.seqid);
271 nfs4_put_state_owner(p->owner);
272 dput(p->dir);
273 dput(p->dentry);
274 kfree(p);
275 }
276}
277
Trond Myklebust95121352005-10-18 14:20:12 -0700278/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100279static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100280 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700281{
282 struct rpc_task *task;
283
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100284 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700285 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700286 rpc_execute(task);
287 return 0;
288}
289
Trond Myklebust06f814a2006-01-03 09:55:07 +0100290static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
291{
292 sigset_t oldset;
293 int ret;
294
295 rpc_clnt_sigmask(task->tk_client, &oldset);
296 ret = rpc_wait_for_completion_task(task);
297 rpc_clnt_sigunmask(task->tk_client, &oldset);
298 return ret;
299}
300
Trond Myklebuste7616922006-01-03 09:55:13 +0100301static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
302{
303 switch (open_flags) {
304 case FMODE_WRITE:
305 state->n_wronly++;
306 break;
307 case FMODE_READ:
308 state->n_rdonly++;
309 break;
310 case FMODE_READ|FMODE_WRITE:
311 state->n_rdwr++;
312 }
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
316{
317 struct inode *inode = state->inode;
318
319 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500320 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700321 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500323 memcpy(&state->stateid, stateid, sizeof(state->stateid));
Trond Myklebuste7616922006-01-03 09:55:13 +0100324 update_open_stateflags(state, open_flags);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500325 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700327 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100330static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
331{
332 struct inode *inode;
333 struct nfs4_state *state = NULL;
334
335 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
336 goto out;
337 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
338 if (inode == NULL)
339 goto out;
340 state = nfs4_get_open_state(inode, data->owner);
341 if (state == NULL)
342 goto put_inode;
343 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
344put_inode:
345 iput(inode);
346out:
347 return state;
348}
349
Trond Myklebust864472e2006-01-03 09:55:15 +0100350static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
351{
352 struct nfs_inode *nfsi = NFS_I(state->inode);
353 struct nfs_open_context *ctx;
354
355 spin_lock(&state->inode->i_lock);
356 list_for_each_entry(ctx, &nfsi->open_files, list) {
357 if (ctx->state != state)
358 continue;
359 get_nfs_open_context(ctx);
360 spin_unlock(&state->inode->i_lock);
361 return ctx;
362 }
363 spin_unlock(&state->inode->i_lock);
364 return ERR_PTR(-ENOENT);
365}
366
367static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, nfs4_stateid *stateid)
368{
369 int ret;
370
371 opendata->o_arg.open_flags = openflags;
372 ret = _nfs4_proc_open(opendata);
373 if (ret != 0)
374 return ret;
375 memcpy(stateid->data, opendata->o_res.stateid.data,
376 sizeof(stateid->data));
377 return 0;
378}
379
380static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
381{
382 nfs4_stateid stateid;
383 struct nfs4_state *newstate;
384 int mode = 0;
385 int delegation = 0;
386 int ret;
387
388 /* memory barrier prior to reading state->n_* */
389 smp_rmb();
390 if (state->n_rdwr != 0) {
391 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &stateid);
392 if (ret != 0)
393 return ret;
394 mode |= FMODE_READ|FMODE_WRITE;
395 if (opendata->o_res.delegation_type != 0)
396 delegation = opendata->o_res.delegation_type;
397 smp_rmb();
398 }
399 if (state->n_wronly != 0) {
400 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &stateid);
401 if (ret != 0)
402 return ret;
403 mode |= FMODE_WRITE;
404 if (opendata->o_res.delegation_type != 0)
405 delegation = opendata->o_res.delegation_type;
406 smp_rmb();
407 }
408 if (state->n_rdonly != 0) {
409 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &stateid);
410 if (ret != 0)
411 return ret;
412 mode |= FMODE_READ;
413 }
414 clear_bit(NFS_DELEGATED_STATE, &state->flags);
415 if (mode == 0)
416 return 0;
417 if (opendata->o_res.delegation_type == 0)
418 opendata->o_res.delegation_type = delegation;
419 opendata->o_arg.open_flags |= mode;
420 newstate = nfs4_opendata_to_nfs4_state(opendata);
421 if (newstate != NULL) {
422 if (opendata->o_res.delegation_type != 0) {
423 struct nfs_inode *nfsi = NFS_I(newstate->inode);
424 int delegation_flags = 0;
425 if (nfsi->delegation)
426 delegation_flags = nfsi->delegation->flags;
427 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
428 nfs_inode_set_delegation(newstate->inode,
429 opendata->owner->so_cred,
430 &opendata->o_res);
431 else
432 nfs_inode_reclaim_delegation(newstate->inode,
433 opendata->owner->so_cred,
434 &opendata->o_res);
435 }
436 nfs4_close_state(newstate, opendata->o_arg.open_flags);
437 }
438 if (newstate != state)
439 return -ESTALE;
440 return 0;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * OPEN_RECLAIM:
445 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Trond Myklebust864472e2006-01-03 09:55:15 +0100447static int _nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Trond Myklebust864472e2006-01-03 09:55:15 +0100449 struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
450 struct nfs4_opendata *opendata;
451 int delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int status;
453
454 if (delegation != NULL) {
455 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
456 memcpy(&state->stateid, &delegation->stateid,
457 sizeof(state->stateid));
458 set_bit(NFS_DELEGATED_STATE, &state->flags);
459 return 0;
460 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100461 delegation_type = delegation->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100463 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
464 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700465 return -ENOMEM;
Trond Myklebust864472e2006-01-03 09:55:15 +0100466 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
467 opendata->o_arg.fh = NFS_FH(state->inode);
468 nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
469 opendata->o_arg.u.delegation_type = delegation_type;
470 status = nfs4_open_recover(opendata, state);
471 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return status;
473}
474
Trond Myklebust864472e2006-01-03 09:55:15 +0100475static int nfs4_do_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct nfs_server *server = NFS_SERVER(state->inode);
478 struct nfs4_exception exception = { };
479 int err;
480 do {
Trond Myklebust864472e2006-01-03 09:55:15 +0100481 err = _nfs4_do_open_reclaim(sp, state, dentry);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000482 if (err != -NFS4ERR_DELAY)
483 break;
484 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 } while (exception.retry);
486 return err;
487}
488
Trond Myklebust864472e2006-01-03 09:55:15 +0100489static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
490{
491 struct nfs_open_context *ctx;
492 int ret;
493
494 ctx = nfs4_state_find_open_context(state);
495 if (IS_ERR(ctx))
496 return PTR_ERR(ctx);
497 ret = nfs4_do_open_reclaim(sp, state, ctx->dentry);
498 put_nfs_open_context(ctx);
499 return ret;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
503{
504 struct nfs4_state_owner *sp = state->owner;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100505 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100506 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust864472e2006-01-03 09:55:15 +0100509 return 0;
510 opendata = nfs4_opendata_alloc(dentry, sp, 0, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100511 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100512 return -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100513 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100514 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
515 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100516 ret = nfs4_open_recover(opendata, state);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100517 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
522{
523 struct nfs4_exception exception = { };
524 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
525 int err;
526 do {
527 err = _nfs4_open_delegation_recall(dentry, state);
528 switch (err) {
529 case 0:
530 return err;
531 case -NFS4ERR_STALE_CLIENTID:
532 case -NFS4ERR_STALE_STATEID:
533 case -NFS4ERR_EXPIRED:
534 /* Don't recall a delegation if it was lost */
535 nfs4_schedule_state_recovery(server->nfs4_state);
536 return err;
537 }
538 err = nfs4_handle_exception(server, err, &exception);
539 } while (exception.retry);
540 return err;
541}
542
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100543static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100545 struct nfs4_opendata *data = calldata;
546 struct rpc_message msg = {
547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
548 .rpc_argp = &data->c_arg,
549 .rpc_resp = &data->c_res,
550 .rpc_cred = data->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 };
Trond Myklebust26e976a2006-01-03 09:55:21 +0100552 data->timestamp = jiffies;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100553 rpc_call_setup(task, &msg, 0);
554}
555
556static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
557{
558 struct nfs4_opendata *data = calldata;
559
560 data->rpc_status = task->tk_status;
561 if (RPC_ASSASSINATED(task))
562 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100563 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100564 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
565 sizeof(data->o_res.stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +0100566 renew_lease(data->o_res.server, data->timestamp);
567 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100568 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
569 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
570}
571
572static void nfs4_open_confirm_release(void *calldata)
573{
574 struct nfs4_opendata *data = calldata;
575 struct nfs4_state *state = NULL;
576
577 /* If this request hasn't been cancelled, do nothing */
578 if (data->cancelled == 0)
579 goto out_free;
580 /* In case of error, no cleanup! */
581 if (data->rpc_status != 0)
582 goto out_free;
583 nfs_confirm_seqid(&data->owner->so_seqid, 0);
584 state = nfs4_opendata_to_nfs4_state(data);
585 if (state != NULL)
586 nfs4_close_state(state, data->o_arg.open_flags);
587out_free:
588 nfs4_opendata_free(data);
589}
590
591static const struct rpc_call_ops nfs4_open_confirm_ops = {
592 .rpc_call_prepare = nfs4_open_confirm_prepare,
593 .rpc_call_done = nfs4_open_confirm_done,
594 .rpc_release = nfs4_open_confirm_release,
595};
596
597/*
598 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
599 */
600static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
601{
602 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
603 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int status;
605
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100606 atomic_inc(&data->count);
607 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
608 if (IS_ERR(task)) {
609 nfs4_opendata_free(data);
610 return PTR_ERR(task);
611 }
612 status = nfs4_wait_for_completion_rpc_task(task);
613 if (status != 0) {
614 data->cancelled = 1;
615 smp_wmb();
616 } else
617 status = data->rpc_status;
618 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return status;
620}
621
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100622static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100624 struct nfs4_opendata *data = calldata;
625 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct rpc_message msg = {
627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100628 .rpc_argp = &data->o_arg,
629 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .rpc_cred = sp->so_cred,
631 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100632
633 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
634 return;
635 /* Update sequence id. */
636 data->o_arg.id = sp->so_id;
637 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust864472e2006-01-03 09:55:15 +0100638 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
639 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust26e976a2006-01-03 09:55:21 +0100640 data->timestamp = jiffies;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100641 rpc_call_setup(task, &msg, 0);
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100644static void nfs4_open_done(struct rpc_task *task, void *calldata)
645{
646 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100648 data->rpc_status = task->tk_status;
649 if (RPC_ASSASSINATED(task))
650 return;
651 if (task->tk_status == 0) {
652 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700653 case S_IFREG:
654 break;
655 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100656 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700657 break;
658 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100659 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700660 break;
661 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100662 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700663 }
Trond Myklebust26e976a2006-01-03 09:55:21 +0100664 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100666 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
667}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700668
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100669static void nfs4_open_release(void *calldata)
670{
671 struct nfs4_opendata *data = calldata;
672 struct nfs4_state *state = NULL;
673
674 /* If this request hasn't been cancelled, do nothing */
675 if (data->cancelled == 0)
676 goto out_free;
677 /* In case of error, no cleanup! */
678 if (data->rpc_status != 0)
679 goto out_free;
680 /* In case we need an open_confirm, no cleanup! */
681 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
682 goto out_free;
683 nfs_confirm_seqid(&data->owner->so_seqid, 0);
684 state = nfs4_opendata_to_nfs4_state(data);
685 if (state != NULL)
686 nfs4_close_state(state, data->o_arg.open_flags);
687out_free:
688 nfs4_opendata_free(data);
689}
690
691static const struct rpc_call_ops nfs4_open_ops = {
692 .rpc_call_prepare = nfs4_open_prepare,
693 .rpc_call_done = nfs4_open_done,
694 .rpc_release = nfs4_open_release,
695};
696
697/*
698 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
699 */
700static int _nfs4_proc_open(struct nfs4_opendata *data)
701{
702 struct inode *dir = data->dir->d_inode;
703 struct nfs_server *server = NFS_SERVER(dir);
704 struct nfs_openargs *o_arg = &data->o_arg;
705 struct nfs_openres *o_res = &data->o_res;
706 struct rpc_task *task;
707 int status;
708
709 atomic_inc(&data->count);
710 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
711 if (IS_ERR(task)) {
712 nfs4_opendata_free(data);
713 return PTR_ERR(task);
714 }
715 status = nfs4_wait_for_completion_rpc_task(task);
716 if (status != 0) {
717 data->cancelled = 1;
718 smp_wmb();
719 } else
720 status = data->rpc_status;
721 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100723 return status;
724
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400725 if (o_arg->open_flags & O_CREAT) {
726 update_changeattr(dir, &o_res->cinfo);
727 nfs_post_op_update_inode(dir, o_res->dir_attr);
728 } else
729 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100731 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100733 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100735 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100737 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
742{
743 struct nfs_access_entry cache;
744 int mask = 0;
745 int status;
746
747 if (openflags & FMODE_READ)
748 mask |= MAY_READ;
749 if (openflags & FMODE_WRITE)
750 mask |= MAY_WRITE;
751 status = nfs_access_get_cached(inode, cred, &cache);
752 if (status == 0)
753 goto out;
754
755 /* Be clever: ask server to check for all possible rights */
756 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
757 cache.cred = cred;
758 cache.jiffies = jiffies;
759 status = _nfs4_proc_access(inode, &cache);
760 if (status != 0)
761 return status;
762 nfs_access_add_cache(inode, &cache);
763out:
764 if ((cache.mask & mask) == mask)
765 return 0;
766 return -EACCES;
767}
768
Trond Myklebust58d97142006-01-03 09:55:24 +0100769int nfs4_recover_expired_lease(struct nfs_server *server)
770{
771 struct nfs4_client *clp = server->nfs4_state;
772
773 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
774 nfs4_schedule_state_recovery(clp);
775 return nfs4_wait_clnt_recover(server->client, clp);
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * OPEN_EXPIRED:
780 * reclaim state on the server after a network partition.
781 * Assumes caller holds the appropriate lock
782 */
783static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100788 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Trond Myklebust864472e2006-01-03 09:55:15 +0100789 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust864472e2006-01-03 09:55:15 +0100792 ret = _nfs4_do_access(inode, sp->so_cred, openflags);
793 if (ret < 0)
794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
796 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100799 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 if (opendata == NULL)
Trond Myklebust864472e2006-01-03 09:55:15 +0100801 return -ENOMEM;
802 ret = nfs4_open_recover(opendata, state);
803 if (ret == -ESTALE) {
804 /* Invalidate the state owner so we don't ever use it again */
805 nfs4_drop_state_owner(sp);
806 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 nfs4_opendata_free(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100809 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Trond Myklebust202b50d2005-06-22 17:16:29 +0000812static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
813{
814 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
815 struct nfs4_exception exception = { };
816 int err;
817
818 do {
819 err = _nfs4_open_expired(sp, state, dentry);
820 if (err == -NFS4ERR_DELAY)
821 nfs4_handle_exception(server, err, &exception);
822 } while (exception.retry);
823 return err;
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +0100829 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Trond Myklebust864472e2006-01-03 09:55:15 +0100831 ctx = nfs4_state_find_open_context(state);
832 if (IS_ERR(ctx))
833 return PTR_ERR(ctx);
834 ret = nfs4_do_open_expired(sp, state, ctx->dentry);
835 put_nfs_open_context(ctx);
836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100840 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
842static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
843{
844 struct nfs_delegation *delegation;
845 struct nfs_server *server = NFS_SERVER(inode);
846 struct nfs4_client *clp = server->nfs4_state;
847 struct nfs_inode *nfsi = NFS_I(inode);
848 struct nfs4_state_owner *sp = NULL;
849 struct nfs4_state *state = NULL;
850 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
851 int err;
852
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100853 err = -ENOMEM;
854 if (!(sp = nfs4_get_state_owner(server, cred))) {
855 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
856 return err;
857 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100858 err = nfs4_recover_expired_lease(server);
859 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100860 goto out_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* Protect against reboot recovery - NOTE ORDER! */
862 down_read(&clp->cl_sem);
863 /* Protect against delegation recall */
864 down_read(&nfsi->rwsem);
865 delegation = NFS_I(inode)->delegation;
866 err = -ENOENT;
867 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
868 goto out_err;
869 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 state = nfs4_get_open_state(inode, sp);
871 if (state == NULL)
872 goto out_err;
873
874 err = -ENOENT;
875 if ((state->state & open_flags) == open_flags) {
876 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100877 update_open_stateflags(state, open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 spin_unlock(&inode->i_lock);
879 goto out_ok;
880 } else if (state->state != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100881 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 lock_kernel();
884 err = _nfs4_do_access(inode, cred, open_flags);
885 unlock_kernel();
886 if (err != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100887 goto out_put_open_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 set_bit(NFS_DELEGATED_STATE, &state->flags);
889 update_open_stateid(state, &delegation->stateid, open_flags);
890out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 nfs4_put_state_owner(sp);
892 up_read(&nfsi->rwsem);
893 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *res = state;
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100895 return 0;
896out_put_open_state:
897 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 up_read(&nfsi->rwsem);
900 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700901 if (err != -EACCES)
902 nfs_inode_return_delegation(inode);
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100903out_put_state_owner:
904 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return err;
906}
907
908static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
909{
910 struct nfs4_exception exception = { };
911 struct nfs4_state *res;
912 int err;
913
914 do {
915 err = _nfs4_open_delegated(inode, flags, cred, &res);
916 if (err == 0)
917 break;
918 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
919 err, &exception));
920 } while (exception.retry);
921 return res;
922}
923
924/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100925 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 */
927static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
928{
929 struct nfs4_state_owner *sp;
930 struct nfs4_state *state = NULL;
931 struct nfs_server *server = NFS_SERVER(dir);
932 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100933 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 status = -ENOMEM;
938 if (!(sp = nfs4_get_state_owner(server, cred))) {
939 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
940 goto out_err;
941 }
Trond Myklebust58d97142006-01-03 09:55:24 +0100942 status = nfs4_recover_expired_lease(server);
943 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100944 goto err_put_state_owner;
Trond Myklebust58d97142006-01-03 09:55:24 +0100945 down_read(&clp->cl_sem);
946 status = -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100947 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
948 if (opendata == NULL)
949 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100951 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (status != 0)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100953 goto err_opendata_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100956 state = nfs4_opendata_to_nfs4_state(opendata);
957 if (state == NULL)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100958 goto err_opendata_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100959 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100960 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 nfs4_put_state_owner(sp);
963 up_read(&clp->cl_sem);
964 *res = state;
965 return 0;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966err_opendata_free:
967 nfs4_opendata_free(opendata);
968err_put_state_owner:
969 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
972 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *res = NULL;
974 return status;
975}
976
977
978static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
979{
980 struct nfs4_exception exception = { };
981 struct nfs4_state *res;
982 int status;
983
984 do {
985 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
986 if (status == 0)
987 break;
988 /* NOTE: BAD_SEQID means the server and client disagree about the
989 * book-keeping w.r.t. state-changing operations
990 * (OPEN/CLOSE/LOCK/LOCKU...)
991 * It is actually a sign of a bug on the client or on the server.
992 *
993 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700994 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * have unhashed the old state_owner for us, and that we can
996 * therefore safely retry using a new one. We should still warn
997 * the user though...
998 */
999 if (status == -NFS4ERR_BAD_SEQID) {
1000 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
1001 exception.retry = 1;
1002 continue;
1003 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001004 /*
1005 * BAD_STATEID on OPEN means that the server cancelled our
1006 * state before it received the OPEN_CONFIRM.
1007 * Recover by retrying the request as per the discussion
1008 * on Page 181 of RFC3530.
1009 */
1010 if (status == -NFS4ERR_BAD_STATEID) {
1011 exception.retry = 1;
1012 continue;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1015 status, &exception));
1016 } while (exception.retry);
1017 return res;
1018}
1019
1020static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1021 struct nfs_fh *fhandle, struct iattr *sattr,
1022 struct nfs4_state *state)
1023{
1024 struct nfs_setattrargs arg = {
1025 .fh = fhandle,
1026 .iap = sattr,
1027 .server = server,
1028 .bitmask = server->attr_bitmask,
1029 };
1030 struct nfs_setattrres res = {
1031 .fattr = fattr,
1032 .server = server,
1033 };
1034 struct rpc_message msg = {
1035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1036 .rpc_argp = &arg,
1037 .rpc_resp = &res,
1038 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001039 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001040 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebust0e574af2005-10-27 22:12:38 -04001042 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001044 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001046 nfs4_copy_stateid(&arg.stateid, state, current->files);
1047 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1049
Trond Myklebust65e43082005-08-16 11:49:44 -04001050 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001051 if (status == 0 && state != NULL)
1052 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001053 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1057 struct nfs_fh *fhandle, struct iattr *sattr,
1058 struct nfs4_state *state)
1059{
1060 struct nfs4_exception exception = { };
1061 int err;
1062 do {
1063 err = nfs4_handle_exception(server,
1064 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1065 state),
1066 &exception);
1067 } while (exception.retry);
1068 return err;
1069}
1070
1071struct nfs4_closedata {
1072 struct inode *inode;
1073 struct nfs4_state *state;
1074 struct nfs_closeargs arg;
1075 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001076 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001077 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078};
1079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001080static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001082 struct nfs4_closedata *calldata = data;
1083 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001084
1085 nfs4_put_open_state(calldata->state);
1086 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001087 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001088 kfree(calldata);
1089}
1090
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001091static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001093 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct nfs_server *server = NFS_SERVER(calldata->inode);
1096
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001097 if (RPC_ASSASSINATED(task))
1098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* hmm. we are done with the inode, and in the process of freeing
1100 * the state_owner. we keep this around to process errors
1101 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001102 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 switch (task->tk_status) {
1104 case 0:
1105 memcpy(&state->stateid, &calldata->res.stateid,
1106 sizeof(state->stateid));
Trond Myklebust26e976a2006-01-03 09:55:21 +01001107 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 case -NFS4ERR_STALE_STATEID:
1110 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 nfs4_schedule_state_recovery(server->nfs4_state);
1112 break;
1113 default:
1114 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1115 rpc_restart_call(task);
1116 return;
1117 }
1118 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001119 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001122static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001124 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001125 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct rpc_message msg = {
1127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1128 .rpc_argp = &calldata->arg,
1129 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001130 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001132 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001133
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001134 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001135 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001136 /* Recalculate the new open mode in case someone reopened the file
1137 * while we were waiting in line to be scheduled.
1138 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001139 spin_lock(&state->owner->so_lock);
1140 spin_lock(&calldata->inode->i_lock);
1141 mode = old_mode = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +01001142 if (state->n_rdwr == 0) {
1143 if (state->n_rdonly == 0)
1144 mode &= ~FMODE_READ;
1145 if (state->n_wronly == 0)
1146 mode &= ~FMODE_WRITE;
1147 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001148 nfs4_state_set_mode_locked(state, mode);
1149 spin_unlock(&calldata->inode->i_lock);
1150 spin_unlock(&state->owner->so_lock);
1151 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001152 /* Note: exit _without_ calling nfs4_close_done */
1153 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001154 return;
1155 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001156 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001157 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001159 calldata->arg.open_flags = mode;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001160 calldata->timestamp = jiffies;
Trond Myklebust95121352005-10-18 14:20:12 -07001161 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001164static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001165 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001166 .rpc_call_done = nfs4_close_done,
1167 .rpc_release = nfs4_free_closedata,
1168};
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * It is possible for data to be read/written from a mem-mapped file
1172 * after the sys_close call (which hits the vfs layer as a flush).
1173 * This means that we can't safely call nfsv4 close on a file until
1174 * the inode is cleared. This in turn means that we are not good
1175 * NFSv4 citizens - we do not indicate to the server to update the file's
1176 * share state even when we are done with one of the three share
1177 * stateid's in the inode.
1178 *
1179 * NOTE: Caller must be holding the sp->so_owner semaphore!
1180 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001181int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Trond Myklebust516a6af2005-10-27 22:12:41 -04001183 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -07001185 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Trond Myklebust95121352005-10-18 14:20:12 -07001187 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 calldata->inode = inode;
1191 calldata->state = state;
1192 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001193 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001195 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001196 if (calldata->arg.seqid == NULL)
1197 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001198 calldata->arg.bitmask = server->attr_bitmask;
1199 calldata->res.fattr = &calldata->fattr;
1200 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -07001201
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001202 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -07001203 if (status == 0)
1204 goto out;
1205
1206 nfs_free_seqid(calldata->arg.seqid);
1207out_free_calldata:
1208 kfree(calldata);
1209out:
1210 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Trond Myklebust02a913a2005-10-18 14:20:17 -07001213static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1214{
1215 struct file *filp;
1216
1217 filp = lookup_instantiate_filp(nd, dentry, NULL);
1218 if (!IS_ERR(filp)) {
1219 struct nfs_open_context *ctx;
1220 ctx = (struct nfs_open_context *)filp->private_data;
1221 ctx->state = state;
1222 } else
1223 nfs4_close_state(state, nd->intent.open.flags);
1224}
1225
1226struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1228{
1229 struct iattr attr;
1230 struct rpc_cred *cred;
1231 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001232 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (nd->flags & LOOKUP_CREATE) {
1235 attr.ia_mode = nd->intent.open.create_mode;
1236 attr.ia_valid = ATTR_MODE;
1237 if (!IS_POSIXACL(dir))
1238 attr.ia_mode &= ~current->fs->umask;
1239 } else {
1240 attr.ia_valid = 0;
1241 BUG_ON(nd->intent.open.flags & O_CREAT);
1242 }
1243
1244 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1245 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001246 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1248 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001249 if (IS_ERR(state)) {
1250 if (PTR_ERR(state) == -ENOENT)
1251 d_add(dentry, NULL);
1252 return (struct dentry *)state;
1253 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001254 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001255 if (res != NULL)
1256 dentry = res;
1257 nfs4_intent_set_file(nd, dentry, state);
1258 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001262nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct rpc_cred *cred;
1265 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1268 if (IS_ERR(cred))
1269 return PTR_ERR(cred);
1270 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1271 if (IS_ERR(state))
1272 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1273 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001274 if (IS_ERR(state)) {
1275 switch (PTR_ERR(state)) {
1276 case -EPERM:
1277 case -EACCES:
1278 case -EDQUOT:
1279 case -ENOSPC:
1280 case -EROFS:
1281 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1282 return 1;
1283 case -ENOENT:
1284 if (dentry->d_inode == NULL)
1285 return 1;
1286 }
1287 goto out_drop;
1288 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001289 if (state->inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001290 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 1;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001294out_drop:
1295 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298
1299
1300static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1301{
1302 struct nfs4_server_caps_res res = {};
1303 struct rpc_message msg = {
1304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1305 .rpc_argp = fhandle,
1306 .rpc_resp = &res,
1307 };
1308 int status;
1309
1310 status = rpc_call_sync(server->client, &msg, 0);
1311 if (status == 0) {
1312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1313 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1314 server->caps |= NFS_CAP_ACLS;
1315 if (res.has_links != 0)
1316 server->caps |= NFS_CAP_HARDLINKS;
1317 if (res.has_symlinks != 0)
1318 server->caps |= NFS_CAP_SYMLINKS;
1319 server->acl_bitmask = res.acl_bitmask;
1320 }
1321 return status;
1322}
1323
1324static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1325{
1326 struct nfs4_exception exception = { };
1327 int err;
1328 do {
1329 err = nfs4_handle_exception(server,
1330 _nfs4_server_capabilities(server, fhandle),
1331 &exception);
1332 } while (exception.retry);
1333 return err;
1334}
1335
1336static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1337 struct nfs_fsinfo *info)
1338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct nfs4_lookup_root_arg args = {
1340 .bitmask = nfs4_fattr_bitmap,
1341 };
1342 struct nfs4_lookup_res res = {
1343 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001344 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .fh = fhandle,
1346 };
1347 struct rpc_message msg = {
1348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1349 .rpc_argp = &args,
1350 .rpc_resp = &res,
1351 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001352 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return rpc_call_sync(server->client, &msg, 0);
1354}
1355
1356static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1357 struct nfs_fsinfo *info)
1358{
1359 struct nfs4_exception exception = { };
1360 int err;
1361 do {
1362 err = nfs4_handle_exception(server,
1363 _nfs4_lookup_root(server, fhandle, info),
1364 &exception);
1365 } while (exception.retry);
1366 return err;
1367}
1368
1369static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1370 struct nfs_fsinfo *info)
1371{
1372 struct nfs_fattr * fattr = info->fattr;
1373 unsigned char * p;
1374 struct qstr q;
1375 struct nfs4_lookup_arg args = {
1376 .dir_fh = fhandle,
1377 .name = &q,
1378 .bitmask = nfs4_fattr_bitmap,
1379 };
1380 struct nfs4_lookup_res res = {
1381 .server = server,
1382 .fattr = fattr,
1383 .fh = fhandle,
1384 };
1385 struct rpc_message msg = {
1386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1387 .rpc_argp = &args,
1388 .rpc_resp = &res,
1389 };
1390 int status;
1391
1392 /*
1393 * Now we do a separate LOOKUP for each component of the mount path.
1394 * The LOOKUPs are done separately so that we can conveniently
1395 * catch an ERR_WRONGSEC if it occurs along the way...
1396 */
1397 status = nfs4_lookup_root(server, fhandle, info);
1398 if (status)
1399 goto out;
1400
1401 p = server->mnt_path;
1402 for (;;) {
1403 struct nfs4_exception exception = { };
1404
1405 while (*p == '/')
1406 p++;
1407 if (!*p)
1408 break;
1409 q.name = p;
1410 while (*p && (*p != '/'))
1411 p++;
1412 q.len = p - q.name;
1413
1414 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001415 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 status = nfs4_handle_exception(server,
1417 rpc_call_sync(server->client, &msg, 0),
1418 &exception);
1419 } while (exception.retry);
1420 if (status == 0)
1421 continue;
1422 if (status == -ENOENT) {
1423 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1424 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1425 }
1426 break;
1427 }
1428 if (status == 0)
1429 status = nfs4_server_capabilities(server, fhandle);
1430 if (status == 0)
1431 status = nfs4_do_fsinfo(server, fhandle, info);
1432out:
1433 return status;
1434}
1435
1436static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1437{
1438 struct nfs4_getattr_arg args = {
1439 .fh = fhandle,
1440 .bitmask = server->attr_bitmask,
1441 };
1442 struct nfs4_getattr_res res = {
1443 .fattr = fattr,
1444 .server = server,
1445 };
1446 struct rpc_message msg = {
1447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1448 .rpc_argp = &args,
1449 .rpc_resp = &res,
1450 };
1451
Trond Myklebust0e574af2005-10-27 22:12:38 -04001452 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return rpc_call_sync(server->client, &msg, 0);
1454}
1455
1456static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1457{
1458 struct nfs4_exception exception = { };
1459 int err;
1460 do {
1461 err = nfs4_handle_exception(server,
1462 _nfs4_proc_getattr(server, fhandle, fattr),
1463 &exception);
1464 } while (exception.retry);
1465 return err;
1466}
1467
1468/*
1469 * The file is not closed if it is opened due to the a request to change
1470 * the size of the file. The open call will not be needed once the
1471 * VFS layer lookup-intents are implemented.
1472 *
1473 * Close is called when the inode is destroyed.
1474 * If we haven't opened the file for O_WRONLY, we
1475 * need to in the size_change case to obtain a stateid.
1476 *
1477 * Got race?
1478 * Because OPEN is always done by name in nfsv4, it is
1479 * possible that we opened a different file by the same
1480 * name. We can recognize this race condition, but we
1481 * can't do anything about it besides returning an error.
1482 *
1483 * This will be fixed with VFS changes (lookup-intent).
1484 */
1485static int
1486nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1487 struct iattr *sattr)
1488{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001489 struct rpc_cred *cred;
1490 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001491 struct nfs_open_context *ctx;
1492 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 int status;
1494
Trond Myklebust0e574af2005-10-27 22:12:38 -04001495 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001497 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1498 if (IS_ERR(cred))
1499 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001500
1501 /* Search for an existing open(O_WRITE) file */
1502 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1503 if (ctx != NULL)
1504 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1507 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001508 if (status == 0)
1509 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001510 if (ctx != NULL)
1511 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001512 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return status;
1514}
1515
1516static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1517 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1518{
1519 int status;
1520 struct nfs_server *server = NFS_SERVER(dir);
1521 struct nfs4_lookup_arg args = {
1522 .bitmask = server->attr_bitmask,
1523 .dir_fh = NFS_FH(dir),
1524 .name = name,
1525 };
1526 struct nfs4_lookup_res res = {
1527 .server = server,
1528 .fattr = fattr,
1529 .fh = fhandle,
1530 };
1531 struct rpc_message msg = {
1532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1533 .rpc_argp = &args,
1534 .rpc_resp = &res,
1535 };
1536
Trond Myklebust0e574af2005-10-27 22:12:38 -04001537 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 dprintk("NFS call lookup %s\n", name->name);
1540 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1541 dprintk("NFS reply lookup: %d\n", status);
1542 return status;
1543}
1544
1545static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1546{
1547 struct nfs4_exception exception = { };
1548 int err;
1549 do {
1550 err = nfs4_handle_exception(NFS_SERVER(dir),
1551 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1552 &exception);
1553 } while (exception.retry);
1554 return err;
1555}
1556
1557static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1558{
1559 struct nfs4_accessargs args = {
1560 .fh = NFS_FH(inode),
1561 };
1562 struct nfs4_accessres res = { 0 };
1563 struct rpc_message msg = {
1564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1565 .rpc_argp = &args,
1566 .rpc_resp = &res,
1567 .rpc_cred = entry->cred,
1568 };
1569 int mode = entry->mask;
1570 int status;
1571
1572 /*
1573 * Determine which access bits we want to ask for...
1574 */
1575 if (mode & MAY_READ)
1576 args.access |= NFS4_ACCESS_READ;
1577 if (S_ISDIR(inode->i_mode)) {
1578 if (mode & MAY_WRITE)
1579 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1580 if (mode & MAY_EXEC)
1581 args.access |= NFS4_ACCESS_LOOKUP;
1582 } else {
1583 if (mode & MAY_WRITE)
1584 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1585 if (mode & MAY_EXEC)
1586 args.access |= NFS4_ACCESS_EXECUTE;
1587 }
1588 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1589 if (!status) {
1590 entry->mask = 0;
1591 if (res.access & NFS4_ACCESS_READ)
1592 entry->mask |= MAY_READ;
1593 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1594 entry->mask |= MAY_WRITE;
1595 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1596 entry->mask |= MAY_EXEC;
1597 }
1598 return status;
1599}
1600
1601static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1602{
1603 struct nfs4_exception exception = { };
1604 int err;
1605 do {
1606 err = nfs4_handle_exception(NFS_SERVER(inode),
1607 _nfs4_proc_access(inode, entry),
1608 &exception);
1609 } while (exception.retry);
1610 return err;
1611}
1612
1613/*
1614 * TODO: For the time being, we don't try to get any attributes
1615 * along with any of the zero-copy operations READ, READDIR,
1616 * READLINK, WRITE.
1617 *
1618 * In the case of the first three, we want to put the GETATTR
1619 * after the read-type operation -- this is because it is hard
1620 * to predict the length of a GETATTR response in v4, and thus
1621 * align the READ data correctly. This means that the GETATTR
1622 * may end up partially falling into the page cache, and we should
1623 * shift it into the 'tail' of the xdr_buf before processing.
1624 * To do this efficiently, we need to know the total length
1625 * of data received, which doesn't seem to be available outside
1626 * of the RPC layer.
1627 *
1628 * In the case of WRITE, we also want to put the GETATTR after
1629 * the operation -- in this case because we want to make sure
1630 * we get the post-operation mtime and size. This means that
1631 * we can't use xdr_encode_pages() as written: we need a variant
1632 * of it which would leave room in the 'tail' iovec.
1633 *
1634 * Both of these changes to the XDR layer would in fact be quite
1635 * minor, but I decided to leave them for a subsequent patch.
1636 */
1637static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1638 unsigned int pgbase, unsigned int pglen)
1639{
1640 struct nfs4_readlink args = {
1641 .fh = NFS_FH(inode),
1642 .pgbase = pgbase,
1643 .pglen = pglen,
1644 .pages = &page,
1645 };
1646 struct rpc_message msg = {
1647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1648 .rpc_argp = &args,
1649 .rpc_resp = NULL,
1650 };
1651
1652 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1653}
1654
1655static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1656 unsigned int pgbase, unsigned int pglen)
1657{
1658 struct nfs4_exception exception = { };
1659 int err;
1660 do {
1661 err = nfs4_handle_exception(NFS_SERVER(inode),
1662 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1663 &exception);
1664 } while (exception.retry);
1665 return err;
1666}
1667
1668static int _nfs4_proc_read(struct nfs_read_data *rdata)
1669{
1670 int flags = rdata->flags;
1671 struct inode *inode = rdata->inode;
1672 struct nfs_fattr *fattr = rdata->res.fattr;
1673 struct nfs_server *server = NFS_SERVER(inode);
1674 struct rpc_message msg = {
1675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1676 .rpc_argp = &rdata->args,
1677 .rpc_resp = &rdata->res,
1678 .rpc_cred = rdata->cred,
1679 };
1680 unsigned long timestamp = jiffies;
1681 int status;
1682
1683 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1684 (long long) rdata->args.offset);
1685
Trond Myklebust0e574af2005-10-27 22:12:38 -04001686 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 status = rpc_call_sync(server->client, &msg, flags);
1688 if (!status)
1689 renew_lease(server, timestamp);
1690 dprintk("NFS reply read: %d\n", status);
1691 return status;
1692}
1693
1694static int nfs4_proc_read(struct nfs_read_data *rdata)
1695{
1696 struct nfs4_exception exception = { };
1697 int err;
1698 do {
1699 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1700 _nfs4_proc_read(rdata),
1701 &exception);
1702 } while (exception.retry);
1703 return err;
1704}
1705
1706static int _nfs4_proc_write(struct nfs_write_data *wdata)
1707{
1708 int rpcflags = wdata->flags;
1709 struct inode *inode = wdata->inode;
1710 struct nfs_fattr *fattr = wdata->res.fattr;
1711 struct nfs_server *server = NFS_SERVER(inode);
1712 struct rpc_message msg = {
1713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1714 .rpc_argp = &wdata->args,
1715 .rpc_resp = &wdata->res,
1716 .rpc_cred = wdata->cred,
1717 };
1718 int status;
1719
1720 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1721 (long long) wdata->args.offset);
1722
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001723 wdata->args.bitmask = server->attr_bitmask;
1724 wdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001725 wdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001726 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 status = rpc_call_sync(server->client, &msg, rpcflags);
1728 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001729 if (status < 0)
1730 return status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001731 renew_lease(server, wdata->timestamp);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001732 nfs_post_op_update_inode(inode, fattr);
1733 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736static int nfs4_proc_write(struct nfs_write_data *wdata)
1737{
1738 struct nfs4_exception exception = { };
1739 int err;
1740 do {
1741 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1742 _nfs4_proc_write(wdata),
1743 &exception);
1744 } while (exception.retry);
1745 return err;
1746}
1747
1748static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1749{
1750 struct inode *inode = cdata->inode;
1751 struct nfs_fattr *fattr = cdata->res.fattr;
1752 struct nfs_server *server = NFS_SERVER(inode);
1753 struct rpc_message msg = {
1754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1755 .rpc_argp = &cdata->args,
1756 .rpc_resp = &cdata->res,
1757 .rpc_cred = cdata->cred,
1758 };
1759 int status;
1760
1761 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1762 (long long) cdata->args.offset);
1763
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001764 cdata->args.bitmask = server->attr_bitmask;
1765 cdata->res.server = server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001766 cdata->timestamp = jiffies;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001767 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001769 if (status >= 0)
1770 renew_lease(server, cdata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001772 if (status >= 0)
1773 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return status;
1775}
1776
1777static int nfs4_proc_commit(struct nfs_write_data *cdata)
1778{
1779 struct nfs4_exception exception = { };
1780 int err;
1781 do {
1782 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1783 _nfs4_proc_commit(cdata),
1784 &exception);
1785 } while (exception.retry);
1786 return err;
1787}
1788
1789/*
1790 * Got race?
1791 * We will need to arrange for the VFS layer to provide an atomic open.
1792 * Until then, this create/open method is prone to inefficiency and race
1793 * conditions due to the lookup, create, and open VFS calls from sys_open()
1794 * placed on the wire.
1795 *
1796 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1797 * The file will be opened again in the subsequent VFS open call
1798 * (nfs4_proc_file_open).
1799 *
1800 * The open for read will just hang around to be used by any process that
1801 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1802 */
1803
1804static int
1805nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001806 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 struct nfs4_state *state;
1809 struct rpc_cred *cred;
1810 int status = 0;
1811
1812 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1813 if (IS_ERR(cred)) {
1814 status = PTR_ERR(cred);
1815 goto out;
1816 }
1817 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1818 put_rpccred(cred);
1819 if (IS_ERR(state)) {
1820 status = PTR_ERR(state);
1821 goto out;
1822 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001823 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (flags & O_EXCL) {
1825 struct nfs_fattr fattr;
1826 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1827 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001828 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001829 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001830 }
1831 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1832 nfs4_intent_set_file(nd, dentry, state);
1833 else
1834 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out:
1836 return status;
1837}
1838
1839static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1840{
Trond Myklebust16e42952005-10-27 22:12:44 -04001841 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 struct nfs4_remove_arg args = {
1843 .fh = NFS_FH(dir),
1844 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001845 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001847 struct nfs_fattr dir_attr;
1848 struct nfs4_remove_res res = {
1849 .server = server,
1850 .dir_attr = &dir_attr,
1851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 struct rpc_message msg = {
1853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1854 .rpc_argp = &args,
1855 .rpc_resp = &res,
1856 };
1857 int status;
1858
Trond Myklebust16e42952005-10-27 22:12:44 -04001859 nfs_fattr_init(res.dir_attr);
1860 status = rpc_call_sync(server->client, &msg, 0);
1861 if (status == 0) {
1862 update_changeattr(dir, &res.cinfo);
1863 nfs_post_op_update_inode(dir, res.dir_attr);
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return status;
1866}
1867
1868static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1869{
1870 struct nfs4_exception exception = { };
1871 int err;
1872 do {
1873 err = nfs4_handle_exception(NFS_SERVER(dir),
1874 _nfs4_proc_remove(dir, name),
1875 &exception);
1876 } while (exception.retry);
1877 return err;
1878}
1879
1880struct unlink_desc {
1881 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001882 struct nfs4_remove_res res;
1883 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884};
1885
1886static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1887 struct qstr *name)
1888{
Trond Myklebust16e42952005-10-27 22:12:44 -04001889 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct unlink_desc *up;
1891
1892 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1893 if (!up)
1894 return -ENOMEM;
1895
1896 up->args.fh = NFS_FH(dir->d_inode);
1897 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001898 up->args.bitmask = server->attr_bitmask;
1899 up->res.server = server;
1900 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1903 msg->rpc_argp = &up->args;
1904 msg->rpc_resp = &up->res;
1905 return 0;
1906}
1907
1908static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1909{
1910 struct rpc_message *msg = &task->tk_msg;
1911 struct unlink_desc *up;
1912
1913 if (msg->rpc_resp != NULL) {
1914 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001915 update_changeattr(dir->d_inode, &up->res.cinfo);
1916 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 kfree(up);
1918 msg->rpc_resp = NULL;
1919 msg->rpc_argp = NULL;
1920 }
1921 return 0;
1922}
1923
1924static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1925 struct inode *new_dir, struct qstr *new_name)
1926{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001927 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct nfs4_rename_arg arg = {
1929 .old_dir = NFS_FH(old_dir),
1930 .new_dir = NFS_FH(new_dir),
1931 .old_name = old_name,
1932 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001933 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001935 struct nfs_fattr old_fattr, new_fattr;
1936 struct nfs4_rename_res res = {
1937 .server = server,
1938 .old_fattr = &old_fattr,
1939 .new_fattr = &new_fattr,
1940 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct rpc_message msg = {
1942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1943 .rpc_argp = &arg,
1944 .rpc_resp = &res,
1945 };
1946 int status;
1947
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001948 nfs_fattr_init(res.old_fattr);
1949 nfs_fattr_init(res.new_fattr);
1950 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 if (!status) {
1953 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001954 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001956 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958 return status;
1959}
1960
1961static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1962 struct inode *new_dir, struct qstr *new_name)
1963{
1964 struct nfs4_exception exception = { };
1965 int err;
1966 do {
1967 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1968 _nfs4_proc_rename(old_dir, old_name,
1969 new_dir, new_name),
1970 &exception);
1971 } while (exception.retry);
1972 return err;
1973}
1974
1975static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1976{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001977 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct nfs4_link_arg arg = {
1979 .fh = NFS_FH(inode),
1980 .dir_fh = NFS_FH(dir),
1981 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001982 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001984 struct nfs_fattr fattr, dir_attr;
1985 struct nfs4_link_res res = {
1986 .server = server,
1987 .fattr = &fattr,
1988 .dir_attr = &dir_attr,
1989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 struct rpc_message msg = {
1991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1992 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001993 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 };
1995 int status;
1996
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001997 nfs_fattr_init(res.fattr);
1998 nfs_fattr_init(res.dir_attr);
1999 status = rpc_call_sync(server->client, &msg, 0);
2000 if (!status) {
2001 update_changeattr(dir, &res.cinfo);
2002 nfs_post_op_update_inode(dir, res.dir_attr);
2003 nfs_refresh_inode(inode, res.fattr);
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 return status;
2007}
2008
2009static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2010{
2011 struct nfs4_exception exception = { };
2012 int err;
2013 do {
2014 err = nfs4_handle_exception(NFS_SERVER(inode),
2015 _nfs4_proc_link(inode, dir, name),
2016 &exception);
2017 } while (exception.retry);
2018 return err;
2019}
2020
2021static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2022 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2023 struct nfs_fattr *fattr)
2024{
2025 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002026 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct nfs4_create_arg arg = {
2028 .dir_fh = NFS_FH(dir),
2029 .server = server,
2030 .name = name,
2031 .attrs = sattr,
2032 .ftype = NF4LNK,
2033 .bitmask = server->attr_bitmask,
2034 };
2035 struct nfs4_create_res res = {
2036 .server = server,
2037 .fh = fhandle,
2038 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002039 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 };
2041 struct rpc_message msg = {
2042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2043 .rpc_argp = &arg,
2044 .rpc_resp = &res,
2045 };
2046 int status;
2047
2048 if (path->len > NFS4_MAXPATHLEN)
2049 return -ENAMETOOLONG;
2050 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04002051 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002052 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2055 if (!status)
2056 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002057 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return status;
2059}
2060
2061static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2062 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2063 struct nfs_fattr *fattr)
2064{
2065 struct nfs4_exception exception = { };
2066 int err;
2067 do {
2068 err = nfs4_handle_exception(NFS_SERVER(dir),
2069 _nfs4_proc_symlink(dir, name, path, sattr,
2070 fhandle, fattr),
2071 &exception);
2072 } while (exception.retry);
2073 return err;
2074}
2075
2076static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2077 struct iattr *sattr)
2078{
2079 struct nfs_server *server = NFS_SERVER(dir);
2080 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002081 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct nfs4_create_arg arg = {
2083 .dir_fh = NFS_FH(dir),
2084 .server = server,
2085 .name = &dentry->d_name,
2086 .attrs = sattr,
2087 .ftype = NF4DIR,
2088 .bitmask = server->attr_bitmask,
2089 };
2090 struct nfs4_create_res res = {
2091 .server = server,
2092 .fh = &fhandle,
2093 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002094 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 };
2096 struct rpc_message msg = {
2097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2098 .rpc_argp = &arg,
2099 .rpc_resp = &res,
2100 };
2101 int status;
2102
Trond Myklebust0e574af2005-10-27 22:12:38 -04002103 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002104 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2107 if (!status) {
2108 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002109 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 status = nfs_instantiate(dentry, &fhandle, &fattr);
2111 }
2112 return status;
2113}
2114
2115static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2116 struct iattr *sattr)
2117{
2118 struct nfs4_exception exception = { };
2119 int err;
2120 do {
2121 err = nfs4_handle_exception(NFS_SERVER(dir),
2122 _nfs4_proc_mkdir(dir, dentry, sattr),
2123 &exception);
2124 } while (exception.retry);
2125 return err;
2126}
2127
2128static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2129 u64 cookie, struct page *page, unsigned int count, int plus)
2130{
2131 struct inode *dir = dentry->d_inode;
2132 struct nfs4_readdir_arg args = {
2133 .fh = NFS_FH(dir),
2134 .pages = &page,
2135 .pgbase = 0,
2136 .count = count,
2137 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2138 };
2139 struct nfs4_readdir_res res;
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2142 .rpc_argp = &args,
2143 .rpc_resp = &res,
2144 .rpc_cred = cred,
2145 };
2146 int status;
2147
Trond Myklebusteadf4592005-06-22 17:16:39 +00002148 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2149 dentry->d_parent->d_name.name,
2150 dentry->d_name.name,
2151 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 lock_kernel();
2153 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2154 res.pgbase = args.pgbase;
2155 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2156 if (status == 0)
2157 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2158 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00002159 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return status;
2161}
2162
2163static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2164 u64 cookie, struct page *page, unsigned int count, int plus)
2165{
2166 struct nfs4_exception exception = { };
2167 int err;
2168 do {
2169 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2170 _nfs4_proc_readdir(dentry, cred, cookie,
2171 page, count, plus),
2172 &exception);
2173 } while (exception.retry);
2174 return err;
2175}
2176
2177static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2178 struct iattr *sattr, dev_t rdev)
2179{
2180 struct nfs_server *server = NFS_SERVER(dir);
2181 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002182 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 struct nfs4_create_arg arg = {
2184 .dir_fh = NFS_FH(dir),
2185 .server = server,
2186 .name = &dentry->d_name,
2187 .attrs = sattr,
2188 .bitmask = server->attr_bitmask,
2189 };
2190 struct nfs4_create_res res = {
2191 .server = server,
2192 .fh = &fh,
2193 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002194 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 };
2196 struct rpc_message msg = {
2197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2198 .rpc_argp = &arg,
2199 .rpc_resp = &res,
2200 };
2201 int status;
2202 int mode = sattr->ia_mode;
2203
Trond Myklebust0e574af2005-10-27 22:12:38 -04002204 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002205 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2208 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2209 if (S_ISFIFO(mode))
2210 arg.ftype = NF4FIFO;
2211 else if (S_ISBLK(mode)) {
2212 arg.ftype = NF4BLK;
2213 arg.u.device.specdata1 = MAJOR(rdev);
2214 arg.u.device.specdata2 = MINOR(rdev);
2215 }
2216 else if (S_ISCHR(mode)) {
2217 arg.ftype = NF4CHR;
2218 arg.u.device.specdata1 = MAJOR(rdev);
2219 arg.u.device.specdata2 = MINOR(rdev);
2220 }
2221 else
2222 arg.ftype = NF4SOCK;
2223
2224 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2225 if (status == 0) {
2226 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002227 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 status = nfs_instantiate(dentry, &fh, &fattr);
2229 }
2230 return status;
2231}
2232
2233static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2234 struct iattr *sattr, dev_t rdev)
2235{
2236 struct nfs4_exception exception = { };
2237 int err;
2238 do {
2239 err = nfs4_handle_exception(NFS_SERVER(dir),
2240 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2241 &exception);
2242 } while (exception.retry);
2243 return err;
2244}
2245
2246static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2247 struct nfs_fsstat *fsstat)
2248{
2249 struct nfs4_statfs_arg args = {
2250 .fh = fhandle,
2251 .bitmask = server->attr_bitmask,
2252 };
2253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2255 .rpc_argp = &args,
2256 .rpc_resp = fsstat,
2257 };
2258
Trond Myklebust0e574af2005-10-27 22:12:38 -04002259 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return rpc_call_sync(server->client, &msg, 0);
2261}
2262
2263static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2264{
2265 struct nfs4_exception exception = { };
2266 int err;
2267 do {
2268 err = nfs4_handle_exception(server,
2269 _nfs4_proc_statfs(server, fhandle, fsstat),
2270 &exception);
2271 } while (exception.retry);
2272 return err;
2273}
2274
2275static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2276 struct nfs_fsinfo *fsinfo)
2277{
2278 struct nfs4_fsinfo_arg args = {
2279 .fh = fhandle,
2280 .bitmask = server->attr_bitmask,
2281 };
2282 struct rpc_message msg = {
2283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2284 .rpc_argp = &args,
2285 .rpc_resp = fsinfo,
2286 };
2287
2288 return rpc_call_sync(server->client, &msg, 0);
2289}
2290
2291static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2292{
2293 struct nfs4_exception exception = { };
2294 int err;
2295
2296 do {
2297 err = nfs4_handle_exception(server,
2298 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2299 &exception);
2300 } while (exception.retry);
2301 return err;
2302}
2303
2304static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2305{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002306 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2308}
2309
2310static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2311 struct nfs_pathconf *pathconf)
2312{
2313 struct nfs4_pathconf_arg args = {
2314 .fh = fhandle,
2315 .bitmask = server->attr_bitmask,
2316 };
2317 struct rpc_message msg = {
2318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2319 .rpc_argp = &args,
2320 .rpc_resp = pathconf,
2321 };
2322
2323 /* None of the pathconf attributes are mandatory to implement */
2324 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2325 memset(pathconf, 0, sizeof(*pathconf));
2326 return 0;
2327 }
2328
Trond Myklebust0e574af2005-10-27 22:12:38 -04002329 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return rpc_call_sync(server->client, &msg, 0);
2331}
2332
2333static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2334 struct nfs_pathconf *pathconf)
2335{
2336 struct nfs4_exception exception = { };
2337 int err;
2338
2339 do {
2340 err = nfs4_handle_exception(server,
2341 _nfs4_proc_pathconf(server, fhandle, pathconf),
2342 &exception);
2343 } while (exception.retry);
2344 return err;
2345}
2346
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002347static void nfs4_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002349 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct inode *inode = data->inode;
2351
2352 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2353 rpc_restart_call(task);
2354 return;
2355 }
2356 if (task->tk_status > 0)
2357 renew_lease(NFS_SERVER(inode), data->timestamp);
2358 /* Call back common NFS readpage processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002359 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002362static const struct rpc_call_ops nfs4_read_ops = {
2363 .rpc_call_done = nfs4_read_done,
2364 .rpc_release = nfs_readdata_release,
2365};
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367static void
2368nfs4_proc_read_setup(struct nfs_read_data *data)
2369{
2370 struct rpc_task *task = &data->task;
2371 struct rpc_message msg = {
2372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2373 .rpc_argp = &data->args,
2374 .rpc_resp = &data->res,
2375 .rpc_cred = data->cred,
2376 };
2377 struct inode *inode = data->inode;
2378 int flags;
2379
2380 data->timestamp = jiffies;
2381
2382 /* N.B. Do we need to test? Never called for swapfile inode */
2383 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2384
2385 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002386 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 rpc_call_setup(task, &msg, 0);
2388}
2389
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002390static void nfs4_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002392 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct inode *inode = data->inode;
2394
2395 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2396 rpc_restart_call(task);
2397 return;
2398 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002399 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002401 nfs_post_op_update_inode(inode, data->res.fattr);
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002404 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002407static const struct rpc_call_ops nfs4_write_ops = {
2408 .rpc_call_done = nfs4_write_done,
2409 .rpc_release = nfs_writedata_release,
2410};
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412static void
2413nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2414{
2415 struct rpc_task *task = &data->task;
2416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2418 .rpc_argp = &data->args,
2419 .rpc_resp = &data->res,
2420 .rpc_cred = data->cred,
2421 };
2422 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002423 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int stable;
2425 int flags;
2426
2427 if (how & FLUSH_STABLE) {
2428 if (!NFS_I(inode)->ncommit)
2429 stable = NFS_FILE_SYNC;
2430 else
2431 stable = NFS_DATA_SYNC;
2432 } else
2433 stable = NFS_UNSTABLE;
2434 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002435 data->args.bitmask = server->attr_bitmask;
2436 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 data->timestamp = jiffies;
2439
2440 /* Set the initial flags for the task. */
2441 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2442
2443 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002444 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 rpc_call_setup(task, &msg, 0);
2446}
2447
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002448static void nfs4_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002450 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct inode *inode = data->inode;
2452
2453 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2454 rpc_restart_call(task);
2455 return;
2456 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002457 if (task->tk_status >= 0)
2458 nfs_post_op_update_inode(inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002460 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002463static const struct rpc_call_ops nfs4_commit_ops = {
2464 .rpc_call_done = nfs4_commit_done,
2465 .rpc_release = nfs_commit_release,
2466};
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468static void
2469nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2470{
2471 struct rpc_task *task = &data->task;
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2474 .rpc_argp = &data->args,
2475 .rpc_resp = &data->res,
2476 .rpc_cred = data->cred,
2477 };
2478 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002479 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int flags;
2481
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002482 data->args.bitmask = server->attr_bitmask;
2483 data->res.server = server;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* Set the initial flags for the task. */
2486 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2487
2488 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002489 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 rpc_call_setup(task, &msg, 0);
2491}
2492
2493/*
2494 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2495 * standalone procedure for queueing an asynchronous RENEW.
2496 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002497static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002500 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 if (task->tk_status < 0) {
2503 switch (task->tk_status) {
2504 case -NFS4ERR_STALE_CLIENTID:
2505 case -NFS4ERR_EXPIRED:
2506 case -NFS4ERR_CB_PATH_DOWN:
2507 nfs4_schedule_state_recovery(clp);
2508 }
2509 return;
2510 }
2511 spin_lock(&clp->cl_lock);
2512 if (time_before(clp->cl_last_renewal,timestamp))
2513 clp->cl_last_renewal = timestamp;
2514 spin_unlock(&clp->cl_lock);
2515}
2516
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002517static const struct rpc_call_ops nfs4_renew_ops = {
2518 .rpc_call_done = nfs4_renew_done,
2519};
2520
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002521int nfs4_proc_async_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 struct rpc_message msg = {
2524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2525 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002526 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 };
2528
2529 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002530 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002533int nfs4_proc_renew(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 struct rpc_message msg = {
2536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2537 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002538 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 };
2540 unsigned long now = jiffies;
2541 int status;
2542
2543 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2544 if (status < 0)
2545 return status;
2546 spin_lock(&clp->cl_lock);
2547 if (time_before(clp->cl_last_renewal,now))
2548 clp->cl_last_renewal = now;
2549 spin_unlock(&clp->cl_lock);
2550 return 0;
2551}
2552
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002553static inline int nfs4_server_supports_acls(struct nfs_server *server)
2554{
2555 return (server->caps & NFS_CAP_ACLS)
2556 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2557 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2558}
2559
2560/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2561 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2562 * the stack.
2563 */
2564#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2565
2566static void buf_to_pages(const void *buf, size_t buflen,
2567 struct page **pages, unsigned int *pgbase)
2568{
2569 const void *p = buf;
2570
2571 *pgbase = offset_in_page(buf);
2572 p -= *pgbase;
2573 while (p < buf + buflen) {
2574 *(pages++) = virt_to_page(p);
2575 p += PAGE_CACHE_SIZE;
2576 }
2577}
2578
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002579struct nfs4_cached_acl {
2580 int cached;
2581 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002582 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002583};
2584
2585static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002586{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002587 struct nfs_inode *nfsi = NFS_I(inode);
2588
2589 spin_lock(&inode->i_lock);
2590 kfree(nfsi->nfs4_acl);
2591 nfsi->nfs4_acl = acl;
2592 spin_unlock(&inode->i_lock);
2593}
2594
2595static void nfs4_zap_acl_attr(struct inode *inode)
2596{
2597 nfs4_set_cached_acl(inode, NULL);
2598}
2599
2600static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2601{
2602 struct nfs_inode *nfsi = NFS_I(inode);
2603 struct nfs4_cached_acl *acl;
2604 int ret = -ENOENT;
2605
2606 spin_lock(&inode->i_lock);
2607 acl = nfsi->nfs4_acl;
2608 if (acl == NULL)
2609 goto out;
2610 if (buf == NULL) /* user is just asking for length */
2611 goto out_len;
2612 if (acl->cached == 0)
2613 goto out;
2614 ret = -ERANGE; /* see getxattr(2) man page */
2615 if (acl->len > buflen)
2616 goto out;
2617 memcpy(buf, acl->data, acl->len);
2618out_len:
2619 ret = acl->len;
2620out:
2621 spin_unlock(&inode->i_lock);
2622 return ret;
2623}
2624
2625static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2626{
2627 struct nfs4_cached_acl *acl;
2628
2629 if (buf && acl_len <= PAGE_SIZE) {
2630 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2631 if (acl == NULL)
2632 goto out;
2633 acl->cached = 1;
2634 memcpy(acl->data, buf, acl_len);
2635 } else {
2636 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2637 if (acl == NULL)
2638 goto out;
2639 acl->cached = 0;
2640 }
2641 acl->len = acl_len;
2642out:
2643 nfs4_set_cached_acl(inode, acl);
2644}
2645
2646static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2647{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002648 struct page *pages[NFS4ACL_MAXPAGES];
2649 struct nfs_getaclargs args = {
2650 .fh = NFS_FH(inode),
2651 .acl_pages = pages,
2652 .acl_len = buflen,
2653 };
2654 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002655 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2658 .rpc_argp = &args,
2659 .rpc_resp = &resp_len,
2660 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002661 struct page *localpage = NULL;
2662 int ret;
2663
2664 if (buflen < PAGE_SIZE) {
2665 /* As long as we're doing a round trip to the server anyway,
2666 * let's be prepared for a page of acl data. */
2667 localpage = alloc_page(GFP_KERNEL);
2668 resp_buf = page_address(localpage);
2669 if (localpage == NULL)
2670 return -ENOMEM;
2671 args.acl_pages[0] = localpage;
2672 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002673 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002674 } else {
2675 resp_buf = buf;
2676 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2677 }
2678 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2679 if (ret)
2680 goto out_free;
2681 if (resp_len > args.acl_len)
2682 nfs4_write_cached_acl(inode, NULL, resp_len);
2683 else
2684 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2685 if (buf) {
2686 ret = -ERANGE;
2687 if (resp_len > buflen)
2688 goto out_free;
2689 if (localpage)
2690 memcpy(buf, resp_buf, resp_len);
2691 }
2692 ret = resp_len;
2693out_free:
2694 if (localpage)
2695 __free_page(localpage);
2696 return ret;
2697}
2698
2699static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2700{
2701 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002702 int ret;
2703
2704 if (!nfs4_server_supports_acls(server))
2705 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002706 ret = nfs_revalidate_inode(server, inode);
2707 if (ret < 0)
2708 return ret;
2709 ret = nfs4_read_cached_acl(inode, buf, buflen);
2710 if (ret != -ENOENT)
2711 return ret;
2712 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002713}
2714
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002715static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2716{
2717 struct nfs_server *server = NFS_SERVER(inode);
2718 struct page *pages[NFS4ACL_MAXPAGES];
2719 struct nfs_setaclargs arg = {
2720 .fh = NFS_FH(inode),
2721 .acl_pages = pages,
2722 .acl_len = buflen,
2723 };
2724 struct rpc_message msg = {
2725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2726 .rpc_argp = &arg,
2727 .rpc_resp = NULL,
2728 };
2729 int ret;
2730
2731 if (!nfs4_server_supports_acls(server))
2732 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002733 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002734 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2735 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002736 if (ret == 0)
2737 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002738 return ret;
2739}
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002742nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct nfs4_client *clp = server->nfs4_state;
2745
2746 if (!clp || task->tk_status >= 0)
2747 return 0;
2748 switch(task->tk_status) {
2749 case -NFS4ERR_STALE_CLIENTID:
2750 case -NFS4ERR_STALE_STATEID:
2751 case -NFS4ERR_EXPIRED:
2752 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2753 nfs4_schedule_state_recovery(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002754 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 rpc_wake_up_task(task);
2756 task->tk_status = 0;
2757 return -EAGAIN;
2758 case -NFS4ERR_GRACE:
2759 case -NFS4ERR_DELAY:
2760 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2761 task->tk_status = 0;
2762 return -EAGAIN;
2763 case -NFS4ERR_OLD_STATEID:
2764 task->tk_status = 0;
2765 return -EAGAIN;
2766 }
2767 task->tk_status = nfs4_map_errors(task->tk_status);
2768 return 0;
2769}
2770
Trond Myklebust433fbe42006-01-03 09:55:22 +01002771static int nfs4_wait_bit_interruptible(void *word)
2772{
2773 if (signal_pending(current))
2774 return -ERESTARTSYS;
2775 schedule();
2776 return 0;
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 sigset_t oldset;
Trond Myklebust433fbe42006-01-03 09:55:22 +01002782 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 might_sleep();
2785
2786 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust433fbe42006-01-03 09:55:22 +01002787 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2788 nfs4_wait_bit_interruptible,
2789 TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 rpc_clnt_sigunmask(clnt, &oldset);
2791 return res;
2792}
2793
2794static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2795{
2796 sigset_t oldset;
2797 int res = 0;
2798
2799 might_sleep();
2800
2801 if (*timeout <= 0)
2802 *timeout = NFS4_POLL_RETRY_MIN;
2803 if (*timeout > NFS4_POLL_RETRY_MAX)
2804 *timeout = NFS4_POLL_RETRY_MAX;
2805 rpc_clnt_sigmask(clnt, &oldset);
2806 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002807 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (signalled())
2809 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002810 } else
2811 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 rpc_clnt_sigunmask(clnt, &oldset);
2813 *timeout <<= 1;
2814 return res;
2815}
2816
2817/* This is the error handling routine for processes that are allowed
2818 * to sleep.
2819 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002820int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 struct nfs4_client *clp = server->nfs4_state;
2823 int ret = errorcode;
2824
2825 exception->retry = 0;
2826 switch(errorcode) {
2827 case 0:
2828 return 0;
2829 case -NFS4ERR_STALE_CLIENTID:
2830 case -NFS4ERR_STALE_STATEID:
2831 case -NFS4ERR_EXPIRED:
Trond Myklebust433fbe42006-01-03 09:55:22 +01002832 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 ret = nfs4_wait_clnt_recover(server->client, clp);
2834 if (ret == 0)
2835 exception->retry = 1;
2836 break;
2837 case -NFS4ERR_GRACE:
2838 case -NFS4ERR_DELAY:
2839 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002840 if (ret != 0)
2841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002843 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845 /* We failed to handle the error */
2846 return nfs4_map_errors(ret);
2847}
2848
Trond Myklebust286d7d62006-01-03 09:55:26 +01002849int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 nfs4_verifier sc_verifier;
2852 struct nfs4_setclientid setclientid = {
2853 .sc_verifier = &sc_verifier,
2854 .sc_prog = program,
2855 };
2856 struct rpc_message msg = {
2857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2858 .rpc_argp = &setclientid,
2859 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002860 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 };
2862 u32 *p;
2863 int loop = 0;
2864 int status;
2865
2866 p = (u32*)sc_verifier.data;
2867 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2868 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2869
2870 for(;;) {
2871 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2872 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2873 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
Trond Myklebust286d7d62006-01-03 09:55:26 +01002874 cred->cr_ops->cr_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 clp->cl_id_uniquifier);
2876 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2877 sizeof(setclientid.sc_netid), "tcp");
2878 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2879 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2880 clp->cl_ipaddr, port >> 8, port & 255);
2881
2882 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2883 if (status != -NFS4ERR_CLID_INUSE)
2884 break;
2885 if (signalled())
2886 break;
2887 if (loop++ & 1)
2888 ssleep(clp->cl_lease_time + 1);
2889 else
2890 if (++clp->cl_id_uniquifier == 0)
2891 break;
2892 }
2893 return status;
2894}
2895
2896int
Trond Myklebust286d7d62006-01-03 09:55:26 +01002897nfs4_proc_setclientid_confirm(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct nfs_fsinfo fsinfo;
2900 struct rpc_message msg = {
2901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2902 .rpc_argp = clp,
2903 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01002904 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 };
2906 unsigned long now;
2907 int status;
2908
2909 now = jiffies;
2910 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2911 if (status == 0) {
2912 spin_lock(&clp->cl_lock);
2913 clp->cl_lease_time = fsinfo.lease_time * HZ;
2914 clp->cl_last_renewal = now;
Trond Myklebust58d97142006-01-03 09:55:24 +01002915 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 spin_unlock(&clp->cl_lock);
2917 }
2918 return status;
2919}
2920
Trond Myklebustfe650402006-01-03 09:55:18 +01002921struct nfs4_delegreturndata {
2922 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002923 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01002924 struct nfs_fh fh;
2925 nfs4_stateid stateid;
2926 struct rpc_cred *cred;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002927 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002928 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01002929 int rpc_status;
2930};
2931
2932static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Trond Myklebustfe650402006-01-03 09:55:18 +01002934 struct nfs4_delegreturndata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct rpc_message msg = {
2936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
Trond Myklebustfe650402006-01-03 09:55:18 +01002937 .rpc_argp = &data->args,
Trond Myklebustfa178f22006-01-03 09:55:38 +01002938 .rpc_resp = &data->res,
Trond Myklebustfe650402006-01-03 09:55:18 +01002939 .rpc_cred = data->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 };
Trond Myklebustfa178f22006-01-03 09:55:38 +01002941 nfs_fattr_init(data->res.fattr);
Trond Myklebustfe650402006-01-03 09:55:18 +01002942 rpc_call_setup(task, &msg, 0);
2943}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Trond Myklebustfe650402006-01-03 09:55:18 +01002945static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2946{
2947 struct nfs4_delegreturndata *data = calldata;
2948 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002949 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01002950 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01002951}
2952
2953static void nfs4_delegreturn_release(void *calldata)
2954{
2955 struct nfs4_delegreturndata *data = calldata;
2956
2957 put_rpccred(data->cred);
2958 kfree(calldata);
2959}
2960
2961const static struct rpc_call_ops nfs4_delegreturn_ops = {
2962 .rpc_call_prepare = nfs4_delegreturn_prepare,
2963 .rpc_call_done = nfs4_delegreturn_done,
2964 .rpc_release = nfs4_delegreturn_release,
2965};
2966
2967static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2968{
2969 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002970 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01002971 struct rpc_task *task;
2972 int status;
2973
2974 data = kmalloc(sizeof(*data), GFP_KERNEL);
2975 if (data == NULL)
2976 return -ENOMEM;
2977 data->args.fhandle = &data->fh;
2978 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002979 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01002980 nfs_copy_fh(&data->fh, NFS_FH(inode));
2981 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01002982 data->res.fattr = &data->fattr;
2983 data->res.server = server;
Trond Myklebustfe650402006-01-03 09:55:18 +01002984 data->cred = get_rpccred(cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002985 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01002986 data->rpc_status = 0;
2987
2988 task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
2989 if (IS_ERR(task)) {
2990 nfs4_delegreturn_release(data);
2991 return PTR_ERR(task);
2992 }
2993 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002994 if (status == 0) {
Trond Myklebustfe650402006-01-03 09:55:18 +01002995 status = data->rpc_status;
Trond Myklebustfa178f22006-01-03 09:55:38 +01002996 if (status == 0)
2997 nfs_post_op_update_inode(inode, &data->fattr);
2998 }
Trond Myklebustfe650402006-01-03 09:55:18 +01002999 rpc_release_task(task);
3000 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
3004{
3005 struct nfs_server *server = NFS_SERVER(inode);
3006 struct nfs4_exception exception = { };
3007 int err;
3008 do {
3009 err = _nfs4_proc_delegreturn(inode, cred, stateid);
3010 switch (err) {
3011 case -NFS4ERR_STALE_STATEID:
3012 case -NFS4ERR_EXPIRED:
3013 nfs4_schedule_state_recovery(server->nfs4_state);
3014 case 0:
3015 return 0;
3016 }
3017 err = nfs4_handle_exception(server, err, &exception);
3018 } while (exception.retry);
3019 return err;
3020}
3021
3022#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3023#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3024
3025/*
3026 * sleep, with exponential backoff, and retry the LOCK operation.
3027 */
3028static unsigned long
3029nfs4_set_lock_task_retry(unsigned long timeout)
3030{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003031 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 timeout <<= 1;
3033 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3034 return NFS4_LOCK_MAXTIMEOUT;
3035 return timeout;
3036}
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3039{
3040 struct inode *inode = state->inode;
3041 struct nfs_server *server = NFS_SERVER(inode);
3042 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003043 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003045 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003047 struct nfs_lockt_res res = {
3048 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 };
3050 struct rpc_message msg = {
3051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3052 .rpc_argp = &arg,
3053 .rpc_resp = &res,
3054 .rpc_cred = state->owner->so_cred,
3055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct nfs4_lock_state *lsp;
3057 int status;
3058
3059 down_read(&clp->cl_sem);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003060 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003061 status = nfs4_set_lock_state(state, request);
3062 if (status != 0)
3063 goto out;
3064 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003065 arg.lock_owner.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 status = rpc_call_sync(server->client, &msg, 0);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003067 switch (status) {
3068 case 0:
3069 request->fl_type = F_UNLCK;
3070 break;
3071 case -NFS4ERR_DENIED:
3072 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003074out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 up_read(&clp->cl_sem);
3076 return status;
3077}
3078
3079static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3080{
3081 struct nfs4_exception exception = { };
3082 int err;
3083
3084 do {
3085 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3086 _nfs4_proc_getlk(state, cmd, request),
3087 &exception);
3088 } while (exception.retry);
3089 return err;
3090}
3091
3092static int do_vfs_lock(struct file *file, struct file_lock *fl)
3093{
3094 int res = 0;
3095 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3096 case FL_POSIX:
3097 res = posix_lock_file_wait(file, fl);
3098 break;
3099 case FL_FLOCK:
3100 res = flock_lock_file_wait(file, fl);
3101 break;
3102 default:
3103 BUG();
3104 }
3105 if (res < 0)
3106 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3107 __FUNCTION__);
3108 return res;
3109}
3110
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003111struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003112 struct nfs_locku_args arg;
3113 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003114 struct nfs4_lock_state *lsp;
3115 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003116 struct file_lock fl;
3117 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003118 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003119};
3120
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003121static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3122 struct nfs_open_context *ctx,
3123 struct nfs4_lock_state *lsp,
3124 struct nfs_seqid *seqid)
3125{
3126 struct nfs4_unlockdata *p;
3127 struct inode *inode = lsp->ls_state->inode;
3128
3129 p = kmalloc(sizeof(*p), GFP_KERNEL);
3130 if (p == NULL)
3131 return NULL;
3132 p->arg.fh = NFS_FH(inode);
3133 p->arg.fl = &p->fl;
3134 p->arg.seqid = seqid;
3135 p->arg.stateid = &lsp->ls_stateid;
3136 p->lsp = lsp;
3137 atomic_inc(&lsp->ls_count);
3138 /* Ensure we don't close file until we're done freeing locks! */
3139 p->ctx = get_nfs_open_context(ctx);
3140 memcpy(&p->fl, fl, sizeof(p->fl));
3141 p->server = NFS_SERVER(inode);
3142 return p;
3143}
3144
Trond Myklebust06f814a2006-01-03 09:55:07 +01003145static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003148 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003149 nfs4_put_lock_state(calldata->lsp);
3150 put_nfs_open_context(calldata->ctx);
3151 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003152}
3153
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003155{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003157
Trond Myklebust06f814a2006-01-03 09:55:07 +01003158 if (RPC_ASSASSINATED(task))
3159 return;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003160 nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003161 switch (task->tk_status) {
3162 case 0:
3163 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003164 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003165 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003166 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003167 break;
3168 case -NFS4ERR_STALE_STATEID:
3169 case -NFS4ERR_EXPIRED:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003170 nfs4_schedule_state_recovery(calldata->server->nfs4_state);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003171 break;
3172 default:
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003173 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) {
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003174 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003175 }
3176 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003177}
3178
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003179static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003180{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003181 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 struct rpc_message msg = {
3183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003184 .rpc_argp = &calldata->arg,
3185 .rpc_resp = &calldata->res,
3186 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003189 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003190 return;
3191 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003192 /* Note: exit _without_ running nfs4_locku_done */
3193 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003194 return;
3195 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003196 calldata->timestamp = jiffies;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003197 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003200static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003201 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003202 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003203 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003204};
3205
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003206static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3207 struct nfs_open_context *ctx,
3208 struct nfs4_lock_state *lsp,
3209 struct nfs_seqid *seqid)
3210{
3211 struct nfs4_unlockdata *data;
3212 struct rpc_task *task;
3213
3214 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3215 if (data == NULL) {
3216 nfs_free_seqid(seqid);
3217 return ERR_PTR(-ENOMEM);
3218 }
3219
3220 /* Unlock _before_ we do the RPC call */
3221 do_vfs_lock(fl->fl_file, fl);
3222 task = rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
3223 if (IS_ERR(task))
3224 nfs4_locku_release_calldata(data);
3225 return task;
3226}
3227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3229{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003230 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003231 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003232 struct rpc_task *task;
3233 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003235 /* Is this a delegated lock? */
3236 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003237 goto out_unlock;
3238 /* Is this open_owner holding any locks on the server? */
3239 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
3240 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003241
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003242 status = nfs4_set_lock_state(state, request);
3243 if (status != 0)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003244 goto out_unlock;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003245 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003246 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003247 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3248 if (seqid == NULL)
3249 goto out_unlock;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003250 task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
3251 status = PTR_ERR(task);
3252 if (IS_ERR(task))
3253 goto out_unlock;
3254 status = nfs4_wait_for_completion_rpc_task(task);
3255 rpc_release_task(task);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003256 return status;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003257out_unlock:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003258 do_vfs_lock(request->fl_file, request);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003262struct nfs4_lockdata {
3263 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003264 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003265 struct nfs4_lock_state *lsp;
3266 struct nfs_open_context *ctx;
3267 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003268 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003269 int rpc_status;
3270 int cancelled;
3271};
3272
3273static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3274 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3275{
3276 struct nfs4_lockdata *p;
3277 struct inode *inode = lsp->ls_state->inode;
3278 struct nfs_server *server = NFS_SERVER(inode);
3279
3280 p = kzalloc(sizeof(*p), GFP_KERNEL);
3281 if (p == NULL)
3282 return NULL;
3283
3284 p->arg.fh = NFS_FH(inode);
3285 p->arg.fl = &p->fl;
3286 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3287 if (p->arg.lock_seqid == NULL)
3288 goto out_free;
3289 p->arg.lock_stateid = &lsp->ls_stateid;
3290 p->arg.lock_owner.clientid = server->nfs4_state->cl_clientid;
3291 p->arg.lock_owner.id = lsp->ls_id;
3292 p->lsp = lsp;
3293 atomic_inc(&lsp->ls_count);
3294 p->ctx = get_nfs_open_context(ctx);
3295 memcpy(&p->fl, fl, sizeof(p->fl));
3296 return p;
3297out_free:
3298 kfree(p);
3299 return NULL;
3300}
3301
3302static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3303{
3304 struct nfs4_lockdata *data = calldata;
3305 struct nfs4_state *state = data->lsp->ls_state;
3306 struct nfs4_state_owner *sp = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3309 .rpc_argp = &data->arg,
3310 .rpc_resp = &data->res,
3311 .rpc_cred = sp->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003314 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3315 return;
3316 dprintk("%s: begin!\n", __FUNCTION__);
3317 /* Do we need to do an open_to_lock_owner? */
3318 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3319 data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
3320 if (data->arg.open_seqid == NULL) {
3321 data->rpc_status = -ENOMEM;
3322 task->tk_action = NULL;
Trond Myklebust06735b32005-10-18 14:20:15 -07003323 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07003324 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003325 data->arg.open_stateid = &state->stateid;
3326 data->arg.new_lock_owner = 1;
3327 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003328 data->timestamp = jiffies;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003329 rpc_call_setup(task, &msg, 0);
Trond Myklebust06735b32005-10-18 14:20:15 -07003330out:
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003331 dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
3332}
3333
3334static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3335{
3336 struct nfs4_lockdata *data = calldata;
3337
3338 dprintk("%s: begin!\n", __FUNCTION__);
3339
3340 data->rpc_status = task->tk_status;
3341 if (RPC_ASSASSINATED(task))
3342 goto out;
3343 if (data->arg.new_lock_owner != 0) {
3344 nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
3345 if (data->rpc_status == 0)
3346 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3347 else
3348 goto out;
3349 }
3350 if (data->rpc_status == 0) {
3351 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3352 sizeof(data->lsp->ls_stateid.data));
3353 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003354 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003355 }
3356 nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
3357out:
3358 dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
3359}
3360
3361static void nfs4_lock_release(void *calldata)
3362{
3363 struct nfs4_lockdata *data = calldata;
3364
3365 dprintk("%s: begin!\n", __FUNCTION__);
3366 if (data->arg.open_seqid != NULL)
3367 nfs_free_seqid(data->arg.open_seqid);
3368 if (data->cancelled != 0) {
3369 struct rpc_task *task;
3370 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3371 data->arg.lock_seqid);
3372 if (!IS_ERR(task))
3373 rpc_release_task(task);
3374 dprintk("%s: cancelling lock!\n", __FUNCTION__);
3375 } else
3376 nfs_free_seqid(data->arg.lock_seqid);
3377 nfs4_put_lock_state(data->lsp);
3378 put_nfs_open_context(data->ctx);
3379 kfree(data);
3380 dprintk("%s: done!\n", __FUNCTION__);
3381}
3382
3383static const struct rpc_call_ops nfs4_lock_ops = {
3384 .rpc_call_prepare = nfs4_lock_prepare,
3385 .rpc_call_done = nfs4_lock_done,
3386 .rpc_release = nfs4_lock_release,
3387};
3388
3389static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3390{
3391 struct nfs4_lockdata *data;
3392 struct rpc_task *task;
3393 int ret;
3394
3395 dprintk("%s: begin!\n", __FUNCTION__);
3396 data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
3397 fl->fl_u.nfs4_fl.owner);
3398 if (data == NULL)
3399 return -ENOMEM;
3400 if (IS_SETLKW(cmd))
3401 data->arg.block = 1;
3402 if (reclaim != 0)
3403 data->arg.reclaim = 1;
3404 task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
3405 &nfs4_lock_ops, data);
3406 if (IS_ERR(task)) {
3407 nfs4_lock_release(data);
3408 return PTR_ERR(task);
3409 }
3410 ret = nfs4_wait_for_completion_rpc_task(task);
3411 if (ret == 0) {
3412 ret = data->rpc_status;
3413 if (ret == -NFS4ERR_DENIED)
3414 ret = -EAGAIN;
3415 } else
3416 data->cancelled = 1;
3417 rpc_release_task(task);
3418 dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
3419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
3422static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3423{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003424 struct nfs_server *server = NFS_SERVER(state->inode);
3425 struct nfs4_exception exception = { };
3426 int err;
3427
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003428 /* Cache the lock if possible... */
3429 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3430 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003431 do {
3432 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3433 if (err != -NFS4ERR_DELAY)
3434 break;
3435 nfs4_handle_exception(server, err, &exception);
3436 } while (exception.retry);
3437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3441{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003442 struct nfs_server *server = NFS_SERVER(state->inode);
3443 struct nfs4_exception exception = { };
3444 int err;
3445
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003446 err = nfs4_set_lock_state(state, request);
3447 if (err != 0)
3448 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003449 do {
3450 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3451 if (err != -NFS4ERR_DELAY)
3452 break;
3453 nfs4_handle_exception(server, err, &exception);
3454 } while (exception.retry);
3455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
3458static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3459{
3460 struct nfs4_client *clp = state->owner->so_client;
3461 int status;
3462
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003463 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003464 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003465 /* Yes: cache locks! */
3466 status = do_vfs_lock(request->fl_file, request);
3467 /* ...but avoid races with delegation recall... */
3468 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003469 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Trond Myklebustff604062005-11-25 17:10:01 -05003471 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003472 status = nfs4_set_lock_state(state, request);
3473 if (status != 0)
3474 goto out;
3475 status = _nfs4_do_setlk(state, cmd, request, 0);
3476 if (status != 0)
3477 goto out;
3478 /* Note: we always want to sleep here! */
3479 request->fl_flags |= FL_SLEEP;
3480 if (do_vfs_lock(request->fl_file, request) < 0)
3481 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3482out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 up_read(&clp->cl_sem);
3484 return status;
3485}
3486
3487static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3488{
3489 struct nfs4_exception exception = { };
3490 int err;
3491
3492 do {
3493 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3494 _nfs4_proc_setlk(state, cmd, request),
3495 &exception);
3496 } while (exception.retry);
3497 return err;
3498}
3499
3500static int
3501nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3502{
3503 struct nfs_open_context *ctx;
3504 struct nfs4_state *state;
3505 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3506 int status;
3507
3508 /* verify open state */
3509 ctx = (struct nfs_open_context *)filp->private_data;
3510 state = ctx->state;
3511
3512 if (request->fl_start < 0 || request->fl_end < 0)
3513 return -EINVAL;
3514
3515 if (IS_GETLK(cmd))
3516 return nfs4_proc_getlk(state, F_GETLK, request);
3517
3518 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3519 return -EINVAL;
3520
3521 if (request->fl_type == F_UNLCK)
3522 return nfs4_proc_unlck(state, cmd, request);
3523
3524 do {
3525 status = nfs4_proc_setlk(state, cmd, request);
3526 if ((status != -EAGAIN) || IS_SETLK(cmd))
3527 break;
3528 timeout = nfs4_set_lock_task_retry(timeout);
3529 status = -ERESTARTSYS;
3530 if (signalled())
3531 break;
3532 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return status;
3534}
3535
Trond Myklebust888e6942005-11-04 15:38:11 -05003536int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3537{
3538 struct nfs_server *server = NFS_SERVER(state->inode);
3539 struct nfs4_exception exception = { };
3540 int err;
3541
3542 err = nfs4_set_lock_state(state, fl);
3543 if (err != 0)
3544 goto out;
3545 do {
3546 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3547 if (err != -NFS4ERR_DELAY)
3548 break;
3549 err = nfs4_handle_exception(server, err, &exception);
3550 } while (exception.retry);
3551out:
3552 return err;
3553}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003554
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003555#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3556
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003557int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3558 size_t buflen, int flags)
3559{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003560 struct inode *inode = dentry->d_inode;
3561
3562 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3563 return -EOPNOTSUPP;
3564
3565 if (!S_ISREG(inode->i_mode) &&
3566 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3567 return -EPERM;
3568
3569 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003570}
3571
3572/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3573 * and that's what we'll do for e.g. user attributes that haven't been set.
3574 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3575 * attributes in kernel-managed attribute namespaces. */
3576ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3577 size_t buflen)
3578{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003579 struct inode *inode = dentry->d_inode;
3580
3581 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3582 return -EOPNOTSUPP;
3583
3584 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003585}
3586
3587ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3588{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003589 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003590
3591 if (buf && buflen < len)
3592 return -ERANGE;
3593 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003594 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3595 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003596}
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3599 .recover_open = nfs4_open_reclaim,
3600 .recover_lock = nfs4_lock_reclaim,
3601};
3602
3603struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3604 .recover_open = nfs4_open_expired,
3605 .recover_lock = nfs4_lock_expired,
3606};
3607
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003608static struct inode_operations nfs4_file_inode_operations = {
3609 .permission = nfs_permission,
3610 .getattr = nfs_getattr,
3611 .setattr = nfs_setattr,
3612 .getxattr = nfs4_getxattr,
3613 .setxattr = nfs4_setxattr,
3614 .listxattr = nfs4_listxattr,
3615};
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617struct nfs_rpc_ops nfs_v4_clientops = {
3618 .version = 4, /* protocol version */
3619 .dentry_ops = &nfs4_dentry_operations,
3620 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003621 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 .getroot = nfs4_proc_get_root,
3623 .getattr = nfs4_proc_getattr,
3624 .setattr = nfs4_proc_setattr,
3625 .lookup = nfs4_proc_lookup,
3626 .access = nfs4_proc_access,
3627 .readlink = nfs4_proc_readlink,
3628 .read = nfs4_proc_read,
3629 .write = nfs4_proc_write,
3630 .commit = nfs4_proc_commit,
3631 .create = nfs4_proc_create,
3632 .remove = nfs4_proc_remove,
3633 .unlink_setup = nfs4_proc_unlink_setup,
3634 .unlink_done = nfs4_proc_unlink_done,
3635 .rename = nfs4_proc_rename,
3636 .link = nfs4_proc_link,
3637 .symlink = nfs4_proc_symlink,
3638 .mkdir = nfs4_proc_mkdir,
3639 .rmdir = nfs4_proc_remove,
3640 .readdir = nfs4_proc_readdir,
3641 .mknod = nfs4_proc_mknod,
3642 .statfs = nfs4_proc_statfs,
3643 .fsinfo = nfs4_proc_fsinfo,
3644 .pathconf = nfs4_proc_pathconf,
3645 .decode_dirent = nfs4_decode_dirent,
3646 .read_setup = nfs4_proc_read_setup,
3647 .write_setup = nfs4_proc_write_setup,
3648 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003649 .file_open = nfs_open,
3650 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003652 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653};
3654
3655/*
3656 * Local variables:
3657 * c-basic-offset: 8
3658 * End:
3659 */