blob: d066aad608add367f0669993228e40d016282a5e [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;
192 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
193}
194
Fred Isamancc6e5342011-01-06 11:36:28 +0000195static void
196destroy_layout_hdr(struct pnfs_layout_hdr *lo)
197{
198 dprintk("%s: freeing layout cache %p\n", __func__, lo);
199 BUG_ON(!list_empty(&lo->plh_layouts));
200 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300201 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400202}
203
204static void
205put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
206{
Fred Isamancc6e5342011-01-06 11:36:28 +0000207 if (atomic_dec_and_test(&lo->plh_refcount))
208 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400209}
210
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400211void
Fred Isamancc6e5342011-01-06 11:36:28 +0000212put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400213{
Fred Isamancc6e5342011-01-06 11:36:28 +0000214 struct inode *inode = lo->plh_inode;
215
216 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
217 destroy_layout_hdr(lo);
218 spin_unlock(&inode->i_lock);
219 }
Andy Adamson974cec82010-10-20 00:18:02 -0400220}
221
222static void
223init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
224{
Fred Isaman566052c2011-01-06 11:36:20 +0000225 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000226 atomic_set(&lseg->pls_refcount, 1);
227 smp_mb();
228 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000229 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400230}
231
Fred Isaman4541d162011-01-06 11:36:23 +0000232static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400233{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000234 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400235
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400236 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000237 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000238 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400239}
240
Fred Isamand684d2a2011-03-01 01:34:13 +0000241static void
242put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400243{
Fred Isamand684d2a2011-03-01 01:34:13 +0000244 struct inode *inode = lseg->pls_layout->plh_inode;
245
Benny Halevyd20581a2011-05-22 19:52:03 +0300246 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000247 list_del_init(&lseg->pls_list);
248 if (list_empty(&lseg->pls_layout->plh_segs)) {
249 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
250 /* Matched by initial refcount set in alloc_init_layout_hdr */
251 put_layout_hdr_locked(lseg->pls_layout);
252 }
253 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
254}
255
Fred Isamanbae724e2011-03-01 01:34:15 +0000256void
Fred Isamand684d2a2011-03-01 01:34:13 +0000257put_lseg(struct pnfs_layout_segment *lseg)
258{
259 struct inode *inode;
260
261 if (!lseg)
262 return;
263
Fred Isaman4541d162011-01-06 11:36:23 +0000264 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
265 atomic_read(&lseg->pls_refcount),
266 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000267 inode = lseg->pls_layout->plh_inode;
268 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
269 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400270
Fred Isamand684d2a2011-03-01 01:34:13 +0000271 put_lseg_common(lseg);
272 list_add(&lseg->pls_list, &free_me);
273 spin_unlock(&inode->i_lock);
274 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000275 }
Andy Adamson974cec82010-10-20 00:18:02 -0400276}
Fred Isamane0c2b382011-03-23 13:27:53 +0000277EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400278
Benny Halevyfb3296e2011-05-22 19:47:26 +0300279static inline u64
280end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000281{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300282 u64 end;
283
284 end = start + len;
285 return end >= start ? end : NFS4_MAX_UINT64;
286}
287
288/* last octet in a range */
289static inline u64
290last_byte_offset(u64 start, u64 len)
291{
292 u64 end;
293
294 BUG_ON(!len);
295 end = start + len;
296 return end > start ? end - 1 : NFS4_MAX_UINT64;
297}
298
299/*
300 * is l2 fully contained in l1?
301 * start1 end1
302 * [----------------------------------)
303 * start2 end2
304 * [----------------)
305 */
306static inline int
307lo_seg_contained(struct pnfs_layout_range *l1,
308 struct pnfs_layout_range *l2)
309{
310 u64 start1 = l1->offset;
311 u64 end1 = end_offset(start1, l1->length);
312 u64 start2 = l2->offset;
313 u64 end2 = end_offset(start2, l2->length);
314
315 return (start1 <= start2) && (end1 >= end2);
316}
317
318/*
319 * is l1 and l2 intersecting?
320 * start1 end1
321 * [----------------------------------)
322 * start2 end2
323 * [----------------)
324 */
325static inline int
326lo_seg_intersecting(struct pnfs_layout_range *l1,
327 struct pnfs_layout_range *l2)
328{
329 u64 start1 = l1->offset;
330 u64 end1 = end_offset(start1, l1->length);
331 u64 start2 = l2->offset;
332 u64 end2 = end_offset(start2, l2->length);
333
334 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
335 (end2 == NFS4_MAX_UINT64 || end2 > start1);
336}
337
Fred Isaman4541d162011-01-06 11:36:23 +0000338static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300339should_free_lseg(struct pnfs_layout_range *lseg_range,
340 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000341{
Benny Halevy778b5502011-05-22 19:48:02 +0300342 return (recall_range->iomode == IOMODE_ANY ||
343 lseg_range->iomode == recall_range->iomode) &&
344 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000345}
346
347/* Returns 1 if lseg is removed from list, 0 otherwise */
348static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
349 struct list_head *tmp_list)
350{
351 int rv = 0;
352
353 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
354 /* Remove the reference keeping the lseg in the
355 * list. It will now be removed when all
356 * outstanding io is finished.
357 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000358 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
359 atomic_read(&lseg->pls_refcount));
360 if (atomic_dec_and_test(&lseg->pls_refcount)) {
361 put_lseg_common(lseg);
362 list_add(&lseg->pls_list, tmp_list);
363 rv = 1;
364 }
Fred Isaman4541d162011-01-06 11:36:23 +0000365 }
366 return rv;
367}
368
369/* Returns count of number of matching invalid lsegs remaining in list
370 * after call.
371 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000372int
Fred Isaman4541d162011-01-06 11:36:23 +0000373mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
374 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300375 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400376{
377 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000378 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400379
380 dprintk("%s:Begin lo %p\n", __func__, lo);
381
Fred Isaman38511722011-02-03 18:28:50 +0000382 if (list_empty(&lo->plh_segs)) {
383 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
384 put_layout_hdr_locked(lo);
385 return 0;
386 }
Fred Isaman4541d162011-01-06 11:36:23 +0000387 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300388 if (!recall_range ||
389 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000390 dprintk("%s: freeing lseg %p iomode %d "
391 "offset %llu length %llu\n", __func__,
392 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
393 lseg->pls_range.length);
394 invalid++;
395 removed += mark_lseg_invalid(lseg, tmp_list);
396 }
397 dprintk("%s:Return %i\n", __func__, invalid - removed);
398 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400399}
400
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000401/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000402void
Fred Isaman4541d162011-01-06 11:36:23 +0000403pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400404{
Fred Isaman4541d162011-01-06 11:36:23 +0000405 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000406 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400407
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000408 if (list_empty(free_me))
409 return;
410
411 lo = list_first_entry(free_me, struct pnfs_layout_segment,
412 pls_list)->pls_layout;
413
414 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
415 struct nfs_client *clp;
416
417 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
418 spin_lock(&clp->cl_lock);
419 list_del_init(&lo->plh_layouts);
420 spin_unlock(&clp->cl_lock);
421 }
Fred Isaman4541d162011-01-06 11:36:23 +0000422 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000423 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000424 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400425 }
426}
427
Benny Halevye5e94012010-10-20 00:18:01 -0400428void
429pnfs_destroy_layout(struct nfs_inode *nfsi)
430{
431 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400432 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400433
434 spin_lock(&nfsi->vfs_inode.i_lock);
435 lo = nfsi->layout;
436 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000437 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300438 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400439 }
440 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400441 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400442}
443
Andy Adamson974cec82010-10-20 00:18:02 -0400444/*
445 * Called by the state manger to remove all layouts established under an
446 * expired lease.
447 */
448void
449pnfs_destroy_all_layouts(struct nfs_client *clp)
450{
451 struct pnfs_layout_hdr *lo;
452 LIST_HEAD(tmp_list);
453
454 spin_lock(&clp->cl_lock);
455 list_splice_init(&clp->cl_layouts, &tmp_list);
456 spin_unlock(&clp->cl_lock);
457
458 while (!list_empty(&tmp_list)) {
459 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000460 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400461 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000462 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400463 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000464 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400465 }
466}
467
Fred Isamanfd6002e2011-01-06 11:36:22 +0000468/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000469void
470pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
471 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400472{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000473 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400474
Fred Isamanfd6002e2011-01-06 11:36:22 +0000475 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
476 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000477 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000478 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000479 if (update_barrier) {
480 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
481
482 if ((int)(new_barrier - lo->plh_barrier))
483 lo->plh_barrier = new_barrier;
484 } else {
485 /* Because of wraparound, we want to keep the barrier
486 * "close" to the current seqids. It needs to be
487 * within 2**31 to count as "behind", so if it
488 * gets too near that limit, give us a litle leeway
489 * and bring it to within 2**30.
490 * NOTE - and yes, this is all unsigned arithmetic.
491 */
492 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
493 lo->plh_barrier = newseq - (1 << 30);
494 }
495 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400496}
497
Fred Isamancf7d63f2011-01-06 11:36:25 +0000498/* lget is set to 1 if called from inside send_layoutget call chain */
499static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000500pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
501 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000502{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000503 if ((stateid) &&
504 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
505 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000506 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000507 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000508 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000509 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000510 (atomic_read(&lo->plh_outstanding) > lget));
511}
512
Fred Isamanfd6002e2011-01-06 11:36:22 +0000513int
514pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
515 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400516{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000517 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400518
519 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000520 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000521 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000522 status = -EAGAIN;
523 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000524 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400525
Fred Isamanfd6002e2011-01-06 11:36:22 +0000526 do {
527 seq = read_seqbegin(&open_state->seqlock);
528 memcpy(dst->data, open_state->stateid.data,
529 sizeof(open_state->stateid.data));
530 } while (read_seqretry(&open_state->seqlock, seq));
531 } else
532 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
533 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400534 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000535 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400536}
537
538/*
539* Get layout from server.
540* for now, assume that whole file layouts are requested.
541* arg->offset: 0
542* arg->length: all ones
543*/
Benny Halevye5e94012010-10-20 00:18:01 -0400544static struct pnfs_layout_segment *
545send_layoutget(struct pnfs_layout_hdr *lo,
546 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300547 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400548 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400549{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000550 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400551 struct nfs_server *server = NFS_SERVER(ino);
552 struct nfs4_layoutget *lgp;
553 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400554 struct page **pages = NULL;
555 int i;
556 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400557
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400558 dprintk("--> %s\n", __func__);
559
560 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400561 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000562 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400563 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400564
565 /* allocate pages for xdr post processing */
566 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
567 max_pages = max_resp_sz >> PAGE_SHIFT;
568
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400569 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400570 if (!pages)
571 goto out_err_free;
572
573 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400574 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400575 if (!pages[i])
576 goto out_err_free;
577 }
578
Benny Halevyfb3296e2011-05-22 19:47:26 +0300579 lgp->args.minlength = PAGE_CACHE_SIZE;
580 if (lgp->args.minlength > range->length)
581 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400582 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300583 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400584 lgp->args.type = server->pnfs_curr_ld->id;
585 lgp->args.inode = ino;
586 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400587 lgp->args.layout.pages = pages;
588 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400589 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400590 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400591
592 /* Synchronously retrieve layout information from server and
593 * store in lseg.
594 */
595 nfs4_proc_layoutget(lgp);
596 if (!lseg) {
597 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300598 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400599 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400600
601 /* free xdr pages */
602 for (i = 0; i < max_pages; i++)
603 __free_page(pages[i]);
604 kfree(pages);
605
Andy Adamson974cec82010-10-20 00:18:02 -0400606 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400607
608out_err_free:
609 /* free any allocated xdr pages, lgp as it's not used */
610 if (pages) {
611 for (i = 0; i < max_pages; i++) {
612 if (!pages[i])
613 break;
614 __free_page(pages[i]);
615 }
616 kfree(pages);
617 }
618 kfree(lgp);
619 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400620}
621
Benny Halevycbe82602011-05-22 19:52:37 +0300622/* Initiates a LAYOUTRETURN(FILE) */
623int
624_pnfs_return_layout(struct inode *ino)
625{
626 struct pnfs_layout_hdr *lo = NULL;
627 struct nfs_inode *nfsi = NFS_I(ino);
628 LIST_HEAD(tmp_list);
629 struct nfs4_layoutreturn *lrp;
630 nfs4_stateid stateid;
631 int status = 0;
632
633 dprintk("--> %s\n", __func__);
634
635 spin_lock(&ino->i_lock);
636 lo = nfsi->layout;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400637 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300638 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400639 dprintk("%s: no layout to return\n", __func__);
640 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300641 }
642 stateid = nfsi->layout->plh_stateid;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400643 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
644 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +0300645 /* Reference matched in nfs4_layoutreturn_release */
646 get_layout_hdr(lo);
647 spin_unlock(&ino->i_lock);
648 pnfs_free_lseg_list(&tmp_list);
649
650 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
651
652 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
653 if (unlikely(lrp == NULL)) {
654 status = -ENOMEM;
655 goto out;
656 }
657
658 lrp->args.stateid = stateid;
659 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
660 lrp->args.inode = ino;
661 lrp->clp = NFS_SERVER(ino)->nfs_client;
662
663 status = nfs4_proc_layoutreturn(lrp);
664out:
665 dprintk("<-- %s status: %d\n", __func__, status);
666 return status;
667}
668
Fred Isamanf7e89172011-01-06 11:36:32 +0000669bool pnfs_roc(struct inode *ino)
670{
671 struct pnfs_layout_hdr *lo;
672 struct pnfs_layout_segment *lseg, *tmp;
673 LIST_HEAD(tmp_list);
674 bool found = false;
675
676 spin_lock(&ino->i_lock);
677 lo = NFS_I(ino)->layout;
678 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
679 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
680 goto out_nolayout;
681 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
682 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
683 mark_lseg_invalid(lseg, &tmp_list);
684 found = true;
685 }
686 if (!found)
687 goto out_nolayout;
688 lo->plh_block_lgets++;
689 get_layout_hdr(lo); /* matched in pnfs_roc_release */
690 spin_unlock(&ino->i_lock);
691 pnfs_free_lseg_list(&tmp_list);
692 return true;
693
694out_nolayout:
695 spin_unlock(&ino->i_lock);
696 return false;
697}
698
699void pnfs_roc_release(struct inode *ino)
700{
701 struct pnfs_layout_hdr *lo;
702
703 spin_lock(&ino->i_lock);
704 lo = NFS_I(ino)->layout;
705 lo->plh_block_lgets--;
706 put_layout_hdr_locked(lo);
707 spin_unlock(&ino->i_lock);
708}
709
710void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
711{
712 struct pnfs_layout_hdr *lo;
713
714 spin_lock(&ino->i_lock);
715 lo = NFS_I(ino)->layout;
716 if ((int)(barrier - lo->plh_barrier) > 0)
717 lo->plh_barrier = barrier;
718 spin_unlock(&ino->i_lock);
719}
720
721bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
722{
723 struct nfs_inode *nfsi = NFS_I(ino);
724 struct pnfs_layout_segment *lseg;
725 bool found = false;
726
727 spin_lock(&ino->i_lock);
728 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
729 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
730 found = true;
731 break;
732 }
733 if (!found) {
734 struct pnfs_layout_hdr *lo = nfsi->layout;
735 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
736
737 /* Since close does not return a layout stateid for use as
738 * a barrier, we choose the worst-case barrier.
739 */
740 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
741 }
742 spin_unlock(&ino->i_lock);
743 return found;
744}
745
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400746/*
747 * Compare two layout segments for sorting into layout cache.
748 * We want to preferentially return RW over RO layouts, so ensure those
749 * are seen first.
750 */
751static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300752cmp_layout(struct pnfs_layout_range *l1,
753 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400754{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300755 s64 d;
756
757 /* high offset > low offset */
758 d = l1->offset - l2->offset;
759 if (d)
760 return d;
761
762 /* short length > long length */
763 d = l2->length - l1->length;
764 if (d)
765 return d;
766
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400767 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300768 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400769}
770
Andy Adamson974cec82010-10-20 00:18:02 -0400771static void
772pnfs_insert_layout(struct pnfs_layout_hdr *lo,
773 struct pnfs_layout_segment *lseg)
774{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400775 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400776
Andy Adamson974cec82010-10-20 00:18:02 -0400777 dprintk("%s:Begin\n", __func__);
778
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000779 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000780 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300781 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400782 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000783 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400784 dprintk("%s: inserted lseg %p "
785 "iomode %d offset %llu length %llu before "
786 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000787 __func__, lseg, lseg->pls_range.iomode,
788 lseg->pls_range.offset, lseg->pls_range.length,
789 lp, lp->pls_range.iomode, lp->pls_range.offset,
790 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300791 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400792 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300793 list_add_tail(&lseg->pls_list, &lo->plh_segs);
794 dprintk("%s: inserted lseg %p "
795 "iomode %d offset %llu length %llu at tail\n",
796 __func__, lseg, lseg->pls_range.iomode,
797 lseg->pls_range.offset, lseg->pls_range.length);
798out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000799 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400800
801 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400802}
803
804static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400805alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400806{
807 struct pnfs_layout_hdr *lo;
808
Benny Halevy636fb9c2011-05-22 19:51:33 +0300809 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400810 if (!lo)
811 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000812 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000813 INIT_LIST_HEAD(&lo->plh_layouts);
814 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000815 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000816 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400817 return lo;
818}
819
820static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400821pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400822{
823 struct nfs_inode *nfsi = NFS_I(ino);
824 struct pnfs_layout_hdr *new = NULL;
825
826 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
827
828 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000829 if (nfsi->layout) {
830 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
831 return NULL;
832 else
833 return nfsi->layout;
834 }
Benny Halevye5e94012010-10-20 00:18:01 -0400835 spin_unlock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400836 new = alloc_init_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400837 spin_lock(&ino->i_lock);
838
839 if (likely(nfsi->layout == NULL)) /* Won the race? */
840 nfsi->layout = new;
841 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300842 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400843 return nfsi->layout;
844}
845
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400846/*
847 * iomode matching rules:
848 * iomode lseg match
849 * ----- ----- -----
850 * ANY READ true
851 * ANY RW true
852 * RW READ false
853 * RW RW true
854 * READ READ true
855 * READ RW true
856 */
857static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300858is_matching_lseg(struct pnfs_layout_range *ls_range,
859 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400860{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300861 struct pnfs_layout_range range1;
862
863 if ((range->iomode == IOMODE_RW &&
864 ls_range->iomode != IOMODE_RW) ||
865 !lo_seg_intersecting(ls_range, range))
866 return 0;
867
868 /* range1 covers only the first byte in the range */
869 range1 = *range;
870 range1.length = 1;
871 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400872}
873
874/*
875 * lookup range in layout
876 */
Benny Halevye5e94012010-10-20 00:18:01 -0400877static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300878pnfs_find_lseg(struct pnfs_layout_hdr *lo,
879 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400880{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400881 struct pnfs_layout_segment *lseg, *ret = NULL;
882
883 dprintk("%s:Begin\n", __func__);
884
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000885 assert_spin_locked(&lo->plh_inode->i_lock);
886 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000887 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300888 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000889 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400890 break;
891 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400892 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400893 break;
894 }
895
896 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000897 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400898 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400899}
900
901/*
902 * Layout segment is retreived from the server if not cached.
903 * The appropriate layout segment is referenced and returned to the caller.
904 */
905struct pnfs_layout_segment *
906pnfs_update_layout(struct inode *ino,
907 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300908 loff_t pos,
909 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400910 enum pnfs_iomode iomode,
911 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400912{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300913 struct pnfs_layout_range arg = {
914 .iomode = iomode,
915 .offset = pos,
916 .length = count,
917 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300918 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400919 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000920 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400921 struct pnfs_layout_hdr *lo;
922 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000923 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400924
925 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
926 return NULL;
927 spin_lock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400928 lo = pnfs_find_alloc_layout(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400929 if (lo == NULL) {
930 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
931 goto out_unlock;
932 }
933
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000934 /* Do we even need to bother with this? */
935 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
936 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
937 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400938 goto out_unlock;
939 }
940
941 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000942 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400943 goto out_unlock;
944
Andy Adamson568e8c42011-03-01 01:34:22 +0000945 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300946 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000947 if (lseg)
948 goto out_unlock;
949
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000950 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000951 goto out_unlock;
952 atomic_inc(&lo->plh_outstanding);
953
Fred Isamancc6e5342011-01-06 11:36:28 +0000954 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000955 if (list_empty(&lo->plh_segs))
956 first = true;
957 spin_unlock(&ino->i_lock);
958 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000959 /* The lo must be on the clp list if there is any
960 * chance of a CB_LAYOUTRECALL(FILE) coming in.
961 */
962 spin_lock(&clp->cl_lock);
963 BUG_ON(!list_empty(&lo->plh_layouts));
964 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
965 spin_unlock(&clp->cl_lock);
966 }
Benny Halevye5e94012010-10-20 00:18:01 -0400967
Benny Halevy707ed5f2011-05-22 19:47:46 +0300968 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
969 if (pg_offset) {
970 arg.offset -= pg_offset;
971 arg.length += pg_offset;
972 }
973 arg.length = PAGE_CACHE_ALIGN(arg.length);
974
Benny Halevyfb3296e2011-05-22 19:47:26 +0300975 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000976 if (!lseg && first) {
977 spin_lock(&clp->cl_lock);
978 list_del_init(&lo->plh_layouts);
979 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +0000980 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000981 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000982 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400983out:
984 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +0000985 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400986 return lseg;
987out_unlock:
988 spin_unlock(&ino->i_lock);
989 goto out;
990}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400991
992int
993pnfs_layout_process(struct nfs4_layoutget *lgp)
994{
995 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
996 struct nfs4_layoutget_res *res = &lgp->res;
997 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000998 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000999 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001000 int status = 0;
1001
1002 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001003 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001004 if (!lseg || IS_ERR(lseg)) {
1005 if (!lseg)
1006 status = -ENOMEM;
1007 else
1008 status = PTR_ERR(lseg);
1009 dprintk("%s: Could not allocate layout: error %d\n",
1010 __func__, status);
1011 goto out;
1012 }
1013
1014 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001015 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1016 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1017 dprintk("%s forget reply due to recall\n", __func__);
1018 goto out_forget_reply;
1019 }
1020
1021 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1022 dprintk("%s forget reply due to state\n", __func__);
1023 goto out_forget_reply;
1024 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001025 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001026 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001027 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001028 pnfs_insert_layout(lo, lseg);
1029
Fred Isamanf7e89172011-01-06 11:36:32 +00001030 if (res->return_on_close) {
1031 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1032 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1033 }
1034
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001035 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001036 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001037 spin_unlock(&ino->i_lock);
1038out:
1039 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001040
1041out_forget_reply:
1042 spin_unlock(&ino->i_lock);
1043 lseg->pls_layout = lo;
1044 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1045 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001046}
1047
Benny Halevy18ad0a92011-05-25 21:03:56 +03001048bool
Benny Halevydfed2062011-05-25 20:25:22 +03001049pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1050 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001051{
Benny Halevydfed2062011-05-25 20:25:22 +03001052 enum pnfs_iomode access_type;
1053 gfp_t gfp_flags;
1054
1055 /* We assume that pg_ioflags == 0 iff we're reading a page */
1056 if (pgio->pg_ioflags == 0) {
1057 access_type = IOMODE_READ;
1058 gfp_flags = GFP_KERNEL;
1059 } else {
1060 access_type = IOMODE_RW;
1061 gfp_flags = GFP_NOFS;
1062 }
1063
Fred Isamanbae724e2011-03-01 01:34:15 +00001064 if (pgio->pg_count == prev->wb_bytes) {
1065 /* This is first coelesce call for a series of nfs_pages */
1066 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1067 prev->wb_context,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001068 req_offset(req),
1069 pgio->pg_count,
Benny Halevydfed2062011-05-25 20:25:22 +03001070 access_type,
1071 gfp_flags);
Benny Halevy89a58e32011-05-25 20:54:40 +03001072 return true;
Fred Isamanbae724e2011-03-01 01:34:15 +00001073 }
Benny Halevy89a58e32011-05-25 20:54:40 +03001074
1075 if (pgio->pg_lseg &&
1076 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1077 pgio->pg_lseg->pls_range.length))
Benny Halevy18ad0a92011-05-25 21:03:56 +03001078 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +03001079
1080 return true;
Fred Isamanbae724e2011-03-01 01:34:15 +00001081}
Benny Halevy89a58e32011-05-25 20:54:40 +03001082EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001083
Benny Halevyd20581a2011-05-22 19:52:03 +03001084/*
1085 * Called by non rpc-based layout drivers
1086 */
1087int
1088pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001089{
Benny Halevyd20581a2011-05-22 19:52:03 +03001090 int status;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001091
Benny Halevyd20581a2011-05-22 19:52:03 +03001092 if (!data->pnfs_error) {
1093 pnfs_set_layoutcommit(data);
1094 data->mds_ops->rpc_call_done(&data->task, data);
1095 data->mds_ops->rpc_release(data);
1096 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001097 }
Fred Isaman44b83792011-03-03 15:13:44 +00001098
Benny Halevyd20581a2011-05-22 19:52:03 +03001099 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1100 data->pnfs_error);
1101 status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
1102 data->mds_ops, NFS_FILE_SYNC);
1103 return status ? : -EAGAIN;
Fred Isaman44b83792011-03-03 15:13:44 +00001104}
Benny Halevyd20581a2011-05-22 19:52:03 +03001105EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001106
Andy Adamson0382b742011-03-03 15:13:45 +00001107enum pnfs_try_status
1108pnfs_try_to_write_data(struct nfs_write_data *wdata,
1109 const struct rpc_call_ops *call_ops, int how)
1110{
1111 struct inode *inode = wdata->inode;
1112 enum pnfs_try_status trypnfs;
1113 struct nfs_server *nfss = NFS_SERVER(inode);
1114
1115 wdata->mds_ops = call_ops;
1116
1117 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1118 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1119
1120 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1121 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1122 put_lseg(wdata->lseg);
1123 wdata->lseg = NULL;
1124 } else
1125 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1126
1127 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1128 return trypnfs;
1129}
1130
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001131/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001132 * Called by non rpc-based layout drivers
1133 */
1134int
1135pnfs_ld_read_done(struct nfs_read_data *data)
1136{
1137 int status;
1138
1139 if (!data->pnfs_error) {
1140 __nfs4_read_done_cb(data);
1141 data->mds_ops->rpc_call_done(&data->task, data);
1142 data->mds_ops->rpc_release(data);
1143 return 0;
1144 }
1145
1146 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1147 data->pnfs_error);
1148 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1149 data->mds_ops);
1150 return status ? : -EAGAIN;
1151}
1152EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1153
1154/*
Andy Adamson64419a92011-03-01 01:34:16 +00001155 * Call the appropriate parallel I/O subsystem read function.
1156 */
1157enum pnfs_try_status
1158pnfs_try_to_read_data(struct nfs_read_data *rdata,
1159 const struct rpc_call_ops *call_ops)
1160{
1161 struct inode *inode = rdata->inode;
1162 struct nfs_server *nfss = NFS_SERVER(inode);
1163 enum pnfs_try_status trypnfs;
1164
1165 rdata->mds_ops = call_ops;
1166
1167 dprintk("%s: Reading ino:%lu %u@%llu\n",
1168 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1169
1170 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1171 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1172 put_lseg(rdata->lseg);
1173 rdata->lseg = NULL;
1174 } else {
1175 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1176 }
1177 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1178 return trypnfs;
1179}
Andy Adamson863a3c62011-03-23 13:27:54 +00001180
1181/*
1182 * Currently there is only one (whole file) write lseg.
1183 */
1184static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1185{
1186 struct pnfs_layout_segment *lseg, *rv = NULL;
1187
1188 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1189 if (lseg->pls_range.iomode == IOMODE_RW)
1190 rv = lseg;
1191 return rv;
1192}
1193
1194void
1195pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1196{
1197 struct nfs_inode *nfsi = NFS_I(wdata->inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001198 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001199 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001200
1201 spin_lock(&nfsi->vfs_inode.i_lock);
1202 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1203 /* references matched in nfs4_layoutcommit_release */
1204 get_lseg(wdata->lseg);
1205 wdata->lseg->pls_lc_cred =
1206 get_rpccred(wdata->args.context->state->owner->so_cred);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001207 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001208 dprintk("%s: Set layoutcommit for inode %lu ",
1209 __func__, wdata->inode->i_ino);
1210 }
1211 if (end_pos > wdata->lseg->pls_end_pos)
1212 wdata->lseg->pls_end_pos = end_pos;
1213 spin_unlock(&nfsi->vfs_inode.i_lock);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001214
1215 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1216 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1217 if (mark_as_dirty)
1218 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001219}
1220EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1221
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001222/*
1223 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1224 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1225 * data to disk to allow the server to recover the data if it crashes.
1226 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1227 * is off, and a COMMIT is sent to a data server, or
1228 * if WRITEs to a data server return NFS_DATA_SYNC.
1229 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001230int
Andy Adamsonef311532011-03-12 02:58:10 -05001231pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001232{
1233 struct nfs4_layoutcommit_data *data;
1234 struct nfs_inode *nfsi = NFS_I(inode);
1235 struct pnfs_layout_segment *lseg;
1236 struct rpc_cred *cred;
1237 loff_t end_pos;
1238 int status = 0;
1239
1240 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1241
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001242 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1243 return 0;
1244
Andy Adamson863a3c62011-03-23 13:27:54 +00001245 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1246 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001247 if (!data) {
1248 mark_inode_dirty_sync(inode);
1249 status = -ENOMEM;
1250 goto out;
1251 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001252
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001253 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001254 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1255 spin_unlock(&inode->i_lock);
1256 kfree(data);
1257 goto out;
1258 }
1259 /*
1260 * Currently only one (whole file) write lseg which is referenced
1261 * in pnfs_set_layoutcommit and will be found.
1262 */
1263 lseg = pnfs_list_write_lseg(inode);
1264
1265 end_pos = lseg->pls_end_pos;
1266 cred = lseg->pls_lc_cred;
1267 lseg->pls_end_pos = 0;
1268 lseg->pls_lc_cred = NULL;
1269
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001270 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1271 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001272 spin_unlock(&inode->i_lock);
1273
1274 data->args.inode = inode;
1275 data->lseg = lseg;
1276 data->cred = cred;
1277 nfs_fattr_init(&data->fattr);
1278 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1279 data->res.fattr = &data->fattr;
1280 data->args.lastbytewritten = end_pos - 1;
1281 data->res.server = NFS_SERVER(inode);
1282
1283 status = nfs4_proc_layoutcommit(data, sync);
1284out:
1285 dprintk("<-- %s status %d\n", __func__, status);
1286 return status;
1287}