blob: 20436a5e76cd93ca4d191d7ba7fe4c050db61b45 [file] [log] [blame]
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001/*
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>
Andy Adamson974cec82010-10-20 00:18:02 -040031#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040032#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000033#include "iostat.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040034
35#define NFSDBG_FACILITY NFSDBG_PNFS
36
Fred Isaman02c35fc2010-10-20 00:17:59 -040037/* Locking:
38 *
39 * pnfs_spinlock:
40 * protects pnfs_modules_tbl.
41 */
42static DEFINE_SPINLOCK(pnfs_spinlock);
43
44/*
45 * pnfs_modules_tbl holds all pnfs modules
46 */
47static LIST_HEAD(pnfs_modules_tbl);
48
49/* Return the registered pnfs layout driver module matching given id */
50static struct pnfs_layoutdriver_type *
51find_pnfs_driver_locked(u32 id)
52{
53 struct pnfs_layoutdriver_type *local;
54
55 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
56 if (local->id == id)
57 goto out;
58 local = NULL;
59out:
60 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
61 return local;
62}
63
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040064static struct pnfs_layoutdriver_type *
65find_pnfs_driver(u32 id)
66{
Fred Isaman02c35fc2010-10-20 00:17:59 -040067 struct pnfs_layoutdriver_type *local;
68
69 spin_lock(&pnfs_spinlock);
70 local = find_pnfs_driver_locked(id);
71 spin_unlock(&pnfs_spinlock);
72 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073}
74
75void
76unset_pnfs_layoutdriver(struct nfs_server *nfss)
77{
Christoph Hellwigea8eecd2011-03-01 01:34:21 +000078 if (nfss->pnfs_curr_ld)
Fred Isaman02c35fc2010-10-20 00:17:59 -040079 module_put(nfss->pnfs_curr_ld->owner);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040080 nfss->pnfs_curr_ld = NULL;
81}
82
83/*
84 * Try to set the server's pnfs module to the pnfs layout type specified by id.
85 * Currently only one pNFS layout driver per filesystem is supported.
86 *
87 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
88 */
89void
90set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
91{
92 struct pnfs_layoutdriver_type *ld_type = NULL;
93
94 if (id == 0)
95 goto out_no_driver;
96 if (!(server->nfs_client->cl_exchange_flags &
97 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
98 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
99 id, server->nfs_client->cl_exchange_flags);
100 goto out_no_driver;
101 }
102 ld_type = find_pnfs_driver(id);
103 if (!ld_type) {
104 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
105 ld_type = find_pnfs_driver(id);
106 if (!ld_type) {
107 dprintk("%s: No pNFS module found for %u.\n",
108 __func__, id);
109 goto out_no_driver;
110 }
111 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400112 if (!try_module_get(ld_type->owner)) {
113 dprintk("%s: Could not grab reference on module\n", __func__);
114 goto out_no_driver;
115 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400116 server->pnfs_curr_ld = ld_type;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000117
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400118 dprintk("%s: pNFS module for %u set\n", __func__, id);
119 return;
120
121out_no_driver:
122 dprintk("%s: Using NFSv4 I/O\n", __func__);
123 server->pnfs_curr_ld = NULL;
124}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400125
126int
127pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
128{
129 int status = -EINVAL;
130 struct pnfs_layoutdriver_type *tmp;
131
132 if (ld_type->id == 0) {
133 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
134 return status;
135 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400136 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
137 printk(KERN_ERR "%s Layout driver must provide "
138 "alloc_lseg and free_lseg.\n", __func__);
139 return status;
140 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400141
142 spin_lock(&pnfs_spinlock);
143 tmp = find_pnfs_driver_locked(ld_type->id);
144 if (!tmp) {
145 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
146 status = 0;
147 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
148 ld_type->name);
149 } else {
150 printk(KERN_ERR "%s Module with id %d already loaded!\n",
151 __func__, ld_type->id);
152 }
153 spin_unlock(&pnfs_spinlock);
154
155 return status;
156}
157EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
158
159void
160pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
161{
162 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
163 spin_lock(&pnfs_spinlock);
164 list_del(&ld_type->pnfs_tblid);
165 spin_unlock(&pnfs_spinlock);
166}
167EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400168
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400169/*
170 * pNFS client layout cache
171 */
172
Fred Isamancc6e5342011-01-06 11:36:28 +0000173/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000174void
Fred Isamancc6e5342011-01-06 11:36:28 +0000175get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400176{
Fred Isamancc6e5342011-01-06 11:36:28 +0000177 atomic_inc(&lo->plh_refcount);
178}
179
180static void
181destroy_layout_hdr(struct pnfs_layout_hdr *lo)
182{
183 dprintk("%s: freeing layout cache %p\n", __func__, lo);
184 BUG_ON(!list_empty(&lo->plh_layouts));
185 NFS_I(lo->plh_inode)->layout = NULL;
186 kfree(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400187}
188
189static void
190put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
191{
Fred Isamancc6e5342011-01-06 11:36:28 +0000192 if (atomic_dec_and_test(&lo->plh_refcount))
193 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400194}
195
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400196void
Fred Isamancc6e5342011-01-06 11:36:28 +0000197put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400198{
Fred Isamancc6e5342011-01-06 11:36:28 +0000199 struct inode *inode = lo->plh_inode;
200
201 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
202 destroy_layout_hdr(lo);
203 spin_unlock(&inode->i_lock);
204 }
Andy Adamson974cec82010-10-20 00:18:02 -0400205}
206
207static void
208init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
209{
Fred Isaman566052c2011-01-06 11:36:20 +0000210 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000211 atomic_set(&lseg->pls_refcount, 1);
212 smp_mb();
213 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000214 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400215}
216
Fred Isaman4541d162011-01-06 11:36:23 +0000217static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400218{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000219 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400220
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400221 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000222 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000223 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400224}
225
Fred Isamand684d2a2011-03-01 01:34:13 +0000226static void
227put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400228{
Fred Isamand684d2a2011-03-01 01:34:13 +0000229 struct inode *inode = lseg->pls_layout->plh_inode;
230
231 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
232 list_del_init(&lseg->pls_list);
233 if (list_empty(&lseg->pls_layout->plh_segs)) {
234 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
235 /* Matched by initial refcount set in alloc_init_layout_hdr */
236 put_layout_hdr_locked(lseg->pls_layout);
237 }
238 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
239}
240
Fred Isamanbae724e2011-03-01 01:34:15 +0000241void
Fred Isamand684d2a2011-03-01 01:34:13 +0000242put_lseg(struct pnfs_layout_segment *lseg)
243{
244 struct inode *inode;
245
246 if (!lseg)
247 return;
248
Fred Isaman4541d162011-01-06 11:36:23 +0000249 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
250 atomic_read(&lseg->pls_refcount),
251 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000252 inode = lseg->pls_layout->plh_inode;
253 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
254 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400255
Fred Isamand684d2a2011-03-01 01:34:13 +0000256 put_lseg_common(lseg);
257 list_add(&lseg->pls_list, &free_me);
258 spin_unlock(&inode->i_lock);
259 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000260 }
Andy Adamson974cec82010-10-20 00:18:02 -0400261}
Fred Isamane0c2b382011-03-23 13:27:53 +0000262EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400263
Benny Halevyfb3296e2011-05-22 19:47:26 +0300264static inline u64
265end_offset(u64 start, u64 len)
266{
267 u64 end;
268
269 end = start + len;
270 return end >= start ? end : NFS4_MAX_UINT64;
271}
272
273/* last octet in a range */
274static inline u64
275last_byte_offset(u64 start, u64 len)
276{
277 u64 end;
278
279 BUG_ON(!len);
280 end = start + len;
281 return end > start ? end - 1 : NFS4_MAX_UINT64;
282}
283
284/*
285 * is l2 fully contained in l1?
286 * start1 end1
287 * [----------------------------------)
288 * start2 end2
289 * [----------------)
290 */
291static inline int
292lo_seg_contained(struct pnfs_layout_range *l1,
293 struct pnfs_layout_range *l2)
294{
295 u64 start1 = l1->offset;
296 u64 end1 = end_offset(start1, l1->length);
297 u64 start2 = l2->offset;
298 u64 end2 = end_offset(start2, l2->length);
299
300 return (start1 <= start2) && (end1 >= end2);
301}
302
303/*
304 * is l1 and l2 intersecting?
305 * start1 end1
306 * [----------------------------------)
307 * start2 end2
308 * [----------------)
309 */
310static inline int
311lo_seg_intersecting(struct pnfs_layout_range *l1,
312 struct pnfs_layout_range *l2)
313{
314 u64 start1 = l1->offset;
315 u64 end1 = end_offset(start1, l1->length);
316 u64 start2 = l2->offset;
317 u64 end2 = end_offset(start2, l2->length);
318
319 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
320 (end2 == NFS4_MAX_UINT64 || end2 > start1);
321}
322
Fred Isaman4541d162011-01-06 11:36:23 +0000323static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300324should_free_lseg(struct pnfs_layout_range *lseg_range,
325 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000326{
Benny Halevy778b5502011-05-22 19:48:02 +0300327 return (recall_range->iomode == IOMODE_ANY ||
328 lseg_range->iomode == recall_range->iomode) &&
329 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000330}
331
332/* Returns 1 if lseg is removed from list, 0 otherwise */
333static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
334 struct list_head *tmp_list)
335{
336 int rv = 0;
337
338 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
339 /* Remove the reference keeping the lseg in the
340 * list. It will now be removed when all
341 * outstanding io is finished.
342 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000343 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
344 atomic_read(&lseg->pls_refcount));
345 if (atomic_dec_and_test(&lseg->pls_refcount)) {
346 put_lseg_common(lseg);
347 list_add(&lseg->pls_list, tmp_list);
348 rv = 1;
349 }
Fred Isaman4541d162011-01-06 11:36:23 +0000350 }
351 return rv;
352}
353
354/* Returns count of number of matching invalid lsegs remaining in list
355 * after call.
356 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000357int
Fred Isaman4541d162011-01-06 11:36:23 +0000358mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
359 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300360 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400361{
362 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000363 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400364
365 dprintk("%s:Begin lo %p\n", __func__, lo);
366
Fred Isaman38511722011-02-03 18:28:50 +0000367 if (list_empty(&lo->plh_segs)) {
368 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
369 put_layout_hdr_locked(lo);
370 return 0;
371 }
Fred Isaman4541d162011-01-06 11:36:23 +0000372 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300373 if (!recall_range ||
374 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000375 dprintk("%s: freeing lseg %p iomode %d "
376 "offset %llu length %llu\n", __func__,
377 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
378 lseg->pls_range.length);
379 invalid++;
380 removed += mark_lseg_invalid(lseg, tmp_list);
381 }
382 dprintk("%s:Return %i\n", __func__, invalid - removed);
383 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400384}
385
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000386/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000387void
Fred Isaman4541d162011-01-06 11:36:23 +0000388pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400389{
Fred Isaman4541d162011-01-06 11:36:23 +0000390 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000391 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400392
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000393 if (list_empty(free_me))
394 return;
395
396 lo = list_first_entry(free_me, struct pnfs_layout_segment,
397 pls_list)->pls_layout;
398
399 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
400 struct nfs_client *clp;
401
402 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
403 spin_lock(&clp->cl_lock);
404 list_del_init(&lo->plh_layouts);
405 spin_unlock(&clp->cl_lock);
406 }
Fred Isaman4541d162011-01-06 11:36:23 +0000407 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000408 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000409 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400410 }
411}
412
Benny Halevye5e94012010-10-20 00:18:01 -0400413void
414pnfs_destroy_layout(struct nfs_inode *nfsi)
415{
416 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400417 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400418
419 spin_lock(&nfsi->vfs_inode.i_lock);
420 lo = nfsi->layout;
421 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000422 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300423 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400424 }
425 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400426 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400427}
428
Andy Adamson974cec82010-10-20 00:18:02 -0400429/*
430 * Called by the state manger to remove all layouts established under an
431 * expired lease.
432 */
433void
434pnfs_destroy_all_layouts(struct nfs_client *clp)
435{
436 struct pnfs_layout_hdr *lo;
437 LIST_HEAD(tmp_list);
438
439 spin_lock(&clp->cl_lock);
440 list_splice_init(&clp->cl_layouts, &tmp_list);
441 spin_unlock(&clp->cl_lock);
442
443 while (!list_empty(&tmp_list)) {
444 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000445 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400446 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000447 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400448 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000449 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400450 }
451}
452
Fred Isamanfd6002e2011-01-06 11:36:22 +0000453/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000454void
455pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
456 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400457{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000458 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400459
Fred Isamanfd6002e2011-01-06 11:36:22 +0000460 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
461 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000462 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000463 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000464 if (update_barrier) {
465 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
466
467 if ((int)(new_barrier - lo->plh_barrier))
468 lo->plh_barrier = new_barrier;
469 } else {
470 /* Because of wraparound, we want to keep the barrier
471 * "close" to the current seqids. It needs to be
472 * within 2**31 to count as "behind", so if it
473 * gets too near that limit, give us a litle leeway
474 * and bring it to within 2**30.
475 * NOTE - and yes, this is all unsigned arithmetic.
476 */
477 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
478 lo->plh_barrier = newseq - (1 << 30);
479 }
480 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400481}
482
Fred Isamancf7d63f2011-01-06 11:36:25 +0000483/* lget is set to 1 if called from inside send_layoutget call chain */
484static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000485pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
486 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000487{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000488 if ((stateid) &&
489 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
490 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000491 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000492 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000493 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000494 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000495 (atomic_read(&lo->plh_outstanding) > lget));
496}
497
Fred Isamanfd6002e2011-01-06 11:36:22 +0000498int
499pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
500 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400501{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000502 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400503
504 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000505 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000506 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000507 status = -EAGAIN;
508 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000509 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400510
Fred Isamanfd6002e2011-01-06 11:36:22 +0000511 do {
512 seq = read_seqbegin(&open_state->seqlock);
513 memcpy(dst->data, open_state->stateid.data,
514 sizeof(open_state->stateid.data));
515 } while (read_seqretry(&open_state->seqlock, seq));
516 } else
517 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
518 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400519 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000520 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400521}
522
523/*
524* Get layout from server.
525* for now, assume that whole file layouts are requested.
526* arg->offset: 0
527* arg->length: all ones
528*/
Benny Halevye5e94012010-10-20 00:18:01 -0400529static struct pnfs_layout_segment *
530send_layoutget(struct pnfs_layout_hdr *lo,
531 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300532 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400533 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400534{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000535 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400536 struct nfs_server *server = NFS_SERVER(ino);
537 struct nfs4_layoutget *lgp;
538 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400539 struct page **pages = NULL;
540 int i;
541 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400542
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400543 dprintk("--> %s\n", __func__);
544
545 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400546 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000547 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400548 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400549
550 /* allocate pages for xdr post processing */
551 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
552 max_pages = max_resp_sz >> PAGE_SHIFT;
553
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400554 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400555 if (!pages)
556 goto out_err_free;
557
558 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400559 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400560 if (!pages[i])
561 goto out_err_free;
562 }
563
Benny Halevyfb3296e2011-05-22 19:47:26 +0300564 lgp->args.minlength = PAGE_CACHE_SIZE;
565 if (lgp->args.minlength > range->length)
566 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400567 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300568 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400569 lgp->args.type = server->pnfs_curr_ld->id;
570 lgp->args.inode = ino;
571 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400572 lgp->args.layout.pages = pages;
573 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400574 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400575 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400576
577 /* Synchronously retrieve layout information from server and
578 * store in lseg.
579 */
580 nfs4_proc_layoutget(lgp);
581 if (!lseg) {
582 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300583 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400584 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400585
586 /* free xdr pages */
587 for (i = 0; i < max_pages; i++)
588 __free_page(pages[i]);
589 kfree(pages);
590
Andy Adamson974cec82010-10-20 00:18:02 -0400591 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400592
593out_err_free:
594 /* free any allocated xdr pages, lgp as it's not used */
595 if (pages) {
596 for (i = 0; i < max_pages; i++) {
597 if (!pages[i])
598 break;
599 __free_page(pages[i]);
600 }
601 kfree(pages);
602 }
603 kfree(lgp);
604 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400605}
606
Fred Isamanf7e89172011-01-06 11:36:32 +0000607bool pnfs_roc(struct inode *ino)
608{
609 struct pnfs_layout_hdr *lo;
610 struct pnfs_layout_segment *lseg, *tmp;
611 LIST_HEAD(tmp_list);
612 bool found = false;
613
614 spin_lock(&ino->i_lock);
615 lo = NFS_I(ino)->layout;
616 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
617 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
618 goto out_nolayout;
619 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
620 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
621 mark_lseg_invalid(lseg, &tmp_list);
622 found = true;
623 }
624 if (!found)
625 goto out_nolayout;
626 lo->plh_block_lgets++;
627 get_layout_hdr(lo); /* matched in pnfs_roc_release */
628 spin_unlock(&ino->i_lock);
629 pnfs_free_lseg_list(&tmp_list);
630 return true;
631
632out_nolayout:
633 spin_unlock(&ino->i_lock);
634 return false;
635}
636
637void pnfs_roc_release(struct inode *ino)
638{
639 struct pnfs_layout_hdr *lo;
640
641 spin_lock(&ino->i_lock);
642 lo = NFS_I(ino)->layout;
643 lo->plh_block_lgets--;
644 put_layout_hdr_locked(lo);
645 spin_unlock(&ino->i_lock);
646}
647
648void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
649{
650 struct pnfs_layout_hdr *lo;
651
652 spin_lock(&ino->i_lock);
653 lo = NFS_I(ino)->layout;
654 if ((int)(barrier - lo->plh_barrier) > 0)
655 lo->plh_barrier = barrier;
656 spin_unlock(&ino->i_lock);
657}
658
659bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
660{
661 struct nfs_inode *nfsi = NFS_I(ino);
662 struct pnfs_layout_segment *lseg;
663 bool found = false;
664
665 spin_lock(&ino->i_lock);
666 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
667 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
668 found = true;
669 break;
670 }
671 if (!found) {
672 struct pnfs_layout_hdr *lo = nfsi->layout;
673 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
674
675 /* Since close does not return a layout stateid for use as
676 * a barrier, we choose the worst-case barrier.
677 */
678 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
679 }
680 spin_unlock(&ino->i_lock);
681 return found;
682}
683
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400684/*
685 * Compare two layout segments for sorting into layout cache.
686 * We want to preferentially return RW over RO layouts, so ensure those
687 * are seen first.
688 */
689static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300690cmp_layout(struct pnfs_layout_range *l1,
691 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400692{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300693 s64 d;
694
695 /* high offset > low offset */
696 d = l1->offset - l2->offset;
697 if (d)
698 return d;
699
700 /* short length > long length */
701 d = l2->length - l1->length;
702 if (d)
703 return d;
704
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400705 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300706 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400707}
708
Andy Adamson974cec82010-10-20 00:18:02 -0400709static void
710pnfs_insert_layout(struct pnfs_layout_hdr *lo,
711 struct pnfs_layout_segment *lseg)
712{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400713 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400714
Andy Adamson974cec82010-10-20 00:18:02 -0400715 dprintk("%s:Begin\n", __func__);
716
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000717 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000718 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300719 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400720 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000721 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400722 dprintk("%s: inserted lseg %p "
723 "iomode %d offset %llu length %llu before "
724 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000725 __func__, lseg, lseg->pls_range.iomode,
726 lseg->pls_range.offset, lseg->pls_range.length,
727 lp, lp->pls_range.iomode, lp->pls_range.offset,
728 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300729 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400730 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300731 list_add_tail(&lseg->pls_list, &lo->plh_segs);
732 dprintk("%s: inserted lseg %p "
733 "iomode %d offset %llu length %llu at tail\n",
734 __func__, lseg, lseg->pls_range.iomode,
735 lseg->pls_range.offset, lseg->pls_range.length);
736out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000737 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400738
739 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400740}
741
742static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400743alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400744{
745 struct pnfs_layout_hdr *lo;
746
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400747 lo = kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400748 if (!lo)
749 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000750 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000751 INIT_LIST_HEAD(&lo->plh_layouts);
752 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000753 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000754 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400755 return lo;
756}
757
758static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400759pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400760{
761 struct nfs_inode *nfsi = NFS_I(ino);
762 struct pnfs_layout_hdr *new = NULL;
763
764 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
765
766 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000767 if (nfsi->layout) {
768 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
769 return NULL;
770 else
771 return nfsi->layout;
772 }
Benny Halevye5e94012010-10-20 00:18:01 -0400773 spin_unlock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400774 new = alloc_init_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400775 spin_lock(&ino->i_lock);
776
777 if (likely(nfsi->layout == NULL)) /* Won the race? */
778 nfsi->layout = new;
779 else
780 kfree(new);
781 return nfsi->layout;
782}
783
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400784/*
785 * iomode matching rules:
786 * iomode lseg match
787 * ----- ----- -----
788 * ANY READ true
789 * ANY RW true
790 * RW READ false
791 * RW RW true
792 * READ READ true
793 * READ RW true
794 */
795static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300796is_matching_lseg(struct pnfs_layout_range *ls_range,
797 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400798{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300799 struct pnfs_layout_range range1;
800
801 if ((range->iomode == IOMODE_RW &&
802 ls_range->iomode != IOMODE_RW) ||
803 !lo_seg_intersecting(ls_range, range))
804 return 0;
805
806 /* range1 covers only the first byte in the range */
807 range1 = *range;
808 range1.length = 1;
809 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400810}
811
812/*
813 * lookup range in layout
814 */
Benny Halevye5e94012010-10-20 00:18:01 -0400815static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300816pnfs_find_lseg(struct pnfs_layout_hdr *lo,
817 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400818{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400819 struct pnfs_layout_segment *lseg, *ret = NULL;
820
821 dprintk("%s:Begin\n", __func__);
822
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000823 assert_spin_locked(&lo->plh_inode->i_lock);
824 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000825 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300826 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000827 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400828 break;
829 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300830 if (cmp_layout(range, &lseg->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400831 break;
832 }
833
834 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000835 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400836 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400837}
838
839/*
840 * Layout segment is retreived from the server if not cached.
841 * The appropriate layout segment is referenced and returned to the caller.
842 */
843struct pnfs_layout_segment *
844pnfs_update_layout(struct inode *ino,
845 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300846 loff_t pos,
847 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400848 enum pnfs_iomode iomode,
849 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400850{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300851 struct pnfs_layout_range arg = {
852 .iomode = iomode,
853 .offset = pos,
854 .length = count,
855 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300856 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400857 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000858 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400859 struct pnfs_layout_hdr *lo;
860 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000861 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400862
863 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
864 return NULL;
865 spin_lock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400866 lo = pnfs_find_alloc_layout(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400867 if (lo == NULL) {
868 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
869 goto out_unlock;
870 }
871
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000872 /* Do we even need to bother with this? */
873 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
874 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
875 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400876 goto out_unlock;
877 }
878
879 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000880 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400881 goto out_unlock;
882
Andy Adamson568e8c42011-03-01 01:34:22 +0000883 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300884 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000885 if (lseg)
886 goto out_unlock;
887
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000888 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000889 goto out_unlock;
890 atomic_inc(&lo->plh_outstanding);
891
Fred Isamancc6e5342011-01-06 11:36:28 +0000892 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000893 if (list_empty(&lo->plh_segs))
894 first = true;
895 spin_unlock(&ino->i_lock);
896 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000897 /* The lo must be on the clp list if there is any
898 * chance of a CB_LAYOUTRECALL(FILE) coming in.
899 */
900 spin_lock(&clp->cl_lock);
901 BUG_ON(!list_empty(&lo->plh_layouts));
902 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
903 spin_unlock(&clp->cl_lock);
904 }
Benny Halevye5e94012010-10-20 00:18:01 -0400905
Benny Halevy707ed5f2011-05-22 19:47:46 +0300906 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
907 if (pg_offset) {
908 arg.offset -= pg_offset;
909 arg.length += pg_offset;
910 }
911 arg.length = PAGE_CACHE_ALIGN(arg.length);
912
Benny Halevyfb3296e2011-05-22 19:47:26 +0300913 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000914 if (!lseg && first) {
915 spin_lock(&clp->cl_lock);
916 list_del_init(&lo->plh_layouts);
917 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +0000918 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000919 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000920 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400921out:
922 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +0000923 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400924 return lseg;
925out_unlock:
926 spin_unlock(&ino->i_lock);
927 goto out;
928}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400929
930int
931pnfs_layout_process(struct nfs4_layoutget *lgp)
932{
933 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
934 struct nfs4_layoutget_res *res = &lgp->res;
935 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000936 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000937 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400938 int status = 0;
939
940 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400941 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400942 if (!lseg || IS_ERR(lseg)) {
943 if (!lseg)
944 status = -ENOMEM;
945 else
946 status = PTR_ERR(lseg);
947 dprintk("%s: Could not allocate layout: error %d\n",
948 __func__, status);
949 goto out;
950 }
951
952 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000953 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
954 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
955 dprintk("%s forget reply due to recall\n", __func__);
956 goto out_forget_reply;
957 }
958
959 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
960 dprintk("%s forget reply due to state\n", __func__);
961 goto out_forget_reply;
962 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400963 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000964 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +0000965 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400966 pnfs_insert_layout(lo, lseg);
967
Fred Isamanf7e89172011-01-06 11:36:32 +0000968 if (res->return_on_close) {
969 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
970 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
971 }
972
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400973 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000974 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400975 spin_unlock(&ino->i_lock);
976out:
977 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000978
979out_forget_reply:
980 spin_unlock(&ino->i_lock);
981 lseg->pls_layout = lo;
982 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
983 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400984}
985
Fred Isamanbae724e2011-03-01 01:34:15 +0000986static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
987 struct nfs_page *prev,
988 struct nfs_page *req)
989{
990 if (pgio->pg_count == prev->wb_bytes) {
991 /* This is first coelesce call for a series of nfs_pages */
992 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
993 prev->wb_context,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300994 req_offset(req),
995 pgio->pg_count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400996 IOMODE_READ,
997 GFP_KERNEL);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300998 } else if (pgio->pg_lseg &&
999 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1000 pgio->pg_lseg->pls_range.length))
1001 return 0;
Fred Isamanbae724e2011-03-01 01:34:15 +00001002 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
1003}
1004
1005void
1006pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001007{
1008 struct pnfs_layoutdriver_type *ld;
1009
1010 ld = NFS_SERVER(inode)->pnfs_curr_ld;
Fred Isamanbae724e2011-03-01 01:34:15 +00001011 pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001012}
1013
Fred Isaman44b83792011-03-03 15:13:44 +00001014static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
1015 struct nfs_page *prev,
1016 struct nfs_page *req)
1017{
1018 if (pgio->pg_count == prev->wb_bytes) {
1019 /* This is first coelesce call for a series of nfs_pages */
1020 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1021 prev->wb_context,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001022 req_offset(req),
1023 pgio->pg_count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001024 IOMODE_RW,
1025 GFP_NOFS);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001026 } else if (pgio->pg_lseg &&
1027 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1028 pgio->pg_lseg->pls_range.length))
1029 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001030 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
1031}
1032
1033void
1034pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1035{
1036 struct pnfs_layoutdriver_type *ld;
1037
1038 ld = NFS_SERVER(inode)->pnfs_curr_ld;
1039 pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
1040}
1041
Andy Adamson0382b742011-03-03 15:13:45 +00001042enum pnfs_try_status
1043pnfs_try_to_write_data(struct nfs_write_data *wdata,
1044 const struct rpc_call_ops *call_ops, int how)
1045{
1046 struct inode *inode = wdata->inode;
1047 enum pnfs_try_status trypnfs;
1048 struct nfs_server *nfss = NFS_SERVER(inode);
1049
1050 wdata->mds_ops = call_ops;
1051
1052 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1053 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1054
1055 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1056 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1057 put_lseg(wdata->lseg);
1058 wdata->lseg = NULL;
1059 } else
1060 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1061
1062 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1063 return trypnfs;
1064}
1065
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001066/*
Andy Adamson64419a92011-03-01 01:34:16 +00001067 * Call the appropriate parallel I/O subsystem read function.
1068 */
1069enum pnfs_try_status
1070pnfs_try_to_read_data(struct nfs_read_data *rdata,
1071 const struct rpc_call_ops *call_ops)
1072{
1073 struct inode *inode = rdata->inode;
1074 struct nfs_server *nfss = NFS_SERVER(inode);
1075 enum pnfs_try_status trypnfs;
1076
1077 rdata->mds_ops = call_ops;
1078
1079 dprintk("%s: Reading ino:%lu %u@%llu\n",
1080 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1081
1082 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1083 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1084 put_lseg(rdata->lseg);
1085 rdata->lseg = NULL;
1086 } else {
1087 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1088 }
1089 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1090 return trypnfs;
1091}
Andy Adamson863a3c62011-03-23 13:27:54 +00001092
1093/*
1094 * Currently there is only one (whole file) write lseg.
1095 */
1096static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1097{
1098 struct pnfs_layout_segment *lseg, *rv = NULL;
1099
1100 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1101 if (lseg->pls_range.iomode == IOMODE_RW)
1102 rv = lseg;
1103 return rv;
1104}
1105
1106void
1107pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1108{
1109 struct nfs_inode *nfsi = NFS_I(wdata->inode);
1110 loff_t end_pos = wdata->args.offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001111 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001112
1113 spin_lock(&nfsi->vfs_inode.i_lock);
1114 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1115 /* references matched in nfs4_layoutcommit_release */
1116 get_lseg(wdata->lseg);
1117 wdata->lseg->pls_lc_cred =
1118 get_rpccred(wdata->args.context->state->owner->so_cred);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001119 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001120 dprintk("%s: Set layoutcommit for inode %lu ",
1121 __func__, wdata->inode->i_ino);
1122 }
1123 if (end_pos > wdata->lseg->pls_end_pos)
1124 wdata->lseg->pls_end_pos = end_pos;
1125 spin_unlock(&nfsi->vfs_inode.i_lock);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001126
1127 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1128 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1129 if (mark_as_dirty)
1130 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001131}
1132EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1133
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001134/*
1135 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1136 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1137 * data to disk to allow the server to recover the data if it crashes.
1138 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1139 * is off, and a COMMIT is sent to a data server, or
1140 * if WRITEs to a data server return NFS_DATA_SYNC.
1141 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001142int
Andy Adamsonef311532011-03-12 02:58:10 -05001143pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001144{
1145 struct nfs4_layoutcommit_data *data;
1146 struct nfs_inode *nfsi = NFS_I(inode);
1147 struct pnfs_layout_segment *lseg;
1148 struct rpc_cred *cred;
1149 loff_t end_pos;
1150 int status = 0;
1151
1152 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1153
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001154 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1155 return 0;
1156
Andy Adamson863a3c62011-03-23 13:27:54 +00001157 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1158 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001159 if (!data) {
1160 mark_inode_dirty_sync(inode);
1161 status = -ENOMEM;
1162 goto out;
1163 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001164
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001165 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001166 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1167 spin_unlock(&inode->i_lock);
1168 kfree(data);
1169 goto out;
1170 }
1171 /*
1172 * Currently only one (whole file) write lseg which is referenced
1173 * in pnfs_set_layoutcommit and will be found.
1174 */
1175 lseg = pnfs_list_write_lseg(inode);
1176
1177 end_pos = lseg->pls_end_pos;
1178 cred = lseg->pls_lc_cred;
1179 lseg->pls_end_pos = 0;
1180 lseg->pls_lc_cred = NULL;
1181
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001182 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1183 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001184 spin_unlock(&inode->i_lock);
1185
1186 data->args.inode = inode;
1187 data->lseg = lseg;
1188 data->cred = cred;
1189 nfs_fattr_init(&data->fattr);
1190 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1191 data->res.fattr = &data->fattr;
1192 data->args.lastbytewritten = end_pos - 1;
1193 data->res.server = NFS_SERVER(inode);
1194
1195 status = nfs4_proc_layoutcommit(data, sync);
1196out:
1197 dprintk("<-- %s status %d\n", __func__, status);
1198 return status;
1199}