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/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/nfs_idmap.h>
56 #include <linux/sunrpc/bc_xprt.h>
57 #include <linux/xattr.h>
58 #include <linux/utsname.h>
59 #include <linux/freezer.h>
62 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
75 static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
82 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
83 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
90 /* Prevent leaks of NFSv4 errors into userland */
91 static int nfs4_map_errors(int err)
96 case -NFS4ERR_RESOURCE:
98 case -NFS4ERR_WRONGSEC:
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
104 dprintk("%s could not handle NFSv4 error %d\n",
112 * This is our standard bitmap for GETATTR requests.
114 const u32 nfs4_fattr_bitmap[2] = {
116 | FATTR4_WORD0_CHANGE
119 | FATTR4_WORD0_FILEID,
121 | FATTR4_WORD1_NUMLINKS
123 | FATTR4_WORD1_OWNER_GROUP
124 | FATTR4_WORD1_RAWDEV
125 | FATTR4_WORD1_SPACE_USED
126 | FATTR4_WORD1_TIME_ACCESS
127 | FATTR4_WORD1_TIME_METADATA
128 | FATTR4_WORD1_TIME_MODIFY
131 const u32 nfs4_statfs_bitmap[2] = {
132 FATTR4_WORD0_FILES_AVAIL
133 | FATTR4_WORD0_FILES_FREE
134 | FATTR4_WORD0_FILES_TOTAL,
135 FATTR4_WORD1_SPACE_AVAIL
136 | FATTR4_WORD1_SPACE_FREE
137 | FATTR4_WORD1_SPACE_TOTAL
140 const u32 nfs4_pathconf_bitmap[2] = {
142 | FATTR4_WORD0_MAXNAME,
146 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
147 | FATTR4_WORD0_MAXREAD
148 | FATTR4_WORD0_MAXWRITE
149 | FATTR4_WORD0_LEASE_TIME,
150 FATTR4_WORD1_TIME_DELTA
151 | FATTR4_WORD1_FS_LAYOUT_TYPES,
152 FATTR4_WORD2_LAYOUT_BLKSIZE
155 const u32 nfs4_fs_locations_bitmap[2] = {
157 | FATTR4_WORD0_CHANGE
160 | FATTR4_WORD0_FILEID
161 | FATTR4_WORD0_FS_LOCATIONS,
163 | FATTR4_WORD1_NUMLINKS
165 | FATTR4_WORD1_OWNER_GROUP
166 | FATTR4_WORD1_RAWDEV
167 | FATTR4_WORD1_SPACE_USED
168 | FATTR4_WORD1_TIME_ACCESS
169 | FATTR4_WORD1_TIME_METADATA
170 | FATTR4_WORD1_TIME_MODIFY
171 | FATTR4_WORD1_MOUNTED_ON_FILEID
174 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
175 struct nfs4_readdir_arg *readdir)
179 BUG_ON(readdir->count < 80);
181 readdir->cookie = cookie;
182 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
187 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
192 * NFSv4 servers do not return entries for '.' and '..'
193 * Therefore, we fake these entries here. We let '.'
194 * have cookie 0 and '..' have cookie 1. Note that
195 * when talking to the server, we always send cookie 0
198 start = p = kmap_atomic(*readdir->pages, KM_USER0);
201 *p++ = xdr_one; /* next */
202 *p++ = xdr_zero; /* cookie, first word */
203 *p++ = xdr_one; /* cookie, second word */
204 *p++ = xdr_one; /* entry len */
205 memcpy(p, ".\0\0\0", 4); /* entry */
207 *p++ = xdr_one; /* bitmap length */
208 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
209 *p++ = htonl(8); /* attribute buffer length */
210 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
213 *p++ = xdr_one; /* next */
214 *p++ = xdr_zero; /* cookie, first word */
215 *p++ = xdr_two; /* cookie, second word */
216 *p++ = xdr_two; /* entry len */
217 memcpy(p, "..\0\0", 4); /* entry */
219 *p++ = xdr_one; /* bitmap length */
220 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
221 *p++ = htonl(8); /* attribute buffer length */
222 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
224 readdir->pgbase = (char *)p - (char *)start;
225 readdir->count -= readdir->pgbase;
226 kunmap_atomic(start, KM_USER0);
229 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
235 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
236 nfs_wait_bit_killable, TASK_KILLABLE);
240 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
247 *timeout = NFS4_POLL_RETRY_MIN;
248 if (*timeout > NFS4_POLL_RETRY_MAX)
249 *timeout = NFS4_POLL_RETRY_MAX;
250 freezable_schedule_timeout_killable(*timeout);
251 if (fatal_signal_pending(current))
257 /* This is the error handling routine for processes that are allowed
260 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
262 struct nfs_client *clp = server->nfs_client;
263 struct nfs4_state *state = exception->state;
266 exception->retry = 0;
270 case -NFS4ERR_ADMIN_REVOKED:
271 case -NFS4ERR_BAD_STATEID:
272 case -NFS4ERR_OPENMODE:
275 nfs4_schedule_stateid_recovery(server, state);
276 goto wait_on_recovery;
277 case -NFS4ERR_EXPIRED:
279 nfs4_schedule_stateid_recovery(server, state);
280 case -NFS4ERR_STALE_STATEID:
281 case -NFS4ERR_STALE_CLIENTID:
282 nfs4_schedule_lease_recovery(clp);
283 goto wait_on_recovery;
284 #if defined(CONFIG_NFS_V4_1)
285 case -NFS4ERR_BADSESSION:
286 case -NFS4ERR_BADSLOT:
287 case -NFS4ERR_BAD_HIGH_SLOT:
288 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
289 case -NFS4ERR_DEADSESSION:
290 case -NFS4ERR_SEQ_FALSE_RETRY:
291 case -NFS4ERR_SEQ_MISORDERED:
292 dprintk("%s ERROR: %d Reset session\n", __func__,
294 nfs4_schedule_session_recovery(clp->cl_session);
295 exception->retry = 1;
297 #endif /* defined(CONFIG_NFS_V4_1) */
298 case -NFS4ERR_FILE_OPEN:
299 if (exception->timeout > HZ) {
300 /* We have retried a decent amount, time to
309 ret = nfs4_delay(server->client, &exception->timeout);
312 case -NFS4ERR_RETRY_UNCACHED_REP:
313 case -NFS4ERR_OLD_STATEID:
314 exception->retry = 1;
316 case -NFS4ERR_BADOWNER:
317 /* The following works around a Linux server bug! */
318 case -NFS4ERR_BADNAME:
319 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
320 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
321 exception->retry = 1;
322 printk(KERN_WARNING "NFS: v4 server %s "
323 "does not accept raw "
325 "Reenabling the idmapper.\n",
326 server->nfs_client->cl_hostname);
329 /* We failed to handle the error */
330 return nfs4_map_errors(ret);
332 ret = nfs4_wait_clnt_recover(clp);
334 exception->retry = 1;
339 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
341 spin_lock(&clp->cl_lock);
342 if (time_before(clp->cl_last_renewal,timestamp))
343 clp->cl_last_renewal = timestamp;
344 spin_unlock(&clp->cl_lock);
347 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
349 do_renew_lease(server->nfs_client, timestamp);
352 #if defined(CONFIG_NFS_V4_1)
355 * nfs4_free_slot - free a slot and efficiently update slot table.
357 * freeing a slot is trivially done by clearing its respective bit
359 * If the freed slotid equals highest_used_slotid we want to update it
360 * so that the server would be able to size down the slot table if needed,
361 * otherwise we know that the highest_used_slotid is still in use.
362 * When updating highest_used_slotid there may be "holes" in the bitmap
363 * so we need to scan down from highest_used_slotid to 0 looking for the now
364 * highest slotid in use.
365 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
367 * Must be called while holding tbl->slot_tbl_lock
370 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
372 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
379 if (slotid < tbl->max_slots)
380 tbl->highest_used_slotid = slotid;
382 tbl->highest_used_slotid = NFS4_NO_SLOT;
384 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
385 slotid, tbl->highest_used_slotid);
388 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
390 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
395 * Signal state manager thread if session fore channel is drained
397 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
399 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
400 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
401 nfs4_set_task_privileged, NULL);
405 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
408 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
409 complete(&ses->fc_slot_table.complete);
413 * Signal state manager thread if session back channel is drained
415 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
417 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
418 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
420 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
421 complete(&ses->bc_slot_table.complete);
424 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
426 struct nfs4_slot_table *tbl;
428 tbl = &res->sr_session->fc_slot_table;
430 /* just wake up the next guy waiting since
431 * we may have not consumed a slot after all */
432 dprintk("%s: No slot\n", __func__);
436 spin_lock(&tbl->slot_tbl_lock);
437 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
438 nfs4_check_drain_fc_complete(res->sr_session);
439 spin_unlock(&tbl->slot_tbl_lock);
443 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
445 unsigned long timestamp;
446 struct nfs_client *clp;
449 * sr_status remains 1 if an RPC level error occurred. The server
450 * may or may not have processed the sequence operation..
451 * Proceed as if the server received and processed the sequence
454 if (res->sr_status == 1)
455 res->sr_status = NFS_OK;
457 /* don't increment the sequence number if the task wasn't sent */
458 if (!RPC_WAS_SENT(task))
461 /* Check the SEQUENCE operation status */
462 switch (res->sr_status) {
464 /* Update the slot's sequence and clientid lease timer */
465 ++res->sr_slot->seq_nr;
466 timestamp = res->sr_renewal_time;
467 clp = res->sr_session->clp;
468 do_renew_lease(clp, timestamp);
469 /* Check sequence flags */
470 if (res->sr_status_flags != 0)
471 nfs4_schedule_lease_recovery(clp);
474 /* The server detected a resend of the RPC call and
475 * returned NFS4ERR_DELAY as per Section 2.10.6.2
478 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
480 res->sr_slot - res->sr_session->fc_slot_table.slots,
481 res->sr_slot->seq_nr);
484 /* Just update the slot sequence no. */
485 ++res->sr_slot->seq_nr;
488 /* The session may be reset by one of the error handlers. */
489 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
490 nfs41_sequence_free_slot(res);
493 if (!rpc_restart_call(task))
495 rpc_delay(task, NFS4_POLL_RETRY_MAX);
499 static int nfs4_sequence_done(struct rpc_task *task,
500 struct nfs4_sequence_res *res)
502 if (res->sr_session == NULL)
504 return nfs41_sequence_done(task, res);
508 * nfs4_find_slot - efficiently look for a free slot
510 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
511 * If found, we mark the slot as used, update the highest_used_slotid,
512 * and respectively set up the sequence operation args.
513 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
515 * Note: must be called with under the slot_tbl_lock.
518 nfs4_find_slot(struct nfs4_slot_table *tbl)
521 u32 ret_id = NFS4_NO_SLOT;
523 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
524 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
526 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
527 if (slotid >= tbl->max_slots)
529 __set_bit(slotid, tbl->used_slots);
530 if (slotid > tbl->highest_used_slotid ||
531 tbl->highest_used_slotid == NFS4_NO_SLOT)
532 tbl->highest_used_slotid = slotid;
535 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
536 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
540 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
541 struct nfs4_sequence_res *res, int cache_reply)
543 args->sa_session = NULL;
544 args->sa_cache_this = 0;
546 args->sa_cache_this = 1;
547 res->sr_session = NULL;
551 int nfs41_setup_sequence(struct nfs4_session *session,
552 struct nfs4_sequence_args *args,
553 struct nfs4_sequence_res *res,
554 struct rpc_task *task)
556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
560 dprintk("--> %s\n", __func__);
561 /* slot already allocated? */
562 if (res->sr_slot != NULL)
565 tbl = &session->fc_slot_table;
567 spin_lock(&tbl->slot_tbl_lock);
568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
570 /* The state manager will wait until the slot table is empty */
571 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
572 spin_unlock(&tbl->slot_tbl_lock);
573 dprintk("%s session is draining\n", __func__);
577 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
578 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
579 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
580 spin_unlock(&tbl->slot_tbl_lock);
581 dprintk("%s enforce FIFO order\n", __func__);
585 slotid = nfs4_find_slot(tbl);
586 if (slotid == NFS4_NO_SLOT) {
587 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
588 spin_unlock(&tbl->slot_tbl_lock);
589 dprintk("<-- %s: no free slots\n", __func__);
592 spin_unlock(&tbl->slot_tbl_lock);
594 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
595 slot = tbl->slots + slotid;
596 args->sa_session = session;
597 args->sa_slotid = slotid;
599 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
601 res->sr_session = session;
603 res->sr_renewal_time = jiffies;
604 res->sr_status_flags = 0;
606 * sr_status is only set in decode_sequence, and so will remain
607 * set to 1 if an rpc level failure occurs.
612 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
614 int nfs4_setup_sequence(const struct nfs_server *server,
615 struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res,
617 struct rpc_task *task)
619 struct nfs4_session *session = nfs4_get_session(server);
625 dprintk("--> %s clp %p session %p sr_slot %td\n",
626 __func__, session->clp, session, res->sr_slot ?
627 res->sr_slot - session->fc_slot_table.slots : -1);
629 ret = nfs41_setup_sequence(session, args, res, task);
631 dprintk("<-- %s status=%d\n", __func__, ret);
635 struct nfs41_call_sync_data {
636 const struct nfs_server *seq_server;
637 struct nfs4_sequence_args *seq_args;
638 struct nfs4_sequence_res *seq_res;
641 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
643 struct nfs41_call_sync_data *data = calldata;
645 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
647 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
648 data->seq_res, task))
650 rpc_call_start(task);
653 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
655 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
656 nfs41_call_sync_prepare(task, calldata);
659 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
661 struct nfs41_call_sync_data *data = calldata;
663 nfs41_sequence_done(task, data->seq_res);
666 struct rpc_call_ops nfs41_call_sync_ops = {
667 .rpc_call_prepare = nfs41_call_sync_prepare,
668 .rpc_call_done = nfs41_call_sync_done,
671 struct rpc_call_ops nfs41_call_priv_sync_ops = {
672 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
673 .rpc_call_done = nfs41_call_sync_done,
676 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
677 struct nfs_server *server,
678 struct rpc_message *msg,
679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
684 struct rpc_task *task;
685 struct nfs41_call_sync_data data = {
686 .seq_server = server,
690 struct rpc_task_setup task_setup = {
693 .callback_ops = &nfs41_call_sync_ops,
694 .callback_data = &data
698 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
699 task = rpc_run_task(&task_setup);
703 ret = task->tk_status;
709 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
710 struct nfs_server *server,
711 struct rpc_message *msg,
712 struct nfs4_sequence_args *args,
713 struct nfs4_sequence_res *res,
716 nfs41_init_sequence(args, res, cache_reply);
717 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
722 void nfs41_init_sequence(struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res, int cache_reply)
727 static int nfs4_sequence_done(struct rpc_task *task,
728 struct nfs4_sequence_res *res)
732 #endif /* CONFIG_NFS_V4_1 */
734 int _nfs4_call_sync(struct rpc_clnt *clnt,
735 struct nfs_server *server,
736 struct rpc_message *msg,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
741 nfs41_init_sequence(args, res, cache_reply);
742 return rpc_call_sync(clnt, msg, 0);
746 int nfs4_call_sync(struct rpc_clnt *clnt,
747 struct nfs_server *server,
748 struct rpc_message *msg,
749 struct nfs4_sequence_args *args,
750 struct nfs4_sequence_res *res,
753 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
754 args, res, cache_reply);
757 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
759 struct nfs_inode *nfsi = NFS_I(dir);
761 spin_lock(&dir->i_lock);
762 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
763 if (!cinfo->atomic || cinfo->before != dir->i_version)
764 nfs_force_lookup_revalidate(dir);
765 dir->i_version = cinfo->after;
766 spin_unlock(&dir->i_lock);
769 struct nfs4_opendata {
771 struct nfs_openargs o_arg;
772 struct nfs_openres o_res;
773 struct nfs_open_confirmargs c_arg;
774 struct nfs_open_confirmres c_res;
775 struct nfs4_string owner_name;
776 struct nfs4_string group_name;
777 struct nfs_fattr f_attr;
778 struct nfs_fattr dir_attr;
780 struct dentry *dentry;
781 struct nfs4_state_owner *owner;
782 struct nfs4_state *state;
784 unsigned long timestamp;
785 unsigned int rpc_done : 1;
791 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
793 p->o_res.f_attr = &p->f_attr;
794 p->o_res.dir_attr = &p->dir_attr;
795 p->o_res.seqid = p->o_arg.seqid;
796 p->c_res.seqid = p->c_arg.seqid;
797 p->o_res.server = p->o_arg.server;
798 nfs_fattr_init(&p->f_attr);
799 nfs_fattr_init(&p->dir_attr);
800 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
803 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
804 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
805 const struct iattr *attrs,
808 struct dentry *parent = dget_parent(dentry);
809 struct inode *dir = parent->d_inode;
810 struct nfs_server *server = NFS_SERVER(dir);
811 struct nfs4_opendata *p;
813 p = kzalloc(sizeof(*p), gfp_mask);
816 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
817 if (p->o_arg.seqid == NULL)
819 nfs_sb_active(dentry->d_sb);
820 p->dentry = dget(dentry);
823 atomic_inc(&sp->so_count);
824 p->o_arg.fh = NFS_FH(dir);
825 p->o_arg.open_flags = flags;
826 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
827 p->o_arg.clientid = server->nfs_client->cl_clientid;
828 p->o_arg.id = sp->so_seqid.owner_id;
829 p->o_arg.name = &dentry->d_name;
830 p->o_arg.server = server;
831 p->o_arg.bitmask = server->attr_bitmask;
832 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
833 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
834 if (attrs != NULL && attrs->ia_valid != 0) {
837 p->o_arg.u.attrs = &p->attrs;
838 memcpy(&p->attrs, attrs, sizeof(p->attrs));
839 s = (u32 *) p->o_arg.u.verifier.data;
843 p->c_arg.fh = &p->o_res.fh;
844 p->c_arg.stateid = &p->o_res.stateid;
845 p->c_arg.seqid = p->o_arg.seqid;
846 nfs4_init_opendata_res(p);
856 static void nfs4_opendata_free(struct kref *kref)
858 struct nfs4_opendata *p = container_of(kref,
859 struct nfs4_opendata, kref);
860 struct super_block *sb = p->dentry->d_sb;
862 nfs_free_seqid(p->o_arg.seqid);
863 if (p->state != NULL)
864 nfs4_put_open_state(p->state);
865 nfs4_put_state_owner(p->owner);
869 nfs_fattr_free_names(&p->f_attr);
873 static void nfs4_opendata_put(struct nfs4_opendata *p)
876 kref_put(&p->kref, nfs4_opendata_free);
879 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
883 ret = rpc_wait_for_completion_task(task);
887 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
891 if (open_mode & (O_EXCL|O_TRUNC))
893 switch (mode & (FMODE_READ|FMODE_WRITE)) {
895 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
896 && state->n_rdonly != 0;
899 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
900 && state->n_wronly != 0;
902 case FMODE_READ|FMODE_WRITE:
903 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
904 && state->n_rdwr != 0;
910 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
912 if (delegation == NULL)
914 if ((delegation->type & fmode) != fmode)
916 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
918 nfs_mark_delegation_referenced(delegation);
922 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
931 case FMODE_READ|FMODE_WRITE:
934 nfs4_state_set_mode_locked(state, state->state | fmode);
937 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
939 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
940 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
941 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
944 set_bit(NFS_O_RDONLY_STATE, &state->flags);
947 set_bit(NFS_O_WRONLY_STATE, &state->flags);
949 case FMODE_READ|FMODE_WRITE:
950 set_bit(NFS_O_RDWR_STATE, &state->flags);
954 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
956 write_seqlock(&state->seqlock);
957 nfs_set_open_stateid_locked(state, stateid, fmode);
958 write_sequnlock(&state->seqlock);
961 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
964 * Protect the call to nfs4_state_set_mode_locked and
965 * serialise the stateid update
967 write_seqlock(&state->seqlock);
968 if (deleg_stateid != NULL) {
969 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
970 set_bit(NFS_DELEGATED_STATE, &state->flags);
972 if (open_stateid != NULL)
973 nfs_set_open_stateid_locked(state, open_stateid, fmode);
974 write_sequnlock(&state->seqlock);
975 spin_lock(&state->owner->so_lock);
976 update_open_stateflags(state, fmode);
977 spin_unlock(&state->owner->so_lock);
980 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
982 struct nfs_inode *nfsi = NFS_I(state->inode);
983 struct nfs_delegation *deleg_cur;
986 fmode &= (FMODE_READ|FMODE_WRITE);
989 deleg_cur = rcu_dereference(nfsi->delegation);
990 if (deleg_cur == NULL)
993 spin_lock(&deleg_cur->lock);
994 if (nfsi->delegation != deleg_cur ||
995 (deleg_cur->type & fmode) != fmode)
996 goto no_delegation_unlock;
998 if (delegation == NULL)
999 delegation = &deleg_cur->stateid;
1000 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1001 goto no_delegation_unlock;
1003 nfs_mark_delegation_referenced(deleg_cur);
1004 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1006 no_delegation_unlock:
1007 spin_unlock(&deleg_cur->lock);
1011 if (!ret && open_stateid != NULL) {
1012 __update_open_stateid(state, open_stateid, NULL, fmode);
1020 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1022 struct nfs_delegation *delegation;
1025 delegation = rcu_dereference(NFS_I(inode)->delegation);
1026 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1031 nfs_inode_return_delegation(inode);
1034 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1036 struct nfs4_state *state = opendata->state;
1037 struct nfs_inode *nfsi = NFS_I(state->inode);
1038 struct nfs_delegation *delegation;
1039 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1040 fmode_t fmode = opendata->o_arg.fmode;
1041 nfs4_stateid stateid;
1045 if (can_open_cached(state, fmode, open_mode)) {
1046 spin_lock(&state->owner->so_lock);
1047 if (can_open_cached(state, fmode, open_mode)) {
1048 update_open_stateflags(state, fmode);
1049 spin_unlock(&state->owner->so_lock);
1050 goto out_return_state;
1052 spin_unlock(&state->owner->so_lock);
1055 delegation = rcu_dereference(nfsi->delegation);
1056 if (!can_open_delegated(delegation, fmode)) {
1060 /* Save the delegation */
1061 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1063 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1068 /* Try to update the stateid using the delegation */
1069 if (update_open_stateid(state, NULL, &stateid, fmode))
1070 goto out_return_state;
1073 return ERR_PTR(ret);
1075 atomic_inc(&state->count);
1079 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1081 struct inode *inode;
1082 struct nfs4_state *state = NULL;
1083 struct nfs_delegation *delegation;
1086 if (!data->rpc_done) {
1087 state = nfs4_try_open_cached(data);
1092 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1094 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1095 ret = PTR_ERR(inode);
1099 state = nfs4_get_open_state(inode, data->owner);
1102 if (data->o_res.delegation_type != 0) {
1103 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1104 int delegation_flags = 0;
1107 delegation = rcu_dereference(NFS_I(inode)->delegation);
1109 delegation_flags = delegation->flags;
1111 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1112 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1113 "returning a delegation for "
1114 "OPEN(CLAIM_DELEGATE_CUR)\n",
1116 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1117 nfs_inode_set_delegation(state->inode,
1118 data->owner->so_cred,
1121 nfs_inode_reclaim_delegation(state->inode,
1122 data->owner->so_cred,
1126 update_open_stateid(state, &data->o_res.stateid, NULL,
1134 return ERR_PTR(ret);
1137 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1139 struct nfs_inode *nfsi = NFS_I(state->inode);
1140 struct nfs_open_context *ctx;
1142 spin_lock(&state->inode->i_lock);
1143 list_for_each_entry(ctx, &nfsi->open_files, list) {
1144 if (ctx->state != state)
1146 get_nfs_open_context(ctx);
1147 spin_unlock(&state->inode->i_lock);
1150 spin_unlock(&state->inode->i_lock);
1151 return ERR_PTR(-ENOENT);
1154 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1156 struct nfs4_opendata *opendata;
1158 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1159 if (opendata == NULL)
1160 return ERR_PTR(-ENOMEM);
1161 opendata->state = state;
1162 atomic_inc(&state->count);
1166 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1168 struct nfs4_state *newstate;
1171 opendata->o_arg.open_flags = 0;
1172 opendata->o_arg.fmode = fmode;
1173 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1174 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1175 nfs4_init_opendata_res(opendata);
1176 ret = _nfs4_recover_proc_open(opendata);
1179 newstate = nfs4_opendata_to_nfs4_state(opendata);
1180 if (IS_ERR(newstate))
1181 return PTR_ERR(newstate);
1182 nfs4_close_state(newstate, fmode);
1187 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1189 struct nfs4_state *newstate;
1192 /* memory barrier prior to reading state->n_* */
1193 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1195 if (state->n_rdwr != 0) {
1196 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1197 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1200 if (newstate != state)
1203 if (state->n_wronly != 0) {
1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1208 if (newstate != state)
1211 if (state->n_rdonly != 0) {
1212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1213 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1216 if (newstate != state)
1220 * We may have performed cached opens for all three recoveries.
1221 * Check if we need to update the current stateid.
1223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1224 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1225 write_seqlock(&state->seqlock);
1226 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1227 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1228 write_sequnlock(&state->seqlock);
1235 * reclaim state on the server after a reboot.
1237 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1239 struct nfs_delegation *delegation;
1240 struct nfs4_opendata *opendata;
1241 fmode_t delegation_type = 0;
1244 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1245 if (IS_ERR(opendata))
1246 return PTR_ERR(opendata);
1247 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1248 opendata->o_arg.fh = NFS_FH(state->inode);
1250 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1251 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1252 delegation_type = delegation->type;
1254 opendata->o_arg.u.delegation_type = delegation_type;
1255 status = nfs4_open_recover(opendata, state);
1256 nfs4_opendata_put(opendata);
1260 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1262 struct nfs_server *server = NFS_SERVER(state->inode);
1263 struct nfs4_exception exception = { };
1266 err = _nfs4_do_open_reclaim(ctx, state);
1267 if (err != -NFS4ERR_DELAY)
1269 nfs4_handle_exception(server, err, &exception);
1270 } while (exception.retry);
1274 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1276 struct nfs_open_context *ctx;
1279 ctx = nfs4_state_find_open_context(state);
1281 return PTR_ERR(ctx);
1282 ret = nfs4_do_open_reclaim(ctx, state);
1283 put_nfs_open_context(ctx);
1287 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1289 struct nfs4_opendata *opendata;
1292 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1293 if (IS_ERR(opendata))
1294 return PTR_ERR(opendata);
1295 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1296 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1297 sizeof(opendata->o_arg.u.delegation.data));
1298 ret = nfs4_open_recover(opendata, state);
1299 nfs4_opendata_put(opendata);
1303 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1305 struct nfs4_exception exception = { };
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1309 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1315 case -NFS4ERR_BADSESSION:
1316 case -NFS4ERR_BADSLOT:
1317 case -NFS4ERR_BAD_HIGH_SLOT:
1318 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1319 case -NFS4ERR_DEADSESSION:
1320 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1322 case -NFS4ERR_STALE_CLIENTID:
1323 case -NFS4ERR_STALE_STATEID:
1324 case -NFS4ERR_EXPIRED:
1325 /* Don't recall a delegation if it was lost */
1326 nfs4_schedule_lease_recovery(server->nfs_client);
1330 * The show must go on: exit, but mark the
1331 * stateid as needing recovery.
1333 case -NFS4ERR_ADMIN_REVOKED:
1334 case -NFS4ERR_BAD_STATEID:
1335 nfs4_schedule_stateid_recovery(server, state);
1338 * User RPCSEC_GSS context has expired.
1339 * We cannot recover this stateid now, so
1340 * skip it and allow recovery thread to
1347 err = nfs4_handle_exception(server, err, &exception);
1348 } while (exception.retry);
1353 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1355 struct nfs4_opendata *data = calldata;
1357 data->rpc_status = task->tk_status;
1358 if (data->rpc_status == 0) {
1359 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1360 sizeof(data->o_res.stateid.data));
1361 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1362 renew_lease(data->o_res.server, data->timestamp);
1367 static void nfs4_open_confirm_release(void *calldata)
1369 struct nfs4_opendata *data = calldata;
1370 struct nfs4_state *state = NULL;
1372 /* If this request hasn't been cancelled, do nothing */
1373 if (data->cancelled == 0)
1375 /* In case of error, no cleanup! */
1376 if (!data->rpc_done)
1378 state = nfs4_opendata_to_nfs4_state(data);
1380 nfs4_close_state(state, data->o_arg.fmode);
1382 nfs4_opendata_put(data);
1385 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1386 .rpc_call_done = nfs4_open_confirm_done,
1387 .rpc_release = nfs4_open_confirm_release,
1391 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1393 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1395 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1396 struct rpc_task *task;
1397 struct rpc_message msg = {
1398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1399 .rpc_argp = &data->c_arg,
1400 .rpc_resp = &data->c_res,
1401 .rpc_cred = data->owner->so_cred,
1403 struct rpc_task_setup task_setup_data = {
1404 .rpc_client = server->client,
1405 .rpc_message = &msg,
1406 .callback_ops = &nfs4_open_confirm_ops,
1407 .callback_data = data,
1408 .workqueue = nfsiod_workqueue,
1409 .flags = RPC_TASK_ASYNC,
1413 kref_get(&data->kref);
1415 data->rpc_status = 0;
1416 data->timestamp = jiffies;
1417 task = rpc_run_task(&task_setup_data);
1419 return PTR_ERR(task);
1420 status = nfs4_wait_for_completion_rpc_task(task);
1422 data->cancelled = 1;
1425 status = data->rpc_status;
1430 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1432 struct nfs4_opendata *data = calldata;
1433 struct nfs4_state_owner *sp = data->owner;
1435 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1438 * Check if we still need to send an OPEN call, or if we can use
1439 * a delegation instead.
1441 if (data->state != NULL) {
1442 struct nfs_delegation *delegation;
1444 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1447 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1448 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1449 can_open_delegated(delegation, data->o_arg.fmode))
1450 goto unlock_no_action;
1453 /* Update sequence id. */
1454 data->o_arg.id = sp->so_seqid.owner_id;
1455 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1456 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1457 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1458 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1460 data->timestamp = jiffies;
1461 if (nfs4_setup_sequence(data->o_arg.server,
1462 &data->o_arg.seq_args,
1463 &data->o_res.seq_res, task))
1465 rpc_call_start(task);
1470 task->tk_action = NULL;
1474 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1476 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1477 nfs4_open_prepare(task, calldata);
1480 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1482 struct nfs4_opendata *data = calldata;
1484 data->rpc_status = task->tk_status;
1486 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1489 if (task->tk_status == 0) {
1490 switch (data->o_res.f_attr->mode & S_IFMT) {
1494 data->rpc_status = -ELOOP;
1497 data->rpc_status = -EISDIR;
1500 data->rpc_status = -ENOTDIR;
1502 renew_lease(data->o_res.server, data->timestamp);
1503 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1504 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1509 static void nfs4_open_release(void *calldata)
1511 struct nfs4_opendata *data = calldata;
1512 struct nfs4_state *state = NULL;
1514 /* If this request hasn't been cancelled, do nothing */
1515 if (data->cancelled == 0)
1517 /* In case of error, no cleanup! */
1518 if (data->rpc_status != 0 || !data->rpc_done)
1520 /* In case we need an open_confirm, no cleanup! */
1521 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1523 state = nfs4_opendata_to_nfs4_state(data);
1525 nfs4_close_state(state, data->o_arg.fmode);
1527 nfs4_opendata_put(data);
1530 static const struct rpc_call_ops nfs4_open_ops = {
1531 .rpc_call_prepare = nfs4_open_prepare,
1532 .rpc_call_done = nfs4_open_done,
1533 .rpc_release = nfs4_open_release,
1536 static const struct rpc_call_ops nfs4_recover_open_ops = {
1537 .rpc_call_prepare = nfs4_recover_open_prepare,
1538 .rpc_call_done = nfs4_open_done,
1539 .rpc_release = nfs4_open_release,
1542 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1544 struct inode *dir = data->dir->d_inode;
1545 struct nfs_server *server = NFS_SERVER(dir);
1546 struct nfs_openargs *o_arg = &data->o_arg;
1547 struct nfs_openres *o_res = &data->o_res;
1548 struct rpc_task *task;
1549 struct rpc_message msg = {
1550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1553 .rpc_cred = data->owner->so_cred,
1555 struct rpc_task_setup task_setup_data = {
1556 .rpc_client = server->client,
1557 .rpc_message = &msg,
1558 .callback_ops = &nfs4_open_ops,
1559 .callback_data = data,
1560 .workqueue = nfsiod_workqueue,
1561 .flags = RPC_TASK_ASYNC,
1565 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1566 kref_get(&data->kref);
1568 data->rpc_status = 0;
1569 data->cancelled = 0;
1571 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1572 task = rpc_run_task(&task_setup_data);
1574 return PTR_ERR(task);
1575 status = nfs4_wait_for_completion_rpc_task(task);
1577 data->cancelled = 1;
1580 status = data->rpc_status;
1586 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_openres *o_res = &data->o_res;
1592 status = nfs4_run_open_task(data, 1);
1593 if (status != 0 || !data->rpc_done)
1596 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1598 nfs_refresh_inode(dir, o_res->dir_attr);
1600 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1601 status = _nfs4_proc_open_confirm(data);
1610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1612 static int _nfs4_proc_open(struct nfs4_opendata *data)
1614 struct inode *dir = data->dir->d_inode;
1615 struct nfs_server *server = NFS_SERVER(dir);
1616 struct nfs_openargs *o_arg = &data->o_arg;
1617 struct nfs_openres *o_res = &data->o_res;
1620 status = nfs4_run_open_task(data, 0);
1621 if (!data->rpc_done)
1624 if (status == -NFS4ERR_BADNAME &&
1625 !(o_arg->open_flags & O_CREAT))
1630 nfs_fattr_map_and_free_names(server, &data->f_attr);
1632 if (o_arg->open_flags & O_CREAT) {
1633 update_changeattr(dir, &o_res->cinfo);
1634 nfs_post_op_update_inode(dir, o_res->dir_attr);
1636 nfs_refresh_inode(dir, o_res->dir_attr);
1637 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1638 server->caps &= ~NFS_CAP_POSIX_LOCK;
1639 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1640 status = _nfs4_proc_open_confirm(data);
1644 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1645 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1649 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1654 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1655 ret = nfs4_wait_clnt_recover(clp);
1658 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1659 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1661 nfs4_schedule_state_manager(clp);
1667 static int nfs4_recover_expired_lease(struct nfs_server *server)
1669 return nfs4_client_recover_expired_lease(server->nfs_client);
1674 * reclaim state on the server after a network partition.
1675 * Assumes caller holds the appropriate lock
1677 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1679 struct nfs4_opendata *opendata;
1682 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1683 if (IS_ERR(opendata))
1684 return PTR_ERR(opendata);
1685 ret = nfs4_open_recover(opendata, state);
1687 d_drop(ctx->dentry);
1688 nfs4_opendata_put(opendata);
1692 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1694 struct nfs_server *server = NFS_SERVER(state->inode);
1695 struct nfs4_exception exception = { };
1699 err = _nfs4_open_expired(ctx, state);
1703 case -NFS4ERR_GRACE:
1704 case -NFS4ERR_DELAY:
1705 nfs4_handle_exception(server, err, &exception);
1708 } while (exception.retry);
1713 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1715 struct nfs_open_context *ctx;
1718 ctx = nfs4_state_find_open_context(state);
1720 return PTR_ERR(ctx);
1721 ret = nfs4_do_open_expired(ctx, state);
1722 put_nfs_open_context(ctx);
1726 #if defined(CONFIG_NFS_V4_1)
1727 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1729 int status = NFS_OK;
1730 struct nfs_server *server = NFS_SERVER(state->inode);
1732 if (state->flags & flags) {
1733 status = nfs41_test_stateid(server, stateid);
1734 if (status != NFS_OK) {
1735 nfs41_free_stateid(server, stateid);
1736 state->flags &= ~flags;
1742 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1744 int deleg_status, open_status;
1745 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1746 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1748 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1749 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1751 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1753 return nfs4_open_expired(sp, state);
1758 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1759 * fields corresponding to attributes that were used to store the verifier.
1760 * Make sure we clobber those fields in the later setattr call
1762 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1764 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1765 !(sattr->ia_valid & ATTR_ATIME_SET))
1766 sattr->ia_valid |= ATTR_ATIME;
1768 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1769 !(sattr->ia_valid & ATTR_MTIME_SET))
1770 sattr->ia_valid |= ATTR_MTIME;
1774 * Returns a referenced nfs4_state
1776 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1778 struct nfs4_state_owner *sp;
1779 struct nfs4_state *state = NULL;
1780 struct nfs_server *server = NFS_SERVER(dir);
1781 struct nfs4_opendata *opendata;
1784 /* Protect against reboot recovery conflicts */
1786 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1788 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1791 status = nfs4_recover_expired_lease(server);
1793 goto err_put_state_owner;
1794 if (dentry->d_inode != NULL)
1795 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1797 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1798 if (opendata == NULL)
1799 goto err_put_state_owner;
1801 if (dentry->d_inode != NULL)
1802 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1804 status = _nfs4_proc_open(opendata);
1806 goto err_opendata_put;
1808 state = nfs4_opendata_to_nfs4_state(opendata);
1809 status = PTR_ERR(state);
1811 goto err_opendata_put;
1812 if (server->caps & NFS_CAP_POSIX_LOCK)
1813 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1815 if (opendata->o_arg.open_flags & O_EXCL) {
1816 nfs4_exclusive_attrset(opendata, sattr);
1818 nfs_fattr_init(opendata->o_res.f_attr);
1819 status = nfs4_do_setattr(state->inode, cred,
1820 opendata->o_res.f_attr, sattr,
1823 nfs_setattr_update_inode(state->inode, sattr);
1824 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1826 nfs4_opendata_put(opendata);
1827 nfs4_put_state_owner(sp);
1831 nfs4_opendata_put(opendata);
1832 err_put_state_owner:
1833 nfs4_put_state_owner(sp);
1840 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1842 struct nfs4_exception exception = { };
1843 struct nfs4_state *res;
1847 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1850 /* NOTE: BAD_SEQID means the server and client disagree about the
1851 * book-keeping w.r.t. state-changing operations
1852 * (OPEN/CLOSE/LOCK/LOCKU...)
1853 * It is actually a sign of a bug on the client or on the server.
1855 * If we receive a BAD_SEQID error in the particular case of
1856 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1857 * have unhashed the old state_owner for us, and that we can
1858 * therefore safely retry using a new one. We should still warn
1859 * the user though...
1861 if (status == -NFS4ERR_BAD_SEQID) {
1862 printk(KERN_WARNING "NFS: v4 server %s "
1863 " returned a bad sequence-id error!\n",
1864 NFS_SERVER(dir)->nfs_client->cl_hostname);
1865 exception.retry = 1;
1869 * BAD_STATEID on OPEN means that the server cancelled our
1870 * state before it received the OPEN_CONFIRM.
1871 * Recover by retrying the request as per the discussion
1872 * on Page 181 of RFC3530.
1874 if (status == -NFS4ERR_BAD_STATEID) {
1875 exception.retry = 1;
1878 if (status == -EAGAIN) {
1879 /* We must have found a delegation */
1880 exception.retry = 1;
1883 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1884 status, &exception));
1885 } while (exception.retry);
1889 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1890 struct nfs_fattr *fattr, struct iattr *sattr,
1891 struct nfs4_state *state)
1893 struct nfs_server *server = NFS_SERVER(inode);
1894 struct nfs_setattrargs arg = {
1895 .fh = NFS_FH(inode),
1898 .bitmask = server->attr_bitmask,
1900 struct nfs_setattrres res = {
1904 struct rpc_message msg = {
1905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1910 unsigned long timestamp = jiffies;
1913 nfs_fattr_init(fattr);
1915 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1916 /* Use that stateid */
1917 } else if (state != NULL) {
1918 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1920 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1923 if (status == 0 && state != NULL)
1924 renew_lease(server, timestamp);
1928 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1929 struct nfs_fattr *fattr, struct iattr *sattr,
1930 struct nfs4_state *state)
1932 struct nfs_server *server = NFS_SERVER(inode);
1933 struct nfs4_exception exception = { };
1936 err = nfs4_handle_exception(server,
1937 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1939 } while (exception.retry);
1943 struct nfs4_closedata {
1944 struct inode *inode;
1945 struct nfs4_state *state;
1946 struct nfs_closeargs arg;
1947 struct nfs_closeres res;
1948 struct nfs_fattr fattr;
1949 unsigned long timestamp;
1954 static void nfs4_free_closedata(void *data)
1956 struct nfs4_closedata *calldata = data;
1957 struct nfs4_state_owner *sp = calldata->state->owner;
1958 struct super_block *sb = calldata->state->inode->i_sb;
1961 pnfs_roc_release(calldata->state->inode);
1962 nfs4_put_open_state(calldata->state);
1963 nfs_free_seqid(calldata->arg.seqid);
1964 nfs4_put_state_owner(sp);
1965 nfs_sb_deactive(sb);
1969 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1972 spin_lock(&state->owner->so_lock);
1973 if (!(fmode & FMODE_READ))
1974 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1975 if (!(fmode & FMODE_WRITE))
1976 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1977 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1978 spin_unlock(&state->owner->so_lock);
1981 static void nfs4_close_done(struct rpc_task *task, void *data)
1983 struct nfs4_closedata *calldata = data;
1984 struct nfs4_state *state = calldata->state;
1985 struct nfs_server *server = NFS_SERVER(calldata->inode);
1987 dprintk("%s: begin!\n", __func__);
1988 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1990 /* hmm. we are done with the inode, and in the process of freeing
1991 * the state_owner. we keep this around to process errors
1993 switch (task->tk_status) {
1996 pnfs_roc_set_barrier(state->inode,
1997 calldata->roc_barrier);
1998 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1999 renew_lease(server, calldata->timestamp);
2000 nfs4_close_clear_stateid_flags(state,
2001 calldata->arg.fmode);
2003 case -NFS4ERR_STALE_STATEID:
2004 case -NFS4ERR_OLD_STATEID:
2005 case -NFS4ERR_BAD_STATEID:
2006 case -NFS4ERR_EXPIRED:
2007 if (calldata->arg.fmode == 0)
2010 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2011 rpc_restart_call_prepare(task);
2013 nfs_release_seqid(calldata->arg.seqid);
2014 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2015 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2018 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2020 struct nfs4_closedata *calldata = data;
2021 struct nfs4_state *state = calldata->state;
2024 dprintk("%s: begin!\n", __func__);
2025 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2028 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2029 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2030 spin_lock(&state->owner->so_lock);
2031 /* Calculate the change in open mode */
2032 if (state->n_rdwr == 0) {
2033 if (state->n_rdonly == 0) {
2034 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2035 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2036 calldata->arg.fmode &= ~FMODE_READ;
2038 if (state->n_wronly == 0) {
2039 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2040 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2041 calldata->arg.fmode &= ~FMODE_WRITE;
2044 spin_unlock(&state->owner->so_lock);
2047 /* Note: exit _without_ calling nfs4_close_done */
2048 task->tk_action = NULL;
2052 if (calldata->arg.fmode == 0) {
2053 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2054 if (calldata->roc &&
2055 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2056 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2062 nfs_fattr_init(calldata->res.fattr);
2063 calldata->timestamp = jiffies;
2064 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2065 &calldata->arg.seq_args,
2066 &calldata->res.seq_res,
2069 rpc_call_start(task);
2071 dprintk("%s: done!\n", __func__);
2074 static const struct rpc_call_ops nfs4_close_ops = {
2075 .rpc_call_prepare = nfs4_close_prepare,
2076 .rpc_call_done = nfs4_close_done,
2077 .rpc_release = nfs4_free_closedata,
2081 * It is possible for data to be read/written from a mem-mapped file
2082 * after the sys_close call (which hits the vfs layer as a flush).
2083 * This means that we can't safely call nfsv4 close on a file until
2084 * the inode is cleared. This in turn means that we are not good
2085 * NFSv4 citizens - we do not indicate to the server to update the file's
2086 * share state even when we are done with one of the three share
2087 * stateid's in the inode.
2089 * NOTE: Caller must be holding the sp->so_owner semaphore!
2091 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2093 struct nfs_server *server = NFS_SERVER(state->inode);
2094 struct nfs4_closedata *calldata;
2095 struct nfs4_state_owner *sp = state->owner;
2096 struct rpc_task *task;
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2099 .rpc_cred = state->owner->so_cred,
2101 struct rpc_task_setup task_setup_data = {
2102 .rpc_client = server->client,
2103 .rpc_message = &msg,
2104 .callback_ops = &nfs4_close_ops,
2105 .workqueue = nfsiod_workqueue,
2106 .flags = RPC_TASK_ASYNC,
2108 int status = -ENOMEM;
2110 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2111 if (calldata == NULL)
2113 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2114 calldata->inode = state->inode;
2115 calldata->state = state;
2116 calldata->arg.fh = NFS_FH(state->inode);
2117 calldata->arg.stateid = &state->open_stateid;
2118 /* Serialization for the sequence id */
2119 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2120 if (calldata->arg.seqid == NULL)
2121 goto out_free_calldata;
2122 calldata->arg.fmode = 0;
2123 calldata->arg.bitmask = server->cache_consistency_bitmask;
2124 calldata->res.fattr = &calldata->fattr;
2125 calldata->res.seqid = calldata->arg.seqid;
2126 calldata->res.server = server;
2127 calldata->roc = roc;
2128 nfs_sb_active(calldata->inode->i_sb);
2130 msg.rpc_argp = &calldata->arg;
2131 msg.rpc_resp = &calldata->res;
2132 task_setup_data.callback_data = calldata;
2133 task = rpc_run_task(&task_setup_data);
2135 return PTR_ERR(task);
2138 status = rpc_wait_for_completion_task(task);
2145 pnfs_roc_release(state->inode);
2146 nfs4_put_open_state(state);
2147 nfs4_put_state_owner(sp);
2151 static struct inode *
2152 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2154 struct nfs4_state *state;
2156 /* Protect against concurrent sillydeletes */
2157 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2159 return ERR_CAST(state);
2161 return igrab(state->inode);
2164 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2166 if (ctx->state == NULL)
2169 nfs4_close_sync(ctx->state, ctx->mode);
2171 nfs4_close_state(ctx->state, ctx->mode);
2174 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2176 struct nfs4_server_caps_arg args = {
2179 struct nfs4_server_caps_res res = {};
2180 struct rpc_message msg = {
2181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2187 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2189 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2190 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2191 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2192 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2193 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2194 NFS_CAP_CTIME|NFS_CAP_MTIME);
2195 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2196 server->caps |= NFS_CAP_ACLS;
2197 if (res.has_links != 0)
2198 server->caps |= NFS_CAP_HARDLINKS;
2199 if (res.has_symlinks != 0)
2200 server->caps |= NFS_CAP_SYMLINKS;
2201 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2202 server->caps |= NFS_CAP_FILEID;
2203 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2204 server->caps |= NFS_CAP_MODE;
2205 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2206 server->caps |= NFS_CAP_NLINK;
2207 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2208 server->caps |= NFS_CAP_OWNER;
2209 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2210 server->caps |= NFS_CAP_OWNER_GROUP;
2211 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2212 server->caps |= NFS_CAP_ATIME;
2213 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2214 server->caps |= NFS_CAP_CTIME;
2215 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2216 server->caps |= NFS_CAP_MTIME;
2218 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2219 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2220 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2221 server->acl_bitmask = res.acl_bitmask;
2227 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2229 struct nfs4_exception exception = { };
2232 err = nfs4_handle_exception(server,
2233 _nfs4_server_capabilities(server, fhandle),
2235 } while (exception.retry);
2239 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2240 struct nfs_fsinfo *info)
2242 struct nfs4_lookup_root_arg args = {
2243 .bitmask = nfs4_fattr_bitmap,
2245 struct nfs4_lookup_res res = {
2247 .fattr = info->fattr,
2250 struct rpc_message msg = {
2251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2256 nfs_fattr_init(info->fattr);
2257 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2260 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2261 struct nfs_fsinfo *info)
2263 struct nfs4_exception exception = { };
2266 err = _nfs4_lookup_root(server, fhandle, info);
2269 case -NFS4ERR_WRONGSEC:
2272 err = nfs4_handle_exception(server, err, &exception);
2274 } while (exception.retry);
2278 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2279 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2281 struct rpc_auth *auth;
2284 auth = rpcauth_create(flavor, server->client);
2289 ret = nfs4_lookup_root(server, fhandle, info);
2294 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2295 struct nfs_fsinfo *info)
2297 int i, len, status = 0;
2298 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2300 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2301 flav_array[len] = RPC_AUTH_NULL;
2304 for (i = 0; i < len; i++) {
2305 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2306 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2311 * -EACCESS could mean that the user doesn't have correct permissions
2312 * to access the mount. It could also mean that we tried to mount
2313 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2314 * existing mount programs don't handle -EACCES very well so it should
2315 * be mapped to -EPERM instead.
2317 if (status == -EACCES)
2323 * get the file handle for the "/" directory on the server
2325 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2326 struct nfs_fsinfo *info)
2328 int minor_version = server->nfs_client->cl_minorversion;
2329 int status = nfs4_lookup_root(server, fhandle, info);
2330 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2332 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2333 * by nfs4_map_errors() as this function exits.
2335 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2337 status = nfs4_server_capabilities(server, fhandle);
2339 status = nfs4_do_fsinfo(server, fhandle, info);
2340 return nfs4_map_errors(status);
2343 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2345 * Get locations and (maybe) other attributes of a referral.
2346 * Note that we'll actually follow the referral later when
2347 * we detect fsid mismatch in inode revalidation
2349 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2350 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2352 int status = -ENOMEM;
2353 struct page *page = NULL;
2354 struct nfs4_fs_locations *locations = NULL;
2356 page = alloc_page(GFP_KERNEL);
2359 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2360 if (locations == NULL)
2363 status = nfs4_proc_fs_locations(dir, name, locations, page);
2366 /* Make sure server returned a different fsid for the referral */
2367 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2368 dprintk("%s: server did not return a different fsid for"
2369 " a referral at %s\n", __func__, name->name);
2373 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2374 nfs_fixup_referral_attributes(&locations->fattr);
2376 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2377 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2378 memset(fhandle, 0, sizeof(struct nfs_fh));
2386 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2388 struct nfs4_getattr_arg args = {
2390 .bitmask = server->attr_bitmask,
2392 struct nfs4_getattr_res res = {
2396 struct rpc_message msg = {
2397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2402 nfs_fattr_init(fattr);
2403 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2406 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2408 struct nfs4_exception exception = { };
2411 err = nfs4_handle_exception(server,
2412 _nfs4_proc_getattr(server, fhandle, fattr),
2414 } while (exception.retry);
2419 * The file is not closed if it is opened due to the a request to change
2420 * the size of the file. The open call will not be needed once the
2421 * VFS layer lookup-intents are implemented.
2423 * Close is called when the inode is destroyed.
2424 * If we haven't opened the file for O_WRONLY, we
2425 * need to in the size_change case to obtain a stateid.
2428 * Because OPEN is always done by name in nfsv4, it is
2429 * possible that we opened a different file by the same
2430 * name. We can recognize this race condition, but we
2431 * can't do anything about it besides returning an error.
2433 * This will be fixed with VFS changes (lookup-intent).
2436 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2437 struct iattr *sattr)
2439 struct inode *inode = dentry->d_inode;
2440 struct rpc_cred *cred = NULL;
2441 struct nfs4_state *state = NULL;
2444 if (pnfs_ld_layoutret_on_setattr(inode))
2445 pnfs_return_layout(inode);
2447 nfs_fattr_init(fattr);
2449 /* Search for an existing open(O_WRITE) file */
2450 if (sattr->ia_valid & ATTR_FILE) {
2451 struct nfs_open_context *ctx;
2453 ctx = nfs_file_open_context(sattr->ia_file);
2460 /* Deal with open(O_TRUNC) */
2461 if (sattr->ia_valid & ATTR_OPEN)
2462 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2464 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2466 nfs_setattr_update_inode(inode, sattr);
2470 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2471 const struct qstr *name, struct nfs_fh *fhandle,
2472 struct nfs_fattr *fattr)
2474 struct nfs_server *server = NFS_SERVER(dir);
2476 struct nfs4_lookup_arg args = {
2477 .bitmask = server->attr_bitmask,
2478 .dir_fh = NFS_FH(dir),
2481 struct nfs4_lookup_res res = {
2486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2492 nfs_fattr_init(fattr);
2494 dprintk("NFS call lookup %s\n", name->name);
2495 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2496 dprintk("NFS reply lookup: %d\n", status);
2500 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2502 memset(fh, 0, sizeof(struct nfs_fh));
2503 fattr->fsid.major = 1;
2504 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2505 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2506 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2510 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2511 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2513 struct nfs4_exception exception = { };
2518 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2520 case -NFS4ERR_BADNAME:
2522 case -NFS4ERR_MOVED:
2523 return nfs4_get_referral(dir, name, fattr, fhandle);
2524 case -NFS4ERR_WRONGSEC:
2525 nfs_fixup_secinfo_attributes(fattr, fhandle);
2527 err = nfs4_handle_exception(NFS_SERVER(dir),
2528 status, &exception);
2529 } while (exception.retry);
2533 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2535 struct nfs_server *server = NFS_SERVER(inode);
2536 struct nfs4_accessargs args = {
2537 .fh = NFS_FH(inode),
2538 .bitmask = server->cache_consistency_bitmask,
2540 struct nfs4_accessres res = {
2543 struct rpc_message msg = {
2544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2547 .rpc_cred = entry->cred,
2549 int mode = entry->mask;
2553 * Determine which access bits we want to ask for...
2555 if (mode & MAY_READ)
2556 args.access |= NFS4_ACCESS_READ;
2557 if (S_ISDIR(inode->i_mode)) {
2558 if (mode & MAY_WRITE)
2559 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2560 if (mode & MAY_EXEC)
2561 args.access |= NFS4_ACCESS_LOOKUP;
2563 if (mode & MAY_WRITE)
2564 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2565 if (mode & MAY_EXEC)
2566 args.access |= NFS4_ACCESS_EXECUTE;
2569 res.fattr = nfs_alloc_fattr();
2570 if (res.fattr == NULL)
2573 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2576 if (res.access & NFS4_ACCESS_READ)
2577 entry->mask |= MAY_READ;
2578 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2579 entry->mask |= MAY_WRITE;
2580 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2581 entry->mask |= MAY_EXEC;
2582 nfs_refresh_inode(inode, res.fattr);
2584 nfs_free_fattr(res.fattr);
2588 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2590 struct nfs4_exception exception = { };
2593 err = nfs4_handle_exception(NFS_SERVER(inode),
2594 _nfs4_proc_access(inode, entry),
2596 } while (exception.retry);
2601 * TODO: For the time being, we don't try to get any attributes
2602 * along with any of the zero-copy operations READ, READDIR,
2605 * In the case of the first three, we want to put the GETATTR
2606 * after the read-type operation -- this is because it is hard
2607 * to predict the length of a GETATTR response in v4, and thus
2608 * align the READ data correctly. This means that the GETATTR
2609 * may end up partially falling into the page cache, and we should
2610 * shift it into the 'tail' of the xdr_buf before processing.
2611 * To do this efficiently, we need to know the total length
2612 * of data received, which doesn't seem to be available outside
2615 * In the case of WRITE, we also want to put the GETATTR after
2616 * the operation -- in this case because we want to make sure
2617 * we get the post-operation mtime and size. This means that
2618 * we can't use xdr_encode_pages() as written: we need a variant
2619 * of it which would leave room in the 'tail' iovec.
2621 * Both of these changes to the XDR layer would in fact be quite
2622 * minor, but I decided to leave them for a subsequent patch.
2624 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2625 unsigned int pgbase, unsigned int pglen)
2627 struct nfs4_readlink args = {
2628 .fh = NFS_FH(inode),
2633 struct nfs4_readlink_res res;
2634 struct rpc_message msg = {
2635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2640 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2643 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2644 unsigned int pgbase, unsigned int pglen)
2646 struct nfs4_exception exception = { };
2649 err = nfs4_handle_exception(NFS_SERVER(inode),
2650 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2652 } while (exception.retry);
2658 * We will need to arrange for the VFS layer to provide an atomic open.
2659 * Until then, this create/open method is prone to inefficiency and race
2660 * conditions due to the lookup, create, and open VFS calls from sys_open()
2661 * placed on the wire.
2663 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2664 * The file will be opened again in the subsequent VFS open call
2665 * (nfs4_proc_file_open).
2667 * The open for read will just hang around to be used by any process that
2668 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2672 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2673 int flags, struct nfs_open_context *ctx)
2675 struct dentry *de = dentry;
2676 struct nfs4_state *state;
2677 struct rpc_cred *cred = NULL;
2686 sattr->ia_mode &= ~current_umask();
2687 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2689 if (IS_ERR(state)) {
2690 status = PTR_ERR(state);
2693 d_add(dentry, igrab(state->inode));
2694 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2698 nfs4_close_sync(state, fmode);
2703 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2705 struct nfs_server *server = NFS_SERVER(dir);
2706 struct nfs_removeargs args = {
2708 .name.len = name->len,
2709 .name.name = name->name,
2710 .bitmask = server->attr_bitmask,
2712 struct nfs_removeres res = {
2715 struct rpc_message msg = {
2716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2720 int status = -ENOMEM;
2722 res.dir_attr = nfs_alloc_fattr();
2723 if (res.dir_attr == NULL)
2726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2728 update_changeattr(dir, &res.cinfo);
2729 nfs_post_op_update_inode(dir, res.dir_attr);
2731 nfs_free_fattr(res.dir_attr);
2736 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2738 struct nfs4_exception exception = { };
2741 err = nfs4_handle_exception(NFS_SERVER(dir),
2742 _nfs4_proc_remove(dir, name),
2744 } while (exception.retry);
2748 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2750 struct nfs_server *server = NFS_SERVER(dir);
2751 struct nfs_removeargs *args = msg->rpc_argp;
2752 struct nfs_removeres *res = msg->rpc_resp;
2754 args->bitmask = server->cache_consistency_bitmask;
2755 res->server = server;
2756 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2757 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2760 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2762 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2764 if (!nfs4_sequence_done(task, &res->seq_res))
2766 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2768 update_changeattr(dir, &res->cinfo);
2769 nfs_post_op_update_inode(dir, res->dir_attr);
2773 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2775 struct nfs_server *server = NFS_SERVER(dir);
2776 struct nfs_renameargs *arg = msg->rpc_argp;
2777 struct nfs_renameres *res = msg->rpc_resp;
2779 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2780 arg->bitmask = server->attr_bitmask;
2781 res->server = server;
2782 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2785 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2786 struct inode *new_dir)
2788 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2790 if (!nfs4_sequence_done(task, &res->seq_res))
2792 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2795 update_changeattr(old_dir, &res->old_cinfo);
2796 nfs_post_op_update_inode(old_dir, res->old_fattr);
2797 update_changeattr(new_dir, &res->new_cinfo);
2798 nfs_post_op_update_inode(new_dir, res->new_fattr);
2802 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2803 struct inode *new_dir, struct qstr *new_name)
2805 struct nfs_server *server = NFS_SERVER(old_dir);
2806 struct nfs_renameargs arg = {
2807 .old_dir = NFS_FH(old_dir),
2808 .new_dir = NFS_FH(new_dir),
2809 .old_name = old_name,
2810 .new_name = new_name,
2811 .bitmask = server->attr_bitmask,
2813 struct nfs_renameres res = {
2816 struct rpc_message msg = {
2817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2821 int status = -ENOMEM;
2823 res.old_fattr = nfs_alloc_fattr();
2824 res.new_fattr = nfs_alloc_fattr();
2825 if (res.old_fattr == NULL || res.new_fattr == NULL)
2828 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2830 update_changeattr(old_dir, &res.old_cinfo);
2831 nfs_post_op_update_inode(old_dir, res.old_fattr);
2832 update_changeattr(new_dir, &res.new_cinfo);
2833 nfs_post_op_update_inode(new_dir, res.new_fattr);
2836 nfs_free_fattr(res.new_fattr);
2837 nfs_free_fattr(res.old_fattr);
2841 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2842 struct inode *new_dir, struct qstr *new_name)
2844 struct nfs4_exception exception = { };
2847 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2848 _nfs4_proc_rename(old_dir, old_name,
2851 } while (exception.retry);
2855 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2857 struct nfs_server *server = NFS_SERVER(inode);
2858 struct nfs4_link_arg arg = {
2859 .fh = NFS_FH(inode),
2860 .dir_fh = NFS_FH(dir),
2862 .bitmask = server->attr_bitmask,
2864 struct nfs4_link_res res = {
2867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2872 int status = -ENOMEM;
2874 res.fattr = nfs_alloc_fattr();
2875 res.dir_attr = nfs_alloc_fattr();
2876 if (res.fattr == NULL || res.dir_attr == NULL)
2879 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2881 update_changeattr(dir, &res.cinfo);
2882 nfs_post_op_update_inode(dir, res.dir_attr);
2883 nfs_post_op_update_inode(inode, res.fattr);
2886 nfs_free_fattr(res.dir_attr);
2887 nfs_free_fattr(res.fattr);
2891 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2893 struct nfs4_exception exception = { };
2896 err = nfs4_handle_exception(NFS_SERVER(inode),
2897 _nfs4_proc_link(inode, dir, name),
2899 } while (exception.retry);
2903 struct nfs4_createdata {
2904 struct rpc_message msg;
2905 struct nfs4_create_arg arg;
2906 struct nfs4_create_res res;
2908 struct nfs_fattr fattr;
2909 struct nfs_fattr dir_fattr;
2912 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2913 struct qstr *name, struct iattr *sattr, u32 ftype)
2915 struct nfs4_createdata *data;
2917 data = kzalloc(sizeof(*data), GFP_KERNEL);
2919 struct nfs_server *server = NFS_SERVER(dir);
2921 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2922 data->msg.rpc_argp = &data->arg;
2923 data->msg.rpc_resp = &data->res;
2924 data->arg.dir_fh = NFS_FH(dir);
2925 data->arg.server = server;
2926 data->arg.name = name;
2927 data->arg.attrs = sattr;
2928 data->arg.ftype = ftype;
2929 data->arg.bitmask = server->attr_bitmask;
2930 data->res.server = server;
2931 data->res.fh = &data->fh;
2932 data->res.fattr = &data->fattr;
2933 data->res.dir_fattr = &data->dir_fattr;
2934 nfs_fattr_init(data->res.fattr);
2935 nfs_fattr_init(data->res.dir_fattr);
2940 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2942 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2943 &data->arg.seq_args, &data->res.seq_res, 1);
2945 update_changeattr(dir, &data->res.dir_cinfo);
2946 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2947 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2952 static void nfs4_free_createdata(struct nfs4_createdata *data)
2957 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2958 struct page *page, unsigned int len, struct iattr *sattr)
2960 struct nfs4_createdata *data;
2961 int status = -ENAMETOOLONG;
2963 if (len > NFS4_MAXPATHLEN)
2967 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2971 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2972 data->arg.u.symlink.pages = &page;
2973 data->arg.u.symlink.len = len;
2975 status = nfs4_do_create(dir, dentry, data);
2977 nfs4_free_createdata(data);
2982 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2983 struct page *page, unsigned int len, struct iattr *sattr)
2985 struct nfs4_exception exception = { };
2988 err = nfs4_handle_exception(NFS_SERVER(dir),
2989 _nfs4_proc_symlink(dir, dentry, page,
2992 } while (exception.retry);
2996 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2997 struct iattr *sattr)
2999 struct nfs4_createdata *data;
3000 int status = -ENOMEM;
3002 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3006 status = nfs4_do_create(dir, dentry, data);
3008 nfs4_free_createdata(data);
3013 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3014 struct iattr *sattr)
3016 struct nfs4_exception exception = { };
3019 sattr->ia_mode &= ~current_umask();
3021 err = nfs4_handle_exception(NFS_SERVER(dir),
3022 _nfs4_proc_mkdir(dir, dentry, sattr),
3024 } while (exception.retry);
3028 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3029 u64 cookie, struct page **pages, unsigned int count, int plus)
3031 struct inode *dir = dentry->d_inode;
3032 struct nfs4_readdir_arg args = {
3037 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3040 struct nfs4_readdir_res res;
3041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3049 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3050 dentry->d_parent->d_name.name,
3051 dentry->d_name.name,
3052 (unsigned long long)cookie);
3053 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3054 res.pgbase = args.pgbase;
3055 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3057 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3058 status += args.pgbase;
3061 nfs_invalidate_atime(dir);
3063 dprintk("%s: returns %d\n", __func__, status);
3067 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3068 u64 cookie, struct page **pages, unsigned int count, int plus)
3070 struct nfs4_exception exception = { };
3073 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3074 _nfs4_proc_readdir(dentry, cred, cookie,
3075 pages, count, plus),
3077 } while (exception.retry);
3081 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3082 struct iattr *sattr, dev_t rdev)
3084 struct nfs4_createdata *data;
3085 int mode = sattr->ia_mode;
3086 int status = -ENOMEM;
3088 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3089 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3091 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3096 data->arg.ftype = NF4FIFO;
3097 else if (S_ISBLK(mode)) {
3098 data->arg.ftype = NF4BLK;
3099 data->arg.u.device.specdata1 = MAJOR(rdev);
3100 data->arg.u.device.specdata2 = MINOR(rdev);
3102 else if (S_ISCHR(mode)) {
3103 data->arg.ftype = NF4CHR;
3104 data->arg.u.device.specdata1 = MAJOR(rdev);
3105 data->arg.u.device.specdata2 = MINOR(rdev);
3108 status = nfs4_do_create(dir, dentry, data);
3110 nfs4_free_createdata(data);
3115 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3116 struct iattr *sattr, dev_t rdev)
3118 struct nfs4_exception exception = { };
3121 sattr->ia_mode &= ~current_umask();
3123 err = nfs4_handle_exception(NFS_SERVER(dir),
3124 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3126 } while (exception.retry);
3130 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3131 struct nfs_fsstat *fsstat)
3133 struct nfs4_statfs_arg args = {
3135 .bitmask = server->attr_bitmask,
3137 struct nfs4_statfs_res res = {
3140 struct rpc_message msg = {
3141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3146 nfs_fattr_init(fsstat->fattr);
3147 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3150 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3152 struct nfs4_exception exception = { };
3155 err = nfs4_handle_exception(server,
3156 _nfs4_proc_statfs(server, fhandle, fsstat),
3158 } while (exception.retry);
3162 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_fsinfo *fsinfo)
3165 struct nfs4_fsinfo_arg args = {
3167 .bitmask = server->attr_bitmask,
3169 struct nfs4_fsinfo_res res = {
3172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3181 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3183 struct nfs4_exception exception = { };
3187 err = nfs4_handle_exception(server,
3188 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3190 } while (exception.retry);
3194 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3196 nfs_fattr_init(fsinfo->fattr);
3197 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3200 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3201 struct nfs_pathconf *pathconf)
3203 struct nfs4_pathconf_arg args = {
3205 .bitmask = server->attr_bitmask,
3207 struct nfs4_pathconf_res res = {
3208 .pathconf = pathconf,
3210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3216 /* None of the pathconf attributes are mandatory to implement */
3217 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3218 memset(pathconf, 0, sizeof(*pathconf));
3222 nfs_fattr_init(pathconf->fattr);
3223 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3226 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3227 struct nfs_pathconf *pathconf)
3229 struct nfs4_exception exception = { };
3233 err = nfs4_handle_exception(server,
3234 _nfs4_proc_pathconf(server, fhandle, pathconf),
3236 } while (exception.retry);
3240 void __nfs4_read_done_cb(struct nfs_read_data *data)
3242 nfs_invalidate_atime(data->inode);
3245 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3247 struct nfs_server *server = NFS_SERVER(data->inode);
3249 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3250 rpc_restart_call_prepare(task);
3254 __nfs4_read_done_cb(data);
3255 if (task->tk_status > 0)
3256 renew_lease(server, data->timestamp);
3260 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3263 dprintk("--> %s\n", __func__);
3265 if (!nfs4_sequence_done(task, &data->res.seq_res))
3268 return data->read_done_cb ? data->read_done_cb(task, data) :
3269 nfs4_read_done_cb(task, data);
3272 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3274 data->timestamp = jiffies;
3275 data->read_done_cb = nfs4_read_done_cb;
3276 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3277 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3280 /* Reset the the nfs_read_data to send the read to the MDS. */
3281 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3283 dprintk("%s Reset task for i/o through\n", __func__);
3284 put_lseg(data->lseg);
3286 /* offsets will differ in the dense stripe case */
3287 data->args.offset = data->mds_offset;
3288 data->ds_clp = NULL;
3289 data->args.fh = NFS_FH(data->inode);
3290 data->read_done_cb = nfs4_read_done_cb;
3291 task->tk_ops = data->mds_ops;
3292 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3294 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3296 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3298 struct inode *inode = data->inode;
3300 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3301 rpc_restart_call_prepare(task);
3304 if (task->tk_status >= 0) {
3305 renew_lease(NFS_SERVER(inode), data->timestamp);
3306 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3311 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3313 if (!nfs4_sequence_done(task, &data->res.seq_res))
3315 return data->write_done_cb ? data->write_done_cb(task, data) :
3316 nfs4_write_done_cb(task, data);
3319 /* Reset the the nfs_write_data to send the write to the MDS. */
3320 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3322 dprintk("%s Reset task for i/o through\n", __func__);
3323 put_lseg(data->lseg);
3325 data->ds_clp = NULL;
3326 data->write_done_cb = nfs4_write_done_cb;
3327 data->args.fh = NFS_FH(data->inode);
3328 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3329 data->args.offset = data->mds_offset;
3330 data->res.fattr = &data->fattr;
3331 task->tk_ops = data->mds_ops;
3332 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3334 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3336 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3338 struct nfs_server *server = NFS_SERVER(data->inode);
3341 data->args.bitmask = NULL;
3342 data->res.fattr = NULL;
3344 data->args.bitmask = server->cache_consistency_bitmask;
3345 if (!data->write_done_cb)
3346 data->write_done_cb = nfs4_write_done_cb;
3347 data->res.server = server;
3348 data->timestamp = jiffies;
3350 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3351 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3354 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3356 struct inode *inode = data->inode;
3358 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3359 rpc_restart_call_prepare(task);
3362 nfs_refresh_inode(inode, data->res.fattr);
3366 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3368 if (!nfs4_sequence_done(task, &data->res.seq_res))
3370 return data->write_done_cb(task, data);
3373 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3375 struct nfs_server *server = NFS_SERVER(data->inode);
3378 data->args.bitmask = NULL;
3379 data->res.fattr = NULL;
3381 data->args.bitmask = server->cache_consistency_bitmask;
3382 if (!data->write_done_cb)
3383 data->write_done_cb = nfs4_commit_done_cb;
3384 data->res.server = server;
3385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3386 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3389 struct nfs4_renewdata {
3390 struct nfs_client *client;
3391 unsigned long timestamp;
3395 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3396 * standalone procedure for queueing an asynchronous RENEW.
3398 static void nfs4_renew_release(void *calldata)
3400 struct nfs4_renewdata *data = calldata;
3401 struct nfs_client *clp = data->client;
3403 if (atomic_read(&clp->cl_count) > 1)
3404 nfs4_schedule_state_renewal(clp);
3405 nfs_put_client(clp);
3409 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3411 struct nfs4_renewdata *data = calldata;
3412 struct nfs_client *clp = data->client;
3413 unsigned long timestamp = data->timestamp;
3415 if (task->tk_status < 0) {
3416 /* Unless we're shutting down, schedule state recovery! */
3417 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3419 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3420 nfs4_schedule_lease_recovery(clp);
3423 nfs4_schedule_path_down_recovery(clp);
3425 do_renew_lease(clp, timestamp);
3428 static const struct rpc_call_ops nfs4_renew_ops = {
3429 .rpc_call_done = nfs4_renew_done,
3430 .rpc_release = nfs4_renew_release,
3433 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3435 struct rpc_message msg = {
3436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3440 struct nfs4_renewdata *data;
3442 if (renew_flags == 0)
3444 if (!atomic_inc_not_zero(&clp->cl_count))
3446 data = kmalloc(sizeof(*data), GFP_NOFS);
3450 data->timestamp = jiffies;
3451 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3452 &nfs4_renew_ops, data);
3455 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3457 struct rpc_message msg = {
3458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3462 unsigned long now = jiffies;
3465 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3468 do_renew_lease(clp, now);
3472 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3474 return (server->caps & NFS_CAP_ACLS)
3475 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3476 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3479 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3480 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3483 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3485 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3486 struct page **pages, unsigned int *pgbase)
3488 struct page *newpage, **spages;
3494 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3495 newpage = alloc_page(GFP_KERNEL);
3497 if (newpage == NULL)
3499 memcpy(page_address(newpage), buf, len);
3504 } while (buflen != 0);
3510 __free_page(spages[rc-1]);
3514 struct nfs4_cached_acl {
3520 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3522 struct nfs_inode *nfsi = NFS_I(inode);
3524 spin_lock(&inode->i_lock);
3525 kfree(nfsi->nfs4_acl);
3526 nfsi->nfs4_acl = acl;
3527 spin_unlock(&inode->i_lock);
3530 static void nfs4_zap_acl_attr(struct inode *inode)
3532 nfs4_set_cached_acl(inode, NULL);
3535 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3537 struct nfs_inode *nfsi = NFS_I(inode);
3538 struct nfs4_cached_acl *acl;
3541 spin_lock(&inode->i_lock);
3542 acl = nfsi->nfs4_acl;
3545 if (buf == NULL) /* user is just asking for length */
3547 if (acl->cached == 0)
3549 ret = -ERANGE; /* see getxattr(2) man page */
3550 if (acl->len > buflen)
3552 memcpy(buf, acl->data, acl->len);
3556 spin_unlock(&inode->i_lock);
3560 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3562 struct nfs4_cached_acl *acl;
3564 if (buf && acl_len <= PAGE_SIZE) {
3565 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3569 memcpy(acl->data, buf, acl_len);
3571 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3578 nfs4_set_cached_acl(inode, acl);
3582 * The getxattr API returns the required buffer length when called with a
3583 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3584 * the required buf. On a NULL buf, we send a page of data to the server
3585 * guessing that the ACL request can be serviced by a page. If so, we cache
3586 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3587 * the cache. If not so, we throw away the page, and cache the required
3588 * length. The next getxattr call will then produce another round trip to
3589 * the server, this time with the input buf of the required size.
3591 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3593 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3594 struct nfs_getaclargs args = {
3595 .fh = NFS_FH(inode),
3599 struct nfs_getaclres res = {
3603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3608 int ret = -ENOMEM, npages, i, acl_len = 0;
3610 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3611 /* As long as we're doing a round trip to the server anyway,
3612 * let's be prepared for a page of acl data. */
3616 for (i = 0; i < npages; i++) {
3617 pages[i] = alloc_page(GFP_KERNEL);
3622 /* for decoding across pages */
3623 args.acl_scratch = alloc_page(GFP_KERNEL);
3624 if (!args.acl_scratch)
3627 args.acl_len = npages * PAGE_SIZE;
3628 args.acl_pgbase = 0;
3629 /* Let decode_getfacl know not to fail if the ACL data is larger than
3630 * the page we send as a guess */
3632 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3633 resp_buf = page_address(pages[0]);
3635 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3636 __func__, buf, buflen, npages, args.acl_len);
3637 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3638 &msg, &args.seq_args, &res.seq_res, 0);
3642 acl_len = res.acl_len - res.acl_data_offset;
3643 if (acl_len > args.acl_len)
3644 nfs4_write_cached_acl(inode, NULL, acl_len);
3646 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3650 if (acl_len > buflen)
3652 _copy_from_pages(buf, pages, res.acl_data_offset,
3657 for (i = 0; i < npages; i++)
3659 __free_page(pages[i]);
3660 if (args.acl_scratch)
3661 __free_page(args.acl_scratch);
3665 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3667 struct nfs4_exception exception = { };
3670 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3673 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3674 } while (exception.retry);
3678 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3680 struct nfs_server *server = NFS_SERVER(inode);
3683 if (!nfs4_server_supports_acls(server))
3685 ret = nfs_revalidate_inode(server, inode);
3688 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3689 nfs_zap_acl_cache(inode);
3690 ret = nfs4_read_cached_acl(inode, buf, buflen);
3692 /* -ENOENT is returned if there is no ACL or if there is an ACL
3693 * but no cached acl data, just the acl length */
3695 return nfs4_get_acl_uncached(inode, buf, buflen);
3698 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3700 struct nfs_server *server = NFS_SERVER(inode);
3701 struct page *pages[NFS4ACL_MAXPAGES];
3702 struct nfs_setaclargs arg = {
3703 .fh = NFS_FH(inode),
3707 struct nfs_setaclres res;
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3715 if (!nfs4_server_supports_acls(server))
3717 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3720 nfs_inode_return_delegation(inode);
3721 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3724 * Free each page after tx, so the only ref left is
3725 * held by the network stack
3728 put_page(pages[i-1]);
3731 * Acl update can result in inode attribute update.
3732 * so mark the attribute cache invalid.
3734 spin_lock(&inode->i_lock);
3735 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3736 spin_unlock(&inode->i_lock);
3737 nfs_access_zap_cache(inode);
3738 nfs_zap_acl_cache(inode);
3742 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3744 struct nfs4_exception exception = { };
3747 err = nfs4_handle_exception(NFS_SERVER(inode),
3748 __nfs4_proc_set_acl(inode, buf, buflen),
3750 } while (exception.retry);
3755 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3757 struct nfs_client *clp = server->nfs_client;
3759 if (task->tk_status >= 0)
3761 switch(task->tk_status) {
3762 case -NFS4ERR_ADMIN_REVOKED:
3763 case -NFS4ERR_BAD_STATEID:
3764 case -NFS4ERR_OPENMODE:
3767 nfs4_schedule_stateid_recovery(server, state);
3768 goto wait_on_recovery;
3769 case -NFS4ERR_EXPIRED:
3771 nfs4_schedule_stateid_recovery(server, state);
3772 case -NFS4ERR_STALE_STATEID:
3773 case -NFS4ERR_STALE_CLIENTID:
3774 nfs4_schedule_lease_recovery(clp);
3775 goto wait_on_recovery;
3776 #if defined(CONFIG_NFS_V4_1)
3777 case -NFS4ERR_BADSESSION:
3778 case -NFS4ERR_BADSLOT:
3779 case -NFS4ERR_BAD_HIGH_SLOT:
3780 case -NFS4ERR_DEADSESSION:
3781 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3782 case -NFS4ERR_SEQ_FALSE_RETRY:
3783 case -NFS4ERR_SEQ_MISORDERED:
3784 dprintk("%s ERROR %d, Reset session\n", __func__,
3786 nfs4_schedule_session_recovery(clp->cl_session);
3787 task->tk_status = 0;
3789 #endif /* CONFIG_NFS_V4_1 */
3790 case -NFS4ERR_DELAY:
3791 nfs_inc_server_stats(server, NFSIOS_DELAY);
3792 case -NFS4ERR_GRACE:
3794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3795 task->tk_status = 0;
3797 case -NFS4ERR_RETRY_UNCACHED_REP:
3798 case -NFS4ERR_OLD_STATEID:
3799 task->tk_status = 0;
3802 task->tk_status = nfs4_map_errors(task->tk_status);
3805 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3806 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3807 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3808 task->tk_status = 0;
3812 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3813 unsigned short port, struct rpc_cred *cred,
3814 struct nfs4_setclientid_res *res)
3816 nfs4_verifier sc_verifier;
3817 struct nfs4_setclientid setclientid = {
3818 .sc_verifier = &sc_verifier,
3820 .sc_cb_ident = clp->cl_cb_ident,
3822 struct rpc_message msg = {
3823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3824 .rpc_argp = &setclientid,
3832 p = (__be32*)sc_verifier.data;
3833 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3834 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3838 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3839 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3841 rpc_peeraddr2str(clp->cl_rpcclient,
3843 rpc_peeraddr2str(clp->cl_rpcclient,
3845 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3846 clp->cl_id_uniquifier);
3847 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3848 sizeof(setclientid.sc_netid),
3849 rpc_peeraddr2str(clp->cl_rpcclient,
3850 RPC_DISPLAY_NETID));
3851 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3852 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3853 clp->cl_ipaddr, port >> 8, port & 255);
3856 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3857 if (status != -NFS4ERR_CLID_INUSE)
3860 ++clp->cl_id_uniquifier;
3864 ssleep(clp->cl_lease_time / HZ + 1);
3869 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3870 struct nfs4_setclientid_res *arg,
3871 struct rpc_cred *cred)
3873 struct nfs_fsinfo fsinfo;
3874 struct rpc_message msg = {
3875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3877 .rpc_resp = &fsinfo,
3884 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3886 spin_lock(&clp->cl_lock);
3887 clp->cl_lease_time = fsinfo.lease_time * HZ;
3888 clp->cl_last_renewal = now;
3889 spin_unlock(&clp->cl_lock);
3894 struct nfs4_delegreturndata {
3895 struct nfs4_delegreturnargs args;
3896 struct nfs4_delegreturnres res;
3898 nfs4_stateid stateid;
3899 unsigned long timestamp;
3900 struct nfs_fattr fattr;
3904 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3906 struct nfs4_delegreturndata *data = calldata;
3908 if (!nfs4_sequence_done(task, &data->res.seq_res))
3911 switch (task->tk_status) {
3912 case -NFS4ERR_STALE_STATEID:
3913 case -NFS4ERR_EXPIRED:
3915 renew_lease(data->res.server, data->timestamp);
3918 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3920 rpc_restart_call_prepare(task);
3924 data->rpc_status = task->tk_status;
3927 static void nfs4_delegreturn_release(void *calldata)
3932 #if defined(CONFIG_NFS_V4_1)
3933 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3935 struct nfs4_delegreturndata *d_data;
3937 d_data = (struct nfs4_delegreturndata *)data;
3939 if (nfs4_setup_sequence(d_data->res.server,
3940 &d_data->args.seq_args,
3941 &d_data->res.seq_res, task))
3943 rpc_call_start(task);
3945 #endif /* CONFIG_NFS_V4_1 */
3947 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3948 #if defined(CONFIG_NFS_V4_1)
3949 .rpc_call_prepare = nfs4_delegreturn_prepare,
3950 #endif /* CONFIG_NFS_V4_1 */
3951 .rpc_call_done = nfs4_delegreturn_done,
3952 .rpc_release = nfs4_delegreturn_release,
3955 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3957 struct nfs4_delegreturndata *data;
3958 struct nfs_server *server = NFS_SERVER(inode);
3959 struct rpc_task *task;
3960 struct rpc_message msg = {
3961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3964 struct rpc_task_setup task_setup_data = {
3965 .rpc_client = server->client,
3966 .rpc_message = &msg,
3967 .callback_ops = &nfs4_delegreturn_ops,
3968 .flags = RPC_TASK_ASYNC,
3972 data = kzalloc(sizeof(*data), GFP_NOFS);
3975 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3976 data->args.fhandle = &data->fh;
3977 data->args.stateid = &data->stateid;
3978 data->args.bitmask = server->attr_bitmask;
3979 nfs_copy_fh(&data->fh, NFS_FH(inode));
3980 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3981 data->res.fattr = &data->fattr;
3982 data->res.server = server;
3983 nfs_fattr_init(data->res.fattr);
3984 data->timestamp = jiffies;
3985 data->rpc_status = 0;
3987 task_setup_data.callback_data = data;
3988 msg.rpc_argp = &data->args;
3989 msg.rpc_resp = &data->res;
3990 task = rpc_run_task(&task_setup_data);
3992 return PTR_ERR(task);
3995 status = nfs4_wait_for_completion_rpc_task(task);
3998 status = data->rpc_status;
4001 nfs_refresh_inode(inode, &data->fattr);
4007 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4009 struct nfs_server *server = NFS_SERVER(inode);
4010 struct nfs4_exception exception = { };
4013 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4015 case -NFS4ERR_STALE_STATEID:
4016 case -NFS4ERR_EXPIRED:
4020 err = nfs4_handle_exception(server, err, &exception);
4021 } while (exception.retry);
4025 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4026 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4029 * sleep, with exponential backoff, and retry the LOCK operation.
4031 static unsigned long
4032 nfs4_set_lock_task_retry(unsigned long timeout)
4034 freezable_schedule_timeout_killable(timeout);
4036 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4037 return NFS4_LOCK_MAXTIMEOUT;
4041 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4043 struct inode *inode = state->inode;
4044 struct nfs_server *server = NFS_SERVER(inode);
4045 struct nfs_client *clp = server->nfs_client;
4046 struct nfs_lockt_args arg = {
4047 .fh = NFS_FH(inode),
4050 struct nfs_lockt_res res = {
4053 struct rpc_message msg = {
4054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4057 .rpc_cred = state->owner->so_cred,
4059 struct nfs4_lock_state *lsp;
4062 arg.lock_owner.clientid = clp->cl_clientid;
4063 status = nfs4_set_lock_state(state, request);
4066 lsp = request->fl_u.nfs4_fl.owner;
4067 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4068 arg.lock_owner.s_dev = server->s_dev;
4069 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4072 request->fl_type = F_UNLCK;
4074 case -NFS4ERR_DENIED:
4077 request->fl_ops->fl_release_private(request);
4082 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4084 struct nfs4_exception exception = { };
4088 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4089 _nfs4_proc_getlk(state, cmd, request),
4091 } while (exception.retry);
4095 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4098 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4100 res = posix_lock_file_wait(file, fl);
4103 res = flock_lock_file_wait(file, fl);
4111 struct nfs4_unlockdata {
4112 struct nfs_locku_args arg;
4113 struct nfs_locku_res res;
4114 struct nfs4_lock_state *lsp;
4115 struct nfs_open_context *ctx;
4116 struct file_lock fl;
4117 const struct nfs_server *server;
4118 unsigned long timestamp;
4121 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4122 struct nfs_open_context *ctx,
4123 struct nfs4_lock_state *lsp,
4124 struct nfs_seqid *seqid)
4126 struct nfs4_unlockdata *p;
4127 struct inode *inode = lsp->ls_state->inode;
4129 p = kzalloc(sizeof(*p), GFP_NOFS);
4132 p->arg.fh = NFS_FH(inode);
4134 p->arg.seqid = seqid;
4135 p->res.seqid = seqid;
4136 p->arg.stateid = &lsp->ls_stateid;
4138 atomic_inc(&lsp->ls_count);
4139 /* Ensure we don't close file until we're done freeing locks! */
4140 p->ctx = get_nfs_open_context(ctx);
4141 memcpy(&p->fl, fl, sizeof(p->fl));
4142 p->server = NFS_SERVER(inode);
4146 static void nfs4_locku_release_calldata(void *data)
4148 struct nfs4_unlockdata *calldata = data;
4149 nfs_free_seqid(calldata->arg.seqid);
4150 nfs4_put_lock_state(calldata->lsp);
4151 put_nfs_open_context(calldata->ctx);
4155 static void nfs4_locku_done(struct rpc_task *task, void *data)
4157 struct nfs4_unlockdata *calldata = data;
4159 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4161 switch (task->tk_status) {
4163 memcpy(calldata->lsp->ls_stateid.data,
4164 calldata->res.stateid.data,
4165 sizeof(calldata->lsp->ls_stateid.data));
4166 renew_lease(calldata->server, calldata->timestamp);
4168 case -NFS4ERR_BAD_STATEID:
4169 case -NFS4ERR_OLD_STATEID:
4170 case -NFS4ERR_STALE_STATEID:
4171 case -NFS4ERR_EXPIRED:
4174 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4175 rpc_restart_call_prepare(task);
4179 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4181 struct nfs4_unlockdata *calldata = data;
4183 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4185 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4186 /* Note: exit _without_ running nfs4_locku_done */
4187 task->tk_action = NULL;
4190 calldata->timestamp = jiffies;
4191 if (nfs4_setup_sequence(calldata->server,
4192 &calldata->arg.seq_args,
4193 &calldata->res.seq_res, task))
4195 rpc_call_start(task);
4198 static const struct rpc_call_ops nfs4_locku_ops = {
4199 .rpc_call_prepare = nfs4_locku_prepare,
4200 .rpc_call_done = nfs4_locku_done,
4201 .rpc_release = nfs4_locku_release_calldata,
4204 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4205 struct nfs_open_context *ctx,
4206 struct nfs4_lock_state *lsp,
4207 struct nfs_seqid *seqid)
4209 struct nfs4_unlockdata *data;
4210 struct rpc_message msg = {
4211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4212 .rpc_cred = ctx->cred,
4214 struct rpc_task_setup task_setup_data = {
4215 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4216 .rpc_message = &msg,
4217 .callback_ops = &nfs4_locku_ops,
4218 .workqueue = nfsiod_workqueue,
4219 .flags = RPC_TASK_ASYNC,
4222 /* Ensure this is an unlock - when canceling a lock, the
4223 * canceled lock is passed in, and it won't be an unlock.
4225 fl->fl_type = F_UNLCK;
4227 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4229 nfs_free_seqid(seqid);
4230 return ERR_PTR(-ENOMEM);
4233 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4234 msg.rpc_argp = &data->arg;
4235 msg.rpc_resp = &data->res;
4236 task_setup_data.callback_data = data;
4237 return rpc_run_task(&task_setup_data);
4240 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4242 struct nfs_inode *nfsi = NFS_I(state->inode);
4243 struct nfs_seqid *seqid;
4244 struct nfs4_lock_state *lsp;
4245 struct rpc_task *task;
4247 unsigned char fl_flags = request->fl_flags;
4249 status = nfs4_set_lock_state(state, request);
4250 /* Unlock _before_ we do the RPC call */
4251 request->fl_flags |= FL_EXISTS;
4252 down_read(&nfsi->rwsem);
4253 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4254 up_read(&nfsi->rwsem);
4257 up_read(&nfsi->rwsem);
4260 /* Is this a delegated lock? */
4261 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4263 lsp = request->fl_u.nfs4_fl.owner;
4264 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4268 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4269 status = PTR_ERR(task);
4272 status = nfs4_wait_for_completion_rpc_task(task);
4275 request->fl_flags = fl_flags;
4279 struct nfs4_lockdata {
4280 struct nfs_lock_args arg;
4281 struct nfs_lock_res res;
4282 struct nfs4_lock_state *lsp;
4283 struct nfs_open_context *ctx;
4284 struct file_lock fl;
4285 unsigned long timestamp;
4288 struct nfs_server *server;
4291 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4292 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4295 struct nfs4_lockdata *p;
4296 struct inode *inode = lsp->ls_state->inode;
4297 struct nfs_server *server = NFS_SERVER(inode);
4299 p = kzalloc(sizeof(*p), gfp_mask);
4303 p->arg.fh = NFS_FH(inode);
4305 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4306 if (p->arg.open_seqid == NULL)
4308 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4309 if (p->arg.lock_seqid == NULL)
4310 goto out_free_seqid;
4311 p->arg.lock_stateid = &lsp->ls_stateid;
4312 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4313 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4314 p->arg.lock_owner.s_dev = server->s_dev;
4315 p->res.lock_seqid = p->arg.lock_seqid;
4318 atomic_inc(&lsp->ls_count);
4319 p->ctx = get_nfs_open_context(ctx);
4320 memcpy(&p->fl, fl, sizeof(p->fl));
4323 nfs_free_seqid(p->arg.open_seqid);
4329 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4331 struct nfs4_lockdata *data = calldata;
4332 struct nfs4_state *state = data->lsp->ls_state;
4334 dprintk("%s: begin!\n", __func__);
4335 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4337 /* Do we need to do an open_to_lock_owner? */
4338 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4339 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4341 data->arg.open_stateid = &state->stateid;
4342 data->arg.new_lock_owner = 1;
4343 data->res.open_seqid = data->arg.open_seqid;
4345 data->arg.new_lock_owner = 0;
4346 data->timestamp = jiffies;
4347 if (nfs4_setup_sequence(data->server,
4348 &data->arg.seq_args,
4349 &data->res.seq_res, task))
4351 rpc_call_start(task);
4352 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4355 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4357 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4358 nfs4_lock_prepare(task, calldata);
4361 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4363 struct nfs4_lockdata *data = calldata;
4365 dprintk("%s: begin!\n", __func__);
4367 if (!nfs4_sequence_done(task, &data->res.seq_res))
4370 data->rpc_status = task->tk_status;
4371 if (data->arg.new_lock_owner != 0) {
4372 if (data->rpc_status == 0)
4373 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4377 if (data->rpc_status == 0) {
4378 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4379 sizeof(data->lsp->ls_stateid.data));
4380 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4381 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4384 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4387 static void nfs4_lock_release(void *calldata)
4389 struct nfs4_lockdata *data = calldata;
4391 dprintk("%s: begin!\n", __func__);
4392 nfs_free_seqid(data->arg.open_seqid);
4393 if (data->cancelled != 0) {
4394 struct rpc_task *task;
4395 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4396 data->arg.lock_seqid);
4398 rpc_put_task_async(task);
4399 dprintk("%s: cancelling lock!\n", __func__);
4401 nfs_free_seqid(data->arg.lock_seqid);
4402 nfs4_put_lock_state(data->lsp);
4403 put_nfs_open_context(data->ctx);
4405 dprintk("%s: done!\n", __func__);
4408 static const struct rpc_call_ops nfs4_lock_ops = {
4409 .rpc_call_prepare = nfs4_lock_prepare,
4410 .rpc_call_done = nfs4_lock_done,
4411 .rpc_release = nfs4_lock_release,
4414 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4415 .rpc_call_prepare = nfs4_recover_lock_prepare,
4416 .rpc_call_done = nfs4_lock_done,
4417 .rpc_release = nfs4_lock_release,
4420 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4423 case -NFS4ERR_ADMIN_REVOKED:
4424 case -NFS4ERR_BAD_STATEID:
4425 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4426 if (new_lock_owner != 0 ||
4427 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4428 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4430 case -NFS4ERR_STALE_STATEID:
4431 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4432 case -NFS4ERR_EXPIRED:
4433 nfs4_schedule_lease_recovery(server->nfs_client);
4437 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4439 struct nfs4_lockdata *data;
4440 struct rpc_task *task;
4441 struct rpc_message msg = {
4442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4443 .rpc_cred = state->owner->so_cred,
4445 struct rpc_task_setup task_setup_data = {
4446 .rpc_client = NFS_CLIENT(state->inode),
4447 .rpc_message = &msg,
4448 .callback_ops = &nfs4_lock_ops,
4449 .workqueue = nfsiod_workqueue,
4450 .flags = RPC_TASK_ASYNC,
4454 dprintk("%s: begin!\n", __func__);
4455 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4456 fl->fl_u.nfs4_fl.owner,
4457 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4461 data->arg.block = 1;
4462 if (recovery_type > NFS_LOCK_NEW) {
4463 if (recovery_type == NFS_LOCK_RECLAIM)
4464 data->arg.reclaim = NFS_LOCK_RECLAIM;
4465 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4467 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4468 msg.rpc_argp = &data->arg;
4469 msg.rpc_resp = &data->res;
4470 task_setup_data.callback_data = data;
4471 task = rpc_run_task(&task_setup_data);
4473 return PTR_ERR(task);
4474 ret = nfs4_wait_for_completion_rpc_task(task);
4476 ret = data->rpc_status;
4478 nfs4_handle_setlk_error(data->server, data->lsp,
4479 data->arg.new_lock_owner, ret);
4481 data->cancelled = 1;
4483 dprintk("%s: done, ret = %d!\n", __func__, ret);
4487 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4489 struct nfs_server *server = NFS_SERVER(state->inode);
4490 struct nfs4_exception exception = { };
4494 /* Cache the lock if possible... */
4495 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4497 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4498 if (err != -NFS4ERR_DELAY)
4500 nfs4_handle_exception(server, err, &exception);
4501 } while (exception.retry);
4505 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4507 struct nfs_server *server = NFS_SERVER(state->inode);
4508 struct nfs4_exception exception = { };
4511 err = nfs4_set_lock_state(state, request);
4515 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4517 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4521 case -NFS4ERR_GRACE:
4522 case -NFS4ERR_DELAY:
4523 nfs4_handle_exception(server, err, &exception);
4526 } while (exception.retry);
4531 #if defined(CONFIG_NFS_V4_1)
4532 static int nfs41_check_expired_locks(struct nfs4_state *state)
4534 int status, ret = NFS_OK;
4535 struct nfs4_lock_state *lsp;
4536 struct nfs_server *server = NFS_SERVER(state->inode);
4538 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4539 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4540 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4541 if (status != NFS_OK) {
4542 nfs41_free_stateid(server, &lsp->ls_stateid);
4543 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4552 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4554 int status = NFS_OK;
4556 if (test_bit(LK_STATE_IN_USE, &state->flags))
4557 status = nfs41_check_expired_locks(state);
4558 if (status == NFS_OK)
4560 return nfs4_lock_expired(state, request);
4564 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4566 struct nfs_inode *nfsi = NFS_I(state->inode);
4567 unsigned char fl_flags = request->fl_flags;
4568 int status = -ENOLCK;
4570 if ((fl_flags & FL_POSIX) &&
4571 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4573 /* Is this a delegated open? */
4574 status = nfs4_set_lock_state(state, request);
4577 request->fl_flags |= FL_ACCESS;
4578 status = do_vfs_lock(request->fl_file, request);
4581 down_read(&nfsi->rwsem);
4582 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4583 /* Yes: cache locks! */
4584 /* ...but avoid races with delegation recall... */
4585 request->fl_flags = fl_flags & ~FL_SLEEP;
4586 status = do_vfs_lock(request->fl_file, request);
4589 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4592 /* Note: we always want to sleep here! */
4593 request->fl_flags = fl_flags | FL_SLEEP;
4594 if (do_vfs_lock(request->fl_file, request) < 0)
4595 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4596 "manager!\n", __func__);
4598 up_read(&nfsi->rwsem);
4600 request->fl_flags = fl_flags;
4604 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4606 struct nfs4_exception exception = { };
4610 err = _nfs4_proc_setlk(state, cmd, request);
4611 if (err == -NFS4ERR_DENIED)
4613 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4615 } while (exception.retry);
4620 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4622 struct nfs_open_context *ctx;
4623 struct nfs4_state *state;
4624 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4627 /* verify open state */
4628 ctx = nfs_file_open_context(filp);
4631 if (request->fl_start < 0 || request->fl_end < 0)
4634 if (IS_GETLK(cmd)) {
4636 return nfs4_proc_getlk(state, F_GETLK, request);
4640 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4643 if (request->fl_type == F_UNLCK) {
4645 return nfs4_proc_unlck(state, cmd, request);
4652 status = nfs4_proc_setlk(state, cmd, request);
4653 if ((status != -EAGAIN) || IS_SETLK(cmd))
4655 timeout = nfs4_set_lock_task_retry(timeout);
4656 status = -ERESTARTSYS;
4659 } while(status < 0);
4663 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4665 struct nfs_server *server = NFS_SERVER(state->inode);
4666 struct nfs4_exception exception = { };
4669 err = nfs4_set_lock_state(state, fl);
4673 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4676 printk(KERN_ERR "NFS: %s: unhandled error "
4677 "%d.\n", __func__, err);
4681 case -NFS4ERR_EXPIRED:
4682 nfs4_schedule_stateid_recovery(server, state);
4683 case -NFS4ERR_STALE_CLIENTID:
4684 case -NFS4ERR_STALE_STATEID:
4685 nfs4_schedule_lease_recovery(server->nfs_client);
4687 case -NFS4ERR_BADSESSION:
4688 case -NFS4ERR_BADSLOT:
4689 case -NFS4ERR_BAD_HIGH_SLOT:
4690 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4691 case -NFS4ERR_DEADSESSION:
4692 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4696 * The show must go on: exit, but mark the
4697 * stateid as needing recovery.
4699 case -NFS4ERR_ADMIN_REVOKED:
4700 case -NFS4ERR_BAD_STATEID:
4701 case -NFS4ERR_OPENMODE:
4702 nfs4_schedule_stateid_recovery(server, state);
4707 * User RPCSEC_GSS context has expired.
4708 * We cannot recover this stateid now, so
4709 * skip it and allow recovery thread to
4715 case -NFS4ERR_DENIED:
4716 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4719 case -NFS4ERR_DELAY:
4722 err = nfs4_handle_exception(server, err, &exception);
4723 } while (exception.retry);
4728 static void nfs4_release_lockowner_release(void *calldata)
4733 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4734 .rpc_release = nfs4_release_lockowner_release,
4737 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4739 struct nfs_server *server = lsp->ls_state->owner->so_server;
4740 struct nfs_release_lockowner_args *args;
4741 struct rpc_message msg = {
4742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4745 if (server->nfs_client->cl_mvops->minor_version != 0)
4747 args = kmalloc(sizeof(*args), GFP_NOFS);
4750 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4751 args->lock_owner.id = lsp->ls_seqid.owner_id;
4752 args->lock_owner.s_dev = server->s_dev;
4753 msg.rpc_argp = args;
4754 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4757 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4759 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4760 const void *buf, size_t buflen,
4761 int flags, int type)
4763 if (strcmp(key, "") != 0)
4766 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4769 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4770 void *buf, size_t buflen, int type)
4772 if (strcmp(key, "") != 0)
4775 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4778 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4779 size_t list_len, const char *name,
4780 size_t name_len, int type)
4782 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4784 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4787 if (list && len <= list_len)
4788 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4793 * nfs_fhget will use either the mounted_on_fileid or the fileid
4795 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4797 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4798 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4799 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4800 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4803 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4804 NFS_ATTR_FATTR_NLINK;
4805 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4809 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4810 struct nfs4_fs_locations *fs_locations, struct page *page)
4812 struct nfs_server *server = NFS_SERVER(dir);
4814 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4816 struct nfs4_fs_locations_arg args = {
4817 .dir_fh = NFS_FH(dir),
4822 struct nfs4_fs_locations_res res = {
4823 .fs_locations = fs_locations,
4825 struct rpc_message msg = {
4826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4832 dprintk("%s: start\n", __func__);
4834 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4835 * is not supported */
4836 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4837 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4839 bitmask[0] |= FATTR4_WORD0_FILEID;
4841 nfs_fattr_init(&fs_locations->fattr);
4842 fs_locations->server = server;
4843 fs_locations->nlocations = 0;
4844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4845 dprintk("%s: returned status = %d\n", __func__, status);
4849 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4852 struct nfs4_secinfo_arg args = {
4853 .dir_fh = NFS_FH(dir),
4856 struct nfs4_secinfo_res res = {
4859 struct rpc_message msg = {
4860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4865 dprintk("NFS call secinfo %s\n", name->name);
4866 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4867 dprintk("NFS reply secinfo: %d\n", status);
4871 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4873 struct nfs4_exception exception = { };
4876 err = nfs4_handle_exception(NFS_SERVER(dir),
4877 _nfs4_proc_secinfo(dir, name, flavors),
4879 } while (exception.retry);
4883 #ifdef CONFIG_NFS_V4_1
4885 * Check the exchange flags returned by the server for invalid flags, having
4886 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4889 static int nfs4_check_cl_exchange_flags(u32 flags)
4891 if (flags & ~EXCHGID4_FLAG_MASK_R)
4893 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4894 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4896 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4900 return -NFS4ERR_INVAL;
4904 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4906 if (a->server_scope_sz == b->server_scope_sz &&
4907 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4914 * nfs4_proc_exchange_id()
4916 * Since the clientid has expired, all compounds using sessions
4917 * associated with the stale clientid will be returning
4918 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4919 * be in some phase of session reset.
4921 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4923 nfs4_verifier verifier;
4924 struct nfs41_exchange_id_args args = {
4926 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4928 struct nfs41_exchange_id_res res = {
4932 struct rpc_message msg = {
4933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4940 dprintk("--> %s\n", __func__);
4941 BUG_ON(clp == NULL);
4943 p = (u32 *)verifier.data;
4944 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4945 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4946 args.verifier = &verifier;
4948 args.id_len = scnprintf(args.id, sizeof(args.id),
4951 init_utsname()->nodename,
4952 init_utsname()->domainname,
4953 clp->cl_rpcclient->cl_auth->au_flavor);
4955 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4956 if (unlikely(!res.server_scope)) {
4961 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4962 if (unlikely(!res.impl_id)) {
4964 goto out_server_scope;
4967 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4969 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4972 /* use the most recent implementation id */
4973 kfree(clp->impl_id);
4974 clp->impl_id = res.impl_id;
4979 if (clp->server_scope &&
4980 !nfs41_same_server_scope(clp->server_scope,
4981 res.server_scope)) {
4982 dprintk("%s: server_scope mismatch detected\n",
4984 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4985 kfree(clp->server_scope);
4986 clp->server_scope = NULL;
4989 if (!clp->server_scope) {
4990 clp->server_scope = res.server_scope;
4996 kfree(res.server_scope);
4999 dprintk("%s: Server Implementation ID: "
5000 "domain: %s, name: %s, date: %llu,%u\n",
5001 __func__, clp->impl_id->domain, clp->impl_id->name,
5002 clp->impl_id->date.seconds,
5003 clp->impl_id->date.nseconds);
5004 dprintk("<-- %s status= %d\n", __func__, status);
5008 struct nfs4_get_lease_time_data {
5009 struct nfs4_get_lease_time_args *args;
5010 struct nfs4_get_lease_time_res *res;
5011 struct nfs_client *clp;
5014 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5018 struct nfs4_get_lease_time_data *data =
5019 (struct nfs4_get_lease_time_data *)calldata;
5021 dprintk("--> %s\n", __func__);
5022 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5023 /* just setup sequence, do not trigger session recovery
5024 since we're invoked within one */
5025 ret = nfs41_setup_sequence(data->clp->cl_session,
5026 &data->args->la_seq_args,
5027 &data->res->lr_seq_res, task);
5029 BUG_ON(ret == -EAGAIN);
5030 rpc_call_start(task);
5031 dprintk("<-- %s\n", __func__);
5035 * Called from nfs4_state_manager thread for session setup, so don't recover
5036 * from sequence operation or clientid errors.
5038 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5040 struct nfs4_get_lease_time_data *data =
5041 (struct nfs4_get_lease_time_data *)calldata;
5043 dprintk("--> %s\n", __func__);
5044 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5046 switch (task->tk_status) {
5047 case -NFS4ERR_DELAY:
5048 case -NFS4ERR_GRACE:
5049 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5050 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5051 task->tk_status = 0;
5053 case -NFS4ERR_RETRY_UNCACHED_REP:
5054 rpc_restart_call_prepare(task);
5057 dprintk("<-- %s\n", __func__);
5060 struct rpc_call_ops nfs4_get_lease_time_ops = {
5061 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5062 .rpc_call_done = nfs4_get_lease_time_done,
5065 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5067 struct rpc_task *task;
5068 struct nfs4_get_lease_time_args args;
5069 struct nfs4_get_lease_time_res res = {
5070 .lr_fsinfo = fsinfo,
5072 struct nfs4_get_lease_time_data data = {
5077 struct rpc_message msg = {
5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5082 struct rpc_task_setup task_setup = {
5083 .rpc_client = clp->cl_rpcclient,
5084 .rpc_message = &msg,
5085 .callback_ops = &nfs4_get_lease_time_ops,
5086 .callback_data = &data,
5087 .flags = RPC_TASK_TIMEOUT,
5091 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5092 dprintk("--> %s\n", __func__);
5093 task = rpc_run_task(&task_setup);
5096 status = PTR_ERR(task);
5098 status = task->tk_status;
5101 dprintk("<-- %s return %d\n", __func__, status);
5107 * Reset a slot table
5109 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5112 struct nfs4_slot *new = NULL;
5116 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5117 max_reqs, tbl->max_slots);
5119 /* Does the newly negotiated max_reqs match the existing slot table? */
5120 if (max_reqs != tbl->max_slots) {
5122 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
5129 spin_lock(&tbl->slot_tbl_lock);
5132 tbl->max_slots = max_reqs;
5134 for (i = 0; i < tbl->max_slots; ++i)
5135 tbl->slots[i].seq_nr = ivalue;
5136 spin_unlock(&tbl->slot_tbl_lock);
5137 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5138 tbl, tbl->slots, tbl->max_slots);
5140 dprintk("<-- %s: return %d\n", __func__, ret);
5144 /* Destroy the slot table */
5145 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5147 if (session->fc_slot_table.slots != NULL) {
5148 kfree(session->fc_slot_table.slots);
5149 session->fc_slot_table.slots = NULL;
5151 if (session->bc_slot_table.slots != NULL) {
5152 kfree(session->bc_slot_table.slots);
5153 session->bc_slot_table.slots = NULL;
5159 * Initialize slot table
5161 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5162 int max_slots, int ivalue)
5164 struct nfs4_slot *slot;
5167 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5169 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5171 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5176 spin_lock(&tbl->slot_tbl_lock);
5177 tbl->max_slots = max_slots;
5179 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
5180 spin_unlock(&tbl->slot_tbl_lock);
5181 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5182 tbl, tbl->slots, tbl->max_slots);
5184 dprintk("<-- %s: return %d\n", __func__, ret);
5189 * Initialize or reset the forechannel and backchannel tables
5191 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5193 struct nfs4_slot_table *tbl;
5196 dprintk("--> %s\n", __func__);
5198 tbl = &ses->fc_slot_table;
5199 if (tbl->slots == NULL) {
5200 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5201 if (status) /* -ENOMEM */
5204 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5209 tbl = &ses->bc_slot_table;
5210 if (tbl->slots == NULL) {
5211 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5213 /* Fore and back channel share a connection so get
5214 * both slot tables or neither */
5215 nfs4_destroy_slot_tables(ses);
5217 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5221 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5223 struct nfs4_session *session;
5224 struct nfs4_slot_table *tbl;
5226 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5230 tbl = &session->fc_slot_table;
5231 tbl->highest_used_slotid = NFS4_NO_SLOT;
5232 spin_lock_init(&tbl->slot_tbl_lock);
5233 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5234 init_completion(&tbl->complete);
5236 tbl = &session->bc_slot_table;
5237 tbl->highest_used_slotid = NFS4_NO_SLOT;
5238 spin_lock_init(&tbl->slot_tbl_lock);
5239 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5240 init_completion(&tbl->complete);
5242 session->session_state = 1<<NFS4_SESSION_INITING;
5248 void nfs4_destroy_session(struct nfs4_session *session)
5250 struct rpc_xprt *xprt;
5252 nfs4_proc_destroy_session(session);
5255 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5257 dprintk("%s Destroy backchannel for xprt %p\n",
5259 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5260 nfs4_destroy_slot_tables(session);
5265 * Initialize the values to be used by the client in CREATE_SESSION
5266 * If nfs4_init_session set the fore channel request and response sizes,
5269 * Set the back channel max_resp_sz_cached to zero to force the client to
5270 * always set csa_cachethis to FALSE because the current implementation
5271 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5273 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5275 struct nfs4_session *session = args->client->cl_session;
5276 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5277 mxresp_sz = session->fc_attrs.max_resp_sz;
5280 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5282 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5283 /* Fore channel attributes */
5284 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5285 args->fc_attrs.max_resp_sz = mxresp_sz;
5286 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5287 args->fc_attrs.max_reqs = max_session_slots;
5289 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5290 "max_ops=%u max_reqs=%u\n",
5292 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5293 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5295 /* Back channel attributes */
5296 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5297 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5298 args->bc_attrs.max_resp_sz_cached = 0;
5299 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5300 args->bc_attrs.max_reqs = 1;
5302 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5303 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5305 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5306 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5307 args->bc_attrs.max_reqs);
5310 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5312 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5313 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5315 if (rcvd->max_resp_sz > sent->max_resp_sz)
5318 * Our requested max_ops is the minimum we need; we're not
5319 * prepared to break up compounds into smaller pieces than that.
5320 * So, no point even trying to continue if the server won't
5323 if (rcvd->max_ops < sent->max_ops)
5325 if (rcvd->max_reqs == 0)
5327 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5328 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5332 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5334 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5335 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5337 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5339 if (rcvd->max_resp_sz < sent->max_resp_sz)
5341 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5343 /* These would render the backchannel useless: */
5344 if (rcvd->max_ops != sent->max_ops)
5346 if (rcvd->max_reqs != sent->max_reqs)
5351 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5352 struct nfs4_session *session)
5356 ret = nfs4_verify_fore_channel_attrs(args, session);
5359 return nfs4_verify_back_channel_attrs(args, session);
5362 static int _nfs4_proc_create_session(struct nfs_client *clp)
5364 struct nfs4_session *session = clp->cl_session;
5365 struct nfs41_create_session_args args = {
5367 .cb_program = NFS4_CALLBACK,
5369 struct nfs41_create_session_res res = {
5372 struct rpc_message msg = {
5373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5379 nfs4_init_channel_attrs(&args);
5380 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5382 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5385 /* Verify the session's negotiated channel_attrs values */
5386 status = nfs4_verify_channel_attrs(&args, session);
5388 /* Increment the clientid slot sequence id */
5396 * Issues a CREATE_SESSION operation to the server.
5397 * It is the responsibility of the caller to verify the session is
5398 * expired before calling this routine.
5400 int nfs4_proc_create_session(struct nfs_client *clp)
5404 struct nfs4_session *session = clp->cl_session;
5406 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5408 status = _nfs4_proc_create_session(clp);
5412 /* Init or reset the session slot tables */
5413 status = nfs4_setup_session_slot_tables(session);
5414 dprintk("slot table setup returned %d\n", status);
5418 ptr = (unsigned *)&session->sess_id.data[0];
5419 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5420 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5422 dprintk("<-- %s\n", __func__);
5427 * Issue the over-the-wire RPC DESTROY_SESSION.
5428 * The caller must serialize access to this routine.
5430 int nfs4_proc_destroy_session(struct nfs4_session *session)
5433 struct rpc_message msg;
5435 dprintk("--> nfs4_proc_destroy_session\n");
5437 /* session is still being setup */
5438 if (session->clp->cl_cons_state != NFS_CS_READY)
5441 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5442 msg.rpc_argp = session;
5443 msg.rpc_resp = NULL;
5444 msg.rpc_cred = NULL;
5445 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5449 "NFS: Got error %d from the server on DESTROY_SESSION. "
5450 "Session has been destroyed regardless...\n", status);
5452 dprintk("<-- nfs4_proc_destroy_session\n");
5456 int nfs4_init_session(struct nfs_server *server)
5458 struct nfs_client *clp = server->nfs_client;
5459 struct nfs4_session *session;
5460 unsigned int rsize, wsize;
5463 if (!nfs4_has_session(clp))
5466 session = clp->cl_session;
5467 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5470 rsize = server->rsize;
5472 rsize = NFS_MAX_FILE_IO_SIZE;
5473 wsize = server->wsize;
5475 wsize = NFS_MAX_FILE_IO_SIZE;
5477 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5478 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5480 ret = nfs4_recover_expired_lease(server);
5482 ret = nfs4_check_client_ready(clp);
5486 int nfs4_init_ds_session(struct nfs_client *clp)
5488 struct nfs4_session *session = clp->cl_session;
5491 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5494 ret = nfs4_client_recover_expired_lease(clp);
5496 /* Test for the DS role */
5497 if (!is_ds_client(clp))
5500 ret = nfs4_check_client_ready(clp);
5504 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5508 * Renew the cl_session lease.
5510 struct nfs4_sequence_data {
5511 struct nfs_client *clp;
5512 struct nfs4_sequence_args args;
5513 struct nfs4_sequence_res res;
5516 static void nfs41_sequence_release(void *data)
5518 struct nfs4_sequence_data *calldata = data;
5519 struct nfs_client *clp = calldata->clp;
5521 if (atomic_read(&clp->cl_count) > 1)
5522 nfs4_schedule_state_renewal(clp);
5523 nfs_put_client(clp);
5527 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5529 switch(task->tk_status) {
5530 case -NFS4ERR_DELAY:
5531 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5534 nfs4_schedule_lease_recovery(clp);
5539 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5541 struct nfs4_sequence_data *calldata = data;
5542 struct nfs_client *clp = calldata->clp;
5544 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5547 if (task->tk_status < 0) {
5548 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5549 if (atomic_read(&clp->cl_count) == 1)
5552 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5553 rpc_restart_call_prepare(task);
5557 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5559 dprintk("<-- %s\n", __func__);
5562 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5564 struct nfs4_sequence_data *calldata = data;
5565 struct nfs_client *clp = calldata->clp;
5566 struct nfs4_sequence_args *args;
5567 struct nfs4_sequence_res *res;
5569 args = task->tk_msg.rpc_argp;
5570 res = task->tk_msg.rpc_resp;
5572 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5574 rpc_call_start(task);
5577 static const struct rpc_call_ops nfs41_sequence_ops = {
5578 .rpc_call_done = nfs41_sequence_call_done,
5579 .rpc_call_prepare = nfs41_sequence_prepare,
5580 .rpc_release = nfs41_sequence_release,
5583 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5585 struct nfs4_sequence_data *calldata;
5586 struct rpc_message msg = {
5587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5590 struct rpc_task_setup task_setup_data = {
5591 .rpc_client = clp->cl_rpcclient,
5592 .rpc_message = &msg,
5593 .callback_ops = &nfs41_sequence_ops,
5594 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5597 if (!atomic_inc_not_zero(&clp->cl_count))
5598 return ERR_PTR(-EIO);
5599 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5600 if (calldata == NULL) {
5601 nfs_put_client(clp);
5602 return ERR_PTR(-ENOMEM);
5604 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5605 msg.rpc_argp = &calldata->args;
5606 msg.rpc_resp = &calldata->res;
5607 calldata->clp = clp;
5608 task_setup_data.callback_data = calldata;
5610 return rpc_run_task(&task_setup_data);
5613 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5615 struct rpc_task *task;
5618 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5620 task = _nfs41_proc_sequence(clp, cred);
5622 ret = PTR_ERR(task);
5624 rpc_put_task_async(task);
5625 dprintk("<-- %s status=%d\n", __func__, ret);
5629 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5631 struct rpc_task *task;
5634 task = _nfs41_proc_sequence(clp, cred);
5636 ret = PTR_ERR(task);
5639 ret = rpc_wait_for_completion_task(task);
5641 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5643 if (task->tk_status == 0)
5644 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5645 ret = task->tk_status;
5649 dprintk("<-- %s status=%d\n", __func__, ret);
5653 struct nfs4_reclaim_complete_data {
5654 struct nfs_client *clp;
5655 struct nfs41_reclaim_complete_args arg;
5656 struct nfs41_reclaim_complete_res res;
5659 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5661 struct nfs4_reclaim_complete_data *calldata = data;
5663 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5664 if (nfs41_setup_sequence(calldata->clp->cl_session,
5665 &calldata->arg.seq_args,
5666 &calldata->res.seq_res, task))
5669 rpc_call_start(task);
5672 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5674 switch(task->tk_status) {
5676 case -NFS4ERR_COMPLETE_ALREADY:
5677 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5679 case -NFS4ERR_DELAY:
5680 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5682 case -NFS4ERR_RETRY_UNCACHED_REP:
5685 nfs4_schedule_lease_recovery(clp);
5690 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5692 struct nfs4_reclaim_complete_data *calldata = data;
5693 struct nfs_client *clp = calldata->clp;
5694 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5696 dprintk("--> %s\n", __func__);
5697 if (!nfs41_sequence_done(task, res))
5700 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5701 rpc_restart_call_prepare(task);
5704 dprintk("<-- %s\n", __func__);
5707 static void nfs4_free_reclaim_complete_data(void *data)
5709 struct nfs4_reclaim_complete_data *calldata = data;
5714 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5715 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5716 .rpc_call_done = nfs4_reclaim_complete_done,
5717 .rpc_release = nfs4_free_reclaim_complete_data,
5721 * Issue a global reclaim complete.
5723 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5725 struct nfs4_reclaim_complete_data *calldata;
5726 struct rpc_task *task;
5727 struct rpc_message msg = {
5728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5730 struct rpc_task_setup task_setup_data = {
5731 .rpc_client = clp->cl_rpcclient,
5732 .rpc_message = &msg,
5733 .callback_ops = &nfs4_reclaim_complete_call_ops,
5734 .flags = RPC_TASK_ASYNC,
5736 int status = -ENOMEM;
5738 dprintk("--> %s\n", __func__);
5739 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5740 if (calldata == NULL)
5742 calldata->clp = clp;
5743 calldata->arg.one_fs = 0;
5745 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5746 msg.rpc_argp = &calldata->arg;
5747 msg.rpc_resp = &calldata->res;
5748 task_setup_data.callback_data = calldata;
5749 task = rpc_run_task(&task_setup_data);
5751 status = PTR_ERR(task);
5754 status = nfs4_wait_for_completion_rpc_task(task);
5756 status = task->tk_status;
5760 dprintk("<-- %s status=%d\n", __func__, status);
5765 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5767 struct nfs4_layoutget *lgp = calldata;
5768 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5770 dprintk("--> %s\n", __func__);
5771 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5772 * right now covering the LAYOUTGET we are about to send.
5773 * However, that is not so catastrophic, and there seems
5774 * to be no way to prevent it completely.
5776 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5777 &lgp->res.seq_res, task))
5779 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5780 NFS_I(lgp->args.inode)->layout,
5781 lgp->args.ctx->state)) {
5782 rpc_exit(task, NFS4_OK);
5785 rpc_call_start(task);
5788 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5790 struct nfs4_layoutget *lgp = calldata;
5791 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5793 dprintk("--> %s\n", __func__);
5795 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5798 switch (task->tk_status) {
5801 case -NFS4ERR_LAYOUTTRYLATER:
5802 case -NFS4ERR_RECALLCONFLICT:
5803 task->tk_status = -NFS4ERR_DELAY;
5806 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5807 rpc_restart_call_prepare(task);
5811 dprintk("<-- %s\n", __func__);
5814 static void nfs4_layoutget_release(void *calldata)
5816 struct nfs4_layoutget *lgp = calldata;
5818 dprintk("--> %s\n", __func__);
5819 put_nfs_open_context(lgp->args.ctx);
5821 dprintk("<-- %s\n", __func__);
5824 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5825 .rpc_call_prepare = nfs4_layoutget_prepare,
5826 .rpc_call_done = nfs4_layoutget_done,
5827 .rpc_release = nfs4_layoutget_release,
5830 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5832 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5833 struct rpc_task *task;
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5836 .rpc_argp = &lgp->args,
5837 .rpc_resp = &lgp->res,
5839 struct rpc_task_setup task_setup_data = {
5840 .rpc_client = server->client,
5841 .rpc_message = &msg,
5842 .callback_ops = &nfs4_layoutget_call_ops,
5843 .callback_data = lgp,
5844 .flags = RPC_TASK_ASYNC,
5848 dprintk("--> %s\n", __func__);
5850 lgp->res.layoutp = &lgp->args.layout;
5851 lgp->res.seq_res.sr_slot = NULL;
5852 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5853 task = rpc_run_task(&task_setup_data);
5855 return PTR_ERR(task);
5856 status = nfs4_wait_for_completion_rpc_task(task);
5858 status = task->tk_status;
5860 status = pnfs_layout_process(lgp);
5862 dprintk("<-- %s status=%d\n", __func__, status);
5867 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5869 struct nfs4_layoutreturn *lrp = calldata;
5871 dprintk("--> %s\n", __func__);
5872 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5873 &lrp->res.seq_res, task))
5875 rpc_call_start(task);
5878 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5880 struct nfs4_layoutreturn *lrp = calldata;
5881 struct nfs_server *server;
5882 struct pnfs_layout_hdr *lo = lrp->args.layout;
5884 dprintk("--> %s\n", __func__);
5886 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5889 server = NFS_SERVER(lrp->args.inode);
5890 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5891 rpc_restart_call_prepare(task);
5894 spin_lock(&lo->plh_inode->i_lock);
5895 if (task->tk_status == 0) {
5896 if (lrp->res.lrs_present) {
5897 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5899 BUG_ON(!list_empty(&lo->plh_segs));
5901 lo->plh_block_lgets--;
5902 spin_unlock(&lo->plh_inode->i_lock);
5903 dprintk("<-- %s\n", __func__);
5906 static void nfs4_layoutreturn_release(void *calldata)
5908 struct nfs4_layoutreturn *lrp = calldata;
5910 dprintk("--> %s\n", __func__);
5911 put_layout_hdr(lrp->args.layout);
5913 dprintk("<-- %s\n", __func__);
5916 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5917 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5918 .rpc_call_done = nfs4_layoutreturn_done,
5919 .rpc_release = nfs4_layoutreturn_release,
5922 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5924 struct rpc_task *task;
5925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5927 .rpc_argp = &lrp->args,
5928 .rpc_resp = &lrp->res,
5930 struct rpc_task_setup task_setup_data = {
5931 .rpc_client = lrp->clp->cl_rpcclient,
5932 .rpc_message = &msg,
5933 .callback_ops = &nfs4_layoutreturn_call_ops,
5934 .callback_data = lrp,
5938 dprintk("--> %s\n", __func__);
5939 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
5940 task = rpc_run_task(&task_setup_data);
5942 return PTR_ERR(task);
5943 status = task->tk_status;
5944 dprintk("<-- %s status=%d\n", __func__, status);
5950 * Retrieve the list of Data Server devices from the MDS.
5952 static int _nfs4_getdevicelist(struct nfs_server *server,
5953 const struct nfs_fh *fh,
5954 struct pnfs_devicelist *devlist)
5956 struct nfs4_getdevicelist_args args = {
5958 .layoutclass = server->pnfs_curr_ld->id,
5960 struct nfs4_getdevicelist_res res = {
5963 struct rpc_message msg = {
5964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5970 dprintk("--> %s\n", __func__);
5971 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5973 dprintk("<-- %s status=%d\n", __func__, status);
5977 int nfs4_proc_getdevicelist(struct nfs_server *server,
5978 const struct nfs_fh *fh,
5979 struct pnfs_devicelist *devlist)
5981 struct nfs4_exception exception = { };
5985 err = nfs4_handle_exception(server,
5986 _nfs4_getdevicelist(server, fh, devlist),
5988 } while (exception.retry);
5990 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5991 err, devlist->num_devs);
5995 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5998 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6000 struct nfs4_getdeviceinfo_args args = {
6003 struct nfs4_getdeviceinfo_res res = {
6006 struct rpc_message msg = {
6007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6013 dprintk("--> %s\n", __func__);
6014 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6015 dprintk("<-- %s status=%d\n", __func__, status);
6020 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6022 struct nfs4_exception exception = { };
6026 err = nfs4_handle_exception(server,
6027 _nfs4_proc_getdeviceinfo(server, pdev),
6029 } while (exception.retry);
6032 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6034 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6036 struct nfs4_layoutcommit_data *data = calldata;
6037 struct nfs_server *server = NFS_SERVER(data->args.inode);
6039 if (nfs4_setup_sequence(server, &data->args.seq_args,
6040 &data->res.seq_res, task))
6042 rpc_call_start(task);
6046 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6048 struct nfs4_layoutcommit_data *data = calldata;
6049 struct nfs_server *server = NFS_SERVER(data->args.inode);
6051 if (!nfs4_sequence_done(task, &data->res.seq_res))
6054 switch (task->tk_status) { /* Just ignore these failures */
6055 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6056 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6057 case NFS4ERR_BADLAYOUT: /* no layout */
6058 case NFS4ERR_GRACE: /* loca_recalim always false */
6059 task->tk_status = 0;
6062 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6063 rpc_restart_call_prepare(task);
6067 if (task->tk_status == 0)
6068 nfs_post_op_update_inode_force_wcc(data->args.inode,
6072 static void nfs4_layoutcommit_release(void *calldata)
6074 struct nfs4_layoutcommit_data *data = calldata;
6075 struct pnfs_layout_segment *lseg, *tmp;
6076 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6078 pnfs_cleanup_layoutcommit(data);
6079 /* Matched by references in pnfs_set_layoutcommit */
6080 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6081 list_del_init(&lseg->pls_lc_list);
6082 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6087 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6088 smp_mb__after_clear_bit();
6089 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6091 put_rpccred(data->cred);
6095 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6096 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6097 .rpc_call_done = nfs4_layoutcommit_done,
6098 .rpc_release = nfs4_layoutcommit_release,
6102 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6104 struct rpc_message msg = {
6105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6106 .rpc_argp = &data->args,
6107 .rpc_resp = &data->res,
6108 .rpc_cred = data->cred,
6110 struct rpc_task_setup task_setup_data = {
6111 .task = &data->task,
6112 .rpc_client = NFS_CLIENT(data->args.inode),
6113 .rpc_message = &msg,
6114 .callback_ops = &nfs4_layoutcommit_ops,
6115 .callback_data = data,
6116 .flags = RPC_TASK_ASYNC,
6118 struct rpc_task *task;
6121 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6122 "lbw: %llu inode %lu\n",
6123 data->task.tk_pid, sync,
6124 data->args.lastbytewritten,
6125 data->args.inode->i_ino);
6127 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6128 task = rpc_run_task(&task_setup_data);
6130 return PTR_ERR(task);
6133 status = nfs4_wait_for_completion_rpc_task(task);
6136 status = task->tk_status;
6138 dprintk("%s: status %d\n", __func__, status);
6144 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6145 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6147 struct nfs41_secinfo_no_name_args args = {
6148 .style = SECINFO_STYLE_CURRENT_FH,
6150 struct nfs4_secinfo_res res = {
6153 struct rpc_message msg = {
6154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6158 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6162 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6163 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6165 struct nfs4_exception exception = { };
6168 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6171 case -NFS4ERR_WRONGSEC:
6172 case -NFS4ERR_NOTSUPP:
6175 err = nfs4_handle_exception(server, err, &exception);
6177 } while (exception.retry);
6182 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6183 struct nfs_fsinfo *info)
6187 rpc_authflavor_t flavor;
6188 struct nfs4_secinfo_flavors *flavors;
6190 page = alloc_page(GFP_KERNEL);
6196 flavors = page_address(page);
6197 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6200 * Fall back on "guess and check" method if
6201 * the server doesn't support SECINFO_NO_NAME
6203 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6204 err = nfs4_find_root_sec(server, fhandle, info);
6210 flavor = nfs_find_best_sec(flavors);
6212 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6222 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6225 struct nfs41_test_stateid_args args = {
6228 struct nfs41_test_stateid_res res;
6229 struct rpc_message msg = {
6230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6235 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6236 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6238 if (status == NFS_OK)
6243 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6245 struct nfs4_exception exception = { };
6248 err = nfs4_handle_exception(server,
6249 _nfs41_test_stateid(server, stateid),
6251 } while (exception.retry);
6255 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6257 struct nfs41_free_stateid_args args = {
6260 struct nfs41_free_stateid_res res;
6261 struct rpc_message msg = {
6262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6267 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6268 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6271 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6273 struct nfs4_exception exception = { };
6276 err = nfs4_handle_exception(server,
6277 _nfs4_free_stateid(server, stateid),
6279 } while (exception.retry);
6282 #endif /* CONFIG_NFS_V4_1 */
6284 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6285 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6286 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6287 .recover_open = nfs4_open_reclaim,
6288 .recover_lock = nfs4_lock_reclaim,
6289 .establish_clid = nfs4_init_clientid,
6290 .get_clid_cred = nfs4_get_setclientid_cred,
6293 #if defined(CONFIG_NFS_V4_1)
6294 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6295 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6296 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6297 .recover_open = nfs4_open_reclaim,
6298 .recover_lock = nfs4_lock_reclaim,
6299 .establish_clid = nfs41_init_clientid,
6300 .get_clid_cred = nfs4_get_exchange_id_cred,
6301 .reclaim_complete = nfs41_proc_reclaim_complete,
6303 #endif /* CONFIG_NFS_V4_1 */
6305 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6306 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6307 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6308 .recover_open = nfs4_open_expired,
6309 .recover_lock = nfs4_lock_expired,
6310 .establish_clid = nfs4_init_clientid,
6311 .get_clid_cred = nfs4_get_setclientid_cred,
6314 #if defined(CONFIG_NFS_V4_1)
6315 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6316 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6317 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6318 .recover_open = nfs41_open_expired,
6319 .recover_lock = nfs41_lock_expired,
6320 .establish_clid = nfs41_init_clientid,
6321 .get_clid_cred = nfs4_get_exchange_id_cred,
6323 #endif /* CONFIG_NFS_V4_1 */
6325 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6326 .sched_state_renewal = nfs4_proc_async_renew,
6327 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6328 .renew_lease = nfs4_proc_renew,
6331 #if defined(CONFIG_NFS_V4_1)
6332 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6333 .sched_state_renewal = nfs41_proc_async_sequence,
6334 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6335 .renew_lease = nfs4_proc_sequence,
6339 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6341 .call_sync = _nfs4_call_sync,
6342 .validate_stateid = nfs4_validate_delegation_stateid,
6343 .find_root_sec = nfs4_find_root_sec,
6344 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6345 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6346 .state_renewal_ops = &nfs40_state_renewal_ops,
6349 #if defined(CONFIG_NFS_V4_1)
6350 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6352 .call_sync = _nfs4_call_sync_session,
6353 .validate_stateid = nfs41_validate_delegation_stateid,
6354 .find_root_sec = nfs41_find_root_sec,
6355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6357 .state_renewal_ops = &nfs41_state_renewal_ops,
6361 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6362 [0] = &nfs_v4_0_minor_ops,
6363 #if defined(CONFIG_NFS_V4_1)
6364 [1] = &nfs_v4_1_minor_ops,
6368 static const struct inode_operations nfs4_file_inode_operations = {
6369 .permission = nfs_permission,
6370 .getattr = nfs_getattr,
6371 .setattr = nfs_setattr,
6372 .getxattr = generic_getxattr,
6373 .setxattr = generic_setxattr,
6374 .listxattr = generic_listxattr,
6375 .removexattr = generic_removexattr,
6378 const struct nfs_rpc_ops nfs_v4_clientops = {
6379 .version = 4, /* protocol version */
6380 .dentry_ops = &nfs4_dentry_operations,
6381 .dir_inode_ops = &nfs4_dir_inode_operations,
6382 .file_inode_ops = &nfs4_file_inode_operations,
6383 .file_ops = &nfs4_file_operations,
6384 .getroot = nfs4_proc_get_root,
6385 .getattr = nfs4_proc_getattr,
6386 .setattr = nfs4_proc_setattr,
6387 .lookup = nfs4_proc_lookup,
6388 .access = nfs4_proc_access,
6389 .readlink = nfs4_proc_readlink,
6390 .create = nfs4_proc_create,
6391 .remove = nfs4_proc_remove,
6392 .unlink_setup = nfs4_proc_unlink_setup,
6393 .unlink_done = nfs4_proc_unlink_done,
6394 .rename = nfs4_proc_rename,
6395 .rename_setup = nfs4_proc_rename_setup,
6396 .rename_done = nfs4_proc_rename_done,
6397 .link = nfs4_proc_link,
6398 .symlink = nfs4_proc_symlink,
6399 .mkdir = nfs4_proc_mkdir,
6400 .rmdir = nfs4_proc_remove,
6401 .readdir = nfs4_proc_readdir,
6402 .mknod = nfs4_proc_mknod,
6403 .statfs = nfs4_proc_statfs,
6404 .fsinfo = nfs4_proc_fsinfo,
6405 .pathconf = nfs4_proc_pathconf,
6406 .set_capabilities = nfs4_server_capabilities,
6407 .decode_dirent = nfs4_decode_dirent,
6408 .read_setup = nfs4_proc_read_setup,
6409 .read_done = nfs4_read_done,
6410 .write_setup = nfs4_proc_write_setup,
6411 .write_done = nfs4_write_done,
6412 .commit_setup = nfs4_proc_commit_setup,
6413 .commit_done = nfs4_commit_done,
6414 .lock = nfs4_proc_lock,
6415 .clear_acl_cache = nfs4_zap_acl_attr,
6416 .close_context = nfs4_close_context,
6417 .open_context = nfs4_atomic_open,
6418 .init_client = nfs4_init_client,
6419 .secinfo = nfs4_proc_secinfo,
6422 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6423 .prefix = XATTR_NAME_NFSV4_ACL,
6424 .list = nfs4_xattr_list_nfs4_acl,
6425 .get = nfs4_xattr_get_nfs4_acl,
6426 .set = nfs4_xattr_set_nfs4_acl,
6429 const struct xattr_handler *nfs4_xattr_handlers[] = {
6430 &nfs4_xattr_nfs4_acl_handler,
6434 module_param(max_session_slots, ushort, 0644);
6435 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6436 "requests the client will negotiate");