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