xref: /openbmc/linux/fs/nfs/pnfs.c (revision 4ebdac060e5e24a89a7b3ec33ec46a41621e57fe)
1 /*
2  *  pNFS functions to call and manage layout drivers.
3  *
4  *  Copyright (c) 2002 [year of first publication]
5  *  The Regents of the University of Michigan
6  *  All Rights Reserved
7  *
8  *  Dean Hildebrand <dhildebz@umich.edu>
9  *
10  *  Permission is granted to use, copy, create derivative works, and
11  *  redistribute this software and such derivative works for any purpose,
12  *  so long as the name of the University of Michigan is not used in
13  *  any advertising or publicity pertaining to the use or distribution
14  *  of this software without specific, written prior authorization. If
15  *  the above copyright notice or any other identification of the
16  *  University of Michigan is included in any copy of any portion of
17  *  this software, then the disclaimer below must also be included.
18  *
19  *  This software is provided as is, without representation or warranty
20  *  of any kind either express or implied, including without limitation
21  *  the implied warranties of merchantability, fitness for a particular
22  *  purpose, or noninfringement.  The Regents of the University of
23  *  Michigan shall not be liable for any damages, including special,
24  *  indirect, incidental, or consequential damages, with respect to any
25  *  claim arising out of or in connection with the use of the software,
26  *  even if it has been or is hereafter advised of the possibility of
27  *  such damages.
28  */
29 
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_page.h>
32 #include <linux/module.h>
33 #include <linux/sort.h>
34 #include "internal.h"
35 #include "pnfs.h"
36 #include "iostat.h"
37 #include "nfs4trace.h"
38 #include "delegation.h"
39 #include "nfs42.h"
40 #include "nfs4_fs.h"
41 
42 #define NFSDBG_FACILITY		NFSDBG_PNFS
43 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
44 
45 /* Locking:
46  *
47  * pnfs_spinlock:
48  *      protects pnfs_modules_tbl.
49  */
50 static DEFINE_SPINLOCK(pnfs_spinlock);
51 
52 /*
53  * pnfs_modules_tbl holds all pnfs modules
54  */
55 static LIST_HEAD(pnfs_modules_tbl);
56 
57 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
58 static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
59 		struct list_head *free_me,
60 		const struct pnfs_layout_range *range,
61 		u32 seq);
62 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
63 		                struct list_head *tmp_list);
64 
65 /* Return the registered pnfs layout driver module matching given id */
66 static struct pnfs_layoutdriver_type *
find_pnfs_driver_locked(u32 id)67 find_pnfs_driver_locked(u32 id)
68 {
69 	struct pnfs_layoutdriver_type *local;
70 
71 	list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
72 		if (local->id == id)
73 			goto out;
74 	local = NULL;
75 out:
76 	dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
77 	return local;
78 }
79 
80 static struct pnfs_layoutdriver_type *
find_pnfs_driver(u32 id)81 find_pnfs_driver(u32 id)
82 {
83 	struct pnfs_layoutdriver_type *local;
84 
85 	spin_lock(&pnfs_spinlock);
86 	local = find_pnfs_driver_locked(id);
87 	if (local != NULL && !try_module_get(local->owner)) {
88 		dprintk("%s: Could not grab reference on module\n", __func__);
89 		local = NULL;
90 	}
91 	spin_unlock(&pnfs_spinlock);
92 	return local;
93 }
94 
pnfs_find_layoutdriver(u32 id)95 const struct pnfs_layoutdriver_type *pnfs_find_layoutdriver(u32 id)
96 {
97 	return find_pnfs_driver(id);
98 }
99 
pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type * ld)100 void pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type *ld)
101 {
102 	if (ld)
103 		module_put(ld->owner);
104 }
105 
106 void
unset_pnfs_layoutdriver(struct nfs_server * nfss)107 unset_pnfs_layoutdriver(struct nfs_server *nfss)
108 {
109 	if (nfss->pnfs_curr_ld) {
110 		if (nfss->pnfs_curr_ld->clear_layoutdriver)
111 			nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
112 		/* Decrement the MDS count. Purge the deviceid cache if zero */
113 		if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
114 			nfs4_deviceid_purge_client(nfss->nfs_client);
115 		module_put(nfss->pnfs_curr_ld->owner);
116 	}
117 	nfss->pnfs_curr_ld = NULL;
118 }
119 
120 /*
121  * When the server sends a list of layout types, we choose one in the order
122  * given in the list below.
123  *
124  * FIXME: should this list be configurable in some fashion? module param?
125  * 	  mount option? something else?
126  */
127 static const u32 ld_prefs[] = {
128 	LAYOUT_SCSI,
129 	LAYOUT_BLOCK_VOLUME,
130 	LAYOUT_OSD2_OBJECTS,
131 	LAYOUT_FLEX_FILES,
132 	LAYOUT_NFSV4_1_FILES,
133 	0
134 };
135 
136 static int
ld_cmp(const void * e1,const void * e2)137 ld_cmp(const void *e1, const void *e2)
138 {
139 	u32 ld1 = *((u32 *)e1);
140 	u32 ld2 = *((u32 *)e2);
141 	int i;
142 
143 	for (i = 0; ld_prefs[i] != 0; i++) {
144 		if (ld1 == ld_prefs[i])
145 			return -1;
146 
147 		if (ld2 == ld_prefs[i])
148 			return 1;
149 	}
150 	return 0;
151 }
152 
153 /*
154  * Try to set the server's pnfs module to the pnfs layout type specified by id.
155  * Currently only one pNFS layout driver per filesystem is supported.
156  *
157  * @ids array of layout types supported by MDS.
158  */
159 void
set_pnfs_layoutdriver(struct nfs_server * server,const struct nfs_fh * mntfh,struct nfs_fsinfo * fsinfo)160 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
161 		      struct nfs_fsinfo *fsinfo)
162 {
163 	struct pnfs_layoutdriver_type *ld_type = NULL;
164 	u32 id;
165 	int i;
166 
167 	if (fsinfo->nlayouttypes == 0)
168 		goto out_no_driver;
169 	if (!(server->nfs_client->cl_exchange_flags &
170 		 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
171 		printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
172 			__func__, server->nfs_client->cl_exchange_flags);
173 		goto out_no_driver;
174 	}
175 
176 	sort(fsinfo->layouttype, fsinfo->nlayouttypes,
177 		sizeof(*fsinfo->layouttype), ld_cmp, NULL);
178 
179 	for (i = 0; i < fsinfo->nlayouttypes; i++) {
180 		id = fsinfo->layouttype[i];
181 		ld_type = find_pnfs_driver(id);
182 		if (!ld_type) {
183 			request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
184 					id);
185 			ld_type = find_pnfs_driver(id);
186 		}
187 		if (ld_type)
188 			break;
189 	}
190 
191 	if (!ld_type) {
192 		dprintk("%s: No pNFS module found!\n", __func__);
193 		goto out_no_driver;
194 	}
195 
196 	server->pnfs_curr_ld = ld_type;
197 	if (ld_type->set_layoutdriver
198 	    && ld_type->set_layoutdriver(server, mntfh)) {
199 		printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
200 			"driver %u.\n", __func__, id);
201 		module_put(ld_type->owner);
202 		goto out_no_driver;
203 	}
204 	/* Bump the MDS count */
205 	atomic_inc(&server->nfs_client->cl_mds_count);
206 
207 	dprintk("%s: pNFS module for %u set\n", __func__, id);
208 	return;
209 
210 out_no_driver:
211 	dprintk("%s: Using NFSv4 I/O\n", __func__);
212 	server->pnfs_curr_ld = NULL;
213 }
214 
215 int
pnfs_register_layoutdriver(struct pnfs_layoutdriver_type * ld_type)216 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
217 {
218 	int status = -EINVAL;
219 	struct pnfs_layoutdriver_type *tmp;
220 
221 	if (ld_type->id == 0) {
222 		printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
223 		return status;
224 	}
225 	if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
226 		printk(KERN_ERR "NFS: %s Layout driver must provide "
227 		       "alloc_lseg and free_lseg.\n", __func__);
228 		return status;
229 	}
230 
231 	spin_lock(&pnfs_spinlock);
232 	tmp = find_pnfs_driver_locked(ld_type->id);
233 	if (!tmp) {
234 		list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
235 		status = 0;
236 		dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
237 			ld_type->name);
238 	} else {
239 		printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
240 			__func__, ld_type->id);
241 	}
242 	spin_unlock(&pnfs_spinlock);
243 
244 	return status;
245 }
246 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
247 
248 void
pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type * ld_type)249 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
250 {
251 	dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
252 	spin_lock(&pnfs_spinlock);
253 	list_del(&ld_type->pnfs_tblid);
254 	spin_unlock(&pnfs_spinlock);
255 }
256 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
257 
258 /*
259  * pNFS client layout cache
260  */
261 
262 /* Need to hold i_lock if caller does not already hold reference */
263 void
pnfs_get_layout_hdr(struct pnfs_layout_hdr * lo)264 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
265 {
266 	refcount_inc(&lo->plh_refcount);
267 }
268 
269 static struct pnfs_layout_hdr *
pnfs_alloc_layout_hdr(struct inode * ino,gfp_t gfp_flags)270 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
271 {
272 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
273 	return ld->alloc_layout_hdr(ino, gfp_flags);
274 }
275 
276 static void
pnfs_free_layout_hdr(struct pnfs_layout_hdr * lo)277 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
278 {
279 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
280 	struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
281 
282 	if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
283 		struct nfs_client *clp = server->nfs_client;
284 
285 		spin_lock(&clp->cl_lock);
286 		list_del_rcu(&lo->plh_layouts);
287 		spin_unlock(&clp->cl_lock);
288 	}
289 	put_cred(lo->plh_lc_cred);
290 	return ld->free_layout_hdr(lo);
291 }
292 
293 static void
pnfs_detach_layout_hdr(struct pnfs_layout_hdr * lo)294 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
295 {
296 	struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
297 	dprintk("%s: freeing layout cache %p\n", __func__, lo);
298 	nfsi->layout = NULL;
299 	/* Reset MDS Threshold I/O counters */
300 	nfsi->write_io = 0;
301 	nfsi->read_io = 0;
302 }
303 
304 void
pnfs_put_layout_hdr(struct pnfs_layout_hdr * lo)305 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
306 {
307 	struct inode *inode;
308 	unsigned long i_state;
309 
310 	if (!lo)
311 		return;
312 	inode = lo->plh_inode;
313 	pnfs_layoutreturn_before_put_layout_hdr(lo);
314 
315 	if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
316 		if (!list_empty(&lo->plh_segs))
317 			WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
318 		pnfs_detach_layout_hdr(lo);
319 		i_state = inode->i_state;
320 		spin_unlock(&inode->i_lock);
321 		pnfs_free_layout_hdr(lo);
322 		/* Notify pnfs_destroy_layout_final() that we're done */
323 		if (i_state & (I_FREEING | I_CLEAR))
324 			wake_up_var(lo);
325 	}
326 }
327 
328 static struct inode *
pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr * lo)329 pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr *lo)
330 {
331 	struct inode *inode = igrab(lo->plh_inode);
332 	if (inode)
333 		return inode;
334 	set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags);
335 	return NULL;
336 }
337 
338 /*
339  * Compare 2 layout stateid sequence ids, to see which is newer,
340  * taking into account wraparound issues.
341  */
pnfs_seqid_is_newer(u32 s1,u32 s2)342 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
343 {
344 	return (s32)(s1 - s2) > 0;
345 }
346 
pnfs_barrier_update(struct pnfs_layout_hdr * lo,u32 newseq)347 static void pnfs_barrier_update(struct pnfs_layout_hdr *lo, u32 newseq)
348 {
349 	if (pnfs_seqid_is_newer(newseq, lo->plh_barrier) || !lo->plh_barrier)
350 		lo->plh_barrier = newseq;
351 }
352 
353 static void
pnfs_set_plh_return_info(struct pnfs_layout_hdr * lo,enum pnfs_iomode iomode,u32 seq)354 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
355 			 u32 seq)
356 {
357 	if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
358 		iomode = IOMODE_ANY;
359 	lo->plh_return_iomode = iomode;
360 	set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
361 	/*
362 	 * We must set lo->plh_return_seq to avoid livelocks with
363 	 * pnfs_layout_need_return()
364 	 */
365 	if (seq == 0)
366 		seq = be32_to_cpu(lo->plh_stateid.seqid);
367 	if (!lo->plh_return_seq || pnfs_seqid_is_newer(seq, lo->plh_return_seq))
368 		lo->plh_return_seq = seq;
369 	pnfs_barrier_update(lo, seq);
370 }
371 
372 static void
pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr * lo)373 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
374 {
375 	struct pnfs_layout_segment *lseg;
376 	lo->plh_return_iomode = 0;
377 	lo->plh_return_seq = 0;
378 	clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
379 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
380 		if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
381 			continue;
382 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
383 	}
384 }
385 
pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr * lo)386 static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
387 {
388 	clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
389 	clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
390 	smp_mb__after_atomic();
391 	wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
392 	rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
393 }
394 
395 static void
pnfs_clear_lseg_state(struct pnfs_layout_segment * lseg,struct list_head * free_me)396 pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
397 		struct list_head *free_me)
398 {
399 	clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
400 	clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
401 	if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
402 		pnfs_lseg_dec_and_remove_zero(lseg, free_me);
403 	if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
404 		pnfs_lseg_dec_and_remove_zero(lseg, free_me);
405 }
406 
407 /*
408  * Update the seqid of a layout stateid after receiving
409  * NFS4ERR_OLD_STATEID
410  */
nfs4_layout_refresh_old_stateid(nfs4_stateid * dst,struct pnfs_layout_range * dst_range,struct inode * inode)411 bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst,
412 		struct pnfs_layout_range *dst_range,
413 		struct inode *inode)
414 {
415 	struct pnfs_layout_hdr *lo;
416 	struct pnfs_layout_range range = {
417 		.iomode = IOMODE_ANY,
418 		.offset = 0,
419 		.length = NFS4_MAX_UINT64,
420 	};
421 	bool ret = false;
422 	LIST_HEAD(head);
423 	int err;
424 
425 	spin_lock(&inode->i_lock);
426 	lo = NFS_I(inode)->layout;
427 	if (lo &&  pnfs_layout_is_valid(lo) &&
428 	    nfs4_stateid_match_other(dst, &lo->plh_stateid)) {
429 		/* Is our call using the most recent seqid? If so, bump it */
430 		if (!nfs4_stateid_is_newer(&lo->plh_stateid, dst)) {
431 			nfs4_stateid_seqid_inc(dst);
432 			ret = true;
433 			goto out;
434 		}
435 		/* Try to update the seqid to the most recent */
436 		err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
437 		if (err != -EBUSY) {
438 			dst->seqid = lo->plh_stateid.seqid;
439 			*dst_range = range;
440 			ret = true;
441 		}
442 	}
443 out:
444 	spin_unlock(&inode->i_lock);
445 	pnfs_free_lseg_list(&head);
446 	return ret;
447 }
448 
449 /*
450  * Mark a pnfs_layout_hdr and all associated layout segments as invalid
451  *
452  * In order to continue using the pnfs_layout_hdr, a full recovery
453  * is required.
454  * Note that caller must hold inode->i_lock.
455  */
456 int
pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr * lo,struct list_head * lseg_list)457 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
458 		struct list_head *lseg_list)
459 {
460 	struct pnfs_layout_range range = {
461 		.iomode = IOMODE_ANY,
462 		.offset = 0,
463 		.length = NFS4_MAX_UINT64,
464 	};
465 	struct pnfs_layout_segment *lseg, *next;
466 
467 	set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
468 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
469 		pnfs_clear_lseg_state(lseg, lseg_list);
470 	pnfs_clear_layoutreturn_info(lo);
471 	pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
472 	set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
473 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
474 	    !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
475 		pnfs_clear_layoutreturn_waitbit(lo);
476 	return !list_empty(&lo->plh_segs);
477 }
478 
479 static int
pnfs_iomode_to_fail_bit(u32 iomode)480 pnfs_iomode_to_fail_bit(u32 iomode)
481 {
482 	return iomode == IOMODE_RW ?
483 		NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
484 }
485 
486 static void
pnfs_layout_set_fail_bit(struct pnfs_layout_hdr * lo,int fail_bit)487 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
488 {
489 	lo->plh_retry_timestamp = jiffies;
490 	if (!test_and_set_bit(fail_bit, &lo->plh_flags))
491 		refcount_inc(&lo->plh_refcount);
492 }
493 
494 static void
pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr * lo,int fail_bit)495 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
496 {
497 	if (test_and_clear_bit(fail_bit, &lo->plh_flags))
498 		refcount_dec(&lo->plh_refcount);
499 }
500 
501 static void
pnfs_layout_io_set_failed(struct pnfs_layout_hdr * lo,u32 iomode)502 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
503 {
504 	struct inode *inode = lo->plh_inode;
505 	struct pnfs_layout_range range = {
506 		.iomode = iomode,
507 		.offset = 0,
508 		.length = NFS4_MAX_UINT64,
509 	};
510 	LIST_HEAD(head);
511 
512 	spin_lock(&inode->i_lock);
513 	pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
514 	pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
515 	spin_unlock(&inode->i_lock);
516 	pnfs_free_lseg_list(&head);
517 	dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
518 			iomode == IOMODE_RW ?  "RW" : "READ");
519 }
520 
521 static bool
pnfs_layout_io_test_failed(struct pnfs_layout_hdr * lo,u32 iomode)522 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
523 {
524 	unsigned long start, end;
525 	int fail_bit = pnfs_iomode_to_fail_bit(iomode);
526 
527 	if (test_bit(fail_bit, &lo->plh_flags) == 0)
528 		return false;
529 	end = jiffies;
530 	start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
531 	if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
532 		/* It is time to retry the failed layoutgets */
533 		pnfs_layout_clear_fail_bit(lo, fail_bit);
534 		return false;
535 	}
536 	return true;
537 }
538 
539 static void
pnfs_init_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,const struct pnfs_layout_range * range,const nfs4_stateid * stateid)540 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
541 		const struct pnfs_layout_range *range,
542 		const nfs4_stateid *stateid)
543 {
544 	INIT_LIST_HEAD(&lseg->pls_list);
545 	INIT_LIST_HEAD(&lseg->pls_lc_list);
546 	INIT_LIST_HEAD(&lseg->pls_commits);
547 	refcount_set(&lseg->pls_refcount, 1);
548 	set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
549 	lseg->pls_layout = lo;
550 	lseg->pls_range = *range;
551 	lseg->pls_seq = be32_to_cpu(stateid->seqid);
552 }
553 
pnfs_free_lseg(struct pnfs_layout_segment * lseg)554 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
555 {
556 	if (lseg != NULL) {
557 		struct inode *inode = lseg->pls_layout->plh_inode;
558 		NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
559 	}
560 }
561 
562 static void
pnfs_layout_remove_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg)563 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
564 		struct pnfs_layout_segment *lseg)
565 {
566 	WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
567 	list_del_init(&lseg->pls_list);
568 	/* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
569 	refcount_dec(&lo->plh_refcount);
570 	if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
571 		return;
572 	if (list_empty(&lo->plh_segs) &&
573 	    !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
574 	    !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
575 		if (atomic_read(&lo->plh_outstanding) == 0)
576 			set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
577 		clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
578 	}
579 }
580 
581 static bool
pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg)582 pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
583 		struct pnfs_layout_segment *lseg)
584 {
585 	if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
586 	    pnfs_layout_is_valid(lo)) {
587 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
588 		list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
589 		return true;
590 	}
591 	return false;
592 }
593 
594 void
pnfs_put_lseg(struct pnfs_layout_segment * lseg)595 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
596 {
597 	struct pnfs_layout_hdr *lo;
598 	struct inode *inode;
599 
600 	if (!lseg)
601 		return;
602 
603 	dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
604 		refcount_read(&lseg->pls_refcount),
605 		test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
606 
607 	lo = lseg->pls_layout;
608 	inode = lo->plh_inode;
609 
610 	if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
611 		pnfs_get_layout_hdr(lo);
612 		pnfs_layout_remove_lseg(lo, lseg);
613 		if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
614 			lseg = NULL;
615 		spin_unlock(&inode->i_lock);
616 		pnfs_free_lseg(lseg);
617 		pnfs_put_layout_hdr(lo);
618 	}
619 }
620 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
621 
622 /*
623  * is l2 fully contained in l1?
624  *   start1                             end1
625  *   [----------------------------------)
626  *           start2           end2
627  *           [----------------)
628  */
629 static bool
pnfs_lseg_range_contained(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)630 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
631 		 const struct pnfs_layout_range *l2)
632 {
633 	u64 start1 = l1->offset;
634 	u64 end1 = pnfs_end_offset(start1, l1->length);
635 	u64 start2 = l2->offset;
636 	u64 end2 = pnfs_end_offset(start2, l2->length);
637 
638 	return (start1 <= start2) && (end1 >= end2);
639 }
640 
pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment * lseg,struct list_head * tmp_list)641 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
642 		struct list_head *tmp_list)
643 {
644 	if (!refcount_dec_and_test(&lseg->pls_refcount))
645 		return false;
646 	pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
647 	list_add(&lseg->pls_list, tmp_list);
648 	return true;
649 }
650 
651 /* Returns 1 if lseg is removed from list, 0 otherwise */
mark_lseg_invalid(struct pnfs_layout_segment * lseg,struct list_head * tmp_list)652 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
653 			     struct list_head *tmp_list)
654 {
655 	int rv = 0;
656 
657 	if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
658 		/* Remove the reference keeping the lseg in the
659 		 * list.  It will now be removed when all
660 		 * outstanding io is finished.
661 		 */
662 		dprintk("%s: lseg %p ref %d\n", __func__, lseg,
663 			refcount_read(&lseg->pls_refcount));
664 		if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
665 			rv = 1;
666 	}
667 	return rv;
668 }
669 
670 static bool
pnfs_should_free_range(const struct pnfs_layout_range * lseg_range,const struct pnfs_layout_range * recall_range)671 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
672 		 const struct pnfs_layout_range *recall_range)
673 {
674 	return (recall_range->iomode == IOMODE_ANY ||
675 		lseg_range->iomode == recall_range->iomode) &&
676 	       pnfs_lseg_range_intersecting(lseg_range, recall_range);
677 }
678 
679 static bool
pnfs_match_lseg_recall(const struct pnfs_layout_segment * lseg,const struct pnfs_layout_range * recall_range,u32 seq)680 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
681 		const struct pnfs_layout_range *recall_range,
682 		u32 seq)
683 {
684 	if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
685 		return false;
686 	if (recall_range == NULL)
687 		return true;
688 	return pnfs_should_free_range(&lseg->pls_range, recall_range);
689 }
690 
691 /**
692  * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
693  * @lo: layout header containing the lsegs
694  * @tmp_list: list head where doomed lsegs should go
695  * @recall_range: optional recall range argument to match (may be NULL)
696  * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
697  *
698  * Walk the list of lsegs in the layout header, and tear down any that should
699  * be destroyed. If "recall_range" is specified then the segment must match
700  * that range. If "seq" is non-zero, then only match segments that were handed
701  * out at or before that sequence.
702  *
703  * Returns number of matching invalid lsegs remaining in list after scanning
704  * it and purging them.
705  */
706 int
pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr * lo,struct list_head * tmp_list,const struct pnfs_layout_range * recall_range,u32 seq)707 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
708 			    struct list_head *tmp_list,
709 			    const struct pnfs_layout_range *recall_range,
710 			    u32 seq)
711 {
712 	struct pnfs_layout_segment *lseg, *next;
713 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
714 	int remaining = 0;
715 
716 	dprintk("%s:Begin lo %p\n", __func__, lo);
717 
718 	if (list_empty(&lo->plh_segs))
719 		return 0;
720 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
721 		if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
722 			dprintk("%s: freeing lseg %p iomode %d seq %u "
723 				"offset %llu length %llu\n", __func__,
724 				lseg, lseg->pls_range.iomode, lseg->pls_seq,
725 				lseg->pls_range.offset, lseg->pls_range.length);
726 			if (mark_lseg_invalid(lseg, tmp_list))
727 				continue;
728 			remaining++;
729 			pnfs_lseg_cancel_io(server, lseg);
730 		}
731 	dprintk("%s:Return %i\n", __func__, remaining);
732 	return remaining;
733 }
734 
pnfs_reset_return_info(struct pnfs_layout_hdr * lo)735 static void pnfs_reset_return_info(struct pnfs_layout_hdr *lo)
736 {
737 	struct pnfs_layout_segment *lseg;
738 
739 	list_for_each_entry(lseg, &lo->plh_return_segs, pls_list)
740 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
741 }
742 
743 static void
pnfs_free_returned_lsegs(struct pnfs_layout_hdr * lo,struct list_head * free_me,const struct pnfs_layout_range * range,u32 seq)744 pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
745 		struct list_head *free_me,
746 		const struct pnfs_layout_range *range,
747 		u32 seq)
748 {
749 	struct pnfs_layout_segment *lseg, *next;
750 
751 	list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
752 		if (pnfs_match_lseg_recall(lseg, range, seq))
753 			list_move_tail(&lseg->pls_list, free_me);
754 	}
755 }
756 
757 /* note free_me must contain lsegs from a single layout_hdr */
758 void
pnfs_free_lseg_list(struct list_head * free_me)759 pnfs_free_lseg_list(struct list_head *free_me)
760 {
761 	struct pnfs_layout_segment *lseg, *tmp;
762 
763 	if (list_empty(free_me))
764 		return;
765 
766 	list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
767 		list_del(&lseg->pls_list);
768 		pnfs_free_lseg(lseg);
769 	}
770 }
771 
__pnfs_destroy_layout(struct nfs_inode * nfsi)772 static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi)
773 {
774 	struct pnfs_layout_hdr *lo;
775 	LIST_HEAD(tmp_list);
776 
777 	spin_lock(&nfsi->vfs_inode.i_lock);
778 	lo = nfsi->layout;
779 	if (lo) {
780 		pnfs_get_layout_hdr(lo);
781 		pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
782 		pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
783 		pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
784 		spin_unlock(&nfsi->vfs_inode.i_lock);
785 		pnfs_free_lseg_list(&tmp_list);
786 		nfs_commit_inode(&nfsi->vfs_inode, 0);
787 		pnfs_put_layout_hdr(lo);
788 	} else
789 		spin_unlock(&nfsi->vfs_inode.i_lock);
790 	return lo;
791 }
792 
pnfs_destroy_layout(struct nfs_inode * nfsi)793 void pnfs_destroy_layout(struct nfs_inode *nfsi)
794 {
795 	__pnfs_destroy_layout(nfsi);
796 }
797 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
798 
pnfs_layout_removed(struct nfs_inode * nfsi,struct pnfs_layout_hdr * lo)799 static bool pnfs_layout_removed(struct nfs_inode *nfsi,
800 				struct pnfs_layout_hdr *lo)
801 {
802 	bool ret;
803 
804 	spin_lock(&nfsi->vfs_inode.i_lock);
805 	ret = nfsi->layout != lo;
806 	spin_unlock(&nfsi->vfs_inode.i_lock);
807 	return ret;
808 }
809 
pnfs_destroy_layout_final(struct nfs_inode * nfsi)810 void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
811 {
812 	struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi);
813 
814 	if (lo)
815 		wait_var_event(lo, pnfs_layout_removed(nfsi, lo));
816 }
817 
818 static bool
pnfs_layout_add_bulk_destroy_list(struct inode * inode,struct list_head * layout_list)819 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
820 		struct list_head *layout_list)
821 {
822 	struct pnfs_layout_hdr *lo;
823 	bool ret = false;
824 
825 	spin_lock(&inode->i_lock);
826 	lo = NFS_I(inode)->layout;
827 	if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
828 		pnfs_get_layout_hdr(lo);
829 		list_add(&lo->plh_bulk_destroy, layout_list);
830 		ret = true;
831 	}
832 	spin_unlock(&inode->i_lock);
833 	return ret;
834 }
835 
836 /* Caller must hold rcu_read_lock and clp->cl_lock */
837 static int
pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client * clp,struct nfs_server * server,struct list_head * layout_list)838 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
839 		struct nfs_server *server,
840 		struct list_head *layout_list)
841 	__must_hold(&clp->cl_lock)
842 	__must_hold(RCU)
843 {
844 	struct pnfs_layout_hdr *lo, *next;
845 	struct inode *inode;
846 
847 	list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
848 		if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
849 		    test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) ||
850 		    !list_empty(&lo->plh_bulk_destroy))
851 			continue;
852 		/* If the sb is being destroyed, just bail */
853 		if (!nfs_sb_active(server->super))
854 			break;
855 		inode = pnfs_grab_inode_layout_hdr(lo);
856 		if (inode != NULL) {
857 			if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags))
858 				list_del_rcu(&lo->plh_layouts);
859 			if (pnfs_layout_add_bulk_destroy_list(inode,
860 						layout_list))
861 				continue;
862 			rcu_read_unlock();
863 			spin_unlock(&clp->cl_lock);
864 			iput(inode);
865 		} else {
866 			rcu_read_unlock();
867 			spin_unlock(&clp->cl_lock);
868 		}
869 		nfs_sb_deactive(server->super);
870 		spin_lock(&clp->cl_lock);
871 		rcu_read_lock();
872 		return -EAGAIN;
873 	}
874 	return 0;
875 }
876 
877 static int
pnfs_layout_free_bulk_destroy_list(struct list_head * layout_list,bool is_bulk_recall)878 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
879 		bool is_bulk_recall)
880 {
881 	struct pnfs_layout_hdr *lo;
882 	struct inode *inode;
883 	LIST_HEAD(lseg_list);
884 	int ret = 0;
885 
886 	while (!list_empty(layout_list)) {
887 		lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
888 				plh_bulk_destroy);
889 		dprintk("%s freeing layout for inode %lu\n", __func__,
890 			lo->plh_inode->i_ino);
891 		inode = lo->plh_inode;
892 
893 		pnfs_layoutcommit_inode(inode, false);
894 
895 		spin_lock(&inode->i_lock);
896 		list_del_init(&lo->plh_bulk_destroy);
897 		if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
898 			if (is_bulk_recall)
899 				set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
900 			ret = -EAGAIN;
901 		}
902 		spin_unlock(&inode->i_lock);
903 		pnfs_free_lseg_list(&lseg_list);
904 		/* Free all lsegs that are attached to commit buckets */
905 		nfs_commit_inode(inode, 0);
906 		pnfs_put_layout_hdr(lo);
907 		nfs_iput_and_deactive(inode);
908 	}
909 	return ret;
910 }
911 
912 int
pnfs_destroy_layouts_byfsid(struct nfs_client * clp,struct nfs_fsid * fsid,bool is_recall)913 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
914 		struct nfs_fsid *fsid,
915 		bool is_recall)
916 {
917 	struct nfs_server *server;
918 	LIST_HEAD(layout_list);
919 
920 	spin_lock(&clp->cl_lock);
921 	rcu_read_lock();
922 restart:
923 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
924 		if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
925 			continue;
926 		if (pnfs_layout_bulk_destroy_byserver_locked(clp,
927 				server,
928 				&layout_list) != 0)
929 			goto restart;
930 	}
931 	rcu_read_unlock();
932 	spin_unlock(&clp->cl_lock);
933 
934 	if (list_empty(&layout_list))
935 		return 0;
936 	return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
937 }
938 
939 int
pnfs_destroy_layouts_byclid(struct nfs_client * clp,bool is_recall)940 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
941 		bool is_recall)
942 {
943 	struct nfs_server *server;
944 	LIST_HEAD(layout_list);
945 
946 	spin_lock(&clp->cl_lock);
947 	rcu_read_lock();
948 restart:
949 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
950 		if (pnfs_layout_bulk_destroy_byserver_locked(clp,
951 					server,
952 					&layout_list) != 0)
953 			goto restart;
954 	}
955 	rcu_read_unlock();
956 	spin_unlock(&clp->cl_lock);
957 
958 	if (list_empty(&layout_list))
959 		return 0;
960 	return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
961 }
962 
963 /*
964  * Called by the state manager to remove all layouts established under an
965  * expired lease.
966  */
967 void
pnfs_destroy_all_layouts(struct nfs_client * clp)968 pnfs_destroy_all_layouts(struct nfs_client *clp)
969 {
970 	nfs4_deviceid_mark_client_invalid(clp);
971 	nfs4_deviceid_purge_client(clp);
972 
973 	pnfs_destroy_layouts_byclid(clp, false);
974 }
975 
976 static void
pnfs_set_layout_cred(struct pnfs_layout_hdr * lo,const struct cred * cred)977 pnfs_set_layout_cred(struct pnfs_layout_hdr *lo, const struct cred *cred)
978 {
979 	const struct cred *old;
980 
981 	if (cred && cred_fscmp(lo->plh_lc_cred, cred) != 0) {
982 		old = xchg(&lo->plh_lc_cred, get_cred(cred));
983 		put_cred(old);
984 	}
985 }
986 
987 /* update lo->plh_stateid with new if is more recent */
988 void
pnfs_set_layout_stateid(struct pnfs_layout_hdr * lo,const nfs4_stateid * new,const struct cred * cred,bool update_barrier)989 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
990 			const struct cred *cred, bool update_barrier)
991 {
992 	u32 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
993 	u32 newseq = be32_to_cpu(new->seqid);
994 
995 	if (!pnfs_layout_is_valid(lo)) {
996 		pnfs_set_layout_cred(lo, cred);
997 		nfs4_stateid_copy(&lo->plh_stateid, new);
998 		lo->plh_barrier = newseq;
999 		pnfs_clear_layoutreturn_info(lo);
1000 		clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1001 		return;
1002 	}
1003 
1004 	if (pnfs_seqid_is_newer(newseq, oldseq))
1005 		nfs4_stateid_copy(&lo->plh_stateid, new);
1006 
1007 	if (update_barrier) {
1008 		pnfs_barrier_update(lo, newseq);
1009 		return;
1010 	}
1011 	/*
1012 	 * Because of wraparound, we want to keep the barrier
1013 	 * "close" to the current seqids. We really only want to
1014 	 * get here from a layoutget call.
1015 	 */
1016 	if (atomic_read(&lo->plh_outstanding) == 1)
1017 		 pnfs_barrier_update(lo, be32_to_cpu(lo->plh_stateid.seqid));
1018 }
1019 
1020 static bool
pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid)1021 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
1022 		const nfs4_stateid *stateid)
1023 {
1024 	u32 seqid = be32_to_cpu(stateid->seqid);
1025 
1026 	return lo->plh_barrier && pnfs_seqid_is_newer(lo->plh_barrier, seqid);
1027 }
1028 
1029 /* lget is set to 1 if called from inside send_layoutget call chain */
1030 static bool
pnfs_layoutgets_blocked(const struct pnfs_layout_hdr * lo)1031 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
1032 {
1033 	return lo->plh_block_lgets ||
1034 		test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
1035 }
1036 
1037 static struct nfs_server *
pnfs_find_server(struct inode * inode,struct nfs_open_context * ctx)1038 pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx)
1039 {
1040 	struct nfs_server *server;
1041 
1042 	if (inode) {
1043 		server = NFS_SERVER(inode);
1044 	} else {
1045 		struct dentry *parent_dir = dget_parent(ctx->dentry);
1046 		server = NFS_SERVER(parent_dir->d_inode);
1047 		dput(parent_dir);
1048 	}
1049 	return server;
1050 }
1051 
nfs4_free_pages(struct page ** pages,size_t size)1052 static void nfs4_free_pages(struct page **pages, size_t size)
1053 {
1054 	int i;
1055 
1056 	if (!pages)
1057 		return;
1058 
1059 	for (i = 0; i < size; i++) {
1060 		if (!pages[i])
1061 			break;
1062 		__free_page(pages[i]);
1063 	}
1064 	kfree(pages);
1065 }
1066 
nfs4_alloc_pages(size_t size,gfp_t gfp_flags)1067 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
1068 {
1069 	struct page **pages;
1070 	int i;
1071 
1072 	pages = kmalloc_array(size, sizeof(struct page *), gfp_flags);
1073 	if (!pages) {
1074 		dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
1075 		return NULL;
1076 	}
1077 
1078 	for (i = 0; i < size; i++) {
1079 		pages[i] = alloc_page(gfp_flags);
1080 		if (!pages[i]) {
1081 			dprintk("%s: failed to allocate page\n", __func__);
1082 			nfs4_free_pages(pages, i);
1083 			return NULL;
1084 		}
1085 	}
1086 
1087 	return pages;
1088 }
1089 
1090 static struct nfs4_layoutget *
pnfs_alloc_init_layoutget_args(struct inode * ino,struct nfs_open_context * ctx,const nfs4_stateid * stateid,const struct pnfs_layout_range * range,gfp_t gfp_flags)1091 pnfs_alloc_init_layoutget_args(struct inode *ino,
1092 	   struct nfs_open_context *ctx,
1093 	   const nfs4_stateid *stateid,
1094 	   const struct pnfs_layout_range *range,
1095 	   gfp_t gfp_flags)
1096 {
1097 	struct nfs_server *server = pnfs_find_server(ino, ctx);
1098 	size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response;
1099 	size_t max_pages = max_response_pages(server);
1100 	struct nfs4_layoutget *lgp;
1101 
1102 	dprintk("--> %s\n", __func__);
1103 
1104 	lgp = kzalloc(sizeof(*lgp), gfp_flags);
1105 	if (lgp == NULL)
1106 		return NULL;
1107 
1108 	if (max_reply_sz) {
1109 		size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
1110 		if (npages < max_pages)
1111 			max_pages = npages;
1112 	}
1113 
1114 	lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
1115 	if (!lgp->args.layout.pages) {
1116 		kfree(lgp);
1117 		return NULL;
1118 	}
1119 	lgp->args.layout.pglen = max_pages * PAGE_SIZE;
1120 	lgp->res.layoutp = &lgp->args.layout;
1121 
1122 	/* Don't confuse uninitialised result and success */
1123 	lgp->res.status = -NFS4ERR_DELAY;
1124 
1125 	lgp->args.minlength = PAGE_SIZE;
1126 	if (lgp->args.minlength > range->length)
1127 		lgp->args.minlength = range->length;
1128 	if (ino) {
1129 		loff_t i_size = i_size_read(ino);
1130 
1131 		if (range->iomode == IOMODE_READ) {
1132 			if (range->offset >= i_size)
1133 				lgp->args.minlength = 0;
1134 			else if (i_size - range->offset < lgp->args.minlength)
1135 				lgp->args.minlength = i_size - range->offset;
1136 		}
1137 	}
1138 	lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
1139 	pnfs_copy_range(&lgp->args.range, range);
1140 	lgp->args.type = server->pnfs_curr_ld->id;
1141 	lgp->args.inode = ino;
1142 	lgp->args.ctx = get_nfs_open_context(ctx);
1143 	nfs4_stateid_copy(&lgp->args.stateid, stateid);
1144 	lgp->gfp_flags = gfp_flags;
1145 	lgp->cred = ctx->cred;
1146 	return lgp;
1147 }
1148 
pnfs_layoutget_free(struct nfs4_layoutget * lgp)1149 void pnfs_layoutget_free(struct nfs4_layoutget *lgp)
1150 {
1151 	size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE;
1152 
1153 	nfs4_free_pages(lgp->args.layout.pages, max_pages);
1154 	pnfs_put_layout_hdr(lgp->lo);
1155 	put_nfs_open_context(lgp->args.ctx);
1156 	kfree(lgp);
1157 }
1158 
pnfs_clear_layoutcommit(struct inode * inode,struct list_head * head)1159 static void pnfs_clear_layoutcommit(struct inode *inode,
1160 		struct list_head *head)
1161 {
1162 	struct nfs_inode *nfsi = NFS_I(inode);
1163 	struct pnfs_layout_segment *lseg, *tmp;
1164 
1165 	if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1166 		return;
1167 	list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
1168 		if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1169 			continue;
1170 		pnfs_lseg_dec_and_remove_zero(lseg, head);
1171 	}
1172 }
1173 
pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr * lo,const nfs4_stateid * arg_stateid,const struct pnfs_layout_range * range,const nfs4_stateid * stateid)1174 void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
1175 		const nfs4_stateid *arg_stateid,
1176 		const struct pnfs_layout_range *range,
1177 		const nfs4_stateid *stateid)
1178 {
1179 	struct inode *inode = lo->plh_inode;
1180 	LIST_HEAD(freeme);
1181 
1182 	spin_lock(&inode->i_lock);
1183 	if (!nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
1184 		goto out_unlock;
1185 	if (stateid && pnfs_layout_is_valid(lo)) {
1186 		u32 seq = be32_to_cpu(arg_stateid->seqid);
1187 
1188 		pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
1189 		pnfs_free_returned_lsegs(lo, &freeme, range, seq);
1190 		pnfs_set_layout_stateid(lo, stateid, NULL, true);
1191 		pnfs_reset_return_info(lo);
1192 	} else
1193 		pnfs_mark_layout_stateid_invalid(lo, &freeme);
1194 out_unlock:
1195 	pnfs_clear_layoutreturn_waitbit(lo);
1196 	spin_unlock(&inode->i_lock);
1197 	pnfs_free_lseg_list(&freeme);
1198 
1199 }
1200 
1201 static bool
pnfs_prepare_layoutreturn(struct pnfs_layout_hdr * lo,nfs4_stateid * stateid,const struct cred ** cred,enum pnfs_iomode * iomode)1202 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1203 		nfs4_stateid *stateid,
1204 		const struct cred **cred,
1205 		enum pnfs_iomode *iomode)
1206 {
1207 	/* Serialise LAYOUTGET/LAYOUTRETURN */
1208 	if (atomic_read(&lo->plh_outstanding) != 0 && lo->plh_return_seq == 0)
1209 		return false;
1210 	if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
1211 		return false;
1212 	set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1213 	pnfs_get_layout_hdr(lo);
1214 	nfs4_stateid_copy(stateid, &lo->plh_stateid);
1215 	*cred = get_cred(lo->plh_lc_cred);
1216 	if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1217 		if (lo->plh_return_seq != 0)
1218 			stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1219 		if (iomode != NULL)
1220 			*iomode = lo->plh_return_iomode;
1221 		pnfs_clear_layoutreturn_info(lo);
1222 	} else if (iomode != NULL)
1223 		*iomode = IOMODE_ANY;
1224 	pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid));
1225 	return true;
1226 }
1227 
1228 static void
pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args * args,struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid,enum pnfs_iomode iomode)1229 pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1230 		struct pnfs_layout_hdr *lo,
1231 		const nfs4_stateid *stateid,
1232 		enum pnfs_iomode iomode)
1233 {
1234 	struct inode *inode = lo->plh_inode;
1235 
1236 	args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1237 	args->inode = inode;
1238 	args->range.iomode = iomode;
1239 	args->range.offset = 0;
1240 	args->range.length = NFS4_MAX_UINT64;
1241 	args->layout = lo;
1242 	nfs4_stateid_copy(&args->stateid, stateid);
1243 }
1244 
1245 static int
pnfs_send_layoutreturn(struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid,const struct cred ** pcred,enum pnfs_iomode iomode,bool sync)1246 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo,
1247 		       const nfs4_stateid *stateid,
1248 		       const struct cred **pcred,
1249 		       enum pnfs_iomode iomode,
1250 		       bool sync)
1251 {
1252 	struct inode *ino = lo->plh_inode;
1253 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1254 	struct nfs4_layoutreturn *lrp;
1255 	const struct cred *cred = *pcred;
1256 	int status = 0;
1257 
1258 	*pcred = NULL;
1259 	lrp = kzalloc(sizeof(*lrp), nfs_io_gfp_mask());
1260 	if (unlikely(lrp == NULL)) {
1261 		status = -ENOMEM;
1262 		spin_lock(&ino->i_lock);
1263 		pnfs_clear_layoutreturn_waitbit(lo);
1264 		spin_unlock(&ino->i_lock);
1265 		put_cred(cred);
1266 		pnfs_put_layout_hdr(lo);
1267 		goto out;
1268 	}
1269 
1270 	pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
1271 	lrp->args.ld_private = &lrp->ld_private;
1272 	lrp->clp = NFS_SERVER(ino)->nfs_client;
1273 	lrp->cred = cred;
1274 	if (ld->prepare_layoutreturn)
1275 		ld->prepare_layoutreturn(&lrp->args);
1276 
1277 	status = nfs4_proc_layoutreturn(lrp, sync);
1278 out:
1279 	dprintk("<-- %s status: %d\n", __func__, status);
1280 	return status;
1281 }
1282 
1283 static bool
pnfs_layout_segments_returnable(struct pnfs_layout_hdr * lo,enum pnfs_iomode iomode,u32 seq)1284 pnfs_layout_segments_returnable(struct pnfs_layout_hdr *lo,
1285 				enum pnfs_iomode iomode,
1286 				u32 seq)
1287 {
1288 	struct pnfs_layout_range recall_range = {
1289 		.length = NFS4_MAX_UINT64,
1290 		.iomode = iomode,
1291 	};
1292 	return pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
1293 					       &recall_range, seq) != -EBUSY;
1294 }
1295 
1296 /* Return true if layoutreturn is needed */
1297 static bool
pnfs_layout_need_return(struct pnfs_layout_hdr * lo)1298 pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1299 {
1300 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1301 		return false;
1302 	return pnfs_layout_segments_returnable(lo, lo->plh_return_iomode,
1303 					       lo->plh_return_seq);
1304 }
1305 
pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr * lo)1306 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1307 {
1308 	struct inode *inode= lo->plh_inode;
1309 
1310 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1311 		return;
1312 	spin_lock(&inode->i_lock);
1313 	if (pnfs_layout_need_return(lo)) {
1314 		const struct cred *cred;
1315 		nfs4_stateid stateid;
1316 		enum pnfs_iomode iomode;
1317 		bool send;
1318 
1319 		send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
1320 		spin_unlock(&inode->i_lock);
1321 		if (send) {
1322 			/* Send an async layoutreturn so we dont deadlock */
1323 			pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
1324 		}
1325 	} else
1326 		spin_unlock(&inode->i_lock);
1327 }
1328 
1329 /*
1330  * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1331  * when the layout segment list is empty.
1332  *
1333  * Note that a pnfs_layout_hdr can exist with an empty layout segment
1334  * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1335  * deviceid is marked invalid.
1336  */
1337 int
_pnfs_return_layout(struct inode * ino)1338 _pnfs_return_layout(struct inode *ino)
1339 {
1340 	struct pnfs_layout_hdr *lo = NULL;
1341 	struct nfs_inode *nfsi = NFS_I(ino);
1342 	struct pnfs_layout_range range = {
1343 		.iomode		= IOMODE_ANY,
1344 		.offset		= 0,
1345 		.length		= NFS4_MAX_UINT64,
1346 	};
1347 	LIST_HEAD(tmp_list);
1348 	const struct cred *cred;
1349 	nfs4_stateid stateid;
1350 	int status = 0;
1351 	bool send, valid_layout;
1352 
1353 	dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1354 
1355 	spin_lock(&ino->i_lock);
1356 	lo = nfsi->layout;
1357 	if (!lo) {
1358 		spin_unlock(&ino->i_lock);
1359 		dprintk("NFS: %s no layout to return\n", __func__);
1360 		goto out;
1361 	}
1362 	/* Reference matched in nfs4_layoutreturn_release */
1363 	pnfs_get_layout_hdr(lo);
1364 	/* Is there an outstanding layoutreturn ? */
1365 	if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1366 		spin_unlock(&ino->i_lock);
1367 		if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1368 					TASK_UNINTERRUPTIBLE))
1369 			goto out_put_layout_hdr;
1370 		spin_lock(&ino->i_lock);
1371 	}
1372 	valid_layout = pnfs_layout_is_valid(lo);
1373 	pnfs_clear_layoutcommit(ino, &tmp_list);
1374 	pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
1375 
1376 	if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
1377 		NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1378 
1379 	/* Don't send a LAYOUTRETURN if list was initially empty */
1380 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
1381 			!valid_layout) {
1382 		spin_unlock(&ino->i_lock);
1383 		dprintk("NFS: %s no layout segments to return\n", __func__);
1384 		goto out_wait_layoutreturn;
1385 	}
1386 
1387 	send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL);
1388 	spin_unlock(&ino->i_lock);
1389 	if (send)
1390 		status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY, true);
1391 out_wait_layoutreturn:
1392 	wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE);
1393 out_put_layout_hdr:
1394 	pnfs_free_lseg_list(&tmp_list);
1395 	pnfs_put_layout_hdr(lo);
1396 out:
1397 	dprintk("<-- %s status: %d\n", __func__, status);
1398 	return status;
1399 }
1400 
1401 int
pnfs_commit_and_return_layout(struct inode * inode)1402 pnfs_commit_and_return_layout(struct inode *inode)
1403 {
1404 	struct pnfs_layout_hdr *lo;
1405 	int ret;
1406 
1407 	spin_lock(&inode->i_lock);
1408 	lo = NFS_I(inode)->layout;
1409 	if (lo == NULL) {
1410 		spin_unlock(&inode->i_lock);
1411 		return 0;
1412 	}
1413 	pnfs_get_layout_hdr(lo);
1414 	/* Block new layoutgets and read/write to ds */
1415 	lo->plh_block_lgets++;
1416 	spin_unlock(&inode->i_lock);
1417 	filemap_fdatawait(inode->i_mapping);
1418 	ret = pnfs_layoutcommit_inode(inode, true);
1419 	if (ret == 0)
1420 		ret = _pnfs_return_layout(inode);
1421 	spin_lock(&inode->i_lock);
1422 	lo->plh_block_lgets--;
1423 	spin_unlock(&inode->i_lock);
1424 	pnfs_put_layout_hdr(lo);
1425 	return ret;
1426 }
1427 
pnfs_roc(struct inode * ino,struct nfs4_layoutreturn_args * args,struct nfs4_layoutreturn_res * res,const struct cred * cred)1428 bool pnfs_roc(struct inode *ino,
1429 		struct nfs4_layoutreturn_args *args,
1430 		struct nfs4_layoutreturn_res *res,
1431 		const struct cred *cred)
1432 {
1433 	struct nfs_inode *nfsi = NFS_I(ino);
1434 	struct nfs_open_context *ctx;
1435 	struct nfs4_state *state;
1436 	struct pnfs_layout_hdr *lo;
1437 	struct pnfs_layout_segment *lseg, *next;
1438 	const struct cred *lc_cred;
1439 	nfs4_stateid stateid;
1440 	enum pnfs_iomode iomode = 0;
1441 	bool layoutreturn = false, roc = false;
1442 	bool skip_read = false;
1443 
1444 	if (!nfs_have_layout(ino))
1445 		return false;
1446 retry:
1447 	rcu_read_lock();
1448 	spin_lock(&ino->i_lock);
1449 	lo = nfsi->layout;
1450 	if (!lo || !pnfs_layout_is_valid(lo) ||
1451 	    test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1452 		lo = NULL;
1453 		goto out_noroc;
1454 	}
1455 	pnfs_get_layout_hdr(lo);
1456 	if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1457 		spin_unlock(&ino->i_lock);
1458 		rcu_read_unlock();
1459 		wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1460 				TASK_UNINTERRUPTIBLE);
1461 		pnfs_put_layout_hdr(lo);
1462 		goto retry;
1463 	}
1464 
1465 	/* no roc if we hold a delegation */
1466 	if (nfs4_check_delegation(ino, FMODE_READ)) {
1467 		if (nfs4_check_delegation(ino, FMODE_WRITE))
1468 			goto out_noroc;
1469 		skip_read = true;
1470 	}
1471 
1472 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1473 		state = ctx->state;
1474 		if (state == NULL)
1475 			continue;
1476 		/* Don't return layout if there is open file state */
1477 		if (state->state & FMODE_WRITE)
1478 			goto out_noroc;
1479 		if (state->state & FMODE_READ)
1480 			skip_read = true;
1481 	}
1482 
1483 
1484 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
1485 		if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
1486 			continue;
1487 		/* If we are sending layoutreturn, invalidate all valid lsegs */
1488 		if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1489 			continue;
1490 		/*
1491 		 * Note: mark lseg for return so pnfs_layout_remove_lseg
1492 		 * doesn't invalidate the layout for us.
1493 		 */
1494 		set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1495 		if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1496 			continue;
1497 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
1498 	}
1499 
1500 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1501 		goto out_noroc;
1502 
1503 	/* ROC in two conditions:
1504 	 * 1. there are ROC lsegs
1505 	 * 2. we don't send layoutreturn
1506 	 */
1507 	/* lo ref dropped in pnfs_roc_release() */
1508 	layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode);
1509 	/* If the creds don't match, we can't compound the layoutreturn */
1510 	if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0)
1511 		goto out_noroc;
1512 
1513 	roc = layoutreturn;
1514 	pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1515 	res->lrs_present = 0;
1516 	layoutreturn = false;
1517 	put_cred(lc_cred);
1518 
1519 out_noroc:
1520 	spin_unlock(&ino->i_lock);
1521 	rcu_read_unlock();
1522 	pnfs_layoutcommit_inode(ino, true);
1523 	if (roc) {
1524 		struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1525 		if (ld->prepare_layoutreturn)
1526 			ld->prepare_layoutreturn(args);
1527 		pnfs_put_layout_hdr(lo);
1528 		return true;
1529 	}
1530 	if (layoutreturn)
1531 		pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, true);
1532 	pnfs_put_layout_hdr(lo);
1533 	return false;
1534 }
1535 
pnfs_roc_done(struct rpc_task * task,struct nfs4_layoutreturn_args ** argpp,struct nfs4_layoutreturn_res ** respp,int * ret)1536 int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp,
1537 		  struct nfs4_layoutreturn_res **respp, int *ret)
1538 {
1539 	struct nfs4_layoutreturn_args *arg = *argpp;
1540 	int retval = -EAGAIN;
1541 
1542 	if (!arg)
1543 		return 0;
1544 	/* Handle Layoutreturn errors */
1545 	switch (*ret) {
1546 	case 0:
1547 		retval = 0;
1548 		break;
1549 	case -NFS4ERR_NOMATCHING_LAYOUT:
1550 		/* Was there an RPC level error? If not, retry */
1551 		if (task->tk_rpc_status == 0)
1552 			break;
1553 		/* If the call was not sent, let caller handle it */
1554 		if (!RPC_WAS_SENT(task))
1555 			return 0;
1556 		/*
1557 		 * Otherwise, assume the call succeeded and
1558 		 * that we need to release the layout
1559 		 */
1560 		*ret = 0;
1561 		(*respp)->lrs_present = 0;
1562 		retval = 0;
1563 		break;
1564 	case -NFS4ERR_DELAY:
1565 		/* Let the caller handle the retry */
1566 		*ret = -NFS4ERR_NOMATCHING_LAYOUT;
1567 		return 0;
1568 	case -NFS4ERR_OLD_STATEID:
1569 		if (!nfs4_layout_refresh_old_stateid(&arg->stateid,
1570 						     &arg->range, arg->inode))
1571 			break;
1572 		*ret = -NFS4ERR_NOMATCHING_LAYOUT;
1573 		return -EAGAIN;
1574 	}
1575 	*argpp = NULL;
1576 	*respp = NULL;
1577 	return retval;
1578 }
1579 
pnfs_roc_release(struct nfs4_layoutreturn_args * args,struct nfs4_layoutreturn_res * res,int ret)1580 void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1581 		struct nfs4_layoutreturn_res *res,
1582 		int ret)
1583 {
1584 	struct pnfs_layout_hdr *lo = args->layout;
1585 	struct inode *inode = args->inode;
1586 	const nfs4_stateid *res_stateid = NULL;
1587 	struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
1588 
1589 	switch (ret) {
1590 	case -NFS4ERR_NOMATCHING_LAYOUT:
1591 		spin_lock(&inode->i_lock);
1592 		if (pnfs_layout_is_valid(lo) &&
1593 		    nfs4_stateid_match_other(&args->stateid, &lo->plh_stateid))
1594 			pnfs_set_plh_return_info(lo, args->range.iomode, 0);
1595 		pnfs_clear_layoutreturn_waitbit(lo);
1596 		spin_unlock(&inode->i_lock);
1597 		break;
1598 	case 0:
1599 		if (res->lrs_present)
1600 			res_stateid = &res->stateid;
1601 		fallthrough;
1602 	default:
1603 		pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range,
1604 					     res_stateid);
1605 	}
1606 	trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret);
1607 	if (ld_private && ld_private->ops && ld_private->ops->free)
1608 		ld_private->ops->free(ld_private);
1609 	pnfs_put_layout_hdr(lo);
1610 }
1611 
pnfs_wait_on_layoutreturn(struct inode * ino,struct rpc_task * task)1612 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1613 {
1614 	struct nfs_inode *nfsi = NFS_I(ino);
1615         struct pnfs_layout_hdr *lo;
1616         bool sleep = false;
1617 
1618 	/* we might not have grabbed lo reference. so need to check under
1619 	 * i_lock */
1620         spin_lock(&ino->i_lock);
1621         lo = nfsi->layout;
1622         if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1623                 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1624                 sleep = true;
1625 	}
1626         spin_unlock(&ino->i_lock);
1627         return sleep;
1628 }
1629 
1630 /*
1631  * Compare two layout segments for sorting into layout cache.
1632  * We want to preferentially return RW over RO layouts, so ensure those
1633  * are seen first.
1634  */
1635 static s64
pnfs_lseg_range_cmp(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)1636 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1637 	   const struct pnfs_layout_range *l2)
1638 {
1639 	s64 d;
1640 
1641 	/* high offset > low offset */
1642 	d = l1->offset - l2->offset;
1643 	if (d)
1644 		return d;
1645 
1646 	/* short length > long length */
1647 	d = l2->length - l1->length;
1648 	if (d)
1649 		return d;
1650 
1651 	/* read > read/write */
1652 	return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1653 }
1654 
1655 static bool
pnfs_lseg_range_is_after(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)1656 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1657 		const struct pnfs_layout_range *l2)
1658 {
1659 	return pnfs_lseg_range_cmp(l1, l2) > 0;
1660 }
1661 
1662 static bool
pnfs_lseg_no_merge(struct pnfs_layout_segment * lseg,struct pnfs_layout_segment * old)1663 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1664 		struct pnfs_layout_segment *old)
1665 {
1666 	return false;
1667 }
1668 
1669 void
pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,bool (* is_after)(const struct pnfs_layout_range *,const struct pnfs_layout_range *),bool (* do_merge)(struct pnfs_layout_segment *,struct pnfs_layout_segment *),struct list_head * free_me)1670 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1671 		   struct pnfs_layout_segment *lseg,
1672 		   bool (*is_after)(const struct pnfs_layout_range *,
1673 			   const struct pnfs_layout_range *),
1674 		   bool (*do_merge)(struct pnfs_layout_segment *,
1675 			   struct pnfs_layout_segment *),
1676 		   struct list_head *free_me)
1677 {
1678 	struct pnfs_layout_segment *lp, *tmp;
1679 
1680 	dprintk("%s:Begin\n", __func__);
1681 
1682 	list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1683 		if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1684 			continue;
1685 		if (do_merge(lseg, lp)) {
1686 			mark_lseg_invalid(lp, free_me);
1687 			continue;
1688 		}
1689 		if (is_after(&lseg->pls_range, &lp->pls_range))
1690 			continue;
1691 		list_add_tail(&lseg->pls_list, &lp->pls_list);
1692 		dprintk("%s: inserted lseg %p "
1693 			"iomode %d offset %llu length %llu before "
1694 			"lp %p iomode %d offset %llu length %llu\n",
1695 			__func__, lseg, lseg->pls_range.iomode,
1696 			lseg->pls_range.offset, lseg->pls_range.length,
1697 			lp, lp->pls_range.iomode, lp->pls_range.offset,
1698 			lp->pls_range.length);
1699 		goto out;
1700 	}
1701 	list_add_tail(&lseg->pls_list, &lo->plh_segs);
1702 	dprintk("%s: inserted lseg %p "
1703 		"iomode %d offset %llu length %llu at tail\n",
1704 		__func__, lseg, lseg->pls_range.iomode,
1705 		lseg->pls_range.offset, lseg->pls_range.length);
1706 out:
1707 	pnfs_get_layout_hdr(lo);
1708 
1709 	dprintk("%s:Return\n", __func__);
1710 }
1711 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1712 
1713 static void
pnfs_layout_insert_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,struct list_head * free_me)1714 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1715 		   struct pnfs_layout_segment *lseg,
1716 		   struct list_head *free_me)
1717 {
1718 	struct inode *inode = lo->plh_inode;
1719 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1720 
1721 	if (ld->add_lseg != NULL)
1722 		ld->add_lseg(lo, lseg, free_me);
1723 	else
1724 		pnfs_generic_layout_insert_lseg(lo, lseg,
1725 				pnfs_lseg_range_is_after,
1726 				pnfs_lseg_no_merge,
1727 				free_me);
1728 }
1729 
1730 static struct pnfs_layout_hdr *
alloc_init_layout_hdr(struct inode * ino,struct nfs_open_context * ctx,gfp_t gfp_flags)1731 alloc_init_layout_hdr(struct inode *ino,
1732 		      struct nfs_open_context *ctx,
1733 		      gfp_t gfp_flags)
1734 {
1735 	struct pnfs_layout_hdr *lo;
1736 
1737 	lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1738 	if (!lo)
1739 		return NULL;
1740 	refcount_set(&lo->plh_refcount, 1);
1741 	INIT_LIST_HEAD(&lo->plh_layouts);
1742 	INIT_LIST_HEAD(&lo->plh_segs);
1743 	INIT_LIST_HEAD(&lo->plh_return_segs);
1744 	INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1745 	lo->plh_inode = ino;
1746 	lo->plh_lc_cred = get_cred(ctx->cred);
1747 	lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1748 	return lo;
1749 }
1750 
1751 static struct pnfs_layout_hdr *
pnfs_find_alloc_layout(struct inode * ino,struct nfs_open_context * ctx,gfp_t gfp_flags)1752 pnfs_find_alloc_layout(struct inode *ino,
1753 		       struct nfs_open_context *ctx,
1754 		       gfp_t gfp_flags)
1755 	__releases(&ino->i_lock)
1756 	__acquires(&ino->i_lock)
1757 {
1758 	struct nfs_inode *nfsi = NFS_I(ino);
1759 	struct pnfs_layout_hdr *new = NULL;
1760 
1761 	dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1762 
1763 	if (nfsi->layout != NULL)
1764 		goto out_existing;
1765 	spin_unlock(&ino->i_lock);
1766 	new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1767 	spin_lock(&ino->i_lock);
1768 
1769 	if (likely(nfsi->layout == NULL)) {	/* Won the race? */
1770 		nfsi->layout = new;
1771 		return new;
1772 	} else if (new != NULL)
1773 		pnfs_free_layout_hdr(new);
1774 out_existing:
1775 	pnfs_get_layout_hdr(nfsi->layout);
1776 	return nfsi->layout;
1777 }
1778 
1779 /*
1780  * iomode matching rules:
1781  * iomode	lseg	strict match
1782  *                      iomode
1783  * -----	-----	------ -----
1784  * ANY		READ	N/A    true
1785  * ANY		RW	N/A    true
1786  * RW		READ	N/A    false
1787  * RW		RW	N/A    true
1788  * READ		READ	N/A    true
1789  * READ		RW	true   false
1790  * READ		RW	false  true
1791  */
1792 static bool
pnfs_lseg_range_match(const struct pnfs_layout_range * ls_range,const struct pnfs_layout_range * range,bool strict_iomode)1793 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1794 		 const struct pnfs_layout_range *range,
1795 		 bool strict_iomode)
1796 {
1797 	struct pnfs_layout_range range1;
1798 
1799 	if ((range->iomode == IOMODE_RW &&
1800 	     ls_range->iomode != IOMODE_RW) ||
1801 	    (range->iomode != ls_range->iomode &&
1802 	     strict_iomode) ||
1803 	    !pnfs_lseg_range_intersecting(ls_range, range))
1804 		return false;
1805 
1806 	/* range1 covers only the first byte in the range */
1807 	range1 = *range;
1808 	range1.length = 1;
1809 	return pnfs_lseg_range_contained(ls_range, &range1);
1810 }
1811 
1812 /*
1813  * lookup range in layout
1814  */
1815 static struct pnfs_layout_segment *
pnfs_find_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_range * range,bool strict_iomode)1816 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1817 		struct pnfs_layout_range *range,
1818 		bool strict_iomode)
1819 {
1820 	struct pnfs_layout_segment *lseg, *ret = NULL;
1821 
1822 	dprintk("%s:Begin\n", __func__);
1823 
1824 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1825 		if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1826 		    pnfs_lseg_range_match(&lseg->pls_range, range,
1827 					  strict_iomode)) {
1828 			ret = pnfs_get_lseg(lseg);
1829 			break;
1830 		}
1831 	}
1832 
1833 	dprintk("%s:Return lseg %p ref %d\n",
1834 		__func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
1835 	return ret;
1836 }
1837 
1838 /*
1839  * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1840  * to the MDS or over pNFS
1841  *
1842  * The nfs_inode read_io and write_io fields are cumulative counters reset
1843  * when there are no layout segments. Note that in pnfs_update_layout iomode
1844  * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1845  * WRITE request.
1846  *
1847  * A return of true means use MDS I/O.
1848  *
1849  * From rfc 5661:
1850  * If a file's size is smaller than the file size threshold, data accesses
1851  * SHOULD be sent to the metadata server.  If an I/O request has a length that
1852  * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1853  * server.  If both file size and I/O size are provided, the client SHOULD
1854  * reach or exceed  both thresholds before sending its read or write
1855  * requests to the data server.
1856  */
pnfs_within_mdsthreshold(struct nfs_open_context * ctx,struct inode * ino,int iomode)1857 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1858 				     struct inode *ino, int iomode)
1859 {
1860 	struct nfs4_threshold *t = ctx->mdsthreshold;
1861 	struct nfs_inode *nfsi = NFS_I(ino);
1862 	loff_t fsize = i_size_read(ino);
1863 	bool size = false, size_set = false, io = false, io_set = false, ret = false;
1864 
1865 	if (t == NULL)
1866 		return ret;
1867 
1868 	dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1869 		__func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1870 
1871 	switch (iomode) {
1872 	case IOMODE_READ:
1873 		if (t->bm & THRESHOLD_RD) {
1874 			dprintk("%s fsize %llu\n", __func__, fsize);
1875 			size_set = true;
1876 			if (fsize < t->rd_sz)
1877 				size = true;
1878 		}
1879 		if (t->bm & THRESHOLD_RD_IO) {
1880 			dprintk("%s nfsi->read_io %llu\n", __func__,
1881 				nfsi->read_io);
1882 			io_set = true;
1883 			if (nfsi->read_io < t->rd_io_sz)
1884 				io = true;
1885 		}
1886 		break;
1887 	case IOMODE_RW:
1888 		if (t->bm & THRESHOLD_WR) {
1889 			dprintk("%s fsize %llu\n", __func__, fsize);
1890 			size_set = true;
1891 			if (fsize < t->wr_sz)
1892 				size = true;
1893 		}
1894 		if (t->bm & THRESHOLD_WR_IO) {
1895 			dprintk("%s nfsi->write_io %llu\n", __func__,
1896 				nfsi->write_io);
1897 			io_set = true;
1898 			if (nfsi->write_io < t->wr_io_sz)
1899 				io = true;
1900 		}
1901 		break;
1902 	}
1903 	if (size_set && io_set) {
1904 		if (size && io)
1905 			ret = true;
1906 	} else if (size || io)
1907 		ret = true;
1908 
1909 	dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1910 	return ret;
1911 }
1912 
pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr * lo)1913 static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1914 {
1915 	/*
1916 	 * send layoutcommit as it can hold up layoutreturn due to lseg
1917 	 * reference
1918 	 */
1919 	pnfs_layoutcommit_inode(lo->plh_inode, false);
1920 	return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1921 				   nfs_wait_bit_killable,
1922 				   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
1923 }
1924 
nfs_layoutget_begin(struct pnfs_layout_hdr * lo)1925 static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
1926 {
1927 	atomic_inc(&lo->plh_outstanding);
1928 }
1929 
nfs_layoutget_end(struct pnfs_layout_hdr * lo)1930 static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
1931 {
1932 	if (atomic_dec_and_test(&lo->plh_outstanding) &&
1933 	    test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags)) {
1934 		smp_mb__after_atomic();
1935 		wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN);
1936 	}
1937 }
1938 
pnfs_is_first_layoutget(struct pnfs_layout_hdr * lo)1939 static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo)
1940 {
1941 	return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags);
1942 }
1943 
pnfs_clear_first_layoutget(struct pnfs_layout_hdr * lo)1944 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1945 {
1946 	unsigned long *bitlock = &lo->plh_flags;
1947 
1948 	clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1949 	smp_mb__after_atomic();
1950 	wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1951 }
1952 
_add_to_server_list(struct pnfs_layout_hdr * lo,struct nfs_server * server)1953 static void _add_to_server_list(struct pnfs_layout_hdr *lo,
1954 				struct nfs_server *server)
1955 {
1956 	if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
1957 		struct nfs_client *clp = server->nfs_client;
1958 
1959 		/* The lo must be on the clp list if there is any
1960 		 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1961 		 */
1962 		spin_lock(&clp->cl_lock);
1963 		list_add_tail_rcu(&lo->plh_layouts, &server->layouts);
1964 		spin_unlock(&clp->cl_lock);
1965 	}
1966 }
1967 
1968 /*
1969  * Layout segment is retreived from the server if not cached.
1970  * The appropriate layout segment is referenced and returned to the caller.
1971  */
1972 struct pnfs_layout_segment *
pnfs_update_layout(struct inode * ino,struct nfs_open_context * ctx,loff_t pos,u64 count,enum pnfs_iomode iomode,bool strict_iomode,gfp_t gfp_flags)1973 pnfs_update_layout(struct inode *ino,
1974 		   struct nfs_open_context *ctx,
1975 		   loff_t pos,
1976 		   u64 count,
1977 		   enum pnfs_iomode iomode,
1978 		   bool strict_iomode,
1979 		   gfp_t gfp_flags)
1980 {
1981 	struct pnfs_layout_range arg = {
1982 		.iomode = iomode,
1983 		.offset = pos,
1984 		.length = count,
1985 	};
1986 	unsigned pg_offset;
1987 	struct nfs_server *server = NFS_SERVER(ino);
1988 	struct nfs_client *clp = server->nfs_client;
1989 	struct pnfs_layout_hdr *lo = NULL;
1990 	struct pnfs_layout_segment *lseg = NULL;
1991 	struct nfs4_layoutget *lgp;
1992 	nfs4_stateid stateid;
1993 	long timeout = 0;
1994 	unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1995 	bool first;
1996 
1997 	if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1998 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1999 				 PNFS_UPDATE_LAYOUT_NO_PNFS);
2000 		goto out;
2001 	}
2002 
2003 	if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
2004 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2005 				 PNFS_UPDATE_LAYOUT_MDSTHRESH);
2006 		goto out;
2007 	}
2008 
2009 lookup_again:
2010 	if (!nfs4_valid_open_stateid(ctx->state)) {
2011 		trace_pnfs_update_layout(ino, pos, count,
2012 					 iomode, lo, lseg,
2013 					 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2014 		lseg = ERR_PTR(-EIO);
2015 		goto out;
2016 	}
2017 
2018 	lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
2019 	if (IS_ERR(lseg))
2020 		goto out;
2021 	first = false;
2022 	spin_lock(&ino->i_lock);
2023 	lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
2024 	if (lo == NULL) {
2025 		spin_unlock(&ino->i_lock);
2026 		lseg = ERR_PTR(-ENOMEM);
2027 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2028 				 PNFS_UPDATE_LAYOUT_NOMEM);
2029 		goto out;
2030 	}
2031 
2032 	/* Do we even need to bother with this? */
2033 	if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
2034 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2035 				 PNFS_UPDATE_LAYOUT_BULK_RECALL);
2036 		dprintk("%s matches recall, use MDS\n", __func__);
2037 		goto out_unlock;
2038 	}
2039 
2040 	/* if LAYOUTGET already failed once we don't try again */
2041 	if (pnfs_layout_io_test_failed(lo, iomode)) {
2042 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2043 				 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
2044 		goto out_unlock;
2045 	}
2046 
2047 	/*
2048 	 * If the layout segment list is empty, but there are outstanding
2049 	 * layoutget calls, then they might be subject to a layoutrecall.
2050 	 */
2051 	if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2052 	    atomic_read(&lo->plh_outstanding) != 0) {
2053 		spin_unlock(&ino->i_lock);
2054 		lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN,
2055 					   TASK_KILLABLE));
2056 		if (IS_ERR(lseg))
2057 			goto out_put_layout_hdr;
2058 		pnfs_put_layout_hdr(lo);
2059 		goto lookup_again;
2060 	}
2061 
2062 	/*
2063 	 * Because we free lsegs when sending LAYOUTRETURN, we need to wait
2064 	 * for LAYOUTRETURN.
2065 	 */
2066 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
2067 		spin_unlock(&ino->i_lock);
2068 		dprintk("%s wait for layoutreturn\n", __func__);
2069 		lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
2070 		if (!IS_ERR(lseg)) {
2071 			pnfs_put_layout_hdr(lo);
2072 			dprintk("%s retrying\n", __func__);
2073 			trace_pnfs_update_layout(ino, pos, count, iomode, lo,
2074 						 lseg,
2075 						 PNFS_UPDATE_LAYOUT_RETRY);
2076 			goto lookup_again;
2077 		}
2078 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2079 					 PNFS_UPDATE_LAYOUT_RETURN);
2080 		goto out_put_layout_hdr;
2081 	}
2082 
2083 	lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
2084 	if (lseg) {
2085 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2086 				PNFS_UPDATE_LAYOUT_FOUND_CACHED);
2087 		goto out_unlock;
2088 	}
2089 
2090 	/*
2091 	 * Choose a stateid for the LAYOUTGET. If we don't have a layout
2092 	 * stateid, or it has been invalidated, then we must use the open
2093 	 * stateid.
2094 	 */
2095 	if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
2096 		int status;
2097 
2098 		/*
2099 		 * The first layoutget for the file. Need to serialize per
2100 		 * RFC 5661 Errata 3208.
2101 		 */
2102 		if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
2103 				     &lo->plh_flags)) {
2104 			spin_unlock(&ino->i_lock);
2105 			lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
2106 						NFS_LAYOUT_FIRST_LAYOUTGET,
2107 						TASK_KILLABLE));
2108 			if (IS_ERR(lseg))
2109 				goto out_put_layout_hdr;
2110 			pnfs_put_layout_hdr(lo);
2111 			dprintk("%s retrying\n", __func__);
2112 			goto lookup_again;
2113 		}
2114 
2115 		spin_unlock(&ino->i_lock);
2116 		first = true;
2117 		status = nfs4_select_rw_stateid(ctx->state,
2118 					iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
2119 					NULL, &stateid, NULL);
2120 		if (status != 0) {
2121 			lseg = ERR_PTR(status);
2122 			trace_pnfs_update_layout(ino, pos, count,
2123 					iomode, lo, lseg,
2124 					PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2125 			nfs4_schedule_stateid_recovery(server, ctx->state);
2126 			pnfs_clear_first_layoutget(lo);
2127 			pnfs_put_layout_hdr(lo);
2128 			goto lookup_again;
2129 		}
2130 		spin_lock(&ino->i_lock);
2131 	} else {
2132 		nfs4_stateid_copy(&stateid, &lo->plh_stateid);
2133 	}
2134 
2135 	if (pnfs_layoutgets_blocked(lo)) {
2136 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2137 				PNFS_UPDATE_LAYOUT_BLOCKED);
2138 		goto out_unlock;
2139 	}
2140 	nfs_layoutget_begin(lo);
2141 	spin_unlock(&ino->i_lock);
2142 
2143 	_add_to_server_list(lo, server);
2144 
2145 	pg_offset = arg.offset & ~PAGE_MASK;
2146 	if (pg_offset) {
2147 		arg.offset -= pg_offset;
2148 		arg.length += pg_offset;
2149 	}
2150 	if (arg.length != NFS4_MAX_UINT64)
2151 		arg.length = PAGE_ALIGN(arg.length);
2152 
2153 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
2154 	if (!lgp) {
2155 		lseg = ERR_PTR(-ENOMEM);
2156 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
2157 					 PNFS_UPDATE_LAYOUT_NOMEM);
2158 		nfs_layoutget_end(lo);
2159 		goto out_put_layout_hdr;
2160 	}
2161 
2162 	lgp->lo = lo;
2163 	pnfs_get_layout_hdr(lo);
2164 
2165 	lseg = nfs4_proc_layoutget(lgp, &timeout);
2166 	trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2167 				 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
2168 	nfs_layoutget_end(lo);
2169 	if (IS_ERR(lseg)) {
2170 		switch(PTR_ERR(lseg)) {
2171 		case -EBUSY:
2172 			if (time_after(jiffies, giveup))
2173 				lseg = NULL;
2174 			break;
2175 		case -ERECALLCONFLICT:
2176 		case -EAGAIN:
2177 			break;
2178 		case -ENODATA:
2179 			/* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
2180 			pnfs_layout_set_fail_bit(
2181 				lo, pnfs_iomode_to_fail_bit(iomode));
2182 			lseg = NULL;
2183 			goto out_put_layout_hdr;
2184 		default:
2185 			if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
2186 				pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2187 				lseg = NULL;
2188 			}
2189 			goto out_put_layout_hdr;
2190 		}
2191 		if (lseg) {
2192 			if (first)
2193 				pnfs_clear_first_layoutget(lo);
2194 			trace_pnfs_update_layout(ino, pos, count,
2195 				iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
2196 			pnfs_put_layout_hdr(lo);
2197 			goto lookup_again;
2198 		}
2199 	} else {
2200 		pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2201 	}
2202 
2203 out_put_layout_hdr:
2204 	if (first)
2205 		pnfs_clear_first_layoutget(lo);
2206 	trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2207 				 PNFS_UPDATE_LAYOUT_EXIT);
2208 	pnfs_put_layout_hdr(lo);
2209 out:
2210 	dprintk("%s: inode %s/%llu pNFS layout segment %s for "
2211 			"(%s, offset: %llu, length: %llu)\n",
2212 			__func__, ino->i_sb->s_id,
2213 			(unsigned long long)NFS_FILEID(ino),
2214 			IS_ERR_OR_NULL(lseg) ? "not found" : "found",
2215 			iomode==IOMODE_RW ?  "read/write" : "read-only",
2216 			(unsigned long long)pos,
2217 			(unsigned long long)count);
2218 	return lseg;
2219 out_unlock:
2220 	spin_unlock(&ino->i_lock);
2221 	goto out_put_layout_hdr;
2222 }
2223 EXPORT_SYMBOL_GPL(pnfs_update_layout);
2224 
2225 static bool
pnfs_sanity_check_layout_range(struct pnfs_layout_range * range)2226 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
2227 {
2228 	switch (range->iomode) {
2229 	case IOMODE_READ:
2230 	case IOMODE_RW:
2231 		break;
2232 	default:
2233 		return false;
2234 	}
2235 	if (range->offset == NFS4_MAX_UINT64)
2236 		return false;
2237 	if (range->length == 0)
2238 		return false;
2239 	if (range->length != NFS4_MAX_UINT64 &&
2240 	    range->length > NFS4_MAX_UINT64 - range->offset)
2241 		return false;
2242 	return true;
2243 }
2244 
2245 static struct pnfs_layout_hdr *
_pnfs_grab_empty_layout(struct inode * ino,struct nfs_open_context * ctx)2246 _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
2247 {
2248 	struct pnfs_layout_hdr *lo;
2249 
2250 	spin_lock(&ino->i_lock);
2251 	lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask());
2252 	if (!lo)
2253 		goto out_unlock;
2254 	if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
2255 		goto out_unlock;
2256 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
2257 		goto out_unlock;
2258 	if (pnfs_layoutgets_blocked(lo))
2259 		goto out_unlock;
2260 	if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
2261 		goto out_unlock;
2262 	nfs_layoutget_begin(lo);
2263 	spin_unlock(&ino->i_lock);
2264 	_add_to_server_list(lo, NFS_SERVER(ino));
2265 	return lo;
2266 
2267 out_unlock:
2268 	spin_unlock(&ino->i_lock);
2269 	pnfs_put_layout_hdr(lo);
2270 	return NULL;
2271 }
2272 
_lgopen_prepare_attached(struct nfs4_opendata * data,struct nfs_open_context * ctx)2273 static void _lgopen_prepare_attached(struct nfs4_opendata *data,
2274 				     struct nfs_open_context *ctx)
2275 {
2276 	struct inode *ino = data->dentry->d_inode;
2277 	struct pnfs_layout_range rng = {
2278 		.iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2279 			  IOMODE_RW: IOMODE_READ,
2280 		.offset = 0,
2281 		.length = NFS4_MAX_UINT64,
2282 	};
2283 	struct nfs4_layoutget *lgp;
2284 	struct pnfs_layout_hdr *lo;
2285 
2286 	/* Heuristic: don't send layoutget if we have cached data */
2287 	if (rng.iomode == IOMODE_READ &&
2288 	   (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
2289 		return;
2290 
2291 	lo = _pnfs_grab_empty_layout(ino, ctx);
2292 	if (!lo)
2293 		return;
2294 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
2295 					     nfs_io_gfp_mask());
2296 	if (!lgp) {
2297 		pnfs_clear_first_layoutget(lo);
2298 		nfs_layoutget_end(lo);
2299 		pnfs_put_layout_hdr(lo);
2300 		return;
2301 	}
2302 	lgp->lo = lo;
2303 	data->lgp = lgp;
2304 	data->o_arg.lg_args = &lgp->args;
2305 	data->o_res.lg_res = &lgp->res;
2306 }
2307 
_lgopen_prepare_floating(struct nfs4_opendata * data,struct nfs_open_context * ctx)2308 static void _lgopen_prepare_floating(struct nfs4_opendata *data,
2309 				     struct nfs_open_context *ctx)
2310 {
2311 	struct inode *ino = data->dentry->d_inode;
2312 	struct pnfs_layout_range rng = {
2313 		.iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2314 			  IOMODE_RW: IOMODE_READ,
2315 		.offset = 0,
2316 		.length = NFS4_MAX_UINT64,
2317 	};
2318 	struct nfs4_layoutget *lgp;
2319 
2320 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &current_stateid, &rng,
2321 					     nfs_io_gfp_mask());
2322 	if (!lgp)
2323 		return;
2324 	data->lgp = lgp;
2325 	data->o_arg.lg_args = &lgp->args;
2326 	data->o_res.lg_res = &lgp->res;
2327 }
2328 
pnfs_lgopen_prepare(struct nfs4_opendata * data,struct nfs_open_context * ctx)2329 void pnfs_lgopen_prepare(struct nfs4_opendata *data,
2330 			 struct nfs_open_context *ctx)
2331 {
2332 	struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
2333 
2334 	if (!(pnfs_enabled_sb(server) &&
2335 	      server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
2336 		return;
2337 	/* Could check on max_ops, but currently hardcoded high enough */
2338 	if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
2339 		return;
2340 	if (data->lgp)
2341 		return;
2342 	if (data->state)
2343 		_lgopen_prepare_attached(data, ctx);
2344 	else
2345 		_lgopen_prepare_floating(data, ctx);
2346 }
2347 
pnfs_parse_lgopen(struct inode * ino,struct nfs4_layoutget * lgp,struct nfs_open_context * ctx)2348 void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
2349 		       struct nfs_open_context *ctx)
2350 {
2351 	struct pnfs_layout_hdr *lo;
2352 	struct pnfs_layout_segment *lseg;
2353 	struct nfs_server *srv = NFS_SERVER(ino);
2354 	u32 iomode;
2355 
2356 	if (!lgp)
2357 		return;
2358 	dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
2359 	if (lgp->res.status) {
2360 		switch (lgp->res.status) {
2361 		default:
2362 			break;
2363 		/*
2364 		 * Halt lgopen attempts if the server doesn't recognise
2365 		 * the "current stateid" value, the layout type, or the
2366 		 * layoutget operation as being valid.
2367 		 * Also if it complains about too many ops in the compound
2368 		 * or of the request/reply being too big.
2369 		 */
2370 		case -NFS4ERR_BAD_STATEID:
2371 		case -NFS4ERR_NOTSUPP:
2372 		case -NFS4ERR_REP_TOO_BIG:
2373 		case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
2374 		case -NFS4ERR_REQ_TOO_BIG:
2375 		case -NFS4ERR_TOO_MANY_OPS:
2376 		case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2377 			srv->caps &= ~NFS_CAP_LGOPEN;
2378 		}
2379 		return;
2380 	}
2381 	if (!lgp->lo) {
2382 		lo = _pnfs_grab_empty_layout(ino, ctx);
2383 		if (!lo)
2384 			return;
2385 		lgp->lo = lo;
2386 	} else
2387 		lo = lgp->lo;
2388 
2389 	lseg = pnfs_layout_process(lgp);
2390 	if (!IS_ERR(lseg)) {
2391 		iomode = lgp->args.range.iomode;
2392 		pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2393 		pnfs_put_lseg(lseg);
2394 	}
2395 }
2396 
nfs4_lgopen_release(struct nfs4_layoutget * lgp)2397 void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
2398 {
2399 	if (lgp != NULL) {
2400 		if (lgp->lo) {
2401 			pnfs_clear_first_layoutget(lgp->lo);
2402 			nfs_layoutget_end(lgp->lo);
2403 		}
2404 		pnfs_layoutget_free(lgp);
2405 	}
2406 }
2407 
2408 struct pnfs_layout_segment *
pnfs_layout_process(struct nfs4_layoutget * lgp)2409 pnfs_layout_process(struct nfs4_layoutget *lgp)
2410 {
2411 	struct pnfs_layout_hdr *lo = lgp->lo;
2412 	struct nfs4_layoutget_res *res = &lgp->res;
2413 	struct pnfs_layout_segment *lseg;
2414 	struct inode *ino = lo->plh_inode;
2415 	LIST_HEAD(free_me);
2416 
2417 	if (!pnfs_sanity_check_layout_range(&res->range))
2418 		return ERR_PTR(-EINVAL);
2419 
2420 	/* Inject layout blob into I/O device driver */
2421 	lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
2422 	if (IS_ERR_OR_NULL(lseg)) {
2423 		if (!lseg)
2424 			lseg = ERR_PTR(-ENOMEM);
2425 
2426 		dprintk("%s: Could not allocate layout: error %ld\n",
2427 		       __func__, PTR_ERR(lseg));
2428 		return lseg;
2429 	}
2430 
2431 	pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
2432 
2433 	spin_lock(&ino->i_lock);
2434 	if (pnfs_layoutgets_blocked(lo)) {
2435 		dprintk("%s forget reply due to state\n", __func__);
2436 		goto out_forget;
2437 	}
2438 
2439 	if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2440 	    !pnfs_is_first_layoutget(lo))
2441 		goto out_forget;
2442 
2443 	if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
2444 		/* existing state ID, make sure the sequence number matches. */
2445 		if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
2446 			if (!pnfs_layout_is_valid(lo))
2447 				lo->plh_barrier = 0;
2448 			dprintk("%s forget reply due to sequence\n", __func__);
2449 			goto out_forget;
2450 		}
2451 		pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false);
2452 	} else if (pnfs_layout_is_valid(lo)) {
2453 		/*
2454 		 * We got an entirely new state ID.  Mark all segments for the
2455 		 * inode invalid, and retry the layoutget
2456 		 */
2457 		struct pnfs_layout_range range = {
2458 			.iomode = IOMODE_ANY,
2459 			.length = NFS4_MAX_UINT64,
2460 		};
2461 		pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0);
2462 		goto out_forget;
2463 	} else {
2464 		/* We have a completely new layout */
2465 		pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true);
2466 	}
2467 
2468 	pnfs_get_lseg(lseg);
2469 	pnfs_layout_insert_lseg(lo, lseg, &free_me);
2470 
2471 
2472 	if (res->return_on_close)
2473 		set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
2474 
2475 	spin_unlock(&ino->i_lock);
2476 	pnfs_free_lseg_list(&free_me);
2477 	return lseg;
2478 
2479 out_forget:
2480 	spin_unlock(&ino->i_lock);
2481 	lseg->pls_layout = lo;
2482 	NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
2483 	return ERR_PTR(-EAGAIN);
2484 }
2485 
2486 /**
2487  * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
2488  * @lo: pointer to layout header
2489  * @tmp_list: list header to be used with pnfs_free_lseg_list()
2490  * @return_range: describe layout segment ranges to be returned
2491  * @seq: stateid seqid to match
2492  *
2493  * This function is mainly intended for use by layoutrecall. It attempts
2494  * to free the layout segment immediately, or else to mark it for return
2495  * as soon as its reference count drops to zero.
2496  *
2497  * Returns
2498  * - 0: a layoutreturn needs to be scheduled.
2499  * - EBUSY: there are layout segment that are still in use.
2500  * - ENOENT: there are no layout segments that need to be returned.
2501  */
2502 int
pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr * lo,struct list_head * tmp_list,const struct pnfs_layout_range * return_range,u32 seq)2503 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2504 				struct list_head *tmp_list,
2505 				const struct pnfs_layout_range *return_range,
2506 				u32 seq)
2507 {
2508 	struct pnfs_layout_segment *lseg, *next;
2509 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
2510 	int remaining = 0;
2511 
2512 	dprintk("%s:Begin lo %p\n", __func__, lo);
2513 
2514 	assert_spin_locked(&lo->plh_inode->i_lock);
2515 
2516 	if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2517 		tmp_list = &lo->plh_return_segs;
2518 
2519 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2520 		if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2521 			dprintk("%s: marking lseg %p iomode %d "
2522 				"offset %llu length %llu\n", __func__,
2523 				lseg, lseg->pls_range.iomode,
2524 				lseg->pls_range.offset,
2525 				lseg->pls_range.length);
2526 			if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2527 				tmp_list = &lo->plh_return_segs;
2528 			if (mark_lseg_invalid(lseg, tmp_list))
2529 				continue;
2530 			remaining++;
2531 			set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2532 			pnfs_lseg_cancel_io(server, lseg);
2533 		}
2534 
2535 	if (remaining) {
2536 		pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2537 		return -EBUSY;
2538 	}
2539 
2540 	if (!list_empty(&lo->plh_return_segs)) {
2541 		pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2542 		return 0;
2543 	}
2544 
2545 	return -ENOENT;
2546 }
2547 
2548 static void
pnfs_mark_layout_for_return(struct inode * inode,const struct pnfs_layout_range * range)2549 pnfs_mark_layout_for_return(struct inode *inode,
2550 			    const struct pnfs_layout_range *range)
2551 {
2552 	struct pnfs_layout_hdr *lo;
2553 	bool return_now = false;
2554 
2555 	spin_lock(&inode->i_lock);
2556 	lo = NFS_I(inode)->layout;
2557 	if (!pnfs_layout_is_valid(lo)) {
2558 		spin_unlock(&inode->i_lock);
2559 		return;
2560 	}
2561 	pnfs_set_plh_return_info(lo, range->iomode, 0);
2562 	/*
2563 	 * mark all matching lsegs so that we are sure to have no live
2564 	 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2565 	 * for how it works.
2566 	 */
2567 	if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) {
2568 		const struct cred *cred;
2569 		nfs4_stateid stateid;
2570 		enum pnfs_iomode iomode;
2571 
2572 		return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
2573 		spin_unlock(&inode->i_lock);
2574 		if (return_now)
2575 			pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
2576 	} else {
2577 		spin_unlock(&inode->i_lock);
2578 		nfs_commit_inode(inode, 0);
2579 	}
2580 }
2581 
pnfs_error_mark_layout_for_return(struct inode * inode,struct pnfs_layout_segment * lseg)2582 void pnfs_error_mark_layout_for_return(struct inode *inode,
2583 				       struct pnfs_layout_segment *lseg)
2584 {
2585 	struct pnfs_layout_range range = {
2586 		.iomode = lseg->pls_range.iomode,
2587 		.offset = 0,
2588 		.length = NFS4_MAX_UINT64,
2589 	};
2590 
2591 	pnfs_mark_layout_for_return(inode, &range);
2592 }
2593 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2594 
2595 static bool
pnfs_layout_can_be_returned(struct pnfs_layout_hdr * lo)2596 pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo)
2597 {
2598 	return pnfs_layout_is_valid(lo) &&
2599 		!test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) &&
2600 		!test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
2601 }
2602 
2603 static struct pnfs_layout_segment *
pnfs_find_first_lseg(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range,enum pnfs_iomode iomode)2604 pnfs_find_first_lseg(struct pnfs_layout_hdr *lo,
2605 		     const struct pnfs_layout_range *range,
2606 		     enum pnfs_iomode iomode)
2607 {
2608 	struct pnfs_layout_segment *lseg;
2609 
2610 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
2611 		if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
2612 			continue;
2613 		if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2614 			continue;
2615 		if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY)
2616 			continue;
2617 		if (pnfs_lseg_range_intersecting(&lseg->pls_range, range))
2618 			return lseg;
2619 	}
2620 	return NULL;
2621 }
2622 
2623 /* Find open file states whose mode matches that of the range */
2624 static bool
pnfs_should_return_unused_layout(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range)2625 pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo,
2626 				 const struct pnfs_layout_range *range)
2627 {
2628 	struct list_head *head;
2629 	struct nfs_open_context *ctx;
2630 	fmode_t mode = 0;
2631 
2632 	if (!pnfs_layout_can_be_returned(lo) ||
2633 	    !pnfs_find_first_lseg(lo, range, range->iomode))
2634 		return false;
2635 
2636 	head = &NFS_I(lo->plh_inode)->open_files;
2637 	list_for_each_entry_rcu(ctx, head, list) {
2638 		if (ctx->state)
2639 			mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE);
2640 	}
2641 
2642 	switch (range->iomode) {
2643 	default:
2644 		break;
2645 	case IOMODE_READ:
2646 		mode &= ~FMODE_WRITE;
2647 		break;
2648 	case IOMODE_RW:
2649 		if (pnfs_find_first_lseg(lo, range, IOMODE_READ))
2650 			mode &= ~FMODE_READ;
2651 	}
2652 	return mode == 0;
2653 }
2654 
pnfs_layout_return_unused_byserver(struct nfs_server * server,void * data)2655 static int pnfs_layout_return_unused_byserver(struct nfs_server *server,
2656 					      void *data)
2657 {
2658 	const struct pnfs_layout_range *range = data;
2659 	const struct cred *cred;
2660 	struct pnfs_layout_hdr *lo;
2661 	struct inode *inode;
2662 	nfs4_stateid stateid;
2663 	enum pnfs_iomode iomode;
2664 
2665 restart:
2666 	rcu_read_lock();
2667 	list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
2668 		inode = lo->plh_inode;
2669 		if (!inode || !pnfs_layout_can_be_returned(lo) ||
2670 		    test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2671 			continue;
2672 		spin_lock(&inode->i_lock);
2673 		if (!lo->plh_inode ||
2674 		    !pnfs_should_return_unused_layout(lo, range)) {
2675 			spin_unlock(&inode->i_lock);
2676 			continue;
2677 		}
2678 		pnfs_get_layout_hdr(lo);
2679 		pnfs_set_plh_return_info(lo, range->iomode, 0);
2680 		if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
2681 						    range, 0) != 0 ||
2682 		    !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) {
2683 			spin_unlock(&inode->i_lock);
2684 			rcu_read_unlock();
2685 			pnfs_put_layout_hdr(lo);
2686 			cond_resched();
2687 			goto restart;
2688 		}
2689 		spin_unlock(&inode->i_lock);
2690 		rcu_read_unlock();
2691 		pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
2692 		pnfs_put_layout_hdr(lo);
2693 		cond_resched();
2694 		goto restart;
2695 	}
2696 	rcu_read_unlock();
2697 	return 0;
2698 }
2699 
2700 void
pnfs_layout_return_unused_byclid(struct nfs_client * clp,enum pnfs_iomode iomode)2701 pnfs_layout_return_unused_byclid(struct nfs_client *clp,
2702 				 enum pnfs_iomode iomode)
2703 {
2704 	struct pnfs_layout_range range = {
2705 		.iomode = iomode,
2706 		.offset = 0,
2707 		.length = NFS4_MAX_UINT64,
2708 	};
2709 
2710 	nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver,
2711 			&range);
2712 }
2713 
2714 void
pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor * pgio)2715 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
2716 {
2717 	if (pgio->pg_lseg == NULL ||
2718 	    test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
2719 		return;
2720 	pnfs_put_lseg(pgio->pg_lseg);
2721 	pgio->pg_lseg = NULL;
2722 }
2723 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2724 
2725 /*
2726  * Check for any intersection between the request and the pgio->pg_lseg,
2727  * and if none, put this pgio->pg_lseg away.
2728  */
2729 void
pnfs_generic_pg_check_range(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2730 pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2731 {
2732 	if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
2733 		pnfs_put_lseg(pgio->pg_lseg);
2734 		pgio->pg_lseg = NULL;
2735 	}
2736 }
2737 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_range);
2738 
2739 void
pnfs_generic_pg_init_read(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2740 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2741 {
2742 	u64 rd_size;
2743 
2744 	pnfs_generic_pg_check_layout(pgio);
2745 	pnfs_generic_pg_check_range(pgio, req);
2746 	if (pgio->pg_lseg == NULL) {
2747 		if (pgio->pg_dreq == NULL)
2748 			rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2749 		else
2750 			rd_size = nfs_dreq_bytes_left(pgio->pg_dreq,
2751 						      req_offset(req));
2752 
2753 		pgio->pg_lseg =
2754 			pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2755 					   req_offset(req), rd_size,
2756 					   IOMODE_READ, false,
2757 					   nfs_io_gfp_mask());
2758 		if (IS_ERR(pgio->pg_lseg)) {
2759 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2760 			pgio->pg_lseg = NULL;
2761 			return;
2762 		}
2763 	}
2764 	/* If no lseg, fall back to read through mds */
2765 	if (pgio->pg_lseg == NULL)
2766 		nfs_pageio_reset_read_mds(pgio);
2767 
2768 }
2769 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2770 
2771 void
pnfs_generic_pg_init_write(struct nfs_pageio_descriptor * pgio,struct nfs_page * req,u64 wb_size)2772 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2773 			   struct nfs_page *req, u64 wb_size)
2774 {
2775 	pnfs_generic_pg_check_layout(pgio);
2776 	pnfs_generic_pg_check_range(pgio, req);
2777 	if (pgio->pg_lseg == NULL) {
2778 		pgio->pg_lseg =
2779 			pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2780 					   req_offset(req), wb_size, IOMODE_RW,
2781 					   false, nfs_io_gfp_mask());
2782 		if (IS_ERR(pgio->pg_lseg)) {
2783 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2784 			pgio->pg_lseg = NULL;
2785 			return;
2786 		}
2787 	}
2788 	/* If no lseg, fall back to write through mds */
2789 	if (pgio->pg_lseg == NULL)
2790 		nfs_pageio_reset_write_mds(pgio);
2791 }
2792 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2793 
2794 void
pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor * desc)2795 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2796 {
2797 	if (desc->pg_lseg) {
2798 		pnfs_put_lseg(desc->pg_lseg);
2799 		desc->pg_lseg = NULL;
2800 	}
2801 }
2802 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2803 
2804 /*
2805  * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2806  * of bytes (maximum @req->wb_bytes) that can be coalesced.
2807  */
2808 size_t
pnfs_generic_pg_test(struct nfs_pageio_descriptor * pgio,struct nfs_page * prev,struct nfs_page * req)2809 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2810 		     struct nfs_page *prev, struct nfs_page *req)
2811 {
2812 	unsigned int size;
2813 	u64 seg_end, req_start, seg_left;
2814 
2815 	size = nfs_generic_pg_test(pgio, prev, req);
2816 	if (!size)
2817 		return 0;
2818 
2819 	/*
2820 	 * 'size' contains the number of bytes left in the current page (up
2821 	 * to the original size asked for in @req->wb_bytes).
2822 	 *
2823 	 * Calculate how many bytes are left in the layout segment
2824 	 * and if there are less bytes than 'size', return that instead.
2825 	 *
2826 	 * Please also note that 'end_offset' is actually the offset of the
2827 	 * first byte that lies outside the pnfs_layout_range. FIXME?
2828 	 *
2829 	 */
2830 	if (pgio->pg_lseg) {
2831 		seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2832 				     pgio->pg_lseg->pls_range.length);
2833 		req_start = req_offset(req);
2834 
2835 		/* start of request is past the last byte of this segment */
2836 		if (req_start >= seg_end)
2837 			return 0;
2838 
2839 		/* adjust 'size' iff there are fewer bytes left in the
2840 		 * segment than what nfs_generic_pg_test returned */
2841 		seg_left = seg_end - req_start;
2842 		if (seg_left < size)
2843 			size = (unsigned int)seg_left;
2844 	}
2845 
2846 	return size;
2847 }
2848 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2849 
pnfs_write_done_resend_to_mds(struct nfs_pgio_header * hdr)2850 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2851 {
2852 	struct nfs_pageio_descriptor pgio;
2853 
2854 	/* Resend all requests through the MDS */
2855 	nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2856 			      hdr->completion_ops);
2857 	return nfs_pageio_resend(&pgio, hdr);
2858 }
2859 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2860 
pnfs_ld_handle_write_error(struct nfs_pgio_header * hdr)2861 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2862 {
2863 
2864 	dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2865 	if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2866 	    PNFS_LAYOUTRET_ON_ERROR) {
2867 		pnfs_return_layout(hdr->inode);
2868 	}
2869 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2870 		hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2871 }
2872 
2873 /*
2874  * Called by non rpc-based layout drivers
2875  */
pnfs_ld_write_done(struct nfs_pgio_header * hdr)2876 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2877 {
2878 	if (likely(!hdr->pnfs_error)) {
2879 		pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2880 				hdr->mds_offset + hdr->res.count);
2881 		hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2882 	}
2883 	trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2884 	if (unlikely(hdr->pnfs_error))
2885 		pnfs_ld_handle_write_error(hdr);
2886 	hdr->mds_ops->rpc_release(hdr);
2887 }
2888 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2889 
2890 static void
pnfs_write_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)2891 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2892 		struct nfs_pgio_header *hdr)
2893 {
2894 	struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2895 
2896 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2897 		list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2898 		nfs_pageio_reset_write_mds(desc);
2899 		mirror->pg_recoalesce = 1;
2900 	}
2901 	hdr->completion_ops->completion(hdr);
2902 }
2903 
2904 static enum pnfs_try_status
pnfs_try_to_write_data(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops,struct pnfs_layout_segment * lseg,int how)2905 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2906 			const struct rpc_call_ops *call_ops,
2907 			struct pnfs_layout_segment *lseg,
2908 			int how)
2909 {
2910 	struct inode *inode = hdr->inode;
2911 	enum pnfs_try_status trypnfs;
2912 	struct nfs_server *nfss = NFS_SERVER(inode);
2913 
2914 	hdr->mds_ops = call_ops;
2915 
2916 	dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2917 		inode->i_ino, hdr->args.count, hdr->args.offset, how);
2918 	trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2919 	if (trypnfs != PNFS_NOT_ATTEMPTED)
2920 		nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2921 	dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2922 	return trypnfs;
2923 }
2924 
2925 static void
pnfs_do_write(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr,int how)2926 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2927 	      struct nfs_pgio_header *hdr, int how)
2928 {
2929 	const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2930 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
2931 	enum pnfs_try_status trypnfs;
2932 
2933 	trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2934 	switch (trypnfs) {
2935 	case PNFS_NOT_ATTEMPTED:
2936 		pnfs_write_through_mds(desc, hdr);
2937 		break;
2938 	case PNFS_ATTEMPTED:
2939 		break;
2940 	case PNFS_TRY_AGAIN:
2941 		/* cleanup hdr and prepare to redo pnfs */
2942 		if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2943 			struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2944 			list_splice_init(&hdr->pages, &mirror->pg_list);
2945 			mirror->pg_recoalesce = 1;
2946 		}
2947 		hdr->mds_ops->rpc_release(hdr);
2948 	}
2949 }
2950 
pnfs_writehdr_free(struct nfs_pgio_header * hdr)2951 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2952 {
2953 	pnfs_put_lseg(hdr->lseg);
2954 	nfs_pgio_header_free(hdr);
2955 }
2956 
2957 int
pnfs_generic_pg_writepages(struct nfs_pageio_descriptor * desc)2958 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2959 {
2960 	struct nfs_pgio_header *hdr;
2961 	int ret;
2962 
2963 	hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2964 	if (!hdr) {
2965 		desc->pg_error = -ENOMEM;
2966 		return desc->pg_error;
2967 	}
2968 	nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2969 
2970 	hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2971 	ret = nfs_generic_pgio(desc, hdr);
2972 	if (!ret)
2973 		pnfs_do_write(desc, hdr, desc->pg_ioflags);
2974 
2975 	return ret;
2976 }
2977 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2978 
pnfs_read_done_resend_to_mds(struct nfs_pgio_header * hdr)2979 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2980 {
2981 	struct nfs_pageio_descriptor pgio;
2982 
2983 	/* Resend all requests through the MDS */
2984 	nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2985 	return nfs_pageio_resend(&pgio, hdr);
2986 }
2987 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2988 
pnfs_ld_handle_read_error(struct nfs_pgio_header * hdr)2989 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2990 {
2991 	dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2992 	if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2993 	    PNFS_LAYOUTRET_ON_ERROR) {
2994 		pnfs_return_layout(hdr->inode);
2995 	}
2996 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2997 		hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2998 }
2999 
3000 /*
3001  * Called by non rpc-based layout drivers
3002  */
pnfs_ld_read_done(struct nfs_pgio_header * hdr)3003 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
3004 {
3005 	if (likely(!hdr->pnfs_error))
3006 		hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
3007 	trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
3008 	if (unlikely(hdr->pnfs_error))
3009 		pnfs_ld_handle_read_error(hdr);
3010 	hdr->mds_ops->rpc_release(hdr);
3011 }
3012 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
3013 
3014 static void
pnfs_read_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3015 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
3016 		struct nfs_pgio_header *hdr)
3017 {
3018 	struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3019 
3020 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3021 		list_splice_tail_init(&hdr->pages, &mirror->pg_list);
3022 		nfs_pageio_reset_read_mds(desc);
3023 		mirror->pg_recoalesce = 1;
3024 	}
3025 	hdr->completion_ops->completion(hdr);
3026 }
3027 
3028 /*
3029  * Call the appropriate parallel I/O subsystem read function.
3030  */
3031 static enum pnfs_try_status
pnfs_try_to_read_data(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops,struct pnfs_layout_segment * lseg)3032 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
3033 		       const struct rpc_call_ops *call_ops,
3034 		       struct pnfs_layout_segment *lseg)
3035 {
3036 	struct inode *inode = hdr->inode;
3037 	struct nfs_server *nfss = NFS_SERVER(inode);
3038 	enum pnfs_try_status trypnfs;
3039 
3040 	hdr->mds_ops = call_ops;
3041 
3042 	dprintk("%s: Reading ino:%lu %u@%llu\n",
3043 		__func__, inode->i_ino, hdr->args.count, hdr->args.offset);
3044 
3045 	trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
3046 	if (trypnfs != PNFS_NOT_ATTEMPTED)
3047 		nfs_inc_stats(inode, NFSIOS_PNFS_READ);
3048 	dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
3049 	return trypnfs;
3050 }
3051 
3052 /* Resend all requests through pnfs. */
pnfs_read_resend_pnfs(struct nfs_pgio_header * hdr,unsigned int mirror_idx)3053 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr,
3054 			   unsigned int mirror_idx)
3055 {
3056 	struct nfs_pageio_descriptor pgio;
3057 
3058 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3059 		/* Prevent deadlocks with layoutreturn! */
3060 		pnfs_put_lseg(hdr->lseg);
3061 		hdr->lseg = NULL;
3062 
3063 		nfs_pageio_init_read(&pgio, hdr->inode, false,
3064 					hdr->completion_ops);
3065 		pgio.pg_mirror_idx = mirror_idx;
3066 		hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
3067 	}
3068 }
3069 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
3070 
3071 static void
pnfs_do_read(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3072 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
3073 {
3074 	const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
3075 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
3076 	enum pnfs_try_status trypnfs;
3077 
3078 	trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
3079 	switch (trypnfs) {
3080 	case PNFS_NOT_ATTEMPTED:
3081 		pnfs_read_through_mds(desc, hdr);
3082 		break;
3083 	case PNFS_ATTEMPTED:
3084 		break;
3085 	case PNFS_TRY_AGAIN:
3086 		/* cleanup hdr and prepare to redo pnfs */
3087 		if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3088 			struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3089 			list_splice_init(&hdr->pages, &mirror->pg_list);
3090 			mirror->pg_recoalesce = 1;
3091 		}
3092 		hdr->mds_ops->rpc_release(hdr);
3093 	}
3094 }
3095 
pnfs_readhdr_free(struct nfs_pgio_header * hdr)3096 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
3097 {
3098 	pnfs_put_lseg(hdr->lseg);
3099 	nfs_pgio_header_free(hdr);
3100 }
3101 
3102 int
pnfs_generic_pg_readpages(struct nfs_pageio_descriptor * desc)3103 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
3104 {
3105 	struct nfs_pgio_header *hdr;
3106 	int ret;
3107 
3108 	hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
3109 	if (!hdr) {
3110 		desc->pg_error = -ENOMEM;
3111 		return desc->pg_error;
3112 	}
3113 	nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
3114 	hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
3115 	ret = nfs_generic_pgio(desc, hdr);
3116 	if (!ret)
3117 		pnfs_do_read(desc, hdr);
3118 	return ret;
3119 }
3120 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
3121 
pnfs_clear_layoutcommitting(struct inode * inode)3122 static void pnfs_clear_layoutcommitting(struct inode *inode)
3123 {
3124 	unsigned long *bitlock = &NFS_I(inode)->flags;
3125 
3126 	clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
3127 	smp_mb__after_atomic();
3128 	wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
3129 }
3130 
3131 /*
3132  * There can be multiple RW segments.
3133  */
pnfs_list_write_lseg(struct inode * inode,struct list_head * listp)3134 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
3135 {
3136 	struct pnfs_layout_segment *lseg;
3137 
3138 	list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
3139 		if (lseg->pls_range.iomode == IOMODE_RW &&
3140 		    test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
3141 			list_add(&lseg->pls_lc_list, listp);
3142 	}
3143 }
3144 
pnfs_list_write_lseg_done(struct inode * inode,struct list_head * listp)3145 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
3146 {
3147 	struct pnfs_layout_segment *lseg, *tmp;
3148 
3149 	/* Matched by references in pnfs_set_layoutcommit */
3150 	list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
3151 		list_del_init(&lseg->pls_lc_list);
3152 		pnfs_put_lseg(lseg);
3153 	}
3154 
3155 	pnfs_clear_layoutcommitting(inode);
3156 }
3157 
pnfs_set_lo_fail(struct pnfs_layout_segment * lseg)3158 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
3159 {
3160 	pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
3161 }
3162 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
3163 
3164 void
pnfs_set_layoutcommit(struct inode * inode,struct pnfs_layout_segment * lseg,loff_t end_pos)3165 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
3166 		loff_t end_pos)
3167 {
3168 	struct nfs_inode *nfsi = NFS_I(inode);
3169 	bool mark_as_dirty = false;
3170 
3171 	spin_lock(&inode->i_lock);
3172 	if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
3173 		nfsi->layout->plh_lwb = end_pos;
3174 		mark_as_dirty = true;
3175 		dprintk("%s: Set layoutcommit for inode %lu ",
3176 			__func__, inode->i_ino);
3177 	} else if (end_pos > nfsi->layout->plh_lwb)
3178 		nfsi->layout->plh_lwb = end_pos;
3179 	if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
3180 		/* references matched in nfs4_layoutcommit_release */
3181 		pnfs_get_lseg(lseg);
3182 	}
3183 	spin_unlock(&inode->i_lock);
3184 	dprintk("%s: lseg %p end_pos %llu\n",
3185 		__func__, lseg, nfsi->layout->plh_lwb);
3186 
3187 	/* if pnfs_layoutcommit_inode() runs between inode locks, the next one
3188 	 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
3189 	if (mark_as_dirty)
3190 		mark_inode_dirty_sync(inode);
3191 }
3192 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
3193 
pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data * data)3194 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
3195 {
3196 	struct nfs_server *nfss = NFS_SERVER(data->args.inode);
3197 
3198 	if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
3199 		nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
3200 	pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
3201 }
3202 
3203 /*
3204  * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
3205  * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
3206  * data to disk to allow the server to recover the data if it crashes.
3207  * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
3208  * is off, and a COMMIT is sent to a data server, or
3209  * if WRITEs to a data server return NFS_DATA_SYNC.
3210  */
3211 int
pnfs_layoutcommit_inode(struct inode * inode,bool sync)3212 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
3213 {
3214 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3215 	struct nfs4_layoutcommit_data *data;
3216 	struct nfs_inode *nfsi = NFS_I(inode);
3217 	loff_t end_pos;
3218 	int status;
3219 
3220 	if (!pnfs_layoutcommit_outstanding(inode))
3221 		return 0;
3222 
3223 	dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
3224 
3225 	status = -EAGAIN;
3226 	if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
3227 		if (!sync)
3228 			goto out;
3229 		status = wait_on_bit_lock_action(&nfsi->flags,
3230 				NFS_INO_LAYOUTCOMMITTING,
3231 				nfs_wait_bit_killable,
3232 				TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
3233 		if (status)
3234 			goto out;
3235 	}
3236 
3237 	status = -ENOMEM;
3238 	/* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
3239 	data = kzalloc(sizeof(*data), nfs_io_gfp_mask());
3240 	if (!data)
3241 		goto clear_layoutcommitting;
3242 
3243 	status = 0;
3244 	spin_lock(&inode->i_lock);
3245 	if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
3246 		goto out_unlock;
3247 
3248 	INIT_LIST_HEAD(&data->lseg_list);
3249 	pnfs_list_write_lseg(inode, &data->lseg_list);
3250 
3251 	end_pos = nfsi->layout->plh_lwb;
3252 
3253 	nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
3254 	data->cred = get_cred(nfsi->layout->plh_lc_cred);
3255 	spin_unlock(&inode->i_lock);
3256 
3257 	data->args.inode = inode;
3258 	nfs_fattr_init(&data->fattr);
3259 	data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3260 	data->res.fattr = &data->fattr;
3261 	if (end_pos != 0)
3262 		data->args.lastbytewritten = end_pos - 1;
3263 	else
3264 		data->args.lastbytewritten = U64_MAX;
3265 	data->res.server = NFS_SERVER(inode);
3266 
3267 	if (ld->prepare_layoutcommit) {
3268 		status = ld->prepare_layoutcommit(&data->args);
3269 		if (status) {
3270 			put_cred(data->cred);
3271 			spin_lock(&inode->i_lock);
3272 			set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
3273 			if (end_pos > nfsi->layout->plh_lwb)
3274 				nfsi->layout->plh_lwb = end_pos;
3275 			goto out_unlock;
3276 		}
3277 	}
3278 
3279 
3280 	status = nfs4_proc_layoutcommit(data, sync);
3281 out:
3282 	if (status)
3283 		mark_inode_dirty_sync(inode);
3284 	dprintk("<-- %s status %d\n", __func__, status);
3285 	return status;
3286 out_unlock:
3287 	spin_unlock(&inode->i_lock);
3288 	kfree(data);
3289 clear_layoutcommitting:
3290 	pnfs_clear_layoutcommitting(inode);
3291 	goto out;
3292 }
3293 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
3294 
3295 int
pnfs_generic_sync(struct inode * inode,bool datasync)3296 pnfs_generic_sync(struct inode *inode, bool datasync)
3297 {
3298 	return pnfs_layoutcommit_inode(inode, true);
3299 }
3300 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
3301 
pnfs_mdsthreshold_alloc(void)3302 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
3303 {
3304 	struct nfs4_threshold *thp;
3305 
3306 	thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask());
3307 	if (!thp) {
3308 		dprintk("%s mdsthreshold allocation failed\n", __func__);
3309 		return NULL;
3310 	}
3311 	return thp;
3312 }
3313 
3314 #if IS_ENABLED(CONFIG_NFS_V4_2)
3315 int
pnfs_report_layoutstat(struct inode * inode,gfp_t gfp_flags)3316 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
3317 {
3318 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3319 	struct nfs_server *server = NFS_SERVER(inode);
3320 	struct nfs_inode *nfsi = NFS_I(inode);
3321 	struct nfs42_layoutstat_data *data;
3322 	struct pnfs_layout_hdr *hdr;
3323 	int status = 0;
3324 
3325 	if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
3326 		goto out;
3327 
3328 	if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
3329 		goto out;
3330 
3331 	if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
3332 		goto out;
3333 
3334 	spin_lock(&inode->i_lock);
3335 	if (!NFS_I(inode)->layout) {
3336 		spin_unlock(&inode->i_lock);
3337 		goto out_clear_layoutstats;
3338 	}
3339 	hdr = NFS_I(inode)->layout;
3340 	pnfs_get_layout_hdr(hdr);
3341 	spin_unlock(&inode->i_lock);
3342 
3343 	data = kzalloc(sizeof(*data), gfp_flags);
3344 	if (!data) {
3345 		status = -ENOMEM;
3346 		goto out_put;
3347 	}
3348 
3349 	data->args.fh = NFS_FH(inode);
3350 	data->args.inode = inode;
3351 	status = ld->prepare_layoutstats(&data->args);
3352 	if (status)
3353 		goto out_free;
3354 
3355 	status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
3356 
3357 out:
3358 	dprintk("%s returns %d\n", __func__, status);
3359 	return status;
3360 
3361 out_free:
3362 	kfree(data);
3363 out_put:
3364 	pnfs_put_layout_hdr(hdr);
3365 out_clear_layoutstats:
3366 	smp_mb__before_atomic();
3367 	clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
3368 	smp_mb__after_atomic();
3369 	goto out;
3370 }
3371 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
3372 #endif
3373 
3374 unsigned int layoutstats_timer;
3375 module_param(layoutstats_timer, uint, 0644);
3376 EXPORT_SYMBOL_GPL(layoutstats_timer);
3377