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_find_slot - efficiently look for a free slot
279 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
280 * If found, we mark the slot as used, update the highest_used_slotid,
281 * and respectively set up the sequence operation args.
282 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
284 * Note: must be called with under the slot_tbl_lock.
287 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
290 u8 ret_id = NFS4_MAX_SLOT_TABLE;
291 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
293 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
294 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
296 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
297 if (slotid >= tbl->max_slots)
299 __set_bit(slotid, tbl->used_slots);
300 if (slotid > tbl->highest_used_slotid)
301 tbl->highest_used_slotid = slotid;
304 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
305 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
309 static int nfs41_setup_sequence(struct nfs4_session *session,
310 struct nfs4_sequence_args *args,
311 struct nfs4_sequence_res *res,
313 struct rpc_task *task)
315 struct nfs4_slot *slot;
316 struct nfs4_slot_table *tbl;
319 dprintk("--> %s\n", __func__);
320 /* slot already allocated? */
321 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
324 memset(res, 0, sizeof(*res));
325 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
326 tbl = &session->fc_slot_table;
328 spin_lock(&tbl->slot_tbl_lock);
329 slotid = nfs4_find_slot(tbl, task);
330 if (slotid == NFS4_MAX_SLOT_TABLE) {
331 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
332 spin_unlock(&tbl->slot_tbl_lock);
333 dprintk("<-- %s: no free slots\n", __func__);
336 spin_unlock(&tbl->slot_tbl_lock);
338 slot = tbl->slots + slotid;
339 args->sa_slotid = slotid;
340 args->sa_cache_this = cache_reply;
342 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
344 res->sr_slotid = slotid;
345 res->sr_renewal_time = jiffies;
347 * sr_status is only set in decode_sequence, and so will remain
348 * set to 1 if an rpc level failure occurs.
354 int nfs4_setup_sequence(struct nfs_client *clp,
355 struct nfs4_sequence_args *args,
356 struct nfs4_sequence_res *res,
358 struct rpc_task *task)
362 dprintk("--> %s clp %p session %p sr_slotid %d\n",
363 __func__, clp, clp->cl_session, res->sr_slotid);
365 if (!nfs4_has_session(clp))
367 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
369 if (ret != -EAGAIN) {
370 /* terminate rpc task */
371 task->tk_status = ret;
372 task->tk_action = NULL;
375 dprintk("<-- %s status=%d\n", __func__, ret);
379 struct nfs41_call_sync_data {
380 struct nfs_client *clp;
381 struct nfs4_sequence_args *seq_args;
382 struct nfs4_sequence_res *seq_res;
386 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
388 struct nfs41_call_sync_data *data = calldata;
390 dprintk("--> %s data->clp->cl_session %p\n", __func__,
391 data->clp->cl_session);
392 if (nfs4_setup_sequence(data->clp, data->seq_args,
393 data->seq_res, data->cache_reply, task))
395 rpc_call_start(task);
398 struct rpc_call_ops nfs41_call_sync_ops = {
399 .rpc_call_prepare = nfs41_call_sync_prepare,
402 static int nfs4_call_sync_sequence(struct nfs_client *clp,
403 struct rpc_clnt *clnt,
404 struct rpc_message *msg,
405 struct nfs4_sequence_args *args,
406 struct nfs4_sequence_res *res,
410 struct rpc_task *task;
411 struct nfs41_call_sync_data data = {
415 .cache_reply = cache_reply,
417 struct rpc_task_setup task_setup = {
420 .callback_ops = &nfs41_call_sync_ops,
421 .callback_data = &data
424 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
425 task = rpc_run_task(&task_setup);
429 ret = task->tk_status;
435 int _nfs4_call_sync_session(struct nfs_server *server,
436 struct rpc_message *msg,
437 struct nfs4_sequence_args *args,
438 struct nfs4_sequence_res *res,
441 return nfs4_call_sync_sequence(server->nfs_client, server->client,
442 msg, args, res, cache_reply);
445 #endif /* CONFIG_NFS_V4_1 */
447 int _nfs4_call_sync(struct nfs_server *server,
448 struct rpc_message *msg,
449 struct nfs4_sequence_args *args,
450 struct nfs4_sequence_res *res,
453 args->sa_session = res->sr_session = NULL;
454 return rpc_call_sync(server->client, msg, 0);
457 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
458 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
459 &(res)->seq_res, (cache_reply))
461 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
463 struct nfs_inode *nfsi = NFS_I(dir);
465 spin_lock(&dir->i_lock);
466 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
467 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
468 nfs_force_lookup_revalidate(dir);
469 nfsi->change_attr = cinfo->after;
470 spin_unlock(&dir->i_lock);
473 struct nfs4_opendata {
475 struct nfs_openargs o_arg;
476 struct nfs_openres o_res;
477 struct nfs_open_confirmargs c_arg;
478 struct nfs_open_confirmres c_res;
479 struct nfs_fattr f_attr;
480 struct nfs_fattr dir_attr;
483 struct nfs4_state_owner *owner;
484 struct nfs4_state *state;
486 unsigned long timestamp;
487 unsigned int rpc_done : 1;
493 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
495 p->o_res.f_attr = &p->f_attr;
496 p->o_res.dir_attr = &p->dir_attr;
497 p->o_res.seqid = p->o_arg.seqid;
498 p->c_res.seqid = p->c_arg.seqid;
499 p->o_res.server = p->o_arg.server;
500 nfs_fattr_init(&p->f_attr);
501 nfs_fattr_init(&p->dir_attr);
504 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
505 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
506 const struct iattr *attrs)
508 struct dentry *parent = dget_parent(path->dentry);
509 struct inode *dir = parent->d_inode;
510 struct nfs_server *server = NFS_SERVER(dir);
511 struct nfs4_opendata *p;
513 p = kzalloc(sizeof(*p), GFP_KERNEL);
516 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
517 if (p->o_arg.seqid == NULL)
519 p->path.mnt = mntget(path->mnt);
520 p->path.dentry = dget(path->dentry);
523 atomic_inc(&sp->so_count);
524 p->o_arg.fh = NFS_FH(dir);
525 p->o_arg.open_flags = flags;
526 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
527 p->o_arg.clientid = server->nfs_client->cl_clientid;
528 p->o_arg.id = sp->so_owner_id.id;
529 p->o_arg.name = &p->path.dentry->d_name;
530 p->o_arg.server = server;
531 p->o_arg.bitmask = server->attr_bitmask;
532 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
533 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
534 if (flags & O_EXCL) {
535 u32 *s = (u32 *) p->o_arg.u.verifier.data;
538 } else if (flags & O_CREAT) {
539 p->o_arg.u.attrs = &p->attrs;
540 memcpy(&p->attrs, attrs, sizeof(p->attrs));
542 p->c_arg.fh = &p->o_res.fh;
543 p->c_arg.stateid = &p->o_res.stateid;
544 p->c_arg.seqid = p->o_arg.seqid;
545 nfs4_init_opendata_res(p);
555 static void nfs4_opendata_free(struct kref *kref)
557 struct nfs4_opendata *p = container_of(kref,
558 struct nfs4_opendata, kref);
560 nfs_free_seqid(p->o_arg.seqid);
561 if (p->state != NULL)
562 nfs4_put_open_state(p->state);
563 nfs4_put_state_owner(p->owner);
569 static void nfs4_opendata_put(struct nfs4_opendata *p)
572 kref_put(&p->kref, nfs4_opendata_free);
575 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
579 ret = rpc_wait_for_completion_task(task);
583 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
587 if (open_mode & O_EXCL)
589 switch (mode & (FMODE_READ|FMODE_WRITE)) {
591 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
594 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
596 case FMODE_READ|FMODE_WRITE:
597 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
603 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
605 if ((delegation->type & fmode) != fmode)
607 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
609 nfs_mark_delegation_referenced(delegation);
613 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
622 case FMODE_READ|FMODE_WRITE:
625 nfs4_state_set_mode_locked(state, state->state | fmode);
628 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
630 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
631 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
632 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
635 set_bit(NFS_O_RDONLY_STATE, &state->flags);
638 set_bit(NFS_O_WRONLY_STATE, &state->flags);
640 case FMODE_READ|FMODE_WRITE:
641 set_bit(NFS_O_RDWR_STATE, &state->flags);
645 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
647 write_seqlock(&state->seqlock);
648 nfs_set_open_stateid_locked(state, stateid, fmode);
649 write_sequnlock(&state->seqlock);
652 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
655 * Protect the call to nfs4_state_set_mode_locked and
656 * serialise the stateid update
658 write_seqlock(&state->seqlock);
659 if (deleg_stateid != NULL) {
660 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
661 set_bit(NFS_DELEGATED_STATE, &state->flags);
663 if (open_stateid != NULL)
664 nfs_set_open_stateid_locked(state, open_stateid, fmode);
665 write_sequnlock(&state->seqlock);
666 spin_lock(&state->owner->so_lock);
667 update_open_stateflags(state, fmode);
668 spin_unlock(&state->owner->so_lock);
671 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
673 struct nfs_inode *nfsi = NFS_I(state->inode);
674 struct nfs_delegation *deleg_cur;
677 fmode &= (FMODE_READ|FMODE_WRITE);
680 deleg_cur = rcu_dereference(nfsi->delegation);
681 if (deleg_cur == NULL)
684 spin_lock(&deleg_cur->lock);
685 if (nfsi->delegation != deleg_cur ||
686 (deleg_cur->type & fmode) != fmode)
687 goto no_delegation_unlock;
689 if (delegation == NULL)
690 delegation = &deleg_cur->stateid;
691 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
692 goto no_delegation_unlock;
694 nfs_mark_delegation_referenced(deleg_cur);
695 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
697 no_delegation_unlock:
698 spin_unlock(&deleg_cur->lock);
702 if (!ret && open_stateid != NULL) {
703 __update_open_stateid(state, open_stateid, NULL, fmode);
711 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
713 struct nfs_delegation *delegation;
716 delegation = rcu_dereference(NFS_I(inode)->delegation);
717 if (delegation == NULL || (delegation->type & fmode) == fmode) {
722 nfs_inode_return_delegation(inode);
725 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
727 struct nfs4_state *state = opendata->state;
728 struct nfs_inode *nfsi = NFS_I(state->inode);
729 struct nfs_delegation *delegation;
730 int open_mode = opendata->o_arg.open_flags & O_EXCL;
731 fmode_t fmode = opendata->o_arg.fmode;
732 nfs4_stateid stateid;
736 if (can_open_cached(state, fmode, open_mode)) {
737 spin_lock(&state->owner->so_lock);
738 if (can_open_cached(state, fmode, open_mode)) {
739 update_open_stateflags(state, fmode);
740 spin_unlock(&state->owner->so_lock);
741 goto out_return_state;
743 spin_unlock(&state->owner->so_lock);
746 delegation = rcu_dereference(nfsi->delegation);
747 if (delegation == NULL ||
748 !can_open_delegated(delegation, fmode)) {
752 /* Save the delegation */
753 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
755 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
760 /* Try to update the stateid using the delegation */
761 if (update_open_stateid(state, NULL, &stateid, fmode))
762 goto out_return_state;
767 atomic_inc(&state->count);
771 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
774 struct nfs4_state *state = NULL;
775 struct nfs_delegation *delegation;
778 if (!data->rpc_done) {
779 state = nfs4_try_open_cached(data);
784 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
786 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
787 ret = PTR_ERR(inode);
791 state = nfs4_get_open_state(inode, data->owner);
794 if (data->o_res.delegation_type != 0) {
795 int delegation_flags = 0;
798 delegation = rcu_dereference(NFS_I(inode)->delegation);
800 delegation_flags = delegation->flags;
802 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
803 nfs_inode_set_delegation(state->inode,
804 data->owner->so_cred,
807 nfs_inode_reclaim_delegation(state->inode,
808 data->owner->so_cred,
812 update_open_stateid(state, &data->o_res.stateid, NULL,
823 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
825 struct nfs_inode *nfsi = NFS_I(state->inode);
826 struct nfs_open_context *ctx;
828 spin_lock(&state->inode->i_lock);
829 list_for_each_entry(ctx, &nfsi->open_files, list) {
830 if (ctx->state != state)
832 get_nfs_open_context(ctx);
833 spin_unlock(&state->inode->i_lock);
836 spin_unlock(&state->inode->i_lock);
837 return ERR_PTR(-ENOENT);
840 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
842 struct nfs4_opendata *opendata;
844 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
845 if (opendata == NULL)
846 return ERR_PTR(-ENOMEM);
847 opendata->state = state;
848 atomic_inc(&state->count);
852 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
854 struct nfs4_state *newstate;
857 opendata->o_arg.open_flags = 0;
858 opendata->o_arg.fmode = fmode;
859 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
860 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
861 nfs4_init_opendata_res(opendata);
862 ret = _nfs4_proc_open(opendata);
865 newstate = nfs4_opendata_to_nfs4_state(opendata);
866 if (IS_ERR(newstate))
867 return PTR_ERR(newstate);
868 nfs4_close_state(&opendata->path, newstate, fmode);
873 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
875 struct nfs4_state *newstate;
878 /* memory barrier prior to reading state->n_* */
879 clear_bit(NFS_DELEGATED_STATE, &state->flags);
881 if (state->n_rdwr != 0) {
882 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
885 if (newstate != state)
888 if (state->n_wronly != 0) {
889 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
892 if (newstate != state)
895 if (state->n_rdonly != 0) {
896 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
899 if (newstate != state)
903 * We may have performed cached opens for all three recoveries.
904 * Check if we need to update the current stateid.
906 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
907 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
908 write_seqlock(&state->seqlock);
909 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
910 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
911 write_sequnlock(&state->seqlock);
918 * reclaim state on the server after a reboot.
920 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
922 struct nfs_delegation *delegation;
923 struct nfs4_opendata *opendata;
924 fmode_t delegation_type = 0;
927 opendata = nfs4_open_recoverdata_alloc(ctx, state);
928 if (IS_ERR(opendata))
929 return PTR_ERR(opendata);
930 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
931 opendata->o_arg.fh = NFS_FH(state->inode);
933 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
934 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
935 delegation_type = delegation->type;
937 opendata->o_arg.u.delegation_type = delegation_type;
938 status = nfs4_open_recover(opendata, state);
939 nfs4_opendata_put(opendata);
943 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
945 struct nfs_server *server = NFS_SERVER(state->inode);
946 struct nfs4_exception exception = { };
949 err = _nfs4_do_open_reclaim(ctx, state);
950 if (err != -NFS4ERR_DELAY)
952 nfs4_handle_exception(server, err, &exception);
953 } while (exception.retry);
957 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
959 struct nfs_open_context *ctx;
962 ctx = nfs4_state_find_open_context(state);
965 ret = nfs4_do_open_reclaim(ctx, state);
966 put_nfs_open_context(ctx);
970 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
972 struct nfs4_opendata *opendata;
975 opendata = nfs4_open_recoverdata_alloc(ctx, state);
976 if (IS_ERR(opendata))
977 return PTR_ERR(opendata);
978 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
979 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
980 sizeof(opendata->o_arg.u.delegation.data));
981 ret = nfs4_open_recover(opendata, state);
982 nfs4_opendata_put(opendata);
986 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
988 struct nfs4_exception exception = { };
989 struct nfs_server *server = NFS_SERVER(state->inode);
992 err = _nfs4_open_delegation_recall(ctx, state, stateid);
996 case -NFS4ERR_STALE_CLIENTID:
997 case -NFS4ERR_STALE_STATEID:
998 case -NFS4ERR_EXPIRED:
999 /* Don't recall a delegation if it was lost */
1000 nfs4_schedule_state_recovery(server->nfs_client);
1003 err = nfs4_handle_exception(server, err, &exception);
1004 } while (exception.retry);
1008 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1010 struct nfs4_opendata *data = calldata;
1012 data->rpc_status = task->tk_status;
1013 if (RPC_ASSASSINATED(task))
1015 if (data->rpc_status == 0) {
1016 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1017 sizeof(data->o_res.stateid.data));
1018 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1019 renew_lease(data->o_res.server, data->timestamp);
1024 static void nfs4_open_confirm_release(void *calldata)
1026 struct nfs4_opendata *data = calldata;
1027 struct nfs4_state *state = NULL;
1029 /* If this request hasn't been cancelled, do nothing */
1030 if (data->cancelled == 0)
1032 /* In case of error, no cleanup! */
1033 if (!data->rpc_done)
1035 state = nfs4_opendata_to_nfs4_state(data);
1037 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1039 nfs4_opendata_put(data);
1042 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1043 .rpc_call_done = nfs4_open_confirm_done,
1044 .rpc_release = nfs4_open_confirm_release,
1048 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1050 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1052 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1053 struct rpc_task *task;
1054 struct rpc_message msg = {
1055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1056 .rpc_argp = &data->c_arg,
1057 .rpc_resp = &data->c_res,
1058 .rpc_cred = data->owner->so_cred,
1060 struct rpc_task_setup task_setup_data = {
1061 .rpc_client = server->client,
1062 .rpc_message = &msg,
1063 .callback_ops = &nfs4_open_confirm_ops,
1064 .callback_data = data,
1065 .workqueue = nfsiod_workqueue,
1066 .flags = RPC_TASK_ASYNC,
1070 kref_get(&data->kref);
1072 data->rpc_status = 0;
1073 data->timestamp = jiffies;
1074 task = rpc_run_task(&task_setup_data);
1076 return PTR_ERR(task);
1077 status = nfs4_wait_for_completion_rpc_task(task);
1079 data->cancelled = 1;
1082 status = data->rpc_status;
1087 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1089 struct nfs4_opendata *data = calldata;
1090 struct nfs4_state_owner *sp = data->owner;
1092 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1095 * Check if we still need to send an OPEN call, or if we can use
1096 * a delegation instead.
1098 if (data->state != NULL) {
1099 struct nfs_delegation *delegation;
1101 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1104 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1105 if (delegation != NULL &&
1106 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1112 /* Update sequence id. */
1113 data->o_arg.id = sp->so_owner_id.id;
1114 data->o_arg.clientid = sp->so_client->cl_clientid;
1115 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1116 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1117 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1119 data->timestamp = jiffies;
1120 rpc_call_start(task);
1123 task->tk_action = NULL;
1127 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1129 struct nfs4_opendata *data = calldata;
1131 data->rpc_status = task->tk_status;
1132 if (RPC_ASSASSINATED(task))
1134 if (task->tk_status == 0) {
1135 switch (data->o_res.f_attr->mode & S_IFMT) {
1139 data->rpc_status = -ELOOP;
1142 data->rpc_status = -EISDIR;
1145 data->rpc_status = -ENOTDIR;
1147 renew_lease(data->o_res.server, data->timestamp);
1148 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1149 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1154 static void nfs4_open_release(void *calldata)
1156 struct nfs4_opendata *data = calldata;
1157 struct nfs4_state *state = NULL;
1159 /* If this request hasn't been cancelled, do nothing */
1160 if (data->cancelled == 0)
1162 /* In case of error, no cleanup! */
1163 if (data->rpc_status != 0 || !data->rpc_done)
1165 /* In case we need an open_confirm, no cleanup! */
1166 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1168 state = nfs4_opendata_to_nfs4_state(data);
1170 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1172 nfs4_opendata_put(data);
1175 static const struct rpc_call_ops nfs4_open_ops = {
1176 .rpc_call_prepare = nfs4_open_prepare,
1177 .rpc_call_done = nfs4_open_done,
1178 .rpc_release = nfs4_open_release,
1182 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1184 static int _nfs4_proc_open(struct nfs4_opendata *data)
1186 struct inode *dir = data->dir->d_inode;
1187 struct nfs_server *server = NFS_SERVER(dir);
1188 struct nfs_openargs *o_arg = &data->o_arg;
1189 struct nfs_openres *o_res = &data->o_res;
1190 struct rpc_task *task;
1191 struct rpc_message msg = {
1192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1195 .rpc_cred = data->owner->so_cred,
1197 struct rpc_task_setup task_setup_data = {
1198 .rpc_client = server->client,
1199 .rpc_message = &msg,
1200 .callback_ops = &nfs4_open_ops,
1201 .callback_data = data,
1202 .workqueue = nfsiod_workqueue,
1203 .flags = RPC_TASK_ASYNC,
1207 kref_get(&data->kref);
1209 data->rpc_status = 0;
1210 data->cancelled = 0;
1211 task = rpc_run_task(&task_setup_data);
1213 return PTR_ERR(task);
1214 status = nfs4_wait_for_completion_rpc_task(task);
1216 data->cancelled = 1;
1219 status = data->rpc_status;
1221 if (status != 0 || !data->rpc_done)
1224 if (o_res->fh.size == 0)
1225 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1227 if (o_arg->open_flags & O_CREAT) {
1228 update_changeattr(dir, &o_res->cinfo);
1229 nfs_post_op_update_inode(dir, o_res->dir_attr);
1231 nfs_refresh_inode(dir, o_res->dir_attr);
1232 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1233 status = _nfs4_proc_open_confirm(data);
1237 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1238 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1242 static int nfs4_recover_expired_lease(struct nfs_server *server)
1244 struct nfs_client *clp = server->nfs_client;
1248 ret = nfs4_wait_clnt_recover(clp);
1251 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1252 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1254 nfs4_schedule_state_recovery(clp);
1261 * reclaim state on the server after a network partition.
1262 * Assumes caller holds the appropriate lock
1264 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1266 struct nfs4_opendata *opendata;
1269 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1270 if (IS_ERR(opendata))
1271 return PTR_ERR(opendata);
1272 ret = nfs4_open_recover(opendata, state);
1274 d_drop(ctx->path.dentry);
1275 nfs4_opendata_put(opendata);
1279 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1281 struct nfs_server *server = NFS_SERVER(state->inode);
1282 struct nfs4_exception exception = { };
1286 err = _nfs4_open_expired(ctx, state);
1287 if (err != -NFS4ERR_DELAY)
1289 nfs4_handle_exception(server, err, &exception);
1290 } while (exception.retry);
1294 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1296 struct nfs_open_context *ctx;
1299 ctx = nfs4_state_find_open_context(state);
1301 return PTR_ERR(ctx);
1302 ret = nfs4_do_open_expired(ctx, state);
1303 put_nfs_open_context(ctx);
1308 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1309 * fields corresponding to attributes that were used to store the verifier.
1310 * Make sure we clobber those fields in the later setattr call
1312 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1314 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1315 !(sattr->ia_valid & ATTR_ATIME_SET))
1316 sattr->ia_valid |= ATTR_ATIME;
1318 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1319 !(sattr->ia_valid & ATTR_MTIME_SET))
1320 sattr->ia_valid |= ATTR_MTIME;
1324 * Returns a referenced nfs4_state
1326 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)
1328 struct nfs4_state_owner *sp;
1329 struct nfs4_state *state = NULL;
1330 struct nfs_server *server = NFS_SERVER(dir);
1331 struct nfs4_opendata *opendata;
1334 /* Protect against reboot recovery conflicts */
1336 if (!(sp = nfs4_get_state_owner(server, cred))) {
1337 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1340 status = nfs4_recover_expired_lease(server);
1342 goto err_put_state_owner;
1343 if (path->dentry->d_inode != NULL)
1344 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1346 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1347 if (opendata == NULL)
1348 goto err_put_state_owner;
1350 if (path->dentry->d_inode != NULL)
1351 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1353 status = _nfs4_proc_open(opendata);
1355 goto err_opendata_put;
1357 if (opendata->o_arg.open_flags & O_EXCL)
1358 nfs4_exclusive_attrset(opendata, sattr);
1360 state = nfs4_opendata_to_nfs4_state(opendata);
1361 status = PTR_ERR(state);
1363 goto err_opendata_put;
1364 nfs4_opendata_put(opendata);
1365 nfs4_put_state_owner(sp);
1369 nfs4_opendata_put(opendata);
1370 err_put_state_owner:
1371 nfs4_put_state_owner(sp);
1378 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)
1380 struct nfs4_exception exception = { };
1381 struct nfs4_state *res;
1385 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1388 /* NOTE: BAD_SEQID means the server and client disagree about the
1389 * book-keeping w.r.t. state-changing operations
1390 * (OPEN/CLOSE/LOCK/LOCKU...)
1391 * It is actually a sign of a bug on the client or on the server.
1393 * If we receive a BAD_SEQID error in the particular case of
1394 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1395 * have unhashed the old state_owner for us, and that we can
1396 * therefore safely retry using a new one. We should still warn
1397 * the user though...
1399 if (status == -NFS4ERR_BAD_SEQID) {
1400 printk(KERN_WARNING "NFS: v4 server %s "
1401 " returned a bad sequence-id error!\n",
1402 NFS_SERVER(dir)->nfs_client->cl_hostname);
1403 exception.retry = 1;
1407 * BAD_STATEID on OPEN means that the server cancelled our
1408 * state before it received the OPEN_CONFIRM.
1409 * Recover by retrying the request as per the discussion
1410 * on Page 181 of RFC3530.
1412 if (status == -NFS4ERR_BAD_STATEID) {
1413 exception.retry = 1;
1416 if (status == -EAGAIN) {
1417 /* We must have found a delegation */
1418 exception.retry = 1;
1421 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1422 status, &exception));
1423 } while (exception.retry);
1427 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1428 struct nfs_fattr *fattr, struct iattr *sattr,
1429 struct nfs4_state *state)
1431 struct nfs_server *server = NFS_SERVER(inode);
1432 struct nfs_setattrargs arg = {
1433 .fh = NFS_FH(inode),
1436 .bitmask = server->attr_bitmask,
1438 struct nfs_setattrres res = {
1442 struct rpc_message msg = {
1443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1448 unsigned long timestamp = jiffies;
1451 nfs_fattr_init(fattr);
1453 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1454 /* Use that stateid */
1455 } else if (state != NULL) {
1456 nfs4_copy_stateid(&arg.stateid, state, current->files);
1458 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1460 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1461 if (status == 0 && state != NULL)
1462 renew_lease(server, timestamp);
1466 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1467 struct nfs_fattr *fattr, struct iattr *sattr,
1468 struct nfs4_state *state)
1470 struct nfs_server *server = NFS_SERVER(inode);
1471 struct nfs4_exception exception = { };
1474 err = nfs4_handle_exception(server,
1475 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1477 } while (exception.retry);
1481 struct nfs4_closedata {
1483 struct inode *inode;
1484 struct nfs4_state *state;
1485 struct nfs_closeargs arg;
1486 struct nfs_closeres res;
1487 struct nfs_fattr fattr;
1488 unsigned long timestamp;
1491 static void nfs4_free_closedata(void *data)
1493 struct nfs4_closedata *calldata = data;
1494 struct nfs4_state_owner *sp = calldata->state->owner;
1496 nfs4_put_open_state(calldata->state);
1497 nfs_free_seqid(calldata->arg.seqid);
1498 nfs4_put_state_owner(sp);
1499 path_put(&calldata->path);
1503 static void nfs4_close_done(struct rpc_task *task, void *data)
1505 struct nfs4_closedata *calldata = data;
1506 struct nfs4_state *state = calldata->state;
1507 struct nfs_server *server = NFS_SERVER(calldata->inode);
1509 if (RPC_ASSASSINATED(task))
1511 /* hmm. we are done with the inode, and in the process of freeing
1512 * the state_owner. we keep this around to process errors
1514 switch (task->tk_status) {
1516 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1517 renew_lease(server, calldata->timestamp);
1519 case -NFS4ERR_STALE_STATEID:
1520 case -NFS4ERR_OLD_STATEID:
1521 case -NFS4ERR_BAD_STATEID:
1522 case -NFS4ERR_EXPIRED:
1523 if (calldata->arg.fmode == 0)
1526 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1527 rpc_restart_call(task);
1531 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1534 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1536 struct nfs4_closedata *calldata = data;
1537 struct nfs4_state *state = calldata->state;
1538 int clear_rd, clear_wr, clear_rdwr;
1540 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1543 clear_rd = clear_wr = clear_rdwr = 0;
1544 spin_lock(&state->owner->so_lock);
1545 /* Calculate the change in open mode */
1546 if (state->n_rdwr == 0) {
1547 if (state->n_rdonly == 0) {
1548 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1549 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1551 if (state->n_wronly == 0) {
1552 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1553 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1556 spin_unlock(&state->owner->so_lock);
1557 if (!clear_rd && !clear_wr && !clear_rdwr) {
1558 /* Note: exit _without_ calling nfs4_close_done */
1559 task->tk_action = NULL;
1562 nfs_fattr_init(calldata->res.fattr);
1563 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1564 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1565 calldata->arg.fmode = FMODE_READ;
1566 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1567 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1568 calldata->arg.fmode = FMODE_WRITE;
1570 calldata->timestamp = jiffies;
1571 rpc_call_start(task);
1574 static const struct rpc_call_ops nfs4_close_ops = {
1575 .rpc_call_prepare = nfs4_close_prepare,
1576 .rpc_call_done = nfs4_close_done,
1577 .rpc_release = nfs4_free_closedata,
1581 * It is possible for data to be read/written from a mem-mapped file
1582 * after the sys_close call (which hits the vfs layer as a flush).
1583 * This means that we can't safely call nfsv4 close on a file until
1584 * the inode is cleared. This in turn means that we are not good
1585 * NFSv4 citizens - we do not indicate to the server to update the file's
1586 * share state even when we are done with one of the three share
1587 * stateid's in the inode.
1589 * NOTE: Caller must be holding the sp->so_owner semaphore!
1591 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1593 struct nfs_server *server = NFS_SERVER(state->inode);
1594 struct nfs4_closedata *calldata;
1595 struct nfs4_state_owner *sp = state->owner;
1596 struct rpc_task *task;
1597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1599 .rpc_cred = state->owner->so_cred,
1601 struct rpc_task_setup task_setup_data = {
1602 .rpc_client = server->client,
1603 .rpc_message = &msg,
1604 .callback_ops = &nfs4_close_ops,
1605 .workqueue = nfsiod_workqueue,
1606 .flags = RPC_TASK_ASYNC,
1608 int status = -ENOMEM;
1610 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1611 if (calldata == NULL)
1613 calldata->inode = state->inode;
1614 calldata->state = state;
1615 calldata->arg.fh = NFS_FH(state->inode);
1616 calldata->arg.stateid = &state->open_stateid;
1617 /* Serialization for the sequence id */
1618 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1619 if (calldata->arg.seqid == NULL)
1620 goto out_free_calldata;
1621 calldata->arg.fmode = 0;
1622 calldata->arg.bitmask = server->cache_consistency_bitmask;
1623 calldata->res.fattr = &calldata->fattr;
1624 calldata->res.seqid = calldata->arg.seqid;
1625 calldata->res.server = server;
1626 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1627 calldata->path.mnt = mntget(path->mnt);
1628 calldata->path.dentry = dget(path->dentry);
1630 msg.rpc_argp = &calldata->arg,
1631 msg.rpc_resp = &calldata->res,
1632 task_setup_data.callback_data = calldata;
1633 task = rpc_run_task(&task_setup_data);
1635 return PTR_ERR(task);
1638 status = rpc_wait_for_completion_task(task);
1644 nfs4_put_open_state(state);
1645 nfs4_put_state_owner(sp);
1649 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1654 /* If the open_intent is for execute, we have an extra check to make */
1655 if (fmode & FMODE_EXEC) {
1656 ret = nfs_may_open(state->inode,
1657 state->owner->so_cred,
1658 nd->intent.open.flags);
1662 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1663 if (!IS_ERR(filp)) {
1664 struct nfs_open_context *ctx;
1665 ctx = nfs_file_open_context(filp);
1669 ret = PTR_ERR(filp);
1671 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1676 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1678 struct path path = {
1679 .mnt = nd->path.mnt,
1682 struct dentry *parent;
1684 struct rpc_cred *cred;
1685 struct nfs4_state *state;
1687 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1689 if (nd->flags & LOOKUP_CREATE) {
1690 attr.ia_mode = nd->intent.open.create_mode;
1691 attr.ia_valid = ATTR_MODE;
1692 if (!IS_POSIXACL(dir))
1693 attr.ia_mode &= ~current_umask();
1696 BUG_ON(nd->intent.open.flags & O_CREAT);
1699 cred = rpc_lookup_cred();
1701 return (struct dentry *)cred;
1702 parent = dentry->d_parent;
1703 /* Protect against concurrent sillydeletes */
1704 nfs_block_sillyrename(parent);
1705 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1707 if (IS_ERR(state)) {
1708 if (PTR_ERR(state) == -ENOENT) {
1709 d_add(dentry, NULL);
1710 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1712 nfs_unblock_sillyrename(parent);
1713 return (struct dentry *)state;
1715 res = d_add_unique(dentry, igrab(state->inode));
1718 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1719 nfs_unblock_sillyrename(parent);
1720 nfs4_intent_set_file(nd, &path, state, fmode);
1725 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1727 struct path path = {
1728 .mnt = nd->path.mnt,
1731 struct rpc_cred *cred;
1732 struct nfs4_state *state;
1733 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1735 cred = rpc_lookup_cred();
1737 return PTR_ERR(cred);
1738 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1740 if (IS_ERR(state)) {
1741 switch (PTR_ERR(state)) {
1747 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1753 if (state->inode == dentry->d_inode) {
1754 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1755 nfs4_intent_set_file(nd, &path, state, fmode);
1758 nfs4_close_sync(&path, state, fmode);
1764 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1766 if (ctx->state == NULL)
1769 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1771 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1774 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1776 struct nfs4_server_caps_arg args = {
1779 struct nfs4_server_caps_res res = {};
1780 struct rpc_message msg = {
1781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1787 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1789 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1790 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1791 server->caps |= NFS_CAP_ACLS;
1792 if (res.has_links != 0)
1793 server->caps |= NFS_CAP_HARDLINKS;
1794 if (res.has_symlinks != 0)
1795 server->caps |= NFS_CAP_SYMLINKS;
1796 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1797 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1798 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1799 server->acl_bitmask = res.acl_bitmask;
1805 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1807 struct nfs4_exception exception = { };
1810 err = nfs4_handle_exception(server,
1811 _nfs4_server_capabilities(server, fhandle),
1813 } while (exception.retry);
1817 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1818 struct nfs_fsinfo *info)
1820 struct nfs4_lookup_root_arg args = {
1821 .bitmask = nfs4_fattr_bitmap,
1823 struct nfs4_lookup_res res = {
1825 .fattr = info->fattr,
1828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1833 nfs_fattr_init(info->fattr);
1834 return nfs4_call_sync(server, &msg, &args, &res, 0);
1837 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1838 struct nfs_fsinfo *info)
1840 struct nfs4_exception exception = { };
1843 err = nfs4_handle_exception(server,
1844 _nfs4_lookup_root(server, fhandle, info),
1846 } while (exception.retry);
1851 * get the file handle for the "/" directory on the server
1853 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1854 struct nfs_fsinfo *info)
1858 status = nfs4_lookup_root(server, fhandle, info);
1860 status = nfs4_server_capabilities(server, fhandle);
1862 status = nfs4_do_fsinfo(server, fhandle, info);
1863 return nfs4_map_errors(status);
1867 * Get locations and (maybe) other attributes of a referral.
1868 * Note that we'll actually follow the referral later when
1869 * we detect fsid mismatch in inode revalidation
1871 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1873 int status = -ENOMEM;
1874 struct page *page = NULL;
1875 struct nfs4_fs_locations *locations = NULL;
1877 page = alloc_page(GFP_KERNEL);
1880 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1881 if (locations == NULL)
1884 status = nfs4_proc_fs_locations(dir, name, locations, page);
1887 /* Make sure server returned a different fsid for the referral */
1888 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1889 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1894 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1895 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1897 fattr->mode = S_IFDIR;
1898 memset(fhandle, 0, sizeof(struct nfs_fh));
1907 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1909 struct nfs4_getattr_arg args = {
1911 .bitmask = server->attr_bitmask,
1913 struct nfs4_getattr_res res = {
1917 struct rpc_message msg = {
1918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1923 nfs_fattr_init(fattr);
1924 return nfs4_call_sync(server, &msg, &args, &res, 0);
1927 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1929 struct nfs4_exception exception = { };
1932 err = nfs4_handle_exception(server,
1933 _nfs4_proc_getattr(server, fhandle, fattr),
1935 } while (exception.retry);
1940 * The file is not closed if it is opened due to the a request to change
1941 * the size of the file. The open call will not be needed once the
1942 * VFS layer lookup-intents are implemented.
1944 * Close is called when the inode is destroyed.
1945 * If we haven't opened the file for O_WRONLY, we
1946 * need to in the size_change case to obtain a stateid.
1949 * Because OPEN is always done by name in nfsv4, it is
1950 * possible that we opened a different file by the same
1951 * name. We can recognize this race condition, but we
1952 * can't do anything about it besides returning an error.
1954 * This will be fixed with VFS changes (lookup-intent).
1957 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1958 struct iattr *sattr)
1960 struct inode *inode = dentry->d_inode;
1961 struct rpc_cred *cred = NULL;
1962 struct nfs4_state *state = NULL;
1965 nfs_fattr_init(fattr);
1967 /* Search for an existing open(O_WRITE) file */
1968 if (sattr->ia_valid & ATTR_FILE) {
1969 struct nfs_open_context *ctx;
1971 ctx = nfs_file_open_context(sattr->ia_file);
1978 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1980 nfs_setattr_update_inode(inode, sattr);
1984 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1985 const struct qstr *name, struct nfs_fh *fhandle,
1986 struct nfs_fattr *fattr)
1989 struct nfs4_lookup_arg args = {
1990 .bitmask = server->attr_bitmask,
1994 struct nfs4_lookup_res res = {
1999 struct rpc_message msg = {
2000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2005 nfs_fattr_init(fattr);
2007 dprintk("NFS call lookupfh %s\n", name->name);
2008 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2009 dprintk("NFS reply lookupfh: %d\n", status);
2013 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2014 struct qstr *name, struct nfs_fh *fhandle,
2015 struct nfs_fattr *fattr)
2017 struct nfs4_exception exception = { };
2020 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2022 if (err == -NFS4ERR_MOVED) {
2026 err = nfs4_handle_exception(server, err, &exception);
2027 } while (exception.retry);
2031 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2032 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2036 dprintk("NFS call lookup %s\n", name->name);
2037 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2038 if (status == -NFS4ERR_MOVED)
2039 status = nfs4_get_referral(dir, name, fattr, fhandle);
2040 dprintk("NFS reply lookup: %d\n", status);
2044 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2046 struct nfs4_exception exception = { };
2049 err = nfs4_handle_exception(NFS_SERVER(dir),
2050 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2052 } while (exception.retry);
2056 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2058 struct nfs_server *server = NFS_SERVER(inode);
2059 struct nfs_fattr fattr;
2060 struct nfs4_accessargs args = {
2061 .fh = NFS_FH(inode),
2062 .bitmask = server->attr_bitmask,
2064 struct nfs4_accessres res = {
2068 struct rpc_message msg = {
2069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2072 .rpc_cred = entry->cred,
2074 int mode = entry->mask;
2078 * Determine which access bits we want to ask for...
2080 if (mode & MAY_READ)
2081 args.access |= NFS4_ACCESS_READ;
2082 if (S_ISDIR(inode->i_mode)) {
2083 if (mode & MAY_WRITE)
2084 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2085 if (mode & MAY_EXEC)
2086 args.access |= NFS4_ACCESS_LOOKUP;
2088 if (mode & MAY_WRITE)
2089 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2090 if (mode & MAY_EXEC)
2091 args.access |= NFS4_ACCESS_EXECUTE;
2093 nfs_fattr_init(&fattr);
2094 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2097 if (res.access & NFS4_ACCESS_READ)
2098 entry->mask |= MAY_READ;
2099 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2100 entry->mask |= MAY_WRITE;
2101 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2102 entry->mask |= MAY_EXEC;
2103 nfs_refresh_inode(inode, &fattr);
2108 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2110 struct nfs4_exception exception = { };
2113 err = nfs4_handle_exception(NFS_SERVER(inode),
2114 _nfs4_proc_access(inode, entry),
2116 } while (exception.retry);
2121 * TODO: For the time being, we don't try to get any attributes
2122 * along with any of the zero-copy operations READ, READDIR,
2125 * In the case of the first three, we want to put the GETATTR
2126 * after the read-type operation -- this is because it is hard
2127 * to predict the length of a GETATTR response in v4, and thus
2128 * align the READ data correctly. This means that the GETATTR
2129 * may end up partially falling into the page cache, and we should
2130 * shift it into the 'tail' of the xdr_buf before processing.
2131 * To do this efficiently, we need to know the total length
2132 * of data received, which doesn't seem to be available outside
2135 * In the case of WRITE, we also want to put the GETATTR after
2136 * the operation -- in this case because we want to make sure
2137 * we get the post-operation mtime and size. This means that
2138 * we can't use xdr_encode_pages() as written: we need a variant
2139 * of it which would leave room in the 'tail' iovec.
2141 * Both of these changes to the XDR layer would in fact be quite
2142 * minor, but I decided to leave them for a subsequent patch.
2144 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2145 unsigned int pgbase, unsigned int pglen)
2147 struct nfs4_readlink args = {
2148 .fh = NFS_FH(inode),
2153 struct nfs4_readlink_res res;
2154 struct rpc_message msg = {
2155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2160 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2163 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2164 unsigned int pgbase, unsigned int pglen)
2166 struct nfs4_exception exception = { };
2169 err = nfs4_handle_exception(NFS_SERVER(inode),
2170 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2172 } while (exception.retry);
2178 * We will need to arrange for the VFS layer to provide an atomic open.
2179 * Until then, this create/open method is prone to inefficiency and race
2180 * conditions due to the lookup, create, and open VFS calls from sys_open()
2181 * placed on the wire.
2183 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2184 * The file will be opened again in the subsequent VFS open call
2185 * (nfs4_proc_file_open).
2187 * The open for read will just hang around to be used by any process that
2188 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2192 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2193 int flags, struct nameidata *nd)
2195 struct path path = {
2196 .mnt = nd->path.mnt,
2199 struct nfs4_state *state;
2200 struct rpc_cred *cred;
2201 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2204 cred = rpc_lookup_cred();
2206 status = PTR_ERR(cred);
2209 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2211 if (IS_ERR(state)) {
2212 status = PTR_ERR(state);
2215 d_add(dentry, igrab(state->inode));
2216 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2217 if (flags & O_EXCL) {
2218 struct nfs_fattr fattr;
2219 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2221 nfs_setattr_update_inode(state->inode, sattr);
2222 nfs_post_op_update_inode(state->inode, &fattr);
2224 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2225 status = nfs4_intent_set_file(nd, &path, state, fmode);
2227 nfs4_close_sync(&path, state, fmode);
2234 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2236 struct nfs_server *server = NFS_SERVER(dir);
2237 struct nfs_removeargs args = {
2239 .name.len = name->len,
2240 .name.name = name->name,
2241 .bitmask = server->attr_bitmask,
2243 struct nfs_removeres res = {
2246 struct rpc_message msg = {
2247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2253 nfs_fattr_init(&res.dir_attr);
2254 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2256 update_changeattr(dir, &res.cinfo);
2257 nfs_post_op_update_inode(dir, &res.dir_attr);
2262 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2264 struct nfs4_exception exception = { };
2267 err = nfs4_handle_exception(NFS_SERVER(dir),
2268 _nfs4_proc_remove(dir, name),
2270 } while (exception.retry);
2274 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2276 struct nfs_server *server = NFS_SERVER(dir);
2277 struct nfs_removeargs *args = msg->rpc_argp;
2278 struct nfs_removeres *res = msg->rpc_resp;
2280 args->bitmask = server->cache_consistency_bitmask;
2281 res->server = server;
2282 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2285 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2287 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2289 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2291 update_changeattr(dir, &res->cinfo);
2292 nfs_post_op_update_inode(dir, &res->dir_attr);
2296 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2297 struct inode *new_dir, struct qstr *new_name)
2299 struct nfs_server *server = NFS_SERVER(old_dir);
2300 struct nfs4_rename_arg arg = {
2301 .old_dir = NFS_FH(old_dir),
2302 .new_dir = NFS_FH(new_dir),
2303 .old_name = old_name,
2304 .new_name = new_name,
2305 .bitmask = server->attr_bitmask,
2307 struct nfs_fattr old_fattr, new_fattr;
2308 struct nfs4_rename_res res = {
2310 .old_fattr = &old_fattr,
2311 .new_fattr = &new_fattr,
2313 struct rpc_message msg = {
2314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2320 nfs_fattr_init(res.old_fattr);
2321 nfs_fattr_init(res.new_fattr);
2322 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2325 update_changeattr(old_dir, &res.old_cinfo);
2326 nfs_post_op_update_inode(old_dir, res.old_fattr);
2327 update_changeattr(new_dir, &res.new_cinfo);
2328 nfs_post_op_update_inode(new_dir, res.new_fattr);
2333 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2334 struct inode *new_dir, struct qstr *new_name)
2336 struct nfs4_exception exception = { };
2339 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2340 _nfs4_proc_rename(old_dir, old_name,
2343 } while (exception.retry);
2347 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2349 struct nfs_server *server = NFS_SERVER(inode);
2350 struct nfs4_link_arg arg = {
2351 .fh = NFS_FH(inode),
2352 .dir_fh = NFS_FH(dir),
2354 .bitmask = server->attr_bitmask,
2356 struct nfs_fattr fattr, dir_attr;
2357 struct nfs4_link_res res = {
2360 .dir_attr = &dir_attr,
2362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2369 nfs_fattr_init(res.fattr);
2370 nfs_fattr_init(res.dir_attr);
2371 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2373 update_changeattr(dir, &res.cinfo);
2374 nfs_post_op_update_inode(dir, res.dir_attr);
2375 nfs_post_op_update_inode(inode, res.fattr);
2381 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2383 struct nfs4_exception exception = { };
2386 err = nfs4_handle_exception(NFS_SERVER(inode),
2387 _nfs4_proc_link(inode, dir, name),
2389 } while (exception.retry);
2393 struct nfs4_createdata {
2394 struct rpc_message msg;
2395 struct nfs4_create_arg arg;
2396 struct nfs4_create_res res;
2398 struct nfs_fattr fattr;
2399 struct nfs_fattr dir_fattr;
2402 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2403 struct qstr *name, struct iattr *sattr, u32 ftype)
2405 struct nfs4_createdata *data;
2407 data = kzalloc(sizeof(*data), GFP_KERNEL);
2409 struct nfs_server *server = NFS_SERVER(dir);
2411 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2412 data->msg.rpc_argp = &data->arg;
2413 data->msg.rpc_resp = &data->res;
2414 data->arg.dir_fh = NFS_FH(dir);
2415 data->arg.server = server;
2416 data->arg.name = name;
2417 data->arg.attrs = sattr;
2418 data->arg.ftype = ftype;
2419 data->arg.bitmask = server->attr_bitmask;
2420 data->res.server = server;
2421 data->res.fh = &data->fh;
2422 data->res.fattr = &data->fattr;
2423 data->res.dir_fattr = &data->dir_fattr;
2424 nfs_fattr_init(data->res.fattr);
2425 nfs_fattr_init(data->res.dir_fattr);
2430 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2432 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2433 &data->arg, &data->res, 1);
2435 update_changeattr(dir, &data->res.dir_cinfo);
2436 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2437 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2442 static void nfs4_free_createdata(struct nfs4_createdata *data)
2447 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2448 struct page *page, unsigned int len, struct iattr *sattr)
2450 struct nfs4_createdata *data;
2451 int status = -ENAMETOOLONG;
2453 if (len > NFS4_MAXPATHLEN)
2457 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2461 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2462 data->arg.u.symlink.pages = &page;
2463 data->arg.u.symlink.len = len;
2465 status = nfs4_do_create(dir, dentry, data);
2467 nfs4_free_createdata(data);
2472 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2473 struct page *page, unsigned int len, struct iattr *sattr)
2475 struct nfs4_exception exception = { };
2478 err = nfs4_handle_exception(NFS_SERVER(dir),
2479 _nfs4_proc_symlink(dir, dentry, page,
2482 } while (exception.retry);
2486 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2487 struct iattr *sattr)
2489 struct nfs4_createdata *data;
2490 int status = -ENOMEM;
2492 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2496 status = nfs4_do_create(dir, dentry, data);
2498 nfs4_free_createdata(data);
2503 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2504 struct iattr *sattr)
2506 struct nfs4_exception exception = { };
2509 err = nfs4_handle_exception(NFS_SERVER(dir),
2510 _nfs4_proc_mkdir(dir, dentry, sattr),
2512 } while (exception.retry);
2516 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2517 u64 cookie, struct page *page, unsigned int count, int plus)
2519 struct inode *dir = dentry->d_inode;
2520 struct nfs4_readdir_arg args = {
2525 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2527 struct nfs4_readdir_res res;
2528 struct rpc_message msg = {
2529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2536 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2537 dentry->d_parent->d_name.name,
2538 dentry->d_name.name,
2539 (unsigned long long)cookie);
2540 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2541 res.pgbase = args.pgbase;
2542 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2544 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2546 nfs_invalidate_atime(dir);
2548 dprintk("%s: returns %d\n", __func__, status);
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 nfs4_exception exception = { };
2558 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2559 _nfs4_proc_readdir(dentry, cred, cookie,
2562 } while (exception.retry);
2566 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2567 struct iattr *sattr, dev_t rdev)
2569 struct nfs4_createdata *data;
2570 int mode = sattr->ia_mode;
2571 int status = -ENOMEM;
2573 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2574 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2576 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2581 data->arg.ftype = NF4FIFO;
2582 else if (S_ISBLK(mode)) {
2583 data->arg.ftype = NF4BLK;
2584 data->arg.u.device.specdata1 = MAJOR(rdev);
2585 data->arg.u.device.specdata2 = MINOR(rdev);
2587 else if (S_ISCHR(mode)) {
2588 data->arg.ftype = NF4CHR;
2589 data->arg.u.device.specdata1 = MAJOR(rdev);
2590 data->arg.u.device.specdata2 = MINOR(rdev);
2593 status = nfs4_do_create(dir, dentry, data);
2595 nfs4_free_createdata(data);
2600 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2601 struct iattr *sattr, dev_t rdev)
2603 struct nfs4_exception exception = { };
2606 err = nfs4_handle_exception(NFS_SERVER(dir),
2607 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2609 } while (exception.retry);
2613 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2614 struct nfs_fsstat *fsstat)
2616 struct nfs4_statfs_arg args = {
2618 .bitmask = server->attr_bitmask,
2620 struct nfs4_statfs_res res = {
2623 struct rpc_message msg = {
2624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2629 nfs_fattr_init(fsstat->fattr);
2630 return nfs4_call_sync(server, &msg, &args, &res, 0);
2633 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2635 struct nfs4_exception exception = { };
2638 err = nfs4_handle_exception(server,
2639 _nfs4_proc_statfs(server, fhandle, fsstat),
2641 } while (exception.retry);
2645 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2646 struct nfs_fsinfo *fsinfo)
2648 struct nfs4_fsinfo_arg args = {
2650 .bitmask = server->attr_bitmask,
2652 struct nfs4_fsinfo_res res = {
2655 struct rpc_message msg = {
2656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2661 return nfs4_call_sync(server, &msg, &args, &res, 0);
2664 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2666 struct nfs4_exception exception = { };
2670 err = nfs4_handle_exception(server,
2671 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2673 } while (exception.retry);
2677 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2679 nfs_fattr_init(fsinfo->fattr);
2680 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2683 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2684 struct nfs_pathconf *pathconf)
2686 struct nfs4_pathconf_arg args = {
2688 .bitmask = server->attr_bitmask,
2690 struct nfs4_pathconf_res res = {
2691 .pathconf = pathconf,
2693 struct rpc_message msg = {
2694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2699 /* None of the pathconf attributes are mandatory to implement */
2700 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2701 memset(pathconf, 0, sizeof(*pathconf));
2705 nfs_fattr_init(pathconf->fattr);
2706 return nfs4_call_sync(server, &msg, &args, &res, 0);
2709 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2710 struct nfs_pathconf *pathconf)
2712 struct nfs4_exception exception = { };
2716 err = nfs4_handle_exception(server,
2717 _nfs4_proc_pathconf(server, fhandle, pathconf),
2719 } while (exception.retry);
2723 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2725 struct nfs_server *server = NFS_SERVER(data->inode);
2727 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2728 rpc_restart_call(task);
2732 nfs_invalidate_atime(data->inode);
2733 if (task->tk_status > 0)
2734 renew_lease(server, data->timestamp);
2738 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2740 data->timestamp = jiffies;
2741 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2744 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2746 struct inode *inode = data->inode;
2748 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2749 rpc_restart_call(task);
2752 if (task->tk_status >= 0) {
2753 renew_lease(NFS_SERVER(inode), data->timestamp);
2754 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2759 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2761 struct nfs_server *server = NFS_SERVER(data->inode);
2763 data->args.bitmask = server->cache_consistency_bitmask;
2764 data->res.server = server;
2765 data->timestamp = jiffies;
2767 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2770 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2772 struct inode *inode = data->inode;
2774 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2775 rpc_restart_call(task);
2778 nfs_refresh_inode(inode, data->res.fattr);
2782 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2784 struct nfs_server *server = NFS_SERVER(data->inode);
2786 data->args.bitmask = server->cache_consistency_bitmask;
2787 data->res.server = server;
2788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2792 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2793 * standalone procedure for queueing an asynchronous RENEW.
2795 static void nfs4_renew_done(struct rpc_task *task, void *data)
2797 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2798 unsigned long timestamp = (unsigned long)data;
2800 if (task->tk_status < 0) {
2801 /* Unless we're shutting down, schedule state recovery! */
2802 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2803 nfs4_schedule_state_recovery(clp);
2806 spin_lock(&clp->cl_lock);
2807 if (time_before(clp->cl_last_renewal,timestamp))
2808 clp->cl_last_renewal = timestamp;
2809 spin_unlock(&clp->cl_lock);
2812 static const struct rpc_call_ops nfs4_renew_ops = {
2813 .rpc_call_done = nfs4_renew_done,
2816 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2824 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2825 &nfs4_renew_ops, (void *)jiffies);
2828 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2835 unsigned long now = jiffies;
2838 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2841 spin_lock(&clp->cl_lock);
2842 if (time_before(clp->cl_last_renewal,now))
2843 clp->cl_last_renewal = now;
2844 spin_unlock(&clp->cl_lock);
2848 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2850 return (server->caps & NFS_CAP_ACLS)
2851 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2852 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2855 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2856 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2859 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2861 static void buf_to_pages(const void *buf, size_t buflen,
2862 struct page **pages, unsigned int *pgbase)
2864 const void *p = buf;
2866 *pgbase = offset_in_page(buf);
2868 while (p < buf + buflen) {
2869 *(pages++) = virt_to_page(p);
2870 p += PAGE_CACHE_SIZE;
2874 struct nfs4_cached_acl {
2880 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2882 struct nfs_inode *nfsi = NFS_I(inode);
2884 spin_lock(&inode->i_lock);
2885 kfree(nfsi->nfs4_acl);
2886 nfsi->nfs4_acl = acl;
2887 spin_unlock(&inode->i_lock);
2890 static void nfs4_zap_acl_attr(struct inode *inode)
2892 nfs4_set_cached_acl(inode, NULL);
2895 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2897 struct nfs_inode *nfsi = NFS_I(inode);
2898 struct nfs4_cached_acl *acl;
2901 spin_lock(&inode->i_lock);
2902 acl = nfsi->nfs4_acl;
2905 if (buf == NULL) /* user is just asking for length */
2907 if (acl->cached == 0)
2909 ret = -ERANGE; /* see getxattr(2) man page */
2910 if (acl->len > buflen)
2912 memcpy(buf, acl->data, acl->len);
2916 spin_unlock(&inode->i_lock);
2920 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2922 struct nfs4_cached_acl *acl;
2924 if (buf && acl_len <= PAGE_SIZE) {
2925 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2929 memcpy(acl->data, buf, acl_len);
2931 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2938 nfs4_set_cached_acl(inode, acl);
2941 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2943 struct page *pages[NFS4ACL_MAXPAGES];
2944 struct nfs_getaclargs args = {
2945 .fh = NFS_FH(inode),
2949 struct nfs_getaclres res = {
2953 struct rpc_message msg = {
2954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2958 struct page *localpage = NULL;
2961 if (buflen < PAGE_SIZE) {
2962 /* As long as we're doing a round trip to the server anyway,
2963 * let's be prepared for a page of acl data. */
2964 localpage = alloc_page(GFP_KERNEL);
2965 resp_buf = page_address(localpage);
2966 if (localpage == NULL)
2968 args.acl_pages[0] = localpage;
2969 args.acl_pgbase = 0;
2970 args.acl_len = PAGE_SIZE;
2973 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2975 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2978 if (res.acl_len > args.acl_len)
2979 nfs4_write_cached_acl(inode, NULL, res.acl_len);
2981 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
2984 if (res.acl_len > buflen)
2987 memcpy(buf, resp_buf, res.acl_len);
2992 __free_page(localpage);
2996 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2998 struct nfs4_exception exception = { };
3001 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3004 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3005 } while (exception.retry);
3009 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3011 struct nfs_server *server = NFS_SERVER(inode);
3014 if (!nfs4_server_supports_acls(server))
3016 ret = nfs_revalidate_inode(server, inode);
3019 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3020 nfs_zap_acl_cache(inode);
3021 ret = nfs4_read_cached_acl(inode, buf, buflen);
3024 return nfs4_get_acl_uncached(inode, buf, buflen);
3027 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3029 struct nfs_server *server = NFS_SERVER(inode);
3030 struct page *pages[NFS4ACL_MAXPAGES];
3031 struct nfs_setaclargs arg = {
3032 .fh = NFS_FH(inode),
3036 struct nfs_setaclres res;
3037 struct rpc_message msg = {
3038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3044 if (!nfs4_server_supports_acls(server))
3046 nfs_inode_return_delegation(inode);
3047 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3048 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3049 nfs_access_zap_cache(inode);
3050 nfs_zap_acl_cache(inode);
3054 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3056 struct nfs4_exception exception = { };
3059 err = nfs4_handle_exception(NFS_SERVER(inode),
3060 __nfs4_proc_set_acl(inode, buf, buflen),
3062 } while (exception.retry);
3067 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3069 struct nfs_client *clp = server->nfs_client;
3071 if (!clp || task->tk_status >= 0)
3073 switch(task->tk_status) {
3074 case -NFS4ERR_ADMIN_REVOKED:
3075 case -NFS4ERR_BAD_STATEID:
3076 case -NFS4ERR_OPENMODE:
3079 nfs4_state_mark_reclaim_nograce(clp, state);
3080 case -NFS4ERR_STALE_CLIENTID:
3081 case -NFS4ERR_STALE_STATEID:
3082 case -NFS4ERR_EXPIRED:
3083 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3084 nfs4_schedule_state_recovery(clp);
3085 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3086 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3087 task->tk_status = 0;
3089 case -NFS4ERR_DELAY:
3090 nfs_inc_server_stats(server, NFSIOS_DELAY);
3091 case -NFS4ERR_GRACE:
3092 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3093 task->tk_status = 0;
3095 case -NFS4ERR_OLD_STATEID:
3096 task->tk_status = 0;
3099 task->tk_status = nfs4_map_errors(task->tk_status);
3103 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3105 nfs4_verifier sc_verifier;
3106 struct nfs4_setclientid setclientid = {
3107 .sc_verifier = &sc_verifier,
3110 struct rpc_message msg = {
3111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3112 .rpc_argp = &setclientid,
3120 p = (__be32*)sc_verifier.data;
3121 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3122 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3125 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3126 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3128 rpc_peeraddr2str(clp->cl_rpcclient,
3130 rpc_peeraddr2str(clp->cl_rpcclient,
3132 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3133 clp->cl_id_uniquifier);
3134 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3135 sizeof(setclientid.sc_netid),
3136 rpc_peeraddr2str(clp->cl_rpcclient,
3137 RPC_DISPLAY_NETID));
3138 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3139 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3140 clp->cl_ipaddr, port >> 8, port & 255);
3142 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3143 if (status != -NFS4ERR_CLID_INUSE)
3148 ssleep(clp->cl_lease_time + 1);
3150 if (++clp->cl_id_uniquifier == 0)
3156 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3158 struct nfs_fsinfo fsinfo;
3159 struct rpc_message msg = {
3160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3162 .rpc_resp = &fsinfo,
3169 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3171 spin_lock(&clp->cl_lock);
3172 clp->cl_lease_time = fsinfo.lease_time * HZ;
3173 clp->cl_last_renewal = now;
3174 spin_unlock(&clp->cl_lock);
3179 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3184 err = _nfs4_proc_setclientid_confirm(clp, cred);
3188 case -NFS4ERR_RESOURCE:
3189 /* The IBM lawyers misread another document! */
3190 case -NFS4ERR_DELAY:
3191 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3197 struct nfs4_delegreturndata {
3198 struct nfs4_delegreturnargs args;
3199 struct nfs4_delegreturnres res;
3201 nfs4_stateid stateid;
3202 unsigned long timestamp;
3203 struct nfs_fattr fattr;
3207 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3209 struct nfs4_delegreturndata *data = calldata;
3210 data->rpc_status = task->tk_status;
3211 if (data->rpc_status == 0)
3212 renew_lease(data->res.server, data->timestamp);
3215 static void nfs4_delegreturn_release(void *calldata)
3220 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3221 .rpc_call_done = nfs4_delegreturn_done,
3222 .rpc_release = nfs4_delegreturn_release,
3225 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3227 struct nfs4_delegreturndata *data;
3228 struct nfs_server *server = NFS_SERVER(inode);
3229 struct rpc_task *task;
3230 struct rpc_message msg = {
3231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3234 struct rpc_task_setup task_setup_data = {
3235 .rpc_client = server->client,
3236 .rpc_message = &msg,
3237 .callback_ops = &nfs4_delegreturn_ops,
3238 .flags = RPC_TASK_ASYNC,
3242 data = kmalloc(sizeof(*data), GFP_KERNEL);
3245 data->args.fhandle = &data->fh;
3246 data->args.stateid = &data->stateid;
3247 data->args.bitmask = server->attr_bitmask;
3248 nfs_copy_fh(&data->fh, NFS_FH(inode));
3249 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3250 data->res.fattr = &data->fattr;
3251 data->res.server = server;
3252 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3253 nfs_fattr_init(data->res.fattr);
3254 data->timestamp = jiffies;
3255 data->rpc_status = 0;
3257 task_setup_data.callback_data = data;
3258 msg.rpc_argp = &data->args,
3259 msg.rpc_resp = &data->res,
3260 task = rpc_run_task(&task_setup_data);
3262 return PTR_ERR(task);
3265 status = nfs4_wait_for_completion_rpc_task(task);
3268 status = data->rpc_status;
3271 nfs_refresh_inode(inode, &data->fattr);
3277 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3279 struct nfs_server *server = NFS_SERVER(inode);
3280 struct nfs4_exception exception = { };
3283 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3285 case -NFS4ERR_STALE_STATEID:
3286 case -NFS4ERR_EXPIRED:
3290 err = nfs4_handle_exception(server, err, &exception);
3291 } while (exception.retry);
3295 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3296 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3299 * sleep, with exponential backoff, and retry the LOCK operation.
3301 static unsigned long
3302 nfs4_set_lock_task_retry(unsigned long timeout)
3304 schedule_timeout_killable(timeout);
3306 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3307 return NFS4_LOCK_MAXTIMEOUT;
3311 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3313 struct inode *inode = state->inode;
3314 struct nfs_server *server = NFS_SERVER(inode);
3315 struct nfs_client *clp = server->nfs_client;
3316 struct nfs_lockt_args arg = {
3317 .fh = NFS_FH(inode),
3320 struct nfs_lockt_res res = {
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3327 .rpc_cred = state->owner->so_cred,
3329 struct nfs4_lock_state *lsp;
3332 arg.lock_owner.clientid = clp->cl_clientid;
3333 status = nfs4_set_lock_state(state, request);
3336 lsp = request->fl_u.nfs4_fl.owner;
3337 arg.lock_owner.id = lsp->ls_id.id;
3338 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3341 request->fl_type = F_UNLCK;
3343 case -NFS4ERR_DENIED:
3346 request->fl_ops->fl_release_private(request);
3351 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3353 struct nfs4_exception exception = { };
3357 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3358 _nfs4_proc_getlk(state, cmd, request),
3360 } while (exception.retry);
3364 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3367 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3369 res = posix_lock_file_wait(file, fl);
3372 res = flock_lock_file_wait(file, fl);
3380 struct nfs4_unlockdata {
3381 struct nfs_locku_args arg;
3382 struct nfs_locku_res res;
3383 struct nfs4_lock_state *lsp;
3384 struct nfs_open_context *ctx;
3385 struct file_lock fl;
3386 const struct nfs_server *server;
3387 unsigned long timestamp;
3390 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3391 struct nfs_open_context *ctx,
3392 struct nfs4_lock_state *lsp,
3393 struct nfs_seqid *seqid)
3395 struct nfs4_unlockdata *p;
3396 struct inode *inode = lsp->ls_state->inode;
3398 p = kmalloc(sizeof(*p), GFP_KERNEL);
3401 p->arg.fh = NFS_FH(inode);
3403 p->arg.seqid = seqid;
3404 p->res.seqid = seqid;
3405 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3406 p->arg.stateid = &lsp->ls_stateid;
3408 atomic_inc(&lsp->ls_count);
3409 /* Ensure we don't close file until we're done freeing locks! */
3410 p->ctx = get_nfs_open_context(ctx);
3411 memcpy(&p->fl, fl, sizeof(p->fl));
3412 p->server = NFS_SERVER(inode);
3416 static void nfs4_locku_release_calldata(void *data)
3418 struct nfs4_unlockdata *calldata = data;
3419 nfs_free_seqid(calldata->arg.seqid);
3420 nfs4_put_lock_state(calldata->lsp);
3421 put_nfs_open_context(calldata->ctx);
3425 static void nfs4_locku_done(struct rpc_task *task, void *data)
3427 struct nfs4_unlockdata *calldata = data;
3429 if (RPC_ASSASSINATED(task))
3431 switch (task->tk_status) {
3433 memcpy(calldata->lsp->ls_stateid.data,
3434 calldata->res.stateid.data,
3435 sizeof(calldata->lsp->ls_stateid.data));
3436 renew_lease(calldata->server, calldata->timestamp);
3438 case -NFS4ERR_BAD_STATEID:
3439 case -NFS4ERR_OLD_STATEID:
3440 case -NFS4ERR_STALE_STATEID:
3441 case -NFS4ERR_EXPIRED:
3444 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3445 rpc_restart_call(task);
3449 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3451 struct nfs4_unlockdata *calldata = data;
3453 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3455 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3456 /* Note: exit _without_ running nfs4_locku_done */
3457 task->tk_action = NULL;
3460 calldata->timestamp = jiffies;
3461 rpc_call_start(task);
3464 static const struct rpc_call_ops nfs4_locku_ops = {
3465 .rpc_call_prepare = nfs4_locku_prepare,
3466 .rpc_call_done = nfs4_locku_done,
3467 .rpc_release = nfs4_locku_release_calldata,
3470 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3471 struct nfs_open_context *ctx,
3472 struct nfs4_lock_state *lsp,
3473 struct nfs_seqid *seqid)
3475 struct nfs4_unlockdata *data;
3476 struct rpc_message msg = {
3477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3478 .rpc_cred = ctx->cred,
3480 struct rpc_task_setup task_setup_data = {
3481 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3482 .rpc_message = &msg,
3483 .callback_ops = &nfs4_locku_ops,
3484 .workqueue = nfsiod_workqueue,
3485 .flags = RPC_TASK_ASYNC,
3488 /* Ensure this is an unlock - when canceling a lock, the
3489 * canceled lock is passed in, and it won't be an unlock.
3491 fl->fl_type = F_UNLCK;
3493 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3495 nfs_free_seqid(seqid);
3496 return ERR_PTR(-ENOMEM);
3499 msg.rpc_argp = &data->arg,
3500 msg.rpc_resp = &data->res,
3501 task_setup_data.callback_data = data;
3502 return rpc_run_task(&task_setup_data);
3505 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3507 struct nfs_inode *nfsi = NFS_I(state->inode);
3508 struct nfs_seqid *seqid;
3509 struct nfs4_lock_state *lsp;
3510 struct rpc_task *task;
3512 unsigned char fl_flags = request->fl_flags;
3514 status = nfs4_set_lock_state(state, request);
3515 /* Unlock _before_ we do the RPC call */
3516 request->fl_flags |= FL_EXISTS;
3517 down_read(&nfsi->rwsem);
3518 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3519 up_read(&nfsi->rwsem);
3522 up_read(&nfsi->rwsem);
3525 /* Is this a delegated lock? */
3526 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3528 lsp = request->fl_u.nfs4_fl.owner;
3529 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3533 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3534 status = PTR_ERR(task);
3537 status = nfs4_wait_for_completion_rpc_task(task);
3540 request->fl_flags = fl_flags;
3544 struct nfs4_lockdata {
3545 struct nfs_lock_args arg;
3546 struct nfs_lock_res res;
3547 struct nfs4_lock_state *lsp;
3548 struct nfs_open_context *ctx;
3549 struct file_lock fl;
3550 unsigned long timestamp;
3555 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3556 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3558 struct nfs4_lockdata *p;
3559 struct inode *inode = lsp->ls_state->inode;
3560 struct nfs_server *server = NFS_SERVER(inode);
3562 p = kzalloc(sizeof(*p), GFP_KERNEL);
3566 p->arg.fh = NFS_FH(inode);
3568 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3569 if (p->arg.open_seqid == NULL)
3571 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3572 if (p->arg.lock_seqid == NULL)
3573 goto out_free_seqid;
3574 p->arg.lock_stateid = &lsp->ls_stateid;
3575 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3576 p->arg.lock_owner.id = lsp->ls_id.id;
3577 p->res.lock_seqid = p->arg.lock_seqid;
3578 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3580 atomic_inc(&lsp->ls_count);
3581 p->ctx = get_nfs_open_context(ctx);
3582 memcpy(&p->fl, fl, sizeof(p->fl));
3585 nfs_free_seqid(p->arg.open_seqid);
3591 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3593 struct nfs4_lockdata *data = calldata;
3594 struct nfs4_state *state = data->lsp->ls_state;
3596 dprintk("%s: begin!\n", __func__);
3597 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3599 /* Do we need to do an open_to_lock_owner? */
3600 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3601 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3603 data->arg.open_stateid = &state->stateid;
3604 data->arg.new_lock_owner = 1;
3605 data->res.open_seqid = data->arg.open_seqid;
3607 data->arg.new_lock_owner = 0;
3608 data->timestamp = jiffies;
3609 rpc_call_start(task);
3610 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3613 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3615 struct nfs4_lockdata *data = calldata;
3617 dprintk("%s: begin!\n", __func__);
3619 data->rpc_status = task->tk_status;
3620 if (RPC_ASSASSINATED(task))
3622 if (data->arg.new_lock_owner != 0) {
3623 if (data->rpc_status == 0)
3624 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3628 if (data->rpc_status == 0) {
3629 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3630 sizeof(data->lsp->ls_stateid.data));
3631 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3632 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3635 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3638 static void nfs4_lock_release(void *calldata)
3640 struct nfs4_lockdata *data = calldata;
3642 dprintk("%s: begin!\n", __func__);
3643 nfs_free_seqid(data->arg.open_seqid);
3644 if (data->cancelled != 0) {
3645 struct rpc_task *task;
3646 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3647 data->arg.lock_seqid);
3650 dprintk("%s: cancelling lock!\n", __func__);
3652 nfs_free_seqid(data->arg.lock_seqid);
3653 nfs4_put_lock_state(data->lsp);
3654 put_nfs_open_context(data->ctx);
3656 dprintk("%s: done!\n", __func__);
3659 static const struct rpc_call_ops nfs4_lock_ops = {
3660 .rpc_call_prepare = nfs4_lock_prepare,
3661 .rpc_call_done = nfs4_lock_done,
3662 .rpc_release = nfs4_lock_release,
3665 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3667 struct nfs4_lockdata *data;
3668 struct rpc_task *task;
3669 struct rpc_message msg = {
3670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3671 .rpc_cred = state->owner->so_cred,
3673 struct rpc_task_setup task_setup_data = {
3674 .rpc_client = NFS_CLIENT(state->inode),
3675 .rpc_message = &msg,
3676 .callback_ops = &nfs4_lock_ops,
3677 .workqueue = nfsiod_workqueue,
3678 .flags = RPC_TASK_ASYNC,
3682 dprintk("%s: begin!\n", __func__);
3683 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3684 fl->fl_u.nfs4_fl.owner);
3688 data->arg.block = 1;
3690 data->arg.reclaim = 1;
3691 msg.rpc_argp = &data->arg,
3692 msg.rpc_resp = &data->res,
3693 task_setup_data.callback_data = data;
3694 task = rpc_run_task(&task_setup_data);
3696 return PTR_ERR(task);
3697 ret = nfs4_wait_for_completion_rpc_task(task);
3699 ret = data->rpc_status;
3700 if (ret == -NFS4ERR_DENIED)
3703 data->cancelled = 1;
3705 dprintk("%s: done, ret = %d!\n", __func__, ret);
3709 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3711 struct nfs_server *server = NFS_SERVER(state->inode);
3712 struct nfs4_exception exception = { };
3716 /* Cache the lock if possible... */
3717 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3719 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3720 if (err != -NFS4ERR_DELAY)
3722 nfs4_handle_exception(server, err, &exception);
3723 } while (exception.retry);
3727 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3729 struct nfs_server *server = NFS_SERVER(state->inode);
3730 struct nfs4_exception exception = { };
3733 err = nfs4_set_lock_state(state, request);
3737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3739 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3740 if (err != -NFS4ERR_DELAY)
3742 nfs4_handle_exception(server, err, &exception);
3743 } while (exception.retry);
3747 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3749 struct nfs_inode *nfsi = NFS_I(state->inode);
3750 unsigned char fl_flags = request->fl_flags;
3753 /* Is this a delegated open? */
3754 status = nfs4_set_lock_state(state, request);
3757 request->fl_flags |= FL_ACCESS;
3758 status = do_vfs_lock(request->fl_file, request);
3761 down_read(&nfsi->rwsem);
3762 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3763 /* Yes: cache locks! */
3764 /* ...but avoid races with delegation recall... */
3765 request->fl_flags = fl_flags & ~FL_SLEEP;
3766 status = do_vfs_lock(request->fl_file, request);
3769 status = _nfs4_do_setlk(state, cmd, request, 0);
3772 /* Note: we always want to sleep here! */
3773 request->fl_flags = fl_flags | FL_SLEEP;
3774 if (do_vfs_lock(request->fl_file, request) < 0)
3775 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3777 up_read(&nfsi->rwsem);
3779 request->fl_flags = fl_flags;
3783 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3785 struct nfs4_exception exception = { };
3789 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3790 _nfs4_proc_setlk(state, cmd, request),
3792 } while (exception.retry);
3797 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3799 struct nfs_open_context *ctx;
3800 struct nfs4_state *state;
3801 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3804 /* verify open state */
3805 ctx = nfs_file_open_context(filp);
3808 if (request->fl_start < 0 || request->fl_end < 0)
3812 return nfs4_proc_getlk(state, F_GETLK, request);
3814 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3817 if (request->fl_type == F_UNLCK)
3818 return nfs4_proc_unlck(state, cmd, request);
3821 status = nfs4_proc_setlk(state, cmd, request);
3822 if ((status != -EAGAIN) || IS_SETLK(cmd))
3824 timeout = nfs4_set_lock_task_retry(timeout);
3825 status = -ERESTARTSYS;
3828 } while(status < 0);
3832 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3834 struct nfs_server *server = NFS_SERVER(state->inode);
3835 struct nfs4_exception exception = { };
3838 err = nfs4_set_lock_state(state, fl);
3842 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3843 if (err != -NFS4ERR_DELAY)
3845 err = nfs4_handle_exception(server, err, &exception);
3846 } while (exception.retry);
3851 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3853 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3854 size_t buflen, int flags)
3856 struct inode *inode = dentry->d_inode;
3858 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3861 return nfs4_proc_set_acl(inode, buf, buflen);
3864 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3865 * and that's what we'll do for e.g. user attributes that haven't been set.
3866 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3867 * attributes in kernel-managed attribute namespaces. */
3868 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3871 struct inode *inode = dentry->d_inode;
3873 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3876 return nfs4_proc_get_acl(inode, buf, buflen);
3879 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3881 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3883 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3885 if (buf && buflen < len)
3888 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3892 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3894 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3895 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3896 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3899 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3900 NFS_ATTR_FATTR_NLINK;
3901 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3905 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3906 struct nfs4_fs_locations *fs_locations, struct page *page)
3908 struct nfs_server *server = NFS_SERVER(dir);
3910 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3911 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3913 struct nfs4_fs_locations_arg args = {
3914 .dir_fh = NFS_FH(dir),
3919 struct nfs4_fs_locations_res res = {
3920 .fs_locations = fs_locations,
3922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3929 dprintk("%s: start\n", __func__);
3930 nfs_fattr_init(&fs_locations->fattr);
3931 fs_locations->server = server;
3932 fs_locations->nlocations = 0;
3933 status = nfs4_call_sync(server, &msg, &args, &res, 0);
3934 nfs_fixup_referral_attributes(&fs_locations->fattr);
3935 dprintk("%s: returned status = %d\n", __func__, status);
3939 #ifdef CONFIG_NFS_V4_1
3940 /* Destroy the slot table */
3941 static void nfs4_destroy_slot_table(struct nfs4_session *session)
3943 if (session->fc_slot_table.slots == NULL)
3945 kfree(session->fc_slot_table.slots);
3946 session->fc_slot_table.slots = NULL;
3950 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3952 struct nfs4_session *session;
3953 struct nfs4_slot_table *tbl;
3955 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3958 tbl = &session->fc_slot_table;
3959 spin_lock_init(&tbl->slot_tbl_lock);
3960 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3965 void nfs4_destroy_session(struct nfs4_session *session)
3967 nfs4_destroy_slot_table(session);
3971 #endif /* CONFIG_NFS_V4_1 */
3973 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3974 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
3975 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
3976 .recover_open = nfs4_open_reclaim,
3977 .recover_lock = nfs4_lock_reclaim,
3980 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
3981 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
3982 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
3983 .recover_open = nfs4_open_expired,
3984 .recover_lock = nfs4_lock_expired,
3987 static const struct inode_operations nfs4_file_inode_operations = {
3988 .permission = nfs_permission,
3989 .getattr = nfs_getattr,
3990 .setattr = nfs_setattr,
3991 .getxattr = nfs4_getxattr,
3992 .setxattr = nfs4_setxattr,
3993 .listxattr = nfs4_listxattr,
3996 const struct nfs_rpc_ops nfs_v4_clientops = {
3997 .version = 4, /* protocol version */
3998 .dentry_ops = &nfs4_dentry_operations,
3999 .dir_inode_ops = &nfs4_dir_inode_operations,
4000 .file_inode_ops = &nfs4_file_inode_operations,
4001 .getroot = nfs4_proc_get_root,
4002 .getattr = nfs4_proc_getattr,
4003 .setattr = nfs4_proc_setattr,
4004 .lookupfh = nfs4_proc_lookupfh,
4005 .lookup = nfs4_proc_lookup,
4006 .access = nfs4_proc_access,
4007 .readlink = nfs4_proc_readlink,
4008 .create = nfs4_proc_create,
4009 .remove = nfs4_proc_remove,
4010 .unlink_setup = nfs4_proc_unlink_setup,
4011 .unlink_done = nfs4_proc_unlink_done,
4012 .rename = nfs4_proc_rename,
4013 .link = nfs4_proc_link,
4014 .symlink = nfs4_proc_symlink,
4015 .mkdir = nfs4_proc_mkdir,
4016 .rmdir = nfs4_proc_remove,
4017 .readdir = nfs4_proc_readdir,
4018 .mknod = nfs4_proc_mknod,
4019 .statfs = nfs4_proc_statfs,
4020 .fsinfo = nfs4_proc_fsinfo,
4021 .pathconf = nfs4_proc_pathconf,
4022 .set_capabilities = nfs4_server_capabilities,
4023 .decode_dirent = nfs4_decode_dirent,
4024 .read_setup = nfs4_proc_read_setup,
4025 .read_done = nfs4_read_done,
4026 .write_setup = nfs4_proc_write_setup,
4027 .write_done = nfs4_write_done,
4028 .commit_setup = nfs4_proc_commit_setup,
4029 .commit_done = nfs4_commit_done,
4030 .lock = nfs4_proc_lock,
4031 .clear_acl_cache = nfs4_zap_acl_attr,
4032 .close_context = nfs4_close_context,