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 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN);
1935 }
1936
pnfs_is_first_layoutget(struct pnfs_layout_hdr * lo)1937 static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo)
1938 {
1939 return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags);
1940 }
1941
pnfs_clear_first_layoutget(struct pnfs_layout_hdr * lo)1942 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1943 {
1944 unsigned long *bitlock = &lo->plh_flags;
1945
1946 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1947 smp_mb__after_atomic();
1948 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1949 }
1950
_add_to_server_list(struct pnfs_layout_hdr * lo,struct nfs_server * server)1951 static void _add_to_server_list(struct pnfs_layout_hdr *lo,
1952 struct nfs_server *server)
1953 {
1954 if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
1955 struct nfs_client *clp = server->nfs_client;
1956
1957 /* The lo must be on the clp list if there is any
1958 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1959 */
1960 spin_lock(&clp->cl_lock);
1961 list_add_tail_rcu(&lo->plh_layouts, &server->layouts);
1962 spin_unlock(&clp->cl_lock);
1963 }
1964 }
1965
1966 /*
1967 * Layout segment is retreived from the server if not cached.
1968 * The appropriate layout segment is referenced and returned to the caller.
1969 */
1970 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)1971 pnfs_update_layout(struct inode *ino,
1972 struct nfs_open_context *ctx,
1973 loff_t pos,
1974 u64 count,
1975 enum pnfs_iomode iomode,
1976 bool strict_iomode,
1977 gfp_t gfp_flags)
1978 {
1979 struct pnfs_layout_range arg = {
1980 .iomode = iomode,
1981 .offset = pos,
1982 .length = count,
1983 };
1984 unsigned pg_offset;
1985 struct nfs_server *server = NFS_SERVER(ino);
1986 struct nfs_client *clp = server->nfs_client;
1987 struct pnfs_layout_hdr *lo = NULL;
1988 struct pnfs_layout_segment *lseg = NULL;
1989 struct nfs4_layoutget *lgp;
1990 nfs4_stateid stateid;
1991 long timeout = 0;
1992 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1993 bool first;
1994
1995 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1996 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1997 PNFS_UPDATE_LAYOUT_NO_PNFS);
1998 goto out;
1999 }
2000
2001 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
2002 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2003 PNFS_UPDATE_LAYOUT_MDSTHRESH);
2004 goto out;
2005 }
2006
2007 lookup_again:
2008 if (!nfs4_valid_open_stateid(ctx->state)) {
2009 trace_pnfs_update_layout(ino, pos, count,
2010 iomode, lo, lseg,
2011 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2012 lseg = ERR_PTR(-EIO);
2013 goto out;
2014 }
2015
2016 lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
2017 if (IS_ERR(lseg))
2018 goto out;
2019 first = false;
2020 spin_lock(&ino->i_lock);
2021 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
2022 if (lo == NULL) {
2023 spin_unlock(&ino->i_lock);
2024 lseg = ERR_PTR(-ENOMEM);
2025 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2026 PNFS_UPDATE_LAYOUT_NOMEM);
2027 goto out;
2028 }
2029
2030 /* Do we even need to bother with this? */
2031 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
2032 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2033 PNFS_UPDATE_LAYOUT_BULK_RECALL);
2034 dprintk("%s matches recall, use MDS\n", __func__);
2035 goto out_unlock;
2036 }
2037
2038 /* if LAYOUTGET already failed once we don't try again */
2039 if (pnfs_layout_io_test_failed(lo, iomode)) {
2040 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2041 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
2042 goto out_unlock;
2043 }
2044
2045 /*
2046 * If the layout segment list is empty, but there are outstanding
2047 * layoutget calls, then they might be subject to a layoutrecall.
2048 */
2049 if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2050 atomic_read(&lo->plh_outstanding) != 0) {
2051 spin_unlock(&ino->i_lock);
2052 lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN,
2053 TASK_KILLABLE));
2054 if (IS_ERR(lseg))
2055 goto out_put_layout_hdr;
2056 pnfs_put_layout_hdr(lo);
2057 goto lookup_again;
2058 }
2059
2060 /*
2061 * Because we free lsegs when sending LAYOUTRETURN, we need to wait
2062 * for LAYOUTRETURN.
2063 */
2064 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
2065 spin_unlock(&ino->i_lock);
2066 dprintk("%s wait for layoutreturn\n", __func__);
2067 lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
2068 if (!IS_ERR(lseg)) {
2069 pnfs_put_layout_hdr(lo);
2070 dprintk("%s retrying\n", __func__);
2071 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
2072 lseg,
2073 PNFS_UPDATE_LAYOUT_RETRY);
2074 goto lookup_again;
2075 }
2076 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2077 PNFS_UPDATE_LAYOUT_RETURN);
2078 goto out_put_layout_hdr;
2079 }
2080
2081 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
2082 if (lseg) {
2083 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2084 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
2085 goto out_unlock;
2086 }
2087
2088 /*
2089 * Choose a stateid for the LAYOUTGET. If we don't have a layout
2090 * stateid, or it has been invalidated, then we must use the open
2091 * stateid.
2092 */
2093 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
2094 int status;
2095
2096 /*
2097 * The first layoutget for the file. Need to serialize per
2098 * RFC 5661 Errata 3208.
2099 */
2100 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
2101 &lo->plh_flags)) {
2102 spin_unlock(&ino->i_lock);
2103 lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
2104 NFS_LAYOUT_FIRST_LAYOUTGET,
2105 TASK_KILLABLE));
2106 if (IS_ERR(lseg))
2107 goto out_put_layout_hdr;
2108 pnfs_put_layout_hdr(lo);
2109 dprintk("%s retrying\n", __func__);
2110 goto lookup_again;
2111 }
2112
2113 spin_unlock(&ino->i_lock);
2114 first = true;
2115 status = nfs4_select_rw_stateid(ctx->state,
2116 iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
2117 NULL, &stateid, NULL);
2118 if (status != 0) {
2119 lseg = ERR_PTR(status);
2120 trace_pnfs_update_layout(ino, pos, count,
2121 iomode, lo, lseg,
2122 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2123 nfs4_schedule_stateid_recovery(server, ctx->state);
2124 pnfs_clear_first_layoutget(lo);
2125 pnfs_put_layout_hdr(lo);
2126 goto lookup_again;
2127 }
2128 spin_lock(&ino->i_lock);
2129 } else {
2130 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
2131 }
2132
2133 if (pnfs_layoutgets_blocked(lo)) {
2134 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2135 PNFS_UPDATE_LAYOUT_BLOCKED);
2136 goto out_unlock;
2137 }
2138 nfs_layoutget_begin(lo);
2139 spin_unlock(&ino->i_lock);
2140
2141 _add_to_server_list(lo, server);
2142
2143 pg_offset = arg.offset & ~PAGE_MASK;
2144 if (pg_offset) {
2145 arg.offset -= pg_offset;
2146 arg.length += pg_offset;
2147 }
2148 if (arg.length != NFS4_MAX_UINT64)
2149 arg.length = PAGE_ALIGN(arg.length);
2150
2151 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
2152 if (!lgp) {
2153 lseg = ERR_PTR(-ENOMEM);
2154 trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
2155 PNFS_UPDATE_LAYOUT_NOMEM);
2156 nfs_layoutget_end(lo);
2157 goto out_put_layout_hdr;
2158 }
2159
2160 lgp->lo = lo;
2161 pnfs_get_layout_hdr(lo);
2162
2163 lseg = nfs4_proc_layoutget(lgp, &timeout);
2164 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2165 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
2166 nfs_layoutget_end(lo);
2167 if (IS_ERR(lseg)) {
2168 switch(PTR_ERR(lseg)) {
2169 case -EBUSY:
2170 if (time_after(jiffies, giveup))
2171 lseg = NULL;
2172 break;
2173 case -ERECALLCONFLICT:
2174 case -EAGAIN:
2175 break;
2176 case -ENODATA:
2177 /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
2178 pnfs_layout_set_fail_bit(
2179 lo, pnfs_iomode_to_fail_bit(iomode));
2180 lseg = NULL;
2181 goto out_put_layout_hdr;
2182 default:
2183 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
2184 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2185 lseg = NULL;
2186 }
2187 goto out_put_layout_hdr;
2188 }
2189 if (lseg) {
2190 if (first)
2191 pnfs_clear_first_layoutget(lo);
2192 trace_pnfs_update_layout(ino, pos, count,
2193 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
2194 pnfs_put_layout_hdr(lo);
2195 goto lookup_again;
2196 }
2197 } else {
2198 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2199 }
2200
2201 out_put_layout_hdr:
2202 if (first)
2203 pnfs_clear_first_layoutget(lo);
2204 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2205 PNFS_UPDATE_LAYOUT_EXIT);
2206 pnfs_put_layout_hdr(lo);
2207 out:
2208 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
2209 "(%s, offset: %llu, length: %llu)\n",
2210 __func__, ino->i_sb->s_id,
2211 (unsigned long long)NFS_FILEID(ino),
2212 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
2213 iomode==IOMODE_RW ? "read/write" : "read-only",
2214 (unsigned long long)pos,
2215 (unsigned long long)count);
2216 return lseg;
2217 out_unlock:
2218 spin_unlock(&ino->i_lock);
2219 goto out_put_layout_hdr;
2220 }
2221 EXPORT_SYMBOL_GPL(pnfs_update_layout);
2222
2223 static bool
pnfs_sanity_check_layout_range(struct pnfs_layout_range * range)2224 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
2225 {
2226 switch (range->iomode) {
2227 case IOMODE_READ:
2228 case IOMODE_RW:
2229 break;
2230 default:
2231 return false;
2232 }
2233 if (range->offset == NFS4_MAX_UINT64)
2234 return false;
2235 if (range->length == 0)
2236 return false;
2237 if (range->length != NFS4_MAX_UINT64 &&
2238 range->length > NFS4_MAX_UINT64 - range->offset)
2239 return false;
2240 return true;
2241 }
2242
2243 static struct pnfs_layout_hdr *
_pnfs_grab_empty_layout(struct inode * ino,struct nfs_open_context * ctx)2244 _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
2245 {
2246 struct pnfs_layout_hdr *lo;
2247
2248 spin_lock(&ino->i_lock);
2249 lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask());
2250 if (!lo)
2251 goto out_unlock;
2252 if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
2253 goto out_unlock;
2254 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
2255 goto out_unlock;
2256 if (pnfs_layoutgets_blocked(lo))
2257 goto out_unlock;
2258 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
2259 goto out_unlock;
2260 nfs_layoutget_begin(lo);
2261 spin_unlock(&ino->i_lock);
2262 _add_to_server_list(lo, NFS_SERVER(ino));
2263 return lo;
2264
2265 out_unlock:
2266 spin_unlock(&ino->i_lock);
2267 pnfs_put_layout_hdr(lo);
2268 return NULL;
2269 }
2270
_lgopen_prepare_attached(struct nfs4_opendata * data,struct nfs_open_context * ctx)2271 static void _lgopen_prepare_attached(struct nfs4_opendata *data,
2272 struct nfs_open_context *ctx)
2273 {
2274 struct inode *ino = data->dentry->d_inode;
2275 struct pnfs_layout_range rng = {
2276 .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2277 IOMODE_RW: IOMODE_READ,
2278 .offset = 0,
2279 .length = NFS4_MAX_UINT64,
2280 };
2281 struct nfs4_layoutget *lgp;
2282 struct pnfs_layout_hdr *lo;
2283
2284 /* Heuristic: don't send layoutget if we have cached data */
2285 if (rng.iomode == IOMODE_READ &&
2286 (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
2287 return;
2288
2289 lo = _pnfs_grab_empty_layout(ino, ctx);
2290 if (!lo)
2291 return;
2292 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng,
2293 nfs_io_gfp_mask());
2294 if (!lgp) {
2295 pnfs_clear_first_layoutget(lo);
2296 nfs_layoutget_end(lo);
2297 pnfs_put_layout_hdr(lo);
2298 return;
2299 }
2300 lgp->lo = lo;
2301 data->lgp = lgp;
2302 data->o_arg.lg_args = &lgp->args;
2303 data->o_res.lg_res = &lgp->res;
2304 }
2305
_lgopen_prepare_floating(struct nfs4_opendata * data,struct nfs_open_context * ctx)2306 static void _lgopen_prepare_floating(struct nfs4_opendata *data,
2307 struct nfs_open_context *ctx)
2308 {
2309 struct inode *ino = data->dentry->d_inode;
2310 struct pnfs_layout_range rng = {
2311 .iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2312 IOMODE_RW: IOMODE_READ,
2313 .offset = 0,
2314 .length = NFS4_MAX_UINT64,
2315 };
2316 struct nfs4_layoutget *lgp;
2317
2318 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng,
2319 nfs_io_gfp_mask());
2320 if (!lgp)
2321 return;
2322 data->lgp = lgp;
2323 data->o_arg.lg_args = &lgp->args;
2324 data->o_res.lg_res = &lgp->res;
2325 }
2326
pnfs_lgopen_prepare(struct nfs4_opendata * data,struct nfs_open_context * ctx)2327 void pnfs_lgopen_prepare(struct nfs4_opendata *data,
2328 struct nfs_open_context *ctx)
2329 {
2330 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
2331
2332 if (!(pnfs_enabled_sb(server) &&
2333 server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
2334 return;
2335 /* Could check on max_ops, but currently hardcoded high enough */
2336 if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
2337 return;
2338 if (data->lgp)
2339 return;
2340 if (data->state)
2341 _lgopen_prepare_attached(data, ctx);
2342 else
2343 _lgopen_prepare_floating(data, ctx);
2344 }
2345
pnfs_parse_lgopen(struct inode * ino,struct nfs4_layoutget * lgp,struct nfs_open_context * ctx)2346 void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
2347 struct nfs_open_context *ctx)
2348 {
2349 struct pnfs_layout_hdr *lo;
2350 struct pnfs_layout_segment *lseg;
2351 struct nfs_server *srv = NFS_SERVER(ino);
2352 u32 iomode;
2353
2354 if (!lgp)
2355 return;
2356 dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
2357 if (lgp->res.status) {
2358 switch (lgp->res.status) {
2359 default:
2360 break;
2361 /*
2362 * Halt lgopen attempts if the server doesn't recognise
2363 * the "current stateid" value, the layout type, or the
2364 * layoutget operation as being valid.
2365 * Also if it complains about too many ops in the compound
2366 * or of the request/reply being too big.
2367 */
2368 case -NFS4ERR_BAD_STATEID:
2369 case -NFS4ERR_NOTSUPP:
2370 case -NFS4ERR_REP_TOO_BIG:
2371 case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
2372 case -NFS4ERR_REQ_TOO_BIG:
2373 case -NFS4ERR_TOO_MANY_OPS:
2374 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2375 srv->caps &= ~NFS_CAP_LGOPEN;
2376 }
2377 return;
2378 }
2379 if (!lgp->lo) {
2380 lo = _pnfs_grab_empty_layout(ino, ctx);
2381 if (!lo)
2382 return;
2383 lgp->lo = lo;
2384 } else
2385 lo = lgp->lo;
2386
2387 lseg = pnfs_layout_process(lgp);
2388 if (!IS_ERR(lseg)) {
2389 iomode = lgp->args.range.iomode;
2390 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2391 pnfs_put_lseg(lseg);
2392 }
2393 }
2394
nfs4_lgopen_release(struct nfs4_layoutget * lgp)2395 void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
2396 {
2397 if (lgp != NULL) {
2398 if (lgp->lo) {
2399 pnfs_clear_first_layoutget(lgp->lo);
2400 nfs_layoutget_end(lgp->lo);
2401 }
2402 pnfs_layoutget_free(lgp);
2403 }
2404 }
2405
2406 struct pnfs_layout_segment *
pnfs_layout_process(struct nfs4_layoutget * lgp)2407 pnfs_layout_process(struct nfs4_layoutget *lgp)
2408 {
2409 struct pnfs_layout_hdr *lo = lgp->lo;
2410 struct nfs4_layoutget_res *res = &lgp->res;
2411 struct pnfs_layout_segment *lseg;
2412 struct inode *ino = lo->plh_inode;
2413 LIST_HEAD(free_me);
2414
2415 if (!pnfs_sanity_check_layout_range(&res->range))
2416 return ERR_PTR(-EINVAL);
2417
2418 /* Inject layout blob into I/O device driver */
2419 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
2420 if (IS_ERR_OR_NULL(lseg)) {
2421 if (!lseg)
2422 lseg = ERR_PTR(-ENOMEM);
2423
2424 dprintk("%s: Could not allocate layout: error %ld\n",
2425 __func__, PTR_ERR(lseg));
2426 return lseg;
2427 }
2428
2429 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
2430
2431 spin_lock(&ino->i_lock);
2432 if (pnfs_layoutgets_blocked(lo)) {
2433 dprintk("%s forget reply due to state\n", __func__);
2434 goto out_forget;
2435 }
2436
2437 if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2438 !pnfs_is_first_layoutget(lo))
2439 goto out_forget;
2440
2441 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
2442 /* existing state ID, make sure the sequence number matches. */
2443 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
2444 if (!pnfs_layout_is_valid(lo))
2445 lo->plh_barrier = 0;
2446 dprintk("%s forget reply due to sequence\n", __func__);
2447 goto out_forget;
2448 }
2449 pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false);
2450 } else if (pnfs_layout_is_valid(lo)) {
2451 /*
2452 * We got an entirely new state ID. Mark all segments for the
2453 * inode invalid, and retry the layoutget
2454 */
2455 struct pnfs_layout_range range = {
2456 .iomode = IOMODE_ANY,
2457 .length = NFS4_MAX_UINT64,
2458 };
2459 pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0);
2460 goto out_forget;
2461 } else {
2462 /* We have a completely new layout */
2463 pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true);
2464 }
2465
2466 pnfs_get_lseg(lseg);
2467 pnfs_layout_insert_lseg(lo, lseg, &free_me);
2468
2469
2470 if (res->return_on_close)
2471 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
2472
2473 spin_unlock(&ino->i_lock);
2474 pnfs_free_lseg_list(&free_me);
2475 return lseg;
2476
2477 out_forget:
2478 spin_unlock(&ino->i_lock);
2479 lseg->pls_layout = lo;
2480 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
2481 return ERR_PTR(-EAGAIN);
2482 }
2483
2484 /**
2485 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
2486 * @lo: pointer to layout header
2487 * @tmp_list: list header to be used with pnfs_free_lseg_list()
2488 * @return_range: describe layout segment ranges to be returned
2489 * @seq: stateid seqid to match
2490 *
2491 * This function is mainly intended for use by layoutrecall. It attempts
2492 * to free the layout segment immediately, or else to mark it for return
2493 * as soon as its reference count drops to zero.
2494 *
2495 * Returns
2496 * - 0: a layoutreturn needs to be scheduled.
2497 * - EBUSY: there are layout segment that are still in use.
2498 * - ENOENT: there are no layout segments that need to be returned.
2499 */
2500 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)2501 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2502 struct list_head *tmp_list,
2503 const struct pnfs_layout_range *return_range,
2504 u32 seq)
2505 {
2506 struct pnfs_layout_segment *lseg, *next;
2507 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
2508 int remaining = 0;
2509
2510 dprintk("%s:Begin lo %p\n", __func__, lo);
2511
2512 assert_spin_locked(&lo->plh_inode->i_lock);
2513
2514 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2515 tmp_list = &lo->plh_return_segs;
2516
2517 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2518 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2519 dprintk("%s: marking lseg %p iomode %d "
2520 "offset %llu length %llu\n", __func__,
2521 lseg, lseg->pls_range.iomode,
2522 lseg->pls_range.offset,
2523 lseg->pls_range.length);
2524 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2525 tmp_list = &lo->plh_return_segs;
2526 if (mark_lseg_invalid(lseg, tmp_list))
2527 continue;
2528 remaining++;
2529 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2530 pnfs_lseg_cancel_io(server, lseg);
2531 }
2532
2533 if (remaining) {
2534 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2535 return -EBUSY;
2536 }
2537
2538 if (!list_empty(&lo->plh_return_segs)) {
2539 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2540 return 0;
2541 }
2542
2543 return -ENOENT;
2544 }
2545
2546 static void
pnfs_mark_layout_for_return(struct inode * inode,const struct pnfs_layout_range * range)2547 pnfs_mark_layout_for_return(struct inode *inode,
2548 const struct pnfs_layout_range *range)
2549 {
2550 struct pnfs_layout_hdr *lo;
2551 bool return_now = false;
2552
2553 spin_lock(&inode->i_lock);
2554 lo = NFS_I(inode)->layout;
2555 if (!pnfs_layout_is_valid(lo)) {
2556 spin_unlock(&inode->i_lock);
2557 return;
2558 }
2559 pnfs_set_plh_return_info(lo, range->iomode, 0);
2560 /*
2561 * mark all matching lsegs so that we are sure to have no live
2562 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2563 * for how it works.
2564 */
2565 if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) {
2566 const struct cred *cred;
2567 nfs4_stateid stateid;
2568 enum pnfs_iomode iomode;
2569
2570 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
2571 spin_unlock(&inode->i_lock);
2572 if (return_now)
2573 pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
2574 } else {
2575 spin_unlock(&inode->i_lock);
2576 nfs_commit_inode(inode, 0);
2577 }
2578 }
2579
pnfs_error_mark_layout_for_return(struct inode * inode,struct pnfs_layout_segment * lseg)2580 void pnfs_error_mark_layout_for_return(struct inode *inode,
2581 struct pnfs_layout_segment *lseg)
2582 {
2583 struct pnfs_layout_range range = {
2584 .iomode = lseg->pls_range.iomode,
2585 .offset = 0,
2586 .length = NFS4_MAX_UINT64,
2587 };
2588
2589 pnfs_mark_layout_for_return(inode, &range);
2590 }
2591 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2592
2593 static bool
pnfs_layout_can_be_returned(struct pnfs_layout_hdr * lo)2594 pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo)
2595 {
2596 return pnfs_layout_is_valid(lo) &&
2597 !test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) &&
2598 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
2599 }
2600
2601 static struct pnfs_layout_segment *
pnfs_find_first_lseg(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range,enum pnfs_iomode iomode)2602 pnfs_find_first_lseg(struct pnfs_layout_hdr *lo,
2603 const struct pnfs_layout_range *range,
2604 enum pnfs_iomode iomode)
2605 {
2606 struct pnfs_layout_segment *lseg;
2607
2608 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
2609 if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
2610 continue;
2611 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2612 continue;
2613 if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY)
2614 continue;
2615 if (pnfs_lseg_range_intersecting(&lseg->pls_range, range))
2616 return lseg;
2617 }
2618 return NULL;
2619 }
2620
2621 /* Find open file states whose mode matches that of the range */
2622 static bool
pnfs_should_return_unused_layout(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range)2623 pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo,
2624 const struct pnfs_layout_range *range)
2625 {
2626 struct list_head *head;
2627 struct nfs_open_context *ctx;
2628 fmode_t mode = 0;
2629
2630 if (!pnfs_layout_can_be_returned(lo) ||
2631 !pnfs_find_first_lseg(lo, range, range->iomode))
2632 return false;
2633
2634 head = &NFS_I(lo->plh_inode)->open_files;
2635 list_for_each_entry_rcu(ctx, head, list) {
2636 if (ctx->state)
2637 mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE);
2638 }
2639
2640 switch (range->iomode) {
2641 default:
2642 break;
2643 case IOMODE_READ:
2644 mode &= ~FMODE_WRITE;
2645 break;
2646 case IOMODE_RW:
2647 if (pnfs_find_first_lseg(lo, range, IOMODE_READ))
2648 mode &= ~FMODE_READ;
2649 }
2650 return mode == 0;
2651 }
2652
pnfs_layout_return_unused_byserver(struct nfs_server * server,void * data)2653 static int pnfs_layout_return_unused_byserver(struct nfs_server *server,
2654 void *data)
2655 {
2656 const struct pnfs_layout_range *range = data;
2657 const struct cred *cred;
2658 struct pnfs_layout_hdr *lo;
2659 struct inode *inode;
2660 nfs4_stateid stateid;
2661 enum pnfs_iomode iomode;
2662
2663 restart:
2664 rcu_read_lock();
2665 list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
2666 inode = lo->plh_inode;
2667 if (!inode || !pnfs_layout_can_be_returned(lo) ||
2668 test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2669 continue;
2670 spin_lock(&inode->i_lock);
2671 if (!lo->plh_inode ||
2672 !pnfs_should_return_unused_layout(lo, range)) {
2673 spin_unlock(&inode->i_lock);
2674 continue;
2675 }
2676 pnfs_get_layout_hdr(lo);
2677 pnfs_set_plh_return_info(lo, range->iomode, 0);
2678 if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
2679 range, 0) != 0 ||
2680 !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) {
2681 spin_unlock(&inode->i_lock);
2682 rcu_read_unlock();
2683 pnfs_put_layout_hdr(lo);
2684 cond_resched();
2685 goto restart;
2686 }
2687 spin_unlock(&inode->i_lock);
2688 rcu_read_unlock();
2689 pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, false);
2690 pnfs_put_layout_hdr(lo);
2691 cond_resched();
2692 goto restart;
2693 }
2694 rcu_read_unlock();
2695 return 0;
2696 }
2697
2698 void
pnfs_layout_return_unused_byclid(struct nfs_client * clp,enum pnfs_iomode iomode)2699 pnfs_layout_return_unused_byclid(struct nfs_client *clp,
2700 enum pnfs_iomode iomode)
2701 {
2702 struct pnfs_layout_range range = {
2703 .iomode = iomode,
2704 .offset = 0,
2705 .length = NFS4_MAX_UINT64,
2706 };
2707
2708 nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver,
2709 &range);
2710 }
2711
2712 void
pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor * pgio)2713 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
2714 {
2715 if (pgio->pg_lseg == NULL ||
2716 test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
2717 return;
2718 pnfs_put_lseg(pgio->pg_lseg);
2719 pgio->pg_lseg = NULL;
2720 }
2721 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2722
2723 /*
2724 * Check for any intersection between the request and the pgio->pg_lseg,
2725 * and if none, put this pgio->pg_lseg away.
2726 */
2727 void
pnfs_generic_pg_check_range(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2728 pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2729 {
2730 if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
2731 pnfs_put_lseg(pgio->pg_lseg);
2732 pgio->pg_lseg = NULL;
2733 }
2734 }
2735 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_range);
2736
2737 void
pnfs_generic_pg_init_read(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2738 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2739 {
2740 u64 rd_size;
2741
2742 pnfs_generic_pg_check_layout(pgio);
2743 pnfs_generic_pg_check_range(pgio, req);
2744 if (pgio->pg_lseg == NULL) {
2745 if (pgio->pg_dreq == NULL)
2746 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2747 else
2748 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq,
2749 req_offset(req));
2750
2751 pgio->pg_lseg =
2752 pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2753 req_offset(req), rd_size,
2754 IOMODE_READ, false,
2755 nfs_io_gfp_mask());
2756 if (IS_ERR(pgio->pg_lseg)) {
2757 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2758 pgio->pg_lseg = NULL;
2759 return;
2760 }
2761 }
2762 /* If no lseg, fall back to read through mds */
2763 if (pgio->pg_lseg == NULL)
2764 nfs_pageio_reset_read_mds(pgio);
2765
2766 }
2767 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2768
2769 void
pnfs_generic_pg_init_write(struct nfs_pageio_descriptor * pgio,struct nfs_page * req,u64 wb_size)2770 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2771 struct nfs_page *req, u64 wb_size)
2772 {
2773 pnfs_generic_pg_check_layout(pgio);
2774 pnfs_generic_pg_check_range(pgio, req);
2775 if (pgio->pg_lseg == NULL) {
2776 pgio->pg_lseg =
2777 pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2778 req_offset(req), wb_size, IOMODE_RW,
2779 false, nfs_io_gfp_mask());
2780 if (IS_ERR(pgio->pg_lseg)) {
2781 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2782 pgio->pg_lseg = NULL;
2783 return;
2784 }
2785 }
2786 /* If no lseg, fall back to write through mds */
2787 if (pgio->pg_lseg == NULL)
2788 nfs_pageio_reset_write_mds(pgio);
2789 }
2790 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2791
2792 void
pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor * desc)2793 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2794 {
2795 if (desc->pg_lseg) {
2796 pnfs_put_lseg(desc->pg_lseg);
2797 desc->pg_lseg = NULL;
2798 }
2799 }
2800 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2801
2802 /*
2803 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2804 * of bytes (maximum @req->wb_bytes) that can be coalesced.
2805 */
2806 size_t
pnfs_generic_pg_test(struct nfs_pageio_descriptor * pgio,struct nfs_page * prev,struct nfs_page * req)2807 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2808 struct nfs_page *prev, struct nfs_page *req)
2809 {
2810 unsigned int size;
2811 u64 seg_end, req_start, seg_left;
2812
2813 size = nfs_generic_pg_test(pgio, prev, req);
2814 if (!size)
2815 return 0;
2816
2817 /*
2818 * 'size' contains the number of bytes left in the current page (up
2819 * to the original size asked for in @req->wb_bytes).
2820 *
2821 * Calculate how many bytes are left in the layout segment
2822 * and if there are less bytes than 'size', return that instead.
2823 *
2824 * Please also note that 'end_offset' is actually the offset of the
2825 * first byte that lies outside the pnfs_layout_range. FIXME?
2826 *
2827 */
2828 if (pgio->pg_lseg) {
2829 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2830 pgio->pg_lseg->pls_range.length);
2831 req_start = req_offset(req);
2832
2833 /* start of request is past the last byte of this segment */
2834 if (req_start >= seg_end)
2835 return 0;
2836
2837 /* adjust 'size' iff there are fewer bytes left in the
2838 * segment than what nfs_generic_pg_test returned */
2839 seg_left = seg_end - req_start;
2840 if (seg_left < size)
2841 size = (unsigned int)seg_left;
2842 }
2843
2844 return size;
2845 }
2846 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2847
pnfs_write_done_resend_to_mds(struct nfs_pgio_header * hdr)2848 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2849 {
2850 struct nfs_pageio_descriptor pgio;
2851
2852 /* Resend all requests through the MDS */
2853 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2854 hdr->completion_ops);
2855 return nfs_pageio_resend(&pgio, hdr);
2856 }
2857 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2858
pnfs_ld_handle_write_error(struct nfs_pgio_header * hdr)2859 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2860 {
2861
2862 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2863 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2864 PNFS_LAYOUTRET_ON_ERROR) {
2865 pnfs_return_layout(hdr->inode);
2866 }
2867 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2868 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2869 }
2870
2871 /*
2872 * Called by non rpc-based layout drivers
2873 */
pnfs_ld_write_done(struct nfs_pgio_header * hdr)2874 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2875 {
2876 if (likely(!hdr->pnfs_error)) {
2877 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2878 hdr->mds_offset + hdr->res.count);
2879 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2880 }
2881 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2882 if (unlikely(hdr->pnfs_error))
2883 pnfs_ld_handle_write_error(hdr);
2884 hdr->mds_ops->rpc_release(hdr);
2885 }
2886 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2887
2888 static void
pnfs_write_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)2889 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2890 struct nfs_pgio_header *hdr)
2891 {
2892 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2893
2894 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2895 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2896 nfs_pageio_reset_write_mds(desc);
2897 mirror->pg_recoalesce = 1;
2898 }
2899 hdr->completion_ops->completion(hdr);
2900 }
2901
2902 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)2903 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2904 const struct rpc_call_ops *call_ops,
2905 struct pnfs_layout_segment *lseg,
2906 int how)
2907 {
2908 struct inode *inode = hdr->inode;
2909 enum pnfs_try_status trypnfs;
2910 struct nfs_server *nfss = NFS_SERVER(inode);
2911
2912 hdr->mds_ops = call_ops;
2913
2914 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2915 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2916 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2917 if (trypnfs != PNFS_NOT_ATTEMPTED)
2918 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2919 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2920 return trypnfs;
2921 }
2922
2923 static void
pnfs_do_write(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr,int how)2924 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2925 struct nfs_pgio_header *hdr, int how)
2926 {
2927 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2928 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2929 enum pnfs_try_status trypnfs;
2930
2931 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2932 switch (trypnfs) {
2933 case PNFS_NOT_ATTEMPTED:
2934 pnfs_write_through_mds(desc, hdr);
2935 break;
2936 case PNFS_ATTEMPTED:
2937 break;
2938 case PNFS_TRY_AGAIN:
2939 /* cleanup hdr and prepare to redo pnfs */
2940 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2941 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2942 list_splice_init(&hdr->pages, &mirror->pg_list);
2943 mirror->pg_recoalesce = 1;
2944 }
2945 hdr->mds_ops->rpc_release(hdr);
2946 }
2947 }
2948
pnfs_writehdr_free(struct nfs_pgio_header * hdr)2949 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2950 {
2951 pnfs_put_lseg(hdr->lseg);
2952 nfs_pgio_header_free(hdr);
2953 }
2954
2955 int
pnfs_generic_pg_writepages(struct nfs_pageio_descriptor * desc)2956 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2957 {
2958 struct nfs_pgio_header *hdr;
2959 int ret;
2960
2961 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2962 if (!hdr) {
2963 desc->pg_error = -ENOMEM;
2964 return desc->pg_error;
2965 }
2966 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2967
2968 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2969 ret = nfs_generic_pgio(desc, hdr);
2970 if (!ret)
2971 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2972
2973 return ret;
2974 }
2975 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2976
pnfs_read_done_resend_to_mds(struct nfs_pgio_header * hdr)2977 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2978 {
2979 struct nfs_pageio_descriptor pgio;
2980
2981 /* Resend all requests through the MDS */
2982 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2983 return nfs_pageio_resend(&pgio, hdr);
2984 }
2985 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2986
pnfs_ld_handle_read_error(struct nfs_pgio_header * hdr)2987 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2988 {
2989 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2990 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2991 PNFS_LAYOUTRET_ON_ERROR) {
2992 pnfs_return_layout(hdr->inode);
2993 }
2994 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2995 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2996 }
2997
2998 /*
2999 * Called by non rpc-based layout drivers
3000 */
pnfs_ld_read_done(struct nfs_pgio_header * hdr)3001 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
3002 {
3003 if (likely(!hdr->pnfs_error))
3004 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
3005 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
3006 if (unlikely(hdr->pnfs_error))
3007 pnfs_ld_handle_read_error(hdr);
3008 hdr->mds_ops->rpc_release(hdr);
3009 }
3010 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
3011
3012 static void
pnfs_read_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3013 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
3014 struct nfs_pgio_header *hdr)
3015 {
3016 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3017
3018 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3019 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
3020 nfs_pageio_reset_read_mds(desc);
3021 mirror->pg_recoalesce = 1;
3022 }
3023 hdr->completion_ops->completion(hdr);
3024 }
3025
3026 /*
3027 * Call the appropriate parallel I/O subsystem read function.
3028 */
3029 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)3030 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
3031 const struct rpc_call_ops *call_ops,
3032 struct pnfs_layout_segment *lseg)
3033 {
3034 struct inode *inode = hdr->inode;
3035 struct nfs_server *nfss = NFS_SERVER(inode);
3036 enum pnfs_try_status trypnfs;
3037
3038 hdr->mds_ops = call_ops;
3039
3040 dprintk("%s: Reading ino:%lu %u@%llu\n",
3041 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
3042
3043 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
3044 if (trypnfs != PNFS_NOT_ATTEMPTED)
3045 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
3046 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
3047 return trypnfs;
3048 }
3049
3050 /* Resend all requests through pnfs. */
pnfs_read_resend_pnfs(struct nfs_pgio_header * hdr,unsigned int mirror_idx)3051 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr,
3052 unsigned int mirror_idx)
3053 {
3054 struct nfs_pageio_descriptor pgio;
3055
3056 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3057 /* Prevent deadlocks with layoutreturn! */
3058 pnfs_put_lseg(hdr->lseg);
3059 hdr->lseg = NULL;
3060
3061 nfs_pageio_init_read(&pgio, hdr->inode, false,
3062 hdr->completion_ops);
3063 pgio.pg_mirror_idx = mirror_idx;
3064 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
3065 }
3066 }
3067 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
3068
3069 static void
pnfs_do_read(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3070 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
3071 {
3072 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
3073 struct pnfs_layout_segment *lseg = desc->pg_lseg;
3074 enum pnfs_try_status trypnfs;
3075
3076 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
3077 switch (trypnfs) {
3078 case PNFS_NOT_ATTEMPTED:
3079 pnfs_read_through_mds(desc, hdr);
3080 break;
3081 case PNFS_ATTEMPTED:
3082 break;
3083 case PNFS_TRY_AGAIN:
3084 /* cleanup hdr and prepare to redo pnfs */
3085 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3086 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3087 list_splice_init(&hdr->pages, &mirror->pg_list);
3088 mirror->pg_recoalesce = 1;
3089 }
3090 hdr->mds_ops->rpc_release(hdr);
3091 }
3092 }
3093
pnfs_readhdr_free(struct nfs_pgio_header * hdr)3094 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
3095 {
3096 pnfs_put_lseg(hdr->lseg);
3097 nfs_pgio_header_free(hdr);
3098 }
3099
3100 int
pnfs_generic_pg_readpages(struct nfs_pageio_descriptor * desc)3101 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
3102 {
3103 struct nfs_pgio_header *hdr;
3104 int ret;
3105
3106 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
3107 if (!hdr) {
3108 desc->pg_error = -ENOMEM;
3109 return desc->pg_error;
3110 }
3111 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
3112 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
3113 ret = nfs_generic_pgio(desc, hdr);
3114 if (!ret)
3115 pnfs_do_read(desc, hdr);
3116 return ret;
3117 }
3118 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
3119
pnfs_clear_layoutcommitting(struct inode * inode)3120 static void pnfs_clear_layoutcommitting(struct inode *inode)
3121 {
3122 unsigned long *bitlock = &NFS_I(inode)->flags;
3123
3124 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
3125 smp_mb__after_atomic();
3126 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
3127 }
3128
3129 /*
3130 * There can be multiple RW segments.
3131 */
pnfs_list_write_lseg(struct inode * inode,struct list_head * listp)3132 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
3133 {
3134 struct pnfs_layout_segment *lseg;
3135
3136 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
3137 if (lseg->pls_range.iomode == IOMODE_RW &&
3138 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
3139 list_add(&lseg->pls_lc_list, listp);
3140 }
3141 }
3142
pnfs_list_write_lseg_done(struct inode * inode,struct list_head * listp)3143 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
3144 {
3145 struct pnfs_layout_segment *lseg, *tmp;
3146
3147 /* Matched by references in pnfs_set_layoutcommit */
3148 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
3149 list_del_init(&lseg->pls_lc_list);
3150 pnfs_put_lseg(lseg);
3151 }
3152
3153 pnfs_clear_layoutcommitting(inode);
3154 }
3155
pnfs_set_lo_fail(struct pnfs_layout_segment * lseg)3156 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
3157 {
3158 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
3159 }
3160 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
3161
3162 void
pnfs_set_layoutcommit(struct inode * inode,struct pnfs_layout_segment * lseg,loff_t end_pos)3163 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
3164 loff_t end_pos)
3165 {
3166 struct nfs_inode *nfsi = NFS_I(inode);
3167 bool mark_as_dirty = false;
3168
3169 spin_lock(&inode->i_lock);
3170 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
3171 nfsi->layout->plh_lwb = end_pos;
3172 mark_as_dirty = true;
3173 dprintk("%s: Set layoutcommit for inode %lu ",
3174 __func__, inode->i_ino);
3175 } else if (end_pos > nfsi->layout->plh_lwb)
3176 nfsi->layout->plh_lwb = end_pos;
3177 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
3178 /* references matched in nfs4_layoutcommit_release */
3179 pnfs_get_lseg(lseg);
3180 }
3181 spin_unlock(&inode->i_lock);
3182 dprintk("%s: lseg %p end_pos %llu\n",
3183 __func__, lseg, nfsi->layout->plh_lwb);
3184
3185 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
3186 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
3187 if (mark_as_dirty)
3188 mark_inode_dirty_sync(inode);
3189 }
3190 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
3191
pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data * data)3192 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
3193 {
3194 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
3195
3196 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
3197 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
3198 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
3199 }
3200
3201 /*
3202 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
3203 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
3204 * data to disk to allow the server to recover the data if it crashes.
3205 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
3206 * is off, and a COMMIT is sent to a data server, or
3207 * if WRITEs to a data server return NFS_DATA_SYNC.
3208 */
3209 int
pnfs_layoutcommit_inode(struct inode * inode,bool sync)3210 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
3211 {
3212 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3213 struct nfs4_layoutcommit_data *data;
3214 struct nfs_inode *nfsi = NFS_I(inode);
3215 loff_t end_pos;
3216 int status;
3217
3218 if (!pnfs_layoutcommit_outstanding(inode))
3219 return 0;
3220
3221 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
3222
3223 status = -EAGAIN;
3224 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
3225 if (!sync)
3226 goto out;
3227 status = wait_on_bit_lock_action(&nfsi->flags,
3228 NFS_INO_LAYOUTCOMMITTING,
3229 nfs_wait_bit_killable,
3230 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
3231 if (status)
3232 goto out;
3233 }
3234
3235 status = -ENOMEM;
3236 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
3237 data = kzalloc(sizeof(*data), nfs_io_gfp_mask());
3238 if (!data)
3239 goto clear_layoutcommitting;
3240
3241 status = 0;
3242 spin_lock(&inode->i_lock);
3243 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
3244 goto out_unlock;
3245
3246 INIT_LIST_HEAD(&data->lseg_list);
3247 pnfs_list_write_lseg(inode, &data->lseg_list);
3248
3249 end_pos = nfsi->layout->plh_lwb;
3250
3251 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
3252 data->cred = get_cred(nfsi->layout->plh_lc_cred);
3253 spin_unlock(&inode->i_lock);
3254
3255 data->args.inode = inode;
3256 nfs_fattr_init(&data->fattr);
3257 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3258 data->res.fattr = &data->fattr;
3259 if (end_pos != 0)
3260 data->args.lastbytewritten = end_pos - 1;
3261 else
3262 data->args.lastbytewritten = U64_MAX;
3263 data->res.server = NFS_SERVER(inode);
3264
3265 if (ld->prepare_layoutcommit) {
3266 status = ld->prepare_layoutcommit(&data->args);
3267 if (status) {
3268 put_cred(data->cred);
3269 spin_lock(&inode->i_lock);
3270 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
3271 if (end_pos > nfsi->layout->plh_lwb)
3272 nfsi->layout->plh_lwb = end_pos;
3273 goto out_unlock;
3274 }
3275 }
3276
3277
3278 status = nfs4_proc_layoutcommit(data, sync);
3279 out:
3280 if (status)
3281 mark_inode_dirty_sync(inode);
3282 dprintk("<-- %s status %d\n", __func__, status);
3283 return status;
3284 out_unlock:
3285 spin_unlock(&inode->i_lock);
3286 kfree(data);
3287 clear_layoutcommitting:
3288 pnfs_clear_layoutcommitting(inode);
3289 goto out;
3290 }
3291 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
3292
3293 int
pnfs_generic_sync(struct inode * inode,bool datasync)3294 pnfs_generic_sync(struct inode *inode, bool datasync)
3295 {
3296 return pnfs_layoutcommit_inode(inode, true);
3297 }
3298 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
3299
pnfs_mdsthreshold_alloc(void)3300 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
3301 {
3302 struct nfs4_threshold *thp;
3303
3304 thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask());
3305 if (!thp) {
3306 dprintk("%s mdsthreshold allocation failed\n", __func__);
3307 return NULL;
3308 }
3309 return thp;
3310 }
3311
3312 #if IS_ENABLED(CONFIG_NFS_V4_2)
3313 int
pnfs_report_layoutstat(struct inode * inode,gfp_t gfp_flags)3314 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
3315 {
3316 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3317 struct nfs_server *server = NFS_SERVER(inode);
3318 struct nfs_inode *nfsi = NFS_I(inode);
3319 struct nfs42_layoutstat_data *data;
3320 struct pnfs_layout_hdr *hdr;
3321 int status = 0;
3322
3323 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
3324 goto out;
3325
3326 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
3327 goto out;
3328
3329 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
3330 goto out;
3331
3332 spin_lock(&inode->i_lock);
3333 if (!NFS_I(inode)->layout) {
3334 spin_unlock(&inode->i_lock);
3335 goto out_clear_layoutstats;
3336 }
3337 hdr = NFS_I(inode)->layout;
3338 pnfs_get_layout_hdr(hdr);
3339 spin_unlock(&inode->i_lock);
3340
3341 data = kzalloc(sizeof(*data), gfp_flags);
3342 if (!data) {
3343 status = -ENOMEM;
3344 goto out_put;
3345 }
3346
3347 data->args.fh = NFS_FH(inode);
3348 data->args.inode = inode;
3349 status = ld->prepare_layoutstats(&data->args);
3350 if (status)
3351 goto out_free;
3352
3353 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
3354
3355 out:
3356 dprintk("%s returns %d\n", __func__, status);
3357 return status;
3358
3359 out_free:
3360 kfree(data);
3361 out_put:
3362 pnfs_put_layout_hdr(hdr);
3363 out_clear_layoutstats:
3364 smp_mb__before_atomic();
3365 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
3366 smp_mb__after_atomic();
3367 goto out;
3368 }
3369 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
3370 #endif
3371
3372 unsigned int layoutstats_timer;
3373 module_param(layoutstats_timer, uint, 0644);
3374 EXPORT_SYMBOL_GPL(layoutstats_timer);
3375