xref: /linux/fs/nfs/nfs4proc.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
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/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
58 
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4session.h"
67 
68 
69 #define NFSDBG_FACILITY		NFSDBG_PROC
70 
71 #define NFS4_POLL_RETRY_MIN	(HZ/10)
72 #define NFS4_POLL_RETRY_MAX	(15*HZ)
73 
74 struct nfs4_opendata;
75 static int _nfs4_proc_open(struct nfs4_opendata *data);
76 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
77 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
78 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
79 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
80 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
81 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
82 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 			    struct nfs_fattr *fattr, struct iattr *sattr,
84 			    struct nfs4_state *state);
85 #ifdef CONFIG_NFS_V4_1
86 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
88 #endif
89 /* Prevent leaks of NFSv4 errors into userland */
90 static int nfs4_map_errors(int err)
91 {
92 	if (err >= -1000)
93 		return err;
94 	switch (err) {
95 	case -NFS4ERR_RESOURCE:
96 		return -EREMOTEIO;
97 	case -NFS4ERR_WRONGSEC:
98 		return -EPERM;
99 	case -NFS4ERR_BADOWNER:
100 	case -NFS4ERR_BADNAME:
101 		return -EINVAL;
102 	case -NFS4ERR_SHARE_DENIED:
103 		return -EACCES;
104 	case -NFS4ERR_MINOR_VERS_MISMATCH:
105 		return -EPROTONOSUPPORT;
106 	case -NFS4ERR_ACCESS:
107 		return -EACCES;
108 	default:
109 		dprintk("%s could not handle NFSv4 error %d\n",
110 				__func__, -err);
111 		break;
112 	}
113 	return -EIO;
114 }
115 
116 /*
117  * This is our standard bitmap for GETATTR requests.
118  */
119 const u32 nfs4_fattr_bitmap[3] = {
120 	FATTR4_WORD0_TYPE
121 	| FATTR4_WORD0_CHANGE
122 	| FATTR4_WORD0_SIZE
123 	| FATTR4_WORD0_FSID
124 	| FATTR4_WORD0_FILEID,
125 	FATTR4_WORD1_MODE
126 	| FATTR4_WORD1_NUMLINKS
127 	| FATTR4_WORD1_OWNER
128 	| FATTR4_WORD1_OWNER_GROUP
129 	| FATTR4_WORD1_RAWDEV
130 	| FATTR4_WORD1_SPACE_USED
131 	| FATTR4_WORD1_TIME_ACCESS
132 	| FATTR4_WORD1_TIME_METADATA
133 	| FATTR4_WORD1_TIME_MODIFY
134 };
135 
136 static const u32 nfs4_pnfs_open_bitmap[3] = {
137 	FATTR4_WORD0_TYPE
138 	| FATTR4_WORD0_CHANGE
139 	| FATTR4_WORD0_SIZE
140 	| FATTR4_WORD0_FSID
141 	| FATTR4_WORD0_FILEID,
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_WORD2_MDSTHRESHOLD
152 };
153 
154 static const u32 nfs4_open_noattr_bitmap[3] = {
155 	FATTR4_WORD0_TYPE
156 	| FATTR4_WORD0_CHANGE
157 	| FATTR4_WORD0_FILEID,
158 };
159 
160 const u32 nfs4_statfs_bitmap[2] = {
161 	FATTR4_WORD0_FILES_AVAIL
162 	| FATTR4_WORD0_FILES_FREE
163 	| FATTR4_WORD0_FILES_TOTAL,
164 	FATTR4_WORD1_SPACE_AVAIL
165 	| FATTR4_WORD1_SPACE_FREE
166 	| FATTR4_WORD1_SPACE_TOTAL
167 };
168 
169 const u32 nfs4_pathconf_bitmap[2] = {
170 	FATTR4_WORD0_MAXLINK
171 	| FATTR4_WORD0_MAXNAME,
172 	0
173 };
174 
175 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
176 			| FATTR4_WORD0_MAXREAD
177 			| FATTR4_WORD0_MAXWRITE
178 			| FATTR4_WORD0_LEASE_TIME,
179 			FATTR4_WORD1_TIME_DELTA
180 			| FATTR4_WORD1_FS_LAYOUT_TYPES,
181 			FATTR4_WORD2_LAYOUT_BLKSIZE
182 };
183 
184 const u32 nfs4_fs_locations_bitmap[2] = {
185 	FATTR4_WORD0_TYPE
186 	| FATTR4_WORD0_CHANGE
187 	| FATTR4_WORD0_SIZE
188 	| FATTR4_WORD0_FSID
189 	| FATTR4_WORD0_FILEID
190 	| FATTR4_WORD0_FS_LOCATIONS,
191 	FATTR4_WORD1_MODE
192 	| FATTR4_WORD1_NUMLINKS
193 	| FATTR4_WORD1_OWNER
194 	| FATTR4_WORD1_OWNER_GROUP
195 	| FATTR4_WORD1_RAWDEV
196 	| FATTR4_WORD1_SPACE_USED
197 	| FATTR4_WORD1_TIME_ACCESS
198 	| FATTR4_WORD1_TIME_METADATA
199 	| FATTR4_WORD1_TIME_MODIFY
200 	| FATTR4_WORD1_MOUNTED_ON_FILEID
201 };
202 
203 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
204 		struct nfs4_readdir_arg *readdir)
205 {
206 	__be32 *start, *p;
207 
208 	if (cookie > 2) {
209 		readdir->cookie = cookie;
210 		memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 		return;
212 	}
213 
214 	readdir->cookie = 0;
215 	memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 	if (cookie == 2)
217 		return;
218 
219 	/*
220 	 * NFSv4 servers do not return entries for '.' and '..'
221 	 * Therefore, we fake these entries here.  We let '.'
222 	 * have cookie 0 and '..' have cookie 1.  Note that
223 	 * when talking to the server, we always send cookie 0
224 	 * instead of 1 or 2.
225 	 */
226 	start = p = kmap_atomic(*readdir->pages);
227 
228 	if (cookie == 0) {
229 		*p++ = xdr_one;                                  /* next */
230 		*p++ = xdr_zero;                   /* cookie, first word */
231 		*p++ = xdr_one;                   /* cookie, second word */
232 		*p++ = xdr_one;                             /* entry len */
233 		memcpy(p, ".\0\0\0", 4);                        /* entry */
234 		p++;
235 		*p++ = xdr_one;                         /* bitmap length */
236 		*p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
237 		*p++ = htonl(8);              /* attribute buffer length */
238 		p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
239 	}
240 
241 	*p++ = xdr_one;                                  /* next */
242 	*p++ = xdr_zero;                   /* cookie, first word */
243 	*p++ = xdr_two;                   /* cookie, second word */
244 	*p++ = xdr_two;                             /* entry len */
245 	memcpy(p, "..\0\0", 4);                         /* entry */
246 	p++;
247 	*p++ = xdr_one;                         /* bitmap length */
248 	*p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
249 	*p++ = htonl(8);              /* attribute buffer length */
250 	p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
251 
252 	readdir->pgbase = (char *)p - (char *)start;
253 	readdir->count -= readdir->pgbase;
254 	kunmap_atomic(start);
255 }
256 
257 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
258 {
259 	int res = 0;
260 
261 	might_sleep();
262 
263 	if (*timeout <= 0)
264 		*timeout = NFS4_POLL_RETRY_MIN;
265 	if (*timeout > NFS4_POLL_RETRY_MAX)
266 		*timeout = NFS4_POLL_RETRY_MAX;
267 	freezable_schedule_timeout_killable(*timeout);
268 	if (fatal_signal_pending(current))
269 		res = -ERESTARTSYS;
270 	*timeout <<= 1;
271 	return res;
272 }
273 
274 /* This is the error handling routine for processes that are allowed
275  * to sleep.
276  */
277 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
278 {
279 	struct nfs_client *clp = server->nfs_client;
280 	struct nfs4_state *state = exception->state;
281 	struct inode *inode = exception->inode;
282 	int ret = errorcode;
283 
284 	exception->retry = 0;
285 	switch(errorcode) {
286 		case 0:
287 			return 0;
288 		case -NFS4ERR_OPENMODE:
289 			if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
290 				nfs4_inode_return_delegation(inode);
291 				exception->retry = 1;
292 				return 0;
293 			}
294 			if (state == NULL)
295 				break;
296 			nfs4_schedule_stateid_recovery(server, state);
297 			goto wait_on_recovery;
298 		case -NFS4ERR_DELEG_REVOKED:
299 		case -NFS4ERR_ADMIN_REVOKED:
300 		case -NFS4ERR_BAD_STATEID:
301 			if (state == NULL)
302 				break;
303 			nfs_remove_bad_delegation(state->inode);
304 			nfs4_schedule_stateid_recovery(server, state);
305 			goto wait_on_recovery;
306 		case -NFS4ERR_EXPIRED:
307 			if (state != NULL)
308 				nfs4_schedule_stateid_recovery(server, state);
309 		case -NFS4ERR_STALE_STATEID:
310 		case -NFS4ERR_STALE_CLIENTID:
311 			nfs4_schedule_lease_recovery(clp);
312 			goto wait_on_recovery;
313 #if defined(CONFIG_NFS_V4_1)
314 		case -NFS4ERR_BADSESSION:
315 		case -NFS4ERR_BADSLOT:
316 		case -NFS4ERR_BAD_HIGH_SLOT:
317 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 		case -NFS4ERR_DEADSESSION:
319 		case -NFS4ERR_SEQ_FALSE_RETRY:
320 		case -NFS4ERR_SEQ_MISORDERED:
321 			dprintk("%s ERROR: %d Reset session\n", __func__,
322 				errorcode);
323 			nfs4_schedule_session_recovery(clp->cl_session, errorcode);
324 			goto wait_on_recovery;
325 #endif /* defined(CONFIG_NFS_V4_1) */
326 		case -NFS4ERR_FILE_OPEN:
327 			if (exception->timeout > HZ) {
328 				/* We have retried a decent amount, time to
329 				 * fail
330 				 */
331 				ret = -EBUSY;
332 				break;
333 			}
334 		case -NFS4ERR_GRACE:
335 		case -NFS4ERR_DELAY:
336 			ret = nfs4_delay(server->client, &exception->timeout);
337 			if (ret != 0)
338 				break;
339 		case -NFS4ERR_RETRY_UNCACHED_REP:
340 		case -NFS4ERR_OLD_STATEID:
341 			exception->retry = 1;
342 			break;
343 		case -NFS4ERR_BADOWNER:
344 			/* The following works around a Linux server bug! */
345 		case -NFS4ERR_BADNAME:
346 			if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 				server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 				exception->retry = 1;
349 				printk(KERN_WARNING "NFS: v4 server %s "
350 						"does not accept raw "
351 						"uid/gids. "
352 						"Reenabling the idmapper.\n",
353 						server->nfs_client->cl_hostname);
354 			}
355 	}
356 	/* We failed to handle the error */
357 	return nfs4_map_errors(ret);
358 wait_on_recovery:
359 	ret = nfs4_wait_clnt_recover(clp);
360 	if (ret == 0)
361 		exception->retry = 1;
362 	return ret;
363 }
364 
365 
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
367 {
368 	spin_lock(&clp->cl_lock);
369 	if (time_before(clp->cl_last_renewal,timestamp))
370 		clp->cl_last_renewal = timestamp;
371 	spin_unlock(&clp->cl_lock);
372 }
373 
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
375 {
376 	do_renew_lease(server->nfs_client, timestamp);
377 }
378 
379 #if defined(CONFIG_NFS_V4_1)
380 
381 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
382 {
383 	struct nfs4_session *session;
384 	struct nfs4_slot_table *tbl;
385 	bool send_new_highest_used_slotid = false;
386 
387 	if (!res->sr_slot) {
388 		/* just wake up the next guy waiting since
389 		 * we may have not consumed a slot after all */
390 		dprintk("%s: No slot\n", __func__);
391 		return;
392 	}
393 	tbl = res->sr_slot->table;
394 	session = tbl->session;
395 
396 	spin_lock(&tbl->slot_tbl_lock);
397 	/* Be nice to the server: try to ensure that the last transmitted
398 	 * value for highest_user_slotid <= target_highest_slotid
399 	 */
400 	if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 		send_new_highest_used_slotid = true;
402 
403 	if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 		send_new_highest_used_slotid = false;
405 		goto out_unlock;
406 	}
407 	nfs4_free_slot(tbl, res->sr_slot);
408 
409 	if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 		send_new_highest_used_slotid = false;
411 out_unlock:
412 	spin_unlock(&tbl->slot_tbl_lock);
413 	res->sr_slot = NULL;
414 	if (send_new_highest_used_slotid)
415 		nfs41_server_notify_highest_slotid_update(session->clp);
416 }
417 
418 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
419 {
420 	struct nfs4_session *session;
421 	struct nfs4_slot *slot;
422 	struct nfs_client *clp;
423 	bool interrupted = false;
424 	int ret = 1;
425 
426 	/* don't increment the sequence number if the task wasn't sent */
427 	if (!RPC_WAS_SENT(task))
428 		goto out;
429 
430 	slot = res->sr_slot;
431 	session = slot->table->session;
432 
433 	if (slot->interrupted) {
434 		slot->interrupted = 0;
435 		interrupted = true;
436 	}
437 
438 	/* Check the SEQUENCE operation status */
439 	switch (res->sr_status) {
440 	case 0:
441 		/* Update the slot's sequence and clientid lease timer */
442 		++slot->seq_nr;
443 		clp = session->clp;
444 		do_renew_lease(clp, res->sr_timestamp);
445 		/* Check sequence flags */
446 		if (res->sr_status_flags != 0)
447 			nfs4_schedule_lease_recovery(clp);
448 		nfs41_update_target_slotid(slot->table, slot, res);
449 		break;
450 	case 1:
451 		/*
452 		 * sr_status remains 1 if an RPC level error occurred.
453 		 * The server may or may not have processed the sequence
454 		 * operation..
455 		 * Mark the slot as having hosted an interrupted RPC call.
456 		 */
457 		slot->interrupted = 1;
458 		goto out;
459 	case -NFS4ERR_DELAY:
460 		/* The server detected a resend of the RPC call and
461 		 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 		 * of RFC5661.
463 		 */
464 		dprintk("%s: slot=%u seq=%u: Operation in progress\n",
465 			__func__,
466 			slot->slot_nr,
467 			slot->seq_nr);
468 		goto out_retry;
469 	case -NFS4ERR_BADSLOT:
470 		/*
471 		 * The slot id we used was probably retired. Try again
472 		 * using a different slot id.
473 		 */
474 		goto retry_nowait;
475 	case -NFS4ERR_SEQ_MISORDERED:
476 		/*
477 		 * Was the last operation on this sequence interrupted?
478 		 * If so, retry after bumping the sequence number.
479 		 */
480 		if (interrupted) {
481 			++slot->seq_nr;
482 			goto retry_nowait;
483 		}
484 		/*
485 		 * Could this slot have been previously retired?
486 		 * If so, then the server may be expecting seq_nr = 1!
487 		 */
488 		if (slot->seq_nr != 1) {
489 			slot->seq_nr = 1;
490 			goto retry_nowait;
491 		}
492 		break;
493 	case -NFS4ERR_SEQ_FALSE_RETRY:
494 		++slot->seq_nr;
495 		goto retry_nowait;
496 	default:
497 		/* Just update the slot sequence no. */
498 		++slot->seq_nr;
499 	}
500 out:
501 	/* The session may be reset by one of the error handlers. */
502 	dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503 	nfs41_sequence_free_slot(res);
504 	return ret;
505 retry_nowait:
506 	if (rpc_restart_call_prepare(task)) {
507 		task->tk_status = 0;
508 		ret = 0;
509 	}
510 	goto out;
511 out_retry:
512 	if (!rpc_restart_call(task))
513 		goto out;
514 	rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 	return 0;
516 }
517 
518 static int nfs4_sequence_done(struct rpc_task *task,
519 			       struct nfs4_sequence_res *res)
520 {
521 	if (res->sr_slot == NULL)
522 		return 1;
523 	return nfs41_sequence_done(task, res);
524 }
525 
526 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 		struct nfs4_sequence_res *res, int cache_reply)
528 {
529 	args->sa_slot = NULL;
530 	args->sa_cache_this = 0;
531 	args->sa_privileged = 0;
532 	if (cache_reply)
533 		args->sa_cache_this = 1;
534 	res->sr_slot = NULL;
535 }
536 
537 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
538 {
539 	args->sa_privileged = 1;
540 }
541 
542 int nfs41_setup_sequence(struct nfs4_session *session,
543 				struct nfs4_sequence_args *args,
544 				struct nfs4_sequence_res *res,
545 				struct rpc_task *task)
546 {
547 	struct nfs4_slot *slot;
548 	struct nfs4_slot_table *tbl;
549 
550 	dprintk("--> %s\n", __func__);
551 	/* slot already allocated? */
552 	if (res->sr_slot != NULL)
553 		goto out_success;
554 
555 	tbl = &session->fc_slot_table;
556 
557 	task->tk_timeout = 0;
558 
559 	spin_lock(&tbl->slot_tbl_lock);
560 	if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
561 	    !args->sa_privileged) {
562 		/* The state manager will wait until the slot table is empty */
563 		dprintk("%s session is draining\n", __func__);
564 		goto out_sleep;
565 	}
566 
567 	slot = nfs4_alloc_slot(tbl);
568 	if (IS_ERR(slot)) {
569 		/* If out of memory, try again in 1/4 second */
570 		if (slot == ERR_PTR(-ENOMEM))
571 			task->tk_timeout = HZ >> 2;
572 		dprintk("<-- %s: no free slots\n", __func__);
573 		goto out_sleep;
574 	}
575 	spin_unlock(&tbl->slot_tbl_lock);
576 
577 	args->sa_slot = slot;
578 
579 	dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 			slot->slot_nr, slot->seq_nr);
581 
582 	res->sr_slot = slot;
583 	res->sr_timestamp = jiffies;
584 	res->sr_status_flags = 0;
585 	/*
586 	 * sr_status is only set in decode_sequence, and so will remain
587 	 * set to 1 if an rpc level failure occurs.
588 	 */
589 	res->sr_status = 1;
590 out_success:
591 	rpc_call_start(task);
592 	return 0;
593 out_sleep:
594 	/* Privileged tasks are queued with top priority */
595 	if (args->sa_privileged)
596 		rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 				NULL, RPC_PRIORITY_PRIVILEGED);
598 	else
599 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
600 	spin_unlock(&tbl->slot_tbl_lock);
601 	return -EAGAIN;
602 }
603 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
604 
605 int nfs4_setup_sequence(const struct nfs_server *server,
606 			struct nfs4_sequence_args *args,
607 			struct nfs4_sequence_res *res,
608 			struct rpc_task *task)
609 {
610 	struct nfs4_session *session = nfs4_get_session(server);
611 	int ret = 0;
612 
613 	if (session == NULL) {
614 		rpc_call_start(task);
615 		goto out;
616 	}
617 
618 	dprintk("--> %s clp %p session %p sr_slot %d\n",
619 		__func__, session->clp, session, res->sr_slot ?
620 			res->sr_slot->slot_nr : -1);
621 
622 	ret = nfs41_setup_sequence(session, args, res, task);
623 out:
624 	dprintk("<-- %s status=%d\n", __func__, ret);
625 	return ret;
626 }
627 
628 struct nfs41_call_sync_data {
629 	const struct nfs_server *seq_server;
630 	struct nfs4_sequence_args *seq_args;
631 	struct nfs4_sequence_res *seq_res;
632 };
633 
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635 {
636 	struct nfs41_call_sync_data *data = calldata;
637 	struct nfs4_session *session = nfs4_get_session(data->seq_server);
638 
639 	dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640 
641 	nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
642 }
643 
644 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
645 {
646 	struct nfs41_call_sync_data *data = calldata;
647 
648 	nfs41_sequence_done(task, data->seq_res);
649 }
650 
651 static const struct rpc_call_ops nfs41_call_sync_ops = {
652 	.rpc_call_prepare = nfs41_call_sync_prepare,
653 	.rpc_call_done = nfs41_call_sync_done,
654 };
655 
656 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 				   struct nfs_server *server,
658 				   struct rpc_message *msg,
659 				   struct nfs4_sequence_args *args,
660 				   struct nfs4_sequence_res *res)
661 {
662 	int ret;
663 	struct rpc_task *task;
664 	struct nfs41_call_sync_data data = {
665 		.seq_server = server,
666 		.seq_args = args,
667 		.seq_res = res,
668 	};
669 	struct rpc_task_setup task_setup = {
670 		.rpc_client = clnt,
671 		.rpc_message = msg,
672 		.callback_ops = &nfs41_call_sync_ops,
673 		.callback_data = &data
674 	};
675 
676 	task = rpc_run_task(&task_setup);
677 	if (IS_ERR(task))
678 		ret = PTR_ERR(task);
679 	else {
680 		ret = task->tk_status;
681 		rpc_put_task(task);
682 	}
683 	return ret;
684 }
685 
686 #else
687 static
688 void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 		struct nfs4_sequence_res *res, int cache_reply)
690 {
691 }
692 
693 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
694 {
695 }
696 
697 
698 static int nfs4_sequence_done(struct rpc_task *task,
699 			       struct nfs4_sequence_res *res)
700 {
701 	return 1;
702 }
703 #endif /* CONFIG_NFS_V4_1 */
704 
705 static
706 int _nfs4_call_sync(struct rpc_clnt *clnt,
707 		    struct nfs_server *server,
708 		    struct rpc_message *msg,
709 		    struct nfs4_sequence_args *args,
710 		    struct nfs4_sequence_res *res)
711 {
712 	return rpc_call_sync(clnt, msg, 0);
713 }
714 
715 static
716 int nfs4_call_sync(struct rpc_clnt *clnt,
717 		   struct nfs_server *server,
718 		   struct rpc_message *msg,
719 		   struct nfs4_sequence_args *args,
720 		   struct nfs4_sequence_res *res,
721 		   int cache_reply)
722 {
723 	nfs41_init_sequence(args, res, cache_reply);
724 	return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
725 						args, res);
726 }
727 
728 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
729 {
730 	struct nfs_inode *nfsi = NFS_I(dir);
731 
732 	spin_lock(&dir->i_lock);
733 	nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
734 	if (!cinfo->atomic || cinfo->before != dir->i_version)
735 		nfs_force_lookup_revalidate(dir);
736 	dir->i_version = cinfo->after;
737 	spin_unlock(&dir->i_lock);
738 }
739 
740 struct nfs4_opendata {
741 	struct kref kref;
742 	struct nfs_openargs o_arg;
743 	struct nfs_openres o_res;
744 	struct nfs_open_confirmargs c_arg;
745 	struct nfs_open_confirmres c_res;
746 	struct nfs4_string owner_name;
747 	struct nfs4_string group_name;
748 	struct nfs_fattr f_attr;
749 	struct dentry *dir;
750 	struct dentry *dentry;
751 	struct nfs4_state_owner *owner;
752 	struct nfs4_state *state;
753 	struct iattr attrs;
754 	unsigned long timestamp;
755 	unsigned int rpc_done : 1;
756 	int rpc_status;
757 	int cancelled;
758 };
759 
760 
761 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
762 {
763 	p->o_res.f_attr = &p->f_attr;
764 	p->o_res.seqid = p->o_arg.seqid;
765 	p->c_res.seqid = p->c_arg.seqid;
766 	p->o_res.server = p->o_arg.server;
767 	p->o_res.access_request = p->o_arg.access;
768 	nfs_fattr_init(&p->f_attr);
769 	nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
770 }
771 
772 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
773 		struct nfs4_state_owner *sp, fmode_t fmode, int flags,
774 		const struct iattr *attrs,
775 		gfp_t gfp_mask)
776 {
777 	struct dentry *parent = dget_parent(dentry);
778 	struct inode *dir = parent->d_inode;
779 	struct nfs_server *server = NFS_SERVER(dir);
780 	struct nfs4_opendata *p;
781 
782 	p = kzalloc(sizeof(*p), gfp_mask);
783 	if (p == NULL)
784 		goto err;
785 	p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
786 	if (p->o_arg.seqid == NULL)
787 		goto err_free;
788 	nfs_sb_active(dentry->d_sb);
789 	p->dentry = dget(dentry);
790 	p->dir = parent;
791 	p->owner = sp;
792 	atomic_inc(&sp->so_count);
793 	p->o_arg.fh = NFS_FH(dir);
794 	p->o_arg.open_flags = flags;
795 	p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
796 	/* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
797 	 * will return permission denied for all bits until close */
798 	if (!(flags & O_EXCL)) {
799 		/* ask server to check for all possible rights as results
800 		 * are cached */
801 		p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
802 				  NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
803 	}
804 	p->o_arg.clientid = server->nfs_client->cl_clientid;
805 	p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
806 	p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
807 	p->o_arg.name = &dentry->d_name;
808 	p->o_arg.server = server;
809 	p->o_arg.bitmask = server->attr_bitmask;
810 	p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
811 	p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
812 	if (attrs != NULL && attrs->ia_valid != 0) {
813 		__be32 verf[2];
814 
815 		p->o_arg.u.attrs = &p->attrs;
816 		memcpy(&p->attrs, attrs, sizeof(p->attrs));
817 
818 		verf[0] = jiffies;
819 		verf[1] = current->pid;
820 		memcpy(p->o_arg.u.verifier.data, verf,
821 				sizeof(p->o_arg.u.verifier.data));
822 	}
823 	p->c_arg.fh = &p->o_res.fh;
824 	p->c_arg.stateid = &p->o_res.stateid;
825 	p->c_arg.seqid = p->o_arg.seqid;
826 	nfs4_init_opendata_res(p);
827 	kref_init(&p->kref);
828 	return p;
829 err_free:
830 	kfree(p);
831 err:
832 	dput(parent);
833 	return NULL;
834 }
835 
836 static void nfs4_opendata_free(struct kref *kref)
837 {
838 	struct nfs4_opendata *p = container_of(kref,
839 			struct nfs4_opendata, kref);
840 	struct super_block *sb = p->dentry->d_sb;
841 
842 	nfs_free_seqid(p->o_arg.seqid);
843 	if (p->state != NULL)
844 		nfs4_put_open_state(p->state);
845 	nfs4_put_state_owner(p->owner);
846 	dput(p->dir);
847 	dput(p->dentry);
848 	nfs_sb_deactive(sb);
849 	nfs_fattr_free_names(&p->f_attr);
850 	kfree(p);
851 }
852 
853 static void nfs4_opendata_put(struct nfs4_opendata *p)
854 {
855 	if (p != NULL)
856 		kref_put(&p->kref, nfs4_opendata_free);
857 }
858 
859 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
860 {
861 	int ret;
862 
863 	ret = rpc_wait_for_completion_task(task);
864 	return ret;
865 }
866 
867 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
868 {
869 	int ret = 0;
870 
871 	if (open_mode & (O_EXCL|O_TRUNC))
872 		goto out;
873 	switch (mode & (FMODE_READ|FMODE_WRITE)) {
874 		case FMODE_READ:
875 			ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
876 				&& state->n_rdonly != 0;
877 			break;
878 		case FMODE_WRITE:
879 			ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
880 				&& state->n_wronly != 0;
881 			break;
882 		case FMODE_READ|FMODE_WRITE:
883 			ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
884 				&& state->n_rdwr != 0;
885 	}
886 out:
887 	return ret;
888 }
889 
890 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
891 {
892 	if (delegation == NULL)
893 		return 0;
894 	if ((delegation->type & fmode) != fmode)
895 		return 0;
896 	if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
897 		return 0;
898 	nfs_mark_delegation_referenced(delegation);
899 	return 1;
900 }
901 
902 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
903 {
904 	switch (fmode) {
905 		case FMODE_WRITE:
906 			state->n_wronly++;
907 			break;
908 		case FMODE_READ:
909 			state->n_rdonly++;
910 			break;
911 		case FMODE_READ|FMODE_WRITE:
912 			state->n_rdwr++;
913 	}
914 	nfs4_state_set_mode_locked(state, state->state | fmode);
915 }
916 
917 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
918 {
919 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
920 		nfs4_stateid_copy(&state->stateid, stateid);
921 	nfs4_stateid_copy(&state->open_stateid, stateid);
922 	switch (fmode) {
923 		case FMODE_READ:
924 			set_bit(NFS_O_RDONLY_STATE, &state->flags);
925 			break;
926 		case FMODE_WRITE:
927 			set_bit(NFS_O_WRONLY_STATE, &state->flags);
928 			break;
929 		case FMODE_READ|FMODE_WRITE:
930 			set_bit(NFS_O_RDWR_STATE, &state->flags);
931 	}
932 }
933 
934 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
935 {
936 	write_seqlock(&state->seqlock);
937 	nfs_set_open_stateid_locked(state, stateid, fmode);
938 	write_sequnlock(&state->seqlock);
939 }
940 
941 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
942 {
943 	/*
944 	 * Protect the call to nfs4_state_set_mode_locked and
945 	 * serialise the stateid update
946 	 */
947 	write_seqlock(&state->seqlock);
948 	if (deleg_stateid != NULL) {
949 		nfs4_stateid_copy(&state->stateid, deleg_stateid);
950 		set_bit(NFS_DELEGATED_STATE, &state->flags);
951 	}
952 	if (open_stateid != NULL)
953 		nfs_set_open_stateid_locked(state, open_stateid, fmode);
954 	write_sequnlock(&state->seqlock);
955 	spin_lock(&state->owner->so_lock);
956 	update_open_stateflags(state, fmode);
957 	spin_unlock(&state->owner->so_lock);
958 }
959 
960 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
961 {
962 	struct nfs_inode *nfsi = NFS_I(state->inode);
963 	struct nfs_delegation *deleg_cur;
964 	int ret = 0;
965 
966 	fmode &= (FMODE_READ|FMODE_WRITE);
967 
968 	rcu_read_lock();
969 	deleg_cur = rcu_dereference(nfsi->delegation);
970 	if (deleg_cur == NULL)
971 		goto no_delegation;
972 
973 	spin_lock(&deleg_cur->lock);
974 	if (nfsi->delegation != deleg_cur ||
975 	    (deleg_cur->type & fmode) != fmode)
976 		goto no_delegation_unlock;
977 
978 	if (delegation == NULL)
979 		delegation = &deleg_cur->stateid;
980 	else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
981 		goto no_delegation_unlock;
982 
983 	nfs_mark_delegation_referenced(deleg_cur);
984 	__update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
985 	ret = 1;
986 no_delegation_unlock:
987 	spin_unlock(&deleg_cur->lock);
988 no_delegation:
989 	rcu_read_unlock();
990 
991 	if (!ret && open_stateid != NULL) {
992 		__update_open_stateid(state, open_stateid, NULL, fmode);
993 		ret = 1;
994 	}
995 
996 	return ret;
997 }
998 
999 
1000 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1001 {
1002 	struct nfs_delegation *delegation;
1003 
1004 	rcu_read_lock();
1005 	delegation = rcu_dereference(NFS_I(inode)->delegation);
1006 	if (delegation == NULL || (delegation->type & fmode) == fmode) {
1007 		rcu_read_unlock();
1008 		return;
1009 	}
1010 	rcu_read_unlock();
1011 	nfs4_inode_return_delegation(inode);
1012 }
1013 
1014 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1015 {
1016 	struct nfs4_state *state = opendata->state;
1017 	struct nfs_inode *nfsi = NFS_I(state->inode);
1018 	struct nfs_delegation *delegation;
1019 	int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1020 	fmode_t fmode = opendata->o_arg.fmode;
1021 	nfs4_stateid stateid;
1022 	int ret = -EAGAIN;
1023 
1024 	for (;;) {
1025 		if (can_open_cached(state, fmode, open_mode)) {
1026 			spin_lock(&state->owner->so_lock);
1027 			if (can_open_cached(state, fmode, open_mode)) {
1028 				update_open_stateflags(state, fmode);
1029 				spin_unlock(&state->owner->so_lock);
1030 				goto out_return_state;
1031 			}
1032 			spin_unlock(&state->owner->so_lock);
1033 		}
1034 		rcu_read_lock();
1035 		delegation = rcu_dereference(nfsi->delegation);
1036 		if (!can_open_delegated(delegation, fmode)) {
1037 			rcu_read_unlock();
1038 			break;
1039 		}
1040 		/* Save the delegation */
1041 		nfs4_stateid_copy(&stateid, &delegation->stateid);
1042 		rcu_read_unlock();
1043 		ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1044 		if (ret != 0)
1045 			goto out;
1046 		ret = -EAGAIN;
1047 
1048 		/* Try to update the stateid using the delegation */
1049 		if (update_open_stateid(state, NULL, &stateid, fmode))
1050 			goto out_return_state;
1051 	}
1052 out:
1053 	return ERR_PTR(ret);
1054 out_return_state:
1055 	atomic_inc(&state->count);
1056 	return state;
1057 }
1058 
1059 static void
1060 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1061 {
1062 	struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1063 	struct nfs_delegation *delegation;
1064 	int delegation_flags = 0;
1065 
1066 	rcu_read_lock();
1067 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1068 	if (delegation)
1069 		delegation_flags = delegation->flags;
1070 	rcu_read_unlock();
1071 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1072 		pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1073 				   "returning a delegation for "
1074 				   "OPEN(CLAIM_DELEGATE_CUR)\n",
1075 				   clp->cl_hostname);
1076 	} else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1077 		nfs_inode_set_delegation(state->inode,
1078 					 data->owner->so_cred,
1079 					 &data->o_res);
1080 	else
1081 		nfs_inode_reclaim_delegation(state->inode,
1082 					     data->owner->so_cred,
1083 					     &data->o_res);
1084 }
1085 
1086 /*
1087  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1088  * and update the nfs4_state.
1089  */
1090 static struct nfs4_state *
1091 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1092 {
1093 	struct inode *inode = data->state->inode;
1094 	struct nfs4_state *state = data->state;
1095 	int ret;
1096 
1097 	if (!data->rpc_done) {
1098 		ret = data->rpc_status;
1099 		goto err;
1100 	}
1101 
1102 	ret = -ESTALE;
1103 	if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1104 	    !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1105 	    !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1106 		goto err;
1107 
1108 	ret = -ENOMEM;
1109 	state = nfs4_get_open_state(inode, data->owner);
1110 	if (state == NULL)
1111 		goto err;
1112 
1113 	ret = nfs_refresh_inode(inode, &data->f_attr);
1114 	if (ret)
1115 		goto err;
1116 
1117 	if (data->o_res.delegation_type != 0)
1118 		nfs4_opendata_check_deleg(data, state);
1119 	update_open_stateid(state, &data->o_res.stateid, NULL,
1120 			    data->o_arg.fmode);
1121 
1122 	return state;
1123 err:
1124 	return ERR_PTR(ret);
1125 
1126 }
1127 
1128 static struct nfs4_state *
1129 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1130 {
1131 	struct inode *inode;
1132 	struct nfs4_state *state = NULL;
1133 	int ret;
1134 
1135 	if (!data->rpc_done) {
1136 		state = nfs4_try_open_cached(data);
1137 		goto out;
1138 	}
1139 
1140 	ret = -EAGAIN;
1141 	if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1142 		goto err;
1143 	inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1144 	ret = PTR_ERR(inode);
1145 	if (IS_ERR(inode))
1146 		goto err;
1147 	ret = -ENOMEM;
1148 	state = nfs4_get_open_state(inode, data->owner);
1149 	if (state == NULL)
1150 		goto err_put_inode;
1151 	if (data->o_res.delegation_type != 0)
1152 		nfs4_opendata_check_deleg(data, state);
1153 	update_open_stateid(state, &data->o_res.stateid, NULL,
1154 			data->o_arg.fmode);
1155 	iput(inode);
1156 out:
1157 	return state;
1158 err_put_inode:
1159 	iput(inode);
1160 err:
1161 	return ERR_PTR(ret);
1162 }
1163 
1164 static struct nfs4_state *
1165 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1166 {
1167 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1168 		return _nfs4_opendata_reclaim_to_nfs4_state(data);
1169 	return _nfs4_opendata_to_nfs4_state(data);
1170 }
1171 
1172 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1173 {
1174 	struct nfs_inode *nfsi = NFS_I(state->inode);
1175 	struct nfs_open_context *ctx;
1176 
1177 	spin_lock(&state->inode->i_lock);
1178 	list_for_each_entry(ctx, &nfsi->open_files, list) {
1179 		if (ctx->state != state)
1180 			continue;
1181 		get_nfs_open_context(ctx);
1182 		spin_unlock(&state->inode->i_lock);
1183 		return ctx;
1184 	}
1185 	spin_unlock(&state->inode->i_lock);
1186 	return ERR_PTR(-ENOENT);
1187 }
1188 
1189 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1190 {
1191 	struct nfs4_opendata *opendata;
1192 
1193 	opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1194 	if (opendata == NULL)
1195 		return ERR_PTR(-ENOMEM);
1196 	opendata->state = state;
1197 	atomic_inc(&state->count);
1198 	return opendata;
1199 }
1200 
1201 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1202 {
1203 	struct nfs4_state *newstate;
1204 	int ret;
1205 
1206 	opendata->o_arg.open_flags = 0;
1207 	opendata->o_arg.fmode = fmode;
1208 	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1209 	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1210 	nfs4_init_opendata_res(opendata);
1211 	ret = _nfs4_recover_proc_open(opendata);
1212 	if (ret != 0)
1213 		return ret;
1214 	newstate = nfs4_opendata_to_nfs4_state(opendata);
1215 	if (IS_ERR(newstate))
1216 		return PTR_ERR(newstate);
1217 	nfs4_close_state(newstate, fmode);
1218 	*res = newstate;
1219 	return 0;
1220 }
1221 
1222 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1223 {
1224 	struct nfs4_state *newstate;
1225 	int ret;
1226 
1227 	/* memory barrier prior to reading state->n_* */
1228 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1229 	smp_rmb();
1230 	if (state->n_rdwr != 0) {
1231 		clear_bit(NFS_O_RDWR_STATE, &state->flags);
1232 		ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1233 		if (ret != 0)
1234 			return ret;
1235 		if (newstate != state)
1236 			return -ESTALE;
1237 	}
1238 	if (state->n_wronly != 0) {
1239 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1240 		ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1241 		if (ret != 0)
1242 			return ret;
1243 		if (newstate != state)
1244 			return -ESTALE;
1245 	}
1246 	if (state->n_rdonly != 0) {
1247 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1248 		ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1249 		if (ret != 0)
1250 			return ret;
1251 		if (newstate != state)
1252 			return -ESTALE;
1253 	}
1254 	/*
1255 	 * We may have performed cached opens for all three recoveries.
1256 	 * Check if we need to update the current stateid.
1257 	 */
1258 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1259 	    !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1260 		write_seqlock(&state->seqlock);
1261 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1262 			nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1263 		write_sequnlock(&state->seqlock);
1264 	}
1265 	return 0;
1266 }
1267 
1268 /*
1269  * OPEN_RECLAIM:
1270  * 	reclaim state on the server after a reboot.
1271  */
1272 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1273 {
1274 	struct nfs_delegation *delegation;
1275 	struct nfs4_opendata *opendata;
1276 	fmode_t delegation_type = 0;
1277 	int status;
1278 
1279 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1280 	if (IS_ERR(opendata))
1281 		return PTR_ERR(opendata);
1282 	opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1283 	opendata->o_arg.fh = NFS_FH(state->inode);
1284 	rcu_read_lock();
1285 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1286 	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1287 		delegation_type = delegation->type;
1288 	rcu_read_unlock();
1289 	opendata->o_arg.u.delegation_type = delegation_type;
1290 	status = nfs4_open_recover(opendata, state);
1291 	nfs4_opendata_put(opendata);
1292 	return status;
1293 }
1294 
1295 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1296 {
1297 	struct nfs_server *server = NFS_SERVER(state->inode);
1298 	struct nfs4_exception exception = { };
1299 	int err;
1300 	do {
1301 		err = _nfs4_do_open_reclaim(ctx, state);
1302 		if (err != -NFS4ERR_DELAY)
1303 			break;
1304 		nfs4_handle_exception(server, err, &exception);
1305 	} while (exception.retry);
1306 	return err;
1307 }
1308 
1309 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1310 {
1311 	struct nfs_open_context *ctx;
1312 	int ret;
1313 
1314 	ctx = nfs4_state_find_open_context(state);
1315 	if (IS_ERR(ctx))
1316 		return PTR_ERR(ctx);
1317 	ret = nfs4_do_open_reclaim(ctx, state);
1318 	put_nfs_open_context(ctx);
1319 	return ret;
1320 }
1321 
1322 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1323 {
1324 	struct nfs4_opendata *opendata;
1325 	int ret;
1326 
1327 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1328 	if (IS_ERR(opendata))
1329 		return PTR_ERR(opendata);
1330 	opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1331 	nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1332 	ret = nfs4_open_recover(opendata, state);
1333 	nfs4_opendata_put(opendata);
1334 	return ret;
1335 }
1336 
1337 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1338 {
1339 	struct nfs4_exception exception = { };
1340 	struct nfs_server *server = NFS_SERVER(state->inode);
1341 	int err;
1342 	do {
1343 		err = _nfs4_open_delegation_recall(ctx, state, stateid);
1344 		switch (err) {
1345 			case 0:
1346 			case -ENOENT:
1347 			case -ESTALE:
1348 				goto out;
1349 			case -NFS4ERR_BADSESSION:
1350 			case -NFS4ERR_BADSLOT:
1351 			case -NFS4ERR_BAD_HIGH_SLOT:
1352 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1353 			case -NFS4ERR_DEADSESSION:
1354 				nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1355 				goto out;
1356 			case -NFS4ERR_STALE_CLIENTID:
1357 			case -NFS4ERR_STALE_STATEID:
1358 			case -NFS4ERR_EXPIRED:
1359 				/* Don't recall a delegation if it was lost */
1360 				nfs4_schedule_lease_recovery(server->nfs_client);
1361 				goto out;
1362 			case -ERESTARTSYS:
1363 				/*
1364 				 * The show must go on: exit, but mark the
1365 				 * stateid as needing recovery.
1366 				 */
1367 			case -NFS4ERR_DELEG_REVOKED:
1368 			case -NFS4ERR_ADMIN_REVOKED:
1369 			case -NFS4ERR_BAD_STATEID:
1370 				nfs_inode_find_state_and_recover(state->inode,
1371 						stateid);
1372 				nfs4_schedule_stateid_recovery(server, state);
1373 			case -ENOMEM:
1374 				err = 0;
1375 				goto out;
1376 		}
1377 		err = nfs4_handle_exception(server, err, &exception);
1378 	} while (exception.retry);
1379 out:
1380 	return err;
1381 }
1382 
1383 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1384 {
1385 	struct nfs4_opendata *data = calldata;
1386 
1387 	data->rpc_status = task->tk_status;
1388 	if (data->rpc_status == 0) {
1389 		nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1390 		nfs_confirm_seqid(&data->owner->so_seqid, 0);
1391 		renew_lease(data->o_res.server, data->timestamp);
1392 		data->rpc_done = 1;
1393 	}
1394 }
1395 
1396 static void nfs4_open_confirm_release(void *calldata)
1397 {
1398 	struct nfs4_opendata *data = calldata;
1399 	struct nfs4_state *state = NULL;
1400 
1401 	/* If this request hasn't been cancelled, do nothing */
1402 	if (data->cancelled == 0)
1403 		goto out_free;
1404 	/* In case of error, no cleanup! */
1405 	if (!data->rpc_done)
1406 		goto out_free;
1407 	state = nfs4_opendata_to_nfs4_state(data);
1408 	if (!IS_ERR(state))
1409 		nfs4_close_state(state, data->o_arg.fmode);
1410 out_free:
1411 	nfs4_opendata_put(data);
1412 }
1413 
1414 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1415 	.rpc_call_done = nfs4_open_confirm_done,
1416 	.rpc_release = nfs4_open_confirm_release,
1417 };
1418 
1419 /*
1420  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1421  */
1422 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1423 {
1424 	struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1425 	struct rpc_task *task;
1426 	struct  rpc_message msg = {
1427 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1428 		.rpc_argp = &data->c_arg,
1429 		.rpc_resp = &data->c_res,
1430 		.rpc_cred = data->owner->so_cred,
1431 	};
1432 	struct rpc_task_setup task_setup_data = {
1433 		.rpc_client = server->client,
1434 		.rpc_message = &msg,
1435 		.callback_ops = &nfs4_open_confirm_ops,
1436 		.callback_data = data,
1437 		.workqueue = nfsiod_workqueue,
1438 		.flags = RPC_TASK_ASYNC,
1439 	};
1440 	int status;
1441 
1442 	kref_get(&data->kref);
1443 	data->rpc_done = 0;
1444 	data->rpc_status = 0;
1445 	data->timestamp = jiffies;
1446 	task = rpc_run_task(&task_setup_data);
1447 	if (IS_ERR(task))
1448 		return PTR_ERR(task);
1449 	status = nfs4_wait_for_completion_rpc_task(task);
1450 	if (status != 0) {
1451 		data->cancelled = 1;
1452 		smp_wmb();
1453 	} else
1454 		status = data->rpc_status;
1455 	rpc_put_task(task);
1456 	return status;
1457 }
1458 
1459 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1460 {
1461 	struct nfs4_opendata *data = calldata;
1462 	struct nfs4_state_owner *sp = data->owner;
1463 
1464 	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1465 		return;
1466 	/*
1467 	 * Check if we still need to send an OPEN call, or if we can use
1468 	 * a delegation instead.
1469 	 */
1470 	if (data->state != NULL) {
1471 		struct nfs_delegation *delegation;
1472 
1473 		if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1474 			goto out_no_action;
1475 		rcu_read_lock();
1476 		delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1477 		if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1478 		    can_open_delegated(delegation, data->o_arg.fmode))
1479 			goto unlock_no_action;
1480 		rcu_read_unlock();
1481 	}
1482 	/* Update client id. */
1483 	data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1484 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1485 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1486 		data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1487 		nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1488 	}
1489 	data->timestamp = jiffies;
1490 	if (nfs4_setup_sequence(data->o_arg.server,
1491 				&data->o_arg.seq_args,
1492 				&data->o_res.seq_res,
1493 				task) != 0)
1494 		nfs_release_seqid(data->o_arg.seqid);
1495 	return;
1496 unlock_no_action:
1497 	rcu_read_unlock();
1498 out_no_action:
1499 	task->tk_action = NULL;
1500 	nfs4_sequence_done(task, &data->o_res.seq_res);
1501 }
1502 
1503 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1504 {
1505 	struct nfs4_opendata *data = calldata;
1506 
1507 	data->rpc_status = task->tk_status;
1508 
1509 	if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1510 		return;
1511 
1512 	if (task->tk_status == 0) {
1513 		if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1514 			switch (data->o_res.f_attr->mode & S_IFMT) {
1515 			case S_IFREG:
1516 				break;
1517 			case S_IFLNK:
1518 				data->rpc_status = -ELOOP;
1519 				break;
1520 			case S_IFDIR:
1521 				data->rpc_status = -EISDIR;
1522 				break;
1523 			default:
1524 				data->rpc_status = -ENOTDIR;
1525 			}
1526 		}
1527 		renew_lease(data->o_res.server, data->timestamp);
1528 		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1529 			nfs_confirm_seqid(&data->owner->so_seqid, 0);
1530 	}
1531 	data->rpc_done = 1;
1532 }
1533 
1534 static void nfs4_open_release(void *calldata)
1535 {
1536 	struct nfs4_opendata *data = calldata;
1537 	struct nfs4_state *state = NULL;
1538 
1539 	/* If this request hasn't been cancelled, do nothing */
1540 	if (data->cancelled == 0)
1541 		goto out_free;
1542 	/* In case of error, no cleanup! */
1543 	if (data->rpc_status != 0 || !data->rpc_done)
1544 		goto out_free;
1545 	/* In case we need an open_confirm, no cleanup! */
1546 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1547 		goto out_free;
1548 	state = nfs4_opendata_to_nfs4_state(data);
1549 	if (!IS_ERR(state))
1550 		nfs4_close_state(state, data->o_arg.fmode);
1551 out_free:
1552 	nfs4_opendata_put(data);
1553 }
1554 
1555 static const struct rpc_call_ops nfs4_open_ops = {
1556 	.rpc_call_prepare = nfs4_open_prepare,
1557 	.rpc_call_done = nfs4_open_done,
1558 	.rpc_release = nfs4_open_release,
1559 };
1560 
1561 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1562 {
1563 	struct inode *dir = data->dir->d_inode;
1564 	struct nfs_server *server = NFS_SERVER(dir);
1565 	struct nfs_openargs *o_arg = &data->o_arg;
1566 	struct nfs_openres *o_res = &data->o_res;
1567 	struct rpc_task *task;
1568 	struct rpc_message msg = {
1569 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1570 		.rpc_argp = o_arg,
1571 		.rpc_resp = o_res,
1572 		.rpc_cred = data->owner->so_cred,
1573 	};
1574 	struct rpc_task_setup task_setup_data = {
1575 		.rpc_client = server->client,
1576 		.rpc_message = &msg,
1577 		.callback_ops = &nfs4_open_ops,
1578 		.callback_data = data,
1579 		.workqueue = nfsiod_workqueue,
1580 		.flags = RPC_TASK_ASYNC,
1581 	};
1582 	int status;
1583 
1584 	nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1585 	kref_get(&data->kref);
1586 	data->rpc_done = 0;
1587 	data->rpc_status = 0;
1588 	data->cancelled = 0;
1589 	if (isrecover)
1590 		nfs4_set_sequence_privileged(&o_arg->seq_args);
1591 	task = rpc_run_task(&task_setup_data);
1592         if (IS_ERR(task))
1593                 return PTR_ERR(task);
1594         status = nfs4_wait_for_completion_rpc_task(task);
1595         if (status != 0) {
1596                 data->cancelled = 1;
1597                 smp_wmb();
1598         } else
1599                 status = data->rpc_status;
1600         rpc_put_task(task);
1601 
1602 	return status;
1603 }
1604 
1605 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1606 {
1607 	struct inode *dir = data->dir->d_inode;
1608 	struct nfs_openres *o_res = &data->o_res;
1609         int status;
1610 
1611 	status = nfs4_run_open_task(data, 1);
1612 	if (status != 0 || !data->rpc_done)
1613 		return status;
1614 
1615 	nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1616 
1617 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618 		status = _nfs4_proc_open_confirm(data);
1619 		if (status != 0)
1620 			return status;
1621 	}
1622 
1623 	return status;
1624 }
1625 
1626 static int nfs4_opendata_access(struct rpc_cred *cred,
1627 				struct nfs4_opendata *opendata,
1628 				struct nfs4_state *state, fmode_t fmode)
1629 {
1630 	struct nfs_access_entry cache;
1631 	u32 mask;
1632 
1633 	/* access call failed or for some reason the server doesn't
1634 	 * support any access modes -- defer access call until later */
1635 	if (opendata->o_res.access_supported == 0)
1636 		return 0;
1637 
1638 	mask = 0;
1639 	/* don't check MAY_WRITE - a newly created file may not have
1640 	 * write mode bits, but POSIX allows the creating process to write */
1641 	if (fmode & FMODE_READ)
1642 		mask |= MAY_READ;
1643 	if (fmode & FMODE_EXEC)
1644 		mask |= MAY_EXEC;
1645 
1646 	cache.cred = cred;
1647 	cache.jiffies = jiffies;
1648 	nfs_access_set_mask(&cache, opendata->o_res.access_result);
1649 	nfs_access_add_cache(state->inode, &cache);
1650 
1651 	if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1652 		return 0;
1653 
1654 	/* even though OPEN succeeded, access is denied. Close the file */
1655 	nfs4_close_state(state, fmode);
1656 	return -EACCES;
1657 }
1658 
1659 /*
1660  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1661  */
1662 static int _nfs4_proc_open(struct nfs4_opendata *data)
1663 {
1664 	struct inode *dir = data->dir->d_inode;
1665 	struct nfs_server *server = NFS_SERVER(dir);
1666 	struct nfs_openargs *o_arg = &data->o_arg;
1667 	struct nfs_openres *o_res = &data->o_res;
1668 	int status;
1669 
1670 	status = nfs4_run_open_task(data, 0);
1671 	if (!data->rpc_done)
1672 		return status;
1673 	if (status != 0) {
1674 		if (status == -NFS4ERR_BADNAME &&
1675 				!(o_arg->open_flags & O_CREAT))
1676 			return -ENOENT;
1677 		return status;
1678 	}
1679 
1680 	nfs_fattr_map_and_free_names(server, &data->f_attr);
1681 
1682 	if (o_arg->open_flags & O_CREAT)
1683 		update_changeattr(dir, &o_res->cinfo);
1684 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1685 		server->caps &= ~NFS_CAP_POSIX_LOCK;
1686 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1687 		status = _nfs4_proc_open_confirm(data);
1688 		if (status != 0)
1689 			return status;
1690 	}
1691 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1692 		_nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1693 	return 0;
1694 }
1695 
1696 static int nfs4_recover_expired_lease(struct nfs_server *server)
1697 {
1698 	return nfs4_client_recover_expired_lease(server->nfs_client);
1699 }
1700 
1701 /*
1702  * OPEN_EXPIRED:
1703  * 	reclaim state on the server after a network partition.
1704  * 	Assumes caller holds the appropriate lock
1705  */
1706 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1707 {
1708 	struct nfs4_opendata *opendata;
1709 	int ret;
1710 
1711 	opendata = nfs4_open_recoverdata_alloc(ctx, state);
1712 	if (IS_ERR(opendata))
1713 		return PTR_ERR(opendata);
1714 	ret = nfs4_open_recover(opendata, state);
1715 	if (ret == -ESTALE)
1716 		d_drop(ctx->dentry);
1717 	nfs4_opendata_put(opendata);
1718 	return ret;
1719 }
1720 
1721 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1722 {
1723 	struct nfs_server *server = NFS_SERVER(state->inode);
1724 	struct nfs4_exception exception = { };
1725 	int err;
1726 
1727 	do {
1728 		err = _nfs4_open_expired(ctx, state);
1729 		switch (err) {
1730 		default:
1731 			goto out;
1732 		case -NFS4ERR_GRACE:
1733 		case -NFS4ERR_DELAY:
1734 			nfs4_handle_exception(server, err, &exception);
1735 			err = 0;
1736 		}
1737 	} while (exception.retry);
1738 out:
1739 	return err;
1740 }
1741 
1742 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1743 {
1744 	struct nfs_open_context *ctx;
1745 	int ret;
1746 
1747 	ctx = nfs4_state_find_open_context(state);
1748 	if (IS_ERR(ctx))
1749 		return PTR_ERR(ctx);
1750 	ret = nfs4_do_open_expired(ctx, state);
1751 	put_nfs_open_context(ctx);
1752 	return ret;
1753 }
1754 
1755 #if defined(CONFIG_NFS_V4_1)
1756 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1757 {
1758 	struct nfs_server *server = NFS_SERVER(state->inode);
1759 	nfs4_stateid *stateid = &state->stateid;
1760 	int status;
1761 
1762 	/* If a state reset has been done, test_stateid is unneeded */
1763 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1764 		return;
1765 
1766 	status = nfs41_test_stateid(server, stateid);
1767 	if (status != NFS_OK) {
1768 		/* Free the stateid unless the server explicitly
1769 		 * informs us the stateid is unrecognized. */
1770 		if (status != -NFS4ERR_BAD_STATEID)
1771 			nfs41_free_stateid(server, stateid);
1772 		nfs_remove_bad_delegation(state->inode);
1773 
1774 		write_seqlock(&state->seqlock);
1775 		nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1776 		write_sequnlock(&state->seqlock);
1777 		clear_bit(NFS_DELEGATED_STATE, &state->flags);
1778 	}
1779 }
1780 
1781 /**
1782  * nfs41_check_open_stateid - possibly free an open stateid
1783  *
1784  * @state: NFSv4 state for an inode
1785  *
1786  * Returns NFS_OK if recovery for this stateid is now finished.
1787  * Otherwise a negative NFS4ERR value is returned.
1788  */
1789 static int nfs41_check_open_stateid(struct nfs4_state *state)
1790 {
1791 	struct nfs_server *server = NFS_SERVER(state->inode);
1792 	nfs4_stateid *stateid = &state->open_stateid;
1793 	int status;
1794 
1795 	/* If a state reset has been done, test_stateid is unneeded */
1796 	if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1797 	    (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1798 	    (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1799 		return -NFS4ERR_BAD_STATEID;
1800 
1801 	status = nfs41_test_stateid(server, stateid);
1802 	if (status != NFS_OK) {
1803 		/* Free the stateid unless the server explicitly
1804 		 * informs us the stateid is unrecognized. */
1805 		if (status != -NFS4ERR_BAD_STATEID)
1806 			nfs41_free_stateid(server, stateid);
1807 
1808 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1809 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1810 		clear_bit(NFS_O_RDWR_STATE, &state->flags);
1811 	}
1812 	return status;
1813 }
1814 
1815 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1816 {
1817 	int status;
1818 
1819 	nfs41_clear_delegation_stateid(state);
1820 	status = nfs41_check_open_stateid(state);
1821 	if (status != NFS_OK)
1822 		status = nfs4_open_expired(sp, state);
1823 	return status;
1824 }
1825 #endif
1826 
1827 /*
1828  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1829  * fields corresponding to attributes that were used to store the verifier.
1830  * Make sure we clobber those fields in the later setattr call
1831  */
1832 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1833 {
1834 	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1835 	    !(sattr->ia_valid & ATTR_ATIME_SET))
1836 		sattr->ia_valid |= ATTR_ATIME;
1837 
1838 	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1839 	    !(sattr->ia_valid & ATTR_MTIME_SET))
1840 		sattr->ia_valid |= ATTR_MTIME;
1841 }
1842 
1843 /*
1844  * Returns a referenced nfs4_state
1845  */
1846 static int _nfs4_do_open(struct inode *dir,
1847 			struct dentry *dentry,
1848 			fmode_t fmode,
1849 			int flags,
1850 			struct iattr *sattr,
1851 			struct rpc_cred *cred,
1852 			struct nfs4_state **res,
1853 			struct nfs4_threshold **ctx_th)
1854 {
1855 	struct nfs4_state_owner  *sp;
1856 	struct nfs4_state     *state = NULL;
1857 	struct nfs_server       *server = NFS_SERVER(dir);
1858 	struct nfs4_opendata *opendata;
1859 	int status;
1860 
1861 	/* Protect against reboot recovery conflicts */
1862 	status = -ENOMEM;
1863 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1864 	if (sp == NULL) {
1865 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1866 		goto out_err;
1867 	}
1868 	status = nfs4_recover_expired_lease(server);
1869 	if (status != 0)
1870 		goto err_put_state_owner;
1871 	if (dentry->d_inode != NULL)
1872 		nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1873 	status = -ENOMEM;
1874 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1875 	if (opendata == NULL)
1876 		goto err_put_state_owner;
1877 
1878 	if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1879 		opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1880 		if (!opendata->f_attr.mdsthreshold)
1881 			goto err_opendata_put;
1882 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1883 	}
1884 	if (dentry->d_inode != NULL)
1885 		opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1886 
1887 	status = _nfs4_proc_open(opendata);
1888 	if (status != 0)
1889 		goto err_opendata_put;
1890 
1891 	state = nfs4_opendata_to_nfs4_state(opendata);
1892 	status = PTR_ERR(state);
1893 	if (IS_ERR(state))
1894 		goto err_opendata_put;
1895 	if (server->caps & NFS_CAP_POSIX_LOCK)
1896 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1897 
1898 	status = nfs4_opendata_access(cred, opendata, state, fmode);
1899 	if (status != 0)
1900 		goto err_opendata_put;
1901 
1902 	if (opendata->o_arg.open_flags & O_EXCL) {
1903 		nfs4_exclusive_attrset(opendata, sattr);
1904 
1905 		nfs_fattr_init(opendata->o_res.f_attr);
1906 		status = nfs4_do_setattr(state->inode, cred,
1907 				opendata->o_res.f_attr, sattr,
1908 				state);
1909 		if (status == 0)
1910 			nfs_setattr_update_inode(state->inode, sattr);
1911 		nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1912 	}
1913 
1914 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1915 		*ctx_th = opendata->f_attr.mdsthreshold;
1916 	else
1917 		kfree(opendata->f_attr.mdsthreshold);
1918 	opendata->f_attr.mdsthreshold = NULL;
1919 
1920 	nfs4_opendata_put(opendata);
1921 	nfs4_put_state_owner(sp);
1922 	*res = state;
1923 	return 0;
1924 err_opendata_put:
1925 	kfree(opendata->f_attr.mdsthreshold);
1926 	nfs4_opendata_put(opendata);
1927 err_put_state_owner:
1928 	nfs4_put_state_owner(sp);
1929 out_err:
1930 	*res = NULL;
1931 	return status;
1932 }
1933 
1934 
1935 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1936 					struct dentry *dentry,
1937 					fmode_t fmode,
1938 					int flags,
1939 					struct iattr *sattr,
1940 					struct rpc_cred *cred,
1941 					struct nfs4_threshold **ctx_th)
1942 {
1943 	struct nfs4_exception exception = { };
1944 	struct nfs4_state *res;
1945 	int status;
1946 
1947 	fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1948 	do {
1949 		status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1950 				       &res, ctx_th);
1951 		if (status == 0)
1952 			break;
1953 		/* NOTE: BAD_SEQID means the server and client disagree about the
1954 		 * book-keeping w.r.t. state-changing operations
1955 		 * (OPEN/CLOSE/LOCK/LOCKU...)
1956 		 * It is actually a sign of a bug on the client or on the server.
1957 		 *
1958 		 * If we receive a BAD_SEQID error in the particular case of
1959 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1960 		 * have unhashed the old state_owner for us, and that we can
1961 		 * therefore safely retry using a new one. We should still warn
1962 		 * the user though...
1963 		 */
1964 		if (status == -NFS4ERR_BAD_SEQID) {
1965 			pr_warn_ratelimited("NFS: v4 server %s "
1966 					" returned a bad sequence-id error!\n",
1967 					NFS_SERVER(dir)->nfs_client->cl_hostname);
1968 			exception.retry = 1;
1969 			continue;
1970 		}
1971 		/*
1972 		 * BAD_STATEID on OPEN means that the server cancelled our
1973 		 * state before it received the OPEN_CONFIRM.
1974 		 * Recover by retrying the request as per the discussion
1975 		 * on Page 181 of RFC3530.
1976 		 */
1977 		if (status == -NFS4ERR_BAD_STATEID) {
1978 			exception.retry = 1;
1979 			continue;
1980 		}
1981 		if (status == -EAGAIN) {
1982 			/* We must have found a delegation */
1983 			exception.retry = 1;
1984 			continue;
1985 		}
1986 		res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1987 					status, &exception));
1988 	} while (exception.retry);
1989 	return res;
1990 }
1991 
1992 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1993 			    struct nfs_fattr *fattr, struct iattr *sattr,
1994 			    struct nfs4_state *state)
1995 {
1996 	struct nfs_server *server = NFS_SERVER(inode);
1997         struct nfs_setattrargs  arg = {
1998                 .fh             = NFS_FH(inode),
1999                 .iap            = sattr,
2000 		.server		= server,
2001 		.bitmask = server->attr_bitmask,
2002         };
2003         struct nfs_setattrres  res = {
2004 		.fattr		= fattr,
2005 		.server		= server,
2006         };
2007         struct rpc_message msg = {
2008 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2009 		.rpc_argp	= &arg,
2010 		.rpc_resp	= &res,
2011 		.rpc_cred	= cred,
2012         };
2013 	unsigned long timestamp = jiffies;
2014 	int status;
2015 
2016 	nfs_fattr_init(fattr);
2017 
2018 	if (state != NULL) {
2019 		struct nfs_lockowner lockowner = {
2020 			.l_owner = current->files,
2021 			.l_pid = current->tgid,
2022 		};
2023 		nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2024 				&lockowner);
2025 	} else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2026 				FMODE_WRITE)) {
2027 		/* Use that stateid */
2028 	} else
2029 		nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2030 
2031 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2032 	if (status == 0 && state != NULL)
2033 		renew_lease(server, timestamp);
2034 	return status;
2035 }
2036 
2037 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2038 			   struct nfs_fattr *fattr, struct iattr *sattr,
2039 			   struct nfs4_state *state)
2040 {
2041 	struct nfs_server *server = NFS_SERVER(inode);
2042 	struct nfs4_exception exception = {
2043 		.state = state,
2044 		.inode = inode,
2045 	};
2046 	int err;
2047 	do {
2048 		err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2049 		switch (err) {
2050 		case -NFS4ERR_OPENMODE:
2051 			if (state && !(state->state & FMODE_WRITE)) {
2052 				err = -EBADF;
2053 				if (sattr->ia_valid & ATTR_OPEN)
2054 					err = -EACCES;
2055 				goto out;
2056 			}
2057 		}
2058 		err = nfs4_handle_exception(server, err, &exception);
2059 	} while (exception.retry);
2060 out:
2061 	return err;
2062 }
2063 
2064 struct nfs4_closedata {
2065 	struct inode *inode;
2066 	struct nfs4_state *state;
2067 	struct nfs_closeargs arg;
2068 	struct nfs_closeres res;
2069 	struct nfs_fattr fattr;
2070 	unsigned long timestamp;
2071 	bool roc;
2072 	u32 roc_barrier;
2073 };
2074 
2075 static void nfs4_free_closedata(void *data)
2076 {
2077 	struct nfs4_closedata *calldata = data;
2078 	struct nfs4_state_owner *sp = calldata->state->owner;
2079 	struct super_block *sb = calldata->state->inode->i_sb;
2080 
2081 	if (calldata->roc)
2082 		pnfs_roc_release(calldata->state->inode);
2083 	nfs4_put_open_state(calldata->state);
2084 	nfs_free_seqid(calldata->arg.seqid);
2085 	nfs4_put_state_owner(sp);
2086 	nfs_sb_deactive_async(sb);
2087 	kfree(calldata);
2088 }
2089 
2090 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2091 		fmode_t fmode)
2092 {
2093 	spin_lock(&state->owner->so_lock);
2094 	if (!(fmode & FMODE_READ))
2095 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2096 	if (!(fmode & FMODE_WRITE))
2097 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2098 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
2099 	spin_unlock(&state->owner->so_lock);
2100 }
2101 
2102 static void nfs4_close_done(struct rpc_task *task, void *data)
2103 {
2104 	struct nfs4_closedata *calldata = data;
2105 	struct nfs4_state *state = calldata->state;
2106 	struct nfs_server *server = NFS_SERVER(calldata->inode);
2107 
2108 	dprintk("%s: begin!\n", __func__);
2109 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2110 		return;
2111         /* hmm. we are done with the inode, and in the process of freeing
2112 	 * the state_owner. we keep this around to process errors
2113 	 */
2114 	switch (task->tk_status) {
2115 		case 0:
2116 			if (calldata->roc)
2117 				pnfs_roc_set_barrier(state->inode,
2118 						     calldata->roc_barrier);
2119 			nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2120 			renew_lease(server, calldata->timestamp);
2121 			nfs4_close_clear_stateid_flags(state,
2122 					calldata->arg.fmode);
2123 			break;
2124 		case -NFS4ERR_STALE_STATEID:
2125 		case -NFS4ERR_OLD_STATEID:
2126 		case -NFS4ERR_BAD_STATEID:
2127 		case -NFS4ERR_EXPIRED:
2128 			if (calldata->arg.fmode == 0)
2129 				break;
2130 		default:
2131 			if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2132 				rpc_restart_call_prepare(task);
2133 	}
2134 	nfs_release_seqid(calldata->arg.seqid);
2135 	nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2136 	dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2137 }
2138 
2139 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2140 {
2141 	struct nfs4_closedata *calldata = data;
2142 	struct nfs4_state *state = calldata->state;
2143 	struct inode *inode = calldata->inode;
2144 	int call_close = 0;
2145 
2146 	dprintk("%s: begin!\n", __func__);
2147 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2148 		return;
2149 
2150 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2151 	calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2152 	spin_lock(&state->owner->so_lock);
2153 	/* Calculate the change in open mode */
2154 	if (state->n_rdwr == 0) {
2155 		if (state->n_rdonly == 0) {
2156 			call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2157 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2158 			calldata->arg.fmode &= ~FMODE_READ;
2159 		}
2160 		if (state->n_wronly == 0) {
2161 			call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2162 			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2163 			calldata->arg.fmode &= ~FMODE_WRITE;
2164 		}
2165 	}
2166 	spin_unlock(&state->owner->so_lock);
2167 
2168 	if (!call_close) {
2169 		/* Note: exit _without_ calling nfs4_close_done */
2170 		task->tk_action = NULL;
2171 		nfs4_sequence_done(task, &calldata->res.seq_res);
2172 		goto out;
2173 	}
2174 
2175 	if (calldata->arg.fmode == 0) {
2176 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2177 		if (calldata->roc &&
2178 		    pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2179 			goto out;
2180 	}
2181 
2182 	nfs_fattr_init(calldata->res.fattr);
2183 	calldata->timestamp = jiffies;
2184 	if (nfs4_setup_sequence(NFS_SERVER(inode),
2185 				&calldata->arg.seq_args,
2186 				&calldata->res.seq_res,
2187 				task) != 0)
2188 		nfs_release_seqid(calldata->arg.seqid);
2189 out:
2190 	dprintk("%s: done!\n", __func__);
2191 }
2192 
2193 static const struct rpc_call_ops nfs4_close_ops = {
2194 	.rpc_call_prepare = nfs4_close_prepare,
2195 	.rpc_call_done = nfs4_close_done,
2196 	.rpc_release = nfs4_free_closedata,
2197 };
2198 
2199 /*
2200  * It is possible for data to be read/written from a mem-mapped file
2201  * after the sys_close call (which hits the vfs layer as a flush).
2202  * This means that we can't safely call nfsv4 close on a file until
2203  * the inode is cleared. This in turn means that we are not good
2204  * NFSv4 citizens - we do not indicate to the server to update the file's
2205  * share state even when we are done with one of the three share
2206  * stateid's in the inode.
2207  *
2208  * NOTE: Caller must be holding the sp->so_owner semaphore!
2209  */
2210 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2211 {
2212 	struct nfs_server *server = NFS_SERVER(state->inode);
2213 	struct nfs4_closedata *calldata;
2214 	struct nfs4_state_owner *sp = state->owner;
2215 	struct rpc_task *task;
2216 	struct rpc_message msg = {
2217 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2218 		.rpc_cred = state->owner->so_cred,
2219 	};
2220 	struct rpc_task_setup task_setup_data = {
2221 		.rpc_client = server->client,
2222 		.rpc_message = &msg,
2223 		.callback_ops = &nfs4_close_ops,
2224 		.workqueue = nfsiod_workqueue,
2225 		.flags = RPC_TASK_ASYNC,
2226 	};
2227 	int status = -ENOMEM;
2228 
2229 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
2230 	if (calldata == NULL)
2231 		goto out;
2232 	nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2233 	calldata->inode = state->inode;
2234 	calldata->state = state;
2235 	calldata->arg.fh = NFS_FH(state->inode);
2236 	calldata->arg.stateid = &state->open_stateid;
2237 	/* Serialization for the sequence id */
2238 	calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2239 	if (calldata->arg.seqid == NULL)
2240 		goto out_free_calldata;
2241 	calldata->arg.fmode = 0;
2242 	calldata->arg.bitmask = server->cache_consistency_bitmask;
2243 	calldata->res.fattr = &calldata->fattr;
2244 	calldata->res.seqid = calldata->arg.seqid;
2245 	calldata->res.server = server;
2246 	calldata->roc = pnfs_roc(state->inode);
2247 	nfs_sb_active(calldata->inode->i_sb);
2248 
2249 	msg.rpc_argp = &calldata->arg;
2250 	msg.rpc_resp = &calldata->res;
2251 	task_setup_data.callback_data = calldata;
2252 	task = rpc_run_task(&task_setup_data);
2253 	if (IS_ERR(task))
2254 		return PTR_ERR(task);
2255 	status = 0;
2256 	if (wait)
2257 		status = rpc_wait_for_completion_task(task);
2258 	rpc_put_task(task);
2259 	return status;
2260 out_free_calldata:
2261 	kfree(calldata);
2262 out:
2263 	nfs4_put_open_state(state);
2264 	nfs4_put_state_owner(sp);
2265 	return status;
2266 }
2267 
2268 static struct inode *
2269 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2270 {
2271 	struct nfs4_state *state;
2272 
2273 	/* Protect against concurrent sillydeletes */
2274 	state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2275 			     ctx->cred, &ctx->mdsthreshold);
2276 	if (IS_ERR(state))
2277 		return ERR_CAST(state);
2278 	ctx->state = state;
2279 	return igrab(state->inode);
2280 }
2281 
2282 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2283 {
2284 	if (ctx->state == NULL)
2285 		return;
2286 	if (is_sync)
2287 		nfs4_close_sync(ctx->state, ctx->mode);
2288 	else
2289 		nfs4_close_state(ctx->state, ctx->mode);
2290 }
2291 
2292 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2293 {
2294 	struct nfs4_server_caps_arg args = {
2295 		.fhandle = fhandle,
2296 	};
2297 	struct nfs4_server_caps_res res = {};
2298 	struct rpc_message msg = {
2299 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2300 		.rpc_argp = &args,
2301 		.rpc_resp = &res,
2302 	};
2303 	int status;
2304 
2305 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2306 	if (status == 0) {
2307 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2308 		server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2309 				NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2310 				NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2311 				NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2312 				NFS_CAP_CTIME|NFS_CAP_MTIME);
2313 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2314 			server->caps |= NFS_CAP_ACLS;
2315 		if (res.has_links != 0)
2316 			server->caps |= NFS_CAP_HARDLINKS;
2317 		if (res.has_symlinks != 0)
2318 			server->caps |= NFS_CAP_SYMLINKS;
2319 		if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2320 			server->caps |= NFS_CAP_FILEID;
2321 		if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2322 			server->caps |= NFS_CAP_MODE;
2323 		if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2324 			server->caps |= NFS_CAP_NLINK;
2325 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2326 			server->caps |= NFS_CAP_OWNER;
2327 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2328 			server->caps |= NFS_CAP_OWNER_GROUP;
2329 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2330 			server->caps |= NFS_CAP_ATIME;
2331 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2332 			server->caps |= NFS_CAP_CTIME;
2333 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2334 			server->caps |= NFS_CAP_MTIME;
2335 
2336 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2337 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2338 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2339 		server->acl_bitmask = res.acl_bitmask;
2340 		server->fh_expire_type = res.fh_expire_type;
2341 	}
2342 
2343 	return status;
2344 }
2345 
2346 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2347 {
2348 	struct nfs4_exception exception = { };
2349 	int err;
2350 	do {
2351 		err = nfs4_handle_exception(server,
2352 				_nfs4_server_capabilities(server, fhandle),
2353 				&exception);
2354 	} while (exception.retry);
2355 	return err;
2356 }
2357 
2358 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2359 		struct nfs_fsinfo *info)
2360 {
2361 	struct nfs4_lookup_root_arg args = {
2362 		.bitmask = nfs4_fattr_bitmap,
2363 	};
2364 	struct nfs4_lookup_res res = {
2365 		.server = server,
2366 		.fattr = info->fattr,
2367 		.fh = fhandle,
2368 	};
2369 	struct rpc_message msg = {
2370 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2371 		.rpc_argp = &args,
2372 		.rpc_resp = &res,
2373 	};
2374 
2375 	nfs_fattr_init(info->fattr);
2376 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2377 }
2378 
2379 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2380 		struct nfs_fsinfo *info)
2381 {
2382 	struct nfs4_exception exception = { };
2383 	int err;
2384 	do {
2385 		err = _nfs4_lookup_root(server, fhandle, info);
2386 		switch (err) {
2387 		case 0:
2388 		case -NFS4ERR_WRONGSEC:
2389 			goto out;
2390 		default:
2391 			err = nfs4_handle_exception(server, err, &exception);
2392 		}
2393 	} while (exception.retry);
2394 out:
2395 	return err;
2396 }
2397 
2398 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2399 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2400 {
2401 	struct rpc_auth *auth;
2402 	int ret;
2403 
2404 	auth = rpcauth_create(flavor, server->client);
2405 	if (IS_ERR(auth)) {
2406 		ret = -EIO;
2407 		goto out;
2408 	}
2409 	ret = nfs4_lookup_root(server, fhandle, info);
2410 out:
2411 	return ret;
2412 }
2413 
2414 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2415 			      struct nfs_fsinfo *info)
2416 {
2417 	int i, len, status = 0;
2418 	rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2419 
2420 	len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2421 	if (len < 0)
2422 		return len;
2423 
2424 	for (i = 0; i < len; i++) {
2425 		/* AUTH_UNIX is the default flavor if none was specified,
2426 		 * thus has already been tried. */
2427 		if (flav_array[i] == RPC_AUTH_UNIX)
2428 			continue;
2429 
2430 		status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2431 		if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2432 			continue;
2433 		break;
2434 	}
2435 	/*
2436 	 * -EACCESS could mean that the user doesn't have correct permissions
2437 	 * to access the mount.  It could also mean that we tried to mount
2438 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2439 	 * existing mount programs don't handle -EACCES very well so it should
2440 	 * be mapped to -EPERM instead.
2441 	 */
2442 	if (status == -EACCES)
2443 		status = -EPERM;
2444 	return status;
2445 }
2446 
2447 /*
2448  * get the file handle for the "/" directory on the server
2449  */
2450 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2451 			 struct nfs_fsinfo *info)
2452 {
2453 	int minor_version = server->nfs_client->cl_minorversion;
2454 	int status = nfs4_lookup_root(server, fhandle, info);
2455 	if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2456 		/*
2457 		 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2458 		 * by nfs4_map_errors() as this function exits.
2459 		 */
2460 		status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2461 	if (status == 0)
2462 		status = nfs4_server_capabilities(server, fhandle);
2463 	if (status == 0)
2464 		status = nfs4_do_fsinfo(server, fhandle, info);
2465 	return nfs4_map_errors(status);
2466 }
2467 
2468 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2469 			      struct nfs_fsinfo *info)
2470 {
2471 	int error;
2472 	struct nfs_fattr *fattr = info->fattr;
2473 
2474 	error = nfs4_server_capabilities(server, mntfh);
2475 	if (error < 0) {
2476 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2477 		return error;
2478 	}
2479 
2480 	error = nfs4_proc_getattr(server, mntfh, fattr);
2481 	if (error < 0) {
2482 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
2483 		return error;
2484 	}
2485 
2486 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2487 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2488 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2489 
2490 	return error;
2491 }
2492 
2493 /*
2494  * Get locations and (maybe) other attributes of a referral.
2495  * Note that we'll actually follow the referral later when
2496  * we detect fsid mismatch in inode revalidation
2497  */
2498 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2499 			     const struct qstr *name, struct nfs_fattr *fattr,
2500 			     struct nfs_fh *fhandle)
2501 {
2502 	int status = -ENOMEM;
2503 	struct page *page = NULL;
2504 	struct nfs4_fs_locations *locations = NULL;
2505 
2506 	page = alloc_page(GFP_KERNEL);
2507 	if (page == NULL)
2508 		goto out;
2509 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2510 	if (locations == NULL)
2511 		goto out;
2512 
2513 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2514 	if (status != 0)
2515 		goto out;
2516 	/* Make sure server returned a different fsid for the referral */
2517 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2518 		dprintk("%s: server did not return a different fsid for"
2519 			" a referral at %s\n", __func__, name->name);
2520 		status = -EIO;
2521 		goto out;
2522 	}
2523 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2524 	nfs_fixup_referral_attributes(&locations->fattr);
2525 
2526 	/* replace the lookup nfs_fattr with the locations nfs_fattr */
2527 	memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2528 	memset(fhandle, 0, sizeof(struct nfs_fh));
2529 out:
2530 	if (page)
2531 		__free_page(page);
2532 	kfree(locations);
2533 	return status;
2534 }
2535 
2536 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2537 {
2538 	struct nfs4_getattr_arg args = {
2539 		.fh = fhandle,
2540 		.bitmask = server->attr_bitmask,
2541 	};
2542 	struct nfs4_getattr_res res = {
2543 		.fattr = fattr,
2544 		.server = server,
2545 	};
2546 	struct rpc_message msg = {
2547 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2548 		.rpc_argp = &args,
2549 		.rpc_resp = &res,
2550 	};
2551 
2552 	nfs_fattr_init(fattr);
2553 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2554 }
2555 
2556 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2557 {
2558 	struct nfs4_exception exception = { };
2559 	int err;
2560 	do {
2561 		err = nfs4_handle_exception(server,
2562 				_nfs4_proc_getattr(server, fhandle, fattr),
2563 				&exception);
2564 	} while (exception.retry);
2565 	return err;
2566 }
2567 
2568 /*
2569  * The file is not closed if it is opened due to the a request to change
2570  * the size of the file. The open call will not be needed once the
2571  * VFS layer lookup-intents are implemented.
2572  *
2573  * Close is called when the inode is destroyed.
2574  * If we haven't opened the file for O_WRONLY, we
2575  * need to in the size_change case to obtain a stateid.
2576  *
2577  * Got race?
2578  * Because OPEN is always done by name in nfsv4, it is
2579  * possible that we opened a different file by the same
2580  * name.  We can recognize this race condition, but we
2581  * can't do anything about it besides returning an error.
2582  *
2583  * This will be fixed with VFS changes (lookup-intent).
2584  */
2585 static int
2586 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2587 		  struct iattr *sattr)
2588 {
2589 	struct inode *inode = dentry->d_inode;
2590 	struct rpc_cred *cred = NULL;
2591 	struct nfs4_state *state = NULL;
2592 	int status;
2593 
2594 	if (pnfs_ld_layoutret_on_setattr(inode))
2595 		pnfs_return_layout(inode);
2596 
2597 	nfs_fattr_init(fattr);
2598 
2599 	/* Deal with open(O_TRUNC) */
2600 	if (sattr->ia_valid & ATTR_OPEN)
2601 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2602 
2603 	/* Optimization: if the end result is no change, don't RPC */
2604 	if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2605 		return 0;
2606 
2607 	/* Search for an existing open(O_WRITE) file */
2608 	if (sattr->ia_valid & ATTR_FILE) {
2609 		struct nfs_open_context *ctx;
2610 
2611 		ctx = nfs_file_open_context(sattr->ia_file);
2612 		if (ctx) {
2613 			cred = ctx->cred;
2614 			state = ctx->state;
2615 		}
2616 	}
2617 
2618 	status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2619 	if (status == 0)
2620 		nfs_setattr_update_inode(inode, sattr);
2621 	return status;
2622 }
2623 
2624 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2625 		const struct qstr *name, struct nfs_fh *fhandle,
2626 		struct nfs_fattr *fattr)
2627 {
2628 	struct nfs_server *server = NFS_SERVER(dir);
2629 	int		       status;
2630 	struct nfs4_lookup_arg args = {
2631 		.bitmask = server->attr_bitmask,
2632 		.dir_fh = NFS_FH(dir),
2633 		.name = name,
2634 	};
2635 	struct nfs4_lookup_res res = {
2636 		.server = server,
2637 		.fattr = fattr,
2638 		.fh = fhandle,
2639 	};
2640 	struct rpc_message msg = {
2641 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2642 		.rpc_argp = &args,
2643 		.rpc_resp = &res,
2644 	};
2645 
2646 	nfs_fattr_init(fattr);
2647 
2648 	dprintk("NFS call  lookup %s\n", name->name);
2649 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2650 	dprintk("NFS reply lookup: %d\n", status);
2651 	return status;
2652 }
2653 
2654 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2655 {
2656 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2657 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2658 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2659 	fattr->nlink = 2;
2660 }
2661 
2662 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2663 				   struct qstr *name, struct nfs_fh *fhandle,
2664 				   struct nfs_fattr *fattr)
2665 {
2666 	struct nfs4_exception exception = { };
2667 	struct rpc_clnt *client = *clnt;
2668 	int err;
2669 	do {
2670 		err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2671 		switch (err) {
2672 		case -NFS4ERR_BADNAME:
2673 			err = -ENOENT;
2674 			goto out;
2675 		case -NFS4ERR_MOVED:
2676 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2677 			goto out;
2678 		case -NFS4ERR_WRONGSEC:
2679 			err = -EPERM;
2680 			if (client != *clnt)
2681 				goto out;
2682 
2683 			client = nfs4_create_sec_client(client, dir, name);
2684 			if (IS_ERR(client))
2685 				return PTR_ERR(client);
2686 
2687 			exception.retry = 1;
2688 			break;
2689 		default:
2690 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2691 		}
2692 	} while (exception.retry);
2693 
2694 out:
2695 	if (err == 0)
2696 		*clnt = client;
2697 	else if (client != *clnt)
2698 		rpc_shutdown_client(client);
2699 
2700 	return err;
2701 }
2702 
2703 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2704 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2705 {
2706 	int status;
2707 	struct rpc_clnt *client = NFS_CLIENT(dir);
2708 
2709 	status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2710 	if (client != NFS_CLIENT(dir)) {
2711 		rpc_shutdown_client(client);
2712 		nfs_fixup_secinfo_attributes(fattr);
2713 	}
2714 	return status;
2715 }
2716 
2717 struct rpc_clnt *
2718 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2719 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2720 {
2721 	int status;
2722 	struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2723 
2724 	status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2725 	if (status < 0) {
2726 		rpc_shutdown_client(client);
2727 		return ERR_PTR(status);
2728 	}
2729 	return client;
2730 }
2731 
2732 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2733 {
2734 	struct nfs_server *server = NFS_SERVER(inode);
2735 	struct nfs4_accessargs args = {
2736 		.fh = NFS_FH(inode),
2737 		.bitmask = server->cache_consistency_bitmask,
2738 	};
2739 	struct nfs4_accessres res = {
2740 		.server = server,
2741 	};
2742 	struct rpc_message msg = {
2743 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2744 		.rpc_argp = &args,
2745 		.rpc_resp = &res,
2746 		.rpc_cred = entry->cred,
2747 	};
2748 	int mode = entry->mask;
2749 	int status;
2750 
2751 	/*
2752 	 * Determine which access bits we want to ask for...
2753 	 */
2754 	if (mode & MAY_READ)
2755 		args.access |= NFS4_ACCESS_READ;
2756 	if (S_ISDIR(inode->i_mode)) {
2757 		if (mode & MAY_WRITE)
2758 			args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2759 		if (mode & MAY_EXEC)
2760 			args.access |= NFS4_ACCESS_LOOKUP;
2761 	} else {
2762 		if (mode & MAY_WRITE)
2763 			args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2764 		if (mode & MAY_EXEC)
2765 			args.access |= NFS4_ACCESS_EXECUTE;
2766 	}
2767 
2768 	res.fattr = nfs_alloc_fattr();
2769 	if (res.fattr == NULL)
2770 		return -ENOMEM;
2771 
2772 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2773 	if (!status) {
2774 		nfs_access_set_mask(entry, res.access);
2775 		nfs_refresh_inode(inode, res.fattr);
2776 	}
2777 	nfs_free_fattr(res.fattr);
2778 	return status;
2779 }
2780 
2781 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2782 {
2783 	struct nfs4_exception exception = { };
2784 	int err;
2785 	do {
2786 		err = nfs4_handle_exception(NFS_SERVER(inode),
2787 				_nfs4_proc_access(inode, entry),
2788 				&exception);
2789 	} while (exception.retry);
2790 	return err;
2791 }
2792 
2793 /*
2794  * TODO: For the time being, we don't try to get any attributes
2795  * along with any of the zero-copy operations READ, READDIR,
2796  * READLINK, WRITE.
2797  *
2798  * In the case of the first three, we want to put the GETATTR
2799  * after the read-type operation -- this is because it is hard
2800  * to predict the length of a GETATTR response in v4, and thus
2801  * align the READ data correctly.  This means that the GETATTR
2802  * may end up partially falling into the page cache, and we should
2803  * shift it into the 'tail' of the xdr_buf before processing.
2804  * To do this efficiently, we need to know the total length
2805  * of data received, which doesn't seem to be available outside
2806  * of the RPC layer.
2807  *
2808  * In the case of WRITE, we also want to put the GETATTR after
2809  * the operation -- in this case because we want to make sure
2810  * we get the post-operation mtime and size.
2811  *
2812  * Both of these changes to the XDR layer would in fact be quite
2813  * minor, but I decided to leave them for a subsequent patch.
2814  */
2815 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2816 		unsigned int pgbase, unsigned int pglen)
2817 {
2818 	struct nfs4_readlink args = {
2819 		.fh       = NFS_FH(inode),
2820 		.pgbase	  = pgbase,
2821 		.pglen    = pglen,
2822 		.pages    = &page,
2823 	};
2824 	struct nfs4_readlink_res res;
2825 	struct rpc_message msg = {
2826 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2827 		.rpc_argp = &args,
2828 		.rpc_resp = &res,
2829 	};
2830 
2831 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2832 }
2833 
2834 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2835 		unsigned int pgbase, unsigned int pglen)
2836 {
2837 	struct nfs4_exception exception = { };
2838 	int err;
2839 	do {
2840 		err = nfs4_handle_exception(NFS_SERVER(inode),
2841 				_nfs4_proc_readlink(inode, page, pgbase, pglen),
2842 				&exception);
2843 	} while (exception.retry);
2844 	return err;
2845 }
2846 
2847 /*
2848  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
2849  */
2850 static int
2851 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2852 		 int flags)
2853 {
2854 	struct nfs_open_context *ctx;
2855 	struct nfs4_state *state;
2856 	int status = 0;
2857 
2858 	ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2859 	if (IS_ERR(ctx))
2860 		return PTR_ERR(ctx);
2861 
2862 	sattr->ia_mode &= ~current_umask();
2863 	state = nfs4_do_open(dir, dentry, ctx->mode,
2864 			flags, sattr, ctx->cred,
2865 			&ctx->mdsthreshold);
2866 	d_drop(dentry);
2867 	if (IS_ERR(state)) {
2868 		status = PTR_ERR(state);
2869 		goto out;
2870 	}
2871 	d_add(dentry, igrab(state->inode));
2872 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2873 	ctx->state = state;
2874 out:
2875 	put_nfs_open_context(ctx);
2876 	return status;
2877 }
2878 
2879 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2880 {
2881 	struct nfs_server *server = NFS_SERVER(dir);
2882 	struct nfs_removeargs args = {
2883 		.fh = NFS_FH(dir),
2884 		.name = *name,
2885 	};
2886 	struct nfs_removeres res = {
2887 		.server = server,
2888 	};
2889 	struct rpc_message msg = {
2890 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2891 		.rpc_argp = &args,
2892 		.rpc_resp = &res,
2893 	};
2894 	int status;
2895 
2896 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2897 	if (status == 0)
2898 		update_changeattr(dir, &res.cinfo);
2899 	return status;
2900 }
2901 
2902 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2903 {
2904 	struct nfs4_exception exception = { };
2905 	int err;
2906 	do {
2907 		err = nfs4_handle_exception(NFS_SERVER(dir),
2908 				_nfs4_proc_remove(dir, name),
2909 				&exception);
2910 	} while (exception.retry);
2911 	return err;
2912 }
2913 
2914 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2915 {
2916 	struct nfs_server *server = NFS_SERVER(dir);
2917 	struct nfs_removeargs *args = msg->rpc_argp;
2918 	struct nfs_removeres *res = msg->rpc_resp;
2919 
2920 	res->server = server;
2921 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2922 	nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2923 }
2924 
2925 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2926 {
2927 	nfs4_setup_sequence(NFS_SERVER(data->dir),
2928 			&data->args.seq_args,
2929 			&data->res.seq_res,
2930 			task);
2931 }
2932 
2933 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2934 {
2935 	struct nfs_removeres *res = task->tk_msg.rpc_resp;
2936 
2937 	if (!nfs4_sequence_done(task, &res->seq_res))
2938 		return 0;
2939 	if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2940 		return 0;
2941 	update_changeattr(dir, &res->cinfo);
2942 	return 1;
2943 }
2944 
2945 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2946 {
2947 	struct nfs_server *server = NFS_SERVER(dir);
2948 	struct nfs_renameargs *arg = msg->rpc_argp;
2949 	struct nfs_renameres *res = msg->rpc_resp;
2950 
2951 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2952 	res->server = server;
2953 	nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2954 }
2955 
2956 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2957 {
2958 	nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2959 			&data->args.seq_args,
2960 			&data->res.seq_res,
2961 			task);
2962 }
2963 
2964 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2965 				 struct inode *new_dir)
2966 {
2967 	struct nfs_renameres *res = task->tk_msg.rpc_resp;
2968 
2969 	if (!nfs4_sequence_done(task, &res->seq_res))
2970 		return 0;
2971 	if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2972 		return 0;
2973 
2974 	update_changeattr(old_dir, &res->old_cinfo);
2975 	update_changeattr(new_dir, &res->new_cinfo);
2976 	return 1;
2977 }
2978 
2979 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2980 		struct inode *new_dir, struct qstr *new_name)
2981 {
2982 	struct nfs_server *server = NFS_SERVER(old_dir);
2983 	struct nfs_renameargs arg = {
2984 		.old_dir = NFS_FH(old_dir),
2985 		.new_dir = NFS_FH(new_dir),
2986 		.old_name = old_name,
2987 		.new_name = new_name,
2988 	};
2989 	struct nfs_renameres res = {
2990 		.server = server,
2991 	};
2992 	struct rpc_message msg = {
2993 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2994 		.rpc_argp = &arg,
2995 		.rpc_resp = &res,
2996 	};
2997 	int status = -ENOMEM;
2998 
2999 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3000 	if (!status) {
3001 		update_changeattr(old_dir, &res.old_cinfo);
3002 		update_changeattr(new_dir, &res.new_cinfo);
3003 	}
3004 	return status;
3005 }
3006 
3007 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3008 		struct inode *new_dir, struct qstr *new_name)
3009 {
3010 	struct nfs4_exception exception = { };
3011 	int err;
3012 	do {
3013 		err = nfs4_handle_exception(NFS_SERVER(old_dir),
3014 				_nfs4_proc_rename(old_dir, old_name,
3015 					new_dir, new_name),
3016 				&exception);
3017 	} while (exception.retry);
3018 	return err;
3019 }
3020 
3021 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3022 {
3023 	struct nfs_server *server = NFS_SERVER(inode);
3024 	struct nfs4_link_arg arg = {
3025 		.fh     = NFS_FH(inode),
3026 		.dir_fh = NFS_FH(dir),
3027 		.name   = name,
3028 		.bitmask = server->attr_bitmask,
3029 	};
3030 	struct nfs4_link_res res = {
3031 		.server = server,
3032 	};
3033 	struct rpc_message msg = {
3034 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3035 		.rpc_argp = &arg,
3036 		.rpc_resp = &res,
3037 	};
3038 	int status = -ENOMEM;
3039 
3040 	res.fattr = nfs_alloc_fattr();
3041 	if (res.fattr == NULL)
3042 		goto out;
3043 
3044 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3045 	if (!status) {
3046 		update_changeattr(dir, &res.cinfo);
3047 		nfs_post_op_update_inode(inode, res.fattr);
3048 	}
3049 out:
3050 	nfs_free_fattr(res.fattr);
3051 	return status;
3052 }
3053 
3054 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3055 {
3056 	struct nfs4_exception exception = { };
3057 	int err;
3058 	do {
3059 		err = nfs4_handle_exception(NFS_SERVER(inode),
3060 				_nfs4_proc_link(inode, dir, name),
3061 				&exception);
3062 	} while (exception.retry);
3063 	return err;
3064 }
3065 
3066 struct nfs4_createdata {
3067 	struct rpc_message msg;
3068 	struct nfs4_create_arg arg;
3069 	struct nfs4_create_res res;
3070 	struct nfs_fh fh;
3071 	struct nfs_fattr fattr;
3072 };
3073 
3074 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3075 		struct qstr *name, struct iattr *sattr, u32 ftype)
3076 {
3077 	struct nfs4_createdata *data;
3078 
3079 	data = kzalloc(sizeof(*data), GFP_KERNEL);
3080 	if (data != NULL) {
3081 		struct nfs_server *server = NFS_SERVER(dir);
3082 
3083 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3084 		data->msg.rpc_argp = &data->arg;
3085 		data->msg.rpc_resp = &data->res;
3086 		data->arg.dir_fh = NFS_FH(dir);
3087 		data->arg.server = server;
3088 		data->arg.name = name;
3089 		data->arg.attrs = sattr;
3090 		data->arg.ftype = ftype;
3091 		data->arg.bitmask = server->attr_bitmask;
3092 		data->res.server = server;
3093 		data->res.fh = &data->fh;
3094 		data->res.fattr = &data->fattr;
3095 		nfs_fattr_init(data->res.fattr);
3096 	}
3097 	return data;
3098 }
3099 
3100 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3101 {
3102 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3103 				    &data->arg.seq_args, &data->res.seq_res, 1);
3104 	if (status == 0) {
3105 		update_changeattr(dir, &data->res.dir_cinfo);
3106 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3107 	}
3108 	return status;
3109 }
3110 
3111 static void nfs4_free_createdata(struct nfs4_createdata *data)
3112 {
3113 	kfree(data);
3114 }
3115 
3116 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3117 		struct page *page, unsigned int len, struct iattr *sattr)
3118 {
3119 	struct nfs4_createdata *data;
3120 	int status = -ENAMETOOLONG;
3121 
3122 	if (len > NFS4_MAXPATHLEN)
3123 		goto out;
3124 
3125 	status = -ENOMEM;
3126 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3127 	if (data == NULL)
3128 		goto out;
3129 
3130 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3131 	data->arg.u.symlink.pages = &page;
3132 	data->arg.u.symlink.len = len;
3133 
3134 	status = nfs4_do_create(dir, dentry, data);
3135 
3136 	nfs4_free_createdata(data);
3137 out:
3138 	return status;
3139 }
3140 
3141 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3142 		struct page *page, unsigned int len, struct iattr *sattr)
3143 {
3144 	struct nfs4_exception exception = { };
3145 	int err;
3146 	do {
3147 		err = nfs4_handle_exception(NFS_SERVER(dir),
3148 				_nfs4_proc_symlink(dir, dentry, page,
3149 							len, sattr),
3150 				&exception);
3151 	} while (exception.retry);
3152 	return err;
3153 }
3154 
3155 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3156 		struct iattr *sattr)
3157 {
3158 	struct nfs4_createdata *data;
3159 	int status = -ENOMEM;
3160 
3161 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3162 	if (data == NULL)
3163 		goto out;
3164 
3165 	status = nfs4_do_create(dir, dentry, data);
3166 
3167 	nfs4_free_createdata(data);
3168 out:
3169 	return status;
3170 }
3171 
3172 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3173 		struct iattr *sattr)
3174 {
3175 	struct nfs4_exception exception = { };
3176 	int err;
3177 
3178 	sattr->ia_mode &= ~current_umask();
3179 	do {
3180 		err = nfs4_handle_exception(NFS_SERVER(dir),
3181 				_nfs4_proc_mkdir(dir, dentry, sattr),
3182 				&exception);
3183 	} while (exception.retry);
3184 	return err;
3185 }
3186 
3187 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3188 		u64 cookie, struct page **pages, unsigned int count, int plus)
3189 {
3190 	struct inode		*dir = dentry->d_inode;
3191 	struct nfs4_readdir_arg args = {
3192 		.fh = NFS_FH(dir),
3193 		.pages = pages,
3194 		.pgbase = 0,
3195 		.count = count,
3196 		.bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3197 		.plus = plus,
3198 	};
3199 	struct nfs4_readdir_res res;
3200 	struct rpc_message msg = {
3201 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3202 		.rpc_argp = &args,
3203 		.rpc_resp = &res,
3204 		.rpc_cred = cred,
3205 	};
3206 	int			status;
3207 
3208 	dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3209 			dentry->d_parent->d_name.name,
3210 			dentry->d_name.name,
3211 			(unsigned long long)cookie);
3212 	nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3213 	res.pgbase = args.pgbase;
3214 	status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3215 	if (status >= 0) {
3216 		memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3217 		status += args.pgbase;
3218 	}
3219 
3220 	nfs_invalidate_atime(dir);
3221 
3222 	dprintk("%s: returns %d\n", __func__, status);
3223 	return status;
3224 }
3225 
3226 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3227 		u64 cookie, struct page **pages, unsigned int count, int plus)
3228 {
3229 	struct nfs4_exception exception = { };
3230 	int err;
3231 	do {
3232 		err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3233 				_nfs4_proc_readdir(dentry, cred, cookie,
3234 					pages, count, plus),
3235 				&exception);
3236 	} while (exception.retry);
3237 	return err;
3238 }
3239 
3240 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3241 		struct iattr *sattr, dev_t rdev)
3242 {
3243 	struct nfs4_createdata *data;
3244 	int mode = sattr->ia_mode;
3245 	int status = -ENOMEM;
3246 
3247 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3248 	if (data == NULL)
3249 		goto out;
3250 
3251 	if (S_ISFIFO(mode))
3252 		data->arg.ftype = NF4FIFO;
3253 	else if (S_ISBLK(mode)) {
3254 		data->arg.ftype = NF4BLK;
3255 		data->arg.u.device.specdata1 = MAJOR(rdev);
3256 		data->arg.u.device.specdata2 = MINOR(rdev);
3257 	}
3258 	else if (S_ISCHR(mode)) {
3259 		data->arg.ftype = NF4CHR;
3260 		data->arg.u.device.specdata1 = MAJOR(rdev);
3261 		data->arg.u.device.specdata2 = MINOR(rdev);
3262 	} else if (!S_ISSOCK(mode)) {
3263 		status = -EINVAL;
3264 		goto out_free;
3265 	}
3266 
3267 	status = nfs4_do_create(dir, dentry, data);
3268 out_free:
3269 	nfs4_free_createdata(data);
3270 out:
3271 	return status;
3272 }
3273 
3274 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3275 		struct iattr *sattr, dev_t rdev)
3276 {
3277 	struct nfs4_exception exception = { };
3278 	int err;
3279 
3280 	sattr->ia_mode &= ~current_umask();
3281 	do {
3282 		err = nfs4_handle_exception(NFS_SERVER(dir),
3283 				_nfs4_proc_mknod(dir, dentry, sattr, rdev),
3284 				&exception);
3285 	} while (exception.retry);
3286 	return err;
3287 }
3288 
3289 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3290 		 struct nfs_fsstat *fsstat)
3291 {
3292 	struct nfs4_statfs_arg args = {
3293 		.fh = fhandle,
3294 		.bitmask = server->attr_bitmask,
3295 	};
3296 	struct nfs4_statfs_res res = {
3297 		.fsstat = fsstat,
3298 	};
3299 	struct rpc_message msg = {
3300 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3301 		.rpc_argp = &args,
3302 		.rpc_resp = &res,
3303 	};
3304 
3305 	nfs_fattr_init(fsstat->fattr);
3306 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3307 }
3308 
3309 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3310 {
3311 	struct nfs4_exception exception = { };
3312 	int err;
3313 	do {
3314 		err = nfs4_handle_exception(server,
3315 				_nfs4_proc_statfs(server, fhandle, fsstat),
3316 				&exception);
3317 	} while (exception.retry);
3318 	return err;
3319 }
3320 
3321 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3322 		struct nfs_fsinfo *fsinfo)
3323 {
3324 	struct nfs4_fsinfo_arg args = {
3325 		.fh = fhandle,
3326 		.bitmask = server->attr_bitmask,
3327 	};
3328 	struct nfs4_fsinfo_res res = {
3329 		.fsinfo = fsinfo,
3330 	};
3331 	struct rpc_message msg = {
3332 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3333 		.rpc_argp = &args,
3334 		.rpc_resp = &res,
3335 	};
3336 
3337 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3338 }
3339 
3340 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3341 {
3342 	struct nfs4_exception exception = { };
3343 	int err;
3344 
3345 	do {
3346 		err = nfs4_handle_exception(server,
3347 				_nfs4_do_fsinfo(server, fhandle, fsinfo),
3348 				&exception);
3349 	} while (exception.retry);
3350 	return err;
3351 }
3352 
3353 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3354 {
3355 	int error;
3356 
3357 	nfs_fattr_init(fsinfo->fattr);
3358 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3359 	if (error == 0) {
3360 		/* block layout checks this! */
3361 		server->pnfs_blksize = fsinfo->blksize;
3362 		set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3363 	}
3364 
3365 	return error;
3366 }
3367 
3368 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3369 		struct nfs_pathconf *pathconf)
3370 {
3371 	struct nfs4_pathconf_arg args = {
3372 		.fh = fhandle,
3373 		.bitmask = server->attr_bitmask,
3374 	};
3375 	struct nfs4_pathconf_res res = {
3376 		.pathconf = pathconf,
3377 	};
3378 	struct rpc_message msg = {
3379 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3380 		.rpc_argp = &args,
3381 		.rpc_resp = &res,
3382 	};
3383 
3384 	/* None of the pathconf attributes are mandatory to implement */
3385 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3386 		memset(pathconf, 0, sizeof(*pathconf));
3387 		return 0;
3388 	}
3389 
3390 	nfs_fattr_init(pathconf->fattr);
3391 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3392 }
3393 
3394 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3395 		struct nfs_pathconf *pathconf)
3396 {
3397 	struct nfs4_exception exception = { };
3398 	int err;
3399 
3400 	do {
3401 		err = nfs4_handle_exception(server,
3402 				_nfs4_proc_pathconf(server, fhandle, pathconf),
3403 				&exception);
3404 	} while (exception.retry);
3405 	return err;
3406 }
3407 
3408 void __nfs4_read_done_cb(struct nfs_read_data *data)
3409 {
3410 	nfs_invalidate_atime(data->header->inode);
3411 }
3412 
3413 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3414 {
3415 	struct nfs_server *server = NFS_SERVER(data->header->inode);
3416 
3417 	if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3418 		rpc_restart_call_prepare(task);
3419 		return -EAGAIN;
3420 	}
3421 
3422 	__nfs4_read_done_cb(data);
3423 	if (task->tk_status > 0)
3424 		renew_lease(server, data->timestamp);
3425 	return 0;
3426 }
3427 
3428 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3429 {
3430 
3431 	dprintk("--> %s\n", __func__);
3432 
3433 	if (!nfs4_sequence_done(task, &data->res.seq_res))
3434 		return -EAGAIN;
3435 
3436 	return data->read_done_cb ? data->read_done_cb(task, data) :
3437 				    nfs4_read_done_cb(task, data);
3438 }
3439 
3440 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3441 {
3442 	data->timestamp   = jiffies;
3443 	data->read_done_cb = nfs4_read_done_cb;
3444 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3445 	nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3446 }
3447 
3448 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3449 {
3450 	nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3451 			&data->args.seq_args,
3452 			&data->res.seq_res,
3453 			task);
3454 }
3455 
3456 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3457 {
3458 	struct inode *inode = data->header->inode;
3459 
3460 	if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3461 		rpc_restart_call_prepare(task);
3462 		return -EAGAIN;
3463 	}
3464 	if (task->tk_status >= 0) {
3465 		renew_lease(NFS_SERVER(inode), data->timestamp);
3466 		nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3467 	}
3468 	return 0;
3469 }
3470 
3471 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3472 {
3473 	if (!nfs4_sequence_done(task, &data->res.seq_res))
3474 		return -EAGAIN;
3475 	return data->write_done_cb ? data->write_done_cb(task, data) :
3476 		nfs4_write_done_cb(task, data);
3477 }
3478 
3479 static
3480 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3481 {
3482 	const struct nfs_pgio_header *hdr = data->header;
3483 
3484 	/* Don't request attributes for pNFS or O_DIRECT writes */
3485 	if (data->ds_clp != NULL || hdr->dreq != NULL)
3486 		return false;
3487 	/* Otherwise, request attributes if and only if we don't hold
3488 	 * a delegation
3489 	 */
3490 	return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3491 }
3492 
3493 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3494 {
3495 	struct nfs_server *server = NFS_SERVER(data->header->inode);
3496 
3497 	if (!nfs4_write_need_cache_consistency_data(data)) {
3498 		data->args.bitmask = NULL;
3499 		data->res.fattr = NULL;
3500 	} else
3501 		data->args.bitmask = server->cache_consistency_bitmask;
3502 
3503 	if (!data->write_done_cb)
3504 		data->write_done_cb = nfs4_write_done_cb;
3505 	data->res.server = server;
3506 	data->timestamp   = jiffies;
3507 
3508 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3509 	nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3510 }
3511 
3512 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3513 {
3514 	nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3515 			&data->args.seq_args,
3516 			&data->res.seq_res,
3517 			task);
3518 }
3519 
3520 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3521 {
3522 	nfs4_setup_sequence(NFS_SERVER(data->inode),
3523 			&data->args.seq_args,
3524 			&data->res.seq_res,
3525 			task);
3526 }
3527 
3528 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3529 {
3530 	struct inode *inode = data->inode;
3531 
3532 	if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3533 		rpc_restart_call_prepare(task);
3534 		return -EAGAIN;
3535 	}
3536 	return 0;
3537 }
3538 
3539 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3540 {
3541 	if (!nfs4_sequence_done(task, &data->res.seq_res))
3542 		return -EAGAIN;
3543 	return data->commit_done_cb(task, data);
3544 }
3545 
3546 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3547 {
3548 	struct nfs_server *server = NFS_SERVER(data->inode);
3549 
3550 	if (data->commit_done_cb == NULL)
3551 		data->commit_done_cb = nfs4_commit_done_cb;
3552 	data->res.server = server;
3553 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3554 	nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3555 }
3556 
3557 struct nfs4_renewdata {
3558 	struct nfs_client	*client;
3559 	unsigned long		timestamp;
3560 };
3561 
3562 /*
3563  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3564  * standalone procedure for queueing an asynchronous RENEW.
3565  */
3566 static void nfs4_renew_release(void *calldata)
3567 {
3568 	struct nfs4_renewdata *data = calldata;
3569 	struct nfs_client *clp = data->client;
3570 
3571 	if (atomic_read(&clp->cl_count) > 1)
3572 		nfs4_schedule_state_renewal(clp);
3573 	nfs_put_client(clp);
3574 	kfree(data);
3575 }
3576 
3577 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3578 {
3579 	struct nfs4_renewdata *data = calldata;
3580 	struct nfs_client *clp = data->client;
3581 	unsigned long timestamp = data->timestamp;
3582 
3583 	if (task->tk_status < 0) {
3584 		/* Unless we're shutting down, schedule state recovery! */
3585 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3586 			return;
3587 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3588 			nfs4_schedule_lease_recovery(clp);
3589 			return;
3590 		}
3591 		nfs4_schedule_path_down_recovery(clp);
3592 	}
3593 	do_renew_lease(clp, timestamp);
3594 }
3595 
3596 static const struct rpc_call_ops nfs4_renew_ops = {
3597 	.rpc_call_done = nfs4_renew_done,
3598 	.rpc_release = nfs4_renew_release,
3599 };
3600 
3601 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3602 {
3603 	struct rpc_message msg = {
3604 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3605 		.rpc_argp	= clp,
3606 		.rpc_cred	= cred,
3607 	};
3608 	struct nfs4_renewdata *data;
3609 
3610 	if (renew_flags == 0)
3611 		return 0;
3612 	if (!atomic_inc_not_zero(&clp->cl_count))
3613 		return -EIO;
3614 	data = kmalloc(sizeof(*data), GFP_NOFS);
3615 	if (data == NULL)
3616 		return -ENOMEM;
3617 	data->client = clp;
3618 	data->timestamp = jiffies;
3619 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3620 			&nfs4_renew_ops, data);
3621 }
3622 
3623 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3624 {
3625 	struct rpc_message msg = {
3626 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3627 		.rpc_argp	= clp,
3628 		.rpc_cred	= cred,
3629 	};
3630 	unsigned long now = jiffies;
3631 	int status;
3632 
3633 	status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3634 	if (status < 0)
3635 		return status;
3636 	do_renew_lease(clp, now);
3637 	return 0;
3638 }
3639 
3640 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3641 {
3642 	return (server->caps & NFS_CAP_ACLS)
3643 		&& (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3644 		&& (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3645 }
3646 
3647 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3648  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3649  * the stack.
3650  */
3651 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3652 
3653 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3654 		struct page **pages, unsigned int *pgbase)
3655 {
3656 	struct page *newpage, **spages;
3657 	int rc = 0;
3658 	size_t len;
3659 	spages = pages;
3660 
3661 	do {
3662 		len = min_t(size_t, PAGE_SIZE, buflen);
3663 		newpage = alloc_page(GFP_KERNEL);
3664 
3665 		if (newpage == NULL)
3666 			goto unwind;
3667 		memcpy(page_address(newpage), buf, len);
3668                 buf += len;
3669                 buflen -= len;
3670 		*pages++ = newpage;
3671 		rc++;
3672 	} while (buflen != 0);
3673 
3674 	return rc;
3675 
3676 unwind:
3677 	for(; rc > 0; rc--)
3678 		__free_page(spages[rc-1]);
3679 	return -ENOMEM;
3680 }
3681 
3682 struct nfs4_cached_acl {
3683 	int cached;
3684 	size_t len;
3685 	char data[0];
3686 };
3687 
3688 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3689 {
3690 	struct nfs_inode *nfsi = NFS_I(inode);
3691 
3692 	spin_lock(&inode->i_lock);
3693 	kfree(nfsi->nfs4_acl);
3694 	nfsi->nfs4_acl = acl;
3695 	spin_unlock(&inode->i_lock);
3696 }
3697 
3698 static void nfs4_zap_acl_attr(struct inode *inode)
3699 {
3700 	nfs4_set_cached_acl(inode, NULL);
3701 }
3702 
3703 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3704 {
3705 	struct nfs_inode *nfsi = NFS_I(inode);
3706 	struct nfs4_cached_acl *acl;
3707 	int ret = -ENOENT;
3708 
3709 	spin_lock(&inode->i_lock);
3710 	acl = nfsi->nfs4_acl;
3711 	if (acl == NULL)
3712 		goto out;
3713 	if (buf == NULL) /* user is just asking for length */
3714 		goto out_len;
3715 	if (acl->cached == 0)
3716 		goto out;
3717 	ret = -ERANGE; /* see getxattr(2) man page */
3718 	if (acl->len > buflen)
3719 		goto out;
3720 	memcpy(buf, acl->data, acl->len);
3721 out_len:
3722 	ret = acl->len;
3723 out:
3724 	spin_unlock(&inode->i_lock);
3725 	return ret;
3726 }
3727 
3728 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3729 {
3730 	struct nfs4_cached_acl *acl;
3731 	size_t buflen = sizeof(*acl) + acl_len;
3732 
3733 	if (buflen <= PAGE_SIZE) {
3734 		acl = kmalloc(buflen, GFP_KERNEL);
3735 		if (acl == NULL)
3736 			goto out;
3737 		acl->cached = 1;
3738 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
3739 	} else {
3740 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3741 		if (acl == NULL)
3742 			goto out;
3743 		acl->cached = 0;
3744 	}
3745 	acl->len = acl_len;
3746 out:
3747 	nfs4_set_cached_acl(inode, acl);
3748 }
3749 
3750 /*
3751  * The getxattr API returns the required buffer length when called with a
3752  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3753  * the required buf.  On a NULL buf, we send a page of data to the server
3754  * guessing that the ACL request can be serviced by a page. If so, we cache
3755  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3756  * the cache. If not so, we throw away the page, and cache the required
3757  * length. The next getxattr call will then produce another round trip to
3758  * the server, this time with the input buf of the required size.
3759  */
3760 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3761 {
3762 	struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3763 	struct nfs_getaclargs args = {
3764 		.fh = NFS_FH(inode),
3765 		.acl_pages = pages,
3766 		.acl_len = buflen,
3767 	};
3768 	struct nfs_getaclres res = {
3769 		.acl_len = buflen,
3770 	};
3771 	struct rpc_message msg = {
3772 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3773 		.rpc_argp = &args,
3774 		.rpc_resp = &res,
3775 	};
3776 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3777 	int ret = -ENOMEM, i;
3778 
3779 	/* As long as we're doing a round trip to the server anyway,
3780 	 * let's be prepared for a page of acl data. */
3781 	if (npages == 0)
3782 		npages = 1;
3783 	if (npages > ARRAY_SIZE(pages))
3784 		return -ERANGE;
3785 
3786 	for (i = 0; i < npages; i++) {
3787 		pages[i] = alloc_page(GFP_KERNEL);
3788 		if (!pages[i])
3789 			goto out_free;
3790 	}
3791 
3792 	/* for decoding across pages */
3793 	res.acl_scratch = alloc_page(GFP_KERNEL);
3794 	if (!res.acl_scratch)
3795 		goto out_free;
3796 
3797 	args.acl_len = npages * PAGE_SIZE;
3798 	args.acl_pgbase = 0;
3799 
3800 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
3801 		__func__, buf, buflen, npages, args.acl_len);
3802 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3803 			     &msg, &args.seq_args, &res.seq_res, 0);
3804 	if (ret)
3805 		goto out_free;
3806 
3807 	/* Handle the case where the passed-in buffer is too short */
3808 	if (res.acl_flags & NFS4_ACL_TRUNC) {
3809 		/* Did the user only issue a request for the acl length? */
3810 		if (buf == NULL)
3811 			goto out_ok;
3812 		ret = -ERANGE;
3813 		goto out_free;
3814 	}
3815 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3816 	if (buf) {
3817 		if (res.acl_len > buflen) {
3818 			ret = -ERANGE;
3819 			goto out_free;
3820 		}
3821 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3822 	}
3823 out_ok:
3824 	ret = res.acl_len;
3825 out_free:
3826 	for (i = 0; i < npages; i++)
3827 		if (pages[i])
3828 			__free_page(pages[i]);
3829 	if (res.acl_scratch)
3830 		__free_page(res.acl_scratch);
3831 	return ret;
3832 }
3833 
3834 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3835 {
3836 	struct nfs4_exception exception = { };
3837 	ssize_t ret;
3838 	do {
3839 		ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3840 		if (ret >= 0)
3841 			break;
3842 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3843 	} while (exception.retry);
3844 	return ret;
3845 }
3846 
3847 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3848 {
3849 	struct nfs_server *server = NFS_SERVER(inode);
3850 	int ret;
3851 
3852 	if (!nfs4_server_supports_acls(server))
3853 		return -EOPNOTSUPP;
3854 	ret = nfs_revalidate_inode(server, inode);
3855 	if (ret < 0)
3856 		return ret;
3857 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3858 		nfs_zap_acl_cache(inode);
3859 	ret = nfs4_read_cached_acl(inode, buf, buflen);
3860 	if (ret != -ENOENT)
3861 		/* -ENOENT is returned if there is no ACL or if there is an ACL
3862 		 * but no cached acl data, just the acl length */
3863 		return ret;
3864 	return nfs4_get_acl_uncached(inode, buf, buflen);
3865 }
3866 
3867 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3868 {
3869 	struct nfs_server *server = NFS_SERVER(inode);
3870 	struct page *pages[NFS4ACL_MAXPAGES];
3871 	struct nfs_setaclargs arg = {
3872 		.fh		= NFS_FH(inode),
3873 		.acl_pages	= pages,
3874 		.acl_len	= buflen,
3875 	};
3876 	struct nfs_setaclres res;
3877 	struct rpc_message msg = {
3878 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3879 		.rpc_argp	= &arg,
3880 		.rpc_resp	= &res,
3881 	};
3882 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3883 	int ret, i;
3884 
3885 	if (!nfs4_server_supports_acls(server))
3886 		return -EOPNOTSUPP;
3887 	if (npages > ARRAY_SIZE(pages))
3888 		return -ERANGE;
3889 	i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3890 	if (i < 0)
3891 		return i;
3892 	nfs4_inode_return_delegation(inode);
3893 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3894 
3895 	/*
3896 	 * Free each page after tx, so the only ref left is
3897 	 * held by the network stack
3898 	 */
3899 	for (; i > 0; i--)
3900 		put_page(pages[i-1]);
3901 
3902 	/*
3903 	 * Acl update can result in inode attribute update.
3904 	 * so mark the attribute cache invalid.
3905 	 */
3906 	spin_lock(&inode->i_lock);
3907 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3908 	spin_unlock(&inode->i_lock);
3909 	nfs_access_zap_cache(inode);
3910 	nfs_zap_acl_cache(inode);
3911 	return ret;
3912 }
3913 
3914 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3915 {
3916 	struct nfs4_exception exception = { };
3917 	int err;
3918 	do {
3919 		err = nfs4_handle_exception(NFS_SERVER(inode),
3920 				__nfs4_proc_set_acl(inode, buf, buflen),
3921 				&exception);
3922 	} while (exception.retry);
3923 	return err;
3924 }
3925 
3926 static int
3927 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3928 {
3929 	struct nfs_client *clp = server->nfs_client;
3930 
3931 	if (task->tk_status >= 0)
3932 		return 0;
3933 	switch(task->tk_status) {
3934 		case -NFS4ERR_DELEG_REVOKED:
3935 		case -NFS4ERR_ADMIN_REVOKED:
3936 		case -NFS4ERR_BAD_STATEID:
3937 			if (state == NULL)
3938 				break;
3939 			nfs_remove_bad_delegation(state->inode);
3940 		case -NFS4ERR_OPENMODE:
3941 			if (state == NULL)
3942 				break;
3943 			nfs4_schedule_stateid_recovery(server, state);
3944 			goto wait_on_recovery;
3945 		case -NFS4ERR_EXPIRED:
3946 			if (state != NULL)
3947 				nfs4_schedule_stateid_recovery(server, state);
3948 		case -NFS4ERR_STALE_STATEID:
3949 		case -NFS4ERR_STALE_CLIENTID:
3950 			nfs4_schedule_lease_recovery(clp);
3951 			goto wait_on_recovery;
3952 #if defined(CONFIG_NFS_V4_1)
3953 		case -NFS4ERR_BADSESSION:
3954 		case -NFS4ERR_BADSLOT:
3955 		case -NFS4ERR_BAD_HIGH_SLOT:
3956 		case -NFS4ERR_DEADSESSION:
3957 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3958 		case -NFS4ERR_SEQ_FALSE_RETRY:
3959 		case -NFS4ERR_SEQ_MISORDERED:
3960 			dprintk("%s ERROR %d, Reset session\n", __func__,
3961 				task->tk_status);
3962 			nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3963 			task->tk_status = 0;
3964 			return -EAGAIN;
3965 #endif /* CONFIG_NFS_V4_1 */
3966 		case -NFS4ERR_DELAY:
3967 			nfs_inc_server_stats(server, NFSIOS_DELAY);
3968 		case -NFS4ERR_GRACE:
3969 			rpc_delay(task, NFS4_POLL_RETRY_MAX);
3970 			task->tk_status = 0;
3971 			return -EAGAIN;
3972 		case -NFS4ERR_RETRY_UNCACHED_REP:
3973 		case -NFS4ERR_OLD_STATEID:
3974 			task->tk_status = 0;
3975 			return -EAGAIN;
3976 	}
3977 	task->tk_status = nfs4_map_errors(task->tk_status);
3978 	return 0;
3979 wait_on_recovery:
3980 	rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3981 	if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3982 		rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3983 	task->tk_status = 0;
3984 	return -EAGAIN;
3985 }
3986 
3987 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3988 				    nfs4_verifier *bootverf)
3989 {
3990 	__be32 verf[2];
3991 
3992 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3993 		/* An impossible timestamp guarantees this value
3994 		 * will never match a generated boot time. */
3995 		verf[0] = 0;
3996 		verf[1] = (__be32)(NSEC_PER_SEC + 1);
3997 	} else {
3998 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3999 		verf[0] = (__be32)nn->boot_time.tv_sec;
4000 		verf[1] = (__be32)nn->boot_time.tv_nsec;
4001 	}
4002 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
4003 }
4004 
4005 static unsigned int
4006 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4007 				   char *buf, size_t len)
4008 {
4009 	unsigned int result;
4010 
4011 	rcu_read_lock();
4012 	result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4013 				clp->cl_ipaddr,
4014 				rpc_peeraddr2str(clp->cl_rpcclient,
4015 							RPC_DISPLAY_ADDR),
4016 				rpc_peeraddr2str(clp->cl_rpcclient,
4017 							RPC_DISPLAY_PROTO));
4018 	rcu_read_unlock();
4019 	return result;
4020 }
4021 
4022 static unsigned int
4023 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4024 				char *buf, size_t len)
4025 {
4026 	char *nodename = clp->cl_rpcclient->cl_nodename;
4027 
4028 	if (nfs4_client_id_uniquifier[0] != '\0')
4029 		nodename = nfs4_client_id_uniquifier;
4030 	return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4031 				clp->rpc_ops->version, clp->cl_minorversion,
4032 				nodename);
4033 }
4034 
4035 /**
4036  * nfs4_proc_setclientid - Negotiate client ID
4037  * @clp: state data structure
4038  * @program: RPC program for NFSv4 callback service
4039  * @port: IP port number for NFS4 callback service
4040  * @cred: RPC credential to use for this call
4041  * @res: where to place the result
4042  *
4043  * Returns zero, a negative errno, or a negative NFS4ERR status code.
4044  */
4045 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4046 		unsigned short port, struct rpc_cred *cred,
4047 		struct nfs4_setclientid_res *res)
4048 {
4049 	nfs4_verifier sc_verifier;
4050 	struct nfs4_setclientid setclientid = {
4051 		.sc_verifier = &sc_verifier,
4052 		.sc_prog = program,
4053 		.sc_cb_ident = clp->cl_cb_ident,
4054 	};
4055 	struct rpc_message msg = {
4056 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4057 		.rpc_argp = &setclientid,
4058 		.rpc_resp = res,
4059 		.rpc_cred = cred,
4060 	};
4061 	int status;
4062 
4063 	/* nfs_client_id4 */
4064 	nfs4_init_boot_verifier(clp, &sc_verifier);
4065 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4066 		setclientid.sc_name_len =
4067 				nfs4_init_uniform_client_string(clp,
4068 						setclientid.sc_name,
4069 						sizeof(setclientid.sc_name));
4070 	else
4071 		setclientid.sc_name_len =
4072 				nfs4_init_nonuniform_client_string(clp,
4073 						setclientid.sc_name,
4074 						sizeof(setclientid.sc_name));
4075 	/* cb_client4 */
4076 	rcu_read_lock();
4077 	setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4078 				sizeof(setclientid.sc_netid),
4079 				rpc_peeraddr2str(clp->cl_rpcclient,
4080 							RPC_DISPLAY_NETID));
4081 	rcu_read_unlock();
4082 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4083 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4084 				clp->cl_ipaddr, port >> 8, port & 255);
4085 
4086 	dprintk("NFS call  setclientid auth=%s, '%.*s'\n",
4087 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
4088 		setclientid.sc_name_len, setclientid.sc_name);
4089 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4090 	dprintk("NFS reply setclientid: %d\n", status);
4091 	return status;
4092 }
4093 
4094 /**
4095  * nfs4_proc_setclientid_confirm - Confirm client ID
4096  * @clp: state data structure
4097  * @res: result of a previous SETCLIENTID
4098  * @cred: RPC credential to use for this call
4099  *
4100  * Returns zero, a negative errno, or a negative NFS4ERR status code.
4101  */
4102 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4103 		struct nfs4_setclientid_res *arg,
4104 		struct rpc_cred *cred)
4105 {
4106 	struct nfs_fsinfo fsinfo;
4107 	struct rpc_message msg = {
4108 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4109 		.rpc_argp = arg,
4110 		.rpc_resp = &fsinfo,
4111 		.rpc_cred = cred,
4112 	};
4113 	unsigned long now;
4114 	int status;
4115 
4116 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
4117 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
4118 		clp->cl_clientid);
4119 	now = jiffies;
4120 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4121 	if (status == 0) {
4122 		spin_lock(&clp->cl_lock);
4123 		clp->cl_lease_time = fsinfo.lease_time * HZ;
4124 		clp->cl_last_renewal = now;
4125 		spin_unlock(&clp->cl_lock);
4126 	}
4127 	dprintk("NFS reply setclientid_confirm: %d\n", status);
4128 	return status;
4129 }
4130 
4131 struct nfs4_delegreturndata {
4132 	struct nfs4_delegreturnargs args;
4133 	struct nfs4_delegreturnres res;
4134 	struct nfs_fh fh;
4135 	nfs4_stateid stateid;
4136 	unsigned long timestamp;
4137 	struct nfs_fattr fattr;
4138 	int rpc_status;
4139 };
4140 
4141 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4142 {
4143 	struct nfs4_delegreturndata *data = calldata;
4144 
4145 	if (!nfs4_sequence_done(task, &data->res.seq_res))
4146 		return;
4147 
4148 	switch (task->tk_status) {
4149 	case -NFS4ERR_STALE_STATEID:
4150 	case -NFS4ERR_EXPIRED:
4151 	case 0:
4152 		renew_lease(data->res.server, data->timestamp);
4153 		break;
4154 	default:
4155 		if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4156 				-EAGAIN) {
4157 			rpc_restart_call_prepare(task);
4158 			return;
4159 		}
4160 	}
4161 	data->rpc_status = task->tk_status;
4162 }
4163 
4164 static void nfs4_delegreturn_release(void *calldata)
4165 {
4166 	kfree(calldata);
4167 }
4168 
4169 #if defined(CONFIG_NFS_V4_1)
4170 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4171 {
4172 	struct nfs4_delegreturndata *d_data;
4173 
4174 	d_data = (struct nfs4_delegreturndata *)data;
4175 
4176 	nfs4_setup_sequence(d_data->res.server,
4177 			&d_data->args.seq_args,
4178 			&d_data->res.seq_res,
4179 			task);
4180 }
4181 #endif /* CONFIG_NFS_V4_1 */
4182 
4183 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4184 #if defined(CONFIG_NFS_V4_1)
4185 	.rpc_call_prepare = nfs4_delegreturn_prepare,
4186 #endif /* CONFIG_NFS_V4_1 */
4187 	.rpc_call_done = nfs4_delegreturn_done,
4188 	.rpc_release = nfs4_delegreturn_release,
4189 };
4190 
4191 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4192 {
4193 	struct nfs4_delegreturndata *data;
4194 	struct nfs_server *server = NFS_SERVER(inode);
4195 	struct rpc_task *task;
4196 	struct rpc_message msg = {
4197 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4198 		.rpc_cred = cred,
4199 	};
4200 	struct rpc_task_setup task_setup_data = {
4201 		.rpc_client = server->client,
4202 		.rpc_message = &msg,
4203 		.callback_ops = &nfs4_delegreturn_ops,
4204 		.flags = RPC_TASK_ASYNC,
4205 	};
4206 	int status = 0;
4207 
4208 	data = kzalloc(sizeof(*data), GFP_NOFS);
4209 	if (data == NULL)
4210 		return -ENOMEM;
4211 	nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4212 	data->args.fhandle = &data->fh;
4213 	data->args.stateid = &data->stateid;
4214 	data->args.bitmask = server->cache_consistency_bitmask;
4215 	nfs_copy_fh(&data->fh, NFS_FH(inode));
4216 	nfs4_stateid_copy(&data->stateid, stateid);
4217 	data->res.fattr = &data->fattr;
4218 	data->res.server = server;
4219 	nfs_fattr_init(data->res.fattr);
4220 	data->timestamp = jiffies;
4221 	data->rpc_status = 0;
4222 
4223 	task_setup_data.callback_data = data;
4224 	msg.rpc_argp = &data->args;
4225 	msg.rpc_resp = &data->res;
4226 	task = rpc_run_task(&task_setup_data);
4227 	if (IS_ERR(task))
4228 		return PTR_ERR(task);
4229 	if (!issync)
4230 		goto out;
4231 	status = nfs4_wait_for_completion_rpc_task(task);
4232 	if (status != 0)
4233 		goto out;
4234 	status = data->rpc_status;
4235 	if (status == 0)
4236 		nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4237 	else
4238 		nfs_refresh_inode(inode, &data->fattr);
4239 out:
4240 	rpc_put_task(task);
4241 	return status;
4242 }
4243 
4244 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4245 {
4246 	struct nfs_server *server = NFS_SERVER(inode);
4247 	struct nfs4_exception exception = { };
4248 	int err;
4249 	do {
4250 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4251 		switch (err) {
4252 			case -NFS4ERR_STALE_STATEID:
4253 			case -NFS4ERR_EXPIRED:
4254 			case 0:
4255 				return 0;
4256 		}
4257 		err = nfs4_handle_exception(server, err, &exception);
4258 	} while (exception.retry);
4259 	return err;
4260 }
4261 
4262 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4263 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4264 
4265 /*
4266  * sleep, with exponential backoff, and retry the LOCK operation.
4267  */
4268 static unsigned long
4269 nfs4_set_lock_task_retry(unsigned long timeout)
4270 {
4271 	freezable_schedule_timeout_killable(timeout);
4272 	timeout <<= 1;
4273 	if (timeout > NFS4_LOCK_MAXTIMEOUT)
4274 		return NFS4_LOCK_MAXTIMEOUT;
4275 	return timeout;
4276 }
4277 
4278 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4279 {
4280 	struct inode *inode = state->inode;
4281 	struct nfs_server *server = NFS_SERVER(inode);
4282 	struct nfs_client *clp = server->nfs_client;
4283 	struct nfs_lockt_args arg = {
4284 		.fh = NFS_FH(inode),
4285 		.fl = request,
4286 	};
4287 	struct nfs_lockt_res res = {
4288 		.denied = request,
4289 	};
4290 	struct rpc_message msg = {
4291 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4292 		.rpc_argp       = &arg,
4293 		.rpc_resp       = &res,
4294 		.rpc_cred	= state->owner->so_cred,
4295 	};
4296 	struct nfs4_lock_state *lsp;
4297 	int status;
4298 
4299 	arg.lock_owner.clientid = clp->cl_clientid;
4300 	status = nfs4_set_lock_state(state, request);
4301 	if (status != 0)
4302 		goto out;
4303 	lsp = request->fl_u.nfs4_fl.owner;
4304 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
4305 	arg.lock_owner.s_dev = server->s_dev;
4306 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4307 	switch (status) {
4308 		case 0:
4309 			request->fl_type = F_UNLCK;
4310 			break;
4311 		case -NFS4ERR_DENIED:
4312 			status = 0;
4313 	}
4314 	request->fl_ops->fl_release_private(request);
4315 out:
4316 	return status;
4317 }
4318 
4319 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320 {
4321 	struct nfs4_exception exception = { };
4322 	int err;
4323 
4324 	do {
4325 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
4326 				_nfs4_proc_getlk(state, cmd, request),
4327 				&exception);
4328 	} while (exception.retry);
4329 	return err;
4330 }
4331 
4332 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4333 {
4334 	int res = 0;
4335 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4336 		case FL_POSIX:
4337 			res = posix_lock_file_wait(file, fl);
4338 			break;
4339 		case FL_FLOCK:
4340 			res = flock_lock_file_wait(file, fl);
4341 			break;
4342 		default:
4343 			BUG();
4344 	}
4345 	return res;
4346 }
4347 
4348 struct nfs4_unlockdata {
4349 	struct nfs_locku_args arg;
4350 	struct nfs_locku_res res;
4351 	struct nfs4_lock_state *lsp;
4352 	struct nfs_open_context *ctx;
4353 	struct file_lock fl;
4354 	const struct nfs_server *server;
4355 	unsigned long timestamp;
4356 };
4357 
4358 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4359 		struct nfs_open_context *ctx,
4360 		struct nfs4_lock_state *lsp,
4361 		struct nfs_seqid *seqid)
4362 {
4363 	struct nfs4_unlockdata *p;
4364 	struct inode *inode = lsp->ls_state->inode;
4365 
4366 	p = kzalloc(sizeof(*p), GFP_NOFS);
4367 	if (p == NULL)
4368 		return NULL;
4369 	p->arg.fh = NFS_FH(inode);
4370 	p->arg.fl = &p->fl;
4371 	p->arg.seqid = seqid;
4372 	p->res.seqid = seqid;
4373 	p->arg.stateid = &lsp->ls_stateid;
4374 	p->lsp = lsp;
4375 	atomic_inc(&lsp->ls_count);
4376 	/* Ensure we don't close file until we're done freeing locks! */
4377 	p->ctx = get_nfs_open_context(ctx);
4378 	memcpy(&p->fl, fl, sizeof(p->fl));
4379 	p->server = NFS_SERVER(inode);
4380 	return p;
4381 }
4382 
4383 static void nfs4_locku_release_calldata(void *data)
4384 {
4385 	struct nfs4_unlockdata *calldata = data;
4386 	nfs_free_seqid(calldata->arg.seqid);
4387 	nfs4_put_lock_state(calldata->lsp);
4388 	put_nfs_open_context(calldata->ctx);
4389 	kfree(calldata);
4390 }
4391 
4392 static void nfs4_locku_done(struct rpc_task *task, void *data)
4393 {
4394 	struct nfs4_unlockdata *calldata = data;
4395 
4396 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4397 		return;
4398 	switch (task->tk_status) {
4399 		case 0:
4400 			nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4401 					&calldata->res.stateid);
4402 			renew_lease(calldata->server, calldata->timestamp);
4403 			break;
4404 		case -NFS4ERR_BAD_STATEID:
4405 		case -NFS4ERR_OLD_STATEID:
4406 		case -NFS4ERR_STALE_STATEID:
4407 		case -NFS4ERR_EXPIRED:
4408 			break;
4409 		default:
4410 			if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4411 				rpc_restart_call_prepare(task);
4412 	}
4413 	nfs_release_seqid(calldata->arg.seqid);
4414 }
4415 
4416 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4417 {
4418 	struct nfs4_unlockdata *calldata = data;
4419 
4420 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4421 		return;
4422 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4423 		/* Note: exit _without_ running nfs4_locku_done */
4424 		task->tk_action = NULL;
4425 		nfs4_sequence_done(task, &calldata->res.seq_res);
4426 		return;
4427 	}
4428 	calldata->timestamp = jiffies;
4429 	if (nfs4_setup_sequence(calldata->server,
4430 				&calldata->arg.seq_args,
4431 				&calldata->res.seq_res,
4432 				task) != 0)
4433 		nfs_release_seqid(calldata->arg.seqid);
4434 }
4435 
4436 static const struct rpc_call_ops nfs4_locku_ops = {
4437 	.rpc_call_prepare = nfs4_locku_prepare,
4438 	.rpc_call_done = nfs4_locku_done,
4439 	.rpc_release = nfs4_locku_release_calldata,
4440 };
4441 
4442 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4443 		struct nfs_open_context *ctx,
4444 		struct nfs4_lock_state *lsp,
4445 		struct nfs_seqid *seqid)
4446 {
4447 	struct nfs4_unlockdata *data;
4448 	struct rpc_message msg = {
4449 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4450 		.rpc_cred = ctx->cred,
4451 	};
4452 	struct rpc_task_setup task_setup_data = {
4453 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4454 		.rpc_message = &msg,
4455 		.callback_ops = &nfs4_locku_ops,
4456 		.workqueue = nfsiod_workqueue,
4457 		.flags = RPC_TASK_ASYNC,
4458 	};
4459 
4460 	/* Ensure this is an unlock - when canceling a lock, the
4461 	 * canceled lock is passed in, and it won't be an unlock.
4462 	 */
4463 	fl->fl_type = F_UNLCK;
4464 
4465 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4466 	if (data == NULL) {
4467 		nfs_free_seqid(seqid);
4468 		return ERR_PTR(-ENOMEM);
4469 	}
4470 
4471 	nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4472 	msg.rpc_argp = &data->arg;
4473 	msg.rpc_resp = &data->res;
4474 	task_setup_data.callback_data = data;
4475 	return rpc_run_task(&task_setup_data);
4476 }
4477 
4478 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4479 {
4480 	struct nfs_inode *nfsi = NFS_I(state->inode);
4481 	struct nfs_seqid *seqid;
4482 	struct nfs4_lock_state *lsp;
4483 	struct rpc_task *task;
4484 	int status = 0;
4485 	unsigned char fl_flags = request->fl_flags;
4486 
4487 	status = nfs4_set_lock_state(state, request);
4488 	/* Unlock _before_ we do the RPC call */
4489 	request->fl_flags |= FL_EXISTS;
4490 	down_read(&nfsi->rwsem);
4491 	if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4492 		up_read(&nfsi->rwsem);
4493 		goto out;
4494 	}
4495 	up_read(&nfsi->rwsem);
4496 	if (status != 0)
4497 		goto out;
4498 	/* Is this a delegated lock? */
4499 	if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4500 		goto out;
4501 	lsp = request->fl_u.nfs4_fl.owner;
4502 	seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4503 	status = -ENOMEM;
4504 	if (seqid == NULL)
4505 		goto out;
4506 	task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4507 	status = PTR_ERR(task);
4508 	if (IS_ERR(task))
4509 		goto out;
4510 	status = nfs4_wait_for_completion_rpc_task(task);
4511 	rpc_put_task(task);
4512 out:
4513 	request->fl_flags = fl_flags;
4514 	return status;
4515 }
4516 
4517 struct nfs4_lockdata {
4518 	struct nfs_lock_args arg;
4519 	struct nfs_lock_res res;
4520 	struct nfs4_lock_state *lsp;
4521 	struct nfs_open_context *ctx;
4522 	struct file_lock fl;
4523 	unsigned long timestamp;
4524 	int rpc_status;
4525 	int cancelled;
4526 	struct nfs_server *server;
4527 };
4528 
4529 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4530 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4531 		gfp_t gfp_mask)
4532 {
4533 	struct nfs4_lockdata *p;
4534 	struct inode *inode = lsp->ls_state->inode;
4535 	struct nfs_server *server = NFS_SERVER(inode);
4536 
4537 	p = kzalloc(sizeof(*p), gfp_mask);
4538 	if (p == NULL)
4539 		return NULL;
4540 
4541 	p->arg.fh = NFS_FH(inode);
4542 	p->arg.fl = &p->fl;
4543 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4544 	if (p->arg.open_seqid == NULL)
4545 		goto out_free;
4546 	p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4547 	if (p->arg.lock_seqid == NULL)
4548 		goto out_free_seqid;
4549 	p->arg.lock_stateid = &lsp->ls_stateid;
4550 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4551 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4552 	p->arg.lock_owner.s_dev = server->s_dev;
4553 	p->res.lock_seqid = p->arg.lock_seqid;
4554 	p->lsp = lsp;
4555 	p->server = server;
4556 	atomic_inc(&lsp->ls_count);
4557 	p->ctx = get_nfs_open_context(ctx);
4558 	memcpy(&p->fl, fl, sizeof(p->fl));
4559 	return p;
4560 out_free_seqid:
4561 	nfs_free_seqid(p->arg.open_seqid);
4562 out_free:
4563 	kfree(p);
4564 	return NULL;
4565 }
4566 
4567 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4568 {
4569 	struct nfs4_lockdata *data = calldata;
4570 	struct nfs4_state *state = data->lsp->ls_state;
4571 
4572 	dprintk("%s: begin!\n", __func__);
4573 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4574 		return;
4575 	/* Do we need to do an open_to_lock_owner? */
4576 	if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4577 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4578 			goto out_release_lock_seqid;
4579 		}
4580 		data->arg.open_stateid = &state->stateid;
4581 		data->arg.new_lock_owner = 1;
4582 		data->res.open_seqid = data->arg.open_seqid;
4583 	} else
4584 		data->arg.new_lock_owner = 0;
4585 	data->timestamp = jiffies;
4586 	if (nfs4_setup_sequence(data->server,
4587 				&data->arg.seq_args,
4588 				&data->res.seq_res,
4589 				task) == 0)
4590 		return;
4591 	nfs_release_seqid(data->arg.open_seqid);
4592 out_release_lock_seqid:
4593 	nfs_release_seqid(data->arg.lock_seqid);
4594 	dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4595 }
4596 
4597 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4598 {
4599 	struct nfs4_lockdata *data = calldata;
4600 
4601 	dprintk("%s: begin!\n", __func__);
4602 
4603 	if (!nfs4_sequence_done(task, &data->res.seq_res))
4604 		return;
4605 
4606 	data->rpc_status = task->tk_status;
4607 	if (data->arg.new_lock_owner != 0) {
4608 		if (data->rpc_status == 0)
4609 			nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4610 		else
4611 			goto out;
4612 	}
4613 	if (data->rpc_status == 0) {
4614 		nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4615 		set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4616 		renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4617 	}
4618 out:
4619 	dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4620 }
4621 
4622 static void nfs4_lock_release(void *calldata)
4623 {
4624 	struct nfs4_lockdata *data = calldata;
4625 
4626 	dprintk("%s: begin!\n", __func__);
4627 	nfs_free_seqid(data->arg.open_seqid);
4628 	if (data->cancelled != 0) {
4629 		struct rpc_task *task;
4630 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4631 				data->arg.lock_seqid);
4632 		if (!IS_ERR(task))
4633 			rpc_put_task_async(task);
4634 		dprintk("%s: cancelling lock!\n", __func__);
4635 	} else
4636 		nfs_free_seqid(data->arg.lock_seqid);
4637 	nfs4_put_lock_state(data->lsp);
4638 	put_nfs_open_context(data->ctx);
4639 	kfree(data);
4640 	dprintk("%s: done!\n", __func__);
4641 }
4642 
4643 static const struct rpc_call_ops nfs4_lock_ops = {
4644 	.rpc_call_prepare = nfs4_lock_prepare,
4645 	.rpc_call_done = nfs4_lock_done,
4646 	.rpc_release = nfs4_lock_release,
4647 };
4648 
4649 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4650 {
4651 	switch (error) {
4652 	case -NFS4ERR_ADMIN_REVOKED:
4653 	case -NFS4ERR_BAD_STATEID:
4654 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4655 		if (new_lock_owner != 0 ||
4656 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4657 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4658 		break;
4659 	case -NFS4ERR_STALE_STATEID:
4660 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4661 	case -NFS4ERR_EXPIRED:
4662 		nfs4_schedule_lease_recovery(server->nfs_client);
4663 	};
4664 }
4665 
4666 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4667 {
4668 	struct nfs4_lockdata *data;
4669 	struct rpc_task *task;
4670 	struct rpc_message msg = {
4671 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4672 		.rpc_cred = state->owner->so_cred,
4673 	};
4674 	struct rpc_task_setup task_setup_data = {
4675 		.rpc_client = NFS_CLIENT(state->inode),
4676 		.rpc_message = &msg,
4677 		.callback_ops = &nfs4_lock_ops,
4678 		.workqueue = nfsiod_workqueue,
4679 		.flags = RPC_TASK_ASYNC,
4680 	};
4681 	int ret;
4682 
4683 	dprintk("%s: begin!\n", __func__);
4684 	data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4685 			fl->fl_u.nfs4_fl.owner,
4686 			recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4687 	if (data == NULL)
4688 		return -ENOMEM;
4689 	if (IS_SETLKW(cmd))
4690 		data->arg.block = 1;
4691 	nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4692 	msg.rpc_argp = &data->arg;
4693 	msg.rpc_resp = &data->res;
4694 	task_setup_data.callback_data = data;
4695 	if (recovery_type > NFS_LOCK_NEW) {
4696 		if (recovery_type == NFS_LOCK_RECLAIM)
4697 			data->arg.reclaim = NFS_LOCK_RECLAIM;
4698 		nfs4_set_sequence_privileged(&data->arg.seq_args);
4699 	}
4700 	task = rpc_run_task(&task_setup_data);
4701 	if (IS_ERR(task))
4702 		return PTR_ERR(task);
4703 	ret = nfs4_wait_for_completion_rpc_task(task);
4704 	if (ret == 0) {
4705 		ret = data->rpc_status;
4706 		if (ret)
4707 			nfs4_handle_setlk_error(data->server, data->lsp,
4708 					data->arg.new_lock_owner, ret);
4709 	} else
4710 		data->cancelled = 1;
4711 	rpc_put_task(task);
4712 	dprintk("%s: done, ret = %d!\n", __func__, ret);
4713 	return ret;
4714 }
4715 
4716 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4717 {
4718 	struct nfs_server *server = NFS_SERVER(state->inode);
4719 	struct nfs4_exception exception = {
4720 		.inode = state->inode,
4721 	};
4722 	int err;
4723 
4724 	do {
4725 		/* Cache the lock if possible... */
4726 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4727 			return 0;
4728 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4729 		if (err != -NFS4ERR_DELAY)
4730 			break;
4731 		nfs4_handle_exception(server, err, &exception);
4732 	} while (exception.retry);
4733 	return err;
4734 }
4735 
4736 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4737 {
4738 	struct nfs_server *server = NFS_SERVER(state->inode);
4739 	struct nfs4_exception exception = {
4740 		.inode = state->inode,
4741 	};
4742 	int err;
4743 
4744 	err = nfs4_set_lock_state(state, request);
4745 	if (err != 0)
4746 		return err;
4747 	do {
4748 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4749 			return 0;
4750 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4751 		switch (err) {
4752 		default:
4753 			goto out;
4754 		case -NFS4ERR_GRACE:
4755 		case -NFS4ERR_DELAY:
4756 			nfs4_handle_exception(server, err, &exception);
4757 			err = 0;
4758 		}
4759 	} while (exception.retry);
4760 out:
4761 	return err;
4762 }
4763 
4764 #if defined(CONFIG_NFS_V4_1)
4765 /**
4766  * nfs41_check_expired_locks - possibly free a lock stateid
4767  *
4768  * @state: NFSv4 state for an inode
4769  *
4770  * Returns NFS_OK if recovery for this stateid is now finished.
4771  * Otherwise a negative NFS4ERR value is returned.
4772  */
4773 static int nfs41_check_expired_locks(struct nfs4_state *state)
4774 {
4775 	int status, ret = -NFS4ERR_BAD_STATEID;
4776 	struct nfs4_lock_state *lsp;
4777 	struct nfs_server *server = NFS_SERVER(state->inode);
4778 
4779 	list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4780 		if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4781 			status = nfs41_test_stateid(server, &lsp->ls_stateid);
4782 			if (status != NFS_OK) {
4783 				/* Free the stateid unless the server
4784 				 * informs us the stateid is unrecognized. */
4785 				if (status != -NFS4ERR_BAD_STATEID)
4786 					nfs41_free_stateid(server,
4787 							&lsp->ls_stateid);
4788 				clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4789 				ret = status;
4790 			}
4791 		}
4792 	};
4793 
4794 	return ret;
4795 }
4796 
4797 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4798 {
4799 	int status = NFS_OK;
4800 
4801 	if (test_bit(LK_STATE_IN_USE, &state->flags))
4802 		status = nfs41_check_expired_locks(state);
4803 	if (status != NFS_OK)
4804 		status = nfs4_lock_expired(state, request);
4805 	return status;
4806 }
4807 #endif
4808 
4809 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4810 {
4811 	struct nfs_inode *nfsi = NFS_I(state->inode);
4812 	unsigned char fl_flags = request->fl_flags;
4813 	int status = -ENOLCK;
4814 
4815 	if ((fl_flags & FL_POSIX) &&
4816 			!test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4817 		goto out;
4818 	/* Is this a delegated open? */
4819 	status = nfs4_set_lock_state(state, request);
4820 	if (status != 0)
4821 		goto out;
4822 	request->fl_flags |= FL_ACCESS;
4823 	status = do_vfs_lock(request->fl_file, request);
4824 	if (status < 0)
4825 		goto out;
4826 	down_read(&nfsi->rwsem);
4827 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4828 		/* Yes: cache locks! */
4829 		/* ...but avoid races with delegation recall... */
4830 		request->fl_flags = fl_flags & ~FL_SLEEP;
4831 		status = do_vfs_lock(request->fl_file, request);
4832 		goto out_unlock;
4833 	}
4834 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4835 	if (status != 0)
4836 		goto out_unlock;
4837 	/* Note: we always want to sleep here! */
4838 	request->fl_flags = fl_flags | FL_SLEEP;
4839 	if (do_vfs_lock(request->fl_file, request) < 0)
4840 		printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4841 			"manager!\n", __func__);
4842 out_unlock:
4843 	up_read(&nfsi->rwsem);
4844 out:
4845 	request->fl_flags = fl_flags;
4846 	return status;
4847 }
4848 
4849 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4850 {
4851 	struct nfs4_exception exception = {
4852 		.state = state,
4853 		.inode = state->inode,
4854 	};
4855 	int err;
4856 
4857 	do {
4858 		err = _nfs4_proc_setlk(state, cmd, request);
4859 		if (err == -NFS4ERR_DENIED)
4860 			err = -EAGAIN;
4861 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
4862 				err, &exception);
4863 	} while (exception.retry);
4864 	return err;
4865 }
4866 
4867 static int
4868 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4869 {
4870 	struct nfs_open_context *ctx;
4871 	struct nfs4_state *state;
4872 	unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4873 	int status;
4874 
4875 	/* verify open state */
4876 	ctx = nfs_file_open_context(filp);
4877 	state = ctx->state;
4878 
4879 	if (request->fl_start < 0 || request->fl_end < 0)
4880 		return -EINVAL;
4881 
4882 	if (IS_GETLK(cmd)) {
4883 		if (state != NULL)
4884 			return nfs4_proc_getlk(state, F_GETLK, request);
4885 		return 0;
4886 	}
4887 
4888 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4889 		return -EINVAL;
4890 
4891 	if (request->fl_type == F_UNLCK) {
4892 		if (state != NULL)
4893 			return nfs4_proc_unlck(state, cmd, request);
4894 		return 0;
4895 	}
4896 
4897 	if (state == NULL)
4898 		return -ENOLCK;
4899 	/*
4900 	 * Don't rely on the VFS having checked the file open mode,
4901 	 * since it won't do this for flock() locks.
4902 	 */
4903 	switch (request->fl_type) {
4904 	case F_RDLCK:
4905 		if (!(filp->f_mode & FMODE_READ))
4906 			return -EBADF;
4907 		break;
4908 	case F_WRLCK:
4909 		if (!(filp->f_mode & FMODE_WRITE))
4910 			return -EBADF;
4911 	}
4912 
4913 	do {
4914 		status = nfs4_proc_setlk(state, cmd, request);
4915 		if ((status != -EAGAIN) || IS_SETLK(cmd))
4916 			break;
4917 		timeout = nfs4_set_lock_task_retry(timeout);
4918 		status = -ERESTARTSYS;
4919 		if (signalled())
4920 			break;
4921 	} while(status < 0);
4922 	return status;
4923 }
4924 
4925 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4926 {
4927 	struct nfs_server *server = NFS_SERVER(state->inode);
4928 	struct nfs4_exception exception = { };
4929 	int err;
4930 
4931 	err = nfs4_set_lock_state(state, fl);
4932 	if (err != 0)
4933 		goto out;
4934 	do {
4935 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4936 		switch (err) {
4937 			default:
4938 				printk(KERN_ERR "NFS: %s: unhandled error "
4939 					"%d.\n", __func__, err);
4940 			case 0:
4941 			case -ESTALE:
4942 				goto out;
4943 			case -NFS4ERR_EXPIRED:
4944 				nfs4_schedule_stateid_recovery(server, state);
4945 			case -NFS4ERR_STALE_CLIENTID:
4946 			case -NFS4ERR_STALE_STATEID:
4947 				nfs4_schedule_lease_recovery(server->nfs_client);
4948 				goto out;
4949 			case -NFS4ERR_BADSESSION:
4950 			case -NFS4ERR_BADSLOT:
4951 			case -NFS4ERR_BAD_HIGH_SLOT:
4952 			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4953 			case -NFS4ERR_DEADSESSION:
4954 				nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4955 				goto out;
4956 			case -ERESTARTSYS:
4957 				/*
4958 				 * The show must go on: exit, but mark the
4959 				 * stateid as needing recovery.
4960 				 */
4961 			case -NFS4ERR_DELEG_REVOKED:
4962 			case -NFS4ERR_ADMIN_REVOKED:
4963 			case -NFS4ERR_BAD_STATEID:
4964 			case -NFS4ERR_OPENMODE:
4965 				nfs4_schedule_stateid_recovery(server, state);
4966 				err = 0;
4967 				goto out;
4968 			case -ENOMEM:
4969 			case -NFS4ERR_DENIED:
4970 				/* kill_proc(fl->fl_pid, SIGLOST, 1); */
4971 				err = 0;
4972 				goto out;
4973 			case -NFS4ERR_DELAY:
4974 				break;
4975 		}
4976 		err = nfs4_handle_exception(server, err, &exception);
4977 	} while (exception.retry);
4978 out:
4979 	return err;
4980 }
4981 
4982 struct nfs_release_lockowner_data {
4983 	struct nfs4_lock_state *lsp;
4984 	struct nfs_server *server;
4985 	struct nfs_release_lockowner_args args;
4986 };
4987 
4988 static void nfs4_release_lockowner_release(void *calldata)
4989 {
4990 	struct nfs_release_lockowner_data *data = calldata;
4991 	nfs4_free_lock_state(data->server, data->lsp);
4992 	kfree(calldata);
4993 }
4994 
4995 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4996 	.rpc_release = nfs4_release_lockowner_release,
4997 };
4998 
4999 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5000 {
5001 	struct nfs_server *server = lsp->ls_state->owner->so_server;
5002 	struct nfs_release_lockowner_data *data;
5003 	struct rpc_message msg = {
5004 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5005 	};
5006 
5007 	if (server->nfs_client->cl_mvops->minor_version != 0)
5008 		return -EINVAL;
5009 	data = kmalloc(sizeof(*data), GFP_NOFS);
5010 	if (!data)
5011 		return -ENOMEM;
5012 	data->lsp = lsp;
5013 	data->server = server;
5014 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5015 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5016 	data->args.lock_owner.s_dev = server->s_dev;
5017 	msg.rpc_argp = &data->args;
5018 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5019 	return 0;
5020 }
5021 
5022 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5023 
5024 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5025 				   const void *buf, size_t buflen,
5026 				   int flags, int type)
5027 {
5028 	if (strcmp(key, "") != 0)
5029 		return -EINVAL;
5030 
5031 	return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5032 }
5033 
5034 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5035 				   void *buf, size_t buflen, int type)
5036 {
5037 	if (strcmp(key, "") != 0)
5038 		return -EINVAL;
5039 
5040 	return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5041 }
5042 
5043 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5044 				       size_t list_len, const char *name,
5045 				       size_t name_len, int type)
5046 {
5047 	size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5048 
5049 	if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5050 		return 0;
5051 
5052 	if (list && len <= list_len)
5053 		memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5054 	return len;
5055 }
5056 
5057 /*
5058  * nfs_fhget will use either the mounted_on_fileid or the fileid
5059  */
5060 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5061 {
5062 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5063 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5064 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5065 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5066 		return;
5067 
5068 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5069 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5070 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5071 	fattr->nlink = 2;
5072 }
5073 
5074 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5075 				   const struct qstr *name,
5076 				   struct nfs4_fs_locations *fs_locations,
5077 				   struct page *page)
5078 {
5079 	struct nfs_server *server = NFS_SERVER(dir);
5080 	u32 bitmask[2] = {
5081 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5082 	};
5083 	struct nfs4_fs_locations_arg args = {
5084 		.dir_fh = NFS_FH(dir),
5085 		.name = name,
5086 		.page = page,
5087 		.bitmask = bitmask,
5088 	};
5089 	struct nfs4_fs_locations_res res = {
5090 		.fs_locations = fs_locations,
5091 	};
5092 	struct rpc_message msg = {
5093 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5094 		.rpc_argp = &args,
5095 		.rpc_resp = &res,
5096 	};
5097 	int status;
5098 
5099 	dprintk("%s: start\n", __func__);
5100 
5101 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
5102 	 * is not supported */
5103 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5104 		bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5105 	else
5106 		bitmask[0] |= FATTR4_WORD0_FILEID;
5107 
5108 	nfs_fattr_init(&fs_locations->fattr);
5109 	fs_locations->server = server;
5110 	fs_locations->nlocations = 0;
5111 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5112 	dprintk("%s: returned status = %d\n", __func__, status);
5113 	return status;
5114 }
5115 
5116 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5117 			   const struct qstr *name,
5118 			   struct nfs4_fs_locations *fs_locations,
5119 			   struct page *page)
5120 {
5121 	struct nfs4_exception exception = { };
5122 	int err;
5123 	do {
5124 		err = nfs4_handle_exception(NFS_SERVER(dir),
5125 				_nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5126 				&exception);
5127 	} while (exception.retry);
5128 	return err;
5129 }
5130 
5131 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5132 {
5133 	int status;
5134 	struct nfs4_secinfo_arg args = {
5135 		.dir_fh = NFS_FH(dir),
5136 		.name   = name,
5137 	};
5138 	struct nfs4_secinfo_res res = {
5139 		.flavors     = flavors,
5140 	};
5141 	struct rpc_message msg = {
5142 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5143 		.rpc_argp = &args,
5144 		.rpc_resp = &res,
5145 	};
5146 
5147 	dprintk("NFS call  secinfo %s\n", name->name);
5148 	status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5149 	dprintk("NFS reply  secinfo: %d\n", status);
5150 	return status;
5151 }
5152 
5153 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5154 		      struct nfs4_secinfo_flavors *flavors)
5155 {
5156 	struct nfs4_exception exception = { };
5157 	int err;
5158 	do {
5159 		err = nfs4_handle_exception(NFS_SERVER(dir),
5160 				_nfs4_proc_secinfo(dir, name, flavors),
5161 				&exception);
5162 	} while (exception.retry);
5163 	return err;
5164 }
5165 
5166 #ifdef CONFIG_NFS_V4_1
5167 /*
5168  * Check the exchange flags returned by the server for invalid flags, having
5169  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5170  * DS flags set.
5171  */
5172 static int nfs4_check_cl_exchange_flags(u32 flags)
5173 {
5174 	if (flags & ~EXCHGID4_FLAG_MASK_R)
5175 		goto out_inval;
5176 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5177 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5178 		goto out_inval;
5179 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5180 		goto out_inval;
5181 	return NFS_OK;
5182 out_inval:
5183 	return -NFS4ERR_INVAL;
5184 }
5185 
5186 static bool
5187 nfs41_same_server_scope(struct nfs41_server_scope *a,
5188 			struct nfs41_server_scope *b)
5189 {
5190 	if (a->server_scope_sz == b->server_scope_sz &&
5191 	    memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5192 		return true;
5193 
5194 	return false;
5195 }
5196 
5197 /*
5198  * nfs4_proc_bind_conn_to_session()
5199  *
5200  * The 4.1 client currently uses the same TCP connection for the
5201  * fore and backchannel.
5202  */
5203 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5204 {
5205 	int status;
5206 	struct nfs41_bind_conn_to_session_res res;
5207 	struct rpc_message msg = {
5208 		.rpc_proc =
5209 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5210 		.rpc_argp = clp,
5211 		.rpc_resp = &res,
5212 		.rpc_cred = cred,
5213 	};
5214 
5215 	dprintk("--> %s\n", __func__);
5216 
5217 	res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5218 	if (unlikely(res.session == NULL)) {
5219 		status = -ENOMEM;
5220 		goto out;
5221 	}
5222 
5223 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5224 	if (status == 0) {
5225 		if (memcmp(res.session->sess_id.data,
5226 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5227 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
5228 			status = -EIO;
5229 			goto out_session;
5230 		}
5231 		if (res.dir != NFS4_CDFS4_BOTH) {
5232 			dprintk("NFS: %s: Unexpected direction from server\n",
5233 				__func__);
5234 			status = -EIO;
5235 			goto out_session;
5236 		}
5237 		if (res.use_conn_in_rdma_mode) {
5238 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
5239 				__func__);
5240 			status = -EIO;
5241 			goto out_session;
5242 		}
5243 	}
5244 out_session:
5245 	kfree(res.session);
5246 out:
5247 	dprintk("<-- %s status= %d\n", __func__, status);
5248 	return status;
5249 }
5250 
5251 /*
5252  * nfs4_proc_exchange_id()
5253  *
5254  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5255  *
5256  * Since the clientid has expired, all compounds using sessions
5257  * associated with the stale clientid will be returning
5258  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5259  * be in some phase of session reset.
5260  */
5261 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5262 {
5263 	nfs4_verifier verifier;
5264 	struct nfs41_exchange_id_args args = {
5265 		.verifier = &verifier,
5266 		.client = clp,
5267 		.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5268 	};
5269 	struct nfs41_exchange_id_res res = {
5270 		0
5271 	};
5272 	int status;
5273 	struct rpc_message msg = {
5274 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5275 		.rpc_argp = &args,
5276 		.rpc_resp = &res,
5277 		.rpc_cred = cred,
5278 	};
5279 
5280 	nfs4_init_boot_verifier(clp, &verifier);
5281 	args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5282 							sizeof(args.id));
5283 	dprintk("NFS call  exchange_id auth=%s, '%.*s'\n",
5284 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
5285 		args.id_len, args.id);
5286 
5287 	res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5288 					GFP_NOFS);
5289 	if (unlikely(res.server_owner == NULL)) {
5290 		status = -ENOMEM;
5291 		goto out;
5292 	}
5293 
5294 	res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5295 					GFP_NOFS);
5296 	if (unlikely(res.server_scope == NULL)) {
5297 		status = -ENOMEM;
5298 		goto out_server_owner;
5299 	}
5300 
5301 	res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5302 	if (unlikely(res.impl_id == NULL)) {
5303 		status = -ENOMEM;
5304 		goto out_server_scope;
5305 	}
5306 
5307 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5308 	if (status == 0)
5309 		status = nfs4_check_cl_exchange_flags(res.flags);
5310 
5311 	if (status == 0) {
5312 		clp->cl_clientid = res.clientid;
5313 		clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5314 		if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5315 			clp->cl_seqid = res.seqid;
5316 
5317 		kfree(clp->cl_serverowner);
5318 		clp->cl_serverowner = res.server_owner;
5319 		res.server_owner = NULL;
5320 
5321 		/* use the most recent implementation id */
5322 		kfree(clp->cl_implid);
5323 		clp->cl_implid = res.impl_id;
5324 
5325 		if (clp->cl_serverscope != NULL &&
5326 		    !nfs41_same_server_scope(clp->cl_serverscope,
5327 					     res.server_scope)) {
5328 			dprintk("%s: server_scope mismatch detected\n",
5329 				__func__);
5330 			set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5331 			kfree(clp->cl_serverscope);
5332 			clp->cl_serverscope = NULL;
5333 		}
5334 
5335 		if (clp->cl_serverscope == NULL) {
5336 			clp->cl_serverscope = res.server_scope;
5337 			goto out;
5338 		}
5339 	} else
5340 		kfree(res.impl_id);
5341 
5342 out_server_owner:
5343 	kfree(res.server_owner);
5344 out_server_scope:
5345 	kfree(res.server_scope);
5346 out:
5347 	if (clp->cl_implid != NULL)
5348 		dprintk("NFS reply exchange_id: Server Implementation ID: "
5349 			"domain: %s, name: %s, date: %llu,%u\n",
5350 			clp->cl_implid->domain, clp->cl_implid->name,
5351 			clp->cl_implid->date.seconds,
5352 			clp->cl_implid->date.nseconds);
5353 	dprintk("NFS reply exchange_id: %d\n", status);
5354 	return status;
5355 }
5356 
5357 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5358 		struct rpc_cred *cred)
5359 {
5360 	struct rpc_message msg = {
5361 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5362 		.rpc_argp = clp,
5363 		.rpc_cred = cred,
5364 	};
5365 	int status;
5366 
5367 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5368 	if (status)
5369 		dprintk("NFS: Got error %d from the server %s on "
5370 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
5371 	return status;
5372 }
5373 
5374 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5375 		struct rpc_cred *cred)
5376 {
5377 	unsigned int loop;
5378 	int ret;
5379 
5380 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5381 		ret = _nfs4_proc_destroy_clientid(clp, cred);
5382 		switch (ret) {
5383 		case -NFS4ERR_DELAY:
5384 		case -NFS4ERR_CLIENTID_BUSY:
5385 			ssleep(1);
5386 			break;
5387 		default:
5388 			return ret;
5389 		}
5390 	}
5391 	return 0;
5392 }
5393 
5394 int nfs4_destroy_clientid(struct nfs_client *clp)
5395 {
5396 	struct rpc_cred *cred;
5397 	int ret = 0;
5398 
5399 	if (clp->cl_mvops->minor_version < 1)
5400 		goto out;
5401 	if (clp->cl_exchange_flags == 0)
5402 		goto out;
5403 	if (clp->cl_preserve_clid)
5404 		goto out;
5405 	cred = nfs4_get_exchange_id_cred(clp);
5406 	ret = nfs4_proc_destroy_clientid(clp, cred);
5407 	if (cred)
5408 		put_rpccred(cred);
5409 	switch (ret) {
5410 	case 0:
5411 	case -NFS4ERR_STALE_CLIENTID:
5412 		clp->cl_exchange_flags = 0;
5413 	}
5414 out:
5415 	return ret;
5416 }
5417 
5418 struct nfs4_get_lease_time_data {
5419 	struct nfs4_get_lease_time_args *args;
5420 	struct nfs4_get_lease_time_res *res;
5421 	struct nfs_client *clp;
5422 };
5423 
5424 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5425 					void *calldata)
5426 {
5427 	struct nfs4_get_lease_time_data *data =
5428 			(struct nfs4_get_lease_time_data *)calldata;
5429 
5430 	dprintk("--> %s\n", __func__);
5431 	/* just setup sequence, do not trigger session recovery
5432 	   since we're invoked within one */
5433 	nfs41_setup_sequence(data->clp->cl_session,
5434 			&data->args->la_seq_args,
5435 			&data->res->lr_seq_res,
5436 			task);
5437 	dprintk("<-- %s\n", __func__);
5438 }
5439 
5440 /*
5441  * Called from nfs4_state_manager thread for session setup, so don't recover
5442  * from sequence operation or clientid errors.
5443  */
5444 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5445 {
5446 	struct nfs4_get_lease_time_data *data =
5447 			(struct nfs4_get_lease_time_data *)calldata;
5448 
5449 	dprintk("--> %s\n", __func__);
5450 	if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5451 		return;
5452 	switch (task->tk_status) {
5453 	case -NFS4ERR_DELAY:
5454 	case -NFS4ERR_GRACE:
5455 		dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5456 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
5457 		task->tk_status = 0;
5458 		/* fall through */
5459 	case -NFS4ERR_RETRY_UNCACHED_REP:
5460 		rpc_restart_call_prepare(task);
5461 		return;
5462 	}
5463 	dprintk("<-- %s\n", __func__);
5464 }
5465 
5466 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5467 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
5468 	.rpc_call_done = nfs4_get_lease_time_done,
5469 };
5470 
5471 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5472 {
5473 	struct rpc_task *task;
5474 	struct nfs4_get_lease_time_args args;
5475 	struct nfs4_get_lease_time_res res = {
5476 		.lr_fsinfo = fsinfo,
5477 	};
5478 	struct nfs4_get_lease_time_data data = {
5479 		.args = &args,
5480 		.res = &res,
5481 		.clp = clp,
5482 	};
5483 	struct rpc_message msg = {
5484 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5485 		.rpc_argp = &args,
5486 		.rpc_resp = &res,
5487 	};
5488 	struct rpc_task_setup task_setup = {
5489 		.rpc_client = clp->cl_rpcclient,
5490 		.rpc_message = &msg,
5491 		.callback_ops = &nfs4_get_lease_time_ops,
5492 		.callback_data = &data,
5493 		.flags = RPC_TASK_TIMEOUT,
5494 	};
5495 	int status;
5496 
5497 	nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5498 	nfs4_set_sequence_privileged(&args.la_seq_args);
5499 	dprintk("--> %s\n", __func__);
5500 	task = rpc_run_task(&task_setup);
5501 
5502 	if (IS_ERR(task))
5503 		status = PTR_ERR(task);
5504 	else {
5505 		status = task->tk_status;
5506 		rpc_put_task(task);
5507 	}
5508 	dprintk("<-- %s return %d\n", __func__, status);
5509 
5510 	return status;
5511 }
5512 
5513 /*
5514  * Initialize the values to be used by the client in CREATE_SESSION
5515  * If nfs4_init_session set the fore channel request and response sizes,
5516  * use them.
5517  *
5518  * Set the back channel max_resp_sz_cached to zero to force the client to
5519  * always set csa_cachethis to FALSE because the current implementation
5520  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5521  */
5522 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5523 {
5524 	struct nfs4_session *session = args->client->cl_session;
5525 	unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5526 		     mxresp_sz = session->fc_target_max_resp_sz;
5527 
5528 	if (mxrqst_sz == 0)
5529 		mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5530 	if (mxresp_sz == 0)
5531 		mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5532 	/* Fore channel attributes */
5533 	args->fc_attrs.max_rqst_sz = mxrqst_sz;
5534 	args->fc_attrs.max_resp_sz = mxresp_sz;
5535 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
5536 	args->fc_attrs.max_reqs = max_session_slots;
5537 
5538 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5539 		"max_ops=%u max_reqs=%u\n",
5540 		__func__,
5541 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5542 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5543 
5544 	/* Back channel attributes */
5545 	args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5546 	args->bc_attrs.max_resp_sz = PAGE_SIZE;
5547 	args->bc_attrs.max_resp_sz_cached = 0;
5548 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5549 	args->bc_attrs.max_reqs = 1;
5550 
5551 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5552 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5553 		__func__,
5554 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5555 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5556 		args->bc_attrs.max_reqs);
5557 }
5558 
5559 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5560 {
5561 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
5562 	struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5563 
5564 	if (rcvd->max_resp_sz > sent->max_resp_sz)
5565 		return -EINVAL;
5566 	/*
5567 	 * Our requested max_ops is the minimum we need; we're not
5568 	 * prepared to break up compounds into smaller pieces than that.
5569 	 * So, no point even trying to continue if the server won't
5570 	 * cooperate:
5571 	 */
5572 	if (rcvd->max_ops < sent->max_ops)
5573 		return -EINVAL;
5574 	if (rcvd->max_reqs == 0)
5575 		return -EINVAL;
5576 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5577 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5578 	return 0;
5579 }
5580 
5581 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5582 {
5583 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
5584 	struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5585 
5586 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5587 		return -EINVAL;
5588 	if (rcvd->max_resp_sz < sent->max_resp_sz)
5589 		return -EINVAL;
5590 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5591 		return -EINVAL;
5592 	/* These would render the backchannel useless: */
5593 	if (rcvd->max_ops != sent->max_ops)
5594 		return -EINVAL;
5595 	if (rcvd->max_reqs != sent->max_reqs)
5596 		return -EINVAL;
5597 	return 0;
5598 }
5599 
5600 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5601 				     struct nfs4_session *session)
5602 {
5603 	int ret;
5604 
5605 	ret = nfs4_verify_fore_channel_attrs(args, session);
5606 	if (ret)
5607 		return ret;
5608 	return nfs4_verify_back_channel_attrs(args, session);
5609 }
5610 
5611 static int _nfs4_proc_create_session(struct nfs_client *clp,
5612 		struct rpc_cred *cred)
5613 {
5614 	struct nfs4_session *session = clp->cl_session;
5615 	struct nfs41_create_session_args args = {
5616 		.client = clp,
5617 		.cb_program = NFS4_CALLBACK,
5618 	};
5619 	struct nfs41_create_session_res res = {
5620 		.client = clp,
5621 	};
5622 	struct rpc_message msg = {
5623 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5624 		.rpc_argp = &args,
5625 		.rpc_resp = &res,
5626 		.rpc_cred = cred,
5627 	};
5628 	int status;
5629 
5630 	nfs4_init_channel_attrs(&args);
5631 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5632 
5633 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5634 
5635 	if (!status) {
5636 		/* Verify the session's negotiated channel_attrs values */
5637 		status = nfs4_verify_channel_attrs(&args, session);
5638 		/* Increment the clientid slot sequence id */
5639 		clp->cl_seqid++;
5640 	}
5641 
5642 	return status;
5643 }
5644 
5645 /*
5646  * Issues a CREATE_SESSION operation to the server.
5647  * It is the responsibility of the caller to verify the session is
5648  * expired before calling this routine.
5649  */
5650 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5651 {
5652 	int status;
5653 	unsigned *ptr;
5654 	struct nfs4_session *session = clp->cl_session;
5655 
5656 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5657 
5658 	status = _nfs4_proc_create_session(clp, cred);
5659 	if (status)
5660 		goto out;
5661 
5662 	/* Init or reset the session slot tables */
5663 	status = nfs4_setup_session_slot_tables(session);
5664 	dprintk("slot table setup returned %d\n", status);
5665 	if (status)
5666 		goto out;
5667 
5668 	ptr = (unsigned *)&session->sess_id.data[0];
5669 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5670 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5671 out:
5672 	dprintk("<-- %s\n", __func__);
5673 	return status;
5674 }
5675 
5676 /*
5677  * Issue the over-the-wire RPC DESTROY_SESSION.
5678  * The caller must serialize access to this routine.
5679  */
5680 int nfs4_proc_destroy_session(struct nfs4_session *session,
5681 		struct rpc_cred *cred)
5682 {
5683 	struct rpc_message msg = {
5684 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5685 		.rpc_argp = session,
5686 		.rpc_cred = cred,
5687 	};
5688 	int status = 0;
5689 
5690 	dprintk("--> nfs4_proc_destroy_session\n");
5691 
5692 	/* session is still being setup */
5693 	if (session->clp->cl_cons_state != NFS_CS_READY)
5694 		return status;
5695 
5696 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5697 
5698 	if (status)
5699 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5700 			"Session has been destroyed regardless...\n", status);
5701 
5702 	dprintk("<-- nfs4_proc_destroy_session\n");
5703 	return status;
5704 }
5705 
5706 /*
5707  * Renew the cl_session lease.
5708  */
5709 struct nfs4_sequence_data {
5710 	struct nfs_client *clp;
5711 	struct nfs4_sequence_args args;
5712 	struct nfs4_sequence_res res;
5713 };
5714 
5715 static void nfs41_sequence_release(void *data)
5716 {
5717 	struct nfs4_sequence_data *calldata = data;
5718 	struct nfs_client *clp = calldata->clp;
5719 
5720 	if (atomic_read(&clp->cl_count) > 1)
5721 		nfs4_schedule_state_renewal(clp);
5722 	nfs_put_client(clp);
5723 	kfree(calldata);
5724 }
5725 
5726 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5727 {
5728 	switch(task->tk_status) {
5729 	case -NFS4ERR_DELAY:
5730 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
5731 		return -EAGAIN;
5732 	default:
5733 		nfs4_schedule_lease_recovery(clp);
5734 	}
5735 	return 0;
5736 }
5737 
5738 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5739 {
5740 	struct nfs4_sequence_data *calldata = data;
5741 	struct nfs_client *clp = calldata->clp;
5742 
5743 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5744 		return;
5745 
5746 	if (task->tk_status < 0) {
5747 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
5748 		if (atomic_read(&clp->cl_count) == 1)
5749 			goto out;
5750 
5751 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5752 			rpc_restart_call_prepare(task);
5753 			return;
5754 		}
5755 	}
5756 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5757 out:
5758 	dprintk("<-- %s\n", __func__);
5759 }
5760 
5761 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5762 {
5763 	struct nfs4_sequence_data *calldata = data;
5764 	struct nfs_client *clp = calldata->clp;
5765 	struct nfs4_sequence_args *args;
5766 	struct nfs4_sequence_res *res;
5767 
5768 	args = task->tk_msg.rpc_argp;
5769 	res = task->tk_msg.rpc_resp;
5770 
5771 	nfs41_setup_sequence(clp->cl_session, args, res, task);
5772 }
5773 
5774 static const struct rpc_call_ops nfs41_sequence_ops = {
5775 	.rpc_call_done = nfs41_sequence_call_done,
5776 	.rpc_call_prepare = nfs41_sequence_prepare,
5777 	.rpc_release = nfs41_sequence_release,
5778 };
5779 
5780 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5781 		struct rpc_cred *cred,
5782 		bool is_privileged)
5783 {
5784 	struct nfs4_sequence_data *calldata;
5785 	struct rpc_message msg = {
5786 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5787 		.rpc_cred = cred,
5788 	};
5789 	struct rpc_task_setup task_setup_data = {
5790 		.rpc_client = clp->cl_rpcclient,
5791 		.rpc_message = &msg,
5792 		.callback_ops = &nfs41_sequence_ops,
5793 		.flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5794 	};
5795 
5796 	if (!atomic_inc_not_zero(&clp->cl_count))
5797 		return ERR_PTR(-EIO);
5798 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5799 	if (calldata == NULL) {
5800 		nfs_put_client(clp);
5801 		return ERR_PTR(-ENOMEM);
5802 	}
5803 	nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5804 	if (is_privileged)
5805 		nfs4_set_sequence_privileged(&calldata->args);
5806 	msg.rpc_argp = &calldata->args;
5807 	msg.rpc_resp = &calldata->res;
5808 	calldata->clp = clp;
5809 	task_setup_data.callback_data = calldata;
5810 
5811 	return rpc_run_task(&task_setup_data);
5812 }
5813 
5814 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5815 {
5816 	struct rpc_task *task;
5817 	int ret = 0;
5818 
5819 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5820 		return 0;
5821 	task = _nfs41_proc_sequence(clp, cred, false);
5822 	if (IS_ERR(task))
5823 		ret = PTR_ERR(task);
5824 	else
5825 		rpc_put_task_async(task);
5826 	dprintk("<-- %s status=%d\n", __func__, ret);
5827 	return ret;
5828 }
5829 
5830 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5831 {
5832 	struct rpc_task *task;
5833 	int ret;
5834 
5835 	task = _nfs41_proc_sequence(clp, cred, true);
5836 	if (IS_ERR(task)) {
5837 		ret = PTR_ERR(task);
5838 		goto out;
5839 	}
5840 	ret = rpc_wait_for_completion_task(task);
5841 	if (!ret) {
5842 		struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5843 
5844 		if (task->tk_status == 0)
5845 			nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5846 		ret = task->tk_status;
5847 	}
5848 	rpc_put_task(task);
5849 out:
5850 	dprintk("<-- %s status=%d\n", __func__, ret);
5851 	return ret;
5852 }
5853 
5854 struct nfs4_reclaim_complete_data {
5855 	struct nfs_client *clp;
5856 	struct nfs41_reclaim_complete_args arg;
5857 	struct nfs41_reclaim_complete_res res;
5858 };
5859 
5860 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5861 {
5862 	struct nfs4_reclaim_complete_data *calldata = data;
5863 
5864 	nfs41_setup_sequence(calldata->clp->cl_session,
5865 			&calldata->arg.seq_args,
5866 			&calldata->res.seq_res,
5867 			task);
5868 }
5869 
5870 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5871 {
5872 	switch(task->tk_status) {
5873 	case 0:
5874 	case -NFS4ERR_COMPLETE_ALREADY:
5875 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
5876 		break;
5877 	case -NFS4ERR_DELAY:
5878 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
5879 		/* fall through */
5880 	case -NFS4ERR_RETRY_UNCACHED_REP:
5881 		return -EAGAIN;
5882 	default:
5883 		nfs4_schedule_lease_recovery(clp);
5884 	}
5885 	return 0;
5886 }
5887 
5888 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5889 {
5890 	struct nfs4_reclaim_complete_data *calldata = data;
5891 	struct nfs_client *clp = calldata->clp;
5892 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
5893 
5894 	dprintk("--> %s\n", __func__);
5895 	if (!nfs41_sequence_done(task, res))
5896 		return;
5897 
5898 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5899 		rpc_restart_call_prepare(task);
5900 		return;
5901 	}
5902 	dprintk("<-- %s\n", __func__);
5903 }
5904 
5905 static void nfs4_free_reclaim_complete_data(void *data)
5906 {
5907 	struct nfs4_reclaim_complete_data *calldata = data;
5908 
5909 	kfree(calldata);
5910 }
5911 
5912 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5913 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
5914 	.rpc_call_done = nfs4_reclaim_complete_done,
5915 	.rpc_release = nfs4_free_reclaim_complete_data,
5916 };
5917 
5918 /*
5919  * Issue a global reclaim complete.
5920  */
5921 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5922 {
5923 	struct nfs4_reclaim_complete_data *calldata;
5924 	struct rpc_task *task;
5925 	struct rpc_message msg = {
5926 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5927 	};
5928 	struct rpc_task_setup task_setup_data = {
5929 		.rpc_client = clp->cl_rpcclient,
5930 		.rpc_message = &msg,
5931 		.callback_ops = &nfs4_reclaim_complete_call_ops,
5932 		.flags = RPC_TASK_ASYNC,
5933 	};
5934 	int status = -ENOMEM;
5935 
5936 	dprintk("--> %s\n", __func__);
5937 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5938 	if (calldata == NULL)
5939 		goto out;
5940 	calldata->clp = clp;
5941 	calldata->arg.one_fs = 0;
5942 
5943 	nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5944 	nfs4_set_sequence_privileged(&calldata->arg.seq_args);
5945 	msg.rpc_argp = &calldata->arg;
5946 	msg.rpc_resp = &calldata->res;
5947 	task_setup_data.callback_data = calldata;
5948 	task = rpc_run_task(&task_setup_data);
5949 	if (IS_ERR(task)) {
5950 		status = PTR_ERR(task);
5951 		goto out;
5952 	}
5953 	status = nfs4_wait_for_completion_rpc_task(task);
5954 	if (status == 0)
5955 		status = task->tk_status;
5956 	rpc_put_task(task);
5957 	return 0;
5958 out:
5959 	dprintk("<-- %s status=%d\n", __func__, status);
5960 	return status;
5961 }
5962 
5963 static void
5964 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5965 {
5966 	struct nfs4_layoutget *lgp = calldata;
5967 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5968 	struct nfs4_session *session = nfs4_get_session(server);
5969 
5970 	dprintk("--> %s\n", __func__);
5971 	/* Note the is a race here, where a CB_LAYOUTRECALL can come in
5972 	 * right now covering the LAYOUTGET we are about to send.
5973 	 * However, that is not so catastrophic, and there seems
5974 	 * to be no way to prevent it completely.
5975 	 */
5976 	if (nfs41_setup_sequence(session, &lgp->args.seq_args,
5977 				&lgp->res.seq_res, task))
5978 		return;
5979 	if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5980 					  NFS_I(lgp->args.inode)->layout,
5981 					  lgp->args.ctx->state)) {
5982 		rpc_exit(task, NFS4_OK);
5983 	}
5984 }
5985 
5986 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5987 {
5988 	struct nfs4_layoutget *lgp = calldata;
5989 	struct inode *inode = lgp->args.inode;
5990 	struct nfs_server *server = NFS_SERVER(inode);
5991 	struct pnfs_layout_hdr *lo;
5992 	struct nfs4_state *state = NULL;
5993 
5994 	dprintk("--> %s\n", __func__);
5995 
5996 	if (!nfs41_sequence_done(task, &lgp->res.seq_res))
5997 		goto out;
5998 
5999 	switch (task->tk_status) {
6000 	case 0:
6001 		goto out;
6002 	case -NFS4ERR_LAYOUTTRYLATER:
6003 	case -NFS4ERR_RECALLCONFLICT:
6004 		task->tk_status = -NFS4ERR_DELAY;
6005 		break;
6006 	case -NFS4ERR_EXPIRED:
6007 	case -NFS4ERR_BAD_STATEID:
6008 		spin_lock(&inode->i_lock);
6009 		lo = NFS_I(inode)->layout;
6010 		if (!lo || list_empty(&lo->plh_segs)) {
6011 			spin_unlock(&inode->i_lock);
6012 			/* If the open stateid was bad, then recover it. */
6013 			state = lgp->args.ctx->state;
6014 		} else {
6015 			LIST_HEAD(head);
6016 
6017 			pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6018 			spin_unlock(&inode->i_lock);
6019 			/* Mark the bad layout state as invalid, then
6020 			 * retry using the open stateid. */
6021 			pnfs_free_lseg_list(&head);
6022 		}
6023 	}
6024 	if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6025 		rpc_restart_call_prepare(task);
6026 out:
6027 	dprintk("<-- %s\n", __func__);
6028 }
6029 
6030 static size_t max_response_pages(struct nfs_server *server)
6031 {
6032 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6033 	return nfs_page_array_len(0, max_resp_sz);
6034 }
6035 
6036 static void nfs4_free_pages(struct page **pages, size_t size)
6037 {
6038 	int i;
6039 
6040 	if (!pages)
6041 		return;
6042 
6043 	for (i = 0; i < size; i++) {
6044 		if (!pages[i])
6045 			break;
6046 		__free_page(pages[i]);
6047 	}
6048 	kfree(pages);
6049 }
6050 
6051 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6052 {
6053 	struct page **pages;
6054 	int i;
6055 
6056 	pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6057 	if (!pages) {
6058 		dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6059 		return NULL;
6060 	}
6061 
6062 	for (i = 0; i < size; i++) {
6063 		pages[i] = alloc_page(gfp_flags);
6064 		if (!pages[i]) {
6065 			dprintk("%s: failed to allocate page\n", __func__);
6066 			nfs4_free_pages(pages, size);
6067 			return NULL;
6068 		}
6069 	}
6070 
6071 	return pages;
6072 }
6073 
6074 static void nfs4_layoutget_release(void *calldata)
6075 {
6076 	struct nfs4_layoutget *lgp = calldata;
6077 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6078 	size_t max_pages = max_response_pages(server);
6079 
6080 	dprintk("--> %s\n", __func__);
6081 	nfs4_free_pages(lgp->args.layout.pages, max_pages);
6082 	put_nfs_open_context(lgp->args.ctx);
6083 	kfree(calldata);
6084 	dprintk("<-- %s\n", __func__);
6085 }
6086 
6087 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6088 	.rpc_call_prepare = nfs4_layoutget_prepare,
6089 	.rpc_call_done = nfs4_layoutget_done,
6090 	.rpc_release = nfs4_layoutget_release,
6091 };
6092 
6093 struct pnfs_layout_segment *
6094 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6095 {
6096 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6097 	size_t max_pages = max_response_pages(server);
6098 	struct rpc_task *task;
6099 	struct rpc_message msg = {
6100 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6101 		.rpc_argp = &lgp->args,
6102 		.rpc_resp = &lgp->res,
6103 	};
6104 	struct rpc_task_setup task_setup_data = {
6105 		.rpc_client = server->client,
6106 		.rpc_message = &msg,
6107 		.callback_ops = &nfs4_layoutget_call_ops,
6108 		.callback_data = lgp,
6109 		.flags = RPC_TASK_ASYNC,
6110 	};
6111 	struct pnfs_layout_segment *lseg = NULL;
6112 	int status = 0;
6113 
6114 	dprintk("--> %s\n", __func__);
6115 
6116 	lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6117 	if (!lgp->args.layout.pages) {
6118 		nfs4_layoutget_release(lgp);
6119 		return ERR_PTR(-ENOMEM);
6120 	}
6121 	lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6122 
6123 	lgp->res.layoutp = &lgp->args.layout;
6124 	lgp->res.seq_res.sr_slot = NULL;
6125 	nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6126 	task = rpc_run_task(&task_setup_data);
6127 	if (IS_ERR(task))
6128 		return ERR_CAST(task);
6129 	status = nfs4_wait_for_completion_rpc_task(task);
6130 	if (status == 0)
6131 		status = task->tk_status;
6132 	if (status == 0)
6133 		lseg = pnfs_layout_process(lgp);
6134 	rpc_put_task(task);
6135 	dprintk("<-- %s status=%d\n", __func__, status);
6136 	if (status)
6137 		return ERR_PTR(status);
6138 	return lseg;
6139 }
6140 
6141 static void
6142 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6143 {
6144 	struct nfs4_layoutreturn *lrp = calldata;
6145 
6146 	dprintk("--> %s\n", __func__);
6147 	nfs41_setup_sequence(lrp->clp->cl_session,
6148 			&lrp->args.seq_args,
6149 			&lrp->res.seq_res,
6150 			task);
6151 }
6152 
6153 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6154 {
6155 	struct nfs4_layoutreturn *lrp = calldata;
6156 	struct nfs_server *server;
6157 
6158 	dprintk("--> %s\n", __func__);
6159 
6160 	if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6161 		return;
6162 
6163 	server = NFS_SERVER(lrp->args.inode);
6164 	if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6165 		rpc_restart_call_prepare(task);
6166 		return;
6167 	}
6168 	dprintk("<-- %s\n", __func__);
6169 }
6170 
6171 static void nfs4_layoutreturn_release(void *calldata)
6172 {
6173 	struct nfs4_layoutreturn *lrp = calldata;
6174 	struct pnfs_layout_hdr *lo = lrp->args.layout;
6175 
6176 	dprintk("--> %s\n", __func__);
6177 	spin_lock(&lo->plh_inode->i_lock);
6178 	if (lrp->res.lrs_present)
6179 		pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6180 	lo->plh_block_lgets--;
6181 	spin_unlock(&lo->plh_inode->i_lock);
6182 	pnfs_put_layout_hdr(lrp->args.layout);
6183 	kfree(calldata);
6184 	dprintk("<-- %s\n", __func__);
6185 }
6186 
6187 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6188 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
6189 	.rpc_call_done = nfs4_layoutreturn_done,
6190 	.rpc_release = nfs4_layoutreturn_release,
6191 };
6192 
6193 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6194 {
6195 	struct rpc_task *task;
6196 	struct rpc_message msg = {
6197 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6198 		.rpc_argp = &lrp->args,
6199 		.rpc_resp = &lrp->res,
6200 	};
6201 	struct rpc_task_setup task_setup_data = {
6202 		.rpc_client = lrp->clp->cl_rpcclient,
6203 		.rpc_message = &msg,
6204 		.callback_ops = &nfs4_layoutreturn_call_ops,
6205 		.callback_data = lrp,
6206 	};
6207 	int status;
6208 
6209 	dprintk("--> %s\n", __func__);
6210 	nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6211 	task = rpc_run_task(&task_setup_data);
6212 	if (IS_ERR(task))
6213 		return PTR_ERR(task);
6214 	status = task->tk_status;
6215 	dprintk("<-- %s status=%d\n", __func__, status);
6216 	rpc_put_task(task);
6217 	return status;
6218 }
6219 
6220 /*
6221  * Retrieve the list of Data Server devices from the MDS.
6222  */
6223 static int _nfs4_getdevicelist(struct nfs_server *server,
6224 				    const struct nfs_fh *fh,
6225 				    struct pnfs_devicelist *devlist)
6226 {
6227 	struct nfs4_getdevicelist_args args = {
6228 		.fh = fh,
6229 		.layoutclass = server->pnfs_curr_ld->id,
6230 	};
6231 	struct nfs4_getdevicelist_res res = {
6232 		.devlist = devlist,
6233 	};
6234 	struct rpc_message msg = {
6235 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6236 		.rpc_argp = &args,
6237 		.rpc_resp = &res,
6238 	};
6239 	int status;
6240 
6241 	dprintk("--> %s\n", __func__);
6242 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6243 				&res.seq_res, 0);
6244 	dprintk("<-- %s status=%d\n", __func__, status);
6245 	return status;
6246 }
6247 
6248 int nfs4_proc_getdevicelist(struct nfs_server *server,
6249 			    const struct nfs_fh *fh,
6250 			    struct pnfs_devicelist *devlist)
6251 {
6252 	struct nfs4_exception exception = { };
6253 	int err;
6254 
6255 	do {
6256 		err = nfs4_handle_exception(server,
6257 				_nfs4_getdevicelist(server, fh, devlist),
6258 				&exception);
6259 	} while (exception.retry);
6260 
6261 	dprintk("%s: err=%d, num_devs=%u\n", __func__,
6262 		err, devlist->num_devs);
6263 
6264 	return err;
6265 }
6266 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6267 
6268 static int
6269 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6270 {
6271 	struct nfs4_getdeviceinfo_args args = {
6272 		.pdev = pdev,
6273 	};
6274 	struct nfs4_getdeviceinfo_res res = {
6275 		.pdev = pdev,
6276 	};
6277 	struct rpc_message msg = {
6278 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6279 		.rpc_argp = &args,
6280 		.rpc_resp = &res,
6281 	};
6282 	int status;
6283 
6284 	dprintk("--> %s\n", __func__);
6285 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6286 	dprintk("<-- %s status=%d\n", __func__, status);
6287 
6288 	return status;
6289 }
6290 
6291 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6292 {
6293 	struct nfs4_exception exception = { };
6294 	int err;
6295 
6296 	do {
6297 		err = nfs4_handle_exception(server,
6298 					_nfs4_proc_getdeviceinfo(server, pdev),
6299 					&exception);
6300 	} while (exception.retry);
6301 	return err;
6302 }
6303 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6304 
6305 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6306 {
6307 	struct nfs4_layoutcommit_data *data = calldata;
6308 	struct nfs_server *server = NFS_SERVER(data->args.inode);
6309 	struct nfs4_session *session = nfs4_get_session(server);
6310 
6311 	nfs41_setup_sequence(session,
6312 			&data->args.seq_args,
6313 			&data->res.seq_res,
6314 			task);
6315 }
6316 
6317 static void
6318 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6319 {
6320 	struct nfs4_layoutcommit_data *data = calldata;
6321 	struct nfs_server *server = NFS_SERVER(data->args.inode);
6322 
6323 	if (!nfs41_sequence_done(task, &data->res.seq_res))
6324 		return;
6325 
6326 	switch (task->tk_status) { /* Just ignore these failures */
6327 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6328 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
6329 	case -NFS4ERR_BADLAYOUT:     /* no layout */
6330 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
6331 		task->tk_status = 0;
6332 		break;
6333 	case 0:
6334 		nfs_post_op_update_inode_force_wcc(data->args.inode,
6335 						   data->res.fattr);
6336 		break;
6337 	default:
6338 		if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6339 			rpc_restart_call_prepare(task);
6340 			return;
6341 		}
6342 	}
6343 }
6344 
6345 static void nfs4_layoutcommit_release(void *calldata)
6346 {
6347 	struct nfs4_layoutcommit_data *data = calldata;
6348 	struct pnfs_layout_segment *lseg, *tmp;
6349 	unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6350 
6351 	pnfs_cleanup_layoutcommit(data);
6352 	/* Matched by references in pnfs_set_layoutcommit */
6353 	list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6354 		list_del_init(&lseg->pls_lc_list);
6355 		if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6356 				       &lseg->pls_flags))
6357 			pnfs_put_lseg(lseg);
6358 	}
6359 
6360 	clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6361 	smp_mb__after_clear_bit();
6362 	wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6363 
6364 	put_rpccred(data->cred);
6365 	kfree(data);
6366 }
6367 
6368 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6369 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
6370 	.rpc_call_done = nfs4_layoutcommit_done,
6371 	.rpc_release = nfs4_layoutcommit_release,
6372 };
6373 
6374 int
6375 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6376 {
6377 	struct rpc_message msg = {
6378 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6379 		.rpc_argp = &data->args,
6380 		.rpc_resp = &data->res,
6381 		.rpc_cred = data->cred,
6382 	};
6383 	struct rpc_task_setup task_setup_data = {
6384 		.task = &data->task,
6385 		.rpc_client = NFS_CLIENT(data->args.inode),
6386 		.rpc_message = &msg,
6387 		.callback_ops = &nfs4_layoutcommit_ops,
6388 		.callback_data = data,
6389 		.flags = RPC_TASK_ASYNC,
6390 	};
6391 	struct rpc_task *task;
6392 	int status = 0;
6393 
6394 	dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6395 		"lbw: %llu inode %lu\n",
6396 		data->task.tk_pid, sync,
6397 		data->args.lastbytewritten,
6398 		data->args.inode->i_ino);
6399 
6400 	nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6401 	task = rpc_run_task(&task_setup_data);
6402 	if (IS_ERR(task))
6403 		return PTR_ERR(task);
6404 	if (sync == false)
6405 		goto out;
6406 	status = nfs4_wait_for_completion_rpc_task(task);
6407 	if (status != 0)
6408 		goto out;
6409 	status = task->tk_status;
6410 out:
6411 	dprintk("%s: status %d\n", __func__, status);
6412 	rpc_put_task(task);
6413 	return status;
6414 }
6415 
6416 static int
6417 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6418 		    struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6419 {
6420 	struct nfs41_secinfo_no_name_args args = {
6421 		.style = SECINFO_STYLE_CURRENT_FH,
6422 	};
6423 	struct nfs4_secinfo_res res = {
6424 		.flavors = flavors,
6425 	};
6426 	struct rpc_message msg = {
6427 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6428 		.rpc_argp = &args,
6429 		.rpc_resp = &res,
6430 	};
6431 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6432 }
6433 
6434 static int
6435 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6436 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6437 {
6438 	struct nfs4_exception exception = { };
6439 	int err;
6440 	do {
6441 		err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6442 		switch (err) {
6443 		case 0:
6444 		case -NFS4ERR_WRONGSEC:
6445 		case -NFS4ERR_NOTSUPP:
6446 			goto out;
6447 		default:
6448 			err = nfs4_handle_exception(server, err, &exception);
6449 		}
6450 	} while (exception.retry);
6451 out:
6452 	return err;
6453 }
6454 
6455 static int
6456 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6457 		    struct nfs_fsinfo *info)
6458 {
6459 	int err;
6460 	struct page *page;
6461 	rpc_authflavor_t flavor;
6462 	struct nfs4_secinfo_flavors *flavors;
6463 
6464 	page = alloc_page(GFP_KERNEL);
6465 	if (!page) {
6466 		err = -ENOMEM;
6467 		goto out;
6468 	}
6469 
6470 	flavors = page_address(page);
6471 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6472 
6473 	/*
6474 	 * Fall back on "guess and check" method if
6475 	 * the server doesn't support SECINFO_NO_NAME
6476 	 */
6477 	if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6478 		err = nfs4_find_root_sec(server, fhandle, info);
6479 		goto out_freepage;
6480 	}
6481 	if (err)
6482 		goto out_freepage;
6483 
6484 	flavor = nfs_find_best_sec(flavors);
6485 	if (err == 0)
6486 		err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6487 
6488 out_freepage:
6489 	put_page(page);
6490 	if (err == -EACCES)
6491 		return -EPERM;
6492 out:
6493 	return err;
6494 }
6495 
6496 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6497 {
6498 	int status;
6499 	struct nfs41_test_stateid_args args = {
6500 		.stateid = stateid,
6501 	};
6502 	struct nfs41_test_stateid_res res;
6503 	struct rpc_message msg = {
6504 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6505 		.rpc_argp = &args,
6506 		.rpc_resp = &res,
6507 	};
6508 
6509 	dprintk("NFS call  test_stateid %p\n", stateid);
6510 	nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6511 	nfs4_set_sequence_privileged(&args.seq_args);
6512 	status = nfs4_call_sync_sequence(server->client, server, &msg,
6513 			&args.seq_args, &res.seq_res);
6514 	if (status != NFS_OK) {
6515 		dprintk("NFS reply test_stateid: failed, %d\n", status);
6516 		return status;
6517 	}
6518 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6519 	return -res.status;
6520 }
6521 
6522 /**
6523  * nfs41_test_stateid - perform a TEST_STATEID operation
6524  *
6525  * @server: server / transport on which to perform the operation
6526  * @stateid: state ID to test
6527  *
6528  * Returns NFS_OK if the server recognizes that "stateid" is valid.
6529  * Otherwise a negative NFS4ERR value is returned if the operation
6530  * failed or the state ID is not currently valid.
6531  */
6532 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6533 {
6534 	struct nfs4_exception exception = { };
6535 	int err;
6536 	do {
6537 		err = _nfs41_test_stateid(server, stateid);
6538 		if (err != -NFS4ERR_DELAY)
6539 			break;
6540 		nfs4_handle_exception(server, err, &exception);
6541 	} while (exception.retry);
6542 	return err;
6543 }
6544 
6545 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6546 {
6547 	struct nfs41_free_stateid_args args = {
6548 		.stateid = stateid,
6549 	};
6550 	struct nfs41_free_stateid_res res;
6551 	struct rpc_message msg = {
6552 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6553 		.rpc_argp = &args,
6554 		.rpc_resp = &res,
6555 	};
6556 	int status;
6557 
6558 	dprintk("NFS call  free_stateid %p\n", stateid);
6559 	nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6560 	nfs4_set_sequence_privileged(&args.seq_args);
6561 	status = nfs4_call_sync_sequence(server->client, server, &msg,
6562 			&args.seq_args, &res.seq_res);
6563 	dprintk("NFS reply free_stateid: %d\n", status);
6564 	return status;
6565 }
6566 
6567 /**
6568  * nfs41_free_stateid - perform a FREE_STATEID operation
6569  *
6570  * @server: server / transport on which to perform the operation
6571  * @stateid: state ID to release
6572  *
6573  * Returns NFS_OK if the server freed "stateid".  Otherwise a
6574  * negative NFS4ERR value is returned.
6575  */
6576 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6577 {
6578 	struct nfs4_exception exception = { };
6579 	int err;
6580 	do {
6581 		err = _nfs4_free_stateid(server, stateid);
6582 		if (err != -NFS4ERR_DELAY)
6583 			break;
6584 		nfs4_handle_exception(server, err, &exception);
6585 	} while (exception.retry);
6586 	return err;
6587 }
6588 
6589 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6590 		const nfs4_stateid *s2)
6591 {
6592 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6593 		return false;
6594 
6595 	if (s1->seqid == s2->seqid)
6596 		return true;
6597 	if (s1->seqid == 0 || s2->seqid == 0)
6598 		return true;
6599 
6600 	return false;
6601 }
6602 
6603 #endif /* CONFIG_NFS_V4_1 */
6604 
6605 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6606 		const nfs4_stateid *s2)
6607 {
6608 	return nfs4_stateid_match(s1, s2);
6609 }
6610 
6611 
6612 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6613 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6614 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
6615 	.recover_open	= nfs4_open_reclaim,
6616 	.recover_lock	= nfs4_lock_reclaim,
6617 	.establish_clid = nfs4_init_clientid,
6618 	.get_clid_cred	= nfs4_get_setclientid_cred,
6619 	.detect_trunking = nfs40_discover_server_trunking,
6620 };
6621 
6622 #if defined(CONFIG_NFS_V4_1)
6623 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6624 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6625 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
6626 	.recover_open	= nfs4_open_reclaim,
6627 	.recover_lock	= nfs4_lock_reclaim,
6628 	.establish_clid = nfs41_init_clientid,
6629 	.get_clid_cred	= nfs4_get_exchange_id_cred,
6630 	.reclaim_complete = nfs41_proc_reclaim_complete,
6631 	.detect_trunking = nfs41_discover_server_trunking,
6632 };
6633 #endif /* CONFIG_NFS_V4_1 */
6634 
6635 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6636 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6637 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
6638 	.recover_open	= nfs4_open_expired,
6639 	.recover_lock	= nfs4_lock_expired,
6640 	.establish_clid = nfs4_init_clientid,
6641 	.get_clid_cred	= nfs4_get_setclientid_cred,
6642 };
6643 
6644 #if defined(CONFIG_NFS_V4_1)
6645 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6646 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6647 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
6648 	.recover_open	= nfs41_open_expired,
6649 	.recover_lock	= nfs41_lock_expired,
6650 	.establish_clid = nfs41_init_clientid,
6651 	.get_clid_cred	= nfs4_get_exchange_id_cred,
6652 };
6653 #endif /* CONFIG_NFS_V4_1 */
6654 
6655 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6656 	.sched_state_renewal = nfs4_proc_async_renew,
6657 	.get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6658 	.renew_lease = nfs4_proc_renew,
6659 };
6660 
6661 #if defined(CONFIG_NFS_V4_1)
6662 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6663 	.sched_state_renewal = nfs41_proc_async_sequence,
6664 	.get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6665 	.renew_lease = nfs4_proc_sequence,
6666 };
6667 #endif
6668 
6669 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6670 	.minor_version = 0,
6671 	.call_sync = _nfs4_call_sync,
6672 	.match_stateid = nfs4_match_stateid,
6673 	.find_root_sec = nfs4_find_root_sec,
6674 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6675 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6676 	.state_renewal_ops = &nfs40_state_renewal_ops,
6677 };
6678 
6679 #if defined(CONFIG_NFS_V4_1)
6680 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6681 	.minor_version = 1,
6682 	.call_sync = nfs4_call_sync_sequence,
6683 	.match_stateid = nfs41_match_stateid,
6684 	.find_root_sec = nfs41_find_root_sec,
6685 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6686 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6687 	.state_renewal_ops = &nfs41_state_renewal_ops,
6688 };
6689 #endif
6690 
6691 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6692 	[0] = &nfs_v4_0_minor_ops,
6693 #if defined(CONFIG_NFS_V4_1)
6694 	[1] = &nfs_v4_1_minor_ops,
6695 #endif
6696 };
6697 
6698 const struct inode_operations nfs4_dir_inode_operations = {
6699 	.create		= nfs_create,
6700 	.lookup		= nfs_lookup,
6701 	.atomic_open	= nfs_atomic_open,
6702 	.link		= nfs_link,
6703 	.unlink		= nfs_unlink,
6704 	.symlink	= nfs_symlink,
6705 	.mkdir		= nfs_mkdir,
6706 	.rmdir		= nfs_rmdir,
6707 	.mknod		= nfs_mknod,
6708 	.rename		= nfs_rename,
6709 	.permission	= nfs_permission,
6710 	.getattr	= nfs_getattr,
6711 	.setattr	= nfs_setattr,
6712 	.getxattr	= generic_getxattr,
6713 	.setxattr	= generic_setxattr,
6714 	.listxattr	= generic_listxattr,
6715 	.removexattr	= generic_removexattr,
6716 };
6717 
6718 static const struct inode_operations nfs4_file_inode_operations = {
6719 	.permission	= nfs_permission,
6720 	.getattr	= nfs_getattr,
6721 	.setattr	= nfs_setattr,
6722 	.getxattr	= generic_getxattr,
6723 	.setxattr	= generic_setxattr,
6724 	.listxattr	= generic_listxattr,
6725 	.removexattr	= generic_removexattr,
6726 };
6727 
6728 const struct nfs_rpc_ops nfs_v4_clientops = {
6729 	.version	= 4,			/* protocol version */
6730 	.dentry_ops	= &nfs4_dentry_operations,
6731 	.dir_inode_ops	= &nfs4_dir_inode_operations,
6732 	.file_inode_ops	= &nfs4_file_inode_operations,
6733 	.file_ops	= &nfs4_file_operations,
6734 	.getroot	= nfs4_proc_get_root,
6735 	.submount	= nfs4_submount,
6736 	.try_mount	= nfs4_try_mount,
6737 	.getattr	= nfs4_proc_getattr,
6738 	.setattr	= nfs4_proc_setattr,
6739 	.lookup		= nfs4_proc_lookup,
6740 	.access		= nfs4_proc_access,
6741 	.readlink	= nfs4_proc_readlink,
6742 	.create		= nfs4_proc_create,
6743 	.remove		= nfs4_proc_remove,
6744 	.unlink_setup	= nfs4_proc_unlink_setup,
6745 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6746 	.unlink_done	= nfs4_proc_unlink_done,
6747 	.rename		= nfs4_proc_rename,
6748 	.rename_setup	= nfs4_proc_rename_setup,
6749 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6750 	.rename_done	= nfs4_proc_rename_done,
6751 	.link		= nfs4_proc_link,
6752 	.symlink	= nfs4_proc_symlink,
6753 	.mkdir		= nfs4_proc_mkdir,
6754 	.rmdir		= nfs4_proc_remove,
6755 	.readdir	= nfs4_proc_readdir,
6756 	.mknod		= nfs4_proc_mknod,
6757 	.statfs		= nfs4_proc_statfs,
6758 	.fsinfo		= nfs4_proc_fsinfo,
6759 	.pathconf	= nfs4_proc_pathconf,
6760 	.set_capabilities = nfs4_server_capabilities,
6761 	.decode_dirent	= nfs4_decode_dirent,
6762 	.read_setup	= nfs4_proc_read_setup,
6763 	.read_pageio_init = pnfs_pageio_init_read,
6764 	.read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6765 	.read_done	= nfs4_read_done,
6766 	.write_setup	= nfs4_proc_write_setup,
6767 	.write_pageio_init = pnfs_pageio_init_write,
6768 	.write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6769 	.write_done	= nfs4_write_done,
6770 	.commit_setup	= nfs4_proc_commit_setup,
6771 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6772 	.commit_done	= nfs4_commit_done,
6773 	.lock		= nfs4_proc_lock,
6774 	.clear_acl_cache = nfs4_zap_acl_attr,
6775 	.close_context  = nfs4_close_context,
6776 	.open_context	= nfs4_atomic_open,
6777 	.have_delegation = nfs4_have_delegation,
6778 	.return_delegation = nfs4_inode_return_delegation,
6779 	.alloc_client	= nfs4_alloc_client,
6780 	.init_client	= nfs4_init_client,
6781 	.free_client	= nfs4_free_client,
6782 	.create_server	= nfs4_create_server,
6783 	.clone_server	= nfs_clone_server,
6784 };
6785 
6786 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6787 	.prefix	= XATTR_NAME_NFSV4_ACL,
6788 	.list	= nfs4_xattr_list_nfs4_acl,
6789 	.get	= nfs4_xattr_get_nfs4_acl,
6790 	.set	= nfs4_xattr_set_nfs4_acl,
6791 };
6792 
6793 const struct xattr_handler *nfs4_xattr_handlers[] = {
6794 	&nfs4_xattr_nfs4_acl_handler,
6795 	NULL
6796 };
6797 
6798 /*
6799  * Local variables:
6800  *  c-basic-offset: 8
6801  * End:
6802  */
6803