4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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
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.
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>
53 #include "delegation.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
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 *, struct nfs4_state *);
66 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69 /* Prevent leaks of NFSv4 errors into userland */
70 static int nfs4_map_errors(int err)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap[2] = {
88 | FATTR4_WORD0_FILEID,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap[2] = {
111 | FATTR4_WORD0_MAXNAME,
115 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
122 const u32 nfs4_fs_locations_bitmap[2] = {
124 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
141 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
142 struct nfs4_readdir_arg *readdir)
146 BUG_ON(readdir->count < 80);
148 readdir->cookie = cookie;
149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
196 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
203 nfs_wait_bit_killable, TASK_KILLABLE);
207 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
224 /* This is the error handling routine for processes that are allowed
227 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
229 struct nfs_client *clp = server->nfs_client;
230 struct nfs4_state *state = exception->state;
233 exception->retry = 0;
237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
242 nfs4_state_mark_reclaim_nograce(clp, state);
243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
249 exception->retry = 1;
251 case -NFS4ERR_FILE_OPEN:
254 ret = nfs4_delay(server->client, &exception->timeout);
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
265 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
267 struct nfs_client *clp = server->nfs_client;
268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
274 #if defined(CONFIG_NFS_V4_1)
277 * nfs4_free_slot - free a slot and efficiently update slot table.
279 * freeing a slot is trivially done by clearing its respective bit
281 * If the freed slotid equals highest_used_slotid we want to update it
282 * so that the server would be able to size down the slot table if needed,
283 * otherwise we know that the highest_used_slotid is still in use.
284 * When updating highest_used_slotid there may be "holes" in the bitmap
285 * so we need to scan down from highest_used_slotid to 0 looking for the now
286 * highest slotid in use.
287 * If none found, highest_used_slotid is set to -1.
290 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
292 int slotid = free_slotid;
294 spin_lock(&tbl->slot_tbl_lock);
295 /* clear used bit in bitmap */
296 __clear_bit(slotid, tbl->used_slots);
298 /* update highest_used_slotid when it is freed */
299 if (slotid == tbl->highest_used_slotid) {
300 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
301 if (slotid >= 0 && slotid < tbl->max_slots)
302 tbl->highest_used_slotid = slotid;
304 tbl->highest_used_slotid = -1;
306 rpc_wake_up_next(&tbl->slot_tbl_waitq);
307 spin_unlock(&tbl->slot_tbl_lock);
308 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
309 free_slotid, tbl->highest_used_slotid);
312 void nfs41_sequence_free_slot(const struct nfs_client *clp,
313 struct nfs4_sequence_res *res)
315 struct nfs4_slot_table *tbl;
317 if (!nfs4_has_session(clp)) {
318 dprintk("%s: No session\n", __func__);
321 tbl = &clp->cl_session->fc_slot_table;
322 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
323 dprintk("%s: No slot\n", __func__);
324 /* just wake up the next guy waiting since
325 * we may have not consumed a slot after all */
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
329 nfs4_free_slot(tbl, res->sr_slotid);
330 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
334 * nfs4_find_slot - efficiently look for a free slot
336 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
337 * If found, we mark the slot as used, update the highest_used_slotid,
338 * and respectively set up the sequence operation args.
339 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
341 * Note: must be called with under the slot_tbl_lock.
344 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
347 u8 ret_id = NFS4_MAX_SLOT_TABLE;
348 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
350 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
351 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
353 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
354 if (slotid >= tbl->max_slots)
356 __set_bit(slotid, tbl->used_slots);
357 if (slotid > tbl->highest_used_slotid)
358 tbl->highest_used_slotid = slotid;
361 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
362 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
366 static int nfs41_setup_sequence(struct nfs4_session *session,
367 struct nfs4_sequence_args *args,
368 struct nfs4_sequence_res *res,
370 struct rpc_task *task)
372 struct nfs4_slot *slot;
373 struct nfs4_slot_table *tbl;
376 dprintk("--> %s\n", __func__);
377 /* slot already allocated? */
378 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
381 memset(res, 0, sizeof(*res));
382 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
383 tbl = &session->fc_slot_table;
385 spin_lock(&tbl->slot_tbl_lock);
386 slotid = nfs4_find_slot(tbl, task);
387 if (slotid == NFS4_MAX_SLOT_TABLE) {
388 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
389 spin_unlock(&tbl->slot_tbl_lock);
390 dprintk("<-- %s: no free slots\n", __func__);
393 spin_unlock(&tbl->slot_tbl_lock);
395 slot = tbl->slots + slotid;
396 args->sa_slotid = slotid;
397 args->sa_cache_this = cache_reply;
399 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
401 res->sr_slotid = slotid;
402 res->sr_renewal_time = jiffies;
404 * sr_status is only set in decode_sequence, and so will remain
405 * set to 1 if an rpc level failure occurs.
411 int nfs4_setup_sequence(struct nfs_client *clp,
412 struct nfs4_sequence_args *args,
413 struct nfs4_sequence_res *res,
415 struct rpc_task *task)
419 dprintk("--> %s clp %p session %p sr_slotid %d\n",
420 __func__, clp, clp->cl_session, res->sr_slotid);
422 if (!nfs4_has_session(clp))
424 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
426 if (ret != -EAGAIN) {
427 /* terminate rpc task */
428 task->tk_status = ret;
429 task->tk_action = NULL;
432 dprintk("<-- %s status=%d\n", __func__, ret);
436 struct nfs41_call_sync_data {
437 struct nfs_client *clp;
438 struct nfs4_sequence_args *seq_args;
439 struct nfs4_sequence_res *seq_res;
443 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
445 struct nfs41_call_sync_data *data = calldata;
447 dprintk("--> %s data->clp->cl_session %p\n", __func__,
448 data->clp->cl_session);
449 if (nfs4_setup_sequence(data->clp, data->seq_args,
450 data->seq_res, data->cache_reply, task))
452 rpc_call_start(task);
455 struct rpc_call_ops nfs41_call_sync_ops = {
456 .rpc_call_prepare = nfs41_call_sync_prepare,
459 static int nfs4_call_sync_sequence(struct nfs_client *clp,
460 struct rpc_clnt *clnt,
461 struct rpc_message *msg,
462 struct nfs4_sequence_args *args,
463 struct nfs4_sequence_res *res,
467 struct rpc_task *task;
468 struct nfs41_call_sync_data data = {
472 .cache_reply = cache_reply,
474 struct rpc_task_setup task_setup = {
477 .callback_ops = &nfs41_call_sync_ops,
478 .callback_data = &data
481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
482 task = rpc_run_task(&task_setup);
486 ret = task->tk_status;
492 int _nfs4_call_sync_session(struct nfs_server *server,
493 struct rpc_message *msg,
494 struct nfs4_sequence_args *args,
495 struct nfs4_sequence_res *res,
498 return nfs4_call_sync_sequence(server->nfs_client, server->client,
499 msg, args, res, cache_reply);
502 #endif /* CONFIG_NFS_V4_1 */
504 int _nfs4_call_sync(struct nfs_server *server,
505 struct rpc_message *msg,
506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
510 args->sa_session = res->sr_session = NULL;
511 return rpc_call_sync(server->client, msg, 0);
514 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
515 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
516 &(res)->seq_res, (cache_reply))
518 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
520 struct nfs_inode *nfsi = NFS_I(dir);
522 spin_lock(&dir->i_lock);
523 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
524 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
525 nfs_force_lookup_revalidate(dir);
526 nfsi->change_attr = cinfo->after;
527 spin_unlock(&dir->i_lock);
530 struct nfs4_opendata {
532 struct nfs_openargs o_arg;
533 struct nfs_openres o_res;
534 struct nfs_open_confirmargs c_arg;
535 struct nfs_open_confirmres c_res;
536 struct nfs_fattr f_attr;
537 struct nfs_fattr dir_attr;
540 struct nfs4_state_owner *owner;
541 struct nfs4_state *state;
543 unsigned long timestamp;
544 unsigned int rpc_done : 1;
550 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
552 p->o_res.f_attr = &p->f_attr;
553 p->o_res.dir_attr = &p->dir_attr;
554 p->o_res.seqid = p->o_arg.seqid;
555 p->c_res.seqid = p->c_arg.seqid;
556 p->o_res.server = p->o_arg.server;
557 nfs_fattr_init(&p->f_attr);
558 nfs_fattr_init(&p->dir_attr);
561 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
562 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
563 const struct iattr *attrs)
565 struct dentry *parent = dget_parent(path->dentry);
566 struct inode *dir = parent->d_inode;
567 struct nfs_server *server = NFS_SERVER(dir);
568 struct nfs4_opendata *p;
570 p = kzalloc(sizeof(*p), GFP_KERNEL);
573 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
574 if (p->o_arg.seqid == NULL)
576 p->path.mnt = mntget(path->mnt);
577 p->path.dentry = dget(path->dentry);
580 atomic_inc(&sp->so_count);
581 p->o_arg.fh = NFS_FH(dir);
582 p->o_arg.open_flags = flags;
583 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
584 p->o_arg.clientid = server->nfs_client->cl_clientid;
585 p->o_arg.id = sp->so_owner_id.id;
586 p->o_arg.name = &p->path.dentry->d_name;
587 p->o_arg.server = server;
588 p->o_arg.bitmask = server->attr_bitmask;
589 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
590 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
591 if (flags & O_EXCL) {
592 u32 *s = (u32 *) p->o_arg.u.verifier.data;
595 } else if (flags & O_CREAT) {
596 p->o_arg.u.attrs = &p->attrs;
597 memcpy(&p->attrs, attrs, sizeof(p->attrs));
599 p->c_arg.fh = &p->o_res.fh;
600 p->c_arg.stateid = &p->o_res.stateid;
601 p->c_arg.seqid = p->o_arg.seqid;
602 nfs4_init_opendata_res(p);
612 static void nfs4_opendata_free(struct kref *kref)
614 struct nfs4_opendata *p = container_of(kref,
615 struct nfs4_opendata, kref);
617 nfs_free_seqid(p->o_arg.seqid);
618 if (p->state != NULL)
619 nfs4_put_open_state(p->state);
620 nfs4_put_state_owner(p->owner);
626 static void nfs4_opendata_put(struct nfs4_opendata *p)
629 kref_put(&p->kref, nfs4_opendata_free);
632 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
636 ret = rpc_wait_for_completion_task(task);
640 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
644 if (open_mode & O_EXCL)
646 switch (mode & (FMODE_READ|FMODE_WRITE)) {
648 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
651 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
653 case FMODE_READ|FMODE_WRITE:
654 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
660 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
662 if ((delegation->type & fmode) != fmode)
664 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
666 nfs_mark_delegation_referenced(delegation);
670 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
679 case FMODE_READ|FMODE_WRITE:
682 nfs4_state_set_mode_locked(state, state->state | fmode);
685 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
687 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
688 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
689 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
692 set_bit(NFS_O_RDONLY_STATE, &state->flags);
695 set_bit(NFS_O_WRONLY_STATE, &state->flags);
697 case FMODE_READ|FMODE_WRITE:
698 set_bit(NFS_O_RDWR_STATE, &state->flags);
702 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
704 write_seqlock(&state->seqlock);
705 nfs_set_open_stateid_locked(state, stateid, fmode);
706 write_sequnlock(&state->seqlock);
709 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
712 * Protect the call to nfs4_state_set_mode_locked and
713 * serialise the stateid update
715 write_seqlock(&state->seqlock);
716 if (deleg_stateid != NULL) {
717 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
718 set_bit(NFS_DELEGATED_STATE, &state->flags);
720 if (open_stateid != NULL)
721 nfs_set_open_stateid_locked(state, open_stateid, fmode);
722 write_sequnlock(&state->seqlock);
723 spin_lock(&state->owner->so_lock);
724 update_open_stateflags(state, fmode);
725 spin_unlock(&state->owner->so_lock);
728 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
730 struct nfs_inode *nfsi = NFS_I(state->inode);
731 struct nfs_delegation *deleg_cur;
734 fmode &= (FMODE_READ|FMODE_WRITE);
737 deleg_cur = rcu_dereference(nfsi->delegation);
738 if (deleg_cur == NULL)
741 spin_lock(&deleg_cur->lock);
742 if (nfsi->delegation != deleg_cur ||
743 (deleg_cur->type & fmode) != fmode)
744 goto no_delegation_unlock;
746 if (delegation == NULL)
747 delegation = &deleg_cur->stateid;
748 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
749 goto no_delegation_unlock;
751 nfs_mark_delegation_referenced(deleg_cur);
752 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
754 no_delegation_unlock:
755 spin_unlock(&deleg_cur->lock);
759 if (!ret && open_stateid != NULL) {
760 __update_open_stateid(state, open_stateid, NULL, fmode);
768 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
770 struct nfs_delegation *delegation;
773 delegation = rcu_dereference(NFS_I(inode)->delegation);
774 if (delegation == NULL || (delegation->type & fmode) == fmode) {
779 nfs_inode_return_delegation(inode);
782 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
784 struct nfs4_state *state = opendata->state;
785 struct nfs_inode *nfsi = NFS_I(state->inode);
786 struct nfs_delegation *delegation;
787 int open_mode = opendata->o_arg.open_flags & O_EXCL;
788 fmode_t fmode = opendata->o_arg.fmode;
789 nfs4_stateid stateid;
793 if (can_open_cached(state, fmode, open_mode)) {
794 spin_lock(&state->owner->so_lock);
795 if (can_open_cached(state, fmode, open_mode)) {
796 update_open_stateflags(state, fmode);
797 spin_unlock(&state->owner->so_lock);
798 goto out_return_state;
800 spin_unlock(&state->owner->so_lock);
803 delegation = rcu_dereference(nfsi->delegation);
804 if (delegation == NULL ||
805 !can_open_delegated(delegation, fmode)) {
809 /* Save the delegation */
810 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
812 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
817 /* Try to update the stateid using the delegation */
818 if (update_open_stateid(state, NULL, &stateid, fmode))
819 goto out_return_state;
824 atomic_inc(&state->count);
828 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
831 struct nfs4_state *state = NULL;
832 struct nfs_delegation *delegation;
835 if (!data->rpc_done) {
836 state = nfs4_try_open_cached(data);
841 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
843 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
844 ret = PTR_ERR(inode);
848 state = nfs4_get_open_state(inode, data->owner);
851 if (data->o_res.delegation_type != 0) {
852 int delegation_flags = 0;
855 delegation = rcu_dereference(NFS_I(inode)->delegation);
857 delegation_flags = delegation->flags;
859 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
860 nfs_inode_set_delegation(state->inode,
861 data->owner->so_cred,
864 nfs_inode_reclaim_delegation(state->inode,
865 data->owner->so_cred,
869 update_open_stateid(state, &data->o_res.stateid, NULL,
880 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
882 struct nfs_inode *nfsi = NFS_I(state->inode);
883 struct nfs_open_context *ctx;
885 spin_lock(&state->inode->i_lock);
886 list_for_each_entry(ctx, &nfsi->open_files, list) {
887 if (ctx->state != state)
889 get_nfs_open_context(ctx);
890 spin_unlock(&state->inode->i_lock);
893 spin_unlock(&state->inode->i_lock);
894 return ERR_PTR(-ENOENT);
897 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
899 struct nfs4_opendata *opendata;
901 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
902 if (opendata == NULL)
903 return ERR_PTR(-ENOMEM);
904 opendata->state = state;
905 atomic_inc(&state->count);
909 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
911 struct nfs4_state *newstate;
914 opendata->o_arg.open_flags = 0;
915 opendata->o_arg.fmode = fmode;
916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
918 nfs4_init_opendata_res(opendata);
919 ret = _nfs4_proc_open(opendata);
922 newstate = nfs4_opendata_to_nfs4_state(opendata);
923 if (IS_ERR(newstate))
924 return PTR_ERR(newstate);
925 nfs4_close_state(&opendata->path, newstate, fmode);
930 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
932 struct nfs4_state *newstate;
935 /* memory barrier prior to reading state->n_* */
936 clear_bit(NFS_DELEGATED_STATE, &state->flags);
938 if (state->n_rdwr != 0) {
939 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
942 if (newstate != state)
945 if (state->n_wronly != 0) {
946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
949 if (newstate != state)
952 if (state->n_rdonly != 0) {
953 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
956 if (newstate != state)
960 * We may have performed cached opens for all three recoveries.
961 * Check if we need to update the current stateid.
963 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
964 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
965 write_seqlock(&state->seqlock);
966 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
967 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
968 write_sequnlock(&state->seqlock);
975 * reclaim state on the server after a reboot.
977 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
979 struct nfs_delegation *delegation;
980 struct nfs4_opendata *opendata;
981 fmode_t delegation_type = 0;
984 opendata = nfs4_open_recoverdata_alloc(ctx, state);
985 if (IS_ERR(opendata))
986 return PTR_ERR(opendata);
987 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
988 opendata->o_arg.fh = NFS_FH(state->inode);
990 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
991 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
992 delegation_type = delegation->type;
994 opendata->o_arg.u.delegation_type = delegation_type;
995 status = nfs4_open_recover(opendata, state);
996 nfs4_opendata_put(opendata);
1000 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1002 struct nfs_server *server = NFS_SERVER(state->inode);
1003 struct nfs4_exception exception = { };
1006 err = _nfs4_do_open_reclaim(ctx, state);
1007 if (err != -NFS4ERR_DELAY)
1009 nfs4_handle_exception(server, err, &exception);
1010 } while (exception.retry);
1014 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1016 struct nfs_open_context *ctx;
1019 ctx = nfs4_state_find_open_context(state);
1021 return PTR_ERR(ctx);
1022 ret = nfs4_do_open_reclaim(ctx, state);
1023 put_nfs_open_context(ctx);
1027 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1029 struct nfs4_opendata *opendata;
1032 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1033 if (IS_ERR(opendata))
1034 return PTR_ERR(opendata);
1035 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1036 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1037 sizeof(opendata->o_arg.u.delegation.data));
1038 ret = nfs4_open_recover(opendata, state);
1039 nfs4_opendata_put(opendata);
1043 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1045 struct nfs4_exception exception = { };
1046 struct nfs_server *server = NFS_SERVER(state->inode);
1049 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1053 case -NFS4ERR_STALE_CLIENTID:
1054 case -NFS4ERR_STALE_STATEID:
1055 case -NFS4ERR_EXPIRED:
1056 /* Don't recall a delegation if it was lost */
1057 nfs4_schedule_state_recovery(server->nfs_client);
1060 err = nfs4_handle_exception(server, err, &exception);
1061 } while (exception.retry);
1065 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1067 struct nfs4_opendata *data = calldata;
1069 data->rpc_status = task->tk_status;
1070 if (RPC_ASSASSINATED(task))
1072 if (data->rpc_status == 0) {
1073 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1074 sizeof(data->o_res.stateid.data));
1075 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1076 renew_lease(data->o_res.server, data->timestamp);
1081 static void nfs4_open_confirm_release(void *calldata)
1083 struct nfs4_opendata *data = calldata;
1084 struct nfs4_state *state = NULL;
1086 /* If this request hasn't been cancelled, do nothing */
1087 if (data->cancelled == 0)
1089 /* In case of error, no cleanup! */
1090 if (!data->rpc_done)
1092 state = nfs4_opendata_to_nfs4_state(data);
1094 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1096 nfs4_opendata_put(data);
1099 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1100 .rpc_call_done = nfs4_open_confirm_done,
1101 .rpc_release = nfs4_open_confirm_release,
1105 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1107 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1109 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1110 struct rpc_task *task;
1111 struct rpc_message msg = {
1112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1113 .rpc_argp = &data->c_arg,
1114 .rpc_resp = &data->c_res,
1115 .rpc_cred = data->owner->so_cred,
1117 struct rpc_task_setup task_setup_data = {
1118 .rpc_client = server->client,
1119 .rpc_message = &msg,
1120 .callback_ops = &nfs4_open_confirm_ops,
1121 .callback_data = data,
1122 .workqueue = nfsiod_workqueue,
1123 .flags = RPC_TASK_ASYNC,
1127 kref_get(&data->kref);
1129 data->rpc_status = 0;
1130 data->timestamp = jiffies;
1131 task = rpc_run_task(&task_setup_data);
1133 return PTR_ERR(task);
1134 status = nfs4_wait_for_completion_rpc_task(task);
1136 data->cancelled = 1;
1139 status = data->rpc_status;
1144 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1146 struct nfs4_opendata *data = calldata;
1147 struct nfs4_state_owner *sp = data->owner;
1149 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1152 * Check if we still need to send an OPEN call, or if we can use
1153 * a delegation instead.
1155 if (data->state != NULL) {
1156 struct nfs_delegation *delegation;
1158 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1161 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1162 if (delegation != NULL &&
1163 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1169 /* Update sequence id. */
1170 data->o_arg.id = sp->so_owner_id.id;
1171 data->o_arg.clientid = sp->so_client->cl_clientid;
1172 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1173 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1174 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1176 data->timestamp = jiffies;
1177 rpc_call_start(task);
1180 task->tk_action = NULL;
1184 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1186 struct nfs4_opendata *data = calldata;
1188 data->rpc_status = task->tk_status;
1189 if (RPC_ASSASSINATED(task))
1191 if (task->tk_status == 0) {
1192 switch (data->o_res.f_attr->mode & S_IFMT) {
1196 data->rpc_status = -ELOOP;
1199 data->rpc_status = -EISDIR;
1202 data->rpc_status = -ENOTDIR;
1204 renew_lease(data->o_res.server, data->timestamp);
1205 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1206 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1211 static void nfs4_open_release(void *calldata)
1213 struct nfs4_opendata *data = calldata;
1214 struct nfs4_state *state = NULL;
1216 /* If this request hasn't been cancelled, do nothing */
1217 if (data->cancelled == 0)
1219 /* In case of error, no cleanup! */
1220 if (data->rpc_status != 0 || !data->rpc_done)
1222 /* In case we need an open_confirm, no cleanup! */
1223 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1225 state = nfs4_opendata_to_nfs4_state(data);
1227 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1229 nfs4_opendata_put(data);
1232 static const struct rpc_call_ops nfs4_open_ops = {
1233 .rpc_call_prepare = nfs4_open_prepare,
1234 .rpc_call_done = nfs4_open_done,
1235 .rpc_release = nfs4_open_release,
1239 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1241 static int _nfs4_proc_open(struct nfs4_opendata *data)
1243 struct inode *dir = data->dir->d_inode;
1244 struct nfs_server *server = NFS_SERVER(dir);
1245 struct nfs_openargs *o_arg = &data->o_arg;
1246 struct nfs_openres *o_res = &data->o_res;
1247 struct rpc_task *task;
1248 struct rpc_message msg = {
1249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1252 .rpc_cred = data->owner->so_cred,
1254 struct rpc_task_setup task_setup_data = {
1255 .rpc_client = server->client,
1256 .rpc_message = &msg,
1257 .callback_ops = &nfs4_open_ops,
1258 .callback_data = data,
1259 .workqueue = nfsiod_workqueue,
1260 .flags = RPC_TASK_ASYNC,
1264 kref_get(&data->kref);
1266 data->rpc_status = 0;
1267 data->cancelled = 0;
1268 task = rpc_run_task(&task_setup_data);
1270 return PTR_ERR(task);
1271 status = nfs4_wait_for_completion_rpc_task(task);
1273 data->cancelled = 1;
1276 status = data->rpc_status;
1278 if (status != 0 || !data->rpc_done)
1281 if (o_res->fh.size == 0)
1282 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1284 if (o_arg->open_flags & O_CREAT) {
1285 update_changeattr(dir, &o_res->cinfo);
1286 nfs_post_op_update_inode(dir, o_res->dir_attr);
1288 nfs_refresh_inode(dir, o_res->dir_attr);
1289 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1290 status = _nfs4_proc_open_confirm(data);
1294 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1295 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1299 static int nfs4_recover_expired_lease(struct nfs_server *server)
1301 struct nfs_client *clp = server->nfs_client;
1305 ret = nfs4_wait_clnt_recover(clp);
1308 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1309 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1311 nfs4_schedule_state_recovery(clp);
1318 * reclaim state on the server after a network partition.
1319 * Assumes caller holds the appropriate lock
1321 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1323 struct nfs4_opendata *opendata;
1326 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1327 if (IS_ERR(opendata))
1328 return PTR_ERR(opendata);
1329 ret = nfs4_open_recover(opendata, state);
1331 d_drop(ctx->path.dentry);
1332 nfs4_opendata_put(opendata);
1336 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1338 struct nfs_server *server = NFS_SERVER(state->inode);
1339 struct nfs4_exception exception = { };
1343 err = _nfs4_open_expired(ctx, state);
1344 if (err != -NFS4ERR_DELAY)
1346 nfs4_handle_exception(server, err, &exception);
1347 } while (exception.retry);
1351 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1353 struct nfs_open_context *ctx;
1356 ctx = nfs4_state_find_open_context(state);
1358 return PTR_ERR(ctx);
1359 ret = nfs4_do_open_expired(ctx, state);
1360 put_nfs_open_context(ctx);
1365 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1366 * fields corresponding to attributes that were used to store the verifier.
1367 * Make sure we clobber those fields in the later setattr call
1369 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1371 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1372 !(sattr->ia_valid & ATTR_ATIME_SET))
1373 sattr->ia_valid |= ATTR_ATIME;
1375 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1376 !(sattr->ia_valid & ATTR_MTIME_SET))
1377 sattr->ia_valid |= ATTR_MTIME;
1381 * Returns a referenced nfs4_state
1383 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1385 struct nfs4_state_owner *sp;
1386 struct nfs4_state *state = NULL;
1387 struct nfs_server *server = NFS_SERVER(dir);
1388 struct nfs4_opendata *opendata;
1391 /* Protect against reboot recovery conflicts */
1393 if (!(sp = nfs4_get_state_owner(server, cred))) {
1394 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1397 status = nfs4_recover_expired_lease(server);
1399 goto err_put_state_owner;
1400 if (path->dentry->d_inode != NULL)
1401 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1403 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1404 if (opendata == NULL)
1405 goto err_put_state_owner;
1407 if (path->dentry->d_inode != NULL)
1408 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1410 status = _nfs4_proc_open(opendata);
1412 goto err_opendata_put;
1414 if (opendata->o_arg.open_flags & O_EXCL)
1415 nfs4_exclusive_attrset(opendata, sattr);
1417 state = nfs4_opendata_to_nfs4_state(opendata);
1418 status = PTR_ERR(state);
1420 goto err_opendata_put;
1421 nfs4_opendata_put(opendata);
1422 nfs4_put_state_owner(sp);
1426 nfs4_opendata_put(opendata);
1427 err_put_state_owner:
1428 nfs4_put_state_owner(sp);
1435 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1437 struct nfs4_exception exception = { };
1438 struct nfs4_state *res;
1442 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1445 /* NOTE: BAD_SEQID means the server and client disagree about the
1446 * book-keeping w.r.t. state-changing operations
1447 * (OPEN/CLOSE/LOCK/LOCKU...)
1448 * It is actually a sign of a bug on the client or on the server.
1450 * If we receive a BAD_SEQID error in the particular case of
1451 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1452 * have unhashed the old state_owner for us, and that we can
1453 * therefore safely retry using a new one. We should still warn
1454 * the user though...
1456 if (status == -NFS4ERR_BAD_SEQID) {
1457 printk(KERN_WARNING "NFS: v4 server %s "
1458 " returned a bad sequence-id error!\n",
1459 NFS_SERVER(dir)->nfs_client->cl_hostname);
1460 exception.retry = 1;
1464 * BAD_STATEID on OPEN means that the server cancelled our
1465 * state before it received the OPEN_CONFIRM.
1466 * Recover by retrying the request as per the discussion
1467 * on Page 181 of RFC3530.
1469 if (status == -NFS4ERR_BAD_STATEID) {
1470 exception.retry = 1;
1473 if (status == -EAGAIN) {
1474 /* We must have found a delegation */
1475 exception.retry = 1;
1478 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1479 status, &exception));
1480 } while (exception.retry);
1484 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1485 struct nfs_fattr *fattr, struct iattr *sattr,
1486 struct nfs4_state *state)
1488 struct nfs_server *server = NFS_SERVER(inode);
1489 struct nfs_setattrargs arg = {
1490 .fh = NFS_FH(inode),
1493 .bitmask = server->attr_bitmask,
1495 struct nfs_setattrres res = {
1499 struct rpc_message msg = {
1500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1505 unsigned long timestamp = jiffies;
1508 nfs_fattr_init(fattr);
1510 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1511 /* Use that stateid */
1512 } else if (state != NULL) {
1513 nfs4_copy_stateid(&arg.stateid, state, current->files);
1515 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1517 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1518 if (status == 0 && state != NULL)
1519 renew_lease(server, timestamp);
1523 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1524 struct nfs_fattr *fattr, struct iattr *sattr,
1525 struct nfs4_state *state)
1527 struct nfs_server *server = NFS_SERVER(inode);
1528 struct nfs4_exception exception = { };
1531 err = nfs4_handle_exception(server,
1532 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1534 } while (exception.retry);
1538 struct nfs4_closedata {
1540 struct inode *inode;
1541 struct nfs4_state *state;
1542 struct nfs_closeargs arg;
1543 struct nfs_closeres res;
1544 struct nfs_fattr fattr;
1545 unsigned long timestamp;
1548 static void nfs4_free_closedata(void *data)
1550 struct nfs4_closedata *calldata = data;
1551 struct nfs4_state_owner *sp = calldata->state->owner;
1553 nfs4_put_open_state(calldata->state);
1554 nfs_free_seqid(calldata->arg.seqid);
1555 nfs4_put_state_owner(sp);
1556 path_put(&calldata->path);
1560 static void nfs4_close_done(struct rpc_task *task, void *data)
1562 struct nfs4_closedata *calldata = data;
1563 struct nfs4_state *state = calldata->state;
1564 struct nfs_server *server = NFS_SERVER(calldata->inode);
1566 if (RPC_ASSASSINATED(task))
1568 /* hmm. we are done with the inode, and in the process of freeing
1569 * the state_owner. we keep this around to process errors
1571 switch (task->tk_status) {
1573 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1574 renew_lease(server, calldata->timestamp);
1576 case -NFS4ERR_STALE_STATEID:
1577 case -NFS4ERR_OLD_STATEID:
1578 case -NFS4ERR_BAD_STATEID:
1579 case -NFS4ERR_EXPIRED:
1580 if (calldata->arg.fmode == 0)
1583 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1584 rpc_restart_call(task);
1588 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1591 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1593 struct nfs4_closedata *calldata = data;
1594 struct nfs4_state *state = calldata->state;
1595 int clear_rd, clear_wr, clear_rdwr;
1597 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1600 clear_rd = clear_wr = clear_rdwr = 0;
1601 spin_lock(&state->owner->so_lock);
1602 /* Calculate the change in open mode */
1603 if (state->n_rdwr == 0) {
1604 if (state->n_rdonly == 0) {
1605 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1606 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1608 if (state->n_wronly == 0) {
1609 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1610 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1613 spin_unlock(&state->owner->so_lock);
1614 if (!clear_rd && !clear_wr && !clear_rdwr) {
1615 /* Note: exit _without_ calling nfs4_close_done */
1616 task->tk_action = NULL;
1619 nfs_fattr_init(calldata->res.fattr);
1620 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1621 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1622 calldata->arg.fmode = FMODE_READ;
1623 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1624 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1625 calldata->arg.fmode = FMODE_WRITE;
1627 calldata->timestamp = jiffies;
1628 rpc_call_start(task);
1631 static const struct rpc_call_ops nfs4_close_ops = {
1632 .rpc_call_prepare = nfs4_close_prepare,
1633 .rpc_call_done = nfs4_close_done,
1634 .rpc_release = nfs4_free_closedata,
1638 * It is possible for data to be read/written from a mem-mapped file
1639 * after the sys_close call (which hits the vfs layer as a flush).
1640 * This means that we can't safely call nfsv4 close on a file until
1641 * the inode is cleared. This in turn means that we are not good
1642 * NFSv4 citizens - we do not indicate to the server to update the file's
1643 * share state even when we are done with one of the three share
1644 * stateid's in the inode.
1646 * NOTE: Caller must be holding the sp->so_owner semaphore!
1648 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1650 struct nfs_server *server = NFS_SERVER(state->inode);
1651 struct nfs4_closedata *calldata;
1652 struct nfs4_state_owner *sp = state->owner;
1653 struct rpc_task *task;
1654 struct rpc_message msg = {
1655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1656 .rpc_cred = state->owner->so_cred,
1658 struct rpc_task_setup task_setup_data = {
1659 .rpc_client = server->client,
1660 .rpc_message = &msg,
1661 .callback_ops = &nfs4_close_ops,
1662 .workqueue = nfsiod_workqueue,
1663 .flags = RPC_TASK_ASYNC,
1665 int status = -ENOMEM;
1667 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1668 if (calldata == NULL)
1670 calldata->inode = state->inode;
1671 calldata->state = state;
1672 calldata->arg.fh = NFS_FH(state->inode);
1673 calldata->arg.stateid = &state->open_stateid;
1674 /* Serialization for the sequence id */
1675 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1676 if (calldata->arg.seqid == NULL)
1677 goto out_free_calldata;
1678 calldata->arg.fmode = 0;
1679 calldata->arg.bitmask = server->cache_consistency_bitmask;
1680 calldata->res.fattr = &calldata->fattr;
1681 calldata->res.seqid = calldata->arg.seqid;
1682 calldata->res.server = server;
1683 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1684 calldata->path.mnt = mntget(path->mnt);
1685 calldata->path.dentry = dget(path->dentry);
1687 msg.rpc_argp = &calldata->arg,
1688 msg.rpc_resp = &calldata->res,
1689 task_setup_data.callback_data = calldata;
1690 task = rpc_run_task(&task_setup_data);
1692 return PTR_ERR(task);
1695 status = rpc_wait_for_completion_task(task);
1701 nfs4_put_open_state(state);
1702 nfs4_put_state_owner(sp);
1706 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1711 /* If the open_intent is for execute, we have an extra check to make */
1712 if (fmode & FMODE_EXEC) {
1713 ret = nfs_may_open(state->inode,
1714 state->owner->so_cred,
1715 nd->intent.open.flags);
1719 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1720 if (!IS_ERR(filp)) {
1721 struct nfs_open_context *ctx;
1722 ctx = nfs_file_open_context(filp);
1726 ret = PTR_ERR(filp);
1728 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1733 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1735 struct path path = {
1736 .mnt = nd->path.mnt,
1739 struct dentry *parent;
1741 struct rpc_cred *cred;
1742 struct nfs4_state *state;
1744 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1746 if (nd->flags & LOOKUP_CREATE) {
1747 attr.ia_mode = nd->intent.open.create_mode;
1748 attr.ia_valid = ATTR_MODE;
1749 if (!IS_POSIXACL(dir))
1750 attr.ia_mode &= ~current_umask();
1753 BUG_ON(nd->intent.open.flags & O_CREAT);
1756 cred = rpc_lookup_cred();
1758 return (struct dentry *)cred;
1759 parent = dentry->d_parent;
1760 /* Protect against concurrent sillydeletes */
1761 nfs_block_sillyrename(parent);
1762 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1764 if (IS_ERR(state)) {
1765 if (PTR_ERR(state) == -ENOENT) {
1766 d_add(dentry, NULL);
1767 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1769 nfs_unblock_sillyrename(parent);
1770 return (struct dentry *)state;
1772 res = d_add_unique(dentry, igrab(state->inode));
1775 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1776 nfs_unblock_sillyrename(parent);
1777 nfs4_intent_set_file(nd, &path, state, fmode);
1782 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1784 struct path path = {
1785 .mnt = nd->path.mnt,
1788 struct rpc_cred *cred;
1789 struct nfs4_state *state;
1790 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1792 cred = rpc_lookup_cred();
1794 return PTR_ERR(cred);
1795 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1797 if (IS_ERR(state)) {
1798 switch (PTR_ERR(state)) {
1804 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1810 if (state->inode == dentry->d_inode) {
1811 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1812 nfs4_intent_set_file(nd, &path, state, fmode);
1815 nfs4_close_sync(&path, state, fmode);
1821 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1823 if (ctx->state == NULL)
1826 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1828 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1831 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1833 struct nfs4_server_caps_arg args = {
1836 struct nfs4_server_caps_res res = {};
1837 struct rpc_message msg = {
1838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1844 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1846 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1847 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1848 server->caps |= NFS_CAP_ACLS;
1849 if (res.has_links != 0)
1850 server->caps |= NFS_CAP_HARDLINKS;
1851 if (res.has_symlinks != 0)
1852 server->caps |= NFS_CAP_SYMLINKS;
1853 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1854 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1855 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1856 server->acl_bitmask = res.acl_bitmask;
1862 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1864 struct nfs4_exception exception = { };
1867 err = nfs4_handle_exception(server,
1868 _nfs4_server_capabilities(server, fhandle),
1870 } while (exception.retry);
1874 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1875 struct nfs_fsinfo *info)
1877 struct nfs4_lookup_root_arg args = {
1878 .bitmask = nfs4_fattr_bitmap,
1880 struct nfs4_lookup_res res = {
1882 .fattr = info->fattr,
1885 struct rpc_message msg = {
1886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1890 nfs_fattr_init(info->fattr);
1891 return nfs4_call_sync(server, &msg, &args, &res, 0);
1894 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1895 struct nfs_fsinfo *info)
1897 struct nfs4_exception exception = { };
1900 err = nfs4_handle_exception(server,
1901 _nfs4_lookup_root(server, fhandle, info),
1903 } while (exception.retry);
1908 * get the file handle for the "/" directory on the server
1910 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1911 struct nfs_fsinfo *info)
1915 status = nfs4_lookup_root(server, fhandle, info);
1917 status = nfs4_server_capabilities(server, fhandle);
1919 status = nfs4_do_fsinfo(server, fhandle, info);
1920 return nfs4_map_errors(status);
1924 * Get locations and (maybe) other attributes of a referral.
1925 * Note that we'll actually follow the referral later when
1926 * we detect fsid mismatch in inode revalidation
1928 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1930 int status = -ENOMEM;
1931 struct page *page = NULL;
1932 struct nfs4_fs_locations *locations = NULL;
1934 page = alloc_page(GFP_KERNEL);
1937 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1938 if (locations == NULL)
1941 status = nfs4_proc_fs_locations(dir, name, locations, page);
1944 /* Make sure server returned a different fsid for the referral */
1945 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1946 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1951 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1952 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1954 fattr->mode = S_IFDIR;
1955 memset(fhandle, 0, sizeof(struct nfs_fh));
1964 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1966 struct nfs4_getattr_arg args = {
1968 .bitmask = server->attr_bitmask,
1970 struct nfs4_getattr_res res = {
1974 struct rpc_message msg = {
1975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1980 nfs_fattr_init(fattr);
1981 return nfs4_call_sync(server, &msg, &args, &res, 0);
1984 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1986 struct nfs4_exception exception = { };
1989 err = nfs4_handle_exception(server,
1990 _nfs4_proc_getattr(server, fhandle, fattr),
1992 } while (exception.retry);
1997 * The file is not closed if it is opened due to the a request to change
1998 * the size of the file. The open call will not be needed once the
1999 * VFS layer lookup-intents are implemented.
2001 * Close is called when the inode is destroyed.
2002 * If we haven't opened the file for O_WRONLY, we
2003 * need to in the size_change case to obtain a stateid.
2006 * Because OPEN is always done by name in nfsv4, it is
2007 * possible that we opened a different file by the same
2008 * name. We can recognize this race condition, but we
2009 * can't do anything about it besides returning an error.
2011 * This will be fixed with VFS changes (lookup-intent).
2014 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2015 struct iattr *sattr)
2017 struct inode *inode = dentry->d_inode;
2018 struct rpc_cred *cred = NULL;
2019 struct nfs4_state *state = NULL;
2022 nfs_fattr_init(fattr);
2024 /* Search for an existing open(O_WRITE) file */
2025 if (sattr->ia_valid & ATTR_FILE) {
2026 struct nfs_open_context *ctx;
2028 ctx = nfs_file_open_context(sattr->ia_file);
2035 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2037 nfs_setattr_update_inode(inode, sattr);
2041 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2042 const struct qstr *name, struct nfs_fh *fhandle,
2043 struct nfs_fattr *fattr)
2046 struct nfs4_lookup_arg args = {
2047 .bitmask = server->attr_bitmask,
2051 struct nfs4_lookup_res res = {
2056 struct rpc_message msg = {
2057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2062 nfs_fattr_init(fattr);
2064 dprintk("NFS call lookupfh %s\n", name->name);
2065 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2066 dprintk("NFS reply lookupfh: %d\n", status);
2070 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2071 struct qstr *name, struct nfs_fh *fhandle,
2072 struct nfs_fattr *fattr)
2074 struct nfs4_exception exception = { };
2077 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2079 if (err == -NFS4ERR_MOVED) {
2083 err = nfs4_handle_exception(server, err, &exception);
2084 } while (exception.retry);
2088 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2089 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2093 dprintk("NFS call lookup %s\n", name->name);
2094 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2095 if (status == -NFS4ERR_MOVED)
2096 status = nfs4_get_referral(dir, name, fattr, fhandle);
2097 dprintk("NFS reply lookup: %d\n", status);
2101 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2103 struct nfs4_exception exception = { };
2106 err = nfs4_handle_exception(NFS_SERVER(dir),
2107 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2109 } while (exception.retry);
2113 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2115 struct nfs_server *server = NFS_SERVER(inode);
2116 struct nfs_fattr fattr;
2117 struct nfs4_accessargs args = {
2118 .fh = NFS_FH(inode),
2119 .bitmask = server->attr_bitmask,
2121 struct nfs4_accessres res = {
2125 struct rpc_message msg = {
2126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2129 .rpc_cred = entry->cred,
2131 int mode = entry->mask;
2135 * Determine which access bits we want to ask for...
2137 if (mode & MAY_READ)
2138 args.access |= NFS4_ACCESS_READ;
2139 if (S_ISDIR(inode->i_mode)) {
2140 if (mode & MAY_WRITE)
2141 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2142 if (mode & MAY_EXEC)
2143 args.access |= NFS4_ACCESS_LOOKUP;
2145 if (mode & MAY_WRITE)
2146 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2147 if (mode & MAY_EXEC)
2148 args.access |= NFS4_ACCESS_EXECUTE;
2150 nfs_fattr_init(&fattr);
2151 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2154 if (res.access & NFS4_ACCESS_READ)
2155 entry->mask |= MAY_READ;
2156 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2157 entry->mask |= MAY_WRITE;
2158 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2159 entry->mask |= MAY_EXEC;
2160 nfs_refresh_inode(inode, &fattr);
2165 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2167 struct nfs4_exception exception = { };
2170 err = nfs4_handle_exception(NFS_SERVER(inode),
2171 _nfs4_proc_access(inode, entry),
2173 } while (exception.retry);
2178 * TODO: For the time being, we don't try to get any attributes
2179 * along with any of the zero-copy operations READ, READDIR,
2182 * In the case of the first three, we want to put the GETATTR
2183 * after the read-type operation -- this is because it is hard
2184 * to predict the length of a GETATTR response in v4, and thus
2185 * align the READ data correctly. This means that the GETATTR
2186 * may end up partially falling into the page cache, and we should
2187 * shift it into the 'tail' of the xdr_buf before processing.
2188 * To do this efficiently, we need to know the total length
2189 * of data received, which doesn't seem to be available outside
2192 * In the case of WRITE, we also want to put the GETATTR after
2193 * the operation -- in this case because we want to make sure
2194 * we get the post-operation mtime and size. This means that
2195 * we can't use xdr_encode_pages() as written: we need a variant
2196 * of it which would leave room in the 'tail' iovec.
2198 * Both of these changes to the XDR layer would in fact be quite
2199 * minor, but I decided to leave them for a subsequent patch.
2201 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2202 unsigned int pgbase, unsigned int pglen)
2204 struct nfs4_readlink args = {
2205 .fh = NFS_FH(inode),
2210 struct nfs4_readlink_res res;
2211 struct rpc_message msg = {
2212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2217 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2220 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2221 unsigned int pgbase, unsigned int pglen)
2223 struct nfs4_exception exception = { };
2226 err = nfs4_handle_exception(NFS_SERVER(inode),
2227 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2229 } while (exception.retry);
2235 * We will need to arrange for the VFS layer to provide an atomic open.
2236 * Until then, this create/open method is prone to inefficiency and race
2237 * conditions due to the lookup, create, and open VFS calls from sys_open()
2238 * placed on the wire.
2240 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2241 * The file will be opened again in the subsequent VFS open call
2242 * (nfs4_proc_file_open).
2244 * The open for read will just hang around to be used by any process that
2245 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2249 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2250 int flags, struct nameidata *nd)
2252 struct path path = {
2253 .mnt = nd->path.mnt,
2256 struct nfs4_state *state;
2257 struct rpc_cred *cred;
2258 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2261 cred = rpc_lookup_cred();
2263 status = PTR_ERR(cred);
2266 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2268 if (IS_ERR(state)) {
2269 status = PTR_ERR(state);
2272 d_add(dentry, igrab(state->inode));
2273 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2274 if (flags & O_EXCL) {
2275 struct nfs_fattr fattr;
2276 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2278 nfs_setattr_update_inode(state->inode, sattr);
2279 nfs_post_op_update_inode(state->inode, &fattr);
2281 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2282 status = nfs4_intent_set_file(nd, &path, state, fmode);
2284 nfs4_close_sync(&path, state, fmode);
2291 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2293 struct nfs_server *server = NFS_SERVER(dir);
2294 struct nfs_removeargs args = {
2296 .name.len = name->len,
2297 .name.name = name->name,
2298 .bitmask = server->attr_bitmask,
2300 struct nfs_removeres res = {
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2310 nfs_fattr_init(&res.dir_attr);
2311 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2313 update_changeattr(dir, &res.cinfo);
2314 nfs_post_op_update_inode(dir, &res.dir_attr);
2319 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2321 struct nfs4_exception exception = { };
2324 err = nfs4_handle_exception(NFS_SERVER(dir),
2325 _nfs4_proc_remove(dir, name),
2327 } while (exception.retry);
2331 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2333 struct nfs_server *server = NFS_SERVER(dir);
2334 struct nfs_removeargs *args = msg->rpc_argp;
2335 struct nfs_removeres *res = msg->rpc_resp;
2337 args->bitmask = server->cache_consistency_bitmask;
2338 res->server = server;
2339 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2342 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2344 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2346 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2348 update_changeattr(dir, &res->cinfo);
2349 nfs_post_op_update_inode(dir, &res->dir_attr);
2353 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2354 struct inode *new_dir, struct qstr *new_name)
2356 struct nfs_server *server = NFS_SERVER(old_dir);
2357 struct nfs4_rename_arg arg = {
2358 .old_dir = NFS_FH(old_dir),
2359 .new_dir = NFS_FH(new_dir),
2360 .old_name = old_name,
2361 .new_name = new_name,
2362 .bitmask = server->attr_bitmask,
2364 struct nfs_fattr old_fattr, new_fattr;
2365 struct nfs4_rename_res res = {
2367 .old_fattr = &old_fattr,
2368 .new_fattr = &new_fattr,
2370 struct rpc_message msg = {
2371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2377 nfs_fattr_init(res.old_fattr);
2378 nfs_fattr_init(res.new_fattr);
2379 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2382 update_changeattr(old_dir, &res.old_cinfo);
2383 nfs_post_op_update_inode(old_dir, res.old_fattr);
2384 update_changeattr(new_dir, &res.new_cinfo);
2385 nfs_post_op_update_inode(new_dir, res.new_fattr);
2390 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2391 struct inode *new_dir, struct qstr *new_name)
2393 struct nfs4_exception exception = { };
2396 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2397 _nfs4_proc_rename(old_dir, old_name,
2400 } while (exception.retry);
2404 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2406 struct nfs_server *server = NFS_SERVER(inode);
2407 struct nfs4_link_arg arg = {
2408 .fh = NFS_FH(inode),
2409 .dir_fh = NFS_FH(dir),
2411 .bitmask = server->attr_bitmask,
2413 struct nfs_fattr fattr, dir_attr;
2414 struct nfs4_link_res res = {
2417 .dir_attr = &dir_attr,
2419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2426 nfs_fattr_init(res.fattr);
2427 nfs_fattr_init(res.dir_attr);
2428 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2430 update_changeattr(dir, &res.cinfo);
2431 nfs_post_op_update_inode(dir, res.dir_attr);
2432 nfs_post_op_update_inode(inode, res.fattr);
2438 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2440 struct nfs4_exception exception = { };
2443 err = nfs4_handle_exception(NFS_SERVER(inode),
2444 _nfs4_proc_link(inode, dir, name),
2446 } while (exception.retry);
2450 struct nfs4_createdata {
2451 struct rpc_message msg;
2452 struct nfs4_create_arg arg;
2453 struct nfs4_create_res res;
2455 struct nfs_fattr fattr;
2456 struct nfs_fattr dir_fattr;
2459 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2460 struct qstr *name, struct iattr *sattr, u32 ftype)
2462 struct nfs4_createdata *data;
2464 data = kzalloc(sizeof(*data), GFP_KERNEL);
2466 struct nfs_server *server = NFS_SERVER(dir);
2468 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2469 data->msg.rpc_argp = &data->arg;
2470 data->msg.rpc_resp = &data->res;
2471 data->arg.dir_fh = NFS_FH(dir);
2472 data->arg.server = server;
2473 data->arg.name = name;
2474 data->arg.attrs = sattr;
2475 data->arg.ftype = ftype;
2476 data->arg.bitmask = server->attr_bitmask;
2477 data->res.server = server;
2478 data->res.fh = &data->fh;
2479 data->res.fattr = &data->fattr;
2480 data->res.dir_fattr = &data->dir_fattr;
2481 nfs_fattr_init(data->res.fattr);
2482 nfs_fattr_init(data->res.dir_fattr);
2487 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2489 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2490 &data->arg, &data->res, 1);
2492 update_changeattr(dir, &data->res.dir_cinfo);
2493 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2494 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2499 static void nfs4_free_createdata(struct nfs4_createdata *data)
2504 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2505 struct page *page, unsigned int len, struct iattr *sattr)
2507 struct nfs4_createdata *data;
2508 int status = -ENAMETOOLONG;
2510 if (len > NFS4_MAXPATHLEN)
2514 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2518 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2519 data->arg.u.symlink.pages = &page;
2520 data->arg.u.symlink.len = len;
2522 status = nfs4_do_create(dir, dentry, data);
2524 nfs4_free_createdata(data);
2529 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2530 struct page *page, unsigned int len, struct iattr *sattr)
2532 struct nfs4_exception exception = { };
2535 err = nfs4_handle_exception(NFS_SERVER(dir),
2536 _nfs4_proc_symlink(dir, dentry, page,
2539 } while (exception.retry);
2543 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2544 struct iattr *sattr)
2546 struct nfs4_createdata *data;
2547 int status = -ENOMEM;
2549 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2553 status = nfs4_do_create(dir, dentry, data);
2555 nfs4_free_createdata(data);
2560 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2561 struct iattr *sattr)
2563 struct nfs4_exception exception = { };
2566 err = nfs4_handle_exception(NFS_SERVER(dir),
2567 _nfs4_proc_mkdir(dir, dentry, sattr),
2569 } while (exception.retry);
2573 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2574 u64 cookie, struct page *page, unsigned int count, int plus)
2576 struct inode *dir = dentry->d_inode;
2577 struct nfs4_readdir_arg args = {
2582 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2584 struct nfs4_readdir_res res;
2585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2593 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2594 dentry->d_parent->d_name.name,
2595 dentry->d_name.name,
2596 (unsigned long long)cookie);
2597 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2598 res.pgbase = args.pgbase;
2599 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2601 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2603 nfs_invalidate_atime(dir);
2605 dprintk("%s: returns %d\n", __func__, status);
2609 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2610 u64 cookie, struct page *page, unsigned int count, int plus)
2612 struct nfs4_exception exception = { };
2615 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2616 _nfs4_proc_readdir(dentry, cred, cookie,
2619 } while (exception.retry);
2623 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2624 struct iattr *sattr, dev_t rdev)
2626 struct nfs4_createdata *data;
2627 int mode = sattr->ia_mode;
2628 int status = -ENOMEM;
2630 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2631 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2633 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2638 data->arg.ftype = NF4FIFO;
2639 else if (S_ISBLK(mode)) {
2640 data->arg.ftype = NF4BLK;
2641 data->arg.u.device.specdata1 = MAJOR(rdev);
2642 data->arg.u.device.specdata2 = MINOR(rdev);
2644 else if (S_ISCHR(mode)) {
2645 data->arg.ftype = NF4CHR;
2646 data->arg.u.device.specdata1 = MAJOR(rdev);
2647 data->arg.u.device.specdata2 = MINOR(rdev);
2650 status = nfs4_do_create(dir, dentry, data);
2652 nfs4_free_createdata(data);
2657 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2658 struct iattr *sattr, dev_t rdev)
2660 struct nfs4_exception exception = { };
2663 err = nfs4_handle_exception(NFS_SERVER(dir),
2664 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2666 } while (exception.retry);
2670 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2671 struct nfs_fsstat *fsstat)
2673 struct nfs4_statfs_arg args = {
2675 .bitmask = server->attr_bitmask,
2677 struct nfs4_statfs_res res = {
2680 struct rpc_message msg = {
2681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2686 nfs_fattr_init(fsstat->fattr);
2687 return nfs4_call_sync(server, &msg, &args, &res, 0);
2690 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2692 struct nfs4_exception exception = { };
2695 err = nfs4_handle_exception(server,
2696 _nfs4_proc_statfs(server, fhandle, fsstat),
2698 } while (exception.retry);
2702 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2703 struct nfs_fsinfo *fsinfo)
2705 struct nfs4_fsinfo_arg args = {
2707 .bitmask = server->attr_bitmask,
2709 struct nfs4_fsinfo_res res = {
2712 struct rpc_message msg = {
2713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2718 return nfs4_call_sync(server, &msg, &args, &res, 0);
2721 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2723 struct nfs4_exception exception = { };
2727 err = nfs4_handle_exception(server,
2728 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2730 } while (exception.retry);
2734 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2736 nfs_fattr_init(fsinfo->fattr);
2737 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2740 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2741 struct nfs_pathconf *pathconf)
2743 struct nfs4_pathconf_arg args = {
2745 .bitmask = server->attr_bitmask,
2747 struct nfs4_pathconf_res res = {
2748 .pathconf = pathconf,
2750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2756 /* None of the pathconf attributes are mandatory to implement */
2757 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2758 memset(pathconf, 0, sizeof(*pathconf));
2762 nfs_fattr_init(pathconf->fattr);
2763 return nfs4_call_sync(server, &msg, &args, &res, 0);
2766 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2767 struct nfs_pathconf *pathconf)
2769 struct nfs4_exception exception = { };
2773 err = nfs4_handle_exception(server,
2774 _nfs4_proc_pathconf(server, fhandle, pathconf),
2776 } while (exception.retry);
2780 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2782 struct nfs_server *server = NFS_SERVER(data->inode);
2784 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2785 rpc_restart_call(task);
2789 nfs_invalidate_atime(data->inode);
2790 if (task->tk_status > 0)
2791 renew_lease(server, data->timestamp);
2795 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2797 data->timestamp = jiffies;
2798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2801 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2803 struct inode *inode = data->inode;
2805 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2806 rpc_restart_call(task);
2809 if (task->tk_status >= 0) {
2810 renew_lease(NFS_SERVER(inode), data->timestamp);
2811 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2816 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2818 struct nfs_server *server = NFS_SERVER(data->inode);
2820 data->args.bitmask = server->cache_consistency_bitmask;
2821 data->res.server = server;
2822 data->timestamp = jiffies;
2824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2827 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2829 struct inode *inode = data->inode;
2831 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2832 rpc_restart_call(task);
2835 nfs_refresh_inode(inode, data->res.fattr);
2839 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2841 struct nfs_server *server = NFS_SERVER(data->inode);
2843 data->args.bitmask = server->cache_consistency_bitmask;
2844 data->res.server = server;
2845 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2849 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2850 * standalone procedure for queueing an asynchronous RENEW.
2852 static void nfs4_renew_done(struct rpc_task *task, void *data)
2854 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2855 unsigned long timestamp = (unsigned long)data;
2857 if (task->tk_status < 0) {
2858 /* Unless we're shutting down, schedule state recovery! */
2859 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2860 nfs4_schedule_state_recovery(clp);
2863 spin_lock(&clp->cl_lock);
2864 if (time_before(clp->cl_last_renewal,timestamp))
2865 clp->cl_last_renewal = timestamp;
2866 spin_unlock(&clp->cl_lock);
2869 static const struct rpc_call_ops nfs4_renew_ops = {
2870 .rpc_call_done = nfs4_renew_done,
2873 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2881 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2882 &nfs4_renew_ops, (void *)jiffies);
2885 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2887 struct rpc_message msg = {
2888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2892 unsigned long now = jiffies;
2895 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2898 spin_lock(&clp->cl_lock);
2899 if (time_before(clp->cl_last_renewal,now))
2900 clp->cl_last_renewal = now;
2901 spin_unlock(&clp->cl_lock);
2905 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2907 return (server->caps & NFS_CAP_ACLS)
2908 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2909 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2912 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2913 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2916 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2918 static void buf_to_pages(const void *buf, size_t buflen,
2919 struct page **pages, unsigned int *pgbase)
2921 const void *p = buf;
2923 *pgbase = offset_in_page(buf);
2925 while (p < buf + buflen) {
2926 *(pages++) = virt_to_page(p);
2927 p += PAGE_CACHE_SIZE;
2931 struct nfs4_cached_acl {
2937 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2939 struct nfs_inode *nfsi = NFS_I(inode);
2941 spin_lock(&inode->i_lock);
2942 kfree(nfsi->nfs4_acl);
2943 nfsi->nfs4_acl = acl;
2944 spin_unlock(&inode->i_lock);
2947 static void nfs4_zap_acl_attr(struct inode *inode)
2949 nfs4_set_cached_acl(inode, NULL);
2952 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2954 struct nfs_inode *nfsi = NFS_I(inode);
2955 struct nfs4_cached_acl *acl;
2958 spin_lock(&inode->i_lock);
2959 acl = nfsi->nfs4_acl;
2962 if (buf == NULL) /* user is just asking for length */
2964 if (acl->cached == 0)
2966 ret = -ERANGE; /* see getxattr(2) man page */
2967 if (acl->len > buflen)
2969 memcpy(buf, acl->data, acl->len);
2973 spin_unlock(&inode->i_lock);
2977 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2979 struct nfs4_cached_acl *acl;
2981 if (buf && acl_len <= PAGE_SIZE) {
2982 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2986 memcpy(acl->data, buf, acl_len);
2988 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2995 nfs4_set_cached_acl(inode, acl);
2998 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3000 struct page *pages[NFS4ACL_MAXPAGES];
3001 struct nfs_getaclargs args = {
3002 .fh = NFS_FH(inode),
3006 struct nfs_getaclres res = {
3010 struct rpc_message msg = {
3011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3015 struct page *localpage = NULL;
3018 if (buflen < PAGE_SIZE) {
3019 /* As long as we're doing a round trip to the server anyway,
3020 * let's be prepared for a page of acl data. */
3021 localpage = alloc_page(GFP_KERNEL);
3022 resp_buf = page_address(localpage);
3023 if (localpage == NULL)
3025 args.acl_pages[0] = localpage;
3026 args.acl_pgbase = 0;
3027 args.acl_len = PAGE_SIZE;
3030 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3032 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3035 if (res.acl_len > args.acl_len)
3036 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3038 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3041 if (res.acl_len > buflen)
3044 memcpy(buf, resp_buf, res.acl_len);
3049 __free_page(localpage);
3053 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3055 struct nfs4_exception exception = { };
3058 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3061 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3062 } while (exception.retry);
3066 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3068 struct nfs_server *server = NFS_SERVER(inode);
3071 if (!nfs4_server_supports_acls(server))
3073 ret = nfs_revalidate_inode(server, inode);
3076 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3077 nfs_zap_acl_cache(inode);
3078 ret = nfs4_read_cached_acl(inode, buf, buflen);
3081 return nfs4_get_acl_uncached(inode, buf, buflen);
3084 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3086 struct nfs_server *server = NFS_SERVER(inode);
3087 struct page *pages[NFS4ACL_MAXPAGES];
3088 struct nfs_setaclargs arg = {
3089 .fh = NFS_FH(inode),
3093 struct nfs_setaclres res;
3094 struct rpc_message msg = {
3095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3101 if (!nfs4_server_supports_acls(server))
3103 nfs_inode_return_delegation(inode);
3104 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3105 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3106 nfs_access_zap_cache(inode);
3107 nfs_zap_acl_cache(inode);
3111 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3113 struct nfs4_exception exception = { };
3116 err = nfs4_handle_exception(NFS_SERVER(inode),
3117 __nfs4_proc_set_acl(inode, buf, buflen),
3119 } while (exception.retry);
3124 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3126 struct nfs_client *clp = server->nfs_client;
3128 if (!clp || task->tk_status >= 0)
3130 switch(task->tk_status) {
3131 case -NFS4ERR_ADMIN_REVOKED:
3132 case -NFS4ERR_BAD_STATEID:
3133 case -NFS4ERR_OPENMODE:
3136 nfs4_state_mark_reclaim_nograce(clp, state);
3137 case -NFS4ERR_STALE_CLIENTID:
3138 case -NFS4ERR_STALE_STATEID:
3139 case -NFS4ERR_EXPIRED:
3140 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3141 nfs4_schedule_state_recovery(clp);
3142 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3143 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3144 task->tk_status = 0;
3146 case -NFS4ERR_DELAY:
3147 nfs_inc_server_stats(server, NFSIOS_DELAY);
3148 case -NFS4ERR_GRACE:
3149 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3150 task->tk_status = 0;
3152 case -NFS4ERR_OLD_STATEID:
3153 task->tk_status = 0;
3156 task->tk_status = nfs4_map_errors(task->tk_status);
3160 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3162 nfs4_verifier sc_verifier;
3163 struct nfs4_setclientid setclientid = {
3164 .sc_verifier = &sc_verifier,
3167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3169 .rpc_argp = &setclientid,
3177 p = (__be32*)sc_verifier.data;
3178 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3179 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3182 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3183 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3185 rpc_peeraddr2str(clp->cl_rpcclient,
3187 rpc_peeraddr2str(clp->cl_rpcclient,
3189 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3190 clp->cl_id_uniquifier);
3191 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3192 sizeof(setclientid.sc_netid),
3193 rpc_peeraddr2str(clp->cl_rpcclient,
3194 RPC_DISPLAY_NETID));
3195 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3196 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3197 clp->cl_ipaddr, port >> 8, port & 255);
3199 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3200 if (status != -NFS4ERR_CLID_INUSE)
3205 ssleep(clp->cl_lease_time + 1);
3207 if (++clp->cl_id_uniquifier == 0)
3213 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3215 struct nfs_fsinfo fsinfo;
3216 struct rpc_message msg = {
3217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3219 .rpc_resp = &fsinfo,
3226 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3228 spin_lock(&clp->cl_lock);
3229 clp->cl_lease_time = fsinfo.lease_time * HZ;
3230 clp->cl_last_renewal = now;
3231 spin_unlock(&clp->cl_lock);
3236 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3241 err = _nfs4_proc_setclientid_confirm(clp, cred);
3245 case -NFS4ERR_RESOURCE:
3246 /* The IBM lawyers misread another document! */
3247 case -NFS4ERR_DELAY:
3248 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3254 struct nfs4_delegreturndata {
3255 struct nfs4_delegreturnargs args;
3256 struct nfs4_delegreturnres res;
3258 nfs4_stateid stateid;
3259 unsigned long timestamp;
3260 struct nfs_fattr fattr;
3264 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3266 struct nfs4_delegreturndata *data = calldata;
3267 data->rpc_status = task->tk_status;
3268 if (data->rpc_status == 0)
3269 renew_lease(data->res.server, data->timestamp);
3272 static void nfs4_delegreturn_release(void *calldata)
3277 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3278 .rpc_call_done = nfs4_delegreturn_done,
3279 .rpc_release = nfs4_delegreturn_release,
3282 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3284 struct nfs4_delegreturndata *data;
3285 struct nfs_server *server = NFS_SERVER(inode);
3286 struct rpc_task *task;
3287 struct rpc_message msg = {
3288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3291 struct rpc_task_setup task_setup_data = {
3292 .rpc_client = server->client,
3293 .rpc_message = &msg,
3294 .callback_ops = &nfs4_delegreturn_ops,
3295 .flags = RPC_TASK_ASYNC,
3299 data = kmalloc(sizeof(*data), GFP_KERNEL);
3302 data->args.fhandle = &data->fh;
3303 data->args.stateid = &data->stateid;
3304 data->args.bitmask = server->attr_bitmask;
3305 nfs_copy_fh(&data->fh, NFS_FH(inode));
3306 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3307 data->res.fattr = &data->fattr;
3308 data->res.server = server;
3309 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3310 nfs_fattr_init(data->res.fattr);
3311 data->timestamp = jiffies;
3312 data->rpc_status = 0;
3314 task_setup_data.callback_data = data;
3315 msg.rpc_argp = &data->args,
3316 msg.rpc_resp = &data->res,
3317 task = rpc_run_task(&task_setup_data);
3319 return PTR_ERR(task);
3322 status = nfs4_wait_for_completion_rpc_task(task);
3325 status = data->rpc_status;
3328 nfs_refresh_inode(inode, &data->fattr);
3334 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3336 struct nfs_server *server = NFS_SERVER(inode);
3337 struct nfs4_exception exception = { };
3340 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3342 case -NFS4ERR_STALE_STATEID:
3343 case -NFS4ERR_EXPIRED:
3347 err = nfs4_handle_exception(server, err, &exception);
3348 } while (exception.retry);
3352 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3353 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3356 * sleep, with exponential backoff, and retry the LOCK operation.
3358 static unsigned long
3359 nfs4_set_lock_task_retry(unsigned long timeout)
3361 schedule_timeout_killable(timeout);
3363 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3364 return NFS4_LOCK_MAXTIMEOUT;
3368 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3370 struct inode *inode = state->inode;
3371 struct nfs_server *server = NFS_SERVER(inode);
3372 struct nfs_client *clp = server->nfs_client;
3373 struct nfs_lockt_args arg = {
3374 .fh = NFS_FH(inode),
3377 struct nfs_lockt_res res = {
3380 struct rpc_message msg = {
3381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3384 .rpc_cred = state->owner->so_cred,
3386 struct nfs4_lock_state *lsp;
3389 arg.lock_owner.clientid = clp->cl_clientid;
3390 status = nfs4_set_lock_state(state, request);
3393 lsp = request->fl_u.nfs4_fl.owner;
3394 arg.lock_owner.id = lsp->ls_id.id;
3395 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3398 request->fl_type = F_UNLCK;
3400 case -NFS4ERR_DENIED:
3403 request->fl_ops->fl_release_private(request);
3408 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3410 struct nfs4_exception exception = { };
3414 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3415 _nfs4_proc_getlk(state, cmd, request),
3417 } while (exception.retry);
3421 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3424 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3426 res = posix_lock_file_wait(file, fl);
3429 res = flock_lock_file_wait(file, fl);
3437 struct nfs4_unlockdata {
3438 struct nfs_locku_args arg;
3439 struct nfs_locku_res res;
3440 struct nfs4_lock_state *lsp;
3441 struct nfs_open_context *ctx;
3442 struct file_lock fl;
3443 const struct nfs_server *server;
3444 unsigned long timestamp;
3447 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3448 struct nfs_open_context *ctx,
3449 struct nfs4_lock_state *lsp,
3450 struct nfs_seqid *seqid)
3452 struct nfs4_unlockdata *p;
3453 struct inode *inode = lsp->ls_state->inode;
3455 p = kmalloc(sizeof(*p), GFP_KERNEL);
3458 p->arg.fh = NFS_FH(inode);
3460 p->arg.seqid = seqid;
3461 p->res.seqid = seqid;
3462 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3463 p->arg.stateid = &lsp->ls_stateid;
3465 atomic_inc(&lsp->ls_count);
3466 /* Ensure we don't close file until we're done freeing locks! */
3467 p->ctx = get_nfs_open_context(ctx);
3468 memcpy(&p->fl, fl, sizeof(p->fl));
3469 p->server = NFS_SERVER(inode);
3473 static void nfs4_locku_release_calldata(void *data)
3475 struct nfs4_unlockdata *calldata = data;
3476 nfs_free_seqid(calldata->arg.seqid);
3477 nfs4_put_lock_state(calldata->lsp);
3478 put_nfs_open_context(calldata->ctx);
3482 static void nfs4_locku_done(struct rpc_task *task, void *data)
3484 struct nfs4_unlockdata *calldata = data;
3486 if (RPC_ASSASSINATED(task))
3488 switch (task->tk_status) {
3490 memcpy(calldata->lsp->ls_stateid.data,
3491 calldata->res.stateid.data,
3492 sizeof(calldata->lsp->ls_stateid.data));
3493 renew_lease(calldata->server, calldata->timestamp);
3495 case -NFS4ERR_BAD_STATEID:
3496 case -NFS4ERR_OLD_STATEID:
3497 case -NFS4ERR_STALE_STATEID:
3498 case -NFS4ERR_EXPIRED:
3501 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3502 rpc_restart_call(task);
3506 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3508 struct nfs4_unlockdata *calldata = data;
3510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3512 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3513 /* Note: exit _without_ running nfs4_locku_done */
3514 task->tk_action = NULL;
3517 calldata->timestamp = jiffies;
3518 rpc_call_start(task);
3521 static const struct rpc_call_ops nfs4_locku_ops = {
3522 .rpc_call_prepare = nfs4_locku_prepare,
3523 .rpc_call_done = nfs4_locku_done,
3524 .rpc_release = nfs4_locku_release_calldata,
3527 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3528 struct nfs_open_context *ctx,
3529 struct nfs4_lock_state *lsp,
3530 struct nfs_seqid *seqid)
3532 struct nfs4_unlockdata *data;
3533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3535 .rpc_cred = ctx->cred,
3537 struct rpc_task_setup task_setup_data = {
3538 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3539 .rpc_message = &msg,
3540 .callback_ops = &nfs4_locku_ops,
3541 .workqueue = nfsiod_workqueue,
3542 .flags = RPC_TASK_ASYNC,
3545 /* Ensure this is an unlock - when canceling a lock, the
3546 * canceled lock is passed in, and it won't be an unlock.
3548 fl->fl_type = F_UNLCK;
3550 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3552 nfs_free_seqid(seqid);
3553 return ERR_PTR(-ENOMEM);
3556 msg.rpc_argp = &data->arg,
3557 msg.rpc_resp = &data->res,
3558 task_setup_data.callback_data = data;
3559 return rpc_run_task(&task_setup_data);
3562 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3564 struct nfs_inode *nfsi = NFS_I(state->inode);
3565 struct nfs_seqid *seqid;
3566 struct nfs4_lock_state *lsp;
3567 struct rpc_task *task;
3569 unsigned char fl_flags = request->fl_flags;
3571 status = nfs4_set_lock_state(state, request);
3572 /* Unlock _before_ we do the RPC call */
3573 request->fl_flags |= FL_EXISTS;
3574 down_read(&nfsi->rwsem);
3575 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3576 up_read(&nfsi->rwsem);
3579 up_read(&nfsi->rwsem);
3582 /* Is this a delegated lock? */
3583 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3585 lsp = request->fl_u.nfs4_fl.owner;
3586 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3590 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3591 status = PTR_ERR(task);
3594 status = nfs4_wait_for_completion_rpc_task(task);
3597 request->fl_flags = fl_flags;
3601 struct nfs4_lockdata {
3602 struct nfs_lock_args arg;
3603 struct nfs_lock_res res;
3604 struct nfs4_lock_state *lsp;
3605 struct nfs_open_context *ctx;
3606 struct file_lock fl;
3607 unsigned long timestamp;
3612 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3613 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3615 struct nfs4_lockdata *p;
3616 struct inode *inode = lsp->ls_state->inode;
3617 struct nfs_server *server = NFS_SERVER(inode);
3619 p = kzalloc(sizeof(*p), GFP_KERNEL);
3623 p->arg.fh = NFS_FH(inode);
3625 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3626 if (p->arg.open_seqid == NULL)
3628 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3629 if (p->arg.lock_seqid == NULL)
3630 goto out_free_seqid;
3631 p->arg.lock_stateid = &lsp->ls_stateid;
3632 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3633 p->arg.lock_owner.id = lsp->ls_id.id;
3634 p->res.lock_seqid = p->arg.lock_seqid;
3635 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3637 atomic_inc(&lsp->ls_count);
3638 p->ctx = get_nfs_open_context(ctx);
3639 memcpy(&p->fl, fl, sizeof(p->fl));
3642 nfs_free_seqid(p->arg.open_seqid);
3648 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3650 struct nfs4_lockdata *data = calldata;
3651 struct nfs4_state *state = data->lsp->ls_state;
3653 dprintk("%s: begin!\n", __func__);
3654 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3656 /* Do we need to do an open_to_lock_owner? */
3657 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3658 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3660 data->arg.open_stateid = &state->stateid;
3661 data->arg.new_lock_owner = 1;
3662 data->res.open_seqid = data->arg.open_seqid;
3664 data->arg.new_lock_owner = 0;
3665 data->timestamp = jiffies;
3666 rpc_call_start(task);
3667 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3670 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3672 struct nfs4_lockdata *data = calldata;
3674 dprintk("%s: begin!\n", __func__);
3676 data->rpc_status = task->tk_status;
3677 if (RPC_ASSASSINATED(task))
3679 if (data->arg.new_lock_owner != 0) {
3680 if (data->rpc_status == 0)
3681 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3685 if (data->rpc_status == 0) {
3686 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3687 sizeof(data->lsp->ls_stateid.data));
3688 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3689 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3692 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3695 static void nfs4_lock_release(void *calldata)
3697 struct nfs4_lockdata *data = calldata;
3699 dprintk("%s: begin!\n", __func__);
3700 nfs_free_seqid(data->arg.open_seqid);
3701 if (data->cancelled != 0) {
3702 struct rpc_task *task;
3703 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3704 data->arg.lock_seqid);
3707 dprintk("%s: cancelling lock!\n", __func__);
3709 nfs_free_seqid(data->arg.lock_seqid);
3710 nfs4_put_lock_state(data->lsp);
3711 put_nfs_open_context(data->ctx);
3713 dprintk("%s: done!\n", __func__);
3716 static const struct rpc_call_ops nfs4_lock_ops = {
3717 .rpc_call_prepare = nfs4_lock_prepare,
3718 .rpc_call_done = nfs4_lock_done,
3719 .rpc_release = nfs4_lock_release,
3722 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3724 struct nfs4_lockdata *data;
3725 struct rpc_task *task;
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3728 .rpc_cred = state->owner->so_cred,
3730 struct rpc_task_setup task_setup_data = {
3731 .rpc_client = NFS_CLIENT(state->inode),
3732 .rpc_message = &msg,
3733 .callback_ops = &nfs4_lock_ops,
3734 .workqueue = nfsiod_workqueue,
3735 .flags = RPC_TASK_ASYNC,
3739 dprintk("%s: begin!\n", __func__);
3740 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3741 fl->fl_u.nfs4_fl.owner);
3745 data->arg.block = 1;
3747 data->arg.reclaim = 1;
3748 msg.rpc_argp = &data->arg,
3749 msg.rpc_resp = &data->res,
3750 task_setup_data.callback_data = data;
3751 task = rpc_run_task(&task_setup_data);
3753 return PTR_ERR(task);
3754 ret = nfs4_wait_for_completion_rpc_task(task);
3756 ret = data->rpc_status;
3757 if (ret == -NFS4ERR_DENIED)
3760 data->cancelled = 1;
3762 dprintk("%s: done, ret = %d!\n", __func__, ret);
3766 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3768 struct nfs_server *server = NFS_SERVER(state->inode);
3769 struct nfs4_exception exception = { };
3773 /* Cache the lock if possible... */
3774 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3776 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3777 if (err != -NFS4ERR_DELAY)
3779 nfs4_handle_exception(server, err, &exception);
3780 } while (exception.retry);
3784 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3786 struct nfs_server *server = NFS_SERVER(state->inode);
3787 struct nfs4_exception exception = { };
3790 err = nfs4_set_lock_state(state, request);
3794 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3796 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3797 if (err != -NFS4ERR_DELAY)
3799 nfs4_handle_exception(server, err, &exception);
3800 } while (exception.retry);
3804 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3806 struct nfs_inode *nfsi = NFS_I(state->inode);
3807 unsigned char fl_flags = request->fl_flags;
3810 /* Is this a delegated open? */
3811 status = nfs4_set_lock_state(state, request);
3814 request->fl_flags |= FL_ACCESS;
3815 status = do_vfs_lock(request->fl_file, request);
3818 down_read(&nfsi->rwsem);
3819 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3820 /* Yes: cache locks! */
3821 /* ...but avoid races with delegation recall... */
3822 request->fl_flags = fl_flags & ~FL_SLEEP;
3823 status = do_vfs_lock(request->fl_file, request);
3826 status = _nfs4_do_setlk(state, cmd, request, 0);
3829 /* Note: we always want to sleep here! */
3830 request->fl_flags = fl_flags | FL_SLEEP;
3831 if (do_vfs_lock(request->fl_file, request) < 0)
3832 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3834 up_read(&nfsi->rwsem);
3836 request->fl_flags = fl_flags;
3840 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3842 struct nfs4_exception exception = { };
3846 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3847 _nfs4_proc_setlk(state, cmd, request),
3849 } while (exception.retry);
3854 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3856 struct nfs_open_context *ctx;
3857 struct nfs4_state *state;
3858 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3861 /* verify open state */
3862 ctx = nfs_file_open_context(filp);
3865 if (request->fl_start < 0 || request->fl_end < 0)
3869 return nfs4_proc_getlk(state, F_GETLK, request);
3871 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3874 if (request->fl_type == F_UNLCK)
3875 return nfs4_proc_unlck(state, cmd, request);
3878 status = nfs4_proc_setlk(state, cmd, request);
3879 if ((status != -EAGAIN) || IS_SETLK(cmd))
3881 timeout = nfs4_set_lock_task_retry(timeout);
3882 status = -ERESTARTSYS;
3885 } while(status < 0);
3889 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3891 struct nfs_server *server = NFS_SERVER(state->inode);
3892 struct nfs4_exception exception = { };
3895 err = nfs4_set_lock_state(state, fl);
3899 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3900 if (err != -NFS4ERR_DELAY)
3902 err = nfs4_handle_exception(server, err, &exception);
3903 } while (exception.retry);
3908 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3910 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3911 size_t buflen, int flags)
3913 struct inode *inode = dentry->d_inode;
3915 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3918 return nfs4_proc_set_acl(inode, buf, buflen);
3921 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3922 * and that's what we'll do for e.g. user attributes that haven't been set.
3923 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3924 * attributes in kernel-managed attribute namespaces. */
3925 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3928 struct inode *inode = dentry->d_inode;
3930 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3933 return nfs4_proc_get_acl(inode, buf, buflen);
3936 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3938 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3940 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3942 if (buf && buflen < len)
3945 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3949 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3951 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3952 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3953 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3956 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3957 NFS_ATTR_FATTR_NLINK;
3958 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3962 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3963 struct nfs4_fs_locations *fs_locations, struct page *page)
3965 struct nfs_server *server = NFS_SERVER(dir);
3967 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3968 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3970 struct nfs4_fs_locations_arg args = {
3971 .dir_fh = NFS_FH(dir),
3976 struct nfs4_fs_locations_res res = {
3977 .fs_locations = fs_locations,
3979 struct rpc_message msg = {
3980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3986 dprintk("%s: start\n", __func__);
3987 nfs_fattr_init(&fs_locations->fattr);
3988 fs_locations->server = server;
3989 fs_locations->nlocations = 0;
3990 status = nfs4_call_sync(server, &msg, &args, &res, 0);
3991 nfs_fixup_referral_attributes(&fs_locations->fattr);
3992 dprintk("%s: returned status = %d\n", __func__, status);
3996 #ifdef CONFIG_NFS_V4_1
3997 /* Destroy the slot table */
3998 static void nfs4_destroy_slot_table(struct nfs4_session *session)
4000 if (session->fc_slot_table.slots == NULL)
4002 kfree(session->fc_slot_table.slots);
4003 session->fc_slot_table.slots = NULL;
4007 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4009 struct nfs4_session *session;
4010 struct nfs4_slot_table *tbl;
4012 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4015 tbl = &session->fc_slot_table;
4016 spin_lock_init(&tbl->slot_tbl_lock);
4017 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4022 void nfs4_destroy_session(struct nfs4_session *session)
4024 nfs4_destroy_slot_table(session);
4028 #endif /* CONFIG_NFS_V4_1 */
4030 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
4031 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4032 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4033 .recover_open = nfs4_open_reclaim,
4034 .recover_lock = nfs4_lock_reclaim,
4037 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
4038 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4039 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4040 .recover_open = nfs4_open_expired,
4041 .recover_lock = nfs4_lock_expired,
4044 static const struct inode_operations nfs4_file_inode_operations = {
4045 .permission = nfs_permission,
4046 .getattr = nfs_getattr,
4047 .setattr = nfs_setattr,
4048 .getxattr = nfs4_getxattr,
4049 .setxattr = nfs4_setxattr,
4050 .listxattr = nfs4_listxattr,
4053 const struct nfs_rpc_ops nfs_v4_clientops = {
4054 .version = 4, /* protocol version */
4055 .dentry_ops = &nfs4_dentry_operations,
4056 .dir_inode_ops = &nfs4_dir_inode_operations,
4057 .file_inode_ops = &nfs4_file_inode_operations,
4058 .getroot = nfs4_proc_get_root,
4059 .getattr = nfs4_proc_getattr,
4060 .setattr = nfs4_proc_setattr,
4061 .lookupfh = nfs4_proc_lookupfh,
4062 .lookup = nfs4_proc_lookup,
4063 .access = nfs4_proc_access,
4064 .readlink = nfs4_proc_readlink,
4065 .create = nfs4_proc_create,
4066 .remove = nfs4_proc_remove,
4067 .unlink_setup = nfs4_proc_unlink_setup,
4068 .unlink_done = nfs4_proc_unlink_done,
4069 .rename = nfs4_proc_rename,
4070 .link = nfs4_proc_link,
4071 .symlink = nfs4_proc_symlink,
4072 .mkdir = nfs4_proc_mkdir,
4073 .rmdir = nfs4_proc_remove,
4074 .readdir = nfs4_proc_readdir,
4075 .mknod = nfs4_proc_mknod,
4076 .statfs = nfs4_proc_statfs,
4077 .fsinfo = nfs4_proc_fsinfo,
4078 .pathconf = nfs4_proc_pathconf,
4079 .set_capabilities = nfs4_server_capabilities,
4080 .decode_dirent = nfs4_decode_dirent,
4081 .read_setup = nfs4_proc_read_setup,
4082 .read_done = nfs4_read_done,
4083 .write_setup = nfs4_proc_write_setup,
4084 .write_done = nfs4_write_done,
4085 .commit_setup = nfs4_proc_commit_setup,
4086 .commit_done = nfs4_commit_done,
4087 .lock = nfs4_proc_lock,
4088 .clear_acl_cache = nfs4_zap_acl_attr,
4089 .close_context = nfs4_close_context,