NFSv4.1: Replace dprintk() in pnfs_update_layout with something less buggy
[deliverable/linux.git] / fs / nfs / pnfs.c
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 "internal.h"
34 #include "pnfs.h"
35 #include "iostat.h"
36
37 #define NFSDBG_FACILITY NFSDBG_PNFS
38
39 /* Locking:
40 *
41 * pnfs_spinlock:
42 * protects pnfs_modules_tbl.
43 */
44 static DEFINE_SPINLOCK(pnfs_spinlock);
45
46 /*
47 * pnfs_modules_tbl holds all pnfs modules
48 */
49 static LIST_HEAD(pnfs_modules_tbl);
50
51 /* Return the registered pnfs layout driver module matching given id */
52 static struct pnfs_layoutdriver_type *
53 find_pnfs_driver_locked(u32 id)
54 {
55 struct pnfs_layoutdriver_type *local;
56
57 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
58 if (local->id == id)
59 goto out;
60 local = NULL;
61 out:
62 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
63 return local;
64 }
65
66 static struct pnfs_layoutdriver_type *
67 find_pnfs_driver(u32 id)
68 {
69 struct pnfs_layoutdriver_type *local;
70
71 spin_lock(&pnfs_spinlock);
72 local = find_pnfs_driver_locked(id);
73 if (local != NULL && !try_module_get(local->owner)) {
74 dprintk("%s: Could not grab reference on module\n", __func__);
75 local = NULL;
76 }
77 spin_unlock(&pnfs_spinlock);
78 return local;
79 }
80
81 void
82 unset_pnfs_layoutdriver(struct nfs_server *nfss)
83 {
84 if (nfss->pnfs_curr_ld) {
85 if (nfss->pnfs_curr_ld->clear_layoutdriver)
86 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
87 /* Decrement the MDS count. Purge the deviceid cache if zero */
88 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
89 nfs4_deviceid_purge_client(nfss->nfs_client);
90 module_put(nfss->pnfs_curr_ld->owner);
91 }
92 nfss->pnfs_curr_ld = NULL;
93 }
94
95 /*
96 * Try to set the server's pnfs module to the pnfs layout type specified by id.
97 * Currently only one pNFS layout driver per filesystem is supported.
98 *
99 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
100 */
101 void
102 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
103 u32 id)
104 {
105 struct pnfs_layoutdriver_type *ld_type = NULL;
106
107 if (id == 0)
108 goto out_no_driver;
109 if (!(server->nfs_client->cl_exchange_flags &
110 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
111 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
112 __func__, id, server->nfs_client->cl_exchange_flags);
113 goto out_no_driver;
114 }
115 ld_type = find_pnfs_driver(id);
116 if (!ld_type) {
117 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
118 ld_type = find_pnfs_driver(id);
119 if (!ld_type) {
120 dprintk("%s: No pNFS module found for %u.\n",
121 __func__, id);
122 goto out_no_driver;
123 }
124 }
125 server->pnfs_curr_ld = ld_type;
126 if (ld_type->set_layoutdriver
127 && ld_type->set_layoutdriver(server, mntfh)) {
128 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
129 "driver %u.\n", __func__, id);
130 module_put(ld_type->owner);
131 goto out_no_driver;
132 }
133 /* Bump the MDS count */
134 atomic_inc(&server->nfs_client->cl_mds_count);
135
136 dprintk("%s: pNFS module for %u set\n", __func__, id);
137 return;
138
139 out_no_driver:
140 dprintk("%s: Using NFSv4 I/O\n", __func__);
141 server->pnfs_curr_ld = NULL;
142 }
143
144 int
145 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
146 {
147 int status = -EINVAL;
148 struct pnfs_layoutdriver_type *tmp;
149
150 if (ld_type->id == 0) {
151 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
152 return status;
153 }
154 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
155 printk(KERN_ERR "NFS: %s Layout driver must provide "
156 "alloc_lseg and free_lseg.\n", __func__);
157 return status;
158 }
159
160 spin_lock(&pnfs_spinlock);
161 tmp = find_pnfs_driver_locked(ld_type->id);
162 if (!tmp) {
163 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
164 status = 0;
165 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
166 ld_type->name);
167 } else {
168 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
169 __func__, ld_type->id);
170 }
171 spin_unlock(&pnfs_spinlock);
172
173 return status;
174 }
175 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
176
177 void
178 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
179 {
180 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
181 spin_lock(&pnfs_spinlock);
182 list_del(&ld_type->pnfs_tblid);
183 spin_unlock(&pnfs_spinlock);
184 }
185 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
186
187 /*
188 * pNFS client layout cache
189 */
190
191 /* Need to hold i_lock if caller does not already hold reference */
192 void
193 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
194 {
195 atomic_inc(&lo->plh_refcount);
196 }
197
198 static struct pnfs_layout_hdr *
199 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
200 {
201 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
202 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
203 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
204 }
205
206 static void
207 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
208 {
209 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
210 put_rpccred(lo->plh_lc_cred);
211 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
212 }
213
214 static void
215 destroy_layout_hdr(struct pnfs_layout_hdr *lo)
216 {
217 dprintk("%s: freeing layout cache %p\n", __func__, lo);
218 BUG_ON(!list_empty(&lo->plh_layouts));
219 NFS_I(lo->plh_inode)->layout = NULL;
220 pnfs_free_layout_hdr(lo);
221 }
222
223 static void
224 pnfs_put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
225 {
226 if (atomic_dec_and_test(&lo->plh_refcount))
227 destroy_layout_hdr(lo);
228 }
229
230 void
231 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
232 {
233 struct inode *inode = lo->plh_inode;
234
235 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
236 destroy_layout_hdr(lo);
237 spin_unlock(&inode->i_lock);
238 }
239 }
240
241 static void
242 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
243 {
244 INIT_LIST_HEAD(&lseg->pls_list);
245 INIT_LIST_HEAD(&lseg->pls_lc_list);
246 atomic_set(&lseg->pls_refcount, 1);
247 smp_mb();
248 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
249 lseg->pls_layout = lo;
250 }
251
252 static void free_lseg(struct pnfs_layout_segment *lseg)
253 {
254 struct inode *ino = lseg->pls_layout->plh_inode;
255
256 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
257 /* Matched by pnfs_get_layout_hdr in pnfs_insert_layout */
258 pnfs_put_layout_hdr(NFS_I(ino)->layout);
259 }
260
261 static void
262 pnfs_put_lseg_common(struct pnfs_layout_segment *lseg)
263 {
264 struct inode *inode = lseg->pls_layout->plh_inode;
265
266 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
267 list_del_init(&lseg->pls_list);
268 if (list_empty(&lseg->pls_layout->plh_segs)) {
269 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
270 /* Matched by initial refcount set in alloc_init_layout_hdr */
271 pnfs_put_layout_hdr_locked(lseg->pls_layout);
272 }
273 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
274 }
275
276 void
277 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
278 {
279 struct inode *inode;
280
281 if (!lseg)
282 return;
283
284 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
285 atomic_read(&lseg->pls_refcount),
286 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
287 inode = lseg->pls_layout->plh_inode;
288 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
289 LIST_HEAD(free_me);
290
291 pnfs_put_lseg_common(lseg);
292 list_add(&lseg->pls_list, &free_me);
293 spin_unlock(&inode->i_lock);
294 pnfs_free_lseg_list(&free_me);
295 }
296 }
297 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
298
299 static inline u64
300 end_offset(u64 start, u64 len)
301 {
302 u64 end;
303
304 end = start + len;
305 return end >= start ? end : NFS4_MAX_UINT64;
306 }
307
308 /* last octet in a range */
309 static inline u64
310 last_byte_offset(u64 start, u64 len)
311 {
312 u64 end;
313
314 BUG_ON(!len);
315 end = start + len;
316 return end > start ? end - 1 : NFS4_MAX_UINT64;
317 }
318
319 /*
320 * is l2 fully contained in l1?
321 * start1 end1
322 * [----------------------------------)
323 * start2 end2
324 * [----------------)
325 */
326 static inline int
327 lo_seg_contained(struct pnfs_layout_range *l1,
328 struct pnfs_layout_range *l2)
329 {
330 u64 start1 = l1->offset;
331 u64 end1 = end_offset(start1, l1->length);
332 u64 start2 = l2->offset;
333 u64 end2 = end_offset(start2, l2->length);
334
335 return (start1 <= start2) && (end1 >= end2);
336 }
337
338 /*
339 * is l1 and l2 intersecting?
340 * start1 end1
341 * [----------------------------------)
342 * start2 end2
343 * [----------------)
344 */
345 static inline int
346 lo_seg_intersecting(struct pnfs_layout_range *l1,
347 struct pnfs_layout_range *l2)
348 {
349 u64 start1 = l1->offset;
350 u64 end1 = end_offset(start1, l1->length);
351 u64 start2 = l2->offset;
352 u64 end2 = end_offset(start2, l2->length);
353
354 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
355 (end2 == NFS4_MAX_UINT64 || end2 > start1);
356 }
357
358 static bool
359 should_free_lseg(struct pnfs_layout_range *lseg_range,
360 struct pnfs_layout_range *recall_range)
361 {
362 return (recall_range->iomode == IOMODE_ANY ||
363 lseg_range->iomode == recall_range->iomode) &&
364 lo_seg_intersecting(lseg_range, recall_range);
365 }
366
367 /* Returns 1 if lseg is removed from list, 0 otherwise */
368 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
369 struct list_head *tmp_list)
370 {
371 int rv = 0;
372
373 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
374 /* Remove the reference keeping the lseg in the
375 * list. It will now be removed when all
376 * outstanding io is finished.
377 */
378 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
379 atomic_read(&lseg->pls_refcount));
380 if (atomic_dec_and_test(&lseg->pls_refcount)) {
381 pnfs_put_lseg_common(lseg);
382 list_add(&lseg->pls_list, tmp_list);
383 rv = 1;
384 }
385 }
386 return rv;
387 }
388
389 /* Returns count of number of matching invalid lsegs remaining in list
390 * after call.
391 */
392 int
393 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
394 struct list_head *tmp_list,
395 struct pnfs_layout_range *recall_range)
396 {
397 struct pnfs_layout_segment *lseg, *next;
398 int invalid = 0, removed = 0;
399
400 dprintk("%s:Begin lo %p\n", __func__, lo);
401
402 if (list_empty(&lo->plh_segs)) {
403 /* Reset MDS Threshold I/O counters */
404 NFS_I(lo->plh_inode)->write_io = 0;
405 NFS_I(lo->plh_inode)->read_io = 0;
406 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
407 pnfs_put_layout_hdr_locked(lo);
408 return 0;
409 }
410 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
411 if (!recall_range ||
412 should_free_lseg(&lseg->pls_range, recall_range)) {
413 dprintk("%s: freeing lseg %p iomode %d "
414 "offset %llu length %llu\n", __func__,
415 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
416 lseg->pls_range.length);
417 invalid++;
418 removed += mark_lseg_invalid(lseg, tmp_list);
419 }
420 dprintk("%s:Return %i\n", __func__, invalid - removed);
421 return invalid - removed;
422 }
423
424 /* note free_me must contain lsegs from a single layout_hdr */
425 void
426 pnfs_free_lseg_list(struct list_head *free_me)
427 {
428 struct pnfs_layout_segment *lseg, *tmp;
429 struct pnfs_layout_hdr *lo;
430
431 if (list_empty(free_me))
432 return;
433
434 lo = list_first_entry(free_me, struct pnfs_layout_segment,
435 pls_list)->pls_layout;
436
437 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
438 struct nfs_client *clp;
439
440 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
441 spin_lock(&clp->cl_lock);
442 list_del_init(&lo->plh_layouts);
443 spin_unlock(&clp->cl_lock);
444 }
445 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
446 list_del(&lseg->pls_list);
447 free_lseg(lseg);
448 }
449 }
450
451 void
452 pnfs_destroy_layout(struct nfs_inode *nfsi)
453 {
454 struct pnfs_layout_hdr *lo;
455 LIST_HEAD(tmp_list);
456
457 spin_lock(&nfsi->vfs_inode.i_lock);
458 lo = nfsi->layout;
459 if (lo) {
460 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
461 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
462 }
463 spin_unlock(&nfsi->vfs_inode.i_lock);
464 pnfs_free_lseg_list(&tmp_list);
465 }
466 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
467
468 /*
469 * Called by the state manger to remove all layouts established under an
470 * expired lease.
471 */
472 void
473 pnfs_destroy_all_layouts(struct nfs_client *clp)
474 {
475 struct nfs_server *server;
476 struct pnfs_layout_hdr *lo;
477 LIST_HEAD(tmp_list);
478
479 nfs4_deviceid_mark_client_invalid(clp);
480 nfs4_deviceid_purge_client(clp);
481
482 spin_lock(&clp->cl_lock);
483 rcu_read_lock();
484 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
485 if (!list_empty(&server->layouts))
486 list_splice_init(&server->layouts, &tmp_list);
487 }
488 rcu_read_unlock();
489 spin_unlock(&clp->cl_lock);
490
491 while (!list_empty(&tmp_list)) {
492 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
493 plh_layouts);
494 dprintk("%s freeing layout for inode %lu\n", __func__,
495 lo->plh_inode->i_ino);
496 list_del_init(&lo->plh_layouts);
497 pnfs_destroy_layout(NFS_I(lo->plh_inode));
498 }
499 }
500
501 /* update lo->plh_stateid with new if is more recent */
502 void
503 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
504 bool update_barrier)
505 {
506 u32 oldseq, newseq;
507
508 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
509 newseq = be32_to_cpu(new->seqid);
510 if ((int)(newseq - oldseq) > 0) {
511 nfs4_stateid_copy(&lo->plh_stateid, new);
512 if (update_barrier) {
513 u32 new_barrier = be32_to_cpu(new->seqid);
514
515 if ((int)(new_barrier - lo->plh_barrier))
516 lo->plh_barrier = new_barrier;
517 } else {
518 /* Because of wraparound, we want to keep the barrier
519 * "close" to the current seqids. It needs to be
520 * within 2**31 to count as "behind", so if it
521 * gets too near that limit, give us a litle leeway
522 * and bring it to within 2**30.
523 * NOTE - and yes, this is all unsigned arithmetic.
524 */
525 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
526 lo->plh_barrier = newseq - (1 << 30);
527 }
528 }
529 }
530
531 /* lget is set to 1 if called from inside send_layoutget call chain */
532 static bool
533 pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
534 int lget)
535 {
536 if ((stateid) &&
537 (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
538 return true;
539 return lo->plh_block_lgets ||
540 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
541 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
542 (list_empty(&lo->plh_segs) &&
543 (atomic_read(&lo->plh_outstanding) > lget));
544 }
545
546 int
547 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
548 struct nfs4_state *open_state)
549 {
550 int status = 0;
551
552 dprintk("--> %s\n", __func__);
553 spin_lock(&lo->plh_inode->i_lock);
554 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
555 status = -EAGAIN;
556 } else if (list_empty(&lo->plh_segs)) {
557 int seq;
558
559 do {
560 seq = read_seqbegin(&open_state->seqlock);
561 nfs4_stateid_copy(dst, &open_state->stateid);
562 } while (read_seqretry(&open_state->seqlock, seq));
563 } else
564 nfs4_stateid_copy(dst, &lo->plh_stateid);
565 spin_unlock(&lo->plh_inode->i_lock);
566 dprintk("<-- %s\n", __func__);
567 return status;
568 }
569
570 /*
571 * Get layout from server.
572 * for now, assume that whole file layouts are requested.
573 * arg->offset: 0
574 * arg->length: all ones
575 */
576 static struct pnfs_layout_segment *
577 send_layoutget(struct pnfs_layout_hdr *lo,
578 struct nfs_open_context *ctx,
579 struct pnfs_layout_range *range,
580 gfp_t gfp_flags)
581 {
582 struct inode *ino = lo->plh_inode;
583 struct nfs_server *server = NFS_SERVER(ino);
584 struct nfs4_layoutget *lgp;
585 struct pnfs_layout_segment *lseg;
586
587 dprintk("--> %s\n", __func__);
588
589 BUG_ON(ctx == NULL);
590 lgp = kzalloc(sizeof(*lgp), gfp_flags);
591 if (lgp == NULL)
592 return NULL;
593
594 lgp->args.minlength = PAGE_CACHE_SIZE;
595 if (lgp->args.minlength > range->length)
596 lgp->args.minlength = range->length;
597 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
598 lgp->args.range = *range;
599 lgp->args.type = server->pnfs_curr_ld->id;
600 lgp->args.inode = ino;
601 lgp->args.ctx = get_nfs_open_context(ctx);
602 lgp->gfp_flags = gfp_flags;
603
604 /* Synchronously retrieve layout information from server and
605 * store in lseg.
606 */
607 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
608 if (IS_ERR(lseg)) {
609 switch (PTR_ERR(lseg)) {
610 case -ENOMEM:
611 case -ERESTARTSYS:
612 break;
613 default:
614 /* remember that LAYOUTGET failed and suspend trying */
615 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
616 }
617 return NULL;
618 }
619
620 return lseg;
621 }
622
623 /*
624 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
625 * when the layout segment list is empty.
626 *
627 * Note that a pnfs_layout_hdr can exist with an empty layout segment
628 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
629 * deviceid is marked invalid.
630 */
631 int
632 _pnfs_return_layout(struct inode *ino)
633 {
634 struct pnfs_layout_hdr *lo = NULL;
635 struct nfs_inode *nfsi = NFS_I(ino);
636 LIST_HEAD(tmp_list);
637 struct nfs4_layoutreturn *lrp;
638 nfs4_stateid stateid;
639 int status = 0, empty;
640
641 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
642
643 spin_lock(&ino->i_lock);
644 lo = nfsi->layout;
645 if (!lo || pnfs_test_layout_returned(lo)) {
646 spin_unlock(&ino->i_lock);
647 dprintk("NFS: %s no layout to return\n", __func__);
648 goto out;
649 }
650 stateid = nfsi->layout->plh_stateid;
651 /* Reference matched in nfs4_layoutreturn_release */
652 pnfs_get_layout_hdr(lo);
653 empty = list_empty(&lo->plh_segs);
654 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
655 /* Don't send a LAYOUTRETURN if list was initially empty */
656 if (empty) {
657 spin_unlock(&ino->i_lock);
658 pnfs_put_layout_hdr(lo);
659 dprintk("NFS: %s no layout segments to return\n", __func__);
660 goto out;
661 }
662 lo->plh_block_lgets++;
663 pnfs_mark_layout_returned(lo);
664 spin_unlock(&ino->i_lock);
665 pnfs_free_lseg_list(&tmp_list);
666
667 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
668
669 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
670 if (unlikely(lrp == NULL)) {
671 status = -ENOMEM;
672 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
673 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
674 pnfs_clear_layout_returned(lo);
675 pnfs_put_layout_hdr(lo);
676 goto out;
677 }
678
679 lrp->args.stateid = stateid;
680 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
681 lrp->args.inode = ino;
682 lrp->args.layout = lo;
683 lrp->clp = NFS_SERVER(ino)->nfs_client;
684
685 status = nfs4_proc_layoutreturn(lrp);
686 out:
687 dprintk("<-- %s status: %d\n", __func__, status);
688 return status;
689 }
690 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
691
692 bool pnfs_roc(struct inode *ino)
693 {
694 struct pnfs_layout_hdr *lo;
695 struct pnfs_layout_segment *lseg, *tmp;
696 LIST_HEAD(tmp_list);
697 bool found = false;
698
699 spin_lock(&ino->i_lock);
700 lo = NFS_I(ino)->layout;
701 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
702 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
703 goto out_nolayout;
704 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
705 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
706 mark_lseg_invalid(lseg, &tmp_list);
707 found = true;
708 }
709 if (!found)
710 goto out_nolayout;
711 lo->plh_block_lgets++;
712 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
713 spin_unlock(&ino->i_lock);
714 pnfs_free_lseg_list(&tmp_list);
715 return true;
716
717 out_nolayout:
718 spin_unlock(&ino->i_lock);
719 return false;
720 }
721
722 void pnfs_roc_release(struct inode *ino)
723 {
724 struct pnfs_layout_hdr *lo;
725
726 spin_lock(&ino->i_lock);
727 lo = NFS_I(ino)->layout;
728 lo->plh_block_lgets--;
729 pnfs_put_layout_hdr_locked(lo);
730 spin_unlock(&ino->i_lock);
731 }
732
733 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
734 {
735 struct pnfs_layout_hdr *lo;
736
737 spin_lock(&ino->i_lock);
738 lo = NFS_I(ino)->layout;
739 if ((int)(barrier - lo->plh_barrier) > 0)
740 lo->plh_barrier = barrier;
741 spin_unlock(&ino->i_lock);
742 }
743
744 bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
745 {
746 struct nfs_inode *nfsi = NFS_I(ino);
747 struct pnfs_layout_segment *lseg;
748 bool found = false;
749
750 spin_lock(&ino->i_lock);
751 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
752 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
753 found = true;
754 break;
755 }
756 if (!found) {
757 struct pnfs_layout_hdr *lo = nfsi->layout;
758 u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
759
760 /* Since close does not return a layout stateid for use as
761 * a barrier, we choose the worst-case barrier.
762 */
763 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
764 }
765 spin_unlock(&ino->i_lock);
766 return found;
767 }
768
769 /*
770 * Compare two layout segments for sorting into layout cache.
771 * We want to preferentially return RW over RO layouts, so ensure those
772 * are seen first.
773 */
774 static s64
775 cmp_layout(struct pnfs_layout_range *l1,
776 struct pnfs_layout_range *l2)
777 {
778 s64 d;
779
780 /* high offset > low offset */
781 d = l1->offset - l2->offset;
782 if (d)
783 return d;
784
785 /* short length > long length */
786 d = l2->length - l1->length;
787 if (d)
788 return d;
789
790 /* read > read/write */
791 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
792 }
793
794 static void
795 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
796 struct pnfs_layout_segment *lseg)
797 {
798 struct pnfs_layout_segment *lp;
799
800 dprintk("%s:Begin\n", __func__);
801
802 assert_spin_locked(&lo->plh_inode->i_lock);
803 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
804 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
805 continue;
806 list_add_tail(&lseg->pls_list, &lp->pls_list);
807 dprintk("%s: inserted lseg %p "
808 "iomode %d offset %llu length %llu before "
809 "lp %p iomode %d offset %llu length %llu\n",
810 __func__, lseg, lseg->pls_range.iomode,
811 lseg->pls_range.offset, lseg->pls_range.length,
812 lp, lp->pls_range.iomode, lp->pls_range.offset,
813 lp->pls_range.length);
814 goto out;
815 }
816 list_add_tail(&lseg->pls_list, &lo->plh_segs);
817 dprintk("%s: inserted lseg %p "
818 "iomode %d offset %llu length %llu at tail\n",
819 __func__, lseg, lseg->pls_range.iomode,
820 lseg->pls_range.offset, lseg->pls_range.length);
821 out:
822 pnfs_get_layout_hdr(lo);
823
824 dprintk("%s:Return\n", __func__);
825 }
826
827 static struct pnfs_layout_hdr *
828 alloc_init_layout_hdr(struct inode *ino,
829 struct nfs_open_context *ctx,
830 gfp_t gfp_flags)
831 {
832 struct pnfs_layout_hdr *lo;
833
834 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
835 if (!lo)
836 return NULL;
837 atomic_set(&lo->plh_refcount, 1);
838 INIT_LIST_HEAD(&lo->plh_layouts);
839 INIT_LIST_HEAD(&lo->plh_segs);
840 INIT_LIST_HEAD(&lo->plh_bulk_recall);
841 lo->plh_inode = ino;
842 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
843 return lo;
844 }
845
846 static struct pnfs_layout_hdr *
847 pnfs_find_alloc_layout(struct inode *ino,
848 struct nfs_open_context *ctx,
849 gfp_t gfp_flags)
850 {
851 struct nfs_inode *nfsi = NFS_I(ino);
852 struct pnfs_layout_hdr *new = NULL;
853
854 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
855
856 assert_spin_locked(&ino->i_lock);
857 if (nfsi->layout) {
858 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
859 return NULL;
860 else
861 return nfsi->layout;
862 }
863 spin_unlock(&ino->i_lock);
864 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
865 spin_lock(&ino->i_lock);
866
867 if (likely(nfsi->layout == NULL)) /* Won the race? */
868 nfsi->layout = new;
869 else
870 pnfs_free_layout_hdr(new);
871 return nfsi->layout;
872 }
873
874 /*
875 * iomode matching rules:
876 * iomode lseg match
877 * ----- ----- -----
878 * ANY READ true
879 * ANY RW true
880 * RW READ false
881 * RW RW true
882 * READ READ true
883 * READ RW true
884 */
885 static int
886 is_matching_lseg(struct pnfs_layout_range *ls_range,
887 struct pnfs_layout_range *range)
888 {
889 struct pnfs_layout_range range1;
890
891 if ((range->iomode == IOMODE_RW &&
892 ls_range->iomode != IOMODE_RW) ||
893 !lo_seg_intersecting(ls_range, range))
894 return 0;
895
896 /* range1 covers only the first byte in the range */
897 range1 = *range;
898 range1.length = 1;
899 return lo_seg_contained(ls_range, &range1);
900 }
901
902 /*
903 * lookup range in layout
904 */
905 static struct pnfs_layout_segment *
906 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
907 struct pnfs_layout_range *range)
908 {
909 struct pnfs_layout_segment *lseg, *ret = NULL;
910
911 dprintk("%s:Begin\n", __func__);
912
913 assert_spin_locked(&lo->plh_inode->i_lock);
914 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
915 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
916 is_matching_lseg(&lseg->pls_range, range)) {
917 ret = pnfs_get_lseg(lseg);
918 break;
919 }
920 if (lseg->pls_range.offset > range->offset)
921 break;
922 }
923
924 dprintk("%s:Return lseg %p ref %d\n",
925 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
926 return ret;
927 }
928
929 /*
930 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
931 * to the MDS or over pNFS
932 *
933 * The nfs_inode read_io and write_io fields are cumulative counters reset
934 * when there are no layout segments. Note that in pnfs_update_layout iomode
935 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
936 * WRITE request.
937 *
938 * A return of true means use MDS I/O.
939 *
940 * From rfc 5661:
941 * If a file's size is smaller than the file size threshold, data accesses
942 * SHOULD be sent to the metadata server. If an I/O request has a length that
943 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
944 * server. If both file size and I/O size are provided, the client SHOULD
945 * reach or exceed both thresholds before sending its read or write
946 * requests to the data server.
947 */
948 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
949 struct inode *ino, int iomode)
950 {
951 struct nfs4_threshold *t = ctx->mdsthreshold;
952 struct nfs_inode *nfsi = NFS_I(ino);
953 loff_t fsize = i_size_read(ino);
954 bool size = false, size_set = false, io = false, io_set = false, ret = false;
955
956 if (t == NULL)
957 return ret;
958
959 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
960 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
961
962 switch (iomode) {
963 case IOMODE_READ:
964 if (t->bm & THRESHOLD_RD) {
965 dprintk("%s fsize %llu\n", __func__, fsize);
966 size_set = true;
967 if (fsize < t->rd_sz)
968 size = true;
969 }
970 if (t->bm & THRESHOLD_RD_IO) {
971 dprintk("%s nfsi->read_io %llu\n", __func__,
972 nfsi->read_io);
973 io_set = true;
974 if (nfsi->read_io < t->rd_io_sz)
975 io = true;
976 }
977 break;
978 case IOMODE_RW:
979 if (t->bm & THRESHOLD_WR) {
980 dprintk("%s fsize %llu\n", __func__, fsize);
981 size_set = true;
982 if (fsize < t->wr_sz)
983 size = true;
984 }
985 if (t->bm & THRESHOLD_WR_IO) {
986 dprintk("%s nfsi->write_io %llu\n", __func__,
987 nfsi->write_io);
988 io_set = true;
989 if (nfsi->write_io < t->wr_io_sz)
990 io = true;
991 }
992 break;
993 }
994 if (size_set && io_set) {
995 if (size && io)
996 ret = true;
997 } else if (size || io)
998 ret = true;
999
1000 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1001 return ret;
1002 }
1003
1004 /*
1005 * Layout segment is retreived from the server if not cached.
1006 * The appropriate layout segment is referenced and returned to the caller.
1007 */
1008 struct pnfs_layout_segment *
1009 pnfs_update_layout(struct inode *ino,
1010 struct nfs_open_context *ctx,
1011 loff_t pos,
1012 u64 count,
1013 enum pnfs_iomode iomode,
1014 gfp_t gfp_flags)
1015 {
1016 struct pnfs_layout_range arg = {
1017 .iomode = iomode,
1018 .offset = pos,
1019 .length = count,
1020 };
1021 unsigned pg_offset;
1022 struct nfs_inode *nfsi = NFS_I(ino);
1023 struct nfs_server *server = NFS_SERVER(ino);
1024 struct nfs_client *clp = server->nfs_client;
1025 struct pnfs_layout_hdr *lo;
1026 struct pnfs_layout_segment *lseg = NULL;
1027 bool first = false;
1028
1029 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1030 goto out;
1031
1032 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1033 goto out;
1034
1035 spin_lock(&ino->i_lock);
1036 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1037 if (lo == NULL)
1038 goto out_unlock;
1039
1040 /* Do we even need to bother with this? */
1041 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1042 dprintk("%s matches recall, use MDS\n", __func__);
1043 goto out_unlock;
1044 }
1045
1046 /* if LAYOUTGET already failed once we don't try again */
1047 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
1048 goto out_unlock;
1049
1050 /* Check to see if the layout for the given range already exists */
1051 lseg = pnfs_find_lseg(lo, &arg);
1052 if (lseg)
1053 goto out_unlock;
1054
1055 if (pnfs_layoutgets_blocked(lo, NULL, 0))
1056 goto out_unlock;
1057 atomic_inc(&lo->plh_outstanding);
1058
1059 pnfs_get_layout_hdr(lo);
1060 if (list_empty(&lo->plh_segs))
1061 first = true;
1062
1063 /* Enable LAYOUTRETURNs */
1064 pnfs_clear_layout_returned(lo);
1065
1066 spin_unlock(&ino->i_lock);
1067 if (first) {
1068 /* The lo must be on the clp list if there is any
1069 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1070 */
1071 spin_lock(&clp->cl_lock);
1072 BUG_ON(!list_empty(&lo->plh_layouts));
1073 list_add_tail(&lo->plh_layouts, &server->layouts);
1074 spin_unlock(&clp->cl_lock);
1075 }
1076
1077 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1078 if (pg_offset) {
1079 arg.offset -= pg_offset;
1080 arg.length += pg_offset;
1081 }
1082 if (arg.length != NFS4_MAX_UINT64)
1083 arg.length = PAGE_CACHE_ALIGN(arg.length);
1084
1085 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
1086 if (!lseg && first) {
1087 spin_lock(&clp->cl_lock);
1088 list_del_init(&lo->plh_layouts);
1089 spin_unlock(&clp->cl_lock);
1090 }
1091 atomic_dec(&lo->plh_outstanding);
1092 pnfs_put_layout_hdr(lo);
1093 out:
1094 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1095 "(%s, offset: %llu, length: %llu)\n",
1096 __func__, ino->i_sb->s_id,
1097 (unsigned long long)NFS_FILEID(ino),
1098 lseg == NULL ? "not found" : "found",
1099 iomode==IOMODE_RW ? "read/write" : "read-only",
1100 (unsigned long long)pos,
1101 (unsigned long long)count);
1102 return lseg;
1103 out_unlock:
1104 spin_unlock(&ino->i_lock);
1105 goto out;
1106 }
1107 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1108
1109 struct pnfs_layout_segment *
1110 pnfs_layout_process(struct nfs4_layoutget *lgp)
1111 {
1112 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1113 struct nfs4_layoutget_res *res = &lgp->res;
1114 struct pnfs_layout_segment *lseg;
1115 struct inode *ino = lo->plh_inode;
1116 int status = 0;
1117
1118 /* Inject layout blob into I/O device driver */
1119 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1120 if (!lseg || IS_ERR(lseg)) {
1121 if (!lseg)
1122 status = -ENOMEM;
1123 else
1124 status = PTR_ERR(lseg);
1125 dprintk("%s: Could not allocate layout: error %d\n",
1126 __func__, status);
1127 goto out;
1128 }
1129
1130 spin_lock(&ino->i_lock);
1131 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1132 dprintk("%s forget reply due to recall\n", __func__);
1133 goto out_forget_reply;
1134 }
1135
1136 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1137 dprintk("%s forget reply due to state\n", __func__);
1138 goto out_forget_reply;
1139 }
1140 init_lseg(lo, lseg);
1141 lseg->pls_range = res->range;
1142 pnfs_get_lseg(lseg);
1143 pnfs_insert_layout(lo, lseg);
1144
1145 if (res->return_on_close) {
1146 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1147 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1148 }
1149
1150 /* Done processing layoutget. Set the layout stateid */
1151 pnfs_set_layout_stateid(lo, &res->stateid, false);
1152 spin_unlock(&ino->i_lock);
1153 return lseg;
1154 out:
1155 return ERR_PTR(status);
1156
1157 out_forget_reply:
1158 spin_unlock(&ino->i_lock);
1159 lseg->pls_layout = lo;
1160 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1161 goto out;
1162 }
1163
1164 void
1165 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1166 {
1167 BUG_ON(pgio->pg_lseg != NULL);
1168
1169 if (req->wb_offset != req->wb_pgbase) {
1170 nfs_pageio_reset_read_mds(pgio);
1171 return;
1172 }
1173 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1174 req->wb_context,
1175 req_offset(req),
1176 req->wb_bytes,
1177 IOMODE_READ,
1178 GFP_KERNEL);
1179 /* If no lseg, fall back to read through mds */
1180 if (pgio->pg_lseg == NULL)
1181 nfs_pageio_reset_read_mds(pgio);
1182
1183 }
1184 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1185
1186 void
1187 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1188 {
1189 BUG_ON(pgio->pg_lseg != NULL);
1190
1191 if (req->wb_offset != req->wb_pgbase) {
1192 nfs_pageio_reset_write_mds(pgio);
1193 return;
1194 }
1195 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1196 req->wb_context,
1197 req_offset(req),
1198 req->wb_bytes,
1199 IOMODE_RW,
1200 GFP_NOFS);
1201 /* If no lseg, fall back to write through mds */
1202 if (pgio->pg_lseg == NULL)
1203 nfs_pageio_reset_write_mds(pgio);
1204 }
1205 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1206
1207 void
1208 pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1209 const struct nfs_pgio_completion_ops *compl_ops)
1210 {
1211 struct nfs_server *server = NFS_SERVER(inode);
1212 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1213
1214 if (ld == NULL)
1215 nfs_pageio_init_read(pgio, inode, compl_ops);
1216 else
1217 nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0);
1218 }
1219
1220 void
1221 pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1222 int ioflags,
1223 const struct nfs_pgio_completion_ops *compl_ops)
1224 {
1225 struct nfs_server *server = NFS_SERVER(inode);
1226 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1227
1228 if (ld == NULL)
1229 nfs_pageio_init_write(pgio, inode, ioflags, compl_ops);
1230 else
1231 nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags);
1232 }
1233
1234 bool
1235 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1236 struct nfs_page *req)
1237 {
1238 if (pgio->pg_lseg == NULL)
1239 return nfs_generic_pg_test(pgio, prev, req);
1240
1241 /*
1242 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1243 * Note that this test makes several assumptions:
1244 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1245 * is known to lie within the range.
1246 * - that the nfs_page being tested is known to be contiguous with the
1247 * previous nfs_page.
1248 * - Layout ranges are page aligned, so we only have to test the
1249 * start offset of the request.
1250 *
1251 * Please also note that 'end_offset' is actually the offset of the
1252 * first byte that lies outside the pnfs_layout_range. FIXME?
1253 *
1254 */
1255 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1256 pgio->pg_lseg->pls_range.length);
1257 }
1258 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
1259
1260 int pnfs_write_done_resend_to_mds(struct inode *inode,
1261 struct list_head *head,
1262 const struct nfs_pgio_completion_ops *compl_ops)
1263 {
1264 struct nfs_pageio_descriptor pgio;
1265 LIST_HEAD(failed);
1266
1267 /* Resend all requests through the MDS */
1268 nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops);
1269 while (!list_empty(head)) {
1270 struct nfs_page *req = nfs_list_entry(head->next);
1271
1272 nfs_list_remove_request(req);
1273 if (!nfs_pageio_add_request(&pgio, req))
1274 nfs_list_add_request(req, &failed);
1275 }
1276 nfs_pageio_complete(&pgio);
1277
1278 if (!list_empty(&failed)) {
1279 /* For some reason our attempt to resend pages. Mark the
1280 * overall send request as having failed, and let
1281 * nfs_writeback_release_full deal with the error.
1282 */
1283 list_move(&failed, head);
1284 return -EIO;
1285 }
1286 return 0;
1287 }
1288 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
1289
1290 static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
1291 {
1292 struct nfs_pgio_header *hdr = data->header;
1293
1294 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1295 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1296 PNFS_LAYOUTRET_ON_ERROR) {
1297 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1298 pnfs_return_layout(hdr->inode);
1299 }
1300 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1301 data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
1302 &hdr->pages,
1303 hdr->completion_ops);
1304 }
1305
1306 /*
1307 * Called by non rpc-based layout drivers
1308 */
1309 void pnfs_ld_write_done(struct nfs_write_data *data)
1310 {
1311 struct nfs_pgio_header *hdr = data->header;
1312
1313 if (!hdr->pnfs_error) {
1314 pnfs_set_layoutcommit(data);
1315 hdr->mds_ops->rpc_call_done(&data->task, data);
1316 } else
1317 pnfs_ld_handle_write_error(data);
1318 hdr->mds_ops->rpc_release(data);
1319 }
1320 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
1321
1322 static void
1323 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1324 struct nfs_write_data *data)
1325 {
1326 struct nfs_pgio_header *hdr = data->header;
1327
1328 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1329 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1330 nfs_pageio_reset_write_mds(desc);
1331 desc->pg_recoalesce = 1;
1332 }
1333 nfs_writedata_release(data);
1334 }
1335
1336 static enum pnfs_try_status
1337 pnfs_try_to_write_data(struct nfs_write_data *wdata,
1338 const struct rpc_call_ops *call_ops,
1339 struct pnfs_layout_segment *lseg,
1340 int how)
1341 {
1342 struct nfs_pgio_header *hdr = wdata->header;
1343 struct inode *inode = hdr->inode;
1344 enum pnfs_try_status trypnfs;
1345 struct nfs_server *nfss = NFS_SERVER(inode);
1346
1347 hdr->mds_ops = call_ops;
1348
1349 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1350 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1351 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1352 if (trypnfs != PNFS_NOT_ATTEMPTED)
1353 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1354 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1355 return trypnfs;
1356 }
1357
1358 static void
1359 pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1360 {
1361 struct nfs_write_data *data;
1362 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1363 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1364
1365 desc->pg_lseg = NULL;
1366 while (!list_empty(head)) {
1367 enum pnfs_try_status trypnfs;
1368
1369 data = list_first_entry(head, struct nfs_write_data, list);
1370 list_del_init(&data->list);
1371
1372 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1373 if (trypnfs == PNFS_NOT_ATTEMPTED)
1374 pnfs_write_through_mds(desc, data);
1375 }
1376 pnfs_put_lseg(lseg);
1377 }
1378
1379 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1380 {
1381 pnfs_put_lseg(hdr->lseg);
1382 nfs_writehdr_free(hdr);
1383 }
1384 EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
1385
1386 int
1387 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1388 {
1389 struct nfs_write_header *whdr;
1390 struct nfs_pgio_header *hdr;
1391 int ret;
1392
1393 whdr = nfs_writehdr_alloc();
1394 if (!whdr) {
1395 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1396 pnfs_put_lseg(desc->pg_lseg);
1397 desc->pg_lseg = NULL;
1398 return -ENOMEM;
1399 }
1400 hdr = &whdr->header;
1401 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
1402 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
1403 atomic_inc(&hdr->refcnt);
1404 ret = nfs_generic_flush(desc, hdr);
1405 if (ret != 0) {
1406 pnfs_put_lseg(desc->pg_lseg);
1407 desc->pg_lseg = NULL;
1408 } else
1409 pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
1410 if (atomic_dec_and_test(&hdr->refcnt))
1411 hdr->completion_ops->completion(hdr);
1412 return ret;
1413 }
1414 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1415
1416 int pnfs_read_done_resend_to_mds(struct inode *inode,
1417 struct list_head *head,
1418 const struct nfs_pgio_completion_ops *compl_ops)
1419 {
1420 struct nfs_pageio_descriptor pgio;
1421 LIST_HEAD(failed);
1422
1423 /* Resend all requests through the MDS */
1424 nfs_pageio_init_read(&pgio, inode, compl_ops);
1425 while (!list_empty(head)) {
1426 struct nfs_page *req = nfs_list_entry(head->next);
1427
1428 nfs_list_remove_request(req);
1429 if (!nfs_pageio_add_request(&pgio, req))
1430 nfs_list_add_request(req, &failed);
1431 }
1432 nfs_pageio_complete(&pgio);
1433
1434 if (!list_empty(&failed)) {
1435 list_move(&failed, head);
1436 return -EIO;
1437 }
1438 return 0;
1439 }
1440 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1441
1442 static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
1443 {
1444 struct nfs_pgio_header *hdr = data->header;
1445
1446 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1447 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1448 PNFS_LAYOUTRET_ON_ERROR) {
1449 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1450 pnfs_return_layout(hdr->inode);
1451 }
1452 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1453 data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
1454 &hdr->pages,
1455 hdr->completion_ops);
1456 }
1457
1458 /*
1459 * Called by non rpc-based layout drivers
1460 */
1461 void pnfs_ld_read_done(struct nfs_read_data *data)
1462 {
1463 struct nfs_pgio_header *hdr = data->header;
1464
1465 if (likely(!hdr->pnfs_error)) {
1466 __nfs4_read_done_cb(data);
1467 hdr->mds_ops->rpc_call_done(&data->task, data);
1468 } else
1469 pnfs_ld_handle_read_error(data);
1470 hdr->mds_ops->rpc_release(data);
1471 }
1472 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1473
1474 static void
1475 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1476 struct nfs_read_data *data)
1477 {
1478 struct nfs_pgio_header *hdr = data->header;
1479
1480 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1481 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1482 nfs_pageio_reset_read_mds(desc);
1483 desc->pg_recoalesce = 1;
1484 }
1485 nfs_readdata_release(data);
1486 }
1487
1488 /*
1489 * Call the appropriate parallel I/O subsystem read function.
1490 */
1491 static enum pnfs_try_status
1492 pnfs_try_to_read_data(struct nfs_read_data *rdata,
1493 const struct rpc_call_ops *call_ops,
1494 struct pnfs_layout_segment *lseg)
1495 {
1496 struct nfs_pgio_header *hdr = rdata->header;
1497 struct inode *inode = hdr->inode;
1498 struct nfs_server *nfss = NFS_SERVER(inode);
1499 enum pnfs_try_status trypnfs;
1500
1501 hdr->mds_ops = call_ops;
1502
1503 dprintk("%s: Reading ino:%lu %u@%llu\n",
1504 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1505
1506 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1507 if (trypnfs != PNFS_NOT_ATTEMPTED)
1508 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1509 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1510 return trypnfs;
1511 }
1512
1513 static void
1514 pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1515 {
1516 struct nfs_read_data *data;
1517 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1518 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1519
1520 desc->pg_lseg = NULL;
1521 while (!list_empty(head)) {
1522 enum pnfs_try_status trypnfs;
1523
1524 data = list_first_entry(head, struct nfs_read_data, list);
1525 list_del_init(&data->list);
1526
1527 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1528 if (trypnfs == PNFS_NOT_ATTEMPTED)
1529 pnfs_read_through_mds(desc, data);
1530 }
1531 pnfs_put_lseg(lseg);
1532 }
1533
1534 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1535 {
1536 pnfs_put_lseg(hdr->lseg);
1537 nfs_readhdr_free(hdr);
1538 }
1539 EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
1540
1541 int
1542 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1543 {
1544 struct nfs_read_header *rhdr;
1545 struct nfs_pgio_header *hdr;
1546 int ret;
1547
1548 rhdr = nfs_readhdr_alloc();
1549 if (!rhdr) {
1550 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1551 ret = -ENOMEM;
1552 pnfs_put_lseg(desc->pg_lseg);
1553 desc->pg_lseg = NULL;
1554 return ret;
1555 }
1556 hdr = &rhdr->header;
1557 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
1558 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
1559 atomic_inc(&hdr->refcnt);
1560 ret = nfs_generic_pagein(desc, hdr);
1561 if (ret != 0) {
1562 pnfs_put_lseg(desc->pg_lseg);
1563 desc->pg_lseg = NULL;
1564 } else
1565 pnfs_do_multiple_reads(desc, &hdr->rpc_list);
1566 if (atomic_dec_and_test(&hdr->refcnt))
1567 hdr->completion_ops->completion(hdr);
1568 return ret;
1569 }
1570 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1571
1572 /*
1573 * There can be multiple RW segments.
1574 */
1575 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
1576 {
1577 struct pnfs_layout_segment *lseg;
1578
1579 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1580 if (lseg->pls_range.iomode == IOMODE_RW &&
1581 test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1582 list_add(&lseg->pls_lc_list, listp);
1583 }
1584 }
1585
1586 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1587 {
1588 if (lseg->pls_range.iomode == IOMODE_RW) {
1589 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
1590 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
1591 } else {
1592 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
1593 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
1594 }
1595 }
1596 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1597
1598 void
1599 pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1600 {
1601 struct nfs_pgio_header *hdr = wdata->header;
1602 struct inode *inode = hdr->inode;
1603 struct nfs_inode *nfsi = NFS_I(inode);
1604 loff_t end_pos = wdata->mds_offset + wdata->res.count;
1605 bool mark_as_dirty = false;
1606
1607 spin_lock(&inode->i_lock);
1608 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1609 mark_as_dirty = true;
1610 dprintk("%s: Set layoutcommit for inode %lu ",
1611 __func__, inode->i_ino);
1612 }
1613 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
1614 /* references matched in nfs4_layoutcommit_release */
1615 pnfs_get_lseg(hdr->lseg);
1616 }
1617 if (end_pos > nfsi->layout->plh_lwb)
1618 nfsi->layout->plh_lwb = end_pos;
1619 spin_unlock(&inode->i_lock);
1620 dprintk("%s: lseg %p end_pos %llu\n",
1621 __func__, hdr->lseg, nfsi->layout->plh_lwb);
1622
1623 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1624 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1625 if (mark_as_dirty)
1626 mark_inode_dirty_sync(inode);
1627 }
1628 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1629
1630 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1631 {
1632 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1633
1634 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1635 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1636 }
1637
1638 /*
1639 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1640 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1641 * data to disk to allow the server to recover the data if it crashes.
1642 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1643 * is off, and a COMMIT is sent to a data server, or
1644 * if WRITEs to a data server return NFS_DATA_SYNC.
1645 */
1646 int
1647 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
1648 {
1649 struct nfs4_layoutcommit_data *data;
1650 struct nfs_inode *nfsi = NFS_I(inode);
1651 loff_t end_pos;
1652 int status = 0;
1653
1654 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1655
1656 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1657 return 0;
1658
1659 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1660 data = kzalloc(sizeof(*data), GFP_NOFS);
1661 if (!data) {
1662 status = -ENOMEM;
1663 goto out;
1664 }
1665
1666 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1667 goto out_free;
1668
1669 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1670 if (!sync) {
1671 status = -EAGAIN;
1672 goto out_free;
1673 }
1674 status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
1675 nfs_wait_bit_killable, TASK_KILLABLE);
1676 if (status)
1677 goto out_free;
1678 }
1679
1680 INIT_LIST_HEAD(&data->lseg_list);
1681 spin_lock(&inode->i_lock);
1682 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1683 clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
1684 spin_unlock(&inode->i_lock);
1685 wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
1686 goto out_free;
1687 }
1688
1689 pnfs_list_write_lseg(inode, &data->lseg_list);
1690
1691 end_pos = nfsi->layout->plh_lwb;
1692 nfsi->layout->plh_lwb = 0;
1693
1694 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
1695 spin_unlock(&inode->i_lock);
1696
1697 data->args.inode = inode;
1698 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
1699 nfs_fattr_init(&data->fattr);
1700 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1701 data->res.fattr = &data->fattr;
1702 data->args.lastbytewritten = end_pos - 1;
1703 data->res.server = NFS_SERVER(inode);
1704
1705 status = nfs4_proc_layoutcommit(data, sync);
1706 out:
1707 if (status)
1708 mark_inode_dirty_sync(inode);
1709 dprintk("<-- %s status %d\n", __func__, status);
1710 return status;
1711 out_free:
1712 kfree(data);
1713 goto out;
1714 }
1715
1716 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
1717 {
1718 struct nfs4_threshold *thp;
1719
1720 thp = kzalloc(sizeof(*thp), GFP_NOFS);
1721 if (!thp) {
1722 dprintk("%s mdsthreshold allocation failed\n", __func__);
1723 return NULL;
1724 }
1725 return thp;
1726 }
This page took 0.10034 seconds and 6 git commands to generate.