4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
53 #include "delegation.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 static int _nfs4_proc_open(struct nfs4_opendata *data);
64 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
65 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
66 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69 /* Prevent leaks of NFSv4 errors into userland */
70 static int nfs4_map_errors(int err)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap[2] = {
88 | FATTR4_WORD0_FILEID,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap[2] = {
111 | FATTR4_WORD0_MAXNAME,
115 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
122 const u32 nfs4_fs_locations_bitmap[2] = {
124 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
141 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
142 struct nfs4_readdir_arg *readdir)
146 BUG_ON(readdir->count < 80);
148 readdir->cookie = cookie;
149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
196 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
203 nfs_wait_bit_killable, TASK_KILLABLE);
207 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
224 /* This is the error handling routine for processes that are allowed
227 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
229 struct nfs_client *clp = server->nfs_client;
230 struct nfs4_state *state = exception->state;
233 exception->retry = 0;
237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
242 nfs4_state_mark_reclaim_nograce(clp, state);
243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
249 exception->retry = 1;
251 case -NFS4ERR_FILE_OPEN:
254 ret = nfs4_delay(server->client, &exception->timeout);
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
265 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
267 struct nfs_client *clp = server->nfs_client;
268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
274 #if defined(CONFIG_NFS_V4_1)
277 * nfs4_free_slot - free a slot and efficiently update slot table.
279 * freeing a slot is trivially done by clearing its respective bit
281 * If the freed slotid equals highest_used_slotid we want to update it
282 * so that the server would be able to size down the slot table if needed,
283 * otherwise we know that the highest_used_slotid is still in use.
284 * When updating highest_used_slotid there may be "holes" in the bitmap
285 * so we need to scan down from highest_used_slotid to 0 looking for the now
286 * highest slotid in use.
287 * If none found, highest_used_slotid is set to -1.
290 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
292 int slotid = free_slotid;
294 spin_lock(&tbl->slot_tbl_lock);
295 /* clear used bit in bitmap */
296 __clear_bit(slotid, tbl->used_slots);
298 /* update highest_used_slotid when it is freed */
299 if (slotid == tbl->highest_used_slotid) {
300 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
301 if (slotid >= 0 && slotid < tbl->max_slots)
302 tbl->highest_used_slotid = slotid;
304 tbl->highest_used_slotid = -1;
306 rpc_wake_up_next(&tbl->slot_tbl_waitq);
307 spin_unlock(&tbl->slot_tbl_lock);
308 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
309 free_slotid, tbl->highest_used_slotid);
312 void nfs41_sequence_free_slot(const struct nfs_client *clp,
313 struct nfs4_sequence_res *res)
315 struct nfs4_slot_table *tbl;
317 if (!nfs4_has_session(clp)) {
318 dprintk("%s: No session\n", __func__);
321 tbl = &clp->cl_session->fc_slot_table;
322 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
323 dprintk("%s: No slot\n", __func__);
324 /* just wake up the next guy waiting since
325 * we may have not consumed a slot after all */
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
329 nfs4_free_slot(tbl, res->sr_slotid);
330 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
333 static void nfs41_sequence_done(struct nfs_client *clp,
334 struct nfs4_sequence_res *res,
337 unsigned long timestamp;
338 struct nfs4_slot_table *tbl;
339 struct nfs4_slot *slot;
342 * sr_status remains 1 if an RPC level error occurred. The server
343 * may or may not have processed the sequence operation..
344 * Proceed as if the server received and processed the sequence
347 if (res->sr_status == 1)
348 res->sr_status = NFS_OK;
350 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
351 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
354 tbl = &clp->cl_session->fc_slot_table;
355 slot = tbl->slots + res->sr_slotid;
357 if (res->sr_status == 0) {
358 /* Update the slot's sequence and clientid lease timer */
360 timestamp = res->sr_renewal_time;
361 spin_lock(&clp->cl_lock);
362 if (time_before(clp->cl_last_renewal, timestamp))
363 clp->cl_last_renewal = timestamp;
364 spin_unlock(&clp->cl_lock);
368 /* The session may be reset by one of the error handlers. */
369 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
370 nfs41_sequence_free_slot(clp, res);
374 * nfs4_find_slot - efficiently look for a free slot
376 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
377 * If found, we mark the slot as used, update the highest_used_slotid,
378 * and respectively set up the sequence operation args.
379 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
381 * Note: must be called with under the slot_tbl_lock.
384 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
387 u8 ret_id = NFS4_MAX_SLOT_TABLE;
388 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
390 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
391 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
393 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
394 if (slotid >= tbl->max_slots)
396 __set_bit(slotid, tbl->used_slots);
397 if (slotid > tbl->highest_used_slotid)
398 tbl->highest_used_slotid = slotid;
401 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
402 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
406 static int nfs41_setup_sequence(struct nfs4_session *session,
407 struct nfs4_sequence_args *args,
408 struct nfs4_sequence_res *res,
410 struct rpc_task *task)
412 struct nfs4_slot *slot;
413 struct nfs4_slot_table *tbl;
416 dprintk("--> %s\n", __func__);
417 /* slot already allocated? */
418 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
421 memset(res, 0, sizeof(*res));
422 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
423 tbl = &session->fc_slot_table;
425 spin_lock(&tbl->slot_tbl_lock);
426 slotid = nfs4_find_slot(tbl, task);
427 if (slotid == NFS4_MAX_SLOT_TABLE) {
428 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
429 spin_unlock(&tbl->slot_tbl_lock);
430 dprintk("<-- %s: no free slots\n", __func__);
433 spin_unlock(&tbl->slot_tbl_lock);
435 slot = tbl->slots + slotid;
436 args->sa_slotid = slotid;
437 args->sa_cache_this = cache_reply;
439 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
441 res->sr_slotid = slotid;
442 res->sr_renewal_time = jiffies;
444 * sr_status is only set in decode_sequence, and so will remain
445 * set to 1 if an rpc level failure occurs.
451 int nfs4_setup_sequence(struct nfs_client *clp,
452 struct nfs4_sequence_args *args,
453 struct nfs4_sequence_res *res,
455 struct rpc_task *task)
459 dprintk("--> %s clp %p session %p sr_slotid %d\n",
460 __func__, clp, clp->cl_session, res->sr_slotid);
462 if (!nfs4_has_session(clp))
464 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
466 if (ret != -EAGAIN) {
467 /* terminate rpc task */
468 task->tk_status = ret;
469 task->tk_action = NULL;
472 dprintk("<-- %s status=%d\n", __func__, ret);
476 struct nfs41_call_sync_data {
477 struct nfs_client *clp;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
483 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
485 struct nfs41_call_sync_data *data = calldata;
487 dprintk("--> %s data->clp->cl_session %p\n", __func__,
488 data->clp->cl_session);
489 if (nfs4_setup_sequence(data->clp, data->seq_args,
490 data->seq_res, data->cache_reply, task))
492 rpc_call_start(task);
495 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
497 struct nfs41_call_sync_data *data = calldata;
499 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
500 nfs41_sequence_free_slot(data->clp, data->seq_res);
503 struct rpc_call_ops nfs41_call_sync_ops = {
504 .rpc_call_prepare = nfs41_call_sync_prepare,
505 .rpc_call_done = nfs41_call_sync_done,
508 static int nfs4_call_sync_sequence(struct nfs_client *clp,
509 struct rpc_clnt *clnt,
510 struct rpc_message *msg,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
516 struct rpc_task *task;
517 struct nfs41_call_sync_data data = {
521 .cache_reply = cache_reply,
523 struct rpc_task_setup task_setup = {
526 .callback_ops = &nfs41_call_sync_ops,
527 .callback_data = &data
530 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
531 task = rpc_run_task(&task_setup);
535 ret = task->tk_status;
541 int _nfs4_call_sync_session(struct nfs_server *server,
542 struct rpc_message *msg,
543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
547 return nfs4_call_sync_sequence(server->nfs_client, server->client,
548 msg, args, res, cache_reply);
551 #endif /* CONFIG_NFS_V4_1 */
553 int _nfs4_call_sync(struct nfs_server *server,
554 struct rpc_message *msg,
555 struct nfs4_sequence_args *args,
556 struct nfs4_sequence_res *res,
559 args->sa_session = res->sr_session = NULL;
560 return rpc_call_sync(server->client, msg, 0);
563 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
564 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
565 &(res)->seq_res, (cache_reply))
567 static void nfs4_sequence_done(const struct nfs_server *server,
568 struct nfs4_sequence_res *res, int rpc_status)
570 #ifdef CONFIG_NFS_V4_1
571 if (nfs4_has_session(server->nfs_client))
572 nfs41_sequence_done(server->nfs_client, res, rpc_status);
573 #endif /* CONFIG_NFS_V4_1 */
576 /* no restart, therefore free slot here */
577 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
578 struct nfs4_sequence_res *res,
581 nfs4_sequence_done(server, res, rpc_status);
582 nfs4_sequence_free_slot(server->nfs_client, res);
585 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
587 struct nfs_inode *nfsi = NFS_I(dir);
589 spin_lock(&dir->i_lock);
590 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
591 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
592 nfs_force_lookup_revalidate(dir);
593 nfsi->change_attr = cinfo->after;
594 spin_unlock(&dir->i_lock);
597 struct nfs4_opendata {
599 struct nfs_openargs o_arg;
600 struct nfs_openres o_res;
601 struct nfs_open_confirmargs c_arg;
602 struct nfs_open_confirmres c_res;
603 struct nfs_fattr f_attr;
604 struct nfs_fattr dir_attr;
607 struct nfs4_state_owner *owner;
608 struct nfs4_state *state;
610 unsigned long timestamp;
611 unsigned int rpc_done : 1;
617 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
619 p->o_res.f_attr = &p->f_attr;
620 p->o_res.dir_attr = &p->dir_attr;
621 p->o_res.seqid = p->o_arg.seqid;
622 p->c_res.seqid = p->c_arg.seqid;
623 p->o_res.server = p->o_arg.server;
624 nfs_fattr_init(&p->f_attr);
625 nfs_fattr_init(&p->dir_attr);
628 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
629 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
630 const struct iattr *attrs)
632 struct dentry *parent = dget_parent(path->dentry);
633 struct inode *dir = parent->d_inode;
634 struct nfs_server *server = NFS_SERVER(dir);
635 struct nfs4_opendata *p;
637 p = kzalloc(sizeof(*p), GFP_KERNEL);
640 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
641 if (p->o_arg.seqid == NULL)
643 p->path.mnt = mntget(path->mnt);
644 p->path.dentry = dget(path->dentry);
647 atomic_inc(&sp->so_count);
648 p->o_arg.fh = NFS_FH(dir);
649 p->o_arg.open_flags = flags;
650 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
651 p->o_arg.clientid = server->nfs_client->cl_clientid;
652 p->o_arg.id = sp->so_owner_id.id;
653 p->o_arg.name = &p->path.dentry->d_name;
654 p->o_arg.server = server;
655 p->o_arg.bitmask = server->attr_bitmask;
656 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
657 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
658 if (flags & O_EXCL) {
659 u32 *s = (u32 *) p->o_arg.u.verifier.data;
662 } else if (flags & O_CREAT) {
663 p->o_arg.u.attrs = &p->attrs;
664 memcpy(&p->attrs, attrs, sizeof(p->attrs));
666 p->c_arg.fh = &p->o_res.fh;
667 p->c_arg.stateid = &p->o_res.stateid;
668 p->c_arg.seqid = p->o_arg.seqid;
669 nfs4_init_opendata_res(p);
679 static void nfs4_opendata_free(struct kref *kref)
681 struct nfs4_opendata *p = container_of(kref,
682 struct nfs4_opendata, kref);
684 nfs_free_seqid(p->o_arg.seqid);
685 if (p->state != NULL)
686 nfs4_put_open_state(p->state);
687 nfs4_put_state_owner(p->owner);
693 static void nfs4_opendata_put(struct nfs4_opendata *p)
696 kref_put(&p->kref, nfs4_opendata_free);
699 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
703 ret = rpc_wait_for_completion_task(task);
707 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
711 if (open_mode & O_EXCL)
713 switch (mode & (FMODE_READ|FMODE_WRITE)) {
715 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
718 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
720 case FMODE_READ|FMODE_WRITE:
721 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
727 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
729 if ((delegation->type & fmode) != fmode)
731 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
733 nfs_mark_delegation_referenced(delegation);
737 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
746 case FMODE_READ|FMODE_WRITE:
749 nfs4_state_set_mode_locked(state, state->state | fmode);
752 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
754 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
755 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
756 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
759 set_bit(NFS_O_RDONLY_STATE, &state->flags);
762 set_bit(NFS_O_WRONLY_STATE, &state->flags);
764 case FMODE_READ|FMODE_WRITE:
765 set_bit(NFS_O_RDWR_STATE, &state->flags);
769 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
771 write_seqlock(&state->seqlock);
772 nfs_set_open_stateid_locked(state, stateid, fmode);
773 write_sequnlock(&state->seqlock);
776 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
779 * Protect the call to nfs4_state_set_mode_locked and
780 * serialise the stateid update
782 write_seqlock(&state->seqlock);
783 if (deleg_stateid != NULL) {
784 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
785 set_bit(NFS_DELEGATED_STATE, &state->flags);
787 if (open_stateid != NULL)
788 nfs_set_open_stateid_locked(state, open_stateid, fmode);
789 write_sequnlock(&state->seqlock);
790 spin_lock(&state->owner->so_lock);
791 update_open_stateflags(state, fmode);
792 spin_unlock(&state->owner->so_lock);
795 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
797 struct nfs_inode *nfsi = NFS_I(state->inode);
798 struct nfs_delegation *deleg_cur;
801 fmode &= (FMODE_READ|FMODE_WRITE);
804 deleg_cur = rcu_dereference(nfsi->delegation);
805 if (deleg_cur == NULL)
808 spin_lock(&deleg_cur->lock);
809 if (nfsi->delegation != deleg_cur ||
810 (deleg_cur->type & fmode) != fmode)
811 goto no_delegation_unlock;
813 if (delegation == NULL)
814 delegation = &deleg_cur->stateid;
815 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
816 goto no_delegation_unlock;
818 nfs_mark_delegation_referenced(deleg_cur);
819 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
821 no_delegation_unlock:
822 spin_unlock(&deleg_cur->lock);
826 if (!ret && open_stateid != NULL) {
827 __update_open_stateid(state, open_stateid, NULL, fmode);
835 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
837 struct nfs_delegation *delegation;
840 delegation = rcu_dereference(NFS_I(inode)->delegation);
841 if (delegation == NULL || (delegation->type & fmode) == fmode) {
846 nfs_inode_return_delegation(inode);
849 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
851 struct nfs4_state *state = opendata->state;
852 struct nfs_inode *nfsi = NFS_I(state->inode);
853 struct nfs_delegation *delegation;
854 int open_mode = opendata->o_arg.open_flags & O_EXCL;
855 fmode_t fmode = opendata->o_arg.fmode;
856 nfs4_stateid stateid;
860 if (can_open_cached(state, fmode, open_mode)) {
861 spin_lock(&state->owner->so_lock);
862 if (can_open_cached(state, fmode, open_mode)) {
863 update_open_stateflags(state, fmode);
864 spin_unlock(&state->owner->so_lock);
865 goto out_return_state;
867 spin_unlock(&state->owner->so_lock);
870 delegation = rcu_dereference(nfsi->delegation);
871 if (delegation == NULL ||
872 !can_open_delegated(delegation, fmode)) {
876 /* Save the delegation */
877 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
879 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
884 /* Try to update the stateid using the delegation */
885 if (update_open_stateid(state, NULL, &stateid, fmode))
886 goto out_return_state;
891 atomic_inc(&state->count);
895 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
898 struct nfs4_state *state = NULL;
899 struct nfs_delegation *delegation;
902 if (!data->rpc_done) {
903 state = nfs4_try_open_cached(data);
908 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
910 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
911 ret = PTR_ERR(inode);
915 state = nfs4_get_open_state(inode, data->owner);
918 if (data->o_res.delegation_type != 0) {
919 int delegation_flags = 0;
922 delegation = rcu_dereference(NFS_I(inode)->delegation);
924 delegation_flags = delegation->flags;
926 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
927 nfs_inode_set_delegation(state->inode,
928 data->owner->so_cred,
931 nfs_inode_reclaim_delegation(state->inode,
932 data->owner->so_cred,
936 update_open_stateid(state, &data->o_res.stateid, NULL,
947 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
949 struct nfs_inode *nfsi = NFS_I(state->inode);
950 struct nfs_open_context *ctx;
952 spin_lock(&state->inode->i_lock);
953 list_for_each_entry(ctx, &nfsi->open_files, list) {
954 if (ctx->state != state)
956 get_nfs_open_context(ctx);
957 spin_unlock(&state->inode->i_lock);
960 spin_unlock(&state->inode->i_lock);
961 return ERR_PTR(-ENOENT);
964 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
966 struct nfs4_opendata *opendata;
968 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
969 if (opendata == NULL)
970 return ERR_PTR(-ENOMEM);
971 opendata->state = state;
972 atomic_inc(&state->count);
976 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
978 struct nfs4_state *newstate;
981 opendata->o_arg.open_flags = 0;
982 opendata->o_arg.fmode = fmode;
983 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
984 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
985 nfs4_init_opendata_res(opendata);
986 ret = _nfs4_proc_open(opendata);
989 newstate = nfs4_opendata_to_nfs4_state(opendata);
990 if (IS_ERR(newstate))
991 return PTR_ERR(newstate);
992 nfs4_close_state(&opendata->path, newstate, fmode);
997 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
999 struct nfs4_state *newstate;
1002 /* memory barrier prior to reading state->n_* */
1003 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1005 if (state->n_rdwr != 0) {
1006 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1009 if (newstate != state)
1012 if (state->n_wronly != 0) {
1013 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1016 if (newstate != state)
1019 if (state->n_rdonly != 0) {
1020 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1023 if (newstate != state)
1027 * We may have performed cached opens for all three recoveries.
1028 * Check if we need to update the current stateid.
1030 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1031 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1032 write_seqlock(&state->seqlock);
1033 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1034 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1035 write_sequnlock(&state->seqlock);
1042 * reclaim state on the server after a reboot.
1044 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1046 struct nfs_delegation *delegation;
1047 struct nfs4_opendata *opendata;
1048 fmode_t delegation_type = 0;
1051 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1052 if (IS_ERR(opendata))
1053 return PTR_ERR(opendata);
1054 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1055 opendata->o_arg.fh = NFS_FH(state->inode);
1057 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1058 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1059 delegation_type = delegation->type;
1061 opendata->o_arg.u.delegation_type = delegation_type;
1062 status = nfs4_open_recover(opendata, state);
1063 nfs4_opendata_put(opendata);
1067 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1069 struct nfs_server *server = NFS_SERVER(state->inode);
1070 struct nfs4_exception exception = { };
1073 err = _nfs4_do_open_reclaim(ctx, state);
1074 if (err != -NFS4ERR_DELAY)
1076 nfs4_handle_exception(server, err, &exception);
1077 } while (exception.retry);
1081 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1083 struct nfs_open_context *ctx;
1086 ctx = nfs4_state_find_open_context(state);
1088 return PTR_ERR(ctx);
1089 ret = nfs4_do_open_reclaim(ctx, state);
1090 put_nfs_open_context(ctx);
1094 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1096 struct nfs4_opendata *opendata;
1099 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1100 if (IS_ERR(opendata))
1101 return PTR_ERR(opendata);
1102 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1103 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1104 sizeof(opendata->o_arg.u.delegation.data));
1105 ret = nfs4_open_recover(opendata, state);
1106 nfs4_opendata_put(opendata);
1110 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1112 struct nfs4_exception exception = { };
1113 struct nfs_server *server = NFS_SERVER(state->inode);
1116 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1120 case -NFS4ERR_STALE_CLIENTID:
1121 case -NFS4ERR_STALE_STATEID:
1122 case -NFS4ERR_EXPIRED:
1123 /* Don't recall a delegation if it was lost */
1124 nfs4_schedule_state_recovery(server->nfs_client);
1127 err = nfs4_handle_exception(server, err, &exception);
1128 } while (exception.retry);
1132 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1134 struct nfs4_opendata *data = calldata;
1136 data->rpc_status = task->tk_status;
1137 if (RPC_ASSASSINATED(task))
1139 if (data->rpc_status == 0) {
1140 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1141 sizeof(data->o_res.stateid.data));
1142 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1143 renew_lease(data->o_res.server, data->timestamp);
1148 static void nfs4_open_confirm_release(void *calldata)
1150 struct nfs4_opendata *data = calldata;
1151 struct nfs4_state *state = NULL;
1153 /* If this request hasn't been cancelled, do nothing */
1154 if (data->cancelled == 0)
1156 /* In case of error, no cleanup! */
1157 if (!data->rpc_done)
1159 state = nfs4_opendata_to_nfs4_state(data);
1161 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1163 nfs4_opendata_put(data);
1166 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1167 .rpc_call_done = nfs4_open_confirm_done,
1168 .rpc_release = nfs4_open_confirm_release,
1172 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1174 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1176 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1177 struct rpc_task *task;
1178 struct rpc_message msg = {
1179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1180 .rpc_argp = &data->c_arg,
1181 .rpc_resp = &data->c_res,
1182 .rpc_cred = data->owner->so_cred,
1184 struct rpc_task_setup task_setup_data = {
1185 .rpc_client = server->client,
1186 .rpc_message = &msg,
1187 .callback_ops = &nfs4_open_confirm_ops,
1188 .callback_data = data,
1189 .workqueue = nfsiod_workqueue,
1190 .flags = RPC_TASK_ASYNC,
1194 kref_get(&data->kref);
1196 data->rpc_status = 0;
1197 data->timestamp = jiffies;
1198 task = rpc_run_task(&task_setup_data);
1200 return PTR_ERR(task);
1201 status = nfs4_wait_for_completion_rpc_task(task);
1203 data->cancelled = 1;
1206 status = data->rpc_status;
1211 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1213 struct nfs4_opendata *data = calldata;
1214 struct nfs4_state_owner *sp = data->owner;
1216 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1219 * Check if we still need to send an OPEN call, or if we can use
1220 * a delegation instead.
1222 if (data->state != NULL) {
1223 struct nfs_delegation *delegation;
1225 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1228 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1229 if (delegation != NULL &&
1230 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1236 /* Update sequence id. */
1237 data->o_arg.id = sp->so_owner_id.id;
1238 data->o_arg.clientid = sp->so_client->cl_clientid;
1239 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1240 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1241 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1243 data->timestamp = jiffies;
1244 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1245 &data->o_arg.seq_args,
1246 &data->o_res.seq_res, 1, task))
1248 rpc_call_start(task);
1251 task->tk_action = NULL;
1255 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1257 struct nfs4_opendata *data = calldata;
1259 data->rpc_status = task->tk_status;
1261 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1264 if (RPC_ASSASSINATED(task))
1266 if (task->tk_status == 0) {
1267 switch (data->o_res.f_attr->mode & S_IFMT) {
1271 data->rpc_status = -ELOOP;
1274 data->rpc_status = -EISDIR;
1277 data->rpc_status = -ENOTDIR;
1279 renew_lease(data->o_res.server, data->timestamp);
1280 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1281 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1286 static void nfs4_open_release(void *calldata)
1288 struct nfs4_opendata *data = calldata;
1289 struct nfs4_state *state = NULL;
1291 /* If this request hasn't been cancelled, do nothing */
1292 if (data->cancelled == 0)
1294 /* In case of error, no cleanup! */
1295 if (data->rpc_status != 0 || !data->rpc_done)
1297 /* In case we need an open_confirm, no cleanup! */
1298 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1300 state = nfs4_opendata_to_nfs4_state(data);
1302 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1304 nfs4_opendata_put(data);
1307 static const struct rpc_call_ops nfs4_open_ops = {
1308 .rpc_call_prepare = nfs4_open_prepare,
1309 .rpc_call_done = nfs4_open_done,
1310 .rpc_release = nfs4_open_release,
1314 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1316 static int _nfs4_proc_open(struct nfs4_opendata *data)
1318 struct inode *dir = data->dir->d_inode;
1319 struct nfs_server *server = NFS_SERVER(dir);
1320 struct nfs_openargs *o_arg = &data->o_arg;
1321 struct nfs_openres *o_res = &data->o_res;
1322 struct rpc_task *task;
1323 struct rpc_message msg = {
1324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1327 .rpc_cred = data->owner->so_cred,
1329 struct rpc_task_setup task_setup_data = {
1330 .rpc_client = server->client,
1331 .rpc_message = &msg,
1332 .callback_ops = &nfs4_open_ops,
1333 .callback_data = data,
1334 .workqueue = nfsiod_workqueue,
1335 .flags = RPC_TASK_ASYNC,
1339 kref_get(&data->kref);
1341 data->rpc_status = 0;
1342 data->cancelled = 0;
1343 task = rpc_run_task(&task_setup_data);
1345 return PTR_ERR(task);
1346 status = nfs4_wait_for_completion_rpc_task(task);
1348 data->cancelled = 1;
1351 status = data->rpc_status;
1353 if (status != 0 || !data->rpc_done)
1356 if (o_res->fh.size == 0)
1357 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1359 if (o_arg->open_flags & O_CREAT) {
1360 update_changeattr(dir, &o_res->cinfo);
1361 nfs_post_op_update_inode(dir, o_res->dir_attr);
1363 nfs_refresh_inode(dir, o_res->dir_attr);
1364 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1365 status = _nfs4_proc_open_confirm(data);
1369 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1370 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1374 static int nfs4_recover_expired_lease(struct nfs_server *server)
1376 struct nfs_client *clp = server->nfs_client;
1380 ret = nfs4_wait_clnt_recover(clp);
1383 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1384 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1386 nfs4_schedule_state_recovery(clp);
1393 * reclaim state on the server after a network partition.
1394 * Assumes caller holds the appropriate lock
1396 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1398 struct nfs4_opendata *opendata;
1401 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1402 if (IS_ERR(opendata))
1403 return PTR_ERR(opendata);
1404 ret = nfs4_open_recover(opendata, state);
1406 d_drop(ctx->path.dentry);
1407 nfs4_opendata_put(opendata);
1411 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1413 struct nfs_server *server = NFS_SERVER(state->inode);
1414 struct nfs4_exception exception = { };
1418 err = _nfs4_open_expired(ctx, state);
1419 if (err != -NFS4ERR_DELAY)
1421 nfs4_handle_exception(server, err, &exception);
1422 } while (exception.retry);
1426 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1428 struct nfs_open_context *ctx;
1431 ctx = nfs4_state_find_open_context(state);
1433 return PTR_ERR(ctx);
1434 ret = nfs4_do_open_expired(ctx, state);
1435 put_nfs_open_context(ctx);
1440 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1441 * fields corresponding to attributes that were used to store the verifier.
1442 * Make sure we clobber those fields in the later setattr call
1444 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1446 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1447 !(sattr->ia_valid & ATTR_ATIME_SET))
1448 sattr->ia_valid |= ATTR_ATIME;
1450 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1451 !(sattr->ia_valid & ATTR_MTIME_SET))
1452 sattr->ia_valid |= ATTR_MTIME;
1456 * Returns a referenced nfs4_state
1458 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)
1460 struct nfs4_state_owner *sp;
1461 struct nfs4_state *state = NULL;
1462 struct nfs_server *server = NFS_SERVER(dir);
1463 struct nfs4_opendata *opendata;
1466 /* Protect against reboot recovery conflicts */
1468 if (!(sp = nfs4_get_state_owner(server, cred))) {
1469 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1472 status = nfs4_recover_expired_lease(server);
1474 goto err_put_state_owner;
1475 if (path->dentry->d_inode != NULL)
1476 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1478 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1479 if (opendata == NULL)
1480 goto err_put_state_owner;
1482 if (path->dentry->d_inode != NULL)
1483 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1485 status = _nfs4_proc_open(opendata);
1487 goto err_opendata_put;
1489 if (opendata->o_arg.open_flags & O_EXCL)
1490 nfs4_exclusive_attrset(opendata, sattr);
1492 state = nfs4_opendata_to_nfs4_state(opendata);
1493 status = PTR_ERR(state);
1495 goto err_opendata_put;
1496 nfs4_opendata_put(opendata);
1497 nfs4_put_state_owner(sp);
1501 nfs4_opendata_put(opendata);
1502 err_put_state_owner:
1503 nfs4_put_state_owner(sp);
1510 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)
1512 struct nfs4_exception exception = { };
1513 struct nfs4_state *res;
1517 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1520 /* NOTE: BAD_SEQID means the server and client disagree about the
1521 * book-keeping w.r.t. state-changing operations
1522 * (OPEN/CLOSE/LOCK/LOCKU...)
1523 * It is actually a sign of a bug on the client or on the server.
1525 * If we receive a BAD_SEQID error in the particular case of
1526 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1527 * have unhashed the old state_owner for us, and that we can
1528 * therefore safely retry using a new one. We should still warn
1529 * the user though...
1531 if (status == -NFS4ERR_BAD_SEQID) {
1532 printk(KERN_WARNING "NFS: v4 server %s "
1533 " returned a bad sequence-id error!\n",
1534 NFS_SERVER(dir)->nfs_client->cl_hostname);
1535 exception.retry = 1;
1539 * BAD_STATEID on OPEN means that the server cancelled our
1540 * state before it received the OPEN_CONFIRM.
1541 * Recover by retrying the request as per the discussion
1542 * on Page 181 of RFC3530.
1544 if (status == -NFS4ERR_BAD_STATEID) {
1545 exception.retry = 1;
1548 if (status == -EAGAIN) {
1549 /* We must have found a delegation */
1550 exception.retry = 1;
1553 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1554 status, &exception));
1555 } while (exception.retry);
1559 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1560 struct nfs_fattr *fattr, struct iattr *sattr,
1561 struct nfs4_state *state)
1563 struct nfs_server *server = NFS_SERVER(inode);
1564 struct nfs_setattrargs arg = {
1565 .fh = NFS_FH(inode),
1568 .bitmask = server->attr_bitmask,
1570 struct nfs_setattrres res = {
1574 struct rpc_message msg = {
1575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1580 unsigned long timestamp = jiffies;
1583 nfs_fattr_init(fattr);
1585 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1586 /* Use that stateid */
1587 } else if (state != NULL) {
1588 nfs4_copy_stateid(&arg.stateid, state, current->files);
1590 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1592 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1593 if (status == 0 && state != NULL)
1594 renew_lease(server, timestamp);
1598 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1599 struct nfs_fattr *fattr, struct iattr *sattr,
1600 struct nfs4_state *state)
1602 struct nfs_server *server = NFS_SERVER(inode);
1603 struct nfs4_exception exception = { };
1606 err = nfs4_handle_exception(server,
1607 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1609 } while (exception.retry);
1613 struct nfs4_closedata {
1615 struct inode *inode;
1616 struct nfs4_state *state;
1617 struct nfs_closeargs arg;
1618 struct nfs_closeres res;
1619 struct nfs_fattr fattr;
1620 unsigned long timestamp;
1623 static void nfs4_free_closedata(void *data)
1625 struct nfs4_closedata *calldata = data;
1626 struct nfs4_state_owner *sp = calldata->state->owner;
1628 nfs4_put_open_state(calldata->state);
1629 nfs_free_seqid(calldata->arg.seqid);
1630 nfs4_put_state_owner(sp);
1631 path_put(&calldata->path);
1635 static void nfs4_close_done(struct rpc_task *task, void *data)
1637 struct nfs4_closedata *calldata = data;
1638 struct nfs4_state *state = calldata->state;
1639 struct nfs_server *server = NFS_SERVER(calldata->inode);
1641 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1642 if (RPC_ASSASSINATED(task))
1644 /* hmm. we are done with the inode, and in the process of freeing
1645 * the state_owner. we keep this around to process errors
1647 switch (task->tk_status) {
1649 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1650 renew_lease(server, calldata->timestamp);
1652 case -NFS4ERR_STALE_STATEID:
1653 case -NFS4ERR_OLD_STATEID:
1654 case -NFS4ERR_BAD_STATEID:
1655 case -NFS4ERR_EXPIRED:
1656 if (calldata->arg.fmode == 0)
1659 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1660 rpc_restart_call(task);
1664 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1665 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1668 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1670 struct nfs4_closedata *calldata = data;
1671 struct nfs4_state *state = calldata->state;
1672 int clear_rd, clear_wr, clear_rdwr;
1674 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1677 clear_rd = clear_wr = clear_rdwr = 0;
1678 spin_lock(&state->owner->so_lock);
1679 /* Calculate the change in open mode */
1680 if (state->n_rdwr == 0) {
1681 if (state->n_rdonly == 0) {
1682 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1683 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1685 if (state->n_wronly == 0) {
1686 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1687 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1690 spin_unlock(&state->owner->so_lock);
1691 if (!clear_rd && !clear_wr && !clear_rdwr) {
1692 /* Note: exit _without_ calling nfs4_close_done */
1693 task->tk_action = NULL;
1696 nfs_fattr_init(calldata->res.fattr);
1697 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1698 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1699 calldata->arg.fmode = FMODE_READ;
1700 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1701 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1702 calldata->arg.fmode = FMODE_WRITE;
1704 calldata->timestamp = jiffies;
1705 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1706 &calldata->arg.seq_args, &calldata->res.seq_res,
1709 rpc_call_start(task);
1712 static const struct rpc_call_ops nfs4_close_ops = {
1713 .rpc_call_prepare = nfs4_close_prepare,
1714 .rpc_call_done = nfs4_close_done,
1715 .rpc_release = nfs4_free_closedata,
1719 * It is possible for data to be read/written from a mem-mapped file
1720 * after the sys_close call (which hits the vfs layer as a flush).
1721 * This means that we can't safely call nfsv4 close on a file until
1722 * the inode is cleared. This in turn means that we are not good
1723 * NFSv4 citizens - we do not indicate to the server to update the file's
1724 * share state even when we are done with one of the three share
1725 * stateid's in the inode.
1727 * NOTE: Caller must be holding the sp->so_owner semaphore!
1729 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1731 struct nfs_server *server = NFS_SERVER(state->inode);
1732 struct nfs4_closedata *calldata;
1733 struct nfs4_state_owner *sp = state->owner;
1734 struct rpc_task *task;
1735 struct rpc_message msg = {
1736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1737 .rpc_cred = state->owner->so_cred,
1739 struct rpc_task_setup task_setup_data = {
1740 .rpc_client = server->client,
1741 .rpc_message = &msg,
1742 .callback_ops = &nfs4_close_ops,
1743 .workqueue = nfsiod_workqueue,
1744 .flags = RPC_TASK_ASYNC,
1746 int status = -ENOMEM;
1748 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1749 if (calldata == NULL)
1751 calldata->inode = state->inode;
1752 calldata->state = state;
1753 calldata->arg.fh = NFS_FH(state->inode);
1754 calldata->arg.stateid = &state->open_stateid;
1755 /* Serialization for the sequence id */
1756 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1757 if (calldata->arg.seqid == NULL)
1758 goto out_free_calldata;
1759 calldata->arg.fmode = 0;
1760 calldata->arg.bitmask = server->cache_consistency_bitmask;
1761 calldata->res.fattr = &calldata->fattr;
1762 calldata->res.seqid = calldata->arg.seqid;
1763 calldata->res.server = server;
1764 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1765 calldata->path.mnt = mntget(path->mnt);
1766 calldata->path.dentry = dget(path->dentry);
1768 msg.rpc_argp = &calldata->arg,
1769 msg.rpc_resp = &calldata->res,
1770 task_setup_data.callback_data = calldata;
1771 task = rpc_run_task(&task_setup_data);
1773 return PTR_ERR(task);
1776 status = rpc_wait_for_completion_task(task);
1782 nfs4_put_open_state(state);
1783 nfs4_put_state_owner(sp);
1787 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1792 /* If the open_intent is for execute, we have an extra check to make */
1793 if (fmode & FMODE_EXEC) {
1794 ret = nfs_may_open(state->inode,
1795 state->owner->so_cred,
1796 nd->intent.open.flags);
1800 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1801 if (!IS_ERR(filp)) {
1802 struct nfs_open_context *ctx;
1803 ctx = nfs_file_open_context(filp);
1807 ret = PTR_ERR(filp);
1809 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1814 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1816 struct path path = {
1817 .mnt = nd->path.mnt,
1820 struct dentry *parent;
1822 struct rpc_cred *cred;
1823 struct nfs4_state *state;
1825 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1827 if (nd->flags & LOOKUP_CREATE) {
1828 attr.ia_mode = nd->intent.open.create_mode;
1829 attr.ia_valid = ATTR_MODE;
1830 if (!IS_POSIXACL(dir))
1831 attr.ia_mode &= ~current_umask();
1834 BUG_ON(nd->intent.open.flags & O_CREAT);
1837 cred = rpc_lookup_cred();
1839 return (struct dentry *)cred;
1840 parent = dentry->d_parent;
1841 /* Protect against concurrent sillydeletes */
1842 nfs_block_sillyrename(parent);
1843 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1845 if (IS_ERR(state)) {
1846 if (PTR_ERR(state) == -ENOENT) {
1847 d_add(dentry, NULL);
1848 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1850 nfs_unblock_sillyrename(parent);
1851 return (struct dentry *)state;
1853 res = d_add_unique(dentry, igrab(state->inode));
1856 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1857 nfs_unblock_sillyrename(parent);
1858 nfs4_intent_set_file(nd, &path, state, fmode);
1863 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1865 struct path path = {
1866 .mnt = nd->path.mnt,
1869 struct rpc_cred *cred;
1870 struct nfs4_state *state;
1871 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1873 cred = rpc_lookup_cred();
1875 return PTR_ERR(cred);
1876 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1878 if (IS_ERR(state)) {
1879 switch (PTR_ERR(state)) {
1885 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1891 if (state->inode == dentry->d_inode) {
1892 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1893 nfs4_intent_set_file(nd, &path, state, fmode);
1896 nfs4_close_sync(&path, state, fmode);
1902 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1904 if (ctx->state == NULL)
1907 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1909 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1912 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1914 struct nfs4_server_caps_arg args = {
1917 struct nfs4_server_caps_res res = {};
1918 struct rpc_message msg = {
1919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1925 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1927 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1928 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1929 server->caps |= NFS_CAP_ACLS;
1930 if (res.has_links != 0)
1931 server->caps |= NFS_CAP_HARDLINKS;
1932 if (res.has_symlinks != 0)
1933 server->caps |= NFS_CAP_SYMLINKS;
1934 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1935 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1936 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1937 server->acl_bitmask = res.acl_bitmask;
1943 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1945 struct nfs4_exception exception = { };
1948 err = nfs4_handle_exception(server,
1949 _nfs4_server_capabilities(server, fhandle),
1951 } while (exception.retry);
1955 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1956 struct nfs_fsinfo *info)
1958 struct nfs4_lookup_root_arg args = {
1959 .bitmask = nfs4_fattr_bitmap,
1961 struct nfs4_lookup_res res = {
1963 .fattr = info->fattr,
1966 struct rpc_message msg = {
1967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1971 nfs_fattr_init(info->fattr);
1972 return nfs4_call_sync(server, &msg, &args, &res, 0);
1975 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1976 struct nfs_fsinfo *info)
1978 struct nfs4_exception exception = { };
1981 err = nfs4_handle_exception(server,
1982 _nfs4_lookup_root(server, fhandle, info),
1984 } while (exception.retry);
1989 * get the file handle for the "/" directory on the server
1991 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1992 struct nfs_fsinfo *info)
1996 status = nfs4_lookup_root(server, fhandle, info);
1998 status = nfs4_server_capabilities(server, fhandle);
2000 status = nfs4_do_fsinfo(server, fhandle, info);
2001 return nfs4_map_errors(status);
2005 * Get locations and (maybe) other attributes of a referral.
2006 * Note that we'll actually follow the referral later when
2007 * we detect fsid mismatch in inode revalidation
2009 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2011 int status = -ENOMEM;
2012 struct page *page = NULL;
2013 struct nfs4_fs_locations *locations = NULL;
2015 page = alloc_page(GFP_KERNEL);
2018 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2019 if (locations == NULL)
2022 status = nfs4_proc_fs_locations(dir, name, locations, page);
2025 /* Make sure server returned a different fsid for the referral */
2026 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2027 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2032 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2033 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2035 fattr->mode = S_IFDIR;
2036 memset(fhandle, 0, sizeof(struct nfs_fh));
2045 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2047 struct nfs4_getattr_arg args = {
2049 .bitmask = server->attr_bitmask,
2051 struct nfs4_getattr_res res = {
2055 struct rpc_message msg = {
2056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2061 nfs_fattr_init(fattr);
2062 return nfs4_call_sync(server, &msg, &args, &res, 0);
2065 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2067 struct nfs4_exception exception = { };
2070 err = nfs4_handle_exception(server,
2071 _nfs4_proc_getattr(server, fhandle, fattr),
2073 } while (exception.retry);
2078 * The file is not closed if it is opened due to the a request to change
2079 * the size of the file. The open call will not be needed once the
2080 * VFS layer lookup-intents are implemented.
2082 * Close is called when the inode is destroyed.
2083 * If we haven't opened the file for O_WRONLY, we
2084 * need to in the size_change case to obtain a stateid.
2087 * Because OPEN is always done by name in nfsv4, it is
2088 * possible that we opened a different file by the same
2089 * name. We can recognize this race condition, but we
2090 * can't do anything about it besides returning an error.
2092 * This will be fixed with VFS changes (lookup-intent).
2095 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2096 struct iattr *sattr)
2098 struct inode *inode = dentry->d_inode;
2099 struct rpc_cred *cred = NULL;
2100 struct nfs4_state *state = NULL;
2103 nfs_fattr_init(fattr);
2105 /* Search for an existing open(O_WRITE) file */
2106 if (sattr->ia_valid & ATTR_FILE) {
2107 struct nfs_open_context *ctx;
2109 ctx = nfs_file_open_context(sattr->ia_file);
2116 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2118 nfs_setattr_update_inode(inode, sattr);
2122 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2123 const struct qstr *name, struct nfs_fh *fhandle,
2124 struct nfs_fattr *fattr)
2127 struct nfs4_lookup_arg args = {
2128 .bitmask = server->attr_bitmask,
2132 struct nfs4_lookup_res res = {
2137 struct rpc_message msg = {
2138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2143 nfs_fattr_init(fattr);
2145 dprintk("NFS call lookupfh %s\n", name->name);
2146 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2147 dprintk("NFS reply lookupfh: %d\n", status);
2151 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2152 struct qstr *name, struct nfs_fh *fhandle,
2153 struct nfs_fattr *fattr)
2155 struct nfs4_exception exception = { };
2158 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2160 if (err == -NFS4ERR_MOVED) {
2164 err = nfs4_handle_exception(server, err, &exception);
2165 } while (exception.retry);
2169 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2170 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2174 dprintk("NFS call lookup %s\n", name->name);
2175 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2176 if (status == -NFS4ERR_MOVED)
2177 status = nfs4_get_referral(dir, name, fattr, fhandle);
2178 dprintk("NFS reply lookup: %d\n", status);
2182 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2184 struct nfs4_exception exception = { };
2187 err = nfs4_handle_exception(NFS_SERVER(dir),
2188 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2190 } while (exception.retry);
2194 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2196 struct nfs_server *server = NFS_SERVER(inode);
2197 struct nfs_fattr fattr;
2198 struct nfs4_accessargs args = {
2199 .fh = NFS_FH(inode),
2200 .bitmask = server->attr_bitmask,
2202 struct nfs4_accessres res = {
2206 struct rpc_message msg = {
2207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2210 .rpc_cred = entry->cred,
2212 int mode = entry->mask;
2216 * Determine which access bits we want to ask for...
2218 if (mode & MAY_READ)
2219 args.access |= NFS4_ACCESS_READ;
2220 if (S_ISDIR(inode->i_mode)) {
2221 if (mode & MAY_WRITE)
2222 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2223 if (mode & MAY_EXEC)
2224 args.access |= NFS4_ACCESS_LOOKUP;
2226 if (mode & MAY_WRITE)
2227 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2228 if (mode & MAY_EXEC)
2229 args.access |= NFS4_ACCESS_EXECUTE;
2231 nfs_fattr_init(&fattr);
2232 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2235 if (res.access & NFS4_ACCESS_READ)
2236 entry->mask |= MAY_READ;
2237 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2238 entry->mask |= MAY_WRITE;
2239 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2240 entry->mask |= MAY_EXEC;
2241 nfs_refresh_inode(inode, &fattr);
2246 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2248 struct nfs4_exception exception = { };
2251 err = nfs4_handle_exception(NFS_SERVER(inode),
2252 _nfs4_proc_access(inode, entry),
2254 } while (exception.retry);
2259 * TODO: For the time being, we don't try to get any attributes
2260 * along with any of the zero-copy operations READ, READDIR,
2263 * In the case of the first three, we want to put the GETATTR
2264 * after the read-type operation -- this is because it is hard
2265 * to predict the length of a GETATTR response in v4, and thus
2266 * align the READ data correctly. This means that the GETATTR
2267 * may end up partially falling into the page cache, and we should
2268 * shift it into the 'tail' of the xdr_buf before processing.
2269 * To do this efficiently, we need to know the total length
2270 * of data received, which doesn't seem to be available outside
2273 * In the case of WRITE, we also want to put the GETATTR after
2274 * the operation -- in this case because we want to make sure
2275 * we get the post-operation mtime and size. This means that
2276 * we can't use xdr_encode_pages() as written: we need a variant
2277 * of it which would leave room in the 'tail' iovec.
2279 * Both of these changes to the XDR layer would in fact be quite
2280 * minor, but I decided to leave them for a subsequent patch.
2282 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2283 unsigned int pgbase, unsigned int pglen)
2285 struct nfs4_readlink args = {
2286 .fh = NFS_FH(inode),
2291 struct nfs4_readlink_res res;
2292 struct rpc_message msg = {
2293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2298 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2301 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2302 unsigned int pgbase, unsigned int pglen)
2304 struct nfs4_exception exception = { };
2307 err = nfs4_handle_exception(NFS_SERVER(inode),
2308 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2310 } while (exception.retry);
2316 * We will need to arrange for the VFS layer to provide an atomic open.
2317 * Until then, this create/open method is prone to inefficiency and race
2318 * conditions due to the lookup, create, and open VFS calls from sys_open()
2319 * placed on the wire.
2321 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2322 * The file will be opened again in the subsequent VFS open call
2323 * (nfs4_proc_file_open).
2325 * The open for read will just hang around to be used by any process that
2326 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2330 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2331 int flags, struct nameidata *nd)
2333 struct path path = {
2334 .mnt = nd->path.mnt,
2337 struct nfs4_state *state;
2338 struct rpc_cred *cred;
2339 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2342 cred = rpc_lookup_cred();
2344 status = PTR_ERR(cred);
2347 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2349 if (IS_ERR(state)) {
2350 status = PTR_ERR(state);
2353 d_add(dentry, igrab(state->inode));
2354 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2355 if (flags & O_EXCL) {
2356 struct nfs_fattr fattr;
2357 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2359 nfs_setattr_update_inode(state->inode, sattr);
2360 nfs_post_op_update_inode(state->inode, &fattr);
2362 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2363 status = nfs4_intent_set_file(nd, &path, state, fmode);
2365 nfs4_close_sync(&path, state, fmode);
2372 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2374 struct nfs_server *server = NFS_SERVER(dir);
2375 struct nfs_removeargs args = {
2377 .name.len = name->len,
2378 .name.name = name->name,
2379 .bitmask = server->attr_bitmask,
2381 struct nfs_removeres res = {
2384 struct rpc_message msg = {
2385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2391 nfs_fattr_init(&res.dir_attr);
2392 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2394 update_changeattr(dir, &res.cinfo);
2395 nfs_post_op_update_inode(dir, &res.dir_attr);
2400 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2402 struct nfs4_exception exception = { };
2405 err = nfs4_handle_exception(NFS_SERVER(dir),
2406 _nfs4_proc_remove(dir, name),
2408 } while (exception.retry);
2412 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2414 struct nfs_server *server = NFS_SERVER(dir);
2415 struct nfs_removeargs *args = msg->rpc_argp;
2416 struct nfs_removeres *res = msg->rpc_resp;
2418 args->bitmask = server->cache_consistency_bitmask;
2419 res->server = server;
2420 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2423 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2425 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2427 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2428 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2430 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2431 update_changeattr(dir, &res->cinfo);
2432 nfs_post_op_update_inode(dir, &res->dir_attr);
2436 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2437 struct inode *new_dir, struct qstr *new_name)
2439 struct nfs_server *server = NFS_SERVER(old_dir);
2440 struct nfs4_rename_arg arg = {
2441 .old_dir = NFS_FH(old_dir),
2442 .new_dir = NFS_FH(new_dir),
2443 .old_name = old_name,
2444 .new_name = new_name,
2445 .bitmask = server->attr_bitmask,
2447 struct nfs_fattr old_fattr, new_fattr;
2448 struct nfs4_rename_res res = {
2450 .old_fattr = &old_fattr,
2451 .new_fattr = &new_fattr,
2453 struct rpc_message msg = {
2454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2460 nfs_fattr_init(res.old_fattr);
2461 nfs_fattr_init(res.new_fattr);
2462 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2465 update_changeattr(old_dir, &res.old_cinfo);
2466 nfs_post_op_update_inode(old_dir, res.old_fattr);
2467 update_changeattr(new_dir, &res.new_cinfo);
2468 nfs_post_op_update_inode(new_dir, res.new_fattr);
2473 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2474 struct inode *new_dir, struct qstr *new_name)
2476 struct nfs4_exception exception = { };
2479 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2480 _nfs4_proc_rename(old_dir, old_name,
2483 } while (exception.retry);
2487 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2489 struct nfs_server *server = NFS_SERVER(inode);
2490 struct nfs4_link_arg arg = {
2491 .fh = NFS_FH(inode),
2492 .dir_fh = NFS_FH(dir),
2494 .bitmask = server->attr_bitmask,
2496 struct nfs_fattr fattr, dir_attr;
2497 struct nfs4_link_res res = {
2500 .dir_attr = &dir_attr,
2502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2509 nfs_fattr_init(res.fattr);
2510 nfs_fattr_init(res.dir_attr);
2511 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2513 update_changeattr(dir, &res.cinfo);
2514 nfs_post_op_update_inode(dir, res.dir_attr);
2515 nfs_post_op_update_inode(inode, res.fattr);
2521 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2523 struct nfs4_exception exception = { };
2526 err = nfs4_handle_exception(NFS_SERVER(inode),
2527 _nfs4_proc_link(inode, dir, name),
2529 } while (exception.retry);
2533 struct nfs4_createdata {
2534 struct rpc_message msg;
2535 struct nfs4_create_arg arg;
2536 struct nfs4_create_res res;
2538 struct nfs_fattr fattr;
2539 struct nfs_fattr dir_fattr;
2542 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2543 struct qstr *name, struct iattr *sattr, u32 ftype)
2545 struct nfs4_createdata *data;
2547 data = kzalloc(sizeof(*data), GFP_KERNEL);
2549 struct nfs_server *server = NFS_SERVER(dir);
2551 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2552 data->msg.rpc_argp = &data->arg;
2553 data->msg.rpc_resp = &data->res;
2554 data->arg.dir_fh = NFS_FH(dir);
2555 data->arg.server = server;
2556 data->arg.name = name;
2557 data->arg.attrs = sattr;
2558 data->arg.ftype = ftype;
2559 data->arg.bitmask = server->attr_bitmask;
2560 data->res.server = server;
2561 data->res.fh = &data->fh;
2562 data->res.fattr = &data->fattr;
2563 data->res.dir_fattr = &data->dir_fattr;
2564 nfs_fattr_init(data->res.fattr);
2565 nfs_fattr_init(data->res.dir_fattr);
2570 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2572 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2573 &data->arg, &data->res, 1);
2575 update_changeattr(dir, &data->res.dir_cinfo);
2576 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2577 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2582 static void nfs4_free_createdata(struct nfs4_createdata *data)
2587 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2588 struct page *page, unsigned int len, struct iattr *sattr)
2590 struct nfs4_createdata *data;
2591 int status = -ENAMETOOLONG;
2593 if (len > NFS4_MAXPATHLEN)
2597 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2601 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2602 data->arg.u.symlink.pages = &page;
2603 data->arg.u.symlink.len = len;
2605 status = nfs4_do_create(dir, dentry, data);
2607 nfs4_free_createdata(data);
2612 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2613 struct page *page, unsigned int len, struct iattr *sattr)
2615 struct nfs4_exception exception = { };
2618 err = nfs4_handle_exception(NFS_SERVER(dir),
2619 _nfs4_proc_symlink(dir, dentry, page,
2622 } while (exception.retry);
2626 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2627 struct iattr *sattr)
2629 struct nfs4_createdata *data;
2630 int status = -ENOMEM;
2632 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2636 status = nfs4_do_create(dir, dentry, data);
2638 nfs4_free_createdata(data);
2643 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2644 struct iattr *sattr)
2646 struct nfs4_exception exception = { };
2649 err = nfs4_handle_exception(NFS_SERVER(dir),
2650 _nfs4_proc_mkdir(dir, dentry, sattr),
2652 } while (exception.retry);
2656 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2657 u64 cookie, struct page *page, unsigned int count, int plus)
2659 struct inode *dir = dentry->d_inode;
2660 struct nfs4_readdir_arg args = {
2665 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2667 struct nfs4_readdir_res res;
2668 struct rpc_message msg = {
2669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2676 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2677 dentry->d_parent->d_name.name,
2678 dentry->d_name.name,
2679 (unsigned long long)cookie);
2680 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2681 res.pgbase = args.pgbase;
2682 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2684 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2686 nfs_invalidate_atime(dir);
2688 dprintk("%s: returns %d\n", __func__, status);
2692 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2693 u64 cookie, struct page *page, unsigned int count, int plus)
2695 struct nfs4_exception exception = { };
2698 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2699 _nfs4_proc_readdir(dentry, cred, cookie,
2702 } while (exception.retry);
2706 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2707 struct iattr *sattr, dev_t rdev)
2709 struct nfs4_createdata *data;
2710 int mode = sattr->ia_mode;
2711 int status = -ENOMEM;
2713 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2714 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2716 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2721 data->arg.ftype = NF4FIFO;
2722 else if (S_ISBLK(mode)) {
2723 data->arg.ftype = NF4BLK;
2724 data->arg.u.device.specdata1 = MAJOR(rdev);
2725 data->arg.u.device.specdata2 = MINOR(rdev);
2727 else if (S_ISCHR(mode)) {
2728 data->arg.ftype = NF4CHR;
2729 data->arg.u.device.specdata1 = MAJOR(rdev);
2730 data->arg.u.device.specdata2 = MINOR(rdev);
2733 status = nfs4_do_create(dir, dentry, data);
2735 nfs4_free_createdata(data);
2740 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2741 struct iattr *sattr, dev_t rdev)
2743 struct nfs4_exception exception = { };
2746 err = nfs4_handle_exception(NFS_SERVER(dir),
2747 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2749 } while (exception.retry);
2753 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2754 struct nfs_fsstat *fsstat)
2756 struct nfs4_statfs_arg args = {
2758 .bitmask = server->attr_bitmask,
2760 struct nfs4_statfs_res res = {
2763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2769 nfs_fattr_init(fsstat->fattr);
2770 return nfs4_call_sync(server, &msg, &args, &res, 0);
2773 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2775 struct nfs4_exception exception = { };
2778 err = nfs4_handle_exception(server,
2779 _nfs4_proc_statfs(server, fhandle, fsstat),
2781 } while (exception.retry);
2785 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2786 struct nfs_fsinfo *fsinfo)
2788 struct nfs4_fsinfo_arg args = {
2790 .bitmask = server->attr_bitmask,
2792 struct nfs4_fsinfo_res res = {
2795 struct rpc_message msg = {
2796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2801 return nfs4_call_sync(server, &msg, &args, &res, 0);
2804 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2806 struct nfs4_exception exception = { };
2810 err = nfs4_handle_exception(server,
2811 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2813 } while (exception.retry);
2817 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2819 nfs_fattr_init(fsinfo->fattr);
2820 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2823 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2824 struct nfs_pathconf *pathconf)
2826 struct nfs4_pathconf_arg args = {
2828 .bitmask = server->attr_bitmask,
2830 struct nfs4_pathconf_res res = {
2831 .pathconf = pathconf,
2833 struct rpc_message msg = {
2834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2839 /* None of the pathconf attributes are mandatory to implement */
2840 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2841 memset(pathconf, 0, sizeof(*pathconf));
2845 nfs_fattr_init(pathconf->fattr);
2846 return nfs4_call_sync(server, &msg, &args, &res, 0);
2849 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2850 struct nfs_pathconf *pathconf)
2852 struct nfs4_exception exception = { };
2856 err = nfs4_handle_exception(server,
2857 _nfs4_proc_pathconf(server, fhandle, pathconf),
2859 } while (exception.retry);
2863 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2865 struct nfs_server *server = NFS_SERVER(data->inode);
2867 dprintk("--> %s\n", __func__);
2869 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2870 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2872 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2873 rpc_restart_call(task);
2877 nfs_invalidate_atime(data->inode);
2878 if (task->tk_status > 0)
2879 renew_lease(server, data->timestamp);
2883 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2885 data->timestamp = jiffies;
2886 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2889 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2891 struct inode *inode = data->inode;
2893 /* slot is freed in nfs_writeback_done */
2894 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2897 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2898 rpc_restart_call(task);
2901 if (task->tk_status >= 0) {
2902 renew_lease(NFS_SERVER(inode), data->timestamp);
2903 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2908 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2910 struct nfs_server *server = NFS_SERVER(data->inode);
2912 data->args.bitmask = server->cache_consistency_bitmask;
2913 data->res.server = server;
2914 data->timestamp = jiffies;
2916 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2919 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2921 struct inode *inode = data->inode;
2923 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2924 rpc_restart_call(task);
2927 nfs_refresh_inode(inode, data->res.fattr);
2931 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2933 struct nfs_server *server = NFS_SERVER(data->inode);
2935 data->args.bitmask = server->cache_consistency_bitmask;
2936 data->res.server = server;
2937 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2941 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2942 * standalone procedure for queueing an asynchronous RENEW.
2944 static void nfs4_renew_done(struct rpc_task *task, void *data)
2946 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2947 unsigned long timestamp = (unsigned long)data;
2949 if (task->tk_status < 0) {
2950 /* Unless we're shutting down, schedule state recovery! */
2951 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2952 nfs4_schedule_state_recovery(clp);
2955 spin_lock(&clp->cl_lock);
2956 if (time_before(clp->cl_last_renewal,timestamp))
2957 clp->cl_last_renewal = timestamp;
2958 spin_unlock(&clp->cl_lock);
2961 static const struct rpc_call_ops nfs4_renew_ops = {
2962 .rpc_call_done = nfs4_renew_done,
2965 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2967 struct rpc_message msg = {
2968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2973 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2974 &nfs4_renew_ops, (void *)jiffies);
2977 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2979 struct rpc_message msg = {
2980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2984 unsigned long now = jiffies;
2987 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2990 spin_lock(&clp->cl_lock);
2991 if (time_before(clp->cl_last_renewal,now))
2992 clp->cl_last_renewal = now;
2993 spin_unlock(&clp->cl_lock);
2997 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2999 return (server->caps & NFS_CAP_ACLS)
3000 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3001 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3004 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3005 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3008 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3010 static void buf_to_pages(const void *buf, size_t buflen,
3011 struct page **pages, unsigned int *pgbase)
3013 const void *p = buf;
3015 *pgbase = offset_in_page(buf);
3017 while (p < buf + buflen) {
3018 *(pages++) = virt_to_page(p);
3019 p += PAGE_CACHE_SIZE;
3023 struct nfs4_cached_acl {
3029 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3031 struct nfs_inode *nfsi = NFS_I(inode);
3033 spin_lock(&inode->i_lock);
3034 kfree(nfsi->nfs4_acl);
3035 nfsi->nfs4_acl = acl;
3036 spin_unlock(&inode->i_lock);
3039 static void nfs4_zap_acl_attr(struct inode *inode)
3041 nfs4_set_cached_acl(inode, NULL);
3044 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3046 struct nfs_inode *nfsi = NFS_I(inode);
3047 struct nfs4_cached_acl *acl;
3050 spin_lock(&inode->i_lock);
3051 acl = nfsi->nfs4_acl;
3054 if (buf == NULL) /* user is just asking for length */
3056 if (acl->cached == 0)
3058 ret = -ERANGE; /* see getxattr(2) man page */
3059 if (acl->len > buflen)
3061 memcpy(buf, acl->data, acl->len);
3065 spin_unlock(&inode->i_lock);
3069 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3071 struct nfs4_cached_acl *acl;
3073 if (buf && acl_len <= PAGE_SIZE) {
3074 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3078 memcpy(acl->data, buf, acl_len);
3080 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3087 nfs4_set_cached_acl(inode, acl);
3090 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3092 struct page *pages[NFS4ACL_MAXPAGES];
3093 struct nfs_getaclargs args = {
3094 .fh = NFS_FH(inode),
3098 struct nfs_getaclres res = {
3102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3107 struct page *localpage = NULL;
3110 if (buflen < PAGE_SIZE) {
3111 /* As long as we're doing a round trip to the server anyway,
3112 * let's be prepared for a page of acl data. */
3113 localpage = alloc_page(GFP_KERNEL);
3114 resp_buf = page_address(localpage);
3115 if (localpage == NULL)
3117 args.acl_pages[0] = localpage;
3118 args.acl_pgbase = 0;
3119 args.acl_len = PAGE_SIZE;
3122 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3124 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3127 if (res.acl_len > args.acl_len)
3128 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3130 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3133 if (res.acl_len > buflen)
3136 memcpy(buf, resp_buf, res.acl_len);
3141 __free_page(localpage);
3145 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3147 struct nfs4_exception exception = { };
3150 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3153 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3154 } while (exception.retry);
3158 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3160 struct nfs_server *server = NFS_SERVER(inode);
3163 if (!nfs4_server_supports_acls(server))
3165 ret = nfs_revalidate_inode(server, inode);
3168 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3169 nfs_zap_acl_cache(inode);
3170 ret = nfs4_read_cached_acl(inode, buf, buflen);
3173 return nfs4_get_acl_uncached(inode, buf, buflen);
3176 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3178 struct nfs_server *server = NFS_SERVER(inode);
3179 struct page *pages[NFS4ACL_MAXPAGES];
3180 struct nfs_setaclargs arg = {
3181 .fh = NFS_FH(inode),
3185 struct nfs_setaclres res;
3186 struct rpc_message msg = {
3187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3193 if (!nfs4_server_supports_acls(server))
3195 nfs_inode_return_delegation(inode);
3196 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3197 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3198 nfs_access_zap_cache(inode);
3199 nfs_zap_acl_cache(inode);
3203 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3205 struct nfs4_exception exception = { };
3208 err = nfs4_handle_exception(NFS_SERVER(inode),
3209 __nfs4_proc_set_acl(inode, buf, buflen),
3211 } while (exception.retry);
3216 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3218 struct nfs_client *clp = server->nfs_client;
3220 if (!clp || task->tk_status >= 0)
3222 switch(task->tk_status) {
3223 case -NFS4ERR_ADMIN_REVOKED:
3224 case -NFS4ERR_BAD_STATEID:
3225 case -NFS4ERR_OPENMODE:
3228 nfs4_state_mark_reclaim_nograce(clp, state);
3229 case -NFS4ERR_STALE_CLIENTID:
3230 case -NFS4ERR_STALE_STATEID:
3231 case -NFS4ERR_EXPIRED:
3232 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3233 nfs4_schedule_state_recovery(clp);
3234 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3235 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3236 task->tk_status = 0;
3238 case -NFS4ERR_DELAY:
3239 nfs_inc_server_stats(server, NFSIOS_DELAY);
3240 case -NFS4ERR_GRACE:
3241 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3242 task->tk_status = 0;
3244 case -NFS4ERR_OLD_STATEID:
3245 task->tk_status = 0;
3248 task->tk_status = nfs4_map_errors(task->tk_status);
3252 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3254 nfs4_verifier sc_verifier;
3255 struct nfs4_setclientid setclientid = {
3256 .sc_verifier = &sc_verifier,
3259 struct rpc_message msg = {
3260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3261 .rpc_argp = &setclientid,
3269 p = (__be32*)sc_verifier.data;
3270 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3271 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3274 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3275 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3277 rpc_peeraddr2str(clp->cl_rpcclient,
3279 rpc_peeraddr2str(clp->cl_rpcclient,
3281 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3282 clp->cl_id_uniquifier);
3283 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3284 sizeof(setclientid.sc_netid),
3285 rpc_peeraddr2str(clp->cl_rpcclient,
3286 RPC_DISPLAY_NETID));
3287 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3288 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3289 clp->cl_ipaddr, port >> 8, port & 255);
3291 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3292 if (status != -NFS4ERR_CLID_INUSE)
3297 ssleep(clp->cl_lease_time + 1);
3299 if (++clp->cl_id_uniquifier == 0)
3305 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3307 struct nfs_fsinfo fsinfo;
3308 struct rpc_message msg = {
3309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3311 .rpc_resp = &fsinfo,
3318 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3320 spin_lock(&clp->cl_lock);
3321 clp->cl_lease_time = fsinfo.lease_time * HZ;
3322 clp->cl_last_renewal = now;
3323 spin_unlock(&clp->cl_lock);
3328 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3333 err = _nfs4_proc_setclientid_confirm(clp, cred);
3337 case -NFS4ERR_RESOURCE:
3338 /* The IBM lawyers misread another document! */
3339 case -NFS4ERR_DELAY:
3340 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3346 struct nfs4_delegreturndata {
3347 struct nfs4_delegreturnargs args;
3348 struct nfs4_delegreturnres res;
3350 nfs4_stateid stateid;
3351 unsigned long timestamp;
3352 struct nfs_fattr fattr;
3356 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3358 struct nfs4_delegreturndata *data = calldata;
3359 data->rpc_status = task->tk_status;
3360 if (data->rpc_status == 0)
3361 renew_lease(data->res.server, data->timestamp);
3364 static void nfs4_delegreturn_release(void *calldata)
3369 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3370 .rpc_call_done = nfs4_delegreturn_done,
3371 .rpc_release = nfs4_delegreturn_release,
3374 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3376 struct nfs4_delegreturndata *data;
3377 struct nfs_server *server = NFS_SERVER(inode);
3378 struct rpc_task *task;
3379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3383 struct rpc_task_setup task_setup_data = {
3384 .rpc_client = server->client,
3385 .rpc_message = &msg,
3386 .callback_ops = &nfs4_delegreturn_ops,
3387 .flags = RPC_TASK_ASYNC,
3391 data = kmalloc(sizeof(*data), GFP_KERNEL);
3394 data->args.fhandle = &data->fh;
3395 data->args.stateid = &data->stateid;
3396 data->args.bitmask = server->attr_bitmask;
3397 nfs_copy_fh(&data->fh, NFS_FH(inode));
3398 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3399 data->res.fattr = &data->fattr;
3400 data->res.server = server;
3401 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3402 nfs_fattr_init(data->res.fattr);
3403 data->timestamp = jiffies;
3404 data->rpc_status = 0;
3406 task_setup_data.callback_data = data;
3407 msg.rpc_argp = &data->args,
3408 msg.rpc_resp = &data->res,
3409 task = rpc_run_task(&task_setup_data);
3411 return PTR_ERR(task);
3414 status = nfs4_wait_for_completion_rpc_task(task);
3417 status = data->rpc_status;
3420 nfs_refresh_inode(inode, &data->fattr);
3426 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3428 struct nfs_server *server = NFS_SERVER(inode);
3429 struct nfs4_exception exception = { };
3432 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3434 case -NFS4ERR_STALE_STATEID:
3435 case -NFS4ERR_EXPIRED:
3439 err = nfs4_handle_exception(server, err, &exception);
3440 } while (exception.retry);
3444 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3445 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3448 * sleep, with exponential backoff, and retry the LOCK operation.
3450 static unsigned long
3451 nfs4_set_lock_task_retry(unsigned long timeout)
3453 schedule_timeout_killable(timeout);
3455 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3456 return NFS4_LOCK_MAXTIMEOUT;
3460 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3462 struct inode *inode = state->inode;
3463 struct nfs_server *server = NFS_SERVER(inode);
3464 struct nfs_client *clp = server->nfs_client;
3465 struct nfs_lockt_args arg = {
3466 .fh = NFS_FH(inode),
3469 struct nfs_lockt_res res = {
3472 struct rpc_message msg = {
3473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3476 .rpc_cred = state->owner->so_cred,
3478 struct nfs4_lock_state *lsp;
3481 arg.lock_owner.clientid = clp->cl_clientid;
3482 status = nfs4_set_lock_state(state, request);
3485 lsp = request->fl_u.nfs4_fl.owner;
3486 arg.lock_owner.id = lsp->ls_id.id;
3487 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3490 request->fl_type = F_UNLCK;
3492 case -NFS4ERR_DENIED:
3495 request->fl_ops->fl_release_private(request);
3500 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3502 struct nfs4_exception exception = { };
3506 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3507 _nfs4_proc_getlk(state, cmd, request),
3509 } while (exception.retry);
3513 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3516 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3518 res = posix_lock_file_wait(file, fl);
3521 res = flock_lock_file_wait(file, fl);
3529 struct nfs4_unlockdata {
3530 struct nfs_locku_args arg;
3531 struct nfs_locku_res res;
3532 struct nfs4_lock_state *lsp;
3533 struct nfs_open_context *ctx;
3534 struct file_lock fl;
3535 const struct nfs_server *server;
3536 unsigned long timestamp;
3539 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3540 struct nfs_open_context *ctx,
3541 struct nfs4_lock_state *lsp,
3542 struct nfs_seqid *seqid)
3544 struct nfs4_unlockdata *p;
3545 struct inode *inode = lsp->ls_state->inode;
3547 p = kzalloc(sizeof(*p), GFP_KERNEL);
3550 p->arg.fh = NFS_FH(inode);
3552 p->arg.seqid = seqid;
3553 p->res.seqid = seqid;
3554 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3555 p->arg.stateid = &lsp->ls_stateid;
3557 atomic_inc(&lsp->ls_count);
3558 /* Ensure we don't close file until we're done freeing locks! */
3559 p->ctx = get_nfs_open_context(ctx);
3560 memcpy(&p->fl, fl, sizeof(p->fl));
3561 p->server = NFS_SERVER(inode);
3565 static void nfs4_locku_release_calldata(void *data)
3567 struct nfs4_unlockdata *calldata = data;
3568 nfs_free_seqid(calldata->arg.seqid);
3569 nfs4_put_lock_state(calldata->lsp);
3570 put_nfs_open_context(calldata->ctx);
3574 static void nfs4_locku_done(struct rpc_task *task, void *data)
3576 struct nfs4_unlockdata *calldata = data;
3578 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3580 if (RPC_ASSASSINATED(task))
3582 switch (task->tk_status) {
3584 memcpy(calldata->lsp->ls_stateid.data,
3585 calldata->res.stateid.data,
3586 sizeof(calldata->lsp->ls_stateid.data));
3587 renew_lease(calldata->server, calldata->timestamp);
3589 case -NFS4ERR_BAD_STATEID:
3590 case -NFS4ERR_OLD_STATEID:
3591 case -NFS4ERR_STALE_STATEID:
3592 case -NFS4ERR_EXPIRED:
3595 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3596 rpc_restart_call(task);
3598 nfs4_sequence_free_slot(calldata->server->nfs_client,
3599 &calldata->res.seq_res);
3602 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3604 struct nfs4_unlockdata *calldata = data;
3606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3608 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3609 /* Note: exit _without_ running nfs4_locku_done */
3610 task->tk_action = NULL;
3613 calldata->timestamp = jiffies;
3614 if (nfs4_setup_sequence(calldata->server->nfs_client,
3615 &calldata->arg.seq_args,
3616 &calldata->res.seq_res, 1, task))
3618 rpc_call_start(task);
3621 static const struct rpc_call_ops nfs4_locku_ops = {
3622 .rpc_call_prepare = nfs4_locku_prepare,
3623 .rpc_call_done = nfs4_locku_done,
3624 .rpc_release = nfs4_locku_release_calldata,
3627 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3628 struct nfs_open_context *ctx,
3629 struct nfs4_lock_state *lsp,
3630 struct nfs_seqid *seqid)
3632 struct nfs4_unlockdata *data;
3633 struct rpc_message msg = {
3634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3635 .rpc_cred = ctx->cred,
3637 struct rpc_task_setup task_setup_data = {
3638 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3639 .rpc_message = &msg,
3640 .callback_ops = &nfs4_locku_ops,
3641 .workqueue = nfsiod_workqueue,
3642 .flags = RPC_TASK_ASYNC,
3645 /* Ensure this is an unlock - when canceling a lock, the
3646 * canceled lock is passed in, and it won't be an unlock.
3648 fl->fl_type = F_UNLCK;
3650 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3652 nfs_free_seqid(seqid);
3653 return ERR_PTR(-ENOMEM);
3656 msg.rpc_argp = &data->arg,
3657 msg.rpc_resp = &data->res,
3658 task_setup_data.callback_data = data;
3659 return rpc_run_task(&task_setup_data);
3662 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3664 struct nfs_inode *nfsi = NFS_I(state->inode);
3665 struct nfs_seqid *seqid;
3666 struct nfs4_lock_state *lsp;
3667 struct rpc_task *task;
3669 unsigned char fl_flags = request->fl_flags;
3671 status = nfs4_set_lock_state(state, request);
3672 /* Unlock _before_ we do the RPC call */
3673 request->fl_flags |= FL_EXISTS;
3674 down_read(&nfsi->rwsem);
3675 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3676 up_read(&nfsi->rwsem);
3679 up_read(&nfsi->rwsem);
3682 /* Is this a delegated lock? */
3683 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3685 lsp = request->fl_u.nfs4_fl.owner;
3686 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3690 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3691 status = PTR_ERR(task);
3694 status = nfs4_wait_for_completion_rpc_task(task);
3697 request->fl_flags = fl_flags;
3701 struct nfs4_lockdata {
3702 struct nfs_lock_args arg;
3703 struct nfs_lock_res res;
3704 struct nfs4_lock_state *lsp;
3705 struct nfs_open_context *ctx;
3706 struct file_lock fl;
3707 unsigned long timestamp;
3710 struct nfs_server *server;
3713 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3714 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3716 struct nfs4_lockdata *p;
3717 struct inode *inode = lsp->ls_state->inode;
3718 struct nfs_server *server = NFS_SERVER(inode);
3720 p = kzalloc(sizeof(*p), GFP_KERNEL);
3724 p->arg.fh = NFS_FH(inode);
3726 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3727 if (p->arg.open_seqid == NULL)
3729 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3730 if (p->arg.lock_seqid == NULL)
3731 goto out_free_seqid;
3732 p->arg.lock_stateid = &lsp->ls_stateid;
3733 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3734 p->arg.lock_owner.id = lsp->ls_id.id;
3735 p->res.lock_seqid = p->arg.lock_seqid;
3736 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3739 atomic_inc(&lsp->ls_count);
3740 p->ctx = get_nfs_open_context(ctx);
3741 memcpy(&p->fl, fl, sizeof(p->fl));
3744 nfs_free_seqid(p->arg.open_seqid);
3750 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3752 struct nfs4_lockdata *data = calldata;
3753 struct nfs4_state *state = data->lsp->ls_state;
3755 dprintk("%s: begin!\n", __func__);
3756 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3758 /* Do we need to do an open_to_lock_owner? */
3759 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3760 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3762 data->arg.open_stateid = &state->stateid;
3763 data->arg.new_lock_owner = 1;
3764 data->res.open_seqid = data->arg.open_seqid;
3766 data->arg.new_lock_owner = 0;
3767 data->timestamp = jiffies;
3768 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3769 &data->res.seq_res, 1, task))
3771 rpc_call_start(task);
3772 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3775 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3777 struct nfs4_lockdata *data = calldata;
3779 dprintk("%s: begin!\n", __func__);
3781 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3784 data->rpc_status = task->tk_status;
3785 if (RPC_ASSASSINATED(task))
3787 if (data->arg.new_lock_owner != 0) {
3788 if (data->rpc_status == 0)
3789 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3793 if (data->rpc_status == 0) {
3794 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3795 sizeof(data->lsp->ls_stateid.data));
3796 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3797 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3800 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3803 static void nfs4_lock_release(void *calldata)
3805 struct nfs4_lockdata *data = calldata;
3807 dprintk("%s: begin!\n", __func__);
3808 nfs_free_seqid(data->arg.open_seqid);
3809 if (data->cancelled != 0) {
3810 struct rpc_task *task;
3811 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3812 data->arg.lock_seqid);
3815 dprintk("%s: cancelling lock!\n", __func__);
3817 nfs_free_seqid(data->arg.lock_seqid);
3818 nfs4_put_lock_state(data->lsp);
3819 put_nfs_open_context(data->ctx);
3821 dprintk("%s: done!\n", __func__);
3824 static const struct rpc_call_ops nfs4_lock_ops = {
3825 .rpc_call_prepare = nfs4_lock_prepare,
3826 .rpc_call_done = nfs4_lock_done,
3827 .rpc_release = nfs4_lock_release,
3830 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3832 struct nfs4_lockdata *data;
3833 struct rpc_task *task;
3834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3836 .rpc_cred = state->owner->so_cred,
3838 struct rpc_task_setup task_setup_data = {
3839 .rpc_client = NFS_CLIENT(state->inode),
3840 .rpc_message = &msg,
3841 .callback_ops = &nfs4_lock_ops,
3842 .workqueue = nfsiod_workqueue,
3843 .flags = RPC_TASK_ASYNC,
3847 dprintk("%s: begin!\n", __func__);
3848 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3849 fl->fl_u.nfs4_fl.owner);
3853 data->arg.block = 1;
3855 data->arg.reclaim = 1;
3856 msg.rpc_argp = &data->arg,
3857 msg.rpc_resp = &data->res,
3858 task_setup_data.callback_data = data;
3859 task = rpc_run_task(&task_setup_data);
3861 return PTR_ERR(task);
3862 ret = nfs4_wait_for_completion_rpc_task(task);
3864 ret = data->rpc_status;
3865 if (ret == -NFS4ERR_DENIED)
3868 data->cancelled = 1;
3870 dprintk("%s: done, ret = %d!\n", __func__, ret);
3874 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3876 struct nfs_server *server = NFS_SERVER(state->inode);
3877 struct nfs4_exception exception = { };
3881 /* Cache the lock if possible... */
3882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3884 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3885 if (err != -NFS4ERR_DELAY)
3887 nfs4_handle_exception(server, err, &exception);
3888 } while (exception.retry);
3892 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3894 struct nfs_server *server = NFS_SERVER(state->inode);
3895 struct nfs4_exception exception = { };
3898 err = nfs4_set_lock_state(state, request);
3902 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3904 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3905 if (err != -NFS4ERR_DELAY)
3907 nfs4_handle_exception(server, err, &exception);
3908 } while (exception.retry);
3912 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3914 struct nfs_inode *nfsi = NFS_I(state->inode);
3915 unsigned char fl_flags = request->fl_flags;
3918 /* Is this a delegated open? */
3919 status = nfs4_set_lock_state(state, request);
3922 request->fl_flags |= FL_ACCESS;
3923 status = do_vfs_lock(request->fl_file, request);
3926 down_read(&nfsi->rwsem);
3927 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3928 /* Yes: cache locks! */
3929 /* ...but avoid races with delegation recall... */
3930 request->fl_flags = fl_flags & ~FL_SLEEP;
3931 status = do_vfs_lock(request->fl_file, request);
3934 status = _nfs4_do_setlk(state, cmd, request, 0);
3937 /* Note: we always want to sleep here! */
3938 request->fl_flags = fl_flags | FL_SLEEP;
3939 if (do_vfs_lock(request->fl_file, request) < 0)
3940 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3942 up_read(&nfsi->rwsem);
3944 request->fl_flags = fl_flags;
3948 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3950 struct nfs4_exception exception = { };
3954 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3955 _nfs4_proc_setlk(state, cmd, request),
3957 } while (exception.retry);
3962 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3964 struct nfs_open_context *ctx;
3965 struct nfs4_state *state;
3966 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3969 /* verify open state */
3970 ctx = nfs_file_open_context(filp);
3973 if (request->fl_start < 0 || request->fl_end < 0)
3977 return nfs4_proc_getlk(state, F_GETLK, request);
3979 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3982 if (request->fl_type == F_UNLCK)
3983 return nfs4_proc_unlck(state, cmd, request);
3986 status = nfs4_proc_setlk(state, cmd, request);
3987 if ((status != -EAGAIN) || IS_SETLK(cmd))
3989 timeout = nfs4_set_lock_task_retry(timeout);
3990 status = -ERESTARTSYS;
3993 } while(status < 0);
3997 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3999 struct nfs_server *server = NFS_SERVER(state->inode);
4000 struct nfs4_exception exception = { };
4003 err = nfs4_set_lock_state(state, fl);
4007 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4008 if (err != -NFS4ERR_DELAY)
4010 err = nfs4_handle_exception(server, err, &exception);
4011 } while (exception.retry);
4016 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4018 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4019 size_t buflen, int flags)
4021 struct inode *inode = dentry->d_inode;
4023 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4026 return nfs4_proc_set_acl(inode, buf, buflen);
4029 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4030 * and that's what we'll do for e.g. user attributes that haven't been set.
4031 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4032 * attributes in kernel-managed attribute namespaces. */
4033 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4036 struct inode *inode = dentry->d_inode;
4038 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4041 return nfs4_proc_get_acl(inode, buf, buflen);
4044 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4046 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4048 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4050 if (buf && buflen < len)
4053 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4057 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4059 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4060 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4061 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4064 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4065 NFS_ATTR_FATTR_NLINK;
4066 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4070 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4071 struct nfs4_fs_locations *fs_locations, struct page *page)
4073 struct nfs_server *server = NFS_SERVER(dir);
4075 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4076 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4078 struct nfs4_fs_locations_arg args = {
4079 .dir_fh = NFS_FH(dir),
4084 struct nfs4_fs_locations_res res = {
4085 .fs_locations = fs_locations,
4087 struct rpc_message msg = {
4088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4094 dprintk("%s: start\n", __func__);
4095 nfs_fattr_init(&fs_locations->fattr);
4096 fs_locations->server = server;
4097 fs_locations->nlocations = 0;
4098 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4099 nfs_fixup_referral_attributes(&fs_locations->fattr);
4100 dprintk("%s: returned status = %d\n", __func__, status);
4104 #ifdef CONFIG_NFS_V4_1
4105 /* Destroy the slot table */
4106 static void nfs4_destroy_slot_table(struct nfs4_session *session)
4108 if (session->fc_slot_table.slots == NULL)
4110 kfree(session->fc_slot_table.slots);
4111 session->fc_slot_table.slots = NULL;
4115 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4117 struct nfs4_session *session;
4118 struct nfs4_slot_table *tbl;
4120 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4123 tbl = &session->fc_slot_table;
4124 spin_lock_init(&tbl->slot_tbl_lock);
4125 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4130 void nfs4_destroy_session(struct nfs4_session *session)
4132 nfs4_destroy_slot_table(session);
4136 #endif /* CONFIG_NFS_V4_1 */
4138 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
4139 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4140 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4141 .recover_open = nfs4_open_reclaim,
4142 .recover_lock = nfs4_lock_reclaim,
4145 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
4146 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4147 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4148 .recover_open = nfs4_open_expired,
4149 .recover_lock = nfs4_lock_expired,
4152 static const struct inode_operations nfs4_file_inode_operations = {
4153 .permission = nfs_permission,
4154 .getattr = nfs_getattr,
4155 .setattr = nfs_setattr,
4156 .getxattr = nfs4_getxattr,
4157 .setxattr = nfs4_setxattr,
4158 .listxattr = nfs4_listxattr,
4161 const struct nfs_rpc_ops nfs_v4_clientops = {
4162 .version = 4, /* protocol version */
4163 .dentry_ops = &nfs4_dentry_operations,
4164 .dir_inode_ops = &nfs4_dir_inode_operations,
4165 .file_inode_ops = &nfs4_file_inode_operations,
4166 .getroot = nfs4_proc_get_root,
4167 .getattr = nfs4_proc_getattr,
4168 .setattr = nfs4_proc_setattr,
4169 .lookupfh = nfs4_proc_lookupfh,
4170 .lookup = nfs4_proc_lookup,
4171 .access = nfs4_proc_access,
4172 .readlink = nfs4_proc_readlink,
4173 .create = nfs4_proc_create,
4174 .remove = nfs4_proc_remove,
4175 .unlink_setup = nfs4_proc_unlink_setup,
4176 .unlink_done = nfs4_proc_unlink_done,
4177 .rename = nfs4_proc_rename,
4178 .link = nfs4_proc_link,
4179 .symlink = nfs4_proc_symlink,
4180 .mkdir = nfs4_proc_mkdir,
4181 .rmdir = nfs4_proc_remove,
4182 .readdir = nfs4_proc_readdir,
4183 .mknod = nfs4_proc_mknod,
4184 .statfs = nfs4_proc_statfs,
4185 .fsinfo = nfs4_proc_fsinfo,
4186 .pathconf = nfs4_proc_pathconf,
4187 .set_capabilities = nfs4_server_capabilities,
4188 .decode_dirent = nfs4_decode_dirent,
4189 .read_setup = nfs4_proc_read_setup,
4190 .read_done = nfs4_read_done,
4191 .write_setup = nfs4_proc_write_setup,
4192 .write_done = nfs4_write_done,
4193 .commit_setup = nfs4_proc_commit_setup,
4194 .commit_done = nfs4_commit_done,
4195 .lock = nfs4_proc_lock,
4196 .clear_acl_cache = nfs4_zap_acl_attr,
4197 .close_context = nfs4_close_context,