blob: 4f802b02fbb9b0c6fc61912be044f6cef94acd32 [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"
Trond Myklebustcc668ab2013-08-14 15:31:28 -040036#include "nfs4trace.h"
Trond Myklebust40dd4b72015-01-24 13:54:37 -050037#include "delegation.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040038
39#define NFSDBG_FACILITY NFSDBG_PNFS
Trond Myklebust25c75332012-09-18 17:01:12 -040040#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040041
Fred Isaman02c35fc2010-10-20 00:17:59 -040042/* Locking:
43 *
44 * pnfs_spinlock:
45 * protects pnfs_modules_tbl.
46 */
47static DEFINE_SPINLOCK(pnfs_spinlock);
48
49/*
50 * pnfs_modules_tbl holds all pnfs modules
51 */
52static LIST_HEAD(pnfs_modules_tbl);
53
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080054static int
55pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
Peng Tao6c166052014-11-17 09:30:40 +080056 enum pnfs_iomode iomode, bool sync);
Peng Taoaa1e0e3a2014-09-06 00:53:25 +080057
Fred Isaman02c35fc2010-10-20 00:17:59 -040058/* Return the registered pnfs layout driver module matching given id */
59static struct pnfs_layoutdriver_type *
60find_pnfs_driver_locked(u32 id)
61{
62 struct pnfs_layoutdriver_type *local;
63
64 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
65 if (local->id == id)
66 goto out;
67 local = NULL;
68out:
69 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
70 return local;
71}
72
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073static struct pnfs_layoutdriver_type *
74find_pnfs_driver(u32 id)
75{
Fred Isaman02c35fc2010-10-20 00:17:59 -040076 struct pnfs_layoutdriver_type *local;
77
78 spin_lock(&pnfs_spinlock);
79 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040080 if (local != NULL && !try_module_get(local->owner)) {
81 dprintk("%s: Could not grab reference on module\n", __func__);
82 local = NULL;
83 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040084 spin_unlock(&pnfs_spinlock);
85 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040086}
87
88void
89unset_pnfs_layoutdriver(struct nfs_server *nfss)
90{
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 if (nfss->pnfs_curr_ld) {
92 if (nfss->pnfs_curr_ld->clear_layoutdriver)
93 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040094 /* Decrement the MDS count. Purge the deviceid cache if zero */
95 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
96 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040097 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040098 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040099 nfss->pnfs_curr_ld = NULL;
100}
101
102/*
103 * Try to set the server's pnfs module to the pnfs layout type specified by id.
104 * Currently only one pNFS layout driver per filesystem is supported.
105 *
106 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
107 */
108void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400109set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
110 u32 id)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400111{
112 struct pnfs_layoutdriver_type *ld_type = NULL;
113
114 if (id == 0)
115 goto out_no_driver;
116 if (!(server->nfs_client->cl_exchange_flags &
117 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500118 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
119 __func__, id, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400120 goto out_no_driver;
121 }
122 ld_type = find_pnfs_driver(id);
123 if (!ld_type) {
124 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
125 ld_type = find_pnfs_driver(id);
126 if (!ld_type) {
127 dprintk("%s: No pNFS module found for %u.\n",
128 __func__, id);
129 goto out_no_driver;
130 }
131 }
132 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400133 if (ld_type->set_layoutdriver
134 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500135 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
136 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400137 module_put(ld_type->owner);
138 goto out_no_driver;
139 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400140 /* Bump the MDS count */
141 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000142
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400143 dprintk("%s: pNFS module for %u set\n", __func__, id);
144 return;
145
146out_no_driver:
147 dprintk("%s: Using NFSv4 I/O\n", __func__);
148 server->pnfs_curr_ld = NULL;
149}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400150
151int
152pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
153{
154 int status = -EINVAL;
155 struct pnfs_layoutdriver_type *tmp;
156
157 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500158 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400159 return status;
160 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400161 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500162 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400163 "alloc_lseg and free_lseg.\n", __func__);
164 return status;
165 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400166
167 spin_lock(&pnfs_spinlock);
168 tmp = find_pnfs_driver_locked(ld_type->id);
169 if (!tmp) {
170 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
171 status = 0;
172 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
173 ld_type->name);
174 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500175 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400176 __func__, ld_type->id);
177 }
178 spin_unlock(&pnfs_spinlock);
179
180 return status;
181}
182EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
183
184void
185pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
186{
187 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
188 spin_lock(&pnfs_spinlock);
189 list_del(&ld_type->pnfs_tblid);
190 spin_unlock(&pnfs_spinlock);
191}
192EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400193
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400194/*
195 * pNFS client layout cache
196 */
197
Fred Isamancc6e5342011-01-06 11:36:28 +0000198/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000199void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400200pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400201{
Fred Isamancc6e5342011-01-06 11:36:28 +0000202 atomic_inc(&lo->plh_refcount);
203}
204
Benny Halevy636fb9c2011-05-22 19:51:33 +0300205static struct pnfs_layout_hdr *
206pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
207{
208 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
Trond Myklebust57934272012-09-20 20:37:23 -0400209 return ld->alloc_layout_hdr(ino, gfp_flags);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300210}
211
212static void
213pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
214{
Trond Myklebust9c626382012-09-20 17:31:43 -0400215 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
216 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
217
218 if (!list_empty(&lo->plh_layouts)) {
219 struct nfs_client *clp = server->nfs_client;
220
221 spin_lock(&clp->cl_lock);
222 list_del_init(&lo->plh_layouts);
223 spin_unlock(&clp->cl_lock);
224 }
Peng Tao9fa40752011-07-30 20:52:32 -0400225 put_rpccred(lo->plh_lc_cred);
Trond Myklebust57934272012-09-20 20:37:23 -0400226 return ld->free_layout_hdr(lo);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300227}
228
Fred Isamancc6e5342011-01-06 11:36:28 +0000229static void
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400230pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
Fred Isamancc6e5342011-01-06 11:36:28 +0000231{
Trond Myklebustbb346f62012-09-20 15:52:13 -0400232 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
Fred Isamancc6e5342011-01-06 11:36:28 +0000233 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Trond Myklebustbb346f62012-09-20 15:52:13 -0400234 nfsi->layout = NULL;
235 /* Reset MDS Threshold I/O counters */
236 nfsi->write_io = 0;
237 nfsi->read_io = 0;
Benny Halevye5e94012010-10-20 00:18:01 -0400238}
239
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400240void
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400241pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400242{
Fred Isamancc6e5342011-01-06 11:36:28 +0000243 struct inode *inode = lo->plh_inode;
244
245 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
Peng Tao566f8732014-10-10 23:25:46 +0800246 if (!list_empty(&lo->plh_segs))
247 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400248 pnfs_detach_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000249 spin_unlock(&inode->i_lock);
Trond Myklebust6622c3e2012-09-20 17:23:11 -0400250 pnfs_free_layout_hdr(lo);
Fred Isamancc6e5342011-01-06 11:36:28 +0000251 }
Andy Adamson974cec82010-10-20 00:18:02 -0400252}
253
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400254static int
255pnfs_iomode_to_fail_bit(u32 iomode)
256{
257 return iomode == IOMODE_RW ?
258 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
259}
260
261static void
Trond Myklebust3e621212012-09-24 13:07:16 -0400262pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400263{
Trond Myklebust25c75332012-09-18 17:01:12 -0400264 lo->plh_retry_timestamp = jiffies;
Yanchuan Nian39e88fc2013-01-04 20:19:49 +0800265 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
Trond Myklebust3e621212012-09-24 13:07:16 -0400266 atomic_inc(&lo->plh_refcount);
267}
268
269static void
270pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
271{
272 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
273 atomic_dec(&lo->plh_refcount);
274}
275
276static void
277pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
278{
279 struct inode *inode = lo->plh_inode;
Trond Myklebust115ce572012-09-20 21:19:43 -0400280 struct pnfs_layout_range range = {
281 .iomode = iomode,
282 .offset = 0,
283 .length = NFS4_MAX_UINT64,
284 };
285 LIST_HEAD(head);
Trond Myklebust3e621212012-09-24 13:07:16 -0400286
287 spin_lock(&inode->i_lock);
288 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
Trond Myklebust115ce572012-09-20 21:19:43 -0400289 pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
Trond Myklebust3e621212012-09-24 13:07:16 -0400290 spin_unlock(&inode->i_lock);
Trond Myklebust115ce572012-09-20 21:19:43 -0400291 pnfs_free_lseg_list(&head);
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400292 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
293 iomode == IOMODE_RW ? "RW" : "READ");
294}
295
296static bool
297pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
298{
Trond Myklebust25c75332012-09-18 17:01:12 -0400299 unsigned long start, end;
Trond Myklebust3e621212012-09-24 13:07:16 -0400300 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
301
302 if (test_bit(fail_bit, &lo->plh_flags) == 0)
Trond Myklebust25c75332012-09-18 17:01:12 -0400303 return false;
304 end = jiffies;
305 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
306 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
307 /* It is time to retry the failed layoutgets */
Trond Myklebust3e621212012-09-24 13:07:16 -0400308 pnfs_layout_clear_fail_bit(lo, fail_bit);
Trond Myklebust25c75332012-09-18 17:01:12 -0400309 return false;
310 }
311 return true;
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400312}
313
Andy Adamson974cec82010-10-20 00:18:02 -0400314static void
315init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
316{
Fred Isaman566052c2011-01-06 11:36:20 +0000317 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400318 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000319 atomic_set(&lseg->pls_refcount, 1);
320 smp_mb();
321 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000322 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400323}
324
Trond Myklebust905ca192012-09-20 20:46:49 -0400325static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400326{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000327 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400328
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400329 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400330}
331
Fred Isamand684d2a2011-03-01 01:34:13 +0000332static void
Trond Myklebust57036a32012-09-20 16:33:30 -0400333pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
334 struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400335{
Trond Myklebust57036a32012-09-20 16:33:30 -0400336 struct inode *inode = lo->plh_inode;
Fred Isamand684d2a2011-03-01 01:34:13 +0000337
Benny Halevyd20581a2011-05-22 19:52:03 +0300338 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000339 list_del_init(&lseg->pls_list);
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400340 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
341 atomic_dec(&lo->plh_refcount);
Trond Myklebust173f77e2012-09-21 15:49:42 -0400342 if (list_empty(&lo->plh_segs))
343 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isamand684d2a2011-03-01 01:34:13 +0000344 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
345}
346
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800347/* Return true if layoutreturn is needed */
348static bool
349pnfs_layout_need_return(struct pnfs_layout_hdr *lo,
Peng Tao27b6f532014-10-20 14:44:38 +0800350 struct pnfs_layout_segment *lseg)
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800351{
352 struct pnfs_layout_segment *s;
353
354 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
355 return false;
356
357 list_for_each_entry(s, &lo->plh_segs, pls_list)
Peng Tao27b6f532014-10-20 14:44:38 +0800358 if (s != lseg && test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800359 return false;
360
Peng Taoaa1e0e3a2014-09-06 00:53:25 +0800361 return true;
362}
363
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500364static void pnfs_layoutreturn_before_put_lseg(struct pnfs_layout_segment *lseg,
365 struct pnfs_layout_hdr *lo, struct inode *inode)
Peng Tao27b6f532014-10-20 14:44:38 +0800366{
Peng Tao27b6f532014-10-20 14:44:38 +0800367 lo = lseg->pls_layout;
368 inode = lo->plh_inode;
369
370 spin_lock(&inode->i_lock);
371 if (pnfs_layout_need_return(lo, lseg)) {
372 nfs4_stateid stateid;
373 enum pnfs_iomode iomode;
374
375 stateid = lo->plh_stateid;
376 iomode = lo->plh_return_iomode;
377 /* decreased in pnfs_send_layoutreturn() */
378 lo->plh_block_lgets++;
379 lo->plh_return_iomode = 0;
380 spin_unlock(&inode->i_lock);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500381 pnfs_get_layout_hdr(lo);
Peng Tao27b6f532014-10-20 14:44:38 +0800382
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500383 /* Send an async layoutreturn so we dont deadlock */
384 pnfs_send_layoutreturn(lo, stateid, iomode, false);
Peng Tao27b6f532014-10-20 14:44:38 +0800385 } else
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500386 spin_unlock(&inode->i_lock);
Peng Tao27b6f532014-10-20 14:44:38 +0800387}
388
Fred Isamanbae724e2011-03-01 01:34:15 +0000389void
Trond Myklebust9369a432012-09-18 20:57:08 -0400390pnfs_put_lseg(struct pnfs_layout_segment *lseg)
Fred Isamand684d2a2011-03-01 01:34:13 +0000391{
Trond Myklebust57036a32012-09-20 16:33:30 -0400392 struct pnfs_layout_hdr *lo;
Fred Isamand684d2a2011-03-01 01:34:13 +0000393 struct inode *inode;
394
395 if (!lseg)
396 return;
397
Fred Isaman4541d162011-01-06 11:36:23 +0000398 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
399 atomic_read(&lseg->pls_refcount),
400 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500401
402 /* Handle the case where refcount != 1 */
403 if (atomic_add_unless(&lseg->pls_refcount, -1, 1))
404 return;
405
Trond Myklebust57036a32012-09-20 16:33:30 -0400406 lo = lseg->pls_layout;
407 inode = lo->plh_inode;
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500408 /* Do we need a layoutreturn? */
409 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
410 pnfs_layoutreturn_before_put_lseg(lseg, lo, inode);
411
Fred Isamand684d2a2011-03-01 01:34:13 +0000412 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
Trond Myklebust8f0d27d2012-09-20 20:57:11 -0400413 pnfs_get_layout_hdr(lo);
Trond Myklebust4ef2e4f2015-02-05 17:27:39 -0500414 pnfs_layout_remove_lseg(lo, lseg);
415 spin_unlock(&inode->i_lock);
416 pnfs_free_lseg(lseg);
417 pnfs_put_layout_hdr(lo);
Fred Isaman4541d162011-01-06 11:36:23 +0000418 }
Andy Adamson974cec82010-10-20 00:18:02 -0400419}
Trond Myklebust9369a432012-09-18 20:57:08 -0400420EXPORT_SYMBOL_GPL(pnfs_put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400421
Trond Myklebust6543f802014-10-08 16:39:12 -0400422static void pnfs_free_lseg_async_work(struct work_struct *work)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400423{
424 struct pnfs_layout_segment *lseg;
Trond Myklebust6543f802014-10-08 16:39:12 -0400425 struct pnfs_layout_hdr *lo;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400426
427 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
Trond Myklebust6543f802014-10-08 16:39:12 -0400428 lo = lseg->pls_layout;
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400429
Trond Myklebust6543f802014-10-08 16:39:12 -0400430 pnfs_free_lseg(lseg);
431 pnfs_put_layout_hdr(lo);
432}
433
434static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
435{
436 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
437 schedule_work(&lseg->pls_work);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400438}
439
440void
Trond Myklebust6543f802014-10-08 16:39:12 -0400441pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400442{
Trond Myklebust6543f802014-10-08 16:39:12 -0400443 if (!lseg)
444 return;
445
446 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
447
448 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
449 atomic_read(&lseg->pls_refcount),
450 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
451 if (atomic_dec_and_test(&lseg->pls_refcount)) {
452 struct pnfs_layout_hdr *lo = lseg->pls_layout;
453 pnfs_get_layout_hdr(lo);
454 pnfs_layout_remove_lseg(lo, lseg);
455 pnfs_free_lseg_async(lseg);
456 }
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400457}
Trond Myklebust6543f802014-10-08 16:39:12 -0400458EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
Weston Andros Adamsone6cf82d2014-07-17 20:42:18 -0400459
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400460static u64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300461end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000462{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300463 u64 end;
464
465 end = start + len;
466 return end >= start ? end : NFS4_MAX_UINT64;
467}
468
Benny Halevyfb3296e2011-05-22 19:47:26 +0300469/*
470 * is l2 fully contained in l1?
471 * start1 end1
472 * [----------------------------------)
473 * start2 end2
474 * [----------------)
475 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400476static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400477pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400478 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300479{
480 u64 start1 = l1->offset;
481 u64 end1 = end_offset(start1, l1->length);
482 u64 start2 = l2->offset;
483 u64 end2 = end_offset(start2, l2->length);
484
485 return (start1 <= start2) && (end1 >= end2);
486}
487
488/*
489 * is l1 and l2 intersecting?
490 * start1 end1
491 * [----------------------------------)
492 * start2 end2
493 * [----------------)
494 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400495static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400496pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400497 const struct pnfs_layout_range *l2)
Benny Halevyfb3296e2011-05-22 19:47:26 +0300498{
499 u64 start1 = l1->offset;
500 u64 end1 = end_offset(start1, l1->length);
501 u64 start2 = l2->offset;
502 u64 end2 = end_offset(start2, l2->length);
503
504 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
505 (end2 == NFS4_MAX_UINT64 || end2 > start1);
506}
507
Fred Isaman4541d162011-01-06 11:36:23 +0000508static bool
Trond Myklebust3cb2df12013-06-03 11:24:36 -0400509should_free_lseg(const struct pnfs_layout_range *lseg_range,
510 const struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000511{
Benny Halevy778b5502011-05-22 19:48:02 +0300512 return (recall_range->iomode == IOMODE_ANY ||
513 lseg_range->iomode == recall_range->iomode) &&
Trond Myklebust7dc0ac72013-06-03 11:30:24 -0400514 pnfs_lseg_range_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000515}
516
Trond Myklebust24956802013-03-20 13:03:00 -0400517static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
518 struct list_head *tmp_list)
519{
520 if (!atomic_dec_and_test(&lseg->pls_refcount))
521 return false;
522 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
523 list_add(&lseg->pls_list, tmp_list);
524 return true;
525}
526
Fred Isaman4541d162011-01-06 11:36:23 +0000527/* Returns 1 if lseg is removed from list, 0 otherwise */
528static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
529 struct list_head *tmp_list)
530{
531 int rv = 0;
532
533 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
534 /* Remove the reference keeping the lseg in the
535 * list. It will now be removed when all
536 * outstanding io is finished.
537 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000538 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
539 atomic_read(&lseg->pls_refcount));
Trond Myklebust24956802013-03-20 13:03:00 -0400540 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
Fred Isamand684d2a2011-03-01 01:34:13 +0000541 rv = 1;
Fred Isaman4541d162011-01-06 11:36:23 +0000542 }
543 return rv;
544}
545
546/* Returns count of number of matching invalid lsegs remaining in list
547 * after call.
548 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000549int
Trond Myklebust49a85062012-09-18 20:43:31 -0400550pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
Fred Isaman4541d162011-01-06 11:36:23 +0000551 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300552 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400553{
554 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000555 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400556
557 dprintk("%s:Begin lo %p\n", __func__, lo);
558
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400559 if (list_empty(&lo->plh_segs))
Fred Isaman38511722011-02-03 18:28:50 +0000560 return 0;
Fred Isaman4541d162011-01-06 11:36:23 +0000561 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300562 if (!recall_range ||
563 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000564 dprintk("%s: freeing lseg %p iomode %d "
565 "offset %llu length %llu\n", __func__,
566 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
567 lseg->pls_range.length);
568 invalid++;
569 removed += mark_lseg_invalid(lseg, tmp_list);
570 }
571 dprintk("%s:Return %i\n", __func__, invalid - removed);
572 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400573}
574
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000575/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000576void
Fred Isaman4541d162011-01-06 11:36:23 +0000577pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400578{
Fred Isaman4541d162011-01-06 11:36:23 +0000579 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400580
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000581 if (list_empty(free_me))
582 return;
583
Fred Isaman4541d162011-01-06 11:36:23 +0000584 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000585 list_del(&lseg->pls_list);
Trond Myklebust905ca192012-09-20 20:46:49 -0400586 pnfs_free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400587 }
588}
589
Benny Halevye5e94012010-10-20 00:18:01 -0400590void
591pnfs_destroy_layout(struct nfs_inode *nfsi)
592{
593 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400594 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400595
596 spin_lock(&nfsi->vfs_inode.i_lock);
597 lo = nfsi->layout;
598 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000599 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Trond Myklebust49a85062012-09-18 20:43:31 -0400600 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Trond Myklebust3e621212012-09-24 13:07:16 -0400601 pnfs_get_layout_hdr(lo);
602 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
603 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
Peng Taoc8290132014-12-01 08:22:18 +0800604 pnfs_clear_retry_layoutget(lo);
Trond Myklebust3e621212012-09-24 13:07:16 -0400605 spin_unlock(&nfsi->vfs_inode.i_lock);
606 pnfs_free_lseg_list(&tmp_list);
607 pnfs_put_layout_hdr(lo);
608 } else
609 spin_unlock(&nfsi->vfs_inode.i_lock);
Benny Halevye5e94012010-10-20 00:18:01 -0400610}
Andy Adamson041245c2012-04-27 17:53:53 -0400611EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400612
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500613static bool
614pnfs_layout_add_bulk_destroy_list(struct inode *inode,
615 struct list_head *layout_list)
616{
617 struct pnfs_layout_hdr *lo;
618 bool ret = false;
619
620 spin_lock(&inode->i_lock);
621 lo = NFS_I(inode)->layout;
622 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
623 pnfs_get_layout_hdr(lo);
624 list_add(&lo->plh_bulk_destroy, layout_list);
625 ret = true;
626 }
627 spin_unlock(&inode->i_lock);
628 return ret;
629}
630
631/* Caller must hold rcu_read_lock and clp->cl_lock */
632static int
633pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
634 struct nfs_server *server,
635 struct list_head *layout_list)
636{
637 struct pnfs_layout_hdr *lo, *next;
638 struct inode *inode;
639
640 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
641 inode = igrab(lo->plh_inode);
642 if (inode == NULL)
643 continue;
644 list_del_init(&lo->plh_layouts);
645 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
646 continue;
647 rcu_read_unlock();
648 spin_unlock(&clp->cl_lock);
649 iput(inode);
650 spin_lock(&clp->cl_lock);
651 rcu_read_lock();
652 return -EAGAIN;
653 }
654 return 0;
655}
656
657static int
658pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
659 bool is_bulk_recall)
660{
661 struct pnfs_layout_hdr *lo;
662 struct inode *inode;
663 struct pnfs_layout_range range = {
664 .iomode = IOMODE_ANY,
665 .offset = 0,
666 .length = NFS4_MAX_UINT64,
667 };
668 LIST_HEAD(lseg_list);
669 int ret = 0;
670
671 while (!list_empty(layout_list)) {
672 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
673 plh_bulk_destroy);
674 dprintk("%s freeing layout for inode %lu\n", __func__,
675 lo->plh_inode->i_ino);
676 inode = lo->plh_inode;
Christoph Hellwig7c5d1872014-09-10 08:23:29 -0700677
678 pnfs_layoutcommit_inode(inode, false);
679
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500680 spin_lock(&inode->i_lock);
681 list_del_init(&lo->plh_bulk_destroy);
682 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
683 if (is_bulk_recall)
684 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
685 if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
686 ret = -EAGAIN;
687 spin_unlock(&inode->i_lock);
688 pnfs_free_lseg_list(&lseg_list);
689 pnfs_put_layout_hdr(lo);
690 iput(inode);
691 }
692 return ret;
693}
694
695int
696pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
697 struct nfs_fsid *fsid,
698 bool is_recall)
699{
700 struct nfs_server *server;
701 LIST_HEAD(layout_list);
702
703 spin_lock(&clp->cl_lock);
704 rcu_read_lock();
705restart:
706 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
707 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
708 continue;
709 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
710 server,
711 &layout_list) != 0)
712 goto restart;
713 }
714 rcu_read_unlock();
715 spin_unlock(&clp->cl_lock);
716
717 if (list_empty(&layout_list))
718 return 0;
719 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
720}
721
722int
723pnfs_destroy_layouts_byclid(struct nfs_client *clp,
724 bool is_recall)
725{
726 struct nfs_server *server;
727 LIST_HEAD(layout_list);
728
729 spin_lock(&clp->cl_lock);
730 rcu_read_lock();
731restart:
732 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
733 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
734 server,
735 &layout_list) != 0)
736 goto restart;
737 }
738 rcu_read_unlock();
739 spin_unlock(&clp->cl_lock);
740
741 if (list_empty(&layout_list))
742 return 0;
743 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
744}
745
Andy Adamson974cec82010-10-20 00:18:02 -0400746/*
747 * Called by the state manger to remove all layouts established under an
748 * expired lease.
749 */
750void
751pnfs_destroy_all_layouts(struct nfs_client *clp)
752{
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400753 nfs4_deviceid_mark_client_invalid(clp);
754 nfs4_deviceid_purge_client(clp);
755
Trond Myklebustfd9a8d72013-02-12 09:48:42 -0500756 pnfs_destroy_layouts_byclid(clp, false);
Andy Adamson974cec82010-10-20 00:18:02 -0400757}
758
Trond Myklebust5a655032012-10-02 16:47:14 -0700759/*
760 * Compare 2 layout stateid sequence ids, to see which is newer,
761 * taking into account wraparound issues.
762 */
763static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
764{
Trond Myklebust2c64c572014-02-12 09:36:59 -0500765 return (s32)(s1 - s2) > 0;
Trond Myklebust5a655032012-10-02 16:47:14 -0700766}
767
Fred Isamanfd6002e2011-01-06 11:36:22 +0000768/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000769void
770pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
771 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400772{
Trond Myklebust22aaf712012-10-04 16:32:22 -0700773 u32 oldseq, newseq, new_barrier;
774 int empty = list_empty(&lo->plh_segs);
Andy Adamson974cec82010-10-20 00:18:02 -0400775
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500776 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
777 newseq = be32_to_cpu(new->seqid);
Trond Myklebust22aaf712012-10-04 16:32:22 -0700778 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500779 nfs4_stateid_copy(&lo->plh_stateid, new);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000780 if (update_barrier) {
Trond Myklebust22aaf712012-10-04 16:32:22 -0700781 new_barrier = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000782 } else {
783 /* Because of wraparound, we want to keep the barrier
Trond Myklebust22aaf712012-10-04 16:32:22 -0700784 * "close" to the current seqids.
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000785 */
Trond Myklebust22aaf712012-10-04 16:32:22 -0700786 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000787 }
Trond Myklebust22aaf712012-10-04 16:32:22 -0700788 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
789 lo->plh_barrier = new_barrier;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000790 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400791}
792
Trond Myklebust19c54ab2012-10-05 16:56:58 -0700793static bool
794pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
795 const nfs4_stateid *stateid)
796{
797 u32 seqid = be32_to_cpu(stateid->seqid);
798
799 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
800}
801
Peng Taoce6ab4f2014-09-06 00:53:24 +0800802static bool
803pnfs_layout_returning(const struct pnfs_layout_hdr *lo,
804 struct pnfs_layout_range *range)
805{
806 return test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
807 (lo->plh_return_iomode == IOMODE_ANY ||
808 lo->plh_return_iomode == range->iomode);
809}
810
Fred Isamancf7d63f2011-01-06 11:36:25 +0000811/* lget is set to 1 if called from inside send_layoutget call chain */
812static bool
Peng Taoce6ab4f2014-09-06 00:53:24 +0800813pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo,
814 struct pnfs_layout_range *range, int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000815{
Fred Isamanf7e89172011-01-06 11:36:32 +0000816 return lo->plh_block_lgets ||
817 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000818 (list_empty(&lo->plh_segs) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +0800819 (atomic_read(&lo->plh_outstanding) > lget)) ||
820 pnfs_layout_returning(lo, range);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000821}
822
Fred Isamanfd6002e2011-01-06 11:36:22 +0000823int
824pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
Peng Taoce6ab4f2014-09-06 00:53:24 +0800825 struct pnfs_layout_range *range,
Fred Isamanfd6002e2011-01-06 11:36:22 +0000826 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400827{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000828 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400829
830 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000831 spin_lock(&lo->plh_inode->i_lock);
Peng Taoce6ab4f2014-09-06 00:53:24 +0800832 if (pnfs_layoutgets_blocked(lo, range, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000833 status = -EAGAIN;
Trond Myklebust5d422302013-03-14 16:57:48 -0400834 } else if (!nfs4_valid_open_stateid(open_state)) {
835 status = -EBADF;
Christoph Hellwig47abade2014-08-21 11:09:22 -0500836 } else if (list_empty(&lo->plh_segs) ||
837 test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000838 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400839
Fred Isamanfd6002e2011-01-06 11:36:22 +0000840 do {
841 seq = read_seqbegin(&open_state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500842 nfs4_stateid_copy(dst, &open_state->stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000843 } while (read_seqretry(&open_state->seqlock, seq));
844 } else
Trond Myklebustf597c532012-03-04 18:13:56 -0500845 nfs4_stateid_copy(dst, &lo->plh_stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000846 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400847 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000848 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400849}
850
851/*
852* Get layout from server.
853* for now, assume that whole file layouts are requested.
854* arg->offset: 0
855* arg->length: all ones
856*/
Benny Halevye5e94012010-10-20 00:18:01 -0400857static struct pnfs_layout_segment *
858send_layoutget(struct pnfs_layout_hdr *lo,
859 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300860 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400861 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400862{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000863 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400864 struct nfs_server *server = NFS_SERVER(ino);
865 struct nfs4_layoutget *lgp;
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400866 struct pnfs_layout_segment *lseg;
Benny Halevye5e94012010-10-20 00:18:01 -0400867
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400868 dprintk("--> %s\n", __func__);
869
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400870 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000871 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400872 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400873
Benny Halevyfb3296e2011-05-22 19:47:26 +0300874 lgp->args.minlength = PAGE_CACHE_SIZE;
875 if (lgp->args.minlength > range->length)
876 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400877 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300878 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400879 lgp->args.type = server->pnfs_curr_ld->id;
880 lgp->args.inode = ino;
881 lgp->args.ctx = get_nfs_open_context(ctx);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400882 lgp->gfp_flags = gfp_flags;
Trond Myklebust6ab59342013-05-20 10:49:34 -0400883 lgp->cred = lo->plh_lc_cred;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400884
885 /* Synchronously retrieve layout information from server and
886 * store in lseg.
887 */
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400888 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
889 if (IS_ERR(lseg)) {
890 switch (PTR_ERR(lseg)) {
891 case -ENOMEM:
892 case -ERESTARTSYS:
893 break;
894 default:
895 /* remember that LAYOUTGET failed and suspend trying */
Trond Myklebustb9e028f2012-09-18 16:41:18 -0400896 pnfs_layout_io_set_failed(lo, range->iomode);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -0400897 }
898 return NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500899 } else
900 pnfs_layout_clear_fail_bit(lo,
901 pnfs_iomode_to_fail_bit(range->iomode));
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400902
Andy Adamson974cec82010-10-20 00:18:02 -0400903 return lseg;
904}
905
Trond Myklebust24956802013-03-20 13:03:00 -0400906static void pnfs_clear_layoutcommit(struct inode *inode,
907 struct list_head *head)
908{
909 struct nfs_inode *nfsi = NFS_I(inode);
910 struct pnfs_layout_segment *lseg, *tmp;
911
912 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
913 return;
914 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
915 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
916 continue;
917 pnfs_lseg_dec_and_remove_zero(lseg, head);
918 }
919}
920
Tom Haynesd67ae822014-12-11 17:02:04 -0500921void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
922{
923 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
924 smp_mb__after_atomic();
925 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
926}
927
Peng Taof40eb5d2014-09-06 00:53:22 +0800928static int
929pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
Peng Tao6c166052014-11-17 09:30:40 +0800930 enum pnfs_iomode iomode, bool sync)
Peng Taof40eb5d2014-09-06 00:53:22 +0800931{
932 struct inode *ino = lo->plh_inode;
933 struct nfs4_layoutreturn *lrp;
934 int status = 0;
935
Trond Myklebuste4af4402015-02-05 17:05:08 -0500936 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
Peng Taof40eb5d2014-09-06 00:53:22 +0800937 if (unlikely(lrp == NULL)) {
938 status = -ENOMEM;
939 spin_lock(&ino->i_lock);
940 lo->plh_block_lgets--;
Tom Haynesd67ae822014-12-11 17:02:04 -0500941 pnfs_clear_layoutreturn_waitbit(lo);
Peng Tao193e3aa2014-11-17 09:30:41 +0800942 rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
Peng Taof40eb5d2014-09-06 00:53:22 +0800943 spin_unlock(&ino->i_lock);
944 pnfs_put_layout_hdr(lo);
945 goto out;
946 }
947
948 lrp->args.stateid = stateid;
949 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
950 lrp->args.inode = ino;
Peng Tao15eb67c2014-11-17 09:30:36 +0800951 lrp->args.range.iomode = iomode;
952 lrp->args.range.offset = 0;
953 lrp->args.range.length = NFS4_MAX_UINT64;
Peng Taof40eb5d2014-09-06 00:53:22 +0800954 lrp->args.layout = lo;
955 lrp->clp = NFS_SERVER(ino)->nfs_client;
956 lrp->cred = lo->plh_lc_cred;
957
Peng Tao6c166052014-11-17 09:30:40 +0800958 status = nfs4_proc_layoutreturn(lrp, sync);
Peng Taof40eb5d2014-09-06 00:53:22 +0800959out:
960 dprintk("<-- %s status: %d\n", __func__, status);
961 return status;
962}
963
Andy Adamson293b3b02012-06-20 15:03:34 -0400964/*
965 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
966 * when the layout segment list is empty.
967 *
968 * Note that a pnfs_layout_hdr can exist with an empty layout segment
969 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
970 * deviceid is marked invalid.
971 */
Benny Halevycbe82602011-05-22 19:52:37 +0300972int
973_pnfs_return_layout(struct inode *ino)
974{
975 struct pnfs_layout_hdr *lo = NULL;
976 struct nfs_inode *nfsi = NFS_I(ino);
977 LIST_HEAD(tmp_list);
Benny Halevycbe82602011-05-22 19:52:37 +0300978 nfs4_stateid stateid;
Andy Adamson293b3b02012-06-20 15:03:34 -0400979 int status = 0, empty;
Benny Halevycbe82602011-05-22 19:52:37 +0300980
Andy Adamson366d5052012-06-20 15:03:33 -0400981 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +0300982
983 spin_lock(&ino->i_lock);
984 lo = nfsi->layout;
Trond Myklebuste5929f32012-09-21 16:37:02 -0400985 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300986 spin_unlock(&ino->i_lock);
Andy Adamson293b3b02012-06-20 15:03:34 -0400987 dprintk("NFS: %s no layout to return\n", __func__);
988 goto out;
Benny Halevycbe82602011-05-22 19:52:37 +0300989 }
990 stateid = nfsi->layout->plh_stateid;
991 /* Reference matched in nfs4_layoutreturn_release */
Trond Myklebust70c3bd22012-09-18 20:51:13 -0400992 pnfs_get_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -0400993 empty = list_empty(&lo->plh_segs);
Trond Myklebust24956802013-03-20 13:03:00 -0400994 pnfs_clear_layoutcommit(ino, &tmp_list);
Trond Myklebust49a85062012-09-18 20:43:31 -0400995 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Christoph Hellwigc88953d2014-09-10 08:23:31 -0700996
997 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
998 struct pnfs_layout_range range = {
999 .iomode = IOMODE_ANY,
1000 .offset = 0,
1001 .length = NFS4_MAX_UINT64,
1002 };
1003 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1004 }
1005
Andy Adamson293b3b02012-06-20 15:03:34 -04001006 /* Don't send a LAYOUTRETURN if list was initially empty */
1007 if (empty) {
1008 spin_unlock(&ino->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001009 pnfs_put_layout_hdr(lo);
Andy Adamson293b3b02012-06-20 15:03:34 -04001010 dprintk("NFS: %s no layout segments to return\n", __func__);
1011 goto out;
1012 }
Christoph Hellwig47abade2014-08-21 11:09:22 -05001013
1014 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Fred Isamanea0ded72011-06-15 12:31:02 -04001015 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +03001016 spin_unlock(&ino->i_lock);
1017 pnfs_free_lseg_list(&tmp_list);
1018
Peng Tao6c166052014-11-17 09:30:40 +08001019 status = pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
Benny Halevycbe82602011-05-22 19:52:37 +03001020out:
1021 dprintk("<-- %s status: %d\n", __func__, status);
1022 return status;
1023}
Andy Adamson0a57cda2012-04-27 17:53:50 -04001024EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +03001025
Trond Myklebust24028672013-03-20 13:23:33 -04001026int
1027pnfs_commit_and_return_layout(struct inode *inode)
1028{
1029 struct pnfs_layout_hdr *lo;
1030 int ret;
1031
1032 spin_lock(&inode->i_lock);
1033 lo = NFS_I(inode)->layout;
1034 if (lo == NULL) {
1035 spin_unlock(&inode->i_lock);
1036 return 0;
1037 }
1038 pnfs_get_layout_hdr(lo);
1039 /* Block new layoutgets and read/write to ds */
1040 lo->plh_block_lgets++;
1041 spin_unlock(&inode->i_lock);
1042 filemap_fdatawait(inode->i_mapping);
1043 ret = pnfs_layoutcommit_inode(inode, true);
1044 if (ret == 0)
1045 ret = _pnfs_return_layout(inode);
1046 spin_lock(&inode->i_lock);
1047 lo->plh_block_lgets--;
1048 spin_unlock(&inode->i_lock);
1049 pnfs_put_layout_hdr(lo);
1050 return ret;
1051}
1052
Fred Isamanf7e89172011-01-06 11:36:32 +00001053bool pnfs_roc(struct inode *ino)
1054{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001055 struct nfs_inode *nfsi = NFS_I(ino);
1056 struct nfs_open_context *ctx;
1057 struct nfs4_state *state;
Fred Isamanf7e89172011-01-06 11:36:32 +00001058 struct pnfs_layout_hdr *lo;
1059 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao193e3aa2014-11-17 09:30:41 +08001060 nfs4_stateid stateid;
Fred Isamanf7e89172011-01-06 11:36:32 +00001061 LIST_HEAD(tmp_list);
Peng Tao193e3aa2014-11-17 09:30:41 +08001062 bool found = false, layoutreturn = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001063
1064 spin_lock(&ino->i_lock);
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001065 lo = nfsi->layout;
Fred Isamanf7e89172011-01-06 11:36:32 +00001066 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
1067 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001068 goto out_noroc;
1069
1070 /* Don't return layout if we hold a delegation */
1071 if (nfs4_check_delegation(ino, FMODE_READ))
1072 goto out_noroc;
1073
1074 list_for_each_entry(ctx, &nfsi->open_files, list) {
1075 state = ctx->state;
1076 /* Don't return layout if there is open file state */
1077 if (state != NULL && state->state != 0)
1078 goto out_noroc;
1079 }
1080
Peng Taoc8290132014-12-01 08:22:18 +08001081 pnfs_clear_retry_layoutget(lo);
Fred Isamanf7e89172011-01-06 11:36:32 +00001082 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1083 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1084 mark_lseg_invalid(lseg, &tmp_list);
1085 found = true;
1086 }
1087 if (!found)
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001088 goto out_noroc;
Fred Isamanf7e89172011-01-06 11:36:32 +00001089 lo->plh_block_lgets++;
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001090 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
Fred Isamanf7e89172011-01-06 11:36:32 +00001091 spin_unlock(&ino->i_lock);
1092 pnfs_free_lseg_list(&tmp_list);
1093 return true;
1094
Trond Myklebust40dd4b72015-01-24 13:54:37 -05001095out_noroc:
Peng Tao193e3aa2014-11-17 09:30:41 +08001096 if (lo) {
1097 stateid = lo->plh_stateid;
1098 layoutreturn =
1099 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1100 &lo->plh_flags);
1101 if (layoutreturn) {
1102 lo->plh_block_lgets++;
1103 pnfs_get_layout_hdr(lo);
1104 }
1105 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001106 spin_unlock(&ino->i_lock);
Peng Tao193e3aa2014-11-17 09:30:41 +08001107 if (layoutreturn)
Peng Tao27b6f532014-10-20 14:44:38 +08001108 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
Fred Isamanf7e89172011-01-06 11:36:32 +00001109 return false;
1110}
1111
1112void pnfs_roc_release(struct inode *ino)
1113{
1114 struct pnfs_layout_hdr *lo;
1115
1116 spin_lock(&ino->i_lock);
1117 lo = NFS_I(ino)->layout;
1118 lo->plh_block_lgets--;
Trond Myklebust6622c3e2012-09-20 17:23:11 -04001119 if (atomic_dec_and_test(&lo->plh_refcount)) {
1120 pnfs_detach_layout_hdr(lo);
1121 spin_unlock(&ino->i_lock);
1122 pnfs_free_layout_hdr(lo);
1123 } else
1124 spin_unlock(&ino->i_lock);
Fred Isamanf7e89172011-01-06 11:36:32 +00001125}
1126
1127void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1128{
1129 struct pnfs_layout_hdr *lo;
1130
1131 spin_lock(&ino->i_lock);
1132 lo = NFS_I(ino)->layout;
Trond Myklebust0f35ad62012-10-04 16:28:17 -07001133 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
Fred Isamanf7e89172011-01-06 11:36:32 +00001134 lo->plh_barrier = barrier;
1135 spin_unlock(&ino->i_lock);
1136}
1137
Trond Myklebust7fdab062012-09-20 20:15:57 -04001138bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
Fred Isamanf7e89172011-01-06 11:36:32 +00001139{
1140 struct nfs_inode *nfsi = NFS_I(ino);
Trond Myklebust7fdab062012-09-20 20:15:57 -04001141 struct pnfs_layout_hdr *lo;
Fred Isamanf7e89172011-01-06 11:36:32 +00001142 struct pnfs_layout_segment *lseg;
Peng Tao193e3aa2014-11-17 09:30:41 +08001143 nfs4_stateid stateid;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001144 u32 current_seqid;
Peng Tao193e3aa2014-11-17 09:30:41 +08001145 bool found = false, layoutreturn = false;
Fred Isamanf7e89172011-01-06 11:36:32 +00001146
1147 spin_lock(&ino->i_lock);
1148 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
1149 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
Trond Myklebust7fdab062012-09-20 20:15:57 -04001150 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
Fred Isamanf7e89172011-01-06 11:36:32 +00001151 found = true;
Trond Myklebust7fdab062012-09-20 20:15:57 -04001152 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00001153 }
Trond Myklebust7fdab062012-09-20 20:15:57 -04001154 lo = nfsi->layout;
1155 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +00001156
Trond Myklebust7fdab062012-09-20 20:15:57 -04001157 /* Since close does not return a layout stateid for use as
1158 * a barrier, we choose the worst-case barrier.
1159 */
1160 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1161out:
Peng Tao193e3aa2014-11-17 09:30:41 +08001162 if (!found) {
1163 stateid = lo->plh_stateid;
1164 layoutreturn =
1165 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1166 &lo->plh_flags);
1167 if (layoutreturn) {
1168 lo->plh_block_lgets++;
1169 pnfs_get_layout_hdr(lo);
1170 }
1171 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001172 spin_unlock(&ino->i_lock);
Peng Tao193e3aa2014-11-17 09:30:41 +08001173 if (layoutreturn) {
1174 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
Peng Tao27b6f532014-10-20 14:44:38 +08001175 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false);
Peng Tao193e3aa2014-11-17 09:30:41 +08001176 }
Fred Isamanf7e89172011-01-06 11:36:32 +00001177 return found;
1178}
1179
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001180/*
1181 * Compare two layout segments for sorting into layout cache.
1182 * We want to preferentially return RW over RO layouts, so ensure those
1183 * are seen first.
1184 */
1185static s64
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001186pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001187 const struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001188{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001189 s64 d;
1190
1191 /* high offset > low offset */
1192 d = l1->offset - l2->offset;
1193 if (d)
1194 return d;
1195
1196 /* short length > long length */
1197 d = l2->length - l1->length;
1198 if (d)
1199 return d;
1200
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001201 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001202 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001203}
1204
Andy Adamson974cec82010-10-20 00:18:02 -04001205static void
Trond Myklebust57036a32012-09-20 16:33:30 -04001206pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
Andy Adamson974cec82010-10-20 00:18:02 -04001207 struct pnfs_layout_segment *lseg)
1208{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001209 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001210
Andy Adamson974cec82010-10-20 00:18:02 -04001211 dprintk("%s:Begin\n", __func__);
1212
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001213 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001214 if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001215 continue;
Fred Isaman566052c2011-01-06 11:36:20 +00001216 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001217 dprintk("%s: inserted lseg %p "
1218 "iomode %d offset %llu length %llu before "
1219 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +00001220 __func__, lseg, lseg->pls_range.iomode,
1221 lseg->pls_range.offset, lseg->pls_range.length,
1222 lp, lp->pls_range.iomode, lp->pls_range.offset,
1223 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001224 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -04001225 }
Benny Halevyfb3296e2011-05-22 19:47:26 +03001226 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1227 dprintk("%s: inserted lseg %p "
1228 "iomode %d offset %llu length %llu at tail\n",
1229 __func__, lseg, lseg->pls_range.iomode,
1230 lseg->pls_range.offset, lseg->pls_range.length);
1231out:
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001232 pnfs_get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -04001233
1234 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001235}
1236
1237static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001238alloc_init_layout_hdr(struct inode *ino,
1239 struct nfs_open_context *ctx,
1240 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001241{
1242 struct pnfs_layout_hdr *lo;
1243
Benny Halevy636fb9c2011-05-22 19:51:33 +03001244 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001245 if (!lo)
1246 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +00001247 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001248 INIT_LIST_HEAD(&lo->plh_layouts);
1249 INIT_LIST_HEAD(&lo->plh_segs);
Trond Myklebustfd9a8d72013-02-12 09:48:42 -05001250 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001251 lo->plh_inode = ino;
Trond Myklebust5cc22162013-05-21 09:26:49 -04001252 lo->plh_lc_cred = get_rpccred(ctx->cred);
Benny Halevye5e94012010-10-20 00:18:01 -04001253 return lo;
1254}
1255
1256static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -04001257pnfs_find_alloc_layout(struct inode *ino,
1258 struct nfs_open_context *ctx,
1259 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001260{
1261 struct nfs_inode *nfsi = NFS_I(ino);
1262 struct pnfs_layout_hdr *new = NULL;
1263
1264 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1265
Trond Myklebust251ec412012-10-02 15:41:05 -07001266 if (nfsi->layout != NULL)
1267 goto out_existing;
Benny Halevye5e94012010-10-20 00:18:01 -04001268 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001269 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001270 spin_lock(&ino->i_lock);
1271
Trond Myklebust251ec412012-10-02 15:41:05 -07001272 if (likely(nfsi->layout == NULL)) { /* Won the race? */
Benny Halevye5e94012010-10-20 00:18:01 -04001273 nfsi->layout = new;
Trond Myklebust251ec412012-10-02 15:41:05 -07001274 return new;
Yanchuan Nian7175fe92012-10-31 16:05:48 +08001275 } else if (new != NULL)
1276 pnfs_free_layout_hdr(new);
Trond Myklebust251ec412012-10-02 15:41:05 -07001277out_existing:
1278 pnfs_get_layout_hdr(nfsi->layout);
Benny Halevye5e94012010-10-20 00:18:01 -04001279 return nfsi->layout;
1280}
1281
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001282/*
1283 * iomode matching rules:
1284 * iomode lseg match
1285 * ----- ----- -----
1286 * ANY READ true
1287 * ANY RW true
1288 * RW READ false
1289 * RW RW true
1290 * READ READ true
1291 * READ RW true
1292 */
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001293static bool
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001294pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
Trond Myklebust3cb2df12013-06-03 11:24:36 -04001295 const struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001296{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001297 struct pnfs_layout_range range1;
1298
1299 if ((range->iomode == IOMODE_RW &&
1300 ls_range->iomode != IOMODE_RW) ||
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001301 !pnfs_lseg_range_intersecting(ls_range, range))
Benny Halevyfb3296e2011-05-22 19:47:26 +03001302 return 0;
1303
1304 /* range1 covers only the first byte in the range */
1305 range1 = *range;
1306 range1.length = 1;
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001307 return pnfs_lseg_range_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001308}
1309
1310/*
1311 * lookup range in layout
1312 */
Benny Halevye5e94012010-10-20 00:18:01 -04001313static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +03001314pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1315 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -04001316{
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001317 struct pnfs_layout_segment *lseg, *ret = NULL;
1318
1319 dprintk("%s:Begin\n", __func__);
1320
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001321 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +00001322 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Peng Taoce6ab4f2014-09-06 00:53:24 +08001323 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
Trond Myklebust7dc0ac72013-06-03 11:30:24 -04001324 pnfs_lseg_range_match(&lseg->pls_range, range)) {
Trond Myklebust9369a432012-09-18 20:57:08 -04001325 ret = pnfs_get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001326 break;
1327 }
Benny Halevyd771e3a2011-06-14 16:30:16 -04001328 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001329 break;
1330 }
1331
1332 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +00001333 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001334 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -04001335}
1336
1337/*
Andy Adamsond23d61c2012-05-23 05:02:37 -04001338 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1339 * to the MDS or over pNFS
1340 *
1341 * The nfs_inode read_io and write_io fields are cumulative counters reset
1342 * when there are no layout segments. Note that in pnfs_update_layout iomode
1343 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1344 * WRITE request.
1345 *
1346 * A return of true means use MDS I/O.
1347 *
1348 * From rfc 5661:
1349 * If a file's size is smaller than the file size threshold, data accesses
1350 * SHOULD be sent to the metadata server. If an I/O request has a length that
1351 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1352 * server. If both file size and I/O size are provided, the client SHOULD
1353 * reach or exceed both thresholds before sending its read or write
1354 * requests to the data server.
1355 */
1356static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1357 struct inode *ino, int iomode)
1358{
1359 struct nfs4_threshold *t = ctx->mdsthreshold;
1360 struct nfs_inode *nfsi = NFS_I(ino);
1361 loff_t fsize = i_size_read(ino);
1362 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1363
1364 if (t == NULL)
1365 return ret;
1366
1367 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1368 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1369
1370 switch (iomode) {
1371 case IOMODE_READ:
1372 if (t->bm & THRESHOLD_RD) {
1373 dprintk("%s fsize %llu\n", __func__, fsize);
1374 size_set = true;
1375 if (fsize < t->rd_sz)
1376 size = true;
1377 }
1378 if (t->bm & THRESHOLD_RD_IO) {
1379 dprintk("%s nfsi->read_io %llu\n", __func__,
1380 nfsi->read_io);
1381 io_set = true;
1382 if (nfsi->read_io < t->rd_io_sz)
1383 io = true;
1384 }
1385 break;
1386 case IOMODE_RW:
1387 if (t->bm & THRESHOLD_WR) {
1388 dprintk("%s fsize %llu\n", __func__, fsize);
1389 size_set = true;
1390 if (fsize < t->wr_sz)
1391 size = true;
1392 }
1393 if (t->bm & THRESHOLD_WR_IO) {
1394 dprintk("%s nfsi->write_io %llu\n", __func__,
1395 nfsi->write_io);
1396 io_set = true;
1397 if (nfsi->write_io < t->wr_io_sz)
1398 io = true;
1399 }
1400 break;
1401 }
1402 if (size_set && io_set) {
1403 if (size && io)
1404 ret = true;
1405 } else if (size || io)
1406 ret = true;
1407
1408 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1409 return ret;
1410}
1411
Peng Taoaa8a45e2014-12-01 08:22:23 +08001412/* stop waiting if someone clears NFS_LAYOUT_RETRY_LAYOUTGET bit. */
1413static int pnfs_layoutget_retry_bit_wait(struct wait_bit_key *key)
1414{
1415 if (!test_bit(NFS_LAYOUT_RETRY_LAYOUTGET, key->flags))
1416 return 1;
1417 return nfs_wait_bit_killable(key);
1418}
1419
1420static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1421{
1422 /*
1423 * send layoutcommit as it can hold up layoutreturn due to lseg
1424 * reference
1425 */
1426 pnfs_layoutcommit_inode(lo->plh_inode, false);
1427 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1428 pnfs_layoutget_retry_bit_wait,
1429 TASK_UNINTERRUPTIBLE);
1430}
1431
Tom Haynesd67ae822014-12-11 17:02:04 -05001432static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1433{
1434 unsigned long *bitlock = &lo->plh_flags;
1435
1436 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1437 smp_mb__after_atomic();
1438 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1439}
1440
Andy Adamsond23d61c2012-05-23 05:02:37 -04001441/*
Benny Halevye5e94012010-10-20 00:18:01 -04001442 * Layout segment is retreived from the server if not cached.
1443 * The appropriate layout segment is referenced and returned to the caller.
1444 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001445struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001446pnfs_update_layout(struct inode *ino,
1447 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001448 loff_t pos,
1449 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001450 enum pnfs_iomode iomode,
1451 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001452{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001453 struct pnfs_layout_range arg = {
1454 .iomode = iomode,
1455 .offset = pos,
1456 .length = count,
1457 };
Benny Halevy707ed5f2011-05-22 19:47:46 +03001458 unsigned pg_offset;
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001459 struct nfs_server *server = NFS_SERVER(ino);
1460 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -04001461 struct pnfs_layout_hdr *lo;
1462 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05001463 bool first;
Benny Halevye5e94012010-10-20 00:18:01 -04001464
1465 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001466 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001467
1468 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001469 goto out;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001470
Peng Tao9bf87482014-08-22 17:37:41 +08001471lookup_again:
1472 first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001473 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001474 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001475 if (lo == NULL) {
1476 spin_unlock(&ino->i_lock);
1477 goto out;
1478 }
Benny Halevye5e94012010-10-20 00:18:01 -04001479
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001480 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001481 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001482 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001483 goto out_unlock;
1484 }
1485
1486 /* if LAYOUTGET already failed once we don't try again */
Peng Taoaa8a45e2014-12-01 08:22:23 +08001487 if (pnfs_layout_io_test_failed(lo, iomode) &&
1488 !pnfs_should_retry_layoutget(lo))
Benny Halevye5e94012010-10-20 00:18:01 -04001489 goto out_unlock;
1490
Peng Tao9bf87482014-08-22 17:37:41 +08001491 first = list_empty(&lo->plh_segs);
1492 if (first) {
1493 /* The first layoutget for the file. Need to serialize per
1494 * RFC 5661 Errata 3208.
1495 */
1496 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1497 &lo->plh_flags)) {
1498 spin_unlock(&ino->i_lock);
1499 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1500 TASK_UNINTERRUPTIBLE);
1501 pnfs_put_layout_hdr(lo);
1502 goto lookup_again;
1503 }
1504 } else {
1505 /* Check to see if the layout for the given range
1506 * already exists
1507 */
1508 lseg = pnfs_find_lseg(lo, &arg);
1509 if (lseg)
1510 goto out_unlock;
1511 }
Andy Adamson568e8c42011-03-01 01:34:22 +00001512
Peng Taoaa8a45e2014-12-01 08:22:23 +08001513 /*
1514 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1515 * for LAYOUTRETURN even if first is true.
1516 */
1517 if (!lseg && pnfs_should_retry_layoutget(lo) &&
1518 test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1519 spin_unlock(&ino->i_lock);
1520 dprintk("%s wait for layoutreturn\n", __func__);
1521 if (pnfs_prepare_to_retry_layoutget(lo)) {
Tom Haynesd67ae822014-12-11 17:02:04 -05001522 if (first)
1523 pnfs_clear_first_layoutget(lo);
Peng Taoaa8a45e2014-12-01 08:22:23 +08001524 pnfs_put_layout_hdr(lo);
1525 dprintk("%s retrying\n", __func__);
1526 goto lookup_again;
1527 }
1528 goto out_put_layout_hdr;
1529 }
1530
Peng Taoce6ab4f2014-09-06 00:53:24 +08001531 if (pnfs_layoutgets_blocked(lo, &arg, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +00001532 goto out_unlock;
1533 atomic_inc(&lo->plh_outstanding);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001534 spin_unlock(&ino->i_lock);
Weston Andros Adamson30005122013-02-28 20:30:10 -05001535
Peng Taoabb9a002014-08-22 17:37:40 +08001536 if (list_empty(&lo->plh_layouts)) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001537 /* The lo must be on the clp list if there is any
1538 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1539 */
1540 spin_lock(&clp->cl_lock);
Peng Taoabb9a002014-08-22 17:37:40 +08001541 if (list_empty(&lo->plh_layouts))
1542 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001543 spin_unlock(&clp->cl_lock);
1544 }
Benny Halevye5e94012010-10-20 00:18:01 -04001545
Benny Halevy707ed5f2011-05-22 19:47:46 +03001546 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1547 if (pg_offset) {
1548 arg.offset -= pg_offset;
1549 arg.length += pg_offset;
1550 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001551 if (arg.length != NFS4_MAX_UINT64)
1552 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001553
Benny Halevyfb3296e2011-05-22 19:47:26 +03001554 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Peng Taoc8290132014-12-01 08:22:18 +08001555 pnfs_clear_retry_layoutget(lo);
Fred Isamancf7d63f2011-01-06 11:36:25 +00001556 atomic_dec(&lo->plh_outstanding);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001557out_put_layout_hdr:
Tom Haynesd67ae822014-12-11 17:02:04 -05001558 if (first)
1559 pnfs_clear_first_layoutget(lo);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04001560 pnfs_put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001561out:
Trond Myklebustf86bbcf2012-09-26 11:21:40 -04001562 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1563 "(%s, offset: %llu, length: %llu)\n",
1564 __func__, ino->i_sb->s_id,
1565 (unsigned long long)NFS_FILEID(ino),
1566 lseg == NULL ? "not found" : "found",
1567 iomode==IOMODE_RW ? "read/write" : "read-only",
1568 (unsigned long long)pos,
1569 (unsigned long long)count);
Benny Halevye5e94012010-10-20 00:18:01 -04001570 return lseg;
1571out_unlock:
1572 spin_unlock(&ino->i_lock);
Trond Myklebust830ffb52012-09-20 21:25:19 -04001573 goto out_put_layout_hdr;
Benny Halevye5e94012010-10-20 00:18:01 -04001574}
Andy Adamson7c24d942011-06-13 18:22:38 -04001575EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001576
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001577struct pnfs_layout_segment *
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001578pnfs_layout_process(struct nfs4_layoutget *lgp)
1579{
1580 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1581 struct nfs4_layoutget_res *res = &lgp->res;
1582 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001583 struct inode *ino = lo->plh_inode;
Trond Myklebust78096cc2014-02-12 10:02:27 -05001584 LIST_HEAD(free_me);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001585 int status = 0;
1586
1587 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001588 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001589 if (!lseg || IS_ERR(lseg)) {
1590 if (!lseg)
1591 status = -ENOMEM;
1592 else
1593 status = PTR_ERR(lseg);
1594 dprintk("%s: Could not allocate layout: error %d\n",
1595 __func__, status);
1596 goto out;
1597 }
1598
Christoph Hellwig1013df612014-08-21 11:09:18 -05001599 init_lseg(lo, lseg);
1600 lseg->pls_range = res->range;
1601
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001602 spin_lock(&ino->i_lock);
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001603 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001604 dprintk("%s forget reply due to recall\n", __func__);
1605 goto out_forget_reply;
1606 }
1607
Peng Taoce6ab4f2014-09-06 00:53:24 +08001608 if (pnfs_layoutgets_blocked(lo, &lgp->args.range, 1)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001609 dprintk("%s forget reply due to state\n", __func__);
1610 goto out_forget_reply;
1611 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001612
Christoph Hellwig362f7472014-08-21 11:09:20 -05001613 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1614 /* existing state ID, make sure the sequence number matches. */
1615 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1616 dprintk("%s forget reply due to sequence\n", __func__);
1617 goto out_forget_reply;
1618 }
1619 pnfs_set_layout_stateid(lo, &res->stateid, false);
1620 } else {
1621 /*
1622 * We got an entirely new state ID. Mark all segments for the
1623 * inode invalid, and don't bother validating the stateid
1624 * sequence number.
1625 */
1626 pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
1627
1628 nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
1629 lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
1630 }
Trond Myklebust038d6492012-10-02 16:38:41 -07001631
Christoph Hellwig47abade2014-08-21 11:09:22 -05001632 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1633
Trond Myklebust9369a432012-09-18 20:57:08 -04001634 pnfs_get_lseg(lseg);
Trond Myklebust57036a32012-09-20 16:33:30 -04001635 pnfs_layout_insert_lseg(lo, lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001636
Fred Isamanf7e89172011-01-06 11:36:32 +00001637 if (res->return_on_close) {
1638 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1639 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1640 }
1641
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001642 spin_unlock(&ino->i_lock);
Trond Myklebust78096cc2014-02-12 10:02:27 -05001643 pnfs_free_lseg_list(&free_me);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001644 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001645out:
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04001646 return ERR_PTR(status);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001647
1648out_forget_reply:
1649 spin_unlock(&ino->i_lock);
1650 lseg->pls_layout = lo;
1651 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1652 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001653}
1654
Peng Tao016256d2014-09-06 00:53:23 +08001655static void
1656pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1657 struct list_head *tmp_list,
1658 struct pnfs_layout_range *return_range)
1659{
1660 struct pnfs_layout_segment *lseg, *next;
1661
1662 dprintk("%s:Begin lo %p\n", __func__, lo);
1663
1664 if (list_empty(&lo->plh_segs))
1665 return;
1666
1667 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1668 if (should_free_lseg(&lseg->pls_range, return_range)) {
1669 dprintk("%s: marking lseg %p iomode %d "
1670 "offset %llu length %llu\n", __func__,
1671 lseg, lseg->pls_range.iomode,
1672 lseg->pls_range.offset,
1673 lseg->pls_range.length);
1674 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1675 mark_lseg_invalid(lseg, tmp_list);
1676 }
1677}
1678
1679void pnfs_error_mark_layout_for_return(struct inode *inode,
1680 struct pnfs_layout_segment *lseg)
1681{
1682 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1683 int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode);
1684 struct pnfs_layout_range range = {
1685 .iomode = lseg->pls_range.iomode,
1686 .offset = 0,
1687 .length = NFS4_MAX_UINT64,
1688 };
1689 LIST_HEAD(free_me);
1690
1691 spin_lock(&inode->i_lock);
1692 /* set failure bit so that pnfs path will be retried later */
1693 pnfs_layout_set_fail_bit(lo, iomode);
1694 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1695 if (lo->plh_return_iomode == 0)
1696 lo->plh_return_iomode = range.iomode;
1697 else if (lo->plh_return_iomode != range.iomode)
1698 lo->plh_return_iomode = IOMODE_ANY;
1699 /*
1700 * mark all matching lsegs so that we are sure to have no live
1701 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1702 * for how it works.
1703 */
1704 pnfs_mark_matching_lsegs_return(lo, &free_me, &range);
1705 spin_unlock(&inode->i_lock);
1706 pnfs_free_lseg_list(&free_me);
1707}
1708EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1709
Trond Myklebustd8007d42011-06-10 13:30:23 -04001710void
1711pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1712{
Peng Tao1fd937b2012-09-25 14:55:57 +08001713 u64 rd_size = req->wb_bytes;
1714
Peng Taocb5d04b2015-01-24 22:14:52 +08001715 if (pgio->pg_lseg == NULL) {
1716 if (pgio->pg_dreq == NULL)
1717 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1718 else
1719 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001720
Peng Taocb5d04b2015-01-24 22:14:52 +08001721 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1722 req->wb_context,
1723 req_offset(req),
1724 rd_size,
1725 IOMODE_READ,
1726 GFP_KERNEL);
1727 }
Trond Myklebuste885de12011-06-10 13:30:23 -04001728 /* If no lseg, fall back to read through mds */
1729 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001730 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001731
Trond Myklebustd8007d42011-06-10 13:30:23 -04001732}
1733EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1734
1735void
Peng Tao62965562012-09-25 14:55:57 +08001736pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1737 struct nfs_page *req, u64 wb_size)
Trond Myklebustd8007d42011-06-10 13:30:23 -04001738{
Peng Taocb5d04b2015-01-24 22:14:52 +08001739 if (pgio->pg_lseg == NULL)
1740 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1741 req->wb_context,
1742 req_offset(req),
1743 wb_size,
1744 IOMODE_RW,
1745 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001746 /* If no lseg, fall back to write through mds */
1747 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001748 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001749}
1750EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1751
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001752void
1753pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
1754{
1755 if (desc->pg_lseg) {
1756 pnfs_put_lseg(desc->pg_lseg);
1757 desc->pg_lseg = NULL;
1758 }
1759}
1760EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
1761
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -04001762/*
1763 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1764 * of bytes (maximum @req->wb_bytes) that can be coalesced.
1765 */
1766size_t
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001767pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
1768 struct nfs_page *prev, struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001769{
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001770 unsigned int size;
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001771 u64 seg_end, req_start, seg_left;
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001772
1773 size = nfs_generic_pg_test(pgio, prev, req);
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001774 if (!size)
1775 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +03001776
Trond Myklebust19982ba2011-06-10 13:30:23 -04001777 /*
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001778 * 'size' contains the number of bytes left in the current page (up
1779 * to the original size asked for in @req->wb_bytes).
1780 *
1781 * Calculate how many bytes are left in the layout segment
1782 * and if there are less bytes than 'size', return that instead.
Trond Myklebust19982ba2011-06-10 13:30:23 -04001783 *
1784 * Please also note that 'end_offset' is actually the offset of the
1785 * first byte that lies outside the pnfs_layout_range. FIXME?
1786 *
1787 */
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001788 if (pgio->pg_lseg) {
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001789 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
1790 pgio->pg_lseg->pls_range.length);
1791 req_start = req_offset(req);
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001792 WARN_ON_ONCE(req_start >= seg_end);
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001793 /* start of request is past the last byte of this segment */
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001794 if (req_start >= seg_end) {
1795 /* reference the new lseg */
1796 if (pgio->pg_ops->pg_cleanup)
1797 pgio->pg_ops->pg_cleanup(pgio);
1798 if (pgio->pg_ops->pg_init)
1799 pgio->pg_ops->pg_init(pgio, req);
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001800 return 0;
Weston Andros Adamson7c137892015-01-30 11:01:02 -05001801 }
Weston Andros Adamsonc5e20cb2014-06-09 17:47:26 -04001802
1803 /* adjust 'size' iff there are fewer bytes left in the
1804 * segment than what nfs_generic_pg_test returned */
1805 seg_left = seg_end - req_start;
1806 if (seg_left < size)
1807 size = (unsigned int)seg_left;
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001808 }
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -04001809
Weston Andros Adamson19b54842014-05-15 11:56:55 -04001810 return size;
Fred Isamanbae724e2011-03-01 01:34:15 +00001811}
Benny Halevy89a58e32011-05-25 20:54:40 +03001812EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001813
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001814int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001815{
1816 struct nfs_pageio_descriptor pgio;
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001817
1818 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001819 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
1820 hdr->completion_ops);
1821 return nfs_pageio_resend(&pgio, hdr);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001822}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001823EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001824
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001825static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001826{
Fred Isamancd841602012-04-20 14:47:44 -04001827
1828 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1829 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001830 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001831 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001832 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001833 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001834 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001835}
1836
Benny Halevyd20581a2011-05-22 19:52:03 +03001837/*
1838 * Called by non rpc-based layout drivers
1839 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001840void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001841{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001842 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
Fred Isamancd841602012-04-20 14:47:44 -04001843 if (!hdr->pnfs_error) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001844 pnfs_set_layoutcommit(hdr);
1845 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001846 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001847 pnfs_ld_handle_write_error(hdr);
1848 hdr->mds_ops->rpc_release(hdr);
Fred Isaman44b83792011-03-03 15:13:44 +00001849}
Benny Halevyd20581a2011-05-22 19:52:03 +03001850EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001851
Trond Myklebustdce81292011-07-13 15:59:19 -04001852static void
1853pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001854 struct nfs_pgio_header *hdr)
Trond Myklebustdce81292011-07-13 15:59:19 -04001855{
Peng Tao48d635f2014-11-10 08:35:35 +08001856 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001857
Fred Isaman6c75dc02012-04-20 14:47:47 -04001858 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001859 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001860 nfs_pageio_reset_write_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001861 mirror->pg_recoalesce = 1;
Fred Isaman6c75dc02012-04-20 14:47:47 -04001862 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001863 nfs_pgio_data_destroy(hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001864}
1865
1866static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001867pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
Trond Myklebustdce81292011-07-13 15:59:19 -04001868 const struct rpc_call_ops *call_ops,
1869 struct pnfs_layout_segment *lseg,
1870 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001871{
Fred Isamancd841602012-04-20 14:47:44 -04001872 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00001873 enum pnfs_try_status trypnfs;
1874 struct nfs_server *nfss = NFS_SERVER(inode);
1875
Fred Isamancd841602012-04-20 14:47:44 -04001876 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00001877
1878 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001879 inode->i_ino, hdr->args.count, hdr->args.offset, how);
1880 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001881 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00001882 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00001883 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1884 return trypnfs;
1885}
1886
Trond Myklebustdce81292011-07-13 15:59:19 -04001887static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001888pnfs_do_write(struct nfs_pageio_descriptor *desc,
1889 struct nfs_pgio_header *hdr, int how)
Trond Myklebustdce81292011-07-13 15:59:19 -04001890{
Trond Myklebustdce81292011-07-13 15:59:19 -04001891 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1892 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001893 enum pnfs_try_status trypnfs;
Trond Myklebustdce81292011-07-13 15:59:19 -04001894
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001895 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001896 if (trypnfs == PNFS_NOT_ATTEMPTED)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001897 pnfs_write_through_mds(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001898}
1899
Fred Isaman6c75dc02012-04-20 14:47:47 -04001900static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1901{
Trond Myklebust9369a432012-09-18 20:57:08 -04001902 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04001903 nfs_pgio_header_free(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001904}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04001905EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001906
Trond Myklebustdce81292011-07-13 15:59:19 -04001907int
1908pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1909{
Peng Tao48d635f2014-11-10 08:35:35 +08001910 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001911
Fred Isaman6c75dc02012-04-20 14:47:47 -04001912 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04001913 int ret;
1914
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04001915 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1916 if (!hdr) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001917 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001918 return -ENOMEM;
1919 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001920 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001921
Trond Myklebust9369a432012-09-18 20:57:08 -04001922 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04001923 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04001924 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04001925 pnfs_do_write(desc, hdr, desc->pg_ioflags);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001926
Fred Isaman6c75dc02012-04-20 14:47:47 -04001927 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04001928}
1929EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1930
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001931int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
Trond Myklebust62e4a762011-11-10 14:30:37 -05001932{
1933 struct nfs_pageio_descriptor pgio;
1934
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001935 /* Resend all requests through the MDS */
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001936 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
1937 return nfs_pageio_resend(&pgio, hdr);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001938}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001939EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001940
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001941static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001942{
Fred Isamancd841602012-04-20 14:47:44 -04001943 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1944 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001945 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001946 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001947 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001948 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
Weston Andros Adamson53113ad2014-06-09 11:48:38 -04001949 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001950}
1951
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001952/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001953 * Called by non rpc-based layout drivers
1954 */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001955void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03001956{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001957 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
Fred Isamancd841602012-04-20 14:47:44 -04001958 if (likely(!hdr->pnfs_error)) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001959 __nfs4_read_done_cb(hdr);
1960 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001961 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001962 pnfs_ld_handle_read_error(hdr);
1963 hdr->mds_ops->rpc_release(hdr);
Benny Halevyd20581a2011-05-22 19:52:03 +03001964}
1965EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1966
Trond Myklebust493292d2011-07-13 15:58:28 -04001967static void
1968pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001969 struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04001970{
Peng Tao48d635f2014-11-10 08:35:35 +08001971 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001972
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001973 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001974 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001975 nfs_pageio_reset_read_mds(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04001976 mirror->pg_recoalesce = 1;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001977 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001978 nfs_pgio_data_destroy(hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04001979}
1980
Benny Halevyd20581a2011-05-22 19:52:03 +03001981/*
Andy Adamson64419a92011-03-01 01:34:16 +00001982 * Call the appropriate parallel I/O subsystem read function.
1983 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001984static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001985pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
Trond Myklebust493292d2011-07-13 15:58:28 -04001986 const struct rpc_call_ops *call_ops,
1987 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001988{
Fred Isamancd841602012-04-20 14:47:44 -04001989 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00001990 struct nfs_server *nfss = NFS_SERVER(inode);
1991 enum pnfs_try_status trypnfs;
1992
Fred Isamancd841602012-04-20 14:47:44 -04001993 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00001994
1995 dprintk("%s: Reading ino:%lu %u@%llu\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001996 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
Andy Adamson64419a92011-03-01 01:34:16 +00001997
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04001998 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001999 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00002000 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00002001 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2002 return trypnfs;
2003}
Andy Adamson863a3c62011-03-23 13:27:54 +00002004
Peng Taoceb11e12014-11-10 08:35:38 +08002005/* Resend all requests through pnfs. */
2006int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2007{
2008 struct nfs_pageio_descriptor pgio;
2009
2010 nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops);
2011 return nfs_pageio_resend(&pgio, hdr);
2012}
2013EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2014
Trond Myklebust493292d2011-07-13 15:58:28 -04002015static void
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002016pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
Trond Myklebust493292d2011-07-13 15:58:28 -04002017{
Trond Myklebust493292d2011-07-13 15:58:28 -04002018 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2019 struct pnfs_layout_segment *lseg = desc->pg_lseg;
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002020 enum pnfs_try_status trypnfs;
Peng Taoceb11e12014-11-10 08:35:38 +08002021 int err = 0;
Trond Myklebust493292d2011-07-13 15:58:28 -04002022
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002023 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
Peng Taoceb11e12014-11-10 08:35:38 +08002024 if (trypnfs == PNFS_TRY_AGAIN)
2025 err = pnfs_read_resend_pnfs(hdr);
2026 if (trypnfs == PNFS_NOT_ATTEMPTED || err)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002027 pnfs_read_through_mds(desc, hdr);
Trond Myklebust493292d2011-07-13 15:58:28 -04002028}
2029
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002030static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2031{
Trond Myklebust9369a432012-09-18 20:57:08 -04002032 pnfs_put_lseg(hdr->lseg);
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002033 nfs_pgio_header_free(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002034}
Bryan Schumaker89d77c82012-07-30 16:05:25 -04002035EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002036
Trond Myklebust493292d2011-07-13 15:58:28 -04002037int
2038pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2039{
Peng Tao48d635f2014-11-10 08:35:35 +08002040 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002041
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002042 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04002043 int ret;
2044
Weston Andros Adamson1e7f3a42014-06-09 11:48:33 -04002045 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2046 if (!hdr) {
Weston Andros Adamsona7d42dd2014-09-19 10:55:07 -04002047 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002048 return -ENOMEM;
Trond Myklebust493292d2011-07-13 15:58:28 -04002049 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002050 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
Trond Myklebust9369a432012-09-18 20:57:08 -04002051 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
Anna Schumakeref2c4882014-05-06 09:12:36 -04002052 ret = nfs_generic_pgio(desc, hdr);
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -04002053 if (!ret)
Weston Andros Adamson7f714722014-05-15 11:56:53 -04002054 pnfs_do_read(desc, hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04002055 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04002056}
2057EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2058
Trond Myklebust71244d92014-01-13 13:34:36 -05002059static void pnfs_clear_layoutcommitting(struct inode *inode)
2060{
2061 unsigned long *bitlock = &NFS_I(inode)->flags;
2062
2063 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002064 smp_mb__after_atomic();
Trond Myklebust71244d92014-01-13 13:34:36 -05002065 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2066}
2067
Andy Adamson863a3c62011-03-23 13:27:54 +00002068/*
Peng Taoa9bae562011-07-30 20:52:33 -04002069 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00002070 */
Peng Taoa9bae562011-07-30 20:52:33 -04002071static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00002072{
Peng Taoa9bae562011-07-30 20:52:33 -04002073 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00002074
Peng Taoa9bae562011-07-30 20:52:33 -04002075 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2076 if (lseg->pls_range.iomode == IOMODE_RW &&
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002077 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
Peng Taoa9bae562011-07-30 20:52:33 -04002078 list_add(&lseg->pls_lc_list, listp);
2079 }
Andy Adamson863a3c62011-03-23 13:27:54 +00002080}
2081
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002082static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2083{
2084 struct pnfs_layout_segment *lseg, *tmp;
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002085
2086 /* Matched by references in pnfs_set_layoutcommit */
2087 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2088 list_del_init(&lseg->pls_lc_list);
2089 pnfs_put_lseg(lseg);
2090 }
2091
Trond Myklebust71244d92014-01-13 13:34:36 -05002092 pnfs_clear_layoutcommitting(inode);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002093}
2094
Peng Tao1b0ae062011-09-22 21:50:12 -04002095void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2096{
Trond Myklebustb9e028f2012-09-18 16:41:18 -04002097 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
Peng Tao1b0ae062011-09-22 21:50:12 -04002098}
2099EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2100
Andy Adamson863a3c62011-03-23 13:27:54 +00002101void
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002102pnfs_set_layoutcommit(struct nfs_pgio_header *hdr)
Andy Adamson863a3c62011-03-23 13:27:54 +00002103{
Fred Isamancd841602012-04-20 14:47:44 -04002104 struct inode *inode = hdr->inode;
2105 struct nfs_inode *nfsi = NFS_I(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04002106 loff_t end_pos = hdr->mds_offset + hdr->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002107 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00002108
Fred Isamancd841602012-04-20 14:47:44 -04002109 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00002110 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002111 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00002112 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04002113 __func__, inode->i_ino);
Andy Adamson863a3c62011-03-23 13:27:54 +00002114 }
Fred Isamancd841602012-04-20 14:47:44 -04002115 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04002116 /* references matched in nfs4_layoutcommit_release */
Trond Myklebust9369a432012-09-18 20:57:08 -04002117 pnfs_get_lseg(hdr->lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04002118 }
Peng Taoacff58802011-07-30 20:52:31 -04002119 if (end_pos > nfsi->layout->plh_lwb)
2120 nfsi->layout->plh_lwb = end_pos;
Fred Isamancd841602012-04-20 14:47:44 -04002121 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04002122 dprintk("%s: lseg %p end_pos %llu\n",
Fred Isamancd841602012-04-20 14:47:44 -04002123 __func__, hdr->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04002124
2125 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2126 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2127 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04002128 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002129}
2130EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2131
Peng Tao378520b2014-08-07 10:15:02 +08002132void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data)
2133{
2134 struct inode *inode = data->inode;
2135 struct nfs_inode *nfsi = NFS_I(inode);
2136 bool mark_as_dirty = false;
2137
2138 spin_lock(&inode->i_lock);
2139 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2140 mark_as_dirty = true;
2141 dprintk("%s: Set layoutcommit for inode %lu ",
2142 __func__, inode->i_ino);
2143 }
2144 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) {
2145 /* references matched in nfs4_layoutcommit_release */
2146 pnfs_get_lseg(data->lseg);
2147 }
2148 if (data->lwb > nfsi->layout->plh_lwb)
2149 nfsi->layout->plh_lwb = data->lwb;
2150 spin_unlock(&inode->i_lock);
2151 dprintk("%s: lseg %p end_pos %llu\n",
2152 __func__, data->lseg, nfsi->layout->plh_lwb);
2153
2154 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2155 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2156 if (mark_as_dirty)
2157 mark_inode_dirty_sync(inode);
2158}
2159EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit);
2160
Andy Adamsondb29c082011-07-30 20:52:38 -04002161void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2162{
2163 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2164
2165 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2166 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
Trond Myklebusta073dbf2013-03-20 12:34:32 -04002167 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
Andy Adamsondb29c082011-07-30 20:52:38 -04002168}
2169
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002170/*
2171 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2172 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2173 * data to disk to allow the server to recover the data if it crashes.
2174 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2175 * is off, and a COMMIT is sent to a data server, or
2176 * if WRITEs to a data server return NFS_DATA_SYNC.
2177 */
Andy Adamson863a3c62011-03-23 13:27:54 +00002178int
Andy Adamsonef311532011-03-12 02:58:10 -05002179pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00002180{
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002181 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
Andy Adamson863a3c62011-03-23 13:27:54 +00002182 struct nfs4_layoutcommit_data *data;
2183 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002184 loff_t end_pos;
Trond Myklebust71244d92014-01-13 13:34:36 -05002185 int status;
2186
2187 if (!pnfs_layoutcommit_outstanding(inode))
2188 return 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002189
2190 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2191
Trond Myklebust71244d92014-01-13 13:34:36 -05002192 status = -EAGAIN;
2193 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2194 if (!sync)
2195 goto out;
NeilBrown74316202014-07-07 15:16:04 +10002196 status = wait_on_bit_lock_action(&nfsi->flags,
Trond Myklebust71244d92014-01-13 13:34:36 -05002197 NFS_INO_LAYOUTCOMMITTING,
2198 nfs_wait_bit_killable,
2199 TASK_KILLABLE);
2200 if (status)
2201 goto out;
2202 }
Andy Adamsonde4b15c2011-03-12 02:58:09 -05002203
Trond Myklebust71244d92014-01-13 13:34:36 -05002204 status = -ENOMEM;
Andy Adamson863a3c62011-03-23 13:27:54 +00002205 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2206 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebust71244d92014-01-13 13:34:36 -05002207 if (!data)
2208 goto clear_layoutcommitting;
Andy Adamson863a3c62011-03-23 13:27:54 +00002209
Trond Myklebust71244d92014-01-13 13:34:36 -05002210 status = 0;
2211 spin_lock(&inode->i_lock);
2212 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2213 goto out_unlock;
Peng Tao92407e72011-10-23 20:21:17 -07002214
Peng Taoa9bae562011-07-30 20:52:33 -04002215 INIT_LIST_HEAD(&data->lseg_list);
Peng Taoa9bae562011-07-30 20:52:33 -04002216 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00002217
Peng Taoacff58802011-07-30 20:52:31 -04002218 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04002219 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00002220
Trond Myklebustf597c532012-03-04 18:13:56 -05002221 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00002222 spin_unlock(&inode->i_lock);
2223
2224 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04002225 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00002226 nfs_fattr_init(&data->fattr);
2227 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2228 data->res.fattr = &data->fattr;
2229 data->args.lastbytewritten = end_pos - 1;
2230 data->res.server = NFS_SERVER(inode);
2231
Christoph Hellwig5f919c92014-08-21 11:09:25 -05002232 if (ld->prepare_layoutcommit) {
2233 status = ld->prepare_layoutcommit(&data->args);
2234 if (status) {
2235 spin_lock(&inode->i_lock);
2236 if (end_pos < nfsi->layout->plh_lwb)
2237 nfsi->layout->plh_lwb = end_pos;
2238 spin_unlock(&inode->i_lock);
2239 put_rpccred(data->cred);
2240 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2241 goto clear_layoutcommitting;
2242 }
2243 }
2244
2245
Andy Adamson863a3c62011-03-23 13:27:54 +00002246 status = nfs4_proc_layoutcommit(data, sync);
2247out:
Peng Tao92407e72011-10-23 20:21:17 -07002248 if (status)
2249 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00002250 dprintk("<-- %s status %d\n", __func__, status);
2251 return status;
Trond Myklebust71244d92014-01-13 13:34:36 -05002252out_unlock:
2253 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07002254 kfree(data);
Trond Myklebust71244d92014-01-13 13:34:36 -05002255clear_layoutcommitting:
2256 pnfs_clear_layoutcommitting(inode);
Peng Tao92407e72011-10-23 20:21:17 -07002257 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00002258}
Peng Tao72cff442014-08-07 10:12:38 +08002259EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
Andy Adamson82be4172012-05-23 05:02:35 -04002260
2261struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2262{
2263 struct nfs4_threshold *thp;
2264
2265 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2266 if (!thp) {
2267 dprintk("%s mdsthreshold allocation failed\n", __func__);
2268 return NULL;
2269 }
2270 return thp;
2271}