thinkpad-acpi: R52 brightness_mode has been confirmed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blobad4e800c646e8a20f9950e787ddc4b4e7930cf01
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
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.
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>
50 #include <linux/mount.h>
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54 #include "internal.h"
55 #include "iostat.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
62 struct nfs4_opendata;
63 static int _nfs4_proc_open(struct nfs4_opendata *data);
64 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
65 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
66 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
67 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
68 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
71 /* Prevent leaks of NFSv4 errors into userland */
72 int nfs4_map_errors(int err)
74 if (err >= -1000)
75 return err;
76 switch (err) {
77 case -NFS4ERR_RESOURCE:
78 return -EREMOTEIO;
79 default:
80 dprintk("%s could not handle NFSv4 error %d\n",
81 __func__, -err);
82 break;
84 return -EIO;
88 * This is our standard bitmap for GETATTR requests.
90 const u32 nfs4_fattr_bitmap[2] = {
91 FATTR4_WORD0_TYPE
92 | FATTR4_WORD0_CHANGE
93 | FATTR4_WORD0_SIZE
94 | FATTR4_WORD0_FSID
95 | FATTR4_WORD0_FILEID,
96 FATTR4_WORD1_MODE
97 | FATTR4_WORD1_NUMLINKS
98 | FATTR4_WORD1_OWNER
99 | FATTR4_WORD1_OWNER_GROUP
100 | FATTR4_WORD1_RAWDEV
101 | FATTR4_WORD1_SPACE_USED
102 | FATTR4_WORD1_TIME_ACCESS
103 | FATTR4_WORD1_TIME_METADATA
104 | FATTR4_WORD1_TIME_MODIFY
107 const u32 nfs4_statfs_bitmap[2] = {
108 FATTR4_WORD0_FILES_AVAIL
109 | FATTR4_WORD0_FILES_FREE
110 | FATTR4_WORD0_FILES_TOTAL,
111 FATTR4_WORD1_SPACE_AVAIL
112 | FATTR4_WORD1_SPACE_FREE
113 | FATTR4_WORD1_SPACE_TOTAL
116 const u32 nfs4_pathconf_bitmap[2] = {
117 FATTR4_WORD0_MAXLINK
118 | FATTR4_WORD0_MAXNAME,
122 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
123 | FATTR4_WORD0_MAXREAD
124 | FATTR4_WORD0_MAXWRITE
125 | FATTR4_WORD0_LEASE_TIME,
129 const u32 nfs4_fs_locations_bitmap[2] = {
130 FATTR4_WORD0_TYPE
131 | FATTR4_WORD0_CHANGE
132 | FATTR4_WORD0_SIZE
133 | FATTR4_WORD0_FSID
134 | FATTR4_WORD0_FILEID
135 | FATTR4_WORD0_FS_LOCATIONS,
136 FATTR4_WORD1_MODE
137 | FATTR4_WORD1_NUMLINKS
138 | FATTR4_WORD1_OWNER
139 | FATTR4_WORD1_OWNER_GROUP
140 | FATTR4_WORD1_RAWDEV
141 | FATTR4_WORD1_SPACE_USED
142 | FATTR4_WORD1_TIME_ACCESS
143 | FATTR4_WORD1_TIME_METADATA
144 | FATTR4_WORD1_TIME_MODIFY
145 | FATTR4_WORD1_MOUNTED_ON_FILEID
148 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
149 struct nfs4_readdir_arg *readdir)
151 __be32 *start, *p;
153 BUG_ON(readdir->count < 80);
154 if (cookie > 2) {
155 readdir->cookie = cookie;
156 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
157 return;
160 readdir->cookie = 0;
161 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
162 if (cookie == 2)
163 return;
166 * NFSv4 servers do not return entries for '.' and '..'
167 * Therefore, we fake these entries here. We let '.'
168 * have cookie 0 and '..' have cookie 1. Note that
169 * when talking to the server, we always send cookie 0
170 * instead of 1 or 2.
172 start = p = kmap_atomic(*readdir->pages, KM_USER0);
174 if (cookie == 0) {
175 *p++ = xdr_one; /* next */
176 *p++ = xdr_zero; /* cookie, first word */
177 *p++ = xdr_one; /* cookie, second word */
178 *p++ = xdr_one; /* entry len */
179 memcpy(p, ".\0\0\0", 4); /* entry */
180 p++;
181 *p++ = xdr_one; /* bitmap length */
182 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
183 *p++ = htonl(8); /* attribute buffer length */
184 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
187 *p++ = xdr_one; /* next */
188 *p++ = xdr_zero; /* cookie, first word */
189 *p++ = xdr_two; /* cookie, second word */
190 *p++ = xdr_two; /* entry len */
191 memcpy(p, "..\0\0", 4); /* entry */
192 p++;
193 *p++ = xdr_one; /* bitmap length */
194 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
195 *p++ = htonl(8); /* attribute buffer length */
196 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
198 readdir->pgbase = (char *)p - (char *)start;
199 readdir->count -= readdir->pgbase;
200 kunmap_atomic(start, KM_USER0);
203 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
205 struct nfs_client *clp = server->nfs_client;
206 spin_lock(&clp->cl_lock);
207 if (time_before(clp->cl_last_renewal,timestamp))
208 clp->cl_last_renewal = timestamp;
209 spin_unlock(&clp->cl_lock);
212 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
214 struct nfs_inode *nfsi = NFS_I(dir);
216 spin_lock(&dir->i_lock);
217 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
218 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
219 nfs_force_lookup_revalidate(dir);
220 nfsi->change_attr = cinfo->after;
221 spin_unlock(&dir->i_lock);
224 struct nfs4_opendata {
225 struct kref kref;
226 struct nfs_openargs o_arg;
227 struct nfs_openres o_res;
228 struct nfs_open_confirmargs c_arg;
229 struct nfs_open_confirmres c_res;
230 struct nfs_fattr f_attr;
231 struct nfs_fattr dir_attr;
232 struct path path;
233 struct dentry *dir;
234 struct nfs4_state_owner *owner;
235 struct nfs4_state *state;
236 struct iattr attrs;
237 unsigned long timestamp;
238 unsigned int rpc_done : 1;
239 int rpc_status;
240 int cancelled;
244 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
246 p->o_res.f_attr = &p->f_attr;
247 p->o_res.dir_attr = &p->dir_attr;
248 p->o_res.seqid = p->o_arg.seqid;
249 p->c_res.seqid = p->c_arg.seqid;
250 p->o_res.server = p->o_arg.server;
251 nfs_fattr_init(&p->f_attr);
252 nfs_fattr_init(&p->dir_attr);
255 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
256 struct nfs4_state_owner *sp, int flags,
257 const struct iattr *attrs)
259 struct dentry *parent = dget_parent(path->dentry);
260 struct inode *dir = parent->d_inode;
261 struct nfs_server *server = NFS_SERVER(dir);
262 struct nfs4_opendata *p;
264 p = kzalloc(sizeof(*p), GFP_KERNEL);
265 if (p == NULL)
266 goto err;
267 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
268 if (p->o_arg.seqid == NULL)
269 goto err_free;
270 p->path.mnt = mntget(path->mnt);
271 p->path.dentry = dget(path->dentry);
272 p->dir = parent;
273 p->owner = sp;
274 atomic_inc(&sp->so_count);
275 p->o_arg.fh = NFS_FH(dir);
276 p->o_arg.open_flags = flags,
277 p->o_arg.clientid = server->nfs_client->cl_clientid;
278 p->o_arg.id = sp->so_owner_id.id;
279 p->o_arg.name = &p->path.dentry->d_name;
280 p->o_arg.server = server;
281 p->o_arg.bitmask = server->attr_bitmask;
282 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
283 if (flags & O_EXCL) {
284 u32 *s = (u32 *) p->o_arg.u.verifier.data;
285 s[0] = jiffies;
286 s[1] = current->pid;
287 } else if (flags & O_CREAT) {
288 p->o_arg.u.attrs = &p->attrs;
289 memcpy(&p->attrs, attrs, sizeof(p->attrs));
291 p->c_arg.fh = &p->o_res.fh;
292 p->c_arg.stateid = &p->o_res.stateid;
293 p->c_arg.seqid = p->o_arg.seqid;
294 nfs4_init_opendata_res(p);
295 kref_init(&p->kref);
296 return p;
297 err_free:
298 kfree(p);
299 err:
300 dput(parent);
301 return NULL;
304 static void nfs4_opendata_free(struct kref *kref)
306 struct nfs4_opendata *p = container_of(kref,
307 struct nfs4_opendata, kref);
309 nfs_free_seqid(p->o_arg.seqid);
310 if (p->state != NULL)
311 nfs4_put_open_state(p->state);
312 nfs4_put_state_owner(p->owner);
313 dput(p->dir);
314 path_put(&p->path);
315 kfree(p);
318 static void nfs4_opendata_put(struct nfs4_opendata *p)
320 if (p != NULL)
321 kref_put(&p->kref, nfs4_opendata_free);
324 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
326 int ret;
328 ret = rpc_wait_for_completion_task(task);
329 return ret;
332 static int can_open_cached(struct nfs4_state *state, int mode)
334 int ret = 0;
335 switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
336 case FMODE_READ:
337 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
338 break;
339 case FMODE_WRITE:
340 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
341 break;
342 case FMODE_READ|FMODE_WRITE:
343 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
345 return ret;
348 static int can_open_delegated(struct nfs_delegation *delegation, mode_t open_flags)
350 if ((delegation->type & open_flags) != open_flags)
351 return 0;
352 if (delegation->flags & NFS_DELEGATION_NEED_RECLAIM)
353 return 0;
354 return 1;
357 static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
359 switch (open_flags) {
360 case FMODE_WRITE:
361 state->n_wronly++;
362 break;
363 case FMODE_READ:
364 state->n_rdonly++;
365 break;
366 case FMODE_READ|FMODE_WRITE:
367 state->n_rdwr++;
369 nfs4_state_set_mode_locked(state, state->state | open_flags);
372 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
374 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
375 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
376 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
377 switch (open_flags) {
378 case FMODE_READ:
379 set_bit(NFS_O_RDONLY_STATE, &state->flags);
380 break;
381 case FMODE_WRITE:
382 set_bit(NFS_O_WRONLY_STATE, &state->flags);
383 break;
384 case FMODE_READ|FMODE_WRITE:
385 set_bit(NFS_O_RDWR_STATE, &state->flags);
389 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
391 write_seqlock(&state->seqlock);
392 nfs_set_open_stateid_locked(state, stateid, open_flags);
393 write_sequnlock(&state->seqlock);
396 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
398 open_flags &= (FMODE_READ|FMODE_WRITE);
400 * Protect the call to nfs4_state_set_mode_locked and
401 * serialise the stateid update
403 write_seqlock(&state->seqlock);
404 if (deleg_stateid != NULL) {
405 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
406 set_bit(NFS_DELEGATED_STATE, &state->flags);
408 if (open_stateid != NULL)
409 nfs_set_open_stateid_locked(state, open_stateid, open_flags);
410 write_sequnlock(&state->seqlock);
411 spin_lock(&state->owner->so_lock);
412 update_open_stateflags(state, open_flags);
413 spin_unlock(&state->owner->so_lock);
416 static void nfs4_return_incompatible_delegation(struct inode *inode, mode_t open_flags)
418 struct nfs_delegation *delegation;
420 rcu_read_lock();
421 delegation = rcu_dereference(NFS_I(inode)->delegation);
422 if (delegation == NULL || (delegation->type & open_flags) == open_flags) {
423 rcu_read_unlock();
424 return;
426 rcu_read_unlock();
427 nfs_inode_return_delegation(inode);
430 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
432 struct nfs4_state *state = opendata->state;
433 struct nfs_inode *nfsi = NFS_I(state->inode);
434 struct nfs_delegation *delegation;
435 int open_mode = opendata->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL);
436 nfs4_stateid stateid;
437 int ret = -EAGAIN;
439 rcu_read_lock();
440 delegation = rcu_dereference(nfsi->delegation);
441 for (;;) {
442 if (can_open_cached(state, open_mode)) {
443 spin_lock(&state->owner->so_lock);
444 if (can_open_cached(state, open_mode)) {
445 update_open_stateflags(state, open_mode);
446 spin_unlock(&state->owner->so_lock);
447 rcu_read_unlock();
448 goto out_return_state;
450 spin_unlock(&state->owner->so_lock);
452 if (delegation == NULL)
453 break;
454 if (!can_open_delegated(delegation, open_mode))
455 break;
456 /* Save the delegation */
457 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
458 rcu_read_unlock();
459 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
460 if (ret != 0)
461 goto out;
462 ret = -EAGAIN;
463 rcu_read_lock();
464 delegation = rcu_dereference(nfsi->delegation);
465 /* If no delegation, try a cached open */
466 if (delegation == NULL)
467 continue;
468 /* Is the delegation still valid? */
469 if (memcmp(stateid.data, delegation->stateid.data, sizeof(stateid.data)) != 0)
470 continue;
471 rcu_read_unlock();
472 update_open_stateid(state, NULL, &stateid, open_mode);
473 goto out_return_state;
475 rcu_read_unlock();
476 out:
477 return ERR_PTR(ret);
478 out_return_state:
479 atomic_inc(&state->count);
480 return state;
483 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
485 struct inode *inode;
486 struct nfs4_state *state = NULL;
487 struct nfs_delegation *delegation;
488 nfs4_stateid *deleg_stateid = NULL;
489 int ret;
491 if (!data->rpc_done) {
492 state = nfs4_try_open_cached(data);
493 goto out;
496 ret = -EAGAIN;
497 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
498 goto err;
499 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
500 ret = PTR_ERR(inode);
501 if (IS_ERR(inode))
502 goto err;
503 ret = -ENOMEM;
504 state = nfs4_get_open_state(inode, data->owner);
505 if (state == NULL)
506 goto err_put_inode;
507 if (data->o_res.delegation_type != 0) {
508 int delegation_flags = 0;
510 rcu_read_lock();
511 delegation = rcu_dereference(NFS_I(inode)->delegation);
512 if (delegation)
513 delegation_flags = delegation->flags;
514 rcu_read_unlock();
515 if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM))
516 nfs_inode_set_delegation(state->inode,
517 data->owner->so_cred,
518 &data->o_res);
519 else
520 nfs_inode_reclaim_delegation(state->inode,
521 data->owner->so_cred,
522 &data->o_res);
524 rcu_read_lock();
525 delegation = rcu_dereference(NFS_I(inode)->delegation);
526 if (delegation != NULL)
527 deleg_stateid = &delegation->stateid;
528 update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags);
529 rcu_read_unlock();
530 iput(inode);
531 out:
532 return state;
533 err_put_inode:
534 iput(inode);
535 err:
536 return ERR_PTR(ret);
539 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
541 struct nfs_inode *nfsi = NFS_I(state->inode);
542 struct nfs_open_context *ctx;
544 spin_lock(&state->inode->i_lock);
545 list_for_each_entry(ctx, &nfsi->open_files, list) {
546 if (ctx->state != state)
547 continue;
548 get_nfs_open_context(ctx);
549 spin_unlock(&state->inode->i_lock);
550 return ctx;
552 spin_unlock(&state->inode->i_lock);
553 return ERR_PTR(-ENOENT);
556 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
558 struct nfs4_opendata *opendata;
560 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
561 if (opendata == NULL)
562 return ERR_PTR(-ENOMEM);
563 opendata->state = state;
564 atomic_inc(&state->count);
565 return opendata;
568 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
570 struct nfs4_state *newstate;
571 int ret;
573 opendata->o_arg.open_flags = openflags;
574 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
575 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
576 nfs4_init_opendata_res(opendata);
577 ret = _nfs4_proc_open(opendata);
578 if (ret != 0)
579 return ret;
580 newstate = nfs4_opendata_to_nfs4_state(opendata);
581 if (IS_ERR(newstate))
582 return PTR_ERR(newstate);
583 nfs4_close_state(&opendata->path, newstate, openflags);
584 *res = newstate;
585 return 0;
588 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
590 struct nfs4_state *newstate;
591 int ret;
593 /* memory barrier prior to reading state->n_* */
594 clear_bit(NFS_DELEGATED_STATE, &state->flags);
595 smp_rmb();
596 if (state->n_rdwr != 0) {
597 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
598 if (ret != 0)
599 return ret;
600 if (newstate != state)
601 return -ESTALE;
603 if (state->n_wronly != 0) {
604 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
605 if (ret != 0)
606 return ret;
607 if (newstate != state)
608 return -ESTALE;
610 if (state->n_rdonly != 0) {
611 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
612 if (ret != 0)
613 return ret;
614 if (newstate != state)
615 return -ESTALE;
618 * We may have performed cached opens for all three recoveries.
619 * Check if we need to update the current stateid.
621 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
622 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
623 write_seqlock(&state->seqlock);
624 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
625 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
626 write_sequnlock(&state->seqlock);
628 return 0;
632 * OPEN_RECLAIM:
633 * reclaim state on the server after a reboot.
635 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
637 struct nfs_delegation *delegation;
638 struct nfs4_opendata *opendata;
639 int delegation_type = 0;
640 int status;
642 opendata = nfs4_open_recoverdata_alloc(ctx, state);
643 if (IS_ERR(opendata))
644 return PTR_ERR(opendata);
645 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
646 opendata->o_arg.fh = NFS_FH(state->inode);
647 rcu_read_lock();
648 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
649 if (delegation != NULL && (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) != 0)
650 delegation_type = delegation->type;
651 rcu_read_unlock();
652 opendata->o_arg.u.delegation_type = delegation_type;
653 status = nfs4_open_recover(opendata, state);
654 nfs4_opendata_put(opendata);
655 return status;
658 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
660 struct nfs_server *server = NFS_SERVER(state->inode);
661 struct nfs4_exception exception = { };
662 int err;
663 do {
664 err = _nfs4_do_open_reclaim(ctx, state);
665 if (err != -NFS4ERR_DELAY)
666 break;
667 nfs4_handle_exception(server, err, &exception);
668 } while (exception.retry);
669 return err;
672 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
674 struct nfs_open_context *ctx;
675 int ret;
677 ctx = nfs4_state_find_open_context(state);
678 if (IS_ERR(ctx))
679 return PTR_ERR(ctx);
680 ret = nfs4_do_open_reclaim(ctx, state);
681 put_nfs_open_context(ctx);
682 return ret;
685 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
687 struct nfs4_opendata *opendata;
688 int ret;
690 opendata = nfs4_open_recoverdata_alloc(ctx, state);
691 if (IS_ERR(opendata))
692 return PTR_ERR(opendata);
693 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
694 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
695 sizeof(opendata->o_arg.u.delegation.data));
696 ret = nfs4_open_recover(opendata, state);
697 nfs4_opendata_put(opendata);
698 return ret;
701 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
703 struct nfs4_exception exception = { };
704 struct nfs_server *server = NFS_SERVER(state->inode);
705 int err;
706 do {
707 err = _nfs4_open_delegation_recall(ctx, state, stateid);
708 switch (err) {
709 case 0:
710 return err;
711 case -NFS4ERR_STALE_CLIENTID:
712 case -NFS4ERR_STALE_STATEID:
713 case -NFS4ERR_EXPIRED:
714 /* Don't recall a delegation if it was lost */
715 nfs4_schedule_state_recovery(server->nfs_client);
716 return err;
718 err = nfs4_handle_exception(server, err, &exception);
719 } while (exception.retry);
720 return err;
723 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
725 struct nfs4_opendata *data = calldata;
727 data->rpc_status = task->tk_status;
728 if (RPC_ASSASSINATED(task))
729 return;
730 if (data->rpc_status == 0) {
731 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
732 sizeof(data->o_res.stateid.data));
733 nfs_confirm_seqid(&data->owner->so_seqid, 0);
734 renew_lease(data->o_res.server, data->timestamp);
735 data->rpc_done = 1;
739 static void nfs4_open_confirm_release(void *calldata)
741 struct nfs4_opendata *data = calldata;
742 struct nfs4_state *state = NULL;
744 /* If this request hasn't been cancelled, do nothing */
745 if (data->cancelled == 0)
746 goto out_free;
747 /* In case of error, no cleanup! */
748 if (!data->rpc_done)
749 goto out_free;
750 state = nfs4_opendata_to_nfs4_state(data);
751 if (!IS_ERR(state))
752 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
753 out_free:
754 nfs4_opendata_put(data);
757 static const struct rpc_call_ops nfs4_open_confirm_ops = {
758 .rpc_call_done = nfs4_open_confirm_done,
759 .rpc_release = nfs4_open_confirm_release,
763 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
765 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
767 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
768 struct rpc_task *task;
769 struct rpc_message msg = {
770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
771 .rpc_argp = &data->c_arg,
772 .rpc_resp = &data->c_res,
773 .rpc_cred = data->owner->so_cred,
775 struct rpc_task_setup task_setup_data = {
776 .rpc_client = server->client,
777 .rpc_message = &msg,
778 .callback_ops = &nfs4_open_confirm_ops,
779 .callback_data = data,
780 .workqueue = nfsiod_workqueue,
781 .flags = RPC_TASK_ASYNC,
783 int status;
785 kref_get(&data->kref);
786 data->rpc_done = 0;
787 data->rpc_status = 0;
788 data->timestamp = jiffies;
789 task = rpc_run_task(&task_setup_data);
790 if (IS_ERR(task))
791 return PTR_ERR(task);
792 status = nfs4_wait_for_completion_rpc_task(task);
793 if (status != 0) {
794 data->cancelled = 1;
795 smp_wmb();
796 } else
797 status = data->rpc_status;
798 rpc_put_task(task);
799 return status;
802 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
804 struct nfs4_opendata *data = calldata;
805 struct nfs4_state_owner *sp = data->owner;
807 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
808 return;
810 * Check if we still need to send an OPEN call, or if we can use
811 * a delegation instead.
813 if (data->state != NULL) {
814 struct nfs_delegation *delegation;
816 if (can_open_cached(data->state, data->o_arg.open_flags & (FMODE_READ|FMODE_WRITE|O_EXCL)))
817 goto out_no_action;
818 rcu_read_lock();
819 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
820 if (delegation != NULL &&
821 (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) {
822 rcu_read_unlock();
823 goto out_no_action;
825 rcu_read_unlock();
827 /* Update sequence id. */
828 data->o_arg.id = sp->so_owner_id.id;
829 data->o_arg.clientid = sp->so_client->cl_clientid;
830 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
831 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
832 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
834 data->timestamp = jiffies;
835 rpc_call_start(task);
836 return;
837 out_no_action:
838 task->tk_action = NULL;
842 static void nfs4_open_done(struct rpc_task *task, void *calldata)
844 struct nfs4_opendata *data = calldata;
846 data->rpc_status = task->tk_status;
847 if (RPC_ASSASSINATED(task))
848 return;
849 if (task->tk_status == 0) {
850 switch (data->o_res.f_attr->mode & S_IFMT) {
851 case S_IFREG:
852 break;
853 case S_IFLNK:
854 data->rpc_status = -ELOOP;
855 break;
856 case S_IFDIR:
857 data->rpc_status = -EISDIR;
858 break;
859 default:
860 data->rpc_status = -ENOTDIR;
862 renew_lease(data->o_res.server, data->timestamp);
863 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
864 nfs_confirm_seqid(&data->owner->so_seqid, 0);
866 data->rpc_done = 1;
869 static void nfs4_open_release(void *calldata)
871 struct nfs4_opendata *data = calldata;
872 struct nfs4_state *state = NULL;
874 /* If this request hasn't been cancelled, do nothing */
875 if (data->cancelled == 0)
876 goto out_free;
877 /* In case of error, no cleanup! */
878 if (data->rpc_status != 0 || !data->rpc_done)
879 goto out_free;
880 /* In case we need an open_confirm, no cleanup! */
881 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
882 goto out_free;
883 state = nfs4_opendata_to_nfs4_state(data);
884 if (!IS_ERR(state))
885 nfs4_close_state(&data->path, state, data->o_arg.open_flags);
886 out_free:
887 nfs4_opendata_put(data);
890 static const struct rpc_call_ops nfs4_open_ops = {
891 .rpc_call_prepare = nfs4_open_prepare,
892 .rpc_call_done = nfs4_open_done,
893 .rpc_release = nfs4_open_release,
897 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
899 static int _nfs4_proc_open(struct nfs4_opendata *data)
901 struct inode *dir = data->dir->d_inode;
902 struct nfs_server *server = NFS_SERVER(dir);
903 struct nfs_openargs *o_arg = &data->o_arg;
904 struct nfs_openres *o_res = &data->o_res;
905 struct rpc_task *task;
906 struct rpc_message msg = {
907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
908 .rpc_argp = o_arg,
909 .rpc_resp = o_res,
910 .rpc_cred = data->owner->so_cred,
912 struct rpc_task_setup task_setup_data = {
913 .rpc_client = server->client,
914 .rpc_message = &msg,
915 .callback_ops = &nfs4_open_ops,
916 .callback_data = data,
917 .workqueue = nfsiod_workqueue,
918 .flags = RPC_TASK_ASYNC,
920 int status;
922 kref_get(&data->kref);
923 data->rpc_done = 0;
924 data->rpc_status = 0;
925 data->cancelled = 0;
926 task = rpc_run_task(&task_setup_data);
927 if (IS_ERR(task))
928 return PTR_ERR(task);
929 status = nfs4_wait_for_completion_rpc_task(task);
930 if (status != 0) {
931 data->cancelled = 1;
932 smp_wmb();
933 } else
934 status = data->rpc_status;
935 rpc_put_task(task);
936 if (status != 0 || !data->rpc_done)
937 return status;
939 if (o_res->fh.size == 0)
940 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
942 if (o_arg->open_flags & O_CREAT) {
943 update_changeattr(dir, &o_res->cinfo);
944 nfs_post_op_update_inode(dir, o_res->dir_attr);
945 } else
946 nfs_refresh_inode(dir, o_res->dir_attr);
947 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
948 status = _nfs4_proc_open_confirm(data);
949 if (status != 0)
950 return status;
952 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
953 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
954 return 0;
957 static int nfs4_recover_expired_lease(struct nfs_server *server)
959 struct nfs_client *clp = server->nfs_client;
960 int ret;
962 for (;;) {
963 ret = nfs4_wait_clnt_recover(server->client, clp);
964 if (ret != 0)
965 return ret;
966 if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
967 break;
968 nfs4_schedule_state_recovery(clp);
970 return 0;
974 * OPEN_EXPIRED:
975 * reclaim state on the server after a network partition.
976 * Assumes caller holds the appropriate lock
978 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
980 struct nfs4_opendata *opendata;
981 int ret;
983 opendata = nfs4_open_recoverdata_alloc(ctx, state);
984 if (IS_ERR(opendata))
985 return PTR_ERR(opendata);
986 ret = nfs4_open_recover(opendata, state);
987 if (ret == -ESTALE)
988 d_drop(ctx->path.dentry);
989 nfs4_opendata_put(opendata);
990 return ret;
993 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
995 struct nfs_server *server = NFS_SERVER(state->inode);
996 struct nfs4_exception exception = { };
997 int err;
999 do {
1000 err = _nfs4_open_expired(ctx, state);
1001 if (err == -NFS4ERR_DELAY)
1002 nfs4_handle_exception(server, err, &exception);
1003 } while (exception.retry);
1004 return err;
1007 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1009 struct nfs_open_context *ctx;
1010 int ret;
1012 ctx = nfs4_state_find_open_context(state);
1013 if (IS_ERR(ctx))
1014 return PTR_ERR(ctx);
1015 ret = nfs4_do_open_expired(ctx, state);
1016 put_nfs_open_context(ctx);
1017 return ret;
1021 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1022 * fields corresponding to attributes that were used to store the verifier.
1023 * Make sure we clobber those fields in the later setattr call
1025 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1027 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1028 !(sattr->ia_valid & ATTR_ATIME_SET))
1029 sattr->ia_valid |= ATTR_ATIME;
1031 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1032 !(sattr->ia_valid & ATTR_MTIME_SET))
1033 sattr->ia_valid |= ATTR_MTIME;
1037 * Returns a referenced nfs4_state
1039 static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1041 struct nfs4_state_owner *sp;
1042 struct nfs4_state *state = NULL;
1043 struct nfs_server *server = NFS_SERVER(dir);
1044 struct nfs_client *clp = server->nfs_client;
1045 struct nfs4_opendata *opendata;
1046 int status;
1048 /* Protect against reboot recovery conflicts */
1049 status = -ENOMEM;
1050 if (!(sp = nfs4_get_state_owner(server, cred))) {
1051 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1052 goto out_err;
1054 status = nfs4_recover_expired_lease(server);
1055 if (status != 0)
1056 goto err_put_state_owner;
1057 if (path->dentry->d_inode != NULL)
1058 nfs4_return_incompatible_delegation(path->dentry->d_inode, flags & (FMODE_READ|FMODE_WRITE));
1059 down_read(&clp->cl_sem);
1060 status = -ENOMEM;
1061 opendata = nfs4_opendata_alloc(path, sp, flags, sattr);
1062 if (opendata == NULL)
1063 goto err_release_rwsem;
1065 if (path->dentry->d_inode != NULL)
1066 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1068 status = _nfs4_proc_open(opendata);
1069 if (status != 0)
1070 goto err_opendata_put;
1072 if (opendata->o_arg.open_flags & O_EXCL)
1073 nfs4_exclusive_attrset(opendata, sattr);
1075 state = nfs4_opendata_to_nfs4_state(opendata);
1076 status = PTR_ERR(state);
1077 if (IS_ERR(state))
1078 goto err_opendata_put;
1079 nfs4_opendata_put(opendata);
1080 nfs4_put_state_owner(sp);
1081 up_read(&clp->cl_sem);
1082 *res = state;
1083 return 0;
1084 err_opendata_put:
1085 nfs4_opendata_put(opendata);
1086 err_release_rwsem:
1087 up_read(&clp->cl_sem);
1088 err_put_state_owner:
1089 nfs4_put_state_owner(sp);
1090 out_err:
1091 *res = NULL;
1092 return status;
1096 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred)
1098 struct nfs4_exception exception = { };
1099 struct nfs4_state *res;
1100 int status;
1102 do {
1103 status = _nfs4_do_open(dir, path, flags, sattr, cred, &res);
1104 if (status == 0)
1105 break;
1106 /* NOTE: BAD_SEQID means the server and client disagree about the
1107 * book-keeping w.r.t. state-changing operations
1108 * (OPEN/CLOSE/LOCK/LOCKU...)
1109 * It is actually a sign of a bug on the client or on the server.
1111 * If we receive a BAD_SEQID error in the particular case of
1112 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1113 * have unhashed the old state_owner for us, and that we can
1114 * therefore safely retry using a new one. We should still warn
1115 * the user though...
1117 if (status == -NFS4ERR_BAD_SEQID) {
1118 printk(KERN_WARNING "NFS: v4 server %s "
1119 " returned a bad sequence-id error!\n",
1120 NFS_SERVER(dir)->nfs_client->cl_hostname);
1121 exception.retry = 1;
1122 continue;
1125 * BAD_STATEID on OPEN means that the server cancelled our
1126 * state before it received the OPEN_CONFIRM.
1127 * Recover by retrying the request as per the discussion
1128 * on Page 181 of RFC3530.
1130 if (status == -NFS4ERR_BAD_STATEID) {
1131 exception.retry = 1;
1132 continue;
1134 if (status == -EAGAIN) {
1135 /* We must have found a delegation */
1136 exception.retry = 1;
1137 continue;
1139 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1140 status, &exception));
1141 } while (exception.retry);
1142 return res;
1145 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1146 struct nfs_fattr *fattr, struct iattr *sattr,
1147 struct nfs4_state *state)
1149 struct nfs_server *server = NFS_SERVER(inode);
1150 struct nfs_setattrargs arg = {
1151 .fh = NFS_FH(inode),
1152 .iap = sattr,
1153 .server = server,
1154 .bitmask = server->attr_bitmask,
1156 struct nfs_setattrres res = {
1157 .fattr = fattr,
1158 .server = server,
1160 struct rpc_message msg = {
1161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1162 .rpc_argp = &arg,
1163 .rpc_resp = &res,
1164 .rpc_cred = cred,
1166 unsigned long timestamp = jiffies;
1167 int status;
1169 nfs_fattr_init(fattr);
1171 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1172 /* Use that stateid */
1173 } else if (state != NULL) {
1174 nfs4_copy_stateid(&arg.stateid, state, current->files);
1175 } else
1176 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1178 status = rpc_call_sync(server->client, &msg, 0);
1179 if (status == 0 && state != NULL)
1180 renew_lease(server, timestamp);
1181 return status;
1184 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1185 struct nfs_fattr *fattr, struct iattr *sattr,
1186 struct nfs4_state *state)
1188 struct nfs_server *server = NFS_SERVER(inode);
1189 struct nfs4_exception exception = { };
1190 int err;
1191 do {
1192 err = nfs4_handle_exception(server,
1193 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1194 &exception);
1195 } while (exception.retry);
1196 return err;
1199 struct nfs4_closedata {
1200 struct path path;
1201 struct inode *inode;
1202 struct nfs4_state *state;
1203 struct nfs_closeargs arg;
1204 struct nfs_closeres res;
1205 struct nfs_fattr fattr;
1206 unsigned long timestamp;
1209 static void nfs4_free_closedata(void *data)
1211 struct nfs4_closedata *calldata = data;
1212 struct nfs4_state_owner *sp = calldata->state->owner;
1214 nfs4_put_open_state(calldata->state);
1215 nfs_free_seqid(calldata->arg.seqid);
1216 nfs4_put_state_owner(sp);
1217 path_put(&calldata->path);
1218 kfree(calldata);
1221 static void nfs4_close_done(struct rpc_task *task, void *data)
1223 struct nfs4_closedata *calldata = data;
1224 struct nfs4_state *state = calldata->state;
1225 struct nfs_server *server = NFS_SERVER(calldata->inode);
1227 if (RPC_ASSASSINATED(task))
1228 return;
1229 /* hmm. we are done with the inode, and in the process of freeing
1230 * the state_owner. we keep this around to process errors
1232 switch (task->tk_status) {
1233 case 0:
1234 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1235 renew_lease(server, calldata->timestamp);
1236 break;
1237 case -NFS4ERR_STALE_STATEID:
1238 case -NFS4ERR_EXPIRED:
1239 break;
1240 default:
1241 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1242 rpc_restart_call(task);
1243 return;
1246 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1249 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1251 struct nfs4_closedata *calldata = data;
1252 struct nfs4_state *state = calldata->state;
1253 int clear_rd, clear_wr, clear_rdwr;
1255 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1256 return;
1258 clear_rd = clear_wr = clear_rdwr = 0;
1259 spin_lock(&state->owner->so_lock);
1260 /* Calculate the change in open mode */
1261 if (state->n_rdwr == 0) {
1262 if (state->n_rdonly == 0) {
1263 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1264 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1266 if (state->n_wronly == 0) {
1267 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1268 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1271 spin_unlock(&state->owner->so_lock);
1272 if (!clear_rd && !clear_wr && !clear_rdwr) {
1273 /* Note: exit _without_ calling nfs4_close_done */
1274 task->tk_action = NULL;
1275 return;
1277 nfs_fattr_init(calldata->res.fattr);
1278 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1279 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1280 calldata->arg.open_flags = FMODE_READ;
1281 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1282 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1283 calldata->arg.open_flags = FMODE_WRITE;
1285 calldata->timestamp = jiffies;
1286 rpc_call_start(task);
1289 static const struct rpc_call_ops nfs4_close_ops = {
1290 .rpc_call_prepare = nfs4_close_prepare,
1291 .rpc_call_done = nfs4_close_done,
1292 .rpc_release = nfs4_free_closedata,
1296 * It is possible for data to be read/written from a mem-mapped file
1297 * after the sys_close call (which hits the vfs layer as a flush).
1298 * This means that we can't safely call nfsv4 close on a file until
1299 * the inode is cleared. This in turn means that we are not good
1300 * NFSv4 citizens - we do not indicate to the server to update the file's
1301 * share state even when we are done with one of the three share
1302 * stateid's in the inode.
1304 * NOTE: Caller must be holding the sp->so_owner semaphore!
1306 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1308 struct nfs_server *server = NFS_SERVER(state->inode);
1309 struct nfs4_closedata *calldata;
1310 struct nfs4_state_owner *sp = state->owner;
1311 struct rpc_task *task;
1312 struct rpc_message msg = {
1313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1314 .rpc_cred = state->owner->so_cred,
1316 struct rpc_task_setup task_setup_data = {
1317 .rpc_client = server->client,
1318 .rpc_message = &msg,
1319 .callback_ops = &nfs4_close_ops,
1320 .workqueue = nfsiod_workqueue,
1321 .flags = RPC_TASK_ASYNC,
1323 int status = -ENOMEM;
1325 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1326 if (calldata == NULL)
1327 goto out;
1328 calldata->inode = state->inode;
1329 calldata->state = state;
1330 calldata->arg.fh = NFS_FH(state->inode);
1331 calldata->arg.stateid = &state->open_stateid;
1332 /* Serialization for the sequence id */
1333 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1334 if (calldata->arg.seqid == NULL)
1335 goto out_free_calldata;
1336 calldata->arg.bitmask = server->attr_bitmask;
1337 calldata->res.fattr = &calldata->fattr;
1338 calldata->res.seqid = calldata->arg.seqid;
1339 calldata->res.server = server;
1340 calldata->path.mnt = mntget(path->mnt);
1341 calldata->path.dentry = dget(path->dentry);
1343 msg.rpc_argp = &calldata->arg,
1344 msg.rpc_resp = &calldata->res,
1345 task_setup_data.callback_data = calldata;
1346 task = rpc_run_task(&task_setup_data);
1347 if (IS_ERR(task))
1348 return PTR_ERR(task);
1349 status = 0;
1350 if (wait)
1351 status = rpc_wait_for_completion_task(task);
1352 rpc_put_task(task);
1353 return status;
1354 out_free_calldata:
1355 kfree(calldata);
1356 out:
1357 nfs4_put_open_state(state);
1358 nfs4_put_state_owner(sp);
1359 return status;
1362 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state)
1364 struct file *filp;
1365 int ret;
1367 /* If the open_intent is for execute, we have an extra check to make */
1368 if (nd->intent.open.flags & FMODE_EXEC) {
1369 ret = nfs_may_open(state->inode,
1370 state->owner->so_cred,
1371 nd->intent.open.flags);
1372 if (ret < 0)
1373 goto out_close;
1375 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1376 if (!IS_ERR(filp)) {
1377 struct nfs_open_context *ctx;
1378 ctx = nfs_file_open_context(filp);
1379 ctx->state = state;
1380 return 0;
1382 ret = PTR_ERR(filp);
1383 out_close:
1384 nfs4_close_sync(path, state, nd->intent.open.flags);
1385 return ret;
1388 struct dentry *
1389 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1391 struct path path = {
1392 .mnt = nd->path.mnt,
1393 .dentry = dentry,
1395 struct dentry *parent;
1396 struct iattr attr;
1397 struct rpc_cred *cred;
1398 struct nfs4_state *state;
1399 struct dentry *res;
1401 if (nd->flags & LOOKUP_CREATE) {
1402 attr.ia_mode = nd->intent.open.create_mode;
1403 attr.ia_valid = ATTR_MODE;
1404 if (!IS_POSIXACL(dir))
1405 attr.ia_mode &= ~current->fs->umask;
1406 } else {
1407 attr.ia_valid = 0;
1408 BUG_ON(nd->intent.open.flags & O_CREAT);
1411 cred = rpc_lookup_cred();
1412 if (IS_ERR(cred))
1413 return (struct dentry *)cred;
1414 parent = dentry->d_parent;
1415 /* Protect against concurrent sillydeletes */
1416 nfs_block_sillyrename(parent);
1417 state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred);
1418 put_rpccred(cred);
1419 if (IS_ERR(state)) {
1420 if (PTR_ERR(state) == -ENOENT) {
1421 d_add(dentry, NULL);
1422 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1424 nfs_unblock_sillyrename(parent);
1425 return (struct dentry *)state;
1427 res = d_add_unique(dentry, igrab(state->inode));
1428 if (res != NULL)
1429 path.dentry = res;
1430 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1431 nfs_unblock_sillyrename(parent);
1432 nfs4_intent_set_file(nd, &path, state);
1433 return res;
1437 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1439 struct path path = {
1440 .mnt = nd->path.mnt,
1441 .dentry = dentry,
1443 struct rpc_cred *cred;
1444 struct nfs4_state *state;
1446 cred = rpc_lookup_cred();
1447 if (IS_ERR(cred))
1448 return PTR_ERR(cred);
1449 state = nfs4_do_open(dir, &path, openflags, NULL, cred);
1450 put_rpccred(cred);
1451 if (IS_ERR(state)) {
1452 switch (PTR_ERR(state)) {
1453 case -EPERM:
1454 case -EACCES:
1455 case -EDQUOT:
1456 case -ENOSPC:
1457 case -EROFS:
1458 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1459 return 1;
1460 default:
1461 goto out_drop;
1464 if (state->inode == dentry->d_inode) {
1465 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1466 nfs4_intent_set_file(nd, &path, state);
1467 return 1;
1469 nfs4_close_sync(&path, state, openflags);
1470 out_drop:
1471 d_drop(dentry);
1472 return 0;
1476 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1478 struct nfs4_server_caps_res res = {};
1479 struct rpc_message msg = {
1480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1481 .rpc_argp = fhandle,
1482 .rpc_resp = &res,
1484 int status;
1486 status = rpc_call_sync(server->client, &msg, 0);
1487 if (status == 0) {
1488 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1489 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1490 server->caps |= NFS_CAP_ACLS;
1491 if (res.has_links != 0)
1492 server->caps |= NFS_CAP_HARDLINKS;
1493 if (res.has_symlinks != 0)
1494 server->caps |= NFS_CAP_SYMLINKS;
1495 server->acl_bitmask = res.acl_bitmask;
1497 return status;
1500 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1502 struct nfs4_exception exception = { };
1503 int err;
1504 do {
1505 err = nfs4_handle_exception(server,
1506 _nfs4_server_capabilities(server, fhandle),
1507 &exception);
1508 } while (exception.retry);
1509 return err;
1512 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1513 struct nfs_fsinfo *info)
1515 struct nfs4_lookup_root_arg args = {
1516 .bitmask = nfs4_fattr_bitmap,
1518 struct nfs4_lookup_res res = {
1519 .server = server,
1520 .fattr = info->fattr,
1521 .fh = fhandle,
1523 struct rpc_message msg = {
1524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1525 .rpc_argp = &args,
1526 .rpc_resp = &res,
1528 nfs_fattr_init(info->fattr);
1529 return rpc_call_sync(server->client, &msg, 0);
1532 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1533 struct nfs_fsinfo *info)
1535 struct nfs4_exception exception = { };
1536 int err;
1537 do {
1538 err = nfs4_handle_exception(server,
1539 _nfs4_lookup_root(server, fhandle, info),
1540 &exception);
1541 } while (exception.retry);
1542 return err;
1546 * get the file handle for the "/" directory on the server
1548 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1549 struct nfs_fsinfo *info)
1551 int status;
1553 status = nfs4_lookup_root(server, fhandle, info);
1554 if (status == 0)
1555 status = nfs4_server_capabilities(server, fhandle);
1556 if (status == 0)
1557 status = nfs4_do_fsinfo(server, fhandle, info);
1558 return nfs4_map_errors(status);
1562 * Get locations and (maybe) other attributes of a referral.
1563 * Note that we'll actually follow the referral later when
1564 * we detect fsid mismatch in inode revalidation
1566 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1568 int status = -ENOMEM;
1569 struct page *page = NULL;
1570 struct nfs4_fs_locations *locations = NULL;
1572 page = alloc_page(GFP_KERNEL);
1573 if (page == NULL)
1574 goto out;
1575 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1576 if (locations == NULL)
1577 goto out;
1579 status = nfs4_proc_fs_locations(dir, name, locations, page);
1580 if (status != 0)
1581 goto out;
1582 /* Make sure server returned a different fsid for the referral */
1583 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1584 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1585 status = -EIO;
1586 goto out;
1589 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1590 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1591 if (!fattr->mode)
1592 fattr->mode = S_IFDIR;
1593 memset(fhandle, 0, sizeof(struct nfs_fh));
1594 out:
1595 if (page)
1596 __free_page(page);
1597 if (locations)
1598 kfree(locations);
1599 return status;
1602 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1604 struct nfs4_getattr_arg args = {
1605 .fh = fhandle,
1606 .bitmask = server->attr_bitmask,
1608 struct nfs4_getattr_res res = {
1609 .fattr = fattr,
1610 .server = server,
1612 struct rpc_message msg = {
1613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1614 .rpc_argp = &args,
1615 .rpc_resp = &res,
1618 nfs_fattr_init(fattr);
1619 return rpc_call_sync(server->client, &msg, 0);
1622 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1624 struct nfs4_exception exception = { };
1625 int err;
1626 do {
1627 err = nfs4_handle_exception(server,
1628 _nfs4_proc_getattr(server, fhandle, fattr),
1629 &exception);
1630 } while (exception.retry);
1631 return err;
1635 * The file is not closed if it is opened due to the a request to change
1636 * the size of the file. The open call will not be needed once the
1637 * VFS layer lookup-intents are implemented.
1639 * Close is called when the inode is destroyed.
1640 * If we haven't opened the file for O_WRONLY, we
1641 * need to in the size_change case to obtain a stateid.
1643 * Got race?
1644 * Because OPEN is always done by name in nfsv4, it is
1645 * possible that we opened a different file by the same
1646 * name. We can recognize this race condition, but we
1647 * can't do anything about it besides returning an error.
1649 * This will be fixed with VFS changes (lookup-intent).
1651 static int
1652 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1653 struct iattr *sattr)
1655 struct inode *inode = dentry->d_inode;
1656 struct rpc_cred *cred = NULL;
1657 struct nfs4_state *state = NULL;
1658 int status;
1660 nfs_fattr_init(fattr);
1662 /* Search for an existing open(O_WRITE) file */
1663 if (sattr->ia_valid & ATTR_FILE) {
1664 struct nfs_open_context *ctx;
1666 ctx = nfs_file_open_context(sattr->ia_file);
1667 cred = ctx->cred;
1668 state = ctx->state;
1671 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1672 if (status == 0)
1673 nfs_setattr_update_inode(inode, sattr);
1674 return status;
1677 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1678 const struct qstr *name, struct nfs_fh *fhandle,
1679 struct nfs_fattr *fattr)
1681 int status;
1682 struct nfs4_lookup_arg args = {
1683 .bitmask = server->attr_bitmask,
1684 .dir_fh = dirfh,
1685 .name = name,
1687 struct nfs4_lookup_res res = {
1688 .server = server,
1689 .fattr = fattr,
1690 .fh = fhandle,
1692 struct rpc_message msg = {
1693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1694 .rpc_argp = &args,
1695 .rpc_resp = &res,
1698 nfs_fattr_init(fattr);
1700 dprintk("NFS call lookupfh %s\n", name->name);
1701 status = rpc_call_sync(server->client, &msg, 0);
1702 dprintk("NFS reply lookupfh: %d\n", status);
1703 return status;
1706 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1707 struct qstr *name, struct nfs_fh *fhandle,
1708 struct nfs_fattr *fattr)
1710 struct nfs4_exception exception = { };
1711 int err;
1712 do {
1713 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1714 /* FIXME: !!!! */
1715 if (err == -NFS4ERR_MOVED) {
1716 err = -EREMOTE;
1717 break;
1719 err = nfs4_handle_exception(server, err, &exception);
1720 } while (exception.retry);
1721 return err;
1724 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
1725 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1727 int status;
1729 dprintk("NFS call lookup %s\n", name->name);
1730 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1731 if (status == -NFS4ERR_MOVED)
1732 status = nfs4_get_referral(dir, name, fattr, fhandle);
1733 dprintk("NFS reply lookup: %d\n", status);
1734 return status;
1737 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1739 struct nfs4_exception exception = { };
1740 int err;
1741 do {
1742 err = nfs4_handle_exception(NFS_SERVER(dir),
1743 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1744 &exception);
1745 } while (exception.retry);
1746 return err;
1749 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1751 struct nfs_server *server = NFS_SERVER(inode);
1752 struct nfs_fattr fattr;
1753 struct nfs4_accessargs args = {
1754 .fh = NFS_FH(inode),
1755 .bitmask = server->attr_bitmask,
1757 struct nfs4_accessres res = {
1758 .server = server,
1759 .fattr = &fattr,
1761 struct rpc_message msg = {
1762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1763 .rpc_argp = &args,
1764 .rpc_resp = &res,
1765 .rpc_cred = entry->cred,
1767 int mode = entry->mask;
1768 int status;
1771 * Determine which access bits we want to ask for...
1773 if (mode & MAY_READ)
1774 args.access |= NFS4_ACCESS_READ;
1775 if (S_ISDIR(inode->i_mode)) {
1776 if (mode & MAY_WRITE)
1777 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1778 if (mode & MAY_EXEC)
1779 args.access |= NFS4_ACCESS_LOOKUP;
1780 } else {
1781 if (mode & MAY_WRITE)
1782 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1783 if (mode & MAY_EXEC)
1784 args.access |= NFS4_ACCESS_EXECUTE;
1786 nfs_fattr_init(&fattr);
1787 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1788 if (!status) {
1789 entry->mask = 0;
1790 if (res.access & NFS4_ACCESS_READ)
1791 entry->mask |= MAY_READ;
1792 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1793 entry->mask |= MAY_WRITE;
1794 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1795 entry->mask |= MAY_EXEC;
1796 nfs_refresh_inode(inode, &fattr);
1798 return status;
1801 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1803 struct nfs4_exception exception = { };
1804 int err;
1805 do {
1806 err = nfs4_handle_exception(NFS_SERVER(inode),
1807 _nfs4_proc_access(inode, entry),
1808 &exception);
1809 } while (exception.retry);
1810 return err;
1814 * TODO: For the time being, we don't try to get any attributes
1815 * along with any of the zero-copy operations READ, READDIR,
1816 * READLINK, WRITE.
1818 * In the case of the first three, we want to put the GETATTR
1819 * after the read-type operation -- this is because it is hard
1820 * to predict the length of a GETATTR response in v4, and thus
1821 * align the READ data correctly. This means that the GETATTR
1822 * may end up partially falling into the page cache, and we should
1823 * shift it into the 'tail' of the xdr_buf before processing.
1824 * To do this efficiently, we need to know the total length
1825 * of data received, which doesn't seem to be available outside
1826 * of the RPC layer.
1828 * In the case of WRITE, we also want to put the GETATTR after
1829 * the operation -- in this case because we want to make sure
1830 * we get the post-operation mtime and size. This means that
1831 * we can't use xdr_encode_pages() as written: we need a variant
1832 * of it which would leave room in the 'tail' iovec.
1834 * Both of these changes to the XDR layer would in fact be quite
1835 * minor, but I decided to leave them for a subsequent patch.
1837 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1838 unsigned int pgbase, unsigned int pglen)
1840 struct nfs4_readlink args = {
1841 .fh = NFS_FH(inode),
1842 .pgbase = pgbase,
1843 .pglen = pglen,
1844 .pages = &page,
1846 struct rpc_message msg = {
1847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1848 .rpc_argp = &args,
1849 .rpc_resp = NULL,
1852 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1855 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1856 unsigned int pgbase, unsigned int pglen)
1858 struct nfs4_exception exception = { };
1859 int err;
1860 do {
1861 err = nfs4_handle_exception(NFS_SERVER(inode),
1862 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1863 &exception);
1864 } while (exception.retry);
1865 return err;
1869 * Got race?
1870 * We will need to arrange for the VFS layer to provide an atomic open.
1871 * Until then, this create/open method is prone to inefficiency and race
1872 * conditions due to the lookup, create, and open VFS calls from sys_open()
1873 * placed on the wire.
1875 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1876 * The file will be opened again in the subsequent VFS open call
1877 * (nfs4_proc_file_open).
1879 * The open for read will just hang around to be used by any process that
1880 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1883 static int
1884 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1885 int flags, struct nameidata *nd)
1887 struct path path = {
1888 .mnt = nd->path.mnt,
1889 .dentry = dentry,
1891 struct nfs4_state *state;
1892 struct rpc_cred *cred;
1893 int status = 0;
1895 cred = rpc_lookup_cred();
1896 if (IS_ERR(cred)) {
1897 status = PTR_ERR(cred);
1898 goto out;
1900 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1901 d_drop(dentry);
1902 if (IS_ERR(state)) {
1903 status = PTR_ERR(state);
1904 goto out_putcred;
1906 d_add(dentry, igrab(state->inode));
1907 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1908 if (flags & O_EXCL) {
1909 struct nfs_fattr fattr;
1910 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
1911 if (status == 0)
1912 nfs_setattr_update_inode(state->inode, sattr);
1913 nfs_post_op_update_inode(state->inode, &fattr);
1915 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
1916 status = nfs4_intent_set_file(nd, &path, state);
1917 else
1918 nfs4_close_sync(&path, state, flags);
1919 out_putcred:
1920 put_rpccred(cred);
1921 out:
1922 return status;
1925 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1927 struct nfs_server *server = NFS_SERVER(dir);
1928 struct nfs_removeargs args = {
1929 .fh = NFS_FH(dir),
1930 .name.len = name->len,
1931 .name.name = name->name,
1932 .bitmask = server->attr_bitmask,
1934 struct nfs_removeres res = {
1935 .server = server,
1937 struct rpc_message msg = {
1938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1939 .rpc_argp = &args,
1940 .rpc_resp = &res,
1942 int status;
1944 nfs_fattr_init(&res.dir_attr);
1945 status = rpc_call_sync(server->client, &msg, 0);
1946 if (status == 0) {
1947 update_changeattr(dir, &res.cinfo);
1948 nfs_post_op_update_inode(dir, &res.dir_attr);
1950 return status;
1953 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1955 struct nfs4_exception exception = { };
1956 int err;
1957 do {
1958 err = nfs4_handle_exception(NFS_SERVER(dir),
1959 _nfs4_proc_remove(dir, name),
1960 &exception);
1961 } while (exception.retry);
1962 return err;
1965 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
1967 struct nfs_server *server = NFS_SERVER(dir);
1968 struct nfs_removeargs *args = msg->rpc_argp;
1969 struct nfs_removeres *res = msg->rpc_resp;
1971 args->bitmask = server->attr_bitmask;
1972 res->server = server;
1973 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1976 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
1978 struct nfs_removeres *res = task->tk_msg.rpc_resp;
1980 if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
1981 return 0;
1982 update_changeattr(dir, &res->cinfo);
1983 nfs_post_op_update_inode(dir, &res->dir_attr);
1984 return 1;
1987 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1988 struct inode *new_dir, struct qstr *new_name)
1990 struct nfs_server *server = NFS_SERVER(old_dir);
1991 struct nfs4_rename_arg arg = {
1992 .old_dir = NFS_FH(old_dir),
1993 .new_dir = NFS_FH(new_dir),
1994 .old_name = old_name,
1995 .new_name = new_name,
1996 .bitmask = server->attr_bitmask,
1998 struct nfs_fattr old_fattr, new_fattr;
1999 struct nfs4_rename_res res = {
2000 .server = server,
2001 .old_fattr = &old_fattr,
2002 .new_fattr = &new_fattr,
2004 struct rpc_message msg = {
2005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2006 .rpc_argp = &arg,
2007 .rpc_resp = &res,
2009 int status;
2011 nfs_fattr_init(res.old_fattr);
2012 nfs_fattr_init(res.new_fattr);
2013 status = rpc_call_sync(server->client, &msg, 0);
2015 if (!status) {
2016 update_changeattr(old_dir, &res.old_cinfo);
2017 nfs_post_op_update_inode(old_dir, res.old_fattr);
2018 update_changeattr(new_dir, &res.new_cinfo);
2019 nfs_post_op_update_inode(new_dir, res.new_fattr);
2021 return status;
2024 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2025 struct inode *new_dir, struct qstr *new_name)
2027 struct nfs4_exception exception = { };
2028 int err;
2029 do {
2030 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2031 _nfs4_proc_rename(old_dir, old_name,
2032 new_dir, new_name),
2033 &exception);
2034 } while (exception.retry);
2035 return err;
2038 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2040 struct nfs_server *server = NFS_SERVER(inode);
2041 struct nfs4_link_arg arg = {
2042 .fh = NFS_FH(inode),
2043 .dir_fh = NFS_FH(dir),
2044 .name = name,
2045 .bitmask = server->attr_bitmask,
2047 struct nfs_fattr fattr, dir_attr;
2048 struct nfs4_link_res res = {
2049 .server = server,
2050 .fattr = &fattr,
2051 .dir_attr = &dir_attr,
2053 struct rpc_message msg = {
2054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2055 .rpc_argp = &arg,
2056 .rpc_resp = &res,
2058 int status;
2060 nfs_fattr_init(res.fattr);
2061 nfs_fattr_init(res.dir_attr);
2062 status = rpc_call_sync(server->client, &msg, 0);
2063 if (!status) {
2064 update_changeattr(dir, &res.cinfo);
2065 nfs_post_op_update_inode(dir, res.dir_attr);
2066 nfs_post_op_update_inode(inode, res.fattr);
2069 return status;
2072 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2074 struct nfs4_exception exception = { };
2075 int err;
2076 do {
2077 err = nfs4_handle_exception(NFS_SERVER(inode),
2078 _nfs4_proc_link(inode, dir, name),
2079 &exception);
2080 } while (exception.retry);
2081 return err;
2084 struct nfs4_createdata {
2085 struct rpc_message msg;
2086 struct nfs4_create_arg arg;
2087 struct nfs4_create_res res;
2088 struct nfs_fh fh;
2089 struct nfs_fattr fattr;
2090 struct nfs_fattr dir_fattr;
2093 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2094 struct qstr *name, struct iattr *sattr, u32 ftype)
2096 struct nfs4_createdata *data;
2098 data = kzalloc(sizeof(*data), GFP_KERNEL);
2099 if (data != NULL) {
2100 struct nfs_server *server = NFS_SERVER(dir);
2102 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2103 data->msg.rpc_argp = &data->arg;
2104 data->msg.rpc_resp = &data->res;
2105 data->arg.dir_fh = NFS_FH(dir);
2106 data->arg.server = server;
2107 data->arg.name = name;
2108 data->arg.attrs = sattr;
2109 data->arg.ftype = ftype;
2110 data->arg.bitmask = server->attr_bitmask;
2111 data->res.server = server;
2112 data->res.fh = &data->fh;
2113 data->res.fattr = &data->fattr;
2114 data->res.dir_fattr = &data->dir_fattr;
2115 nfs_fattr_init(data->res.fattr);
2116 nfs_fattr_init(data->res.dir_fattr);
2118 return data;
2121 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2123 int status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
2124 if (status == 0) {
2125 update_changeattr(dir, &data->res.dir_cinfo);
2126 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2127 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2129 return status;
2132 static void nfs4_free_createdata(struct nfs4_createdata *data)
2134 kfree(data);
2137 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2138 struct page *page, unsigned int len, struct iattr *sattr)
2140 struct nfs4_createdata *data;
2141 int status = -ENAMETOOLONG;
2143 if (len > NFS4_MAXPATHLEN)
2144 goto out;
2146 status = -ENOMEM;
2147 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2148 if (data == NULL)
2149 goto out;
2151 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2152 data->arg.u.symlink.pages = &page;
2153 data->arg.u.symlink.len = len;
2155 status = nfs4_do_create(dir, dentry, data);
2157 nfs4_free_createdata(data);
2158 out:
2159 return status;
2162 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2163 struct page *page, unsigned int len, struct iattr *sattr)
2165 struct nfs4_exception exception = { };
2166 int err;
2167 do {
2168 err = nfs4_handle_exception(NFS_SERVER(dir),
2169 _nfs4_proc_symlink(dir, dentry, page,
2170 len, sattr),
2171 &exception);
2172 } while (exception.retry);
2173 return err;
2176 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2177 struct iattr *sattr)
2179 struct nfs4_createdata *data;
2180 int status = -ENOMEM;
2182 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2183 if (data == NULL)
2184 goto out;
2186 status = nfs4_do_create(dir, dentry, data);
2188 nfs4_free_createdata(data);
2189 out:
2190 return status;
2193 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2194 struct iattr *sattr)
2196 struct nfs4_exception exception = { };
2197 int err;
2198 do {
2199 err = nfs4_handle_exception(NFS_SERVER(dir),
2200 _nfs4_proc_mkdir(dir, dentry, sattr),
2201 &exception);
2202 } while (exception.retry);
2203 return err;
2206 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2207 u64 cookie, struct page *page, unsigned int count, int plus)
2209 struct inode *dir = dentry->d_inode;
2210 struct nfs4_readdir_arg args = {
2211 .fh = NFS_FH(dir),
2212 .pages = &page,
2213 .pgbase = 0,
2214 .count = count,
2215 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2217 struct nfs4_readdir_res res;
2218 struct rpc_message msg = {
2219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2220 .rpc_argp = &args,
2221 .rpc_resp = &res,
2222 .rpc_cred = cred,
2224 int status;
2226 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2227 dentry->d_parent->d_name.name,
2228 dentry->d_name.name,
2229 (unsigned long long)cookie);
2230 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2231 res.pgbase = args.pgbase;
2232 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2233 if (status == 0)
2234 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2236 nfs_invalidate_atime(dir);
2238 dprintk("%s: returns %d\n", __func__, status);
2239 return status;
2242 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2243 u64 cookie, struct page *page, unsigned int count, int plus)
2245 struct nfs4_exception exception = { };
2246 int err;
2247 do {
2248 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2249 _nfs4_proc_readdir(dentry, cred, cookie,
2250 page, count, plus),
2251 &exception);
2252 } while (exception.retry);
2253 return err;
2256 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2257 struct iattr *sattr, dev_t rdev)
2259 struct nfs4_createdata *data;
2260 int mode = sattr->ia_mode;
2261 int status = -ENOMEM;
2263 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2264 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2266 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2267 if (data == NULL)
2268 goto out;
2270 if (S_ISFIFO(mode))
2271 data->arg.ftype = NF4FIFO;
2272 else if (S_ISBLK(mode)) {
2273 data->arg.ftype = NF4BLK;
2274 data->arg.u.device.specdata1 = MAJOR(rdev);
2275 data->arg.u.device.specdata2 = MINOR(rdev);
2277 else if (S_ISCHR(mode)) {
2278 data->arg.ftype = NF4CHR;
2279 data->arg.u.device.specdata1 = MAJOR(rdev);
2280 data->arg.u.device.specdata2 = MINOR(rdev);
2283 status = nfs4_do_create(dir, dentry, data);
2285 nfs4_free_createdata(data);
2286 out:
2287 return status;
2290 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2291 struct iattr *sattr, dev_t rdev)
2293 struct nfs4_exception exception = { };
2294 int err;
2295 do {
2296 err = nfs4_handle_exception(NFS_SERVER(dir),
2297 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2298 &exception);
2299 } while (exception.retry);
2300 return err;
2303 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2304 struct nfs_fsstat *fsstat)
2306 struct nfs4_statfs_arg args = {
2307 .fh = fhandle,
2308 .bitmask = server->attr_bitmask,
2310 struct rpc_message msg = {
2311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2312 .rpc_argp = &args,
2313 .rpc_resp = fsstat,
2316 nfs_fattr_init(fsstat->fattr);
2317 return rpc_call_sync(server->client, &msg, 0);
2320 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2322 struct nfs4_exception exception = { };
2323 int err;
2324 do {
2325 err = nfs4_handle_exception(server,
2326 _nfs4_proc_statfs(server, fhandle, fsstat),
2327 &exception);
2328 } while (exception.retry);
2329 return err;
2332 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2333 struct nfs_fsinfo *fsinfo)
2335 struct nfs4_fsinfo_arg args = {
2336 .fh = fhandle,
2337 .bitmask = server->attr_bitmask,
2339 struct rpc_message msg = {
2340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2341 .rpc_argp = &args,
2342 .rpc_resp = fsinfo,
2345 return rpc_call_sync(server->client, &msg, 0);
2348 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2350 struct nfs4_exception exception = { };
2351 int err;
2353 do {
2354 err = nfs4_handle_exception(server,
2355 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2356 &exception);
2357 } while (exception.retry);
2358 return err;
2361 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2363 nfs_fattr_init(fsinfo->fattr);
2364 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2367 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2368 struct nfs_pathconf *pathconf)
2370 struct nfs4_pathconf_arg args = {
2371 .fh = fhandle,
2372 .bitmask = server->attr_bitmask,
2374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2376 .rpc_argp = &args,
2377 .rpc_resp = pathconf,
2380 /* None of the pathconf attributes are mandatory to implement */
2381 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2382 memset(pathconf, 0, sizeof(*pathconf));
2383 return 0;
2386 nfs_fattr_init(pathconf->fattr);
2387 return rpc_call_sync(server->client, &msg, 0);
2390 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2391 struct nfs_pathconf *pathconf)
2393 struct nfs4_exception exception = { };
2394 int err;
2396 do {
2397 err = nfs4_handle_exception(server,
2398 _nfs4_proc_pathconf(server, fhandle, pathconf),
2399 &exception);
2400 } while (exception.retry);
2401 return err;
2404 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2406 struct nfs_server *server = NFS_SERVER(data->inode);
2408 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
2409 rpc_restart_call(task);
2410 return -EAGAIN;
2413 nfs_invalidate_atime(data->inode);
2414 if (task->tk_status > 0)
2415 renew_lease(server, data->timestamp);
2416 return 0;
2419 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2421 data->timestamp = jiffies;
2422 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2425 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2427 struct inode *inode = data->inode;
2429 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2430 rpc_restart_call(task);
2431 return -EAGAIN;
2433 if (task->tk_status >= 0) {
2434 renew_lease(NFS_SERVER(inode), data->timestamp);
2435 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2437 return 0;
2440 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2442 struct nfs_server *server = NFS_SERVER(data->inode);
2444 data->args.bitmask = server->attr_bitmask;
2445 data->res.server = server;
2446 data->timestamp = jiffies;
2448 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2451 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2453 struct inode *inode = data->inode;
2455 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2456 rpc_restart_call(task);
2457 return -EAGAIN;
2459 nfs_refresh_inode(inode, data->res.fattr);
2460 return 0;
2463 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2465 struct nfs_server *server = NFS_SERVER(data->inode);
2467 data->args.bitmask = server->attr_bitmask;
2468 data->res.server = server;
2469 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2473 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2474 * standalone procedure for queueing an asynchronous RENEW.
2476 static void nfs4_renew_done(struct rpc_task *task, void *data)
2478 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2479 unsigned long timestamp = (unsigned long)data;
2481 if (task->tk_status < 0) {
2482 switch (task->tk_status) {
2483 case -NFS4ERR_STALE_CLIENTID:
2484 case -NFS4ERR_EXPIRED:
2485 case -NFS4ERR_CB_PATH_DOWN:
2486 nfs4_schedule_state_recovery(clp);
2488 return;
2490 spin_lock(&clp->cl_lock);
2491 if (time_before(clp->cl_last_renewal,timestamp))
2492 clp->cl_last_renewal = timestamp;
2493 spin_unlock(&clp->cl_lock);
2496 static const struct rpc_call_ops nfs4_renew_ops = {
2497 .rpc_call_done = nfs4_renew_done,
2500 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2504 .rpc_argp = clp,
2505 .rpc_cred = cred,
2508 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2509 &nfs4_renew_ops, (void *)jiffies);
2512 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2516 .rpc_argp = clp,
2517 .rpc_cred = cred,
2519 unsigned long now = jiffies;
2520 int status;
2522 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2523 if (status < 0)
2524 return status;
2525 spin_lock(&clp->cl_lock);
2526 if (time_before(clp->cl_last_renewal,now))
2527 clp->cl_last_renewal = now;
2528 spin_unlock(&clp->cl_lock);
2529 return 0;
2532 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2534 return (server->caps & NFS_CAP_ACLS)
2535 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2536 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2539 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2540 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2541 * the stack.
2543 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2545 static void buf_to_pages(const void *buf, size_t buflen,
2546 struct page **pages, unsigned int *pgbase)
2548 const void *p = buf;
2550 *pgbase = offset_in_page(buf);
2551 p -= *pgbase;
2552 while (p < buf + buflen) {
2553 *(pages++) = virt_to_page(p);
2554 p += PAGE_CACHE_SIZE;
2558 struct nfs4_cached_acl {
2559 int cached;
2560 size_t len;
2561 char data[0];
2564 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2566 struct nfs_inode *nfsi = NFS_I(inode);
2568 spin_lock(&inode->i_lock);
2569 kfree(nfsi->nfs4_acl);
2570 nfsi->nfs4_acl = acl;
2571 spin_unlock(&inode->i_lock);
2574 static void nfs4_zap_acl_attr(struct inode *inode)
2576 nfs4_set_cached_acl(inode, NULL);
2579 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2581 struct nfs_inode *nfsi = NFS_I(inode);
2582 struct nfs4_cached_acl *acl;
2583 int ret = -ENOENT;
2585 spin_lock(&inode->i_lock);
2586 acl = nfsi->nfs4_acl;
2587 if (acl == NULL)
2588 goto out;
2589 if (buf == NULL) /* user is just asking for length */
2590 goto out_len;
2591 if (acl->cached == 0)
2592 goto out;
2593 ret = -ERANGE; /* see getxattr(2) man page */
2594 if (acl->len > buflen)
2595 goto out;
2596 memcpy(buf, acl->data, acl->len);
2597 out_len:
2598 ret = acl->len;
2599 out:
2600 spin_unlock(&inode->i_lock);
2601 return ret;
2604 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2606 struct nfs4_cached_acl *acl;
2608 if (buf && acl_len <= PAGE_SIZE) {
2609 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2610 if (acl == NULL)
2611 goto out;
2612 acl->cached = 1;
2613 memcpy(acl->data, buf, acl_len);
2614 } else {
2615 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2616 if (acl == NULL)
2617 goto out;
2618 acl->cached = 0;
2620 acl->len = acl_len;
2621 out:
2622 nfs4_set_cached_acl(inode, acl);
2625 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2627 struct page *pages[NFS4ACL_MAXPAGES];
2628 struct nfs_getaclargs args = {
2629 .fh = NFS_FH(inode),
2630 .acl_pages = pages,
2631 .acl_len = buflen,
2633 size_t resp_len = buflen;
2634 void *resp_buf;
2635 struct rpc_message msg = {
2636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2637 .rpc_argp = &args,
2638 .rpc_resp = &resp_len,
2640 struct page *localpage = NULL;
2641 int ret;
2643 if (buflen < PAGE_SIZE) {
2644 /* As long as we're doing a round trip to the server anyway,
2645 * let's be prepared for a page of acl data. */
2646 localpage = alloc_page(GFP_KERNEL);
2647 resp_buf = page_address(localpage);
2648 if (localpage == NULL)
2649 return -ENOMEM;
2650 args.acl_pages[0] = localpage;
2651 args.acl_pgbase = 0;
2652 resp_len = args.acl_len = PAGE_SIZE;
2653 } else {
2654 resp_buf = buf;
2655 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2657 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2658 if (ret)
2659 goto out_free;
2660 if (resp_len > args.acl_len)
2661 nfs4_write_cached_acl(inode, NULL, resp_len);
2662 else
2663 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2664 if (buf) {
2665 ret = -ERANGE;
2666 if (resp_len > buflen)
2667 goto out_free;
2668 if (localpage)
2669 memcpy(buf, resp_buf, resp_len);
2671 ret = resp_len;
2672 out_free:
2673 if (localpage)
2674 __free_page(localpage);
2675 return ret;
2678 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2680 struct nfs4_exception exception = { };
2681 ssize_t ret;
2682 do {
2683 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2684 if (ret >= 0)
2685 break;
2686 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2687 } while (exception.retry);
2688 return ret;
2691 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2693 struct nfs_server *server = NFS_SERVER(inode);
2694 int ret;
2696 if (!nfs4_server_supports_acls(server))
2697 return -EOPNOTSUPP;
2698 ret = nfs_revalidate_inode(server, inode);
2699 if (ret < 0)
2700 return ret;
2701 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2702 nfs_zap_acl_cache(inode);
2703 ret = nfs4_read_cached_acl(inode, buf, buflen);
2704 if (ret != -ENOENT)
2705 return ret;
2706 return nfs4_get_acl_uncached(inode, buf, buflen);
2709 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2711 struct nfs_server *server = NFS_SERVER(inode);
2712 struct page *pages[NFS4ACL_MAXPAGES];
2713 struct nfs_setaclargs arg = {
2714 .fh = NFS_FH(inode),
2715 .acl_pages = pages,
2716 .acl_len = buflen,
2718 struct rpc_message msg = {
2719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2720 .rpc_argp = &arg,
2721 .rpc_resp = NULL,
2723 int ret;
2725 if (!nfs4_server_supports_acls(server))
2726 return -EOPNOTSUPP;
2727 nfs_inode_return_delegation(inode);
2728 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2729 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2730 nfs_access_zap_cache(inode);
2731 nfs_zap_acl_cache(inode);
2732 return ret;
2735 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2737 struct nfs4_exception exception = { };
2738 int err;
2739 do {
2740 err = nfs4_handle_exception(NFS_SERVER(inode),
2741 __nfs4_proc_set_acl(inode, buf, buflen),
2742 &exception);
2743 } while (exception.retry);
2744 return err;
2747 static int
2748 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2750 struct nfs_client *clp = server->nfs_client;
2752 if (!clp || task->tk_status >= 0)
2753 return 0;
2754 switch(task->tk_status) {
2755 case -NFS4ERR_STALE_CLIENTID:
2756 case -NFS4ERR_STALE_STATEID:
2757 case -NFS4ERR_EXPIRED:
2758 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
2759 nfs4_schedule_state_recovery(clp);
2760 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
2761 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
2762 task->tk_status = 0;
2763 return -EAGAIN;
2764 case -NFS4ERR_DELAY:
2765 nfs_inc_server_stats(server, NFSIOS_DELAY);
2766 case -NFS4ERR_GRACE:
2767 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2768 task->tk_status = 0;
2769 return -EAGAIN;
2770 case -NFS4ERR_OLD_STATEID:
2771 task->tk_status = 0;
2772 return -EAGAIN;
2774 task->tk_status = nfs4_map_errors(task->tk_status);
2775 return 0;
2778 static int nfs4_wait_bit_killable(void *word)
2780 if (fatal_signal_pending(current))
2781 return -ERESTARTSYS;
2782 schedule();
2783 return 0;
2786 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
2788 int res;
2790 might_sleep();
2792 rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
2794 res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
2795 nfs4_wait_bit_killable, TASK_KILLABLE);
2797 rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
2798 return res;
2801 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2803 int res = 0;
2805 might_sleep();
2807 if (*timeout <= 0)
2808 *timeout = NFS4_POLL_RETRY_MIN;
2809 if (*timeout > NFS4_POLL_RETRY_MAX)
2810 *timeout = NFS4_POLL_RETRY_MAX;
2811 schedule_timeout_killable(*timeout);
2812 if (fatal_signal_pending(current))
2813 res = -ERESTARTSYS;
2814 *timeout <<= 1;
2815 return res;
2818 /* This is the error handling routine for processes that are allowed
2819 * to sleep.
2821 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2823 struct nfs_client *clp = server->nfs_client;
2824 int ret = errorcode;
2826 exception->retry = 0;
2827 switch(errorcode) {
2828 case 0:
2829 return 0;
2830 case -NFS4ERR_STALE_CLIENTID:
2831 case -NFS4ERR_STALE_STATEID:
2832 case -NFS4ERR_EXPIRED:
2833 nfs4_schedule_state_recovery(clp);
2834 ret = nfs4_wait_clnt_recover(server->client, clp);
2835 if (ret == 0)
2836 exception->retry = 1;
2837 break;
2838 case -NFS4ERR_FILE_OPEN:
2839 case -NFS4ERR_GRACE:
2840 case -NFS4ERR_DELAY:
2841 ret = nfs4_delay(server->client, &exception->timeout);
2842 if (ret != 0)
2843 break;
2844 case -NFS4ERR_OLD_STATEID:
2845 exception->retry = 1;
2847 /* We failed to handle the error */
2848 return nfs4_map_errors(ret);
2851 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2853 nfs4_verifier sc_verifier;
2854 struct nfs4_setclientid setclientid = {
2855 .sc_verifier = &sc_verifier,
2856 .sc_prog = program,
2858 struct rpc_message msg = {
2859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2860 .rpc_argp = &setclientid,
2861 .rpc_resp = clp,
2862 .rpc_cred = cred,
2864 __be32 *p;
2865 int loop = 0;
2866 int status;
2868 p = (__be32*)sc_verifier.data;
2869 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2870 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2872 for(;;) {
2873 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2874 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
2875 clp->cl_ipaddr,
2876 rpc_peeraddr2str(clp->cl_rpcclient,
2877 RPC_DISPLAY_ADDR),
2878 rpc_peeraddr2str(clp->cl_rpcclient,
2879 RPC_DISPLAY_PROTO),
2880 clp->cl_rpcclient->cl_auth->au_ops->au_name,
2881 clp->cl_id_uniquifier);
2882 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2883 sizeof(setclientid.sc_netid),
2884 rpc_peeraddr2str(clp->cl_rpcclient,
2885 RPC_DISPLAY_NETID));
2886 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2887 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
2888 clp->cl_ipaddr, port >> 8, port & 255);
2890 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2891 if (status != -NFS4ERR_CLID_INUSE)
2892 break;
2893 if (signalled())
2894 break;
2895 if (loop++ & 1)
2896 ssleep(clp->cl_lease_time + 1);
2897 else
2898 if (++clp->cl_id_uniquifier == 0)
2899 break;
2901 return status;
2904 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2906 struct nfs_fsinfo fsinfo;
2907 struct rpc_message msg = {
2908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2909 .rpc_argp = clp,
2910 .rpc_resp = &fsinfo,
2911 .rpc_cred = cred,
2913 unsigned long now;
2914 int status;
2916 now = jiffies;
2917 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2918 if (status == 0) {
2919 spin_lock(&clp->cl_lock);
2920 clp->cl_lease_time = fsinfo.lease_time * HZ;
2921 clp->cl_last_renewal = now;
2922 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2923 spin_unlock(&clp->cl_lock);
2925 return status;
2928 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2930 long timeout = 0;
2931 int err;
2932 do {
2933 err = _nfs4_proc_setclientid_confirm(clp, cred);
2934 switch (err) {
2935 case 0:
2936 return err;
2937 case -NFS4ERR_RESOURCE:
2938 /* The IBM lawyers misread another document! */
2939 case -NFS4ERR_DELAY:
2940 err = nfs4_delay(clp->cl_rpcclient, &timeout);
2942 } while (err == 0);
2943 return err;
2946 struct nfs4_delegreturndata {
2947 struct nfs4_delegreturnargs args;
2948 struct nfs4_delegreturnres res;
2949 struct nfs_fh fh;
2950 nfs4_stateid stateid;
2951 unsigned long timestamp;
2952 struct nfs_fattr fattr;
2953 int rpc_status;
2956 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
2958 struct nfs4_delegreturndata *data = calldata;
2959 data->rpc_status = task->tk_status;
2960 if (data->rpc_status == 0)
2961 renew_lease(data->res.server, data->timestamp);
2964 static void nfs4_delegreturn_release(void *calldata)
2966 kfree(calldata);
2969 static const struct rpc_call_ops nfs4_delegreturn_ops = {
2970 .rpc_call_done = nfs4_delegreturn_done,
2971 .rpc_release = nfs4_delegreturn_release,
2974 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
2976 struct nfs4_delegreturndata *data;
2977 struct nfs_server *server = NFS_SERVER(inode);
2978 struct rpc_task *task;
2979 struct rpc_message msg = {
2980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2981 .rpc_cred = cred,
2983 struct rpc_task_setup task_setup_data = {
2984 .rpc_client = server->client,
2985 .rpc_message = &msg,
2986 .callback_ops = &nfs4_delegreturn_ops,
2987 .flags = RPC_TASK_ASYNC,
2989 int status = 0;
2991 data = kmalloc(sizeof(*data), GFP_KERNEL);
2992 if (data == NULL)
2993 return -ENOMEM;
2994 data->args.fhandle = &data->fh;
2995 data->args.stateid = &data->stateid;
2996 data->args.bitmask = server->attr_bitmask;
2997 nfs_copy_fh(&data->fh, NFS_FH(inode));
2998 memcpy(&data->stateid, stateid, sizeof(data->stateid));
2999 data->res.fattr = &data->fattr;
3000 data->res.server = server;
3001 nfs_fattr_init(data->res.fattr);
3002 data->timestamp = jiffies;
3003 data->rpc_status = 0;
3005 task_setup_data.callback_data = data;
3006 msg.rpc_argp = &data->args,
3007 msg.rpc_resp = &data->res,
3008 task = rpc_run_task(&task_setup_data);
3009 if (IS_ERR(task))
3010 return PTR_ERR(task);
3011 if (!issync)
3012 goto out;
3013 status = nfs4_wait_for_completion_rpc_task(task);
3014 if (status != 0)
3015 goto out;
3016 status = data->rpc_status;
3017 if (status != 0)
3018 goto out;
3019 nfs_refresh_inode(inode, &data->fattr);
3020 out:
3021 rpc_put_task(task);
3022 return status;
3025 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3027 struct nfs_server *server = NFS_SERVER(inode);
3028 struct nfs4_exception exception = { };
3029 int err;
3030 do {
3031 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3032 switch (err) {
3033 case -NFS4ERR_STALE_STATEID:
3034 case -NFS4ERR_EXPIRED:
3035 case 0:
3036 return 0;
3038 err = nfs4_handle_exception(server, err, &exception);
3039 } while (exception.retry);
3040 return err;
3043 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3044 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3047 * sleep, with exponential backoff, and retry the LOCK operation.
3049 static unsigned long
3050 nfs4_set_lock_task_retry(unsigned long timeout)
3052 schedule_timeout_killable(timeout);
3053 timeout <<= 1;
3054 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3055 return NFS4_LOCK_MAXTIMEOUT;
3056 return timeout;
3059 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3061 struct inode *inode = state->inode;
3062 struct nfs_server *server = NFS_SERVER(inode);
3063 struct nfs_client *clp = server->nfs_client;
3064 struct nfs_lockt_args arg = {
3065 .fh = NFS_FH(inode),
3066 .fl = request,
3068 struct nfs_lockt_res res = {
3069 .denied = request,
3071 struct rpc_message msg = {
3072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3073 .rpc_argp = &arg,
3074 .rpc_resp = &res,
3075 .rpc_cred = state->owner->so_cred,
3077 struct nfs4_lock_state *lsp;
3078 int status;
3080 down_read(&clp->cl_sem);
3081 arg.lock_owner.clientid = clp->cl_clientid;
3082 status = nfs4_set_lock_state(state, request);
3083 if (status != 0)
3084 goto out;
3085 lsp = request->fl_u.nfs4_fl.owner;
3086 arg.lock_owner.id = lsp->ls_id.id;
3087 status = rpc_call_sync(server->client, &msg, 0);
3088 switch (status) {
3089 case 0:
3090 request->fl_type = F_UNLCK;
3091 break;
3092 case -NFS4ERR_DENIED:
3093 status = 0;
3095 request->fl_ops->fl_release_private(request);
3096 out:
3097 up_read(&clp->cl_sem);
3098 return status;
3101 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3103 struct nfs4_exception exception = { };
3104 int err;
3106 do {
3107 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3108 _nfs4_proc_getlk(state, cmd, request),
3109 &exception);
3110 } while (exception.retry);
3111 return err;
3114 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3116 int res = 0;
3117 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3118 case FL_POSIX:
3119 res = posix_lock_file_wait(file, fl);
3120 break;
3121 case FL_FLOCK:
3122 res = flock_lock_file_wait(file, fl);
3123 break;
3124 default:
3125 BUG();
3127 return res;
3130 struct nfs4_unlockdata {
3131 struct nfs_locku_args arg;
3132 struct nfs_locku_res res;
3133 struct nfs4_lock_state *lsp;
3134 struct nfs_open_context *ctx;
3135 struct file_lock fl;
3136 const struct nfs_server *server;
3137 unsigned long timestamp;
3140 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3141 struct nfs_open_context *ctx,
3142 struct nfs4_lock_state *lsp,
3143 struct nfs_seqid *seqid)
3145 struct nfs4_unlockdata *p;
3146 struct inode *inode = lsp->ls_state->inode;
3148 p = kmalloc(sizeof(*p), GFP_KERNEL);
3149 if (p == NULL)
3150 return NULL;
3151 p->arg.fh = NFS_FH(inode);
3152 p->arg.fl = &p->fl;
3153 p->arg.seqid = seqid;
3154 p->res.seqid = seqid;
3155 p->arg.stateid = &lsp->ls_stateid;
3156 p->lsp = lsp;
3157 atomic_inc(&lsp->ls_count);
3158 /* Ensure we don't close file until we're done freeing locks! */
3159 p->ctx = get_nfs_open_context(ctx);
3160 memcpy(&p->fl, fl, sizeof(p->fl));
3161 p->server = NFS_SERVER(inode);
3162 return p;
3165 static void nfs4_locku_release_calldata(void *data)
3167 struct nfs4_unlockdata *calldata = data;
3168 nfs_free_seqid(calldata->arg.seqid);
3169 nfs4_put_lock_state(calldata->lsp);
3170 put_nfs_open_context(calldata->ctx);
3171 kfree(calldata);
3174 static void nfs4_locku_done(struct rpc_task *task, void *data)
3176 struct nfs4_unlockdata *calldata = data;
3178 if (RPC_ASSASSINATED(task))
3179 return;
3180 switch (task->tk_status) {
3181 case 0:
3182 memcpy(calldata->lsp->ls_stateid.data,
3183 calldata->res.stateid.data,
3184 sizeof(calldata->lsp->ls_stateid.data));
3185 renew_lease(calldata->server, calldata->timestamp);
3186 break;
3187 case -NFS4ERR_STALE_STATEID:
3188 case -NFS4ERR_EXPIRED:
3189 break;
3190 default:
3191 if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
3192 rpc_restart_call(task);
3196 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3198 struct nfs4_unlockdata *calldata = data;
3200 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3201 return;
3202 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3203 /* Note: exit _without_ running nfs4_locku_done */
3204 task->tk_action = NULL;
3205 return;
3207 calldata->timestamp = jiffies;
3208 rpc_call_start(task);
3211 static const struct rpc_call_ops nfs4_locku_ops = {
3212 .rpc_call_prepare = nfs4_locku_prepare,
3213 .rpc_call_done = nfs4_locku_done,
3214 .rpc_release = nfs4_locku_release_calldata,
3217 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3218 struct nfs_open_context *ctx,
3219 struct nfs4_lock_state *lsp,
3220 struct nfs_seqid *seqid)
3222 struct nfs4_unlockdata *data;
3223 struct rpc_message msg = {
3224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3225 .rpc_cred = ctx->cred,
3227 struct rpc_task_setup task_setup_data = {
3228 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3229 .rpc_message = &msg,
3230 .callback_ops = &nfs4_locku_ops,
3231 .workqueue = nfsiod_workqueue,
3232 .flags = RPC_TASK_ASYNC,
3235 /* Ensure this is an unlock - when canceling a lock, the
3236 * canceled lock is passed in, and it won't be an unlock.
3238 fl->fl_type = F_UNLCK;
3240 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3241 if (data == NULL) {
3242 nfs_free_seqid(seqid);
3243 return ERR_PTR(-ENOMEM);
3246 msg.rpc_argp = &data->arg,
3247 msg.rpc_resp = &data->res,
3248 task_setup_data.callback_data = data;
3249 return rpc_run_task(&task_setup_data);
3252 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3254 struct nfs_seqid *seqid;
3255 struct nfs4_lock_state *lsp;
3256 struct rpc_task *task;
3257 int status = 0;
3258 unsigned char fl_flags = request->fl_flags;
3260 status = nfs4_set_lock_state(state, request);
3261 /* Unlock _before_ we do the RPC call */
3262 request->fl_flags |= FL_EXISTS;
3263 if (do_vfs_lock(request->fl_file, request) == -ENOENT)
3264 goto out;
3265 if (status != 0)
3266 goto out;
3267 /* Is this a delegated lock? */
3268 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3269 goto out;
3270 lsp = request->fl_u.nfs4_fl.owner;
3271 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3272 status = -ENOMEM;
3273 if (seqid == NULL)
3274 goto out;
3275 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3276 status = PTR_ERR(task);
3277 if (IS_ERR(task))
3278 goto out;
3279 status = nfs4_wait_for_completion_rpc_task(task);
3280 rpc_put_task(task);
3281 out:
3282 request->fl_flags = fl_flags;
3283 return status;
3286 struct nfs4_lockdata {
3287 struct nfs_lock_args arg;
3288 struct nfs_lock_res res;
3289 struct nfs4_lock_state *lsp;
3290 struct nfs_open_context *ctx;
3291 struct file_lock fl;
3292 unsigned long timestamp;
3293 int rpc_status;
3294 int cancelled;
3297 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3298 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3300 struct nfs4_lockdata *p;
3301 struct inode *inode = lsp->ls_state->inode;
3302 struct nfs_server *server = NFS_SERVER(inode);
3304 p = kzalloc(sizeof(*p), GFP_KERNEL);
3305 if (p == NULL)
3306 return NULL;
3308 p->arg.fh = NFS_FH(inode);
3309 p->arg.fl = &p->fl;
3310 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3311 if (p->arg.open_seqid == NULL)
3312 goto out_free;
3313 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3314 if (p->arg.lock_seqid == NULL)
3315 goto out_free_seqid;
3316 p->arg.lock_stateid = &lsp->ls_stateid;
3317 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3318 p->arg.lock_owner.id = lsp->ls_id.id;
3319 p->res.lock_seqid = p->arg.lock_seqid;
3320 p->lsp = lsp;
3321 atomic_inc(&lsp->ls_count);
3322 p->ctx = get_nfs_open_context(ctx);
3323 memcpy(&p->fl, fl, sizeof(p->fl));
3324 return p;
3325 out_free_seqid:
3326 nfs_free_seqid(p->arg.open_seqid);
3327 out_free:
3328 kfree(p);
3329 return NULL;
3332 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3334 struct nfs4_lockdata *data = calldata;
3335 struct nfs4_state *state = data->lsp->ls_state;
3337 dprintk("%s: begin!\n", __func__);
3338 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3339 return;
3340 /* Do we need to do an open_to_lock_owner? */
3341 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3342 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3343 return;
3344 data->arg.open_stateid = &state->stateid;
3345 data->arg.new_lock_owner = 1;
3346 data->res.open_seqid = data->arg.open_seqid;
3347 } else
3348 data->arg.new_lock_owner = 0;
3349 data->timestamp = jiffies;
3350 rpc_call_start(task);
3351 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3354 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3356 struct nfs4_lockdata *data = calldata;
3358 dprintk("%s: begin!\n", __func__);
3360 data->rpc_status = task->tk_status;
3361 if (RPC_ASSASSINATED(task))
3362 goto out;
3363 if (data->arg.new_lock_owner != 0) {
3364 if (data->rpc_status == 0)
3365 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3366 else
3367 goto out;
3369 if (data->rpc_status == 0) {
3370 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3371 sizeof(data->lsp->ls_stateid.data));
3372 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3373 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3375 out:
3376 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3379 static void nfs4_lock_release(void *calldata)
3381 struct nfs4_lockdata *data = calldata;
3383 dprintk("%s: begin!\n", __func__);
3384 nfs_free_seqid(data->arg.open_seqid);
3385 if (data->cancelled != 0) {
3386 struct rpc_task *task;
3387 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3388 data->arg.lock_seqid);
3389 if (!IS_ERR(task))
3390 rpc_put_task(task);
3391 dprintk("%s: cancelling lock!\n", __func__);
3392 } else
3393 nfs_free_seqid(data->arg.lock_seqid);
3394 nfs4_put_lock_state(data->lsp);
3395 put_nfs_open_context(data->ctx);
3396 kfree(data);
3397 dprintk("%s: done!\n", __func__);
3400 static const struct rpc_call_ops nfs4_lock_ops = {
3401 .rpc_call_prepare = nfs4_lock_prepare,
3402 .rpc_call_done = nfs4_lock_done,
3403 .rpc_release = nfs4_lock_release,
3406 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3408 struct nfs4_lockdata *data;
3409 struct rpc_task *task;
3410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3412 .rpc_cred = state->owner->so_cred,
3414 struct rpc_task_setup task_setup_data = {
3415 .rpc_client = NFS_CLIENT(state->inode),
3416 .rpc_message = &msg,
3417 .callback_ops = &nfs4_lock_ops,
3418 .workqueue = nfsiod_workqueue,
3419 .flags = RPC_TASK_ASYNC,
3421 int ret;
3423 dprintk("%s: begin!\n", __func__);
3424 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3425 fl->fl_u.nfs4_fl.owner);
3426 if (data == NULL)
3427 return -ENOMEM;
3428 if (IS_SETLKW(cmd))
3429 data->arg.block = 1;
3430 if (reclaim != 0)
3431 data->arg.reclaim = 1;
3432 msg.rpc_argp = &data->arg,
3433 msg.rpc_resp = &data->res,
3434 task_setup_data.callback_data = data;
3435 task = rpc_run_task(&task_setup_data);
3436 if (IS_ERR(task))
3437 return PTR_ERR(task);
3438 ret = nfs4_wait_for_completion_rpc_task(task);
3439 if (ret == 0) {
3440 ret = data->rpc_status;
3441 if (ret == -NFS4ERR_DENIED)
3442 ret = -EAGAIN;
3443 } else
3444 data->cancelled = 1;
3445 rpc_put_task(task);
3446 dprintk("%s: done, ret = %d!\n", __func__, ret);
3447 return ret;
3450 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3452 struct nfs_server *server = NFS_SERVER(state->inode);
3453 struct nfs4_exception exception = { };
3454 int err;
3456 do {
3457 /* Cache the lock if possible... */
3458 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3459 return 0;
3460 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3461 if (err != -NFS4ERR_DELAY)
3462 break;
3463 nfs4_handle_exception(server, err, &exception);
3464 } while (exception.retry);
3465 return err;
3468 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3470 struct nfs_server *server = NFS_SERVER(state->inode);
3471 struct nfs4_exception exception = { };
3472 int err;
3474 err = nfs4_set_lock_state(state, request);
3475 if (err != 0)
3476 return err;
3477 do {
3478 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3479 return 0;
3480 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3481 if (err != -NFS4ERR_DELAY)
3482 break;
3483 nfs4_handle_exception(server, err, &exception);
3484 } while (exception.retry);
3485 return err;
3488 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3490 struct nfs_client *clp = state->owner->so_client;
3491 unsigned char fl_flags = request->fl_flags;
3492 int status;
3494 /* Is this a delegated open? */
3495 status = nfs4_set_lock_state(state, request);
3496 if (status != 0)
3497 goto out;
3498 request->fl_flags |= FL_ACCESS;
3499 status = do_vfs_lock(request->fl_file, request);
3500 if (status < 0)
3501 goto out;
3502 down_read(&clp->cl_sem);
3503 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3504 struct nfs_inode *nfsi = NFS_I(state->inode);
3505 /* Yes: cache locks! */
3506 down_read(&nfsi->rwsem);
3507 /* ...but avoid races with delegation recall... */
3508 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3509 request->fl_flags = fl_flags & ~FL_SLEEP;
3510 status = do_vfs_lock(request->fl_file, request);
3511 up_read(&nfsi->rwsem);
3512 goto out_unlock;
3514 up_read(&nfsi->rwsem);
3516 status = _nfs4_do_setlk(state, cmd, request, 0);
3517 if (status != 0)
3518 goto out_unlock;
3519 /* Note: we always want to sleep here! */
3520 request->fl_flags = fl_flags | FL_SLEEP;
3521 if (do_vfs_lock(request->fl_file, request) < 0)
3522 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3523 out_unlock:
3524 up_read(&clp->cl_sem);
3525 out:
3526 request->fl_flags = fl_flags;
3527 return status;
3530 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3532 struct nfs4_exception exception = { };
3533 int err;
3535 do {
3536 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3537 _nfs4_proc_setlk(state, cmd, request),
3538 &exception);
3539 } while (exception.retry);
3540 return err;
3543 static int
3544 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3546 struct nfs_open_context *ctx;
3547 struct nfs4_state *state;
3548 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3549 int status;
3551 /* verify open state */
3552 ctx = nfs_file_open_context(filp);
3553 state = ctx->state;
3555 if (request->fl_start < 0 || request->fl_end < 0)
3556 return -EINVAL;
3558 if (IS_GETLK(cmd)) {
3559 if (state != NULL)
3560 return nfs4_proc_getlk(state, F_GETLK, request);
3561 return 0;
3564 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3565 return -EINVAL;
3567 if (request->fl_type == F_UNLCK) {
3568 if (state != NULL)
3569 return nfs4_proc_unlck(state, cmd, request);
3570 return 0;
3573 if (state == NULL)
3574 return -ENOLCK;
3575 do {
3576 status = nfs4_proc_setlk(state, cmd, request);
3577 if ((status != -EAGAIN) || IS_SETLK(cmd))
3578 break;
3579 timeout = nfs4_set_lock_task_retry(timeout);
3580 status = -ERESTARTSYS;
3581 if (signalled())
3582 break;
3583 } while(status < 0);
3584 return status;
3587 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3589 struct nfs_server *server = NFS_SERVER(state->inode);
3590 struct nfs4_exception exception = { };
3591 int err;
3593 err = nfs4_set_lock_state(state, fl);
3594 if (err != 0)
3595 goto out;
3596 do {
3597 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3598 if (err != -NFS4ERR_DELAY)
3599 break;
3600 err = nfs4_handle_exception(server, err, &exception);
3601 } while (exception.retry);
3602 out:
3603 return err;
3606 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3608 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3609 size_t buflen, int flags)
3611 struct inode *inode = dentry->d_inode;
3613 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3614 return -EOPNOTSUPP;
3616 return nfs4_proc_set_acl(inode, buf, buflen);
3619 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3620 * and that's what we'll do for e.g. user attributes that haven't been set.
3621 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3622 * attributes in kernel-managed attribute namespaces. */
3623 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3624 size_t buflen)
3626 struct inode *inode = dentry->d_inode;
3628 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3629 return -EOPNOTSUPP;
3631 return nfs4_proc_get_acl(inode, buf, buflen);
3634 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3636 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3638 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3639 return 0;
3640 if (buf && buflen < len)
3641 return -ERANGE;
3642 if (buf)
3643 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3644 return len;
3647 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3648 struct nfs4_fs_locations *fs_locations, struct page *page)
3650 struct nfs_server *server = NFS_SERVER(dir);
3651 u32 bitmask[2] = {
3652 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3653 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3655 struct nfs4_fs_locations_arg args = {
3656 .dir_fh = NFS_FH(dir),
3657 .name = name,
3658 .page = page,
3659 .bitmask = bitmask,
3661 struct rpc_message msg = {
3662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3663 .rpc_argp = &args,
3664 .rpc_resp = fs_locations,
3666 int status;
3668 dprintk("%s: start\n", __func__);
3669 nfs_fattr_init(&fs_locations->fattr);
3670 fs_locations->server = server;
3671 fs_locations->nlocations = 0;
3672 status = rpc_call_sync(server->client, &msg, 0);
3673 dprintk("%s: returned status = %d\n", __func__, status);
3674 return status;
3677 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3678 .recover_open = nfs4_open_reclaim,
3679 .recover_lock = nfs4_lock_reclaim,
3682 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3683 .recover_open = nfs4_open_expired,
3684 .recover_lock = nfs4_lock_expired,
3687 static const struct inode_operations nfs4_file_inode_operations = {
3688 .permission = nfs_permission,
3689 .getattr = nfs_getattr,
3690 .setattr = nfs_setattr,
3691 .getxattr = nfs4_getxattr,
3692 .setxattr = nfs4_setxattr,
3693 .listxattr = nfs4_listxattr,
3696 const struct nfs_rpc_ops nfs_v4_clientops = {
3697 .version = 4, /* protocol version */
3698 .dentry_ops = &nfs4_dentry_operations,
3699 .dir_inode_ops = &nfs4_dir_inode_operations,
3700 .file_inode_ops = &nfs4_file_inode_operations,
3701 .getroot = nfs4_proc_get_root,
3702 .getattr = nfs4_proc_getattr,
3703 .setattr = nfs4_proc_setattr,
3704 .lookupfh = nfs4_proc_lookupfh,
3705 .lookup = nfs4_proc_lookup,
3706 .access = nfs4_proc_access,
3707 .readlink = nfs4_proc_readlink,
3708 .create = nfs4_proc_create,
3709 .remove = nfs4_proc_remove,
3710 .unlink_setup = nfs4_proc_unlink_setup,
3711 .unlink_done = nfs4_proc_unlink_done,
3712 .rename = nfs4_proc_rename,
3713 .link = nfs4_proc_link,
3714 .symlink = nfs4_proc_symlink,
3715 .mkdir = nfs4_proc_mkdir,
3716 .rmdir = nfs4_proc_remove,
3717 .readdir = nfs4_proc_readdir,
3718 .mknod = nfs4_proc_mknod,
3719 .statfs = nfs4_proc_statfs,
3720 .fsinfo = nfs4_proc_fsinfo,
3721 .pathconf = nfs4_proc_pathconf,
3722 .set_capabilities = nfs4_server_capabilities,
3723 .decode_dirent = nfs4_decode_dirent,
3724 .read_setup = nfs4_proc_read_setup,
3725 .read_done = nfs4_read_done,
3726 .write_setup = nfs4_proc_write_setup,
3727 .write_done = nfs4_write_done,
3728 .commit_setup = nfs4_proc_commit_setup,
3729 .commit_done = nfs4_commit_done,
3730 .lock = nfs4_proc_lock,
3731 .clear_acl_cache = nfs4_zap_acl_attr,
3735 * Local variables:
3736 * c-basic-offset: 8
3737 * End: