]>
Commit | Line | Data |
---|---|---|
85e174ba RL |
1 | /* |
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> | |
493292dd | 31 | #include <linux/nfs_page.h> |
143cb494 | 32 | #include <linux/module.h> |
974cec8c | 33 | #include "internal.h" |
85e174ba | 34 | #include "pnfs.h" |
64419a9b | 35 | #include "iostat.h" |
cc668ab3 | 36 | #include "nfs4trace.h" |
40dd4b7a | 37 | #include "delegation.h" |
8733408d | 38 | #include "nfs42.h" |
85e174ba RL |
39 | |
40 | #define NFSDBG_FACILITY NFSDBG_PNFS | |
25c75333 | 41 | #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) |
85e174ba | 42 | |
02c35fca FI |
43 | /* Locking: |
44 | * | |
45 | * pnfs_spinlock: | |
46 | * protects pnfs_modules_tbl. | |
47 | */ | |
48 | static DEFINE_SPINLOCK(pnfs_spinlock); | |
49 | ||
50 | /* | |
51 | * pnfs_modules_tbl holds all pnfs modules | |
52 | */ | |
53 | static LIST_HEAD(pnfs_modules_tbl); | |
54 | ||
13c13a6a | 55 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo); |
aa1e0e3a | 56 | |
02c35fca FI |
57 | /* Return the registered pnfs layout driver module matching given id */ |
58 | static struct pnfs_layoutdriver_type * | |
59 | find_pnfs_driver_locked(u32 id) | |
60 | { | |
61 | struct pnfs_layoutdriver_type *local; | |
62 | ||
63 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) | |
64 | if (local->id == id) | |
65 | goto out; | |
66 | local = NULL; | |
67 | out: | |
68 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); | |
69 | return local; | |
70 | } | |
71 | ||
85e174ba RL |
72 | static struct pnfs_layoutdriver_type * |
73 | find_pnfs_driver(u32 id) | |
74 | { | |
02c35fca FI |
75 | struct pnfs_layoutdriver_type *local; |
76 | ||
77 | spin_lock(&pnfs_spinlock); | |
78 | local = find_pnfs_driver_locked(id); | |
0a9c63fa TM |
79 | if (local != NULL && !try_module_get(local->owner)) { |
80 | dprintk("%s: Could not grab reference on module\n", __func__); | |
81 | local = NULL; | |
82 | } | |
02c35fca FI |
83 | spin_unlock(&pnfs_spinlock); |
84 | return local; | |
85e174ba RL |
85 | } |
86 | ||
87 | void | |
88 | unset_pnfs_layoutdriver(struct nfs_server *nfss) | |
89 | { | |
738fd0f3 BH |
90 | if (nfss->pnfs_curr_ld) { |
91 | if (nfss->pnfs_curr_ld->clear_layoutdriver) | |
92 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); | |
2a4c8994 TM |
93 | /* Decrement the MDS count. Purge the deviceid cache if zero */ |
94 | if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) | |
95 | nfs4_deviceid_purge_client(nfss->nfs_client); | |
02c35fca | 96 | module_put(nfss->pnfs_curr_ld->owner); |
738fd0f3 | 97 | } |
85e174ba RL |
98 | nfss->pnfs_curr_ld = NULL; |
99 | } | |
100 | ||
101 | /* | |
102 | * Try to set the server's pnfs module to the pnfs layout type specified by id. | |
103 | * Currently only one pNFS layout driver per filesystem is supported. | |
104 | * | |
3132e49e | 105 | * @ids array of layout types supported by MDS. |
85e174ba RL |
106 | */ |
107 | void | |
738fd0f3 | 108 | set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, |
3132e49e | 109 | u32 *ids) |
85e174ba RL |
110 | { |
111 | struct pnfs_layoutdriver_type *ld_type = NULL; | |
3132e49e | 112 | u32 id; |
85e174ba | 113 | |
85e174ba RL |
114 | if (!(server->nfs_client->cl_exchange_flags & |
115 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { | |
3132e49e JL |
116 | printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n", |
117 | __func__, server->nfs_client->cl_exchange_flags); | |
85e174ba RL |
118 | goto out_no_driver; |
119 | } | |
3132e49e JL |
120 | |
121 | id = ids[0]; | |
122 | if (!id) | |
123 | goto out_no_driver; | |
124 | ||
85e174ba RL |
125 | ld_type = find_pnfs_driver(id); |
126 | if (!ld_type) { | |
127 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); | |
128 | ld_type = find_pnfs_driver(id); | |
85e174ba | 129 | } |
3132e49e JL |
130 | |
131 | if (!ld_type) { | |
132 | dprintk("%s: No pNFS module found for %u.\n", __func__, id); | |
133 | goto out_no_driver; | |
134 | } | |
135 | ||
85e174ba | 136 | server->pnfs_curr_ld = ld_type; |
738fd0f3 BH |
137 | if (ld_type->set_layoutdriver |
138 | && ld_type->set_layoutdriver(server, mntfh)) { | |
a030889a WAA |
139 | printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " |
140 | "driver %u.\n", __func__, id); | |
738fd0f3 BH |
141 | module_put(ld_type->owner); |
142 | goto out_no_driver; | |
143 | } | |
2a4c8994 TM |
144 | /* Bump the MDS count */ |
145 | atomic_inc(&server->nfs_client->cl_mds_count); | |
ea8eecdd | 146 | |
85e174ba RL |
147 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
148 | return; | |
149 | ||
150 | out_no_driver: | |
151 | dprintk("%s: Using NFSv4 I/O\n", __func__); | |
152 | server->pnfs_curr_ld = NULL; | |
153 | } | |
02c35fca FI |
154 | |
155 | int | |
156 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
157 | { | |
158 | int status = -EINVAL; | |
159 | struct pnfs_layoutdriver_type *tmp; | |
160 | ||
161 | if (ld_type->id == 0) { | |
a030889a | 162 | printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); |
02c35fca FI |
163 | return status; |
164 | } | |
b1f69b75 | 165 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
a030889a | 166 | printk(KERN_ERR "NFS: %s Layout driver must provide " |
b1f69b75 AA |
167 | "alloc_lseg and free_lseg.\n", __func__); |
168 | return status; | |
169 | } | |
02c35fca FI |
170 | |
171 | spin_lock(&pnfs_spinlock); | |
172 | tmp = find_pnfs_driver_locked(ld_type->id); | |
173 | if (!tmp) { | |
174 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); | |
175 | status = 0; | |
176 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, | |
177 | ld_type->name); | |
178 | } else { | |
a030889a | 179 | printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", |
02c35fca FI |
180 | __func__, ld_type->id); |
181 | } | |
182 | spin_unlock(&pnfs_spinlock); | |
183 | ||
184 | return status; | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); | |
187 | ||
188 | void | |
189 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
190 | { | |
191 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); | |
192 | spin_lock(&pnfs_spinlock); | |
193 | list_del(&ld_type->pnfs_tblid); | |
194 | spin_unlock(&pnfs_spinlock); | |
195 | } | |
196 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); | |
e5e94017 | 197 | |
b1f69b75 AA |
198 | /* |
199 | * pNFS client layout cache | |
200 | */ | |
201 | ||
cc6e5340 | 202 | /* Need to hold i_lock if caller does not already hold reference */ |
43f1b3da | 203 | void |
70c3bd2b | 204 | pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 205 | { |
cc6e5340 | 206 | atomic_inc(&lo->plh_refcount); |
e5e94017 BH |
207 | } |
208 | ||
636fb9c8 BH |
209 | static struct pnfs_layout_hdr * |
210 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) | |
211 | { | |
212 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; | |
57934278 | 213 | return ld->alloc_layout_hdr(ino, gfp_flags); |
636fb9c8 BH |
214 | } |
215 | ||
216 | static void | |
217 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
218 | { | |
9c626381 TM |
219 | struct nfs_server *server = NFS_SERVER(lo->plh_inode); |
220 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; | |
221 | ||
222 | if (!list_empty(&lo->plh_layouts)) { | |
223 | struct nfs_client *clp = server->nfs_client; | |
224 | ||
225 | spin_lock(&clp->cl_lock); | |
226 | list_del_init(&lo->plh_layouts); | |
227 | spin_unlock(&clp->cl_lock); | |
228 | } | |
9fa40758 | 229 | put_rpccred(lo->plh_lc_cred); |
57934278 | 230 | return ld->free_layout_hdr(lo); |
636fb9c8 BH |
231 | } |
232 | ||
e5e94017 | 233 | static void |
6622c3ea | 234 | pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 235 | { |
bb346f63 | 236 | struct nfs_inode *nfsi = NFS_I(lo->plh_inode); |
cc6e5340 | 237 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
bb346f63 TM |
238 | nfsi->layout = NULL; |
239 | /* Reset MDS Threshold I/O counters */ | |
240 | nfsi->write_io = 0; | |
241 | nfsi->read_io = 0; | |
e5e94017 BH |
242 | } |
243 | ||
b1f69b75 | 244 | void |
70c3bd2b | 245 | pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) |
974cec8c | 246 | { |
cc6e5340 FI |
247 | struct inode *inode = lo->plh_inode; |
248 | ||
13c13a6a TM |
249 | pnfs_layoutreturn_before_put_layout_hdr(lo); |
250 | ||
cc6e5340 | 251 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { |
566f8737 PT |
252 | if (!list_empty(&lo->plh_segs)) |
253 | WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); | |
6622c3ea | 254 | pnfs_detach_layout_hdr(lo); |
cc6e5340 | 255 | spin_unlock(&inode->i_lock); |
6622c3ea | 256 | pnfs_free_layout_hdr(lo); |
cc6e5340 | 257 | } |
974cec8c AA |
258 | } |
259 | ||
2454dfea TM |
260 | /* |
261 | * Mark a pnfs_layout_hdr and all associated layout segments as invalid | |
262 | * | |
263 | * In order to continue using the pnfs_layout_hdr, a full recovery | |
264 | * is required. | |
265 | * Note that caller must hold inode->i_lock. | |
266 | */ | |
5f46be04 | 267 | int |
2454dfea TM |
268 | pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo, |
269 | struct list_head *lseg_list) | |
270 | { | |
271 | struct pnfs_layout_range range = { | |
272 | .iomode = IOMODE_ANY, | |
273 | .offset = 0, | |
274 | .length = NFS4_MAX_UINT64, | |
275 | }; | |
276 | ||
277 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
6d597e17 | 278 | return pnfs_mark_matching_lsegs_invalid(lo, lseg_list, &range, 0); |
2454dfea TM |
279 | } |
280 | ||
b9e028fd TM |
281 | static int |
282 | pnfs_iomode_to_fail_bit(u32 iomode) | |
283 | { | |
284 | return iomode == IOMODE_RW ? | |
285 | NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; | |
286 | } | |
287 | ||
288 | static void | |
3e621214 | 289 | pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) |
b9e028fd | 290 | { |
25c75333 | 291 | lo->plh_retry_timestamp = jiffies; |
39e88fcf | 292 | if (!test_and_set_bit(fail_bit, &lo->plh_flags)) |
3e621214 TM |
293 | atomic_inc(&lo->plh_refcount); |
294 | } | |
295 | ||
296 | static void | |
297 | pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) | |
298 | { | |
299 | if (test_and_clear_bit(fail_bit, &lo->plh_flags)) | |
300 | atomic_dec(&lo->plh_refcount); | |
301 | } | |
302 | ||
303 | static void | |
304 | pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
305 | { | |
306 | struct inode *inode = lo->plh_inode; | |
115ce575 TM |
307 | struct pnfs_layout_range range = { |
308 | .iomode = iomode, | |
309 | .offset = 0, | |
310 | .length = NFS4_MAX_UINT64, | |
311 | }; | |
312 | LIST_HEAD(head); | |
3e621214 TM |
313 | |
314 | spin_lock(&inode->i_lock); | |
315 | pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
6d597e17 | 316 | pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0); |
3e621214 | 317 | spin_unlock(&inode->i_lock); |
115ce575 | 318 | pnfs_free_lseg_list(&head); |
b9e028fd TM |
319 | dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, |
320 | iomode == IOMODE_RW ? "RW" : "READ"); | |
321 | } | |
322 | ||
323 | static bool | |
324 | pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
325 | { | |
25c75333 | 326 | unsigned long start, end; |
3e621214 TM |
327 | int fail_bit = pnfs_iomode_to_fail_bit(iomode); |
328 | ||
329 | if (test_bit(fail_bit, &lo->plh_flags) == 0) | |
25c75333 TM |
330 | return false; |
331 | end = jiffies; | |
332 | start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; | |
333 | if (!time_in_range(lo->plh_retry_timestamp, start, end)) { | |
334 | /* It is time to retry the failed layoutgets */ | |
3e621214 | 335 | pnfs_layout_clear_fail_bit(lo, fail_bit); |
25c75333 TM |
336 | return false; |
337 | } | |
338 | return true; | |
b9e028fd TM |
339 | } |
340 | ||
974cec8c | 341 | static void |
119cef97 TM |
342 | pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg, |
343 | const struct pnfs_layout_range *range, | |
344 | const nfs4_stateid *stateid) | |
974cec8c | 345 | { |
566052c5 | 346 | INIT_LIST_HEAD(&lseg->pls_list); |
a9bae566 | 347 | INIT_LIST_HEAD(&lseg->pls_lc_list); |
4541d16c | 348 | atomic_set(&lseg->pls_refcount, 1); |
4541d16c | 349 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); |
566052c5 | 350 | lseg->pls_layout = lo; |
119cef97 TM |
351 | lseg->pls_range = *range; |
352 | lseg->pls_seq = be32_to_cpu(stateid->seqid); | |
974cec8c AA |
353 | } |
354 | ||
905ca191 | 355 | static void pnfs_free_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 356 | { |
b7edfaa1 | 357 | struct inode *ino = lseg->pls_layout->plh_inode; |
974cec8c | 358 | |
b1f69b75 | 359 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
974cec8c AA |
360 | } |
361 | ||
d684d2ae | 362 | static void |
57036a37 TM |
363 | pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo, |
364 | struct pnfs_layout_segment *lseg) | |
d684d2ae | 365 | { |
57036a37 | 366 | struct inode *inode = lo->plh_inode; |
d684d2ae | 367 | |
d20581aa | 368 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
d684d2ae | 369 | list_del_init(&lseg->pls_list); |
8f0d27dc TM |
370 | /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */ |
371 | atomic_dec(&lo->plh_refcount); | |
2d148c7e | 372 | if (list_empty(&lo->plh_segs)) { |
334a8f37 TM |
373 | if (atomic_read(&lo->plh_outstanding) == 0) |
374 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
173f77e9 | 375 | clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
2d148c7e | 376 | } |
d684d2ae FI |
377 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); |
378 | } | |
379 | ||
bae724ef | 380 | void |
9369a431 | 381 | pnfs_put_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 382 | { |
57036a37 | 383 | struct pnfs_layout_hdr *lo; |
d684d2ae FI |
384 | struct inode *inode; |
385 | ||
386 | if (!lseg) | |
387 | return; | |
388 | ||
4541d16c FI |
389 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
390 | atomic_read(&lseg->pls_refcount), | |
391 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); | |
4ef2e4f8 | 392 | |
57036a37 TM |
393 | lo = lseg->pls_layout; |
394 | inode = lo->plh_inode; | |
4ef2e4f8 | 395 | |
d684d2ae | 396 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { |
faa4a54f TM |
397 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { |
398 | spin_unlock(&inode->i_lock); | |
399 | return; | |
400 | } | |
8f0d27dc | 401 | pnfs_get_layout_hdr(lo); |
4ef2e4f8 TM |
402 | pnfs_layout_remove_lseg(lo, lseg); |
403 | spin_unlock(&inode->i_lock); | |
404 | pnfs_free_lseg(lseg); | |
405 | pnfs_put_layout_hdr(lo); | |
4541d16c | 406 | } |
4541d16c | 407 | } |
9369a431 | 408 | EXPORT_SYMBOL_GPL(pnfs_put_lseg); |
974cec8c | 409 | |
6543f803 | 410 | static void pnfs_free_lseg_async_work(struct work_struct *work) |
e6cf82d1 WAA |
411 | { |
412 | struct pnfs_layout_segment *lseg; | |
6543f803 | 413 | struct pnfs_layout_hdr *lo; |
e6cf82d1 WAA |
414 | |
415 | lseg = container_of(work, struct pnfs_layout_segment, pls_work); | |
6543f803 | 416 | lo = lseg->pls_layout; |
e6cf82d1 | 417 | |
6543f803 TM |
418 | pnfs_free_lseg(lseg); |
419 | pnfs_put_layout_hdr(lo); | |
e6cf82d1 WAA |
420 | } |
421 | ||
6543f803 | 422 | static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg) |
e6cf82d1 | 423 | { |
6543f803 | 424 | INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work); |
e6cf82d1 WAA |
425 | schedule_work(&lseg->pls_work); |
426 | } | |
6543f803 TM |
427 | |
428 | void | |
429 | pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg) | |
430 | { | |
431 | if (!lseg) | |
432 | return; | |
433 | ||
434 | assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock); | |
435 | ||
436 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, | |
437 | atomic_read(&lseg->pls_refcount), | |
438 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); | |
439 | if (atomic_dec_and_test(&lseg->pls_refcount)) { | |
440 | struct pnfs_layout_hdr *lo = lseg->pls_layout; | |
faa4a54f TM |
441 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) |
442 | return; | |
6543f803 TM |
443 | pnfs_get_layout_hdr(lo); |
444 | pnfs_layout_remove_lseg(lo, lseg); | |
445 | pnfs_free_lseg_async(lseg); | |
446 | } | |
447 | } | |
448 | EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked); | |
e6cf82d1 | 449 | |
3cb2df17 | 450 | static u64 |
fb3296eb BH |
451 | end_offset(u64 start, u64 len) |
452 | { | |
453 | u64 end; | |
454 | ||
455 | end = start + len; | |
456 | return end >= start ? end : NFS4_MAX_UINT64; | |
457 | } | |
458 | ||
fb3296eb BH |
459 | /* |
460 | * is l2 fully contained in l1? | |
461 | * start1 end1 | |
462 | * [----------------------------------) | |
463 | * start2 end2 | |
464 | * [----------------) | |
465 | */ | |
3cb2df17 | 466 | static bool |
7dc0ac70 | 467 | pnfs_lseg_range_contained(const struct pnfs_layout_range *l1, |
3cb2df17 | 468 | const struct pnfs_layout_range *l2) |
fb3296eb BH |
469 | { |
470 | u64 start1 = l1->offset; | |
471 | u64 end1 = end_offset(start1, l1->length); | |
472 | u64 start2 = l2->offset; | |
473 | u64 end2 = end_offset(start2, l2->length); | |
474 | ||
475 | return (start1 <= start2) && (end1 >= end2); | |
476 | } | |
477 | ||
478 | /* | |
479 | * is l1 and l2 intersecting? | |
480 | * start1 end1 | |
481 | * [----------------------------------) | |
482 | * start2 end2 | |
483 | * [----------------) | |
484 | */ | |
3cb2df17 | 485 | static bool |
7dc0ac70 | 486 | pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1, |
3cb2df17 | 487 | const struct pnfs_layout_range *l2) |
fb3296eb BH |
488 | { |
489 | u64 start1 = l1->offset; | |
490 | u64 end1 = end_offset(start1, l1->length); | |
491 | u64 start2 = l2->offset; | |
492 | u64 end2 = end_offset(start2, l2->length); | |
493 | ||
494 | return (end1 == NFS4_MAX_UINT64 || end1 > start2) && | |
495 | (end2 == NFS4_MAX_UINT64 || end2 > start1); | |
496 | } | |
497 | ||
24956804 TM |
498 | static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, |
499 | struct list_head *tmp_list) | |
500 | { | |
501 | if (!atomic_dec_and_test(&lseg->pls_refcount)) | |
502 | return false; | |
503 | pnfs_layout_remove_lseg(lseg->pls_layout, lseg); | |
504 | list_add(&lseg->pls_list, tmp_list); | |
505 | return true; | |
506 | } | |
507 | ||
4541d16c FI |
508 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
509 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, | |
510 | struct list_head *tmp_list) | |
511 | { | |
512 | int rv = 0; | |
513 | ||
514 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { | |
515 | /* Remove the reference keeping the lseg in the | |
516 | * list. It will now be removed when all | |
517 | * outstanding io is finished. | |
518 | */ | |
d684d2ae FI |
519 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
520 | atomic_read(&lseg->pls_refcount)); | |
24956804 | 521 | if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list)) |
d684d2ae | 522 | rv = 1; |
4541d16c FI |
523 | } |
524 | return rv; | |
525 | } | |
526 | ||
6d597e17 JL |
527 | /* |
528 | * Compare 2 layout stateid sequence ids, to see which is newer, | |
529 | * taking into account wraparound issues. | |
530 | */ | |
531 | static bool pnfs_seqid_is_newer(u32 s1, u32 s2) | |
532 | { | |
533 | return (s32)(s1 - s2) > 0; | |
534 | } | |
535 | ||
e036f464 TM |
536 | static bool |
537 | pnfs_should_free_range(const struct pnfs_layout_range *lseg_range, | |
538 | const struct pnfs_layout_range *recall_range) | |
539 | { | |
540 | return (recall_range->iomode == IOMODE_ANY || | |
541 | lseg_range->iomode == recall_range->iomode) && | |
542 | pnfs_lseg_range_intersecting(lseg_range, recall_range); | |
543 | } | |
544 | ||
545 | static bool | |
546 | pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg, | |
547 | const struct pnfs_layout_range *recall_range, | |
548 | u32 seq) | |
549 | { | |
550 | if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq)) | |
551 | return false; | |
552 | if (recall_range == NULL) | |
553 | return true; | |
554 | return pnfs_should_free_range(&lseg->pls_range, recall_range); | |
555 | } | |
556 | ||
6d597e17 JL |
557 | /** |
558 | * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later | |
559 | * @lo: layout header containing the lsegs | |
560 | * @tmp_list: list head where doomed lsegs should go | |
561 | * @recall_range: optional recall range argument to match (may be NULL) | |
562 | * @seq: only invalidate lsegs obtained prior to this sequence (may be 0) | |
563 | * | |
564 | * Walk the list of lsegs in the layout header, and tear down any that should | |
565 | * be destroyed. If "recall_range" is specified then the segment must match | |
566 | * that range. If "seq" is non-zero, then only match segments that were handed | |
567 | * out at or before that sequence. | |
568 | * | |
569 | * Returns number of matching invalid lsegs remaining in list after scanning | |
570 | * it and purging them. | |
4541d16c | 571 | */ |
43f1b3da | 572 | int |
49a85061 | 573 | pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
4541d16c | 574 | struct list_head *tmp_list, |
6d597e17 JL |
575 | const struct pnfs_layout_range *recall_range, |
576 | u32 seq) | |
974cec8c AA |
577 | { |
578 | struct pnfs_layout_segment *lseg, *next; | |
71b39854 | 579 | int remaining = 0; |
974cec8c AA |
580 | |
581 | dprintk("%s:Begin lo %p\n", __func__, lo); | |
582 | ||
8006bfba | 583 | if (list_empty(&lo->plh_segs)) |
38511722 | 584 | return 0; |
4541d16c | 585 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
e036f464 | 586 | if (pnfs_match_lseg_recall(lseg, recall_range, seq)) { |
6d597e17 | 587 | dprintk("%s: freeing lseg %p iomode %d seq %u" |
4541d16c | 588 | "offset %llu length %llu\n", __func__, |
6d597e17 JL |
589 | lseg, lseg->pls_range.iomode, lseg->pls_seq, |
590 | lseg->pls_range.offset, lseg->pls_range.length); | |
71b39854 TM |
591 | if (!mark_lseg_invalid(lseg, tmp_list)) |
592 | remaining++; | |
4541d16c | 593 | } |
71b39854 TM |
594 | dprintk("%s:Return %i\n", __func__, remaining); |
595 | return remaining; | |
974cec8c AA |
596 | } |
597 | ||
f49f9baa | 598 | /* note free_me must contain lsegs from a single layout_hdr */ |
43f1b3da | 599 | void |
4541d16c | 600 | pnfs_free_lseg_list(struct list_head *free_me) |
974cec8c | 601 | { |
4541d16c | 602 | struct pnfs_layout_segment *lseg, *tmp; |
f49f9baa FI |
603 | |
604 | if (list_empty(free_me)) | |
605 | return; | |
606 | ||
4541d16c | 607 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
566052c5 | 608 | list_del(&lseg->pls_list); |
905ca191 | 609 | pnfs_free_lseg(lseg); |
974cec8c AA |
610 | } |
611 | } | |
612 | ||
e5e94017 BH |
613 | void |
614 | pnfs_destroy_layout(struct nfs_inode *nfsi) | |
615 | { | |
616 | struct pnfs_layout_hdr *lo; | |
974cec8c | 617 | LIST_HEAD(tmp_list); |
e5e94017 BH |
618 | |
619 | spin_lock(&nfsi->vfs_inode.i_lock); | |
620 | lo = nfsi->layout; | |
621 | if (lo) { | |
3e621214 | 622 | pnfs_get_layout_hdr(lo); |
2454dfea | 623 | pnfs_mark_layout_stateid_invalid(lo, &tmp_list); |
3e621214 TM |
624 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED); |
625 | pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED); | |
626 | spin_unlock(&nfsi->vfs_inode.i_lock); | |
627 | pnfs_free_lseg_list(&tmp_list); | |
628 | pnfs_put_layout_hdr(lo); | |
629 | } else | |
630 | spin_unlock(&nfsi->vfs_inode.i_lock); | |
974cec8c | 631 | } |
041245c8 | 632 | EXPORT_SYMBOL_GPL(pnfs_destroy_layout); |
974cec8c | 633 | |
fd9a8d71 TM |
634 | static bool |
635 | pnfs_layout_add_bulk_destroy_list(struct inode *inode, | |
636 | struct list_head *layout_list) | |
974cec8c AA |
637 | { |
638 | struct pnfs_layout_hdr *lo; | |
fd9a8d71 | 639 | bool ret = false; |
974cec8c | 640 | |
fd9a8d71 TM |
641 | spin_lock(&inode->i_lock); |
642 | lo = NFS_I(inode)->layout; | |
643 | if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) { | |
644 | pnfs_get_layout_hdr(lo); | |
645 | list_add(&lo->plh_bulk_destroy, layout_list); | |
646 | ret = true; | |
647 | } | |
648 | spin_unlock(&inode->i_lock); | |
649 | return ret; | |
650 | } | |
651 | ||
652 | /* Caller must hold rcu_read_lock and clp->cl_lock */ | |
653 | static int | |
654 | pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp, | |
655 | struct nfs_server *server, | |
656 | struct list_head *layout_list) | |
657 | { | |
658 | struct pnfs_layout_hdr *lo, *next; | |
659 | struct inode *inode; | |
660 | ||
661 | list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) { | |
662 | inode = igrab(lo->plh_inode); | |
663 | if (inode == NULL) | |
664 | continue; | |
665 | list_del_init(&lo->plh_layouts); | |
666 | if (pnfs_layout_add_bulk_destroy_list(inode, layout_list)) | |
667 | continue; | |
668 | rcu_read_unlock(); | |
669 | spin_unlock(&clp->cl_lock); | |
670 | iput(inode); | |
671 | spin_lock(&clp->cl_lock); | |
672 | rcu_read_lock(); | |
673 | return -EAGAIN; | |
674 | } | |
675 | return 0; | |
676 | } | |
677 | ||
678 | static int | |
679 | pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list, | |
680 | bool is_bulk_recall) | |
681 | { | |
682 | struct pnfs_layout_hdr *lo; | |
683 | struct inode *inode; | |
fd9a8d71 TM |
684 | LIST_HEAD(lseg_list); |
685 | int ret = 0; | |
686 | ||
687 | while (!list_empty(layout_list)) { | |
688 | lo = list_entry(layout_list->next, struct pnfs_layout_hdr, | |
689 | plh_bulk_destroy); | |
690 | dprintk("%s freeing layout for inode %lu\n", __func__, | |
691 | lo->plh_inode->i_ino); | |
692 | inode = lo->plh_inode; | |
7c5d1875 CH |
693 | |
694 | pnfs_layoutcommit_inode(inode, false); | |
695 | ||
fd9a8d71 TM |
696 | spin_lock(&inode->i_lock); |
697 | list_del_init(&lo->plh_bulk_destroy); | |
9fd4b9fc TM |
698 | if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) { |
699 | if (is_bulk_recall) | |
700 | set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); | |
fd9a8d71 | 701 | ret = -EAGAIN; |
9fd4b9fc | 702 | } |
fd9a8d71 TM |
703 | spin_unlock(&inode->i_lock); |
704 | pnfs_free_lseg_list(&lseg_list); | |
b20135d0 TM |
705 | /* Free all lsegs that are attached to commit buckets */ |
706 | nfs_commit_inode(inode, 0); | |
fd9a8d71 TM |
707 | pnfs_put_layout_hdr(lo); |
708 | iput(inode); | |
709 | } | |
710 | return ret; | |
711 | } | |
712 | ||
713 | int | |
714 | pnfs_destroy_layouts_byfsid(struct nfs_client *clp, | |
715 | struct nfs_fsid *fsid, | |
716 | bool is_recall) | |
717 | { | |
718 | struct nfs_server *server; | |
719 | LIST_HEAD(layout_list); | |
c47abcf8 | 720 | |
974cec8c | 721 | spin_lock(&clp->cl_lock); |
6382a441 | 722 | rcu_read_lock(); |
fd9a8d71 | 723 | restart: |
6382a441 | 724 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
fd9a8d71 TM |
725 | if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0) |
726 | continue; | |
727 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, | |
728 | server, | |
729 | &layout_list) != 0) | |
730 | goto restart; | |
6382a441 WAA |
731 | } |
732 | rcu_read_unlock(); | |
974cec8c AA |
733 | spin_unlock(&clp->cl_lock); |
734 | ||
fd9a8d71 TM |
735 | if (list_empty(&layout_list)) |
736 | return 0; | |
737 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); | |
738 | } | |
739 | ||
740 | int | |
741 | pnfs_destroy_layouts_byclid(struct nfs_client *clp, | |
742 | bool is_recall) | |
743 | { | |
744 | struct nfs_server *server; | |
745 | LIST_HEAD(layout_list); | |
746 | ||
747 | spin_lock(&clp->cl_lock); | |
748 | rcu_read_lock(); | |
749 | restart: | |
750 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | |
751 | if (pnfs_layout_bulk_destroy_byserver_locked(clp, | |
752 | server, | |
753 | &layout_list) != 0) | |
754 | goto restart; | |
974cec8c | 755 | } |
fd9a8d71 TM |
756 | rcu_read_unlock(); |
757 | spin_unlock(&clp->cl_lock); | |
758 | ||
759 | if (list_empty(&layout_list)) | |
760 | return 0; | |
761 | return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall); | |
762 | } | |
763 | ||
764 | /* | |
765 | * Called by the state manger to remove all layouts established under an | |
766 | * expired lease. | |
767 | */ | |
768 | void | |
769 | pnfs_destroy_all_layouts(struct nfs_client *clp) | |
770 | { | |
771 | nfs4_deviceid_mark_client_invalid(clp); | |
772 | nfs4_deviceid_purge_client(clp); | |
773 | ||
774 | pnfs_destroy_layouts_byclid(clp, false); | |
e5e94017 BH |
775 | } |
776 | ||
2a59a041 TM |
777 | static void |
778 | pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo) | |
779 | { | |
780 | lo->plh_return_iomode = 0; | |
781 | lo->plh_return_seq = 0; | |
782 | clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); | |
783 | } | |
784 | ||
fd6002e9 | 785 | /* update lo->plh_stateid with new if is more recent */ |
43f1b3da FI |
786 | void |
787 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, | |
788 | bool update_barrier) | |
b1f69b75 | 789 | { |
ecebb80b | 790 | u32 oldseq, newseq, new_barrier = 0; |
b1f69b75 | 791 | |
2d2f24ad TM |
792 | oldseq = be32_to_cpu(lo->plh_stateid.seqid); |
793 | newseq = be32_to_cpu(new->seqid); | |
2a59a041 TM |
794 | |
795 | if (!pnfs_layout_is_valid(lo)) { | |
796 | nfs4_stateid_copy(&lo->plh_stateid, new); | |
797 | lo->plh_barrier = newseq; | |
798 | pnfs_clear_layoutreturn_info(lo); | |
799 | clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); | |
800 | return; | |
801 | } | |
802 | if (pnfs_seqid_is_newer(newseq, oldseq)) { | |
f597c537 | 803 | nfs4_stateid_copy(&lo->plh_stateid, new); |
ecebb80b TM |
804 | /* |
805 | * Because of wraparound, we want to keep the barrier | |
806 | * "close" to the current seqids. | |
807 | */ | |
808 | new_barrier = newseq - atomic_read(&lo->plh_outstanding); | |
43f1b3da | 809 | } |
ecebb80b TM |
810 | if (update_barrier) |
811 | new_barrier = be32_to_cpu(new->seqid); | |
812 | else if (new_barrier == 0) | |
813 | return; | |
2a59a041 | 814 | if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier)) |
ecebb80b | 815 | lo->plh_barrier = new_barrier; |
b1f69b75 AA |
816 | } |
817 | ||
cf7d63f1 | 818 | static bool |
19c54aba TM |
819 | pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo, |
820 | const nfs4_stateid *stateid) | |
43f1b3da | 821 | { |
19c54aba | 822 | u32 seqid = be32_to_cpu(stateid->seqid); |
25a1a621 | 823 | |
19c54aba TM |
824 | return !pnfs_seqid_is_newer(seqid, lo->plh_barrier); |
825 | } | |
826 | ||
827 | /* lget is set to 1 if called from inside send_layoutget call chain */ | |
828 | static bool | |
e1c06f80 | 829 | pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo) |
19c54aba | 830 | { |
f7e8917a | 831 | return lo->plh_block_lgets || |
e1c06f80 | 832 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); |
cf7d63f1 FI |
833 | } |
834 | ||
b1f69b75 | 835 | /* |
183d9e7b JL |
836 | * Get layout from server. |
837 | * for now, assume that whole file layouts are requested. | |
838 | * arg->offset: 0 | |
839 | * arg->length: all ones | |
840 | */ | |
e5e94017 BH |
841 | static struct pnfs_layout_segment * |
842 | send_layoutget(struct pnfs_layout_hdr *lo, | |
843 | struct nfs_open_context *ctx, | |
183d9e7b | 844 | nfs4_stateid *stateid, |
e144e539 | 845 | const struct pnfs_layout_range *range, |
183d9e7b | 846 | long *timeout, gfp_t gfp_flags) |
e5e94017 | 847 | { |
b7edfaa1 | 848 | struct inode *ino = lo->plh_inode; |
b1f69b75 AA |
849 | struct nfs_server *server = NFS_SERVER(ino); |
850 | struct nfs4_layoutget *lgp; | |
2d89a1d3 | 851 | loff_t i_size; |
b1f69b75 AA |
852 | |
853 | dprintk("--> %s\n", __func__); | |
e5e94017 | 854 | |
4f2e9dce JL |
855 | /* |
856 | * Synchronously retrieve layout information from server and | |
857 | * store in lseg. If we race with a concurrent seqid morphing | |
858 | * op, then re-send the LAYOUTGET. | |
859 | */ | |
83026d80 JL |
860 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
861 | if (lgp == NULL) | |
862 | return ERR_PTR(-ENOMEM); | |
863 | ||
864 | i_size = i_size_read(ino); | |
865 | ||
866 | lgp->args.minlength = PAGE_SIZE; | |
867 | if (lgp->args.minlength > range->length) | |
868 | lgp->args.minlength = range->length; | |
869 | if (range->iomode == IOMODE_READ) { | |
870 | if (range->offset >= i_size) | |
871 | lgp->args.minlength = 0; | |
872 | else if (i_size - range->offset < lgp->args.minlength) | |
873 | lgp->args.minlength = i_size - range->offset; | |
d03ab29d | 874 | } |
83026d80 JL |
875 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
876 | pnfs_copy_range(&lgp->args.range, range); | |
877 | lgp->args.type = server->pnfs_curr_ld->id; | |
878 | lgp->args.inode = ino; | |
879 | lgp->args.ctx = get_nfs_open_context(ctx); | |
183d9e7b | 880 | nfs4_stateid_copy(&lgp->args.stateid, stateid); |
83026d80 JL |
881 | lgp->gfp_flags = gfp_flags; |
882 | lgp->cred = lo->plh_lc_cred; | |
35124a09 | 883 | |
183d9e7b | 884 | return nfs4_proc_layoutget(lgp, timeout, gfp_flags); |
974cec8c AA |
885 | } |
886 | ||
24956804 TM |
887 | static void pnfs_clear_layoutcommit(struct inode *inode, |
888 | struct list_head *head) | |
889 | { | |
890 | struct nfs_inode *nfsi = NFS_I(inode); | |
891 | struct pnfs_layout_segment *lseg, *tmp; | |
892 | ||
893 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) | |
894 | return; | |
895 | list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) { | |
896 | if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) | |
897 | continue; | |
898 | pnfs_lseg_dec_and_remove_zero(lseg, head); | |
899 | } | |
900 | } | |
901 | ||
d67ae825 TH |
902 | void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo) |
903 | { | |
904 | clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags); | |
905 | smp_mb__after_atomic(); | |
906 | wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN); | |
7f27392c | 907 | rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq); |
d67ae825 TH |
908 | } |
909 | ||
13c13a6a | 910 | static bool |
e5fd1904 TM |
911 | pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo, |
912 | nfs4_stateid *stateid, | |
913 | enum pnfs_iomode *iomode) | |
13c13a6a | 914 | { |
bf0291dd TM |
915 | /* Serialise LAYOUTGET/LAYOUTRETURN */ |
916 | if (atomic_read(&lo->plh_outstanding) != 0) | |
917 | return false; | |
13c13a6a TM |
918 | if (test_and_set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) |
919 | return false; | |
13c13a6a | 920 | pnfs_get_layout_hdr(lo); |
e5fd1904 TM |
921 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) { |
922 | if (stateid != NULL) { | |
923 | nfs4_stateid_copy(stateid, &lo->plh_stateid); | |
924 | if (lo->plh_return_seq != 0) | |
925 | stateid->seqid = cpu_to_be32(lo->plh_return_seq); | |
926 | } | |
927 | if (iomode != NULL) | |
928 | *iomode = lo->plh_return_iomode; | |
929 | pnfs_clear_layoutreturn_info(lo); | |
930 | return true; | |
931 | } | |
932 | if (stateid != NULL) | |
933 | nfs4_stateid_copy(stateid, &lo->plh_stateid); | |
934 | if (iomode != NULL) | |
935 | *iomode = IOMODE_ANY; | |
13c13a6a TM |
936 | return true; |
937 | } | |
938 | ||
f40eb5d0 | 939 | static int |
ed429d6b | 940 | pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid, |
6c16605d | 941 | enum pnfs_iomode iomode, bool sync) |
f40eb5d0 PT |
942 | { |
943 | struct inode *ino = lo->plh_inode; | |
944 | struct nfs4_layoutreturn *lrp; | |
945 | int status = 0; | |
946 | ||
e4af440a | 947 | lrp = kzalloc(sizeof(*lrp), GFP_NOFS); |
f40eb5d0 PT |
948 | if (unlikely(lrp == NULL)) { |
949 | status = -ENOMEM; | |
950 | spin_lock(&ino->i_lock); | |
d67ae825 | 951 | pnfs_clear_layoutreturn_waitbit(lo); |
f40eb5d0 PT |
952 | spin_unlock(&ino->i_lock); |
953 | pnfs_put_layout_hdr(lo); | |
954 | goto out; | |
955 | } | |
956 | ||
ed429d6b | 957 | nfs4_stateid_copy(&lrp->args.stateid, stateid); |
f40eb5d0 PT |
958 | lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id; |
959 | lrp->args.inode = ino; | |
15eb67c1 PT |
960 | lrp->args.range.iomode = iomode; |
961 | lrp->args.range.offset = 0; | |
962 | lrp->args.range.length = NFS4_MAX_UINT64; | |
f40eb5d0 PT |
963 | lrp->args.layout = lo; |
964 | lrp->clp = NFS_SERVER(ino)->nfs_client; | |
965 | lrp->cred = lo->plh_lc_cred; | |
966 | ||
6c16605d | 967 | status = nfs4_proc_layoutreturn(lrp, sync); |
f40eb5d0 PT |
968 | out: |
969 | dprintk("<-- %s status: %d\n", __func__, status); | |
970 | return status; | |
971 | } | |
972 | ||
13c13a6a TM |
973 | /* Return true if layoutreturn is needed */ |
974 | static bool | |
975 | pnfs_layout_need_return(struct pnfs_layout_hdr *lo) | |
976 | { | |
977 | struct pnfs_layout_segment *s; | |
978 | ||
2370abda | 979 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
13c13a6a TM |
980 | return false; |
981 | ||
982 | /* Defer layoutreturn until all lsegs are done */ | |
983 | list_for_each_entry(s, &lo->plh_segs, pls_list) { | |
984 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags)) | |
985 | return false; | |
986 | } | |
987 | ||
988 | return true; | |
989 | } | |
990 | ||
991 | static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo) | |
992 | { | |
993 | struct inode *inode= lo->plh_inode; | |
994 | ||
2370abda | 995 | if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
13c13a6a TM |
996 | return; |
997 | spin_lock(&inode->i_lock); | |
998 | if (pnfs_layout_need_return(lo)) { | |
999 | nfs4_stateid stateid; | |
1000 | enum pnfs_iomode iomode; | |
1001 | bool send; | |
1002 | ||
e5fd1904 | 1003 | send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode); |
13c13a6a TM |
1004 | spin_unlock(&inode->i_lock); |
1005 | if (send) { | |
1006 | /* Send an async layoutreturn so we dont deadlock */ | |
1007 | pnfs_send_layoutreturn(lo, &stateid, iomode, false); | |
1008 | } | |
1009 | } else | |
1010 | spin_unlock(&inode->i_lock); | |
1011 | } | |
1012 | ||
293b3b06 AA |
1013 | /* |
1014 | * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr | |
1015 | * when the layout segment list is empty. | |
1016 | * | |
1017 | * Note that a pnfs_layout_hdr can exist with an empty layout segment | |
1018 | * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the | |
1019 | * deviceid is marked invalid. | |
1020 | */ | |
cbe82603 BH |
1021 | int |
1022 | _pnfs_return_layout(struct inode *ino) | |
1023 | { | |
1024 | struct pnfs_layout_hdr *lo = NULL; | |
1025 | struct nfs_inode *nfsi = NFS_I(ino); | |
1026 | LIST_HEAD(tmp_list); | |
cbe82603 | 1027 | nfs4_stateid stateid; |
293b3b06 | 1028 | int status = 0, empty; |
7f27392c | 1029 | bool send; |
cbe82603 | 1030 | |
366d5052 | 1031 | dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); |
cbe82603 BH |
1032 | |
1033 | spin_lock(&ino->i_lock); | |
1034 | lo = nfsi->layout; | |
e5929f3c | 1035 | if (!lo) { |
cbe82603 | 1036 | spin_unlock(&ino->i_lock); |
293b3b06 AA |
1037 | dprintk("NFS: %s no layout to return\n", __func__); |
1038 | goto out; | |
cbe82603 | 1039 | } |
cbe82603 | 1040 | /* Reference matched in nfs4_layoutreturn_release */ |
70c3bd2b | 1041 | pnfs_get_layout_hdr(lo); |
293b3b06 | 1042 | empty = list_empty(&lo->plh_segs); |
24956804 | 1043 | pnfs_clear_layoutcommit(ino, &tmp_list); |
6d597e17 | 1044 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0); |
c88953d8 CH |
1045 | |
1046 | if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) { | |
1047 | struct pnfs_layout_range range = { | |
1048 | .iomode = IOMODE_ANY, | |
1049 | .offset = 0, | |
1050 | .length = NFS4_MAX_UINT64, | |
1051 | }; | |
1052 | NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range); | |
1053 | } | |
1054 | ||
293b3b06 AA |
1055 | /* Don't send a LAYOUTRETURN if list was initially empty */ |
1056 | if (empty) { | |
1057 | spin_unlock(&ino->i_lock); | |
293b3b06 | 1058 | dprintk("NFS: %s no layout segments to return\n", __func__); |
7f27392c | 1059 | goto out_put_layout_hdr; |
293b3b06 | 1060 | } |
47abadef | 1061 | |
e5fd1904 | 1062 | send = pnfs_prepare_layoutreturn(lo, &stateid, NULL); |
cbe82603 BH |
1063 | spin_unlock(&ino->i_lock); |
1064 | pnfs_free_lseg_list(&tmp_list); | |
7f27392c | 1065 | if (send) |
ed429d6b | 1066 | status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true); |
7f27392c TM |
1067 | out_put_layout_hdr: |
1068 | pnfs_put_layout_hdr(lo); | |
cbe82603 BH |
1069 | out: |
1070 | dprintk("<-- %s status: %d\n", __func__, status); | |
1071 | return status; | |
1072 | } | |
0a57cdac | 1073 | EXPORT_SYMBOL_GPL(_pnfs_return_layout); |
cbe82603 | 1074 | |
24028672 TM |
1075 | int |
1076 | pnfs_commit_and_return_layout(struct inode *inode) | |
1077 | { | |
1078 | struct pnfs_layout_hdr *lo; | |
1079 | int ret; | |
1080 | ||
1081 | spin_lock(&inode->i_lock); | |
1082 | lo = NFS_I(inode)->layout; | |
1083 | if (lo == NULL) { | |
1084 | spin_unlock(&inode->i_lock); | |
1085 | return 0; | |
1086 | } | |
1087 | pnfs_get_layout_hdr(lo); | |
1088 | /* Block new layoutgets and read/write to ds */ | |
1089 | lo->plh_block_lgets++; | |
1090 | spin_unlock(&inode->i_lock); | |
1091 | filemap_fdatawait(inode->i_mapping); | |
1092 | ret = pnfs_layoutcommit_inode(inode, true); | |
1093 | if (ret == 0) | |
1094 | ret = _pnfs_return_layout(inode); | |
1095 | spin_lock(&inode->i_lock); | |
1096 | lo->plh_block_lgets--; | |
1097 | spin_unlock(&inode->i_lock); | |
1098 | pnfs_put_layout_hdr(lo); | |
1099 | return ret; | |
1100 | } | |
1101 | ||
f7e8917a FI |
1102 | bool pnfs_roc(struct inode *ino) |
1103 | { | |
40dd4b7a TM |
1104 | struct nfs_inode *nfsi = NFS_I(ino); |
1105 | struct nfs_open_context *ctx; | |
1106 | struct nfs4_state *state; | |
f7e8917a FI |
1107 | struct pnfs_layout_hdr *lo; |
1108 | struct pnfs_layout_segment *lseg, *tmp; | |
193e3aa2 | 1109 | nfs4_stateid stateid; |
f7e8917a | 1110 | LIST_HEAD(tmp_list); |
e755d638 | 1111 | bool found = false, layoutreturn = false, roc = false; |
f7e8917a FI |
1112 | |
1113 | spin_lock(&ino->i_lock); | |
40dd4b7a | 1114 | lo = nfsi->layout; |
3976143b | 1115 | if (!lo || test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) |
40dd4b7a TM |
1116 | goto out_noroc; |
1117 | ||
e755d638 | 1118 | /* no roc if we hold a delegation */ |
40dd4b7a TM |
1119 | if (nfs4_check_delegation(ino, FMODE_READ)) |
1120 | goto out_noroc; | |
1121 | ||
1122 | list_for_each_entry(ctx, &nfsi->open_files, list) { | |
1123 | state = ctx->state; | |
1124 | /* Don't return layout if there is open file state */ | |
1125 | if (state != NULL && state->state != 0) | |
1126 | goto out_noroc; | |
1127 | } | |
1128 | ||
e755d638 | 1129 | /* always send layoutreturn if being marked so */ |
e5fd1904 TM |
1130 | if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) |
1131 | layoutreturn = pnfs_prepare_layoutreturn(lo, | |
1132 | &stateid, NULL); | |
e755d638 | 1133 | |
f7e8917a | 1134 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) |
e755d638 PT |
1135 | /* If we are sending layoutreturn, invalidate all valid lsegs */ |
1136 | if (layoutreturn || test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { | |
f7e8917a FI |
1137 | mark_lseg_invalid(lseg, &tmp_list); |
1138 | found = true; | |
1139 | } | |
500d701f | 1140 | /* ROC in two conditions: |
e755d638 PT |
1141 | * 1. there are ROC lsegs |
1142 | * 2. we don't send layoutreturn | |
e755d638 | 1143 | */ |
500d701f PT |
1144 | if (found && !layoutreturn) { |
1145 | /* lo ref dropped in pnfs_roc_release() */ | |
1146 | pnfs_get_layout_hdr(lo); | |
e755d638 | 1147 | roc = true; |
500d701f | 1148 | } |
f7e8917a | 1149 | |
40dd4b7a | 1150 | out_noroc: |
f7e8917a | 1151 | spin_unlock(&ino->i_lock); |
e755d638 PT |
1152 | pnfs_free_lseg_list(&tmp_list); |
1153 | pnfs_layoutcommit_inode(ino, true); | |
1154 | if (layoutreturn) | |
ed429d6b | 1155 | pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true); |
e755d638 | 1156 | return roc; |
f7e8917a FI |
1157 | } |
1158 | ||
1159 | void pnfs_roc_release(struct inode *ino) | |
1160 | { | |
1161 | struct pnfs_layout_hdr *lo; | |
1162 | ||
1163 | spin_lock(&ino->i_lock); | |
1164 | lo = NFS_I(ino)->layout; | |
5c4a79fb | 1165 | pnfs_clear_layoutreturn_waitbit(lo); |
6622c3ea TM |
1166 | if (atomic_dec_and_test(&lo->plh_refcount)) { |
1167 | pnfs_detach_layout_hdr(lo); | |
1168 | spin_unlock(&ino->i_lock); | |
1169 | pnfs_free_layout_hdr(lo); | |
1170 | } else | |
1171 | spin_unlock(&ino->i_lock); | |
f7e8917a FI |
1172 | } |
1173 | ||
1174 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) | |
1175 | { | |
1176 | struct pnfs_layout_hdr *lo; | |
1177 | ||
1178 | spin_lock(&ino->i_lock); | |
1179 | lo = NFS_I(ino)->layout; | |
0f35ad6f | 1180 | if (pnfs_seqid_is_newer(barrier, lo->plh_barrier)) |
f7e8917a FI |
1181 | lo->plh_barrier = barrier; |
1182 | spin_unlock(&ino->i_lock); | |
6a463beb | 1183 | trace_nfs4_layoutreturn_on_close(ino, 0); |
f7e8917a FI |
1184 | } |
1185 | ||
4ff376fe | 1186 | void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier) |
f7e8917a FI |
1187 | { |
1188 | struct nfs_inode *nfsi = NFS_I(ino); | |
7fdab069 | 1189 | struct pnfs_layout_hdr *lo; |
7fdab069 | 1190 | u32 current_seqid; |
f7e8917a FI |
1191 | |
1192 | spin_lock(&ino->i_lock); | |
7fdab069 TM |
1193 | lo = nfsi->layout; |
1194 | current_seqid = be32_to_cpu(lo->plh_stateid.seqid); | |
f7e8917a | 1195 | |
7fdab069 TM |
1196 | /* Since close does not return a layout stateid for use as |
1197 | * a barrier, we choose the worst-case barrier. | |
1198 | */ | |
1199 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); | |
f7e8917a | 1200 | spin_unlock(&ino->i_lock); |
f7e8917a FI |
1201 | } |
1202 | ||
500d701f PT |
1203 | bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task) |
1204 | { | |
1205 | struct nfs_inode *nfsi = NFS_I(ino); | |
1206 | struct pnfs_layout_hdr *lo; | |
1207 | bool sleep = false; | |
1208 | ||
1209 | /* we might not have grabbed lo reference. so need to check under | |
1210 | * i_lock */ | |
1211 | spin_lock(&ino->i_lock); | |
1212 | lo = nfsi->layout; | |
1213 | if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) | |
1214 | sleep = true; | |
1215 | spin_unlock(&ino->i_lock); | |
1216 | ||
1217 | if (sleep) | |
1218 | rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); | |
1219 | ||
1220 | return sleep; | |
1221 | } | |
1222 | ||
b1f69b75 AA |
1223 | /* |
1224 | * Compare two layout segments for sorting into layout cache. | |
1225 | * We want to preferentially return RW over RO layouts, so ensure those | |
1226 | * are seen first. | |
1227 | */ | |
1228 | static s64 | |
7dc0ac70 | 1229 | pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1, |
3cb2df17 | 1230 | const struct pnfs_layout_range *l2) |
b1f69b75 | 1231 | { |
fb3296eb BH |
1232 | s64 d; |
1233 | ||
1234 | /* high offset > low offset */ | |
1235 | d = l1->offset - l2->offset; | |
1236 | if (d) | |
1237 | return d; | |
1238 | ||
1239 | /* short length > long length */ | |
1240 | d = l2->length - l1->length; | |
1241 | if (d) | |
1242 | return d; | |
1243 | ||
b1f69b75 | 1244 | /* read > read/write */ |
fb3296eb | 1245 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
b1f69b75 AA |
1246 | } |
1247 | ||
03772d2f TM |
1248 | static bool |
1249 | pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1, | |
1250 | const struct pnfs_layout_range *l2) | |
1251 | { | |
1252 | return pnfs_lseg_range_cmp(l1, l2) > 0; | |
1253 | } | |
1254 | ||
1255 | static bool | |
1256 | pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg, | |
1257 | struct pnfs_layout_segment *old) | |
1258 | { | |
1259 | return false; | |
1260 | } | |
1261 | ||
1262 | void | |
1263 | pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo, | |
1264 | struct pnfs_layout_segment *lseg, | |
1265 | bool (*is_after)(const struct pnfs_layout_range *, | |
1266 | const struct pnfs_layout_range *), | |
1267 | bool (*do_merge)(struct pnfs_layout_segment *, | |
1268 | struct pnfs_layout_segment *), | |
1269 | struct list_head *free_me) | |
974cec8c | 1270 | { |
03772d2f | 1271 | struct pnfs_layout_segment *lp, *tmp; |
b1f69b75 | 1272 | |
974cec8c AA |
1273 | dprintk("%s:Begin\n", __func__); |
1274 | ||
03772d2f TM |
1275 | list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) { |
1276 | if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0) | |
1277 | continue; | |
1278 | if (do_merge(lseg, lp)) { | |
1279 | mark_lseg_invalid(lp, free_me); | |
1280 | continue; | |
1281 | } | |
1282 | if (is_after(&lseg->pls_range, &lp->pls_range)) | |
b1f69b75 | 1283 | continue; |
566052c5 | 1284 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
b1f69b75 AA |
1285 | dprintk("%s: inserted lseg %p " |
1286 | "iomode %d offset %llu length %llu before " | |
1287 | "lp %p iomode %d offset %llu length %llu\n", | |
566052c5 FI |
1288 | __func__, lseg, lseg->pls_range.iomode, |
1289 | lseg->pls_range.offset, lseg->pls_range.length, | |
1290 | lp, lp->pls_range.iomode, lp->pls_range.offset, | |
1291 | lp->pls_range.length); | |
fb3296eb | 1292 | goto out; |
974cec8c | 1293 | } |
fb3296eb BH |
1294 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
1295 | dprintk("%s: inserted lseg %p " | |
1296 | "iomode %d offset %llu length %llu at tail\n", | |
1297 | __func__, lseg, lseg->pls_range.iomode, | |
1298 | lseg->pls_range.offset, lseg->pls_range.length); | |
1299 | out: | |
70c3bd2b | 1300 | pnfs_get_layout_hdr(lo); |
974cec8c AA |
1301 | |
1302 | dprintk("%s:Return\n", __func__); | |
e5e94017 | 1303 | } |
03772d2f TM |
1304 | EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg); |
1305 | ||
1306 | static void | |
1307 | pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo, | |
1308 | struct pnfs_layout_segment *lseg, | |
1309 | struct list_head *free_me) | |
1310 | { | |
1311 | struct inode *inode = lo->plh_inode; | |
1312 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; | |
1313 | ||
1314 | if (ld->add_lseg != NULL) | |
1315 | ld->add_lseg(lo, lseg, free_me); | |
1316 | else | |
1317 | pnfs_generic_layout_insert_lseg(lo, lseg, | |
1318 | pnfs_lseg_range_is_after, | |
1319 | pnfs_lseg_no_merge, | |
1320 | free_me); | |
1321 | } | |
e5e94017 BH |
1322 | |
1323 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
1324 | alloc_init_layout_hdr(struct inode *ino, |
1325 | struct nfs_open_context *ctx, | |
1326 | gfp_t gfp_flags) | |
e5e94017 BH |
1327 | { |
1328 | struct pnfs_layout_hdr *lo; | |
1329 | ||
636fb9c8 | 1330 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
e5e94017 BH |
1331 | if (!lo) |
1332 | return NULL; | |
cc6e5340 | 1333 | atomic_set(&lo->plh_refcount, 1); |
b7edfaa1 FI |
1334 | INIT_LIST_HEAD(&lo->plh_layouts); |
1335 | INIT_LIST_HEAD(&lo->plh_segs); | |
fd9a8d71 | 1336 | INIT_LIST_HEAD(&lo->plh_bulk_destroy); |
b7edfaa1 | 1337 | lo->plh_inode = ino; |
5cc2216d | 1338 | lo->plh_lc_cred = get_rpccred(ctx->cred); |
67a3b721 | 1339 | lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID; |
e5e94017 BH |
1340 | return lo; |
1341 | } | |
1342 | ||
1343 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
1344 | pnfs_find_alloc_layout(struct inode *ino, |
1345 | struct nfs_open_context *ctx, | |
1346 | gfp_t gfp_flags) | |
e5241e43 TM |
1347 | __releases(&ino->i_lock) |
1348 | __acquires(&ino->i_lock) | |
e5e94017 BH |
1349 | { |
1350 | struct nfs_inode *nfsi = NFS_I(ino); | |
1351 | struct pnfs_layout_hdr *new = NULL; | |
1352 | ||
1353 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); | |
1354 | ||
251ec410 TM |
1355 | if (nfsi->layout != NULL) |
1356 | goto out_existing; | |
e5e94017 | 1357 | spin_unlock(&ino->i_lock); |
9fa40758 | 1358 | new = alloc_init_layout_hdr(ino, ctx, gfp_flags); |
e5e94017 BH |
1359 | spin_lock(&ino->i_lock); |
1360 | ||
251ec410 | 1361 | if (likely(nfsi->layout == NULL)) { /* Won the race? */ |
e5e94017 | 1362 | nfsi->layout = new; |
251ec410 | 1363 | return new; |
7175fe90 YN |
1364 | } else if (new != NULL) |
1365 | pnfs_free_layout_hdr(new); | |
251ec410 TM |
1366 | out_existing: |
1367 | pnfs_get_layout_hdr(nfsi->layout); | |
e5e94017 BH |
1368 | return nfsi->layout; |
1369 | } | |
1370 | ||
b1f69b75 AA |
1371 | /* |
1372 | * iomode matching rules: | |
c7d73af2 TH |
1373 | * iomode lseg strict match |
1374 | * iomode | |
1375 | * ----- ----- ------ ----- | |
1376 | * ANY READ N/A true | |
1377 | * ANY RW N/A true | |
1378 | * RW READ N/A false | |
1379 | * RW RW N/A true | |
1380 | * READ READ N/A true | |
1381 | * READ RW true false | |
1382 | * READ RW false true | |
b1f69b75 | 1383 | */ |
3cb2df17 | 1384 | static bool |
7dc0ac70 | 1385 | pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range, |
c7d73af2 TH |
1386 | const struct pnfs_layout_range *range, |
1387 | bool strict_iomode) | |
b1f69b75 | 1388 | { |
fb3296eb BH |
1389 | struct pnfs_layout_range range1; |
1390 | ||
1391 | if ((range->iomode == IOMODE_RW && | |
1392 | ls_range->iomode != IOMODE_RW) || | |
c7d73af2 TH |
1393 | (range->iomode != ls_range->iomode && |
1394 | strict_iomode == true) || | |
7dc0ac70 | 1395 | !pnfs_lseg_range_intersecting(ls_range, range)) |
fb3296eb BH |
1396 | return 0; |
1397 | ||
1398 | /* range1 covers only the first byte in the range */ | |
1399 | range1 = *range; | |
1400 | range1.length = 1; | |
7dc0ac70 | 1401 | return pnfs_lseg_range_contained(ls_range, &range1); |
b1f69b75 AA |
1402 | } |
1403 | ||
1404 | /* | |
1405 | * lookup range in layout | |
1406 | */ | |
e5e94017 | 1407 | static struct pnfs_layout_segment * |
fb3296eb | 1408 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
c7d73af2 TH |
1409 | struct pnfs_layout_range *range, |
1410 | bool strict_iomode) | |
e5e94017 | 1411 | { |
b1f69b75 AA |
1412 | struct pnfs_layout_segment *lseg, *ret = NULL; |
1413 | ||
1414 | dprintk("%s:Begin\n", __func__); | |
1415 | ||
b7edfaa1 | 1416 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { |
4541d16c | 1417 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
ce6ab4f2 | 1418 | !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) && |
c7d73af2 TH |
1419 | pnfs_lseg_range_match(&lseg->pls_range, range, |
1420 | strict_iomode)) { | |
9369a431 | 1421 | ret = pnfs_get_lseg(lseg); |
b1f69b75 AA |
1422 | break; |
1423 | } | |
b1f69b75 AA |
1424 | } |
1425 | ||
1426 | dprintk("%s:Return lseg %p ref %d\n", | |
4541d16c | 1427 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
b1f69b75 | 1428 | return ret; |
e5e94017 BH |
1429 | } |
1430 | ||
d23d61c8 AA |
1431 | /* |
1432 | * Use mdsthreshold hints set at each OPEN to determine if I/O should go | |
1433 | * to the MDS or over pNFS | |
1434 | * | |
1435 | * The nfs_inode read_io and write_io fields are cumulative counters reset | |
1436 | * when there are no layout segments. Note that in pnfs_update_layout iomode | |
1437 | * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a | |
1438 | * WRITE request. | |
1439 | * | |
1440 | * A return of true means use MDS I/O. | |
1441 | * | |
1442 | * From rfc 5661: | |
1443 | * If a file's size is smaller than the file size threshold, data accesses | |
1444 | * SHOULD be sent to the metadata server. If an I/O request has a length that | |
1445 | * is below the I/O size threshold, the I/O SHOULD be sent to the metadata | |
1446 | * server. If both file size and I/O size are provided, the client SHOULD | |
1447 | * reach or exceed both thresholds before sending its read or write | |
1448 | * requests to the data server. | |
1449 | */ | |
1450 | static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, | |
1451 | struct inode *ino, int iomode) | |
1452 | { | |
1453 | struct nfs4_threshold *t = ctx->mdsthreshold; | |
1454 | struct nfs_inode *nfsi = NFS_I(ino); | |
1455 | loff_t fsize = i_size_read(ino); | |
1456 | bool size = false, size_set = false, io = false, io_set = false, ret = false; | |
1457 | ||
1458 | if (t == NULL) | |
1459 | return ret; | |
1460 | ||
1461 | dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", | |
1462 | __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); | |
1463 | ||
1464 | switch (iomode) { | |
1465 | case IOMODE_READ: | |
1466 | if (t->bm & THRESHOLD_RD) { | |
1467 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1468 | size_set = true; | |
1469 | if (fsize < t->rd_sz) | |
1470 | size = true; | |
1471 | } | |
1472 | if (t->bm & THRESHOLD_RD_IO) { | |
1473 | dprintk("%s nfsi->read_io %llu\n", __func__, | |
1474 | nfsi->read_io); | |
1475 | io_set = true; | |
1476 | if (nfsi->read_io < t->rd_io_sz) | |
1477 | io = true; | |
1478 | } | |
1479 | break; | |
1480 | case IOMODE_RW: | |
1481 | if (t->bm & THRESHOLD_WR) { | |
1482 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1483 | size_set = true; | |
1484 | if (fsize < t->wr_sz) | |
1485 | size = true; | |
1486 | } | |
1487 | if (t->bm & THRESHOLD_WR_IO) { | |
1488 | dprintk("%s nfsi->write_io %llu\n", __func__, | |
1489 | nfsi->write_io); | |
1490 | io_set = true; | |
1491 | if (nfsi->write_io < t->wr_io_sz) | |
1492 | io = true; | |
1493 | } | |
1494 | break; | |
1495 | } | |
1496 | if (size_set && io_set) { | |
1497 | if (size && io) | |
1498 | ret = true; | |
1499 | } else if (size || io) | |
1500 | ret = true; | |
1501 | ||
1502 | dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); | |
1503 | return ret; | |
1504 | } | |
1505 | ||
aa8a45ee PT |
1506 | static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo) |
1507 | { | |
1508 | /* | |
1509 | * send layoutcommit as it can hold up layoutreturn due to lseg | |
1510 | * reference | |
1511 | */ | |
1512 | pnfs_layoutcommit_inode(lo->plh_inode, false); | |
1513 | return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN, | |
2e5b29f0 | 1514 | nfs_wait_bit_killable, |
aa8a45ee PT |
1515 | TASK_UNINTERRUPTIBLE); |
1516 | } | |
1517 | ||
d67ae825 TH |
1518 | static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo) |
1519 | { | |
1520 | unsigned long *bitlock = &lo->plh_flags; | |
1521 | ||
1522 | clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock); | |
1523 | smp_mb__after_atomic(); | |
1524 | wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET); | |
1525 | } | |
1526 | ||
e5e94017 BH |
1527 | /* |
1528 | * Layout segment is retreived from the server if not cached. | |
1529 | * The appropriate layout segment is referenced and returned to the caller. | |
1530 | */ | |
7c24d948 | 1531 | struct pnfs_layout_segment * |
e5e94017 BH |
1532 | pnfs_update_layout(struct inode *ino, |
1533 | struct nfs_open_context *ctx, | |
fb3296eb BH |
1534 | loff_t pos, |
1535 | u64 count, | |
a75b9df9 | 1536 | enum pnfs_iomode iomode, |
c7d73af2 | 1537 | bool strict_iomode, |
a75b9df9 | 1538 | gfp_t gfp_flags) |
e5e94017 | 1539 | { |
fb3296eb BH |
1540 | struct pnfs_layout_range arg = { |
1541 | .iomode = iomode, | |
1542 | .offset = pos, | |
1543 | .length = count, | |
1544 | }; | |
183d9e7b | 1545 | unsigned pg_offset, seq; |
6382a441 WAA |
1546 | struct nfs_server *server = NFS_SERVER(ino); |
1547 | struct nfs_client *clp = server->nfs_client; | |
183d9e7b | 1548 | struct pnfs_layout_hdr *lo = NULL; |
e5e94017 | 1549 | struct pnfs_layout_segment *lseg = NULL; |
183d9e7b JL |
1550 | nfs4_stateid stateid; |
1551 | long timeout = 0; | |
66b53f32 | 1552 | unsigned long giveup = jiffies + (clp->cl_lease_time << 1); |
30005121 | 1553 | bool first; |
e5e94017 | 1554 | |
9a4bf31d | 1555 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) { |
183d9e7b | 1556 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1557 | PNFS_UPDATE_LAYOUT_NO_PNFS); |
f86bbcf8 | 1558 | goto out; |
9a4bf31d | 1559 | } |
d23d61c8 | 1560 | |
9a4bf31d | 1561 | if (iomode == IOMODE_READ && i_size_read(ino) == 0) { |
183d9e7b | 1562 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1563 | PNFS_UPDATE_LAYOUT_RD_ZEROLEN); |
4ae93560 | 1564 | goto out; |
9a4bf31d | 1565 | } |
4ae93560 | 1566 | |
9a4bf31d | 1567 | if (pnfs_within_mdsthreshold(ctx, ino, iomode)) { |
183d9e7b | 1568 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1569 | PNFS_UPDATE_LAYOUT_MDSTHRESH); |
f86bbcf8 | 1570 | goto out; |
9a4bf31d | 1571 | } |
d23d61c8 | 1572 | |
9bf87482 | 1573 | lookup_again: |
b88fa69e | 1574 | nfs4_client_recover_expired_lease(clp); |
9bf87482 | 1575 | first = false; |
e5e94017 | 1576 | spin_lock(&ino->i_lock); |
9fa40758 | 1577 | lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); |
830ffb56 TM |
1578 | if (lo == NULL) { |
1579 | spin_unlock(&ino->i_lock); | |
183d9e7b | 1580 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1581 | PNFS_UPDATE_LAYOUT_NOMEM); |
830ffb56 TM |
1582 | goto out; |
1583 | } | |
e5e94017 | 1584 | |
43f1b3da | 1585 | /* Do we even need to bother with this? */ |
a59c30ac | 1586 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
183d9e7b | 1587 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1588 | PNFS_UPDATE_LAYOUT_BULK_RECALL); |
43f1b3da | 1589 | dprintk("%s matches recall, use MDS\n", __func__); |
e5e94017 BH |
1590 | goto out_unlock; |
1591 | } | |
1592 | ||
1593 | /* if LAYOUTGET already failed once we don't try again */ | |
2e5b29f0 | 1594 | if (pnfs_layout_io_test_failed(lo, iomode)) { |
183d9e7b | 1595 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1596 | PNFS_UPDATE_LAYOUT_IO_TEST_FAIL); |
e5e94017 | 1597 | goto out_unlock; |
9a4bf31d | 1598 | } |
e5e94017 | 1599 | |
c7d73af2 | 1600 | lseg = pnfs_find_lseg(lo, &arg, strict_iomode); |
183d9e7b JL |
1601 | if (lseg) { |
1602 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1603 | PNFS_UPDATE_LAYOUT_FOUND_CACHED); | |
1604 | goto out_unlock; | |
1605 | } | |
1606 | ||
1607 | if (!nfs4_valid_open_stateid(ctx->state)) { | |
1608 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1609 | PNFS_UPDATE_LAYOUT_INVALID_OPEN); | |
1610 | goto out_unlock; | |
1611 | } | |
1612 | ||
1613 | /* | |
1614 | * Choose a stateid for the LAYOUTGET. If we don't have a layout | |
1615 | * stateid, or it has been invalidated, then we must use the open | |
1616 | * stateid. | |
1617 | */ | |
67a3b721 | 1618 | if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) { |
183d9e7b JL |
1619 | |
1620 | /* | |
1621 | * The first layoutget for the file. Need to serialize per | |
9bf87482 PT |
1622 | * RFC 5661 Errata 3208. |
1623 | */ | |
1624 | if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, | |
1625 | &lo->plh_flags)) { | |
1626 | spin_unlock(&ino->i_lock); | |
1627 | wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET, | |
1628 | TASK_UNINTERRUPTIBLE); | |
1629 | pnfs_put_layout_hdr(lo); | |
183d9e7b | 1630 | dprintk("%s retrying\n", __func__); |
9bf87482 PT |
1631 | goto lookup_again; |
1632 | } | |
183d9e7b JL |
1633 | |
1634 | first = true; | |
1635 | do { | |
1636 | seq = read_seqbegin(&ctx->state->seqlock); | |
1637 | nfs4_stateid_copy(&stateid, &ctx->state->stateid); | |
1638 | } while (read_seqretry(&ctx->state->seqlock, seq)); | |
9bf87482 | 1639 | } else { |
183d9e7b | 1640 | nfs4_stateid_copy(&stateid, &lo->plh_stateid); |
9bf87482 | 1641 | } |
568e8c49 | 1642 | |
aa8a45ee PT |
1643 | /* |
1644 | * Because we free lsegs before sending LAYOUTRETURN, we need to wait | |
1645 | * for LAYOUTRETURN even if first is true. | |
1646 | */ | |
8f70f53a | 1647 | if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { |
aa8a45ee PT |
1648 | spin_unlock(&ino->i_lock); |
1649 | dprintk("%s wait for layoutreturn\n", __func__); | |
1650 | if (pnfs_prepare_to_retry_layoutget(lo)) { | |
d67ae825 TH |
1651 | if (first) |
1652 | pnfs_clear_first_layoutget(lo); | |
aa8a45ee PT |
1653 | pnfs_put_layout_hdr(lo); |
1654 | dprintk("%s retrying\n", __func__); | |
183d9e7b JL |
1655 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, |
1656 | lseg, PNFS_UPDATE_LAYOUT_RETRY); | |
aa8a45ee PT |
1657 | goto lookup_again; |
1658 | } | |
183d9e7b | 1659 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1660 | PNFS_UPDATE_LAYOUT_RETURN); |
aa8a45ee PT |
1661 | goto out_put_layout_hdr; |
1662 | } | |
1663 | ||
9a4bf31d | 1664 | if (pnfs_layoutgets_blocked(lo)) { |
183d9e7b | 1665 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, |
9a4bf31d | 1666 | PNFS_UPDATE_LAYOUT_BLOCKED); |
cf7d63f1 | 1667 | goto out_unlock; |
9a4bf31d | 1668 | } |
cf7d63f1 | 1669 | atomic_inc(&lo->plh_outstanding); |
f49f9baa | 1670 | spin_unlock(&ino->i_lock); |
30005121 | 1671 | |
abb9a007 | 1672 | if (list_empty(&lo->plh_layouts)) { |
2130ff66 FI |
1673 | /* The lo must be on the clp list if there is any |
1674 | * chance of a CB_LAYOUTRECALL(FILE) coming in. | |
1675 | */ | |
1676 | spin_lock(&clp->cl_lock); | |
abb9a007 PT |
1677 | if (list_empty(&lo->plh_layouts)) |
1678 | list_add_tail(&lo->plh_layouts, &server->layouts); | |
2130ff66 FI |
1679 | spin_unlock(&clp->cl_lock); |
1680 | } | |
e5e94017 | 1681 | |
09cbfeaf | 1682 | pg_offset = arg.offset & ~PAGE_MASK; |
707ed5fd BH |
1683 | if (pg_offset) { |
1684 | arg.offset -= pg_offset; | |
1685 | arg.length += pg_offset; | |
1686 | } | |
7c24d948 | 1687 | if (arg.length != NFS4_MAX_UINT64) |
09cbfeaf | 1688 | arg.length = PAGE_ALIGN(arg.length); |
707ed5fd | 1689 | |
183d9e7b JL |
1690 | lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags); |
1691 | trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, | |
1692 | PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET); | |
56b38a1f | 1693 | atomic_dec(&lo->plh_outstanding); |
83026d80 | 1694 | if (IS_ERR(lseg)) { |
183d9e7b | 1695 | switch(PTR_ERR(lseg)) { |
e85d7ee4 | 1696 | case -EBUSY: |
183d9e7b JL |
1697 | if (time_after(jiffies, giveup)) |
1698 | lseg = NULL; | |
66b53f32 TM |
1699 | break; |
1700 | case -ERECALLCONFLICT: | |
1701 | /* Huh? We hold no layouts, how is there a recall? */ | |
1702 | if (first) { | |
1703 | lseg = NULL; | |
1704 | break; | |
1705 | } | |
1706 | /* Destroy the existing layout and start over */ | |
1707 | if (time_after(jiffies, giveup)) | |
1708 | pnfs_destroy_layout(NFS_I(ino)); | |
183d9e7b JL |
1709 | /* Fallthrough */ |
1710 | case -EAGAIN: | |
56b38a1f | 1711 | break; |
183d9e7b JL |
1712 | default: |
1713 | if (!nfs_error_is_fatal(PTR_ERR(lseg))) { | |
1714 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
1715 | lseg = NULL; | |
1716 | } | |
56b38a1f TM |
1717 | goto out_put_layout_hdr; |
1718 | } | |
1719 | if (lseg) { | |
1720 | if (first) | |
1721 | pnfs_clear_first_layoutget(lo); | |
1722 | trace_pnfs_update_layout(ino, pos, count, | |
1723 | iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY); | |
1724 | pnfs_put_layout_hdr(lo); | |
1725 | goto lookup_again; | |
83026d80 JL |
1726 | } |
1727 | } else { | |
1728 | pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); | |
1729 | } | |
1730 | ||
830ffb56 | 1731 | out_put_layout_hdr: |
d67ae825 TH |
1732 | if (first) |
1733 | pnfs_clear_first_layoutget(lo); | |
70c3bd2b | 1734 | pnfs_put_layout_hdr(lo); |
e5e94017 | 1735 | out: |
f86bbcf8 TM |
1736 | dprintk("%s: inode %s/%llu pNFS layout segment %s for " |
1737 | "(%s, offset: %llu, length: %llu)\n", | |
1738 | __func__, ino->i_sb->s_id, | |
1739 | (unsigned long long)NFS_FILEID(ino), | |
d600ad1f | 1740 | IS_ERR_OR_NULL(lseg) ? "not found" : "found", |
f86bbcf8 TM |
1741 | iomode==IOMODE_RW ? "read/write" : "read-only", |
1742 | (unsigned long long)pos, | |
1743 | (unsigned long long)count); | |
e5e94017 BH |
1744 | return lseg; |
1745 | out_unlock: | |
1746 | spin_unlock(&ino->i_lock); | |
830ffb56 | 1747 | goto out_put_layout_hdr; |
e5e94017 | 1748 | } |
7c24d948 | 1749 | EXPORT_SYMBOL_GPL(pnfs_update_layout); |
b1f69b75 | 1750 | |
540d9864 TM |
1751 | static bool |
1752 | pnfs_sanity_check_layout_range(struct pnfs_layout_range *range) | |
1753 | { | |
1754 | switch (range->iomode) { | |
1755 | case IOMODE_READ: | |
1756 | case IOMODE_RW: | |
1757 | break; | |
1758 | default: | |
1759 | return false; | |
1760 | } | |
1761 | if (range->offset == NFS4_MAX_UINT64) | |
1762 | return false; | |
1763 | if (range->length == 0) | |
1764 | return false; | |
1765 | if (range->length != NFS4_MAX_UINT64 && | |
1766 | range->length > NFS4_MAX_UINT64 - range->offset) | |
1767 | return false; | |
1768 | return true; | |
1769 | } | |
1770 | ||
a0b0a6e3 | 1771 | struct pnfs_layout_segment * |
b1f69b75 AA |
1772 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
1773 | { | |
1774 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; | |
1775 | struct nfs4_layoutget_res *res = &lgp->res; | |
1776 | struct pnfs_layout_segment *lseg; | |
b7edfaa1 | 1777 | struct inode *ino = lo->plh_inode; |
78096cca | 1778 | LIST_HEAD(free_me); |
540d9864 TM |
1779 | |
1780 | if (!pnfs_sanity_check_layout_range(&res->range)) | |
1b3c6d07 | 1781 | return ERR_PTR(-EINVAL); |
b1f69b75 AA |
1782 | |
1783 | /* Inject layout blob into I/O device driver */ | |
a75b9df9 | 1784 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags); |
1b3c6d07 | 1785 | if (IS_ERR_OR_NULL(lseg)) { |
b1f69b75 | 1786 | if (!lseg) |
1b3c6d07 JL |
1787 | lseg = ERR_PTR(-ENOMEM); |
1788 | ||
1789 | dprintk("%s: Could not allocate layout: error %ld\n", | |
1790 | __func__, PTR_ERR(lseg)); | |
1791 | return lseg; | |
b1f69b75 AA |
1792 | } |
1793 | ||
119cef97 | 1794 | pnfs_init_lseg(lo, lseg, &res->range, &res->stateid); |
1013df61 | 1795 | |
b1f69b75 | 1796 | spin_lock(&ino->i_lock); |
e1c06f80 | 1797 | if (pnfs_layoutgets_blocked(lo)) { |
43f1b3da | 1798 | dprintk("%s forget reply due to state\n", __func__); |
1b3c6d07 | 1799 | goto out_forget; |
43f1b3da | 1800 | } |
038d6493 | 1801 | |
362f7474 CH |
1802 | if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) { |
1803 | /* existing state ID, make sure the sequence number matches. */ | |
1804 | if (pnfs_layout_stateid_blocked(lo, &res->stateid)) { | |
1805 | dprintk("%s forget reply due to sequence\n", __func__); | |
1b3c6d07 | 1806 | goto out_forget; |
362f7474 CH |
1807 | } |
1808 | pnfs_set_layout_stateid(lo, &res->stateid, false); | |
1809 | } else { | |
1810 | /* | |
1811 | * We got an entirely new state ID. Mark all segments for the | |
1812 | * inode invalid, and don't bother validating the stateid | |
1813 | * sequence number. | |
1814 | */ | |
d9b61708 | 1815 | pnfs_mark_layout_stateid_invalid(lo, &free_me); |
362f7474 | 1816 | |
2a59a041 | 1817 | pnfs_set_layout_stateid(lo, &res->stateid, true); |
362f7474 | 1818 | } |
038d6493 | 1819 | |
9369a431 | 1820 | pnfs_get_lseg(lseg); |
03772d2f | 1821 | pnfs_layout_insert_lseg(lo, lseg, &free_me); |
8e0acf90 | 1822 | |
b1f69b75 | 1823 | |
3976143b | 1824 | if (res->return_on_close) |
f7e8917a | 1825 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); |
f7e8917a | 1826 | |
b1f69b75 | 1827 | spin_unlock(&ino->i_lock); |
78096cca | 1828 | pnfs_free_lseg_list(&free_me); |
a0b0a6e3 | 1829 | return lseg; |
43f1b3da | 1830 | |
1b3c6d07 | 1831 | out_forget: |
43f1b3da FI |
1832 | spin_unlock(&ino->i_lock); |
1833 | lseg->pls_layout = lo; | |
1834 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); | |
1b3c6d07 | 1835 | return ERR_PTR(-EAGAIN); |
b1f69b75 AA |
1836 | } |
1837 | ||
016256df | 1838 | static void |
3982a6a2 JL |
1839 | pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode, |
1840 | u32 seq) | |
5c97f5de | 1841 | { |
2d6cf5ab | 1842 | if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode) |
5c97f5de TM |
1843 | iomode = IOMODE_ANY; |
1844 | lo->plh_return_iomode = iomode; | |
e0fa0d01 | 1845 | set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); |
2d6cf5ab TM |
1846 | if (seq != 0) { |
1847 | WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq); | |
3982a6a2 | 1848 | lo->plh_return_seq = seq; |
2d6cf5ab | 1849 | } |
5c97f5de TM |
1850 | } |
1851 | ||
2f215968 TM |
1852 | /** |
1853 | * pnfs_mark_matching_lsegs_return - Free or return matching layout segments | |
1854 | * @lo: pointer to layout header | |
1855 | * @tmp_list: list header to be used with pnfs_free_lseg_list() | |
1856 | * @return_range: describe layout segment ranges to be returned | |
1857 | * | |
1858 | * This function is mainly intended for use by layoutrecall. It attempts | |
1859 | * to free the layout segment immediately, or else to mark it for return | |
1860 | * as soon as its reference count drops to zero. | |
1861 | */ | |
10335556 | 1862 | int |
016256df PT |
1863 | pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, |
1864 | struct list_head *tmp_list, | |
6d597e17 JL |
1865 | const struct pnfs_layout_range *return_range, |
1866 | u32 seq) | |
016256df PT |
1867 | { |
1868 | struct pnfs_layout_segment *lseg, *next; | |
10335556 | 1869 | int remaining = 0; |
016256df PT |
1870 | |
1871 | dprintk("%s:Begin lo %p\n", __func__, lo); | |
1872 | ||
1873 | if (list_empty(&lo->plh_segs)) | |
10335556 | 1874 | return 0; |
016256df | 1875 | |
fc7ff367 | 1876 | assert_spin_locked(&lo->plh_inode->i_lock); |
016256df PT |
1877 | |
1878 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) | |
e036f464 | 1879 | if (pnfs_match_lseg_recall(lseg, return_range, seq)) { |
016256df PT |
1880 | dprintk("%s: marking lseg %p iomode %d " |
1881 | "offset %llu length %llu\n", __func__, | |
1882 | lseg, lseg->pls_range.iomode, | |
1883 | lseg->pls_range.offset, | |
1884 | lseg->pls_range.length); | |
2f215968 TM |
1885 | if (mark_lseg_invalid(lseg, tmp_list)) |
1886 | continue; | |
1887 | remaining++; | |
016256df | 1888 | set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); |
016256df | 1889 | } |
6d597e17 JL |
1890 | |
1891 | if (remaining) | |
1892 | pnfs_set_plh_return_info(lo, return_range->iomode, seq); | |
1893 | ||
10335556 | 1894 | return remaining; |
016256df PT |
1895 | } |
1896 | ||
1897 | void pnfs_error_mark_layout_for_return(struct inode *inode, | |
1898 | struct pnfs_layout_segment *lseg) | |
1899 | { | |
1900 | struct pnfs_layout_hdr *lo = NFS_I(inode)->layout; | |
016256df PT |
1901 | struct pnfs_layout_range range = { |
1902 | .iomode = lseg->pls_range.iomode, | |
1903 | .offset = 0, | |
1904 | .length = NFS4_MAX_UINT64, | |
1905 | }; | |
1906 | LIST_HEAD(free_me); | |
10335556 | 1907 | bool return_now = false; |
016256df PT |
1908 | |
1909 | spin_lock(&inode->i_lock); | |
2d6cf5ab | 1910 | pnfs_set_plh_return_info(lo, range.iomode, 0); |
016256df PT |
1911 | /* |
1912 | * mark all matching lsegs so that we are sure to have no live | |
1913 | * segments at hand when sending layoutreturn. See pnfs_put_lseg() | |
1914 | * for how it works. | |
1915 | */ | |
2d6cf5ab | 1916 | if (!pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0)) { |
10335556 | 1917 | nfs4_stateid stateid; |
e5fd1904 | 1918 | enum pnfs_iomode iomode; |
10335556 | 1919 | |
e5fd1904 | 1920 | return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode); |
10335556 TM |
1921 | spin_unlock(&inode->i_lock); |
1922 | if (return_now) | |
1923 | pnfs_send_layoutreturn(lo, &stateid, iomode, false); | |
1924 | } else { | |
1925 | spin_unlock(&inode->i_lock); | |
1926 | nfs_commit_inode(inode, 0); | |
1927 | } | |
016256df PT |
1928 | pnfs_free_lseg_list(&free_me); |
1929 | } | |
1930 | EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return); | |
1931 | ||
d8007d4d TM |
1932 | void |
1933 | pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1934 | { | |
1fd937bd PT |
1935 | u64 rd_size = req->wb_bytes; |
1936 | ||
cb5d04bc PT |
1937 | if (pgio->pg_lseg == NULL) { |
1938 | if (pgio->pg_dreq == NULL) | |
1939 | rd_size = i_size_read(pgio->pg_inode) - req_offset(req); | |
1940 | else | |
1941 | rd_size = nfs_dreq_bytes_left(pgio->pg_dreq); | |
1942 | ||
1943 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, | |
1944 | req->wb_context, | |
1945 | req_offset(req), | |
1946 | rd_size, | |
1947 | IOMODE_READ, | |
c7d73af2 | 1948 | false, |
cb5d04bc | 1949 | GFP_KERNEL); |
d600ad1f PT |
1950 | if (IS_ERR(pgio->pg_lseg)) { |
1951 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); | |
1952 | pgio->pg_lseg = NULL; | |
1953 | return; | |
1954 | } | |
cb5d04bc | 1955 | } |
e885de1a TM |
1956 | /* If no lseg, fall back to read through mds */ |
1957 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1958 | nfs_pageio_reset_read_mds(pgio); |
e885de1a | 1959 | |
d8007d4d TM |
1960 | } |
1961 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); | |
1962 | ||
1963 | void | |
6296556f PT |
1964 | pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, |
1965 | struct nfs_page *req, u64 wb_size) | |
d8007d4d | 1966 | { |
d600ad1f | 1967 | if (pgio->pg_lseg == NULL) { |
cb5d04bc PT |
1968 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
1969 | req->wb_context, | |
1970 | req_offset(req), | |
1971 | wb_size, | |
1972 | IOMODE_RW, | |
c7d73af2 | 1973 | false, |
cb5d04bc | 1974 | GFP_NOFS); |
d600ad1f PT |
1975 | if (IS_ERR(pgio->pg_lseg)) { |
1976 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); | |
1977 | pgio->pg_lseg = NULL; | |
1978 | return; | |
1979 | } | |
1980 | } | |
e885de1a TM |
1981 | /* If no lseg, fall back to write through mds */ |
1982 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1983 | nfs_pageio_reset_write_mds(pgio); |
d8007d4d TM |
1984 | } |
1985 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); | |
1986 | ||
180bb5ec WAA |
1987 | void |
1988 | pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc) | |
1989 | { | |
1990 | if (desc->pg_lseg) { | |
1991 | pnfs_put_lseg(desc->pg_lseg); | |
1992 | desc->pg_lseg = NULL; | |
1993 | } | |
1994 | } | |
1995 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup); | |
1996 | ||
b4fdac1a WAA |
1997 | /* |
1998 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number | |
1999 | * of bytes (maximum @req->wb_bytes) that can be coalesced. | |
2000 | */ | |
2001 | size_t | |
a7d42ddb WAA |
2002 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, |
2003 | struct nfs_page *prev, struct nfs_page *req) | |
94ad1c80 | 2004 | { |
0f9c429e | 2005 | unsigned int size; |
c5e20cb7 | 2006 | u64 seg_end, req_start, seg_left; |
0f9c429e WAA |
2007 | |
2008 | size = nfs_generic_pg_test(pgio, prev, req); | |
0f9c429e WAA |
2009 | if (!size) |
2010 | return 0; | |
94ad1c80 | 2011 | |
19982ba8 | 2012 | /* |
c5e20cb7 WAA |
2013 | * 'size' contains the number of bytes left in the current page (up |
2014 | * to the original size asked for in @req->wb_bytes). | |
2015 | * | |
2016 | * Calculate how many bytes are left in the layout segment | |
2017 | * and if there are less bytes than 'size', return that instead. | |
19982ba8 TM |
2018 | * |
2019 | * Please also note that 'end_offset' is actually the offset of the | |
2020 | * first byte that lies outside the pnfs_layout_range. FIXME? | |
2021 | * | |
2022 | */ | |
19b54848 | 2023 | if (pgio->pg_lseg) { |
c5e20cb7 WAA |
2024 | seg_end = end_offset(pgio->pg_lseg->pls_range.offset, |
2025 | pgio->pg_lseg->pls_range.length); | |
2026 | req_start = req_offset(req); | |
7c13789e | 2027 | WARN_ON_ONCE(req_start >= seg_end); |
c5e20cb7 | 2028 | /* start of request is past the last byte of this segment */ |
7c13789e WAA |
2029 | if (req_start >= seg_end) { |
2030 | /* reference the new lseg */ | |
2031 | if (pgio->pg_ops->pg_cleanup) | |
2032 | pgio->pg_ops->pg_cleanup(pgio); | |
2033 | if (pgio->pg_ops->pg_init) | |
2034 | pgio->pg_ops->pg_init(pgio, req); | |
19b54848 | 2035 | return 0; |
7c13789e | 2036 | } |
c5e20cb7 WAA |
2037 | |
2038 | /* adjust 'size' iff there are fewer bytes left in the | |
2039 | * segment than what nfs_generic_pg_test returned */ | |
2040 | seg_left = seg_end - req_start; | |
2041 | if (seg_left < size) | |
2042 | size = (unsigned int)seg_left; | |
19b54848 | 2043 | } |
0f9c429e | 2044 | |
19b54848 | 2045 | return size; |
94ad1c80 | 2046 | } |
89a58e32 | 2047 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
94ad1c80 | 2048 | |
53113ad3 | 2049 | int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr) |
e2fecb21 TM |
2050 | { |
2051 | struct nfs_pageio_descriptor pgio; | |
e2fecb21 TM |
2052 | |
2053 | /* Resend all requests through the MDS */ | |
53113ad3 WAA |
2054 | nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true, |
2055 | hdr->completion_ops); | |
c7070113 | 2056 | set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags); |
53113ad3 | 2057 | return nfs_pageio_resend(&pgio, hdr); |
e2fecb21 | 2058 | } |
e7dd79af | 2059 | EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); |
e2fecb21 | 2060 | |
d45f60c6 | 2061 | static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr) |
1acbbb4e | 2062 | { |
cd841605 FI |
2063 | |
2064 | dprintk("pnfs write error = %d\n", hdr->pnfs_error); | |
2065 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 2066 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 | 2067 | pnfs_return_layout(hdr->inode); |
1acbbb4e | 2068 | } |
6c75dc0d | 2069 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
53113ad3 | 2070 | hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr); |
1acbbb4e FI |
2071 | } |
2072 | ||
d20581aa BH |
2073 | /* |
2074 | * Called by non rpc-based layout drivers | |
2075 | */ | |
d45f60c6 | 2076 | void pnfs_ld_write_done(struct nfs_pgio_header *hdr) |
44b83799 | 2077 | { |
f8417b48 | 2078 | if (likely(!hdr->pnfs_error)) { |
67af7611 TM |
2079 | pnfs_set_layoutcommit(hdr->inode, hdr->lseg, |
2080 | hdr->mds_offset + hdr->res.count); | |
d45f60c6 | 2081 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); |
f8417b48 KM |
2082 | } |
2083 | trace_nfs4_pnfs_write(hdr, hdr->pnfs_error); | |
2084 | if (unlikely(hdr->pnfs_error)) | |
d45f60c6 WAA |
2085 | pnfs_ld_handle_write_error(hdr); |
2086 | hdr->mds_ops->rpc_release(hdr); | |
44b83799 | 2087 | } |
d20581aa | 2088 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
44b83799 | 2089 | |
dce81290 TM |
2090 | static void |
2091 | pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, | |
d45f60c6 | 2092 | struct nfs_pgio_header *hdr) |
dce81290 | 2093 | { |
48d635f1 | 2094 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
a7d42ddb | 2095 | |
6c75dc0d | 2096 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
a7d42ddb | 2097 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
6c75dc0d | 2098 | nfs_pageio_reset_write_mds(desc); |
a7d42ddb | 2099 | mirror->pg_recoalesce = 1; |
6c75dc0d | 2100 | } |
d45f60c6 | 2101 | nfs_pgio_data_destroy(hdr); |
1ca018d2 | 2102 | hdr->release(hdr); |
dce81290 TM |
2103 | } |
2104 | ||
2105 | static enum pnfs_try_status | |
d45f60c6 | 2106 | pnfs_try_to_write_data(struct nfs_pgio_header *hdr, |
dce81290 TM |
2107 | const struct rpc_call_ops *call_ops, |
2108 | struct pnfs_layout_segment *lseg, | |
2109 | int how) | |
0382b744 | 2110 | { |
cd841605 | 2111 | struct inode *inode = hdr->inode; |
0382b744 AA |
2112 | enum pnfs_try_status trypnfs; |
2113 | struct nfs_server *nfss = NFS_SERVER(inode); | |
2114 | ||
cd841605 | 2115 | hdr->mds_ops = call_ops; |
0382b744 AA |
2116 | |
2117 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, | |
d45f60c6 WAA |
2118 | inode->i_ino, hdr->args.count, hdr->args.offset, how); |
2119 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); | |
6c75dc0d | 2120 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
0382b744 | 2121 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
0382b744 AA |
2122 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
2123 | return trypnfs; | |
2124 | } | |
2125 | ||
dce81290 | 2126 | static void |
7f714720 WAA |
2127 | pnfs_do_write(struct nfs_pageio_descriptor *desc, |
2128 | struct nfs_pgio_header *hdr, int how) | |
dce81290 | 2129 | { |
dce81290 TM |
2130 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
2131 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
7f714720 | 2132 | enum pnfs_try_status trypnfs; |
dce81290 | 2133 | |
d45f60c6 | 2134 | trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); |
7f714720 | 2135 | if (trypnfs == PNFS_NOT_ATTEMPTED) |
d45f60c6 | 2136 | pnfs_write_through_mds(desc, hdr); |
dce81290 TM |
2137 | } |
2138 | ||
6c75dc0d FI |
2139 | static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) |
2140 | { | |
9369a431 | 2141 | pnfs_put_lseg(hdr->lseg); |
1e7f3a48 | 2142 | nfs_pgio_header_free(hdr); |
6c75dc0d FI |
2143 | } |
2144 | ||
dce81290 TM |
2145 | int |
2146 | pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) | |
2147 | { | |
6c75dc0d | 2148 | struct nfs_pgio_header *hdr; |
dce81290 TM |
2149 | int ret; |
2150 | ||
1e7f3a48 WAA |
2151 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
2152 | if (!hdr) { | |
2bff2288 PT |
2153 | desc->pg_error = -ENOMEM; |
2154 | return desc->pg_error; | |
dce81290 | 2155 | } |
6c75dc0d | 2156 | nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); |
180bb5ec | 2157 | |
9369a431 | 2158 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
ef2c488c | 2159 | ret = nfs_generic_pgio(desc, hdr); |
180bb5ec | 2160 | if (!ret) |
7f714720 | 2161 | pnfs_do_write(desc, hdr, desc->pg_ioflags); |
a7d42ddb | 2162 | |
6c75dc0d | 2163 | return ret; |
dce81290 TM |
2164 | } |
2165 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); | |
2166 | ||
53113ad3 | 2167 | int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr) |
62e4a769 TM |
2168 | { |
2169 | struct nfs_pageio_descriptor pgio; | |
2170 | ||
1acbbb4e | 2171 | /* Resend all requests through the MDS */ |
53113ad3 WAA |
2172 | nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops); |
2173 | return nfs_pageio_resend(&pgio, hdr); | |
1acbbb4e | 2174 | } |
e7dd79af | 2175 | EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); |
1acbbb4e | 2176 | |
d45f60c6 | 2177 | static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr) |
1acbbb4e | 2178 | { |
cd841605 FI |
2179 | dprintk("pnfs read error = %d\n", hdr->pnfs_error); |
2180 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 2181 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 | 2182 | pnfs_return_layout(hdr->inode); |
1acbbb4e | 2183 | } |
4db6e0b7 | 2184 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
53113ad3 | 2185 | hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr); |
62e4a769 TM |
2186 | } |
2187 | ||
d20581aa BH |
2188 | /* |
2189 | * Called by non rpc-based layout drivers | |
2190 | */ | |
d45f60c6 | 2191 | void pnfs_ld_read_done(struct nfs_pgio_header *hdr) |
d20581aa | 2192 | { |
cd841605 | 2193 | if (likely(!hdr->pnfs_error)) { |
d45f60c6 WAA |
2194 | __nfs4_read_done_cb(hdr); |
2195 | hdr->mds_ops->rpc_call_done(&hdr->task, hdr); | |
f8417b48 KM |
2196 | } |
2197 | trace_nfs4_pnfs_read(hdr, hdr->pnfs_error); | |
2198 | if (unlikely(hdr->pnfs_error)) | |
d45f60c6 WAA |
2199 | pnfs_ld_handle_read_error(hdr); |
2200 | hdr->mds_ops->rpc_release(hdr); | |
d20581aa BH |
2201 | } |
2202 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); | |
2203 | ||
493292dd TM |
2204 | static void |
2205 | pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, | |
d45f60c6 | 2206 | struct nfs_pgio_header *hdr) |
493292dd | 2207 | { |
48d635f1 | 2208 | struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); |
a7d42ddb | 2209 | |
4db6e0b7 | 2210 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
a7d42ddb | 2211 | list_splice_tail_init(&hdr->pages, &mirror->pg_list); |
4db6e0b7 | 2212 | nfs_pageio_reset_read_mds(desc); |
a7d42ddb | 2213 | mirror->pg_recoalesce = 1; |
4db6e0b7 | 2214 | } |
d45f60c6 | 2215 | nfs_pgio_data_destroy(hdr); |
1ca018d2 | 2216 | hdr->release(hdr); |
493292dd TM |
2217 | } |
2218 | ||
64419a9b AA |
2219 | /* |
2220 | * Call the appropriate parallel I/O subsystem read function. | |
2221 | */ | |
493292dd | 2222 | static enum pnfs_try_status |
d45f60c6 | 2223 | pnfs_try_to_read_data(struct nfs_pgio_header *hdr, |
493292dd TM |
2224 | const struct rpc_call_ops *call_ops, |
2225 | struct pnfs_layout_segment *lseg) | |
64419a9b | 2226 | { |
cd841605 | 2227 | struct inode *inode = hdr->inode; |
64419a9b AA |
2228 | struct nfs_server *nfss = NFS_SERVER(inode); |
2229 | enum pnfs_try_status trypnfs; | |
2230 | ||
cd841605 | 2231 | hdr->mds_ops = call_ops; |
64419a9b AA |
2232 | |
2233 | dprintk("%s: Reading ino:%lu %u@%llu\n", | |
d45f60c6 | 2234 | __func__, inode->i_ino, hdr->args.count, hdr->args.offset); |
64419a9b | 2235 | |
d45f60c6 | 2236 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); |
4db6e0b7 | 2237 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
64419a9b | 2238 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
64419a9b AA |
2239 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
2240 | return trypnfs; | |
2241 | } | |
863a3c6c | 2242 | |
ceb11e13 | 2243 | /* Resend all requests through pnfs. */ |
1b1bc66b | 2244 | void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr) |
ceb11e13 PT |
2245 | { |
2246 | struct nfs_pageio_descriptor pgio; | |
2247 | ||
1b1bc66b WAA |
2248 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
2249 | nfs_pageio_init_read(&pgio, hdr->inode, false, | |
2250 | hdr->completion_ops); | |
2251 | hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr); | |
2252 | } | |
ceb11e13 PT |
2253 | } |
2254 | EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs); | |
2255 | ||
493292dd | 2256 | static void |
7f714720 | 2257 | pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr) |
493292dd | 2258 | { |
493292dd TM |
2259 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; |
2260 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
7f714720 | 2261 | enum pnfs_try_status trypnfs; |
493292dd | 2262 | |
d45f60c6 | 2263 | trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); |
ceb11e13 | 2264 | if (trypnfs == PNFS_TRY_AGAIN) |
1b1bc66b WAA |
2265 | pnfs_read_resend_pnfs(hdr); |
2266 | if (trypnfs == PNFS_NOT_ATTEMPTED || hdr->task.tk_status) | |
d45f60c6 | 2267 | pnfs_read_through_mds(desc, hdr); |
493292dd TM |
2268 | } |
2269 | ||
4db6e0b7 FI |
2270 | static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) |
2271 | { | |
9369a431 | 2272 | pnfs_put_lseg(hdr->lseg); |
1e7f3a48 | 2273 | nfs_pgio_header_free(hdr); |
4db6e0b7 FI |
2274 | } |
2275 | ||
493292dd TM |
2276 | int |
2277 | pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) | |
2278 | { | |
4db6e0b7 | 2279 | struct nfs_pgio_header *hdr; |
493292dd TM |
2280 | int ret; |
2281 | ||
1e7f3a48 WAA |
2282 | hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); |
2283 | if (!hdr) { | |
2bff2288 PT |
2284 | desc->pg_error = -ENOMEM; |
2285 | return desc->pg_error; | |
493292dd | 2286 | } |
4db6e0b7 | 2287 | nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); |
9369a431 | 2288 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
ef2c488c | 2289 | ret = nfs_generic_pgio(desc, hdr); |
180bb5ec | 2290 | if (!ret) |
7f714720 | 2291 | pnfs_do_read(desc, hdr); |
4db6e0b7 | 2292 | return ret; |
493292dd TM |
2293 | } |
2294 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); | |
2295 | ||
71244d9b TM |
2296 | static void pnfs_clear_layoutcommitting(struct inode *inode) |
2297 | { | |
2298 | unsigned long *bitlock = &NFS_I(inode)->flags; | |
2299 | ||
2300 | clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock); | |
4e857c58 | 2301 | smp_mb__after_atomic(); |
71244d9b TM |
2302 | wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING); |
2303 | } | |
2304 | ||
863a3c6c | 2305 | /* |
a9bae566 | 2306 | * There can be multiple RW segments. |
863a3c6c | 2307 | */ |
a9bae566 | 2308 | static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp) |
863a3c6c | 2309 | { |
a9bae566 | 2310 | struct pnfs_layout_segment *lseg; |
863a3c6c | 2311 | |
a9bae566 PT |
2312 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { |
2313 | if (lseg->pls_range.iomode == IOMODE_RW && | |
a073dbff | 2314 | test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) |
a9bae566 PT |
2315 | list_add(&lseg->pls_lc_list, listp); |
2316 | } | |
863a3c6c AA |
2317 | } |
2318 | ||
a073dbff TM |
2319 | static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp) |
2320 | { | |
2321 | struct pnfs_layout_segment *lseg, *tmp; | |
a073dbff TM |
2322 | |
2323 | /* Matched by references in pnfs_set_layoutcommit */ | |
2324 | list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) { | |
2325 | list_del_init(&lseg->pls_lc_list); | |
2326 | pnfs_put_lseg(lseg); | |
2327 | } | |
2328 | ||
71244d9b | 2329 | pnfs_clear_layoutcommitting(inode); |
a073dbff TM |
2330 | } |
2331 | ||
1b0ae068 PT |
2332 | void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) |
2333 | { | |
b9e028fd | 2334 | pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); |
1b0ae068 PT |
2335 | } |
2336 | EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); | |
2337 | ||
863a3c6c | 2338 | void |
67af7611 TM |
2339 | pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg, |
2340 | loff_t end_pos) | |
863a3c6c | 2341 | { |
cd841605 | 2342 | struct nfs_inode *nfsi = NFS_I(inode); |
79a48a1f | 2343 | bool mark_as_dirty = false; |
863a3c6c | 2344 | |
cd841605 | 2345 | spin_lock(&inode->i_lock); |
863a3c6c | 2346 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
29559b11 | 2347 | nfsi->layout->plh_lwb = end_pos; |
79a48a1f | 2348 | mark_as_dirty = true; |
863a3c6c | 2349 | dprintk("%s: Set layoutcommit for inode %lu ", |
cd841605 | 2350 | __func__, inode->i_ino); |
29559b11 TM |
2351 | } else if (end_pos > nfsi->layout->plh_lwb) |
2352 | nfsi->layout->plh_lwb = end_pos; | |
67af7611 | 2353 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) { |
a9bae566 | 2354 | /* references matched in nfs4_layoutcommit_release */ |
67af7611 | 2355 | pnfs_get_lseg(lseg); |
a9bae566 | 2356 | } |
cd841605 | 2357 | spin_unlock(&inode->i_lock); |
acff5880 | 2358 | dprintk("%s: lseg %p end_pos %llu\n", |
67af7611 | 2359 | __func__, lseg, nfsi->layout->plh_lwb); |
79a48a1f WAA |
2360 | |
2361 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one | |
2362 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ | |
2363 | if (mark_as_dirty) | |
cd841605 | 2364 | mark_inode_dirty_sync(inode); |
863a3c6c AA |
2365 | } |
2366 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); | |
2367 | ||
db29c089 AA |
2368 | void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) |
2369 | { | |
2370 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); | |
2371 | ||
2372 | if (nfss->pnfs_curr_ld->cleanup_layoutcommit) | |
2373 | nfss->pnfs_curr_ld->cleanup_layoutcommit(data); | |
a073dbff | 2374 | pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list); |
db29c089 AA |
2375 | } |
2376 | ||
de4b15c7 AA |
2377 | /* |
2378 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and | |
2379 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough | |
2380 | * data to disk to allow the server to recover the data if it crashes. | |
2381 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag | |
2382 | * is off, and a COMMIT is sent to a data server, or | |
2383 | * if WRITEs to a data server return NFS_DATA_SYNC. | |
2384 | */ | |
863a3c6c | 2385 | int |
ef311537 | 2386 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
863a3c6c | 2387 | { |
5f919c9f | 2388 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; |
863a3c6c AA |
2389 | struct nfs4_layoutcommit_data *data; |
2390 | struct nfs_inode *nfsi = NFS_I(inode); | |
863a3c6c | 2391 | loff_t end_pos; |
71244d9b | 2392 | int status; |
863a3c6c | 2393 | |
71244d9b | 2394 | if (!pnfs_layoutcommit_outstanding(inode)) |
de4b15c7 AA |
2395 | return 0; |
2396 | ||
71244d9b | 2397 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); |
92407e75 | 2398 | |
71244d9b | 2399 | status = -EAGAIN; |
92407e75 | 2400 | if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { |
71244d9b TM |
2401 | if (!sync) |
2402 | goto out; | |
74316201 | 2403 | status = wait_on_bit_lock_action(&nfsi->flags, |
71244d9b TM |
2404 | NFS_INO_LAYOUTCOMMITTING, |
2405 | nfs_wait_bit_killable, | |
2406 | TASK_KILLABLE); | |
92407e75 | 2407 | if (status) |
71244d9b | 2408 | goto out; |
92407e75 PT |
2409 | } |
2410 | ||
71244d9b TM |
2411 | status = -ENOMEM; |
2412 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ | |
2413 | data = kzalloc(sizeof(*data), GFP_NOFS); | |
2414 | if (!data) | |
2415 | goto clear_layoutcommitting; | |
2416 | ||
2417 | status = 0; | |
de4b15c7 | 2418 | spin_lock(&inode->i_lock); |
71244d9b TM |
2419 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
2420 | goto out_unlock; | |
a9bae566 | 2421 | |
71244d9b | 2422 | INIT_LIST_HEAD(&data->lseg_list); |
a9bae566 | 2423 | pnfs_list_write_lseg(inode, &data->lseg_list); |
863a3c6c | 2424 | |
acff5880 | 2425 | end_pos = nfsi->layout->plh_lwb; |
863a3c6c | 2426 | |
f597c537 | 2427 | nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); |
863a3c6c AA |
2428 | spin_unlock(&inode->i_lock); |
2429 | ||
2430 | data->args.inode = inode; | |
9fa40758 | 2431 | data->cred = get_rpccred(nfsi->layout->plh_lc_cred); |
863a3c6c AA |
2432 | nfs_fattr_init(&data->fattr); |
2433 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; | |
2434 | data->res.fattr = &data->fattr; | |
2e18d4d8 TM |
2435 | if (end_pos != 0) |
2436 | data->args.lastbytewritten = end_pos - 1; | |
2437 | else | |
2438 | data->args.lastbytewritten = U64_MAX; | |
863a3c6c AA |
2439 | data->res.server = NFS_SERVER(inode); |
2440 | ||
5f919c9f CH |
2441 | if (ld->prepare_layoutcommit) { |
2442 | status = ld->prepare_layoutcommit(&data->args); | |
2443 | if (status) { | |
3471648a | 2444 | put_rpccred(data->cred); |
5f919c9f | 2445 | spin_lock(&inode->i_lock); |
29559b11 TM |
2446 | set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags); |
2447 | if (end_pos > nfsi->layout->plh_lwb) | |
5f919c9f | 2448 | nfsi->layout->plh_lwb = end_pos; |
3471648a | 2449 | goto out_unlock; |
5f919c9f CH |
2450 | } |
2451 | } | |
2452 | ||
2453 | ||
863a3c6c AA |
2454 | status = nfs4_proc_layoutcommit(data, sync); |
2455 | out: | |
92407e75 PT |
2456 | if (status) |
2457 | mark_inode_dirty_sync(inode); | |
863a3c6c AA |
2458 | dprintk("<-- %s status %d\n", __func__, status); |
2459 | return status; | |
71244d9b TM |
2460 | out_unlock: |
2461 | spin_unlock(&inode->i_lock); | |
92407e75 | 2462 | kfree(data); |
71244d9b TM |
2463 | clear_layoutcommitting: |
2464 | pnfs_clear_layoutcommitting(inode); | |
92407e75 | 2465 | goto out; |
863a3c6c | 2466 | } |
72cff449 | 2467 | EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode); |
82be417a | 2468 | |
5bb89b47 TM |
2469 | int |
2470 | pnfs_generic_sync(struct inode *inode, bool datasync) | |
2471 | { | |
2472 | return pnfs_layoutcommit_inode(inode, true); | |
2473 | } | |
2474 | EXPORT_SYMBOL_GPL(pnfs_generic_sync); | |
2475 | ||
82be417a AA |
2476 | struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) |
2477 | { | |
2478 | struct nfs4_threshold *thp; | |
2479 | ||
2480 | thp = kzalloc(sizeof(*thp), GFP_NOFS); | |
2481 | if (!thp) { | |
2482 | dprintk("%s mdsthreshold allocation failed\n", __func__); | |
2483 | return NULL; | |
2484 | } | |
2485 | return thp; | |
2486 | } | |
8733408d | 2487 | |
865a7ecb | 2488 | #if IS_ENABLED(CONFIG_NFS_V4_2) |
8733408d | 2489 | int |
c8ad8894 | 2490 | pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags) |
8733408d PT |
2491 | { |
2492 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; | |
2493 | struct nfs_server *server = NFS_SERVER(inode); | |
1bfe3b25 | 2494 | struct nfs_inode *nfsi = NFS_I(inode); |
8733408d PT |
2495 | struct nfs42_layoutstat_data *data; |
2496 | struct pnfs_layout_hdr *hdr; | |
2497 | int status = 0; | |
2498 | ||
2499 | if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats) | |
2500 | goto out; | |
2501 | ||
6c5a0d89 TM |
2502 | if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS)) |
2503 | goto out; | |
2504 | ||
1bfe3b25 PT |
2505 | if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags)) |
2506 | goto out; | |
2507 | ||
8733408d PT |
2508 | spin_lock(&inode->i_lock); |
2509 | if (!NFS_I(inode)->layout) { | |
2510 | spin_unlock(&inode->i_lock); | |
f538d0ba | 2511 | goto out_clear_layoutstats; |
8733408d PT |
2512 | } |
2513 | hdr = NFS_I(inode)->layout; | |
2514 | pnfs_get_layout_hdr(hdr); | |
2515 | spin_unlock(&inode->i_lock); | |
2516 | ||
c8ad8894 | 2517 | data = kzalloc(sizeof(*data), gfp_flags); |
8733408d PT |
2518 | if (!data) { |
2519 | status = -ENOMEM; | |
2520 | goto out_put; | |
2521 | } | |
2522 | ||
2523 | data->args.fh = NFS_FH(inode); | |
2524 | data->args.inode = inode; | |
8733408d PT |
2525 | status = ld->prepare_layoutstats(&data->args); |
2526 | if (status) | |
2527 | goto out_free; | |
2528 | ||
2529 | status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data); | |
2530 | ||
2531 | out: | |
2532 | dprintk("%s returns %d\n", __func__, status); | |
2533 | return status; | |
2534 | ||
2535 | out_free: | |
2536 | kfree(data); | |
2537 | out_put: | |
2538 | pnfs_put_layout_hdr(hdr); | |
f538d0ba | 2539 | out_clear_layoutstats: |
1bfe3b25 PT |
2540 | smp_mb__before_atomic(); |
2541 | clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags); | |
2542 | smp_mb__after_atomic(); | |
8733408d PT |
2543 | goto out; |
2544 | } | |
2545 | EXPORT_SYMBOL_GPL(pnfs_report_layoutstat); | |
865a7ecb | 2546 | #endif |
bbf58bf3 TM |
2547 | |
2548 | unsigned int layoutstats_timer; | |
2549 | module_param(layoutstats_timer, uint, 0644); | |
2550 | EXPORT_SYMBOL_GPL(layoutstats_timer); |