Commit | Line | Data |
---|---|---|
155e7524 FI |
1 | /* |
2 | * linux/fs/nfs/blocklayout/blocklayout.c | |
3 | * | |
4 | * Module for the NFSv4.1 pNFS block layout driver. | |
5 | * | |
6 | * Copyright (c) 2006 The Regents of the University of Michigan. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Andy Adamson <andros@citi.umich.edu> | |
10 | * Fred Isaman <iisaman@umich.edu> | |
11 | * | |
12 | * permission is granted to use, copy, create derivative works and | |
13 | * redistribute this software and such derivative works for any purpose, | |
14 | * so long as the name of the university of michigan is not used in | |
15 | * any advertising or publicity pertaining to the use or distribution | |
16 | * of this software without specific, written prior authorization. if | |
17 | * the above copyright notice or any other identification of the | |
18 | * university of michigan is included in any copy of any portion of | |
19 | * this software, then the disclaimer below must also be included. | |
20 | * | |
21 | * this software is provided as is, without representation from the | |
22 | * university of michigan as to its fitness for any purpose, and without | |
23 | * warranty by the university of michigan of any kind, either express | |
24 | * or implied, including without limitation the implied warranties of | |
25 | * merchantability and fitness for a particular purpose. the regents | |
26 | * of the university of michigan shall not be liable for any damages, | |
27 | * including special, indirect, incidental, or consequential damages, | |
28 | * with respect to any claim arising out or in connection with the use | |
29 | * of the software, even if it has been or is hereafter advised of the | |
30 | * possibility of such damages. | |
31 | */ | |
9549ec01 | 32 | |
155e7524 FI |
33 | #include <linux/module.h> |
34 | #include <linux/init.h> | |
fe0a9b74 JR |
35 | #include <linux/mount.h> |
36 | #include <linux/namei.h> | |
9549ec01 | 37 | #include <linux/bio.h> /* struct bio */ |
71cdd40f | 38 | #include <linux/buffer_head.h> /* various write calls */ |
88c9e421 | 39 | #include <linux/prefetch.h> |
6296556f | 40 | #include <linux/pagevec.h> |
155e7524 | 41 | |
10bd295a | 42 | #include "../pnfs.h" |
76e697ba | 43 | #include "../nfs4session.h" |
10bd295a | 44 | #include "../internal.h" |
155e7524 FI |
45 | #include "blocklayout.h" |
46 | ||
47 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD | |
48 | ||
49 | MODULE_LICENSE("GPL"); | |
50 | MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>"); | |
51 | MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver"); | |
52 | ||
9549ec01 FI |
53 | static void print_page(struct page *page) |
54 | { | |
55 | dprintk("PRINTPAGE page %p\n", page); | |
56 | dprintk(" PagePrivate %d\n", PagePrivate(page)); | |
57 | dprintk(" PageUptodate %d\n", PageUptodate(page)); | |
58 | dprintk(" PageError %d\n", PageError(page)); | |
59 | dprintk(" PageDirty %d\n", PageDirty(page)); | |
60 | dprintk(" PageReferenced %d\n", PageReferenced(page)); | |
61 | dprintk(" PageLocked %d\n", PageLocked(page)); | |
62 | dprintk(" PageWriteback %d\n", PageWriteback(page)); | |
63 | dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page)); | |
64 | dprintk("\n"); | |
65 | } | |
66 | ||
67 | /* Given the be associated with isect, determine if page data needs to be | |
68 | * initialized. | |
69 | */ | |
70 | static int is_hole(struct pnfs_block_extent *be, sector_t isect) | |
71 | { | |
72 | if (be->be_state == PNFS_BLOCK_NONE_DATA) | |
73 | return 1; | |
74 | else if (be->be_state != PNFS_BLOCK_INVALID_DATA) | |
75 | return 0; | |
76 | else | |
77 | return !bl_is_sector_init(be->be_inval, isect); | |
78 | } | |
79 | ||
650e2d39 FI |
80 | /* Given the be associated with isect, determine if page data can be |
81 | * written to disk. | |
82 | */ | |
83 | static int is_writable(struct pnfs_block_extent *be, sector_t isect) | |
84 | { | |
71cdd40f PT |
85 | return (be->be_state == PNFS_BLOCK_READWRITE_DATA || |
86 | be->be_state == PNFS_BLOCK_INVALID_DATA); | |
650e2d39 FI |
87 | } |
88 | ||
9549ec01 FI |
89 | /* The data we are handed might be spread across several bios. We need |
90 | * to track when the last one is finished. | |
91 | */ | |
92 | struct parallel_io { | |
93 | struct kref refcnt; | |
7c5465d6 | 94 | void (*pnfs_callback) (void *data, int num_se); |
9549ec01 | 95 | void *data; |
7c5465d6 | 96 | int bse_count; |
9549ec01 FI |
97 | }; |
98 | ||
99 | static inline struct parallel_io *alloc_parallel(void *data) | |
100 | { | |
101 | struct parallel_io *rv; | |
102 | ||
103 | rv = kmalloc(sizeof(*rv), GFP_NOFS); | |
104 | if (rv) { | |
105 | rv->data = data; | |
106 | kref_init(&rv->refcnt); | |
7c5465d6 | 107 | rv->bse_count = 0; |
9549ec01 FI |
108 | } |
109 | return rv; | |
110 | } | |
111 | ||
112 | static inline void get_parallel(struct parallel_io *p) | |
113 | { | |
114 | kref_get(&p->refcnt); | |
115 | } | |
116 | ||
117 | static void destroy_parallel(struct kref *kref) | |
118 | { | |
119 | struct parallel_io *p = container_of(kref, struct parallel_io, refcnt); | |
120 | ||
121 | dprintk("%s enter\n", __func__); | |
7c5465d6 | 122 | p->pnfs_callback(p->data, p->bse_count); |
9549ec01 FI |
123 | kfree(p); |
124 | } | |
125 | ||
126 | static inline void put_parallel(struct parallel_io *p) | |
127 | { | |
128 | kref_put(&p->refcnt, destroy_parallel); | |
129 | } | |
130 | ||
131 | static struct bio * | |
132 | bl_submit_bio(int rw, struct bio *bio) | |
133 | { | |
134 | if (bio) { | |
135 | get_parallel(bio->bi_private); | |
136 | dprintk("%s submitting %s bio %u@%llu\n", __func__, | |
4f024f37 KO |
137 | rw == READ ? "read" : "write", bio->bi_iter.bi_size, |
138 | (unsigned long long)bio->bi_iter.bi_sector); | |
9549ec01 FI |
139 | submit_bio(rw, bio); |
140 | } | |
141 | return NULL; | |
142 | } | |
143 | ||
144 | static struct bio *bl_alloc_init_bio(int npg, sector_t isect, | |
145 | struct pnfs_block_extent *be, | |
146 | void (*end_io)(struct bio *, int err), | |
147 | struct parallel_io *par) | |
148 | { | |
149 | struct bio *bio; | |
150 | ||
74a6eeb4 | 151 | npg = min(npg, BIO_MAX_PAGES); |
9549ec01 | 152 | bio = bio_alloc(GFP_NOIO, npg); |
74a6eeb4 PT |
153 | if (!bio && (current->flags & PF_MEMALLOC)) { |
154 | while (!bio && (npg /= 2)) | |
155 | bio = bio_alloc(GFP_NOIO, npg); | |
156 | } | |
9549ec01 | 157 | |
74a6eeb4 | 158 | if (bio) { |
4f024f37 KO |
159 | bio->bi_iter.bi_sector = isect - be->be_f_offset + |
160 | be->be_v_offset; | |
74a6eeb4 PT |
161 | bio->bi_bdev = be->be_mdev; |
162 | bio->bi_end_io = end_io; | |
163 | bio->bi_private = par; | |
164 | } | |
9549ec01 FI |
165 | return bio; |
166 | } | |
167 | ||
fe6e1e8d | 168 | static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw, |
9549ec01 FI |
169 | sector_t isect, struct page *page, |
170 | struct pnfs_block_extent *be, | |
171 | void (*end_io)(struct bio *, int err), | |
fe6e1e8d PT |
172 | struct parallel_io *par, |
173 | unsigned int offset, int len) | |
9549ec01 | 174 | { |
fe6e1e8d PT |
175 | isect = isect + (offset >> SECTOR_SHIFT); |
176 | dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__, | |
177 | npg, rw, (unsigned long long)isect, offset, len); | |
9549ec01 FI |
178 | retry: |
179 | if (!bio) { | |
180 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); | |
181 | if (!bio) | |
182 | return ERR_PTR(-ENOMEM); | |
183 | } | |
fe6e1e8d | 184 | if (bio_add_page(bio, page, len, offset) < len) { |
9549ec01 FI |
185 | bio = bl_submit_bio(rw, bio); |
186 | goto retry; | |
187 | } | |
188 | return bio; | |
189 | } | |
190 | ||
fe6e1e8d PT |
191 | static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw, |
192 | sector_t isect, struct page *page, | |
193 | struct pnfs_block_extent *be, | |
194 | void (*end_io)(struct bio *, int err), | |
195 | struct parallel_io *par) | |
196 | { | |
197 | return do_add_page_to_bio(bio, npg, rw, isect, page, be, | |
198 | end_io, par, 0, PAGE_CACHE_SIZE); | |
199 | } | |
200 | ||
9549ec01 FI |
201 | /* This is basically copied from mpage_end_io_read */ |
202 | static void bl_end_io_read(struct bio *bio, int err) | |
203 | { | |
204 | struct parallel_io *par = bio->bi_private; | |
2c30c71b KO |
205 | struct bio_vec *bvec; |
206 | int i; | |
9549ec01 | 207 | |
2c30c71b KO |
208 | if (!err) |
209 | bio_for_each_segment_all(bvec, bio, i) | |
210 | SetPageUptodate(bvec->bv_page); | |
9549ec01 | 211 | |
2c30c71b | 212 | if (err) { |
d45f60c6 | 213 | struct nfs_pgio_header *header = par->data; |
cd841605 FI |
214 | |
215 | if (!header->pnfs_error) | |
216 | header->pnfs_error = -EIO; | |
217 | pnfs_set_lo_fail(header->lseg); | |
9549ec01 FI |
218 | } |
219 | bio_put(bio); | |
220 | put_parallel(par); | |
221 | } | |
222 | ||
223 | static void bl_read_cleanup(struct work_struct *work) | |
224 | { | |
225 | struct rpc_task *task; | |
d45f60c6 | 226 | struct nfs_pgio_header *hdr; |
9549ec01 FI |
227 | dprintk("%s enter\n", __func__); |
228 | task = container_of(work, struct rpc_task, u.tk_work); | |
d45f60c6 WAA |
229 | hdr = container_of(task, struct nfs_pgio_header, task); |
230 | pnfs_ld_read_done(hdr); | |
9549ec01 FI |
231 | } |
232 | ||
233 | static void | |
7c5465d6 | 234 | bl_end_par_io_read(void *data, int unused) |
9549ec01 | 235 | { |
d45f60c6 | 236 | struct nfs_pgio_header *hdr = data; |
9549ec01 | 237 | |
d45f60c6 WAA |
238 | hdr->task.tk_status = hdr->pnfs_error; |
239 | INIT_WORK(&hdr->task.u.tk_work, bl_read_cleanup); | |
240 | schedule_work(&hdr->task.u.tk_work); | |
9549ec01 FI |
241 | } |
242 | ||
155e7524 | 243 | static enum pnfs_try_status |
d45f60c6 | 244 | bl_read_pagelist(struct nfs_pgio_header *hdr) |
155e7524 | 245 | { |
d45f60c6 | 246 | struct nfs_pgio_header *header = hdr; |
9549ec01 FI |
247 | int i, hole; |
248 | struct bio *bio = NULL; | |
249 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; | |
250 | sector_t isect, extent_length = 0; | |
251 | struct parallel_io *par; | |
d45f60c6 WAA |
252 | loff_t f_offset = hdr->args.offset; |
253 | size_t bytes_left = hdr->args.count; | |
f742dc4a | 254 | unsigned int pg_offset, pg_len; |
d45f60c6 WAA |
255 | struct page **pages = hdr->args.pages; |
256 | int pg_index = hdr->args.pgbase >> PAGE_CACHE_SHIFT; | |
f742dc4a | 257 | const bool is_dio = (header->dreq != NULL); |
9549ec01 | 258 | |
6f00866d | 259 | dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__, |
d45f60c6 WAA |
260 | hdr->page_array.npages, f_offset, |
261 | (unsigned int)hdr->args.count); | |
9549ec01 | 262 | |
d45f60c6 | 263 | par = alloc_parallel(hdr); |
9549ec01 FI |
264 | if (!par) |
265 | goto use_mds; | |
9549ec01 FI |
266 | par->pnfs_callback = bl_end_par_io_read; |
267 | /* At this point, we can no longer jump to use_mds */ | |
268 | ||
269 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); | |
270 | /* Code assumes extents are page-aligned */ | |
d45f60c6 | 271 | for (i = pg_index; i < hdr->page_array.npages; i++) { |
9549ec01 FI |
272 | if (!extent_length) { |
273 | /* We've used up the previous extent */ | |
274 | bl_put_extent(be); | |
275 | bl_put_extent(cow_read); | |
276 | bio = bl_submit_bio(READ, bio); | |
277 | /* Get the next one */ | |
cd841605 | 278 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
9549ec01 FI |
279 | isect, &cow_read); |
280 | if (!be) { | |
cd841605 | 281 | header->pnfs_error = -EIO; |
9549ec01 FI |
282 | goto out; |
283 | } | |
284 | extent_length = be->be_length - | |
285 | (isect - be->be_f_offset); | |
286 | if (cow_read) { | |
287 | sector_t cow_length = cow_read->be_length - | |
288 | (isect - cow_read->be_f_offset); | |
289 | extent_length = min(extent_length, cow_length); | |
290 | } | |
291 | } | |
f742dc4a PT |
292 | |
293 | if (is_dio) { | |
294 | pg_offset = f_offset & ~PAGE_CACHE_MASK; | |
295 | if (pg_offset + bytes_left > PAGE_CACHE_SIZE) | |
296 | pg_len = PAGE_CACHE_SIZE - pg_offset; | |
297 | else | |
298 | pg_len = bytes_left; | |
299 | ||
300 | f_offset += pg_len; | |
301 | bytes_left -= pg_len; | |
302 | isect += (pg_offset >> SECTOR_SHIFT); | |
303 | } else { | |
304 | pg_offset = 0; | |
305 | pg_len = PAGE_CACHE_SIZE; | |
306 | } | |
307 | ||
9549ec01 FI |
308 | hole = is_hole(be, isect); |
309 | if (hole && !cow_read) { | |
310 | bio = bl_submit_bio(READ, bio); | |
311 | /* Fill hole w/ zeroes w/o accessing device */ | |
312 | dprintk("%s Zeroing page for hole\n", __func__); | |
f742dc4a | 313 | zero_user_segment(pages[i], pg_offset, pg_len); |
9549ec01 FI |
314 | print_page(pages[i]); |
315 | SetPageUptodate(pages[i]); | |
316 | } else { | |
317 | struct pnfs_block_extent *be_read; | |
318 | ||
319 | be_read = (hole && cow_read) ? cow_read : be; | |
823b0c9d | 320 | bio = do_add_page_to_bio(bio, |
d45f60c6 | 321 | hdr->page_array.npages - i, |
30dd374f | 322 | READ, |
9549ec01 | 323 | isect, pages[i], be_read, |
f742dc4a PT |
324 | bl_end_io_read, par, |
325 | pg_offset, pg_len); | |
9549ec01 | 326 | if (IS_ERR(bio)) { |
cd841605 | 327 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 328 | bio = NULL; |
9549ec01 FI |
329 | goto out; |
330 | } | |
331 | } | |
f742dc4a | 332 | isect += (pg_len >> SECTOR_SHIFT); |
9549ec01 FI |
333 | extent_length -= PAGE_CACHE_SECTORS; |
334 | } | |
cd841605 | 335 | if ((isect << SECTOR_SHIFT) >= header->inode->i_size) { |
d45f60c6 WAA |
336 | hdr->res.eof = 1; |
337 | hdr->res.count = header->inode->i_size - hdr->args.offset; | |
9549ec01 | 338 | } else { |
d45f60c6 | 339 | hdr->res.count = (isect << SECTOR_SHIFT) - hdr->args.offset; |
9549ec01 FI |
340 | } |
341 | out: | |
342 | bl_put_extent(be); | |
343 | bl_put_extent(cow_read); | |
344 | bl_submit_bio(READ, bio); | |
345 | put_parallel(par); | |
346 | return PNFS_ATTEMPTED; | |
347 | ||
348 | use_mds: | |
349 | dprintk("Giving up and using normal NFS\n"); | |
155e7524 FI |
350 | return PNFS_NOT_ATTEMPTED; |
351 | } | |
352 | ||
31e6306a FI |
353 | static void mark_extents_written(struct pnfs_block_layout *bl, |
354 | __u64 offset, __u32 count) | |
355 | { | |
356 | sector_t isect, end; | |
357 | struct pnfs_block_extent *be; | |
7c5465d6 | 358 | struct pnfs_block_short_extent *se; |
31e6306a FI |
359 | |
360 | dprintk("%s(%llu, %u)\n", __func__, offset, count); | |
361 | if (count == 0) | |
362 | return; | |
363 | isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT; | |
364 | end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK); | |
365 | end >>= SECTOR_SHIFT; | |
366 | while (isect < end) { | |
367 | sector_t len; | |
368 | be = bl_find_get_extent(bl, isect, NULL); | |
369 | BUG_ON(!be); /* FIXME */ | |
370 | len = min(end, be->be_f_offset + be->be_length) - isect; | |
7c5465d6 PT |
371 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
372 | se = bl_pop_one_short_extent(be->be_inval); | |
373 | BUG_ON(!se); | |
374 | bl_mark_for_commit(be, isect, len, se); | |
375 | } | |
31e6306a FI |
376 | isect += len; |
377 | bl_put_extent(be); | |
378 | } | |
379 | } | |
380 | ||
71cdd40f PT |
381 | static void bl_end_io_write_zero(struct bio *bio, int err) |
382 | { | |
383 | struct parallel_io *par = bio->bi_private; | |
2c30c71b KO |
384 | struct bio_vec *bvec; |
385 | int i; | |
71cdd40f | 386 | |
2c30c71b | 387 | bio_for_each_segment_all(bvec, bio, i) { |
71cdd40f | 388 | /* This is the zeroing page we added */ |
2c30c71b KO |
389 | end_page_writeback(bvec->bv_page); |
390 | page_cache_release(bvec->bv_page); | |
391 | } | |
7c5465d6 | 392 | |
2c30c71b | 393 | if (unlikely(err)) { |
d45f60c6 | 394 | struct nfs_pgio_header *header = par->data; |
cd841605 FI |
395 | |
396 | if (!header->pnfs_error) | |
397 | header->pnfs_error = -EIO; | |
398 | pnfs_set_lo_fail(header->lseg); | |
71cdd40f PT |
399 | } |
400 | bio_put(bio); | |
401 | put_parallel(par); | |
402 | } | |
403 | ||
650e2d39 FI |
404 | static void bl_end_io_write(struct bio *bio, int err) |
405 | { | |
406 | struct parallel_io *par = bio->bi_private; | |
407 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
d45f60c6 | 408 | struct nfs_pgio_header *header = par->data; |
650e2d39 FI |
409 | |
410 | if (!uptodate) { | |
cd841605 FI |
411 | if (!header->pnfs_error) |
412 | header->pnfs_error = -EIO; | |
413 | pnfs_set_lo_fail(header->lseg); | |
650e2d39 FI |
414 | } |
415 | bio_put(bio); | |
416 | put_parallel(par); | |
417 | } | |
418 | ||
419 | /* Function scheduled for call during bl_end_par_io_write, | |
420 | * it marks sectors as written and extends the commitlist. | |
421 | */ | |
422 | static void bl_write_cleanup(struct work_struct *work) | |
423 | { | |
424 | struct rpc_task *task; | |
d45f60c6 | 425 | struct nfs_pgio_header *hdr; |
650e2d39 FI |
426 | dprintk("%s enter\n", __func__); |
427 | task = container_of(work, struct rpc_task, u.tk_work); | |
d45f60c6 WAA |
428 | hdr = container_of(task, struct nfs_pgio_header, task); |
429 | if (likely(!hdr->pnfs_error)) { | |
31e6306a | 430 | /* Marks for LAYOUTCOMMIT */ |
d45f60c6 WAA |
431 | mark_extents_written(BLK_LSEG2EXT(hdr->lseg), |
432 | hdr->args.offset, hdr->args.count); | |
31e6306a | 433 | } |
d45f60c6 | 434 | pnfs_ld_write_done(hdr); |
650e2d39 FI |
435 | } |
436 | ||
437 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ | |
7c5465d6 | 438 | static void bl_end_par_io_write(void *data, int num_se) |
650e2d39 | 439 | { |
d45f60c6 | 440 | struct nfs_pgio_header *hdr = data; |
650e2d39 | 441 | |
d45f60c6 WAA |
442 | if (unlikely(hdr->pnfs_error)) { |
443 | bl_free_short_extents(&BLK_LSEG2EXT(hdr->lseg)->bl_inval, | |
7c5465d6 PT |
444 | num_se); |
445 | } | |
446 | ||
d45f60c6 | 447 | hdr->task.tk_status = hdr->pnfs_error; |
c65e6254 | 448 | hdr->verf.committed = NFS_FILE_SYNC; |
d45f60c6 WAA |
449 | INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup); |
450 | schedule_work(&hdr->task.u.tk_work); | |
650e2d39 FI |
451 | } |
452 | ||
71cdd40f PT |
453 | /* FIXME STUB - mark intersection of layout and page as bad, so is not |
454 | * used again. | |
455 | */ | |
456 | static void mark_bad_read(void) | |
457 | { | |
458 | return; | |
459 | } | |
460 | ||
461 | /* | |
462 | * map_block: map a requested I/0 block (isect) into an offset in the LVM | |
463 | * block_device | |
464 | */ | |
465 | static void | |
466 | map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be) | |
467 | { | |
468 | dprintk("%s enter be=%p\n", __func__, be); | |
469 | ||
470 | set_buffer_mapped(bh); | |
471 | bh->b_bdev = be->be_mdev; | |
472 | bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >> | |
473 | (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT); | |
474 | ||
475 | dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n", | |
476 | __func__, (unsigned long long)isect, (long)bh->b_blocknr, | |
477 | bh->b_size); | |
478 | return; | |
479 | } | |
480 | ||
fe6e1e8d PT |
481 | static void |
482 | bl_read_single_end_io(struct bio *bio, int error) | |
483 | { | |
484 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
485 | struct page *page = bvec->bv_page; | |
486 | ||
487 | /* Only one page in bvec */ | |
488 | unlock_page(page); | |
489 | } | |
490 | ||
491 | static int | |
492 | bl_do_readpage_sync(struct page *page, struct pnfs_block_extent *be, | |
493 | unsigned int offset, unsigned int len) | |
494 | { | |
495 | struct bio *bio; | |
496 | struct page *shadow_page; | |
497 | sector_t isect; | |
498 | char *kaddr, *kshadow_addr; | |
499 | int ret = 0; | |
500 | ||
501 | dprintk("%s: offset %u len %u\n", __func__, offset, len); | |
502 | ||
503 | shadow_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
504 | if (shadow_page == NULL) | |
505 | return -ENOMEM; | |
506 | ||
507 | bio = bio_alloc(GFP_NOIO, 1); | |
508 | if (bio == NULL) | |
509 | return -ENOMEM; | |
510 | ||
511 | isect = (page->index << PAGE_CACHE_SECTOR_SHIFT) + | |
512 | (offset / SECTOR_SIZE); | |
513 | ||
4f024f37 | 514 | bio->bi_iter.bi_sector = isect - be->be_f_offset + be->be_v_offset; |
fe6e1e8d PT |
515 | bio->bi_bdev = be->be_mdev; |
516 | bio->bi_end_io = bl_read_single_end_io; | |
517 | ||
518 | lock_page(shadow_page); | |
519 | if (bio_add_page(bio, shadow_page, | |
520 | SECTOR_SIZE, round_down(offset, SECTOR_SIZE)) == 0) { | |
521 | unlock_page(shadow_page); | |
522 | bio_put(bio); | |
523 | return -EIO; | |
524 | } | |
525 | ||
526 | submit_bio(READ, bio); | |
527 | wait_on_page_locked(shadow_page); | |
528 | if (unlikely(!test_bit(BIO_UPTODATE, &bio->bi_flags))) { | |
529 | ret = -EIO; | |
530 | } else { | |
531 | kaddr = kmap_atomic(page); | |
532 | kshadow_addr = kmap_atomic(shadow_page); | |
533 | memcpy(kaddr + offset, kshadow_addr + offset, len); | |
534 | kunmap_atomic(kshadow_addr); | |
535 | kunmap_atomic(kaddr); | |
536 | } | |
537 | __free_page(shadow_page); | |
538 | bio_put(bio); | |
539 | ||
540 | return ret; | |
541 | } | |
542 | ||
543 | static int | |
544 | bl_read_partial_page_sync(struct page *page, struct pnfs_block_extent *be, | |
545 | unsigned int dirty_offset, unsigned int dirty_len, | |
546 | bool full_page) | |
547 | { | |
548 | int ret = 0; | |
549 | unsigned int start, end; | |
550 | ||
551 | if (full_page) { | |
552 | start = 0; | |
553 | end = PAGE_CACHE_SIZE; | |
554 | } else { | |
555 | start = round_down(dirty_offset, SECTOR_SIZE); | |
556 | end = round_up(dirty_offset + dirty_len, SECTOR_SIZE); | |
557 | } | |
558 | ||
559 | dprintk("%s: offset %u len %d\n", __func__, dirty_offset, dirty_len); | |
560 | if (!be) { | |
561 | zero_user_segments(page, start, dirty_offset, | |
562 | dirty_offset + dirty_len, end); | |
563 | if (start == 0 && end == PAGE_CACHE_SIZE && | |
564 | trylock_page(page)) { | |
565 | SetPageUptodate(page); | |
566 | unlock_page(page); | |
567 | } | |
568 | return ret; | |
569 | } | |
570 | ||
571 | if (start != dirty_offset) | |
572 | ret = bl_do_readpage_sync(page, be, start, dirty_offset - start); | |
573 | ||
574 | if (!ret && (dirty_offset + dirty_len < end)) | |
575 | ret = bl_do_readpage_sync(page, be, dirty_offset + dirty_len, | |
576 | end - dirty_offset - dirty_len); | |
577 | ||
578 | return ret; | |
579 | } | |
580 | ||
71cdd40f PT |
581 | /* Given an unmapped page, zero it or read in page for COW, page is locked |
582 | * by caller. | |
583 | */ | |
584 | static int | |
585 | init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read) | |
586 | { | |
587 | struct buffer_head *bh = NULL; | |
588 | int ret = 0; | |
589 | sector_t isect; | |
590 | ||
591 | dprintk("%s enter, %p\n", __func__, page); | |
592 | BUG_ON(PageUptodate(page)); | |
593 | if (!cow_read) { | |
594 | zero_user_segment(page, 0, PAGE_SIZE); | |
595 | SetPageUptodate(page); | |
596 | goto cleanup; | |
597 | } | |
598 | ||
599 | bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0); | |
600 | if (!bh) { | |
601 | ret = -ENOMEM; | |
602 | goto cleanup; | |
603 | } | |
604 | ||
605 | isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT; | |
606 | map_block(bh, isect, cow_read); | |
607 | if (!bh_uptodate_or_lock(bh)) | |
608 | ret = bh_submit_read(bh); | |
609 | if (ret) | |
610 | goto cleanup; | |
611 | SetPageUptodate(page); | |
612 | ||
613 | cleanup: | |
71cdd40f PT |
614 | if (bh) |
615 | free_buffer_head(bh); | |
616 | if (ret) { | |
617 | /* Need to mark layout with bad read...should now | |
618 | * just use nfs4 for reads and writes. | |
619 | */ | |
620 | mark_bad_read(); | |
621 | } | |
622 | return ret; | |
623 | } | |
624 | ||
72c50887 PT |
625 | /* Find or create a zeroing page marked being writeback. |
626 | * Return ERR_PTR on error, NULL to indicate skip this page and page itself | |
627 | * to indicate write out. | |
628 | */ | |
629 | static struct page * | |
630 | bl_find_get_zeroing_page(struct inode *inode, pgoff_t index, | |
631 | struct pnfs_block_extent *cow_read) | |
632 | { | |
633 | struct page *page; | |
634 | int locked = 0; | |
635 | page = find_get_page(inode->i_mapping, index); | |
636 | if (page) | |
637 | goto check_page; | |
638 | ||
639 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
640 | if (unlikely(!page)) { | |
641 | dprintk("%s oom\n", __func__); | |
642 | return ERR_PTR(-ENOMEM); | |
643 | } | |
644 | locked = 1; | |
645 | ||
646 | check_page: | |
647 | /* PageDirty: Other will write this out | |
648 | * PageWriteback: Other is writing this out | |
649 | * PageUptodate: It was read before | |
650 | */ | |
651 | if (PageDirty(page) || PageWriteback(page)) { | |
652 | print_page(page); | |
653 | if (locked) | |
654 | unlock_page(page); | |
655 | page_cache_release(page); | |
656 | return NULL; | |
657 | } | |
658 | ||
659 | if (!locked) { | |
660 | lock_page(page); | |
661 | locked = 1; | |
662 | goto check_page; | |
663 | } | |
664 | if (!PageUptodate(page)) { | |
665 | /* New page, readin or zero it */ | |
666 | init_page_for_write(page, cow_read); | |
667 | } | |
668 | set_page_writeback(page); | |
669 | unlock_page(page); | |
670 | ||
671 | return page; | |
672 | } | |
673 | ||
155e7524 | 674 | static enum pnfs_try_status |
d45f60c6 | 675 | bl_write_pagelist(struct nfs_pgio_header *header, int sync) |
155e7524 | 676 | { |
71cdd40f | 677 | int i, ret, npg_zero, pg_index, last = 0; |
650e2d39 | 678 | struct bio *bio = NULL; |
71cdd40f PT |
679 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
680 | sector_t isect, last_isect = 0, extent_length = 0; | |
96c9eae6 | 681 | struct parallel_io *par = NULL; |
d45f60c6 WAA |
682 | loff_t offset = header->args.offset; |
683 | size_t count = header->args.count; | |
fe6e1e8d | 684 | unsigned int pg_offset, pg_len, saved_len; |
d45f60c6 | 685 | struct page **pages = header->args.pages; |
71cdd40f PT |
686 | struct page *page; |
687 | pgoff_t index; | |
688 | u64 temp; | |
689 | int npg_per_block = | |
cd841605 | 690 | NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT; |
650e2d39 FI |
691 | |
692 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); | |
96c9eae6 PT |
693 | |
694 | if (header->dreq != NULL && | |
695 | (!IS_ALIGNED(offset, NFS_SERVER(header->inode)->pnfs_blksize) || | |
696 | !IS_ALIGNED(count, NFS_SERVER(header->inode)->pnfs_blksize))) { | |
697 | dprintk("pnfsblock nonblock aligned DIO writes. Resend MDS\n"); | |
698 | goto out_mds; | |
699 | } | |
d45f60c6 | 700 | /* At this point, header->page_aray is a (sequential) list of nfs_pages. |
71cdd40f PT |
701 | * We want to write each, and if there is an error set pnfs_error |
702 | * to have it redone using nfs. | |
650e2d39 | 703 | */ |
d45f60c6 | 704 | par = alloc_parallel(header); |
650e2d39 | 705 | if (!par) |
7c5465d6 | 706 | goto out_mds; |
650e2d39 FI |
707 | par->pnfs_callback = bl_end_par_io_write; |
708 | /* At this point, have to be more careful with error handling */ | |
709 | ||
710 | isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
cd841605 | 711 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read); |
71cdd40f PT |
712 | if (!be || !is_writable(be, isect)) { |
713 | dprintk("%s no matching extents!\n", __func__); | |
7c5465d6 | 714 | goto out_mds; |
71cdd40f PT |
715 | } |
716 | ||
717 | /* First page inside INVALID extent */ | |
718 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
7c5465d6 PT |
719 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
720 | par->bse_count++; | |
721 | else | |
722 | goto out_mds; | |
71cdd40f PT |
723 | temp = offset >> PAGE_CACHE_SHIFT; |
724 | npg_zero = do_div(temp, npg_per_block); | |
725 | isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) & | |
726 | (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
727 | extent_length = be->be_length - (isect - be->be_f_offset); | |
728 | ||
729 | fill_invalid_ext: | |
730 | dprintk("%s need to zero %d pages\n", __func__, npg_zero); | |
731 | for (;npg_zero > 0; npg_zero--) { | |
75422745 PT |
732 | if (bl_is_sector_init(be->be_inval, isect)) { |
733 | dprintk("isect %llu already init\n", | |
734 | (unsigned long long)isect); | |
735 | goto next_page; | |
736 | } | |
71cdd40f PT |
737 | /* page ref released in bl_end_io_write_zero */ |
738 | index = isect >> PAGE_CACHE_SECTOR_SHIFT; | |
739 | dprintk("%s zero %dth page: index %lu isect %llu\n", | |
740 | __func__, npg_zero, index, | |
741 | (unsigned long long)isect); | |
cd841605 | 742 | page = bl_find_get_zeroing_page(header->inode, index, |
72c50887 PT |
743 | cow_read); |
744 | if (unlikely(IS_ERR(page))) { | |
cd841605 | 745 | header->pnfs_error = PTR_ERR(page); |
71cdd40f | 746 | goto out; |
72c50887 | 747 | } else if (page == NULL) |
71cdd40f | 748 | goto next_page; |
71cdd40f PT |
749 | |
750 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
60c52e3a | 751 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
752 | if (unlikely(ret)) { |
753 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
754 | __func__, ret); | |
755 | end_page_writeback(page); | |
756 | page_cache_release(page); | |
cd841605 | 757 | header->pnfs_error = ret; |
71cdd40f PT |
758 | goto out; |
759 | } | |
7c5465d6 PT |
760 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
761 | par->bse_count++; | |
762 | else { | |
763 | end_page_writeback(page); | |
764 | page_cache_release(page); | |
cd841605 | 765 | header->pnfs_error = -ENOMEM; |
7c5465d6 PT |
766 | goto out; |
767 | } | |
768 | /* FIXME: This should be done in bi_end_io */ | |
cd841605 | 769 | mark_extents_written(BLK_LSEG2EXT(header->lseg), |
7c5465d6 PT |
770 | page->index << PAGE_CACHE_SHIFT, |
771 | PAGE_CACHE_SIZE); | |
772 | ||
71cdd40f PT |
773 | bio = bl_add_page_to_bio(bio, npg_zero, WRITE, |
774 | isect, page, be, | |
775 | bl_end_io_write_zero, par); | |
776 | if (IS_ERR(bio)) { | |
cd841605 | 777 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 778 | bio = NULL; |
71cdd40f PT |
779 | goto out; |
780 | } | |
71cdd40f PT |
781 | next_page: |
782 | isect += PAGE_CACHE_SECTORS; | |
783 | extent_length -= PAGE_CACHE_SECTORS; | |
784 | } | |
785 | if (last) | |
786 | goto write_done; | |
787 | } | |
788 | bio = bl_submit_bio(WRITE, bio); | |
789 | ||
790 | /* Middle pages */ | |
d45f60c6 WAA |
791 | pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT; |
792 | for (i = pg_index; i < header->page_array.npages; i++) { | |
650e2d39 FI |
793 | if (!extent_length) { |
794 | /* We've used up the previous extent */ | |
795 | bl_put_extent(be); | |
fe6e1e8d | 796 | bl_put_extent(cow_read); |
650e2d39 FI |
797 | bio = bl_submit_bio(WRITE, bio); |
798 | /* Get the next one */ | |
cd841605 | 799 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
fe6e1e8d | 800 | isect, &cow_read); |
650e2d39 | 801 | if (!be || !is_writable(be, isect)) { |
cd841605 | 802 | header->pnfs_error = -EINVAL; |
650e2d39 FI |
803 | goto out; |
804 | } | |
7c5465d6 PT |
805 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
806 | if (likely(!bl_push_one_short_extent( | |
807 | be->be_inval))) | |
808 | par->bse_count++; | |
809 | else { | |
cd841605 | 810 | header->pnfs_error = -ENOMEM; |
7c5465d6 PT |
811 | goto out; |
812 | } | |
813 | } | |
650e2d39 | 814 | extent_length = be->be_length - |
71cdd40f | 815 | (isect - be->be_f_offset); |
650e2d39 | 816 | } |
fe6e1e8d PT |
817 | |
818 | dprintk("%s offset %lld count %Zu\n", __func__, offset, count); | |
819 | pg_offset = offset & ~PAGE_CACHE_MASK; | |
820 | if (pg_offset + count > PAGE_CACHE_SIZE) | |
821 | pg_len = PAGE_CACHE_SIZE - pg_offset; | |
822 | else | |
823 | pg_len = count; | |
824 | ||
825 | saved_len = pg_len; | |
826 | if (be->be_state == PNFS_BLOCK_INVALID_DATA && | |
827 | !bl_is_sector_init(be->be_inval, isect)) { | |
828 | ret = bl_read_partial_page_sync(pages[i], cow_read, | |
829 | pg_offset, pg_len, true); | |
830 | if (ret) { | |
831 | dprintk("%s bl_read_partial_page_sync fail %d\n", | |
832 | __func__, ret); | |
833 | header->pnfs_error = ret; | |
834 | goto out; | |
835 | } | |
836 | ||
71cdd40f | 837 | ret = bl_mark_sectors_init(be->be_inval, isect, |
60c52e3a | 838 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
839 | if (unlikely(ret)) { |
840 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
841 | __func__, ret); | |
cd841605 | 842 | header->pnfs_error = ret; |
71cdd40f | 843 | goto out; |
650e2d39 | 844 | } |
fe6e1e8d PT |
845 | |
846 | /* Expand to full page write */ | |
847 | pg_offset = 0; | |
848 | pg_len = PAGE_CACHE_SIZE; | |
849 | } else if ((pg_offset & (SECTOR_SIZE - 1)) || | |
850 | (pg_len & (SECTOR_SIZE - 1))){ | |
851 | /* ahh, nasty case. We have to do sync full sector | |
852 | * read-modify-write cycles. | |
853 | */ | |
854 | unsigned int saved_offset = pg_offset; | |
855 | ret = bl_read_partial_page_sync(pages[i], be, pg_offset, | |
856 | pg_len, false); | |
857 | pg_offset = round_down(pg_offset, SECTOR_SIZE); | |
858 | pg_len = round_up(saved_offset + pg_len, SECTOR_SIZE) | |
859 | - pg_offset; | |
71cdd40f | 860 | } |
fe6e1e8d PT |
861 | |
862 | ||
d45f60c6 | 863 | bio = do_add_page_to_bio(bio, header->page_array.npages - i, |
823b0c9d | 864 | WRITE, |
71cdd40f | 865 | isect, pages[i], be, |
fe6e1e8d PT |
866 | bl_end_io_write, par, |
867 | pg_offset, pg_len); | |
71cdd40f | 868 | if (IS_ERR(bio)) { |
cd841605 | 869 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 870 | bio = NULL; |
71cdd40f | 871 | goto out; |
650e2d39 | 872 | } |
fe6e1e8d PT |
873 | offset += saved_len; |
874 | count -= saved_len; | |
650e2d39 | 875 | isect += PAGE_CACHE_SECTORS; |
71cdd40f | 876 | last_isect = isect; |
650e2d39 FI |
877 | extent_length -= PAGE_CACHE_SECTORS; |
878 | } | |
71cdd40f PT |
879 | |
880 | /* Last page inside INVALID extent */ | |
881 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
882 | bio = bl_submit_bio(WRITE, bio); | |
883 | temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT; | |
884 | npg_zero = npg_per_block - do_div(temp, npg_per_block); | |
885 | if (npg_zero < npg_per_block) { | |
886 | last = 1; | |
887 | goto fill_invalid_ext; | |
888 | } | |
889 | } | |
890 | ||
891 | write_done: | |
d45f60c6 | 892 | header->res.count = header->args.count; |
650e2d39 FI |
893 | out: |
894 | bl_put_extent(be); | |
fe6e1e8d | 895 | bl_put_extent(cow_read); |
650e2d39 FI |
896 | bl_submit_bio(WRITE, bio); |
897 | put_parallel(par); | |
898 | return PNFS_ATTEMPTED; | |
7c5465d6 PT |
899 | out_mds: |
900 | bl_put_extent(be); | |
fe6e1e8d | 901 | bl_put_extent(cow_read); |
7c5465d6 PT |
902 | kfree(par); |
903 | return PNFS_NOT_ATTEMPTED; | |
155e7524 FI |
904 | } |
905 | ||
9e692969 | 906 | /* FIXME - range ignored */ |
155e7524 | 907 | static void |
9e692969 | 908 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
155e7524 | 909 | { |
9e692969 FI |
910 | int i; |
911 | struct pnfs_block_extent *be; | |
912 | ||
913 | spin_lock(&bl->bl_ext_lock); | |
914 | for (i = 0; i < EXTENT_LISTS; i++) { | |
915 | while (!list_empty(&bl->bl_extents[i])) { | |
916 | be = list_first_entry(&bl->bl_extents[i], | |
917 | struct pnfs_block_extent, | |
918 | be_node); | |
919 | list_del(&be->be_node); | |
920 | bl_put_extent(be); | |
921 | } | |
922 | } | |
923 | spin_unlock(&bl->bl_ext_lock); | |
155e7524 FI |
924 | } |
925 | ||
155e7524 FI |
926 | static void |
927 | release_inval_marks(struct pnfs_inval_markings *marks) | |
928 | { | |
c1c2a4cd | 929 | struct pnfs_inval_tracking *pos, *temp; |
7c5465d6 | 930 | struct pnfs_block_short_extent *se, *stemp; |
c1c2a4cd FI |
931 | |
932 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { | |
933 | list_del(&pos->it_link); | |
934 | kfree(pos); | |
935 | } | |
7c5465d6 PT |
936 | |
937 | list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) { | |
938 | list_del(&se->bse_node); | |
939 | kfree(se); | |
940 | } | |
155e7524 FI |
941 | return; |
942 | } | |
943 | ||
944 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
945 | { | |
946 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); | |
947 | ||
948 | dprintk("%s enter\n", __func__); | |
949 | release_extents(bl, NULL); | |
950 | release_inval_marks(&bl->bl_inval); | |
951 | kfree(bl); | |
952 | } | |
953 | ||
954 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, | |
955 | gfp_t gfp_flags) | |
956 | { | |
957 | struct pnfs_block_layout *bl; | |
958 | ||
959 | dprintk("%s enter\n", __func__); | |
960 | bl = kzalloc(sizeof(*bl), gfp_flags); | |
961 | if (!bl) | |
962 | return NULL; | |
963 | spin_lock_init(&bl->bl_ext_lock); | |
964 | INIT_LIST_HEAD(&bl->bl_extents[0]); | |
965 | INIT_LIST_HEAD(&bl->bl_extents[1]); | |
966 | INIT_LIST_HEAD(&bl->bl_commit); | |
967 | INIT_LIST_HEAD(&bl->bl_committing); | |
968 | bl->bl_count = 0; | |
969 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; | |
970 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); | |
971 | return &bl->bl_layout; | |
972 | } | |
973 | ||
a60d2ebd | 974 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
155e7524 | 975 | { |
a60d2ebd FI |
976 | dprintk("%s enter\n", __func__); |
977 | kfree(lseg); | |
155e7524 FI |
978 | } |
979 | ||
a60d2ebd FI |
980 | /* We pretty much ignore lseg, and store all data layout wide, so we |
981 | * can correctly merge. | |
982 | */ | |
983 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, | |
984 | struct nfs4_layoutget_res *lgr, | |
985 | gfp_t gfp_flags) | |
155e7524 | 986 | { |
a60d2ebd FI |
987 | struct pnfs_layout_segment *lseg; |
988 | int status; | |
989 | ||
990 | dprintk("%s enter\n", __func__); | |
991 | lseg = kzalloc(sizeof(*lseg), gfp_flags); | |
992 | if (!lseg) | |
993 | return ERR_PTR(-ENOMEM); | |
994 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); | |
995 | if (status) { | |
996 | /* We don't want to call the full-blown bl_free_lseg, | |
997 | * since on error extents were not touched. | |
998 | */ | |
999 | kfree(lseg); | |
1000 | return ERR_PTR(status); | |
1001 | } | |
1002 | return lseg; | |
155e7524 FI |
1003 | } |
1004 | ||
1005 | static void | |
1006 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, | |
1007 | const struct nfs4_layoutcommit_args *arg) | |
1008 | { | |
90ace12a FI |
1009 | dprintk("%s enter\n", __func__); |
1010 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); | |
155e7524 FI |
1011 | } |
1012 | ||
1013 | static void | |
1014 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) | |
1015 | { | |
b2be7811 FI |
1016 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
1017 | ||
1018 | dprintk("%s enter\n", __func__); | |
1019 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); | |
155e7524 FI |
1020 | } |
1021 | ||
2f9fd182 FI |
1022 | static void free_blk_mountid(struct block_mount_id *mid) |
1023 | { | |
1024 | if (mid) { | |
93a3844e PT |
1025 | struct pnfs_block_dev *dev, *tmp; |
1026 | ||
1027 | /* No need to take bm_lock as we are last user freeing bm_devlist */ | |
1028 | list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) { | |
2f9fd182 FI |
1029 | list_del(&dev->bm_node); |
1030 | bl_free_block_dev(dev); | |
1031 | } | |
2f9fd182 FI |
1032 | kfree(mid); |
1033 | } | |
1034 | } | |
1035 | ||
78e4e05c | 1036 | /* This is mostly copied from the filelayout_get_device_info function. |
2f9fd182 FI |
1037 | * It seems much of this should be at the generic pnfs level. |
1038 | */ | |
1039 | static struct pnfs_block_dev * | |
1040 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, | |
1041 | struct nfs4_deviceid *d_id) | |
1042 | { | |
1043 | struct pnfs_device *dev; | |
516f2e24 | 1044 | struct pnfs_block_dev *rv; |
2f9fd182 FI |
1045 | u32 max_resp_sz; |
1046 | int max_pages; | |
1047 | struct page **pages = NULL; | |
1048 | int i, rc; | |
1049 | ||
1050 | /* | |
1051 | * Use the session max response size as the basis for setting | |
1052 | * GETDEVICEINFO's maxcount | |
1053 | */ | |
1054 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
10bd295a | 1055 | max_pages = nfs_page_array_len(0, max_resp_sz); |
2f9fd182 FI |
1056 | dprintk("%s max_resp_sz %u max_pages %d\n", |
1057 | __func__, max_resp_sz, max_pages); | |
1058 | ||
1059 | dev = kmalloc(sizeof(*dev), GFP_NOFS); | |
1060 | if (!dev) { | |
1061 | dprintk("%s kmalloc failed\n", __func__); | |
516f2e24 | 1062 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
1063 | } |
1064 | ||
f15b5041 | 1065 | pages = kcalloc(max_pages, sizeof(struct page *), GFP_NOFS); |
2f9fd182 FI |
1066 | if (pages == NULL) { |
1067 | kfree(dev); | |
516f2e24 | 1068 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
1069 | } |
1070 | for (i = 0; i < max_pages; i++) { | |
1071 | pages[i] = alloc_page(GFP_NOFS); | |
516f2e24 JR |
1072 | if (!pages[i]) { |
1073 | rv = ERR_PTR(-ENOMEM); | |
2f9fd182 | 1074 | goto out_free; |
516f2e24 | 1075 | } |
2f9fd182 FI |
1076 | } |
1077 | ||
1078 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); | |
1079 | dev->layout_type = LAYOUT_BLOCK_VOLUME; | |
1080 | dev->pages = pages; | |
1081 | dev->pgbase = 0; | |
1082 | dev->pglen = PAGE_SIZE * max_pages; | |
1083 | dev->mincount = 0; | |
968fe252 | 1084 | dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead; |
2f9fd182 FI |
1085 | |
1086 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); | |
cd5875fe | 1087 | rc = nfs4_proc_getdeviceinfo(server, dev, NULL); |
2f9fd182 | 1088 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
516f2e24 JR |
1089 | if (rc) { |
1090 | rv = ERR_PTR(rc); | |
2f9fd182 | 1091 | goto out_free; |
516f2e24 | 1092 | } |
2f9fd182 FI |
1093 | |
1094 | rv = nfs4_blk_decode_device(server, dev); | |
1095 | out_free: | |
1096 | for (i = 0; i < max_pages; i++) | |
1097 | __free_page(pages[i]); | |
1098 | kfree(pages); | |
1099 | kfree(dev); | |
1100 | return rv; | |
1101 | } | |
1102 | ||
155e7524 FI |
1103 | static int |
1104 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) | |
1105 | { | |
2f9fd182 FI |
1106 | struct block_mount_id *b_mt_id = NULL; |
1107 | struct pnfs_devicelist *dlist = NULL; | |
1108 | struct pnfs_block_dev *bdev; | |
1109 | LIST_HEAD(block_disklist); | |
516f2e24 | 1110 | int status, i; |
2f9fd182 | 1111 | |
155e7524 | 1112 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
1113 | |
1114 | if (server->pnfs_blksize == 0) { | |
1115 | dprintk("%s Server did not return blksize\n", __func__); | |
1116 | return -EINVAL; | |
1117 | } | |
e3aaf7f2 CH |
1118 | if (server->pnfs_blksize > PAGE_SIZE) { |
1119 | printk(KERN_ERR "%s: pNFS blksize %d not supported.\n", | |
1120 | __func__, server->pnfs_blksize); | |
1121 | return -EINVAL; | |
1122 | } | |
1123 | ||
2f9fd182 FI |
1124 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); |
1125 | if (!b_mt_id) { | |
1126 | status = -ENOMEM; | |
1127 | goto out_error; | |
1128 | } | |
1129 | /* Initialize nfs4 block layout mount id */ | |
1130 | spin_lock_init(&b_mt_id->bm_lock); | |
1131 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); | |
1132 | ||
1133 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); | |
1134 | if (!dlist) { | |
1135 | status = -ENOMEM; | |
1136 | goto out_error; | |
1137 | } | |
1138 | dlist->eof = 0; | |
1139 | while (!dlist->eof) { | |
1140 | status = nfs4_proc_getdevicelist(server, fh, dlist); | |
1141 | if (status) | |
1142 | goto out_error; | |
1143 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", | |
1144 | __func__, dlist->num_devs, dlist->eof); | |
1145 | for (i = 0; i < dlist->num_devs; i++) { | |
1146 | bdev = nfs4_blk_get_deviceinfo(server, fh, | |
1147 | &dlist->dev_id[i]); | |
516f2e24 JR |
1148 | if (IS_ERR(bdev)) { |
1149 | status = PTR_ERR(bdev); | |
2f9fd182 FI |
1150 | goto out_error; |
1151 | } | |
1152 | spin_lock(&b_mt_id->bm_lock); | |
1153 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); | |
1154 | spin_unlock(&b_mt_id->bm_lock); | |
1155 | } | |
1156 | } | |
1157 | dprintk("%s SUCCESS\n", __func__); | |
1158 | server->pnfs_ld_data = b_mt_id; | |
1159 | ||
1160 | out_return: | |
1161 | kfree(dlist); | |
1162 | return status; | |
1163 | ||
1164 | out_error: | |
1165 | free_blk_mountid(b_mt_id); | |
1166 | goto out_return; | |
155e7524 FI |
1167 | } |
1168 | ||
1169 | static int | |
1170 | bl_clear_layoutdriver(struct nfs_server *server) | |
1171 | { | |
2f9fd182 FI |
1172 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
1173 | ||
155e7524 | 1174 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
1175 | free_blk_mountid(b_mt_id); |
1176 | dprintk("%s RETURNS\n", __func__); | |
155e7524 FI |
1177 | return 0; |
1178 | } | |
1179 | ||
f742dc4a PT |
1180 | static bool |
1181 | is_aligned_req(struct nfs_page *req, unsigned int alignment) | |
1182 | { | |
1183 | return IS_ALIGNED(req->wb_offset, alignment) && | |
1184 | IS_ALIGNED(req->wb_bytes, alignment); | |
1185 | } | |
1186 | ||
1187 | static void | |
1188 | bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1189 | { | |
1190 | if (pgio->pg_dreq != NULL && | |
1191 | !is_aligned_req(req, SECTOR_SIZE)) | |
1192 | nfs_pageio_reset_read_mds(pgio); | |
1193 | else | |
1194 | pnfs_generic_pg_init_read(pgio, req); | |
1195 | } | |
1196 | ||
b4fdac1a WAA |
1197 | /* |
1198 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number | |
1199 | * of bytes (maximum @req->wb_bytes) that can be coalesced. | |
1200 | */ | |
1201 | static size_t | |
f742dc4a PT |
1202 | bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
1203 | struct nfs_page *req) | |
1204 | { | |
1205 | if (pgio->pg_dreq != NULL && | |
1206 | !is_aligned_req(req, SECTOR_SIZE)) | |
b4fdac1a | 1207 | return 0; |
f742dc4a PT |
1208 | |
1209 | return pnfs_generic_pg_test(pgio, prev, req); | |
1210 | } | |
1211 | ||
6296556f PT |
1212 | /* |
1213 | * Return the number of contiguous bytes for a given inode | |
1214 | * starting at page frame idx. | |
1215 | */ | |
1216 | static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx) | |
1217 | { | |
1218 | struct address_space *mapping = inode->i_mapping; | |
1219 | pgoff_t end; | |
1220 | ||
1221 | /* Optimize common case that writes from 0 to end of file */ | |
1222 | end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE); | |
1223 | if (end != NFS_I(inode)->npages) { | |
1224 | rcu_read_lock(); | |
e7b563bb | 1225 | end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX); |
6296556f PT |
1226 | rcu_read_unlock(); |
1227 | } | |
1228 | ||
1229 | if (!end) | |
1230 | return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT); | |
1231 | else | |
1232 | return (end - idx) << PAGE_CACHE_SHIFT; | |
1233 | } | |
1234 | ||
6f018efa | 1235 | static void |
96c9eae6 PT |
1236 | bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
1237 | { | |
1238 | if (pgio->pg_dreq != NULL && | |
6296556f | 1239 | !is_aligned_req(req, PAGE_CACHE_SIZE)) { |
96c9eae6 | 1240 | nfs_pageio_reset_write_mds(pgio); |
6296556f PT |
1241 | } else { |
1242 | u64 wb_size; | |
1243 | if (pgio->pg_dreq == NULL) | |
1244 | wb_size = pnfs_num_cont_bytes(pgio->pg_inode, | |
1245 | req->wb_index); | |
1246 | else | |
1247 | wb_size = nfs_dreq_bytes_left(pgio->pg_dreq); | |
1248 | ||
1249 | pnfs_generic_pg_init_write(pgio, req, wb_size); | |
1250 | } | |
96c9eae6 PT |
1251 | } |
1252 | ||
b4fdac1a WAA |
1253 | /* |
1254 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number | |
1255 | * of bytes (maximum @req->wb_bytes) that can be coalesced. | |
1256 | */ | |
1257 | static size_t | |
96c9eae6 PT |
1258 | bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
1259 | struct nfs_page *req) | |
1260 | { | |
1261 | if (pgio->pg_dreq != NULL && | |
1262 | !is_aligned_req(req, PAGE_CACHE_SIZE)) | |
b4fdac1a | 1263 | return 0; |
96c9eae6 PT |
1264 | |
1265 | return pnfs_generic_pg_test(pgio, prev, req); | |
1266 | } | |
1267 | ||
e9643fe8 | 1268 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
f742dc4a PT |
1269 | .pg_init = bl_pg_init_read, |
1270 | .pg_test = bl_pg_test_read, | |
e9643fe8 BH |
1271 | .pg_doio = pnfs_generic_pg_readpages, |
1272 | }; | |
1273 | ||
1274 | static const struct nfs_pageio_ops bl_pg_write_ops = { | |
96c9eae6 PT |
1275 | .pg_init = bl_pg_init_write, |
1276 | .pg_test = bl_pg_test_write, | |
e9643fe8 BH |
1277 | .pg_doio = pnfs_generic_pg_writepages, |
1278 | }; | |
1279 | ||
155e7524 FI |
1280 | static struct pnfs_layoutdriver_type blocklayout_type = { |
1281 | .id = LAYOUT_BLOCK_VOLUME, | |
1282 | .name = "LAYOUT_BLOCK_VOLUME", | |
5a12cca6 | 1283 | .owner = THIS_MODULE, |
155e7524 FI |
1284 | .read_pagelist = bl_read_pagelist, |
1285 | .write_pagelist = bl_write_pagelist, | |
1286 | .alloc_layout_hdr = bl_alloc_layout_hdr, | |
1287 | .free_layout_hdr = bl_free_layout_hdr, | |
1288 | .alloc_lseg = bl_alloc_lseg, | |
1289 | .free_lseg = bl_free_lseg, | |
1290 | .encode_layoutcommit = bl_encode_layoutcommit, | |
1291 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, | |
1292 | .set_layoutdriver = bl_set_layoutdriver, | |
1293 | .clear_layoutdriver = bl_clear_layoutdriver, | |
e9643fe8 BH |
1294 | .pg_read_ops = &bl_pg_read_ops, |
1295 | .pg_write_ops = &bl_pg_write_ops, | |
155e7524 FI |
1296 | }; |
1297 | ||
fe0a9b74 | 1298 | static const struct rpc_pipe_ops bl_upcall_ops = { |
c1225158 | 1299 | .upcall = rpc_pipe_generic_upcall, |
fe0a9b74 JR |
1300 | .downcall = bl_pipe_downcall, |
1301 | .destroy_msg = bl_pipe_destroy_msg, | |
1302 | }; | |
1303 | ||
332dfab6 SK |
1304 | static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb, |
1305 | struct rpc_pipe *pipe) | |
1306 | { | |
1307 | struct dentry *dir, *dentry; | |
1308 | ||
1309 | dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); | |
1310 | if (dir == NULL) | |
1311 | return ERR_PTR(-ENOENT); | |
1312 | dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); | |
1313 | dput(dir); | |
1314 | return dentry; | |
1315 | } | |
1316 | ||
1317 | static void nfs4blocklayout_unregister_sb(struct super_block *sb, | |
1318 | struct rpc_pipe *pipe) | |
1319 | { | |
1320 | if (pipe->dentry) | |
1321 | rpc_unlink(pipe->dentry); | |
1322 | } | |
1323 | ||
627f3066 SK |
1324 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
1325 | void *ptr) | |
1326 | { | |
1327 | struct super_block *sb = ptr; | |
1328 | struct net *net = sb->s_fs_info; | |
1329 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1330 | struct dentry *dentry; | |
1331 | int ret = 0; | |
1332 | ||
1333 | if (!try_module_get(THIS_MODULE)) | |
1334 | return 0; | |
1335 | ||
1336 | if (nn->bl_device_pipe == NULL) { | |
1337 | module_put(THIS_MODULE); | |
1338 | return 0; | |
1339 | } | |
1340 | ||
1341 | switch (event) { | |
1342 | case RPC_PIPEFS_MOUNT: | |
1343 | dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); | |
1344 | if (IS_ERR(dentry)) { | |
1345 | ret = PTR_ERR(dentry); | |
1346 | break; | |
1347 | } | |
1348 | nn->bl_device_pipe->dentry = dentry; | |
1349 | break; | |
1350 | case RPC_PIPEFS_UMOUNT: | |
1351 | if (nn->bl_device_pipe->dentry) | |
1352 | nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe); | |
1353 | break; | |
1354 | default: | |
1355 | ret = -ENOTSUPP; | |
1356 | break; | |
1357 | } | |
1358 | module_put(THIS_MODULE); | |
1359 | return ret; | |
1360 | } | |
1361 | ||
1362 | static struct notifier_block nfs4blocklayout_block = { | |
1363 | .notifier_call = rpc_pipefs_event, | |
1364 | }; | |
1365 | ||
332dfab6 SK |
1366 | static struct dentry *nfs4blocklayout_register_net(struct net *net, |
1367 | struct rpc_pipe *pipe) | |
1368 | { | |
1369 | struct super_block *pipefs_sb; | |
1370 | struct dentry *dentry; | |
1371 | ||
1372 | pipefs_sb = rpc_get_sb_net(net); | |
1373 | if (!pipefs_sb) | |
2561d618 | 1374 | return NULL; |
332dfab6 SK |
1375 | dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
1376 | rpc_put_sb_net(net); | |
1377 | return dentry; | |
1378 | } | |
1379 | ||
1380 | static void nfs4blocklayout_unregister_net(struct net *net, | |
1381 | struct rpc_pipe *pipe) | |
1382 | { | |
1383 | struct super_block *pipefs_sb; | |
1384 | ||
1385 | pipefs_sb = rpc_get_sb_net(net); | |
1386 | if (pipefs_sb) { | |
1387 | nfs4blocklayout_unregister_sb(pipefs_sb, pipe); | |
1388 | rpc_put_sb_net(net); | |
1389 | } | |
1390 | } | |
1391 | ||
9e2e74db SK |
1392 | static int nfs4blocklayout_net_init(struct net *net) |
1393 | { | |
1394 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1395 | struct dentry *dentry; | |
1396 | ||
5ffaf855 | 1397 | init_waitqueue_head(&nn->bl_wq); |
9e2e74db SK |
1398 | nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
1399 | if (IS_ERR(nn->bl_device_pipe)) | |
1400 | return PTR_ERR(nn->bl_device_pipe); | |
1401 | dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe); | |
1402 | if (IS_ERR(dentry)) { | |
1403 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1404 | return PTR_ERR(dentry); | |
1405 | } | |
1406 | nn->bl_device_pipe->dentry = dentry; | |
1407 | return 0; | |
1408 | } | |
1409 | ||
1410 | static void nfs4blocklayout_net_exit(struct net *net) | |
1411 | { | |
1412 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1413 | ||
1414 | nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); | |
1415 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1416 | nn->bl_device_pipe = NULL; | |
1417 | } | |
1418 | ||
1419 | static struct pernet_operations nfs4blocklayout_net_ops = { | |
1420 | .init = nfs4blocklayout_net_init, | |
1421 | .exit = nfs4blocklayout_net_exit, | |
1422 | }; | |
1423 | ||
155e7524 FI |
1424 | static int __init nfs4blocklayout_init(void) |
1425 | { | |
1426 | int ret; | |
1427 | ||
1428 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); | |
1429 | ||
1430 | ret = pnfs_register_layoutdriver(&blocklayout_type); | |
fe0a9b74 JR |
1431 | if (ret) |
1432 | goto out; | |
1433 | ||
627f3066 | 1434 | ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
9e2e74db SK |
1435 | if (ret) |
1436 | goto out_remove; | |
627f3066 SK |
1437 | ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
1438 | if (ret) | |
1439 | goto out_notifier; | |
fe0a9b74 JR |
1440 | out: |
1441 | return ret; | |
1442 | ||
627f3066 SK |
1443 | out_notifier: |
1444 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); | |
fe0a9b74 JR |
1445 | out_remove: |
1446 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
155e7524 FI |
1447 | return ret; |
1448 | } | |
1449 | ||
1450 | static void __exit nfs4blocklayout_exit(void) | |
1451 | { | |
1452 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", | |
1453 | __func__); | |
1454 | ||
627f3066 | 1455 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
9e2e74db | 1456 | unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
155e7524 FI |
1457 | pnfs_unregister_layoutdriver(&blocklayout_type); |
1458 | } | |
1459 | ||
1460 | MODULE_ALIAS("nfs-layouttype4-3"); | |
1461 | ||
1462 | module_init(nfs4blocklayout_init); | |
1463 | module_exit(nfs4blocklayout_exit); |