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