blob: f46f9bc4f767dc90e84364954ff0d25a6eea6ee6 [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>
Trond Myklebust493292d2011-07-13 15:58:28 -040031#include <linux/nfs_page.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040032#include <linux/module.h>
Andy Adamson974cec82010-10-20 00:18:02 -040033#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040034#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000035#include "iostat.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040036
37#define NFSDBG_FACILITY NFSDBG_PNFS
38
Fred Isaman02c35fc2010-10-20 00:17:59 -040039/* Locking:
40 *
41 * pnfs_spinlock:
42 * protects pnfs_modules_tbl.
43 */
44static DEFINE_SPINLOCK(pnfs_spinlock);
45
46/*
47 * pnfs_modules_tbl holds all pnfs modules
48 */
49static LIST_HEAD(pnfs_modules_tbl);
50
51/* Return the registered pnfs layout driver module matching given id */
52static struct pnfs_layoutdriver_type *
53find_pnfs_driver_locked(u32 id)
54{
55 struct pnfs_layoutdriver_type *local;
56
57 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
58 if (local->id == id)
59 goto out;
60 local = NULL;
61out:
62 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
63 return local;
64}
65
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040066static struct pnfs_layoutdriver_type *
67find_pnfs_driver(u32 id)
68{
Fred Isaman02c35fc2010-10-20 00:17:59 -040069 struct pnfs_layoutdriver_type *local;
70
71 spin_lock(&pnfs_spinlock);
72 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040073 if (local != NULL && !try_module_get(local->owner)) {
74 dprintk("%s: Could not grab reference on module\n", __func__);
75 local = NULL;
76 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040077 spin_unlock(&pnfs_spinlock);
78 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040079}
80
81void
82unset_pnfs_layoutdriver(struct nfs_server *nfss)
83{
Benny Halevy738fd0f32011-07-30 20:52:36 -040084 if (nfss->pnfs_curr_ld) {
85 if (nfss->pnfs_curr_ld->clear_layoutdriver)
86 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040087 /* Decrement the MDS count. Purge the deviceid cache if zero */
88 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
89 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040090 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040092 nfss->pnfs_curr_ld = NULL;
93}
94
95/*
96 * Try to set the server's pnfs module to the pnfs layout type specified by id.
97 * Currently only one pNFS layout driver per filesystem is supported.
98 *
99 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
100 */
101void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400102set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
103 u32 id)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400104{
105 struct pnfs_layoutdriver_type *ld_type = NULL;
106
107 if (id == 0)
108 goto out_no_driver;
109 if (!(server->nfs_client->cl_exchange_flags &
110 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500111 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
112 __func__, id, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400113 goto out_no_driver;
114 }
115 ld_type = find_pnfs_driver(id);
116 if (!ld_type) {
117 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
118 ld_type = find_pnfs_driver(id);
119 if (!ld_type) {
120 dprintk("%s: No pNFS module found for %u.\n",
121 __func__, id);
122 goto out_no_driver;
123 }
124 }
125 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400126 if (ld_type->set_layoutdriver
127 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500128 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
129 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400130 module_put(ld_type->owner);
131 goto out_no_driver;
132 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400133 /* Bump the MDS count */
134 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000135
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400136 dprintk("%s: pNFS module for %u set\n", __func__, id);
137 return;
138
139out_no_driver:
140 dprintk("%s: Using NFSv4 I/O\n", __func__);
141 server->pnfs_curr_ld = NULL;
142}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400143
144int
145pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
146{
147 int status = -EINVAL;
148 struct pnfs_layoutdriver_type *tmp;
149
150 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500151 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400152 return status;
153 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400154 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500155 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400156 "alloc_lseg and free_lseg.\n", __func__);
157 return status;
158 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400159
160 spin_lock(&pnfs_spinlock);
161 tmp = find_pnfs_driver_locked(ld_type->id);
162 if (!tmp) {
163 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
164 status = 0;
165 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
166 ld_type->name);
167 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500168 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400169 __func__, ld_type->id);
170 }
171 spin_unlock(&pnfs_spinlock);
172
173 return status;
174}
175EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
176
177void
178pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
179{
180 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
181 spin_lock(&pnfs_spinlock);
182 list_del(&ld_type->pnfs_tblid);
183 spin_unlock(&pnfs_spinlock);
184}
185EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400186
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400187/*
188 * pNFS client layout cache
189 */
190
Fred Isamancc6e5342011-01-06 11:36:28 +0000191/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000192void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400193pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400194{
Fred Isamancc6e5342011-01-06 11:36:28 +0000195 atomic_inc(&lo->plh_refcount);
196}
197
Benny Halevy636fb9c2011-05-22 19:51:33 +0300198static struct pnfs_layout_hdr *
199pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
200{
201 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
202 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
203 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
204}
205
206static void
207pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
208{
209 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
Peng Tao9fa40752011-07-30 20:52:32 -0400210 put_rpccred(lo->plh_lc_cred);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300211 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
212}
213
Fred Isamancc6e5342011-01-06 11:36:28 +0000214static void
215destroy_layout_hdr(struct pnfs_layout_hdr *lo)
216{
217 dprintk("%s: freeing layout cache %p\n", __func__, lo);
218 BUG_ON(!list_empty(&lo->plh_layouts));
219 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300220 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400221}
222
223static void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400224pnfs_put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400225{
Fred Isamancc6e5342011-01-06 11:36:28 +0000226 if (atomic_dec_and_test(&lo->plh_refcount))
227 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400228}
229
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400230void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400231pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400232{
Fred Isamancc6e5342011-01-06 11:36:28 +0000233 struct inode *inode = lo->plh_inode;
234
235 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
236 destroy_layout_hdr(lo);
237 spin_unlock(&inode->i_lock);
238 }
Andy Adamson974cec82010-10-20 00:18:02 -0400239}
240
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400241static int
242pnfs_iomode_to_fail_bit(u32 iomode)
243{
244 return iomode == IOMODE_RW ?
245 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
246}
247
248static void
249pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
250{
251 set_bit(pnfs_iomode_to_fail_bit(iomode), &lo->plh_flags);
252 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
253 iomode == IOMODE_RW ? "RW" : "READ");
254}
255
256static bool
257pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
258{
259 return test_bit(pnfs_iomode_to_fail_bit(iomode), &lo->plh_flags) != 0;
260}
261
Andy Adamson974cec82010-10-20 00:18:02 -0400262static void
263init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
264{
Fred Isaman566052c2011-01-06 11:36:20 +0000265 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400266 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000267 atomic_set(&lseg->pls_refcount, 1);
268 smp_mb();
269 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000270 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400271}
272
Fred Isaman4541d162011-01-06 11:36:23 +0000273static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400274{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000275 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400276
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400277 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400278 /* Matched by pnfs_get_layout_hdr in pnfs_insert_layout */
279 pnfs_put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400280}
281
Fred Isamand684d2a2011-03-01 01:34:13 +0000282static void
Trond Myklebust9369a432012-09-18 20:57:08 -0400283pnfs_put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400284{
Fred Isamand684d2a2011-03-01 01:34:13 +0000285 struct inode *inode = lseg->pls_layout->plh_inode;
286
Benny Halevyd20581a2011-05-22 19:52:03 +0300287 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000288 list_del_init(&lseg->pls_list);
289 if (list_empty(&lseg->pls_layout->plh_segs)) {
290 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
291 /* Matched by initial refcount set in alloc_init_layout_hdr */
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400292 pnfs_put_layout_hdr_locked(lseg->pls_layout);
Fred Isamand684d2a2011-03-01 01:34:13 +0000293 }
294 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
295}
296
Fred Isamanbae724e2011-03-01 01:34:15 +0000297void
Trond Myklebust9369a432012-09-18 20:57:08 -0400298pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000299{
300 struct inode *inode;
301
302 if (!lseg)
303 return;
304
Fred Isaman4541d162011-01-06 11:36:23 +0000305 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
306 atomic_read(&lseg->pls_refcount),
307 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000308 inode = lseg->pls_layout->plh_inode;
309 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
310 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400311
Trond Myklebust9369a432012-09-18 20:57:08 -0400312 pnfs_put_lseg_common(lseg);
Fred Isamand684d2a2011-03-01 01:34:13 +0000313 list_add(&lseg->pls_list, &free_me);
314 spin_unlock(&inode->i_lock);
315 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000316 }
Andy Adamson974cec82010-10-20 00:18:02 -0400317}
Trond Myklebust9369a432012-09-18 20:57:08 -0400318EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400319
Benny Halevyfb3296e2011-05-22 19:47:26 +0300320static inline u64
321end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000322{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300323 u64 end;
324
325 end = start + len;
326 return end >= start ? end : NFS4_MAX_UINT64;
327}
328
329/* last octet in a range */
330static inline u64
331last_byte_offset(u64 start, u64 len)
332{
333 u64 end;
334
335 BUG_ON(!len);
336 end = start + len;
337 return end > start ? end - 1 : NFS4_MAX_UINT64;
338}
339
340/*
341 * is l2 fully contained in l1?
342 * start1 end1
343 * [----------------------------------)
344 * start2 end2
345 * [----------------)
346 */
347static inline int
348lo_seg_contained(struct pnfs_layout_range *l1,
349 struct pnfs_layout_range *l2)
350{
351 u64 start1 = l1->offset;
352 u64 end1 = end_offset(start1, l1->length);
353 u64 start2 = l2->offset;
354 u64 end2 = end_offset(start2, l2->length);
355
356 return (start1 <= start2) && (end1 >= end2);
357}
358
359/*
360 * is l1 and l2 intersecting?
361 * start1 end1
362 * [----------------------------------)
363 * start2 end2
364 * [----------------)
365 */
366static inline int
367lo_seg_intersecting(struct pnfs_layout_range *l1,
368 struct pnfs_layout_range *l2)
369{
370 u64 start1 = l1->offset;
371 u64 end1 = end_offset(start1, l1->length);
372 u64 start2 = l2->offset;
373 u64 end2 = end_offset(start2, l2->length);
374
375 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
376 (end2 == NFS4_MAX_UINT64 || end2 > start1);
377}
378
Fred Isaman4541d162011-01-06 11:36:23 +0000379static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300380should_free_lseg(struct pnfs_layout_range *lseg_range,
381 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000382{
Benny Halevy778b5502011-05-22 19:48:02 +0300383 return (recall_range->iomode == IOMODE_ANY ||
384 lseg_range->iomode == recall_range->iomode) &&
385 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000386}
387
388/* Returns 1 if lseg is removed from list, 0 otherwise */
389static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
390 struct list_head *tmp_list)
391{
392 int rv = 0;
393
394 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
395 /* Remove the reference keeping the lseg in the
396 * list. It will now be removed when all
397 * outstanding io is finished.
398 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000399 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
400 atomic_read(&lseg->pls_refcount));
401 if (atomic_dec_and_test(&lseg->pls_refcount)) {
Trond Myklebust9369a432012-09-18 20:57:08 -0400402 pnfs_put_lseg_common(lseg);
Fred Isamand684d2a2011-03-01 01:34:13 +0000403 list_add(&lseg->pls_list, tmp_list);
404 rv = 1;
405 }
Fred Isaman4541d162011-01-06 11:36:23 +0000406 }
407 return rv;
408}
409
410/* Returns count of number of matching invalid lsegs remaining in list
411 * after call.
412 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000413int
Trond Myklebust49a85062012-09-18 20:43:31 -0400414pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000415 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300416 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400417{
418 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000419 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400420
421 dprintk("%s:Begin lo %p\n", __func__, lo);
422
Fred Isaman38511722011-02-03 18:28:50 +0000423 if (list_empty(&lo->plh_segs)) {
Andy Adamson2701d082012-05-24 13:13:24 -0400424 /* Reset MDS Threshold I/O counters */
425 NFS_I(lo->plh_inode)->write_io = 0;
426 NFS_I(lo->plh_inode)->read_io = 0;
Fred Isaman38511722011-02-03 18:28:50 +0000427 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400428 pnfs_put_layout_hdr_locked(lo);
Fred Isaman38511722011-02-03 18:28:50 +0000429 return 0;
430 }
Fred Isaman4541d162011-01-06 11:36:23 +0000431 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300432 if (!recall_range ||
433 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000434 dprintk("%s: freeing lseg %p iomode %d "
435 "offset %llu length %llu\n", __func__,
436 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
437 lseg->pls_range.length);
438 invalid++;
439 removed += mark_lseg_invalid(lseg, tmp_list);
440 }
441 dprintk("%s:Return %i\n", __func__, invalid - removed);
442 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400443}
444
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000445/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000446void
Fred Isaman4541d162011-01-06 11:36:23 +0000447pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400448{
Fred Isaman4541d162011-01-06 11:36:23 +0000449 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000450 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400451
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000452 if (list_empty(free_me))
453 return;
454
455 lo = list_first_entry(free_me, struct pnfs_layout_segment,
456 pls_list)->pls_layout;
457
458 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
459 struct nfs_client *clp;
460
461 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
462 spin_lock(&clp->cl_lock);
463 list_del_init(&lo->plh_layouts);
464 spin_unlock(&clp->cl_lock);
465 }
Fred Isaman4541d162011-01-06 11:36:23 +0000466 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000467 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000468 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400469 }
470}
471
Benny Halevye5e94012010-10-20 00:18:01 -0400472void
473pnfs_destroy_layout(struct nfs_inode *nfsi)
474{
475 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400476 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400477
478 spin_lock(&nfsi->vfs_inode.i_lock);
479 lo = nfsi->layout;
480 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000481 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Trond Myklebust49a85062012-09-18 20:43:31 -0400482 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400483 }
484 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400485 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400486}
Andy Adamson041245c2012-04-27 17:53:53 -0400487EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400488
Andy Adamson974cec82010-10-20 00:18:02 -0400489/*
490 * Called by the state manger to remove all layouts established under an
491 * expired lease.
492 */
493void
494pnfs_destroy_all_layouts(struct nfs_client *clp)
495{
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400496 struct nfs_server *server;
Andy Adamson974cec82010-10-20 00:18:02 -0400497 struct pnfs_layout_hdr *lo;
498 LIST_HEAD(tmp_list);
499
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400500 nfs4_deviceid_mark_client_invalid(clp);
501 nfs4_deviceid_purge_client(clp);
502
Andy Adamson974cec82010-10-20 00:18:02 -0400503 spin_lock(&clp->cl_lock);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400504 rcu_read_lock();
505 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
506 if (!list_empty(&server->layouts))
507 list_splice_init(&server->layouts, &tmp_list);
508 }
509 rcu_read_unlock();
Andy Adamson974cec82010-10-20 00:18:02 -0400510 spin_unlock(&clp->cl_lock);
511
512 while (!list_empty(&tmp_list)) {
513 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000514 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400515 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000516 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400517 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000518 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400519 }
520}
521
Fred Isamanfd6002e2011-01-06 11:36:22 +0000522/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000523void
524pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
525 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400526{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000527 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400528
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500529 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
530 newseq = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000531 if ((int)(newseq - oldseq) > 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500532 nfs4_stateid_copy(&lo->plh_stateid, new);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000533 if (update_barrier) {
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500534 u32 new_barrier = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000535
536 if ((int)(new_barrier - lo->plh_barrier))
537 lo->plh_barrier = new_barrier;
538 } else {
539 /* Because of wraparound, we want to keep the barrier
540 * "close" to the current seqids. It needs to be
541 * within 2**31 to count as "behind", so if it
542 * gets too near that limit, give us a litle leeway
543 * and bring it to within 2**30.
544 * NOTE - and yes, this is all unsigned arithmetic.
545 */
546 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
547 lo->plh_barrier = newseq - (1 << 30);
548 }
549 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400550}
551
Fred Isamancf7d63f2011-01-06 11:36:25 +0000552/* lget is set to 1 if called from inside send_layoutget call chain */
553static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000554pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
555 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000556{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000557 if ((stateid) &&
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500558 (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000559 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000560 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000561 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000562 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000563 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000564 (atomic_read(&lo->plh_outstanding) > lget));
565}
566
Fred Isamanfd6002e2011-01-06 11:36:22 +0000567int
568pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
569 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400570{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000571 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400572
573 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000574 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000575 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000576 status = -EAGAIN;
577 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000578 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400579
Fred Isamanfd6002e2011-01-06 11:36:22 +0000580 do {
581 seq = read_seqbegin(&open_state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500582 nfs4_stateid_copy(dst, &open_state->stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000583 } while (read_seqretry(&open_state->seqlock, seq));
584 } else
Trond Myklebustf597c532012-03-04 18:13:56 -0500585 nfs4_stateid_copy(dst, &lo->plh_stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000586 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400587 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000588 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400589}
590
591/*
592* Get layout from server.
593* for now, assume that whole file layouts are requested.
594* arg->offset: 0
595* arg->length: all ones
596*/
Benny Halevye5e94012010-10-20 00:18:01 -0400597static struct pnfs_layout_segment *
598send_layoutget(struct pnfs_layout_hdr *lo,
599 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300600 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400601 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400602{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000603 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400604 struct nfs_server *server = NFS_SERVER(ino);
605 struct nfs4_layoutget *lgp;
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400606 struct pnfs_layout_segment *lseg;
Benny Halevye5e94012010-10-20 00:18:01 -0400607
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400608 dprintk("--> %s\n", __func__);
609
610 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400611 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000612 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400613 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400614
Benny Halevyfb3296e2011-05-22 19:47:26 +0300615 lgp->args.minlength = PAGE_CACHE_SIZE;
616 if (lgp->args.minlength > range->length)
617 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400618 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300619 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400620 lgp->args.type = server->pnfs_curr_ld->id;
621 lgp->args.inode = ino;
622 lgp->args.ctx = get_nfs_open_context(ctx);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400623 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400624
625 /* Synchronously retrieve layout information from server and
626 * store in lseg.
627 */
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400628 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
629 if (IS_ERR(lseg)) {
630 switch (PTR_ERR(lseg)) {
631 case -ENOMEM:
632 case -ERESTARTSYS:
633 break;
634 default:
635 /* remember that LAYOUTGET failed and suspend trying */
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400636 pnfs_layout_io_set_failed(lo, range->iomode);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400637 }
638 return NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400639 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400640
Andy Adamson974cec82010-10-20 00:18:02 -0400641 return lseg;
642}
643
Andy Adamson293b3b02012-06-20 15:03:34 -0400644/*
645 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
646 * when the layout segment list is empty.
647 *
648 * Note that a pnfs_layout_hdr can exist with an empty layout segment
649 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
650 * deviceid is marked invalid.
651 */
Benny Halevycbe82602011-05-22 19:52:37 +0300652int
653_pnfs_return_layout(struct inode *ino)
654{
655 struct pnfs_layout_hdr *lo = NULL;
656 struct nfs_inode *nfsi = NFS_I(ino);
657 LIST_HEAD(tmp_list);
658 struct nfs4_layoutreturn *lrp;
659 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -0400660 int status = 0, empty;
Benny Halevycbe82602011-05-22 19:52:37 +0300661
Andy Adamson366d5052012-06-20 15:03:33 -0400662 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +0300663
664 spin_lock(&ino->i_lock);
665 lo = nfsi->layout;
Andy Adamson366d5052012-06-20 15:03:33 -0400666 if (!lo || pnfs_test_layout_returned(lo)) {
Benny Halevycbe82602011-05-22 19:52:37 +0300667 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -0400668 dprintk("NFS: %s no layout to return\n", __func__);
669 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +0300670 }
671 stateid = nfsi->layout->plh_stateid;
672 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400673 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -0400674 empty = list_empty(&lo->plh_segs);
Trond Myklebust49a85062012-09-18 20:43:31 -0400675 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Andy Adamson293b3b02012-06-20 15:03:34 -0400676 /* Don't send a LAYOUTRETURN if list was initially empty */
677 if (empty) {
678 spin_unlock(&ino->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400679 pnfs_put_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -0400680 dprintk("NFS: %s no layout segments to return\n", __func__);
681 goto out;
682 }
Fred Isamanea0ded72011-06-15 12:31:02 -0400683 lo->plh_block_lgets++;
Andy Adamson366d5052012-06-20 15:03:33 -0400684 pnfs_mark_layout_returned(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300685 spin_unlock(&ino->i_lock);
686 pnfs_free_lseg_list(&tmp_list);
687
688 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
689
690 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
691 if (unlikely(lrp == NULL)) {
692 status = -ENOMEM;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400693 pnfs_layout_io_set_failed(lo, IOMODE_RW);
694 pnfs_layout_io_set_failed(lo, IOMODE_READ);
Andy Adamson366d5052012-06-20 15:03:33 -0400695 pnfs_clear_layout_returned(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400696 pnfs_put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300697 goto out;
698 }
699
700 lrp->args.stateid = stateid;
701 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
702 lrp->args.inode = ino;
Trond Myklebusta56aaa02011-06-15 11:59:10 -0400703 lrp->args.layout = lo;
Benny Halevycbe82602011-05-22 19:52:37 +0300704 lrp->clp = NFS_SERVER(ino)->nfs_client;
705
706 status = nfs4_proc_layoutreturn(lrp);
707out:
708 dprintk("<-- %s status: %d\n", __func__, status);
709 return status;
710}
Andy Adamson0a57cda2012-04-27 17:53:50 -0400711EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +0300712
Fred Isamanf7e89172011-01-06 11:36:32 +0000713bool pnfs_roc(struct inode *ino)
714{
715 struct pnfs_layout_hdr *lo;
716 struct pnfs_layout_segment *lseg, *tmp;
717 LIST_HEAD(tmp_list);
718 bool found = false;
719
720 spin_lock(&ino->i_lock);
721 lo = NFS_I(ino)->layout;
722 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
723 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
724 goto out_nolayout;
725 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
726 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
727 mark_lseg_invalid(lseg, &tmp_list);
728 found = true;
729 }
730 if (!found)
731 goto out_nolayout;
732 lo->plh_block_lgets++;
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400733 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
Fred Isamanf7e89172011-01-06 11:36:32 +0000734 spin_unlock(&ino->i_lock);
735 pnfs_free_lseg_list(&tmp_list);
736 return true;
737
738out_nolayout:
739 spin_unlock(&ino->i_lock);
740 return false;
741}
742
743void pnfs_roc_release(struct inode *ino)
744{
745 struct pnfs_layout_hdr *lo;
746
747 spin_lock(&ino->i_lock);
748 lo = NFS_I(ino)->layout;
749 lo->plh_block_lgets--;
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400750 pnfs_put_layout_hdr_locked(lo);
Fred Isamanf7e89172011-01-06 11:36:32 +0000751 spin_unlock(&ino->i_lock);
752}
753
754void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
755{
756 struct pnfs_layout_hdr *lo;
757
758 spin_lock(&ino->i_lock);
759 lo = NFS_I(ino)->layout;
760 if ((int)(barrier - lo->plh_barrier) > 0)
761 lo->plh_barrier = barrier;
762 spin_unlock(&ino->i_lock);
763}
764
765bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
766{
767 struct nfs_inode *nfsi = NFS_I(ino);
768 struct pnfs_layout_segment *lseg;
769 bool found = false;
770
771 spin_lock(&ino->i_lock);
772 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
773 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
774 found = true;
775 break;
776 }
777 if (!found) {
778 struct pnfs_layout_hdr *lo = nfsi->layout;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500779 u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +0000780
781 /* Since close does not return a layout stateid for use as
782 * a barrier, we choose the worst-case barrier.
783 */
784 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
785 }
786 spin_unlock(&ino->i_lock);
787 return found;
788}
789
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400790/*
791 * Compare two layout segments for sorting into layout cache.
792 * We want to preferentially return RW over RO layouts, so ensure those
793 * are seen first.
794 */
795static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300796cmp_layout(struct pnfs_layout_range *l1,
797 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400798{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300799 s64 d;
800
801 /* high offset > low offset */
802 d = l1->offset - l2->offset;
803 if (d)
804 return d;
805
806 /* short length > long length */
807 d = l2->length - l1->length;
808 if (d)
809 return d;
810
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400811 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300812 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400813}
814
Andy Adamson974cec82010-10-20 00:18:02 -0400815static void
816pnfs_insert_layout(struct pnfs_layout_hdr *lo,
817 struct pnfs_layout_segment *lseg)
818{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400819 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400820
Andy Adamson974cec82010-10-20 00:18:02 -0400821 dprintk("%s:Begin\n", __func__);
822
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000823 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000824 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300825 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400826 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000827 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400828 dprintk("%s: inserted lseg %p "
829 "iomode %d offset %llu length %llu before "
830 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000831 __func__, lseg, lseg->pls_range.iomode,
832 lseg->pls_range.offset, lseg->pls_range.length,
833 lp, lp->pls_range.iomode, lp->pls_range.offset,
834 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300835 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400836 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300837 list_add_tail(&lseg->pls_list, &lo->plh_segs);
838 dprintk("%s: inserted lseg %p "
839 "iomode %d offset %llu length %llu at tail\n",
840 __func__, lseg, lseg->pls_range.iomode,
841 lseg->pls_range.offset, lseg->pls_range.length);
842out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400843 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400844
845 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400846}
847
848static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400849alloc_init_layout_hdr(struct inode *ino,
850 struct nfs_open_context *ctx,
851 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400852{
853 struct pnfs_layout_hdr *lo;
854
Benny Halevy636fb9c2011-05-22 19:51:33 +0300855 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400856 if (!lo)
857 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000858 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000859 INIT_LIST_HEAD(&lo->plh_layouts);
860 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000861 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000862 lo->plh_inode = ino;
Peng Tao9fa40752011-07-30 20:52:32 -0400863 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
Benny Halevye5e94012010-10-20 00:18:01 -0400864 return lo;
865}
866
867static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400868pnfs_find_alloc_layout(struct inode *ino,
869 struct nfs_open_context *ctx,
870 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400871{
872 struct nfs_inode *nfsi = NFS_I(ino);
873 struct pnfs_layout_hdr *new = NULL;
874
875 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
876
877 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000878 if (nfsi->layout) {
879 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
880 return NULL;
881 else
882 return nfsi->layout;
883 }
Benny Halevye5e94012010-10-20 00:18:01 -0400884 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400885 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400886 spin_lock(&ino->i_lock);
887
888 if (likely(nfsi->layout == NULL)) /* Won the race? */
889 nfsi->layout = new;
890 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300891 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400892 return nfsi->layout;
893}
894
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400895/*
896 * iomode matching rules:
897 * iomode lseg match
898 * ----- ----- -----
899 * ANY READ true
900 * ANY RW true
901 * RW READ false
902 * RW RW true
903 * READ READ true
904 * READ RW true
905 */
906static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300907is_matching_lseg(struct pnfs_layout_range *ls_range,
908 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400909{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300910 struct pnfs_layout_range range1;
911
912 if ((range->iomode == IOMODE_RW &&
913 ls_range->iomode != IOMODE_RW) ||
914 !lo_seg_intersecting(ls_range, range))
915 return 0;
916
917 /* range1 covers only the first byte in the range */
918 range1 = *range;
919 range1.length = 1;
920 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400921}
922
923/*
924 * lookup range in layout
925 */
Benny Halevye5e94012010-10-20 00:18:01 -0400926static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300927pnfs_find_lseg(struct pnfs_layout_hdr *lo,
928 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400929{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400930 struct pnfs_layout_segment *lseg, *ret = NULL;
931
932 dprintk("%s:Begin\n", __func__);
933
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000934 assert_spin_locked(&lo->plh_inode->i_lock);
935 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000936 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300937 is_matching_lseg(&lseg->pls_range, range)) {
Trond Myklebust9369a432012-09-18 20:57:08 -0400938 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400939 break;
940 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400941 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400942 break;
943 }
944
945 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000946 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400947 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400948}
949
950/*
Andy Adamsond23d61c2012-05-23 05:02:37 -0400951 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
952 * to the MDS or over pNFS
953 *
954 * The nfs_inode read_io and write_io fields are cumulative counters reset
955 * when there are no layout segments. Note that in pnfs_update_layout iomode
956 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
957 * WRITE request.
958 *
959 * A return of true means use MDS I/O.
960 *
961 * From rfc 5661:
962 * If a file's size is smaller than the file size threshold, data accesses
963 * SHOULD be sent to the metadata server. If an I/O request has a length that
964 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
965 * server. If both file size and I/O size are provided, the client SHOULD
966 * reach or exceed both thresholds before sending its read or write
967 * requests to the data server.
968 */
969static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
970 struct inode *ino, int iomode)
971{
972 struct nfs4_threshold *t = ctx->mdsthreshold;
973 struct nfs_inode *nfsi = NFS_I(ino);
974 loff_t fsize = i_size_read(ino);
975 bool size = false, size_set = false, io = false, io_set = false, ret = false;
976
977 if (t == NULL)
978 return ret;
979
980 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
981 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
982
983 switch (iomode) {
984 case IOMODE_READ:
985 if (t->bm & THRESHOLD_RD) {
986 dprintk("%s fsize %llu\n", __func__, fsize);
987 size_set = true;
988 if (fsize < t->rd_sz)
989 size = true;
990 }
991 if (t->bm & THRESHOLD_RD_IO) {
992 dprintk("%s nfsi->read_io %llu\n", __func__,
993 nfsi->read_io);
994 io_set = true;
995 if (nfsi->read_io < t->rd_io_sz)
996 io = true;
997 }
998 break;
999 case IOMODE_RW:
1000 if (t->bm & THRESHOLD_WR) {
1001 dprintk("%s fsize %llu\n", __func__, fsize);
1002 size_set = true;
1003 if (fsize < t->wr_sz)
1004 size = true;
1005 }
1006 if (t->bm & THRESHOLD_WR_IO) {
1007 dprintk("%s nfsi->write_io %llu\n", __func__,
1008 nfsi->write_io);
1009 io_set = true;
1010 if (nfsi->write_io < t->wr_io_sz)
1011 io = true;
1012 }
1013 break;
1014 }
1015 if (size_set && io_set) {
1016 if (size && io)
1017 ret = true;
1018 } else if (size || io)
1019 ret = true;
1020
1021 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1022 return ret;
1023}
1024
1025/*
Benny Halevye5e94012010-10-20 00:18:01 -04001026 * Layout segment is retreived from the server if not cached.
1027 * The appropriate layout segment is referenced and returned to the caller.
1028 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001029struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001030pnfs_update_layout(struct inode *ino,
1031 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001032 loff_t pos,
1033 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001034 enum pnfs_iomode iomode,
1035 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001036{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001037 struct pnfs_layout_range arg = {
1038 .iomode = iomode,
1039 .offset = pos,
1040 .length = count,
1041 };
Benny Halevy707ed5f2011-05-22 19:47:46 +03001042 unsigned pg_offset;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001043 struct nfs_server *server = NFS_SERVER(ino);
1044 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -04001045 struct pnfs_layout_hdr *lo;
1046 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001047 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001048
1049 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001050 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001051
1052 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001053 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001054
Benny Halevye5e94012010-10-20 00:18:01 -04001055 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001056 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001057 if (lo == NULL)
Benny Halevye5e94012010-10-20 00:18:01 -04001058 goto out_unlock;
Benny Halevye5e94012010-10-20 00:18:01 -04001059
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001060 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001061 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001062 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001063 goto out_unlock;
1064 }
1065
1066 /* if LAYOUTGET already failed once we don't try again */
Trond Myklebustb9e028f2012-09-18 16:41:18 -04001067 if (pnfs_layout_io_test_failed(lo, iomode))
Benny Halevye5e94012010-10-20 00:18:01 -04001068 goto out_unlock;
1069
Andy Adamson568e8c42011-03-01 01:34:22 +00001070 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001071 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +00001072 if (lseg)
1073 goto out_unlock;
1074
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001075 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +00001076 goto out_unlock;
1077 atomic_inc(&lo->plh_outstanding);
1078
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001079 pnfs_get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001080 if (list_empty(&lo->plh_segs))
1081 first = true;
Andy Adamson366d5052012-06-20 15:03:33 -04001082
1083 /* Enable LAYOUTRETURNs */
1084 pnfs_clear_layout_returned(lo);
1085
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001086 spin_unlock(&ino->i_lock);
1087 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001088 /* The lo must be on the clp list if there is any
1089 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1090 */
1091 spin_lock(&clp->cl_lock);
1092 BUG_ON(!list_empty(&lo->plh_layouts));
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001093 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001094 spin_unlock(&clp->cl_lock);
1095 }
Benny Halevye5e94012010-10-20 00:18:01 -04001096
Benny Halevy707ed5f2011-05-22 19:47:46 +03001097 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1098 if (pg_offset) {
1099 arg.offset -= pg_offset;
1100 arg.length += pg_offset;
1101 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001102 if (arg.length != NFS4_MAX_UINT64)
1103 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001104
Benny Halevyfb3296e2011-05-22 19:47:26 +03001105 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001106 if (!lseg && first) {
1107 spin_lock(&clp->cl_lock);
1108 list_del_init(&lo->plh_layouts);
1109 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +00001110 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001111 atomic_dec(&lo->plh_outstanding);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001112 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001113out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001114 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1115 "(%s, offset: %llu, length: %llu)\n",
1116 __func__, ino->i_sb->s_id,
1117 (unsigned long long)NFS_FILEID(ino),
1118 lseg == NULL ? "not found" : "found",
1119 iomode==IOMODE_RW ? "read/write" : "read-only",
1120 (unsigned long long)pos,
1121 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001122 return lseg;
1123out_unlock:
1124 spin_unlock(&ino->i_lock);
1125 goto out;
1126}
Andy Adamson7c24d942011-06-13 18:22:38 -04001127EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001128
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001129struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001130pnfs_layout_process(struct nfs4_layoutget *lgp)
1131{
1132 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1133 struct nfs4_layoutget_res *res = &lgp->res;
1134 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001135 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001136 int status = 0;
1137
1138 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001139 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001140 if (!lseg || IS_ERR(lseg)) {
1141 if (!lseg)
1142 status = -ENOMEM;
1143 else
1144 status = PTR_ERR(lseg);
1145 dprintk("%s: Could not allocate layout: error %d\n",
1146 __func__, status);
1147 goto out;
1148 }
1149
1150 spin_lock(&ino->i_lock);
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001151 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001152 dprintk("%s forget reply due to recall\n", __func__);
1153 goto out_forget_reply;
1154 }
1155
1156 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1157 dprintk("%s forget reply due to state\n", __func__);
1158 goto out_forget_reply;
1159 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001160 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001161 lseg->pls_range = res->range;
Trond Myklebust9369a432012-09-18 20:57:08 -04001162 pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001163 pnfs_insert_layout(lo, lseg);
1164
Fred Isamanf7e89172011-01-06 11:36:32 +00001165 if (res->return_on_close) {
1166 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1167 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1168 }
1169
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001170 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001171 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001172 spin_unlock(&ino->i_lock);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001173 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001174out:
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001175 return ERR_PTR(status);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001176
1177out_forget_reply:
1178 spin_unlock(&ino->i_lock);
1179 lseg->pls_layout = lo;
1180 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1181 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001182}
1183
Trond Myklebustd8007d42011-06-10 13:30:23 -04001184void
1185pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1186{
1187 BUG_ON(pgio->pg_lseg != NULL);
1188
Fred Isaman1825a0d2012-04-20 19:55:31 -04001189 if (req->wb_offset != req->wb_pgbase) {
1190 nfs_pageio_reset_read_mds(pgio);
1191 return;
1192 }
Trond Myklebustd8007d42011-06-10 13:30:23 -04001193 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1194 req->wb_context,
1195 req_offset(req),
1196 req->wb_bytes,
1197 IOMODE_READ,
1198 GFP_KERNEL);
Trond Myklebuste885de12011-06-10 13:30:23 -04001199 /* If no lseg, fall back to read through mds */
1200 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001201 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001202
Trond Myklebustd8007d42011-06-10 13:30:23 -04001203}
1204EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1205
1206void
1207pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1208{
1209 BUG_ON(pgio->pg_lseg != NULL);
1210
Fred Isaman1825a0d2012-04-20 19:55:31 -04001211 if (req->wb_offset != req->wb_pgbase) {
1212 nfs_pageio_reset_write_mds(pgio);
1213 return;
1214 }
Trond Myklebustd8007d42011-06-10 13:30:23 -04001215 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1216 req->wb_context,
1217 req_offset(req),
1218 req->wb_bytes,
1219 IOMODE_RW,
1220 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001221 /* If no lseg, fall back to write through mds */
1222 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001223 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001224}
1225EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1226
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001227void
Fred Isaman061ae2e2012-04-20 14:47:48 -04001228pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1229 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -04001230{
1231 struct nfs_server *server = NFS_SERVER(inode);
1232 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1233
1234 if (ld == NULL)
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001235 nfs_pageio_init_read(pgio, inode, compl_ops);
1236 else
1237 nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0);
Trond Myklebust1751c362011-06-10 13:30:23 -04001238}
1239
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001240void
Fred Isaman061ae2e2012-04-20 14:47:48 -04001241pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1242 int ioflags,
1243 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -04001244{
1245 struct nfs_server *server = NFS_SERVER(inode);
1246 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1247
1248 if (ld == NULL)
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001249 nfs_pageio_init_write(pgio, inode, ioflags, compl_ops);
1250 else
1251 nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags);
Trond Myklebust1751c362011-06-10 13:30:23 -04001252}
1253
1254bool
Benny Halevydfed2062011-05-25 20:25:22 +03001255pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1256 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001257{
Trond Myklebustd8007d42011-06-10 13:30:23 -04001258 if (pgio->pg_lseg == NULL)
1259 return nfs_generic_pg_test(pgio, prev, req);
Benny Halevy89a58e32011-05-25 20:54:40 +03001260
Trond Myklebust19982ba2011-06-10 13:30:23 -04001261 /*
1262 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1263 * Note that this test makes several assumptions:
1264 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1265 * is known to lie within the range.
1266 * - that the nfs_page being tested is known to be contiguous with the
1267 * previous nfs_page.
1268 * - Layout ranges are page aligned, so we only have to test the
1269 * start offset of the request.
1270 *
1271 * Please also note that 'end_offset' is actually the offset of the
1272 * first byte that lies outside the pnfs_layout_range. FIXME?
1273 *
1274 */
1275 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1276 pgio->pg_lseg->pls_range.length);
Fred Isamanbae724e2011-03-01 01:34:15 +00001277}
Benny Halevy89a58e32011-05-25 20:54:40 +03001278EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001279
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001280int pnfs_write_done_resend_to_mds(struct inode *inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001281 struct list_head *head,
1282 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001283{
1284 struct nfs_pageio_descriptor pgio;
1285 LIST_HEAD(failed);
1286
1287 /* Resend all requests through the MDS */
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001288 nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001289 while (!list_empty(head)) {
1290 struct nfs_page *req = nfs_list_entry(head->next);
1291
1292 nfs_list_remove_request(req);
1293 if (!nfs_pageio_add_request(&pgio, req))
1294 nfs_list_add_request(req, &failed);
1295 }
1296 nfs_pageio_complete(&pgio);
1297
1298 if (!list_empty(&failed)) {
1299 /* For some reason our attempt to resend pages. Mark the
1300 * overall send request as having failed, and let
1301 * nfs_writeback_release_full deal with the error.
1302 */
1303 list_move(&failed, head);
1304 return -EIO;
1305 }
1306 return 0;
1307}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001308EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001309
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001310static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
1311{
Fred Isamancd841602012-04-20 14:47:44 -04001312 struct nfs_pgio_header *hdr = data->header;
1313
1314 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1315 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001316 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001317 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1318 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001319 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001320 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1321 data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001322 &hdr->pages,
1323 hdr->completion_ops);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001324}
1325
Benny Halevyd20581a2011-05-22 19:52:03 +03001326/*
1327 * Called by non rpc-based layout drivers
1328 */
Peng Tao8ce160c2011-09-22 21:50:14 -04001329void pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001330{
Fred Isamancd841602012-04-20 14:47:44 -04001331 struct nfs_pgio_header *hdr = data->header;
1332
1333 if (!hdr->pnfs_error) {
Benny Halevyd20581a2011-05-22 19:52:03 +03001334 pnfs_set_layoutcommit(data);
Fred Isamancd841602012-04-20 14:47:44 -04001335 hdr->mds_ops->rpc_call_done(&data->task, data);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001336 } else
1337 pnfs_ld_handle_write_error(data);
Fred Isamancd841602012-04-20 14:47:44 -04001338 hdr->mds_ops->rpc_release(data);
Fred Isaman44b83792011-03-03 15:13:44 +00001339}
Benny Halevyd20581a2011-05-22 19:52:03 +03001340EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001341
Trond Myklebustdce81292011-07-13 15:59:19 -04001342static void
1343pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1344 struct nfs_write_data *data)
1345{
Fred Isamancd841602012-04-20 14:47:44 -04001346 struct nfs_pgio_header *hdr = data->header;
1347
Fred Isaman6c75dc02012-04-20 14:47:47 -04001348 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1349 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1350 nfs_pageio_reset_write_mds(desc);
1351 desc->pg_recoalesce = 1;
1352 }
Trond Myklebustdce81292011-07-13 15:59:19 -04001353 nfs_writedata_release(data);
1354}
1355
1356static enum pnfs_try_status
Andy Adamson0382b742011-03-03 15:13:45 +00001357pnfs_try_to_write_data(struct nfs_write_data *wdata,
Trond Myklebustdce81292011-07-13 15:59:19 -04001358 const struct rpc_call_ops *call_ops,
1359 struct pnfs_layout_segment *lseg,
1360 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001361{
Fred Isamancd841602012-04-20 14:47:44 -04001362 struct nfs_pgio_header *hdr = wdata->header;
1363 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00001364 enum pnfs_try_status trypnfs;
1365 struct nfs_server *nfss = NFS_SERVER(inode);
1366
Fred Isamancd841602012-04-20 14:47:44 -04001367 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00001368
1369 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1370 inode->i_ino, wdata->args.count, wdata->args.offset, how);
Andy Adamson0382b742011-03-03 15:13:45 +00001371 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001372 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00001373 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00001374 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1375 return trypnfs;
1376}
1377
Trond Myklebustdce81292011-07-13 15:59:19 -04001378static void
1379pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1380{
1381 struct nfs_write_data *data;
1382 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1383 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1384
1385 desc->pg_lseg = NULL;
1386 while (!list_empty(head)) {
1387 enum pnfs_try_status trypnfs;
1388
Fred Isaman6c75dc02012-04-20 14:47:47 -04001389 data = list_first_entry(head, struct nfs_write_data, list);
Trond Myklebustdce81292011-07-13 15:59:19 -04001390 list_del_init(&data->list);
1391
1392 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1393 if (trypnfs == PNFS_NOT_ATTEMPTED)
1394 pnfs_write_through_mds(desc, data);
1395 }
Trond Myklebust9369a432012-09-18 20:57:08 -04001396 pnfs_put_lseg(lseg);
Trond Myklebustdce81292011-07-13 15:59:19 -04001397}
1398
Fred Isaman6c75dc02012-04-20 14:47:47 -04001399static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1400{
Trond Myklebust9369a432012-09-18 20:57:08 -04001401 pnfs_put_lseg(hdr->lseg);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001402 nfs_writehdr_free(hdr);
1403}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001404EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001405
Trond Myklebustdce81292011-07-13 15:59:19 -04001406int
1407pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1408{
Fred Isaman6c75dc02012-04-20 14:47:47 -04001409 struct nfs_write_header *whdr;
1410 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04001411 int ret;
1412
Fred Isaman6c75dc02012-04-20 14:47:47 -04001413 whdr = nfs_writehdr_alloc();
1414 if (!whdr) {
Trond Myklebust9b5415b2012-04-27 14:31:47 -04001415 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Trond Myklebust9369a432012-09-18 20:57:08 -04001416 pnfs_put_lseg(desc->pg_lseg);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001417 desc->pg_lseg = NULL;
1418 return -ENOMEM;
1419 }
1420 hdr = &whdr->header;
1421 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04001422 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001423 atomic_inc(&hdr->refcnt);
1424 ret = nfs_generic_flush(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001425 if (ret != 0) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001426 pnfs_put_lseg(desc->pg_lseg);
Trond Myklebustdce81292011-07-13 15:59:19 -04001427 desc->pg_lseg = NULL;
Fred Isaman6c75dc02012-04-20 14:47:47 -04001428 } else
1429 pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
1430 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -04001431 hdr->completion_ops->completion(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001432 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04001433}
1434EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1435
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001436int pnfs_read_done_resend_to_mds(struct inode *inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001437 struct list_head *head,
1438 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust62e4a762011-11-10 14:30:37 -05001439{
1440 struct nfs_pageio_descriptor pgio;
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001441 LIST_HEAD(failed);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001442
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001443 /* Resend all requests through the MDS */
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001444 nfs_pageio_init_read(&pgio, inode, compl_ops);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001445 while (!list_empty(head)) {
1446 struct nfs_page *req = nfs_list_entry(head->next);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001447
1448 nfs_list_remove_request(req);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001449 if (!nfs_pageio_add_request(&pgio, req))
1450 nfs_list_add_request(req, &failed);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001451 }
1452 nfs_pageio_complete(&pgio);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001453
1454 if (!list_empty(&failed)) {
1455 list_move(&failed, head);
1456 return -EIO;
1457 }
1458 return 0;
1459}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001460EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001461
1462static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
1463{
Fred Isamancd841602012-04-20 14:47:44 -04001464 struct nfs_pgio_header *hdr = data->header;
1465
1466 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1467 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001468 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001469 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1470 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001471 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001472 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1473 data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001474 &hdr->pages,
1475 hdr->completion_ops);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001476}
1477
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001478/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001479 * Called by non rpc-based layout drivers
1480 */
Peng Tao9b7eecd2011-09-22 21:50:15 -04001481void pnfs_ld_read_done(struct nfs_read_data *data)
Benny Halevyd20581a2011-05-22 19:52:03 +03001482{
Fred Isamancd841602012-04-20 14:47:44 -04001483 struct nfs_pgio_header *hdr = data->header;
1484
1485 if (likely(!hdr->pnfs_error)) {
Benny Halevyd20581a2011-05-22 19:52:03 +03001486 __nfs4_read_done_cb(data);
Fred Isamancd841602012-04-20 14:47:44 -04001487 hdr->mds_ops->rpc_call_done(&data->task, data);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001488 } else
1489 pnfs_ld_handle_read_error(data);
Fred Isamancd841602012-04-20 14:47:44 -04001490 hdr->mds_ops->rpc_release(data);
Benny Halevyd20581a2011-05-22 19:52:03 +03001491}
1492EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1493
Trond Myklebust493292d2011-07-13 15:58:28 -04001494static void
1495pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1496 struct nfs_read_data *data)
1497{
Fred Isamancd841602012-04-20 14:47:44 -04001498 struct nfs_pgio_header *hdr = data->header;
1499
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001500 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1501 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1502 nfs_pageio_reset_read_mds(desc);
1503 desc->pg_recoalesce = 1;
1504 }
Trond Myklebust493292d2011-07-13 15:58:28 -04001505 nfs_readdata_release(data);
1506}
1507
Benny Halevyd20581a2011-05-22 19:52:03 +03001508/*
Andy Adamson64419a92011-03-01 01:34:16 +00001509 * Call the appropriate parallel I/O subsystem read function.
1510 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001511static enum pnfs_try_status
Andy Adamson64419a92011-03-01 01:34:16 +00001512pnfs_try_to_read_data(struct nfs_read_data *rdata,
Trond Myklebust493292d2011-07-13 15:58:28 -04001513 const struct rpc_call_ops *call_ops,
1514 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001515{
Fred Isamancd841602012-04-20 14:47:44 -04001516 struct nfs_pgio_header *hdr = rdata->header;
1517 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00001518 struct nfs_server *nfss = NFS_SERVER(inode);
1519 enum pnfs_try_status trypnfs;
1520
Fred Isamancd841602012-04-20 14:47:44 -04001521 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00001522
1523 dprintk("%s: Reading ino:%lu %u@%llu\n",
1524 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1525
1526 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001527 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00001528 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00001529 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1530 return trypnfs;
1531}
Andy Adamson863a3c62011-03-23 13:27:54 +00001532
Trond Myklebust493292d2011-07-13 15:58:28 -04001533static void
1534pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1535{
1536 struct nfs_read_data *data;
1537 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1538 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1539
1540 desc->pg_lseg = NULL;
1541 while (!list_empty(head)) {
1542 enum pnfs_try_status trypnfs;
1543
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001544 data = list_first_entry(head, struct nfs_read_data, list);
Trond Myklebust493292d2011-07-13 15:58:28 -04001545 list_del_init(&data->list);
1546
1547 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1548 if (trypnfs == PNFS_NOT_ATTEMPTED)
1549 pnfs_read_through_mds(desc, data);
1550 }
Trond Myklebust9369a432012-09-18 20:57:08 -04001551 pnfs_put_lseg(lseg);
Trond Myklebust493292d2011-07-13 15:58:28 -04001552}
1553
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001554static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1555{
Trond Myklebust9369a432012-09-18 20:57:08 -04001556 pnfs_put_lseg(hdr->lseg);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001557 nfs_readhdr_free(hdr);
1558}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001559EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001560
Trond Myklebust493292d2011-07-13 15:58:28 -04001561int
1562pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1563{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001564 struct nfs_read_header *rhdr;
1565 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04001566 int ret;
1567
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001568 rhdr = nfs_readhdr_alloc();
1569 if (!rhdr) {
Fred Isaman061ae2e2012-04-20 14:47:48 -04001570 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001571 ret = -ENOMEM;
Trond Myklebust9369a432012-09-18 20:57:08 -04001572 pnfs_put_lseg(desc->pg_lseg);
Trond Myklebust493292d2011-07-13 15:58:28 -04001573 desc->pg_lseg = NULL;
1574 return ret;
1575 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001576 hdr = &rhdr->header;
1577 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04001578 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001579 atomic_inc(&hdr->refcnt);
1580 ret = nfs_generic_pagein(desc, hdr);
1581 if (ret != 0) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001582 pnfs_put_lseg(desc->pg_lseg);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001583 desc->pg_lseg = NULL;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001584 } else
1585 pnfs_do_multiple_reads(desc, &hdr->rpc_list);
1586 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -04001587 hdr->completion_ops->completion(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001588 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04001589}
1590EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1591
Andy Adamson863a3c62011-03-23 13:27:54 +00001592/*
Peng Taoa9bae562011-07-30 20:52:33 -04001593 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00001594 */
Peng Taoa9bae562011-07-30 20:52:33 -04001595static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00001596{
Peng Taoa9bae562011-07-30 20:52:33 -04001597 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00001598
Peng Taoa9bae562011-07-30 20:52:33 -04001599 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1600 if (lseg->pls_range.iomode == IOMODE_RW &&
1601 test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1602 list_add(&lseg->pls_lc_list, listp);
1603 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001604}
1605
Peng Tao1b0ae062011-09-22 21:50:12 -04001606void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1607{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04001608 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04001609}
1610EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1611
Andy Adamson863a3c62011-03-23 13:27:54 +00001612void
1613pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1614{
Fred Isamancd841602012-04-20 14:47:44 -04001615 struct nfs_pgio_header *hdr = wdata->header;
1616 struct inode *inode = hdr->inode;
1617 struct nfs_inode *nfsi = NFS_I(inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001618 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001619 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001620
Fred Isamancd841602012-04-20 14:47:44 -04001621 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001622 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001623 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001624 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04001625 __func__, inode->i_ino);
Andy Adamson863a3c62011-03-23 13:27:54 +00001626 }
Fred Isamancd841602012-04-20 14:47:44 -04001627 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04001628 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust9369a432012-09-18 20:57:08 -04001629 pnfs_get_lseg(hdr->lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04001630 }
Peng Taoacff58802011-07-30 20:52:31 -04001631 if (end_pos > nfsi->layout->plh_lwb)
1632 nfsi->layout->plh_lwb = end_pos;
Fred Isamancd841602012-04-20 14:47:44 -04001633 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04001634 dprintk("%s: lseg %p end_pos %llu\n",
Fred Isamancd841602012-04-20 14:47:44 -04001635 __func__, hdr->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001636
1637 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1638 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1639 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04001640 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001641}
1642EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1643
Andy Adamsondb29c082011-07-30 20:52:38 -04001644void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1645{
1646 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1647
1648 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1649 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1650}
1651
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001652/*
1653 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1654 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1655 * data to disk to allow the server to recover the data if it crashes.
1656 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1657 * is off, and a COMMIT is sent to a data server, or
1658 * if WRITEs to a data server return NFS_DATA_SYNC.
1659 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001660int
Andy Adamsonef311532011-03-12 02:58:10 -05001661pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001662{
1663 struct nfs4_layoutcommit_data *data;
1664 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001665 loff_t end_pos;
1666 int status = 0;
1667
1668 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1669
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001670 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1671 return 0;
1672
Andy Adamson863a3c62011-03-23 13:27:54 +00001673 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1674 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001675 if (!data) {
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001676 status = -ENOMEM;
1677 goto out;
1678 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001679
Peng Tao92407e72011-10-23 20:21:17 -07001680 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1681 goto out_free;
1682
1683 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1684 if (!sync) {
1685 status = -EAGAIN;
1686 goto out_free;
1687 }
1688 status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
1689 nfs_wait_bit_killable, TASK_KILLABLE);
1690 if (status)
1691 goto out_free;
1692 }
1693
Peng Taoa9bae562011-07-30 20:52:33 -04001694 INIT_LIST_HEAD(&data->lseg_list);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001695 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001696 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Peng Tao92407e72011-10-23 20:21:17 -07001697 clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
Andy Adamson863a3c62011-03-23 13:27:54 +00001698 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07001699 wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
1700 goto out_free;
Andy Adamson863a3c62011-03-23 13:27:54 +00001701 }
Peng Taoa9bae562011-07-30 20:52:33 -04001702
1703 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00001704
Peng Taoacff58802011-07-30 20:52:31 -04001705 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04001706 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00001707
Trond Myklebustf597c532012-03-04 18:13:56 -05001708 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00001709 spin_unlock(&inode->i_lock);
1710
1711 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04001712 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00001713 nfs_fattr_init(&data->fattr);
1714 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1715 data->res.fattr = &data->fattr;
1716 data->args.lastbytewritten = end_pos - 1;
1717 data->res.server = NFS_SERVER(inode);
1718
1719 status = nfs4_proc_layoutcommit(data, sync);
1720out:
Peng Tao92407e72011-10-23 20:21:17 -07001721 if (status)
1722 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001723 dprintk("<-- %s status %d\n", __func__, status);
1724 return status;
Peng Tao92407e72011-10-23 20:21:17 -07001725out_free:
1726 kfree(data);
1727 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00001728}
Andy Adamson82be4172012-05-23 05:02:35 -04001729
1730struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
1731{
1732 struct nfs4_threshold *thp;
1733
1734 thp = kzalloc(sizeof(*thp), GFP_NOFS);
1735 if (!thp) {
1736 dprintk("%s mdsthreshold allocation failed\n", __func__);
1737 return NULL;
1738 }
1739 return thp;
1740}