NFSv4: Convert open() into an asynchronous RPC call
[linux-2.6-block.git] / fs / nfs / nfs4proc.c
CommitLineData
1da177e4
LT
1/*
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>
02a913a7 50#include <linux/mount.h>
1da177e4 51
4ce79717 52#include "nfs4_fs.h"
1da177e4
LT
53#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
0a8838f9 60static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid);
1da177e4 61static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
faf5f49c 62static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
1da177e4 63static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
faf5f49c 64static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
1da177e4
LT
65extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66extern struct rpc_procinfo nfs4_procedures[];
67
1da177e4
LT
68/* Prevent leaks of NFSv4 errors into userland */
69int nfs4_map_errors(int err)
70{
71 if (err < -1000) {
72 dprintk("%s could not handle NFSv4 error %d\n",
73 __FUNCTION__, -err);
74 return -EIO;
75 }
76 return err;
77}
78
79/*
80 * This is our standard bitmap for GETATTR requests.
81 */
82const u32 nfs4_fattr_bitmap[2] = {
83 FATTR4_WORD0_TYPE
84 | FATTR4_WORD0_CHANGE
85 | FATTR4_WORD0_SIZE
86 | FATTR4_WORD0_FSID
87 | FATTR4_WORD0_FILEID,
88 FATTR4_WORD1_MODE
89 | FATTR4_WORD1_NUMLINKS
90 | FATTR4_WORD1_OWNER
91 | FATTR4_WORD1_OWNER_GROUP
92 | FATTR4_WORD1_RAWDEV
93 | FATTR4_WORD1_SPACE_USED
94 | FATTR4_WORD1_TIME_ACCESS
95 | FATTR4_WORD1_TIME_METADATA
96 | FATTR4_WORD1_TIME_MODIFY
97};
98
99const u32 nfs4_statfs_bitmap[2] = {
100 FATTR4_WORD0_FILES_AVAIL
101 | FATTR4_WORD0_FILES_FREE
102 | FATTR4_WORD0_FILES_TOTAL,
103 FATTR4_WORD1_SPACE_AVAIL
104 | FATTR4_WORD1_SPACE_FREE
105 | FATTR4_WORD1_SPACE_TOTAL
106};
107
4ce79717 108const u32 nfs4_pathconf_bitmap[2] = {
1da177e4
LT
109 FATTR4_WORD0_MAXLINK
110 | FATTR4_WORD0_MAXNAME,
111 0
112};
113
114const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
115 | FATTR4_WORD0_MAXREAD
116 | FATTR4_WORD0_MAXWRITE
117 | FATTR4_WORD0_LEASE_TIME,
118 0
119};
120
121static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
122 struct nfs4_readdir_arg *readdir)
123{
124 u32 *start, *p;
125
126 BUG_ON(readdir->count < 80);
127 if (cookie > 2) {
b7ef1956 128 readdir->cookie = cookie;
1da177e4
LT
129 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
130 return;
131 }
132
133 readdir->cookie = 0;
134 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
135 if (cookie == 2)
136 return;
137
138 /*
139 * NFSv4 servers do not return entries for '.' and '..'
140 * Therefore, we fake these entries here. We let '.'
141 * have cookie 0 and '..' have cookie 1. Note that
142 * when talking to the server, we always send cookie 0
143 * instead of 1 or 2.
144 */
145 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
146
147 if (cookie == 0) {
148 *p++ = xdr_one; /* next */
149 *p++ = xdr_zero; /* cookie, first word */
150 *p++ = xdr_one; /* cookie, second word */
151 *p++ = xdr_one; /* entry len */
152 memcpy(p, ".\0\0\0", 4); /* entry */
153 p++;
154 *p++ = xdr_one; /* bitmap length */
155 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
156 *p++ = htonl(8); /* attribute buffer length */
157 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
158 }
159
160 *p++ = xdr_one; /* next */
161 *p++ = xdr_zero; /* cookie, first word */
162 *p++ = xdr_two; /* cookie, second word */
163 *p++ = xdr_two; /* entry len */
164 memcpy(p, "..\0\0", 4); /* entry */
165 p++;
166 *p++ = xdr_one; /* bitmap length */
167 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
168 *p++ = htonl(8); /* attribute buffer length */
169 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
170
171 readdir->pgbase = (char *)p - (char *)start;
172 readdir->count -= readdir->pgbase;
173 kunmap_atomic(start, KM_USER0);
174}
175
176static void
177renew_lease(struct nfs_server *server, unsigned long timestamp)
178{
179 struct nfs4_client *clp = server->nfs4_state;
180 spin_lock(&clp->cl_lock);
181 if (time_before(clp->cl_last_renewal,timestamp))
182 clp->cl_last_renewal = timestamp;
183 spin_unlock(&clp->cl_lock);
184}
185
186static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
187{
188 struct nfs_inode *nfsi = NFS_I(inode);
189
decf491f
TM
190 spin_lock(&inode->i_lock);
191 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
1da177e4
LT
192 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193 nfsi->change_attr = cinfo->after;
decf491f 194 spin_unlock(&inode->i_lock);
1da177e4
LT
195}
196
e56e0b78 197struct nfs4_opendata {
24ac23ab 198 atomic_t count;
e56e0b78
TM
199 struct nfs_openargs o_arg;
200 struct nfs_openres o_res;
201 struct nfs_fattr f_attr;
202 struct nfs_fattr dir_attr;
203 struct dentry *dentry;
204 struct dentry *dir;
205 struct nfs4_state_owner *owner;
206 struct iattr attrs;
24ac23ab
TM
207 int rpc_status;
208 int cancelled;
e56e0b78
TM
209};
210
211static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
212 struct nfs4_state_owner *sp, int flags,
213 const struct iattr *attrs)
214{
215 struct dentry *parent = dget_parent(dentry);
216 struct inode *dir = parent->d_inode;
217 struct nfs_server *server = NFS_SERVER(dir);
218 struct nfs4_opendata *p;
219
220 p = kzalloc(sizeof(*p), GFP_KERNEL);
221 if (p == NULL)
222 goto err;
223 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
224 if (p->o_arg.seqid == NULL)
225 goto err_free;
24ac23ab 226 atomic_set(&p->count, 1);
e56e0b78
TM
227 p->dentry = dget(dentry);
228 p->dir = parent;
229 p->owner = sp;
230 atomic_inc(&sp->so_count);
231 p->o_arg.fh = NFS_FH(dir);
232 p->o_arg.open_flags = flags,
233 p->o_arg.clientid = server->nfs4_state->cl_clientid;
234 p->o_arg.id = sp->so_id;
235 p->o_arg.name = &dentry->d_name;
236 p->o_arg.server = server;
237 p->o_arg.bitmask = server->attr_bitmask;
238 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
239 p->o_res.f_attr = &p->f_attr;
240 p->o_res.dir_attr = &p->dir_attr;
241 p->o_res.server = server;
242 nfs_fattr_init(&p->f_attr);
243 nfs_fattr_init(&p->dir_attr);
244 if (flags & O_EXCL) {
245 u32 *s = (u32 *) p->o_arg.u.verifier.data;
246 s[0] = jiffies;
247 s[1] = current->pid;
248 } else if (flags & O_CREAT) {
249 p->o_arg.u.attrs = &p->attrs;
250 memcpy(&p->attrs, attrs, sizeof(p->attrs));
251 }
252 return p;
253err_free:
254 kfree(p);
255err:
256 dput(parent);
257 return NULL;
258}
259
260static void nfs4_opendata_free(struct nfs4_opendata *p)
261{
24ac23ab 262 if (p != NULL && atomic_dec_and_test(&p->count)) {
e56e0b78
TM
263 nfs_free_seqid(p->o_arg.seqid);
264 nfs4_put_state_owner(p->owner);
265 dput(p->dir);
266 dput(p->dentry);
267 kfree(p);
268 }
269}
270
9512135d 271/* Helper for asynchronous RPC calls */
4ce70ada 272static int nfs4_call_async(struct rpc_clnt *clnt,
963d8fe5 273 const struct rpc_call_ops *tk_ops, void *calldata)
9512135d
TM
274{
275 struct rpc_task *task;
276
963d8fe5 277 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
9512135d 278 return -ENOMEM;
9512135d
TM
279 rpc_execute(task);
280 return 0;
281}
282
06f814a3
TM
283static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
284{
285 sigset_t oldset;
286 int ret;
287
288 rpc_clnt_sigmask(task->tk_client, &oldset);
289 ret = rpc_wait_for_completion_task(task);
290 rpc_clnt_sigunmask(task->tk_client, &oldset);
291 return ret;
292}
293
1da177e4
LT
294static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
295{
296 struct inode *inode = state->inode;
297
298 open_flags &= (FMODE_READ|FMODE_WRITE);
d530838b 299 /* Protect against nfs4_find_state_byowner() */
ec073428 300 spin_lock(&state->owner->so_lock);
1da177e4 301 spin_lock(&inode->i_lock);
4cecb76f
TM
302 memcpy(&state->stateid, stateid, sizeof(state->stateid));
303 if ((open_flags & FMODE_WRITE))
304 state->nwriters++;
1da177e4
LT
305 if (open_flags & FMODE_READ)
306 state->nreaders++;
4cecb76f 307 nfs4_state_set_mode_locked(state, state->state | open_flags);
1da177e4 308 spin_unlock(&inode->i_lock);
ec073428 309 spin_unlock(&state->owner->so_lock);
1da177e4
LT
310}
311
24ac23ab
TM
312static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
313{
314 struct inode *inode;
315 struct nfs4_state *state = NULL;
316
317 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
318 goto out;
319 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
320 if (inode == NULL)
321 goto out;
322 state = nfs4_get_open_state(inode, data->owner);
323 if (state == NULL)
324 goto put_inode;
325 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
326put_inode:
327 iput(inode);
328out:
329 return state;
330}
331
1da177e4
LT
332/*
333 * OPEN_RECLAIM:
334 * reclaim state on the server after a reboot.
1da177e4
LT
335 */
336static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
337{
338 struct inode *inode = state->inode;
339 struct nfs_server *server = NFS_SERVER(inode);
340 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
341 struct nfs_openargs o_arg = {
342 .fh = NFS_FH(inode),
1da177e4
LT
343 .id = sp->so_id,
344 .open_flags = state->state,
345 .clientid = server->nfs4_state->cl_clientid,
346 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
347 .bitmask = server->attr_bitmask,
348 };
349 struct nfs_openres o_res = {
350 .server = server, /* Grrr */
351 };
352 struct rpc_message msg = {
353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
354 .rpc_argp = &o_arg,
355 .rpc_resp = &o_res,
356 .rpc_cred = sp->so_cred,
357 };
358 int status;
359
360 if (delegation != NULL) {
361 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
362 memcpy(&state->stateid, &delegation->stateid,
363 sizeof(state->stateid));
364 set_bit(NFS_DELEGATED_STATE, &state->flags);
365 return 0;
366 }
367 o_arg.u.delegation_type = delegation->type;
368 }
cee54fc9
TM
369 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
370 if (o_arg.seqid == NULL)
371 return -ENOMEM;
1da177e4 372 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
cee54fc9
TM
373 /* Confirm the sequence as being established */
374 nfs_confirm_seqid(&sp->so_seqid, status);
375 nfs_increment_open_seqid(status, o_arg.seqid);
1da177e4
LT
376 if (status == 0) {
377 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
378 if (o_res.delegation_type != 0) {
379 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
380 /* Did the server issue an immediate delegation recall? */
381 if (o_res.do_recall)
382 nfs_async_inode_return_delegation(inode, &o_res.stateid);
383 }
384 }
cee54fc9 385 nfs_free_seqid(o_arg.seqid);
1da177e4
LT
386 clear_bit(NFS_DELEGATED_STATE, &state->flags);
387 /* Ensure we update the inode attributes */
388 NFS_CACHEINV(inode);
389 return status;
390}
391
392static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
393{
394 struct nfs_server *server = NFS_SERVER(state->inode);
395 struct nfs4_exception exception = { };
396 int err;
397 do {
398 err = _nfs4_open_reclaim(sp, state);
202b50dc
TM
399 if (err != -NFS4ERR_DELAY)
400 break;
401 nfs4_handle_exception(server, err, &exception);
1da177e4
LT
402 } while (exception.retry);
403 return err;
404}
405
406static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
407{
408 struct nfs4_state_owner *sp = state->owner;
409 struct inode *inode = dentry->d_inode;
410 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
411 struct rpc_message msg = {
412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
1da177e4
LT
413 .rpc_cred = sp->so_cred,
414 };
e56e0b78 415 struct nfs4_opendata *opendata;
1da177e4
LT
416 int status = 0;
417
1da177e4
LT
418 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
419 goto out;
420 if (state->state == 0)
421 goto out;
e56e0b78 422 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
cee54fc9 423 status = -ENOMEM;
e56e0b78 424 if (opendata == NULL)
cee54fc9 425 goto out;
e56e0b78
TM
426 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
427 msg.rpc_argp = &opendata->o_arg;
428 msg.rpc_resp = &opendata->o_res;
429 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
430 sizeof(opendata->o_arg.u.delegation.data));
1da177e4 431 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
e56e0b78 432 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
0a8838f9
TM
433 if (status != 0)
434 goto out_free;
e56e0b78 435 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
0a8838f9 436 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
e56e0b78 437 sp, &opendata->o_res.stateid, opendata->o_arg.seqid);
0a8838f9
TM
438 if (status != 0)
439 goto out_free;
440 }
441 nfs_confirm_seqid(&sp->so_seqid, 0);
1da177e4 442 if (status >= 0) {
e56e0b78 443 memcpy(state->stateid.data, opendata->o_res.stateid.data,
1da177e4
LT
444 sizeof(state->stateid.data));
445 clear_bit(NFS_DELEGATED_STATE, &state->flags);
446 }
0a8838f9 447out_free:
e56e0b78 448 nfs4_opendata_free(opendata);
1da177e4 449out:
1da177e4
LT
450 return status;
451}
452
453int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
454{
455 struct nfs4_exception exception = { };
456 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
457 int err;
458 do {
459 err = _nfs4_open_delegation_recall(dentry, state);
460 switch (err) {
461 case 0:
462 return err;
463 case -NFS4ERR_STALE_CLIENTID:
464 case -NFS4ERR_STALE_STATEID:
465 case -NFS4ERR_EXPIRED:
466 /* Don't recall a delegation if it was lost */
467 nfs4_schedule_state_recovery(server->nfs4_state);
468 return err;
469 }
470 err = nfs4_handle_exception(server, err, &exception);
471 } while (exception.retry);
472 return err;
473}
474
0a8838f9 475static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
1da177e4
LT
476{
477 struct nfs_open_confirmargs arg = {
478 .fh = fh,
cee54fc9 479 .seqid = seqid,
1da177e4
LT
480 .stateid = *stateid,
481 };
482 struct nfs_open_confirmres res;
483 struct rpc_message msg = {
484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
485 .rpc_argp = &arg,
486 .rpc_resp = &res,
487 .rpc_cred = sp->so_cred,
488 };
489 int status;
490
491 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
cee54fc9
TM
492 /* Confirm the sequence as being established */
493 nfs_confirm_seqid(&sp->so_seqid, status);
494 nfs_increment_open_seqid(status, seqid);
1da177e4
LT
495 if (status >= 0)
496 memcpy(stateid, &res.stateid, sizeof(*stateid));
497 return status;
498}
499
24ac23ab 500static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1da177e4 501{
24ac23ab
TM
502 struct nfs4_opendata *data = calldata;
503 struct nfs4_state_owner *sp = data->owner;
1da177e4
LT
504 struct rpc_message msg = {
505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
24ac23ab
TM
506 .rpc_argp = &data->o_arg,
507 .rpc_resp = &data->o_res,
1da177e4
LT
508 .rpc_cred = sp->so_cred,
509 };
24ac23ab
TM
510
511 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
512 return;
513 /* Update sequence id. */
514 data->o_arg.id = sp->so_id;
515 data->o_arg.clientid = sp->so_client->cl_clientid;
516 rpc_call_setup(task, &msg, 0);
517}
1da177e4 518
24ac23ab
TM
519static void nfs4_open_done(struct rpc_task *task, void *calldata)
520{
521 struct nfs4_opendata *data = calldata;
1da177e4 522
24ac23ab
TM
523 data->rpc_status = task->tk_status;
524 if (RPC_ASSASSINATED(task))
525 return;
526 if (task->tk_status == 0) {
527 switch (data->o_res.f_attr->mode & S_IFMT) {
6f926b5b
TM
528 case S_IFREG:
529 break;
530 case S_IFLNK:
24ac23ab 531 data->rpc_status = -ELOOP;
6f926b5b
TM
532 break;
533 case S_IFDIR:
24ac23ab 534 data->rpc_status = -EISDIR;
6f926b5b
TM
535 break;
536 default:
24ac23ab 537 data->rpc_status = -ENOTDIR;
6f926b5b
TM
538 }
539 }
24ac23ab
TM
540 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
541}
6f926b5b 542
24ac23ab
TM
543static void nfs4_open_release(void *calldata)
544{
545 struct nfs4_opendata *data = calldata;
546 struct nfs4_state *state = NULL;
547
548 /* If this request hasn't been cancelled, do nothing */
549 if (data->cancelled == 0)
550 goto out_free;
551 /* In case of error, no cleanup! */
552 if (data->rpc_status != 0)
553 goto out_free;
554 /* In case we need an open_confirm, no cleanup! */
555 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
556 goto out_free;
557 nfs_confirm_seqid(&data->owner->so_seqid, 0);
558 state = nfs4_opendata_to_nfs4_state(data);
559 if (state != NULL)
560 nfs4_close_state(state, data->o_arg.open_flags);
561out_free:
562 nfs4_opendata_free(data);
563}
564
565static const struct rpc_call_ops nfs4_open_ops = {
566 .rpc_call_prepare = nfs4_open_prepare,
567 .rpc_call_done = nfs4_open_done,
568 .rpc_release = nfs4_open_release,
569};
570
571/*
572 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
573 */
574static int _nfs4_proc_open(struct nfs4_opendata *data)
575{
576 struct inode *dir = data->dir->d_inode;
577 struct nfs_server *server = NFS_SERVER(dir);
578 struct nfs_openargs *o_arg = &data->o_arg;
579 struct nfs_openres *o_res = &data->o_res;
580 struct rpc_task *task;
581 int status;
582
583 atomic_inc(&data->count);
584 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
585 if (IS_ERR(task)) {
586 nfs4_opendata_free(data);
587 return PTR_ERR(task);
588 }
589 status = nfs4_wait_for_completion_rpc_task(task);
590 if (status != 0) {
591 data->cancelled = 1;
592 smp_wmb();
593 } else
594 status = data->rpc_status;
595 rpc_release_task(task);
1da177e4 596 if (status != 0)
24ac23ab
TM
597 return status;
598
56ae19f3
TM
599 if (o_arg->open_flags & O_CREAT) {
600 update_changeattr(dir, &o_res->cinfo);
601 nfs_post_op_update_inode(dir, o_res->dir_attr);
602 } else
603 nfs_refresh_inode(dir, o_res->dir_attr);
1da177e4
LT
604 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
605 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
24ac23ab 606 data->owner, &o_res->stateid, o_arg->seqid);
1da177e4 607 if (status != 0)
24ac23ab 608 return status;
1da177e4 609 }
24ac23ab 610 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1da177e4 611 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
24ac23ab
TM
612 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
613 return 0;
1da177e4
LT
614}
615
616static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
617{
618 struct nfs_access_entry cache;
619 int mask = 0;
620 int status;
621
622 if (openflags & FMODE_READ)
623 mask |= MAY_READ;
624 if (openflags & FMODE_WRITE)
625 mask |= MAY_WRITE;
626 status = nfs_access_get_cached(inode, cred, &cache);
627 if (status == 0)
628 goto out;
629
630 /* Be clever: ask server to check for all possible rights */
631 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
632 cache.cred = cred;
633 cache.jiffies = jiffies;
634 status = _nfs4_proc_access(inode, &cache);
635 if (status != 0)
636 return status;
637 nfs_access_add_cache(inode, &cache);
638out:
639 if ((cache.mask & mask) == mask)
640 return 0;
641 return -EACCES;
642}
643
644/*
645 * OPEN_EXPIRED:
646 * reclaim state on the server after a network partition.
647 * Assumes caller holds the appropriate lock
648 */
649static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
650{
651 struct dentry *parent = dget_parent(dentry);
1da177e4 652 struct inode *inode = state->inode;
1da177e4 653 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
e56e0b78 654 struct nfs4_opendata *opendata;
24ac23ab
TM
655 struct nfs4_state *newstate;
656 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
1da177e4
LT
657 int status = 0;
658
659 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
24ac23ab 660 status = _nfs4_do_access(inode, sp->so_cred, openflags);
1da177e4
LT
661 if (status < 0)
662 goto out;
663 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
664 set_bit(NFS_DELEGATED_STATE, &state->flags);
665 goto out;
666 }
cee54fc9 667 status = -ENOMEM;
24ac23ab 668 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
e56e0b78 669 if (opendata == NULL)
cee54fc9 670 goto out;
24ac23ab 671 status = _nfs4_proc_open(opendata);
1da177e4
LT
672 if (status != 0)
673 goto out_nodeleg;
24ac23ab
TM
674 newstate = nfs4_opendata_to_nfs4_state(opendata);
675 if (newstate != state)
1da177e4 676 goto out_stale;
e56e0b78 677 if (opendata->o_res.delegation_type != 0) {
1da177e4 678 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
e56e0b78
TM
679 nfs_inode_set_delegation(inode, sp->so_cred,
680 &opendata->o_res);
1da177e4 681 else
e56e0b78
TM
682 nfs_inode_reclaim_delegation(inode, sp->so_cred,
683 &opendata->o_res);
1da177e4 684 }
24ac23ab
TM
685out_close_state:
686 nfs4_close_state(newstate, openflags);
1da177e4 687out_nodeleg:
e56e0b78 688 nfs4_opendata_free(opendata);
1da177e4
LT
689 clear_bit(NFS_DELEGATED_STATE, &state->flags);
690out:
691 dput(parent);
692 return status;
693out_stale:
694 status = -ESTALE;
695 /* Invalidate the state owner so we don't ever use it again */
696 nfs4_drop_state_owner(sp);
697 d_drop(dentry);
698 /* Should we be trying to close that stateid? */
24ac23ab 699 goto out_close_state;
1da177e4
LT
700}
701
202b50dc
TM
702static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
703{
704 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
705 struct nfs4_exception exception = { };
706 int err;
707
708 do {
709 err = _nfs4_open_expired(sp, state, dentry);
710 if (err == -NFS4ERR_DELAY)
711 nfs4_handle_exception(server, err, &exception);
712 } while (exception.retry);
713 return err;
714}
715
1da177e4
LT
716static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
717{
718 struct nfs_inode *nfsi = NFS_I(state->inode);
719 struct nfs_open_context *ctx;
720 int status;
721
722 spin_lock(&state->inode->i_lock);
723 list_for_each_entry(ctx, &nfsi->open_files, list) {
724 if (ctx->state != state)
725 continue;
726 get_nfs_open_context(ctx);
727 spin_unlock(&state->inode->i_lock);
202b50dc 728 status = nfs4_do_open_expired(sp, state, ctx->dentry);
1da177e4
LT
729 put_nfs_open_context(ctx);
730 return status;
731 }
732 spin_unlock(&state->inode->i_lock);
733 return -ENOENT;
734}
735
736/*
24ac23ab 737 * Returns a referenced nfs4_state if there is an open delegation on the file
1da177e4
LT
738 */
739static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
740{
741 struct nfs_delegation *delegation;
742 struct nfs_server *server = NFS_SERVER(inode);
743 struct nfs4_client *clp = server->nfs4_state;
744 struct nfs_inode *nfsi = NFS_I(inode);
745 struct nfs4_state_owner *sp = NULL;
746 struct nfs4_state *state = NULL;
747 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
748 int err;
749
750 /* Protect against reboot recovery - NOTE ORDER! */
751 down_read(&clp->cl_sem);
752 /* Protect against delegation recall */
753 down_read(&nfsi->rwsem);
754 delegation = NFS_I(inode)->delegation;
755 err = -ENOENT;
756 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
757 goto out_err;
758 err = -ENOMEM;
759 if (!(sp = nfs4_get_state_owner(server, cred))) {
760 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
761 goto out_err;
762 }
1da177e4
LT
763 state = nfs4_get_open_state(inode, sp);
764 if (state == NULL)
765 goto out_err;
766
767 err = -ENOENT;
768 if ((state->state & open_flags) == open_flags) {
769 spin_lock(&inode->i_lock);
770 if (open_flags & FMODE_READ)
771 state->nreaders++;
772 if (open_flags & FMODE_WRITE)
773 state->nwriters++;
774 spin_unlock(&inode->i_lock);
775 goto out_ok;
776 } else if (state->state != 0)
777 goto out_err;
778
779 lock_kernel();
780 err = _nfs4_do_access(inode, cred, open_flags);
781 unlock_kernel();
782 if (err != 0)
783 goto out_err;
784 set_bit(NFS_DELEGATED_STATE, &state->flags);
785 update_open_stateid(state, &delegation->stateid, open_flags);
786out_ok:
1da177e4
LT
787 nfs4_put_state_owner(sp);
788 up_read(&nfsi->rwsem);
789 up_read(&clp->cl_sem);
1da177e4
LT
790 *res = state;
791 return 0;
792out_err:
793 if (sp != NULL) {
794 if (state != NULL)
795 nfs4_put_open_state(state);
1da177e4
LT
796 nfs4_put_state_owner(sp);
797 }
798 up_read(&nfsi->rwsem);
799 up_read(&clp->cl_sem);
b8e5c4c2
TM
800 if (err != -EACCES)
801 nfs_inode_return_delegation(inode);
1da177e4
LT
802 return err;
803}
804
805static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
806{
807 struct nfs4_exception exception = { };
808 struct nfs4_state *res;
809 int err;
810
811 do {
812 err = _nfs4_open_delegated(inode, flags, cred, &res);
813 if (err == 0)
814 break;
815 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
816 err, &exception));
817 } while (exception.retry);
818 return res;
819}
820
821/*
24ac23ab 822 * Returns a referenced nfs4_state
1da177e4
LT
823 */
824static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
825{
826 struct nfs4_state_owner *sp;
827 struct nfs4_state *state = NULL;
828 struct nfs_server *server = NFS_SERVER(dir);
829 struct nfs4_client *clp = server->nfs4_state;
e56e0b78 830 struct nfs4_opendata *opendata;
1da177e4 831 int status;
1da177e4
LT
832
833 /* Protect against reboot recovery conflicts */
834 down_read(&clp->cl_sem);
835 status = -ENOMEM;
836 if (!(sp = nfs4_get_state_owner(server, cred))) {
837 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
838 goto out_err;
839 }
e56e0b78
TM
840 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
841 if (opendata == NULL)
842 goto err_put_state_owner;
1da177e4 843
24ac23ab 844 status = _nfs4_proc_open(opendata);
1da177e4 845 if (status != 0)
e56e0b78 846 goto err_opendata_free;
1da177e4
LT
847
848 status = -ENOMEM;
24ac23ab
TM
849 state = nfs4_opendata_to_nfs4_state(opendata);
850 if (state == NULL)
e56e0b78 851 goto err_opendata_free;
e56e0b78 852 if (opendata->o_res.delegation_type != 0)
24ac23ab 853 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
e56e0b78 854 nfs4_opendata_free(opendata);
1da177e4
LT
855 nfs4_put_state_owner(sp);
856 up_read(&clp->cl_sem);
857 *res = state;
858 return 0;
e56e0b78
TM
859err_opendata_free:
860 nfs4_opendata_free(opendata);
861err_put_state_owner:
862 nfs4_put_state_owner(sp);
1da177e4 863out_err:
1da177e4
LT
864 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
865 up_read(&clp->cl_sem);
1da177e4
LT
866 *res = NULL;
867 return status;
868}
869
870
871static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
872{
873 struct nfs4_exception exception = { };
874 struct nfs4_state *res;
875 int status;
876
877 do {
878 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
879 if (status == 0)
880 break;
881 /* NOTE: BAD_SEQID means the server and client disagree about the
882 * book-keeping w.r.t. state-changing operations
883 * (OPEN/CLOSE/LOCK/LOCKU...)
884 * It is actually a sign of a bug on the client or on the server.
885 *
886 * If we receive a BAD_SEQID error in the particular case of
cee54fc9 887 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1da177e4
LT
888 * have unhashed the old state_owner for us, and that we can
889 * therefore safely retry using a new one. We should still warn
890 * the user though...
891 */
892 if (status == -NFS4ERR_BAD_SEQID) {
893 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
894 exception.retry = 1;
895 continue;
896 }
550f5747
TM
897 /*
898 * BAD_STATEID on OPEN means that the server cancelled our
899 * state before it received the OPEN_CONFIRM.
900 * Recover by retrying the request as per the discussion
901 * on Page 181 of RFC3530.
902 */
903 if (status == -NFS4ERR_BAD_STATEID) {
904 exception.retry = 1;
905 continue;
906 }
1da177e4
LT
907 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
908 status, &exception));
909 } while (exception.retry);
910 return res;
911}
912
913static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
914 struct nfs_fh *fhandle, struct iattr *sattr,
915 struct nfs4_state *state)
916{
917 struct nfs_setattrargs arg = {
918 .fh = fhandle,
919 .iap = sattr,
920 .server = server,
921 .bitmask = server->attr_bitmask,
922 };
923 struct nfs_setattrres res = {
924 .fattr = fattr,
925 .server = server,
926 };
927 struct rpc_message msg = {
928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
929 .rpc_argp = &arg,
930 .rpc_resp = &res,
931 };
65e4308d 932 int status;
1da177e4 933
0e574af1 934 nfs_fattr_init(fattr);
1da177e4 935
08e9eac4 936 if (state != NULL) {
1da177e4 937 msg.rpc_cred = state->owner->so_cred;
08e9eac4
TM
938 nfs4_copy_stateid(&arg.stateid, state, current->files);
939 } else
1da177e4
LT
940 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
941
65e4308d
TM
942 status = rpc_call_sync(server->client, &msg, 0);
943 return status;
1da177e4
LT
944}
945
946static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
947 struct nfs_fh *fhandle, struct iattr *sattr,
948 struct nfs4_state *state)
949{
950 struct nfs4_exception exception = { };
951 int err;
952 do {
953 err = nfs4_handle_exception(server,
954 _nfs4_do_setattr(server, fattr, fhandle, sattr,
955 state),
956 &exception);
957 } while (exception.retry);
958 return err;
959}
960
961struct nfs4_closedata {
962 struct inode *inode;
963 struct nfs4_state *state;
964 struct nfs_closeargs arg;
965 struct nfs_closeres res;
516a6af6 966 struct nfs_fattr fattr;
1da177e4
LT
967};
968
963d8fe5 969static void nfs4_free_closedata(void *data)
9512135d 970{
963d8fe5
TM
971 struct nfs4_closedata *calldata = data;
972 struct nfs4_state_owner *sp = calldata->state->owner;
9512135d
TM
973
974 nfs4_put_open_state(calldata->state);
975 nfs_free_seqid(calldata->arg.seqid);
9512135d 976 nfs4_put_state_owner(sp);
9512135d
TM
977 kfree(calldata);
978}
979
963d8fe5 980static void nfs4_close_done(struct rpc_task *task, void *data)
1da177e4 981{
963d8fe5 982 struct nfs4_closedata *calldata = data;
1da177e4 983 struct nfs4_state *state = calldata->state;
1da177e4
LT
984 struct nfs_server *server = NFS_SERVER(calldata->inode);
985
4ce70ada
TM
986 if (RPC_ASSASSINATED(task))
987 return;
1da177e4
LT
988 /* hmm. we are done with the inode, and in the process of freeing
989 * the state_owner. we keep this around to process errors
990 */
cee54fc9 991 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1da177e4
LT
992 switch (task->tk_status) {
993 case 0:
994 memcpy(&state->stateid, &calldata->res.stateid,
995 sizeof(state->stateid));
996 break;
997 case -NFS4ERR_STALE_STATEID:
998 case -NFS4ERR_EXPIRED:
1da177e4
LT
999 nfs4_schedule_state_recovery(server->nfs4_state);
1000 break;
1001 default:
1002 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1003 rpc_restart_call(task);
1004 return;
1005 }
1006 }
516a6af6 1007 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1da177e4
LT
1008}
1009
4ce70ada 1010static void nfs4_close_prepare(struct rpc_task *task, void *data)
1da177e4 1011{
4ce70ada 1012 struct nfs4_closedata *calldata = data;
9512135d 1013 struct nfs4_state *state = calldata->state;
1da177e4
LT
1014 struct rpc_message msg = {
1015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1016 .rpc_argp = &calldata->arg,
1017 .rpc_resp = &calldata->res,
9512135d 1018 .rpc_cred = state->owner->so_cred,
1da177e4 1019 };
4cecb76f 1020 int mode = 0, old_mode;
9512135d 1021
963d8fe5 1022 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
9512135d 1023 return;
9512135d
TM
1024 /* Recalculate the new open mode in case someone reopened the file
1025 * while we were waiting in line to be scheduled.
1026 */
4cecb76f
TM
1027 spin_lock(&state->owner->so_lock);
1028 spin_lock(&calldata->inode->i_lock);
1029 mode = old_mode = state->state;
1030 if (state->nreaders == 0)
1031 mode &= ~FMODE_READ;
1032 if (state->nwriters == 0)
1033 mode &= ~FMODE_WRITE;
1034 nfs4_state_set_mode_locked(state, mode);
1035 spin_unlock(&calldata->inode->i_lock);
1036 spin_unlock(&state->owner->so_lock);
1037 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
963d8fe5
TM
1038 /* Note: exit _without_ calling nfs4_close_done */
1039 task->tk_action = NULL;
9512135d
TM
1040 return;
1041 }
516a6af6 1042 nfs_fattr_init(calldata->res.fattr);
9512135d 1043 if (mode != 0)
1da177e4 1044 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
9512135d
TM
1045 calldata->arg.open_flags = mode;
1046 rpc_call_setup(task, &msg, 0);
1da177e4
LT
1047}
1048
963d8fe5 1049static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 1050 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
1051 .rpc_call_done = nfs4_close_done,
1052 .rpc_release = nfs4_free_closedata,
1053};
1054
1da177e4
LT
1055/*
1056 * It is possible for data to be read/written from a mem-mapped file
1057 * after the sys_close call (which hits the vfs layer as a flush).
1058 * This means that we can't safely call nfsv4 close on a file until
1059 * the inode is cleared. This in turn means that we are not good
1060 * NFSv4 citizens - we do not indicate to the server to update the file's
1061 * share state even when we are done with one of the three share
1062 * stateid's in the inode.
1063 *
1064 * NOTE: Caller must be holding the sp->so_owner semaphore!
1065 */
4cecb76f 1066int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
1da177e4 1067{
516a6af6 1068 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 1069 struct nfs4_closedata *calldata;
9512135d 1070 int status = -ENOMEM;
1da177e4 1071
9512135d 1072 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1da177e4 1073 if (calldata == NULL)
9512135d 1074 goto out;
1da177e4
LT
1075 calldata->inode = inode;
1076 calldata->state = state;
1077 calldata->arg.fh = NFS_FH(inode);
9512135d 1078 calldata->arg.stateid = &state->stateid;
1da177e4 1079 /* Serialization for the sequence id */
cee54fc9 1080 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
9512135d
TM
1081 if (calldata->arg.seqid == NULL)
1082 goto out_free_calldata;
516a6af6
TM
1083 calldata->arg.bitmask = server->attr_bitmask;
1084 calldata->res.fattr = &calldata->fattr;
1085 calldata->res.server = server;
9512135d 1086
4ce70ada 1087 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
9512135d
TM
1088 if (status == 0)
1089 goto out;
1090
1091 nfs_free_seqid(calldata->arg.seqid);
1092out_free_calldata:
1093 kfree(calldata);
1094out:
1095 return status;
1da177e4
LT
1096}
1097
02a913a7
TM
1098static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1099{
1100 struct file *filp;
1101
1102 filp = lookup_instantiate_filp(nd, dentry, NULL);
1103 if (!IS_ERR(filp)) {
1104 struct nfs_open_context *ctx;
1105 ctx = (struct nfs_open_context *)filp->private_data;
1106 ctx->state = state;
1107 } else
1108 nfs4_close_state(state, nd->intent.open.flags);
1109}
1110
1111struct dentry *
1da177e4
LT
1112nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1113{
1114 struct iattr attr;
1115 struct rpc_cred *cred;
1116 struct nfs4_state *state;
02a913a7 1117 struct dentry *res;
1da177e4
LT
1118
1119 if (nd->flags & LOOKUP_CREATE) {
1120 attr.ia_mode = nd->intent.open.create_mode;
1121 attr.ia_valid = ATTR_MODE;
1122 if (!IS_POSIXACL(dir))
1123 attr.ia_mode &= ~current->fs->umask;
1124 } else {
1125 attr.ia_valid = 0;
1126 BUG_ON(nd->intent.open.flags & O_CREAT);
1127 }
1128
1129 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1130 if (IS_ERR(cred))
02a913a7 1131 return (struct dentry *)cred;
1da177e4
LT
1132 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1133 put_rpccred(cred);
02a913a7
TM
1134 if (IS_ERR(state)) {
1135 if (PTR_ERR(state) == -ENOENT)
1136 d_add(dentry, NULL);
1137 return (struct dentry *)state;
1138 }
24ac23ab 1139 res = d_add_unique(dentry, igrab(state->inode));
02a913a7
TM
1140 if (res != NULL)
1141 dentry = res;
1142 nfs4_intent_set_file(nd, dentry, state);
1143 return res;
1da177e4
LT
1144}
1145
1146int
02a913a7 1147nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1da177e4
LT
1148{
1149 struct rpc_cred *cred;
1150 struct nfs4_state *state;
1da177e4
LT
1151
1152 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1153 if (IS_ERR(cred))
1154 return PTR_ERR(cred);
1155 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1156 if (IS_ERR(state))
1157 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1158 put_rpccred(cred);
02a913a7
TM
1159 if (IS_ERR(state)) {
1160 switch (PTR_ERR(state)) {
1161 case -EPERM:
1162 case -EACCES:
1163 case -EDQUOT:
1164 case -ENOSPC:
1165 case -EROFS:
1166 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1167 return 1;
1168 case -ENOENT:
1169 if (dentry->d_inode == NULL)
1170 return 1;
1171 }
1172 goto out_drop;
1173 }
24ac23ab 1174 if (state->inode == dentry->d_inode) {
02a913a7 1175 nfs4_intent_set_file(nd, dentry, state);
1da177e4
LT
1176 return 1;
1177 }
1da177e4 1178 nfs4_close_state(state, openflags);
02a913a7
TM
1179out_drop:
1180 d_drop(dentry);
1da177e4
LT
1181 return 0;
1182}
1183
1184
1185static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1186{
1187 struct nfs4_server_caps_res res = {};
1188 struct rpc_message msg = {
1189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1190 .rpc_argp = fhandle,
1191 .rpc_resp = &res,
1192 };
1193 int status;
1194
1195 status = rpc_call_sync(server->client, &msg, 0);
1196 if (status == 0) {
1197 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1198 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1199 server->caps |= NFS_CAP_ACLS;
1200 if (res.has_links != 0)
1201 server->caps |= NFS_CAP_HARDLINKS;
1202 if (res.has_symlinks != 0)
1203 server->caps |= NFS_CAP_SYMLINKS;
1204 server->acl_bitmask = res.acl_bitmask;
1205 }
1206 return status;
1207}
1208
1209static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1210{
1211 struct nfs4_exception exception = { };
1212 int err;
1213 do {
1214 err = nfs4_handle_exception(server,
1215 _nfs4_server_capabilities(server, fhandle),
1216 &exception);
1217 } while (exception.retry);
1218 return err;
1219}
1220
1221static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1222 struct nfs_fsinfo *info)
1223{
1da177e4
LT
1224 struct nfs4_lookup_root_arg args = {
1225 .bitmask = nfs4_fattr_bitmap,
1226 };
1227 struct nfs4_lookup_res res = {
1228 .server = server,
0e574af1 1229 .fattr = info->fattr,
1da177e4
LT
1230 .fh = fhandle,
1231 };
1232 struct rpc_message msg = {
1233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1234 .rpc_argp = &args,
1235 .rpc_resp = &res,
1236 };
0e574af1 1237 nfs_fattr_init(info->fattr);
1da177e4
LT
1238 return rpc_call_sync(server->client, &msg, 0);
1239}
1240
1241static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1242 struct nfs_fsinfo *info)
1243{
1244 struct nfs4_exception exception = { };
1245 int err;
1246 do {
1247 err = nfs4_handle_exception(server,
1248 _nfs4_lookup_root(server, fhandle, info),
1249 &exception);
1250 } while (exception.retry);
1251 return err;
1252}
1253
1254static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1255 struct nfs_fsinfo *info)
1256{
1257 struct nfs_fattr * fattr = info->fattr;
1258 unsigned char * p;
1259 struct qstr q;
1260 struct nfs4_lookup_arg args = {
1261 .dir_fh = fhandle,
1262 .name = &q,
1263 .bitmask = nfs4_fattr_bitmap,
1264 };
1265 struct nfs4_lookup_res res = {
1266 .server = server,
1267 .fattr = fattr,
1268 .fh = fhandle,
1269 };
1270 struct rpc_message msg = {
1271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1272 .rpc_argp = &args,
1273 .rpc_resp = &res,
1274 };
1275 int status;
1276
1277 /*
1278 * Now we do a separate LOOKUP for each component of the mount path.
1279 * The LOOKUPs are done separately so that we can conveniently
1280 * catch an ERR_WRONGSEC if it occurs along the way...
1281 */
1282 status = nfs4_lookup_root(server, fhandle, info);
1283 if (status)
1284 goto out;
1285
1286 p = server->mnt_path;
1287 for (;;) {
1288 struct nfs4_exception exception = { };
1289
1290 while (*p == '/')
1291 p++;
1292 if (!*p)
1293 break;
1294 q.name = p;
1295 while (*p && (*p != '/'))
1296 p++;
1297 q.len = p - q.name;
1298
1299 do {
0e574af1 1300 nfs_fattr_init(fattr);
1da177e4
LT
1301 status = nfs4_handle_exception(server,
1302 rpc_call_sync(server->client, &msg, 0),
1303 &exception);
1304 } while (exception.retry);
1305 if (status == 0)
1306 continue;
1307 if (status == -ENOENT) {
1308 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1309 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1310 }
1311 break;
1312 }
1313 if (status == 0)
1314 status = nfs4_server_capabilities(server, fhandle);
1315 if (status == 0)
1316 status = nfs4_do_fsinfo(server, fhandle, info);
1317out:
1318 return status;
1319}
1320
1321static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1322{
1323 struct nfs4_getattr_arg args = {
1324 .fh = fhandle,
1325 .bitmask = server->attr_bitmask,
1326 };
1327 struct nfs4_getattr_res res = {
1328 .fattr = fattr,
1329 .server = server,
1330 };
1331 struct rpc_message msg = {
1332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1333 .rpc_argp = &args,
1334 .rpc_resp = &res,
1335 };
1336
0e574af1 1337 nfs_fattr_init(fattr);
1da177e4
LT
1338 return rpc_call_sync(server->client, &msg, 0);
1339}
1340
1341static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1342{
1343 struct nfs4_exception exception = { };
1344 int err;
1345 do {
1346 err = nfs4_handle_exception(server,
1347 _nfs4_proc_getattr(server, fhandle, fattr),
1348 &exception);
1349 } while (exception.retry);
1350 return err;
1351}
1352
1353/*
1354 * The file is not closed if it is opened due to the a request to change
1355 * the size of the file. The open call will not be needed once the
1356 * VFS layer lookup-intents are implemented.
1357 *
1358 * Close is called when the inode is destroyed.
1359 * If we haven't opened the file for O_WRONLY, we
1360 * need to in the size_change case to obtain a stateid.
1361 *
1362 * Got race?
1363 * Because OPEN is always done by name in nfsv4, it is
1364 * possible that we opened a different file by the same
1365 * name. We can recognize this race condition, but we
1366 * can't do anything about it besides returning an error.
1367 *
1368 * This will be fixed with VFS changes (lookup-intent).
1369 */
1370static int
1371nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1372 struct iattr *sattr)
1373{
08e9eac4
TM
1374 struct rpc_cred *cred;
1375 struct inode *inode = dentry->d_inode;
d530838b
TM
1376 struct nfs_open_context *ctx;
1377 struct nfs4_state *state = NULL;
1da177e4
LT
1378 int status;
1379
0e574af1 1380 nfs_fattr_init(fattr);
1da177e4 1381
08e9eac4
TM
1382 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1383 if (IS_ERR(cred))
1384 return PTR_ERR(cred);
d530838b
TM
1385
1386 /* Search for an existing open(O_WRITE) file */
1387 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1388 if (ctx != NULL)
1389 state = ctx->state;
08e9eac4 1390
1da177e4
LT
1391 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1392 NFS_FH(inode), sattr, state);
65e4308d
TM
1393 if (status == 0)
1394 nfs_setattr_update_inode(inode, sattr);
d530838b
TM
1395 if (ctx != NULL)
1396 put_nfs_open_context(ctx);
08e9eac4 1397 put_rpccred(cred);
1da177e4
LT
1398 return status;
1399}
1400
1401static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1402 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1403{
1404 int status;
1405 struct nfs_server *server = NFS_SERVER(dir);
1406 struct nfs4_lookup_arg args = {
1407 .bitmask = server->attr_bitmask,
1408 .dir_fh = NFS_FH(dir),
1409 .name = name,
1410 };
1411 struct nfs4_lookup_res res = {
1412 .server = server,
1413 .fattr = fattr,
1414 .fh = fhandle,
1415 };
1416 struct rpc_message msg = {
1417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1418 .rpc_argp = &args,
1419 .rpc_resp = &res,
1420 };
1421
0e574af1 1422 nfs_fattr_init(fattr);
1da177e4
LT
1423
1424 dprintk("NFS call lookup %s\n", name->name);
1425 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1426 dprintk("NFS reply lookup: %d\n", status);
1427 return status;
1428}
1429
1430static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1431{
1432 struct nfs4_exception exception = { };
1433 int err;
1434 do {
1435 err = nfs4_handle_exception(NFS_SERVER(dir),
1436 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1437 &exception);
1438 } while (exception.retry);
1439 return err;
1440}
1441
1442static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1443{
1444 struct nfs4_accessargs args = {
1445 .fh = NFS_FH(inode),
1446 };
1447 struct nfs4_accessres res = { 0 };
1448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1450 .rpc_argp = &args,
1451 .rpc_resp = &res,
1452 .rpc_cred = entry->cred,
1453 };
1454 int mode = entry->mask;
1455 int status;
1456
1457 /*
1458 * Determine which access bits we want to ask for...
1459 */
1460 if (mode & MAY_READ)
1461 args.access |= NFS4_ACCESS_READ;
1462 if (S_ISDIR(inode->i_mode)) {
1463 if (mode & MAY_WRITE)
1464 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1465 if (mode & MAY_EXEC)
1466 args.access |= NFS4_ACCESS_LOOKUP;
1467 } else {
1468 if (mode & MAY_WRITE)
1469 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1470 if (mode & MAY_EXEC)
1471 args.access |= NFS4_ACCESS_EXECUTE;
1472 }
1473 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1474 if (!status) {
1475 entry->mask = 0;
1476 if (res.access & NFS4_ACCESS_READ)
1477 entry->mask |= MAY_READ;
1478 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1479 entry->mask |= MAY_WRITE;
1480 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1481 entry->mask |= MAY_EXEC;
1482 }
1483 return status;
1484}
1485
1486static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1487{
1488 struct nfs4_exception exception = { };
1489 int err;
1490 do {
1491 err = nfs4_handle_exception(NFS_SERVER(inode),
1492 _nfs4_proc_access(inode, entry),
1493 &exception);
1494 } while (exception.retry);
1495 return err;
1496}
1497
1498/*
1499 * TODO: For the time being, we don't try to get any attributes
1500 * along with any of the zero-copy operations READ, READDIR,
1501 * READLINK, WRITE.
1502 *
1503 * In the case of the first three, we want to put the GETATTR
1504 * after the read-type operation -- this is because it is hard
1505 * to predict the length of a GETATTR response in v4, and thus
1506 * align the READ data correctly. This means that the GETATTR
1507 * may end up partially falling into the page cache, and we should
1508 * shift it into the 'tail' of the xdr_buf before processing.
1509 * To do this efficiently, we need to know the total length
1510 * of data received, which doesn't seem to be available outside
1511 * of the RPC layer.
1512 *
1513 * In the case of WRITE, we also want to put the GETATTR after
1514 * the operation -- in this case because we want to make sure
1515 * we get the post-operation mtime and size. This means that
1516 * we can't use xdr_encode_pages() as written: we need a variant
1517 * of it which would leave room in the 'tail' iovec.
1518 *
1519 * Both of these changes to the XDR layer would in fact be quite
1520 * minor, but I decided to leave them for a subsequent patch.
1521 */
1522static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1523 unsigned int pgbase, unsigned int pglen)
1524{
1525 struct nfs4_readlink args = {
1526 .fh = NFS_FH(inode),
1527 .pgbase = pgbase,
1528 .pglen = pglen,
1529 .pages = &page,
1530 };
1531 struct rpc_message msg = {
1532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1533 .rpc_argp = &args,
1534 .rpc_resp = NULL,
1535 };
1536
1537 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1538}
1539
1540static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1541 unsigned int pgbase, unsigned int pglen)
1542{
1543 struct nfs4_exception exception = { };
1544 int err;
1545 do {
1546 err = nfs4_handle_exception(NFS_SERVER(inode),
1547 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1548 &exception);
1549 } while (exception.retry);
1550 return err;
1551}
1552
1553static int _nfs4_proc_read(struct nfs_read_data *rdata)
1554{
1555 int flags = rdata->flags;
1556 struct inode *inode = rdata->inode;
1557 struct nfs_fattr *fattr = rdata->res.fattr;
1558 struct nfs_server *server = NFS_SERVER(inode);
1559 struct rpc_message msg = {
1560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1561 .rpc_argp = &rdata->args,
1562 .rpc_resp = &rdata->res,
1563 .rpc_cred = rdata->cred,
1564 };
1565 unsigned long timestamp = jiffies;
1566 int status;
1567
1568 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1569 (long long) rdata->args.offset);
1570
0e574af1 1571 nfs_fattr_init(fattr);
1da177e4
LT
1572 status = rpc_call_sync(server->client, &msg, flags);
1573 if (!status)
1574 renew_lease(server, timestamp);
1575 dprintk("NFS reply read: %d\n", status);
1576 return status;
1577}
1578
1579static int nfs4_proc_read(struct nfs_read_data *rdata)
1580{
1581 struct nfs4_exception exception = { };
1582 int err;
1583 do {
1584 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1585 _nfs4_proc_read(rdata),
1586 &exception);
1587 } while (exception.retry);
1588 return err;
1589}
1590
1591static int _nfs4_proc_write(struct nfs_write_data *wdata)
1592{
1593 int rpcflags = wdata->flags;
1594 struct inode *inode = wdata->inode;
1595 struct nfs_fattr *fattr = wdata->res.fattr;
1596 struct nfs_server *server = NFS_SERVER(inode);
1597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1599 .rpc_argp = &wdata->args,
1600 .rpc_resp = &wdata->res,
1601 .rpc_cred = wdata->cred,
1602 };
1603 int status;
1604
1605 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1606 (long long) wdata->args.offset);
1607
3b6efee9
TM
1608 wdata->args.bitmask = server->attr_bitmask;
1609 wdata->res.server = server;
0e574af1 1610 nfs_fattr_init(fattr);
1da177e4
LT
1611 status = rpc_call_sync(server->client, &msg, rpcflags);
1612 dprintk("NFS reply write: %d\n", status);
3b6efee9
TM
1613 if (status < 0)
1614 return status;
1615 nfs_post_op_update_inode(inode, fattr);
1616 return wdata->res.count;
1da177e4
LT
1617}
1618
1619static int nfs4_proc_write(struct nfs_write_data *wdata)
1620{
1621 struct nfs4_exception exception = { };
1622 int err;
1623 do {
1624 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1625 _nfs4_proc_write(wdata),
1626 &exception);
1627 } while (exception.retry);
1628 return err;
1629}
1630
1631static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1632{
1633 struct inode *inode = cdata->inode;
1634 struct nfs_fattr *fattr = cdata->res.fattr;
1635 struct nfs_server *server = NFS_SERVER(inode);
1636 struct rpc_message msg = {
1637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1638 .rpc_argp = &cdata->args,
1639 .rpc_resp = &cdata->res,
1640 .rpc_cred = cdata->cred,
1641 };
1642 int status;
1643
1644 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1645 (long long) cdata->args.offset);
1646
3b6efee9
TM
1647 cdata->args.bitmask = server->attr_bitmask;
1648 cdata->res.server = server;
0e574af1 1649 nfs_fattr_init(fattr);
1da177e4
LT
1650 status = rpc_call_sync(server->client, &msg, 0);
1651 dprintk("NFS reply commit: %d\n", status);
3b6efee9
TM
1652 if (status >= 0)
1653 nfs_post_op_update_inode(inode, fattr);
1da177e4
LT
1654 return status;
1655}
1656
1657static int nfs4_proc_commit(struct nfs_write_data *cdata)
1658{
1659 struct nfs4_exception exception = { };
1660 int err;
1661 do {
1662 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1663 _nfs4_proc_commit(cdata),
1664 &exception);
1665 } while (exception.retry);
1666 return err;
1667}
1668
1669/*
1670 * Got race?
1671 * We will need to arrange for the VFS layer to provide an atomic open.
1672 * Until then, this create/open method is prone to inefficiency and race
1673 * conditions due to the lookup, create, and open VFS calls from sys_open()
1674 * placed on the wire.
1675 *
1676 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1677 * The file will be opened again in the subsequent VFS open call
1678 * (nfs4_proc_file_open).
1679 *
1680 * The open for read will just hang around to be used by any process that
1681 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1682 */
1683
1684static int
1685nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
02a913a7 1686 int flags, struct nameidata *nd)
1da177e4
LT
1687{
1688 struct nfs4_state *state;
1689 struct rpc_cred *cred;
1690 int status = 0;
1691
1692 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1693 if (IS_ERR(cred)) {
1694 status = PTR_ERR(cred);
1695 goto out;
1696 }
1697 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1698 put_rpccred(cred);
1699 if (IS_ERR(state)) {
1700 status = PTR_ERR(state);
1701 goto out;
1702 }
24ac23ab 1703 d_instantiate(dentry, igrab(state->inode));
1da177e4
LT
1704 if (flags & O_EXCL) {
1705 struct nfs_fattr fattr;
1706 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1707 NFS_FH(state->inode), sattr, state);
02a913a7 1708 if (status == 0)
65e4308d 1709 nfs_setattr_update_inode(state->inode, sattr);
02a913a7
TM
1710 }
1711 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1712 nfs4_intent_set_file(nd, dentry, state);
1713 else
1714 nfs4_close_state(state, flags);
1da177e4
LT
1715out:
1716 return status;
1717}
1718
1719static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1720{
16e42959 1721 struct nfs_server *server = NFS_SERVER(dir);
1da177e4
LT
1722 struct nfs4_remove_arg args = {
1723 .fh = NFS_FH(dir),
1724 .name = name,
16e42959
TM
1725 .bitmask = server->attr_bitmask,
1726 };
1727 struct nfs_fattr dir_attr;
1728 struct nfs4_remove_res res = {
1729 .server = server,
1730 .dir_attr = &dir_attr,
1da177e4 1731 };
1da177e4
LT
1732 struct rpc_message msg = {
1733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1734 .rpc_argp = &args,
1735 .rpc_resp = &res,
1736 };
1737 int status;
1738
16e42959
TM
1739 nfs_fattr_init(res.dir_attr);
1740 status = rpc_call_sync(server->client, &msg, 0);
1741 if (status == 0) {
1742 update_changeattr(dir, &res.cinfo);
1743 nfs_post_op_update_inode(dir, res.dir_attr);
1744 }
1da177e4
LT
1745 return status;
1746}
1747
1748static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1749{
1750 struct nfs4_exception exception = { };
1751 int err;
1752 do {
1753 err = nfs4_handle_exception(NFS_SERVER(dir),
1754 _nfs4_proc_remove(dir, name),
1755 &exception);
1756 } while (exception.retry);
1757 return err;
1758}
1759
1760struct unlink_desc {
1761 struct nfs4_remove_arg args;
16e42959
TM
1762 struct nfs4_remove_res res;
1763 struct nfs_fattr dir_attr;
1da177e4
LT
1764};
1765
1766static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1767 struct qstr *name)
1768{
16e42959 1769 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1da177e4
LT
1770 struct unlink_desc *up;
1771
1772 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1773 if (!up)
1774 return -ENOMEM;
1775
1776 up->args.fh = NFS_FH(dir->d_inode);
1777 up->args.name = name;
16e42959
TM
1778 up->args.bitmask = server->attr_bitmask;
1779 up->res.server = server;
1780 up->res.dir_attr = &up->dir_attr;
1da177e4
LT
1781
1782 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1783 msg->rpc_argp = &up->args;
1784 msg->rpc_resp = &up->res;
1785 return 0;
1786}
1787
1788static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1789{
1790 struct rpc_message *msg = &task->tk_msg;
1791 struct unlink_desc *up;
1792
1793 if (msg->rpc_resp != NULL) {
1794 up = container_of(msg->rpc_resp, struct unlink_desc, res);
16e42959
TM
1795 update_changeattr(dir->d_inode, &up->res.cinfo);
1796 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1da177e4
LT
1797 kfree(up);
1798 msg->rpc_resp = NULL;
1799 msg->rpc_argp = NULL;
1800 }
1801 return 0;
1802}
1803
1804static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1805 struct inode *new_dir, struct qstr *new_name)
1806{
6caf2c82 1807 struct nfs_server *server = NFS_SERVER(old_dir);
1da177e4
LT
1808 struct nfs4_rename_arg arg = {
1809 .old_dir = NFS_FH(old_dir),
1810 .new_dir = NFS_FH(new_dir),
1811 .old_name = old_name,
1812 .new_name = new_name,
6caf2c82
TM
1813 .bitmask = server->attr_bitmask,
1814 };
1815 struct nfs_fattr old_fattr, new_fattr;
1816 struct nfs4_rename_res res = {
1817 .server = server,
1818 .old_fattr = &old_fattr,
1819 .new_fattr = &new_fattr,
1da177e4 1820 };
1da177e4
LT
1821 struct rpc_message msg = {
1822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1823 .rpc_argp = &arg,
1824 .rpc_resp = &res,
1825 };
1826 int status;
1827
6caf2c82
TM
1828 nfs_fattr_init(res.old_fattr);
1829 nfs_fattr_init(res.new_fattr);
1830 status = rpc_call_sync(server->client, &msg, 0);
1da177e4
LT
1831
1832 if (!status) {
1833 update_changeattr(old_dir, &res.old_cinfo);
6caf2c82 1834 nfs_post_op_update_inode(old_dir, res.old_fattr);
1da177e4 1835 update_changeattr(new_dir, &res.new_cinfo);
6caf2c82 1836 nfs_post_op_update_inode(new_dir, res.new_fattr);
1da177e4
LT
1837 }
1838 return status;
1839}
1840
1841static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1842 struct inode *new_dir, struct qstr *new_name)
1843{
1844 struct nfs4_exception exception = { };
1845 int err;
1846 do {
1847 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1848 _nfs4_proc_rename(old_dir, old_name,
1849 new_dir, new_name),
1850 &exception);
1851 } while (exception.retry);
1852 return err;
1853}
1854
1855static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1856{
91ba2eee 1857 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
1858 struct nfs4_link_arg arg = {
1859 .fh = NFS_FH(inode),
1860 .dir_fh = NFS_FH(dir),
1861 .name = name,
91ba2eee
TM
1862 .bitmask = server->attr_bitmask,
1863 };
1864 struct nfs_fattr fattr, dir_attr;
1865 struct nfs4_link_res res = {
1866 .server = server,
1867 .fattr = &fattr,
1868 .dir_attr = &dir_attr,
1da177e4 1869 };
1da177e4
LT
1870 struct rpc_message msg = {
1871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1872 .rpc_argp = &arg,
91ba2eee 1873 .rpc_resp = &res,
1da177e4
LT
1874 };
1875 int status;
1876
91ba2eee
TM
1877 nfs_fattr_init(res.fattr);
1878 nfs_fattr_init(res.dir_attr);
1879 status = rpc_call_sync(server->client, &msg, 0);
1880 if (!status) {
1881 update_changeattr(dir, &res.cinfo);
1882 nfs_post_op_update_inode(dir, res.dir_attr);
1883 nfs_refresh_inode(inode, res.fattr);
1884 }
1da177e4
LT
1885
1886 return status;
1887}
1888
1889static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1890{
1891 struct nfs4_exception exception = { };
1892 int err;
1893 do {
1894 err = nfs4_handle_exception(NFS_SERVER(inode),
1895 _nfs4_proc_link(inode, dir, name),
1896 &exception);
1897 } while (exception.retry);
1898 return err;
1899}
1900
1901static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1902 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1903 struct nfs_fattr *fattr)
1904{
1905 struct nfs_server *server = NFS_SERVER(dir);
56ae19f3 1906 struct nfs_fattr dir_fattr;
1da177e4
LT
1907 struct nfs4_create_arg arg = {
1908 .dir_fh = NFS_FH(dir),
1909 .server = server,
1910 .name = name,
1911 .attrs = sattr,
1912 .ftype = NF4LNK,
1913 .bitmask = server->attr_bitmask,
1914 };
1915 struct nfs4_create_res res = {
1916 .server = server,
1917 .fh = fhandle,
1918 .fattr = fattr,
56ae19f3 1919 .dir_fattr = &dir_fattr,
1da177e4
LT
1920 };
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1923 .rpc_argp = &arg,
1924 .rpc_resp = &res,
1925 };
1926 int status;
1927
1928 if (path->len > NFS4_MAXPATHLEN)
1929 return -ENAMETOOLONG;
1930 arg.u.symlink = path;
0e574af1 1931 nfs_fattr_init(fattr);
56ae19f3 1932 nfs_fattr_init(&dir_fattr);
1da177e4
LT
1933
1934 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1935 if (!status)
1936 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 1937 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
1938 return status;
1939}
1940
1941static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1942 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1943 struct nfs_fattr *fattr)
1944{
1945 struct nfs4_exception exception = { };
1946 int err;
1947 do {
1948 err = nfs4_handle_exception(NFS_SERVER(dir),
1949 _nfs4_proc_symlink(dir, name, path, sattr,
1950 fhandle, fattr),
1951 &exception);
1952 } while (exception.retry);
1953 return err;
1954}
1955
1956static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1957 struct iattr *sattr)
1958{
1959 struct nfs_server *server = NFS_SERVER(dir);
1960 struct nfs_fh fhandle;
56ae19f3 1961 struct nfs_fattr fattr, dir_fattr;
1da177e4
LT
1962 struct nfs4_create_arg arg = {
1963 .dir_fh = NFS_FH(dir),
1964 .server = server,
1965 .name = &dentry->d_name,
1966 .attrs = sattr,
1967 .ftype = NF4DIR,
1968 .bitmask = server->attr_bitmask,
1969 };
1970 struct nfs4_create_res res = {
1971 .server = server,
1972 .fh = &fhandle,
1973 .fattr = &fattr,
56ae19f3 1974 .dir_fattr = &dir_fattr,
1da177e4
LT
1975 };
1976 struct rpc_message msg = {
1977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1978 .rpc_argp = &arg,
1979 .rpc_resp = &res,
1980 };
1981 int status;
1982
0e574af1 1983 nfs_fattr_init(&fattr);
56ae19f3 1984 nfs_fattr_init(&dir_fattr);
1da177e4
LT
1985
1986 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1987 if (!status) {
1988 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 1989 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
1990 status = nfs_instantiate(dentry, &fhandle, &fattr);
1991 }
1992 return status;
1993}
1994
1995static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1996 struct iattr *sattr)
1997{
1998 struct nfs4_exception exception = { };
1999 int err;
2000 do {
2001 err = nfs4_handle_exception(NFS_SERVER(dir),
2002 _nfs4_proc_mkdir(dir, dentry, sattr),
2003 &exception);
2004 } while (exception.retry);
2005 return err;
2006}
2007
2008static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2009 u64 cookie, struct page *page, unsigned int count, int plus)
2010{
2011 struct inode *dir = dentry->d_inode;
2012 struct nfs4_readdir_arg args = {
2013 .fh = NFS_FH(dir),
2014 .pages = &page,
2015 .pgbase = 0,
2016 .count = count,
2017 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2018 };
2019 struct nfs4_readdir_res res;
2020 struct rpc_message msg = {
2021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2022 .rpc_argp = &args,
2023 .rpc_resp = &res,
2024 .rpc_cred = cred,
2025 };
2026 int status;
2027
eadf4598
TM
2028 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2029 dentry->d_parent->d_name.name,
2030 dentry->d_name.name,
2031 (unsigned long long)cookie);
1da177e4
LT
2032 lock_kernel();
2033 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2034 res.pgbase = args.pgbase;
2035 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2036 if (status == 0)
2037 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2038 unlock_kernel();
eadf4598 2039 dprintk("%s: returns %d\n", __FUNCTION__, status);
1da177e4
LT
2040 return status;
2041}
2042
2043static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2044 u64 cookie, struct page *page, unsigned int count, int plus)
2045{
2046 struct nfs4_exception exception = { };
2047 int err;
2048 do {
2049 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2050 _nfs4_proc_readdir(dentry, cred, cookie,
2051 page, count, plus),
2052 &exception);
2053 } while (exception.retry);
2054 return err;
2055}
2056
2057static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2058 struct iattr *sattr, dev_t rdev)
2059{
2060 struct nfs_server *server = NFS_SERVER(dir);
2061 struct nfs_fh fh;
56ae19f3 2062 struct nfs_fattr fattr, dir_fattr;
1da177e4
LT
2063 struct nfs4_create_arg arg = {
2064 .dir_fh = NFS_FH(dir),
2065 .server = server,
2066 .name = &dentry->d_name,
2067 .attrs = sattr,
2068 .bitmask = server->attr_bitmask,
2069 };
2070 struct nfs4_create_res res = {
2071 .server = server,
2072 .fh = &fh,
2073 .fattr = &fattr,
56ae19f3 2074 .dir_fattr = &dir_fattr,
1da177e4
LT
2075 };
2076 struct rpc_message msg = {
2077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2078 .rpc_argp = &arg,
2079 .rpc_resp = &res,
2080 };
2081 int status;
2082 int mode = sattr->ia_mode;
2083
0e574af1 2084 nfs_fattr_init(&fattr);
56ae19f3 2085 nfs_fattr_init(&dir_fattr);
1da177e4
LT
2086
2087 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2088 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2089 if (S_ISFIFO(mode))
2090 arg.ftype = NF4FIFO;
2091 else if (S_ISBLK(mode)) {
2092 arg.ftype = NF4BLK;
2093 arg.u.device.specdata1 = MAJOR(rdev);
2094 arg.u.device.specdata2 = MINOR(rdev);
2095 }
2096 else if (S_ISCHR(mode)) {
2097 arg.ftype = NF4CHR;
2098 arg.u.device.specdata1 = MAJOR(rdev);
2099 arg.u.device.specdata2 = MINOR(rdev);
2100 }
2101 else
2102 arg.ftype = NF4SOCK;
2103
2104 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2105 if (status == 0) {
2106 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 2107 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
2108 status = nfs_instantiate(dentry, &fh, &fattr);
2109 }
2110 return status;
2111}
2112
2113static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2114 struct iattr *sattr, dev_t rdev)
2115{
2116 struct nfs4_exception exception = { };
2117 int err;
2118 do {
2119 err = nfs4_handle_exception(NFS_SERVER(dir),
2120 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2121 &exception);
2122 } while (exception.retry);
2123 return err;
2124}
2125
2126static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2127 struct nfs_fsstat *fsstat)
2128{
2129 struct nfs4_statfs_arg args = {
2130 .fh = fhandle,
2131 .bitmask = server->attr_bitmask,
2132 };
2133 struct rpc_message msg = {
2134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2135 .rpc_argp = &args,
2136 .rpc_resp = fsstat,
2137 };
2138
0e574af1 2139 nfs_fattr_init(fsstat->fattr);
1da177e4
LT
2140 return rpc_call_sync(server->client, &msg, 0);
2141}
2142
2143static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2144{
2145 struct nfs4_exception exception = { };
2146 int err;
2147 do {
2148 err = nfs4_handle_exception(server,
2149 _nfs4_proc_statfs(server, fhandle, fsstat),
2150 &exception);
2151 } while (exception.retry);
2152 return err;
2153}
2154
2155static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2156 struct nfs_fsinfo *fsinfo)
2157{
2158 struct nfs4_fsinfo_arg args = {
2159 .fh = fhandle,
2160 .bitmask = server->attr_bitmask,
2161 };
2162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2164 .rpc_argp = &args,
2165 .rpc_resp = fsinfo,
2166 };
2167
2168 return rpc_call_sync(server->client, &msg, 0);
2169}
2170
2171static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2172{
2173 struct nfs4_exception exception = { };
2174 int err;
2175
2176 do {
2177 err = nfs4_handle_exception(server,
2178 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2179 &exception);
2180 } while (exception.retry);
2181 return err;
2182}
2183
2184static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2185{
0e574af1 2186 nfs_fattr_init(fsinfo->fattr);
1da177e4
LT
2187 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2188}
2189
2190static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2191 struct nfs_pathconf *pathconf)
2192{
2193 struct nfs4_pathconf_arg args = {
2194 .fh = fhandle,
2195 .bitmask = server->attr_bitmask,
2196 };
2197 struct rpc_message msg = {
2198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2199 .rpc_argp = &args,
2200 .rpc_resp = pathconf,
2201 };
2202
2203 /* None of the pathconf attributes are mandatory to implement */
2204 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2205 memset(pathconf, 0, sizeof(*pathconf));
2206 return 0;
2207 }
2208
0e574af1 2209 nfs_fattr_init(pathconf->fattr);
1da177e4
LT
2210 return rpc_call_sync(server->client, &msg, 0);
2211}
2212
2213static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2214 struct nfs_pathconf *pathconf)
2215{
2216 struct nfs4_exception exception = { };
2217 int err;
2218
2219 do {
2220 err = nfs4_handle_exception(server,
2221 _nfs4_proc_pathconf(server, fhandle, pathconf),
2222 &exception);
2223 } while (exception.retry);
2224 return err;
2225}
2226
963d8fe5 2227static void nfs4_read_done(struct rpc_task *task, void *calldata)
1da177e4 2228{
963d8fe5 2229 struct nfs_read_data *data = calldata;
1da177e4
LT
2230 struct inode *inode = data->inode;
2231
2232 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2233 rpc_restart_call(task);
2234 return;
2235 }
2236 if (task->tk_status > 0)
2237 renew_lease(NFS_SERVER(inode), data->timestamp);
2238 /* Call back common NFS readpage processing */
963d8fe5 2239 nfs_readpage_result(task, calldata);
1da177e4
LT
2240}
2241
963d8fe5
TM
2242static const struct rpc_call_ops nfs4_read_ops = {
2243 .rpc_call_done = nfs4_read_done,
2244 .rpc_release = nfs_readdata_release,
2245};
2246
1da177e4
LT
2247static void
2248nfs4_proc_read_setup(struct nfs_read_data *data)
2249{
2250 struct rpc_task *task = &data->task;
2251 struct rpc_message msg = {
2252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2253 .rpc_argp = &data->args,
2254 .rpc_resp = &data->res,
2255 .rpc_cred = data->cred,
2256 };
2257 struct inode *inode = data->inode;
2258 int flags;
2259
2260 data->timestamp = jiffies;
2261
2262 /* N.B. Do we need to test? Never called for swapfile inode */
2263 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2264
2265 /* Finalize the task. */
963d8fe5 2266 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
1da177e4
LT
2267 rpc_call_setup(task, &msg, 0);
2268}
2269
963d8fe5 2270static void nfs4_write_done(struct rpc_task *task, void *calldata)
1da177e4 2271{
963d8fe5 2272 struct nfs_write_data *data = calldata;
1da177e4
LT
2273 struct inode *inode = data->inode;
2274
2275 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2276 rpc_restart_call(task);
2277 return;
2278 }
4f9838c7 2279 if (task->tk_status >= 0) {
1da177e4 2280 renew_lease(NFS_SERVER(inode), data->timestamp);
4f9838c7
TM
2281 nfs_post_op_update_inode(inode, data->res.fattr);
2282 }
1da177e4 2283 /* Call back common NFS writeback processing */
963d8fe5 2284 nfs_writeback_done(task, calldata);
1da177e4
LT
2285}
2286
963d8fe5
TM
2287static const struct rpc_call_ops nfs4_write_ops = {
2288 .rpc_call_done = nfs4_write_done,
2289 .rpc_release = nfs_writedata_release,
2290};
2291
1da177e4
LT
2292static void
2293nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2294{
2295 struct rpc_task *task = &data->task;
2296 struct rpc_message msg = {
2297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2298 .rpc_argp = &data->args,
2299 .rpc_resp = &data->res,
2300 .rpc_cred = data->cred,
2301 };
2302 struct inode *inode = data->inode;
4f9838c7 2303 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2304 int stable;
2305 int flags;
2306
2307 if (how & FLUSH_STABLE) {
2308 if (!NFS_I(inode)->ncommit)
2309 stable = NFS_FILE_SYNC;
2310 else
2311 stable = NFS_DATA_SYNC;
2312 } else
2313 stable = NFS_UNSTABLE;
2314 data->args.stable = stable;
4f9838c7
TM
2315 data->args.bitmask = server->attr_bitmask;
2316 data->res.server = server;
1da177e4
LT
2317
2318 data->timestamp = jiffies;
2319
2320 /* Set the initial flags for the task. */
2321 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2322
2323 /* Finalize the task. */
963d8fe5 2324 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
1da177e4
LT
2325 rpc_call_setup(task, &msg, 0);
2326}
2327
963d8fe5 2328static void nfs4_commit_done(struct rpc_task *task, void *calldata)
1da177e4 2329{
963d8fe5 2330 struct nfs_write_data *data = calldata;
1da177e4
LT
2331 struct inode *inode = data->inode;
2332
2333 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2334 rpc_restart_call(task);
2335 return;
2336 }
4f9838c7
TM
2337 if (task->tk_status >= 0)
2338 nfs_post_op_update_inode(inode, data->res.fattr);
1da177e4 2339 /* Call back common NFS writeback processing */
963d8fe5 2340 nfs_commit_done(task, calldata);
1da177e4
LT
2341}
2342
963d8fe5
TM
2343static const struct rpc_call_ops nfs4_commit_ops = {
2344 .rpc_call_done = nfs4_commit_done,
2345 .rpc_release = nfs_commit_release,
2346};
2347
1da177e4
LT
2348static void
2349nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2350{
2351 struct rpc_task *task = &data->task;
2352 struct rpc_message msg = {
2353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2354 .rpc_argp = &data->args,
2355 .rpc_resp = &data->res,
2356 .rpc_cred = data->cred,
2357 };
2358 struct inode *inode = data->inode;
4f9838c7 2359 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2360 int flags;
2361
4f9838c7
TM
2362 data->args.bitmask = server->attr_bitmask;
2363 data->res.server = server;
2364
1da177e4
LT
2365 /* Set the initial flags for the task. */
2366 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2367
2368 /* Finalize the task. */
963d8fe5 2369 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
1da177e4
LT
2370 rpc_call_setup(task, &msg, 0);
2371}
2372
2373/*
2374 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2375 * standalone procedure for queueing an asynchronous RENEW.
2376 */
963d8fe5 2377static void nfs4_renew_done(struct rpc_task *task, void *data)
1da177e4
LT
2378{
2379 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
963d8fe5 2380 unsigned long timestamp = (unsigned long)data;
1da177e4
LT
2381
2382 if (task->tk_status < 0) {
2383 switch (task->tk_status) {
2384 case -NFS4ERR_STALE_CLIENTID:
2385 case -NFS4ERR_EXPIRED:
2386 case -NFS4ERR_CB_PATH_DOWN:
2387 nfs4_schedule_state_recovery(clp);
2388 }
2389 return;
2390 }
2391 spin_lock(&clp->cl_lock);
2392 if (time_before(clp->cl_last_renewal,timestamp))
2393 clp->cl_last_renewal = timestamp;
2394 spin_unlock(&clp->cl_lock);
2395}
2396
963d8fe5
TM
2397static const struct rpc_call_ops nfs4_renew_ops = {
2398 .rpc_call_done = nfs4_renew_done,
2399};
2400
1da177e4
LT
2401int
2402nfs4_proc_async_renew(struct nfs4_client *clp)
2403{
2404 struct rpc_message msg = {
2405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2406 .rpc_argp = clp,
2407 .rpc_cred = clp->cl_cred,
2408 };
2409
2410 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
963d8fe5 2411 &nfs4_renew_ops, (void *)jiffies);
1da177e4
LT
2412}
2413
2414int
2415nfs4_proc_renew(struct nfs4_client *clp)
2416{
2417 struct rpc_message msg = {
2418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2419 .rpc_argp = clp,
2420 .rpc_cred = clp->cl_cred,
2421 };
2422 unsigned long now = jiffies;
2423 int status;
2424
2425 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2426 if (status < 0)
2427 return status;
2428 spin_lock(&clp->cl_lock);
2429 if (time_before(clp->cl_last_renewal,now))
2430 clp->cl_last_renewal = now;
2431 spin_unlock(&clp->cl_lock);
2432 return 0;
2433}
2434
aa1870af
BF
2435static inline int nfs4_server_supports_acls(struct nfs_server *server)
2436{
2437 return (server->caps & NFS_CAP_ACLS)
2438 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2439 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2440}
2441
2442/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2443 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2444 * the stack.
2445 */
2446#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2447
2448static void buf_to_pages(const void *buf, size_t buflen,
2449 struct page **pages, unsigned int *pgbase)
2450{
2451 const void *p = buf;
2452
2453 *pgbase = offset_in_page(buf);
2454 p -= *pgbase;
2455 while (p < buf + buflen) {
2456 *(pages++) = virt_to_page(p);
2457 p += PAGE_CACHE_SIZE;
2458 }
2459}
2460
e50a1c2e
BF
2461struct nfs4_cached_acl {
2462 int cached;
2463 size_t len;
3e9d4154 2464 char data[0];
e50a1c2e
BF
2465};
2466
2467static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2468{
2469 struct nfs_inode *nfsi = NFS_I(inode);
2470
2471 spin_lock(&inode->i_lock);
2472 kfree(nfsi->nfs4_acl);
2473 nfsi->nfs4_acl = acl;
2474 spin_unlock(&inode->i_lock);
2475}
2476
2477static void nfs4_zap_acl_attr(struct inode *inode)
2478{
2479 nfs4_set_cached_acl(inode, NULL);
2480}
2481
2482static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2483{
2484 struct nfs_inode *nfsi = NFS_I(inode);
2485 struct nfs4_cached_acl *acl;
2486 int ret = -ENOENT;
2487
2488 spin_lock(&inode->i_lock);
2489 acl = nfsi->nfs4_acl;
2490 if (acl == NULL)
2491 goto out;
2492 if (buf == NULL) /* user is just asking for length */
2493 goto out_len;
2494 if (acl->cached == 0)
2495 goto out;
2496 ret = -ERANGE; /* see getxattr(2) man page */
2497 if (acl->len > buflen)
2498 goto out;
2499 memcpy(buf, acl->data, acl->len);
2500out_len:
2501 ret = acl->len;
2502out:
2503 spin_unlock(&inode->i_lock);
2504 return ret;
2505}
2506
2507static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2508{
2509 struct nfs4_cached_acl *acl;
2510
2511 if (buf && acl_len <= PAGE_SIZE) {
2512 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2513 if (acl == NULL)
2514 goto out;
2515 acl->cached = 1;
2516 memcpy(acl->data, buf, acl_len);
2517 } else {
2518 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2519 if (acl == NULL)
2520 goto out;
2521 acl->cached = 0;
2522 }
2523 acl->len = acl_len;
2524out:
2525 nfs4_set_cached_acl(inode, acl);
2526}
2527
2528static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 2529{
aa1870af
BF
2530 struct page *pages[NFS4ACL_MAXPAGES];
2531 struct nfs_getaclargs args = {
2532 .fh = NFS_FH(inode),
2533 .acl_pages = pages,
2534 .acl_len = buflen,
2535 };
2536 size_t resp_len = buflen;
e50a1c2e 2537 void *resp_buf;
aa1870af
BF
2538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2540 .rpc_argp = &args,
2541 .rpc_resp = &resp_len,
2542 };
e50a1c2e 2543 struct page *localpage = NULL;
aa1870af
BF
2544 int ret;
2545
e50a1c2e
BF
2546 if (buflen < PAGE_SIZE) {
2547 /* As long as we're doing a round trip to the server anyway,
2548 * let's be prepared for a page of acl data. */
2549 localpage = alloc_page(GFP_KERNEL);
2550 resp_buf = page_address(localpage);
2551 if (localpage == NULL)
2552 return -ENOMEM;
2553 args.acl_pages[0] = localpage;
2554 args.acl_pgbase = 0;
1d95db8e 2555 resp_len = args.acl_len = PAGE_SIZE;
e50a1c2e
BF
2556 } else {
2557 resp_buf = buf;
2558 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2559 }
aa1870af 2560 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
e50a1c2e
BF
2561 if (ret)
2562 goto out_free;
2563 if (resp_len > args.acl_len)
2564 nfs4_write_cached_acl(inode, NULL, resp_len);
2565 else
2566 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2567 if (buf) {
2568 ret = -ERANGE;
2569 if (resp_len > buflen)
2570 goto out_free;
2571 if (localpage)
2572 memcpy(buf, resp_buf, resp_len);
2573 }
2574 ret = resp_len;
2575out_free:
2576 if (localpage)
2577 __free_page(localpage);
aa1870af
BF
2578 return ret;
2579}
2580
e50a1c2e
BF
2581static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2582{
2583 struct nfs_server *server = NFS_SERVER(inode);
2584 int ret;
2585
2586 if (!nfs4_server_supports_acls(server))
2587 return -EOPNOTSUPP;
2588 ret = nfs_revalidate_inode(server, inode);
2589 if (ret < 0)
2590 return ret;
2591 ret = nfs4_read_cached_acl(inode, buf, buflen);
2592 if (ret != -ENOENT)
2593 return ret;
2594 return nfs4_get_acl_uncached(inode, buf, buflen);
2595}
2596
4b580ee3
BF
2597static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2598{
2599 struct nfs_server *server = NFS_SERVER(inode);
2600 struct page *pages[NFS4ACL_MAXPAGES];
2601 struct nfs_setaclargs arg = {
2602 .fh = NFS_FH(inode),
2603 .acl_pages = pages,
2604 .acl_len = buflen,
2605 };
2606 struct rpc_message msg = {
2607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2608 .rpc_argp = &arg,
2609 .rpc_resp = NULL,
2610 };
2611 int ret;
2612
2613 if (!nfs4_server_supports_acls(server))
2614 return -EOPNOTSUPP;
642ac549 2615 nfs_inode_return_delegation(inode);
4b580ee3
BF
2616 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2617 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
e50a1c2e
BF
2618 if (ret == 0)
2619 nfs4_write_cached_acl(inode, buf, buflen);
4b580ee3
BF
2620 return ret;
2621}
2622
1da177e4 2623static int
faf5f49c 2624nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
1da177e4
LT
2625{
2626 struct nfs4_client *clp = server->nfs4_state;
2627
2628 if (!clp || task->tk_status >= 0)
2629 return 0;
2630 switch(task->tk_status) {
2631 case -NFS4ERR_STALE_CLIENTID:
2632 case -NFS4ERR_STALE_STATEID:
2633 case -NFS4ERR_EXPIRED:
2634 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2635 nfs4_schedule_state_recovery(clp);
2636 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2637 rpc_wake_up_task(task);
2638 task->tk_status = 0;
2639 return -EAGAIN;
2640 case -NFS4ERR_GRACE:
2641 case -NFS4ERR_DELAY:
2642 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2643 task->tk_status = 0;
2644 return -EAGAIN;
2645 case -NFS4ERR_OLD_STATEID:
2646 task->tk_status = 0;
2647 return -EAGAIN;
2648 }
2649 task->tk_status = nfs4_map_errors(task->tk_status);
2650 return 0;
2651}
2652
2653static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2654{
2655 DEFINE_WAIT(wait);
2656 sigset_t oldset;
2657 int interruptible, res = 0;
2658
2659 might_sleep();
2660
2661 rpc_clnt_sigmask(clnt, &oldset);
2662 interruptible = TASK_UNINTERRUPTIBLE;
2663 if (clnt->cl_intr)
2664 interruptible = TASK_INTERRUPTIBLE;
2665 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2666 nfs4_schedule_state_recovery(clp);
2667 if (clnt->cl_intr && signalled())
2668 res = -ERESTARTSYS;
2669 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2670 schedule();
2671 finish_wait(&clp->cl_waitq, &wait);
2672 rpc_clnt_sigunmask(clnt, &oldset);
2673 return res;
2674}
2675
2676static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2677{
2678 sigset_t oldset;
2679 int res = 0;
2680
2681 might_sleep();
2682
2683 if (*timeout <= 0)
2684 *timeout = NFS4_POLL_RETRY_MIN;
2685 if (*timeout > NFS4_POLL_RETRY_MAX)
2686 *timeout = NFS4_POLL_RETRY_MAX;
2687 rpc_clnt_sigmask(clnt, &oldset);
2688 if (clnt->cl_intr) {
041e0e3b 2689 schedule_timeout_interruptible(*timeout);
1da177e4
LT
2690 if (signalled())
2691 res = -ERESTARTSYS;
041e0e3b
NA
2692 } else
2693 schedule_timeout_uninterruptible(*timeout);
1da177e4
LT
2694 rpc_clnt_sigunmask(clnt, &oldset);
2695 *timeout <<= 1;
2696 return res;
2697}
2698
2699/* This is the error handling routine for processes that are allowed
2700 * to sleep.
2701 */
faf5f49c 2702int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
1da177e4
LT
2703{
2704 struct nfs4_client *clp = server->nfs4_state;
2705 int ret = errorcode;
2706
2707 exception->retry = 0;
2708 switch(errorcode) {
2709 case 0:
2710 return 0;
2711 case -NFS4ERR_STALE_CLIENTID:
2712 case -NFS4ERR_STALE_STATEID:
2713 case -NFS4ERR_EXPIRED:
2714 ret = nfs4_wait_clnt_recover(server->client, clp);
2715 if (ret == 0)
2716 exception->retry = 1;
2717 break;
2718 case -NFS4ERR_GRACE:
2719 case -NFS4ERR_DELAY:
2720 ret = nfs4_delay(server->client, &exception->timeout);
2c56617d
TM
2721 if (ret != 0)
2722 break;
1da177e4 2723 case -NFS4ERR_OLD_STATEID:
2c56617d 2724 exception->retry = 1;
1da177e4
LT
2725 }
2726 /* We failed to handle the error */
2727 return nfs4_map_errors(ret);
2728}
2729
2730int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2731{
2732 nfs4_verifier sc_verifier;
2733 struct nfs4_setclientid setclientid = {
2734 .sc_verifier = &sc_verifier,
2735 .sc_prog = program,
2736 };
2737 struct rpc_message msg = {
2738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2739 .rpc_argp = &setclientid,
2740 .rpc_resp = clp,
2741 .rpc_cred = clp->cl_cred,
2742 };
2743 u32 *p;
2744 int loop = 0;
2745 int status;
2746
2747 p = (u32*)sc_verifier.data;
2748 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2749 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2750
2751 for(;;) {
2752 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2753 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2754 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2755 clp->cl_cred->cr_ops->cr_name,
2756 clp->cl_id_uniquifier);
2757 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2758 sizeof(setclientid.sc_netid), "tcp");
2759 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2760 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2761 clp->cl_ipaddr, port >> 8, port & 255);
2762
2763 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2764 if (status != -NFS4ERR_CLID_INUSE)
2765 break;
2766 if (signalled())
2767 break;
2768 if (loop++ & 1)
2769 ssleep(clp->cl_lease_time + 1);
2770 else
2771 if (++clp->cl_id_uniquifier == 0)
2772 break;
2773 }
2774 return status;
2775}
2776
2777int
2778nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2779{
2780 struct nfs_fsinfo fsinfo;
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2783 .rpc_argp = clp,
2784 .rpc_resp = &fsinfo,
2785 .rpc_cred = clp->cl_cred,
2786 };
2787 unsigned long now;
2788 int status;
2789
2790 now = jiffies;
2791 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2792 if (status == 0) {
2793 spin_lock(&clp->cl_lock);
2794 clp->cl_lease_time = fsinfo.lease_time * HZ;
2795 clp->cl_last_renewal = now;
2796 spin_unlock(&clp->cl_lock);
2797 }
2798 return status;
2799}
2800
2801static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2802{
2803 struct nfs4_delegreturnargs args = {
2804 .fhandle = NFS_FH(inode),
2805 .stateid = stateid,
2806 };
2807 struct rpc_message msg = {
2808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2809 .rpc_argp = &args,
2810 .rpc_cred = cred,
2811 };
2812
2813 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2814}
2815
2816int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2817{
2818 struct nfs_server *server = NFS_SERVER(inode);
2819 struct nfs4_exception exception = { };
2820 int err;
2821 do {
2822 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2823 switch (err) {
2824 case -NFS4ERR_STALE_STATEID:
2825 case -NFS4ERR_EXPIRED:
2826 nfs4_schedule_state_recovery(server->nfs4_state);
2827 case 0:
2828 return 0;
2829 }
2830 err = nfs4_handle_exception(server, err, &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
2835#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2836#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2837
2838/*
2839 * sleep, with exponential backoff, and retry the LOCK operation.
2840 */
2841static unsigned long
2842nfs4_set_lock_task_retry(unsigned long timeout)
2843{
041e0e3b 2844 schedule_timeout_interruptible(timeout);
1da177e4
LT
2845 timeout <<= 1;
2846 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2847 return NFS4_LOCK_MAXTIMEOUT;
2848 return timeout;
2849}
2850
2851static inline int
2852nfs4_lck_type(int cmd, struct file_lock *request)
2853{
2854 /* set lock type */
2855 switch (request->fl_type) {
2856 case F_RDLCK:
2857 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2858 case F_WRLCK:
2859 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2860 case F_UNLCK:
2861 return NFS4_WRITE_LT;
2862 }
2863 BUG();
2864 return 0;
2865}
2866
2867static inline uint64_t
2868nfs4_lck_length(struct file_lock *request)
2869{
2870 if (request->fl_end == OFFSET_MAX)
2871 return ~(uint64_t)0;
2872 return request->fl_end - request->fl_start + 1;
2873}
2874
2875static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2876{
2877 struct inode *inode = state->inode;
2878 struct nfs_server *server = NFS_SERVER(inode);
2879 struct nfs4_client *clp = server->nfs4_state;
2880 struct nfs_lockargs arg = {
2881 .fh = NFS_FH(inode),
2882 .type = nfs4_lck_type(cmd, request),
2883 .offset = request->fl_start,
2884 .length = nfs4_lck_length(request),
2885 };
2886 struct nfs_lockres res = {
2887 .server = server,
2888 };
2889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2891 .rpc_argp = &arg,
2892 .rpc_resp = &res,
2893 .rpc_cred = state->owner->so_cred,
2894 };
2895 struct nfs_lowner nlo;
2896 struct nfs4_lock_state *lsp;
2897 int status;
2898
2899 down_read(&clp->cl_sem);
2900 nlo.clientid = clp->cl_clientid;
8d0a8a9d
TM
2901 status = nfs4_set_lock_state(state, request);
2902 if (status != 0)
2903 goto out;
2904 lsp = request->fl_u.nfs4_fl.owner;
2905 nlo.id = lsp->ls_id;
1da177e4
LT
2906 arg.u.lockt = &nlo;
2907 status = rpc_call_sync(server->client, &msg, 0);
2908 if (!status) {
2909 request->fl_type = F_UNLCK;
2910 } else if (status == -NFS4ERR_DENIED) {
2911 int64_t len, start, end;
2912 start = res.u.denied.offset;
2913 len = res.u.denied.length;
2914 end = start + len - 1;
2915 if (end < 0 || len == 0)
2916 request->fl_end = OFFSET_MAX;
2917 else
2918 request->fl_end = (loff_t)end;
2919 request->fl_start = (loff_t)start;
2920 request->fl_type = F_WRLCK;
2921 if (res.u.denied.type & 1)
2922 request->fl_type = F_RDLCK;
2923 request->fl_pid = 0;
2924 status = 0;
2925 }
8d0a8a9d 2926out:
1da177e4
LT
2927 up_read(&clp->cl_sem);
2928 return status;
2929}
2930
2931static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2932{
2933 struct nfs4_exception exception = { };
2934 int err;
2935
2936 do {
2937 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2938 _nfs4_proc_getlk(state, cmd, request),
2939 &exception);
2940 } while (exception.retry);
2941 return err;
2942}
2943
2944static int do_vfs_lock(struct file *file, struct file_lock *fl)
2945{
2946 int res = 0;
2947 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2948 case FL_POSIX:
2949 res = posix_lock_file_wait(file, fl);
2950 break;
2951 case FL_FLOCK:
2952 res = flock_lock_file_wait(file, fl);
2953 break;
2954 default:
2955 BUG();
2956 }
2957 if (res < 0)
2958 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2959 __FUNCTION__);
2960 return res;
2961}
2962
faf5f49c
TM
2963struct nfs4_unlockdata {
2964 struct nfs_lockargs arg;
2965 struct nfs_locku_opargs luargs;
2966 struct nfs_lockres res;
2967 struct nfs4_lock_state *lsp;
2968 struct nfs_open_context *ctx;
faf5f49c
TM
2969};
2970
06f814a3 2971static void nfs4_locku_release_calldata(void *data)
faf5f49c 2972{
963d8fe5 2973 struct nfs4_unlockdata *calldata = data;
06f814a3
TM
2974 nfs_free_seqid(calldata->luargs.seqid);
2975 nfs4_put_lock_state(calldata->lsp);
2976 put_nfs_open_context(calldata->ctx);
2977 kfree(calldata);
faf5f49c
TM
2978}
2979
963d8fe5 2980static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 2981{
963d8fe5 2982 struct nfs4_unlockdata *calldata = data;
faf5f49c 2983
06f814a3
TM
2984 if (RPC_ASSASSINATED(task))
2985 return;
faf5f49c
TM
2986 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2987 switch (task->tk_status) {
2988 case 0:
2989 memcpy(calldata->lsp->ls_stateid.data,
2990 calldata->res.u.stateid.data,
2991 sizeof(calldata->lsp->ls_stateid.data));
2992 break;
2993 case -NFS4ERR_STALE_STATEID:
2994 case -NFS4ERR_EXPIRED:
2995 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2996 break;
2997 default:
2998 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2999 rpc_restart_call(task);
faf5f49c
TM
3000 }
3001 }
faf5f49c
TM
3002}
3003
4ce70ada 3004static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 3005{
4ce70ada 3006 struct nfs4_unlockdata *calldata = data;
1da177e4
LT
3007 struct rpc_message msg = {
3008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
faf5f49c
TM
3009 .rpc_argp = &calldata->arg,
3010 .rpc_resp = &calldata->res,
3011 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
1da177e4 3012 };
faf5f49c
TM
3013 int status;
3014
3015 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
3016 if (status != 0)
3017 return;
3018 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
963d8fe5
TM
3019 /* Note: exit _without_ running nfs4_locku_done */
3020 task->tk_action = NULL;
faf5f49c
TM
3021 return;
3022 }
3023 rpc_call_setup(task, &msg, 0);
3024}
3025
963d8fe5 3026static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 3027 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 3028 .rpc_call_done = nfs4_locku_done,
06f814a3 3029 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
3030};
3031
faf5f49c
TM
3032static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3033{
3034 struct nfs4_unlockdata *calldata;
3035 struct inode *inode = state->inode;
3036 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 3037 struct nfs4_lock_state *lsp;
06f814a3
TM
3038 struct rpc_task *task;
3039 int status = 0;
faf5f49c 3040
6bfc93ef
TM
3041 /* Is this a delegated lock? */
3042 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
06f814a3 3043 goto out;
6bfc93ef 3044
8d0a8a9d
TM
3045 status = nfs4_set_lock_state(state, request);
3046 if (status != 0)
06f814a3 3047 goto out;
8d0a8a9d 3048 lsp = request->fl_u.nfs4_fl.owner;
1da177e4 3049 /* We might have lost the locks! */
8d0a8a9d 3050 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
06f814a3
TM
3051 goto out;
3052 status = -ENOMEM;
faf5f49c
TM
3053 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
3054 if (calldata == NULL)
06f814a3 3055 goto out;
faf5f49c
TM
3056 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3057 if (calldata->luargs.seqid == NULL) {
3058 kfree(calldata);
06f814a3 3059 goto out;
faf5f49c
TM
3060 }
3061 calldata->luargs.stateid = &lsp->ls_stateid;
3062 calldata->arg.fh = NFS_FH(inode);
3063 calldata->arg.type = nfs4_lck_type(cmd, request);
3064 calldata->arg.offset = request->fl_start;
3065 calldata->arg.length = nfs4_lck_length(request);
3066 calldata->arg.u.locku = &calldata->luargs;
3067 calldata->res.server = server;
3068 calldata->lsp = lsp;
3069 atomic_inc(&lsp->ls_count);
3070
3071 /* Ensure we don't close file until we're done freeing locks! */
3072 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3073
06f814a3
TM
3074 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3075 if (!IS_ERR(task)) {
3076 status = nfs4_wait_for_completion_rpc_task(task);
3077 rpc_release_task(task);
3078 } else {
3079 status = PTR_ERR(task);
3080 nfs4_locku_release_calldata(calldata);
3081 }
3082out:
faf5f49c 3083 do_vfs_lock(request->fl_file, request);
1da177e4
LT
3084 return status;
3085}
3086
1da177e4
LT
3087static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3088{
3089 struct inode *inode = state->inode;
3090 struct nfs_server *server = NFS_SERVER(inode);
8d0a8a9d 3091 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
06735b34
TM
3092 struct nfs_lock_opargs largs = {
3093 .lock_stateid = &lsp->ls_stateid,
3094 .open_stateid = &state->stateid,
3095 .lock_owner = {
3096 .clientid = server->nfs4_state->cl_clientid,
3097 .id = lsp->ls_id,
3098 },
3099 .reclaim = reclaim,
3100 };
1da177e4
LT
3101 struct nfs_lockargs arg = {
3102 .fh = NFS_FH(inode),
3103 .type = nfs4_lck_type(cmd, request),
3104 .offset = request->fl_start,
3105 .length = nfs4_lck_length(request),
06735b34
TM
3106 .u = {
3107 .lock = &largs,
3108 },
1da177e4
LT
3109 };
3110 struct nfs_lockres res = {
3111 .server = server,
3112 };
3113 struct rpc_message msg = {
3114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3115 .rpc_argp = &arg,
3116 .rpc_resp = &res,
3117 .rpc_cred = state->owner->so_cred,
3118 };
cee54fc9 3119 int status = -ENOMEM;
1da177e4 3120
06735b34
TM
3121 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3122 if (largs.lock_seqid == NULL)
cee54fc9
TM
3123 return -ENOMEM;
3124 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
1da177e4 3125 struct nfs4_state_owner *owner = state->owner;
06735b34
TM
3126
3127 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3128 if (largs.open_seqid == NULL)
3129 goto out;
1da177e4 3130 largs.new_lock_owner = 1;
06735b34
TM
3131 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3132 /* increment open seqid on success, and seqid mutating errors */
3133 if (largs.new_lock_owner != 0) {
3134 nfs_increment_open_seqid(status, largs.open_seqid);
3135 if (status == 0)
3136 nfs_confirm_seqid(&lsp->ls_seqid, 0);
8d0a8a9d 3137 }
06735b34
TM
3138 nfs_free_seqid(largs.open_seqid);
3139 } else
1da177e4 3140 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
06735b34
TM
3141 /* increment lock seqid on success, and seqid mutating errors*/
3142 nfs_increment_lock_seqid(status, largs.lock_seqid);
1da177e4 3143 /* save the returned stateid. */
cee54fc9 3144 if (status == 0) {
06735b34
TM
3145 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3146 sizeof(lsp->ls_stateid.data));
cee54fc9
TM
3147 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3148 } else if (status == -NFS4ERR_DENIED)
1da177e4 3149 status = -EAGAIN;
06735b34
TM
3150out:
3151 nfs_free_seqid(largs.lock_seqid);
1da177e4
LT
3152 return status;
3153}
3154
3155static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3156{
202b50dc
TM
3157 struct nfs_server *server = NFS_SERVER(state->inode);
3158 struct nfs4_exception exception = { };
3159 int err;
3160
6bfc93ef
TM
3161 /* Cache the lock if possible... */
3162 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3163 return 0;
202b50dc
TM
3164 do {
3165 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3166 if (err != -NFS4ERR_DELAY)
3167 break;
3168 nfs4_handle_exception(server, err, &exception);
3169 } while (exception.retry);
3170 return err;
1da177e4
LT
3171}
3172
3173static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3174{
202b50dc
TM
3175 struct nfs_server *server = NFS_SERVER(state->inode);
3176 struct nfs4_exception exception = { };
3177 int err;
3178
6bfc93ef
TM
3179 err = nfs4_set_lock_state(state, request);
3180 if (err != 0)
3181 return err;
202b50dc
TM
3182 do {
3183 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3184 if (err != -NFS4ERR_DELAY)
3185 break;
3186 nfs4_handle_exception(server, err, &exception);
3187 } while (exception.retry);
3188 return err;
1da177e4
LT
3189}
3190
3191static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3192{
3193 struct nfs4_client *clp = state->owner->so_client;
3194 int status;
3195
6bfc93ef 3196 /* Is this a delegated open? */
ff604066 3197 if (NFS_I(state->inode)->delegation_state != 0) {
6bfc93ef
TM
3198 /* Yes: cache locks! */
3199 status = do_vfs_lock(request->fl_file, request);
3200 /* ...but avoid races with delegation recall... */
3201 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
ff604066 3202 return status;
1da177e4 3203 }
ff604066 3204 down_read(&clp->cl_sem);
6bfc93ef
TM
3205 status = nfs4_set_lock_state(state, request);
3206 if (status != 0)
3207 goto out;
3208 status = _nfs4_do_setlk(state, cmd, request, 0);
3209 if (status != 0)
3210 goto out;
3211 /* Note: we always want to sleep here! */
3212 request->fl_flags |= FL_SLEEP;
3213 if (do_vfs_lock(request->fl_file, request) < 0)
3214 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3215out:
1da177e4
LT
3216 up_read(&clp->cl_sem);
3217 return status;
3218}
3219
3220static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3221{
3222 struct nfs4_exception exception = { };
3223 int err;
3224
3225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3227 _nfs4_proc_setlk(state, cmd, request),
3228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
3233static int
3234nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3235{
3236 struct nfs_open_context *ctx;
3237 struct nfs4_state *state;
3238 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3239 int status;
3240
3241 /* verify open state */
3242 ctx = (struct nfs_open_context *)filp->private_data;
3243 state = ctx->state;
3244
3245 if (request->fl_start < 0 || request->fl_end < 0)
3246 return -EINVAL;
3247
3248 if (IS_GETLK(cmd))
3249 return nfs4_proc_getlk(state, F_GETLK, request);
3250
3251 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3252 return -EINVAL;
3253
3254 if (request->fl_type == F_UNLCK)
3255 return nfs4_proc_unlck(state, cmd, request);
3256
3257 do {
3258 status = nfs4_proc_setlk(state, cmd, request);
3259 if ((status != -EAGAIN) || IS_SETLK(cmd))
3260 break;
3261 timeout = nfs4_set_lock_task_retry(timeout);
3262 status = -ERESTARTSYS;
3263 if (signalled())
3264 break;
3265 } while(status < 0);
1da177e4
LT
3266 return status;
3267}
3268
888e694c
TM
3269int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3270{
3271 struct nfs_server *server = NFS_SERVER(state->inode);
3272 struct nfs4_exception exception = { };
3273 int err;
3274
3275 err = nfs4_set_lock_state(state, fl);
3276 if (err != 0)
3277 goto out;
3278 do {
3279 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3280 if (err != -NFS4ERR_DELAY)
3281 break;
3282 err = nfs4_handle_exception(server, err, &exception);
3283 } while (exception.retry);
3284out:
3285 return err;
3286}
6b3b5496 3287
aa1870af
BF
3288#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3289
6b3b5496
BF
3290int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3291 size_t buflen, int flags)
3292{
4b580ee3
BF
3293 struct inode *inode = dentry->d_inode;
3294
3295 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3296 return -EOPNOTSUPP;
3297
3298 if (!S_ISREG(inode->i_mode) &&
3299 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3300 return -EPERM;
3301
3302 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
3303}
3304
3305/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3306 * and that's what we'll do for e.g. user attributes that haven't been set.
3307 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3308 * attributes in kernel-managed attribute namespaces. */
3309ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3310 size_t buflen)
3311{
aa1870af
BF
3312 struct inode *inode = dentry->d_inode;
3313
3314 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3315 return -EOPNOTSUPP;
3316
3317 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
3318}
3319
3320ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3321{
aa1870af 3322 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
6b3b5496
BF
3323
3324 if (buf && buflen < len)
3325 return -ERANGE;
3326 if (buf)
aa1870af
BF
3327 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3328 return len;
6b3b5496
BF
3329}
3330
1da177e4
LT
3331struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3332 .recover_open = nfs4_open_reclaim,
3333 .recover_lock = nfs4_lock_reclaim,
3334};
3335
3336struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3337 .recover_open = nfs4_open_expired,
3338 .recover_lock = nfs4_lock_expired,
3339};
3340
6b3b5496
BF
3341static struct inode_operations nfs4_file_inode_operations = {
3342 .permission = nfs_permission,
3343 .getattr = nfs_getattr,
3344 .setattr = nfs_setattr,
3345 .getxattr = nfs4_getxattr,
3346 .setxattr = nfs4_setxattr,
3347 .listxattr = nfs4_listxattr,
3348};
3349
1da177e4
LT
3350struct nfs_rpc_ops nfs_v4_clientops = {
3351 .version = 4, /* protocol version */
3352 .dentry_ops = &nfs4_dentry_operations,
3353 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 3354 .file_inode_ops = &nfs4_file_inode_operations,
1da177e4
LT
3355 .getroot = nfs4_proc_get_root,
3356 .getattr = nfs4_proc_getattr,
3357 .setattr = nfs4_proc_setattr,
3358 .lookup = nfs4_proc_lookup,
3359 .access = nfs4_proc_access,
3360 .readlink = nfs4_proc_readlink,
3361 .read = nfs4_proc_read,
3362 .write = nfs4_proc_write,
3363 .commit = nfs4_proc_commit,
3364 .create = nfs4_proc_create,
3365 .remove = nfs4_proc_remove,
3366 .unlink_setup = nfs4_proc_unlink_setup,
3367 .unlink_done = nfs4_proc_unlink_done,
3368 .rename = nfs4_proc_rename,
3369 .link = nfs4_proc_link,
3370 .symlink = nfs4_proc_symlink,
3371 .mkdir = nfs4_proc_mkdir,
3372 .rmdir = nfs4_proc_remove,
3373 .readdir = nfs4_proc_readdir,
3374 .mknod = nfs4_proc_mknod,
3375 .statfs = nfs4_proc_statfs,
3376 .fsinfo = nfs4_proc_fsinfo,
3377 .pathconf = nfs4_proc_pathconf,
3378 .decode_dirent = nfs4_decode_dirent,
3379 .read_setup = nfs4_proc_read_setup,
3380 .write_setup = nfs4_proc_write_setup,
3381 .commit_setup = nfs4_proc_commit_setup,
02a913a7
TM
3382 .file_open = nfs_open,
3383 .file_release = nfs_release,
1da177e4 3384 .lock = nfs4_proc_lock,
e50a1c2e 3385 .clear_acl_cache = nfs4_zap_acl_attr,
1da177e4
LT
3386};
3387
3388/*
3389 * Local variables:
3390 * c-basic-offset: 8
3391 * End:
3392 */