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)
276 int _nfs4_call_sync_session(struct nfs_server *server,
277 struct rpc_message *msg,
278 struct nfs4_sequence_args *args,
279 struct nfs4_sequence_res *res,
282 /* in preparation for setting up the sequence op */
283 return rpc_call_sync(server->client, msg, 0);
286 #endif /* CONFIG_NFS_V4_1 */
288 int _nfs4_call_sync(struct nfs_server *server,
289 struct rpc_message *msg,
290 struct nfs4_sequence_args *args,
291 struct nfs4_sequence_res *res,
294 return rpc_call_sync(server->client, msg, 0);
297 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
298 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
299 &(res)->seq_res, (cache_reply))
301 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
303 struct nfs_inode *nfsi = NFS_I(dir);
305 spin_lock(&dir->i_lock);
306 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
307 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
308 nfs_force_lookup_revalidate(dir);
309 nfsi->change_attr = cinfo->after;
310 spin_unlock(&dir->i_lock);
313 struct nfs4_opendata {
315 struct nfs_openargs o_arg;
316 struct nfs_openres o_res;
317 struct nfs_open_confirmargs c_arg;
318 struct nfs_open_confirmres c_res;
319 struct nfs_fattr f_attr;
320 struct nfs_fattr dir_attr;
323 struct nfs4_state_owner *owner;
324 struct nfs4_state *state;
326 unsigned long timestamp;
327 unsigned int rpc_done : 1;
333 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
335 p->o_res.f_attr = &p->f_attr;
336 p->o_res.dir_attr = &p->dir_attr;
337 p->o_res.seqid = p->o_arg.seqid;
338 p->c_res.seqid = p->c_arg.seqid;
339 p->o_res.server = p->o_arg.server;
340 nfs_fattr_init(&p->f_attr);
341 nfs_fattr_init(&p->dir_attr);
344 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
345 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
346 const struct iattr *attrs)
348 struct dentry *parent = dget_parent(path->dentry);
349 struct inode *dir = parent->d_inode;
350 struct nfs_server *server = NFS_SERVER(dir);
351 struct nfs4_opendata *p;
353 p = kzalloc(sizeof(*p), GFP_KERNEL);
356 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
357 if (p->o_arg.seqid == NULL)
359 p->path.mnt = mntget(path->mnt);
360 p->path.dentry = dget(path->dentry);
363 atomic_inc(&sp->so_count);
364 p->o_arg.fh = NFS_FH(dir);
365 p->o_arg.open_flags = flags;
366 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
367 p->o_arg.clientid = server->nfs_client->cl_clientid;
368 p->o_arg.id = sp->so_owner_id.id;
369 p->o_arg.name = &p->path.dentry->d_name;
370 p->o_arg.server = server;
371 p->o_arg.bitmask = server->attr_bitmask;
372 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
373 if (flags & O_EXCL) {
374 u32 *s = (u32 *) p->o_arg.u.verifier.data;
377 } else if (flags & O_CREAT) {
378 p->o_arg.u.attrs = &p->attrs;
379 memcpy(&p->attrs, attrs, sizeof(p->attrs));
381 p->c_arg.fh = &p->o_res.fh;
382 p->c_arg.stateid = &p->o_res.stateid;
383 p->c_arg.seqid = p->o_arg.seqid;
384 nfs4_init_opendata_res(p);
394 static void nfs4_opendata_free(struct kref *kref)
396 struct nfs4_opendata *p = container_of(kref,
397 struct nfs4_opendata, kref);
399 nfs_free_seqid(p->o_arg.seqid);
400 if (p->state != NULL)
401 nfs4_put_open_state(p->state);
402 nfs4_put_state_owner(p->owner);
408 static void nfs4_opendata_put(struct nfs4_opendata *p)
411 kref_put(&p->kref, nfs4_opendata_free);
414 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
418 ret = rpc_wait_for_completion_task(task);
422 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
426 if (open_mode & O_EXCL)
428 switch (mode & (FMODE_READ|FMODE_WRITE)) {
430 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
433 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
435 case FMODE_READ|FMODE_WRITE:
436 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
442 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
444 if ((delegation->type & fmode) != fmode)
446 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
448 nfs_mark_delegation_referenced(delegation);
452 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
461 case FMODE_READ|FMODE_WRITE:
464 nfs4_state_set_mode_locked(state, state->state | fmode);
467 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
469 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
470 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
471 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
474 set_bit(NFS_O_RDONLY_STATE, &state->flags);
477 set_bit(NFS_O_WRONLY_STATE, &state->flags);
479 case FMODE_READ|FMODE_WRITE:
480 set_bit(NFS_O_RDWR_STATE, &state->flags);
484 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
486 write_seqlock(&state->seqlock);
487 nfs_set_open_stateid_locked(state, stateid, fmode);
488 write_sequnlock(&state->seqlock);
491 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
494 * Protect the call to nfs4_state_set_mode_locked and
495 * serialise the stateid update
497 write_seqlock(&state->seqlock);
498 if (deleg_stateid != NULL) {
499 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
500 set_bit(NFS_DELEGATED_STATE, &state->flags);
502 if (open_stateid != NULL)
503 nfs_set_open_stateid_locked(state, open_stateid, fmode);
504 write_sequnlock(&state->seqlock);
505 spin_lock(&state->owner->so_lock);
506 update_open_stateflags(state, fmode);
507 spin_unlock(&state->owner->so_lock);
510 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
512 struct nfs_inode *nfsi = NFS_I(state->inode);
513 struct nfs_delegation *deleg_cur;
516 fmode &= (FMODE_READ|FMODE_WRITE);
519 deleg_cur = rcu_dereference(nfsi->delegation);
520 if (deleg_cur == NULL)
523 spin_lock(&deleg_cur->lock);
524 if (nfsi->delegation != deleg_cur ||
525 (deleg_cur->type & fmode) != fmode)
526 goto no_delegation_unlock;
528 if (delegation == NULL)
529 delegation = &deleg_cur->stateid;
530 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
531 goto no_delegation_unlock;
533 nfs_mark_delegation_referenced(deleg_cur);
534 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
536 no_delegation_unlock:
537 spin_unlock(&deleg_cur->lock);
541 if (!ret && open_stateid != NULL) {
542 __update_open_stateid(state, open_stateid, NULL, fmode);
550 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
552 struct nfs_delegation *delegation;
555 delegation = rcu_dereference(NFS_I(inode)->delegation);
556 if (delegation == NULL || (delegation->type & fmode) == fmode) {
561 nfs_inode_return_delegation(inode);
564 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
566 struct nfs4_state *state = opendata->state;
567 struct nfs_inode *nfsi = NFS_I(state->inode);
568 struct nfs_delegation *delegation;
569 int open_mode = opendata->o_arg.open_flags & O_EXCL;
570 fmode_t fmode = opendata->o_arg.fmode;
571 nfs4_stateid stateid;
575 if (can_open_cached(state, fmode, open_mode)) {
576 spin_lock(&state->owner->so_lock);
577 if (can_open_cached(state, fmode, open_mode)) {
578 update_open_stateflags(state, fmode);
579 spin_unlock(&state->owner->so_lock);
580 goto out_return_state;
582 spin_unlock(&state->owner->so_lock);
585 delegation = rcu_dereference(nfsi->delegation);
586 if (delegation == NULL ||
587 !can_open_delegated(delegation, fmode)) {
591 /* Save the delegation */
592 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
594 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
599 /* Try to update the stateid using the delegation */
600 if (update_open_stateid(state, NULL, &stateid, fmode))
601 goto out_return_state;
606 atomic_inc(&state->count);
610 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
613 struct nfs4_state *state = NULL;
614 struct nfs_delegation *delegation;
617 if (!data->rpc_done) {
618 state = nfs4_try_open_cached(data);
623 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
625 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
626 ret = PTR_ERR(inode);
630 state = nfs4_get_open_state(inode, data->owner);
633 if (data->o_res.delegation_type != 0) {
634 int delegation_flags = 0;
637 delegation = rcu_dereference(NFS_I(inode)->delegation);
639 delegation_flags = delegation->flags;
641 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
642 nfs_inode_set_delegation(state->inode,
643 data->owner->so_cred,
646 nfs_inode_reclaim_delegation(state->inode,
647 data->owner->so_cred,
651 update_open_stateid(state, &data->o_res.stateid, NULL,
662 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
664 struct nfs_inode *nfsi = NFS_I(state->inode);
665 struct nfs_open_context *ctx;
667 spin_lock(&state->inode->i_lock);
668 list_for_each_entry(ctx, &nfsi->open_files, list) {
669 if (ctx->state != state)
671 get_nfs_open_context(ctx);
672 spin_unlock(&state->inode->i_lock);
675 spin_unlock(&state->inode->i_lock);
676 return ERR_PTR(-ENOENT);
679 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
681 struct nfs4_opendata *opendata;
683 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
684 if (opendata == NULL)
685 return ERR_PTR(-ENOMEM);
686 opendata->state = state;
687 atomic_inc(&state->count);
691 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
693 struct nfs4_state *newstate;
696 opendata->o_arg.open_flags = 0;
697 opendata->o_arg.fmode = fmode;
698 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
699 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
700 nfs4_init_opendata_res(opendata);
701 ret = _nfs4_proc_open(opendata);
704 newstate = nfs4_opendata_to_nfs4_state(opendata);
705 if (IS_ERR(newstate))
706 return PTR_ERR(newstate);
707 nfs4_close_state(&opendata->path, newstate, fmode);
712 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
714 struct nfs4_state *newstate;
717 /* memory barrier prior to reading state->n_* */
718 clear_bit(NFS_DELEGATED_STATE, &state->flags);
720 if (state->n_rdwr != 0) {
721 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
724 if (newstate != state)
727 if (state->n_wronly != 0) {
728 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
731 if (newstate != state)
734 if (state->n_rdonly != 0) {
735 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
738 if (newstate != state)
742 * We may have performed cached opens for all three recoveries.
743 * Check if we need to update the current stateid.
745 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
746 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
747 write_seqlock(&state->seqlock);
748 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
749 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
750 write_sequnlock(&state->seqlock);
757 * reclaim state on the server after a reboot.
759 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
761 struct nfs_delegation *delegation;
762 struct nfs4_opendata *opendata;
763 fmode_t delegation_type = 0;
766 opendata = nfs4_open_recoverdata_alloc(ctx, state);
767 if (IS_ERR(opendata))
768 return PTR_ERR(opendata);
769 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
770 opendata->o_arg.fh = NFS_FH(state->inode);
772 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
773 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
774 delegation_type = delegation->type;
776 opendata->o_arg.u.delegation_type = delegation_type;
777 status = nfs4_open_recover(opendata, state);
778 nfs4_opendata_put(opendata);
782 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
784 struct nfs_server *server = NFS_SERVER(state->inode);
785 struct nfs4_exception exception = { };
788 err = _nfs4_do_open_reclaim(ctx, state);
789 if (err != -NFS4ERR_DELAY)
791 nfs4_handle_exception(server, err, &exception);
792 } while (exception.retry);
796 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
798 struct nfs_open_context *ctx;
801 ctx = nfs4_state_find_open_context(state);
804 ret = nfs4_do_open_reclaim(ctx, state);
805 put_nfs_open_context(ctx);
809 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
811 struct nfs4_opendata *opendata;
814 opendata = nfs4_open_recoverdata_alloc(ctx, state);
815 if (IS_ERR(opendata))
816 return PTR_ERR(opendata);
817 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
818 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
819 sizeof(opendata->o_arg.u.delegation.data));
820 ret = nfs4_open_recover(opendata, state);
821 nfs4_opendata_put(opendata);
825 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
827 struct nfs4_exception exception = { };
828 struct nfs_server *server = NFS_SERVER(state->inode);
831 err = _nfs4_open_delegation_recall(ctx, state, stateid);
835 case -NFS4ERR_STALE_CLIENTID:
836 case -NFS4ERR_STALE_STATEID:
837 case -NFS4ERR_EXPIRED:
838 /* Don't recall a delegation if it was lost */
839 nfs4_schedule_state_recovery(server->nfs_client);
842 err = nfs4_handle_exception(server, err, &exception);
843 } while (exception.retry);
847 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
849 struct nfs4_opendata *data = calldata;
851 data->rpc_status = task->tk_status;
852 if (RPC_ASSASSINATED(task))
854 if (data->rpc_status == 0) {
855 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
856 sizeof(data->o_res.stateid.data));
857 nfs_confirm_seqid(&data->owner->so_seqid, 0);
858 renew_lease(data->o_res.server, data->timestamp);
863 static void nfs4_open_confirm_release(void *calldata)
865 struct nfs4_opendata *data = calldata;
866 struct nfs4_state *state = NULL;
868 /* If this request hasn't been cancelled, do nothing */
869 if (data->cancelled == 0)
871 /* In case of error, no cleanup! */
874 state = nfs4_opendata_to_nfs4_state(data);
876 nfs4_close_state(&data->path, state, data->o_arg.fmode);
878 nfs4_opendata_put(data);
881 static const struct rpc_call_ops nfs4_open_confirm_ops = {
882 .rpc_call_done = nfs4_open_confirm_done,
883 .rpc_release = nfs4_open_confirm_release,
887 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
889 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
891 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
892 struct rpc_task *task;
893 struct rpc_message msg = {
894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
895 .rpc_argp = &data->c_arg,
896 .rpc_resp = &data->c_res,
897 .rpc_cred = data->owner->so_cred,
899 struct rpc_task_setup task_setup_data = {
900 .rpc_client = server->client,
902 .callback_ops = &nfs4_open_confirm_ops,
903 .callback_data = data,
904 .workqueue = nfsiod_workqueue,
905 .flags = RPC_TASK_ASYNC,
909 kref_get(&data->kref);
911 data->rpc_status = 0;
912 data->timestamp = jiffies;
913 task = rpc_run_task(&task_setup_data);
915 return PTR_ERR(task);
916 status = nfs4_wait_for_completion_rpc_task(task);
921 status = data->rpc_status;
926 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
928 struct nfs4_opendata *data = calldata;
929 struct nfs4_state_owner *sp = data->owner;
931 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
934 * Check if we still need to send an OPEN call, or if we can use
935 * a delegation instead.
937 if (data->state != NULL) {
938 struct nfs_delegation *delegation;
940 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
943 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
944 if (delegation != NULL &&
945 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
951 /* Update sequence id. */
952 data->o_arg.id = sp->so_owner_id.id;
953 data->o_arg.clientid = sp->so_client->cl_clientid;
954 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
955 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
956 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
958 data->timestamp = jiffies;
959 rpc_call_start(task);
962 task->tk_action = NULL;
966 static void nfs4_open_done(struct rpc_task *task, void *calldata)
968 struct nfs4_opendata *data = calldata;
970 data->rpc_status = task->tk_status;
971 if (RPC_ASSASSINATED(task))
973 if (task->tk_status == 0) {
974 switch (data->o_res.f_attr->mode & S_IFMT) {
978 data->rpc_status = -ELOOP;
981 data->rpc_status = -EISDIR;
984 data->rpc_status = -ENOTDIR;
986 renew_lease(data->o_res.server, data->timestamp);
987 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
988 nfs_confirm_seqid(&data->owner->so_seqid, 0);
993 static void nfs4_open_release(void *calldata)
995 struct nfs4_opendata *data = calldata;
996 struct nfs4_state *state = NULL;
998 /* If this request hasn't been cancelled, do nothing */
999 if (data->cancelled == 0)
1001 /* In case of error, no cleanup! */
1002 if (data->rpc_status != 0 || !data->rpc_done)
1004 /* In case we need an open_confirm, no cleanup! */
1005 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1007 state = nfs4_opendata_to_nfs4_state(data);
1009 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1011 nfs4_opendata_put(data);
1014 static const struct rpc_call_ops nfs4_open_ops = {
1015 .rpc_call_prepare = nfs4_open_prepare,
1016 .rpc_call_done = nfs4_open_done,
1017 .rpc_release = nfs4_open_release,
1021 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1023 static int _nfs4_proc_open(struct nfs4_opendata *data)
1025 struct inode *dir = data->dir->d_inode;
1026 struct nfs_server *server = NFS_SERVER(dir);
1027 struct nfs_openargs *o_arg = &data->o_arg;
1028 struct nfs_openres *o_res = &data->o_res;
1029 struct rpc_task *task;
1030 struct rpc_message msg = {
1031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1034 .rpc_cred = data->owner->so_cred,
1036 struct rpc_task_setup task_setup_data = {
1037 .rpc_client = server->client,
1038 .rpc_message = &msg,
1039 .callback_ops = &nfs4_open_ops,
1040 .callback_data = data,
1041 .workqueue = nfsiod_workqueue,
1042 .flags = RPC_TASK_ASYNC,
1046 kref_get(&data->kref);
1048 data->rpc_status = 0;
1049 data->cancelled = 0;
1050 task = rpc_run_task(&task_setup_data);
1052 return PTR_ERR(task);
1053 status = nfs4_wait_for_completion_rpc_task(task);
1055 data->cancelled = 1;
1058 status = data->rpc_status;
1060 if (status != 0 || !data->rpc_done)
1063 if (o_res->fh.size == 0)
1064 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1066 if (o_arg->open_flags & O_CREAT) {
1067 update_changeattr(dir, &o_res->cinfo);
1068 nfs_post_op_update_inode(dir, o_res->dir_attr);
1070 nfs_refresh_inode(dir, o_res->dir_attr);
1071 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1072 status = _nfs4_proc_open_confirm(data);
1076 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1077 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1081 static int nfs4_recover_expired_lease(struct nfs_server *server)
1083 struct nfs_client *clp = server->nfs_client;
1087 ret = nfs4_wait_clnt_recover(clp);
1090 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1091 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1093 nfs4_schedule_state_recovery(clp);
1100 * reclaim state on the server after a network partition.
1101 * Assumes caller holds the appropriate lock
1103 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1105 struct nfs4_opendata *opendata;
1108 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1109 if (IS_ERR(opendata))
1110 return PTR_ERR(opendata);
1111 ret = nfs4_open_recover(opendata, state);
1113 d_drop(ctx->path.dentry);
1114 nfs4_opendata_put(opendata);
1118 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1120 struct nfs_server *server = NFS_SERVER(state->inode);
1121 struct nfs4_exception exception = { };
1125 err = _nfs4_open_expired(ctx, state);
1126 if (err != -NFS4ERR_DELAY)
1128 nfs4_handle_exception(server, err, &exception);
1129 } while (exception.retry);
1133 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1135 struct nfs_open_context *ctx;
1138 ctx = nfs4_state_find_open_context(state);
1140 return PTR_ERR(ctx);
1141 ret = nfs4_do_open_expired(ctx, state);
1142 put_nfs_open_context(ctx);
1147 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1148 * fields corresponding to attributes that were used to store the verifier.
1149 * Make sure we clobber those fields in the later setattr call
1151 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1153 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1154 !(sattr->ia_valid & ATTR_ATIME_SET))
1155 sattr->ia_valid |= ATTR_ATIME;
1157 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1158 !(sattr->ia_valid & ATTR_MTIME_SET))
1159 sattr->ia_valid |= ATTR_MTIME;
1163 * Returns a referenced nfs4_state
1165 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)
1167 struct nfs4_state_owner *sp;
1168 struct nfs4_state *state = NULL;
1169 struct nfs_server *server = NFS_SERVER(dir);
1170 struct nfs4_opendata *opendata;
1173 /* Protect against reboot recovery conflicts */
1175 if (!(sp = nfs4_get_state_owner(server, cred))) {
1176 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1179 status = nfs4_recover_expired_lease(server);
1181 goto err_put_state_owner;
1182 if (path->dentry->d_inode != NULL)
1183 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1185 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1186 if (opendata == NULL)
1187 goto err_put_state_owner;
1189 if (path->dentry->d_inode != NULL)
1190 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1192 status = _nfs4_proc_open(opendata);
1194 goto err_opendata_put;
1196 if (opendata->o_arg.open_flags & O_EXCL)
1197 nfs4_exclusive_attrset(opendata, sattr);
1199 state = nfs4_opendata_to_nfs4_state(opendata);
1200 status = PTR_ERR(state);
1202 goto err_opendata_put;
1203 nfs4_opendata_put(opendata);
1204 nfs4_put_state_owner(sp);
1208 nfs4_opendata_put(opendata);
1209 err_put_state_owner:
1210 nfs4_put_state_owner(sp);
1217 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)
1219 struct nfs4_exception exception = { };
1220 struct nfs4_state *res;
1224 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1227 /* NOTE: BAD_SEQID means the server and client disagree about the
1228 * book-keeping w.r.t. state-changing operations
1229 * (OPEN/CLOSE/LOCK/LOCKU...)
1230 * It is actually a sign of a bug on the client or on the server.
1232 * If we receive a BAD_SEQID error in the particular case of
1233 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1234 * have unhashed the old state_owner for us, and that we can
1235 * therefore safely retry using a new one. We should still warn
1236 * the user though...
1238 if (status == -NFS4ERR_BAD_SEQID) {
1239 printk(KERN_WARNING "NFS: v4 server %s "
1240 " returned a bad sequence-id error!\n",
1241 NFS_SERVER(dir)->nfs_client->cl_hostname);
1242 exception.retry = 1;
1246 * BAD_STATEID on OPEN means that the server cancelled our
1247 * state before it received the OPEN_CONFIRM.
1248 * Recover by retrying the request as per the discussion
1249 * on Page 181 of RFC3530.
1251 if (status == -NFS4ERR_BAD_STATEID) {
1252 exception.retry = 1;
1255 if (status == -EAGAIN) {
1256 /* We must have found a delegation */
1257 exception.retry = 1;
1260 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1261 status, &exception));
1262 } while (exception.retry);
1266 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1267 struct nfs_fattr *fattr, struct iattr *sattr,
1268 struct nfs4_state *state)
1270 struct nfs_server *server = NFS_SERVER(inode);
1271 struct nfs_setattrargs arg = {
1272 .fh = NFS_FH(inode),
1275 .bitmask = server->attr_bitmask,
1277 struct nfs_setattrres res = {
1281 struct rpc_message msg = {
1282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1287 unsigned long timestamp = jiffies;
1290 nfs_fattr_init(fattr);
1292 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1293 /* Use that stateid */
1294 } else if (state != NULL) {
1295 nfs4_copy_stateid(&arg.stateid, state, current->files);
1297 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1299 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1300 if (status == 0 && state != NULL)
1301 renew_lease(server, timestamp);
1305 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1306 struct nfs_fattr *fattr, struct iattr *sattr,
1307 struct nfs4_state *state)
1309 struct nfs_server *server = NFS_SERVER(inode);
1310 struct nfs4_exception exception = { };
1313 err = nfs4_handle_exception(server,
1314 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1316 } while (exception.retry);
1320 struct nfs4_closedata {
1322 struct inode *inode;
1323 struct nfs4_state *state;
1324 struct nfs_closeargs arg;
1325 struct nfs_closeres res;
1326 struct nfs_fattr fattr;
1327 unsigned long timestamp;
1330 static void nfs4_free_closedata(void *data)
1332 struct nfs4_closedata *calldata = data;
1333 struct nfs4_state_owner *sp = calldata->state->owner;
1335 nfs4_put_open_state(calldata->state);
1336 nfs_free_seqid(calldata->arg.seqid);
1337 nfs4_put_state_owner(sp);
1338 path_put(&calldata->path);
1342 static void nfs4_close_done(struct rpc_task *task, void *data)
1344 struct nfs4_closedata *calldata = data;
1345 struct nfs4_state *state = calldata->state;
1346 struct nfs_server *server = NFS_SERVER(calldata->inode);
1348 if (RPC_ASSASSINATED(task))
1350 /* hmm. we are done with the inode, and in the process of freeing
1351 * the state_owner. we keep this around to process errors
1353 switch (task->tk_status) {
1355 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1356 renew_lease(server, calldata->timestamp);
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_OLD_STATEID:
1360 case -NFS4ERR_BAD_STATEID:
1361 case -NFS4ERR_EXPIRED:
1362 if (calldata->arg.fmode == 0)
1365 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1366 rpc_restart_call(task);
1370 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1373 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1375 struct nfs4_closedata *calldata = data;
1376 struct nfs4_state *state = calldata->state;
1377 int clear_rd, clear_wr, clear_rdwr;
1379 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1382 clear_rd = clear_wr = clear_rdwr = 0;
1383 spin_lock(&state->owner->so_lock);
1384 /* Calculate the change in open mode */
1385 if (state->n_rdwr == 0) {
1386 if (state->n_rdonly == 0) {
1387 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1388 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1390 if (state->n_wronly == 0) {
1391 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1392 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1395 spin_unlock(&state->owner->so_lock);
1396 if (!clear_rd && !clear_wr && !clear_rdwr) {
1397 /* Note: exit _without_ calling nfs4_close_done */
1398 task->tk_action = NULL;
1401 nfs_fattr_init(calldata->res.fattr);
1402 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1403 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1404 calldata->arg.fmode = FMODE_READ;
1405 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1406 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1407 calldata->arg.fmode = FMODE_WRITE;
1409 calldata->timestamp = jiffies;
1410 rpc_call_start(task);
1413 static const struct rpc_call_ops nfs4_close_ops = {
1414 .rpc_call_prepare = nfs4_close_prepare,
1415 .rpc_call_done = nfs4_close_done,
1416 .rpc_release = nfs4_free_closedata,
1420 * It is possible for data to be read/written from a mem-mapped file
1421 * after the sys_close call (which hits the vfs layer as a flush).
1422 * This means that we can't safely call nfsv4 close on a file until
1423 * the inode is cleared. This in turn means that we are not good
1424 * NFSv4 citizens - we do not indicate to the server to update the file's
1425 * share state even when we are done with one of the three share
1426 * stateid's in the inode.
1428 * NOTE: Caller must be holding the sp->so_owner semaphore!
1430 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1432 struct nfs_server *server = NFS_SERVER(state->inode);
1433 struct nfs4_closedata *calldata;
1434 struct nfs4_state_owner *sp = state->owner;
1435 struct rpc_task *task;
1436 struct rpc_message msg = {
1437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1438 .rpc_cred = state->owner->so_cred,
1440 struct rpc_task_setup task_setup_data = {
1441 .rpc_client = server->client,
1442 .rpc_message = &msg,
1443 .callback_ops = &nfs4_close_ops,
1444 .workqueue = nfsiod_workqueue,
1445 .flags = RPC_TASK_ASYNC,
1447 int status = -ENOMEM;
1449 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1450 if (calldata == NULL)
1452 calldata->inode = state->inode;
1453 calldata->state = state;
1454 calldata->arg.fh = NFS_FH(state->inode);
1455 calldata->arg.stateid = &state->open_stateid;
1456 /* Serialization for the sequence id */
1457 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1458 if (calldata->arg.seqid == NULL)
1459 goto out_free_calldata;
1460 calldata->arg.fmode = 0;
1461 calldata->arg.bitmask = server->cache_consistency_bitmask;
1462 calldata->res.fattr = &calldata->fattr;
1463 calldata->res.seqid = calldata->arg.seqid;
1464 calldata->res.server = server;
1465 calldata->path.mnt = mntget(path->mnt);
1466 calldata->path.dentry = dget(path->dentry);
1468 msg.rpc_argp = &calldata->arg,
1469 msg.rpc_resp = &calldata->res,
1470 task_setup_data.callback_data = calldata;
1471 task = rpc_run_task(&task_setup_data);
1473 return PTR_ERR(task);
1476 status = rpc_wait_for_completion_task(task);
1482 nfs4_put_open_state(state);
1483 nfs4_put_state_owner(sp);
1487 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1492 /* If the open_intent is for execute, we have an extra check to make */
1493 if (fmode & FMODE_EXEC) {
1494 ret = nfs_may_open(state->inode,
1495 state->owner->so_cred,
1496 nd->intent.open.flags);
1500 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1501 if (!IS_ERR(filp)) {
1502 struct nfs_open_context *ctx;
1503 ctx = nfs_file_open_context(filp);
1507 ret = PTR_ERR(filp);
1509 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1514 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1516 struct path path = {
1517 .mnt = nd->path.mnt,
1520 struct dentry *parent;
1522 struct rpc_cred *cred;
1523 struct nfs4_state *state;
1525 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1527 if (nd->flags & LOOKUP_CREATE) {
1528 attr.ia_mode = nd->intent.open.create_mode;
1529 attr.ia_valid = ATTR_MODE;
1530 if (!IS_POSIXACL(dir))
1531 attr.ia_mode &= ~current_umask();
1534 BUG_ON(nd->intent.open.flags & O_CREAT);
1537 cred = rpc_lookup_cred();
1539 return (struct dentry *)cred;
1540 parent = dentry->d_parent;
1541 /* Protect against concurrent sillydeletes */
1542 nfs_block_sillyrename(parent);
1543 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1545 if (IS_ERR(state)) {
1546 if (PTR_ERR(state) == -ENOENT) {
1547 d_add(dentry, NULL);
1548 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1550 nfs_unblock_sillyrename(parent);
1551 return (struct dentry *)state;
1553 res = d_add_unique(dentry, igrab(state->inode));
1556 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1557 nfs_unblock_sillyrename(parent);
1558 nfs4_intent_set_file(nd, &path, state, fmode);
1563 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1565 struct path path = {
1566 .mnt = nd->path.mnt,
1569 struct rpc_cred *cred;
1570 struct nfs4_state *state;
1571 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1573 cred = rpc_lookup_cred();
1575 return PTR_ERR(cred);
1576 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1578 if (IS_ERR(state)) {
1579 switch (PTR_ERR(state)) {
1585 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1591 if (state->inode == dentry->d_inode) {
1592 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1593 nfs4_intent_set_file(nd, &path, state, fmode);
1596 nfs4_close_sync(&path, state, fmode);
1602 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1604 if (ctx->state == NULL)
1607 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1609 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1612 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1614 struct nfs4_server_caps_arg args = {
1617 struct nfs4_server_caps_res res = {};
1618 struct rpc_message msg = {
1619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1625 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1627 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1628 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1629 server->caps |= NFS_CAP_ACLS;
1630 if (res.has_links != 0)
1631 server->caps |= NFS_CAP_HARDLINKS;
1632 if (res.has_symlinks != 0)
1633 server->caps |= NFS_CAP_SYMLINKS;
1634 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1635 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1636 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1637 server->acl_bitmask = res.acl_bitmask;
1643 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1645 struct nfs4_exception exception = { };
1648 err = nfs4_handle_exception(server,
1649 _nfs4_server_capabilities(server, fhandle),
1651 } while (exception.retry);
1655 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1656 struct nfs_fsinfo *info)
1658 struct nfs4_lookup_root_arg args = {
1659 .bitmask = nfs4_fattr_bitmap,
1661 struct nfs4_lookup_res res = {
1663 .fattr = info->fattr,
1666 struct rpc_message msg = {
1667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1671 nfs_fattr_init(info->fattr);
1672 return nfs4_call_sync(server, &msg, &args, &res, 0);
1675 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1676 struct nfs_fsinfo *info)
1678 struct nfs4_exception exception = { };
1681 err = nfs4_handle_exception(server,
1682 _nfs4_lookup_root(server, fhandle, info),
1684 } while (exception.retry);
1689 * get the file handle for the "/" directory on the server
1691 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1692 struct nfs_fsinfo *info)
1696 status = nfs4_lookup_root(server, fhandle, info);
1698 status = nfs4_server_capabilities(server, fhandle);
1700 status = nfs4_do_fsinfo(server, fhandle, info);
1701 return nfs4_map_errors(status);
1705 * Get locations and (maybe) other attributes of a referral.
1706 * Note that we'll actually follow the referral later when
1707 * we detect fsid mismatch in inode revalidation
1709 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
1711 int status = -ENOMEM;
1712 struct page *page = NULL;
1713 struct nfs4_fs_locations *locations = NULL;
1715 page = alloc_page(GFP_KERNEL);
1718 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1719 if (locations == NULL)
1722 status = nfs4_proc_fs_locations(dir, name, locations, page);
1725 /* Make sure server returned a different fsid for the referral */
1726 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
1727 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
1732 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1733 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1735 fattr->mode = S_IFDIR;
1736 memset(fhandle, 0, sizeof(struct nfs_fh));
1745 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1747 struct nfs4_getattr_arg args = {
1749 .bitmask = server->attr_bitmask,
1751 struct nfs4_getattr_res res = {
1755 struct rpc_message msg = {
1756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1761 nfs_fattr_init(fattr);
1762 return nfs4_call_sync(server, &msg, &args, &res, 0);
1765 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1767 struct nfs4_exception exception = { };
1770 err = nfs4_handle_exception(server,
1771 _nfs4_proc_getattr(server, fhandle, fattr),
1773 } while (exception.retry);
1778 * The file is not closed if it is opened due to the a request to change
1779 * the size of the file. The open call will not be needed once the
1780 * VFS layer lookup-intents are implemented.
1782 * Close is called when the inode is destroyed.
1783 * If we haven't opened the file for O_WRONLY, we
1784 * need to in the size_change case to obtain a stateid.
1787 * Because OPEN is always done by name in nfsv4, it is
1788 * possible that we opened a different file by the same
1789 * name. We can recognize this race condition, but we
1790 * can't do anything about it besides returning an error.
1792 * This will be fixed with VFS changes (lookup-intent).
1795 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1796 struct iattr *sattr)
1798 struct inode *inode = dentry->d_inode;
1799 struct rpc_cred *cred = NULL;
1800 struct nfs4_state *state = NULL;
1803 nfs_fattr_init(fattr);
1805 /* Search for an existing open(O_WRITE) file */
1806 if (sattr->ia_valid & ATTR_FILE) {
1807 struct nfs_open_context *ctx;
1809 ctx = nfs_file_open_context(sattr->ia_file);
1816 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1818 nfs_setattr_update_inode(inode, sattr);
1822 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1823 const struct qstr *name, struct nfs_fh *fhandle,
1824 struct nfs_fattr *fattr)
1827 struct nfs4_lookup_arg args = {
1828 .bitmask = server->attr_bitmask,
1832 struct nfs4_lookup_res res = {
1837 struct rpc_message msg = {
1838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1843 nfs_fattr_init(fattr);
1845 dprintk("NFS call lookupfh %s\n", name->name);
1846 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1847 dprintk("NFS reply lookupfh: %d\n", status);
1851 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1852 struct qstr *name, struct nfs_fh *fhandle,
1853 struct nfs_fattr *fattr)
1855 struct nfs4_exception exception = { };
1858 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1860 if (err == -NFS4ERR_MOVED) {
1864 err = nfs4_handle_exception(server, err, &exception);
1865 } while (exception.retry);
1869 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
1870 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1874 dprintk("NFS call lookup %s\n", name->name);
1875 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
1876 if (status == -NFS4ERR_MOVED)
1877 status = nfs4_get_referral(dir, name, fattr, fhandle);
1878 dprintk("NFS reply lookup: %d\n", status);
1882 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1884 struct nfs4_exception exception = { };
1887 err = nfs4_handle_exception(NFS_SERVER(dir),
1888 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1890 } while (exception.retry);
1894 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1896 struct nfs_server *server = NFS_SERVER(inode);
1897 struct nfs_fattr fattr;
1898 struct nfs4_accessargs args = {
1899 .fh = NFS_FH(inode),
1900 .bitmask = server->attr_bitmask,
1902 struct nfs4_accessres res = {
1906 struct rpc_message msg = {
1907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1910 .rpc_cred = entry->cred,
1912 int mode = entry->mask;
1916 * Determine which access bits we want to ask for...
1918 if (mode & MAY_READ)
1919 args.access |= NFS4_ACCESS_READ;
1920 if (S_ISDIR(inode->i_mode)) {
1921 if (mode & MAY_WRITE)
1922 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1923 if (mode & MAY_EXEC)
1924 args.access |= NFS4_ACCESS_LOOKUP;
1926 if (mode & MAY_WRITE)
1927 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1928 if (mode & MAY_EXEC)
1929 args.access |= NFS4_ACCESS_EXECUTE;
1931 nfs_fattr_init(&fattr);
1932 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1935 if (res.access & NFS4_ACCESS_READ)
1936 entry->mask |= MAY_READ;
1937 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1938 entry->mask |= MAY_WRITE;
1939 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1940 entry->mask |= MAY_EXEC;
1941 nfs_refresh_inode(inode, &fattr);
1946 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1948 struct nfs4_exception exception = { };
1951 err = nfs4_handle_exception(NFS_SERVER(inode),
1952 _nfs4_proc_access(inode, entry),
1954 } while (exception.retry);
1959 * TODO: For the time being, we don't try to get any attributes
1960 * along with any of the zero-copy operations READ, READDIR,
1963 * In the case of the first three, we want to put the GETATTR
1964 * after the read-type operation -- this is because it is hard
1965 * to predict the length of a GETATTR response in v4, and thus
1966 * align the READ data correctly. This means that the GETATTR
1967 * may end up partially falling into the page cache, and we should
1968 * shift it into the 'tail' of the xdr_buf before processing.
1969 * To do this efficiently, we need to know the total length
1970 * of data received, which doesn't seem to be available outside
1973 * In the case of WRITE, we also want to put the GETATTR after
1974 * the operation -- in this case because we want to make sure
1975 * we get the post-operation mtime and size. This means that
1976 * we can't use xdr_encode_pages() as written: we need a variant
1977 * of it which would leave room in the 'tail' iovec.
1979 * Both of these changes to the XDR layer would in fact be quite
1980 * minor, but I decided to leave them for a subsequent patch.
1982 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1983 unsigned int pgbase, unsigned int pglen)
1985 struct nfs4_readlink args = {
1986 .fh = NFS_FH(inode),
1991 struct nfs4_readlink_res res;
1992 struct rpc_message msg = {
1993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1998 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2001 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2002 unsigned int pgbase, unsigned int pglen)
2004 struct nfs4_exception exception = { };
2007 err = nfs4_handle_exception(NFS_SERVER(inode),
2008 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2010 } while (exception.retry);
2016 * We will need to arrange for the VFS layer to provide an atomic open.
2017 * Until then, this create/open method is prone to inefficiency and race
2018 * conditions due to the lookup, create, and open VFS calls from sys_open()
2019 * placed on the wire.
2021 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2022 * The file will be opened again in the subsequent VFS open call
2023 * (nfs4_proc_file_open).
2025 * The open for read will just hang around to be used by any process that
2026 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2030 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2031 int flags, struct nameidata *nd)
2033 struct path path = {
2034 .mnt = nd->path.mnt,
2037 struct nfs4_state *state;
2038 struct rpc_cred *cred;
2039 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2042 cred = rpc_lookup_cred();
2044 status = PTR_ERR(cred);
2047 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2049 if (IS_ERR(state)) {
2050 status = PTR_ERR(state);
2053 d_add(dentry, igrab(state->inode));
2054 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2055 if (flags & O_EXCL) {
2056 struct nfs_fattr fattr;
2057 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2059 nfs_setattr_update_inode(state->inode, sattr);
2060 nfs_post_op_update_inode(state->inode, &fattr);
2062 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2063 status = nfs4_intent_set_file(nd, &path, state, fmode);
2065 nfs4_close_sync(&path, state, fmode);
2072 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2074 struct nfs_server *server = NFS_SERVER(dir);
2075 struct nfs_removeargs args = {
2077 .name.len = name->len,
2078 .name.name = name->name,
2079 .bitmask = server->attr_bitmask,
2081 struct nfs_removeres res = {
2084 struct rpc_message msg = {
2085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2091 nfs_fattr_init(&res.dir_attr);
2092 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2094 update_changeattr(dir, &res.cinfo);
2095 nfs_post_op_update_inode(dir, &res.dir_attr);
2100 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2102 struct nfs4_exception exception = { };
2105 err = nfs4_handle_exception(NFS_SERVER(dir),
2106 _nfs4_proc_remove(dir, name),
2108 } while (exception.retry);
2112 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2114 struct nfs_server *server = NFS_SERVER(dir);
2115 struct nfs_removeargs *args = msg->rpc_argp;
2116 struct nfs_removeres *res = msg->rpc_resp;
2118 args->bitmask = server->cache_consistency_bitmask;
2119 res->server = server;
2120 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2123 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2125 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2127 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2129 update_changeattr(dir, &res->cinfo);
2130 nfs_post_op_update_inode(dir, &res->dir_attr);
2134 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2135 struct inode *new_dir, struct qstr *new_name)
2137 struct nfs_server *server = NFS_SERVER(old_dir);
2138 struct nfs4_rename_arg arg = {
2139 .old_dir = NFS_FH(old_dir),
2140 .new_dir = NFS_FH(new_dir),
2141 .old_name = old_name,
2142 .new_name = new_name,
2143 .bitmask = server->attr_bitmask,
2145 struct nfs_fattr old_fattr, new_fattr;
2146 struct nfs4_rename_res res = {
2148 .old_fattr = &old_fattr,
2149 .new_fattr = &new_fattr,
2151 struct rpc_message msg = {
2152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2158 nfs_fattr_init(res.old_fattr);
2159 nfs_fattr_init(res.new_fattr);
2160 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2163 update_changeattr(old_dir, &res.old_cinfo);
2164 nfs_post_op_update_inode(old_dir, res.old_fattr);
2165 update_changeattr(new_dir, &res.new_cinfo);
2166 nfs_post_op_update_inode(new_dir, res.new_fattr);
2171 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2172 struct inode *new_dir, struct qstr *new_name)
2174 struct nfs4_exception exception = { };
2177 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2178 _nfs4_proc_rename(old_dir, old_name,
2181 } while (exception.retry);
2185 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2187 struct nfs_server *server = NFS_SERVER(inode);
2188 struct nfs4_link_arg arg = {
2189 .fh = NFS_FH(inode),
2190 .dir_fh = NFS_FH(dir),
2192 .bitmask = server->attr_bitmask,
2194 struct nfs_fattr fattr, dir_attr;
2195 struct nfs4_link_res res = {
2198 .dir_attr = &dir_attr,
2200 struct rpc_message msg = {
2201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2207 nfs_fattr_init(res.fattr);
2208 nfs_fattr_init(res.dir_attr);
2209 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2211 update_changeattr(dir, &res.cinfo);
2212 nfs_post_op_update_inode(dir, res.dir_attr);
2213 nfs_post_op_update_inode(inode, res.fattr);
2219 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2221 struct nfs4_exception exception = { };
2224 err = nfs4_handle_exception(NFS_SERVER(inode),
2225 _nfs4_proc_link(inode, dir, name),
2227 } while (exception.retry);
2231 struct nfs4_createdata {
2232 struct rpc_message msg;
2233 struct nfs4_create_arg arg;
2234 struct nfs4_create_res res;
2236 struct nfs_fattr fattr;
2237 struct nfs_fattr dir_fattr;
2240 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2241 struct qstr *name, struct iattr *sattr, u32 ftype)
2243 struct nfs4_createdata *data;
2245 data = kzalloc(sizeof(*data), GFP_KERNEL);
2247 struct nfs_server *server = NFS_SERVER(dir);
2249 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2250 data->msg.rpc_argp = &data->arg;
2251 data->msg.rpc_resp = &data->res;
2252 data->arg.dir_fh = NFS_FH(dir);
2253 data->arg.server = server;
2254 data->arg.name = name;
2255 data->arg.attrs = sattr;
2256 data->arg.ftype = ftype;
2257 data->arg.bitmask = server->attr_bitmask;
2258 data->res.server = server;
2259 data->res.fh = &data->fh;
2260 data->res.fattr = &data->fattr;
2261 data->res.dir_fattr = &data->dir_fattr;
2262 nfs_fattr_init(data->res.fattr);
2263 nfs_fattr_init(data->res.dir_fattr);
2268 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2270 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2271 &data->arg, &data->res, 1);
2273 update_changeattr(dir, &data->res.dir_cinfo);
2274 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2275 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2280 static void nfs4_free_createdata(struct nfs4_createdata *data)
2285 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2286 struct page *page, unsigned int len, struct iattr *sattr)
2288 struct nfs4_createdata *data;
2289 int status = -ENAMETOOLONG;
2291 if (len > NFS4_MAXPATHLEN)
2295 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2299 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2300 data->arg.u.symlink.pages = &page;
2301 data->arg.u.symlink.len = len;
2303 status = nfs4_do_create(dir, dentry, data);
2305 nfs4_free_createdata(data);
2310 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2311 struct page *page, unsigned int len, struct iattr *sattr)
2313 struct nfs4_exception exception = { };
2316 err = nfs4_handle_exception(NFS_SERVER(dir),
2317 _nfs4_proc_symlink(dir, dentry, page,
2320 } while (exception.retry);
2324 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2325 struct iattr *sattr)
2327 struct nfs4_createdata *data;
2328 int status = -ENOMEM;
2330 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2334 status = nfs4_do_create(dir, dentry, data);
2336 nfs4_free_createdata(data);
2341 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2342 struct iattr *sattr)
2344 struct nfs4_exception exception = { };
2347 err = nfs4_handle_exception(NFS_SERVER(dir),
2348 _nfs4_proc_mkdir(dir, dentry, sattr),
2350 } while (exception.retry);
2354 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2355 u64 cookie, struct page *page, unsigned int count, int plus)
2357 struct inode *dir = dentry->d_inode;
2358 struct nfs4_readdir_arg args = {
2363 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2365 struct nfs4_readdir_res res;
2366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2374 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2375 dentry->d_parent->d_name.name,
2376 dentry->d_name.name,
2377 (unsigned long long)cookie);
2378 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2379 res.pgbase = args.pgbase;
2380 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2382 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2384 nfs_invalidate_atime(dir);
2386 dprintk("%s: returns %d\n", __func__, status);
2390 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2391 u64 cookie, struct page *page, unsigned int count, int plus)
2393 struct nfs4_exception exception = { };
2396 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2397 _nfs4_proc_readdir(dentry, cred, cookie,
2400 } while (exception.retry);
2404 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2405 struct iattr *sattr, dev_t rdev)
2407 struct nfs4_createdata *data;
2408 int mode = sattr->ia_mode;
2409 int status = -ENOMEM;
2411 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2412 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2414 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2419 data->arg.ftype = NF4FIFO;
2420 else if (S_ISBLK(mode)) {
2421 data->arg.ftype = NF4BLK;
2422 data->arg.u.device.specdata1 = MAJOR(rdev);
2423 data->arg.u.device.specdata2 = MINOR(rdev);
2425 else if (S_ISCHR(mode)) {
2426 data->arg.ftype = NF4CHR;
2427 data->arg.u.device.specdata1 = MAJOR(rdev);
2428 data->arg.u.device.specdata2 = MINOR(rdev);
2431 status = nfs4_do_create(dir, dentry, data);
2433 nfs4_free_createdata(data);
2438 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2439 struct iattr *sattr, dev_t rdev)
2441 struct nfs4_exception exception = { };
2444 err = nfs4_handle_exception(NFS_SERVER(dir),
2445 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2447 } while (exception.retry);
2451 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2452 struct nfs_fsstat *fsstat)
2454 struct nfs4_statfs_arg args = {
2456 .bitmask = server->attr_bitmask,
2458 struct nfs4_statfs_res res = {
2461 struct rpc_message msg = {
2462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2467 nfs_fattr_init(fsstat->fattr);
2468 return nfs4_call_sync(server, &msg, &args, &res, 0);
2471 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2473 struct nfs4_exception exception = { };
2476 err = nfs4_handle_exception(server,
2477 _nfs4_proc_statfs(server, fhandle, fsstat),
2479 } while (exception.retry);
2483 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2484 struct nfs_fsinfo *fsinfo)
2486 struct nfs4_fsinfo_arg args = {
2488 .bitmask = server->attr_bitmask,
2490 struct nfs4_fsinfo_res res = {
2493 struct rpc_message msg = {
2494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2499 return nfs4_call_sync(server, &msg, &args, &res, 0);
2502 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2504 struct nfs4_exception exception = { };
2508 err = nfs4_handle_exception(server,
2509 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2511 } while (exception.retry);
2515 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2517 nfs_fattr_init(fsinfo->fattr);
2518 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2521 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2522 struct nfs_pathconf *pathconf)
2524 struct nfs4_pathconf_arg args = {
2526 .bitmask = server->attr_bitmask,
2528 struct nfs4_pathconf_res res = {
2529 .pathconf = pathconf,
2531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2537 /* None of the pathconf attributes are mandatory to implement */
2538 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2539 memset(pathconf, 0, sizeof(*pathconf));
2543 nfs_fattr_init(pathconf->fattr);
2544 return nfs4_call_sync(server, &msg, &args, &res, 0);
2547 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2548 struct nfs_pathconf *pathconf)
2550 struct nfs4_exception exception = { };
2554 err = nfs4_handle_exception(server,
2555 _nfs4_proc_pathconf(server, fhandle, pathconf),
2557 } while (exception.retry);
2561 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2563 struct nfs_server *server = NFS_SERVER(data->inode);
2565 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2566 rpc_restart_call(task);
2570 nfs_invalidate_atime(data->inode);
2571 if (task->tk_status > 0)
2572 renew_lease(server, data->timestamp);
2576 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2578 data->timestamp = jiffies;
2579 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2582 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2584 struct inode *inode = data->inode;
2586 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2587 rpc_restart_call(task);
2590 if (task->tk_status >= 0) {
2591 renew_lease(NFS_SERVER(inode), data->timestamp);
2592 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2597 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2599 struct nfs_server *server = NFS_SERVER(data->inode);
2601 data->args.bitmask = server->cache_consistency_bitmask;
2602 data->res.server = server;
2603 data->timestamp = jiffies;
2605 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2608 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2610 struct inode *inode = data->inode;
2612 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2613 rpc_restart_call(task);
2616 nfs_refresh_inode(inode, data->res.fattr);
2620 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2622 struct nfs_server *server = NFS_SERVER(data->inode);
2624 data->args.bitmask = server->cache_consistency_bitmask;
2625 data->res.server = server;
2626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2630 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2631 * standalone procedure for queueing an asynchronous RENEW.
2633 static void nfs4_renew_done(struct rpc_task *task, void *data)
2635 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2636 unsigned long timestamp = (unsigned long)data;
2638 if (task->tk_status < 0) {
2639 /* Unless we're shutting down, schedule state recovery! */
2640 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2641 nfs4_schedule_state_recovery(clp);
2644 spin_lock(&clp->cl_lock);
2645 if (time_before(clp->cl_last_renewal,timestamp))
2646 clp->cl_last_renewal = timestamp;
2647 spin_unlock(&clp->cl_lock);
2650 static const struct rpc_call_ops nfs4_renew_ops = {
2651 .rpc_call_done = nfs4_renew_done,
2654 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2662 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2663 &nfs4_renew_ops, (void *)jiffies);
2666 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2668 struct rpc_message msg = {
2669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2673 unsigned long now = jiffies;
2676 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2679 spin_lock(&clp->cl_lock);
2680 if (time_before(clp->cl_last_renewal,now))
2681 clp->cl_last_renewal = now;
2682 spin_unlock(&clp->cl_lock);
2686 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2688 return (server->caps & NFS_CAP_ACLS)
2689 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2690 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2693 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2694 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2697 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2699 static void buf_to_pages(const void *buf, size_t buflen,
2700 struct page **pages, unsigned int *pgbase)
2702 const void *p = buf;
2704 *pgbase = offset_in_page(buf);
2706 while (p < buf + buflen) {
2707 *(pages++) = virt_to_page(p);
2708 p += PAGE_CACHE_SIZE;
2712 struct nfs4_cached_acl {
2718 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2720 struct nfs_inode *nfsi = NFS_I(inode);
2722 spin_lock(&inode->i_lock);
2723 kfree(nfsi->nfs4_acl);
2724 nfsi->nfs4_acl = acl;
2725 spin_unlock(&inode->i_lock);
2728 static void nfs4_zap_acl_attr(struct inode *inode)
2730 nfs4_set_cached_acl(inode, NULL);
2733 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2735 struct nfs_inode *nfsi = NFS_I(inode);
2736 struct nfs4_cached_acl *acl;
2739 spin_lock(&inode->i_lock);
2740 acl = nfsi->nfs4_acl;
2743 if (buf == NULL) /* user is just asking for length */
2745 if (acl->cached == 0)
2747 ret = -ERANGE; /* see getxattr(2) man page */
2748 if (acl->len > buflen)
2750 memcpy(buf, acl->data, acl->len);
2754 spin_unlock(&inode->i_lock);
2758 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2760 struct nfs4_cached_acl *acl;
2762 if (buf && acl_len <= PAGE_SIZE) {
2763 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2767 memcpy(acl->data, buf, acl_len);
2769 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2776 nfs4_set_cached_acl(inode, acl);
2779 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2781 struct page *pages[NFS4ACL_MAXPAGES];
2782 struct nfs_getaclargs args = {
2783 .fh = NFS_FH(inode),
2787 struct nfs_getaclres res = {
2791 struct rpc_message msg = {
2792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2796 struct page *localpage = NULL;
2799 if (buflen < PAGE_SIZE) {
2800 /* As long as we're doing a round trip to the server anyway,
2801 * let's be prepared for a page of acl data. */
2802 localpage = alloc_page(GFP_KERNEL);
2803 resp_buf = page_address(localpage);
2804 if (localpage == NULL)
2806 args.acl_pages[0] = localpage;
2807 args.acl_pgbase = 0;
2808 args.acl_len = PAGE_SIZE;
2811 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2813 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2816 if (res.acl_len > args.acl_len)
2817 nfs4_write_cached_acl(inode, NULL, res.acl_len);
2819 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
2822 if (res.acl_len > buflen)
2825 memcpy(buf, resp_buf, res.acl_len);
2830 __free_page(localpage);
2834 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2836 struct nfs4_exception exception = { };
2839 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2842 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2843 } while (exception.retry);
2847 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2849 struct nfs_server *server = NFS_SERVER(inode);
2852 if (!nfs4_server_supports_acls(server))
2854 ret = nfs_revalidate_inode(server, inode);
2857 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2858 nfs_zap_acl_cache(inode);
2859 ret = nfs4_read_cached_acl(inode, buf, buflen);
2862 return nfs4_get_acl_uncached(inode, buf, buflen);
2865 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2867 struct nfs_server *server = NFS_SERVER(inode);
2868 struct page *pages[NFS4ACL_MAXPAGES];
2869 struct nfs_setaclargs arg = {
2870 .fh = NFS_FH(inode),
2874 struct nfs_setaclres res;
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2882 if (!nfs4_server_supports_acls(server))
2884 nfs_inode_return_delegation(inode);
2885 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2886 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
2887 nfs_access_zap_cache(inode);
2888 nfs_zap_acl_cache(inode);
2892 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2894 struct nfs4_exception exception = { };
2897 err = nfs4_handle_exception(NFS_SERVER(inode),
2898 __nfs4_proc_set_acl(inode, buf, buflen),
2900 } while (exception.retry);
2905 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
2907 struct nfs_client *clp = server->nfs_client;
2909 if (!clp || task->tk_status >= 0)
2911 switch(task->tk_status) {
2912 case -NFS4ERR_ADMIN_REVOKED:
2913 case -NFS4ERR_BAD_STATEID:
2914 case -NFS4ERR_OPENMODE:
2917 nfs4_state_mark_reclaim_nograce(clp, state);
2918 case -NFS4ERR_STALE_CLIENTID:
2919 case -NFS4ERR_STALE_STATEID:
2920 case -NFS4ERR_EXPIRED:
2921 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
2922 nfs4_schedule_state_recovery(clp);
2923 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
2924 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
2925 task->tk_status = 0;
2927 case -NFS4ERR_DELAY:
2928 nfs_inc_server_stats(server, NFSIOS_DELAY);
2929 case -NFS4ERR_GRACE:
2930 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2931 task->tk_status = 0;
2933 case -NFS4ERR_OLD_STATEID:
2934 task->tk_status = 0;
2937 task->tk_status = nfs4_map_errors(task->tk_status);
2941 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
2943 nfs4_verifier sc_verifier;
2944 struct nfs4_setclientid setclientid = {
2945 .sc_verifier = &sc_verifier,
2948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2950 .rpc_argp = &setclientid,
2958 p = (__be32*)sc_verifier.data;
2959 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2960 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2963 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2964 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
2966 rpc_peeraddr2str(clp->cl_rpcclient,
2968 rpc_peeraddr2str(clp->cl_rpcclient,
2970 clp->cl_rpcclient->cl_auth->au_ops->au_name,
2971 clp->cl_id_uniquifier);
2972 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2973 sizeof(setclientid.sc_netid),
2974 rpc_peeraddr2str(clp->cl_rpcclient,
2975 RPC_DISPLAY_NETID));
2976 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2977 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
2978 clp->cl_ipaddr, port >> 8, port & 255);
2980 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2981 if (status != -NFS4ERR_CLID_INUSE)
2986 ssleep(clp->cl_lease_time + 1);
2988 if (++clp->cl_id_uniquifier == 0)
2994 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2996 struct nfs_fsinfo fsinfo;
2997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3000 .rpc_resp = &fsinfo,
3007 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3009 spin_lock(&clp->cl_lock);
3010 clp->cl_lease_time = fsinfo.lease_time * HZ;
3011 clp->cl_last_renewal = now;
3012 spin_unlock(&clp->cl_lock);
3017 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3022 err = _nfs4_proc_setclientid_confirm(clp, cred);
3026 case -NFS4ERR_RESOURCE:
3027 /* The IBM lawyers misread another document! */
3028 case -NFS4ERR_DELAY:
3029 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3035 struct nfs4_delegreturndata {
3036 struct nfs4_delegreturnargs args;
3037 struct nfs4_delegreturnres res;
3039 nfs4_stateid stateid;
3040 unsigned long timestamp;
3041 struct nfs_fattr fattr;
3045 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3047 struct nfs4_delegreturndata *data = calldata;
3048 data->rpc_status = task->tk_status;
3049 if (data->rpc_status == 0)
3050 renew_lease(data->res.server, data->timestamp);
3053 static void nfs4_delegreturn_release(void *calldata)
3058 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3059 .rpc_call_done = nfs4_delegreturn_done,
3060 .rpc_release = nfs4_delegreturn_release,
3063 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3065 struct nfs4_delegreturndata *data;
3066 struct nfs_server *server = NFS_SERVER(inode);
3067 struct rpc_task *task;
3068 struct rpc_message msg = {
3069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3072 struct rpc_task_setup task_setup_data = {
3073 .rpc_client = server->client,
3074 .rpc_message = &msg,
3075 .callback_ops = &nfs4_delegreturn_ops,
3076 .flags = RPC_TASK_ASYNC,
3080 data = kmalloc(sizeof(*data), GFP_KERNEL);
3083 data->args.fhandle = &data->fh;
3084 data->args.stateid = &data->stateid;
3085 data->args.bitmask = server->attr_bitmask;
3086 nfs_copy_fh(&data->fh, NFS_FH(inode));
3087 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3088 data->res.fattr = &data->fattr;
3089 data->res.server = server;
3090 nfs_fattr_init(data->res.fattr);
3091 data->timestamp = jiffies;
3092 data->rpc_status = 0;
3094 task_setup_data.callback_data = data;
3095 msg.rpc_argp = &data->args,
3096 msg.rpc_resp = &data->res,
3097 task = rpc_run_task(&task_setup_data);
3099 return PTR_ERR(task);
3102 status = nfs4_wait_for_completion_rpc_task(task);
3105 status = data->rpc_status;
3108 nfs_refresh_inode(inode, &data->fattr);
3114 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3116 struct nfs_server *server = NFS_SERVER(inode);
3117 struct nfs4_exception exception = { };
3120 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3122 case -NFS4ERR_STALE_STATEID:
3123 case -NFS4ERR_EXPIRED:
3127 err = nfs4_handle_exception(server, err, &exception);
3128 } while (exception.retry);
3132 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3133 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3136 * sleep, with exponential backoff, and retry the LOCK operation.
3138 static unsigned long
3139 nfs4_set_lock_task_retry(unsigned long timeout)
3141 schedule_timeout_killable(timeout);
3143 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3144 return NFS4_LOCK_MAXTIMEOUT;
3148 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3150 struct inode *inode = state->inode;
3151 struct nfs_server *server = NFS_SERVER(inode);
3152 struct nfs_client *clp = server->nfs_client;
3153 struct nfs_lockt_args arg = {
3154 .fh = NFS_FH(inode),
3157 struct nfs_lockt_res res = {
3160 struct rpc_message msg = {
3161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3164 .rpc_cred = state->owner->so_cred,
3166 struct nfs4_lock_state *lsp;
3169 arg.lock_owner.clientid = clp->cl_clientid;
3170 status = nfs4_set_lock_state(state, request);
3173 lsp = request->fl_u.nfs4_fl.owner;
3174 arg.lock_owner.id = lsp->ls_id.id;
3175 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3178 request->fl_type = F_UNLCK;
3180 case -NFS4ERR_DENIED:
3183 request->fl_ops->fl_release_private(request);
3188 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3190 struct nfs4_exception exception = { };
3194 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3195 _nfs4_proc_getlk(state, cmd, request),
3197 } while (exception.retry);
3201 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3204 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3206 res = posix_lock_file_wait(file, fl);
3209 res = flock_lock_file_wait(file, fl);
3217 struct nfs4_unlockdata {
3218 struct nfs_locku_args arg;
3219 struct nfs_locku_res res;
3220 struct nfs4_lock_state *lsp;
3221 struct nfs_open_context *ctx;
3222 struct file_lock fl;
3223 const struct nfs_server *server;
3224 unsigned long timestamp;
3227 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3228 struct nfs_open_context *ctx,
3229 struct nfs4_lock_state *lsp,
3230 struct nfs_seqid *seqid)
3232 struct nfs4_unlockdata *p;
3233 struct inode *inode = lsp->ls_state->inode;
3235 p = kmalloc(sizeof(*p), GFP_KERNEL);
3238 p->arg.fh = NFS_FH(inode);
3240 p->arg.seqid = seqid;
3241 p->res.seqid = seqid;
3242 p->arg.stateid = &lsp->ls_stateid;
3244 atomic_inc(&lsp->ls_count);
3245 /* Ensure we don't close file until we're done freeing locks! */
3246 p->ctx = get_nfs_open_context(ctx);
3247 memcpy(&p->fl, fl, sizeof(p->fl));
3248 p->server = NFS_SERVER(inode);
3252 static void nfs4_locku_release_calldata(void *data)
3254 struct nfs4_unlockdata *calldata = data;
3255 nfs_free_seqid(calldata->arg.seqid);
3256 nfs4_put_lock_state(calldata->lsp);
3257 put_nfs_open_context(calldata->ctx);
3261 static void nfs4_locku_done(struct rpc_task *task, void *data)
3263 struct nfs4_unlockdata *calldata = data;
3265 if (RPC_ASSASSINATED(task))
3267 switch (task->tk_status) {
3269 memcpy(calldata->lsp->ls_stateid.data,
3270 calldata->res.stateid.data,
3271 sizeof(calldata->lsp->ls_stateid.data));
3272 renew_lease(calldata->server, calldata->timestamp);
3274 case -NFS4ERR_BAD_STATEID:
3275 case -NFS4ERR_OLD_STATEID:
3276 case -NFS4ERR_STALE_STATEID:
3277 case -NFS4ERR_EXPIRED:
3280 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3281 rpc_restart_call(task);
3285 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3287 struct nfs4_unlockdata *calldata = data;
3289 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3291 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3292 /* Note: exit _without_ running nfs4_locku_done */
3293 task->tk_action = NULL;
3296 calldata->timestamp = jiffies;
3297 rpc_call_start(task);
3300 static const struct rpc_call_ops nfs4_locku_ops = {
3301 .rpc_call_prepare = nfs4_locku_prepare,
3302 .rpc_call_done = nfs4_locku_done,
3303 .rpc_release = nfs4_locku_release_calldata,
3306 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3307 struct nfs_open_context *ctx,
3308 struct nfs4_lock_state *lsp,
3309 struct nfs_seqid *seqid)
3311 struct nfs4_unlockdata *data;
3312 struct rpc_message msg = {
3313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3314 .rpc_cred = ctx->cred,
3316 struct rpc_task_setup task_setup_data = {
3317 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3318 .rpc_message = &msg,
3319 .callback_ops = &nfs4_locku_ops,
3320 .workqueue = nfsiod_workqueue,
3321 .flags = RPC_TASK_ASYNC,
3324 /* Ensure this is an unlock - when canceling a lock, the
3325 * canceled lock is passed in, and it won't be an unlock.
3327 fl->fl_type = F_UNLCK;
3329 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3331 nfs_free_seqid(seqid);
3332 return ERR_PTR(-ENOMEM);
3335 msg.rpc_argp = &data->arg,
3336 msg.rpc_resp = &data->res,
3337 task_setup_data.callback_data = data;
3338 return rpc_run_task(&task_setup_data);
3341 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3343 struct nfs_inode *nfsi = NFS_I(state->inode);
3344 struct nfs_seqid *seqid;
3345 struct nfs4_lock_state *lsp;
3346 struct rpc_task *task;
3348 unsigned char fl_flags = request->fl_flags;
3350 status = nfs4_set_lock_state(state, request);
3351 /* Unlock _before_ we do the RPC call */
3352 request->fl_flags |= FL_EXISTS;
3353 down_read(&nfsi->rwsem);
3354 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3355 up_read(&nfsi->rwsem);
3358 up_read(&nfsi->rwsem);
3361 /* Is this a delegated lock? */
3362 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3364 lsp = request->fl_u.nfs4_fl.owner;
3365 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3369 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3370 status = PTR_ERR(task);
3373 status = nfs4_wait_for_completion_rpc_task(task);
3376 request->fl_flags = fl_flags;
3380 struct nfs4_lockdata {
3381 struct nfs_lock_args arg;
3382 struct nfs_lock_res res;
3383 struct nfs4_lock_state *lsp;
3384 struct nfs_open_context *ctx;
3385 struct file_lock fl;
3386 unsigned long timestamp;
3391 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3392 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3394 struct nfs4_lockdata *p;
3395 struct inode *inode = lsp->ls_state->inode;
3396 struct nfs_server *server = NFS_SERVER(inode);
3398 p = kzalloc(sizeof(*p), GFP_KERNEL);
3402 p->arg.fh = NFS_FH(inode);
3404 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3405 if (p->arg.open_seqid == NULL)
3407 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3408 if (p->arg.lock_seqid == NULL)
3409 goto out_free_seqid;
3410 p->arg.lock_stateid = &lsp->ls_stateid;
3411 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3412 p->arg.lock_owner.id = lsp->ls_id.id;
3413 p->res.lock_seqid = p->arg.lock_seqid;
3415 atomic_inc(&lsp->ls_count);
3416 p->ctx = get_nfs_open_context(ctx);
3417 memcpy(&p->fl, fl, sizeof(p->fl));
3420 nfs_free_seqid(p->arg.open_seqid);
3426 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3428 struct nfs4_lockdata *data = calldata;
3429 struct nfs4_state *state = data->lsp->ls_state;
3431 dprintk("%s: begin!\n", __func__);
3432 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3434 /* Do we need to do an open_to_lock_owner? */
3435 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3436 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3438 data->arg.open_stateid = &state->stateid;
3439 data->arg.new_lock_owner = 1;
3440 data->res.open_seqid = data->arg.open_seqid;
3442 data->arg.new_lock_owner = 0;
3443 data->timestamp = jiffies;
3444 rpc_call_start(task);
3445 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3448 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3450 struct nfs4_lockdata *data = calldata;
3452 dprintk("%s: begin!\n", __func__);
3454 data->rpc_status = task->tk_status;
3455 if (RPC_ASSASSINATED(task))
3457 if (data->arg.new_lock_owner != 0) {
3458 if (data->rpc_status == 0)
3459 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3463 if (data->rpc_status == 0) {
3464 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3465 sizeof(data->lsp->ls_stateid.data));
3466 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3467 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3470 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3473 static void nfs4_lock_release(void *calldata)
3475 struct nfs4_lockdata *data = calldata;
3477 dprintk("%s: begin!\n", __func__);
3478 nfs_free_seqid(data->arg.open_seqid);
3479 if (data->cancelled != 0) {
3480 struct rpc_task *task;
3481 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3482 data->arg.lock_seqid);
3485 dprintk("%s: cancelling lock!\n", __func__);
3487 nfs_free_seqid(data->arg.lock_seqid);
3488 nfs4_put_lock_state(data->lsp);
3489 put_nfs_open_context(data->ctx);
3491 dprintk("%s: done!\n", __func__);
3494 static const struct rpc_call_ops nfs4_lock_ops = {
3495 .rpc_call_prepare = nfs4_lock_prepare,
3496 .rpc_call_done = nfs4_lock_done,
3497 .rpc_release = nfs4_lock_release,
3500 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3502 struct nfs4_lockdata *data;
3503 struct rpc_task *task;
3504 struct rpc_message msg = {
3505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3506 .rpc_cred = state->owner->so_cred,
3508 struct rpc_task_setup task_setup_data = {
3509 .rpc_client = NFS_CLIENT(state->inode),
3510 .rpc_message = &msg,
3511 .callback_ops = &nfs4_lock_ops,
3512 .workqueue = nfsiod_workqueue,
3513 .flags = RPC_TASK_ASYNC,
3517 dprintk("%s: begin!\n", __func__);
3518 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3519 fl->fl_u.nfs4_fl.owner);
3523 data->arg.block = 1;
3525 data->arg.reclaim = 1;
3526 msg.rpc_argp = &data->arg,
3527 msg.rpc_resp = &data->res,
3528 task_setup_data.callback_data = data;
3529 task = rpc_run_task(&task_setup_data);
3531 return PTR_ERR(task);
3532 ret = nfs4_wait_for_completion_rpc_task(task);
3534 ret = data->rpc_status;
3535 if (ret == -NFS4ERR_DENIED)
3538 data->cancelled = 1;
3540 dprintk("%s: done, ret = %d!\n", __func__, ret);
3544 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3546 struct nfs_server *server = NFS_SERVER(state->inode);
3547 struct nfs4_exception exception = { };
3551 /* Cache the lock if possible... */
3552 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3554 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3555 if (err != -NFS4ERR_DELAY)
3557 nfs4_handle_exception(server, err, &exception);
3558 } while (exception.retry);
3562 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3564 struct nfs_server *server = NFS_SERVER(state->inode);
3565 struct nfs4_exception exception = { };
3568 err = nfs4_set_lock_state(state, request);
3572 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3574 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3575 if (err != -NFS4ERR_DELAY)
3577 nfs4_handle_exception(server, err, &exception);
3578 } while (exception.retry);
3582 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3584 struct nfs_inode *nfsi = NFS_I(state->inode);
3585 unsigned char fl_flags = request->fl_flags;
3588 /* Is this a delegated open? */
3589 status = nfs4_set_lock_state(state, request);
3592 request->fl_flags |= FL_ACCESS;
3593 status = do_vfs_lock(request->fl_file, request);
3596 down_read(&nfsi->rwsem);
3597 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3598 /* Yes: cache locks! */
3599 /* ...but avoid races with delegation recall... */
3600 request->fl_flags = fl_flags & ~FL_SLEEP;
3601 status = do_vfs_lock(request->fl_file, request);
3604 status = _nfs4_do_setlk(state, cmd, request, 0);
3607 /* Note: we always want to sleep here! */
3608 request->fl_flags = fl_flags | FL_SLEEP;
3609 if (do_vfs_lock(request->fl_file, request) < 0)
3610 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3612 up_read(&nfsi->rwsem);
3614 request->fl_flags = fl_flags;
3618 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3620 struct nfs4_exception exception = { };
3624 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3625 _nfs4_proc_setlk(state, cmd, request),
3627 } while (exception.retry);
3632 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3634 struct nfs_open_context *ctx;
3635 struct nfs4_state *state;
3636 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3639 /* verify open state */
3640 ctx = nfs_file_open_context(filp);
3643 if (request->fl_start < 0 || request->fl_end < 0)
3647 return nfs4_proc_getlk(state, F_GETLK, request);
3649 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3652 if (request->fl_type == F_UNLCK)
3653 return nfs4_proc_unlck(state, cmd, request);
3656 status = nfs4_proc_setlk(state, cmd, request);
3657 if ((status != -EAGAIN) || IS_SETLK(cmd))
3659 timeout = nfs4_set_lock_task_retry(timeout);
3660 status = -ERESTARTSYS;
3663 } while(status < 0);
3667 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3669 struct nfs_server *server = NFS_SERVER(state->inode);
3670 struct nfs4_exception exception = { };
3673 err = nfs4_set_lock_state(state, fl);
3677 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3678 if (err != -NFS4ERR_DELAY)
3680 err = nfs4_handle_exception(server, err, &exception);
3681 } while (exception.retry);
3686 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3688 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3689 size_t buflen, int flags)
3691 struct inode *inode = dentry->d_inode;
3693 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3696 return nfs4_proc_set_acl(inode, buf, buflen);
3699 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3700 * and that's what we'll do for e.g. user attributes that haven't been set.
3701 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3702 * attributes in kernel-managed attribute namespaces. */
3703 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3706 struct inode *inode = dentry->d_inode;
3708 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3711 return nfs4_proc_get_acl(inode, buf, buflen);
3714 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3716 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3718 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3720 if (buf && buflen < len)
3723 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3727 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3729 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3730 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3731 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3734 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3735 NFS_ATTR_FATTR_NLINK;
3736 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3740 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
3741 struct nfs4_fs_locations *fs_locations, struct page *page)
3743 struct nfs_server *server = NFS_SERVER(dir);
3745 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3746 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
3748 struct nfs4_fs_locations_arg args = {
3749 .dir_fh = NFS_FH(dir),
3754 struct nfs4_fs_locations_res res = {
3755 .fs_locations = fs_locations,
3757 struct rpc_message msg = {
3758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3764 dprintk("%s: start\n", __func__);
3765 nfs_fattr_init(&fs_locations->fattr);
3766 fs_locations->server = server;
3767 fs_locations->nlocations = 0;
3768 status = nfs4_call_sync(server, &msg, &args, &res, 0);
3769 nfs_fixup_referral_attributes(&fs_locations->fattr);
3770 dprintk("%s: returned status = %d\n", __func__, status);
3774 #ifdef CONFIG_NFS_V4_1
3775 /* Destroy the slot table */
3776 static void nfs4_destroy_slot_table(struct nfs4_session *session)
3778 if (session->fc_slot_table.slots == NULL)
3780 kfree(session->fc_slot_table.slots);
3781 session->fc_slot_table.slots = NULL;
3785 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3787 struct nfs4_session *session;
3788 struct nfs4_slot_table *tbl;
3790 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3793 tbl = &session->fc_slot_table;
3794 spin_lock_init(&tbl->slot_tbl_lock);
3795 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3800 void nfs4_destroy_session(struct nfs4_session *session)
3802 nfs4_destroy_slot_table(session);
3806 #endif /* CONFIG_NFS_V4_1 */
3808 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3809 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
3810 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
3811 .recover_open = nfs4_open_reclaim,
3812 .recover_lock = nfs4_lock_reclaim,
3815 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
3816 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
3817 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
3818 .recover_open = nfs4_open_expired,
3819 .recover_lock = nfs4_lock_expired,
3822 static const struct inode_operations nfs4_file_inode_operations = {
3823 .permission = nfs_permission,
3824 .getattr = nfs_getattr,
3825 .setattr = nfs_setattr,
3826 .getxattr = nfs4_getxattr,
3827 .setxattr = nfs4_setxattr,
3828 .listxattr = nfs4_listxattr,
3831 const struct nfs_rpc_ops nfs_v4_clientops = {
3832 .version = 4, /* protocol version */
3833 .dentry_ops = &nfs4_dentry_operations,
3834 .dir_inode_ops = &nfs4_dir_inode_operations,
3835 .file_inode_ops = &nfs4_file_inode_operations,
3836 .getroot = nfs4_proc_get_root,
3837 .getattr = nfs4_proc_getattr,
3838 .setattr = nfs4_proc_setattr,
3839 .lookupfh = nfs4_proc_lookupfh,
3840 .lookup = nfs4_proc_lookup,
3841 .access = nfs4_proc_access,
3842 .readlink = nfs4_proc_readlink,
3843 .create = nfs4_proc_create,
3844 .remove = nfs4_proc_remove,
3845 .unlink_setup = nfs4_proc_unlink_setup,
3846 .unlink_done = nfs4_proc_unlink_done,
3847 .rename = nfs4_proc_rename,
3848 .link = nfs4_proc_link,
3849 .symlink = nfs4_proc_symlink,
3850 .mkdir = nfs4_proc_mkdir,
3851 .rmdir = nfs4_proc_remove,
3852 .readdir = nfs4_proc_readdir,
3853 .mknod = nfs4_proc_mknod,
3854 .statfs = nfs4_proc_statfs,
3855 .fsinfo = nfs4_proc_fsinfo,
3856 .pathconf = nfs4_proc_pathconf,
3857 .set_capabilities = nfs4_server_capabilities,
3858 .decode_dirent = nfs4_decode_dirent,
3859 .read_setup = nfs4_proc_read_setup,
3860 .read_done = nfs4_read_done,
3861 .write_setup = nfs4_proc_write_setup,
3862 .write_done = nfs4_write_done,
3863 .commit_setup = nfs4_proc_commit_setup,
3864 .commit_done = nfs4_commit_done,
3865 .lock = nfs4_proc_lock,
3866 .clear_acl_cache = nfs4_zap_acl_attr,
3867 .close_context = nfs4_close_context,