blob: baa2a042c6c432fdbda82f5cdd925025070906a5 [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
Benny Halevy636fb9c2011-05-22 19:51:33 +0300180static struct pnfs_layout_hdr *
181pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
182{
183 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
184 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
185 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
186}
187
188static void
189pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
190{
191 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
Peng Taof45c1d42011-07-30 20:52:32 -0400192 put_rpccred(lo->plh_lc_cred);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300193 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
194}
195
Fred Isamancc6e5342011-01-06 11:36:28 +0000196static void
197destroy_layout_hdr(struct pnfs_layout_hdr *lo)
198{
199 dprintk("%s: freeing layout cache %p\n", __func__, lo);
200 BUG_ON(!list_empty(&lo->plh_layouts));
201 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300202 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400203}
204
205static void
206put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
207{
Fred Isamancc6e5342011-01-06 11:36:28 +0000208 if (atomic_dec_and_test(&lo->plh_refcount))
209 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400210}
211
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400212void
Fred Isamancc6e5342011-01-06 11:36:28 +0000213put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400214{
Fred Isamancc6e5342011-01-06 11:36:28 +0000215 struct inode *inode = lo->plh_inode;
216
217 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
218 destroy_layout_hdr(lo);
219 spin_unlock(&inode->i_lock);
220 }
Andy Adamson974cec82010-10-20 00:18:02 -0400221}
222
223static void
224init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
225{
Fred Isaman566052c2011-01-06 11:36:20 +0000226 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000227 atomic_set(&lseg->pls_refcount, 1);
228 smp_mb();
229 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000230 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400231}
232
Fred Isaman4541d162011-01-06 11:36:23 +0000233static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400234{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000235 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400236
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400237 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000238 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000239 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400240}
241
Fred Isamand684d2a2011-03-01 01:34:13 +0000242static void
243put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400244{
Fred Isamand684d2a2011-03-01 01:34:13 +0000245 struct inode *inode = lseg->pls_layout->plh_inode;
246
Benny Halevyd20581a2011-05-22 19:52:03 +0300247 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000248 list_del_init(&lseg->pls_list);
249 if (list_empty(&lseg->pls_layout->plh_segs)) {
250 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
251 /* Matched by initial refcount set in alloc_init_layout_hdr */
252 put_layout_hdr_locked(lseg->pls_layout);
253 }
254 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
255}
256
Fred Isamanbae724e2011-03-01 01:34:15 +0000257void
Fred Isamand684d2a2011-03-01 01:34:13 +0000258put_lseg(struct pnfs_layout_segment *lseg)
259{
260 struct inode *inode;
261
262 if (!lseg)
263 return;
264
Fred Isaman4541d162011-01-06 11:36:23 +0000265 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
266 atomic_read(&lseg->pls_refcount),
267 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000268 inode = lseg->pls_layout->plh_inode;
269 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
270 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400271
Fred Isamand684d2a2011-03-01 01:34:13 +0000272 put_lseg_common(lseg);
273 list_add(&lseg->pls_list, &free_me);
274 spin_unlock(&inode->i_lock);
275 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000276 }
Andy Adamson974cec82010-10-20 00:18:02 -0400277}
Fred Isamane0c2b382011-03-23 13:27:53 +0000278EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400279
Benny Halevyfb3296e2011-05-22 19:47:26 +0300280static inline u64
281end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000282{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300283 u64 end;
284
285 end = start + len;
286 return end >= start ? end : NFS4_MAX_UINT64;
287}
288
289/* last octet in a range */
290static inline u64
291last_byte_offset(u64 start, u64 len)
292{
293 u64 end;
294
295 BUG_ON(!len);
296 end = start + len;
297 return end > start ? end - 1 : NFS4_MAX_UINT64;
298}
299
300/*
301 * is l2 fully contained in l1?
302 * start1 end1
303 * [----------------------------------)
304 * start2 end2
305 * [----------------)
306 */
307static inline int
308lo_seg_contained(struct pnfs_layout_range *l1,
309 struct pnfs_layout_range *l2)
310{
311 u64 start1 = l1->offset;
312 u64 end1 = end_offset(start1, l1->length);
313 u64 start2 = l2->offset;
314 u64 end2 = end_offset(start2, l2->length);
315
316 return (start1 <= start2) && (end1 >= end2);
317}
318
319/*
320 * is l1 and l2 intersecting?
321 * start1 end1
322 * [----------------------------------)
323 * start2 end2
324 * [----------------)
325 */
326static inline int
327lo_seg_intersecting(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 (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
336 (end2 == NFS4_MAX_UINT64 || end2 > start1);
337}
338
Fred Isaman4541d162011-01-06 11:36:23 +0000339static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300340should_free_lseg(struct pnfs_layout_range *lseg_range,
341 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000342{
Benny Halevy778b5502011-05-22 19:48:02 +0300343 return (recall_range->iomode == IOMODE_ANY ||
344 lseg_range->iomode == recall_range->iomode) &&
345 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000346}
347
348/* Returns 1 if lseg is removed from list, 0 otherwise */
349static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
350 struct list_head *tmp_list)
351{
352 int rv = 0;
353
354 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
355 /* Remove the reference keeping the lseg in the
356 * list. It will now be removed when all
357 * outstanding io is finished.
358 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000359 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
360 atomic_read(&lseg->pls_refcount));
361 if (atomic_dec_and_test(&lseg->pls_refcount)) {
362 put_lseg_common(lseg);
363 list_add(&lseg->pls_list, tmp_list);
364 rv = 1;
365 }
Fred Isaman4541d162011-01-06 11:36:23 +0000366 }
367 return rv;
368}
369
370/* Returns count of number of matching invalid lsegs remaining in list
371 * after call.
372 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000373int
Fred Isaman4541d162011-01-06 11:36:23 +0000374mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
375 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300376 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400377{
378 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000379 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400380
381 dprintk("%s:Begin lo %p\n", __func__, lo);
382
Fred Isaman38511722011-02-03 18:28:50 +0000383 if (list_empty(&lo->plh_segs)) {
384 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
385 put_layout_hdr_locked(lo);
386 return 0;
387 }
Fred Isaman4541d162011-01-06 11:36:23 +0000388 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300389 if (!recall_range ||
390 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000391 dprintk("%s: freeing lseg %p iomode %d "
392 "offset %llu length %llu\n", __func__,
393 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
394 lseg->pls_range.length);
395 invalid++;
396 removed += mark_lseg_invalid(lseg, tmp_list);
397 }
398 dprintk("%s:Return %i\n", __func__, invalid - removed);
399 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400400}
401
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000402/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000403void
Fred Isaman4541d162011-01-06 11:36:23 +0000404pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400405{
Fred Isaman4541d162011-01-06 11:36:23 +0000406 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000407 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400408
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000409 if (list_empty(free_me))
410 return;
411
412 lo = list_first_entry(free_me, struct pnfs_layout_segment,
413 pls_list)->pls_layout;
414
415 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
416 struct nfs_client *clp;
417
418 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
419 spin_lock(&clp->cl_lock);
420 list_del_init(&lo->plh_layouts);
421 spin_unlock(&clp->cl_lock);
422 }
Fred Isaman4541d162011-01-06 11:36:23 +0000423 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000424 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000425 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400426 }
427}
428
Benny Halevye5e94012010-10-20 00:18:01 -0400429void
430pnfs_destroy_layout(struct nfs_inode *nfsi)
431{
432 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400433 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400434
435 spin_lock(&nfsi->vfs_inode.i_lock);
436 lo = nfsi->layout;
437 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000438 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300439 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400440 }
441 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400442 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400443}
444
Andy Adamson974cec82010-10-20 00:18:02 -0400445/*
446 * Called by the state manger to remove all layouts established under an
447 * expired lease.
448 */
449void
450pnfs_destroy_all_layouts(struct nfs_client *clp)
451{
452 struct pnfs_layout_hdr *lo;
453 LIST_HEAD(tmp_list);
454
455 spin_lock(&clp->cl_lock);
456 list_splice_init(&clp->cl_layouts, &tmp_list);
457 spin_unlock(&clp->cl_lock);
458
459 while (!list_empty(&tmp_list)) {
460 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000461 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400462 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000463 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400464 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000465 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400466 }
467}
468
Fred Isamanfd6002e2011-01-06 11:36:22 +0000469/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000470void
471pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
472 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400473{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000474 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400475
Fred Isamanfd6002e2011-01-06 11:36:22 +0000476 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
477 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000478 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000479 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000480 if (update_barrier) {
481 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
482
483 if ((int)(new_barrier - lo->plh_barrier))
484 lo->plh_barrier = new_barrier;
485 } else {
486 /* Because of wraparound, we want to keep the barrier
487 * "close" to the current seqids. It needs to be
488 * within 2**31 to count as "behind", so if it
489 * gets too near that limit, give us a litle leeway
490 * and bring it to within 2**30.
491 * NOTE - and yes, this is all unsigned arithmetic.
492 */
493 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
494 lo->plh_barrier = newseq - (1 << 30);
495 }
496 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400497}
498
Fred Isamancf7d63f2011-01-06 11:36:25 +0000499/* lget is set to 1 if called from inside send_layoutget call chain */
500static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000501pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
502 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000503{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000504 if ((stateid) &&
505 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
506 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000507 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000508 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000509 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000510 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000511 (atomic_read(&lo->plh_outstanding) > lget));
512}
513
Fred Isamanfd6002e2011-01-06 11:36:22 +0000514int
515pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
516 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400517{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000518 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400519
520 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000521 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000522 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000523 status = -EAGAIN;
524 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000525 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400526
Fred Isamanfd6002e2011-01-06 11:36:22 +0000527 do {
528 seq = read_seqbegin(&open_state->seqlock);
529 memcpy(dst->data, open_state->stateid.data,
530 sizeof(open_state->stateid.data));
531 } while (read_seqretry(&open_state->seqlock, seq));
532 } else
533 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
534 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400535 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000536 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400537}
538
539/*
540* Get layout from server.
541* for now, assume that whole file layouts are requested.
542* arg->offset: 0
543* arg->length: all ones
544*/
Benny Halevye5e94012010-10-20 00:18:01 -0400545static struct pnfs_layout_segment *
546send_layoutget(struct pnfs_layout_hdr *lo,
547 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300548 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400549 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400550{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000551 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400552 struct nfs_server *server = NFS_SERVER(ino);
553 struct nfs4_layoutget *lgp;
554 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400555 struct page **pages = NULL;
556 int i;
557 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400558
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400559 dprintk("--> %s\n", __func__);
560
561 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400562 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000563 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400564 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400565
566 /* allocate pages for xdr post processing */
567 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
568 max_pages = max_resp_sz >> PAGE_SHIFT;
569
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400570 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400571 if (!pages)
572 goto out_err_free;
573
574 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400575 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400576 if (!pages[i])
577 goto out_err_free;
578 }
579
Benny Halevyfb3296e2011-05-22 19:47:26 +0300580 lgp->args.minlength = PAGE_CACHE_SIZE;
581 if (lgp->args.minlength > range->length)
582 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400583 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300584 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400585 lgp->args.type = server->pnfs_curr_ld->id;
586 lgp->args.inode = ino;
587 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400588 lgp->args.layout.pages = pages;
589 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400590 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400591 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400592
593 /* Synchronously retrieve layout information from server and
594 * store in lseg.
595 */
596 nfs4_proc_layoutget(lgp);
597 if (!lseg) {
598 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300599 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400600 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400601
602 /* free xdr pages */
603 for (i = 0; i < max_pages; i++)
604 __free_page(pages[i]);
605 kfree(pages);
606
Andy Adamson974cec82010-10-20 00:18:02 -0400607 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400608
609out_err_free:
610 /* free any allocated xdr pages, lgp as it's not used */
611 if (pages) {
612 for (i = 0; i < max_pages; i++) {
613 if (!pages[i])
614 break;
615 __free_page(pages[i]);
616 }
617 kfree(pages);
618 }
619 kfree(lgp);
620 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400621}
622
Benny Halevycbe82602011-05-22 19:52:37 +0300623/* Initiates a LAYOUTRETURN(FILE) */
624int
625_pnfs_return_layout(struct inode *ino)
626{
627 struct pnfs_layout_hdr *lo = NULL;
628 struct nfs_inode *nfsi = NFS_I(ino);
629 LIST_HEAD(tmp_list);
630 struct nfs4_layoutreturn *lrp;
631 nfs4_stateid stateid;
632 int status = 0;
633
634 dprintk("--> %s\n", __func__);
635
636 spin_lock(&ino->i_lock);
637 lo = nfsi->layout;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400638 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300639 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400640 dprintk("%s: no layout to return\n", __func__);
641 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300642 }
643 stateid = nfsi->layout->plh_stateid;
644 /* Reference matched in nfs4_layoutreturn_release */
645 get_layout_hdr(lo);
Fred Isamanea0ded72011-06-15 12:31:02 -0400646 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
647 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +0300648 spin_unlock(&ino->i_lock);
649 pnfs_free_lseg_list(&tmp_list);
650
651 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
652
653 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
654 if (unlikely(lrp == NULL)) {
655 status = -ENOMEM;
Fred Isaman9e2dfdb2011-06-15 14:32:02 -0400656 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
657 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
Benny Halevy1ed3a852011-06-15 11:39:57 -0400658 put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300659 goto out;
660 }
661
662 lrp->args.stateid = stateid;
663 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
664 lrp->args.inode = ino;
665 lrp->clp = NFS_SERVER(ino)->nfs_client;
666
667 status = nfs4_proc_layoutreturn(lrp);
668out:
669 dprintk("<-- %s status: %d\n", __func__, status);
670 return status;
671}
672
Fred Isamanf7e89172011-01-06 11:36:32 +0000673bool pnfs_roc(struct inode *ino)
674{
675 struct pnfs_layout_hdr *lo;
676 struct pnfs_layout_segment *lseg, *tmp;
677 LIST_HEAD(tmp_list);
678 bool found = false;
679
680 spin_lock(&ino->i_lock);
681 lo = NFS_I(ino)->layout;
682 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
683 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
684 goto out_nolayout;
685 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
686 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
687 mark_lseg_invalid(lseg, &tmp_list);
688 found = true;
689 }
690 if (!found)
691 goto out_nolayout;
692 lo->plh_block_lgets++;
693 get_layout_hdr(lo); /* matched in pnfs_roc_release */
694 spin_unlock(&ino->i_lock);
695 pnfs_free_lseg_list(&tmp_list);
696 return true;
697
698out_nolayout:
699 spin_unlock(&ino->i_lock);
700 return false;
701}
702
703void pnfs_roc_release(struct inode *ino)
704{
705 struct pnfs_layout_hdr *lo;
706
707 spin_lock(&ino->i_lock);
708 lo = NFS_I(ino)->layout;
709 lo->plh_block_lgets--;
710 put_layout_hdr_locked(lo);
711 spin_unlock(&ino->i_lock);
712}
713
714void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
715{
716 struct pnfs_layout_hdr *lo;
717
718 spin_lock(&ino->i_lock);
719 lo = NFS_I(ino)->layout;
720 if ((int)(barrier - lo->plh_barrier) > 0)
721 lo->plh_barrier = barrier;
722 spin_unlock(&ino->i_lock);
723}
724
725bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
726{
727 struct nfs_inode *nfsi = NFS_I(ino);
728 struct pnfs_layout_segment *lseg;
729 bool found = false;
730
731 spin_lock(&ino->i_lock);
732 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
733 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
734 found = true;
735 break;
736 }
737 if (!found) {
738 struct pnfs_layout_hdr *lo = nfsi->layout;
739 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
740
741 /* Since close does not return a layout stateid for use as
742 * a barrier, we choose the worst-case barrier.
743 */
744 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
745 }
746 spin_unlock(&ino->i_lock);
747 return found;
748}
749
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400750/*
751 * Compare two layout segments for sorting into layout cache.
752 * We want to preferentially return RW over RO layouts, so ensure those
753 * are seen first.
754 */
755static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300756cmp_layout(struct pnfs_layout_range *l1,
757 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400758{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300759 s64 d;
760
761 /* high offset > low offset */
762 d = l1->offset - l2->offset;
763 if (d)
764 return d;
765
766 /* short length > long length */
767 d = l2->length - l1->length;
768 if (d)
769 return d;
770
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400771 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300772 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400773}
774
Andy Adamson974cec82010-10-20 00:18:02 -0400775static void
776pnfs_insert_layout(struct pnfs_layout_hdr *lo,
777 struct pnfs_layout_segment *lseg)
778{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400779 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400780
Andy Adamson974cec82010-10-20 00:18:02 -0400781 dprintk("%s:Begin\n", __func__);
782
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000783 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000784 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300785 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400786 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000787 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400788 dprintk("%s: inserted lseg %p "
789 "iomode %d offset %llu length %llu before "
790 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000791 __func__, lseg, lseg->pls_range.iomode,
792 lseg->pls_range.offset, lseg->pls_range.length,
793 lp, lp->pls_range.iomode, lp->pls_range.offset,
794 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300795 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400796 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300797 list_add_tail(&lseg->pls_list, &lo->plh_segs);
798 dprintk("%s: inserted lseg %p "
799 "iomode %d offset %llu length %llu at tail\n",
800 __func__, lseg, lseg->pls_range.iomode,
801 lseg->pls_range.offset, lseg->pls_range.length);
802out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000803 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400804
805 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400806}
807
808static struct pnfs_layout_hdr *
Peng Taof45c1d42011-07-30 20:52:32 -0400809alloc_init_layout_hdr(struct inode *ino,
810 struct nfs_open_context *ctx,
811 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400812{
813 struct pnfs_layout_hdr *lo;
814
Benny Halevy636fb9c2011-05-22 19:51:33 +0300815 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400816 if (!lo)
817 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000818 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000819 INIT_LIST_HEAD(&lo->plh_layouts);
820 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000821 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000822 lo->plh_inode = ino;
Peng Taof45c1d42011-07-30 20:52:32 -0400823 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
Benny Halevye5e94012010-10-20 00:18:01 -0400824 return lo;
825}
826
827static struct pnfs_layout_hdr *
Peng Taof45c1d42011-07-30 20:52:32 -0400828pnfs_find_alloc_layout(struct inode *ino,
829 struct nfs_open_context *ctx,
830 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400831{
832 struct nfs_inode *nfsi = NFS_I(ino);
833 struct pnfs_layout_hdr *new = NULL;
834
835 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
836
837 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000838 if (nfsi->layout) {
839 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
840 return NULL;
841 else
842 return nfsi->layout;
843 }
Benny Halevye5e94012010-10-20 00:18:01 -0400844 spin_unlock(&ino->i_lock);
Peng Taof45c1d42011-07-30 20:52:32 -0400845 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400846 spin_lock(&ino->i_lock);
847
848 if (likely(nfsi->layout == NULL)) /* Won the race? */
849 nfsi->layout = new;
850 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300851 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400852 return nfsi->layout;
853}
854
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400855/*
856 * iomode matching rules:
857 * iomode lseg match
858 * ----- ----- -----
859 * ANY READ true
860 * ANY RW true
861 * RW READ false
862 * RW RW true
863 * READ READ true
864 * READ RW true
865 */
866static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300867is_matching_lseg(struct pnfs_layout_range *ls_range,
868 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400869{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300870 struct pnfs_layout_range range1;
871
872 if ((range->iomode == IOMODE_RW &&
873 ls_range->iomode != IOMODE_RW) ||
874 !lo_seg_intersecting(ls_range, range))
875 return 0;
876
877 /* range1 covers only the first byte in the range */
878 range1 = *range;
879 range1.length = 1;
880 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400881}
882
883/*
884 * lookup range in layout
885 */
Benny Halevye5e94012010-10-20 00:18:01 -0400886static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300887pnfs_find_lseg(struct pnfs_layout_hdr *lo,
888 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400889{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400890 struct pnfs_layout_segment *lseg, *ret = NULL;
891
892 dprintk("%s:Begin\n", __func__);
893
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000894 assert_spin_locked(&lo->plh_inode->i_lock);
895 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000896 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300897 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000898 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400899 break;
900 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400901 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400902 break;
903 }
904
905 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000906 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400907 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400908}
909
910/*
911 * Layout segment is retreived from the server if not cached.
912 * The appropriate layout segment is referenced and returned to the caller.
913 */
914struct pnfs_layout_segment *
915pnfs_update_layout(struct inode *ino,
916 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300917 loff_t pos,
918 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400919 enum pnfs_iomode iomode,
920 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400921{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300922 struct pnfs_layout_range arg = {
923 .iomode = iomode,
924 .offset = pos,
925 .length = count,
926 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300927 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400928 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000929 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400930 struct pnfs_layout_hdr *lo;
931 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000932 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400933
934 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
935 return NULL;
936 spin_lock(&ino->i_lock);
Peng Taof45c1d42011-07-30 20:52:32 -0400937 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400938 if (lo == NULL) {
939 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
940 goto out_unlock;
941 }
942
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000943 /* Do we even need to bother with this? */
944 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
945 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
946 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400947 goto out_unlock;
948 }
949
950 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000951 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400952 goto out_unlock;
953
Andy Adamson568e8c42011-03-01 01:34:22 +0000954 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300955 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000956 if (lseg)
957 goto out_unlock;
958
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000959 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000960 goto out_unlock;
961 atomic_inc(&lo->plh_outstanding);
962
Fred Isamancc6e5342011-01-06 11:36:28 +0000963 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000964 if (list_empty(&lo->plh_segs))
965 first = true;
966 spin_unlock(&ino->i_lock);
967 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000968 /* The lo must be on the clp list if there is any
969 * chance of a CB_LAYOUTRECALL(FILE) coming in.
970 */
971 spin_lock(&clp->cl_lock);
972 BUG_ON(!list_empty(&lo->plh_layouts));
973 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
974 spin_unlock(&clp->cl_lock);
975 }
Benny Halevye5e94012010-10-20 00:18:01 -0400976
Benny Halevy707ed5f2011-05-22 19:47:46 +0300977 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
978 if (pg_offset) {
979 arg.offset -= pg_offset;
980 arg.length += pg_offset;
981 }
982 arg.length = PAGE_CACHE_ALIGN(arg.length);
983
Benny Halevyfb3296e2011-05-22 19:47:26 +0300984 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000985 if (!lseg && first) {
986 spin_lock(&clp->cl_lock);
987 list_del_init(&lo->plh_layouts);
988 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +0000989 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000990 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000991 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400992out:
993 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +0000994 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400995 return lseg;
996out_unlock:
997 spin_unlock(&ino->i_lock);
998 goto out;
999}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001000
1001int
1002pnfs_layout_process(struct nfs4_layoutget *lgp)
1003{
1004 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1005 struct nfs4_layoutget_res *res = &lgp->res;
1006 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001007 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001008 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001009 int status = 0;
1010
1011 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001012 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001013 if (!lseg || IS_ERR(lseg)) {
1014 if (!lseg)
1015 status = -ENOMEM;
1016 else
1017 status = PTR_ERR(lseg);
1018 dprintk("%s: Could not allocate layout: error %d\n",
1019 __func__, status);
1020 goto out;
1021 }
1022
1023 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001024 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1025 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1026 dprintk("%s forget reply due to recall\n", __func__);
1027 goto out_forget_reply;
1028 }
1029
1030 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1031 dprintk("%s forget reply due to state\n", __func__);
1032 goto out_forget_reply;
1033 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001034 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001035 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001036 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001037 pnfs_insert_layout(lo, lseg);
1038
Fred Isamanf7e89172011-01-06 11:36:32 +00001039 if (res->return_on_close) {
1040 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1041 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1042 }
1043
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001044 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001045 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001046 spin_unlock(&ino->i_lock);
1047out:
1048 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001049
1050out_forget_reply:
1051 spin_unlock(&ino->i_lock);
1052 lseg->pls_layout = lo;
1053 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1054 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001055}
1056
Benny Halevy18ad0a92011-05-25 21:03:56 +03001057bool
Benny Halevydfed2062011-05-25 20:25:22 +03001058pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1059 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001060{
Benny Halevydfed2062011-05-25 20:25:22 +03001061 enum pnfs_iomode access_type;
1062 gfp_t gfp_flags;
1063
1064 /* We assume that pg_ioflags == 0 iff we're reading a page */
1065 if (pgio->pg_ioflags == 0) {
1066 access_type = IOMODE_READ;
1067 gfp_flags = GFP_KERNEL;
1068 } else {
1069 access_type = IOMODE_RW;
1070 gfp_flags = GFP_NOFS;
1071 }
1072
Trond Myklebust8f7d5ef2011-06-10 13:30:22 -04001073 if (pgio->pg_lseg == NULL) {
1074 if (pgio->pg_count != prev->wb_bytes)
1075 return true;
Fred Isamanbae724e2011-03-01 01:34:15 +00001076 /* This is first coelesce call for a series of nfs_pages */
1077 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1078 prev->wb_context,
Trond Myklebust8f7d5ef2011-06-10 13:30:22 -04001079 req_offset(prev),
Benny Halevyfb3296e2011-05-22 19:47:26 +03001080 pgio->pg_count,
Benny Halevydfed2062011-05-25 20:25:22 +03001081 access_type,
1082 gfp_flags);
Trond Myklebust8f7d5ef2011-06-10 13:30:22 -04001083 if (pgio->pg_lseg == NULL)
1084 return true;
Fred Isamanbae724e2011-03-01 01:34:15 +00001085 }
Benny Halevy89a58e32011-05-25 20:54:40 +03001086
Trond Myklebust19982ba2011-06-10 13:30:23 -04001087 /*
1088 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1089 * Note that this test makes several assumptions:
1090 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1091 * is known to lie within the range.
1092 * - that the nfs_page being tested is known to be contiguous with the
1093 * previous nfs_page.
1094 * - Layout ranges are page aligned, so we only have to test the
1095 * start offset of the request.
1096 *
1097 * Please also note that 'end_offset' is actually the offset of the
1098 * first byte that lies outside the pnfs_layout_range. FIXME?
1099 *
1100 */
1101 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1102 pgio->pg_lseg->pls_range.length);
Fred Isamanbae724e2011-03-01 01:34:15 +00001103}
Benny Halevy89a58e32011-05-25 20:54:40 +03001104EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001105
Benny Halevyd20581a2011-05-22 19:52:03 +03001106/*
1107 * Called by non rpc-based layout drivers
1108 */
1109int
1110pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001111{
Benny Halevyd20581a2011-05-22 19:52:03 +03001112 int status;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001113
Benny Halevyd20581a2011-05-22 19:52:03 +03001114 if (!data->pnfs_error) {
1115 pnfs_set_layoutcommit(data);
1116 data->mds_ops->rpc_call_done(&data->task, data);
1117 data->mds_ops->rpc_release(data);
1118 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001119 }
Fred Isaman44b83792011-03-03 15:13:44 +00001120
Benny Halevyd20581a2011-05-22 19:52:03 +03001121 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1122 data->pnfs_error);
1123 status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
1124 data->mds_ops, NFS_FILE_SYNC);
1125 return status ? : -EAGAIN;
Fred Isaman44b83792011-03-03 15:13:44 +00001126}
Benny Halevyd20581a2011-05-22 19:52:03 +03001127EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001128
Andy Adamson0382b742011-03-03 15:13:45 +00001129enum pnfs_try_status
1130pnfs_try_to_write_data(struct nfs_write_data *wdata,
1131 const struct rpc_call_ops *call_ops, int how)
1132{
1133 struct inode *inode = wdata->inode;
1134 enum pnfs_try_status trypnfs;
1135 struct nfs_server *nfss = NFS_SERVER(inode);
1136
1137 wdata->mds_ops = call_ops;
1138
1139 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1140 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1141
1142 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1143 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1144 put_lseg(wdata->lseg);
1145 wdata->lseg = NULL;
1146 } else
1147 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1148
1149 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1150 return trypnfs;
1151}
1152
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001153/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001154 * Called by non rpc-based layout drivers
1155 */
1156int
1157pnfs_ld_read_done(struct nfs_read_data *data)
1158{
1159 int status;
1160
1161 if (!data->pnfs_error) {
1162 __nfs4_read_done_cb(data);
1163 data->mds_ops->rpc_call_done(&data->task, data);
1164 data->mds_ops->rpc_release(data);
1165 return 0;
1166 }
1167
1168 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1169 data->pnfs_error);
1170 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1171 data->mds_ops);
1172 return status ? : -EAGAIN;
1173}
1174EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1175
1176/*
Andy Adamson64419a92011-03-01 01:34:16 +00001177 * Call the appropriate parallel I/O subsystem read function.
1178 */
1179enum pnfs_try_status
1180pnfs_try_to_read_data(struct nfs_read_data *rdata,
1181 const struct rpc_call_ops *call_ops)
1182{
1183 struct inode *inode = rdata->inode;
1184 struct nfs_server *nfss = NFS_SERVER(inode);
1185 enum pnfs_try_status trypnfs;
1186
1187 rdata->mds_ops = call_ops;
1188
1189 dprintk("%s: Reading ino:%lu %u@%llu\n",
1190 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1191
1192 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1193 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1194 put_lseg(rdata->lseg);
1195 rdata->lseg = NULL;
1196 } else {
1197 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1198 }
1199 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1200 return trypnfs;
1201}
Andy Adamson863a3c62011-03-23 13:27:54 +00001202
1203/*
1204 * Currently there is only one (whole file) write lseg.
1205 */
1206static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1207{
1208 struct pnfs_layout_segment *lseg, *rv = NULL;
1209
1210 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1211 if (lseg->pls_range.iomode == IOMODE_RW)
1212 rv = lseg;
1213 return rv;
1214}
1215
1216void
1217pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1218{
1219 struct nfs_inode *nfsi = NFS_I(wdata->inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001220 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001221 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001222
1223 spin_lock(&nfsi->vfs_inode.i_lock);
1224 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1225 /* references matched in nfs4_layoutcommit_release */
1226 get_lseg(wdata->lseg);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001227 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001228 dprintk("%s: Set layoutcommit for inode %lu ",
1229 __func__, wdata->inode->i_ino);
1230 }
Peng Taoa14f1912011-07-30 20:52:31 -04001231 if (end_pos > nfsi->layout->plh_lwb)
1232 nfsi->layout->plh_lwb = end_pos;
Andy Adamson863a3c62011-03-23 13:27:54 +00001233 spin_unlock(&nfsi->vfs_inode.i_lock);
Peng Taoa14f1912011-07-30 20:52:31 -04001234 dprintk("%s: lseg %p end_pos %llu\n",
1235 __func__, wdata->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001236
1237 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1238 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1239 if (mark_as_dirty)
1240 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001241}
1242EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1243
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001244/*
1245 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1246 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1247 * data to disk to allow the server to recover the data if it crashes.
1248 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1249 * is off, and a COMMIT is sent to a data server, or
1250 * if WRITEs to a data server return NFS_DATA_SYNC.
1251 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001252int
Andy Adamsonef311532011-03-12 02:58:10 -05001253pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001254{
1255 struct nfs4_layoutcommit_data *data;
1256 struct nfs_inode *nfsi = NFS_I(inode);
1257 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00001258 loff_t end_pos;
1259 int status = 0;
1260
1261 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1262
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001263 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1264 return 0;
1265
Andy Adamson863a3c62011-03-23 13:27:54 +00001266 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1267 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001268 if (!data) {
1269 mark_inode_dirty_sync(inode);
1270 status = -ENOMEM;
1271 goto out;
1272 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001273
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001274 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001275 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1276 spin_unlock(&inode->i_lock);
1277 kfree(data);
1278 goto out;
1279 }
1280 /*
1281 * Currently only one (whole file) write lseg which is referenced
1282 * in pnfs_set_layoutcommit and will be found.
1283 */
1284 lseg = pnfs_list_write_lseg(inode);
1285
Peng Taoa14f1912011-07-30 20:52:31 -04001286 end_pos = nfsi->layout->plh_lwb;
Peng Taoa14f1912011-07-30 20:52:31 -04001287 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00001288
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001289 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1290 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001291 spin_unlock(&inode->i_lock);
1292
1293 data->args.inode = inode;
1294 data->lseg = lseg;
Peng Taof45c1d42011-07-30 20:52:32 -04001295 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00001296 nfs_fattr_init(&data->fattr);
1297 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1298 data->res.fattr = &data->fattr;
1299 data->args.lastbytewritten = end_pos - 1;
1300 data->res.server = NFS_SERVER(inode);
1301
1302 status = nfs4_proc_layoutcommit(data, sync);
1303out:
1304 dprintk("<-- %s status %d\n", __func__, status);
1305 return status;
1306}