Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 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 | */ |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 32 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 33 | #include <linux/module.h> |
| 34 | #include <linux/init.h> |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 35 | #include <linux/mount.h> |
| 36 | #include <linux/namei.h> |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 37 | #include <linux/bio.h> /* struct bio */ |
Heiko Carstens | 88c9e42 | 2011-08-02 09:57:35 +0200 | [diff] [blame] | 38 | #include <linux/prefetch.h> |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 39 | #include <linux/pagevec.h> |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 40 | |
Jim Rees | 10bd295a | 2012-04-09 22:33:39 -0400 | [diff] [blame] | 41 | #include "../pnfs.h" |
Trond Myklebust | 76e697b | 2012-11-26 14:20:49 -0500 | [diff] [blame] | 42 | #include "../nfs4session.h" |
Jim Rees | 10bd295a | 2012-04-09 22:33:39 -0400 | [diff] [blame] | 43 | #include "../internal.h" |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 44 | #include "blocklayout.h" |
| 45 | |
| 46 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 47 | |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>"); |
| 50 | MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver"); |
| 51 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 52 | /* Given the be associated with isect, determine if page data needs to be |
| 53 | * initialized. |
| 54 | */ |
| 55 | static int is_hole(struct pnfs_block_extent *be, sector_t isect) |
| 56 | { |
| 57 | if (be->be_state == PNFS_BLOCK_NONE_DATA) |
| 58 | return 1; |
| 59 | else if (be->be_state != PNFS_BLOCK_INVALID_DATA) |
| 60 | return 0; |
| 61 | else |
| 62 | return !bl_is_sector_init(be->be_inval, isect); |
| 63 | } |
| 64 | |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 65 | /* Given the be associated with isect, determine if page data can be |
| 66 | * written to disk. |
| 67 | */ |
| 68 | static int is_writable(struct pnfs_block_extent *be, sector_t isect) |
| 69 | { |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 70 | return (be->be_state == PNFS_BLOCK_READWRITE_DATA || |
| 71 | be->be_state == PNFS_BLOCK_INVALID_DATA); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 74 | /* The data we are handed might be spread across several bios. We need |
| 75 | * to track when the last one is finished. |
| 76 | */ |
| 77 | struct parallel_io { |
| 78 | struct kref refcnt; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 79 | void (*pnfs_callback) (void *data, int num_se); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 80 | void *data; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 81 | int bse_count; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static inline struct parallel_io *alloc_parallel(void *data) |
| 85 | { |
| 86 | struct parallel_io *rv; |
| 87 | |
| 88 | rv = kmalloc(sizeof(*rv), GFP_NOFS); |
| 89 | if (rv) { |
| 90 | rv->data = data; |
| 91 | kref_init(&rv->refcnt); |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 92 | rv->bse_count = 0; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 93 | } |
| 94 | return rv; |
| 95 | } |
| 96 | |
| 97 | static inline void get_parallel(struct parallel_io *p) |
| 98 | { |
| 99 | kref_get(&p->refcnt); |
| 100 | } |
| 101 | |
| 102 | static void destroy_parallel(struct kref *kref) |
| 103 | { |
| 104 | struct parallel_io *p = container_of(kref, struct parallel_io, refcnt); |
| 105 | |
| 106 | dprintk("%s enter\n", __func__); |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 107 | p->pnfs_callback(p->data, p->bse_count); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 108 | kfree(p); |
| 109 | } |
| 110 | |
| 111 | static inline void put_parallel(struct parallel_io *p) |
| 112 | { |
| 113 | kref_put(&p->refcnt, destroy_parallel); |
| 114 | } |
| 115 | |
| 116 | static struct bio * |
| 117 | bl_submit_bio(int rw, struct bio *bio) |
| 118 | { |
| 119 | if (bio) { |
| 120 | get_parallel(bio->bi_private); |
| 121 | dprintk("%s submitting %s bio %u@%llu\n", __func__, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 122 | rw == READ ? "read" : "write", bio->bi_iter.bi_size, |
| 123 | (unsigned long long)bio->bi_iter.bi_sector); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 124 | submit_bio(rw, bio); |
| 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | static struct bio *bl_alloc_init_bio(int npg, sector_t isect, |
| 130 | struct pnfs_block_extent *be, |
| 131 | void (*end_io)(struct bio *, int err), |
| 132 | struct parallel_io *par) |
| 133 | { |
| 134 | struct bio *bio; |
| 135 | |
Peng Tao | 74a6eeb | 2012-01-12 23:18:48 +0800 | [diff] [blame] | 136 | npg = min(npg, BIO_MAX_PAGES); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 137 | bio = bio_alloc(GFP_NOIO, npg); |
Peng Tao | 74a6eeb | 2012-01-12 23:18:48 +0800 | [diff] [blame] | 138 | if (!bio && (current->flags & PF_MEMALLOC)) { |
| 139 | while (!bio && (npg /= 2)) |
| 140 | bio = bio_alloc(GFP_NOIO, npg); |
| 141 | } |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 142 | |
Peng Tao | 74a6eeb | 2012-01-12 23:18:48 +0800 | [diff] [blame] | 143 | if (bio) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 144 | bio->bi_iter.bi_sector = isect - be->be_f_offset + |
| 145 | be->be_v_offset; |
Peng Tao | 74a6eeb | 2012-01-12 23:18:48 +0800 | [diff] [blame] | 146 | bio->bi_bdev = be->be_mdev; |
| 147 | bio->bi_end_io = end_io; |
| 148 | bio->bi_private = par; |
| 149 | } |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 150 | return bio; |
| 151 | } |
| 152 | |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 153 | static struct bio *do_add_page_to_bio(struct bio *bio, int npg, int rw, |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 154 | sector_t isect, struct page *page, |
| 155 | struct pnfs_block_extent *be, |
| 156 | void (*end_io)(struct bio *, int err), |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 157 | struct parallel_io *par, |
| 158 | unsigned int offset, int len) |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 159 | { |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 160 | isect = isect + (offset >> SECTOR_SHIFT); |
| 161 | dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__, |
| 162 | npg, rw, (unsigned long long)isect, offset, len); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 163 | retry: |
| 164 | if (!bio) { |
| 165 | bio = bl_alloc_init_bio(npg, isect, be, end_io, par); |
| 166 | if (!bio) |
| 167 | return ERR_PTR(-ENOMEM); |
| 168 | } |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 169 | if (bio_add_page(bio, page, len, offset) < len) { |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 170 | bio = bl_submit_bio(rw, bio); |
| 171 | goto retry; |
| 172 | } |
| 173 | return bio; |
| 174 | } |
| 175 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 176 | static void bl_end_io_read(struct bio *bio, int err) |
| 177 | { |
| 178 | struct parallel_io *par = bio->bi_private; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 179 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 180 | if (err) { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 181 | struct nfs_pgio_header *header = par->data; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 182 | |
| 183 | if (!header->pnfs_error) |
| 184 | header->pnfs_error = -EIO; |
| 185 | pnfs_set_lo_fail(header->lseg); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 186 | } |
Christoph Hellwig | 8c792ea | 2014-09-10 08:23:33 -0700 | [diff] [blame^] | 187 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 188 | bio_put(bio); |
| 189 | put_parallel(par); |
| 190 | } |
| 191 | |
| 192 | static void bl_read_cleanup(struct work_struct *work) |
| 193 | { |
| 194 | struct rpc_task *task; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 195 | struct nfs_pgio_header *hdr; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 196 | dprintk("%s enter\n", __func__); |
| 197 | task = container_of(work, struct rpc_task, u.tk_work); |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 198 | hdr = container_of(task, struct nfs_pgio_header, task); |
| 199 | pnfs_ld_read_done(hdr); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 203 | bl_end_par_io_read(void *data, int unused) |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 204 | { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 205 | struct nfs_pgio_header *hdr = data; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 206 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 207 | hdr->task.tk_status = hdr->pnfs_error; |
| 208 | INIT_WORK(&hdr->task.u.tk_work, bl_read_cleanup); |
| 209 | schedule_work(&hdr->task.u.tk_work); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 212 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 213 | bl_read_pagelist(struct nfs_pgio_header *hdr) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 214 | { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 215 | struct nfs_pgio_header *header = hdr; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 216 | int i, hole; |
| 217 | struct bio *bio = NULL; |
| 218 | struct pnfs_block_extent *be = NULL, *cow_read = NULL; |
| 219 | sector_t isect, extent_length = 0; |
| 220 | struct parallel_io *par; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 221 | loff_t f_offset = hdr->args.offset; |
| 222 | size_t bytes_left = hdr->args.count; |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 223 | unsigned int pg_offset, pg_len; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 224 | struct page **pages = hdr->args.pages; |
| 225 | int pg_index = hdr->args.pgbase >> PAGE_CACHE_SHIFT; |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 226 | const bool is_dio = (header->dreq != NULL); |
Christoph Hellwig | be98fd0 | 2014-08-21 11:09:28 -0500 | [diff] [blame] | 227 | struct blk_plug plug; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 228 | |
Trond Myklebust | 6f00866 | 2012-03-20 14:12:46 -0400 | [diff] [blame] | 229 | dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 230 | hdr->page_array.npages, f_offset, |
| 231 | (unsigned int)hdr->args.count); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 232 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 233 | par = alloc_parallel(hdr); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 234 | if (!par) |
| 235 | goto use_mds; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 236 | par->pnfs_callback = bl_end_par_io_read; |
| 237 | /* At this point, we can no longer jump to use_mds */ |
| 238 | |
Christoph Hellwig | be98fd0 | 2014-08-21 11:09:28 -0500 | [diff] [blame] | 239 | blk_start_plug(&plug); |
| 240 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 241 | isect = (sector_t) (f_offset >> SECTOR_SHIFT); |
| 242 | /* Code assumes extents are page-aligned */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 243 | for (i = pg_index; i < hdr->page_array.npages; i++) { |
Christoph Hellwig | 921b81a | 2014-08-21 11:09:29 -0500 | [diff] [blame] | 244 | if (extent_length <= 0) { |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 245 | /* We've used up the previous extent */ |
| 246 | bl_put_extent(be); |
| 247 | bl_put_extent(cow_read); |
| 248 | bio = bl_submit_bio(READ, bio); |
| 249 | /* Get the next one */ |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 250 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 251 | isect, &cow_read); |
| 252 | if (!be) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 253 | header->pnfs_error = -EIO; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 254 | goto out; |
| 255 | } |
| 256 | extent_length = be->be_length - |
| 257 | (isect - be->be_f_offset); |
| 258 | if (cow_read) { |
| 259 | sector_t cow_length = cow_read->be_length - |
| 260 | (isect - cow_read->be_f_offset); |
| 261 | extent_length = min(extent_length, cow_length); |
| 262 | } |
| 263 | } |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 264 | |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 265 | pg_offset = f_offset & ~PAGE_CACHE_MASK; |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 266 | if (is_dio) { |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 267 | if (pg_offset + bytes_left > PAGE_CACHE_SIZE) |
| 268 | pg_len = PAGE_CACHE_SIZE - pg_offset; |
| 269 | else |
| 270 | pg_len = bytes_left; |
| 271 | |
| 272 | f_offset += pg_len; |
| 273 | bytes_left -= pg_len; |
| 274 | isect += (pg_offset >> SECTOR_SHIFT); |
Christoph Hellwig | 921b81a | 2014-08-21 11:09:29 -0500 | [diff] [blame] | 275 | extent_length -= (pg_offset >> SECTOR_SHIFT); |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 276 | } else { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 277 | BUG_ON(pg_offset != 0); |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 278 | pg_len = PAGE_CACHE_SIZE; |
| 279 | } |
| 280 | |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 281 | hole = is_hole(be, isect); |
| 282 | if (hole && !cow_read) { |
| 283 | bio = bl_submit_bio(READ, bio); |
| 284 | /* Fill hole w/ zeroes w/o accessing device */ |
| 285 | dprintk("%s Zeroing page for hole\n", __func__); |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 286 | zero_user_segment(pages[i], pg_offset, pg_len); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 287 | } else { |
| 288 | struct pnfs_block_extent *be_read; |
| 289 | |
| 290 | be_read = (hole && cow_read) ? cow_read : be; |
Weston Andros Adamson | 823b0c9 | 2014-06-09 11:48:34 -0400 | [diff] [blame] | 291 | bio = do_add_page_to_bio(bio, |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 292 | hdr->page_array.npages - i, |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 293 | READ, |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 294 | isect, pages[i], be_read, |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 295 | bl_end_io_read, par, |
| 296 | pg_offset, pg_len); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 297 | if (IS_ERR(bio)) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 298 | header->pnfs_error = PTR_ERR(bio); |
Peng Tao | e6d05a7 | 2011-09-22 21:50:16 -0400 | [diff] [blame] | 299 | bio = NULL; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 300 | goto out; |
| 301 | } |
| 302 | } |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 303 | isect += (pg_len >> SECTOR_SHIFT); |
Christoph Hellwig | 921b81a | 2014-08-21 11:09:29 -0500 | [diff] [blame] | 304 | extent_length -= (pg_len >> SECTOR_SHIFT); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 305 | } |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 306 | if ((isect << SECTOR_SHIFT) >= header->inode->i_size) { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 307 | hdr->res.eof = 1; |
| 308 | hdr->res.count = header->inode->i_size - hdr->args.offset; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 309 | } else { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 310 | hdr->res.count = (isect << SECTOR_SHIFT) - hdr->args.offset; |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 311 | } |
| 312 | out: |
| 313 | bl_put_extent(be); |
| 314 | bl_put_extent(cow_read); |
| 315 | bl_submit_bio(READ, bio); |
Christoph Hellwig | be98fd0 | 2014-08-21 11:09:28 -0500 | [diff] [blame] | 316 | blk_finish_plug(&plug); |
Fred Isaman | 9549ec0 | 2011-07-30 20:52:53 -0400 | [diff] [blame] | 317 | put_parallel(par); |
| 318 | return PNFS_ATTEMPTED; |
| 319 | |
| 320 | use_mds: |
| 321 | dprintk("Giving up and using normal NFS\n"); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 322 | return PNFS_NOT_ATTEMPTED; |
| 323 | } |
| 324 | |
Fred Isaman | 31e6306 | 2011-07-30 20:52:55 -0400 | [diff] [blame] | 325 | static void mark_extents_written(struct pnfs_block_layout *bl, |
| 326 | __u64 offset, __u32 count) |
| 327 | { |
| 328 | sector_t isect, end; |
| 329 | struct pnfs_block_extent *be; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 330 | struct pnfs_block_short_extent *se; |
Fred Isaman | 31e6306 | 2011-07-30 20:52:55 -0400 | [diff] [blame] | 331 | |
| 332 | dprintk("%s(%llu, %u)\n", __func__, offset, count); |
| 333 | if (count == 0) |
| 334 | return; |
| 335 | isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT; |
| 336 | end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK); |
| 337 | end >>= SECTOR_SHIFT; |
| 338 | while (isect < end) { |
| 339 | sector_t len; |
| 340 | be = bl_find_get_extent(bl, isect, NULL); |
| 341 | BUG_ON(!be); /* FIXME */ |
| 342 | len = min(end, be->be_f_offset + be->be_length) - isect; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 343 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
| 344 | se = bl_pop_one_short_extent(be->be_inval); |
| 345 | BUG_ON(!se); |
| 346 | bl_mark_for_commit(be, isect, len, se); |
| 347 | } |
Fred Isaman | 31e6306 | 2011-07-30 20:52:55 -0400 | [diff] [blame] | 348 | isect += len; |
| 349 | bl_put_extent(be); |
| 350 | } |
| 351 | } |
| 352 | |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 353 | static void bl_end_io_write(struct bio *bio, int err) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 354 | { |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 355 | struct parallel_io *par = bio->bi_private; |
| 356 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 357 | struct nfs_pgio_header *header = par->data; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 358 | |
| 359 | if (!uptodate) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 360 | if (!header->pnfs_error) |
| 361 | header->pnfs_error = -EIO; |
| 362 | pnfs_set_lo_fail(header->lseg); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 363 | } |
| 364 | bio_put(bio); |
| 365 | put_parallel(par); |
| 366 | } |
| 367 | |
| 368 | /* Function scheduled for call during bl_end_par_io_write, |
| 369 | * it marks sectors as written and extends the commitlist. |
| 370 | */ |
| 371 | static void bl_write_cleanup(struct work_struct *work) |
| 372 | { |
| 373 | struct rpc_task *task; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 374 | struct nfs_pgio_header *hdr; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 375 | dprintk("%s enter\n", __func__); |
| 376 | task = container_of(work, struct rpc_task, u.tk_work); |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 377 | hdr = container_of(task, struct nfs_pgio_header, task); |
| 378 | if (likely(!hdr->pnfs_error)) { |
Fred Isaman | 31e6306 | 2011-07-30 20:52:55 -0400 | [diff] [blame] | 379 | /* Marks for LAYOUTCOMMIT */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 380 | mark_extents_written(BLK_LSEG2EXT(hdr->lseg), |
| 381 | hdr->args.offset, hdr->args.count); |
Fred Isaman | 31e6306 | 2011-07-30 20:52:55 -0400 | [diff] [blame] | 382 | } |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 383 | pnfs_ld_write_done(hdr); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* Called when last of bios associated with a bl_write_pagelist call finishes */ |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 387 | static void bl_end_par_io_write(void *data, int num_se) |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 388 | { |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 389 | struct nfs_pgio_header *hdr = data; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 390 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 391 | if (unlikely(hdr->pnfs_error)) { |
| 392 | bl_free_short_extents(&BLK_LSEG2EXT(hdr->lseg)->bl_inval, |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 393 | num_se); |
| 394 | } |
| 395 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 396 | hdr->task.tk_status = hdr->pnfs_error; |
Weston Andros Adamson | c65e625 | 2014-06-09 11:48:36 -0400 | [diff] [blame] | 397 | hdr->verf.committed = NFS_FILE_SYNC; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 398 | INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup); |
| 399 | schedule_work(&hdr->task.u.tk_work); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static enum pnfs_try_status |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 403 | bl_write_pagelist(struct nfs_pgio_header *header, int sync) |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 404 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 405 | int i, ret; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 406 | struct bio *bio = NULL; |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 407 | struct pnfs_block_extent *be = NULL; |
| 408 | sector_t isect, extent_length = 0; |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 409 | struct parallel_io *par = NULL; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 410 | loff_t offset = header->args.offset; |
| 411 | size_t count = header->args.count; |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 412 | struct page **pages = header->args.pages; |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 413 | int pg_index = pg_index = header->args.pgbase >> PAGE_CACHE_SHIFT; |
Christoph Hellwig | be98fd0 | 2014-08-21 11:09:28 -0500 | [diff] [blame] | 414 | struct blk_plug plug; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 415 | |
| 416 | dprintk("%s enter, %Zu@%lld\n", __func__, count, offset); |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 417 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 418 | /* At this point, header->page_aray is a (sequential) list of nfs_pages. |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 419 | * We want to write each, and if there is an error set pnfs_error |
| 420 | * to have it redone using nfs. |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 421 | */ |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 422 | par = alloc_parallel(header); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 423 | if (!par) |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 424 | goto out_mds; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 425 | par->pnfs_callback = bl_end_par_io_write; |
| 426 | /* At this point, have to be more careful with error handling */ |
| 427 | |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 428 | blk_start_plug(&plug); |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 429 | |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 430 | /* we always write out the whole page */ |
| 431 | offset = offset & (loff_t)PAGE_CACHE_MASK; |
| 432 | isect = offset >> SECTOR_SHIFT; |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 433 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 434 | for (i = pg_index; i < header->page_array.npages; i++) { |
Christoph Hellwig | 921b81a | 2014-08-21 11:09:29 -0500 | [diff] [blame] | 435 | if (extent_length <= 0) { |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 436 | /* We've used up the previous extent */ |
| 437 | bl_put_extent(be); |
| 438 | bio = bl_submit_bio(WRITE, bio); |
| 439 | /* Get the next one */ |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 440 | be = bl_find_get_extent(BLK_LSEG2EXT(header->lseg), |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 441 | isect, NULL); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 442 | if (!be || !is_writable(be, isect)) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 443 | header->pnfs_error = -EINVAL; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 444 | goto out; |
| 445 | } |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 446 | if (be->be_state == PNFS_BLOCK_INVALID_DATA) { |
| 447 | if (likely(!bl_push_one_short_extent( |
| 448 | be->be_inval))) |
| 449 | par->bse_count++; |
| 450 | else { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 451 | header->pnfs_error = -ENOMEM; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 452 | goto out; |
| 453 | } |
| 454 | } |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 455 | extent_length = be->be_length - |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 456 | (isect - be->be_f_offset); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 457 | } |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 458 | |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 459 | BUG_ON(offset & ~PAGE_CACHE_MASK); |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 460 | |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 461 | if (be->be_state == PNFS_BLOCK_INVALID_DATA && |
| 462 | !bl_is_sector_init(be->be_inval, isect)) { |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 463 | ret = bl_mark_sectors_init(be->be_inval, isect, |
Peng Tao | 60c52e3 | 2012-01-12 23:18:40 +0800 | [diff] [blame] | 464 | PAGE_CACHE_SECTORS); |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 465 | if (unlikely(ret)) { |
| 466 | dprintk("%s bl_mark_sectors_init fail %d\n", |
| 467 | __func__, ret); |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 468 | header->pnfs_error = ret; |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 469 | goto out; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 470 | } |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 471 | } |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 472 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 473 | bio = do_add_page_to_bio(bio, header->page_array.npages - i, |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 474 | WRITE, isect, pages[i], be, |
Peng Tao | fe6e1e8 | 2012-08-24 00:27:51 +0800 | [diff] [blame] | 475 | bl_end_io_write, par, |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 476 | 0, PAGE_CACHE_SIZE); |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 477 | if (IS_ERR(bio)) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 478 | header->pnfs_error = PTR_ERR(bio); |
Peng Tao | e6d05a7 | 2011-09-22 21:50:16 -0400 | [diff] [blame] | 479 | bio = NULL; |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 480 | goto out; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 481 | } |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 482 | offset += PAGE_CACHE_SIZE; |
| 483 | count -= PAGE_CACHE_SIZE; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 484 | isect += PAGE_CACHE_SECTORS; |
| 485 | extent_length -= PAGE_CACHE_SECTORS; |
| 486 | } |
Peng Tao | 71cdd40 | 2011-07-30 20:52:56 -0400 | [diff] [blame] | 487 | |
Weston Andros Adamson | d45f60c | 2014-06-09 11:48:35 -0400 | [diff] [blame] | 488 | header->res.count = header->args.count; |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 489 | out: |
| 490 | bl_put_extent(be); |
| 491 | bl_submit_bio(WRITE, bio); |
Christoph Hellwig | be98fd0 | 2014-08-21 11:09:28 -0500 | [diff] [blame] | 492 | blk_finish_plug(&plug); |
Fred Isaman | 650e2d3 | 2011-07-30 20:52:54 -0400 | [diff] [blame] | 493 | put_parallel(par); |
| 494 | return PNFS_ATTEMPTED; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 495 | out_mds: |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 496 | return PNFS_NOT_ATTEMPTED; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 497 | } |
| 498 | |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 499 | /* FIXME - range ignored */ |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 500 | static void |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 501 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 502 | { |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 503 | int i; |
| 504 | struct pnfs_block_extent *be; |
| 505 | |
| 506 | spin_lock(&bl->bl_ext_lock); |
| 507 | for (i = 0; i < EXTENT_LISTS; i++) { |
| 508 | while (!list_empty(&bl->bl_extents[i])) { |
| 509 | be = list_first_entry(&bl->bl_extents[i], |
| 510 | struct pnfs_block_extent, |
| 511 | be_node); |
| 512 | list_del(&be->be_node); |
| 513 | bl_put_extent(be); |
| 514 | } |
| 515 | } |
| 516 | spin_unlock(&bl->bl_ext_lock); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 519 | static void |
| 520 | release_inval_marks(struct pnfs_inval_markings *marks) |
| 521 | { |
Fred Isaman | c1c2a4c | 2011-07-30 20:52:49 -0400 | [diff] [blame] | 522 | struct pnfs_inval_tracking *pos, *temp; |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 523 | struct pnfs_block_short_extent *se, *stemp; |
Fred Isaman | c1c2a4c | 2011-07-30 20:52:49 -0400 | [diff] [blame] | 524 | |
| 525 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { |
| 526 | list_del(&pos->it_link); |
| 527 | kfree(pos); |
| 528 | } |
Peng Tao | 7c5465d | 2012-01-12 23:18:46 +0800 | [diff] [blame] | 529 | |
| 530 | list_for_each_entry_safe(se, stemp, &marks->im_extents, bse_node) { |
| 531 | list_del(&se->bse_node); |
| 532 | kfree(se); |
| 533 | } |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | |
| 537 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 538 | { |
| 539 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); |
| 540 | |
| 541 | dprintk("%s enter\n", __func__); |
| 542 | release_extents(bl, NULL); |
| 543 | release_inval_marks(&bl->bl_inval); |
| 544 | kfree(bl); |
| 545 | } |
| 546 | |
| 547 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, |
| 548 | gfp_t gfp_flags) |
| 549 | { |
| 550 | struct pnfs_block_layout *bl; |
| 551 | |
| 552 | dprintk("%s enter\n", __func__); |
| 553 | bl = kzalloc(sizeof(*bl), gfp_flags); |
| 554 | if (!bl) |
| 555 | return NULL; |
| 556 | spin_lock_init(&bl->bl_ext_lock); |
| 557 | INIT_LIST_HEAD(&bl->bl_extents[0]); |
| 558 | INIT_LIST_HEAD(&bl->bl_extents[1]); |
| 559 | INIT_LIST_HEAD(&bl->bl_commit); |
| 560 | INIT_LIST_HEAD(&bl->bl_committing); |
| 561 | bl->bl_count = 0; |
| 562 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; |
| 563 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); |
| 564 | return &bl->bl_layout; |
| 565 | } |
| 566 | |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 567 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 568 | { |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 569 | dprintk("%s enter\n", __func__); |
| 570 | kfree(lseg); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 571 | } |
| 572 | |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 573 | /* We pretty much ignore lseg, and store all data layout wide, so we |
| 574 | * can correctly merge. |
| 575 | */ |
| 576 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, |
| 577 | struct nfs4_layoutget_res *lgr, |
| 578 | gfp_t gfp_flags) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 579 | { |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 580 | struct pnfs_layout_segment *lseg; |
| 581 | int status; |
| 582 | |
| 583 | dprintk("%s enter\n", __func__); |
| 584 | lseg = kzalloc(sizeof(*lseg), gfp_flags); |
| 585 | if (!lseg) |
| 586 | return ERR_PTR(-ENOMEM); |
| 587 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); |
| 588 | if (status) { |
| 589 | /* We don't want to call the full-blown bl_free_lseg, |
| 590 | * since on error extents were not touched. |
| 591 | */ |
| 592 | kfree(lseg); |
| 593 | return ERR_PTR(status); |
| 594 | } |
| 595 | return lseg; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void |
| 599 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, |
| 600 | const struct nfs4_layoutcommit_args *arg) |
| 601 | { |
Fred Isaman | 90ace12 | 2011-07-30 20:52:51 -0400 | [diff] [blame] | 602 | dprintk("%s enter\n", __func__); |
| 603 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static void |
| 607 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) |
| 608 | { |
Fred Isaman | b2be781 | 2011-07-30 20:52:52 -0400 | [diff] [blame] | 609 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
| 610 | |
| 611 | dprintk("%s enter\n", __func__); |
| 612 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 613 | } |
| 614 | |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 615 | static void free_blk_mountid(struct block_mount_id *mid) |
| 616 | { |
| 617 | if (mid) { |
Peng Tao | 93a3844 | 2012-01-12 23:18:47 +0800 | [diff] [blame] | 618 | struct pnfs_block_dev *dev, *tmp; |
| 619 | |
| 620 | /* No need to take bm_lock as we are last user freeing bm_devlist */ |
| 621 | list_for_each_entry_safe(dev, tmp, &mid->bm_devlist, bm_node) { |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 622 | list_del(&dev->bm_node); |
| 623 | bl_free_block_dev(dev); |
| 624 | } |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 625 | kfree(mid); |
| 626 | } |
| 627 | } |
| 628 | |
Trond Myklebust | 78e4e05 | 2012-09-18 21:02:29 -0400 | [diff] [blame] | 629 | /* This is mostly copied from the filelayout_get_device_info function. |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 630 | * It seems much of this should be at the generic pnfs level. |
| 631 | */ |
| 632 | static struct pnfs_block_dev * |
| 633 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, |
| 634 | struct nfs4_deviceid *d_id) |
| 635 | { |
| 636 | struct pnfs_device *dev; |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 637 | struct pnfs_block_dev *rv; |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 638 | u32 max_resp_sz; |
| 639 | int max_pages; |
| 640 | struct page **pages = NULL; |
| 641 | int i, rc; |
| 642 | |
| 643 | /* |
| 644 | * Use the session max response size as the basis for setting |
| 645 | * GETDEVICEINFO's maxcount |
| 646 | */ |
| 647 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
Jim Rees | 10bd295a | 2012-04-09 22:33:39 -0400 | [diff] [blame] | 648 | max_pages = nfs_page_array_len(0, max_resp_sz); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 649 | dprintk("%s max_resp_sz %u max_pages %d\n", |
| 650 | __func__, max_resp_sz, max_pages); |
| 651 | |
| 652 | dev = kmalloc(sizeof(*dev), GFP_NOFS); |
| 653 | if (!dev) { |
| 654 | dprintk("%s kmalloc failed\n", __func__); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 655 | return ERR_PTR(-ENOMEM); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Fabian Frederick | f15b504 | 2014-06-25 20:26:21 +0200 | [diff] [blame] | 658 | pages = kcalloc(max_pages, sizeof(struct page *), GFP_NOFS); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 659 | if (pages == NULL) { |
| 660 | kfree(dev); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 661 | return ERR_PTR(-ENOMEM); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 662 | } |
| 663 | for (i = 0; i < max_pages; i++) { |
| 664 | pages[i] = alloc_page(GFP_NOFS); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 665 | if (!pages[i]) { |
| 666 | rv = ERR_PTR(-ENOMEM); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 667 | goto out_free; |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 668 | } |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); |
| 672 | dev->layout_type = LAYOUT_BLOCK_VOLUME; |
| 673 | dev->pages = pages; |
| 674 | dev->pgbase = 0; |
| 675 | dev->pglen = PAGE_SIZE * max_pages; |
| 676 | dev->mincount = 0; |
Andy Adamson | 968fe25 | 2013-06-25 19:02:54 -0400 | [diff] [blame] | 677 | dev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead; |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 678 | |
| 679 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); |
Trond Myklebust | cd5875f | 2013-05-20 11:42:54 -0400 | [diff] [blame] | 680 | rc = nfs4_proc_getdeviceinfo(server, dev, NULL); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 681 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 682 | if (rc) { |
| 683 | rv = ERR_PTR(rc); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 684 | goto out_free; |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 685 | } |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 686 | |
| 687 | rv = nfs4_blk_decode_device(server, dev); |
| 688 | out_free: |
| 689 | for (i = 0; i < max_pages; i++) |
| 690 | __free_page(pages[i]); |
| 691 | kfree(pages); |
| 692 | kfree(dev); |
| 693 | return rv; |
| 694 | } |
| 695 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 696 | static int |
| 697 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) |
| 698 | { |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 699 | struct block_mount_id *b_mt_id = NULL; |
| 700 | struct pnfs_devicelist *dlist = NULL; |
| 701 | struct pnfs_block_dev *bdev; |
| 702 | LIST_HEAD(block_disklist); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 703 | int status, i; |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 704 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 705 | dprintk("%s enter\n", __func__); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 706 | |
| 707 | if (server->pnfs_blksize == 0) { |
| 708 | dprintk("%s Server did not return blksize\n", __func__); |
| 709 | return -EINVAL; |
| 710 | } |
Christoph Hellwig | e3aaf7f | 2014-08-21 11:09:26 -0500 | [diff] [blame] | 711 | if (server->pnfs_blksize > PAGE_SIZE) { |
| 712 | printk(KERN_ERR "%s: pNFS blksize %d not supported.\n", |
| 713 | __func__, server->pnfs_blksize); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 717 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); |
| 718 | if (!b_mt_id) { |
| 719 | status = -ENOMEM; |
| 720 | goto out_error; |
| 721 | } |
| 722 | /* Initialize nfs4 block layout mount id */ |
| 723 | spin_lock_init(&b_mt_id->bm_lock); |
| 724 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); |
| 725 | |
| 726 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); |
| 727 | if (!dlist) { |
| 728 | status = -ENOMEM; |
| 729 | goto out_error; |
| 730 | } |
| 731 | dlist->eof = 0; |
| 732 | while (!dlist->eof) { |
| 733 | status = nfs4_proc_getdevicelist(server, fh, dlist); |
| 734 | if (status) |
| 735 | goto out_error; |
| 736 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", |
| 737 | __func__, dlist->num_devs, dlist->eof); |
| 738 | for (i = 0; i < dlist->num_devs; i++) { |
| 739 | bdev = nfs4_blk_get_deviceinfo(server, fh, |
| 740 | &dlist->dev_id[i]); |
Jim Rees | 516f2e2 | 2011-09-22 21:50:08 -0400 | [diff] [blame] | 741 | if (IS_ERR(bdev)) { |
| 742 | status = PTR_ERR(bdev); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 743 | goto out_error; |
| 744 | } |
| 745 | spin_lock(&b_mt_id->bm_lock); |
| 746 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); |
| 747 | spin_unlock(&b_mt_id->bm_lock); |
| 748 | } |
| 749 | } |
| 750 | dprintk("%s SUCCESS\n", __func__); |
| 751 | server->pnfs_ld_data = b_mt_id; |
| 752 | |
| 753 | out_return: |
| 754 | kfree(dlist); |
| 755 | return status; |
| 756 | |
| 757 | out_error: |
| 758 | free_blk_mountid(b_mt_id); |
| 759 | goto out_return; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static int |
| 763 | bl_clear_layoutdriver(struct nfs_server *server) |
| 764 | { |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 765 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
| 766 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 767 | dprintk("%s enter\n", __func__); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 768 | free_blk_mountid(b_mt_id); |
| 769 | dprintk("%s RETURNS\n", __func__); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 773 | static bool |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 774 | is_aligned_req(struct nfs_pageio_descriptor *pgio, |
| 775 | struct nfs_page *req, unsigned int alignment) |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 776 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 777 | /* |
| 778 | * Always accept buffered writes, higher layers take care of the |
| 779 | * right alignment. |
| 780 | */ |
| 781 | if (pgio->pg_dreq == NULL) |
| 782 | return true; |
| 783 | |
| 784 | if (!IS_ALIGNED(req->wb_offset, alignment)) |
| 785 | return false; |
| 786 | |
| 787 | if (IS_ALIGNED(req->wb_bytes, alignment)) |
| 788 | return true; |
| 789 | |
| 790 | if (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode)) { |
| 791 | /* |
| 792 | * If the write goes up to the inode size, just write |
| 793 | * the full page. Data past the inode size is |
| 794 | * guaranteed to be zeroed by the higher level client |
| 795 | * code, and this behaviour is mandated by RFC 5663 |
| 796 | * section 2.3.2. |
| 797 | */ |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | return false; |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static void |
| 805 | bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
| 806 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 807 | if (!is_aligned_req(pgio, req, SECTOR_SIZE)) { |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 808 | nfs_pageio_reset_read_mds(pgio); |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 809 | return; |
| 810 | } |
| 811 | |
| 812 | pnfs_generic_pg_init_read(pgio, req); |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 813 | } |
| 814 | |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 815 | /* |
| 816 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number |
| 817 | * of bytes (maximum @req->wb_bytes) that can be coalesced. |
| 818 | */ |
| 819 | static size_t |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 820 | bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
| 821 | struct nfs_page *req) |
| 822 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 823 | if (!is_aligned_req(pgio, req, SECTOR_SIZE)) |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 824 | return 0; |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 825 | return pnfs_generic_pg_test(pgio, prev, req); |
| 826 | } |
| 827 | |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 828 | /* |
| 829 | * Return the number of contiguous bytes for a given inode |
| 830 | * starting at page frame idx. |
| 831 | */ |
| 832 | static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx) |
| 833 | { |
| 834 | struct address_space *mapping = inode->i_mapping; |
| 835 | pgoff_t end; |
| 836 | |
| 837 | /* Optimize common case that writes from 0 to end of file */ |
| 838 | end = DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE); |
| 839 | if (end != NFS_I(inode)->npages) { |
| 840 | rcu_read_lock(); |
Johannes Weiner | e7b563b | 2014-04-03 14:47:44 -0700 | [diff] [blame] | 841 | end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX); |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 842 | rcu_read_unlock(); |
| 843 | } |
| 844 | |
| 845 | if (!end) |
| 846 | return i_size_read(inode) - (idx << PAGE_CACHE_SHIFT); |
| 847 | else |
| 848 | return (end - idx) << PAGE_CACHE_SHIFT; |
| 849 | } |
| 850 | |
Trond Myklebust | 6f018ef | 2012-10-02 08:29:14 -0700 | [diff] [blame] | 851 | static void |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 852 | bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) |
| 853 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 854 | u64 wb_size; |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 855 | |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 856 | if (!is_aligned_req(pgio, req, PAGE_SIZE)) { |
| 857 | nfs_pageio_reset_write_mds(pgio); |
| 858 | return; |
Peng Tao | 6296556 | 2012-09-25 14:55:57 +0800 | [diff] [blame] | 859 | } |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 860 | |
| 861 | if (pgio->pg_dreq == NULL) |
| 862 | wb_size = pnfs_num_cont_bytes(pgio->pg_inode, |
| 863 | req->wb_index); |
| 864 | else |
| 865 | wb_size = nfs_dreq_bytes_left(pgio->pg_dreq); |
| 866 | |
| 867 | pnfs_generic_pg_init_write(pgio, req, wb_size); |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 868 | } |
| 869 | |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 870 | /* |
| 871 | * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number |
| 872 | * of bytes (maximum @req->wb_bytes) that can be coalesced. |
| 873 | */ |
| 874 | static size_t |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 875 | bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
| 876 | struct nfs_page *req) |
| 877 | { |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 878 | if (!is_aligned_req(pgio, req, PAGE_SIZE)) |
Weston Andros Adamson | b4fdac1 | 2014-05-15 11:56:43 -0400 | [diff] [blame] | 879 | return 0; |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 880 | return pnfs_generic_pg_test(pgio, prev, req); |
| 881 | } |
| 882 | |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 883 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
Peng Tao | f742dc4 | 2012-08-24 00:27:52 +0800 | [diff] [blame] | 884 | .pg_init = bl_pg_init_read, |
| 885 | .pg_test = bl_pg_test_read, |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 886 | .pg_doio = pnfs_generic_pg_readpages, |
| 887 | }; |
| 888 | |
| 889 | static const struct nfs_pageio_ops bl_pg_write_ops = { |
Peng Tao | 96c9eae | 2012-08-24 00:27:53 +0800 | [diff] [blame] | 890 | .pg_init = bl_pg_init_write, |
| 891 | .pg_test = bl_pg_test_write, |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 892 | .pg_doio = pnfs_generic_pg_writepages, |
| 893 | }; |
| 894 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 895 | static struct pnfs_layoutdriver_type blocklayout_type = { |
| 896 | .id = LAYOUT_BLOCK_VOLUME, |
| 897 | .name = "LAYOUT_BLOCK_VOLUME", |
fanchaoting | 5a12cca | 2013-02-04 21:15:02 +0800 | [diff] [blame] | 898 | .owner = THIS_MODULE, |
Christoph Hellwig | 3a6fd1f | 2014-09-10 08:23:32 -0700 | [diff] [blame] | 899 | .flags = PNFS_READ_WHOLE_PAGE, |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 900 | .read_pagelist = bl_read_pagelist, |
| 901 | .write_pagelist = bl_write_pagelist, |
| 902 | .alloc_layout_hdr = bl_alloc_layout_hdr, |
| 903 | .free_layout_hdr = bl_free_layout_hdr, |
| 904 | .alloc_lseg = bl_alloc_lseg, |
| 905 | .free_lseg = bl_free_lseg, |
| 906 | .encode_layoutcommit = bl_encode_layoutcommit, |
| 907 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, |
| 908 | .set_layoutdriver = bl_set_layoutdriver, |
| 909 | .clear_layoutdriver = bl_clear_layoutdriver, |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 910 | .pg_read_ops = &bl_pg_read_ops, |
| 911 | .pg_write_ops = &bl_pg_write_ops, |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 912 | }; |
| 913 | |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 914 | static const struct rpc_pipe_ops bl_upcall_ops = { |
Peng Tao | c122515 | 2011-09-22 21:50:10 -0400 | [diff] [blame] | 915 | .upcall = rpc_pipe_generic_upcall, |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 916 | .downcall = bl_pipe_downcall, |
| 917 | .destroy_msg = bl_pipe_destroy_msg, |
| 918 | }; |
| 919 | |
Stanislav Kinsbursky | 332dfab | 2012-01-10 17:04:16 +0400 | [diff] [blame] | 920 | static struct dentry *nfs4blocklayout_register_sb(struct super_block *sb, |
| 921 | struct rpc_pipe *pipe) |
| 922 | { |
| 923 | struct dentry *dir, *dentry; |
| 924 | |
| 925 | dir = rpc_d_lookup_sb(sb, NFS_PIPE_DIRNAME); |
| 926 | if (dir == NULL) |
| 927 | return ERR_PTR(-ENOENT); |
| 928 | dentry = rpc_mkpipe_dentry(dir, "blocklayout", NULL, pipe); |
| 929 | dput(dir); |
| 930 | return dentry; |
| 931 | } |
| 932 | |
| 933 | static void nfs4blocklayout_unregister_sb(struct super_block *sb, |
| 934 | struct rpc_pipe *pipe) |
| 935 | { |
| 936 | if (pipe->dentry) |
| 937 | rpc_unlink(pipe->dentry); |
| 938 | } |
| 939 | |
Stanislav Kinsbursky | 627f306 | 2012-01-10 17:04:32 +0400 | [diff] [blame] | 940 | static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event, |
| 941 | void *ptr) |
| 942 | { |
| 943 | struct super_block *sb = ptr; |
| 944 | struct net *net = sb->s_fs_info; |
| 945 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 946 | struct dentry *dentry; |
| 947 | int ret = 0; |
| 948 | |
| 949 | if (!try_module_get(THIS_MODULE)) |
| 950 | return 0; |
| 951 | |
| 952 | if (nn->bl_device_pipe == NULL) { |
| 953 | module_put(THIS_MODULE); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | switch (event) { |
| 958 | case RPC_PIPEFS_MOUNT: |
| 959 | dentry = nfs4blocklayout_register_sb(sb, nn->bl_device_pipe); |
| 960 | if (IS_ERR(dentry)) { |
| 961 | ret = PTR_ERR(dentry); |
| 962 | break; |
| 963 | } |
| 964 | nn->bl_device_pipe->dentry = dentry; |
| 965 | break; |
| 966 | case RPC_PIPEFS_UMOUNT: |
| 967 | if (nn->bl_device_pipe->dentry) |
| 968 | nfs4blocklayout_unregister_sb(sb, nn->bl_device_pipe); |
| 969 | break; |
| 970 | default: |
| 971 | ret = -ENOTSUPP; |
| 972 | break; |
| 973 | } |
| 974 | module_put(THIS_MODULE); |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | static struct notifier_block nfs4blocklayout_block = { |
| 979 | .notifier_call = rpc_pipefs_event, |
| 980 | }; |
| 981 | |
Stanislav Kinsbursky | 332dfab | 2012-01-10 17:04:16 +0400 | [diff] [blame] | 982 | static struct dentry *nfs4blocklayout_register_net(struct net *net, |
| 983 | struct rpc_pipe *pipe) |
| 984 | { |
| 985 | struct super_block *pipefs_sb; |
| 986 | struct dentry *dentry; |
| 987 | |
| 988 | pipefs_sb = rpc_get_sb_net(net); |
| 989 | if (!pipefs_sb) |
Stanislav Kinsbursky | 2561d61 | 2012-01-10 17:04:40 +0400 | [diff] [blame] | 990 | return NULL; |
Stanislav Kinsbursky | 332dfab | 2012-01-10 17:04:16 +0400 | [diff] [blame] | 991 | dentry = nfs4blocklayout_register_sb(pipefs_sb, pipe); |
| 992 | rpc_put_sb_net(net); |
| 993 | return dentry; |
| 994 | } |
| 995 | |
| 996 | static void nfs4blocklayout_unregister_net(struct net *net, |
| 997 | struct rpc_pipe *pipe) |
| 998 | { |
| 999 | struct super_block *pipefs_sb; |
| 1000 | |
| 1001 | pipefs_sb = rpc_get_sb_net(net); |
| 1002 | if (pipefs_sb) { |
| 1003 | nfs4blocklayout_unregister_sb(pipefs_sb, pipe); |
| 1004 | rpc_put_sb_net(net); |
| 1005 | } |
| 1006 | } |
| 1007 | |
Stanislav Kinsbursky | 9e2e74d | 2012-01-10 17:04:24 +0400 | [diff] [blame] | 1008 | static int nfs4blocklayout_net_init(struct net *net) |
| 1009 | { |
| 1010 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 1011 | struct dentry *dentry; |
| 1012 | |
Stanislav Kinsbursky | 5ffaf85 | 2012-03-11 18:20:31 +0400 | [diff] [blame] | 1013 | init_waitqueue_head(&nn->bl_wq); |
Stanislav Kinsbursky | 9e2e74d | 2012-01-10 17:04:24 +0400 | [diff] [blame] | 1014 | nn->bl_device_pipe = rpc_mkpipe_data(&bl_upcall_ops, 0); |
| 1015 | if (IS_ERR(nn->bl_device_pipe)) |
| 1016 | return PTR_ERR(nn->bl_device_pipe); |
| 1017 | dentry = nfs4blocklayout_register_net(net, nn->bl_device_pipe); |
| 1018 | if (IS_ERR(dentry)) { |
| 1019 | rpc_destroy_pipe_data(nn->bl_device_pipe); |
| 1020 | return PTR_ERR(dentry); |
| 1021 | } |
| 1022 | nn->bl_device_pipe->dentry = dentry; |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static void nfs4blocklayout_net_exit(struct net *net) |
| 1027 | { |
| 1028 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 1029 | |
| 1030 | nfs4blocklayout_unregister_net(net, nn->bl_device_pipe); |
| 1031 | rpc_destroy_pipe_data(nn->bl_device_pipe); |
| 1032 | nn->bl_device_pipe = NULL; |
| 1033 | } |
| 1034 | |
| 1035 | static struct pernet_operations nfs4blocklayout_net_ops = { |
| 1036 | .init = nfs4blocklayout_net_init, |
| 1037 | .exit = nfs4blocklayout_net_exit, |
| 1038 | }; |
| 1039 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 1040 | static int __init nfs4blocklayout_init(void) |
| 1041 | { |
| 1042 | int ret; |
| 1043 | |
| 1044 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); |
| 1045 | |
| 1046 | ret = pnfs_register_layoutdriver(&blocklayout_type); |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 1047 | if (ret) |
| 1048 | goto out; |
| 1049 | |
Stanislav Kinsbursky | 627f306 | 2012-01-10 17:04:32 +0400 | [diff] [blame] | 1050 | ret = rpc_pipefs_notifier_register(&nfs4blocklayout_block); |
Stanislav Kinsbursky | 9e2e74d | 2012-01-10 17:04:24 +0400 | [diff] [blame] | 1051 | if (ret) |
| 1052 | goto out_remove; |
Stanislav Kinsbursky | 627f306 | 2012-01-10 17:04:32 +0400 | [diff] [blame] | 1053 | ret = register_pernet_subsys(&nfs4blocklayout_net_ops); |
| 1054 | if (ret) |
| 1055 | goto out_notifier; |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 1056 | out: |
| 1057 | return ret; |
| 1058 | |
Stanislav Kinsbursky | 627f306 | 2012-01-10 17:04:32 +0400 | [diff] [blame] | 1059 | out_notifier: |
| 1060 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 1061 | out_remove: |
| 1062 | pnfs_unregister_layoutdriver(&blocklayout_type); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | static void __exit nfs4blocklayout_exit(void) |
| 1067 | { |
| 1068 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", |
| 1069 | __func__); |
| 1070 | |
Stanislav Kinsbursky | 627f306 | 2012-01-10 17:04:32 +0400 | [diff] [blame] | 1071 | rpc_pipefs_notifier_unregister(&nfs4blocklayout_block); |
Stanislav Kinsbursky | 9e2e74d | 2012-01-10 17:04:24 +0400 | [diff] [blame] | 1072 | unregister_pernet_subsys(&nfs4blocklayout_net_ops); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 1073 | pnfs_unregister_layoutdriver(&blocklayout_type); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | MODULE_ALIAS("nfs-layouttype4-3"); |
| 1077 | |
| 1078 | module_init(nfs4blocklayout_init); |
| 1079 | module_exit(nfs4blocklayout_exit); |