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> |
155e7524 | 40 | |
10bd295a JR |
41 | #include "../pnfs.h" |
42 | #include "../internal.h" | |
155e7524 FI |
43 | #include "blocklayout.h" |
44 | ||
45 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD | |
46 | ||
47 | MODULE_LICENSE("GPL"); | |
48 | MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>"); | |
49 | MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver"); | |
50 | ||
9549ec01 FI |
51 | static void print_page(struct page *page) |
52 | { | |
53 | dprintk("PRINTPAGE page %p\n", page); | |
54 | dprintk(" PagePrivate %d\n", PagePrivate(page)); | |
55 | dprintk(" PageUptodate %d\n", PageUptodate(page)); | |
56 | dprintk(" PageError %d\n", PageError(page)); | |
57 | dprintk(" PageDirty %d\n", PageDirty(page)); | |
58 | dprintk(" PageReferenced %d\n", PageReferenced(page)); | |
59 | dprintk(" PageLocked %d\n", PageLocked(page)); | |
60 | dprintk(" PageWriteback %d\n", PageWriteback(page)); | |
61 | dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page)); | |
62 | dprintk("\n"); | |
63 | } | |
64 | ||
65 | /* Given the be associated with isect, determine if page data needs to be | |
66 | * initialized. | |
67 | */ | |
68 | static int is_hole(struct pnfs_block_extent *be, sector_t isect) | |
69 | { | |
70 | if (be->be_state == PNFS_BLOCK_NONE_DATA) | |
71 | return 1; | |
72 | else if (be->be_state != PNFS_BLOCK_INVALID_DATA) | |
73 | return 0; | |
74 | else | |
75 | return !bl_is_sector_init(be->be_inval, isect); | |
76 | } | |
77 | ||
650e2d39 FI |
78 | /* Given the be associated with isect, determine if page data can be |
79 | * written to disk. | |
80 | */ | |
81 | static int is_writable(struct pnfs_block_extent *be, sector_t isect) | |
82 | { | |
71cdd40f PT |
83 | return (be->be_state == PNFS_BLOCK_READWRITE_DATA || |
84 | be->be_state == PNFS_BLOCK_INVALID_DATA); | |
650e2d39 FI |
85 | } |
86 | ||
9549ec01 FI |
87 | /* The data we are handed might be spread across several bios. We need |
88 | * to track when the last one is finished. | |
89 | */ | |
90 | struct parallel_io { | |
91 | struct kref refcnt; | |
7c5465d6 | 92 | void (*pnfs_callback) (void *data, int num_se); |
9549ec01 | 93 | void *data; |
7c5465d6 | 94 | int bse_count; |
9549ec01 FI |
95 | }; |
96 | ||
97 | static inline struct parallel_io *alloc_parallel(void *data) | |
98 | { | |
99 | struct parallel_io *rv; | |
100 | ||
101 | rv = kmalloc(sizeof(*rv), GFP_NOFS); | |
102 | if (rv) { | |
103 | rv->data = data; | |
104 | kref_init(&rv->refcnt); | |
7c5465d6 | 105 | rv->bse_count = 0; |
9549ec01 FI |
106 | } |
107 | return rv; | |
108 | } | |
109 | ||
110 | static inline void get_parallel(struct parallel_io *p) | |
111 | { | |
112 | kref_get(&p->refcnt); | |
113 | } | |
114 | ||
115 | static void destroy_parallel(struct kref *kref) | |
116 | { | |
117 | struct parallel_io *p = container_of(kref, struct parallel_io, refcnt); | |
118 | ||
119 | dprintk("%s enter\n", __func__); | |
7c5465d6 | 120 | p->pnfs_callback(p->data, p->bse_count); |
9549ec01 FI |
121 | kfree(p); |
122 | } | |
123 | ||
124 | static inline void put_parallel(struct parallel_io *p) | |
125 | { | |
126 | kref_put(&p->refcnt, destroy_parallel); | |
127 | } | |
128 | ||
129 | static struct bio * | |
130 | bl_submit_bio(int rw, struct bio *bio) | |
131 | { | |
132 | if (bio) { | |
133 | get_parallel(bio->bi_private); | |
134 | dprintk("%s submitting %s bio %u@%llu\n", __func__, | |
135 | rw == READ ? "read" : "write", | |
136 | bio->bi_size, (unsigned long long)bio->bi_sector); | |
137 | submit_bio(rw, bio); | |
138 | } | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | static struct bio *bl_alloc_init_bio(int npg, sector_t isect, | |
143 | struct pnfs_block_extent *be, | |
144 | void (*end_io)(struct bio *, int err), | |
145 | struct parallel_io *par) | |
146 | { | |
147 | struct bio *bio; | |
148 | ||
74a6eeb4 | 149 | npg = min(npg, BIO_MAX_PAGES); |
9549ec01 | 150 | bio = bio_alloc(GFP_NOIO, npg); |
74a6eeb4 PT |
151 | if (!bio && (current->flags & PF_MEMALLOC)) { |
152 | while (!bio && (npg /= 2)) | |
153 | bio = bio_alloc(GFP_NOIO, npg); | |
154 | } | |
9549ec01 | 155 | |
74a6eeb4 PT |
156 | if (bio) { |
157 | bio->bi_sector = isect - be->be_f_offset + be->be_v_offset; | |
158 | bio->bi_bdev = be->be_mdev; | |
159 | bio->bi_end_io = end_io; | |
160 | bio->bi_private = par; | |
161 | } | |
9549ec01 FI |
162 | return bio; |
163 | } | |
164 | ||
fe6e1e8d | 165 | static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw, |
9549ec01 FI |
166 | sector_t isect, struct page *page, |
167 | struct pnfs_block_extent *be, | |
168 | void (*end_io)(struct bio *, int err), | |
fe6e1e8d PT |
169 | struct parallel_io *par, |
170 | unsigned int offset, int len) | |
9549ec01 | 171 | { |
fe6e1e8d PT |
172 | isect = isect + (offset >> SECTOR_SHIFT); |
173 | dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__, | |
174 | npg, rw, (unsigned long long)isect, offset, len); | |
9549ec01 FI |
175 | retry: |
176 | if (!bio) { | |
177 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); | |
178 | if (!bio) | |
179 | return ERR_PTR(-ENOMEM); | |
180 | } | |
fe6e1e8d | 181 | if (bio_add_page(bio, page, len, offset) < len) { |
9549ec01 FI |
182 | bio = bl_submit_bio(rw, bio); |
183 | goto retry; | |
184 | } | |
185 | return bio; | |
186 | } | |
187 | ||
fe6e1e8d PT |
188 | static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw, |
189 | sector_t isect, struct page *page, | |
190 | struct pnfs_block_extent *be, | |
191 | void (*end_io)(struct bio *, int err), | |
192 | struct parallel_io *par) | |
193 | { | |
194 | return do_add_page_to_bio(bio, npg, rw, isect, page, be, | |
195 | end_io, par, 0, PAGE_CACHE_SIZE); | |
196 | } | |
197 | ||
9549ec01 FI |
198 | /* This is basically copied from mpage_end_io_read */ |
199 | static void bl_end_io_read(struct bio *bio, int err) | |
200 | { | |
201 | struct parallel_io *par = bio->bi_private; | |
202 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
203 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
9549ec01 FI |
204 | |
205 | do { | |
206 | struct page *page = bvec->bv_page; | |
207 | ||
208 | if (--bvec >= bio->bi_io_vec) | |
209 | prefetchw(&bvec->bv_page->flags); | |
210 | if (uptodate) | |
211 | SetPageUptodate(page); | |
212 | } while (bvec >= bio->bi_io_vec); | |
213 | if (!uptodate) { | |
cd841605 FI |
214 | struct nfs_read_data *rdata = par->data; |
215 | struct nfs_pgio_header *header = rdata->header; | |
216 | ||
217 | if (!header->pnfs_error) | |
218 | header->pnfs_error = -EIO; | |
219 | pnfs_set_lo_fail(header->lseg); | |
9549ec01 FI |
220 | } |
221 | bio_put(bio); | |
222 | put_parallel(par); | |
223 | } | |
224 | ||
225 | static void bl_read_cleanup(struct work_struct *work) | |
226 | { | |
227 | struct rpc_task *task; | |
228 | struct nfs_read_data *rdata; | |
229 | dprintk("%s enter\n", __func__); | |
230 | task = container_of(work, struct rpc_task, u.tk_work); | |
231 | rdata = container_of(task, struct nfs_read_data, task); | |
232 | pnfs_ld_read_done(rdata); | |
233 | } | |
234 | ||
235 | static void | |
7c5465d6 | 236 | bl_end_par_io_read(void *data, int unused) |
9549ec01 FI |
237 | { |
238 | struct nfs_read_data *rdata = data; | |
239 | ||
cd841605 | 240 | rdata->task.tk_status = rdata->header->pnfs_error; |
9549ec01 FI |
241 | INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup); |
242 | schedule_work(&rdata->task.u.tk_work); | |
243 | } | |
244 | ||
155e7524 FI |
245 | static enum pnfs_try_status |
246 | bl_read_pagelist(struct nfs_read_data *rdata) | |
247 | { | |
cd841605 | 248 | struct nfs_pgio_header *header = rdata->header; |
9549ec01 FI |
249 | int i, hole; |
250 | struct bio *bio = NULL; | |
251 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; | |
252 | sector_t isect, extent_length = 0; | |
253 | struct parallel_io *par; | |
254 | loff_t f_offset = rdata->args.offset; | |
f742dc4a PT |
255 | size_t bytes_left = rdata->args.count; |
256 | unsigned int pg_offset, pg_len; | |
9549ec01 FI |
257 | struct page **pages = rdata->args.pages; |
258 | int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
f742dc4a | 259 | const bool is_dio = (header->dreq != NULL); |
9549ec01 | 260 | |
6f00866d | 261 | dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__, |
30dd374f | 262 | rdata->pages.npages, f_offset, (unsigned int)rdata->args.count); |
9549ec01 FI |
263 | |
264 | par = alloc_parallel(rdata); | |
265 | if (!par) | |
266 | goto use_mds; | |
9549ec01 FI |
267 | par->pnfs_callback = bl_end_par_io_read; |
268 | /* At this point, we can no longer jump to use_mds */ | |
269 | ||
270 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); | |
271 | /* Code assumes extents are page-aligned */ | |
30dd374f | 272 | for (i = pg_index; i < rdata->pages.npages; i++) { |
9549ec01 FI |
273 | if (!extent_length) { |
274 | /* We've used up the previous extent */ | |
275 | bl_put_extent(be); | |
276 | bl_put_extent(cow_read); | |
277 | bio = bl_submit_bio(READ, bio); | |
278 | /* Get the next one */ | |
cd841605 | 279 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
9549ec01 FI |
280 | isect, &cow_read); |
281 | if (!be) { | |
cd841605 | 282 | header->pnfs_error = -EIO; |
9549ec01 FI |
283 | goto out; |
284 | } | |
285 | extent_length = be->be_length - | |
286 | (isect - be->be_f_offset); | |
287 | if (cow_read) { | |
288 | sector_t cow_length = cow_read->be_length - | |
289 | (isect - cow_read->be_f_offset); | |
290 | extent_length = min(extent_length, cow_length); | |
291 | } | |
292 | } | |
f742dc4a PT |
293 | |
294 | if (is_dio) { | |
295 | pg_offset = f_offset & ~PAGE_CACHE_MASK; | |
296 | if (pg_offset + bytes_left > PAGE_CACHE_SIZE) | |
297 | pg_len = PAGE_CACHE_SIZE - pg_offset; | |
298 | else | |
299 | pg_len = bytes_left; | |
300 | ||
301 | f_offset += pg_len; | |
302 | bytes_left -= pg_len; | |
303 | isect += (pg_offset >> SECTOR_SHIFT); | |
304 | } else { | |
305 | pg_offset = 0; | |
306 | pg_len = PAGE_CACHE_SIZE; | |
307 | } | |
308 | ||
9549ec01 FI |
309 | hole = is_hole(be, isect); |
310 | if (hole && !cow_read) { | |
311 | bio = bl_submit_bio(READ, bio); | |
312 | /* Fill hole w/ zeroes w/o accessing device */ | |
313 | dprintk("%s Zeroing page for hole\n", __func__); | |
f742dc4a | 314 | zero_user_segment(pages[i], pg_offset, pg_len); |
9549ec01 FI |
315 | print_page(pages[i]); |
316 | SetPageUptodate(pages[i]); | |
317 | } else { | |
318 | struct pnfs_block_extent *be_read; | |
319 | ||
320 | be_read = (hole && cow_read) ? cow_read : be; | |
f742dc4a | 321 | bio = do_add_page_to_bio(bio, rdata->pages.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) { |
9549ec01 | 336 | rdata->res.eof = 1; |
f742dc4a | 337 | rdata->res.count = header->inode->i_size - rdata->args.offset; |
9549ec01 | 338 | } else { |
f742dc4a | 339 | rdata->res.count = (isect << SECTOR_SHIFT) - rdata->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; | |
384 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
385 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
71cdd40f PT |
386 | |
387 | do { | |
388 | struct page *page = bvec->bv_page; | |
389 | ||
390 | if (--bvec >= bio->bi_io_vec) | |
391 | prefetchw(&bvec->bv_page->flags); | |
392 | /* This is the zeroing page we added */ | |
393 | end_page_writeback(page); | |
394 | page_cache_release(page); | |
395 | } while (bvec >= bio->bi_io_vec); | |
7c5465d6 PT |
396 | |
397 | if (unlikely(!uptodate)) { | |
cd841605 FI |
398 | struct nfs_write_data *data = par->data; |
399 | struct nfs_pgio_header *header = data->header; | |
400 | ||
401 | if (!header->pnfs_error) | |
402 | header->pnfs_error = -EIO; | |
403 | pnfs_set_lo_fail(header->lseg); | |
71cdd40f PT |
404 | } |
405 | bio_put(bio); | |
406 | put_parallel(par); | |
407 | } | |
408 | ||
650e2d39 FI |
409 | static void bl_end_io_write(struct bio *bio, int err) |
410 | { | |
411 | struct parallel_io *par = bio->bi_private; | |
412 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
cd841605 FI |
413 | struct nfs_write_data *data = par->data; |
414 | struct nfs_pgio_header *header = data->header; | |
650e2d39 FI |
415 | |
416 | if (!uptodate) { | |
cd841605 FI |
417 | if (!header->pnfs_error) |
418 | header->pnfs_error = -EIO; | |
419 | pnfs_set_lo_fail(header->lseg); | |
650e2d39 FI |
420 | } |
421 | bio_put(bio); | |
422 | put_parallel(par); | |
423 | } | |
424 | ||
425 | /* Function scheduled for call during bl_end_par_io_write, | |
426 | * it marks sectors as written and extends the commitlist. | |
427 | */ | |
428 | static void bl_write_cleanup(struct work_struct *work) | |
429 | { | |
430 | struct rpc_task *task; | |
431 | struct nfs_write_data *wdata; | |
432 | dprintk("%s enter\n", __func__); | |
433 | task = container_of(work, struct rpc_task, u.tk_work); | |
434 | wdata = container_of(task, struct nfs_write_data, task); | |
cd841605 | 435 | if (likely(!wdata->header->pnfs_error)) { |
31e6306a | 436 | /* Marks for LAYOUTCOMMIT */ |
cd841605 | 437 | mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg), |
31e6306a FI |
438 | wdata->args.offset, wdata->args.count); |
439 | } | |
650e2d39 FI |
440 | pnfs_ld_write_done(wdata); |
441 | } | |
442 | ||
443 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ | |
7c5465d6 | 444 | static void bl_end_par_io_write(void *data, int num_se) |
650e2d39 FI |
445 | { |
446 | struct nfs_write_data *wdata = data; | |
447 | ||
cd841605 FI |
448 | if (unlikely(wdata->header->pnfs_error)) { |
449 | bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval, | |
7c5465d6 PT |
450 | num_se); |
451 | } | |
452 | ||
cd841605 | 453 | wdata->task.tk_status = wdata->header->pnfs_error; |
650e2d39 FI |
454 | wdata->verf.committed = NFS_FILE_SYNC; |
455 | INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup); | |
456 | schedule_work(&wdata->task.u.tk_work); | |
457 | } | |
458 | ||
71cdd40f PT |
459 | /* FIXME STUB - mark intersection of layout and page as bad, so is not |
460 | * used again. | |
461 | */ | |
462 | static void mark_bad_read(void) | |
463 | { | |
464 | return; | |
465 | } | |
466 | ||
467 | /* | |
468 | * map_block: map a requested I/0 block (isect) into an offset in the LVM | |
469 | * block_device | |
470 | */ | |
471 | static void | |
472 | map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be) | |
473 | { | |
474 | dprintk("%s enter be=%p\n", __func__, be); | |
475 | ||
476 | set_buffer_mapped(bh); | |
477 | bh->b_bdev = be->be_mdev; | |
478 | bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >> | |
479 | (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT); | |
480 | ||
481 | dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n", | |
482 | __func__, (unsigned long long)isect, (long)bh->b_blocknr, | |
483 | bh->b_size); | |
484 | return; | |
485 | } | |
486 | ||
fe6e1e8d PT |
487 | static void |
488 | bl_read_single_end_io(struct bio *bio, int error) | |
489 | { | |
490 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
491 | struct page *page = bvec->bv_page; | |
492 | ||
493 | /* Only one page in bvec */ | |
494 | unlock_page(page); | |
495 | } | |
496 | ||
497 | static int | |
498 | bl_do_readpage_sync(struct page *page, struct pnfs_block_extent *be, | |
499 | unsigned int offset, unsigned int len) | |
500 | { | |
501 | struct bio *bio; | |
502 | struct page *shadow_page; | |
503 | sector_t isect; | |
504 | char *kaddr, *kshadow_addr; | |
505 | int ret = 0; | |
506 | ||
507 | dprintk("%s: offset %u len %u\n", __func__, offset, len); | |
508 | ||
509 | shadow_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
510 | if (shadow_page == NULL) | |
511 | return -ENOMEM; | |
512 | ||
513 | bio = bio_alloc(GFP_NOIO, 1); | |
514 | if (bio == NULL) | |
515 | return -ENOMEM; | |
516 | ||
517 | isect = (page->index << PAGE_CACHE_SECTOR_SHIFT) + | |
518 | (offset / SECTOR_SIZE); | |
519 | ||
520 | bio->bi_sector = isect - be->be_f_offset + be->be_v_offset; | |
521 | bio->bi_bdev = be->be_mdev; | |
522 | bio->bi_end_io = bl_read_single_end_io; | |
523 | ||
524 | lock_page(shadow_page); | |
525 | if (bio_add_page(bio, shadow_page, | |
526 | SECTOR_SIZE, round_down(offset, SECTOR_SIZE)) == 0) { | |
527 | unlock_page(shadow_page); | |
528 | bio_put(bio); | |
529 | return -EIO; | |
530 | } | |
531 | ||
532 | submit_bio(READ, bio); | |
533 | wait_on_page_locked(shadow_page); | |
534 | if (unlikely(!test_bit(BIO_UPTODATE, &bio->bi_flags))) { | |
535 | ret = -EIO; | |
536 | } else { | |
537 | kaddr = kmap_atomic(page); | |
538 | kshadow_addr = kmap_atomic(shadow_page); | |
539 | memcpy(kaddr + offset, kshadow_addr + offset, len); | |
540 | kunmap_atomic(kshadow_addr); | |
541 | kunmap_atomic(kaddr); | |
542 | } | |
543 | __free_page(shadow_page); | |
544 | bio_put(bio); | |
545 | ||
546 | return ret; | |
547 | } | |
548 | ||
549 | static int | |
550 | bl_read_partial_page_sync(struct page *page, struct pnfs_block_extent *be, | |
551 | unsigned int dirty_offset, unsigned int dirty_len, | |
552 | bool full_page) | |
553 | { | |
554 | int ret = 0; | |
555 | unsigned int start, end; | |
556 | ||
557 | if (full_page) { | |
558 | start = 0; | |
559 | end = PAGE_CACHE_SIZE; | |
560 | } else { | |
561 | start = round_down(dirty_offset, SECTOR_SIZE); | |
562 | end = round_up(dirty_offset + dirty_len, SECTOR_SIZE); | |
563 | } | |
564 | ||
565 | dprintk("%s: offset %u len %d\n", __func__, dirty_offset, dirty_len); | |
566 | if (!be) { | |
567 | zero_user_segments(page, start, dirty_offset, | |
568 | dirty_offset + dirty_len, end); | |
569 | if (start == 0 && end == PAGE_CACHE_SIZE && | |
570 | trylock_page(page)) { | |
571 | SetPageUptodate(page); | |
572 | unlock_page(page); | |
573 | } | |
574 | return ret; | |
575 | } | |
576 | ||
577 | if (start != dirty_offset) | |
578 | ret = bl_do_readpage_sync(page, be, start, dirty_offset - start); | |
579 | ||
580 | if (!ret && (dirty_offset + dirty_len < end)) | |
581 | ret = bl_do_readpage_sync(page, be, dirty_offset + dirty_len, | |
582 | end - dirty_offset - dirty_len); | |
583 | ||
584 | return ret; | |
585 | } | |
586 | ||
71cdd40f PT |
587 | /* Given an unmapped page, zero it or read in page for COW, page is locked |
588 | * by caller. | |
589 | */ | |
590 | static int | |
591 | init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read) | |
592 | { | |
593 | struct buffer_head *bh = NULL; | |
594 | int ret = 0; | |
595 | sector_t isect; | |
596 | ||
597 | dprintk("%s enter, %p\n", __func__, page); | |
598 | BUG_ON(PageUptodate(page)); | |
599 | if (!cow_read) { | |
600 | zero_user_segment(page, 0, PAGE_SIZE); | |
601 | SetPageUptodate(page); | |
602 | goto cleanup; | |
603 | } | |
604 | ||
605 | bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0); | |
606 | if (!bh) { | |
607 | ret = -ENOMEM; | |
608 | goto cleanup; | |
609 | } | |
610 | ||
611 | isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT; | |
612 | map_block(bh, isect, cow_read); | |
613 | if (!bh_uptodate_or_lock(bh)) | |
614 | ret = bh_submit_read(bh); | |
615 | if (ret) | |
616 | goto cleanup; | |
617 | SetPageUptodate(page); | |
618 | ||
619 | cleanup: | |
71cdd40f PT |
620 | if (bh) |
621 | free_buffer_head(bh); | |
622 | if (ret) { | |
623 | /* Need to mark layout with bad read...should now | |
624 | * just use nfs4 for reads and writes. | |
625 | */ | |
626 | mark_bad_read(); | |
627 | } | |
628 | return ret; | |
629 | } | |
630 | ||
72c50887 PT |
631 | /* Find or create a zeroing page marked being writeback. |
632 | * Return ERR_PTR on error, NULL to indicate skip this page and page itself | |
633 | * to indicate write out. | |
634 | */ | |
635 | static struct page * | |
636 | bl_find_get_zeroing_page(struct inode *inode, pgoff_t index, | |
637 | struct pnfs_block_extent *cow_read) | |
638 | { | |
639 | struct page *page; | |
640 | int locked = 0; | |
641 | page = find_get_page(inode->i_mapping, index); | |
642 | if (page) | |
643 | goto check_page; | |
644 | ||
645 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
646 | if (unlikely(!page)) { | |
647 | dprintk("%s oom\n", __func__); | |
648 | return ERR_PTR(-ENOMEM); | |
649 | } | |
650 | locked = 1; | |
651 | ||
652 | check_page: | |
653 | /* PageDirty: Other will write this out | |
654 | * PageWriteback: Other is writing this out | |
655 | * PageUptodate: It was read before | |
656 | */ | |
657 | if (PageDirty(page) || PageWriteback(page)) { | |
658 | print_page(page); | |
659 | if (locked) | |
660 | unlock_page(page); | |
661 | page_cache_release(page); | |
662 | return NULL; | |
663 | } | |
664 | ||
665 | if (!locked) { | |
666 | lock_page(page); | |
667 | locked = 1; | |
668 | goto check_page; | |
669 | } | |
670 | if (!PageUptodate(page)) { | |
671 | /* New page, readin or zero it */ | |
672 | init_page_for_write(page, cow_read); | |
673 | } | |
674 | set_page_writeback(page); | |
675 | unlock_page(page); | |
676 | ||
677 | return page; | |
678 | } | |
679 | ||
155e7524 | 680 | static enum pnfs_try_status |
650e2d39 | 681 | bl_write_pagelist(struct nfs_write_data *wdata, int sync) |
155e7524 | 682 | { |
cd841605 | 683 | struct nfs_pgio_header *header = wdata->header; |
71cdd40f | 684 | int i, ret, npg_zero, pg_index, last = 0; |
650e2d39 | 685 | struct bio *bio = NULL; |
71cdd40f PT |
686 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
687 | sector_t isect, last_isect = 0, extent_length = 0; | |
5d0e3a00 | 688 | struct parallel_io *par; |
650e2d39 FI |
689 | loff_t offset = wdata->args.offset; |
690 | size_t count = wdata->args.count; | |
fe6e1e8d | 691 | unsigned int pg_offset, pg_len, saved_len; |
650e2d39 | 692 | struct page **pages = wdata->args.pages; |
71cdd40f PT |
693 | struct page *page; |
694 | pgoff_t index; | |
695 | u64 temp; | |
696 | int npg_per_block = | |
cd841605 | 697 | NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT; |
650e2d39 FI |
698 | |
699 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); | |
700 | /* At this point, wdata->pages 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 FI |
703 | */ |
704 | par = alloc_parallel(wdata); | |
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 */ | |
791 | pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
30dd374f | 792 | for (i = pg_index; i < wdata->pages.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 | ||
863 | bio = do_add_page_to_bio(bio, wdata->pages.npages - i, WRITE, | |
71cdd40f | 864 | isect, pages[i], be, |
fe6e1e8d PT |
865 | bl_end_io_write, par, |
866 | pg_offset, pg_len); | |
71cdd40f | 867 | if (IS_ERR(bio)) { |
cd841605 | 868 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 869 | bio = NULL; |
71cdd40f | 870 | goto out; |
650e2d39 | 871 | } |
fe6e1e8d PT |
872 | offset += saved_len; |
873 | count -= saved_len; | |
650e2d39 | 874 | isect += PAGE_CACHE_SECTORS; |
71cdd40f | 875 | last_isect = isect; |
650e2d39 FI |
876 | extent_length -= PAGE_CACHE_SECTORS; |
877 | } | |
71cdd40f PT |
878 | |
879 | /* Last page inside INVALID extent */ | |
880 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
881 | bio = bl_submit_bio(WRITE, bio); | |
882 | temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT; | |
883 | npg_zero = npg_per_block - do_div(temp, npg_per_block); | |
884 | if (npg_zero < npg_per_block) { | |
885 | last = 1; | |
886 | goto fill_invalid_ext; | |
887 | } | |
888 | } | |
889 | ||
890 | write_done: | |
fe6e1e8d | 891 | wdata->res.count = wdata->args.count; |
650e2d39 FI |
892 | out: |
893 | bl_put_extent(be); | |
fe6e1e8d | 894 | bl_put_extent(cow_read); |
650e2d39 FI |
895 | bl_submit_bio(WRITE, bio); |
896 | put_parallel(par); | |
897 | return PNFS_ATTEMPTED; | |
7c5465d6 PT |
898 | out_mds: |
899 | bl_put_extent(be); | |
fe6e1e8d | 900 | bl_put_extent(cow_read); |
7c5465d6 PT |
901 | kfree(par); |
902 | return PNFS_NOT_ATTEMPTED; | |
155e7524 FI |
903 | } |
904 | ||
9e692969 | 905 | /* FIXME - range ignored */ |
155e7524 | 906 | static void |
9e692969 | 907 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
155e7524 | 908 | { |
9e692969 FI |
909 | int i; |
910 | struct pnfs_block_extent *be; | |
911 | ||
912 | spin_lock(&bl->bl_ext_lock); | |
913 | for (i = 0; i < EXTENT_LISTS; i++) { | |
914 | while (!list_empty(&bl->bl_extents[i])) { | |
915 | be = list_first_entry(&bl->bl_extents[i], | |
916 | struct pnfs_block_extent, | |
917 | be_node); | |
918 | list_del(&be->be_node); | |
919 | bl_put_extent(be); | |
920 | } | |
921 | } | |
922 | spin_unlock(&bl->bl_ext_lock); | |
155e7524 FI |
923 | } |
924 | ||
155e7524 FI |
925 | static void |
926 | release_inval_marks(struct pnfs_inval_markings *marks) | |
927 | { | |
c1c2a4cd | 928 | struct pnfs_inval_tracking *pos, *temp; |
7c5465d6 | 929 | struct pnfs_block_short_extent *se, *stemp; |
c1c2a4cd FI |
930 | |
931 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { | |
932 | list_del(&pos->it_link); | |
933 | kfree(pos); | |
934 | } | |
7c5465d6 PT |
935 | |
936 | list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) { | |
937 | list_del(&se->bse_node); | |
938 | kfree(se); | |
939 | } | |
155e7524 FI |
940 | return; |
941 | } | |
942 | ||
943 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
944 | { | |
945 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); | |
946 | ||
947 | dprintk("%s enter\n", __func__); | |
948 | release_extents(bl, NULL); | |
949 | release_inval_marks(&bl->bl_inval); | |
950 | kfree(bl); | |
951 | } | |
952 | ||
953 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, | |
954 | gfp_t gfp_flags) | |
955 | { | |
956 | struct pnfs_block_layout *bl; | |
957 | ||
958 | dprintk("%s enter\n", __func__); | |
959 | bl = kzalloc(sizeof(*bl), gfp_flags); | |
960 | if (!bl) | |
961 | return NULL; | |
962 | spin_lock_init(&bl->bl_ext_lock); | |
963 | INIT_LIST_HEAD(&bl->bl_extents[0]); | |
964 | INIT_LIST_HEAD(&bl->bl_extents[1]); | |
965 | INIT_LIST_HEAD(&bl->bl_commit); | |
966 | INIT_LIST_HEAD(&bl->bl_committing); | |
967 | bl->bl_count = 0; | |
968 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; | |
969 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); | |
970 | return &bl->bl_layout; | |
971 | } | |
972 | ||
a60d2ebd | 973 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
155e7524 | 974 | { |
a60d2ebd FI |
975 | dprintk("%s enter\n", __func__); |
976 | kfree(lseg); | |
155e7524 FI |
977 | } |
978 | ||
a60d2ebd FI |
979 | /* We pretty much ignore lseg, and store all data layout wide, so we |
980 | * can correctly merge. | |
981 | */ | |
982 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, | |
983 | struct nfs4_layoutget_res *lgr, | |
984 | gfp_t gfp_flags) | |
155e7524 | 985 | { |
a60d2ebd FI |
986 | struct pnfs_layout_segment *lseg; |
987 | int status; | |
988 | ||
989 | dprintk("%s enter\n", __func__); | |
990 | lseg = kzalloc(sizeof(*lseg), gfp_flags); | |
991 | if (!lseg) | |
992 | return ERR_PTR(-ENOMEM); | |
993 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); | |
994 | if (status) { | |
995 | /* We don't want to call the full-blown bl_free_lseg, | |
996 | * since on error extents were not touched. | |
997 | */ | |
998 | kfree(lseg); | |
999 | return ERR_PTR(status); | |
1000 | } | |
1001 | return lseg; | |
155e7524 FI |
1002 | } |
1003 | ||
1004 | static void | |
1005 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, | |
1006 | const struct nfs4_layoutcommit_args *arg) | |
1007 | { | |
90ace12a FI |
1008 | dprintk("%s enter\n", __func__); |
1009 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); | |
155e7524 FI |
1010 | } |
1011 | ||
1012 | static void | |
1013 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) | |
1014 | { | |
b2be7811 FI |
1015 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
1016 | ||
1017 | dprintk("%s enter\n", __func__); | |
1018 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); | |
155e7524 FI |
1019 | } |
1020 | ||
2f9fd182 FI |
1021 | static void free_blk_mountid(struct block_mount_id *mid) |
1022 | { | |
1023 | if (mid) { | |
93a3844e PT |
1024 | struct pnfs_block_dev *dev, *tmp; |
1025 | ||
1026 | /* No need to take bm_lock as we are last user freeing bm_devlist */ | |
1027 | list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) { | |
2f9fd182 FI |
1028 | list_del(&dev->bm_node); |
1029 | bl_free_block_dev(dev); | |
1030 | } | |
2f9fd182 FI |
1031 | kfree(mid); |
1032 | } | |
1033 | } | |
1034 | ||
78e4e05c | 1035 | /* This is mostly copied from the filelayout_get_device_info function. |
2f9fd182 FI |
1036 | * It seems much of this should be at the generic pnfs level. |
1037 | */ | |
1038 | static struct pnfs_block_dev * | |
1039 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, | |
1040 | struct nfs4_deviceid *d_id) | |
1041 | { | |
1042 | struct pnfs_device *dev; | |
516f2e24 | 1043 | struct pnfs_block_dev *rv; |
2f9fd182 FI |
1044 | u32 max_resp_sz; |
1045 | int max_pages; | |
1046 | struct page **pages = NULL; | |
1047 | int i, rc; | |
1048 | ||
1049 | /* | |
1050 | * Use the session max response size as the basis for setting | |
1051 | * GETDEVICEINFO's maxcount | |
1052 | */ | |
1053 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
10bd295a | 1054 | max_pages = nfs_page_array_len(0, max_resp_sz); |
2f9fd182 FI |
1055 | dprintk("%s max_resp_sz %u max_pages %d\n", |
1056 | __func__, max_resp_sz, max_pages); | |
1057 | ||
1058 | dev = kmalloc(sizeof(*dev), GFP_NOFS); | |
1059 | if (!dev) { | |
1060 | dprintk("%s kmalloc failed\n", __func__); | |
516f2e24 | 1061 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
1062 | } |
1063 | ||
1064 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS); | |
1065 | if (pages == NULL) { | |
1066 | kfree(dev); | |
516f2e24 | 1067 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
1068 | } |
1069 | for (i = 0; i < max_pages; i++) { | |
1070 | pages[i] = alloc_page(GFP_NOFS); | |
516f2e24 JR |
1071 | if (!pages[i]) { |
1072 | rv = ERR_PTR(-ENOMEM); | |
2f9fd182 | 1073 | goto out_free; |
516f2e24 | 1074 | } |
2f9fd182 FI |
1075 | } |
1076 | ||
1077 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); | |
1078 | dev->layout_type = LAYOUT_BLOCK_VOLUME; | |
1079 | dev->pages = pages; | |
1080 | dev->pgbase = 0; | |
1081 | dev->pglen = PAGE_SIZE * max_pages; | |
1082 | dev->mincount = 0; | |
1083 | ||
1084 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); | |
1085 | rc = nfs4_proc_getdeviceinfo(server, dev); | |
1086 | dprintk("%s getdevice info returns %d\n", __func__, rc); | |
516f2e24 JR |
1087 | if (rc) { |
1088 | rv = ERR_PTR(rc); | |
2f9fd182 | 1089 | goto out_free; |
516f2e24 | 1090 | } |
2f9fd182 FI |
1091 | |
1092 | rv = nfs4_blk_decode_device(server, dev); | |
1093 | out_free: | |
1094 | for (i = 0; i < max_pages; i++) | |
1095 | __free_page(pages[i]); | |
1096 | kfree(pages); | |
1097 | kfree(dev); | |
1098 | return rv; | |
1099 | } | |
1100 | ||
155e7524 FI |
1101 | static int |
1102 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) | |
1103 | { | |
2f9fd182 FI |
1104 | struct block_mount_id *b_mt_id = NULL; |
1105 | struct pnfs_devicelist *dlist = NULL; | |
1106 | struct pnfs_block_dev *bdev; | |
1107 | LIST_HEAD(block_disklist); | |
516f2e24 | 1108 | int status, i; |
2f9fd182 | 1109 | |
155e7524 | 1110 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
1111 | |
1112 | if (server->pnfs_blksize == 0) { | |
1113 | dprintk("%s Server did not return blksize\n", __func__); | |
1114 | return -EINVAL; | |
1115 | } | |
1116 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); | |
1117 | if (!b_mt_id) { | |
1118 | status = -ENOMEM; | |
1119 | goto out_error; | |
1120 | } | |
1121 | /* Initialize nfs4 block layout mount id */ | |
1122 | spin_lock_init(&b_mt_id->bm_lock); | |
1123 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); | |
1124 | ||
1125 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); | |
1126 | if (!dlist) { | |
1127 | status = -ENOMEM; | |
1128 | goto out_error; | |
1129 | } | |
1130 | dlist->eof = 0; | |
1131 | while (!dlist->eof) { | |
1132 | status = nfs4_proc_getdevicelist(server, fh, dlist); | |
1133 | if (status) | |
1134 | goto out_error; | |
1135 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", | |
1136 | __func__, dlist->num_devs, dlist->eof); | |
1137 | for (i = 0; i < dlist->num_devs; i++) { | |
1138 | bdev = nfs4_blk_get_deviceinfo(server, fh, | |
1139 | &dlist->dev_id[i]); | |
516f2e24 JR |
1140 | if (IS_ERR(bdev)) { |
1141 | status = PTR_ERR(bdev); | |
2f9fd182 FI |
1142 | goto out_error; |
1143 | } | |
1144 | spin_lock(&b_mt_id->bm_lock); | |
1145 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); | |
1146 | spin_unlock(&b_mt_id->bm_lock); | |
1147 | } | |
1148 | } | |
1149 | dprintk("%s SUCCESS\n", __func__); | |
1150 | server->pnfs_ld_data = b_mt_id; | |
1151 | ||
1152 | out_return: | |
1153 | kfree(dlist); | |
1154 | return status; | |
1155 | ||
1156 | out_error: | |
1157 | free_blk_mountid(b_mt_id); | |
1158 | goto out_return; | |
155e7524 FI |
1159 | } |
1160 | ||
1161 | static int | |
1162 | bl_clear_layoutdriver(struct nfs_server *server) | |
1163 | { | |
2f9fd182 FI |
1164 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
1165 | ||
155e7524 | 1166 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
1167 | free_blk_mountid(b_mt_id); |
1168 | dprintk("%s RETURNS\n", __func__); | |
155e7524 FI |
1169 | return 0; |
1170 | } | |
1171 | ||
f742dc4a PT |
1172 | static bool |
1173 | is_aligned_req(struct nfs_page *req, unsigned int alignment) | |
1174 | { | |
1175 | return IS_ALIGNED(req->wb_offset, alignment) && | |
1176 | IS_ALIGNED(req->wb_bytes, alignment); | |
1177 | } | |
1178 | ||
1179 | static void | |
1180 | bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1181 | { | |
1182 | if (pgio->pg_dreq != NULL && | |
1183 | !is_aligned_req(req, SECTOR_SIZE)) | |
1184 | nfs_pageio_reset_read_mds(pgio); | |
1185 | else | |
1186 | pnfs_generic_pg_init_read(pgio, req); | |
1187 | } | |
1188 | ||
1189 | static bool | |
1190 | bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, | |
1191 | struct nfs_page *req) | |
1192 | { | |
1193 | if (pgio->pg_dreq != NULL && | |
1194 | !is_aligned_req(req, SECTOR_SIZE)) | |
1195 | return false; | |
1196 | ||
1197 | return pnfs_generic_pg_test(pgio, prev, req); | |
1198 | } | |
1199 | ||
e9643fe8 | 1200 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
f742dc4a PT |
1201 | .pg_init = bl_pg_init_read, |
1202 | .pg_test = bl_pg_test_read, | |
e9643fe8 BH |
1203 | .pg_doio = pnfs_generic_pg_readpages, |
1204 | }; | |
1205 | ||
1206 | static const struct nfs_pageio_ops bl_pg_write_ops = { | |
5d0e3a00 | 1207 | .pg_init = pnfs_generic_pg_init_write, |
e9643fe8 BH |
1208 | .pg_test = pnfs_generic_pg_test, |
1209 | .pg_doio = pnfs_generic_pg_writepages, | |
1210 | }; | |
1211 | ||
155e7524 FI |
1212 | static struct pnfs_layoutdriver_type blocklayout_type = { |
1213 | .id = LAYOUT_BLOCK_VOLUME, | |
1214 | .name = "LAYOUT_BLOCK_VOLUME", | |
1215 | .read_pagelist = bl_read_pagelist, | |
1216 | .write_pagelist = bl_write_pagelist, | |
1217 | .alloc_layout_hdr = bl_alloc_layout_hdr, | |
1218 | .free_layout_hdr = bl_free_layout_hdr, | |
1219 | .alloc_lseg = bl_alloc_lseg, | |
1220 | .free_lseg = bl_free_lseg, | |
1221 | .encode_layoutcommit = bl_encode_layoutcommit, | |
1222 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, | |
1223 | .set_layoutdriver = bl_set_layoutdriver, | |
1224 | .clear_layoutdriver = bl_clear_layoutdriver, | |
e9643fe8 BH |
1225 | .pg_read_ops = &bl_pg_read_ops, |
1226 | .pg_write_ops = &bl_pg_write_ops, | |
155e7524 FI |
1227 | }; |
1228 | ||
fe0a9b74 | 1229 | static const struct rpc_pipe_ops bl_upcall_ops = { |
c1225158 | 1230 | .upcall = rpc_pipe_generic_upcall, |
fe0a9b74 JR |
1231 | .downcall = bl_pipe_downcall, |
1232 | .destroy_msg = bl_pipe_destroy_msg, | |
1233 | }; | |
1234 | ||
332dfab6 SK |
1235 | static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb, |
1236 | struct rpc_pipe *pipe) | |
1237 | { | |
1238 | struct dentry *dir, *dentry; | |
1239 | ||
1240 | dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); | |
1241 | if (dir == NULL) | |
1242 | return ERR_PTR(-ENOENT); | |
1243 | dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); | |
1244 | dput(dir); | |
1245 | return dentry; | |
1246 | } | |
1247 | ||
1248 | static void nfs4blocklayout_unregister_sb(struct super_block *sb, | |
1249 | struct rpc_pipe *pipe) | |
1250 | { | |
1251 | if (pipe->dentry) | |
1252 | rpc_unlink(pipe->dentry); | |
1253 | } | |
1254 | ||
627f3066 SK |
1255 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
1256 | void *ptr) | |
1257 | { | |
1258 | struct super_block *sb = ptr; | |
1259 | struct net *net = sb->s_fs_info; | |
1260 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1261 | struct dentry *dentry; | |
1262 | int ret = 0; | |
1263 | ||
1264 | if (!try_module_get(THIS_MODULE)) | |
1265 | return 0; | |
1266 | ||
1267 | if (nn->bl_device_pipe == NULL) { | |
1268 | module_put(THIS_MODULE); | |
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | switch (event) { | |
1273 | case RPC_PIPEFS_MOUNT: | |
1274 | dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); | |
1275 | if (IS_ERR(dentry)) { | |
1276 | ret = PTR_ERR(dentry); | |
1277 | break; | |
1278 | } | |
1279 | nn->bl_device_pipe->dentry = dentry; | |
1280 | break; | |
1281 | case RPC_PIPEFS_UMOUNT: | |
1282 | if (nn->bl_device_pipe->dentry) | |
1283 | nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe); | |
1284 | break; | |
1285 | default: | |
1286 | ret = -ENOTSUPP; | |
1287 | break; | |
1288 | } | |
1289 | module_put(THIS_MODULE); | |
1290 | return ret; | |
1291 | } | |
1292 | ||
1293 | static struct notifier_block nfs4blocklayout_block = { | |
1294 | .notifier_call = rpc_pipefs_event, | |
1295 | }; | |
1296 | ||
332dfab6 SK |
1297 | static struct dentry *nfs4blocklayout_register_net(struct net *net, |
1298 | struct rpc_pipe *pipe) | |
1299 | { | |
1300 | struct super_block *pipefs_sb; | |
1301 | struct dentry *dentry; | |
1302 | ||
1303 | pipefs_sb = rpc_get_sb_net(net); | |
1304 | if (!pipefs_sb) | |
2561d618 | 1305 | return NULL; |
332dfab6 SK |
1306 | dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
1307 | rpc_put_sb_net(net); | |
1308 | return dentry; | |
1309 | } | |
1310 | ||
1311 | static void nfs4blocklayout_unregister_net(struct net *net, | |
1312 | struct rpc_pipe *pipe) | |
1313 | { | |
1314 | struct super_block *pipefs_sb; | |
1315 | ||
1316 | pipefs_sb = rpc_get_sb_net(net); | |
1317 | if (pipefs_sb) { | |
1318 | nfs4blocklayout_unregister_sb(pipefs_sb, pipe); | |
1319 | rpc_put_sb_net(net); | |
1320 | } | |
1321 | } | |
1322 | ||
9e2e74db SK |
1323 | static int nfs4blocklayout_net_init(struct net *net) |
1324 | { | |
1325 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1326 | struct dentry *dentry; | |
1327 | ||
5ffaf855 | 1328 | init_waitqueue_head(&nn->bl_wq); |
9e2e74db SK |
1329 | nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
1330 | if (IS_ERR(nn->bl_device_pipe)) | |
1331 | return PTR_ERR(nn->bl_device_pipe); | |
1332 | dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe); | |
1333 | if (IS_ERR(dentry)) { | |
1334 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1335 | return PTR_ERR(dentry); | |
1336 | } | |
1337 | nn->bl_device_pipe->dentry = dentry; | |
1338 | return 0; | |
1339 | } | |
1340 | ||
1341 | static void nfs4blocklayout_net_exit(struct net *net) | |
1342 | { | |
1343 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1344 | ||
1345 | nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); | |
1346 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1347 | nn->bl_device_pipe = NULL; | |
1348 | } | |
1349 | ||
1350 | static struct pernet_operations nfs4blocklayout_net_ops = { | |
1351 | .init = nfs4blocklayout_net_init, | |
1352 | .exit = nfs4blocklayout_net_exit, | |
1353 | }; | |
1354 | ||
155e7524 FI |
1355 | static int __init nfs4blocklayout_init(void) |
1356 | { | |
1357 | int ret; | |
1358 | ||
1359 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); | |
1360 | ||
1361 | ret = pnfs_register_layoutdriver(&blocklayout_type); | |
fe0a9b74 JR |
1362 | if (ret) |
1363 | goto out; | |
1364 | ||
627f3066 | 1365 | ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
9e2e74db SK |
1366 | if (ret) |
1367 | goto out_remove; | |
627f3066 SK |
1368 | ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
1369 | if (ret) | |
1370 | goto out_notifier; | |
fe0a9b74 JR |
1371 | out: |
1372 | return ret; | |
1373 | ||
627f3066 SK |
1374 | out_notifier: |
1375 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); | |
fe0a9b74 JR |
1376 | out_remove: |
1377 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
155e7524 FI |
1378 | return ret; |
1379 | } | |
1380 | ||
1381 | static void __exit nfs4blocklayout_exit(void) | |
1382 | { | |
1383 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", | |
1384 | __func__); | |
1385 | ||
627f3066 | 1386 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
9e2e74db | 1387 | unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
155e7524 FI |
1388 | pnfs_unregister_layoutdriver(&blocklayout_type); |
1389 | } | |
1390 | ||
1391 | MODULE_ALIAS("nfs-layouttype4-3"); | |
1392 | ||
1393 | module_init(nfs4blocklayout_init); | |
1394 | module_exit(nfs4blocklayout_exit); |