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 | ||
165 | static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw, | |
166 | sector_t isect, struct page *page, | |
167 | struct pnfs_block_extent *be, | |
168 | void (*end_io)(struct bio *, int err), | |
169 | struct parallel_io *par) | |
170 | { | |
171 | retry: | |
172 | if (!bio) { | |
173 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); | |
174 | if (!bio) | |
175 | return ERR_PTR(-ENOMEM); | |
176 | } | |
177 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { | |
178 | bio = bl_submit_bio(rw, bio); | |
179 | goto retry; | |
180 | } | |
181 | return bio; | |
182 | } | |
183 | ||
9549ec01 FI |
184 | /* This is basically copied from mpage_end_io_read */ |
185 | static void bl_end_io_read(struct bio *bio, int err) | |
186 | { | |
187 | struct parallel_io *par = bio->bi_private; | |
188 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
189 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
9549ec01 FI |
190 | |
191 | do { | |
192 | struct page *page = bvec->bv_page; | |
193 | ||
194 | if (--bvec >= bio->bi_io_vec) | |
195 | prefetchw(&bvec->bv_page->flags); | |
196 | if (uptodate) | |
197 | SetPageUptodate(page); | |
198 | } while (bvec >= bio->bi_io_vec); | |
199 | if (!uptodate) { | |
cd841605 FI |
200 | struct nfs_read_data *rdata = par->data; |
201 | struct nfs_pgio_header *header = rdata->header; | |
202 | ||
203 | if (!header->pnfs_error) | |
204 | header->pnfs_error = -EIO; | |
205 | pnfs_set_lo_fail(header->lseg); | |
9549ec01 FI |
206 | } |
207 | bio_put(bio); | |
208 | put_parallel(par); | |
209 | } | |
210 | ||
211 | static void bl_read_cleanup(struct work_struct *work) | |
212 | { | |
213 | struct rpc_task *task; | |
214 | struct nfs_read_data *rdata; | |
215 | dprintk("%s enter\n", __func__); | |
216 | task = container_of(work, struct rpc_task, u.tk_work); | |
217 | rdata = container_of(task, struct nfs_read_data, task); | |
218 | pnfs_ld_read_done(rdata); | |
219 | } | |
220 | ||
221 | static void | |
7c5465d6 | 222 | bl_end_par_io_read(void *data, int unused) |
9549ec01 FI |
223 | { |
224 | struct nfs_read_data *rdata = data; | |
225 | ||
cd841605 | 226 | rdata->task.tk_status = rdata->header->pnfs_error; |
9549ec01 FI |
227 | INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup); |
228 | schedule_work(&rdata->task.u.tk_work); | |
229 | } | |
230 | ||
155e7524 FI |
231 | static enum pnfs_try_status |
232 | bl_read_pagelist(struct nfs_read_data *rdata) | |
233 | { | |
cd841605 | 234 | struct nfs_pgio_header *header = rdata->header; |
9549ec01 FI |
235 | int i, hole; |
236 | struct bio *bio = NULL; | |
237 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; | |
238 | sector_t isect, extent_length = 0; | |
239 | struct parallel_io *par; | |
240 | loff_t f_offset = rdata->args.offset; | |
9549ec01 FI |
241 | struct page **pages = rdata->args.pages; |
242 | int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
243 | ||
6f00866d TM |
244 | dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__, |
245 | rdata->npages, f_offset, (unsigned int)rdata->args.count); | |
9549ec01 FI |
246 | |
247 | par = alloc_parallel(rdata); | |
248 | if (!par) | |
249 | goto use_mds; | |
9549ec01 FI |
250 | par->pnfs_callback = bl_end_par_io_read; |
251 | /* At this point, we can no longer jump to use_mds */ | |
252 | ||
253 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); | |
254 | /* Code assumes extents are page-aligned */ | |
255 | for (i = pg_index; i < rdata->npages; i++) { | |
256 | if (!extent_length) { | |
257 | /* We've used up the previous extent */ | |
258 | bl_put_extent(be); | |
259 | bl_put_extent(cow_read); | |
260 | bio = bl_submit_bio(READ, bio); | |
261 | /* Get the next one */ | |
cd841605 | 262 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
9549ec01 FI |
263 | isect, &cow_read); |
264 | if (!be) { | |
cd841605 | 265 | header->pnfs_error = -EIO; |
9549ec01 FI |
266 | goto out; |
267 | } | |
268 | extent_length = be->be_length - | |
269 | (isect - be->be_f_offset); | |
270 | if (cow_read) { | |
271 | sector_t cow_length = cow_read->be_length - | |
272 | (isect - cow_read->be_f_offset); | |
273 | extent_length = min(extent_length, cow_length); | |
274 | } | |
275 | } | |
276 | hole = is_hole(be, isect); | |
277 | if (hole && !cow_read) { | |
278 | bio = bl_submit_bio(READ, bio); | |
279 | /* Fill hole w/ zeroes w/o accessing device */ | |
280 | dprintk("%s Zeroing page for hole\n", __func__); | |
281 | zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE); | |
282 | print_page(pages[i]); | |
283 | SetPageUptodate(pages[i]); | |
284 | } else { | |
285 | struct pnfs_block_extent *be_read; | |
286 | ||
287 | be_read = (hole && cow_read) ? cow_read : be; | |
288 | bio = bl_add_page_to_bio(bio, rdata->npages - i, READ, | |
289 | isect, pages[i], be_read, | |
290 | bl_end_io_read, par); | |
291 | if (IS_ERR(bio)) { | |
cd841605 | 292 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 293 | bio = NULL; |
9549ec01 FI |
294 | goto out; |
295 | } | |
296 | } | |
297 | isect += PAGE_CACHE_SECTORS; | |
298 | extent_length -= PAGE_CACHE_SECTORS; | |
299 | } | |
cd841605 | 300 | if ((isect << SECTOR_SHIFT) >= header->inode->i_size) { |
9549ec01 | 301 | rdata->res.eof = 1; |
cd841605 | 302 | rdata->res.count = header->inode->i_size - f_offset; |
9549ec01 FI |
303 | } else { |
304 | rdata->res.count = (isect << SECTOR_SHIFT) - f_offset; | |
305 | } | |
306 | out: | |
307 | bl_put_extent(be); | |
308 | bl_put_extent(cow_read); | |
309 | bl_submit_bio(READ, bio); | |
310 | put_parallel(par); | |
311 | return PNFS_ATTEMPTED; | |
312 | ||
313 | use_mds: | |
314 | dprintk("Giving up and using normal NFS\n"); | |
155e7524 FI |
315 | return PNFS_NOT_ATTEMPTED; |
316 | } | |
317 | ||
31e6306a FI |
318 | static void mark_extents_written(struct pnfs_block_layout *bl, |
319 | __u64 offset, __u32 count) | |
320 | { | |
321 | sector_t isect, end; | |
322 | struct pnfs_block_extent *be; | |
7c5465d6 | 323 | struct pnfs_block_short_extent *se; |
31e6306a FI |
324 | |
325 | dprintk("%s(%llu, %u)\n", __func__, offset, count); | |
326 | if (count == 0) | |
327 | return; | |
328 | isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT; | |
329 | end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK); | |
330 | end >>= SECTOR_SHIFT; | |
331 | while (isect < end) { | |
332 | sector_t len; | |
333 | be = bl_find_get_extent(bl, isect, NULL); | |
334 | BUG_ON(!be); /* FIXME */ | |
335 | len = min(end, be->be_f_offset + be->be_length) - isect; | |
7c5465d6 PT |
336 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
337 | se = bl_pop_one_short_extent(be->be_inval); | |
338 | BUG_ON(!se); | |
339 | bl_mark_for_commit(be, isect, len, se); | |
340 | } | |
31e6306a FI |
341 | isect += len; |
342 | bl_put_extent(be); | |
343 | } | |
344 | } | |
345 | ||
71cdd40f PT |
346 | static void bl_end_io_write_zero(struct bio *bio, int err) |
347 | { | |
348 | struct parallel_io *par = bio->bi_private; | |
349 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
350 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
71cdd40f PT |
351 | |
352 | do { | |
353 | struct page *page = bvec->bv_page; | |
354 | ||
355 | if (--bvec >= bio->bi_io_vec) | |
356 | prefetchw(&bvec->bv_page->flags); | |
357 | /* This is the zeroing page we added */ | |
358 | end_page_writeback(page); | |
359 | page_cache_release(page); | |
360 | } while (bvec >= bio->bi_io_vec); | |
7c5465d6 PT |
361 | |
362 | if (unlikely(!uptodate)) { | |
cd841605 FI |
363 | struct nfs_write_data *data = par->data; |
364 | struct nfs_pgio_header *header = data->header; | |
365 | ||
366 | if (!header->pnfs_error) | |
367 | header->pnfs_error = -EIO; | |
368 | pnfs_set_lo_fail(header->lseg); | |
71cdd40f PT |
369 | } |
370 | bio_put(bio); | |
371 | put_parallel(par); | |
372 | } | |
373 | ||
650e2d39 FI |
374 | static void bl_end_io_write(struct bio *bio, int err) |
375 | { | |
376 | struct parallel_io *par = bio->bi_private; | |
377 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
cd841605 FI |
378 | struct nfs_write_data *data = par->data; |
379 | struct nfs_pgio_header *header = data->header; | |
650e2d39 FI |
380 | |
381 | if (!uptodate) { | |
cd841605 FI |
382 | if (!header->pnfs_error) |
383 | header->pnfs_error = -EIO; | |
384 | pnfs_set_lo_fail(header->lseg); | |
650e2d39 FI |
385 | } |
386 | bio_put(bio); | |
387 | put_parallel(par); | |
388 | } | |
389 | ||
390 | /* Function scheduled for call during bl_end_par_io_write, | |
391 | * it marks sectors as written and extends the commitlist. | |
392 | */ | |
393 | static void bl_write_cleanup(struct work_struct *work) | |
394 | { | |
395 | struct rpc_task *task; | |
396 | struct nfs_write_data *wdata; | |
397 | dprintk("%s enter\n", __func__); | |
398 | task = container_of(work, struct rpc_task, u.tk_work); | |
399 | wdata = container_of(task, struct nfs_write_data, task); | |
cd841605 | 400 | if (likely(!wdata->header->pnfs_error)) { |
31e6306a | 401 | /* Marks for LAYOUTCOMMIT */ |
cd841605 | 402 | mark_extents_written(BLK_LSEG2EXT(wdata->header->lseg), |
31e6306a FI |
403 | wdata->args.offset, wdata->args.count); |
404 | } | |
650e2d39 FI |
405 | pnfs_ld_write_done(wdata); |
406 | } | |
407 | ||
408 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ | |
7c5465d6 | 409 | static void bl_end_par_io_write(void *data, int num_se) |
650e2d39 FI |
410 | { |
411 | struct nfs_write_data *wdata = data; | |
412 | ||
cd841605 FI |
413 | if (unlikely(wdata->header->pnfs_error)) { |
414 | bl_free_short_extents(&BLK_LSEG2EXT(wdata->header->lseg)->bl_inval, | |
7c5465d6 PT |
415 | num_se); |
416 | } | |
417 | ||
cd841605 | 418 | wdata->task.tk_status = wdata->header->pnfs_error; |
650e2d39 FI |
419 | wdata->verf.committed = NFS_FILE_SYNC; |
420 | INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup); | |
421 | schedule_work(&wdata->task.u.tk_work); | |
422 | } | |
423 | ||
71cdd40f PT |
424 | /* FIXME STUB - mark intersection of layout and page as bad, so is not |
425 | * used again. | |
426 | */ | |
427 | static void mark_bad_read(void) | |
428 | { | |
429 | return; | |
430 | } | |
431 | ||
432 | /* | |
433 | * map_block: map a requested I/0 block (isect) into an offset in the LVM | |
434 | * block_device | |
435 | */ | |
436 | static void | |
437 | map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be) | |
438 | { | |
439 | dprintk("%s enter be=%p\n", __func__, be); | |
440 | ||
441 | set_buffer_mapped(bh); | |
442 | bh->b_bdev = be->be_mdev; | |
443 | bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >> | |
444 | (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT); | |
445 | ||
446 | dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n", | |
447 | __func__, (unsigned long long)isect, (long)bh->b_blocknr, | |
448 | bh->b_size); | |
449 | return; | |
450 | } | |
451 | ||
452 | /* Given an unmapped page, zero it or read in page for COW, page is locked | |
453 | * by caller. | |
454 | */ | |
455 | static int | |
456 | init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read) | |
457 | { | |
458 | struct buffer_head *bh = NULL; | |
459 | int ret = 0; | |
460 | sector_t isect; | |
461 | ||
462 | dprintk("%s enter, %p\n", __func__, page); | |
463 | BUG_ON(PageUptodate(page)); | |
464 | if (!cow_read) { | |
465 | zero_user_segment(page, 0, PAGE_SIZE); | |
466 | SetPageUptodate(page); | |
467 | goto cleanup; | |
468 | } | |
469 | ||
470 | bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0); | |
471 | if (!bh) { | |
472 | ret = -ENOMEM; | |
473 | goto cleanup; | |
474 | } | |
475 | ||
476 | isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT; | |
477 | map_block(bh, isect, cow_read); | |
478 | if (!bh_uptodate_or_lock(bh)) | |
479 | ret = bh_submit_read(bh); | |
480 | if (ret) | |
481 | goto cleanup; | |
482 | SetPageUptodate(page); | |
483 | ||
484 | cleanup: | |
485 | bl_put_extent(cow_read); | |
486 | if (bh) | |
487 | free_buffer_head(bh); | |
488 | if (ret) { | |
489 | /* Need to mark layout with bad read...should now | |
490 | * just use nfs4 for reads and writes. | |
491 | */ | |
492 | mark_bad_read(); | |
493 | } | |
494 | return ret; | |
495 | } | |
496 | ||
72c50887 PT |
497 | /* Find or create a zeroing page marked being writeback. |
498 | * Return ERR_PTR on error, NULL to indicate skip this page and page itself | |
499 | * to indicate write out. | |
500 | */ | |
501 | static struct page * | |
502 | bl_find_get_zeroing_page(struct inode *inode, pgoff_t index, | |
503 | struct pnfs_block_extent *cow_read) | |
504 | { | |
505 | struct page *page; | |
506 | int locked = 0; | |
507 | page = find_get_page(inode->i_mapping, index); | |
508 | if (page) | |
509 | goto check_page; | |
510 | ||
511 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); | |
512 | if (unlikely(!page)) { | |
513 | dprintk("%s oom\n", __func__); | |
514 | return ERR_PTR(-ENOMEM); | |
515 | } | |
516 | locked = 1; | |
517 | ||
518 | check_page: | |
519 | /* PageDirty: Other will write this out | |
520 | * PageWriteback: Other is writing this out | |
521 | * PageUptodate: It was read before | |
522 | */ | |
523 | if (PageDirty(page) || PageWriteback(page)) { | |
524 | print_page(page); | |
525 | if (locked) | |
526 | unlock_page(page); | |
527 | page_cache_release(page); | |
528 | return NULL; | |
529 | } | |
530 | ||
531 | if (!locked) { | |
532 | lock_page(page); | |
533 | locked = 1; | |
534 | goto check_page; | |
535 | } | |
536 | if (!PageUptodate(page)) { | |
537 | /* New page, readin or zero it */ | |
538 | init_page_for_write(page, cow_read); | |
539 | } | |
540 | set_page_writeback(page); | |
541 | unlock_page(page); | |
542 | ||
543 | return page; | |
544 | } | |
545 | ||
155e7524 | 546 | static enum pnfs_try_status |
650e2d39 | 547 | bl_write_pagelist(struct nfs_write_data *wdata, int sync) |
155e7524 | 548 | { |
cd841605 | 549 | struct nfs_pgio_header *header = wdata->header; |
71cdd40f | 550 | int i, ret, npg_zero, pg_index, last = 0; |
650e2d39 | 551 | struct bio *bio = NULL; |
71cdd40f PT |
552 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
553 | sector_t isect, last_isect = 0, extent_length = 0; | |
650e2d39 FI |
554 | struct parallel_io *par; |
555 | loff_t offset = wdata->args.offset; | |
556 | size_t count = wdata->args.count; | |
557 | struct page **pages = wdata->args.pages; | |
71cdd40f PT |
558 | struct page *page; |
559 | pgoff_t index; | |
560 | u64 temp; | |
561 | int npg_per_block = | |
cd841605 | 562 | NFS_SERVER(header->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT; |
650e2d39 FI |
563 | |
564 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); | |
565 | /* At this point, wdata->pages is a (sequential) list of nfs_pages. | |
71cdd40f PT |
566 | * We want to write each, and if there is an error set pnfs_error |
567 | * to have it redone using nfs. | |
650e2d39 FI |
568 | */ |
569 | par = alloc_parallel(wdata); | |
570 | if (!par) | |
7c5465d6 | 571 | goto out_mds; |
650e2d39 FI |
572 | par->pnfs_callback = bl_end_par_io_write; |
573 | /* At this point, have to be more careful with error handling */ | |
574 | ||
575 | isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
cd841605 | 576 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), isect, &cow_read); |
71cdd40f PT |
577 | if (!be || !is_writable(be, isect)) { |
578 | dprintk("%s no matching extents!\n", __func__); | |
7c5465d6 | 579 | goto out_mds; |
71cdd40f PT |
580 | } |
581 | ||
582 | /* First page inside INVALID extent */ | |
583 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
7c5465d6 PT |
584 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
585 | par->bse_count++; | |
586 | else | |
587 | goto out_mds; | |
71cdd40f PT |
588 | temp = offset >> PAGE_CACHE_SHIFT; |
589 | npg_zero = do_div(temp, npg_per_block); | |
590 | isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) & | |
591 | (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT); | |
592 | extent_length = be->be_length - (isect - be->be_f_offset); | |
593 | ||
594 | fill_invalid_ext: | |
595 | dprintk("%s need to zero %d pages\n", __func__, npg_zero); | |
596 | for (;npg_zero > 0; npg_zero--) { | |
75422745 PT |
597 | if (bl_is_sector_init(be->be_inval, isect)) { |
598 | dprintk("isect %llu already init\n", | |
599 | (unsigned long long)isect); | |
600 | goto next_page; | |
601 | } | |
71cdd40f PT |
602 | /* page ref released in bl_end_io_write_zero */ |
603 | index = isect >> PAGE_CACHE_SECTOR_SHIFT; | |
604 | dprintk("%s zero %dth page: index %lu isect %llu\n", | |
605 | __func__, npg_zero, index, | |
606 | (unsigned long long)isect); | |
cd841605 | 607 | page = bl_find_get_zeroing_page(header->inode, index, |
72c50887 PT |
608 | cow_read); |
609 | if (unlikely(IS_ERR(page))) { | |
cd841605 | 610 | header->pnfs_error = PTR_ERR(page); |
71cdd40f | 611 | goto out; |
72c50887 | 612 | } else if (page == NULL) |
71cdd40f | 613 | goto next_page; |
71cdd40f PT |
614 | |
615 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
60c52e3a | 616 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
617 | if (unlikely(ret)) { |
618 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
619 | __func__, ret); | |
620 | end_page_writeback(page); | |
621 | page_cache_release(page); | |
cd841605 | 622 | header->pnfs_error = ret; |
71cdd40f PT |
623 | goto out; |
624 | } | |
7c5465d6 PT |
625 | if (likely(!bl_push_one_short_extent(be->be_inval))) |
626 | par->bse_count++; | |
627 | else { | |
628 | end_page_writeback(page); | |
629 | page_cache_release(page); | |
cd841605 | 630 | header->pnfs_error = -ENOMEM; |
7c5465d6 PT |
631 | goto out; |
632 | } | |
633 | /* FIXME: This should be done in bi_end_io */ | |
cd841605 | 634 | mark_extents_written(BLK_LSEG2EXT(header->lseg), |
7c5465d6 PT |
635 | page->index << PAGE_CACHE_SHIFT, |
636 | PAGE_CACHE_SIZE); | |
637 | ||
71cdd40f PT |
638 | bio = bl_add_page_to_bio(bio, npg_zero, WRITE, |
639 | isect, page, be, | |
640 | bl_end_io_write_zero, par); | |
641 | if (IS_ERR(bio)) { | |
cd841605 | 642 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 643 | bio = NULL; |
71cdd40f PT |
644 | goto out; |
645 | } | |
71cdd40f PT |
646 | next_page: |
647 | isect += PAGE_CACHE_SECTORS; | |
648 | extent_length -= PAGE_CACHE_SECTORS; | |
649 | } | |
650 | if (last) | |
651 | goto write_done; | |
652 | } | |
653 | bio = bl_submit_bio(WRITE, bio); | |
654 | ||
655 | /* Middle pages */ | |
656 | pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT; | |
657 | for (i = pg_index; i < wdata->npages; i++) { | |
650e2d39 FI |
658 | if (!extent_length) { |
659 | /* We've used up the previous extent */ | |
660 | bl_put_extent(be); | |
661 | bio = bl_submit_bio(WRITE, bio); | |
662 | /* Get the next one */ | |
cd841605 | 663 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
650e2d39 FI |
664 | isect, NULL); |
665 | if (!be || !is_writable(be, isect)) { | |
cd841605 | 666 | header->pnfs_error = -EINVAL; |
650e2d39 FI |
667 | goto out; |
668 | } | |
7c5465d6 PT |
669 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
670 | if (likely(!bl_push_one_short_extent( | |
671 | be->be_inval))) | |
672 | par->bse_count++; | |
673 | else { | |
cd841605 | 674 | header->pnfs_error = -ENOMEM; |
7c5465d6 PT |
675 | goto out; |
676 | } | |
677 | } | |
650e2d39 | 678 | extent_length = be->be_length - |
71cdd40f | 679 | (isect - be->be_f_offset); |
650e2d39 | 680 | } |
71cdd40f PT |
681 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
682 | ret = bl_mark_sectors_init(be->be_inval, isect, | |
60c52e3a | 683 | PAGE_CACHE_SECTORS); |
71cdd40f PT |
684 | if (unlikely(ret)) { |
685 | dprintk("%s bl_mark_sectors_init fail %d\n", | |
686 | __func__, ret); | |
cd841605 | 687 | header->pnfs_error = ret; |
71cdd40f | 688 | goto out; |
650e2d39 | 689 | } |
71cdd40f PT |
690 | } |
691 | bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE, | |
692 | isect, pages[i], be, | |
693 | bl_end_io_write, par); | |
694 | if (IS_ERR(bio)) { | |
cd841605 | 695 | header->pnfs_error = PTR_ERR(bio); |
e6d05a75 | 696 | bio = NULL; |
71cdd40f | 697 | goto out; |
650e2d39 FI |
698 | } |
699 | isect += PAGE_CACHE_SECTORS; | |
71cdd40f | 700 | last_isect = isect; |
650e2d39 FI |
701 | extent_length -= PAGE_CACHE_SECTORS; |
702 | } | |
71cdd40f PT |
703 | |
704 | /* Last page inside INVALID extent */ | |
705 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { | |
706 | bio = bl_submit_bio(WRITE, bio); | |
707 | temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT; | |
708 | npg_zero = npg_per_block - do_div(temp, npg_per_block); | |
709 | if (npg_zero < npg_per_block) { | |
710 | last = 1; | |
711 | goto fill_invalid_ext; | |
712 | } | |
713 | } | |
714 | ||
715 | write_done: | |
716 | wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset); | |
717 | if (count < wdata->res.count) { | |
650e2d39 | 718 | wdata->res.count = count; |
71cdd40f | 719 | } |
650e2d39 FI |
720 | out: |
721 | bl_put_extent(be); | |
722 | bl_submit_bio(WRITE, bio); | |
723 | put_parallel(par); | |
724 | return PNFS_ATTEMPTED; | |
7c5465d6 PT |
725 | out_mds: |
726 | bl_put_extent(be); | |
727 | kfree(par); | |
728 | return PNFS_NOT_ATTEMPTED; | |
155e7524 FI |
729 | } |
730 | ||
9e692969 | 731 | /* FIXME - range ignored */ |
155e7524 | 732 | static void |
9e692969 | 733 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
155e7524 | 734 | { |
9e692969 FI |
735 | int i; |
736 | struct pnfs_block_extent *be; | |
737 | ||
738 | spin_lock(&bl->bl_ext_lock); | |
739 | for (i = 0; i < EXTENT_LISTS; i++) { | |
740 | while (!list_empty(&bl->bl_extents[i])) { | |
741 | be = list_first_entry(&bl->bl_extents[i], | |
742 | struct pnfs_block_extent, | |
743 | be_node); | |
744 | list_del(&be->be_node); | |
745 | bl_put_extent(be); | |
746 | } | |
747 | } | |
748 | spin_unlock(&bl->bl_ext_lock); | |
155e7524 FI |
749 | } |
750 | ||
155e7524 FI |
751 | static void |
752 | release_inval_marks(struct pnfs_inval_markings *marks) | |
753 | { | |
c1c2a4cd | 754 | struct pnfs_inval_tracking *pos, *temp; |
7c5465d6 | 755 | struct pnfs_block_short_extent *se, *stemp; |
c1c2a4cd FI |
756 | |
757 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { | |
758 | list_del(&pos->it_link); | |
759 | kfree(pos); | |
760 | } | |
7c5465d6 PT |
761 | |
762 | list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) { | |
763 | list_del(&se->bse_node); | |
764 | kfree(se); | |
765 | } | |
155e7524 FI |
766 | return; |
767 | } | |
768 | ||
769 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
770 | { | |
771 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); | |
772 | ||
773 | dprintk("%s enter\n", __func__); | |
774 | release_extents(bl, NULL); | |
775 | release_inval_marks(&bl->bl_inval); | |
776 | kfree(bl); | |
777 | } | |
778 | ||
779 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, | |
780 | gfp_t gfp_flags) | |
781 | { | |
782 | struct pnfs_block_layout *bl; | |
783 | ||
784 | dprintk("%s enter\n", __func__); | |
785 | bl = kzalloc(sizeof(*bl), gfp_flags); | |
786 | if (!bl) | |
787 | return NULL; | |
788 | spin_lock_init(&bl->bl_ext_lock); | |
789 | INIT_LIST_HEAD(&bl->bl_extents[0]); | |
790 | INIT_LIST_HEAD(&bl->bl_extents[1]); | |
791 | INIT_LIST_HEAD(&bl->bl_commit); | |
792 | INIT_LIST_HEAD(&bl->bl_committing); | |
793 | bl->bl_count = 0; | |
794 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; | |
795 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); | |
796 | return &bl->bl_layout; | |
797 | } | |
798 | ||
a60d2ebd | 799 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
155e7524 | 800 | { |
a60d2ebd FI |
801 | dprintk("%s enter\n", __func__); |
802 | kfree(lseg); | |
155e7524 FI |
803 | } |
804 | ||
a60d2ebd FI |
805 | /* We pretty much ignore lseg, and store all data layout wide, so we |
806 | * can correctly merge. | |
807 | */ | |
808 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, | |
809 | struct nfs4_layoutget_res *lgr, | |
810 | gfp_t gfp_flags) | |
155e7524 | 811 | { |
a60d2ebd FI |
812 | struct pnfs_layout_segment *lseg; |
813 | int status; | |
814 | ||
815 | dprintk("%s enter\n", __func__); | |
816 | lseg = kzalloc(sizeof(*lseg), gfp_flags); | |
817 | if (!lseg) | |
818 | return ERR_PTR(-ENOMEM); | |
819 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); | |
820 | if (status) { | |
821 | /* We don't want to call the full-blown bl_free_lseg, | |
822 | * since on error extents were not touched. | |
823 | */ | |
824 | kfree(lseg); | |
825 | return ERR_PTR(status); | |
826 | } | |
827 | return lseg; | |
155e7524 FI |
828 | } |
829 | ||
830 | static void | |
831 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, | |
832 | const struct nfs4_layoutcommit_args *arg) | |
833 | { | |
90ace12a FI |
834 | dprintk("%s enter\n", __func__); |
835 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); | |
155e7524 FI |
836 | } |
837 | ||
838 | static void | |
839 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) | |
840 | { | |
b2be7811 FI |
841 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
842 | ||
843 | dprintk("%s enter\n", __func__); | |
844 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); | |
155e7524 FI |
845 | } |
846 | ||
2f9fd182 FI |
847 | static void free_blk_mountid(struct block_mount_id *mid) |
848 | { | |
849 | if (mid) { | |
93a3844e PT |
850 | struct pnfs_block_dev *dev, *tmp; |
851 | ||
852 | /* No need to take bm_lock as we are last user freeing bm_devlist */ | |
853 | list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) { | |
2f9fd182 FI |
854 | list_del(&dev->bm_node); |
855 | bl_free_block_dev(dev); | |
856 | } | |
2f9fd182 FI |
857 | kfree(mid); |
858 | } | |
859 | } | |
860 | ||
861 | /* This is mostly copied from the filelayout's get_device_info function. | |
862 | * It seems much of this should be at the generic pnfs level. | |
863 | */ | |
864 | static struct pnfs_block_dev * | |
865 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, | |
866 | struct nfs4_deviceid *d_id) | |
867 | { | |
868 | struct pnfs_device *dev; | |
516f2e24 | 869 | struct pnfs_block_dev *rv; |
2f9fd182 FI |
870 | u32 max_resp_sz; |
871 | int max_pages; | |
872 | struct page **pages = NULL; | |
873 | int i, rc; | |
874 | ||
875 | /* | |
876 | * Use the session max response size as the basis for setting | |
877 | * GETDEVICEINFO's maxcount | |
878 | */ | |
879 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
10bd295a | 880 | max_pages = nfs_page_array_len(0, max_resp_sz); |
2f9fd182 FI |
881 | dprintk("%s max_resp_sz %u max_pages %d\n", |
882 | __func__, max_resp_sz, max_pages); | |
883 | ||
884 | dev = kmalloc(sizeof(*dev), GFP_NOFS); | |
885 | if (!dev) { | |
886 | dprintk("%s kmalloc failed\n", __func__); | |
516f2e24 | 887 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
888 | } |
889 | ||
890 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS); | |
891 | if (pages == NULL) { | |
892 | kfree(dev); | |
516f2e24 | 893 | return ERR_PTR(-ENOMEM); |
2f9fd182 FI |
894 | } |
895 | for (i = 0; i < max_pages; i++) { | |
896 | pages[i] = alloc_page(GFP_NOFS); | |
516f2e24 JR |
897 | if (!pages[i]) { |
898 | rv = ERR_PTR(-ENOMEM); | |
2f9fd182 | 899 | goto out_free; |
516f2e24 | 900 | } |
2f9fd182 FI |
901 | } |
902 | ||
903 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); | |
904 | dev->layout_type = LAYOUT_BLOCK_VOLUME; | |
905 | dev->pages = pages; | |
906 | dev->pgbase = 0; | |
907 | dev->pglen = PAGE_SIZE * max_pages; | |
908 | dev->mincount = 0; | |
909 | ||
910 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); | |
911 | rc = nfs4_proc_getdeviceinfo(server, dev); | |
912 | dprintk("%s getdevice info returns %d\n", __func__, rc); | |
516f2e24 JR |
913 | if (rc) { |
914 | rv = ERR_PTR(rc); | |
2f9fd182 | 915 | goto out_free; |
516f2e24 | 916 | } |
2f9fd182 FI |
917 | |
918 | rv = nfs4_blk_decode_device(server, dev); | |
919 | out_free: | |
920 | for (i = 0; i < max_pages; i++) | |
921 | __free_page(pages[i]); | |
922 | kfree(pages); | |
923 | kfree(dev); | |
924 | return rv; | |
925 | } | |
926 | ||
155e7524 FI |
927 | static int |
928 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) | |
929 | { | |
2f9fd182 FI |
930 | struct block_mount_id *b_mt_id = NULL; |
931 | struct pnfs_devicelist *dlist = NULL; | |
932 | struct pnfs_block_dev *bdev; | |
933 | LIST_HEAD(block_disklist); | |
516f2e24 | 934 | int status, i; |
2f9fd182 | 935 | |
155e7524 | 936 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
937 | |
938 | if (server->pnfs_blksize == 0) { | |
939 | dprintk("%s Server did not return blksize\n", __func__); | |
940 | return -EINVAL; | |
941 | } | |
942 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); | |
943 | if (!b_mt_id) { | |
944 | status = -ENOMEM; | |
945 | goto out_error; | |
946 | } | |
947 | /* Initialize nfs4 block layout mount id */ | |
948 | spin_lock_init(&b_mt_id->bm_lock); | |
949 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); | |
950 | ||
951 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); | |
952 | if (!dlist) { | |
953 | status = -ENOMEM; | |
954 | goto out_error; | |
955 | } | |
956 | dlist->eof = 0; | |
957 | while (!dlist->eof) { | |
958 | status = nfs4_proc_getdevicelist(server, fh, dlist); | |
959 | if (status) | |
960 | goto out_error; | |
961 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", | |
962 | __func__, dlist->num_devs, dlist->eof); | |
963 | for (i = 0; i < dlist->num_devs; i++) { | |
964 | bdev = nfs4_blk_get_deviceinfo(server, fh, | |
965 | &dlist->dev_id[i]); | |
516f2e24 JR |
966 | if (IS_ERR(bdev)) { |
967 | status = PTR_ERR(bdev); | |
2f9fd182 FI |
968 | goto out_error; |
969 | } | |
970 | spin_lock(&b_mt_id->bm_lock); | |
971 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); | |
972 | spin_unlock(&b_mt_id->bm_lock); | |
973 | } | |
974 | } | |
975 | dprintk("%s SUCCESS\n", __func__); | |
976 | server->pnfs_ld_data = b_mt_id; | |
977 | ||
978 | out_return: | |
979 | kfree(dlist); | |
980 | return status; | |
981 | ||
982 | out_error: | |
983 | free_blk_mountid(b_mt_id); | |
984 | goto out_return; | |
155e7524 FI |
985 | } |
986 | ||
987 | static int | |
988 | bl_clear_layoutdriver(struct nfs_server *server) | |
989 | { | |
2f9fd182 FI |
990 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
991 | ||
155e7524 | 992 | dprintk("%s enter\n", __func__); |
2f9fd182 FI |
993 | free_blk_mountid(b_mt_id); |
994 | dprintk("%s RETURNS\n", __func__); | |
155e7524 FI |
995 | return 0; |
996 | } | |
997 | ||
e9643fe8 BH |
998 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
999 | .pg_init = pnfs_generic_pg_init_read, | |
1000 | .pg_test = pnfs_generic_pg_test, | |
1001 | .pg_doio = pnfs_generic_pg_readpages, | |
1002 | }; | |
1003 | ||
1004 | static const struct nfs_pageio_ops bl_pg_write_ops = { | |
1005 | .pg_init = pnfs_generic_pg_init_write, | |
1006 | .pg_test = pnfs_generic_pg_test, | |
1007 | .pg_doio = pnfs_generic_pg_writepages, | |
1008 | }; | |
1009 | ||
155e7524 FI |
1010 | static struct pnfs_layoutdriver_type blocklayout_type = { |
1011 | .id = LAYOUT_BLOCK_VOLUME, | |
1012 | .name = "LAYOUT_BLOCK_VOLUME", | |
1013 | .read_pagelist = bl_read_pagelist, | |
1014 | .write_pagelist = bl_write_pagelist, | |
1015 | .alloc_layout_hdr = bl_alloc_layout_hdr, | |
1016 | .free_layout_hdr = bl_free_layout_hdr, | |
1017 | .alloc_lseg = bl_alloc_lseg, | |
1018 | .free_lseg = bl_free_lseg, | |
1019 | .encode_layoutcommit = bl_encode_layoutcommit, | |
1020 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, | |
1021 | .set_layoutdriver = bl_set_layoutdriver, | |
1022 | .clear_layoutdriver = bl_clear_layoutdriver, | |
e9643fe8 BH |
1023 | .pg_read_ops = &bl_pg_read_ops, |
1024 | .pg_write_ops = &bl_pg_write_ops, | |
155e7524 FI |
1025 | }; |
1026 | ||
fe0a9b74 | 1027 | static const struct rpc_pipe_ops bl_upcall_ops = { |
c1225158 | 1028 | .upcall = rpc_pipe_generic_upcall, |
fe0a9b74 JR |
1029 | .downcall = bl_pipe_downcall, |
1030 | .destroy_msg = bl_pipe_destroy_msg, | |
1031 | }; | |
1032 | ||
332dfab6 SK |
1033 | static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb, |
1034 | struct rpc_pipe *pipe) | |
1035 | { | |
1036 | struct dentry *dir, *dentry; | |
1037 | ||
1038 | dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); | |
1039 | if (dir == NULL) | |
1040 | return ERR_PTR(-ENOENT); | |
1041 | dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); | |
1042 | dput(dir); | |
1043 | return dentry; | |
1044 | } | |
1045 | ||
1046 | static void nfs4blocklayout_unregister_sb(struct super_block *sb, | |
1047 | struct rpc_pipe *pipe) | |
1048 | { | |
1049 | if (pipe->dentry) | |
1050 | rpc_unlink(pipe->dentry); | |
1051 | } | |
1052 | ||
627f3066 SK |
1053 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
1054 | void *ptr) | |
1055 | { | |
1056 | struct super_block *sb = ptr; | |
1057 | struct net *net = sb->s_fs_info; | |
1058 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1059 | struct dentry *dentry; | |
1060 | int ret = 0; | |
1061 | ||
1062 | if (!try_module_get(THIS_MODULE)) | |
1063 | return 0; | |
1064 | ||
1065 | if (nn->bl_device_pipe == NULL) { | |
1066 | module_put(THIS_MODULE); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
1070 | switch (event) { | |
1071 | case RPC_PIPEFS_MOUNT: | |
1072 | dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); | |
1073 | if (IS_ERR(dentry)) { | |
1074 | ret = PTR_ERR(dentry); | |
1075 | break; | |
1076 | } | |
1077 | nn->bl_device_pipe->dentry = dentry; | |
1078 | break; | |
1079 | case RPC_PIPEFS_UMOUNT: | |
1080 | if (nn->bl_device_pipe->dentry) | |
1081 | nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe); | |
1082 | break; | |
1083 | default: | |
1084 | ret = -ENOTSUPP; | |
1085 | break; | |
1086 | } | |
1087 | module_put(THIS_MODULE); | |
1088 | return ret; | |
1089 | } | |
1090 | ||
1091 | static struct notifier_block nfs4blocklayout_block = { | |
1092 | .notifier_call = rpc_pipefs_event, | |
1093 | }; | |
1094 | ||
332dfab6 SK |
1095 | static struct dentry *nfs4blocklayout_register_net(struct net *net, |
1096 | struct rpc_pipe *pipe) | |
1097 | { | |
1098 | struct super_block *pipefs_sb; | |
1099 | struct dentry *dentry; | |
1100 | ||
1101 | pipefs_sb = rpc_get_sb_net(net); | |
1102 | if (!pipefs_sb) | |
2561d618 | 1103 | return NULL; |
332dfab6 SK |
1104 | dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
1105 | rpc_put_sb_net(net); | |
1106 | return dentry; | |
1107 | } | |
1108 | ||
1109 | static void nfs4blocklayout_unregister_net(struct net *net, | |
1110 | struct rpc_pipe *pipe) | |
1111 | { | |
1112 | struct super_block *pipefs_sb; | |
1113 | ||
1114 | pipefs_sb = rpc_get_sb_net(net); | |
1115 | if (pipefs_sb) { | |
1116 | nfs4blocklayout_unregister_sb(pipefs_sb, pipe); | |
1117 | rpc_put_sb_net(net); | |
1118 | } | |
1119 | } | |
1120 | ||
9e2e74db SK |
1121 | static int nfs4blocklayout_net_init(struct net *net) |
1122 | { | |
1123 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1124 | struct dentry *dentry; | |
1125 | ||
5ffaf855 | 1126 | init_waitqueue_head(&nn->bl_wq); |
9e2e74db SK |
1127 | nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
1128 | if (IS_ERR(nn->bl_device_pipe)) | |
1129 | return PTR_ERR(nn->bl_device_pipe); | |
1130 | dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe); | |
1131 | if (IS_ERR(dentry)) { | |
1132 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1133 | return PTR_ERR(dentry); | |
1134 | } | |
1135 | nn->bl_device_pipe->dentry = dentry; | |
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | static void nfs4blocklayout_net_exit(struct net *net) | |
1140 | { | |
1141 | struct nfs_net *nn = net_generic(net, nfs_net_id); | |
1142 | ||
1143 | nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); | |
1144 | rpc_destroy_pipe_data(nn->bl_device_pipe); | |
1145 | nn->bl_device_pipe = NULL; | |
1146 | } | |
1147 | ||
1148 | static struct pernet_operations nfs4blocklayout_net_ops = { | |
1149 | .init = nfs4blocklayout_net_init, | |
1150 | .exit = nfs4blocklayout_net_exit, | |
1151 | }; | |
1152 | ||
155e7524 FI |
1153 | static int __init nfs4blocklayout_init(void) |
1154 | { | |
1155 | int ret; | |
1156 | ||
1157 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); | |
1158 | ||
1159 | ret = pnfs_register_layoutdriver(&blocklayout_type); | |
fe0a9b74 JR |
1160 | if (ret) |
1161 | goto out; | |
1162 | ||
627f3066 | 1163 | ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
9e2e74db SK |
1164 | if (ret) |
1165 | goto out_remove; | |
627f3066 SK |
1166 | ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
1167 | if (ret) | |
1168 | goto out_notifier; | |
fe0a9b74 JR |
1169 | out: |
1170 | return ret; | |
1171 | ||
627f3066 SK |
1172 | out_notifier: |
1173 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); | |
fe0a9b74 JR |
1174 | out_remove: |
1175 | pnfs_unregister_layoutdriver(&blocklayout_type); | |
155e7524 FI |
1176 | return ret; |
1177 | } | |
1178 | ||
1179 | static void __exit nfs4blocklayout_exit(void) | |
1180 | { | |
1181 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", | |
1182 | __func__); | |
1183 | ||
627f3066 | 1184 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
9e2e74db | 1185 | unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
155e7524 FI |
1186 | pnfs_unregister_layoutdriver(&blocklayout_type); |
1187 | } | |
1188 | ||
1189 | MODULE_ALIAS("nfs-layouttype4-3"); | |
1190 | ||
1191 | module_init(nfs4blocklayout_init); | |
1192 | module_exit(nfs4blocklayout_exit); |