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