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