xref: /linux/fs/nfs/nfs4proc.c (revision 5d4a2e29fba5b2bef95b96a46b338ec4d76fa4fd)
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
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.
24  *
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.
36  */
37 
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 
53 #include "nfs4_fs.h"
54 #include "delegation.h"
55 #include "internal.h"
56 #include "iostat.h"
57 #include "callback.h"
58 
59 #define NFSDBG_FACILITY		NFSDBG_PROC
60 
61 #define NFS4_POLL_RETRY_MIN	(HZ/10)
62 #define NFS4_POLL_RETRY_MAX	(15*HZ)
63 
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
65 
66 struct nfs4_opendata;
67 static int _nfs4_proc_open(struct nfs4_opendata *data);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
69 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
70 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
71 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 			    struct nfs_fattr *fattr, struct iattr *sattr,
75 			    struct nfs4_state *state);
76 
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err)
79 {
80 	if (err >= -1000)
81 		return err;
82 	switch (err) {
83 	case -NFS4ERR_RESOURCE:
84 		return -EREMOTEIO;
85 	default:
86 		dprintk("%s could not handle NFSv4 error %d\n",
87 				__func__, -err);
88 		break;
89 	}
90 	return -EIO;
91 }
92 
93 /*
94  * This is our standard bitmap for GETATTR requests.
95  */
96 const u32 nfs4_fattr_bitmap[2] = {
97 	FATTR4_WORD0_TYPE
98 	| FATTR4_WORD0_CHANGE
99 	| FATTR4_WORD0_SIZE
100 	| FATTR4_WORD0_FSID
101 	| FATTR4_WORD0_FILEID,
102 	FATTR4_WORD1_MODE
103 	| FATTR4_WORD1_NUMLINKS
104 	| FATTR4_WORD1_OWNER
105 	| FATTR4_WORD1_OWNER_GROUP
106 	| FATTR4_WORD1_RAWDEV
107 	| FATTR4_WORD1_SPACE_USED
108 	| FATTR4_WORD1_TIME_ACCESS
109 	| FATTR4_WORD1_TIME_METADATA
110 	| FATTR4_WORD1_TIME_MODIFY
111 };
112 
113 const u32 nfs4_statfs_bitmap[2] = {
114 	FATTR4_WORD0_FILES_AVAIL
115 	| FATTR4_WORD0_FILES_FREE
116 	| FATTR4_WORD0_FILES_TOTAL,
117 	FATTR4_WORD1_SPACE_AVAIL
118 	| FATTR4_WORD1_SPACE_FREE
119 	| FATTR4_WORD1_SPACE_TOTAL
120 };
121 
122 const u32 nfs4_pathconf_bitmap[2] = {
123 	FATTR4_WORD0_MAXLINK
124 	| FATTR4_WORD0_MAXNAME,
125 	0
126 };
127 
128 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 			| FATTR4_WORD0_MAXREAD
130 			| FATTR4_WORD0_MAXWRITE
131 			| FATTR4_WORD0_LEASE_TIME,
132 			0
133 };
134 
135 const u32 nfs4_fs_locations_bitmap[2] = {
136 	FATTR4_WORD0_TYPE
137 	| FATTR4_WORD0_CHANGE
138 	| FATTR4_WORD0_SIZE
139 	| FATTR4_WORD0_FSID
140 	| FATTR4_WORD0_FILEID
141 	| FATTR4_WORD0_FS_LOCATIONS,
142 	FATTR4_WORD1_MODE
143 	| FATTR4_WORD1_NUMLINKS
144 	| FATTR4_WORD1_OWNER
145 	| FATTR4_WORD1_OWNER_GROUP
146 	| FATTR4_WORD1_RAWDEV
147 	| FATTR4_WORD1_SPACE_USED
148 	| FATTR4_WORD1_TIME_ACCESS
149 	| FATTR4_WORD1_TIME_METADATA
150 	| FATTR4_WORD1_TIME_MODIFY
151 	| FATTR4_WORD1_MOUNTED_ON_FILEID
152 };
153 
154 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
155 		struct nfs4_readdir_arg *readdir)
156 {
157 	__be32 *start, *p;
158 
159 	BUG_ON(readdir->count < 80);
160 	if (cookie > 2) {
161 		readdir->cookie = cookie;
162 		memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 		return;
164 	}
165 
166 	readdir->cookie = 0;
167 	memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 	if (cookie == 2)
169 		return;
170 
171 	/*
172 	 * NFSv4 servers do not return entries for '.' and '..'
173 	 * Therefore, we fake these entries here.  We let '.'
174 	 * have cookie 0 and '..' have cookie 1.  Note that
175 	 * when talking to the server, we always send cookie 0
176 	 * instead of 1 or 2.
177 	 */
178 	start = p = kmap_atomic(*readdir->pages, KM_USER0);
179 
180 	if (cookie == 0) {
181 		*p++ = xdr_one;                                  /* next */
182 		*p++ = xdr_zero;                   /* cookie, first word */
183 		*p++ = xdr_one;                   /* cookie, second word */
184 		*p++ = xdr_one;                             /* entry len */
185 		memcpy(p, ".\0\0\0", 4);                        /* entry */
186 		p++;
187 		*p++ = xdr_one;                         /* bitmap length */
188 		*p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
189 		*p++ = htonl(8);              /* attribute buffer length */
190 		p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
191 	}
192 
193 	*p++ = xdr_one;                                  /* next */
194 	*p++ = xdr_zero;                   /* cookie, first word */
195 	*p++ = xdr_two;                   /* cookie, second word */
196 	*p++ = xdr_two;                             /* entry len */
197 	memcpy(p, "..\0\0", 4);                         /* entry */
198 	p++;
199 	*p++ = xdr_one;                         /* bitmap length */
200 	*p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
201 	*p++ = htonl(8);              /* attribute buffer length */
202 	p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
203 
204 	readdir->pgbase = (char *)p - (char *)start;
205 	readdir->count -= readdir->pgbase;
206 	kunmap_atomic(start, KM_USER0);
207 }
208 
209 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210 {
211 	int res;
212 
213 	might_sleep();
214 
215 	res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
216 			nfs_wait_bit_killable, TASK_KILLABLE);
217 	return res;
218 }
219 
220 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221 {
222 	int res = 0;
223 
224 	might_sleep();
225 
226 	if (*timeout <= 0)
227 		*timeout = NFS4_POLL_RETRY_MIN;
228 	if (*timeout > NFS4_POLL_RETRY_MAX)
229 		*timeout = NFS4_POLL_RETRY_MAX;
230 	schedule_timeout_killable(*timeout);
231 	if (fatal_signal_pending(current))
232 		res = -ERESTARTSYS;
233 	*timeout <<= 1;
234 	return res;
235 }
236 
237 /* This is the error handling routine for processes that are allowed
238  * to sleep.
239  */
240 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241 {
242 	struct nfs_client *clp = server->nfs_client;
243 	struct nfs4_state *state = exception->state;
244 	int ret = errorcode;
245 
246 	exception->retry = 0;
247 	switch(errorcode) {
248 		case 0:
249 			return 0;
250 		case -NFS4ERR_ADMIN_REVOKED:
251 		case -NFS4ERR_BAD_STATEID:
252 		case -NFS4ERR_OPENMODE:
253 			if (state == NULL)
254 				break;
255 			nfs4_state_mark_reclaim_nograce(clp, state);
256 			goto do_state_recovery;
257 		case -NFS4ERR_STALE_STATEID:
258 			if (state == NULL)
259 				break;
260 			nfs4_state_mark_reclaim_reboot(clp, state);
261 		case -NFS4ERR_STALE_CLIENTID:
262 		case -NFS4ERR_EXPIRED:
263 			goto do_state_recovery;
264 #if defined(CONFIG_NFS_V4_1)
265 		case -NFS4ERR_BADSESSION:
266 		case -NFS4ERR_BADSLOT:
267 		case -NFS4ERR_BAD_HIGH_SLOT:
268 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 		case -NFS4ERR_DEADSESSION:
270 		case -NFS4ERR_SEQ_FALSE_RETRY:
271 		case -NFS4ERR_SEQ_MISORDERED:
272 			dprintk("%s ERROR: %d Reset session\n", __func__,
273 				errorcode);
274 			nfs4_schedule_state_recovery(clp);
275 			exception->retry = 1;
276 			break;
277 #endif /* defined(CONFIG_NFS_V4_1) */
278 		case -NFS4ERR_FILE_OPEN:
279 			if (exception->timeout > HZ) {
280 				/* We have retried a decent amount, time to
281 				 * fail
282 				 */
283 				ret = -EBUSY;
284 				break;
285 			}
286 		case -NFS4ERR_GRACE:
287 		case -NFS4ERR_DELAY:
288 		case -EKEYEXPIRED:
289 			ret = nfs4_delay(server->client, &exception->timeout);
290 			if (ret != 0)
291 				break;
292 		case -NFS4ERR_OLD_STATEID:
293 			exception->retry = 1;
294 	}
295 	/* We failed to handle the error */
296 	return nfs4_map_errors(ret);
297 do_state_recovery:
298 	nfs4_schedule_state_recovery(clp);
299 	ret = nfs4_wait_clnt_recover(clp);
300 	if (ret == 0)
301 		exception->retry = 1;
302 	return ret;
303 }
304 
305 
306 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
307 {
308 	struct nfs_client *clp = server->nfs_client;
309 	spin_lock(&clp->cl_lock);
310 	if (time_before(clp->cl_last_renewal,timestamp))
311 		clp->cl_last_renewal = timestamp;
312 	spin_unlock(&clp->cl_lock);
313 }
314 
315 #if defined(CONFIG_NFS_V4_1)
316 
317 /*
318  * nfs4_free_slot - free a slot and efficiently update slot table.
319  *
320  * freeing a slot is trivially done by clearing its respective bit
321  * in the bitmap.
322  * If the freed slotid equals highest_used_slotid we want to update it
323  * so that the server would be able to size down the slot table if needed,
324  * otherwise we know that the highest_used_slotid is still in use.
325  * When updating highest_used_slotid there may be "holes" in the bitmap
326  * so we need to scan down from highest_used_slotid to 0 looking for the now
327  * highest slotid in use.
328  * If none found, highest_used_slotid is set to -1.
329  *
330  * Must be called while holding tbl->slot_tbl_lock
331  */
332 static void
333 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334 {
335 	int slotid = free_slotid;
336 
337 	/* clear used bit in bitmap */
338 	__clear_bit(slotid, tbl->used_slots);
339 
340 	/* update highest_used_slotid when it is freed */
341 	if (slotid == tbl->highest_used_slotid) {
342 		slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
343 		if (slotid < tbl->max_slots)
344 			tbl->highest_used_slotid = slotid;
345 		else
346 			tbl->highest_used_slotid = -1;
347 	}
348 	dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 		free_slotid, tbl->highest_used_slotid);
350 }
351 
352 /*
353  * Signal state manager thread if session is drained
354  */
355 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356 {
357 	struct rpc_task *task;
358 
359 	if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
360 		task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 		if (task)
362 			rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
363 		return;
364 	}
365 
366 	if (ses->fc_slot_table.highest_used_slotid != -1)
367 		return;
368 
369 	dprintk("%s COMPLETE: Session Drained\n", __func__);
370 	complete(&ses->complete);
371 }
372 
373 static void nfs41_sequence_free_slot(const struct nfs_client *clp,
374 			      struct nfs4_sequence_res *res)
375 {
376 	struct nfs4_slot_table *tbl;
377 
378 	tbl = &clp->cl_session->fc_slot_table;
379 	if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
380 		/* just wake up the next guy waiting since
381 		 * we may have not consumed a slot after all */
382 		dprintk("%s: No slot\n", __func__);
383 		return;
384 	}
385 
386 	spin_lock(&tbl->slot_tbl_lock);
387 	nfs4_free_slot(tbl, res->sr_slotid);
388 	nfs41_check_drain_session_complete(clp->cl_session);
389 	spin_unlock(&tbl->slot_tbl_lock);
390 	res->sr_slotid = NFS4_MAX_SLOT_TABLE;
391 }
392 
393 static void nfs41_sequence_done(struct nfs_client *clp,
394 				struct nfs4_sequence_res *res,
395 				int rpc_status)
396 {
397 	unsigned long timestamp;
398 	struct nfs4_slot_table *tbl;
399 	struct nfs4_slot *slot;
400 
401 	/*
402 	 * sr_status remains 1 if an RPC level error occurred. The server
403 	 * may or may not have processed the sequence operation..
404 	 * Proceed as if the server received and processed the sequence
405 	 * operation.
406 	 */
407 	if (res->sr_status == 1)
408 		res->sr_status = NFS_OK;
409 
410 	/* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
411 	if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
412 		goto out;
413 
414 	/* Check the SEQUENCE operation status */
415 	if (res->sr_status == 0) {
416 		tbl = &clp->cl_session->fc_slot_table;
417 		slot = tbl->slots + res->sr_slotid;
418 		/* Update the slot's sequence and clientid lease timer */
419 		++slot->seq_nr;
420 		timestamp = res->sr_renewal_time;
421 		spin_lock(&clp->cl_lock);
422 		if (time_before(clp->cl_last_renewal, timestamp))
423 			clp->cl_last_renewal = timestamp;
424 		spin_unlock(&clp->cl_lock);
425 		/* Check sequence flags */
426 		if (atomic_read(&clp->cl_count) > 1)
427 			nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
428 	}
429 out:
430 	/* The session may be reset by one of the error handlers. */
431 	dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
432 	nfs41_sequence_free_slot(clp, res);
433 }
434 
435 /*
436  * nfs4_find_slot - efficiently look for a free slot
437  *
438  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
439  * If found, we mark the slot as used, update the highest_used_slotid,
440  * and respectively set up the sequence operation args.
441  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
442  *
443  * Note: must be called with under the slot_tbl_lock.
444  */
445 static u8
446 nfs4_find_slot(struct nfs4_slot_table *tbl)
447 {
448 	int slotid;
449 	u8 ret_id = NFS4_MAX_SLOT_TABLE;
450 	BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
451 
452 	dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
453 		__func__, tbl->used_slots[0], tbl->highest_used_slotid,
454 		tbl->max_slots);
455 	slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
456 	if (slotid >= tbl->max_slots)
457 		goto out;
458 	__set_bit(slotid, tbl->used_slots);
459 	if (slotid > tbl->highest_used_slotid)
460 		tbl->highest_used_slotid = slotid;
461 	ret_id = slotid;
462 out:
463 	dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
464 		__func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
465 	return ret_id;
466 }
467 
468 static int nfs41_setup_sequence(struct nfs4_session *session,
469 				struct nfs4_sequence_args *args,
470 				struct nfs4_sequence_res *res,
471 				int cache_reply,
472 				struct rpc_task *task)
473 {
474 	struct nfs4_slot *slot;
475 	struct nfs4_slot_table *tbl;
476 	u8 slotid;
477 
478 	dprintk("--> %s\n", __func__);
479 	/* slot already allocated? */
480 	if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
481 		return 0;
482 
483 	memset(res, 0, sizeof(*res));
484 	res->sr_slotid = NFS4_MAX_SLOT_TABLE;
485 	tbl = &session->fc_slot_table;
486 
487 	spin_lock(&tbl->slot_tbl_lock);
488 	if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
489 	    !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
490 		/*
491 		 * The state manager will wait until the slot table is empty.
492 		 * Schedule the reset thread
493 		 */
494 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
495 		spin_unlock(&tbl->slot_tbl_lock);
496 		dprintk("%s Schedule Session Reset\n", __func__);
497 		return -EAGAIN;
498 	}
499 
500 	if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
501 	    !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
502 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
503 		spin_unlock(&tbl->slot_tbl_lock);
504 		dprintk("%s enforce FIFO order\n", __func__);
505 		return -EAGAIN;
506 	}
507 
508 	slotid = nfs4_find_slot(tbl);
509 	if (slotid == NFS4_MAX_SLOT_TABLE) {
510 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
511 		spin_unlock(&tbl->slot_tbl_lock);
512 		dprintk("<-- %s: no free slots\n", __func__);
513 		return -EAGAIN;
514 	}
515 	spin_unlock(&tbl->slot_tbl_lock);
516 
517 	rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
518 	slot = tbl->slots + slotid;
519 	args->sa_session = session;
520 	args->sa_slotid = slotid;
521 	args->sa_cache_this = cache_reply;
522 
523 	dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
524 
525 	res->sr_session = session;
526 	res->sr_slotid = slotid;
527 	res->sr_renewal_time = jiffies;
528 	/*
529 	 * sr_status is only set in decode_sequence, and so will remain
530 	 * set to 1 if an rpc level failure occurs.
531 	 */
532 	res->sr_status = 1;
533 	return 0;
534 }
535 
536 int nfs4_setup_sequence(struct nfs_client *clp,
537 			struct nfs4_sequence_args *args,
538 			struct nfs4_sequence_res *res,
539 			int cache_reply,
540 			struct rpc_task *task)
541 {
542 	int ret = 0;
543 
544 	dprintk("--> %s clp %p session %p sr_slotid %d\n",
545 		__func__, clp, clp->cl_session, res->sr_slotid);
546 
547 	if (!nfs4_has_session(clp))
548 		goto out;
549 	ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
550 				   task);
551 	if (ret && ret != -EAGAIN) {
552 		/* terminate rpc task */
553 		task->tk_status = ret;
554 		task->tk_action = NULL;
555 	}
556 out:
557 	dprintk("<-- %s status=%d\n", __func__, ret);
558 	return ret;
559 }
560 
561 struct nfs41_call_sync_data {
562 	struct nfs_client *clp;
563 	struct nfs4_sequence_args *seq_args;
564 	struct nfs4_sequence_res *seq_res;
565 	int cache_reply;
566 };
567 
568 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
569 {
570 	struct nfs41_call_sync_data *data = calldata;
571 
572 	dprintk("--> %s data->clp->cl_session %p\n", __func__,
573 		data->clp->cl_session);
574 	if (nfs4_setup_sequence(data->clp, data->seq_args,
575 				data->seq_res, data->cache_reply, task))
576 		return;
577 	rpc_call_start(task);
578 }
579 
580 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
581 {
582 	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
583 	nfs41_call_sync_prepare(task, calldata);
584 }
585 
586 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
587 {
588 	struct nfs41_call_sync_data *data = calldata;
589 
590 	nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
591 }
592 
593 struct rpc_call_ops nfs41_call_sync_ops = {
594 	.rpc_call_prepare = nfs41_call_sync_prepare,
595 	.rpc_call_done = nfs41_call_sync_done,
596 };
597 
598 struct rpc_call_ops nfs41_call_priv_sync_ops = {
599 	.rpc_call_prepare = nfs41_call_priv_sync_prepare,
600 	.rpc_call_done = nfs41_call_sync_done,
601 };
602 
603 static int nfs4_call_sync_sequence(struct nfs_client *clp,
604 				   struct rpc_clnt *clnt,
605 				   struct rpc_message *msg,
606 				   struct nfs4_sequence_args *args,
607 				   struct nfs4_sequence_res *res,
608 				   int cache_reply,
609 				   int privileged)
610 {
611 	int ret;
612 	struct rpc_task *task;
613 	struct nfs41_call_sync_data data = {
614 		.clp = clp,
615 		.seq_args = args,
616 		.seq_res = res,
617 		.cache_reply = cache_reply,
618 	};
619 	struct rpc_task_setup task_setup = {
620 		.rpc_client = clnt,
621 		.rpc_message = msg,
622 		.callback_ops = &nfs41_call_sync_ops,
623 		.callback_data = &data
624 	};
625 
626 	res->sr_slotid = NFS4_MAX_SLOT_TABLE;
627 	if (privileged)
628 		task_setup.callback_ops = &nfs41_call_priv_sync_ops;
629 	task = rpc_run_task(&task_setup);
630 	if (IS_ERR(task))
631 		ret = PTR_ERR(task);
632 	else {
633 		ret = task->tk_status;
634 		rpc_put_task(task);
635 	}
636 	return ret;
637 }
638 
639 int _nfs4_call_sync_session(struct nfs_server *server,
640 			    struct rpc_message *msg,
641 			    struct nfs4_sequence_args *args,
642 			    struct nfs4_sequence_res *res,
643 			    int cache_reply)
644 {
645 	return nfs4_call_sync_sequence(server->nfs_client, server->client,
646 				       msg, args, res, cache_reply, 0);
647 }
648 
649 #endif /* CONFIG_NFS_V4_1 */
650 
651 int _nfs4_call_sync(struct nfs_server *server,
652 		    struct rpc_message *msg,
653 		    struct nfs4_sequence_args *args,
654 		    struct nfs4_sequence_res *res,
655 		    int cache_reply)
656 {
657 	args->sa_session = res->sr_session = NULL;
658 	return rpc_call_sync(server->client, msg, 0);
659 }
660 
661 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
662 	(server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
663 			&(res)->seq_res, (cache_reply))
664 
665 static void nfs4_sequence_done(const struct nfs_server *server,
666 			       struct nfs4_sequence_res *res, int rpc_status)
667 {
668 #ifdef CONFIG_NFS_V4_1
669 	if (nfs4_has_session(server->nfs_client))
670 		nfs41_sequence_done(server->nfs_client, res, rpc_status);
671 #endif /* CONFIG_NFS_V4_1 */
672 }
673 
674 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
675 {
676 	struct nfs_inode *nfsi = NFS_I(dir);
677 
678 	spin_lock(&dir->i_lock);
679 	nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
680 	if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
681 		nfs_force_lookup_revalidate(dir);
682 	nfsi->change_attr = cinfo->after;
683 	spin_unlock(&dir->i_lock);
684 }
685 
686 struct nfs4_opendata {
687 	struct kref kref;
688 	struct nfs_openargs o_arg;
689 	struct nfs_openres o_res;
690 	struct nfs_open_confirmargs c_arg;
691 	struct nfs_open_confirmres c_res;
692 	struct nfs_fattr f_attr;
693 	struct nfs_fattr dir_attr;
694 	struct path path;
695 	struct dentry *dir;
696 	struct nfs4_state_owner *owner;
697 	struct nfs4_state *state;
698 	struct iattr attrs;
699 	unsigned long timestamp;
700 	unsigned int rpc_done : 1;
701 	int rpc_status;
702 	int cancelled;
703 };
704 
705 
706 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
707 {
708 	p->o_res.f_attr = &p->f_attr;
709 	p->o_res.dir_attr = &p->dir_attr;
710 	p->o_res.seqid = p->o_arg.seqid;
711 	p->c_res.seqid = p->c_arg.seqid;
712 	p->o_res.server = p->o_arg.server;
713 	nfs_fattr_init(&p->f_attr);
714 	nfs_fattr_init(&p->dir_attr);
715 	p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
716 }
717 
718 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
719 		struct nfs4_state_owner *sp, fmode_t fmode, int flags,
720 		const struct iattr *attrs,
721 		gfp_t gfp_mask)
722 {
723 	struct dentry *parent = dget_parent(path->dentry);
724 	struct inode *dir = parent->d_inode;
725 	struct nfs_server *server = NFS_SERVER(dir);
726 	struct nfs4_opendata *p;
727 
728 	p = kzalloc(sizeof(*p), gfp_mask);
729 	if (p == NULL)
730 		goto err;
731 	p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
732 	if (p->o_arg.seqid == NULL)
733 		goto err_free;
734 	path_get(path);
735 	p->path = *path;
736 	p->dir = parent;
737 	p->owner = sp;
738 	atomic_inc(&sp->so_count);
739 	p->o_arg.fh = NFS_FH(dir);
740 	p->o_arg.open_flags = flags;
741 	p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
742 	p->o_arg.clientid = server->nfs_client->cl_clientid;
743 	p->o_arg.id = sp->so_owner_id.id;
744 	p->o_arg.name = &p->path.dentry->d_name;
745 	p->o_arg.server = server;
746 	p->o_arg.bitmask = server->attr_bitmask;
747 	p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
748 	if (flags & O_EXCL) {
749 		if (nfs4_has_persistent_session(server->nfs_client)) {
750 			/* GUARDED */
751 			p->o_arg.u.attrs = &p->attrs;
752 			memcpy(&p->attrs, attrs, sizeof(p->attrs));
753 		} else { /* EXCLUSIVE4_1 */
754 			u32 *s = (u32 *) p->o_arg.u.verifier.data;
755 			s[0] = jiffies;
756 			s[1] = current->pid;
757 		}
758 	} else if (flags & O_CREAT) {
759 		p->o_arg.u.attrs = &p->attrs;
760 		memcpy(&p->attrs, attrs, sizeof(p->attrs));
761 	}
762 	p->c_arg.fh = &p->o_res.fh;
763 	p->c_arg.stateid = &p->o_res.stateid;
764 	p->c_arg.seqid = p->o_arg.seqid;
765 	nfs4_init_opendata_res(p);
766 	kref_init(&p->kref);
767 	return p;
768 err_free:
769 	kfree(p);
770 err:
771 	dput(parent);
772 	return NULL;
773 }
774 
775 static void nfs4_opendata_free(struct kref *kref)
776 {
777 	struct nfs4_opendata *p = container_of(kref,
778 			struct nfs4_opendata, kref);
779 
780 	nfs_free_seqid(p->o_arg.seqid);
781 	if (p->state != NULL)
782 		nfs4_put_open_state(p->state);
783 	nfs4_put_state_owner(p->owner);
784 	dput(p->dir);
785 	path_put(&p->path);
786 	kfree(p);
787 }
788 
789 static void nfs4_opendata_put(struct nfs4_opendata *p)
790 {
791 	if (p != NULL)
792 		kref_put(&p->kref, nfs4_opendata_free);
793 }
794 
795 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
796 {
797 	int ret;
798 
799 	ret = rpc_wait_for_completion_task(task);
800 	return ret;
801 }
802 
803 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
804 {
805 	int ret = 0;
806 
807 	if (open_mode & O_EXCL)
808 		goto out;
809 	switch (mode & (FMODE_READ|FMODE_WRITE)) {
810 		case FMODE_READ:
811 			ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
812 				&& state->n_rdonly != 0;
813 			break;
814 		case FMODE_WRITE:
815 			ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
816 				&& state->n_wronly != 0;
817 			break;
818 		case FMODE_READ|FMODE_WRITE:
819 			ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
820 				&& state->n_rdwr != 0;
821 	}
822 out:
823 	return ret;
824 }
825 
826 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
827 {
828 	if ((delegation->type & fmode) != fmode)
829 		return 0;
830 	if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
831 		return 0;
832 	nfs_mark_delegation_referenced(delegation);
833 	return 1;
834 }
835 
836 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
837 {
838 	switch (fmode) {
839 		case FMODE_WRITE:
840 			state->n_wronly++;
841 			break;
842 		case FMODE_READ:
843 			state->n_rdonly++;
844 			break;
845 		case FMODE_READ|FMODE_WRITE:
846 			state->n_rdwr++;
847 	}
848 	nfs4_state_set_mode_locked(state, state->state | fmode);
849 }
850 
851 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
852 {
853 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
854 		memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
855 	memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
856 	switch (fmode) {
857 		case FMODE_READ:
858 			set_bit(NFS_O_RDONLY_STATE, &state->flags);
859 			break;
860 		case FMODE_WRITE:
861 			set_bit(NFS_O_WRONLY_STATE, &state->flags);
862 			break;
863 		case FMODE_READ|FMODE_WRITE:
864 			set_bit(NFS_O_RDWR_STATE, &state->flags);
865 	}
866 }
867 
868 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
869 {
870 	write_seqlock(&state->seqlock);
871 	nfs_set_open_stateid_locked(state, stateid, fmode);
872 	write_sequnlock(&state->seqlock);
873 }
874 
875 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
876 {
877 	/*
878 	 * Protect the call to nfs4_state_set_mode_locked and
879 	 * serialise the stateid update
880 	 */
881 	write_seqlock(&state->seqlock);
882 	if (deleg_stateid != NULL) {
883 		memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
884 		set_bit(NFS_DELEGATED_STATE, &state->flags);
885 	}
886 	if (open_stateid != NULL)
887 		nfs_set_open_stateid_locked(state, open_stateid, fmode);
888 	write_sequnlock(&state->seqlock);
889 	spin_lock(&state->owner->so_lock);
890 	update_open_stateflags(state, fmode);
891 	spin_unlock(&state->owner->so_lock);
892 }
893 
894 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
895 {
896 	struct nfs_inode *nfsi = NFS_I(state->inode);
897 	struct nfs_delegation *deleg_cur;
898 	int ret = 0;
899 
900 	fmode &= (FMODE_READ|FMODE_WRITE);
901 
902 	rcu_read_lock();
903 	deleg_cur = rcu_dereference(nfsi->delegation);
904 	if (deleg_cur == NULL)
905 		goto no_delegation;
906 
907 	spin_lock(&deleg_cur->lock);
908 	if (nfsi->delegation != deleg_cur ||
909 	    (deleg_cur->type & fmode) != fmode)
910 		goto no_delegation_unlock;
911 
912 	if (delegation == NULL)
913 		delegation = &deleg_cur->stateid;
914 	else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
915 		goto no_delegation_unlock;
916 
917 	nfs_mark_delegation_referenced(deleg_cur);
918 	__update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
919 	ret = 1;
920 no_delegation_unlock:
921 	spin_unlock(&deleg_cur->lock);
922 no_delegation:
923 	rcu_read_unlock();
924 
925 	if (!ret && open_stateid != NULL) {
926 		__update_open_stateid(state, open_stateid, NULL, fmode);
927 		ret = 1;
928 	}
929 
930 	return ret;
931 }
932 
933 
934 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
935 {
936 	struct nfs_delegation *delegation;
937 
938 	rcu_read_lock();
939 	delegation = rcu_dereference(NFS_I(inode)->delegation);
940 	if (delegation == NULL || (delegation->type & fmode) == fmode) {
941 		rcu_read_unlock();
942 		return;
943 	}
944 	rcu_read_unlock();
945 	nfs_inode_return_delegation(inode);
946 }
947 
948 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
949 {
950 	struct nfs4_state *state = opendata->state;
951 	struct nfs_inode *nfsi = NFS_I(state->inode);
952 	struct nfs_delegation *delegation;
953 	int open_mode = opendata->o_arg.open_flags & O_EXCL;
954 	fmode_t fmode = opendata->o_arg.fmode;
955 	nfs4_stateid stateid;
956 	int ret = -EAGAIN;
957 
958 	for (;;) {
959 		if (can_open_cached(state, fmode, open_mode)) {
960 			spin_lock(&state->owner->so_lock);
961 			if (can_open_cached(state, fmode, open_mode)) {
962 				update_open_stateflags(state, fmode);
963 				spin_unlock(&state->owner->so_lock);
964 				goto out_return_state;
965 			}
966 			spin_unlock(&state->owner->so_lock);
967 		}
968 		rcu_read_lock();
969 		delegation = rcu_dereference(nfsi->delegation);
970 		if (delegation == NULL ||
971 		    !can_open_delegated(delegation, fmode)) {
972 			rcu_read_unlock();
973 			break;
974 		}
975 		/* Save the delegation */
976 		memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
977 		rcu_read_unlock();
978 		ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
979 		if (ret != 0)
980 			goto out;
981 		ret = -EAGAIN;
982 
983 		/* Try to update the stateid using the delegation */
984 		if (update_open_stateid(state, NULL, &stateid, fmode))
985 			goto out_return_state;
986 	}
987 out:
988 	return ERR_PTR(ret);
989 out_return_state:
990 	atomic_inc(&state->count);
991 	return state;
992 }
993 
994 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
995 {
996 	struct inode *inode;
997 	struct nfs4_state *state = NULL;
998 	struct nfs_delegation *delegation;
999 	int ret;
1000 
1001 	if (!data->rpc_done) {
1002 		state = nfs4_try_open_cached(data);
1003 		goto out;
1004 	}
1005 
1006 	ret = -EAGAIN;
1007 	if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1008 		goto err;
1009 	inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1010 	ret = PTR_ERR(inode);
1011 	if (IS_ERR(inode))
1012 		goto err;
1013 	ret = -ENOMEM;
1014 	state = nfs4_get_open_state(inode, data->owner);
1015 	if (state == NULL)
1016 		goto err_put_inode;
1017 	if (data->o_res.delegation_type != 0) {
1018 		int delegation_flags = 0;
1019 
1020 		rcu_read_lock();
1021 		delegation = rcu_dereference(NFS_I(inode)->delegation);
1022 		if (delegation)
1023 			delegation_flags = delegation->flags;
1024 		rcu_read_unlock();
1025 		if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1026 			nfs_inode_set_delegation(state->inode,
1027 					data->owner->so_cred,
1028 					&data->o_res);
1029 		else
1030 			nfs_inode_reclaim_delegation(state->inode,
1031 					data->owner->so_cred,
1032 					&data->o_res);
1033 	}
1034 
1035 	update_open_stateid(state, &data->o_res.stateid, NULL,
1036 			data->o_arg.fmode);
1037 	iput(inode);
1038 out:
1039 	return state;
1040 err_put_inode:
1041 	iput(inode);
1042 err:
1043 	return ERR_PTR(ret);
1044 }
1045 
1046 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1047 {
1048 	struct nfs_inode *nfsi = NFS_I(state->inode);
1049 	struct nfs_open_context *ctx;
1050 
1051 	spin_lock(&state->inode->i_lock);
1052 	list_for_each_entry(ctx, &nfsi->open_files, list) {
1053 		if (ctx->state != state)
1054 			continue;
1055 		get_nfs_open_context(ctx);
1056 		spin_unlock(&state->inode->i_lock);
1057 		return ctx;
1058 	}
1059 	spin_unlock(&state->inode->i_lock);
1060 	return ERR_PTR(-ENOENT);
1061 }
1062 
1063 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1064 {
1065 	struct nfs4_opendata *opendata;
1066 
1067 	opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1068 	if (opendata == NULL)
1069 		return ERR_PTR(-ENOMEM);
1070 	opendata->state = state;
1071 	atomic_inc(&state->count);
1072 	return opendata;
1073 }
1074 
1075 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1076 {
1077 	struct nfs4_state *newstate;
1078 	int ret;
1079 
1080 	opendata->o_arg.open_flags = 0;
1081 	opendata->o_arg.fmode = fmode;
1082 	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1083 	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1084 	nfs4_init_opendata_res(opendata);
1085 	ret = _nfs4_recover_proc_open(opendata);
1086 	if (ret != 0)
1087 		return ret;
1088 	newstate = nfs4_opendata_to_nfs4_state(opendata);
1089 	if (IS_ERR(newstate))
1090 		return PTR_ERR(newstate);
1091 	nfs4_close_state(&opendata->path, newstate, fmode);
1092 	*res = newstate;
1093 	return 0;
1094 }
1095 
1096 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1097 {
1098 	struct nfs4_state *newstate;
1099 	int ret;
1100 
1101 	/* memory barrier prior to reading state->n_* */
1102 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1103 	smp_rmb();
1104 	if (state->n_rdwr != 0) {
1105 		ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1106 		if (ret != 0)
1107 			return ret;
1108 		if (newstate != state)
1109 			return -ESTALE;
1110 	}
1111 	if (state->n_wronly != 0) {
1112 		ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1113 		if (ret != 0)
1114 			return ret;
1115 		if (newstate != state)
1116 			return -ESTALE;
1117 	}
1118 	if (state->n_rdonly != 0) {
1119 		ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1120 		if (ret != 0)
1121 			return ret;
1122 		if (newstate != state)
1123 			return -ESTALE;
1124 	}
1125 	/*
1126 	 * We may have performed cached opens for all three recoveries.
1127 	 * Check if we need to update the current stateid.
1128 	 */
1129 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1130 	    memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1131 		write_seqlock(&state->seqlock);
1132 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1133 			memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1134 		write_sequnlock(&state->seqlock);
1135 	}
1136 	return 0;
1137 }
1138 
1139 /*
1140  * OPEN_RECLAIM:
1141  * 	reclaim state on the server after a reboot.
1142  */
1143 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1144 {
1145 	struct nfs_delegation *delegation;
1146 	struct nfs4_opendata *opendata;
1147 	fmode_t delegation_type = 0;
1148 	int status;
1149 
1150 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1151 	if (IS_ERR(opendata))
1152 		return PTR_ERR(opendata);
1153 	opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1154 	opendata->o_arg.fh = NFS_FH(state->inode);
1155 	rcu_read_lock();
1156 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1157 	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1158 		delegation_type = delegation->type;
1159 	rcu_read_unlock();
1160 	opendata->o_arg.u.delegation_type = delegation_type;
1161 	status = nfs4_open_recover(opendata, state);
1162 	nfs4_opendata_put(opendata);
1163 	return status;
1164 }
1165 
1166 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1167 {
1168 	struct nfs_server *server = NFS_SERVER(state->inode);
1169 	struct nfs4_exception exception = { };
1170 	int err;
1171 	do {
1172 		err = _nfs4_do_open_reclaim(ctx, state);
1173 		if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
1174 			break;
1175 		nfs4_handle_exception(server, err, &exception);
1176 	} while (exception.retry);
1177 	return err;
1178 }
1179 
1180 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1181 {
1182 	struct nfs_open_context *ctx;
1183 	int ret;
1184 
1185 	ctx = nfs4_state_find_open_context(state);
1186 	if (IS_ERR(ctx))
1187 		return PTR_ERR(ctx);
1188 	ret = nfs4_do_open_reclaim(ctx, state);
1189 	put_nfs_open_context(ctx);
1190 	return ret;
1191 }
1192 
1193 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1194 {
1195 	struct nfs4_opendata *opendata;
1196 	int ret;
1197 
1198 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1199 	if (IS_ERR(opendata))
1200 		return PTR_ERR(opendata);
1201 	opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1202 	memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1203 			sizeof(opendata->o_arg.u.delegation.data));
1204 	ret = nfs4_open_recover(opendata, state);
1205 	nfs4_opendata_put(opendata);
1206 	return ret;
1207 }
1208 
1209 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1210 {
1211 	struct nfs4_exception exception = { };
1212 	struct nfs_server *server = NFS_SERVER(state->inode);
1213 	int err;
1214 	do {
1215 		err = _nfs4_open_delegation_recall(ctx, state, stateid);
1216 		switch (err) {
1217 			case 0:
1218 			case -ENOENT:
1219 			case -ESTALE:
1220 				goto out;
1221 			case -NFS4ERR_BADSESSION:
1222 			case -NFS4ERR_BADSLOT:
1223 			case -NFS4ERR_BAD_HIGH_SLOT:
1224 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1225 			case -NFS4ERR_DEADSESSION:
1226 				nfs4_schedule_state_recovery(
1227 					server->nfs_client);
1228 				goto out;
1229 			case -NFS4ERR_STALE_CLIENTID:
1230 			case -NFS4ERR_STALE_STATEID:
1231 			case -NFS4ERR_EXPIRED:
1232 				/* Don't recall a delegation if it was lost */
1233 				nfs4_schedule_state_recovery(server->nfs_client);
1234 				goto out;
1235 			case -ERESTARTSYS:
1236 				/*
1237 				 * The show must go on: exit, but mark the
1238 				 * stateid as needing recovery.
1239 				 */
1240 			case -NFS4ERR_ADMIN_REVOKED:
1241 			case -NFS4ERR_BAD_STATEID:
1242 				nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1243 			case -ENOMEM:
1244 				err = 0;
1245 				goto out;
1246 		}
1247 		err = nfs4_handle_exception(server, err, &exception);
1248 	} while (exception.retry);
1249 out:
1250 	return err;
1251 }
1252 
1253 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1254 {
1255 	struct nfs4_opendata *data = calldata;
1256 
1257 	data->rpc_status = task->tk_status;
1258 	if (RPC_ASSASSINATED(task))
1259 		return;
1260 	if (data->rpc_status == 0) {
1261 		memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1262 				sizeof(data->o_res.stateid.data));
1263 		nfs_confirm_seqid(&data->owner->so_seqid, 0);
1264 		renew_lease(data->o_res.server, data->timestamp);
1265 		data->rpc_done = 1;
1266 	}
1267 }
1268 
1269 static void nfs4_open_confirm_release(void *calldata)
1270 {
1271 	struct nfs4_opendata *data = calldata;
1272 	struct nfs4_state *state = NULL;
1273 
1274 	/* If this request hasn't been cancelled, do nothing */
1275 	if (data->cancelled == 0)
1276 		goto out_free;
1277 	/* In case of error, no cleanup! */
1278 	if (!data->rpc_done)
1279 		goto out_free;
1280 	state = nfs4_opendata_to_nfs4_state(data);
1281 	if (!IS_ERR(state))
1282 		nfs4_close_state(&data->path, state, data->o_arg.fmode);
1283 out_free:
1284 	nfs4_opendata_put(data);
1285 }
1286 
1287 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1288 	.rpc_call_done = nfs4_open_confirm_done,
1289 	.rpc_release = nfs4_open_confirm_release,
1290 };
1291 
1292 /*
1293  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1294  */
1295 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1296 {
1297 	struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1298 	struct rpc_task *task;
1299 	struct  rpc_message msg = {
1300 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1301 		.rpc_argp = &data->c_arg,
1302 		.rpc_resp = &data->c_res,
1303 		.rpc_cred = data->owner->so_cred,
1304 	};
1305 	struct rpc_task_setup task_setup_data = {
1306 		.rpc_client = server->client,
1307 		.rpc_message = &msg,
1308 		.callback_ops = &nfs4_open_confirm_ops,
1309 		.callback_data = data,
1310 		.workqueue = nfsiod_workqueue,
1311 		.flags = RPC_TASK_ASYNC,
1312 	};
1313 	int status;
1314 
1315 	kref_get(&data->kref);
1316 	data->rpc_done = 0;
1317 	data->rpc_status = 0;
1318 	data->timestamp = jiffies;
1319 	task = rpc_run_task(&task_setup_data);
1320 	if (IS_ERR(task))
1321 		return PTR_ERR(task);
1322 	status = nfs4_wait_for_completion_rpc_task(task);
1323 	if (status != 0) {
1324 		data->cancelled = 1;
1325 		smp_wmb();
1326 	} else
1327 		status = data->rpc_status;
1328 	rpc_put_task(task);
1329 	return status;
1330 }
1331 
1332 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1333 {
1334 	struct nfs4_opendata *data = calldata;
1335 	struct nfs4_state_owner *sp = data->owner;
1336 
1337 	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1338 		return;
1339 	/*
1340 	 * Check if we still need to send an OPEN call, or if we can use
1341 	 * a delegation instead.
1342 	 */
1343 	if (data->state != NULL) {
1344 		struct nfs_delegation *delegation;
1345 
1346 		if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1347 			goto out_no_action;
1348 		rcu_read_lock();
1349 		delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1350 		if (delegation != NULL &&
1351 		    test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1352 			rcu_read_unlock();
1353 			goto out_no_action;
1354 		}
1355 		rcu_read_unlock();
1356 	}
1357 	/* Update sequence id. */
1358 	data->o_arg.id = sp->so_owner_id.id;
1359 	data->o_arg.clientid = sp->so_client->cl_clientid;
1360 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1361 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1362 		nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1363 	}
1364 	data->timestamp = jiffies;
1365 	if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1366 				&data->o_arg.seq_args,
1367 				&data->o_res.seq_res, 1, task))
1368 		return;
1369 	rpc_call_start(task);
1370 	return;
1371 out_no_action:
1372 	task->tk_action = NULL;
1373 
1374 }
1375 
1376 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1377 {
1378 	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1379 	nfs4_open_prepare(task, calldata);
1380 }
1381 
1382 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1383 {
1384 	struct nfs4_opendata *data = calldata;
1385 
1386 	data->rpc_status = task->tk_status;
1387 
1388 	nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1389 			task->tk_status);
1390 
1391 	if (RPC_ASSASSINATED(task))
1392 		return;
1393 	if (task->tk_status == 0) {
1394 		switch (data->o_res.f_attr->mode & S_IFMT) {
1395 			case S_IFREG:
1396 				break;
1397 			case S_IFLNK:
1398 				data->rpc_status = -ELOOP;
1399 				break;
1400 			case S_IFDIR:
1401 				data->rpc_status = -EISDIR;
1402 				break;
1403 			default:
1404 				data->rpc_status = -ENOTDIR;
1405 		}
1406 		renew_lease(data->o_res.server, data->timestamp);
1407 		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1408 			nfs_confirm_seqid(&data->owner->so_seqid, 0);
1409 	}
1410 	data->rpc_done = 1;
1411 }
1412 
1413 static void nfs4_open_release(void *calldata)
1414 {
1415 	struct nfs4_opendata *data = calldata;
1416 	struct nfs4_state *state = NULL;
1417 
1418 	/* If this request hasn't been cancelled, do nothing */
1419 	if (data->cancelled == 0)
1420 		goto out_free;
1421 	/* In case of error, no cleanup! */
1422 	if (data->rpc_status != 0 || !data->rpc_done)
1423 		goto out_free;
1424 	/* In case we need an open_confirm, no cleanup! */
1425 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1426 		goto out_free;
1427 	state = nfs4_opendata_to_nfs4_state(data);
1428 	if (!IS_ERR(state))
1429 		nfs4_close_state(&data->path, state, data->o_arg.fmode);
1430 out_free:
1431 	nfs4_opendata_put(data);
1432 }
1433 
1434 static const struct rpc_call_ops nfs4_open_ops = {
1435 	.rpc_call_prepare = nfs4_open_prepare,
1436 	.rpc_call_done = nfs4_open_done,
1437 	.rpc_release = nfs4_open_release,
1438 };
1439 
1440 static const struct rpc_call_ops nfs4_recover_open_ops = {
1441 	.rpc_call_prepare = nfs4_recover_open_prepare,
1442 	.rpc_call_done = nfs4_open_done,
1443 	.rpc_release = nfs4_open_release,
1444 };
1445 
1446 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1447 {
1448 	struct inode *dir = data->dir->d_inode;
1449 	struct nfs_server *server = NFS_SERVER(dir);
1450 	struct nfs_openargs *o_arg = &data->o_arg;
1451 	struct nfs_openres *o_res = &data->o_res;
1452 	struct rpc_task *task;
1453 	struct rpc_message msg = {
1454 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1455 		.rpc_argp = o_arg,
1456 		.rpc_resp = o_res,
1457 		.rpc_cred = data->owner->so_cred,
1458 	};
1459 	struct rpc_task_setup task_setup_data = {
1460 		.rpc_client = server->client,
1461 		.rpc_message = &msg,
1462 		.callback_ops = &nfs4_open_ops,
1463 		.callback_data = data,
1464 		.workqueue = nfsiod_workqueue,
1465 		.flags = RPC_TASK_ASYNC,
1466 	};
1467 	int status;
1468 
1469 	kref_get(&data->kref);
1470 	data->rpc_done = 0;
1471 	data->rpc_status = 0;
1472 	data->cancelled = 0;
1473 	if (isrecover)
1474 		task_setup_data.callback_ops = &nfs4_recover_open_ops;
1475 	task = rpc_run_task(&task_setup_data);
1476         if (IS_ERR(task))
1477                 return PTR_ERR(task);
1478         status = nfs4_wait_for_completion_rpc_task(task);
1479         if (status != 0) {
1480                 data->cancelled = 1;
1481                 smp_wmb();
1482         } else
1483                 status = data->rpc_status;
1484         rpc_put_task(task);
1485 
1486 	return status;
1487 }
1488 
1489 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1490 {
1491 	struct inode *dir = data->dir->d_inode;
1492 	struct nfs_openres *o_res = &data->o_res;
1493         int status;
1494 
1495 	status = nfs4_run_open_task(data, 1);
1496 	if (status != 0 || !data->rpc_done)
1497 		return status;
1498 
1499 	nfs_refresh_inode(dir, o_res->dir_attr);
1500 
1501 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1502 		status = _nfs4_proc_open_confirm(data);
1503 		if (status != 0)
1504 			return status;
1505 	}
1506 
1507 	return status;
1508 }
1509 
1510 /*
1511  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1512  */
1513 static int _nfs4_proc_open(struct nfs4_opendata *data)
1514 {
1515 	struct inode *dir = data->dir->d_inode;
1516 	struct nfs_server *server = NFS_SERVER(dir);
1517 	struct nfs_openargs *o_arg = &data->o_arg;
1518 	struct nfs_openres *o_res = &data->o_res;
1519 	int status;
1520 
1521 	status = nfs4_run_open_task(data, 0);
1522 	if (status != 0 || !data->rpc_done)
1523 		return status;
1524 
1525 	if (o_arg->open_flags & O_CREAT) {
1526 		update_changeattr(dir, &o_res->cinfo);
1527 		nfs_post_op_update_inode(dir, o_res->dir_attr);
1528 	} else
1529 		nfs_refresh_inode(dir, o_res->dir_attr);
1530 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1531 		server->caps &= ~NFS_CAP_POSIX_LOCK;
1532 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1533 		status = _nfs4_proc_open_confirm(data);
1534 		if (status != 0)
1535 			return status;
1536 	}
1537 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1538 		_nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1539 	return 0;
1540 }
1541 
1542 static int nfs4_recover_expired_lease(struct nfs_server *server)
1543 {
1544 	struct nfs_client *clp = server->nfs_client;
1545 	unsigned int loop;
1546 	int ret;
1547 
1548 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1549 		ret = nfs4_wait_clnt_recover(clp);
1550 		if (ret != 0)
1551 			break;
1552 		if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1553 		    !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1554 			break;
1555 		nfs4_schedule_state_recovery(clp);
1556 		ret = -EIO;
1557 	}
1558 	return ret;
1559 }
1560 
1561 /*
1562  * OPEN_EXPIRED:
1563  * 	reclaim state on the server after a network partition.
1564  * 	Assumes caller holds the appropriate lock
1565  */
1566 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1567 {
1568 	struct nfs4_opendata *opendata;
1569 	int ret;
1570 
1571 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1572 	if (IS_ERR(opendata))
1573 		return PTR_ERR(opendata);
1574 	ret = nfs4_open_recover(opendata, state);
1575 	if (ret == -ESTALE)
1576 		d_drop(ctx->path.dentry);
1577 	nfs4_opendata_put(opendata);
1578 	return ret;
1579 }
1580 
1581 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1582 {
1583 	struct nfs_server *server = NFS_SERVER(state->inode);
1584 	struct nfs4_exception exception = { };
1585 	int err;
1586 
1587 	do {
1588 		err = _nfs4_open_expired(ctx, state);
1589 		switch (err) {
1590 		default:
1591 			goto out;
1592 		case -NFS4ERR_GRACE:
1593 		case -NFS4ERR_DELAY:
1594 		case -EKEYEXPIRED:
1595 			nfs4_handle_exception(server, err, &exception);
1596 			err = 0;
1597 		}
1598 	} while (exception.retry);
1599 out:
1600 	return err;
1601 }
1602 
1603 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1604 {
1605 	struct nfs_open_context *ctx;
1606 	int ret;
1607 
1608 	ctx = nfs4_state_find_open_context(state);
1609 	if (IS_ERR(ctx))
1610 		return PTR_ERR(ctx);
1611 	ret = nfs4_do_open_expired(ctx, state);
1612 	put_nfs_open_context(ctx);
1613 	return ret;
1614 }
1615 
1616 /*
1617  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1618  * fields corresponding to attributes that were used to store the verifier.
1619  * Make sure we clobber those fields in the later setattr call
1620  */
1621 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1622 {
1623 	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1624 	    !(sattr->ia_valid & ATTR_ATIME_SET))
1625 		sattr->ia_valid |= ATTR_ATIME;
1626 
1627 	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1628 	    !(sattr->ia_valid & ATTR_MTIME_SET))
1629 		sattr->ia_valid |= ATTR_MTIME;
1630 }
1631 
1632 /*
1633  * Returns a referenced nfs4_state
1634  */
1635 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1636 {
1637 	struct nfs4_state_owner  *sp;
1638 	struct nfs4_state     *state = NULL;
1639 	struct nfs_server       *server = NFS_SERVER(dir);
1640 	struct nfs4_opendata *opendata;
1641 	int status;
1642 
1643 	/* Protect against reboot recovery conflicts */
1644 	status = -ENOMEM;
1645 	if (!(sp = nfs4_get_state_owner(server, cred))) {
1646 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1647 		goto out_err;
1648 	}
1649 	status = nfs4_recover_expired_lease(server);
1650 	if (status != 0)
1651 		goto err_put_state_owner;
1652 	if (path->dentry->d_inode != NULL)
1653 		nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1654 	status = -ENOMEM;
1655 	opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1656 	if (opendata == NULL)
1657 		goto err_put_state_owner;
1658 
1659 	if (path->dentry->d_inode != NULL)
1660 		opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1661 
1662 	status = _nfs4_proc_open(opendata);
1663 	if (status != 0)
1664 		goto err_opendata_put;
1665 
1666 	state = nfs4_opendata_to_nfs4_state(opendata);
1667 	status = PTR_ERR(state);
1668 	if (IS_ERR(state))
1669 		goto err_opendata_put;
1670 	if (server->caps & NFS_CAP_POSIX_LOCK)
1671 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1672 
1673 	if (opendata->o_arg.open_flags & O_EXCL) {
1674 		nfs4_exclusive_attrset(opendata, sattr);
1675 
1676 		nfs_fattr_init(opendata->o_res.f_attr);
1677 		status = nfs4_do_setattr(state->inode, cred,
1678 				opendata->o_res.f_attr, sattr,
1679 				state);
1680 		if (status == 0)
1681 			nfs_setattr_update_inode(state->inode, sattr);
1682 		nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1683 	}
1684 	nfs4_opendata_put(opendata);
1685 	nfs4_put_state_owner(sp);
1686 	*res = state;
1687 	return 0;
1688 err_opendata_put:
1689 	nfs4_opendata_put(opendata);
1690 err_put_state_owner:
1691 	nfs4_put_state_owner(sp);
1692 out_err:
1693 	*res = NULL;
1694 	return status;
1695 }
1696 
1697 
1698 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1699 {
1700 	struct nfs4_exception exception = { };
1701 	struct nfs4_state *res;
1702 	int status;
1703 
1704 	do {
1705 		status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1706 		if (status == 0)
1707 			break;
1708 		/* NOTE: BAD_SEQID means the server and client disagree about the
1709 		 * book-keeping w.r.t. state-changing operations
1710 		 * (OPEN/CLOSE/LOCK/LOCKU...)
1711 		 * It is actually a sign of a bug on the client or on the server.
1712 		 *
1713 		 * If we receive a BAD_SEQID error in the particular case of
1714 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1715 		 * have unhashed the old state_owner for us, and that we can
1716 		 * therefore safely retry using a new one. We should still warn
1717 		 * the user though...
1718 		 */
1719 		if (status == -NFS4ERR_BAD_SEQID) {
1720 			printk(KERN_WARNING "NFS: v4 server %s "
1721 					" returned a bad sequence-id error!\n",
1722 					NFS_SERVER(dir)->nfs_client->cl_hostname);
1723 			exception.retry = 1;
1724 			continue;
1725 		}
1726 		/*
1727 		 * BAD_STATEID on OPEN means that the server cancelled our
1728 		 * state before it received the OPEN_CONFIRM.
1729 		 * Recover by retrying the request as per the discussion
1730 		 * on Page 181 of RFC3530.
1731 		 */
1732 		if (status == -NFS4ERR_BAD_STATEID) {
1733 			exception.retry = 1;
1734 			continue;
1735 		}
1736 		if (status == -EAGAIN) {
1737 			/* We must have found a delegation */
1738 			exception.retry = 1;
1739 			continue;
1740 		}
1741 		res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1742 					status, &exception));
1743 	} while (exception.retry);
1744 	return res;
1745 }
1746 
1747 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1748 			    struct nfs_fattr *fattr, struct iattr *sattr,
1749 			    struct nfs4_state *state)
1750 {
1751 	struct nfs_server *server = NFS_SERVER(inode);
1752         struct nfs_setattrargs  arg = {
1753                 .fh             = NFS_FH(inode),
1754                 .iap            = sattr,
1755 		.server		= server,
1756 		.bitmask = server->attr_bitmask,
1757         };
1758         struct nfs_setattrres  res = {
1759 		.fattr		= fattr,
1760 		.server		= server,
1761         };
1762         struct rpc_message msg = {
1763 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1764 		.rpc_argp	= &arg,
1765 		.rpc_resp	= &res,
1766 		.rpc_cred	= cred,
1767         };
1768 	unsigned long timestamp = jiffies;
1769 	int status;
1770 
1771 	nfs_fattr_init(fattr);
1772 
1773 	if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1774 		/* Use that stateid */
1775 	} else if (state != NULL) {
1776 		nfs4_copy_stateid(&arg.stateid, state, current->files);
1777 	} else
1778 		memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1779 
1780 	status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1781 	if (status == 0 && state != NULL)
1782 		renew_lease(server, timestamp);
1783 	return status;
1784 }
1785 
1786 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1787 			   struct nfs_fattr *fattr, struct iattr *sattr,
1788 			   struct nfs4_state *state)
1789 {
1790 	struct nfs_server *server = NFS_SERVER(inode);
1791 	struct nfs4_exception exception = { };
1792 	int err;
1793 	do {
1794 		err = nfs4_handle_exception(server,
1795 				_nfs4_do_setattr(inode, cred, fattr, sattr, state),
1796 				&exception);
1797 	} while (exception.retry);
1798 	return err;
1799 }
1800 
1801 struct nfs4_closedata {
1802 	struct path path;
1803 	struct inode *inode;
1804 	struct nfs4_state *state;
1805 	struct nfs_closeargs arg;
1806 	struct nfs_closeres res;
1807 	struct nfs_fattr fattr;
1808 	unsigned long timestamp;
1809 };
1810 
1811 static void nfs4_free_closedata(void *data)
1812 {
1813 	struct nfs4_closedata *calldata = data;
1814 	struct nfs4_state_owner *sp = calldata->state->owner;
1815 
1816 	nfs4_put_open_state(calldata->state);
1817 	nfs_free_seqid(calldata->arg.seqid);
1818 	nfs4_put_state_owner(sp);
1819 	path_put(&calldata->path);
1820 	kfree(calldata);
1821 }
1822 
1823 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1824 		fmode_t fmode)
1825 {
1826 	spin_lock(&state->owner->so_lock);
1827 	if (!(fmode & FMODE_READ))
1828 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1829 	if (!(fmode & FMODE_WRITE))
1830 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1831 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1832 	spin_unlock(&state->owner->so_lock);
1833 }
1834 
1835 static void nfs4_close_done(struct rpc_task *task, void *data)
1836 {
1837 	struct nfs4_closedata *calldata = data;
1838 	struct nfs4_state *state = calldata->state;
1839 	struct nfs_server *server = NFS_SERVER(calldata->inode);
1840 
1841 	nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1842 	if (RPC_ASSASSINATED(task))
1843 		return;
1844         /* hmm. we are done with the inode, and in the process of freeing
1845 	 * the state_owner. we keep this around to process errors
1846 	 */
1847 	switch (task->tk_status) {
1848 		case 0:
1849 			nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1850 			renew_lease(server, calldata->timestamp);
1851 			nfs4_close_clear_stateid_flags(state,
1852 					calldata->arg.fmode);
1853 			break;
1854 		case -NFS4ERR_STALE_STATEID:
1855 		case -NFS4ERR_OLD_STATEID:
1856 		case -NFS4ERR_BAD_STATEID:
1857 		case -NFS4ERR_EXPIRED:
1858 			if (calldata->arg.fmode == 0)
1859 				break;
1860 		default:
1861 			if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1862 				rpc_restart_call_prepare(task);
1863 	}
1864 	nfs_release_seqid(calldata->arg.seqid);
1865 	nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1866 }
1867 
1868 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1869 {
1870 	struct nfs4_closedata *calldata = data;
1871 	struct nfs4_state *state = calldata->state;
1872 	int call_close = 0;
1873 
1874 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1875 		return;
1876 
1877 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1878 	calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1879 	spin_lock(&state->owner->so_lock);
1880 	/* Calculate the change in open mode */
1881 	if (state->n_rdwr == 0) {
1882 		if (state->n_rdonly == 0) {
1883 			call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1884 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1885 			calldata->arg.fmode &= ~FMODE_READ;
1886 		}
1887 		if (state->n_wronly == 0) {
1888 			call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1889 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1890 			calldata->arg.fmode &= ~FMODE_WRITE;
1891 		}
1892 	}
1893 	spin_unlock(&state->owner->so_lock);
1894 
1895 	if (!call_close) {
1896 		/* Note: exit _without_ calling nfs4_close_done */
1897 		task->tk_action = NULL;
1898 		return;
1899 	}
1900 
1901 	if (calldata->arg.fmode == 0)
1902 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1903 
1904 	nfs_fattr_init(calldata->res.fattr);
1905 	calldata->timestamp = jiffies;
1906 	if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1907 				&calldata->arg.seq_args, &calldata->res.seq_res,
1908 				1, task))
1909 		return;
1910 	rpc_call_start(task);
1911 }
1912 
1913 static const struct rpc_call_ops nfs4_close_ops = {
1914 	.rpc_call_prepare = nfs4_close_prepare,
1915 	.rpc_call_done = nfs4_close_done,
1916 	.rpc_release = nfs4_free_closedata,
1917 };
1918 
1919 /*
1920  * It is possible for data to be read/written from a mem-mapped file
1921  * after the sys_close call (which hits the vfs layer as a flush).
1922  * This means that we can't safely call nfsv4 close on a file until
1923  * the inode is cleared. This in turn means that we are not good
1924  * NFSv4 citizens - we do not indicate to the server to update the file's
1925  * share state even when we are done with one of the three share
1926  * stateid's in the inode.
1927  *
1928  * NOTE: Caller must be holding the sp->so_owner semaphore!
1929  */
1930 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
1931 {
1932 	struct nfs_server *server = NFS_SERVER(state->inode);
1933 	struct nfs4_closedata *calldata;
1934 	struct nfs4_state_owner *sp = state->owner;
1935 	struct rpc_task *task;
1936 	struct rpc_message msg = {
1937 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1938 		.rpc_cred = state->owner->so_cred,
1939 	};
1940 	struct rpc_task_setup task_setup_data = {
1941 		.rpc_client = server->client,
1942 		.rpc_message = &msg,
1943 		.callback_ops = &nfs4_close_ops,
1944 		.workqueue = nfsiod_workqueue,
1945 		.flags = RPC_TASK_ASYNC,
1946 	};
1947 	int status = -ENOMEM;
1948 
1949 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
1950 	if (calldata == NULL)
1951 		goto out;
1952 	calldata->inode = state->inode;
1953 	calldata->state = state;
1954 	calldata->arg.fh = NFS_FH(state->inode);
1955 	calldata->arg.stateid = &state->open_stateid;
1956 	/* Serialization for the sequence id */
1957 	calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
1958 	if (calldata->arg.seqid == NULL)
1959 		goto out_free_calldata;
1960 	calldata->arg.fmode = 0;
1961 	calldata->arg.bitmask = server->cache_consistency_bitmask;
1962 	calldata->res.fattr = &calldata->fattr;
1963 	calldata->res.seqid = calldata->arg.seqid;
1964 	calldata->res.server = server;
1965 	calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1966 	path_get(path);
1967 	calldata->path = *path;
1968 
1969 	msg.rpc_argp = &calldata->arg,
1970 	msg.rpc_resp = &calldata->res,
1971 	task_setup_data.callback_data = calldata;
1972 	task = rpc_run_task(&task_setup_data);
1973 	if (IS_ERR(task))
1974 		return PTR_ERR(task);
1975 	status = 0;
1976 	if (wait)
1977 		status = rpc_wait_for_completion_task(task);
1978 	rpc_put_task(task);
1979 	return status;
1980 out_free_calldata:
1981 	kfree(calldata);
1982 out:
1983 	nfs4_put_open_state(state);
1984 	nfs4_put_state_owner(sp);
1985 	return status;
1986 }
1987 
1988 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1989 {
1990 	struct file *filp;
1991 	int ret;
1992 
1993 	/* If the open_intent is for execute, we have an extra check to make */
1994 	if (fmode & FMODE_EXEC) {
1995 		ret = nfs_may_open(state->inode,
1996 				state->owner->so_cred,
1997 				nd->intent.open.flags);
1998 		if (ret < 0)
1999 			goto out_close;
2000 	}
2001 	filp = lookup_instantiate_filp(nd, path->dentry, NULL);
2002 	if (!IS_ERR(filp)) {
2003 		struct nfs_open_context *ctx;
2004 		ctx = nfs_file_open_context(filp);
2005 		ctx->state = state;
2006 		return 0;
2007 	}
2008 	ret = PTR_ERR(filp);
2009 out_close:
2010 	nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
2011 	return ret;
2012 }
2013 
2014 struct dentry *
2015 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2016 {
2017 	struct path path = {
2018 		.mnt = nd->path.mnt,
2019 		.dentry = dentry,
2020 	};
2021 	struct dentry *parent;
2022 	struct iattr attr;
2023 	struct rpc_cred *cred;
2024 	struct nfs4_state *state;
2025 	struct dentry *res;
2026 	fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
2027 
2028 	if (nd->flags & LOOKUP_CREATE) {
2029 		attr.ia_mode = nd->intent.open.create_mode;
2030 		attr.ia_valid = ATTR_MODE;
2031 		if (!IS_POSIXACL(dir))
2032 			attr.ia_mode &= ~current_umask();
2033 	} else {
2034 		attr.ia_valid = 0;
2035 		BUG_ON(nd->intent.open.flags & O_CREAT);
2036 	}
2037 
2038 	cred = rpc_lookup_cred();
2039 	if (IS_ERR(cred))
2040 		return (struct dentry *)cred;
2041 	parent = dentry->d_parent;
2042 	/* Protect against concurrent sillydeletes */
2043 	nfs_block_sillyrename(parent);
2044 	state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
2045 	put_rpccred(cred);
2046 	if (IS_ERR(state)) {
2047 		if (PTR_ERR(state) == -ENOENT) {
2048 			d_add(dentry, NULL);
2049 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2050 		}
2051 		nfs_unblock_sillyrename(parent);
2052 		return (struct dentry *)state;
2053 	}
2054 	res = d_add_unique(dentry, igrab(state->inode));
2055 	if (res != NULL)
2056 		path.dentry = res;
2057 	nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
2058 	nfs_unblock_sillyrename(parent);
2059 	nfs4_intent_set_file(nd, &path, state, fmode);
2060 	return res;
2061 }
2062 
2063 int
2064 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
2065 {
2066 	struct path path = {
2067 		.mnt = nd->path.mnt,
2068 		.dentry = dentry,
2069 	};
2070 	struct rpc_cred *cred;
2071 	struct nfs4_state *state;
2072 	fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
2073 
2074 	cred = rpc_lookup_cred();
2075 	if (IS_ERR(cred))
2076 		return PTR_ERR(cred);
2077 	state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
2078 	put_rpccred(cred);
2079 	if (IS_ERR(state)) {
2080 		switch (PTR_ERR(state)) {
2081 			case -EPERM:
2082 			case -EACCES:
2083 			case -EDQUOT:
2084 			case -ENOSPC:
2085 			case -EROFS:
2086 				return PTR_ERR(state);
2087 			default:
2088 				goto out_drop;
2089 		}
2090 	}
2091 	if (state->inode == dentry->d_inode) {
2092 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2093 		nfs4_intent_set_file(nd, &path, state, fmode);
2094 		return 1;
2095 	}
2096 	nfs4_close_sync(&path, state, fmode);
2097 out_drop:
2098 	d_drop(dentry);
2099 	return 0;
2100 }
2101 
2102 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2103 {
2104 	if (ctx->state == NULL)
2105 		return;
2106 	if (is_sync)
2107 		nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2108 	else
2109 		nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2110 }
2111 
2112 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2113 {
2114 	struct nfs4_server_caps_arg args = {
2115 		.fhandle = fhandle,
2116 	};
2117 	struct nfs4_server_caps_res res = {};
2118 	struct rpc_message msg = {
2119 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2120 		.rpc_argp = &args,
2121 		.rpc_resp = &res,
2122 	};
2123 	int status;
2124 
2125 	status = nfs4_call_sync(server, &msg, &args, &res, 0);
2126 	if (status == 0) {
2127 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2128 		server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2129 				NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2130 				NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2131 				NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2132 				NFS_CAP_CTIME|NFS_CAP_MTIME);
2133 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2134 			server->caps |= NFS_CAP_ACLS;
2135 		if (res.has_links != 0)
2136 			server->caps |= NFS_CAP_HARDLINKS;
2137 		if (res.has_symlinks != 0)
2138 			server->caps |= NFS_CAP_SYMLINKS;
2139 		if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2140 			server->caps |= NFS_CAP_FILEID;
2141 		if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2142 			server->caps |= NFS_CAP_MODE;
2143 		if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2144 			server->caps |= NFS_CAP_NLINK;
2145 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2146 			server->caps |= NFS_CAP_OWNER;
2147 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2148 			server->caps |= NFS_CAP_OWNER_GROUP;
2149 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2150 			server->caps |= NFS_CAP_ATIME;
2151 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2152 			server->caps |= NFS_CAP_CTIME;
2153 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2154 			server->caps |= NFS_CAP_MTIME;
2155 
2156 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2157 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2158 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2159 		server->acl_bitmask = res.acl_bitmask;
2160 	}
2161 
2162 	return status;
2163 }
2164 
2165 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2166 {
2167 	struct nfs4_exception exception = { };
2168 	int err;
2169 	do {
2170 		err = nfs4_handle_exception(server,
2171 				_nfs4_server_capabilities(server, fhandle),
2172 				&exception);
2173 	} while (exception.retry);
2174 	return err;
2175 }
2176 
2177 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2178 		struct nfs_fsinfo *info)
2179 {
2180 	struct nfs4_lookup_root_arg args = {
2181 		.bitmask = nfs4_fattr_bitmap,
2182 	};
2183 	struct nfs4_lookup_res res = {
2184 		.server = server,
2185 		.fattr = info->fattr,
2186 		.fh = fhandle,
2187 	};
2188 	struct rpc_message msg = {
2189 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2190 		.rpc_argp = &args,
2191 		.rpc_resp = &res,
2192 	};
2193 
2194 	nfs_fattr_init(info->fattr);
2195 	return nfs4_call_sync(server, &msg, &args, &res, 0);
2196 }
2197 
2198 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2199 		struct nfs_fsinfo *info)
2200 {
2201 	struct nfs4_exception exception = { };
2202 	int err;
2203 	do {
2204 		err = nfs4_handle_exception(server,
2205 				_nfs4_lookup_root(server, fhandle, info),
2206 				&exception);
2207 	} while (exception.retry);
2208 	return err;
2209 }
2210 
2211 /*
2212  * get the file handle for the "/" directory on the server
2213  */
2214 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2215 			      struct nfs_fsinfo *info)
2216 {
2217 	int status;
2218 
2219 	status = nfs4_lookup_root(server, fhandle, info);
2220 	if (status == 0)
2221 		status = nfs4_server_capabilities(server, fhandle);
2222 	if (status == 0)
2223 		status = nfs4_do_fsinfo(server, fhandle, info);
2224 	return nfs4_map_errors(status);
2225 }
2226 
2227 /*
2228  * Get locations and (maybe) other attributes of a referral.
2229  * Note that we'll actually follow the referral later when
2230  * we detect fsid mismatch in inode revalidation
2231  */
2232 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2233 {
2234 	int status = -ENOMEM;
2235 	struct page *page = NULL;
2236 	struct nfs4_fs_locations *locations = NULL;
2237 
2238 	page = alloc_page(GFP_KERNEL);
2239 	if (page == NULL)
2240 		goto out;
2241 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2242 	if (locations == NULL)
2243 		goto out;
2244 
2245 	status = nfs4_proc_fs_locations(dir, name, locations, page);
2246 	if (status != 0)
2247 		goto out;
2248 	/* Make sure server returned a different fsid for the referral */
2249 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2250 		dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2251 		status = -EIO;
2252 		goto out;
2253 	}
2254 
2255 	memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2256 	fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2257 	if (!fattr->mode)
2258 		fattr->mode = S_IFDIR;
2259 	memset(fhandle, 0, sizeof(struct nfs_fh));
2260 out:
2261 	if (page)
2262 		__free_page(page);
2263 	if (locations)
2264 		kfree(locations);
2265 	return status;
2266 }
2267 
2268 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2269 {
2270 	struct nfs4_getattr_arg args = {
2271 		.fh = fhandle,
2272 		.bitmask = server->attr_bitmask,
2273 	};
2274 	struct nfs4_getattr_res res = {
2275 		.fattr = fattr,
2276 		.server = server,
2277 	};
2278 	struct rpc_message msg = {
2279 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2280 		.rpc_argp = &args,
2281 		.rpc_resp = &res,
2282 	};
2283 
2284 	nfs_fattr_init(fattr);
2285 	return nfs4_call_sync(server, &msg, &args, &res, 0);
2286 }
2287 
2288 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2289 {
2290 	struct nfs4_exception exception = { };
2291 	int err;
2292 	do {
2293 		err = nfs4_handle_exception(server,
2294 				_nfs4_proc_getattr(server, fhandle, fattr),
2295 				&exception);
2296 	} while (exception.retry);
2297 	return err;
2298 }
2299 
2300 /*
2301  * The file is not closed if it is opened due to the a request to change
2302  * the size of the file. The open call will not be needed once the
2303  * VFS layer lookup-intents are implemented.
2304  *
2305  * Close is called when the inode is destroyed.
2306  * If we haven't opened the file for O_WRONLY, we
2307  * need to in the size_change case to obtain a stateid.
2308  *
2309  * Got race?
2310  * Because OPEN is always done by name in nfsv4, it is
2311  * possible that we opened a different file by the same
2312  * name.  We can recognize this race condition, but we
2313  * can't do anything about it besides returning an error.
2314  *
2315  * This will be fixed with VFS changes (lookup-intent).
2316  */
2317 static int
2318 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2319 		  struct iattr *sattr)
2320 {
2321 	struct inode *inode = dentry->d_inode;
2322 	struct rpc_cred *cred = NULL;
2323 	struct nfs4_state *state = NULL;
2324 	int status;
2325 
2326 	nfs_fattr_init(fattr);
2327 
2328 	/* Search for an existing open(O_WRITE) file */
2329 	if (sattr->ia_valid & ATTR_FILE) {
2330 		struct nfs_open_context *ctx;
2331 
2332 		ctx = nfs_file_open_context(sattr->ia_file);
2333 		if (ctx) {
2334 			cred = ctx->cred;
2335 			state = ctx->state;
2336 		}
2337 	}
2338 
2339 	status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2340 	if (status == 0)
2341 		nfs_setattr_update_inode(inode, sattr);
2342 	return status;
2343 }
2344 
2345 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2346 		const struct qstr *name, struct nfs_fh *fhandle,
2347 		struct nfs_fattr *fattr)
2348 {
2349 	int		       status;
2350 	struct nfs4_lookup_arg args = {
2351 		.bitmask = server->attr_bitmask,
2352 		.dir_fh = dirfh,
2353 		.name = name,
2354 	};
2355 	struct nfs4_lookup_res res = {
2356 		.server = server,
2357 		.fattr = fattr,
2358 		.fh = fhandle,
2359 	};
2360 	struct rpc_message msg = {
2361 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2362 		.rpc_argp = &args,
2363 		.rpc_resp = &res,
2364 	};
2365 
2366 	nfs_fattr_init(fattr);
2367 
2368 	dprintk("NFS call  lookupfh %s\n", name->name);
2369 	status = nfs4_call_sync(server, &msg, &args, &res, 0);
2370 	dprintk("NFS reply lookupfh: %d\n", status);
2371 	return status;
2372 }
2373 
2374 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2375 			      struct qstr *name, struct nfs_fh *fhandle,
2376 			      struct nfs_fattr *fattr)
2377 {
2378 	struct nfs4_exception exception = { };
2379 	int err;
2380 	do {
2381 		err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2382 		/* FIXME: !!!! */
2383 		if (err == -NFS4ERR_MOVED) {
2384 			err = -EREMOTE;
2385 			break;
2386 		}
2387 		err = nfs4_handle_exception(server, err, &exception);
2388 	} while (exception.retry);
2389 	return err;
2390 }
2391 
2392 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2393 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2394 {
2395 	int status;
2396 
2397 	dprintk("NFS call  lookup %s\n", name->name);
2398 	status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2399 	if (status == -NFS4ERR_MOVED)
2400 		status = nfs4_get_referral(dir, name, fattr, fhandle);
2401 	dprintk("NFS reply lookup: %d\n", status);
2402 	return status;
2403 }
2404 
2405 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2406 {
2407 	struct nfs4_exception exception = { };
2408 	int err;
2409 	do {
2410 		err = nfs4_handle_exception(NFS_SERVER(dir),
2411 				_nfs4_proc_lookup(dir, name, fhandle, fattr),
2412 				&exception);
2413 	} while (exception.retry);
2414 	return err;
2415 }
2416 
2417 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2418 {
2419 	struct nfs_server *server = NFS_SERVER(inode);
2420 	struct nfs4_accessargs args = {
2421 		.fh = NFS_FH(inode),
2422 		.bitmask = server->attr_bitmask,
2423 	};
2424 	struct nfs4_accessres res = {
2425 		.server = server,
2426 	};
2427 	struct rpc_message msg = {
2428 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2429 		.rpc_argp = &args,
2430 		.rpc_resp = &res,
2431 		.rpc_cred = entry->cred,
2432 	};
2433 	int mode = entry->mask;
2434 	int status;
2435 
2436 	/*
2437 	 * Determine which access bits we want to ask for...
2438 	 */
2439 	if (mode & MAY_READ)
2440 		args.access |= NFS4_ACCESS_READ;
2441 	if (S_ISDIR(inode->i_mode)) {
2442 		if (mode & MAY_WRITE)
2443 			args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2444 		if (mode & MAY_EXEC)
2445 			args.access |= NFS4_ACCESS_LOOKUP;
2446 	} else {
2447 		if (mode & MAY_WRITE)
2448 			args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2449 		if (mode & MAY_EXEC)
2450 			args.access |= NFS4_ACCESS_EXECUTE;
2451 	}
2452 
2453 	res.fattr = nfs_alloc_fattr();
2454 	if (res.fattr == NULL)
2455 		return -ENOMEM;
2456 
2457 	status = nfs4_call_sync(server, &msg, &args, &res, 0);
2458 	if (!status) {
2459 		entry->mask = 0;
2460 		if (res.access & NFS4_ACCESS_READ)
2461 			entry->mask |= MAY_READ;
2462 		if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2463 			entry->mask |= MAY_WRITE;
2464 		if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2465 			entry->mask |= MAY_EXEC;
2466 		nfs_refresh_inode(inode, res.fattr);
2467 	}
2468 	nfs_free_fattr(res.fattr);
2469 	return status;
2470 }
2471 
2472 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2473 {
2474 	struct nfs4_exception exception = { };
2475 	int err;
2476 	do {
2477 		err = nfs4_handle_exception(NFS_SERVER(inode),
2478 				_nfs4_proc_access(inode, entry),
2479 				&exception);
2480 	} while (exception.retry);
2481 	return err;
2482 }
2483 
2484 /*
2485  * TODO: For the time being, we don't try to get any attributes
2486  * along with any of the zero-copy operations READ, READDIR,
2487  * READLINK, WRITE.
2488  *
2489  * In the case of the first three, we want to put the GETATTR
2490  * after the read-type operation -- this is because it is hard
2491  * to predict the length of a GETATTR response in v4, and thus
2492  * align the READ data correctly.  This means that the GETATTR
2493  * may end up partially falling into the page cache, and we should
2494  * shift it into the 'tail' of the xdr_buf before processing.
2495  * To do this efficiently, we need to know the total length
2496  * of data received, which doesn't seem to be available outside
2497  * of the RPC layer.
2498  *
2499  * In the case of WRITE, we also want to put the GETATTR after
2500  * the operation -- in this case because we want to make sure
2501  * we get the post-operation mtime and size.  This means that
2502  * we can't use xdr_encode_pages() as written: we need a variant
2503  * of it which would leave room in the 'tail' iovec.
2504  *
2505  * Both of these changes to the XDR layer would in fact be quite
2506  * minor, but I decided to leave them for a subsequent patch.
2507  */
2508 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2509 		unsigned int pgbase, unsigned int pglen)
2510 {
2511 	struct nfs4_readlink args = {
2512 		.fh       = NFS_FH(inode),
2513 		.pgbase	  = pgbase,
2514 		.pglen    = pglen,
2515 		.pages    = &page,
2516 	};
2517 	struct nfs4_readlink_res res;
2518 	struct rpc_message msg = {
2519 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2520 		.rpc_argp = &args,
2521 		.rpc_resp = &res,
2522 	};
2523 
2524 	return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2525 }
2526 
2527 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2528 		unsigned int pgbase, unsigned int pglen)
2529 {
2530 	struct nfs4_exception exception = { };
2531 	int err;
2532 	do {
2533 		err = nfs4_handle_exception(NFS_SERVER(inode),
2534 				_nfs4_proc_readlink(inode, page, pgbase, pglen),
2535 				&exception);
2536 	} while (exception.retry);
2537 	return err;
2538 }
2539 
2540 /*
2541  * Got race?
2542  * We will need to arrange for the VFS layer to provide an atomic open.
2543  * Until then, this create/open method is prone to inefficiency and race
2544  * conditions due to the lookup, create, and open VFS calls from sys_open()
2545  * placed on the wire.
2546  *
2547  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2548  * The file will be opened again in the subsequent VFS open call
2549  * (nfs4_proc_file_open).
2550  *
2551  * The open for read will just hang around to be used by any process that
2552  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2553  */
2554 
2555 static int
2556 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2557                  int flags, struct nameidata *nd)
2558 {
2559 	struct path path = {
2560 		.mnt = nd->path.mnt,
2561 		.dentry = dentry,
2562 	};
2563 	struct nfs4_state *state;
2564 	struct rpc_cred *cred;
2565 	fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2566 	int status = 0;
2567 
2568 	cred = rpc_lookup_cred();
2569 	if (IS_ERR(cred)) {
2570 		status = PTR_ERR(cred);
2571 		goto out;
2572 	}
2573 	state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2574 	d_drop(dentry);
2575 	if (IS_ERR(state)) {
2576 		status = PTR_ERR(state);
2577 		goto out_putcred;
2578 	}
2579 	d_add(dentry, igrab(state->inode));
2580 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2581 	if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2582 		status = nfs4_intent_set_file(nd, &path, state, fmode);
2583 	else
2584 		nfs4_close_sync(&path, state, fmode);
2585 out_putcred:
2586 	put_rpccred(cred);
2587 out:
2588 	return status;
2589 }
2590 
2591 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2592 {
2593 	struct nfs_server *server = NFS_SERVER(dir);
2594 	struct nfs_removeargs args = {
2595 		.fh = NFS_FH(dir),
2596 		.name.len = name->len,
2597 		.name.name = name->name,
2598 		.bitmask = server->attr_bitmask,
2599 	};
2600 	struct nfs_removeres res = {
2601 		.server = server,
2602 	};
2603 	struct rpc_message msg = {
2604 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2605 		.rpc_argp = &args,
2606 		.rpc_resp = &res,
2607 	};
2608 	int status = -ENOMEM;
2609 
2610 	res.dir_attr = nfs_alloc_fattr();
2611 	if (res.dir_attr == NULL)
2612 		goto out;
2613 
2614 	status = nfs4_call_sync(server, &msg, &args, &res, 1);
2615 	if (status == 0) {
2616 		update_changeattr(dir, &res.cinfo);
2617 		nfs_post_op_update_inode(dir, res.dir_attr);
2618 	}
2619 	nfs_free_fattr(res.dir_attr);
2620 out:
2621 	return status;
2622 }
2623 
2624 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2625 {
2626 	struct nfs4_exception exception = { };
2627 	int err;
2628 	do {
2629 		err = nfs4_handle_exception(NFS_SERVER(dir),
2630 				_nfs4_proc_remove(dir, name),
2631 				&exception);
2632 	} while (exception.retry);
2633 	return err;
2634 }
2635 
2636 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2637 {
2638 	struct nfs_server *server = NFS_SERVER(dir);
2639 	struct nfs_removeargs *args = msg->rpc_argp;
2640 	struct nfs_removeres *res = msg->rpc_resp;
2641 
2642 	args->bitmask = server->cache_consistency_bitmask;
2643 	res->server = server;
2644 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2645 }
2646 
2647 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2648 {
2649 	struct nfs_removeres *res = task->tk_msg.rpc_resp;
2650 
2651 	nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2652 	if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2653 		return 0;
2654 	update_changeattr(dir, &res->cinfo);
2655 	nfs_post_op_update_inode(dir, res->dir_attr);
2656 	return 1;
2657 }
2658 
2659 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2660 		struct inode *new_dir, struct qstr *new_name)
2661 {
2662 	struct nfs_server *server = NFS_SERVER(old_dir);
2663 	struct nfs4_rename_arg arg = {
2664 		.old_dir = NFS_FH(old_dir),
2665 		.new_dir = NFS_FH(new_dir),
2666 		.old_name = old_name,
2667 		.new_name = new_name,
2668 		.bitmask = server->attr_bitmask,
2669 	};
2670 	struct nfs4_rename_res res = {
2671 		.server = server,
2672 	};
2673 	struct rpc_message msg = {
2674 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2675 		.rpc_argp = &arg,
2676 		.rpc_resp = &res,
2677 	};
2678 	int status = -ENOMEM;
2679 
2680 	res.old_fattr = nfs_alloc_fattr();
2681 	res.new_fattr = nfs_alloc_fattr();
2682 	if (res.old_fattr == NULL || res.new_fattr == NULL)
2683 		goto out;
2684 
2685 	status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2686 	if (!status) {
2687 		update_changeattr(old_dir, &res.old_cinfo);
2688 		nfs_post_op_update_inode(old_dir, res.old_fattr);
2689 		update_changeattr(new_dir, &res.new_cinfo);
2690 		nfs_post_op_update_inode(new_dir, res.new_fattr);
2691 	}
2692 out:
2693 	nfs_free_fattr(res.new_fattr);
2694 	nfs_free_fattr(res.old_fattr);
2695 	return status;
2696 }
2697 
2698 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2699 		struct inode *new_dir, struct qstr *new_name)
2700 {
2701 	struct nfs4_exception exception = { };
2702 	int err;
2703 	do {
2704 		err = nfs4_handle_exception(NFS_SERVER(old_dir),
2705 				_nfs4_proc_rename(old_dir, old_name,
2706 					new_dir, new_name),
2707 				&exception);
2708 	} while (exception.retry);
2709 	return err;
2710 }
2711 
2712 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2713 {
2714 	struct nfs_server *server = NFS_SERVER(inode);
2715 	struct nfs4_link_arg arg = {
2716 		.fh     = NFS_FH(inode),
2717 		.dir_fh = NFS_FH(dir),
2718 		.name   = name,
2719 		.bitmask = server->attr_bitmask,
2720 	};
2721 	struct nfs4_link_res res = {
2722 		.server = server,
2723 	};
2724 	struct rpc_message msg = {
2725 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2726 		.rpc_argp = &arg,
2727 		.rpc_resp = &res,
2728 	};
2729 	int status = -ENOMEM;
2730 
2731 	res.fattr = nfs_alloc_fattr();
2732 	res.dir_attr = nfs_alloc_fattr();
2733 	if (res.fattr == NULL || res.dir_attr == NULL)
2734 		goto out;
2735 
2736 	status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2737 	if (!status) {
2738 		update_changeattr(dir, &res.cinfo);
2739 		nfs_post_op_update_inode(dir, res.dir_attr);
2740 		nfs_post_op_update_inode(inode, res.fattr);
2741 	}
2742 out:
2743 	nfs_free_fattr(res.dir_attr);
2744 	nfs_free_fattr(res.fattr);
2745 	return status;
2746 }
2747 
2748 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2749 {
2750 	struct nfs4_exception exception = { };
2751 	int err;
2752 	do {
2753 		err = nfs4_handle_exception(NFS_SERVER(inode),
2754 				_nfs4_proc_link(inode, dir, name),
2755 				&exception);
2756 	} while (exception.retry);
2757 	return err;
2758 }
2759 
2760 struct nfs4_createdata {
2761 	struct rpc_message msg;
2762 	struct nfs4_create_arg arg;
2763 	struct nfs4_create_res res;
2764 	struct nfs_fh fh;
2765 	struct nfs_fattr fattr;
2766 	struct nfs_fattr dir_fattr;
2767 };
2768 
2769 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2770 		struct qstr *name, struct iattr *sattr, u32 ftype)
2771 {
2772 	struct nfs4_createdata *data;
2773 
2774 	data = kzalloc(sizeof(*data), GFP_KERNEL);
2775 	if (data != NULL) {
2776 		struct nfs_server *server = NFS_SERVER(dir);
2777 
2778 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2779 		data->msg.rpc_argp = &data->arg;
2780 		data->msg.rpc_resp = &data->res;
2781 		data->arg.dir_fh = NFS_FH(dir);
2782 		data->arg.server = server;
2783 		data->arg.name = name;
2784 		data->arg.attrs = sattr;
2785 		data->arg.ftype = ftype;
2786 		data->arg.bitmask = server->attr_bitmask;
2787 		data->res.server = server;
2788 		data->res.fh = &data->fh;
2789 		data->res.fattr = &data->fattr;
2790 		data->res.dir_fattr = &data->dir_fattr;
2791 		nfs_fattr_init(data->res.fattr);
2792 		nfs_fattr_init(data->res.dir_fattr);
2793 	}
2794 	return data;
2795 }
2796 
2797 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2798 {
2799 	int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2800 				    &data->arg, &data->res, 1);
2801 	if (status == 0) {
2802 		update_changeattr(dir, &data->res.dir_cinfo);
2803 		nfs_post_op_update_inode(dir, data->res.dir_fattr);
2804 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2805 	}
2806 	return status;
2807 }
2808 
2809 static void nfs4_free_createdata(struct nfs4_createdata *data)
2810 {
2811 	kfree(data);
2812 }
2813 
2814 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2815 		struct page *page, unsigned int len, struct iattr *sattr)
2816 {
2817 	struct nfs4_createdata *data;
2818 	int status = -ENAMETOOLONG;
2819 
2820 	if (len > NFS4_MAXPATHLEN)
2821 		goto out;
2822 
2823 	status = -ENOMEM;
2824 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2825 	if (data == NULL)
2826 		goto out;
2827 
2828 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2829 	data->arg.u.symlink.pages = &page;
2830 	data->arg.u.symlink.len = len;
2831 
2832 	status = nfs4_do_create(dir, dentry, data);
2833 
2834 	nfs4_free_createdata(data);
2835 out:
2836 	return status;
2837 }
2838 
2839 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2840 		struct page *page, unsigned int len, struct iattr *sattr)
2841 {
2842 	struct nfs4_exception exception = { };
2843 	int err;
2844 	do {
2845 		err = nfs4_handle_exception(NFS_SERVER(dir),
2846 				_nfs4_proc_symlink(dir, dentry, page,
2847 							len, sattr),
2848 				&exception);
2849 	} while (exception.retry);
2850 	return err;
2851 }
2852 
2853 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2854 		struct iattr *sattr)
2855 {
2856 	struct nfs4_createdata *data;
2857 	int status = -ENOMEM;
2858 
2859 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2860 	if (data == NULL)
2861 		goto out;
2862 
2863 	status = nfs4_do_create(dir, dentry, data);
2864 
2865 	nfs4_free_createdata(data);
2866 out:
2867 	return status;
2868 }
2869 
2870 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2871 		struct iattr *sattr)
2872 {
2873 	struct nfs4_exception exception = { };
2874 	int err;
2875 	do {
2876 		err = nfs4_handle_exception(NFS_SERVER(dir),
2877 				_nfs4_proc_mkdir(dir, dentry, sattr),
2878 				&exception);
2879 	} while (exception.retry);
2880 	return err;
2881 }
2882 
2883 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2884                   u64 cookie, struct page *page, unsigned int count, int plus)
2885 {
2886 	struct inode		*dir = dentry->d_inode;
2887 	struct nfs4_readdir_arg args = {
2888 		.fh = NFS_FH(dir),
2889 		.pages = &page,
2890 		.pgbase = 0,
2891 		.count = count,
2892 		.bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2893 	};
2894 	struct nfs4_readdir_res res;
2895 	struct rpc_message msg = {
2896 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2897 		.rpc_argp = &args,
2898 		.rpc_resp = &res,
2899 		.rpc_cred = cred,
2900 	};
2901 	int			status;
2902 
2903 	dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2904 			dentry->d_parent->d_name.name,
2905 			dentry->d_name.name,
2906 			(unsigned long long)cookie);
2907 	nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2908 	res.pgbase = args.pgbase;
2909 	status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2910 	if (status == 0)
2911 		memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2912 
2913 	nfs_invalidate_atime(dir);
2914 
2915 	dprintk("%s: returns %d\n", __func__, status);
2916 	return status;
2917 }
2918 
2919 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2920                   u64 cookie, struct page *page, unsigned int count, int plus)
2921 {
2922 	struct nfs4_exception exception = { };
2923 	int err;
2924 	do {
2925 		err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2926 				_nfs4_proc_readdir(dentry, cred, cookie,
2927 					page, count, plus),
2928 				&exception);
2929 	} while (exception.retry);
2930 	return err;
2931 }
2932 
2933 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2934 		struct iattr *sattr, dev_t rdev)
2935 {
2936 	struct nfs4_createdata *data;
2937 	int mode = sattr->ia_mode;
2938 	int status = -ENOMEM;
2939 
2940 	BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2941 	BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2942 
2943 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2944 	if (data == NULL)
2945 		goto out;
2946 
2947 	if (S_ISFIFO(mode))
2948 		data->arg.ftype = NF4FIFO;
2949 	else if (S_ISBLK(mode)) {
2950 		data->arg.ftype = NF4BLK;
2951 		data->arg.u.device.specdata1 = MAJOR(rdev);
2952 		data->arg.u.device.specdata2 = MINOR(rdev);
2953 	}
2954 	else if (S_ISCHR(mode)) {
2955 		data->arg.ftype = NF4CHR;
2956 		data->arg.u.device.specdata1 = MAJOR(rdev);
2957 		data->arg.u.device.specdata2 = MINOR(rdev);
2958 	}
2959 
2960 	status = nfs4_do_create(dir, dentry, data);
2961 
2962 	nfs4_free_createdata(data);
2963 out:
2964 	return status;
2965 }
2966 
2967 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2968 		struct iattr *sattr, dev_t rdev)
2969 {
2970 	struct nfs4_exception exception = { };
2971 	int err;
2972 	do {
2973 		err = nfs4_handle_exception(NFS_SERVER(dir),
2974 				_nfs4_proc_mknod(dir, dentry, sattr, rdev),
2975 				&exception);
2976 	} while (exception.retry);
2977 	return err;
2978 }
2979 
2980 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2981 		 struct nfs_fsstat *fsstat)
2982 {
2983 	struct nfs4_statfs_arg args = {
2984 		.fh = fhandle,
2985 		.bitmask = server->attr_bitmask,
2986 	};
2987 	struct nfs4_statfs_res res = {
2988 		.fsstat = fsstat,
2989 	};
2990 	struct rpc_message msg = {
2991 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2992 		.rpc_argp = &args,
2993 		.rpc_resp = &res,
2994 	};
2995 
2996 	nfs_fattr_init(fsstat->fattr);
2997 	return  nfs4_call_sync(server, &msg, &args, &res, 0);
2998 }
2999 
3000 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3001 {
3002 	struct nfs4_exception exception = { };
3003 	int err;
3004 	do {
3005 		err = nfs4_handle_exception(server,
3006 				_nfs4_proc_statfs(server, fhandle, fsstat),
3007 				&exception);
3008 	} while (exception.retry);
3009 	return err;
3010 }
3011 
3012 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3013 		struct nfs_fsinfo *fsinfo)
3014 {
3015 	struct nfs4_fsinfo_arg args = {
3016 		.fh = fhandle,
3017 		.bitmask = server->attr_bitmask,
3018 	};
3019 	struct nfs4_fsinfo_res res = {
3020 		.fsinfo = fsinfo,
3021 	};
3022 	struct rpc_message msg = {
3023 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3024 		.rpc_argp = &args,
3025 		.rpc_resp = &res,
3026 	};
3027 
3028 	return nfs4_call_sync(server, &msg, &args, &res, 0);
3029 }
3030 
3031 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3032 {
3033 	struct nfs4_exception exception = { };
3034 	int err;
3035 
3036 	do {
3037 		err = nfs4_handle_exception(server,
3038 				_nfs4_do_fsinfo(server, fhandle, fsinfo),
3039 				&exception);
3040 	} while (exception.retry);
3041 	return err;
3042 }
3043 
3044 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3045 {
3046 	nfs_fattr_init(fsinfo->fattr);
3047 	return nfs4_do_fsinfo(server, fhandle, fsinfo);
3048 }
3049 
3050 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3051 		struct nfs_pathconf *pathconf)
3052 {
3053 	struct nfs4_pathconf_arg args = {
3054 		.fh = fhandle,
3055 		.bitmask = server->attr_bitmask,
3056 	};
3057 	struct nfs4_pathconf_res res = {
3058 		.pathconf = pathconf,
3059 	};
3060 	struct rpc_message msg = {
3061 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3062 		.rpc_argp = &args,
3063 		.rpc_resp = &res,
3064 	};
3065 
3066 	/* None of the pathconf attributes are mandatory to implement */
3067 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3068 		memset(pathconf, 0, sizeof(*pathconf));
3069 		return 0;
3070 	}
3071 
3072 	nfs_fattr_init(pathconf->fattr);
3073 	return nfs4_call_sync(server, &msg, &args, &res, 0);
3074 }
3075 
3076 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3077 		struct nfs_pathconf *pathconf)
3078 {
3079 	struct nfs4_exception exception = { };
3080 	int err;
3081 
3082 	do {
3083 		err = nfs4_handle_exception(server,
3084 				_nfs4_proc_pathconf(server, fhandle, pathconf),
3085 				&exception);
3086 	} while (exception.retry);
3087 	return err;
3088 }
3089 
3090 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3091 {
3092 	struct nfs_server *server = NFS_SERVER(data->inode);
3093 
3094 	dprintk("--> %s\n", __func__);
3095 
3096 	nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3097 
3098 	if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3099 		nfs_restart_rpc(task, server->nfs_client);
3100 		return -EAGAIN;
3101 	}
3102 
3103 	nfs_invalidate_atime(data->inode);
3104 	if (task->tk_status > 0)
3105 		renew_lease(server, data->timestamp);
3106 	return 0;
3107 }
3108 
3109 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3110 {
3111 	data->timestamp   = jiffies;
3112 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3113 }
3114 
3115 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3116 {
3117 	struct inode *inode = data->inode;
3118 
3119 	nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3120 			   task->tk_status);
3121 
3122 	if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3123 		nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3124 		return -EAGAIN;
3125 	}
3126 	if (task->tk_status >= 0) {
3127 		renew_lease(NFS_SERVER(inode), data->timestamp);
3128 		nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3129 	}
3130 	return 0;
3131 }
3132 
3133 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3134 {
3135 	struct nfs_server *server = NFS_SERVER(data->inode);
3136 
3137 	data->args.bitmask = server->cache_consistency_bitmask;
3138 	data->res.server = server;
3139 	data->timestamp   = jiffies;
3140 
3141 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3142 }
3143 
3144 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3145 {
3146 	struct inode *inode = data->inode;
3147 
3148 	nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3149 			   task->tk_status);
3150 	if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3151 		nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3152 		return -EAGAIN;
3153 	}
3154 	nfs_refresh_inode(inode, data->res.fattr);
3155 	return 0;
3156 }
3157 
3158 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3159 {
3160 	struct nfs_server *server = NFS_SERVER(data->inode);
3161 
3162 	data->args.bitmask = server->cache_consistency_bitmask;
3163 	data->res.server = server;
3164 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3165 }
3166 
3167 struct nfs4_renewdata {
3168 	struct nfs_client	*client;
3169 	unsigned long		timestamp;
3170 };
3171 
3172 /*
3173  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3174  * standalone procedure for queueing an asynchronous RENEW.
3175  */
3176 static void nfs4_renew_release(void *calldata)
3177 {
3178 	struct nfs4_renewdata *data = calldata;
3179 	struct nfs_client *clp = data->client;
3180 
3181 	if (atomic_read(&clp->cl_count) > 1)
3182 		nfs4_schedule_state_renewal(clp);
3183 	nfs_put_client(clp);
3184 	kfree(data);
3185 }
3186 
3187 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3188 {
3189 	struct nfs4_renewdata *data = calldata;
3190 	struct nfs_client *clp = data->client;
3191 	unsigned long timestamp = data->timestamp;
3192 
3193 	if (task->tk_status < 0) {
3194 		/* Unless we're shutting down, schedule state recovery! */
3195 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3196 			nfs4_schedule_state_recovery(clp);
3197 		return;
3198 	}
3199 	spin_lock(&clp->cl_lock);
3200 	if (time_before(clp->cl_last_renewal,timestamp))
3201 		clp->cl_last_renewal = timestamp;
3202 	spin_unlock(&clp->cl_lock);
3203 }
3204 
3205 static const struct rpc_call_ops nfs4_renew_ops = {
3206 	.rpc_call_done = nfs4_renew_done,
3207 	.rpc_release = nfs4_renew_release,
3208 };
3209 
3210 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3211 {
3212 	struct rpc_message msg = {
3213 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3214 		.rpc_argp	= clp,
3215 		.rpc_cred	= cred,
3216 	};
3217 	struct nfs4_renewdata *data;
3218 
3219 	if (!atomic_inc_not_zero(&clp->cl_count))
3220 		return -EIO;
3221 	data = kmalloc(sizeof(*data), GFP_KERNEL);
3222 	if (data == NULL)
3223 		return -ENOMEM;
3224 	data->client = clp;
3225 	data->timestamp = jiffies;
3226 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3227 			&nfs4_renew_ops, data);
3228 }
3229 
3230 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3231 {
3232 	struct rpc_message msg = {
3233 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3234 		.rpc_argp	= clp,
3235 		.rpc_cred	= cred,
3236 	};
3237 	unsigned long now = jiffies;
3238 	int status;
3239 
3240 	status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3241 	if (status < 0)
3242 		return status;
3243 	spin_lock(&clp->cl_lock);
3244 	if (time_before(clp->cl_last_renewal,now))
3245 		clp->cl_last_renewal = now;
3246 	spin_unlock(&clp->cl_lock);
3247 	return 0;
3248 }
3249 
3250 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3251 {
3252 	return (server->caps & NFS_CAP_ACLS)
3253 		&& (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3254 		&& (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3255 }
3256 
3257 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3258  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3259  * the stack.
3260  */
3261 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3262 
3263 static void buf_to_pages(const void *buf, size_t buflen,
3264 		struct page **pages, unsigned int *pgbase)
3265 {
3266 	const void *p = buf;
3267 
3268 	*pgbase = offset_in_page(buf);
3269 	p -= *pgbase;
3270 	while (p < buf + buflen) {
3271 		*(pages++) = virt_to_page(p);
3272 		p += PAGE_CACHE_SIZE;
3273 	}
3274 }
3275 
3276 struct nfs4_cached_acl {
3277 	int cached;
3278 	size_t len;
3279 	char data[0];
3280 };
3281 
3282 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3283 {
3284 	struct nfs_inode *nfsi = NFS_I(inode);
3285 
3286 	spin_lock(&inode->i_lock);
3287 	kfree(nfsi->nfs4_acl);
3288 	nfsi->nfs4_acl = acl;
3289 	spin_unlock(&inode->i_lock);
3290 }
3291 
3292 static void nfs4_zap_acl_attr(struct inode *inode)
3293 {
3294 	nfs4_set_cached_acl(inode, NULL);
3295 }
3296 
3297 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3298 {
3299 	struct nfs_inode *nfsi = NFS_I(inode);
3300 	struct nfs4_cached_acl *acl;
3301 	int ret = -ENOENT;
3302 
3303 	spin_lock(&inode->i_lock);
3304 	acl = nfsi->nfs4_acl;
3305 	if (acl == NULL)
3306 		goto out;
3307 	if (buf == NULL) /* user is just asking for length */
3308 		goto out_len;
3309 	if (acl->cached == 0)
3310 		goto out;
3311 	ret = -ERANGE; /* see getxattr(2) man page */
3312 	if (acl->len > buflen)
3313 		goto out;
3314 	memcpy(buf, acl->data, acl->len);
3315 out_len:
3316 	ret = acl->len;
3317 out:
3318 	spin_unlock(&inode->i_lock);
3319 	return ret;
3320 }
3321 
3322 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3323 {
3324 	struct nfs4_cached_acl *acl;
3325 
3326 	if (buf && acl_len <= PAGE_SIZE) {
3327 		acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3328 		if (acl == NULL)
3329 			goto out;
3330 		acl->cached = 1;
3331 		memcpy(acl->data, buf, acl_len);
3332 	} else {
3333 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3334 		if (acl == NULL)
3335 			goto out;
3336 		acl->cached = 0;
3337 	}
3338 	acl->len = acl_len;
3339 out:
3340 	nfs4_set_cached_acl(inode, acl);
3341 }
3342 
3343 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3344 {
3345 	struct page *pages[NFS4ACL_MAXPAGES];
3346 	struct nfs_getaclargs args = {
3347 		.fh = NFS_FH(inode),
3348 		.acl_pages = pages,
3349 		.acl_len = buflen,
3350 	};
3351 	struct nfs_getaclres res = {
3352 		.acl_len = buflen,
3353 	};
3354 	void *resp_buf;
3355 	struct rpc_message msg = {
3356 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3357 		.rpc_argp = &args,
3358 		.rpc_resp = &res,
3359 	};
3360 	struct page *localpage = NULL;
3361 	int ret;
3362 
3363 	if (buflen < PAGE_SIZE) {
3364 		/* As long as we're doing a round trip to the server anyway,
3365 		 * let's be prepared for a page of acl data. */
3366 		localpage = alloc_page(GFP_KERNEL);
3367 		resp_buf = page_address(localpage);
3368 		if (localpage == NULL)
3369 			return -ENOMEM;
3370 		args.acl_pages[0] = localpage;
3371 		args.acl_pgbase = 0;
3372 		args.acl_len = PAGE_SIZE;
3373 	} else {
3374 		resp_buf = buf;
3375 		buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3376 	}
3377 	ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3378 	if (ret)
3379 		goto out_free;
3380 	if (res.acl_len > args.acl_len)
3381 		nfs4_write_cached_acl(inode, NULL, res.acl_len);
3382 	else
3383 		nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3384 	if (buf) {
3385 		ret = -ERANGE;
3386 		if (res.acl_len > buflen)
3387 			goto out_free;
3388 		if (localpage)
3389 			memcpy(buf, resp_buf, res.acl_len);
3390 	}
3391 	ret = res.acl_len;
3392 out_free:
3393 	if (localpage)
3394 		__free_page(localpage);
3395 	return ret;
3396 }
3397 
3398 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3399 {
3400 	struct nfs4_exception exception = { };
3401 	ssize_t ret;
3402 	do {
3403 		ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3404 		if (ret >= 0)
3405 			break;
3406 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3407 	} while (exception.retry);
3408 	return ret;
3409 }
3410 
3411 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3412 {
3413 	struct nfs_server *server = NFS_SERVER(inode);
3414 	int ret;
3415 
3416 	if (!nfs4_server_supports_acls(server))
3417 		return -EOPNOTSUPP;
3418 	ret = nfs_revalidate_inode(server, inode);
3419 	if (ret < 0)
3420 		return ret;
3421 	ret = nfs4_read_cached_acl(inode, buf, buflen);
3422 	if (ret != -ENOENT)
3423 		return ret;
3424 	return nfs4_get_acl_uncached(inode, buf, buflen);
3425 }
3426 
3427 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3428 {
3429 	struct nfs_server *server = NFS_SERVER(inode);
3430 	struct page *pages[NFS4ACL_MAXPAGES];
3431 	struct nfs_setaclargs arg = {
3432 		.fh		= NFS_FH(inode),
3433 		.acl_pages	= pages,
3434 		.acl_len	= buflen,
3435 	};
3436 	struct nfs_setaclres res;
3437 	struct rpc_message msg = {
3438 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3439 		.rpc_argp	= &arg,
3440 		.rpc_resp	= &res,
3441 	};
3442 	int ret;
3443 
3444 	if (!nfs4_server_supports_acls(server))
3445 		return -EOPNOTSUPP;
3446 	nfs_inode_return_delegation(inode);
3447 	buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3448 	ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3449 	nfs_access_zap_cache(inode);
3450 	nfs_zap_acl_cache(inode);
3451 	return ret;
3452 }
3453 
3454 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3455 {
3456 	struct nfs4_exception exception = { };
3457 	int err;
3458 	do {
3459 		err = nfs4_handle_exception(NFS_SERVER(inode),
3460 				__nfs4_proc_set_acl(inode, buf, buflen),
3461 				&exception);
3462 	} while (exception.retry);
3463 	return err;
3464 }
3465 
3466 static int
3467 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3468 {
3469 	if (!clp || task->tk_status >= 0)
3470 		return 0;
3471 	switch(task->tk_status) {
3472 		case -NFS4ERR_ADMIN_REVOKED:
3473 		case -NFS4ERR_BAD_STATEID:
3474 		case -NFS4ERR_OPENMODE:
3475 			if (state == NULL)
3476 				break;
3477 			nfs4_state_mark_reclaim_nograce(clp, state);
3478 			goto do_state_recovery;
3479 		case -NFS4ERR_STALE_STATEID:
3480 			if (state == NULL)
3481 				break;
3482 			nfs4_state_mark_reclaim_reboot(clp, state);
3483 		case -NFS4ERR_STALE_CLIENTID:
3484 		case -NFS4ERR_EXPIRED:
3485 			goto do_state_recovery;
3486 #if defined(CONFIG_NFS_V4_1)
3487 		case -NFS4ERR_BADSESSION:
3488 		case -NFS4ERR_BADSLOT:
3489 		case -NFS4ERR_BAD_HIGH_SLOT:
3490 		case -NFS4ERR_DEADSESSION:
3491 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3492 		case -NFS4ERR_SEQ_FALSE_RETRY:
3493 		case -NFS4ERR_SEQ_MISORDERED:
3494 			dprintk("%s ERROR %d, Reset session\n", __func__,
3495 				task->tk_status);
3496 			nfs4_schedule_state_recovery(clp);
3497 			task->tk_status = 0;
3498 			return -EAGAIN;
3499 #endif /* CONFIG_NFS_V4_1 */
3500 		case -NFS4ERR_DELAY:
3501 			if (server)
3502 				nfs_inc_server_stats(server, NFSIOS_DELAY);
3503 		case -NFS4ERR_GRACE:
3504 		case -EKEYEXPIRED:
3505 			rpc_delay(task, NFS4_POLL_RETRY_MAX);
3506 			task->tk_status = 0;
3507 			return -EAGAIN;
3508 		case -NFS4ERR_OLD_STATEID:
3509 			task->tk_status = 0;
3510 			return -EAGAIN;
3511 	}
3512 	task->tk_status = nfs4_map_errors(task->tk_status);
3513 	return 0;
3514 do_state_recovery:
3515 	rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3516 	nfs4_schedule_state_recovery(clp);
3517 	if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3518 		rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3519 	task->tk_status = 0;
3520 	return -EAGAIN;
3521 }
3522 
3523 static int
3524 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3525 {
3526 	return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3527 }
3528 
3529 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3530 		unsigned short port, struct rpc_cred *cred,
3531 		struct nfs4_setclientid_res *res)
3532 {
3533 	nfs4_verifier sc_verifier;
3534 	struct nfs4_setclientid setclientid = {
3535 		.sc_verifier = &sc_verifier,
3536 		.sc_prog = program,
3537 	};
3538 	struct rpc_message msg = {
3539 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3540 		.rpc_argp = &setclientid,
3541 		.rpc_resp = res,
3542 		.rpc_cred = cred,
3543 	};
3544 	__be32 *p;
3545 	int loop = 0;
3546 	int status;
3547 
3548 	p = (__be32*)sc_verifier.data;
3549 	*p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3550 	*p = htonl((u32)clp->cl_boot_time.tv_nsec);
3551 
3552 	for(;;) {
3553 		setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3554 				sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3555 				clp->cl_ipaddr,
3556 				rpc_peeraddr2str(clp->cl_rpcclient,
3557 							RPC_DISPLAY_ADDR),
3558 				rpc_peeraddr2str(clp->cl_rpcclient,
3559 							RPC_DISPLAY_PROTO),
3560 				clp->cl_rpcclient->cl_auth->au_ops->au_name,
3561 				clp->cl_id_uniquifier);
3562 		setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3563 				sizeof(setclientid.sc_netid),
3564 				rpc_peeraddr2str(clp->cl_rpcclient,
3565 							RPC_DISPLAY_NETID));
3566 		setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3567 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3568 				clp->cl_ipaddr, port >> 8, port & 255);
3569 
3570 		status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3571 		if (status != -NFS4ERR_CLID_INUSE)
3572 			break;
3573 		if (signalled())
3574 			break;
3575 		if (loop++ & 1)
3576 			ssleep(clp->cl_lease_time + 1);
3577 		else
3578 			if (++clp->cl_id_uniquifier == 0)
3579 				break;
3580 	}
3581 	return status;
3582 }
3583 
3584 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3585 		struct nfs4_setclientid_res *arg,
3586 		struct rpc_cred *cred)
3587 {
3588 	struct nfs_fsinfo fsinfo;
3589 	struct rpc_message msg = {
3590 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3591 		.rpc_argp = arg,
3592 		.rpc_resp = &fsinfo,
3593 		.rpc_cred = cred,
3594 	};
3595 	unsigned long now;
3596 	int status;
3597 
3598 	now = jiffies;
3599 	status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3600 	if (status == 0) {
3601 		spin_lock(&clp->cl_lock);
3602 		clp->cl_lease_time = fsinfo.lease_time * HZ;
3603 		clp->cl_last_renewal = now;
3604 		spin_unlock(&clp->cl_lock);
3605 	}
3606 	return status;
3607 }
3608 
3609 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3610 		struct nfs4_setclientid_res *arg,
3611 		struct rpc_cred *cred)
3612 {
3613 	long timeout = 0;
3614 	int err;
3615 	do {
3616 		err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3617 		switch (err) {
3618 			case 0:
3619 				return err;
3620 			case -NFS4ERR_RESOURCE:
3621 				/* The IBM lawyers misread another document! */
3622 			case -NFS4ERR_DELAY:
3623 			case -EKEYEXPIRED:
3624 				err = nfs4_delay(clp->cl_rpcclient, &timeout);
3625 		}
3626 	} while (err == 0);
3627 	return err;
3628 }
3629 
3630 struct nfs4_delegreturndata {
3631 	struct nfs4_delegreturnargs args;
3632 	struct nfs4_delegreturnres res;
3633 	struct nfs_fh fh;
3634 	nfs4_stateid stateid;
3635 	unsigned long timestamp;
3636 	struct nfs_fattr fattr;
3637 	int rpc_status;
3638 };
3639 
3640 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3641 {
3642 	struct nfs4_delegreturndata *data = calldata;
3643 
3644 	nfs4_sequence_done(data->res.server, &data->res.seq_res,
3645 			task->tk_status);
3646 
3647 	switch (task->tk_status) {
3648 	case -NFS4ERR_STALE_STATEID:
3649 	case -NFS4ERR_EXPIRED:
3650 	case 0:
3651 		renew_lease(data->res.server, data->timestamp);
3652 		break;
3653 	default:
3654 		if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3655 				-EAGAIN) {
3656 			nfs_restart_rpc(task, data->res.server->nfs_client);
3657 			return;
3658 		}
3659 	}
3660 	data->rpc_status = task->tk_status;
3661 }
3662 
3663 static void nfs4_delegreturn_release(void *calldata)
3664 {
3665 	kfree(calldata);
3666 }
3667 
3668 #if defined(CONFIG_NFS_V4_1)
3669 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3670 {
3671 	struct nfs4_delegreturndata *d_data;
3672 
3673 	d_data = (struct nfs4_delegreturndata *)data;
3674 
3675 	if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3676 				&d_data->args.seq_args,
3677 				&d_data->res.seq_res, 1, task))
3678 		return;
3679 	rpc_call_start(task);
3680 }
3681 #endif /* CONFIG_NFS_V4_1 */
3682 
3683 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3684 #if defined(CONFIG_NFS_V4_1)
3685 	.rpc_call_prepare = nfs4_delegreturn_prepare,
3686 #endif /* CONFIG_NFS_V4_1 */
3687 	.rpc_call_done = nfs4_delegreturn_done,
3688 	.rpc_release = nfs4_delegreturn_release,
3689 };
3690 
3691 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3692 {
3693 	struct nfs4_delegreturndata *data;
3694 	struct nfs_server *server = NFS_SERVER(inode);
3695 	struct rpc_task *task;
3696 	struct rpc_message msg = {
3697 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3698 		.rpc_cred = cred,
3699 	};
3700 	struct rpc_task_setup task_setup_data = {
3701 		.rpc_client = server->client,
3702 		.rpc_message = &msg,
3703 		.callback_ops = &nfs4_delegreturn_ops,
3704 		.flags = RPC_TASK_ASYNC,
3705 	};
3706 	int status = 0;
3707 
3708 	data = kzalloc(sizeof(*data), GFP_NOFS);
3709 	if (data == NULL)
3710 		return -ENOMEM;
3711 	data->args.fhandle = &data->fh;
3712 	data->args.stateid = &data->stateid;
3713 	data->args.bitmask = server->attr_bitmask;
3714 	nfs_copy_fh(&data->fh, NFS_FH(inode));
3715 	memcpy(&data->stateid, stateid, sizeof(data->stateid));
3716 	data->res.fattr = &data->fattr;
3717 	data->res.server = server;
3718 	data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3719 	nfs_fattr_init(data->res.fattr);
3720 	data->timestamp = jiffies;
3721 	data->rpc_status = 0;
3722 
3723 	task_setup_data.callback_data = data;
3724 	msg.rpc_argp = &data->args,
3725 	msg.rpc_resp = &data->res,
3726 	task = rpc_run_task(&task_setup_data);
3727 	if (IS_ERR(task))
3728 		return PTR_ERR(task);
3729 	if (!issync)
3730 		goto out;
3731 	status = nfs4_wait_for_completion_rpc_task(task);
3732 	if (status != 0)
3733 		goto out;
3734 	status = data->rpc_status;
3735 	if (status != 0)
3736 		goto out;
3737 	nfs_refresh_inode(inode, &data->fattr);
3738 out:
3739 	rpc_put_task(task);
3740 	return status;
3741 }
3742 
3743 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3744 {
3745 	struct nfs_server *server = NFS_SERVER(inode);
3746 	struct nfs4_exception exception = { };
3747 	int err;
3748 	do {
3749 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3750 		switch (err) {
3751 			case -NFS4ERR_STALE_STATEID:
3752 			case -NFS4ERR_EXPIRED:
3753 			case 0:
3754 				return 0;
3755 		}
3756 		err = nfs4_handle_exception(server, err, &exception);
3757 	} while (exception.retry);
3758 	return err;
3759 }
3760 
3761 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3762 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3763 
3764 /*
3765  * sleep, with exponential backoff, and retry the LOCK operation.
3766  */
3767 static unsigned long
3768 nfs4_set_lock_task_retry(unsigned long timeout)
3769 {
3770 	schedule_timeout_killable(timeout);
3771 	timeout <<= 1;
3772 	if (timeout > NFS4_LOCK_MAXTIMEOUT)
3773 		return NFS4_LOCK_MAXTIMEOUT;
3774 	return timeout;
3775 }
3776 
3777 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3778 {
3779 	struct inode *inode = state->inode;
3780 	struct nfs_server *server = NFS_SERVER(inode);
3781 	struct nfs_client *clp = server->nfs_client;
3782 	struct nfs_lockt_args arg = {
3783 		.fh = NFS_FH(inode),
3784 		.fl = request,
3785 	};
3786 	struct nfs_lockt_res res = {
3787 		.denied = request,
3788 	};
3789 	struct rpc_message msg = {
3790 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3791 		.rpc_argp       = &arg,
3792 		.rpc_resp       = &res,
3793 		.rpc_cred	= state->owner->so_cred,
3794 	};
3795 	struct nfs4_lock_state *lsp;
3796 	int status;
3797 
3798 	arg.lock_owner.clientid = clp->cl_clientid;
3799 	status = nfs4_set_lock_state(state, request);
3800 	if (status != 0)
3801 		goto out;
3802 	lsp = request->fl_u.nfs4_fl.owner;
3803 	arg.lock_owner.id = lsp->ls_id.id;
3804 	status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3805 	switch (status) {
3806 		case 0:
3807 			request->fl_type = F_UNLCK;
3808 			break;
3809 		case -NFS4ERR_DENIED:
3810 			status = 0;
3811 	}
3812 	request->fl_ops->fl_release_private(request);
3813 out:
3814 	return status;
3815 }
3816 
3817 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3818 {
3819 	struct nfs4_exception exception = { };
3820 	int err;
3821 
3822 	do {
3823 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
3824 				_nfs4_proc_getlk(state, cmd, request),
3825 				&exception);
3826 	} while (exception.retry);
3827 	return err;
3828 }
3829 
3830 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3831 {
3832 	int res = 0;
3833 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3834 		case FL_POSIX:
3835 			res = posix_lock_file_wait(file, fl);
3836 			break;
3837 		case FL_FLOCK:
3838 			res = flock_lock_file_wait(file, fl);
3839 			break;
3840 		default:
3841 			BUG();
3842 	}
3843 	return res;
3844 }
3845 
3846 struct nfs4_unlockdata {
3847 	struct nfs_locku_args arg;
3848 	struct nfs_locku_res res;
3849 	struct nfs4_lock_state *lsp;
3850 	struct nfs_open_context *ctx;
3851 	struct file_lock fl;
3852 	const struct nfs_server *server;
3853 	unsigned long timestamp;
3854 };
3855 
3856 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3857 		struct nfs_open_context *ctx,
3858 		struct nfs4_lock_state *lsp,
3859 		struct nfs_seqid *seqid)
3860 {
3861 	struct nfs4_unlockdata *p;
3862 	struct inode *inode = lsp->ls_state->inode;
3863 
3864 	p = kzalloc(sizeof(*p), GFP_NOFS);
3865 	if (p == NULL)
3866 		return NULL;
3867 	p->arg.fh = NFS_FH(inode);
3868 	p->arg.fl = &p->fl;
3869 	p->arg.seqid = seqid;
3870 	p->res.seqid = seqid;
3871 	p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3872 	p->arg.stateid = &lsp->ls_stateid;
3873 	p->lsp = lsp;
3874 	atomic_inc(&lsp->ls_count);
3875 	/* Ensure we don't close file until we're done freeing locks! */
3876 	p->ctx = get_nfs_open_context(ctx);
3877 	memcpy(&p->fl, fl, sizeof(p->fl));
3878 	p->server = NFS_SERVER(inode);
3879 	return p;
3880 }
3881 
3882 static void nfs4_locku_release_calldata(void *data)
3883 {
3884 	struct nfs4_unlockdata *calldata = data;
3885 	nfs_free_seqid(calldata->arg.seqid);
3886 	nfs4_put_lock_state(calldata->lsp);
3887 	put_nfs_open_context(calldata->ctx);
3888 	kfree(calldata);
3889 }
3890 
3891 static void nfs4_locku_done(struct rpc_task *task, void *data)
3892 {
3893 	struct nfs4_unlockdata *calldata = data;
3894 
3895 	nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3896 			   task->tk_status);
3897 	if (RPC_ASSASSINATED(task))
3898 		return;
3899 	switch (task->tk_status) {
3900 		case 0:
3901 			memcpy(calldata->lsp->ls_stateid.data,
3902 					calldata->res.stateid.data,
3903 					sizeof(calldata->lsp->ls_stateid.data));
3904 			renew_lease(calldata->server, calldata->timestamp);
3905 			break;
3906 		case -NFS4ERR_BAD_STATEID:
3907 		case -NFS4ERR_OLD_STATEID:
3908 		case -NFS4ERR_STALE_STATEID:
3909 		case -NFS4ERR_EXPIRED:
3910 			break;
3911 		default:
3912 			if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3913 				nfs_restart_rpc(task,
3914 						 calldata->server->nfs_client);
3915 	}
3916 }
3917 
3918 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3919 {
3920 	struct nfs4_unlockdata *calldata = data;
3921 
3922 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3923 		return;
3924 	if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3925 		/* Note: exit _without_ running nfs4_locku_done */
3926 		task->tk_action = NULL;
3927 		return;
3928 	}
3929 	calldata->timestamp = jiffies;
3930 	if (nfs4_setup_sequence(calldata->server->nfs_client,
3931 				&calldata->arg.seq_args,
3932 				&calldata->res.seq_res, 1, task))
3933 		return;
3934 	rpc_call_start(task);
3935 }
3936 
3937 static const struct rpc_call_ops nfs4_locku_ops = {
3938 	.rpc_call_prepare = nfs4_locku_prepare,
3939 	.rpc_call_done = nfs4_locku_done,
3940 	.rpc_release = nfs4_locku_release_calldata,
3941 };
3942 
3943 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3944 		struct nfs_open_context *ctx,
3945 		struct nfs4_lock_state *lsp,
3946 		struct nfs_seqid *seqid)
3947 {
3948 	struct nfs4_unlockdata *data;
3949 	struct rpc_message msg = {
3950 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3951 		.rpc_cred = ctx->cred,
3952 	};
3953 	struct rpc_task_setup task_setup_data = {
3954 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3955 		.rpc_message = &msg,
3956 		.callback_ops = &nfs4_locku_ops,
3957 		.workqueue = nfsiod_workqueue,
3958 		.flags = RPC_TASK_ASYNC,
3959 	};
3960 
3961 	/* Ensure this is an unlock - when canceling a lock, the
3962 	 * canceled lock is passed in, and it won't be an unlock.
3963 	 */
3964 	fl->fl_type = F_UNLCK;
3965 
3966 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3967 	if (data == NULL) {
3968 		nfs_free_seqid(seqid);
3969 		return ERR_PTR(-ENOMEM);
3970 	}
3971 
3972 	msg.rpc_argp = &data->arg,
3973 	msg.rpc_resp = &data->res,
3974 	task_setup_data.callback_data = data;
3975 	return rpc_run_task(&task_setup_data);
3976 }
3977 
3978 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3979 {
3980 	struct nfs_inode *nfsi = NFS_I(state->inode);
3981 	struct nfs_seqid *seqid;
3982 	struct nfs4_lock_state *lsp;
3983 	struct rpc_task *task;
3984 	int status = 0;
3985 	unsigned char fl_flags = request->fl_flags;
3986 
3987 	status = nfs4_set_lock_state(state, request);
3988 	/* Unlock _before_ we do the RPC call */
3989 	request->fl_flags |= FL_EXISTS;
3990 	down_read(&nfsi->rwsem);
3991 	if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3992 		up_read(&nfsi->rwsem);
3993 		goto out;
3994 	}
3995 	up_read(&nfsi->rwsem);
3996 	if (status != 0)
3997 		goto out;
3998 	/* Is this a delegated lock? */
3999 	if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4000 		goto out;
4001 	lsp = request->fl_u.nfs4_fl.owner;
4002 	seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4003 	status = -ENOMEM;
4004 	if (seqid == NULL)
4005 		goto out;
4006 	task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4007 	status = PTR_ERR(task);
4008 	if (IS_ERR(task))
4009 		goto out;
4010 	status = nfs4_wait_for_completion_rpc_task(task);
4011 	rpc_put_task(task);
4012 out:
4013 	request->fl_flags = fl_flags;
4014 	return status;
4015 }
4016 
4017 struct nfs4_lockdata {
4018 	struct nfs_lock_args arg;
4019 	struct nfs_lock_res res;
4020 	struct nfs4_lock_state *lsp;
4021 	struct nfs_open_context *ctx;
4022 	struct file_lock fl;
4023 	unsigned long timestamp;
4024 	int rpc_status;
4025 	int cancelled;
4026 	struct nfs_server *server;
4027 };
4028 
4029 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4030 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4031 		gfp_t gfp_mask)
4032 {
4033 	struct nfs4_lockdata *p;
4034 	struct inode *inode = lsp->ls_state->inode;
4035 	struct nfs_server *server = NFS_SERVER(inode);
4036 
4037 	p = kzalloc(sizeof(*p), gfp_mask);
4038 	if (p == NULL)
4039 		return NULL;
4040 
4041 	p->arg.fh = NFS_FH(inode);
4042 	p->arg.fl = &p->fl;
4043 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4044 	if (p->arg.open_seqid == NULL)
4045 		goto out_free;
4046 	p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4047 	if (p->arg.lock_seqid == NULL)
4048 		goto out_free_seqid;
4049 	p->arg.lock_stateid = &lsp->ls_stateid;
4050 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4051 	p->arg.lock_owner.id = lsp->ls_id.id;
4052 	p->res.lock_seqid = p->arg.lock_seqid;
4053 	p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4054 	p->lsp = lsp;
4055 	p->server = server;
4056 	atomic_inc(&lsp->ls_count);
4057 	p->ctx = get_nfs_open_context(ctx);
4058 	memcpy(&p->fl, fl, sizeof(p->fl));
4059 	return p;
4060 out_free_seqid:
4061 	nfs_free_seqid(p->arg.open_seqid);
4062 out_free:
4063 	kfree(p);
4064 	return NULL;
4065 }
4066 
4067 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4068 {
4069 	struct nfs4_lockdata *data = calldata;
4070 	struct nfs4_state *state = data->lsp->ls_state;
4071 
4072 	dprintk("%s: begin!\n", __func__);
4073 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4074 		return;
4075 	/* Do we need to do an open_to_lock_owner? */
4076 	if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4077 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4078 			return;
4079 		data->arg.open_stateid = &state->stateid;
4080 		data->arg.new_lock_owner = 1;
4081 		data->res.open_seqid = data->arg.open_seqid;
4082 	} else
4083 		data->arg.new_lock_owner = 0;
4084 	data->timestamp = jiffies;
4085 	if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4086 				&data->res.seq_res, 1, task))
4087 		return;
4088 	rpc_call_start(task);
4089 	dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4090 }
4091 
4092 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4093 {
4094 	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4095 	nfs4_lock_prepare(task, calldata);
4096 }
4097 
4098 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4099 {
4100 	struct nfs4_lockdata *data = calldata;
4101 
4102 	dprintk("%s: begin!\n", __func__);
4103 
4104 	nfs4_sequence_done(data->server, &data->res.seq_res,
4105 			task->tk_status);
4106 
4107 	data->rpc_status = task->tk_status;
4108 	if (RPC_ASSASSINATED(task))
4109 		goto out;
4110 	if (data->arg.new_lock_owner != 0) {
4111 		if (data->rpc_status == 0)
4112 			nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4113 		else
4114 			goto out;
4115 	}
4116 	if (data->rpc_status == 0) {
4117 		memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4118 					sizeof(data->lsp->ls_stateid.data));
4119 		data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4120 		renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4121 	}
4122 out:
4123 	dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4124 }
4125 
4126 static void nfs4_lock_release(void *calldata)
4127 {
4128 	struct nfs4_lockdata *data = calldata;
4129 
4130 	dprintk("%s: begin!\n", __func__);
4131 	nfs_free_seqid(data->arg.open_seqid);
4132 	if (data->cancelled != 0) {
4133 		struct rpc_task *task;
4134 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4135 				data->arg.lock_seqid);
4136 		if (!IS_ERR(task))
4137 			rpc_put_task(task);
4138 		dprintk("%s: cancelling lock!\n", __func__);
4139 	} else
4140 		nfs_free_seqid(data->arg.lock_seqid);
4141 	nfs4_put_lock_state(data->lsp);
4142 	put_nfs_open_context(data->ctx);
4143 	kfree(data);
4144 	dprintk("%s: done!\n", __func__);
4145 }
4146 
4147 static const struct rpc_call_ops nfs4_lock_ops = {
4148 	.rpc_call_prepare = nfs4_lock_prepare,
4149 	.rpc_call_done = nfs4_lock_done,
4150 	.rpc_release = nfs4_lock_release,
4151 };
4152 
4153 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4154 	.rpc_call_prepare = nfs4_recover_lock_prepare,
4155 	.rpc_call_done = nfs4_lock_done,
4156 	.rpc_release = nfs4_lock_release,
4157 };
4158 
4159 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4160 {
4161 	struct nfs_client *clp = server->nfs_client;
4162 	struct nfs4_state *state = lsp->ls_state;
4163 
4164 	switch (error) {
4165 	case -NFS4ERR_ADMIN_REVOKED:
4166 	case -NFS4ERR_BAD_STATEID:
4167 	case -NFS4ERR_EXPIRED:
4168 		if (new_lock_owner != 0 ||
4169 		   (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4170 			nfs4_state_mark_reclaim_nograce(clp, state);
4171 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4172 		break;
4173 	case -NFS4ERR_STALE_STATEID:
4174 		if (new_lock_owner != 0 ||
4175 		    (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4176 			nfs4_state_mark_reclaim_reboot(clp, state);
4177 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4178 	};
4179 }
4180 
4181 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4182 {
4183 	struct nfs4_lockdata *data;
4184 	struct rpc_task *task;
4185 	struct rpc_message msg = {
4186 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4187 		.rpc_cred = state->owner->so_cred,
4188 	};
4189 	struct rpc_task_setup task_setup_data = {
4190 		.rpc_client = NFS_CLIENT(state->inode),
4191 		.rpc_message = &msg,
4192 		.callback_ops = &nfs4_lock_ops,
4193 		.workqueue = nfsiod_workqueue,
4194 		.flags = RPC_TASK_ASYNC,
4195 	};
4196 	int ret;
4197 
4198 	dprintk("%s: begin!\n", __func__);
4199 	data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4200 			fl->fl_u.nfs4_fl.owner,
4201 			recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4202 	if (data == NULL)
4203 		return -ENOMEM;
4204 	if (IS_SETLKW(cmd))
4205 		data->arg.block = 1;
4206 	if (recovery_type > NFS_LOCK_NEW) {
4207 		if (recovery_type == NFS_LOCK_RECLAIM)
4208 			data->arg.reclaim = NFS_LOCK_RECLAIM;
4209 		task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4210 	}
4211 	msg.rpc_argp = &data->arg,
4212 	msg.rpc_resp = &data->res,
4213 	task_setup_data.callback_data = data;
4214 	task = rpc_run_task(&task_setup_data);
4215 	if (IS_ERR(task))
4216 		return PTR_ERR(task);
4217 	ret = nfs4_wait_for_completion_rpc_task(task);
4218 	if (ret == 0) {
4219 		ret = data->rpc_status;
4220 		if (ret)
4221 			nfs4_handle_setlk_error(data->server, data->lsp,
4222 					data->arg.new_lock_owner, ret);
4223 	} else
4224 		data->cancelled = 1;
4225 	rpc_put_task(task);
4226 	dprintk("%s: done, ret = %d!\n", __func__, ret);
4227 	return ret;
4228 }
4229 
4230 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4231 {
4232 	struct nfs_server *server = NFS_SERVER(state->inode);
4233 	struct nfs4_exception exception = { };
4234 	int err;
4235 
4236 	do {
4237 		/* Cache the lock if possible... */
4238 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4239 			return 0;
4240 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4241 		if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
4242 			break;
4243 		nfs4_handle_exception(server, err, &exception);
4244 	} while (exception.retry);
4245 	return err;
4246 }
4247 
4248 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4249 {
4250 	struct nfs_server *server = NFS_SERVER(state->inode);
4251 	struct nfs4_exception exception = { };
4252 	int err;
4253 
4254 	err = nfs4_set_lock_state(state, request);
4255 	if (err != 0)
4256 		return err;
4257 	do {
4258 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4259 			return 0;
4260 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4261 		switch (err) {
4262 		default:
4263 			goto out;
4264 		case -NFS4ERR_GRACE:
4265 		case -NFS4ERR_DELAY:
4266 		case -EKEYEXPIRED:
4267 			nfs4_handle_exception(server, err, &exception);
4268 			err = 0;
4269 		}
4270 	} while (exception.retry);
4271 out:
4272 	return err;
4273 }
4274 
4275 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4276 {
4277 	struct nfs_inode *nfsi = NFS_I(state->inode);
4278 	unsigned char fl_flags = request->fl_flags;
4279 	int status = -ENOLCK;
4280 
4281 	if ((fl_flags & FL_POSIX) &&
4282 			!test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4283 		goto out;
4284 	/* Is this a delegated open? */
4285 	status = nfs4_set_lock_state(state, request);
4286 	if (status != 0)
4287 		goto out;
4288 	request->fl_flags |= FL_ACCESS;
4289 	status = do_vfs_lock(request->fl_file, request);
4290 	if (status < 0)
4291 		goto out;
4292 	down_read(&nfsi->rwsem);
4293 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4294 		/* Yes: cache locks! */
4295 		/* ...but avoid races with delegation recall... */
4296 		request->fl_flags = fl_flags & ~FL_SLEEP;
4297 		status = do_vfs_lock(request->fl_file, request);
4298 		goto out_unlock;
4299 	}
4300 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4301 	if (status != 0)
4302 		goto out_unlock;
4303 	/* Note: we always want to sleep here! */
4304 	request->fl_flags = fl_flags | FL_SLEEP;
4305 	if (do_vfs_lock(request->fl_file, request) < 0)
4306 		printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4307 out_unlock:
4308 	up_read(&nfsi->rwsem);
4309 out:
4310 	request->fl_flags = fl_flags;
4311 	return status;
4312 }
4313 
4314 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4315 {
4316 	struct nfs4_exception exception = { };
4317 	int err;
4318 
4319 	do {
4320 		err = _nfs4_proc_setlk(state, cmd, request);
4321 		if (err == -NFS4ERR_DENIED)
4322 			err = -EAGAIN;
4323 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
4324 				err, &exception);
4325 	} while (exception.retry);
4326 	return err;
4327 }
4328 
4329 static int
4330 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4331 {
4332 	struct nfs_open_context *ctx;
4333 	struct nfs4_state *state;
4334 	unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4335 	int status;
4336 
4337 	/* verify open state */
4338 	ctx = nfs_file_open_context(filp);
4339 	state = ctx->state;
4340 
4341 	if (request->fl_start < 0 || request->fl_end < 0)
4342 		return -EINVAL;
4343 
4344 	if (IS_GETLK(cmd)) {
4345 		if (state != NULL)
4346 			return nfs4_proc_getlk(state, F_GETLK, request);
4347 		return 0;
4348 	}
4349 
4350 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4351 		return -EINVAL;
4352 
4353 	if (request->fl_type == F_UNLCK) {
4354 		if (state != NULL)
4355 			return nfs4_proc_unlck(state, cmd, request);
4356 		return 0;
4357 	}
4358 
4359 	if (state == NULL)
4360 		return -ENOLCK;
4361 	do {
4362 		status = nfs4_proc_setlk(state, cmd, request);
4363 		if ((status != -EAGAIN) || IS_SETLK(cmd))
4364 			break;
4365 		timeout = nfs4_set_lock_task_retry(timeout);
4366 		status = -ERESTARTSYS;
4367 		if (signalled())
4368 			break;
4369 	} while(status < 0);
4370 	return status;
4371 }
4372 
4373 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4374 {
4375 	struct nfs_server *server = NFS_SERVER(state->inode);
4376 	struct nfs4_exception exception = { };
4377 	int err;
4378 
4379 	err = nfs4_set_lock_state(state, fl);
4380 	if (err != 0)
4381 		goto out;
4382 	do {
4383 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4384 		switch (err) {
4385 			default:
4386 				printk(KERN_ERR "%s: unhandled error %d.\n",
4387 						__func__, err);
4388 			case 0:
4389 			case -ESTALE:
4390 				goto out;
4391 			case -NFS4ERR_EXPIRED:
4392 			case -NFS4ERR_STALE_CLIENTID:
4393 			case -NFS4ERR_STALE_STATEID:
4394 			case -NFS4ERR_BADSESSION:
4395 			case -NFS4ERR_BADSLOT:
4396 			case -NFS4ERR_BAD_HIGH_SLOT:
4397 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4398 			case -NFS4ERR_DEADSESSION:
4399 				nfs4_schedule_state_recovery(server->nfs_client);
4400 				goto out;
4401 			case -ERESTARTSYS:
4402 				/*
4403 				 * The show must go on: exit, but mark the
4404 				 * stateid as needing recovery.
4405 				 */
4406 			case -NFS4ERR_ADMIN_REVOKED:
4407 			case -NFS4ERR_BAD_STATEID:
4408 			case -NFS4ERR_OPENMODE:
4409 				nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4410 				err = 0;
4411 				goto out;
4412 			case -ENOMEM:
4413 			case -NFS4ERR_DENIED:
4414 				/* kill_proc(fl->fl_pid, SIGLOST, 1); */
4415 				err = 0;
4416 				goto out;
4417 			case -NFS4ERR_DELAY:
4418 			case -EKEYEXPIRED:
4419 				break;
4420 		}
4421 		err = nfs4_handle_exception(server, err, &exception);
4422 	} while (exception.retry);
4423 out:
4424 	return err;
4425 }
4426 
4427 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4428 
4429 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4430 		size_t buflen, int flags)
4431 {
4432 	struct inode *inode = dentry->d_inode;
4433 
4434 	if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4435 		return -EOPNOTSUPP;
4436 
4437 	return nfs4_proc_set_acl(inode, buf, buflen);
4438 }
4439 
4440 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4441  * and that's what we'll do for e.g. user attributes that haven't been set.
4442  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4443  * attributes in kernel-managed attribute namespaces. */
4444 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4445 		size_t buflen)
4446 {
4447 	struct inode *inode = dentry->d_inode;
4448 
4449 	if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4450 		return -EOPNOTSUPP;
4451 
4452 	return nfs4_proc_get_acl(inode, buf, buflen);
4453 }
4454 
4455 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4456 {
4457 	size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4458 
4459 	if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4460 		return 0;
4461 	if (buf && buflen < len)
4462 		return -ERANGE;
4463 	if (buf)
4464 		memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4465 	return len;
4466 }
4467 
4468 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4469 {
4470 	if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4471 		(fattr->valid & NFS_ATTR_FATTR_FSID) &&
4472 		(fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4473 		return;
4474 
4475 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4476 		NFS_ATTR_FATTR_NLINK;
4477 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4478 	fattr->nlink = 2;
4479 }
4480 
4481 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4482 		struct nfs4_fs_locations *fs_locations, struct page *page)
4483 {
4484 	struct nfs_server *server = NFS_SERVER(dir);
4485 	u32 bitmask[2] = {
4486 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4487 		[1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4488 	};
4489 	struct nfs4_fs_locations_arg args = {
4490 		.dir_fh = NFS_FH(dir),
4491 		.name = name,
4492 		.page = page,
4493 		.bitmask = bitmask,
4494 	};
4495 	struct nfs4_fs_locations_res res = {
4496 		.fs_locations = fs_locations,
4497 	};
4498 	struct rpc_message msg = {
4499 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4500 		.rpc_argp = &args,
4501 		.rpc_resp = &res,
4502 	};
4503 	int status;
4504 
4505 	dprintk("%s: start\n", __func__);
4506 	nfs_fattr_init(&fs_locations->fattr);
4507 	fs_locations->server = server;
4508 	fs_locations->nlocations = 0;
4509 	status = nfs4_call_sync(server, &msg, &args, &res, 0);
4510 	nfs_fixup_referral_attributes(&fs_locations->fattr);
4511 	dprintk("%s: returned status = %d\n", __func__, status);
4512 	return status;
4513 }
4514 
4515 #ifdef CONFIG_NFS_V4_1
4516 /*
4517  * nfs4_proc_exchange_id()
4518  *
4519  * Since the clientid has expired, all compounds using sessions
4520  * associated with the stale clientid will be returning
4521  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4522  * be in some phase of session reset.
4523  */
4524 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4525 {
4526 	nfs4_verifier verifier;
4527 	struct nfs41_exchange_id_args args = {
4528 		.client = clp,
4529 		.flags = clp->cl_exchange_flags,
4530 	};
4531 	struct nfs41_exchange_id_res res = {
4532 		.client = clp,
4533 	};
4534 	int status;
4535 	struct rpc_message msg = {
4536 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4537 		.rpc_argp = &args,
4538 		.rpc_resp = &res,
4539 		.rpc_cred = cred,
4540 	};
4541 	__be32 *p;
4542 
4543 	dprintk("--> %s\n", __func__);
4544 	BUG_ON(clp == NULL);
4545 
4546 	/* Remove server-only flags */
4547 	args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4548 
4549 	p = (u32 *)verifier.data;
4550 	*p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4551 	*p = htonl((u32)clp->cl_boot_time.tv_nsec);
4552 	args.verifier = &verifier;
4553 
4554 	while (1) {
4555 		args.id_len = scnprintf(args.id, sizeof(args.id),
4556 					"%s/%s %u",
4557 					clp->cl_ipaddr,
4558 					rpc_peeraddr2str(clp->cl_rpcclient,
4559 							 RPC_DISPLAY_ADDR),
4560 					clp->cl_id_uniquifier);
4561 
4562 		status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4563 
4564 		if (status != -NFS4ERR_CLID_INUSE)
4565 			break;
4566 
4567 		if (signalled())
4568 			break;
4569 
4570 		if (++clp->cl_id_uniquifier == 0)
4571 			break;
4572 	}
4573 
4574 	dprintk("<-- %s status= %d\n", __func__, status);
4575 	return status;
4576 }
4577 
4578 struct nfs4_get_lease_time_data {
4579 	struct nfs4_get_lease_time_args *args;
4580 	struct nfs4_get_lease_time_res *res;
4581 	struct nfs_client *clp;
4582 };
4583 
4584 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4585 					void *calldata)
4586 {
4587 	int ret;
4588 	struct nfs4_get_lease_time_data *data =
4589 			(struct nfs4_get_lease_time_data *)calldata;
4590 
4591 	dprintk("--> %s\n", __func__);
4592 	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4593 	/* just setup sequence, do not trigger session recovery
4594 	   since we're invoked within one */
4595 	ret = nfs41_setup_sequence(data->clp->cl_session,
4596 				   &data->args->la_seq_args,
4597 				   &data->res->lr_seq_res, 0, task);
4598 
4599 	BUG_ON(ret == -EAGAIN);
4600 	rpc_call_start(task);
4601 	dprintk("<-- %s\n", __func__);
4602 }
4603 
4604 /*
4605  * Called from nfs4_state_manager thread for session setup, so don't recover
4606  * from sequence operation or clientid errors.
4607  */
4608 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4609 {
4610 	struct nfs4_get_lease_time_data *data =
4611 			(struct nfs4_get_lease_time_data *)calldata;
4612 
4613 	dprintk("--> %s\n", __func__);
4614 	nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4615 	switch (task->tk_status) {
4616 	case -NFS4ERR_DELAY:
4617 	case -NFS4ERR_GRACE:
4618 	case -EKEYEXPIRED:
4619 		dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4620 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
4621 		task->tk_status = 0;
4622 		nfs_restart_rpc(task, data->clp);
4623 		return;
4624 	}
4625 	dprintk("<-- %s\n", __func__);
4626 }
4627 
4628 struct rpc_call_ops nfs4_get_lease_time_ops = {
4629 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
4630 	.rpc_call_done = nfs4_get_lease_time_done,
4631 };
4632 
4633 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4634 {
4635 	struct rpc_task *task;
4636 	struct nfs4_get_lease_time_args args;
4637 	struct nfs4_get_lease_time_res res = {
4638 		.lr_fsinfo = fsinfo,
4639 	};
4640 	struct nfs4_get_lease_time_data data = {
4641 		.args = &args,
4642 		.res = &res,
4643 		.clp = clp,
4644 	};
4645 	struct rpc_message msg = {
4646 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4647 		.rpc_argp = &args,
4648 		.rpc_resp = &res,
4649 	};
4650 	struct rpc_task_setup task_setup = {
4651 		.rpc_client = clp->cl_rpcclient,
4652 		.rpc_message = &msg,
4653 		.callback_ops = &nfs4_get_lease_time_ops,
4654 		.callback_data = &data
4655 	};
4656 	int status;
4657 
4658 	res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4659 	dprintk("--> %s\n", __func__);
4660 	task = rpc_run_task(&task_setup);
4661 
4662 	if (IS_ERR(task))
4663 		status = PTR_ERR(task);
4664 	else {
4665 		status = task->tk_status;
4666 		rpc_put_task(task);
4667 	}
4668 	dprintk("<-- %s return %d\n", __func__, status);
4669 
4670 	return status;
4671 }
4672 
4673 /*
4674  * Reset a slot table
4675  */
4676 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4677 				 int ivalue)
4678 {
4679 	struct nfs4_slot *new = NULL;
4680 	int i;
4681 	int ret = 0;
4682 
4683 	dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4684 		max_reqs, tbl->max_slots);
4685 
4686 	/* Does the newly negotiated max_reqs match the existing slot table? */
4687 	if (max_reqs != tbl->max_slots) {
4688 		ret = -ENOMEM;
4689 		new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4690 			      GFP_NOFS);
4691 		if (!new)
4692 			goto out;
4693 		ret = 0;
4694 		kfree(tbl->slots);
4695 	}
4696 	spin_lock(&tbl->slot_tbl_lock);
4697 	if (new) {
4698 		tbl->slots = new;
4699 		tbl->max_slots = max_reqs;
4700 	}
4701 	for (i = 0; i < tbl->max_slots; ++i)
4702 		tbl->slots[i].seq_nr = ivalue;
4703 	spin_unlock(&tbl->slot_tbl_lock);
4704 	dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4705 		tbl, tbl->slots, tbl->max_slots);
4706 out:
4707 	dprintk("<-- %s: return %d\n", __func__, ret);
4708 	return ret;
4709 }
4710 
4711 /*
4712  * Reset the forechannel and backchannel slot tables
4713  */
4714 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4715 {
4716 	int status;
4717 
4718 	status = nfs4_reset_slot_table(&session->fc_slot_table,
4719 			session->fc_attrs.max_reqs, 1);
4720 	if (status)
4721 		return status;
4722 
4723 	status = nfs4_reset_slot_table(&session->bc_slot_table,
4724 			session->bc_attrs.max_reqs, 0);
4725 	return status;
4726 }
4727 
4728 /* Destroy the slot table */
4729 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4730 {
4731 	if (session->fc_slot_table.slots != NULL) {
4732 		kfree(session->fc_slot_table.slots);
4733 		session->fc_slot_table.slots = NULL;
4734 	}
4735 	if (session->bc_slot_table.slots != NULL) {
4736 		kfree(session->bc_slot_table.slots);
4737 		session->bc_slot_table.slots = NULL;
4738 	}
4739 	return;
4740 }
4741 
4742 /*
4743  * Initialize slot table
4744  */
4745 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4746 		int max_slots, int ivalue)
4747 {
4748 	struct nfs4_slot *slot;
4749 	int ret = -ENOMEM;
4750 
4751 	BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4752 
4753 	dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4754 
4755 	slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4756 	if (!slot)
4757 		goto out;
4758 	ret = 0;
4759 
4760 	spin_lock(&tbl->slot_tbl_lock);
4761 	tbl->max_slots = max_slots;
4762 	tbl->slots = slot;
4763 	tbl->highest_used_slotid = -1;  /* no slot is currently used */
4764 	spin_unlock(&tbl->slot_tbl_lock);
4765 	dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4766 		tbl, tbl->slots, tbl->max_slots);
4767 out:
4768 	dprintk("<-- %s: return %d\n", __func__, ret);
4769 	return ret;
4770 }
4771 
4772 /*
4773  * Initialize the forechannel and backchannel tables
4774  */
4775 static int nfs4_init_slot_tables(struct nfs4_session *session)
4776 {
4777 	struct nfs4_slot_table *tbl;
4778 	int status = 0;
4779 
4780 	tbl = &session->fc_slot_table;
4781 	if (tbl->slots == NULL) {
4782 		status = nfs4_init_slot_table(tbl,
4783 				session->fc_attrs.max_reqs, 1);
4784 		if (status)
4785 			return status;
4786 	}
4787 
4788 	tbl = &session->bc_slot_table;
4789 	if (tbl->slots == NULL) {
4790 		status = nfs4_init_slot_table(tbl,
4791 				session->bc_attrs.max_reqs, 0);
4792 		if (status)
4793 			nfs4_destroy_slot_tables(session);
4794 	}
4795 
4796 	return status;
4797 }
4798 
4799 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4800 {
4801 	struct nfs4_session *session;
4802 	struct nfs4_slot_table *tbl;
4803 
4804 	session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4805 	if (!session)
4806 		return NULL;
4807 
4808 	/*
4809 	 * The create session reply races with the server back
4810 	 * channel probe. Mark the client NFS_CS_SESSION_INITING
4811 	 * so that the client back channel can find the
4812 	 * nfs_client struct
4813 	 */
4814 	clp->cl_cons_state = NFS_CS_SESSION_INITING;
4815 	init_completion(&session->complete);
4816 
4817 	tbl = &session->fc_slot_table;
4818 	tbl->highest_used_slotid = -1;
4819 	spin_lock_init(&tbl->slot_tbl_lock);
4820 	rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4821 
4822 	tbl = &session->bc_slot_table;
4823 	tbl->highest_used_slotid = -1;
4824 	spin_lock_init(&tbl->slot_tbl_lock);
4825 	rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4826 
4827 	session->clp = clp;
4828 	return session;
4829 }
4830 
4831 void nfs4_destroy_session(struct nfs4_session *session)
4832 {
4833 	nfs4_proc_destroy_session(session);
4834 	dprintk("%s Destroy backchannel for xprt %p\n",
4835 		__func__, session->clp->cl_rpcclient->cl_xprt);
4836 	xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4837 				NFS41_BC_MIN_CALLBACKS);
4838 	nfs4_destroy_slot_tables(session);
4839 	kfree(session);
4840 }
4841 
4842 /*
4843  * Initialize the values to be used by the client in CREATE_SESSION
4844  * If nfs4_init_session set the fore channel request and response sizes,
4845  * use them.
4846  *
4847  * Set the back channel max_resp_sz_cached to zero to force the client to
4848  * always set csa_cachethis to FALSE because the current implementation
4849  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4850  */
4851 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4852 {
4853 	struct nfs4_session *session = args->client->cl_session;
4854 	unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4855 		     mxresp_sz = session->fc_attrs.max_resp_sz;
4856 
4857 	if (mxrqst_sz == 0)
4858 		mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4859 	if (mxresp_sz == 0)
4860 		mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4861 	/* Fore channel attributes */
4862 	args->fc_attrs.headerpadsz = 0;
4863 	args->fc_attrs.max_rqst_sz = mxrqst_sz;
4864 	args->fc_attrs.max_resp_sz = mxresp_sz;
4865 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
4866 	args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4867 
4868 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4869 		"max_ops=%u max_reqs=%u\n",
4870 		__func__,
4871 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4872 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4873 
4874 	/* Back channel attributes */
4875 	args->bc_attrs.headerpadsz = 0;
4876 	args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4877 	args->bc_attrs.max_resp_sz = PAGE_SIZE;
4878 	args->bc_attrs.max_resp_sz_cached = 0;
4879 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4880 	args->bc_attrs.max_reqs = 1;
4881 
4882 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4883 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4884 		__func__,
4885 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4886 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4887 		args->bc_attrs.max_reqs);
4888 }
4889 
4890 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4891 {
4892 	if (rcvd <= sent)
4893 		return 0;
4894 	printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4895 		"sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4896 	return -EINVAL;
4897 }
4898 
4899 #define _verify_fore_channel_attr(_name_) \
4900 	_verify_channel_attr("fore", #_name_, \
4901 			     args->fc_attrs._name_, \
4902 			     session->fc_attrs._name_)
4903 
4904 #define _verify_back_channel_attr(_name_) \
4905 	_verify_channel_attr("back", #_name_, \
4906 			     args->bc_attrs._name_, \
4907 			     session->bc_attrs._name_)
4908 
4909 /*
4910  * The server is not allowed to increase the fore channel header pad size,
4911  * maximum response size, or maximum number of operations.
4912  *
4913  * The back channel attributes are only negotiatied down: We send what the
4914  * (back channel) server insists upon.
4915  */
4916 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4917 				     struct nfs4_session *session)
4918 {
4919 	int ret = 0;
4920 
4921 	ret |= _verify_fore_channel_attr(headerpadsz);
4922 	ret |= _verify_fore_channel_attr(max_resp_sz);
4923 	ret |= _verify_fore_channel_attr(max_ops);
4924 
4925 	ret |= _verify_back_channel_attr(headerpadsz);
4926 	ret |= _verify_back_channel_attr(max_rqst_sz);
4927 	ret |= _verify_back_channel_attr(max_resp_sz);
4928 	ret |= _verify_back_channel_attr(max_resp_sz_cached);
4929 	ret |= _verify_back_channel_attr(max_ops);
4930 	ret |= _verify_back_channel_attr(max_reqs);
4931 
4932 	return ret;
4933 }
4934 
4935 static int _nfs4_proc_create_session(struct nfs_client *clp)
4936 {
4937 	struct nfs4_session *session = clp->cl_session;
4938 	struct nfs41_create_session_args args = {
4939 		.client = clp,
4940 		.cb_program = NFS4_CALLBACK,
4941 	};
4942 	struct nfs41_create_session_res res = {
4943 		.client = clp,
4944 	};
4945 	struct rpc_message msg = {
4946 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4947 		.rpc_argp = &args,
4948 		.rpc_resp = &res,
4949 	};
4950 	int status;
4951 
4952 	nfs4_init_channel_attrs(&args);
4953 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4954 
4955 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4956 
4957 	if (!status)
4958 		/* Verify the session's negotiated channel_attrs values */
4959 		status = nfs4_verify_channel_attrs(&args, session);
4960 	if (!status) {
4961 		/* Increment the clientid slot sequence id */
4962 		clp->cl_seqid++;
4963 	}
4964 
4965 	return status;
4966 }
4967 
4968 /*
4969  * Issues a CREATE_SESSION operation to the server.
4970  * It is the responsibility of the caller to verify the session is
4971  * expired before calling this routine.
4972  */
4973 int nfs4_proc_create_session(struct nfs_client *clp)
4974 {
4975 	int status;
4976 	unsigned *ptr;
4977 	struct nfs4_session *session = clp->cl_session;
4978 
4979 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4980 
4981 	status = _nfs4_proc_create_session(clp);
4982 	if (status)
4983 		goto out;
4984 
4985 	/* Init and reset the fore channel */
4986 	status = nfs4_init_slot_tables(session);
4987 	dprintk("slot table initialization returned %d\n", status);
4988 	if (status)
4989 		goto out;
4990 	status = nfs4_reset_slot_tables(session);
4991 	dprintk("slot table reset returned %d\n", status);
4992 	if (status)
4993 		goto out;
4994 
4995 	ptr = (unsigned *)&session->sess_id.data[0];
4996 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4997 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4998 out:
4999 	dprintk("<-- %s\n", __func__);
5000 	return status;
5001 }
5002 
5003 /*
5004  * Issue the over-the-wire RPC DESTROY_SESSION.
5005  * The caller must serialize access to this routine.
5006  */
5007 int nfs4_proc_destroy_session(struct nfs4_session *session)
5008 {
5009 	int status = 0;
5010 	struct rpc_message msg;
5011 
5012 	dprintk("--> nfs4_proc_destroy_session\n");
5013 
5014 	/* session is still being setup */
5015 	if (session->clp->cl_cons_state != NFS_CS_READY)
5016 		return status;
5017 
5018 	msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5019 	msg.rpc_argp = session;
5020 	msg.rpc_resp = NULL;
5021 	msg.rpc_cred = NULL;
5022 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5023 
5024 	if (status)
5025 		printk(KERN_WARNING
5026 			"Got error %d from the server on DESTROY_SESSION. "
5027 			"Session has been destroyed regardless...\n", status);
5028 
5029 	dprintk("<-- nfs4_proc_destroy_session\n");
5030 	return status;
5031 }
5032 
5033 int nfs4_init_session(struct nfs_server *server)
5034 {
5035 	struct nfs_client *clp = server->nfs_client;
5036 	struct nfs4_session *session;
5037 	unsigned int rsize, wsize;
5038 	int ret;
5039 
5040 	if (!nfs4_has_session(clp))
5041 		return 0;
5042 
5043 	rsize = server->rsize;
5044 	if (rsize == 0)
5045 		rsize = NFS_MAX_FILE_IO_SIZE;
5046 	wsize = server->wsize;
5047 	if (wsize == 0)
5048 		wsize = NFS_MAX_FILE_IO_SIZE;
5049 
5050 	session = clp->cl_session;
5051 	session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5052 	session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5053 
5054 	ret = nfs4_recover_expired_lease(server);
5055 	if (!ret)
5056 		ret = nfs4_check_client_ready(clp);
5057 	return ret;
5058 }
5059 
5060 /*
5061  * Renew the cl_session lease.
5062  */
5063 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5064 {
5065 	struct nfs4_sequence_args args;
5066 	struct nfs4_sequence_res res;
5067 
5068 	struct rpc_message msg = {
5069 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5070 		.rpc_argp = &args,
5071 		.rpc_resp = &res,
5072 		.rpc_cred = cred,
5073 	};
5074 
5075 	args.sa_cache_this = 0;
5076 
5077 	return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
5078 				       &res, args.sa_cache_this, 1);
5079 }
5080 
5081 static void nfs41_sequence_release(void *data)
5082 {
5083 	struct nfs_client *clp = (struct nfs_client *)data;
5084 
5085 	if (atomic_read(&clp->cl_count) > 1)
5086 		nfs4_schedule_state_renewal(clp);
5087 	nfs_put_client(clp);
5088 }
5089 
5090 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5091 {
5092 	struct nfs_client *clp = (struct nfs_client *)data;
5093 
5094 	nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5095 
5096 	if (task->tk_status < 0) {
5097 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
5098 		if (atomic_read(&clp->cl_count) == 1)
5099 			goto out;
5100 
5101 		if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5102 								== -EAGAIN) {
5103 			nfs_restart_rpc(task, clp);
5104 			return;
5105 		}
5106 	}
5107 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5108 out:
5109 	kfree(task->tk_msg.rpc_argp);
5110 	kfree(task->tk_msg.rpc_resp);
5111 
5112 	dprintk("<-- %s\n", __func__);
5113 }
5114 
5115 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5116 {
5117 	struct nfs_client *clp;
5118 	struct nfs4_sequence_args *args;
5119 	struct nfs4_sequence_res *res;
5120 
5121 	clp = (struct nfs_client *)data;
5122 	args = task->tk_msg.rpc_argp;
5123 	res = task->tk_msg.rpc_resp;
5124 
5125 	if (nfs4_setup_sequence(clp, args, res, 0, task))
5126 		return;
5127 	rpc_call_start(task);
5128 }
5129 
5130 static const struct rpc_call_ops nfs41_sequence_ops = {
5131 	.rpc_call_done = nfs41_sequence_call_done,
5132 	.rpc_call_prepare = nfs41_sequence_prepare,
5133 	.rpc_release = nfs41_sequence_release,
5134 };
5135 
5136 static int nfs41_proc_async_sequence(struct nfs_client *clp,
5137 				     struct rpc_cred *cred)
5138 {
5139 	struct nfs4_sequence_args *args;
5140 	struct nfs4_sequence_res *res;
5141 	struct rpc_message msg = {
5142 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5143 		.rpc_cred = cred,
5144 	};
5145 
5146 	if (!atomic_inc_not_zero(&clp->cl_count))
5147 		return -EIO;
5148 	args = kzalloc(sizeof(*args), GFP_NOFS);
5149 	res = kzalloc(sizeof(*res), GFP_NOFS);
5150 	if (!args || !res) {
5151 		kfree(args);
5152 		kfree(res);
5153 		nfs_put_client(clp);
5154 		return -ENOMEM;
5155 	}
5156 	res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5157 	msg.rpc_argp = args;
5158 	msg.rpc_resp = res;
5159 
5160 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5161 			      &nfs41_sequence_ops, (void *)clp);
5162 }
5163 
5164 struct nfs4_reclaim_complete_data {
5165 	struct nfs_client *clp;
5166 	struct nfs41_reclaim_complete_args arg;
5167 	struct nfs41_reclaim_complete_res res;
5168 };
5169 
5170 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5171 {
5172 	struct nfs4_reclaim_complete_data *calldata = data;
5173 
5174 	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5175 	if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5176 				&calldata->res.seq_res, 0, task))
5177 		return;
5178 
5179 	rpc_call_start(task);
5180 }
5181 
5182 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5183 {
5184 	struct nfs4_reclaim_complete_data *calldata = data;
5185 	struct nfs_client *clp = calldata->clp;
5186 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
5187 
5188 	dprintk("--> %s\n", __func__);
5189 	nfs41_sequence_done(clp, res, task->tk_status);
5190 	switch (task->tk_status) {
5191 	case 0:
5192 	case -NFS4ERR_COMPLETE_ALREADY:
5193 		break;
5194 	case -NFS4ERR_BADSESSION:
5195 	case -NFS4ERR_DEADSESSION:
5196 		/*
5197 		 * Handle the session error, but do not retry the operation, as
5198 		 * we have no way of telling whether the clientid had to be
5199 		 * reset before we got our reply.  If reset, a new wave of
5200 		 * reclaim operations will follow, containing their own reclaim
5201 		 * complete.  We don't want our retry to get on the way of
5202 		 * recovery by incorrectly indicating to the server that we're
5203 		 * done reclaiming state since the process had to be restarted.
5204 		 */
5205 		_nfs4_async_handle_error(task, NULL, clp, NULL);
5206 		break;
5207 	default:
5208 		if (_nfs4_async_handle_error(
5209 				task, NULL, clp, NULL) == -EAGAIN) {
5210 			rpc_restart_call_prepare(task);
5211 			return;
5212 		}
5213 	}
5214 
5215 	dprintk("<-- %s\n", __func__);
5216 }
5217 
5218 static void nfs4_free_reclaim_complete_data(void *data)
5219 {
5220 	struct nfs4_reclaim_complete_data *calldata = data;
5221 
5222 	kfree(calldata);
5223 }
5224 
5225 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5226 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
5227 	.rpc_call_done = nfs4_reclaim_complete_done,
5228 	.rpc_release = nfs4_free_reclaim_complete_data,
5229 };
5230 
5231 /*
5232  * Issue a global reclaim complete.
5233  */
5234 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5235 {
5236 	struct nfs4_reclaim_complete_data *calldata;
5237 	struct rpc_task *task;
5238 	struct rpc_message msg = {
5239 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5240 	};
5241 	struct rpc_task_setup task_setup_data = {
5242 		.rpc_client = clp->cl_rpcclient,
5243 		.rpc_message = &msg,
5244 		.callback_ops = &nfs4_reclaim_complete_call_ops,
5245 		.flags = RPC_TASK_ASYNC,
5246 	};
5247 	int status = -ENOMEM;
5248 
5249 	dprintk("--> %s\n", __func__);
5250 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5251 	if (calldata == NULL)
5252 		goto out;
5253 	calldata->clp = clp;
5254 	calldata->arg.one_fs = 0;
5255 	calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5256 
5257 	msg.rpc_argp = &calldata->arg;
5258 	msg.rpc_resp = &calldata->res;
5259 	task_setup_data.callback_data = calldata;
5260 	task = rpc_run_task(&task_setup_data);
5261 	if (IS_ERR(task)) {
5262 		status = PTR_ERR(task);
5263 		goto out;
5264 	}
5265 	rpc_put_task(task);
5266 	return 0;
5267 out:
5268 	dprintk("<-- %s status=%d\n", __func__, status);
5269 	return status;
5270 }
5271 #endif /* CONFIG_NFS_V4_1 */
5272 
5273 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5274 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5275 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
5276 	.recover_open	= nfs4_open_reclaim,
5277 	.recover_lock	= nfs4_lock_reclaim,
5278 	.establish_clid = nfs4_init_clientid,
5279 	.get_clid_cred	= nfs4_get_setclientid_cred,
5280 };
5281 
5282 #if defined(CONFIG_NFS_V4_1)
5283 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5284 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5285 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
5286 	.recover_open	= nfs4_open_reclaim,
5287 	.recover_lock	= nfs4_lock_reclaim,
5288 	.establish_clid = nfs41_init_clientid,
5289 	.get_clid_cred	= nfs4_get_exchange_id_cred,
5290 	.reclaim_complete = nfs41_proc_reclaim_complete,
5291 };
5292 #endif /* CONFIG_NFS_V4_1 */
5293 
5294 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5295 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5296 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
5297 	.recover_open	= nfs4_open_expired,
5298 	.recover_lock	= nfs4_lock_expired,
5299 	.establish_clid = nfs4_init_clientid,
5300 	.get_clid_cred	= nfs4_get_setclientid_cred,
5301 };
5302 
5303 #if defined(CONFIG_NFS_V4_1)
5304 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5305 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5306 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
5307 	.recover_open	= nfs4_open_expired,
5308 	.recover_lock	= nfs4_lock_expired,
5309 	.establish_clid = nfs41_init_clientid,
5310 	.get_clid_cred	= nfs4_get_exchange_id_cred,
5311 };
5312 #endif /* CONFIG_NFS_V4_1 */
5313 
5314 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5315 	.sched_state_renewal = nfs4_proc_async_renew,
5316 	.get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5317 	.renew_lease = nfs4_proc_renew,
5318 };
5319 
5320 #if defined(CONFIG_NFS_V4_1)
5321 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5322 	.sched_state_renewal = nfs41_proc_async_sequence,
5323 	.get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5324 	.renew_lease = nfs4_proc_sequence,
5325 };
5326 #endif
5327 
5328 /*
5329  * Per minor version reboot and network partition recovery ops
5330  */
5331 
5332 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5333 	&nfs40_reboot_recovery_ops,
5334 #if defined(CONFIG_NFS_V4_1)
5335 	&nfs41_reboot_recovery_ops,
5336 #endif
5337 };
5338 
5339 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5340 	&nfs40_nograce_recovery_ops,
5341 #if defined(CONFIG_NFS_V4_1)
5342 	&nfs41_nograce_recovery_ops,
5343 #endif
5344 };
5345 
5346 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5347 	&nfs40_state_renewal_ops,
5348 #if defined(CONFIG_NFS_V4_1)
5349 	&nfs41_state_renewal_ops,
5350 #endif
5351 };
5352 
5353 static const struct inode_operations nfs4_file_inode_operations = {
5354 	.permission	= nfs_permission,
5355 	.getattr	= nfs_getattr,
5356 	.setattr	= nfs_setattr,
5357 	.getxattr	= nfs4_getxattr,
5358 	.setxattr	= nfs4_setxattr,
5359 	.listxattr	= nfs4_listxattr,
5360 };
5361 
5362 const struct nfs_rpc_ops nfs_v4_clientops = {
5363 	.version	= 4,			/* protocol version */
5364 	.dentry_ops	= &nfs4_dentry_operations,
5365 	.dir_inode_ops	= &nfs4_dir_inode_operations,
5366 	.file_inode_ops	= &nfs4_file_inode_operations,
5367 	.getroot	= nfs4_proc_get_root,
5368 	.getattr	= nfs4_proc_getattr,
5369 	.setattr	= nfs4_proc_setattr,
5370 	.lookupfh	= nfs4_proc_lookupfh,
5371 	.lookup		= nfs4_proc_lookup,
5372 	.access		= nfs4_proc_access,
5373 	.readlink	= nfs4_proc_readlink,
5374 	.create		= nfs4_proc_create,
5375 	.remove		= nfs4_proc_remove,
5376 	.unlink_setup	= nfs4_proc_unlink_setup,
5377 	.unlink_done	= nfs4_proc_unlink_done,
5378 	.rename		= nfs4_proc_rename,
5379 	.link		= nfs4_proc_link,
5380 	.symlink	= nfs4_proc_symlink,
5381 	.mkdir		= nfs4_proc_mkdir,
5382 	.rmdir		= nfs4_proc_remove,
5383 	.readdir	= nfs4_proc_readdir,
5384 	.mknod		= nfs4_proc_mknod,
5385 	.statfs		= nfs4_proc_statfs,
5386 	.fsinfo		= nfs4_proc_fsinfo,
5387 	.pathconf	= nfs4_proc_pathconf,
5388 	.set_capabilities = nfs4_server_capabilities,
5389 	.decode_dirent	= nfs4_decode_dirent,
5390 	.read_setup	= nfs4_proc_read_setup,
5391 	.read_done	= nfs4_read_done,
5392 	.write_setup	= nfs4_proc_write_setup,
5393 	.write_done	= nfs4_write_done,
5394 	.commit_setup	= nfs4_proc_commit_setup,
5395 	.commit_done	= nfs4_commit_done,
5396 	.lock		= nfs4_proc_lock,
5397 	.clear_acl_cache = nfs4_zap_acl_attr,
5398 	.close_context  = nfs4_close_context,
5399 };
5400 
5401 /*
5402  * Local variables:
5403  *  c-basic-offset: 8
5404  * End:
5405  */
5406