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