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