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);
313 * nfs4_find_slot - efficiently look for a free slot
315 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
316 * If found, we mark the slot as used, update the highest_used_slotid,
317 * and respectively set up the sequence operation args.
318 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
320 * Note: must be called with under the slot_tbl_lock.
323 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
326 u8 ret_id = NFS4_MAX_SLOT_TABLE;
327 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
329 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
330 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
332 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
333 if (slotid >= tbl->max_slots)
335 __set_bit(slotid, tbl->used_slots);
336 if (slotid > tbl->highest_used_slotid)
337 tbl->highest_used_slotid = slotid;
340 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
341 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
345 static int nfs41_setup_sequence(struct nfs4_session *session,
346 struct nfs4_sequence_args *args,
347 struct nfs4_sequence_res *res,
349 struct rpc_task *task)
351 struct nfs4_slot *slot;
352 struct nfs4_slot_table *tbl;
355 dprintk("--> %s\n", __func__);
356 /* slot already allocated? */
357 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
360 memset(res, 0, sizeof(*res));
361 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
362 tbl = &session->fc_slot_table;
364 spin_lock(&tbl->slot_tbl_lock);
365 slotid = nfs4_find_slot(tbl, task);
366 if (slotid == NFS4_MAX_SLOT_TABLE) {
367 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
368 spin_unlock(&tbl->slot_tbl_lock);
369 dprintk("<-- %s: no free slots\n", __func__);
372 spin_unlock(&tbl->slot_tbl_lock);
374 slot = tbl->slots + slotid;
375 args->sa_slotid = slotid;
376 args->sa_cache_this = cache_reply;
378 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
380 res->sr_slotid = slotid;
381 res->sr_renewal_time = jiffies;
383 * sr_status is only set in decode_sequence, and so will remain
384 * set to 1 if an rpc level failure occurs.
390 int nfs4_setup_sequence(struct nfs_client *clp,
391 struct nfs4_sequence_args *args,
392 struct nfs4_sequence_res *res,
394 struct rpc_task *task)
398 dprintk("--> %s clp %p session %p sr_slotid %d\n",
399 __func__, clp, clp->cl_session, res->sr_slotid);
401 if (!nfs4_has_session(clp))
403 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
405 if (ret != -EAGAIN) {
406 /* terminate rpc task */
407 task->tk_status = ret;
408 task->tk_action = NULL;
411 dprintk("<-- %s status=%d\n", __func__, ret);
415 struct nfs41_call_sync_data {
416 struct nfs_client *clp;
417 struct nfs4_sequence_args *seq_args;
418 struct nfs4_sequence_res *seq_res;
422 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
424 struct nfs41_call_sync_data *data = calldata;
426 dprintk("--> %s data->clp->cl_session %p\n", __func__,
427 data->clp->cl_session);
428 if (nfs4_setup_sequence(data->clp, data->seq_args,
429 data->seq_res, data->cache_reply, task))
431 rpc_call_start(task);
434 struct rpc_call_ops nfs41_call_sync_ops = {
435 .rpc_call_prepare = nfs41_call_sync_prepare,
438 static int nfs4_call_sync_sequence(struct nfs_client *clp,
439 struct rpc_clnt *clnt,
440 struct rpc_message *msg,
441 struct nfs4_sequence_args *args,
442 struct nfs4_sequence_res *res,
446 struct rpc_task *task;
447 struct nfs41_call_sync_data data = {
451 .cache_reply = cache_reply,
453 struct rpc_task_setup task_setup = {
456 .callback_ops = &nfs41_call_sync_ops,
457 .callback_data = &data
460 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
461 task = rpc_run_task(&task_setup);
465 ret = task->tk_status;
471 int _nfs4_call_sync_session(struct nfs_server *server,
472 struct rpc_message *msg,
473 struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res,
477 return nfs4_call_sync_sequence(server->nfs_client, server->client,
478 msg, args, res, cache_reply);
481 #endif /* CONFIG_NFS_V4_1 */
483 int _nfs4_call_sync(struct nfs_server *server,
484 struct rpc_message *msg,
485 struct nfs4_sequence_args *args,
486 struct nfs4_sequence_res *res,
489 args->sa_session = res->sr_session = NULL;
490 return rpc_call_sync(server->client, msg, 0);
493 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
494 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
495 &(res)->seq_res, (cache_reply))
497 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
499 struct nfs_inode *nfsi = NFS_I(dir);
501 spin_lock(&dir->i_lock);
502 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
503 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
504 nfs_force_lookup_revalidate(dir);
505 nfsi->change_attr = cinfo->after;
506 spin_unlock(&dir->i_lock);
509 struct nfs4_opendata {
511 struct nfs_openargs o_arg;
512 struct nfs_openres o_res;
513 struct nfs_open_confirmargs c_arg;
514 struct nfs_open_confirmres c_res;
515 struct nfs_fattr f_attr;
516 struct nfs_fattr dir_attr;
519 struct nfs4_state_owner *owner;
520 struct nfs4_state *state;
522 unsigned long timestamp;
523 unsigned int rpc_done : 1;
529 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
531 p->o_res.f_attr = &p->f_attr;
532 p->o_res.dir_attr = &p->dir_attr;
533 p->o_res.seqid = p->o_arg.seqid;
534 p->c_res.seqid = p->c_arg.seqid;
535 p->o_res.server = p->o_arg.server;
536 nfs_fattr_init(&p->f_attr);
537 nfs_fattr_init(&p->dir_attr);
540 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
541 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
542 const struct iattr *attrs)
544 struct dentry *parent = dget_parent(path->dentry);
545 struct inode *dir = parent->d_inode;
546 struct nfs_server *server = NFS_SERVER(dir);
547 struct nfs4_opendata *p;
549 p = kzalloc(sizeof(*p), GFP_KERNEL);
552 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
553 if (p->o_arg.seqid == NULL)
555 p->path.mnt = mntget(path->mnt);
556 p->path.dentry = dget(path->dentry);
559 atomic_inc(&sp->so_count);
560 p->o_arg.fh = NFS_FH(dir);
561 p->o_arg.open_flags = flags;
562 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
563 p->o_arg.clientid = server->nfs_client->cl_clientid;
564 p->o_arg.id = sp->so_owner_id.id;
565 p->o_arg.name = &p->path.dentry->d_name;
566 p->o_arg.server = server;
567 p->o_arg.bitmask = server->attr_bitmask;
568 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
569 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
570 if (flags & O_EXCL) {
571 u32 *s = (u32 *) p->o_arg.u.verifier.data;
574 } else if (flags & O_CREAT) {
575 p->o_arg.u.attrs = &p->attrs;
576 memcpy(&p->attrs, attrs, sizeof(p->attrs));
578 p->c_arg.fh = &p->o_res.fh;
579 p->c_arg.stateid = &p->o_res.stateid;
580 p->c_arg.seqid = p->o_arg.seqid;
581 nfs4_init_opendata_res(p);
591 static void nfs4_opendata_free(struct kref *kref)
593 struct nfs4_opendata *p = container_of(kref,
594 struct nfs4_opendata, kref);
596 nfs_free_seqid(p->o_arg.seqid);
597 if (p->state != NULL)
598 nfs4_put_open_state(p->state);
599 nfs4_put_state_owner(p->owner);
605 static void nfs4_opendata_put(struct nfs4_opendata *p)
608 kref_put(&p->kref, nfs4_opendata_free);
611 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
615 ret = rpc_wait_for_completion_task(task);
619 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
623 if (open_mode & O_EXCL)
625 switch (mode & (FMODE_READ|FMODE_WRITE)) {
627 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
630 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
632 case FMODE_READ|FMODE_WRITE:
633 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
639 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
641 if ((delegation->type & fmode) != fmode)
643 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
645 nfs_mark_delegation_referenced(delegation);
649 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
658 case FMODE_READ|FMODE_WRITE:
661 nfs4_state_set_mode_locked(state, state->state | fmode);
664 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
666 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
667 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
668 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
671 set_bit(NFS_O_RDONLY_STATE, &state->flags);
674 set_bit(NFS_O_WRONLY_STATE, &state->flags);
676 case FMODE_READ|FMODE_WRITE:
677 set_bit(NFS_O_RDWR_STATE, &state->flags);
681 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
683 write_seqlock(&state->seqlock);
684 nfs_set_open_stateid_locked(state, stateid, fmode);
685 write_sequnlock(&state->seqlock);
688 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
691 * Protect the call to nfs4_state_set_mode_locked and
692 * serialise the stateid update
694 write_seqlock(&state->seqlock);
695 if (deleg_stateid != NULL) {
696 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
697 set_bit(NFS_DELEGATED_STATE, &state->flags);
699 if (open_stateid != NULL)
700 nfs_set_open_stateid_locked(state, open_stateid, fmode);
701 write_sequnlock(&state->seqlock);
702 spin_lock(&state->owner->so_lock);
703 update_open_stateflags(state, fmode);
704 spin_unlock(&state->owner->so_lock);
707 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
709 struct nfs_inode *nfsi = NFS_I(state->inode);
710 struct nfs_delegation *deleg_cur;
713 fmode &= (FMODE_READ|FMODE_WRITE);
716 deleg_cur = rcu_dereference(nfsi->delegation);
717 if (deleg_cur == NULL)
720 spin_lock(&deleg_cur->lock);
721 if (nfsi->delegation != deleg_cur ||
722 (deleg_cur->type & fmode) != fmode)
723 goto no_delegation_unlock;
725 if (delegation == NULL)
726 delegation = &deleg_cur->stateid;
727 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
728 goto no_delegation_unlock;
730 nfs_mark_delegation_referenced(deleg_cur);
731 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
733 no_delegation_unlock:
734 spin_unlock(&deleg_cur->lock);
738 if (!ret && open_stateid != NULL) {
739 __update_open_stateid(state, open_stateid, NULL, fmode);
747 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
749 struct nfs_delegation *delegation;
752 delegation = rcu_dereference(NFS_I(inode)->delegation);
753 if (delegation == NULL || (delegation->type & fmode) == fmode) {
758 nfs_inode_return_delegation(inode);
761 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
763 struct nfs4_state *state = opendata->state;
764 struct nfs_inode *nfsi = NFS_I(state->inode);
765 struct nfs_delegation *delegation;
766 int open_mode = opendata->o_arg.open_flags & O_EXCL;
767 fmode_t fmode = opendata->o_arg.fmode;
768 nfs4_stateid stateid;
772 if (can_open_cached(state, fmode, open_mode)) {
773 spin_lock(&state->owner->so_lock);
774 if (can_open_cached(state, fmode, open_mode)) {
775 update_open_stateflags(state, fmode);
776 spin_unlock(&state->owner->so_lock);
777 goto out_return_state;
779 spin_unlock(&state->owner->so_lock);
782 delegation = rcu_dereference(nfsi->delegation);
783 if (delegation == NULL ||
784 !can_open_delegated(delegation, fmode)) {
788 /* Save the delegation */
789 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
791 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
796 /* Try to update the stateid using the delegation */
797 if (update_open_stateid(state, NULL, &stateid, fmode))
798 goto out_return_state;
803 atomic_inc(&state->count);
807 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
810 struct nfs4_state *state = NULL;
811 struct nfs_delegation *delegation;
814 if (!data->rpc_done) {
815 state = nfs4_try_open_cached(data);
820 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
822 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
823 ret = PTR_ERR(inode);
827 state = nfs4_get_open_state(inode, data->owner);
830 if (data->o_res.delegation_type != 0) {
831 int delegation_flags = 0;
834 delegation = rcu_dereference(NFS_I(inode)->delegation);
836 delegation_flags = delegation->flags;
838 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
839 nfs_inode_set_delegation(state->inode,
840 data->owner->so_cred,
843 nfs_inode_reclaim_delegation(state->inode,
844 data->owner->so_cred,
848 update_open_stateid(state, &data->o_res.stateid, NULL,
859 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
861 struct nfs_inode *nfsi = NFS_I(state->inode);
862 struct nfs_open_context *ctx;
864 spin_lock(&state->inode->i_lock);
865 list_for_each_entry(ctx, &nfsi->open_files, list) {
866 if (ctx->state != state)
868 get_nfs_open_context(ctx);
869 spin_unlock(&state->inode->i_lock);
872 spin_unlock(&state->inode->i_lock);
873 return ERR_PTR(-ENOENT);
876 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
878 struct nfs4_opendata *opendata;
880 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
881 if (opendata == NULL)
882 return ERR_PTR(-ENOMEM);
883 opendata->state = state;
884 atomic_inc(&state->count);
888 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
890 struct nfs4_state *newstate;
893 opendata->o_arg.open_flags = 0;
894 opendata->o_arg.fmode = fmode;
895 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
896 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
897 nfs4_init_opendata_res(opendata);
898 ret = _nfs4_proc_open(opendata);
901 newstate = nfs4_opendata_to_nfs4_state(opendata);
902 if (IS_ERR(newstate))
903 return PTR_ERR(newstate);
904 nfs4_close_state(&opendata->path, newstate, fmode);
909 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
911 struct nfs4_state *newstate;
914 /* memory barrier prior to reading state->n_* */
915 clear_bit(NFS_DELEGATED_STATE, &state->flags);
917 if (state->n_rdwr != 0) {
918 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
921 if (newstate != state)
924 if (state->n_wronly != 0) {
925 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
928 if (newstate != state)
931 if (state->n_rdonly != 0) {
932 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
935 if (newstate != state)
939 * We may have performed cached opens for all three recoveries.
940 * Check if we need to update the current stateid.
942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
943 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
944 write_seqlock(&state->seqlock);
945 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
946 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
947 write_sequnlock(&state->seqlock);
954 * reclaim state on the server after a reboot.
956 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
958 struct nfs_delegation *delegation;
959 struct nfs4_opendata *opendata;
960 fmode_t delegation_type = 0;
963 opendata = nfs4_open_recoverdata_alloc(ctx, state);
964 if (IS_ERR(opendata))
965 return PTR_ERR(opendata);
966 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
967 opendata->o_arg.fh = NFS_FH(state->inode);
969 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
970 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
971 delegation_type = delegation->type;
973 opendata->o_arg.u.delegation_type = delegation_type;
974 status = nfs4_open_recover(opendata, state);
975 nfs4_opendata_put(opendata);
979 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
981 struct nfs_server *server = NFS_SERVER(state->inode);
982 struct nfs4_exception exception = { };
985 err = _nfs4_do_open_reclaim(ctx, state);
986 if (err != -NFS4ERR_DELAY)
988 nfs4_handle_exception(server, err, &exception);
989 } while (exception.retry);
993 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
995 struct nfs_open_context *ctx;
998 ctx = nfs4_state_find_open_context(state);
1000 return PTR_ERR(ctx);
1001 ret = nfs4_do_open_reclaim(ctx, state);
1002 put_nfs_open_context(ctx);
1006 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1008 struct nfs4_opendata *opendata;
1011 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1012 if (IS_ERR(opendata))
1013 return PTR_ERR(opendata);
1014 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1015 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1016 sizeof(opendata->o_arg.u.delegation.data));
1017 ret = nfs4_open_recover(opendata, state);
1018 nfs4_opendata_put(opendata);
1022 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1024 struct nfs4_exception exception = { };
1025 struct nfs_server *server = NFS_SERVER(state->inode);
1028 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1032 case -NFS4ERR_STALE_CLIENTID:
1033 case -NFS4ERR_STALE_STATEID:
1034 case -NFS4ERR_EXPIRED:
1035 /* Don't recall a delegation if it was lost */
1036 nfs4_schedule_state_recovery(server->nfs_client);
1039 err = nfs4_handle_exception(server, err, &exception);
1040 } while (exception.retry);
1044 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1046 struct nfs4_opendata *data = calldata;
1048 data->rpc_status = task->tk_status;
1049 if (RPC_ASSASSINATED(task))
1051 if (data->rpc_status == 0) {
1052 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1053 sizeof(data->o_res.stateid.data));
1054 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1055 renew_lease(data->o_res.server, data->timestamp);
1060 static void nfs4_open_confirm_release(void *calldata)
1062 struct nfs4_opendata *data = calldata;
1063 struct nfs4_state *state = NULL;
1065 /* If this request hasn't been cancelled, do nothing */
1066 if (data->cancelled == 0)
1068 /* In case of error, no cleanup! */
1069 if (!data->rpc_done)
1071 state = nfs4_opendata_to_nfs4_state(data);
1073 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1075 nfs4_opendata_put(data);
1078 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1079 .rpc_call_done = nfs4_open_confirm_done,
1080 .rpc_release = nfs4_open_confirm_release,
1084 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1086 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1088 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1089 struct rpc_task *task;
1090 struct rpc_message msg = {
1091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1092 .rpc_argp = &data->c_arg,
1093 .rpc_resp = &data->c_res,
1094 .rpc_cred = data->owner->so_cred,
1096 struct rpc_task_setup task_setup_data = {
1097 .rpc_client = server->client,
1098 .rpc_message = &msg,
1099 .callback_ops = &nfs4_open_confirm_ops,
1100 .callback_data = data,
1101 .workqueue = nfsiod_workqueue,
1102 .flags = RPC_TASK_ASYNC,
1106 kref_get(&data->kref);
1108 data->rpc_status = 0;
1109 data->timestamp = jiffies;
1110 task = rpc_run_task(&task_setup_data);
1112 return PTR_ERR(task);
1113 status = nfs4_wait_for_completion_rpc_task(task);
1115 data->cancelled = 1;
1118 status = data->rpc_status;
1123 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1125 struct nfs4_opendata *data = calldata;
1126 struct nfs4_state_owner *sp = data->owner;
1128 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1131 * Check if we still need to send an OPEN call, or if we can use
1132 * a delegation instead.
1134 if (data->state != NULL) {
1135 struct nfs_delegation *delegation;
1137 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1140 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1141 if (delegation != NULL &&
1142 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1148 /* Update sequence id. */
1149 data->o_arg.id = sp->so_owner_id.id;
1150 data->o_arg.clientid = sp->so_client->cl_clientid;
1151 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1152 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1153 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1155 data->timestamp = jiffies;
1156 rpc_call_start(task);
1159 task->tk_action = NULL;
1163 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1165 struct nfs4_opendata *data = calldata;
1167 data->rpc_status = task->tk_status;
1168 if (RPC_ASSASSINATED(task))
1170 if (task->tk_status == 0) {
1171 switch (data->o_res.f_attr->mode & S_IFMT) {
1175 data->rpc_status = -ELOOP;
1178 data->rpc_status = -EISDIR;
1181 data->rpc_status = -ENOTDIR;
1183 renew_lease(data->o_res.server, data->timestamp);
1184 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1185 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1190 static void nfs4_open_release(void *calldata)
1192 struct nfs4_opendata *data = calldata;
1193 struct nfs4_state *state = NULL;
1195 /* If this request hasn't been cancelled, do nothing */
1196 if (data->cancelled == 0)
1198 /* In case of error, no cleanup! */
1199 if (data->rpc_status != 0 || !data->rpc_done)
1201 /* In case we need an open_confirm, no cleanup! */
1202 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1204 state = nfs4_opendata_to_nfs4_state(data);
1206 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1208 nfs4_opendata_put(data);
1211 static const struct rpc_call_ops nfs4_open_ops = {
1212 .rpc_call_prepare = nfs4_open_prepare,
1213 .rpc_call_done = nfs4_open_done,
1214 .rpc_release = nfs4_open_release,
1218 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1220 static int _nfs4_proc_open(struct nfs4_opendata *data)
1222 struct inode *dir = data->dir->d_inode;
1223 struct nfs_server *server = NFS_SERVER(dir);
1224 struct nfs_openargs *o_arg = &data->o_arg;
1225 struct nfs_openres *o_res = &data->o_res;
1226 struct rpc_task *task;
1227 struct rpc_message msg = {
1228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1231 .rpc_cred = data->owner->so_cred,
1233 struct rpc_task_setup task_setup_data = {
1234 .rpc_client = server->client,
1235 .rpc_message = &msg,
1236 .callback_ops = &nfs4_open_ops,
1237 .callback_data = data,
1238 .workqueue = nfsiod_workqueue,
1239 .flags = RPC_TASK_ASYNC,
1243 kref_get(&data->kref);
1245 data->rpc_status = 0;
1246 data->cancelled = 0;
1247 task = rpc_run_task(&task_setup_data);
1249 return PTR_ERR(task);
1250 status = nfs4_wait_for_completion_rpc_task(task);
1252 data->cancelled = 1;
1255 status = data->rpc_status;
1257 if (status != 0 || !data->rpc_done)
1260 if (o_res->fh.size == 0)
1261 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1263 if (o_arg->open_flags & O_CREAT) {
1264 update_changeattr(dir, &o_res->cinfo);
1265 nfs_post_op_update_inode(dir, o_res->dir_attr);
1267 nfs_refresh_inode(dir, o_res->dir_attr);
1268 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1269 status = _nfs4_proc_open_confirm(data);
1273 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1274 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1278 static int nfs4_recover_expired_lease(struct nfs_server *server)
1280 struct nfs_client *clp = server->nfs_client;
1284 ret = nfs4_wait_clnt_recover(clp);
1287 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1288 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1290 nfs4_schedule_state_recovery(clp);
1297 * reclaim state on the server after a network partition.
1298 * Assumes caller holds the appropriate lock
1300 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1302 struct nfs4_opendata *opendata;
1305 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1306 if (IS_ERR(opendata))
1307 return PTR_ERR(opendata);
1308 ret = nfs4_open_recover(opendata, state);
1310 d_drop(ctx->path.dentry);
1311 nfs4_opendata_put(opendata);
1315 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1317 struct nfs_server *server = NFS_SERVER(state->inode);
1318 struct nfs4_exception exception = { };
1322 err = _nfs4_open_expired(ctx, state);
1323 if (err != -NFS4ERR_DELAY)
1325 nfs4_handle_exception(server, err, &exception);
1326 } while (exception.retry);
1330 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1332 struct nfs_open_context *ctx;
1335 ctx = nfs4_state_find_open_context(state);
1337 return PTR_ERR(ctx);
1338 ret = nfs4_do_open_expired(ctx, state);
1339 put_nfs_open_context(ctx);
1344 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1345 * fields corresponding to attributes that were used to store the verifier.
1346 * Make sure we clobber those fields in the later setattr call
1348 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1350 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1351 !(sattr->ia_valid & ATTR_ATIME_SET))
1352 sattr->ia_valid |= ATTR_ATIME;
1354 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1355 !(sattr->ia_valid & ATTR_MTIME_SET))
1356 sattr->ia_valid |= ATTR_MTIME;
1360 * Returns a referenced nfs4_state
1362 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)
1364 struct nfs4_state_owner *sp;
1365 struct nfs4_state *state = NULL;
1366 struct nfs_server *server = NFS_SERVER(dir);
1367 struct nfs4_opendata *opendata;
1370 /* Protect against reboot recovery conflicts */
1372 if (!(sp = nfs4_get_state_owner(server, cred))) {
1373 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1376 status = nfs4_recover_expired_lease(server);
1378 goto err_put_state_owner;
1379 if (path->dentry->d_inode != NULL)
1380 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1382 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1383 if (opendata == NULL)
1384 goto err_put_state_owner;
1386 if (path->dentry->d_inode != NULL)
1387 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1389 status = _nfs4_proc_open(opendata);
1391 goto err_opendata_put;
1393 if (opendata->o_arg.open_flags & O_EXCL)
1394 nfs4_exclusive_attrset(opendata, sattr);
1396 state = nfs4_opendata_to_nfs4_state(opendata);
1397 status = PTR_ERR(state);
1399 goto err_opendata_put;
1400 nfs4_opendata_put(opendata);
1401 nfs4_put_state_owner(sp);
1405 nfs4_opendata_put(opendata);
1406 err_put_state_owner:
1407 nfs4_put_state_owner(sp);
1414 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)
1416 struct nfs4_exception exception = { };
1417 struct nfs4_state *res;
1421 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1424 /* NOTE: BAD_SEQID means the server and client disagree about the
1425 * book-keeping w.r.t. state-changing operations
1426 * (OPEN/CLOSE/LOCK/LOCKU...)
1427 * It is actually a sign of a bug on the client or on the server.
1429 * If we receive a BAD_SEQID error in the particular case of
1430 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1431 * have unhashed the old state_owner for us, and that we can
1432 * therefore safely retry using a new one. We should still warn
1433 * the user though...
1435 if (status == -NFS4ERR_BAD_SEQID) {
1436 printk(KERN_WARNING "NFS: v4 server %s "
1437 " returned a bad sequence-id error!\n",
1438 NFS_SERVER(dir)->nfs_client->cl_hostname);
1439 exception.retry = 1;
1443 * BAD_STATEID on OPEN means that the server cancelled our
1444 * state before it received the OPEN_CONFIRM.
1445 * Recover by retrying the request as per the discussion
1446 * on Page 181 of RFC3530.
1448 if (status == -NFS4ERR_BAD_STATEID) {
1449 exception.retry = 1;
1452 if (status == -EAGAIN) {
1453 /* We must have found a delegation */
1454 exception.retry = 1;
1457 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1458 status, &exception));
1459 } while (exception.retry);
1463 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1464 struct nfs_fattr *fattr, struct iattr *sattr,
1465 struct nfs4_state *state)
1467 struct nfs_server *server = NFS_SERVER(inode);
1468 struct nfs_setattrargs arg = {
1469 .fh = NFS_FH(inode),
1472 .bitmask = server->attr_bitmask,
1474 struct nfs_setattrres res = {
1478 struct rpc_message msg = {
1479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1484 unsigned long timestamp = jiffies;
1487 nfs_fattr_init(fattr);
1489 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1490 /* Use that stateid */
1491 } else if (state != NULL) {
1492 nfs4_copy_stateid(&arg.stateid, state, current->files);
1494 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1496 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1497 if (status == 0 && state != NULL)
1498 renew_lease(server, timestamp);
1502 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1503 struct nfs_fattr *fattr, struct iattr *sattr,
1504 struct nfs4_state *state)
1506 struct nfs_server *server = NFS_SERVER(inode);
1507 struct nfs4_exception exception = { };
1510 err = nfs4_handle_exception(server,
1511 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1513 } while (exception.retry);
1517 struct nfs4_closedata {
1519 struct inode *inode;
1520 struct nfs4_state *state;
1521 struct nfs_closeargs arg;
1522 struct nfs_closeres res;
1523 struct nfs_fattr fattr;
1524 unsigned long timestamp;
1527 static void nfs4_free_closedata(void *data)
1529 struct nfs4_closedata *calldata = data;
1530 struct nfs4_state_owner *sp = calldata->state->owner;
1532 nfs4_put_open_state(calldata->state);
1533 nfs_free_seqid(calldata->arg.seqid);
1534 nfs4_put_state_owner(sp);
1535 path_put(&calldata->path);
1539 static void nfs4_close_done(struct rpc_task *task, void *data)
1541 struct nfs4_closedata *calldata = data;
1542 struct nfs4_state *state = calldata->state;
1543 struct nfs_server *server = NFS_SERVER(calldata->inode);
1545 if (RPC_ASSASSINATED(task))
1547 /* hmm. we are done with the inode, and in the process of freeing
1548 * the state_owner. we keep this around to process errors
1550 switch (task->tk_status) {
1552 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1553 renew_lease(server, calldata->timestamp);
1555 case -NFS4ERR_STALE_STATEID:
1556 case -NFS4ERR_OLD_STATEID:
1557 case -NFS4ERR_BAD_STATEID:
1558 case -NFS4ERR_EXPIRED:
1559 if (calldata->arg.fmode == 0)
1562 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1563 rpc_restart_call(task);
1567 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1570 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1572 struct nfs4_closedata *calldata = data;
1573 struct nfs4_state *state = calldata->state;
1574 int clear_rd, clear_wr, clear_rdwr;
1576 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1579 clear_rd = clear_wr = clear_rdwr = 0;
1580 spin_lock(&state->owner->so_lock);
1581 /* Calculate the change in open mode */
1582 if (state->n_rdwr == 0) {
1583 if (state->n_rdonly == 0) {
1584 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1585 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1587 if (state->n_wronly == 0) {
1588 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1589 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1592 spin_unlock(&state->owner->so_lock);
1593 if (!clear_rd && !clear_wr && !clear_rdwr) {
1594 /* Note: exit _without_ calling nfs4_close_done */
1595 task->tk_action = NULL;
1598 nfs_fattr_init(calldata->res.fattr);
1599 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1600 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1601 calldata->arg.fmode = FMODE_READ;
1602 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1603 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1604 calldata->arg.fmode = FMODE_WRITE;
1606 calldata->timestamp = jiffies;
1607 rpc_call_start(task);
1610 static const struct rpc_call_ops nfs4_close_ops = {
1611 .rpc_call_prepare = nfs4_close_prepare,
1612 .rpc_call_done = nfs4_close_done,
1613 .rpc_release = nfs4_free_closedata,
1617 * It is possible for data to be read/written from a mem-mapped file
1618 * after the sys_close call (which hits the vfs layer as a flush).
1619 * This means that we can't safely call nfsv4 close on a file until
1620 * the inode is cleared. This in turn means that we are not good
1621 * NFSv4 citizens - we do not indicate to the server to update the file's
1622 * share state even when we are done with one of the three share
1623 * stateid's in the inode.
1625 * NOTE: Caller must be holding the sp->so_owner semaphore!
1627 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1629 struct nfs_server *server = NFS_SERVER(state->inode);
1630 struct nfs4_closedata *calldata;
1631 struct nfs4_state_owner *sp = state->owner;
1632 struct rpc_task *task;
1633 struct rpc_message msg = {
1634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1635 .rpc_cred = state->owner->so_cred,
1637 struct rpc_task_setup task_setup_data = {
1638 .rpc_client = server->client,
1639 .rpc_message = &msg,
1640 .callback_ops = &nfs4_close_ops,
1641 .workqueue = nfsiod_workqueue,
1642 .flags = RPC_TASK_ASYNC,
1644 int status = -ENOMEM;
1646 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1647 if (calldata == NULL)
1649 calldata->inode = state->inode;
1650 calldata->state = state;
1651 calldata->arg.fh = NFS_FH(state->inode);
1652 calldata->arg.stateid = &state->open_stateid;
1653 /* Serialization for the sequence id */
1654 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1655 if (calldata->arg.seqid == NULL)
1656 goto out_free_calldata;
1657 calldata->arg.fmode = 0;
1658 calldata->arg.bitmask = server->cache_consistency_bitmask;
1659 calldata->res.fattr = &calldata->fattr;
1660 calldata->res.seqid = calldata->arg.seqid;
1661 calldata->res.server = server;
1662 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1663 calldata->path.mnt = mntget(path->mnt);
1664 calldata->path.dentry = dget(path->dentry);
1666 msg.rpc_argp = &calldata->arg,
1667 msg.rpc_resp = &calldata->res,
1668 task_setup_data.callback_data = calldata;
1669 task = rpc_run_task(&task_setup_data);
1671 return PTR_ERR(task);
1674 status = rpc_wait_for_completion_task(task);
1680 nfs4_put_open_state(state);
1681 nfs4_put_state_owner(sp);
1685 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1690 /* If the open_intent is for execute, we have an extra check to make */
1691 if (fmode & FMODE_EXEC) {
1692 ret = nfs_may_open(state->inode,
1693 state->owner->so_cred,
1694 nd->intent.open.flags);
1698 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1699 if (!IS_ERR(filp)) {
1700 struct nfs_open_context *ctx;
1701 ctx = nfs_file_open_context(filp);
1705 ret = PTR_ERR(filp);
1707 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1712 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1714 struct path path = {
1715 .mnt = nd->path.mnt,
1718 struct dentry *parent;
1720 struct rpc_cred *cred;
1721 struct nfs4_state *state;
1723 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1725 if (nd->flags & LOOKUP_CREATE) {
1726 attr.ia_mode = nd->intent.open.create_mode;
1727 attr.ia_valid = ATTR_MODE;
1728 if (!IS_POSIXACL(dir))
1729 attr.ia_mode &= ~current_umask();
1732 BUG_ON(nd->intent.open.flags & O_CREAT);
1735 cred = rpc_lookup_cred();
1737 return (struct dentry *)cred;
1738 parent = dentry->d_parent;
1739 /* Protect against concurrent sillydeletes */
1740 nfs_block_sillyrename(parent);
1741 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1743 if (IS_ERR(state)) {
1744 if (PTR_ERR(state) == -ENOENT) {
1745 d_add(dentry, NULL);
1746 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1748 nfs_unblock_sillyrename(parent);
1749 return (struct dentry *)state;
1751 res = d_add_unique(dentry, igrab(state->inode));
1754 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1755 nfs_unblock_sillyrename(parent);
1756 nfs4_intent_set_file(nd, &path, state, fmode);
1761 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1763 struct path path = {
1764 .mnt = nd->path.mnt,
1767 struct rpc_cred *cred;
1768 struct nfs4_state *state;
1769 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1771 cred = rpc_lookup_cred();
1773 return PTR_ERR(cred);
1774 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1776 if (IS_ERR(state)) {
1777 switch (PTR_ERR(state)) {
1783 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1789 if (state->inode == dentry->d_inode) {
1790 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1791 nfs4_intent_set_file(nd, &path, state, fmode);
1794 nfs4_close_sync(&path, state, fmode);
1800 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1802 if (ctx->state == NULL)
1805 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1807 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1810 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1812 struct nfs4_server_caps_arg args = {
1815 struct nfs4_server_caps_res res = {};
1816 struct rpc_message msg = {
1817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1823 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1825 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1826 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1827 server->caps |= NFS_CAP_ACLS;
1828 if (res.has_links != 0)
1829 server->caps |= NFS_CAP_HARDLINKS;
1830 if (res.has_symlinks != 0)
1831 server->caps |= NFS_CAP_SYMLINKS;
1832 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1833 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1834 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1835 server->acl_bitmask = res.acl_bitmask;
1841 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1843 struct nfs4_exception exception = { };
1846 err = nfs4_handle_exception(server,
1847 _nfs4_server_capabilities(server, fhandle),
1849 } while (exception.retry);
1853 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1854 struct nfs_fsinfo *info)
1856 struct nfs4_lookup_root_arg args = {
1857 .bitmask = nfs4_fattr_bitmap,
1859 struct nfs4_lookup_res res = {
1861 .fattr = info->fattr,
1864 struct rpc_message msg = {
1865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1869 nfs_fattr_init(info->fattr);
1870 return nfs4_call_sync(server, &msg, &args, &res, 0);
1873 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1874 struct nfs_fsinfo *info)
1876 struct nfs4_exception exception = { };
1879 err = nfs4_handle_exception(server,
1880 _nfs4_lookup_root(server, fhandle, info),
1882 } while (exception.retry);
1887 * get the file handle for the "/" directory on the server
1889 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1890 struct nfs_fsinfo *info)
1894 status = nfs4_lookup_root(server, fhandle, info);
1896 status = nfs4_server_capabilities(server, fhandle);
1898 status = nfs4_do_fsinfo(server, fhandle, info);
1899 return nfs4_map_errors(status);
1903 * Get locations and (maybe) other attributes of a referral.
1904 * Note that we'll actually follow the referral later when
1905 * we detect fsid mismatch in inode revalidation
1907 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1909 int status = -ENOMEM;
1910 struct page *page = NULL;
1911 struct nfs4_fs_locations *locations = NULL;
1913 page = alloc_page(GFP_KERNEL);
1916 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1917 if (locations == NULL)
1920 status = nfs4_proc_fs_locations(dir, name, locations, page);
1923 /* Make sure server returned a different fsid for the referral */
1924 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1925 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1930 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1931 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1933 fattr->mode = S_IFDIR;
1934 memset(fhandle, 0, sizeof(struct nfs_fh));
1943 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1945 struct nfs4_getattr_arg args = {
1947 .bitmask = server->attr_bitmask,
1949 struct nfs4_getattr_res res = {
1953 struct rpc_message msg = {
1954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1959 nfs_fattr_init(fattr);
1960 return nfs4_call_sync(server, &msg, &args, &res, 0);
1963 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1965 struct nfs4_exception exception = { };
1968 err = nfs4_handle_exception(server,
1969 _nfs4_proc_getattr(server, fhandle, fattr),
1971 } while (exception.retry);
1976 * The file is not closed if it is opened due to the a request to change
1977 * the size of the file. The open call will not be needed once the
1978 * VFS layer lookup-intents are implemented.
1980 * Close is called when the inode is destroyed.
1981 * If we haven't opened the file for O_WRONLY, we
1982 * need to in the size_change case to obtain a stateid.
1985 * Because OPEN is always done by name in nfsv4, it is
1986 * possible that we opened a different file by the same
1987 * name. We can recognize this race condition, but we
1988 * can't do anything about it besides returning an error.
1990 * This will be fixed with VFS changes (lookup-intent).
1993 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1994 struct iattr *sattr)
1996 struct inode *inode = dentry->d_inode;
1997 struct rpc_cred *cred = NULL;
1998 struct nfs4_state *state = NULL;
2001 nfs_fattr_init(fattr);
2003 /* Search for an existing open(O_WRITE) file */
2004 if (sattr->ia_valid & ATTR_FILE) {
2005 struct nfs_open_context *ctx;
2007 ctx = nfs_file_open_context(sattr->ia_file);
2014 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2016 nfs_setattr_update_inode(inode, sattr);
2020 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2021 const struct qstr *name, struct nfs_fh *fhandle,
2022 struct nfs_fattr *fattr)
2025 struct nfs4_lookup_arg args = {
2026 .bitmask = server->attr_bitmask,
2030 struct nfs4_lookup_res res = {
2035 struct rpc_message msg = {
2036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2041 nfs_fattr_init(fattr);
2043 dprintk("NFS call lookupfh %s\n", name->name);
2044 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2045 dprintk("NFS reply lookupfh: %d\n", status);
2049 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2050 struct qstr *name, struct nfs_fh *fhandle,
2051 struct nfs_fattr *fattr)
2053 struct nfs4_exception exception = { };
2056 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2058 if (err == -NFS4ERR_MOVED) {
2062 err = nfs4_handle_exception(server, err, &exception);
2063 } while (exception.retry);
2067 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2068 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2072 dprintk("NFS call lookup %s\n", name->name);
2073 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2074 if (status == -NFS4ERR_MOVED)
2075 status = nfs4_get_referral(dir, name, fattr, fhandle);
2076 dprintk("NFS reply lookup: %d\n", status);
2080 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2082 struct nfs4_exception exception = { };
2085 err = nfs4_handle_exception(NFS_SERVER(dir),
2086 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2088 } while (exception.retry);
2092 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2094 struct nfs_server *server = NFS_SERVER(inode);
2095 struct nfs_fattr fattr;
2096 struct nfs4_accessargs args = {
2097 .fh = NFS_FH(inode),
2098 .bitmask = server->attr_bitmask,
2100 struct nfs4_accessres res = {
2104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2108 .rpc_cred = entry->cred,
2110 int mode = entry->mask;
2114 * Determine which access bits we want to ask for...
2116 if (mode & MAY_READ)
2117 args.access |= NFS4_ACCESS_READ;
2118 if (S_ISDIR(inode->i_mode)) {
2119 if (mode & MAY_WRITE)
2120 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2121 if (mode & MAY_EXEC)
2122 args.access |= NFS4_ACCESS_LOOKUP;
2124 if (mode & MAY_WRITE)
2125 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2126 if (mode & MAY_EXEC)
2127 args.access |= NFS4_ACCESS_EXECUTE;
2129 nfs_fattr_init(&fattr);
2130 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2133 if (res.access & NFS4_ACCESS_READ)
2134 entry->mask |= MAY_READ;
2135 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2136 entry->mask |= MAY_WRITE;
2137 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2138 entry->mask |= MAY_EXEC;
2139 nfs_refresh_inode(inode, &fattr);
2144 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2146 struct nfs4_exception exception = { };
2149 err = nfs4_handle_exception(NFS_SERVER(inode),
2150 _nfs4_proc_access(inode, entry),
2152 } while (exception.retry);
2157 * TODO: For the time being, we don't try to get any attributes
2158 * along with any of the zero-copy operations READ, READDIR,
2161 * In the case of the first three, we want to put the GETATTR
2162 * after the read-type operation -- this is because it is hard
2163 * to predict the length of a GETATTR response in v4, and thus
2164 * align the READ data correctly. This means that the GETATTR
2165 * may end up partially falling into the page cache, and we should
2166 * shift it into the 'tail' of the xdr_buf before processing.
2167 * To do this efficiently, we need to know the total length
2168 * of data received, which doesn't seem to be available outside
2171 * In the case of WRITE, we also want to put the GETATTR after
2172 * the operation -- in this case because we want to make sure
2173 * we get the post-operation mtime and size. This means that
2174 * we can't use xdr_encode_pages() as written: we need a variant
2175 * of it which would leave room in the 'tail' iovec.
2177 * Both of these changes to the XDR layer would in fact be quite
2178 * minor, but I decided to leave them for a subsequent patch.
2180 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2181 unsigned int pgbase, unsigned int pglen)
2183 struct nfs4_readlink args = {
2184 .fh = NFS_FH(inode),
2189 struct nfs4_readlink_res res;
2190 struct rpc_message msg = {
2191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2196 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2199 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2200 unsigned int pgbase, unsigned int pglen)
2202 struct nfs4_exception exception = { };
2205 err = nfs4_handle_exception(NFS_SERVER(inode),
2206 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2208 } while (exception.retry);
2214 * We will need to arrange for the VFS layer to provide an atomic open.
2215 * Until then, this create/open method is prone to inefficiency and race
2216 * conditions due to the lookup, create, and open VFS calls from sys_open()
2217 * placed on the wire.
2219 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2220 * The file will be opened again in the subsequent VFS open call
2221 * (nfs4_proc_file_open).
2223 * The open for read will just hang around to be used by any process that
2224 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2228 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2229 int flags, struct nameidata *nd)
2231 struct path path = {
2232 .mnt = nd->path.mnt,
2235 struct nfs4_state *state;
2236 struct rpc_cred *cred;
2237 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2240 cred = rpc_lookup_cred();
2242 status = PTR_ERR(cred);
2245 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2247 if (IS_ERR(state)) {
2248 status = PTR_ERR(state);
2251 d_add(dentry, igrab(state->inode));
2252 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2253 if (flags & O_EXCL) {
2254 struct nfs_fattr fattr;
2255 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2257 nfs_setattr_update_inode(state->inode, sattr);
2258 nfs_post_op_update_inode(state->inode, &fattr);
2260 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2261 status = nfs4_intent_set_file(nd, &path, state, fmode);
2263 nfs4_close_sync(&path, state, fmode);
2270 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2272 struct nfs_server *server = NFS_SERVER(dir);
2273 struct nfs_removeargs args = {
2275 .name.len = name->len,
2276 .name.name = name->name,
2277 .bitmask = server->attr_bitmask,
2279 struct nfs_removeres res = {
2282 struct rpc_message msg = {
2283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2289 nfs_fattr_init(&res.dir_attr);
2290 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2292 update_changeattr(dir, &res.cinfo);
2293 nfs_post_op_update_inode(dir, &res.dir_attr);
2298 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2300 struct nfs4_exception exception = { };
2303 err = nfs4_handle_exception(NFS_SERVER(dir),
2304 _nfs4_proc_remove(dir, name),
2306 } while (exception.retry);
2310 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2312 struct nfs_server *server = NFS_SERVER(dir);
2313 struct nfs_removeargs *args = msg->rpc_argp;
2314 struct nfs_removeres *res = msg->rpc_resp;
2316 args->bitmask = server->cache_consistency_bitmask;
2317 res->server = server;
2318 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2321 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2323 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2325 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2327 update_changeattr(dir, &res->cinfo);
2328 nfs_post_op_update_inode(dir, &res->dir_attr);
2332 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2333 struct inode *new_dir, struct qstr *new_name)
2335 struct nfs_server *server = NFS_SERVER(old_dir);
2336 struct nfs4_rename_arg arg = {
2337 .old_dir = NFS_FH(old_dir),
2338 .new_dir = NFS_FH(new_dir),
2339 .old_name = old_name,
2340 .new_name = new_name,
2341 .bitmask = server->attr_bitmask,
2343 struct nfs_fattr old_fattr, new_fattr;
2344 struct nfs4_rename_res res = {
2346 .old_fattr = &old_fattr,
2347 .new_fattr = &new_fattr,
2349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2356 nfs_fattr_init(res.old_fattr);
2357 nfs_fattr_init(res.new_fattr);
2358 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2361 update_changeattr(old_dir, &res.old_cinfo);
2362 nfs_post_op_update_inode(old_dir, res.old_fattr);
2363 update_changeattr(new_dir, &res.new_cinfo);
2364 nfs_post_op_update_inode(new_dir, res.new_fattr);
2369 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2370 struct inode *new_dir, struct qstr *new_name)
2372 struct nfs4_exception exception = { };
2375 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2376 _nfs4_proc_rename(old_dir, old_name,
2379 } while (exception.retry);
2383 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2385 struct nfs_server *server = NFS_SERVER(inode);
2386 struct nfs4_link_arg arg = {
2387 .fh = NFS_FH(inode),
2388 .dir_fh = NFS_FH(dir),
2390 .bitmask = server->attr_bitmask,
2392 struct nfs_fattr fattr, dir_attr;
2393 struct nfs4_link_res res = {
2396 .dir_attr = &dir_attr,
2398 struct rpc_message msg = {
2399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2405 nfs_fattr_init(res.fattr);
2406 nfs_fattr_init(res.dir_attr);
2407 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2409 update_changeattr(dir, &res.cinfo);
2410 nfs_post_op_update_inode(dir, res.dir_attr);
2411 nfs_post_op_update_inode(inode, res.fattr);
2417 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2419 struct nfs4_exception exception = { };
2422 err = nfs4_handle_exception(NFS_SERVER(inode),
2423 _nfs4_proc_link(inode, dir, name),
2425 } while (exception.retry);
2429 struct nfs4_createdata {
2430 struct rpc_message msg;
2431 struct nfs4_create_arg arg;
2432 struct nfs4_create_res res;
2434 struct nfs_fattr fattr;
2435 struct nfs_fattr dir_fattr;
2438 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2439 struct qstr *name, struct iattr *sattr, u32 ftype)
2441 struct nfs4_createdata *data;
2443 data = kzalloc(sizeof(*data), GFP_KERNEL);
2445 struct nfs_server *server = NFS_SERVER(dir);
2447 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2448 data->msg.rpc_argp = &data->arg;
2449 data->msg.rpc_resp = &data->res;
2450 data->arg.dir_fh = NFS_FH(dir);
2451 data->arg.server = server;
2452 data->arg.name = name;
2453 data->arg.attrs = sattr;
2454 data->arg.ftype = ftype;
2455 data->arg.bitmask = server->attr_bitmask;
2456 data->res.server = server;
2457 data->res.fh = &data->fh;
2458 data->res.fattr = &data->fattr;
2459 data->res.dir_fattr = &data->dir_fattr;
2460 nfs_fattr_init(data->res.fattr);
2461 nfs_fattr_init(data->res.dir_fattr);
2466 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2468 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2469 &data->arg, &data->res, 1);
2471 update_changeattr(dir, &data->res.dir_cinfo);
2472 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2473 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2478 static void nfs4_free_createdata(struct nfs4_createdata *data)
2483 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2484 struct page *page, unsigned int len, struct iattr *sattr)
2486 struct nfs4_createdata *data;
2487 int status = -ENAMETOOLONG;
2489 if (len > NFS4_MAXPATHLEN)
2493 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2497 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2498 data->arg.u.symlink.pages = &page;
2499 data->arg.u.symlink.len = len;
2501 status = nfs4_do_create(dir, dentry, data);
2503 nfs4_free_createdata(data);
2508 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2509 struct page *page, unsigned int len, struct iattr *sattr)
2511 struct nfs4_exception exception = { };
2514 err = nfs4_handle_exception(NFS_SERVER(dir),
2515 _nfs4_proc_symlink(dir, dentry, page,
2518 } while (exception.retry);
2522 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2523 struct iattr *sattr)
2525 struct nfs4_createdata *data;
2526 int status = -ENOMEM;
2528 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2532 status = nfs4_do_create(dir, dentry, data);
2534 nfs4_free_createdata(data);
2539 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2540 struct iattr *sattr)
2542 struct nfs4_exception exception = { };
2545 err = nfs4_handle_exception(NFS_SERVER(dir),
2546 _nfs4_proc_mkdir(dir, dentry, sattr),
2548 } while (exception.retry);
2552 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2553 u64 cookie, struct page *page, unsigned int count, int plus)
2555 struct inode *dir = dentry->d_inode;
2556 struct nfs4_readdir_arg args = {
2561 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2563 struct nfs4_readdir_res res;
2564 struct rpc_message msg = {
2565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2572 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2573 dentry->d_parent->d_name.name,
2574 dentry->d_name.name,
2575 (unsigned long long)cookie);
2576 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2577 res.pgbase = args.pgbase;
2578 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2580 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2582 nfs_invalidate_atime(dir);
2584 dprintk("%s: returns %d\n", __func__, status);
2588 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2589 u64 cookie, struct page *page, unsigned int count, int plus)
2591 struct nfs4_exception exception = { };
2594 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2595 _nfs4_proc_readdir(dentry, cred, cookie,
2598 } while (exception.retry);
2602 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2603 struct iattr *sattr, dev_t rdev)
2605 struct nfs4_createdata *data;
2606 int mode = sattr->ia_mode;
2607 int status = -ENOMEM;
2609 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2610 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2612 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2617 data->arg.ftype = NF4FIFO;
2618 else if (S_ISBLK(mode)) {
2619 data->arg.ftype = NF4BLK;
2620 data->arg.u.device.specdata1 = MAJOR(rdev);
2621 data->arg.u.device.specdata2 = MINOR(rdev);
2623 else if (S_ISCHR(mode)) {
2624 data->arg.ftype = NF4CHR;
2625 data->arg.u.device.specdata1 = MAJOR(rdev);
2626 data->arg.u.device.specdata2 = MINOR(rdev);
2629 status = nfs4_do_create(dir, dentry, data);
2631 nfs4_free_createdata(data);
2636 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2637 struct iattr *sattr, dev_t rdev)
2639 struct nfs4_exception exception = { };
2642 err = nfs4_handle_exception(NFS_SERVER(dir),
2643 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2645 } while (exception.retry);
2649 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2650 struct nfs_fsstat *fsstat)
2652 struct nfs4_statfs_arg args = {
2654 .bitmask = server->attr_bitmask,
2656 struct nfs4_statfs_res res = {
2659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2665 nfs_fattr_init(fsstat->fattr);
2666 return nfs4_call_sync(server, &msg, &args, &res, 0);
2669 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2671 struct nfs4_exception exception = { };
2674 err = nfs4_handle_exception(server,
2675 _nfs4_proc_statfs(server, fhandle, fsstat),
2677 } while (exception.retry);
2681 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2682 struct nfs_fsinfo *fsinfo)
2684 struct nfs4_fsinfo_arg args = {
2686 .bitmask = server->attr_bitmask,
2688 struct nfs4_fsinfo_res res = {
2691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2697 return nfs4_call_sync(server, &msg, &args, &res, 0);
2700 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2702 struct nfs4_exception exception = { };
2706 err = nfs4_handle_exception(server,
2707 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2709 } while (exception.retry);
2713 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2715 nfs_fattr_init(fsinfo->fattr);
2716 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2719 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2720 struct nfs_pathconf *pathconf)
2722 struct nfs4_pathconf_arg args = {
2724 .bitmask = server->attr_bitmask,
2726 struct nfs4_pathconf_res res = {
2727 .pathconf = pathconf,
2729 struct rpc_message msg = {
2730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2735 /* None of the pathconf attributes are mandatory to implement */
2736 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2737 memset(pathconf, 0, sizeof(*pathconf));
2741 nfs_fattr_init(pathconf->fattr);
2742 return nfs4_call_sync(server, &msg, &args, &res, 0);
2745 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2746 struct nfs_pathconf *pathconf)
2748 struct nfs4_exception exception = { };
2752 err = nfs4_handle_exception(server,
2753 _nfs4_proc_pathconf(server, fhandle, pathconf),
2755 } while (exception.retry);
2759 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2761 struct nfs_server *server = NFS_SERVER(data->inode);
2763 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2764 rpc_restart_call(task);
2768 nfs_invalidate_atime(data->inode);
2769 if (task->tk_status > 0)
2770 renew_lease(server, data->timestamp);
2774 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2776 data->timestamp = jiffies;
2777 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2780 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2782 struct inode *inode = data->inode;
2784 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2785 rpc_restart_call(task);
2788 if (task->tk_status >= 0) {
2789 renew_lease(NFS_SERVER(inode), data->timestamp);
2790 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2795 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2797 struct nfs_server *server = NFS_SERVER(data->inode);
2799 data->args.bitmask = server->cache_consistency_bitmask;
2800 data->res.server = server;
2801 data->timestamp = jiffies;
2803 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2806 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2808 struct inode *inode = data->inode;
2810 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2811 rpc_restart_call(task);
2814 nfs_refresh_inode(inode, data->res.fattr);
2818 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2820 struct nfs_server *server = NFS_SERVER(data->inode);
2822 data->args.bitmask = server->cache_consistency_bitmask;
2823 data->res.server = server;
2824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2828 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2829 * standalone procedure for queueing an asynchronous RENEW.
2831 static void nfs4_renew_done(struct rpc_task *task, void *data)
2833 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2834 unsigned long timestamp = (unsigned long)data;
2836 if (task->tk_status < 0) {
2837 /* Unless we're shutting down, schedule state recovery! */
2838 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2839 nfs4_schedule_state_recovery(clp);
2842 spin_lock(&clp->cl_lock);
2843 if (time_before(clp->cl_last_renewal,timestamp))
2844 clp->cl_last_renewal = timestamp;
2845 spin_unlock(&clp->cl_lock);
2848 static const struct rpc_call_ops nfs4_renew_ops = {
2849 .rpc_call_done = nfs4_renew_done,
2852 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2860 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2861 &nfs4_renew_ops, (void *)jiffies);
2864 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2866 struct rpc_message msg = {
2867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2871 unsigned long now = jiffies;
2874 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2877 spin_lock(&clp->cl_lock);
2878 if (time_before(clp->cl_last_renewal,now))
2879 clp->cl_last_renewal = now;
2880 spin_unlock(&clp->cl_lock);
2884 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2886 return (server->caps & NFS_CAP_ACLS)
2887 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2888 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2891 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2892 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2895 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2897 static void buf_to_pages(const void *buf, size_t buflen,
2898 struct page **pages, unsigned int *pgbase)
2900 const void *p = buf;
2902 *pgbase = offset_in_page(buf);
2904 while (p < buf + buflen) {
2905 *(pages++) = virt_to_page(p);
2906 p += PAGE_CACHE_SIZE;
2910 struct nfs4_cached_acl {
2916 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2918 struct nfs_inode *nfsi = NFS_I(inode);
2920 spin_lock(&inode->i_lock);
2921 kfree(nfsi->nfs4_acl);
2922 nfsi->nfs4_acl = acl;
2923 spin_unlock(&inode->i_lock);
2926 static void nfs4_zap_acl_attr(struct inode *inode)
2928 nfs4_set_cached_acl(inode, NULL);
2931 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2933 struct nfs_inode *nfsi = NFS_I(inode);
2934 struct nfs4_cached_acl *acl;
2937 spin_lock(&inode->i_lock);
2938 acl = nfsi->nfs4_acl;
2941 if (buf == NULL) /* user is just asking for length */
2943 if (acl->cached == 0)
2945 ret = -ERANGE; /* see getxattr(2) man page */
2946 if (acl->len > buflen)
2948 memcpy(buf, acl->data, acl->len);
2952 spin_unlock(&inode->i_lock);
2956 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2958 struct nfs4_cached_acl *acl;
2960 if (buf && acl_len <= PAGE_SIZE) {
2961 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2965 memcpy(acl->data, buf, acl_len);
2967 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2974 nfs4_set_cached_acl(inode, acl);
2977 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2979 struct page *pages[NFS4ACL_MAXPAGES];
2980 struct nfs_getaclargs args = {
2981 .fh = NFS_FH(inode),
2985 struct nfs_getaclres res = {
2989 struct rpc_message msg = {
2990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2994 struct page *localpage = NULL;
2997 if (buflen < PAGE_SIZE) {
2998 /* As long as we're doing a round trip to the server anyway,
2999 * let's be prepared for a page of acl data. */
3000 localpage = alloc_page(GFP_KERNEL);
3001 resp_buf = page_address(localpage);
3002 if (localpage == NULL)
3004 args.acl_pages[0] = localpage;
3005 args.acl_pgbase = 0;
3006 args.acl_len = PAGE_SIZE;
3009 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3011 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3014 if (res.acl_len > args.acl_len)
3015 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3017 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3020 if (res.acl_len > buflen)
3023 memcpy(buf, resp_buf, res.acl_len);
3028 __free_page(localpage);
3032 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3034 struct nfs4_exception exception = { };
3037 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3040 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3041 } while (exception.retry);
3045 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3047 struct nfs_server *server = NFS_SERVER(inode);
3050 if (!nfs4_server_supports_acls(server))
3052 ret = nfs_revalidate_inode(server, inode);
3055 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3056 nfs_zap_acl_cache(inode);
3057 ret = nfs4_read_cached_acl(inode, buf, buflen);
3060 return nfs4_get_acl_uncached(inode, buf, buflen);
3063 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3065 struct nfs_server *server = NFS_SERVER(inode);
3066 struct page *pages[NFS4ACL_MAXPAGES];
3067 struct nfs_setaclargs arg = {
3068 .fh = NFS_FH(inode),
3072 struct nfs_setaclres res;
3073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3080 if (!nfs4_server_supports_acls(server))
3082 nfs_inode_return_delegation(inode);
3083 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3084 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3085 nfs_access_zap_cache(inode);
3086 nfs_zap_acl_cache(inode);
3090 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3092 struct nfs4_exception exception = { };
3095 err = nfs4_handle_exception(NFS_SERVER(inode),
3096 __nfs4_proc_set_acl(inode, buf, buflen),
3098 } while (exception.retry);
3103 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3105 struct nfs_client *clp = server->nfs_client;
3107 if (!clp || task->tk_status >= 0)
3109 switch(task->tk_status) {
3110 case -NFS4ERR_ADMIN_REVOKED:
3111 case -NFS4ERR_BAD_STATEID:
3112 case -NFS4ERR_OPENMODE:
3115 nfs4_state_mark_reclaim_nograce(clp, state);
3116 case -NFS4ERR_STALE_CLIENTID:
3117 case -NFS4ERR_STALE_STATEID:
3118 case -NFS4ERR_EXPIRED:
3119 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3120 nfs4_schedule_state_recovery(clp);
3121 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3122 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3123 task->tk_status = 0;
3125 case -NFS4ERR_DELAY:
3126 nfs_inc_server_stats(server, NFSIOS_DELAY);
3127 case -NFS4ERR_GRACE:
3128 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3129 task->tk_status = 0;
3131 case -NFS4ERR_OLD_STATEID:
3132 task->tk_status = 0;
3135 task->tk_status = nfs4_map_errors(task->tk_status);
3139 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3141 nfs4_verifier sc_verifier;
3142 struct nfs4_setclientid setclientid = {
3143 .sc_verifier = &sc_verifier,
3146 struct rpc_message msg = {
3147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3148 .rpc_argp = &setclientid,
3156 p = (__be32*)sc_verifier.data;
3157 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3158 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3161 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3162 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3164 rpc_peeraddr2str(clp->cl_rpcclient,
3166 rpc_peeraddr2str(clp->cl_rpcclient,
3168 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3169 clp->cl_id_uniquifier);
3170 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3171 sizeof(setclientid.sc_netid),
3172 rpc_peeraddr2str(clp->cl_rpcclient,
3173 RPC_DISPLAY_NETID));
3174 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3175 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3176 clp->cl_ipaddr, port >> 8, port & 255);
3178 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3179 if (status != -NFS4ERR_CLID_INUSE)
3184 ssleep(clp->cl_lease_time + 1);
3186 if (++clp->cl_id_uniquifier == 0)
3192 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3194 struct nfs_fsinfo fsinfo;
3195 struct rpc_message msg = {
3196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3198 .rpc_resp = &fsinfo,
3205 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3207 spin_lock(&clp->cl_lock);
3208 clp->cl_lease_time = fsinfo.lease_time * HZ;
3209 clp->cl_last_renewal = now;
3210 spin_unlock(&clp->cl_lock);
3215 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3220 err = _nfs4_proc_setclientid_confirm(clp, cred);
3224 case -NFS4ERR_RESOURCE:
3225 /* The IBM lawyers misread another document! */
3226 case -NFS4ERR_DELAY:
3227 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3233 struct nfs4_delegreturndata {
3234 struct nfs4_delegreturnargs args;
3235 struct nfs4_delegreturnres res;
3237 nfs4_stateid stateid;
3238 unsigned long timestamp;
3239 struct nfs_fattr fattr;
3243 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3245 struct nfs4_delegreturndata *data = calldata;
3246 data->rpc_status = task->tk_status;
3247 if (data->rpc_status == 0)
3248 renew_lease(data->res.server, data->timestamp);
3251 static void nfs4_delegreturn_release(void *calldata)
3256 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3257 .rpc_call_done = nfs4_delegreturn_done,
3258 .rpc_release = nfs4_delegreturn_release,
3261 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3263 struct nfs4_delegreturndata *data;
3264 struct nfs_server *server = NFS_SERVER(inode);
3265 struct rpc_task *task;
3266 struct rpc_message msg = {
3267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3270 struct rpc_task_setup task_setup_data = {
3271 .rpc_client = server->client,
3272 .rpc_message = &msg,
3273 .callback_ops = &nfs4_delegreturn_ops,
3274 .flags = RPC_TASK_ASYNC,
3278 data = kmalloc(sizeof(*data), GFP_KERNEL);
3281 data->args.fhandle = &data->fh;
3282 data->args.stateid = &data->stateid;
3283 data->args.bitmask = server->attr_bitmask;
3284 nfs_copy_fh(&data->fh, NFS_FH(inode));
3285 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3286 data->res.fattr = &data->fattr;
3287 data->res.server = server;
3288 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3289 nfs_fattr_init(data->res.fattr);
3290 data->timestamp = jiffies;
3291 data->rpc_status = 0;
3293 task_setup_data.callback_data = data;
3294 msg.rpc_argp = &data->args,
3295 msg.rpc_resp = &data->res,
3296 task = rpc_run_task(&task_setup_data);
3298 return PTR_ERR(task);
3301 status = nfs4_wait_for_completion_rpc_task(task);
3304 status = data->rpc_status;
3307 nfs_refresh_inode(inode, &data->fattr);
3313 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3315 struct nfs_server *server = NFS_SERVER(inode);
3316 struct nfs4_exception exception = { };
3319 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3321 case -NFS4ERR_STALE_STATEID:
3322 case -NFS4ERR_EXPIRED:
3326 err = nfs4_handle_exception(server, err, &exception);
3327 } while (exception.retry);
3331 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3332 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3335 * sleep, with exponential backoff, and retry the LOCK operation.
3337 static unsigned long
3338 nfs4_set_lock_task_retry(unsigned long timeout)
3340 schedule_timeout_killable(timeout);
3342 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3343 return NFS4_LOCK_MAXTIMEOUT;
3347 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3349 struct inode *inode = state->inode;
3350 struct nfs_server *server = NFS_SERVER(inode);
3351 struct nfs_client *clp = server->nfs_client;
3352 struct nfs_lockt_args arg = {
3353 .fh = NFS_FH(inode),
3356 struct nfs_lockt_res res = {
3359 struct rpc_message msg = {
3360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3363 .rpc_cred = state->owner->so_cred,
3365 struct nfs4_lock_state *lsp;
3368 arg.lock_owner.clientid = clp->cl_clientid;
3369 status = nfs4_set_lock_state(state, request);
3372 lsp = request->fl_u.nfs4_fl.owner;
3373 arg.lock_owner.id = lsp->ls_id.id;
3374 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3377 request->fl_type = F_UNLCK;
3379 case -NFS4ERR_DENIED:
3382 request->fl_ops->fl_release_private(request);
3387 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3389 struct nfs4_exception exception = { };
3393 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3394 _nfs4_proc_getlk(state, cmd, request),
3396 } while (exception.retry);
3400 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3403 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3405 res = posix_lock_file_wait(file, fl);
3408 res = flock_lock_file_wait(file, fl);
3416 struct nfs4_unlockdata {
3417 struct nfs_locku_args arg;
3418 struct nfs_locku_res res;
3419 struct nfs4_lock_state *lsp;
3420 struct nfs_open_context *ctx;
3421 struct file_lock fl;
3422 const struct nfs_server *server;
3423 unsigned long timestamp;
3426 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3427 struct nfs_open_context *ctx,
3428 struct nfs4_lock_state *lsp,
3429 struct nfs_seqid *seqid)
3431 struct nfs4_unlockdata *p;
3432 struct inode *inode = lsp->ls_state->inode;
3434 p = kmalloc(sizeof(*p), GFP_KERNEL);
3437 p->arg.fh = NFS_FH(inode);
3439 p->arg.seqid = seqid;
3440 p->res.seqid = seqid;
3441 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3442 p->arg.stateid = &lsp->ls_stateid;
3444 atomic_inc(&lsp->ls_count);
3445 /* Ensure we don't close file until we're done freeing locks! */
3446 p->ctx = get_nfs_open_context(ctx);
3447 memcpy(&p->fl, fl, sizeof(p->fl));
3448 p->server = NFS_SERVER(inode);
3452 static void nfs4_locku_release_calldata(void *data)
3454 struct nfs4_unlockdata *calldata = data;
3455 nfs_free_seqid(calldata->arg.seqid);
3456 nfs4_put_lock_state(calldata->lsp);
3457 put_nfs_open_context(calldata->ctx);
3461 static void nfs4_locku_done(struct rpc_task *task, void *data)
3463 struct nfs4_unlockdata *calldata = data;
3465 if (RPC_ASSASSINATED(task))
3467 switch (task->tk_status) {
3469 memcpy(calldata->lsp->ls_stateid.data,
3470 calldata->res.stateid.data,
3471 sizeof(calldata->lsp->ls_stateid.data));
3472 renew_lease(calldata->server, calldata->timestamp);
3474 case -NFS4ERR_BAD_STATEID:
3475 case -NFS4ERR_OLD_STATEID:
3476 case -NFS4ERR_STALE_STATEID:
3477 case -NFS4ERR_EXPIRED:
3480 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3481 rpc_restart_call(task);
3485 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3487 struct nfs4_unlockdata *calldata = data;
3489 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3491 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3492 /* Note: exit _without_ running nfs4_locku_done */
3493 task->tk_action = NULL;
3496 calldata->timestamp = jiffies;
3497 rpc_call_start(task);
3500 static const struct rpc_call_ops nfs4_locku_ops = {
3501 .rpc_call_prepare = nfs4_locku_prepare,
3502 .rpc_call_done = nfs4_locku_done,
3503 .rpc_release = nfs4_locku_release_calldata,
3506 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3507 struct nfs_open_context *ctx,
3508 struct nfs4_lock_state *lsp,
3509 struct nfs_seqid *seqid)
3511 struct nfs4_unlockdata *data;
3512 struct rpc_message msg = {
3513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3514 .rpc_cred = ctx->cred,
3516 struct rpc_task_setup task_setup_data = {
3517 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3518 .rpc_message = &msg,
3519 .callback_ops = &nfs4_locku_ops,
3520 .workqueue = nfsiod_workqueue,
3521 .flags = RPC_TASK_ASYNC,
3524 /* Ensure this is an unlock - when canceling a lock, the
3525 * canceled lock is passed in, and it won't be an unlock.
3527 fl->fl_type = F_UNLCK;
3529 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3531 nfs_free_seqid(seqid);
3532 return ERR_PTR(-ENOMEM);
3535 msg.rpc_argp = &data->arg,
3536 msg.rpc_resp = &data->res,
3537 task_setup_data.callback_data = data;
3538 return rpc_run_task(&task_setup_data);
3541 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3543 struct nfs_inode *nfsi = NFS_I(state->inode);
3544 struct nfs_seqid *seqid;
3545 struct nfs4_lock_state *lsp;
3546 struct rpc_task *task;
3548 unsigned char fl_flags = request->fl_flags;
3550 status = nfs4_set_lock_state(state, request);
3551 /* Unlock _before_ we do the RPC call */
3552 request->fl_flags |= FL_EXISTS;
3553 down_read(&nfsi->rwsem);
3554 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3555 up_read(&nfsi->rwsem);
3558 up_read(&nfsi->rwsem);
3561 /* Is this a delegated lock? */
3562 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3564 lsp = request->fl_u.nfs4_fl.owner;
3565 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3569 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3570 status = PTR_ERR(task);
3573 status = nfs4_wait_for_completion_rpc_task(task);
3576 request->fl_flags = fl_flags;
3580 struct nfs4_lockdata {
3581 struct nfs_lock_args arg;
3582 struct nfs_lock_res res;
3583 struct nfs4_lock_state *lsp;
3584 struct nfs_open_context *ctx;
3585 struct file_lock fl;
3586 unsigned long timestamp;
3591 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3592 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3594 struct nfs4_lockdata *p;
3595 struct inode *inode = lsp->ls_state->inode;
3596 struct nfs_server *server = NFS_SERVER(inode);
3598 p = kzalloc(sizeof(*p), GFP_KERNEL);
3602 p->arg.fh = NFS_FH(inode);
3604 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3605 if (p->arg.open_seqid == NULL)
3607 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3608 if (p->arg.lock_seqid == NULL)
3609 goto out_free_seqid;
3610 p->arg.lock_stateid = &lsp->ls_stateid;
3611 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3612 p->arg.lock_owner.id = lsp->ls_id.id;
3613 p->res.lock_seqid = p->arg.lock_seqid;
3614 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3616 atomic_inc(&lsp->ls_count);
3617 p->ctx = get_nfs_open_context(ctx);
3618 memcpy(&p->fl, fl, sizeof(p->fl));
3621 nfs_free_seqid(p->arg.open_seqid);
3627 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3629 struct nfs4_lockdata *data = calldata;
3630 struct nfs4_state *state = data->lsp->ls_state;
3632 dprintk("%s: begin!\n", __func__);
3633 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3635 /* Do we need to do an open_to_lock_owner? */
3636 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3637 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3639 data->arg.open_stateid = &state->stateid;
3640 data->arg.new_lock_owner = 1;
3641 data->res.open_seqid = data->arg.open_seqid;
3643 data->arg.new_lock_owner = 0;
3644 data->timestamp = jiffies;
3645 rpc_call_start(task);
3646 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3649 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3651 struct nfs4_lockdata *data = calldata;
3653 dprintk("%s: begin!\n", __func__);
3655 data->rpc_status = task->tk_status;
3656 if (RPC_ASSASSINATED(task))
3658 if (data->arg.new_lock_owner != 0) {
3659 if (data->rpc_status == 0)
3660 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3664 if (data->rpc_status == 0) {
3665 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3666 sizeof(data->lsp->ls_stateid.data));
3667 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3668 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3671 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3674 static void nfs4_lock_release(void *calldata)
3676 struct nfs4_lockdata *data = calldata;
3678 dprintk("%s: begin!\n", __func__);
3679 nfs_free_seqid(data->arg.open_seqid);
3680 if (data->cancelled != 0) {
3681 struct rpc_task *task;
3682 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3683 data->arg.lock_seqid);
3686 dprintk("%s: cancelling lock!\n", __func__);
3688 nfs_free_seqid(data->arg.lock_seqid);
3689 nfs4_put_lock_state(data->lsp);
3690 put_nfs_open_context(data->ctx);
3692 dprintk("%s: done!\n", __func__);
3695 static const struct rpc_call_ops nfs4_lock_ops = {
3696 .rpc_call_prepare = nfs4_lock_prepare,
3697 .rpc_call_done = nfs4_lock_done,
3698 .rpc_release = nfs4_lock_release,
3701 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3703 struct nfs4_lockdata *data;
3704 struct rpc_task *task;
3705 struct rpc_message msg = {
3706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3707 .rpc_cred = state->owner->so_cred,
3709 struct rpc_task_setup task_setup_data = {
3710 .rpc_client = NFS_CLIENT(state->inode),
3711 .rpc_message = &msg,
3712 .callback_ops = &nfs4_lock_ops,
3713 .workqueue = nfsiod_workqueue,
3714 .flags = RPC_TASK_ASYNC,
3718 dprintk("%s: begin!\n", __func__);
3719 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3720 fl->fl_u.nfs4_fl.owner);
3724 data->arg.block = 1;
3726 data->arg.reclaim = 1;
3727 msg.rpc_argp = &data->arg,
3728 msg.rpc_resp = &data->res,
3729 task_setup_data.callback_data = data;
3730 task = rpc_run_task(&task_setup_data);
3732 return PTR_ERR(task);
3733 ret = nfs4_wait_for_completion_rpc_task(task);
3735 ret = data->rpc_status;
3736 if (ret == -NFS4ERR_DENIED)
3739 data->cancelled = 1;
3741 dprintk("%s: done, ret = %d!\n", __func__, ret);
3745 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3747 struct nfs_server *server = NFS_SERVER(state->inode);
3748 struct nfs4_exception exception = { };
3752 /* Cache the lock if possible... */
3753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3755 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3756 if (err != -NFS4ERR_DELAY)
3758 nfs4_handle_exception(server, err, &exception);
3759 } while (exception.retry);
3763 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3765 struct nfs_server *server = NFS_SERVER(state->inode);
3766 struct nfs4_exception exception = { };
3769 err = nfs4_set_lock_state(state, request);
3773 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3775 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3776 if (err != -NFS4ERR_DELAY)
3778 nfs4_handle_exception(server, err, &exception);
3779 } while (exception.retry);
3783 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3785 struct nfs_inode *nfsi = NFS_I(state->inode);
3786 unsigned char fl_flags = request->fl_flags;
3789 /* Is this a delegated open? */
3790 status = nfs4_set_lock_state(state, request);
3793 request->fl_flags |= FL_ACCESS;
3794 status = do_vfs_lock(request->fl_file, request);
3797 down_read(&nfsi->rwsem);
3798 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3799 /* Yes: cache locks! */
3800 /* ...but avoid races with delegation recall... */
3801 request->fl_flags = fl_flags & ~FL_SLEEP;
3802 status = do_vfs_lock(request->fl_file, request);
3805 status = _nfs4_do_setlk(state, cmd, request, 0);
3808 /* Note: we always want to sleep here! */
3809 request->fl_flags = fl_flags | FL_SLEEP;
3810 if (do_vfs_lock(request->fl_file, request) < 0)
3811 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3813 up_read(&nfsi->rwsem);
3815 request->fl_flags = fl_flags;
3819 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3821 struct nfs4_exception exception = { };
3825 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3826 _nfs4_proc_setlk(state, cmd, request),
3828 } while (exception.retry);
3833 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3835 struct nfs_open_context *ctx;
3836 struct nfs4_state *state;
3837 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3840 /* verify open state */
3841 ctx = nfs_file_open_context(filp);
3844 if (request->fl_start < 0 || request->fl_end < 0)
3848 return nfs4_proc_getlk(state, F_GETLK, request);
3850 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3853 if (request->fl_type == F_UNLCK)
3854 return nfs4_proc_unlck(state, cmd, request);
3857 status = nfs4_proc_setlk(state, cmd, request);
3858 if ((status != -EAGAIN) || IS_SETLK(cmd))
3860 timeout = nfs4_set_lock_task_retry(timeout);
3861 status = -ERESTARTSYS;
3864 } while(status < 0);
3868 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3870 struct nfs_server *server = NFS_SERVER(state->inode);
3871 struct nfs4_exception exception = { };
3874 err = nfs4_set_lock_state(state, fl);
3878 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3879 if (err != -NFS4ERR_DELAY)
3881 err = nfs4_handle_exception(server, err, &exception);
3882 } while (exception.retry);
3887 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3889 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3890 size_t buflen, int flags)
3892 struct inode *inode = dentry->d_inode;
3894 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3897 return nfs4_proc_set_acl(inode, buf, buflen);
3900 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3901 * and that's what we'll do for e.g. user attributes that haven't been set.
3902 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3903 * attributes in kernel-managed attribute namespaces. */
3904 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3907 struct inode *inode = dentry->d_inode;
3909 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3912 return nfs4_proc_get_acl(inode, buf, buflen);
3915 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3917 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3919 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3921 if (buf && buflen < len)
3924 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3928 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3930 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3931 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3932 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3935 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3936 NFS_ATTR_FATTR_NLINK;
3937 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3941 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3942 struct nfs4_fs_locations *fs_locations, struct page *page)
3944 struct nfs_server *server = NFS_SERVER(dir);
3946 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3947 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3949 struct nfs4_fs_locations_arg args = {
3950 .dir_fh = NFS_FH(dir),
3955 struct nfs4_fs_locations_res res = {
3956 .fs_locations = fs_locations,
3958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3965 dprintk("%s: start\n", __func__);
3966 nfs_fattr_init(&fs_locations->fattr);
3967 fs_locations->server = server;
3968 fs_locations->nlocations = 0;
3969 status = nfs4_call_sync(server, &msg, &args, &res, 0);
3970 nfs_fixup_referral_attributes(&fs_locations->fattr);
3971 dprintk("%s: returned status = %d\n", __func__, status);
3975 #ifdef CONFIG_NFS_V4_1
3976 /* Destroy the slot table */
3977 static void nfs4_destroy_slot_table(struct nfs4_session *session)
3979 if (session->fc_slot_table.slots == NULL)
3981 kfree(session->fc_slot_table.slots);
3982 session->fc_slot_table.slots = NULL;
3986 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3988 struct nfs4_session *session;
3989 struct nfs4_slot_table *tbl;
3991 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3994 tbl = &session->fc_slot_table;
3995 spin_lock_init(&tbl->slot_tbl_lock);
3996 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4001 void nfs4_destroy_session(struct nfs4_session *session)
4003 nfs4_destroy_slot_table(session);
4007 #endif /* CONFIG_NFS_V4_1 */
4009 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
4010 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4011 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4012 .recover_open = nfs4_open_reclaim,
4013 .recover_lock = nfs4_lock_reclaim,
4016 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
4017 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4018 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4019 .recover_open = nfs4_open_expired,
4020 .recover_lock = nfs4_lock_expired,
4023 static const struct inode_operations nfs4_file_inode_operations = {
4024 .permission = nfs_permission,
4025 .getattr = nfs_getattr,
4026 .setattr = nfs_setattr,
4027 .getxattr = nfs4_getxattr,
4028 .setxattr = nfs4_setxattr,
4029 .listxattr = nfs4_listxattr,
4032 const struct nfs_rpc_ops nfs_v4_clientops = {
4033 .version = 4, /* protocol version */
4034 .dentry_ops = &nfs4_dentry_operations,
4035 .dir_inode_ops = &nfs4_dir_inode_operations,
4036 .file_inode_ops = &nfs4_file_inode_operations,
4037 .getroot = nfs4_proc_get_root,
4038 .getattr = nfs4_proc_getattr,
4039 .setattr = nfs4_proc_setattr,
4040 .lookupfh = nfs4_proc_lookupfh,
4041 .lookup = nfs4_proc_lookup,
4042 .access = nfs4_proc_access,
4043 .readlink = nfs4_proc_readlink,
4044 .create = nfs4_proc_create,
4045 .remove = nfs4_proc_remove,
4046 .unlink_setup = nfs4_proc_unlink_setup,
4047 .unlink_done = nfs4_proc_unlink_done,
4048 .rename = nfs4_proc_rename,
4049 .link = nfs4_proc_link,
4050 .symlink = nfs4_proc_symlink,
4051 .mkdir = nfs4_proc_mkdir,
4052 .rmdir = nfs4_proc_remove,
4053 .readdir = nfs4_proc_readdir,
4054 .mknod = nfs4_proc_mknod,
4055 .statfs = nfs4_proc_statfs,
4056 .fsinfo = nfs4_proc_fsinfo,
4057 .pathconf = nfs4_proc_pathconf,
4058 .set_capabilities = nfs4_server_capabilities,
4059 .decode_dirent = nfs4_decode_dirent,
4060 .read_setup = nfs4_proc_read_setup,
4061 .read_done = nfs4_read_done,
4062 .write_setup = nfs4_proc_write_setup,
4063 .write_done = nfs4_write_done,
4064 .commit_setup = nfs4_proc_commit_setup,
4065 .commit_done = nfs4_commit_done,
4066 .lock = nfs4_proc_lock,
4067 .clear_acl_cache = nfs4_zap_acl_attr,
4068 .close_context = nfs4_close_context,