blocklayout.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/mount.h>
  35. #include <linux/namei.h>
  36. #include <linux/bio.h> /* struct bio */
  37. #include <linux/prefetch.h>
  38. #include <linux/pagevec.h>
  39. #include "../pnfs.h"
  40. #include "../nfs4session.h"
  41. #include "../internal.h"
  42. #include "blocklayout.h"
  43. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  44. MODULE_LICENSE("GPL");
  45. MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
  46. MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
  47. static bool is_hole(struct pnfs_block_extent *be)
  48. {
  49. switch (be->be_state) {
  50. case PNFS_BLOCK_NONE_DATA:
  51. return true;
  52. case PNFS_BLOCK_INVALID_DATA:
  53. return be->be_tag ? false : true;
  54. default:
  55. return false;
  56. }
  57. }
  58. /* The data we are handed might be spread across several bios. We need
  59. * to track when the last one is finished.
  60. */
  61. struct parallel_io {
  62. struct kref refcnt;
  63. void (*pnfs_callback) (void *data);
  64. void *data;
  65. };
  66. static inline struct parallel_io *alloc_parallel(void *data)
  67. {
  68. struct parallel_io *rv;
  69. rv = kmalloc(sizeof(*rv), GFP_NOFS);
  70. if (rv) {
  71. rv->data = data;
  72. kref_init(&rv->refcnt);
  73. }
  74. return rv;
  75. }
  76. static inline void get_parallel(struct parallel_io *p)
  77. {
  78. kref_get(&p->refcnt);
  79. }
  80. static void destroy_parallel(struct kref *kref)
  81. {
  82. struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
  83. dprintk("%s enter\n", __func__);
  84. p->pnfs_callback(p->data);
  85. kfree(p);
  86. }
  87. static inline void put_parallel(struct parallel_io *p)
  88. {
  89. kref_put(&p->refcnt, destroy_parallel);
  90. }
  91. static struct bio *
  92. bl_submit_bio(struct bio *bio)
  93. {
  94. if (bio) {
  95. get_parallel(bio->bi_private);
  96. dprintk("%s submitting %s bio %u@%llu\n", __func__,
  97. bio_op(bio) == READ ? "read" : "write",
  98. bio->bi_iter.bi_size,
  99. (unsigned long long)bio->bi_iter.bi_sector);
  100. submit_bio(bio);
  101. }
  102. return NULL;
  103. }
  104. static struct bio *
  105. bl_alloc_init_bio(int npg, struct block_device *bdev, sector_t disk_sector,
  106. bio_end_io_t end_io, struct parallel_io *par)
  107. {
  108. struct bio *bio;
  109. npg = min(npg, BIO_MAX_PAGES);
  110. bio = bio_alloc(GFP_NOIO, npg);
  111. if (!bio && (current->flags & PF_MEMALLOC)) {
  112. while (!bio && (npg /= 2))
  113. bio = bio_alloc(GFP_NOIO, npg);
  114. }
  115. if (bio) {
  116. bio->bi_iter.bi_sector = disk_sector;
  117. bio->bi_bdev = bdev;
  118. bio->bi_end_io = end_io;
  119. bio->bi_private = par;
  120. }
  121. return bio;
  122. }
  123. static struct bio *
  124. do_add_page_to_bio(struct bio *bio, int npg, int rw, sector_t isect,
  125. struct page *page, struct pnfs_block_dev_map *map,
  126. struct pnfs_block_extent *be, bio_end_io_t end_io,
  127. struct parallel_io *par, unsigned int offset, int *len)
  128. {
  129. struct pnfs_block_dev *dev =
  130. container_of(be->be_device, struct pnfs_block_dev, node);
  131. u64 disk_addr, end;
  132. dprintk("%s: npg %d rw %d isect %llu offset %u len %d\n", __func__,
  133. npg, rw, (unsigned long long)isect, offset, *len);
  134. /* translate to device offset */
  135. isect += be->be_v_offset;
  136. isect -= be->be_f_offset;
  137. /* translate to physical disk offset */
  138. disk_addr = (u64)isect << SECTOR_SHIFT;
  139. if (disk_addr < map->start || disk_addr >= map->start + map->len) {
  140. if (!dev->map(dev, disk_addr, map))
  141. return ERR_PTR(-EIO);
  142. bio = bl_submit_bio(bio);
  143. }
  144. disk_addr += map->disk_offset;
  145. disk_addr -= map->start;
  146. /* limit length to what the device mapping allows */
  147. end = disk_addr + *len;
  148. if (end >= map->start + map->len)
  149. *len = map->start + map->len - disk_addr;
  150. retry:
  151. if (!bio) {
  152. bio = bl_alloc_init_bio(npg, map->bdev,
  153. disk_addr >> SECTOR_SHIFT, end_io, par);
  154. if (!bio)
  155. return ERR_PTR(-ENOMEM);
  156. bio_set_op_attrs(bio, rw, 0);
  157. }
  158. if (bio_add_page(bio, page, *len, offset) < *len) {
  159. bio = bl_submit_bio(bio);
  160. goto retry;
  161. }
  162. return bio;
  163. }
  164. static void bl_end_io_read(struct bio *bio)
  165. {
  166. struct parallel_io *par = bio->bi_private;
  167. if (bio->bi_error) {
  168. struct nfs_pgio_header *header = par->data;
  169. if (!header->pnfs_error)
  170. header->pnfs_error = -EIO;
  171. pnfs_set_lo_fail(header->lseg);
  172. }
  173. bio_put(bio);
  174. put_parallel(par);
  175. }
  176. static void bl_read_cleanup(struct work_struct *work)
  177. {
  178. struct rpc_task *task;
  179. struct nfs_pgio_header *hdr;
  180. dprintk("%s enter\n", __func__);
  181. task = container_of(work, struct rpc_task, u.tk_work);
  182. hdr = container_of(task, struct nfs_pgio_header, task);
  183. pnfs_ld_read_done(hdr);
  184. }
  185. static void
  186. bl_end_par_io_read(void *data)
  187. {
  188. struct nfs_pgio_header *hdr = data;
  189. hdr->task.tk_status = hdr->pnfs_error;
  190. INIT_WORK(&hdr->task.u.tk_work, bl_read_cleanup);
  191. schedule_work(&hdr->task.u.tk_work);
  192. }
  193. static enum pnfs_try_status
  194. bl_read_pagelist(struct nfs_pgio_header *header)
  195. {
  196. struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
  197. struct pnfs_block_dev_map map = { .start = NFS4_MAX_UINT64 };
  198. struct bio *bio = NULL;
  199. struct pnfs_block_extent be;
  200. sector_t isect, extent_length = 0;
  201. struct parallel_io *par;
  202. loff_t f_offset = header->args.offset;
  203. size_t bytes_left = header->args.count;
  204. unsigned int pg_offset = header->args.pgbase, pg_len;
  205. struct page **pages = header->args.pages;
  206. int pg_index = header->args.pgbase >> PAGE_SHIFT;
  207. const bool is_dio = (header->dreq != NULL);
  208. struct blk_plug plug;
  209. int i;
  210. dprintk("%s enter nr_pages %u offset %lld count %u\n", __func__,
  211. header->page_array.npages, f_offset,
  212. (unsigned int)header->args.count);
  213. par = alloc_parallel(header);
  214. if (!par)
  215. return PNFS_NOT_ATTEMPTED;
  216. par->pnfs_callback = bl_end_par_io_read;
  217. blk_start_plug(&plug);
  218. isect = (sector_t) (f_offset >> SECTOR_SHIFT);
  219. /* Code assumes extents are page-aligned */
  220. for (i = pg_index; i < header->page_array.npages; i++) {
  221. if (extent_length <= 0) {
  222. /* We've used up the previous extent */
  223. bio = bl_submit_bio(bio);
  224. /* Get the next one */
  225. if (!ext_tree_lookup(bl, isect, &be, false)) {
  226. header->pnfs_error = -EIO;
  227. goto out;
  228. }
  229. extent_length = be.be_length - (isect - be.be_f_offset);
  230. }
  231. if (is_dio) {
  232. if (pg_offset + bytes_left > PAGE_SIZE)
  233. pg_len = PAGE_SIZE - pg_offset;
  234. else
  235. pg_len = bytes_left;
  236. } else {
  237. BUG_ON(pg_offset != 0);
  238. pg_len = PAGE_SIZE;
  239. }
  240. if (is_hole(&be)) {
  241. bio = bl_submit_bio(bio);
  242. /* Fill hole w/ zeroes w/o accessing device */
  243. dprintk("%s Zeroing page for hole\n", __func__);
  244. zero_user_segment(pages[i], pg_offset, pg_len);
  245. /* invalidate map */
  246. map.start = NFS4_MAX_UINT64;
  247. } else {
  248. bio = do_add_page_to_bio(bio,
  249. header->page_array.npages - i,
  250. READ,
  251. isect, pages[i], &map, &be,
  252. bl_end_io_read, par,
  253. pg_offset, &pg_len);
  254. if (IS_ERR(bio)) {
  255. header->pnfs_error = PTR_ERR(bio);
  256. bio = NULL;
  257. goto out;
  258. }
  259. }
  260. isect += (pg_len >> SECTOR_SHIFT);
  261. extent_length -= (pg_len >> SECTOR_SHIFT);
  262. f_offset += pg_len;
  263. bytes_left -= pg_len;
  264. pg_offset = 0;
  265. }
  266. if ((isect << SECTOR_SHIFT) >= header->inode->i_size) {
  267. header->res.eof = 1;
  268. header->res.count = header->inode->i_size - header->args.offset;
  269. } else {
  270. header->res.count = (isect << SECTOR_SHIFT) - header->args.offset;
  271. }
  272. out:
  273. bl_submit_bio(bio);
  274. blk_finish_plug(&plug);
  275. put_parallel(par);
  276. return PNFS_ATTEMPTED;
  277. }
  278. static void bl_end_io_write(struct bio *bio)
  279. {
  280. struct parallel_io *par = bio->bi_private;
  281. struct nfs_pgio_header *header = par->data;
  282. if (bio->bi_error) {
  283. if (!header->pnfs_error)
  284. header->pnfs_error = -EIO;
  285. pnfs_set_lo_fail(header->lseg);
  286. }
  287. bio_put(bio);
  288. put_parallel(par);
  289. }
  290. /* Function scheduled for call during bl_end_par_io_write,
  291. * it marks sectors as written and extends the commitlist.
  292. */
  293. static void bl_write_cleanup(struct work_struct *work)
  294. {
  295. struct rpc_task *task = container_of(work, struct rpc_task, u.tk_work);
  296. struct nfs_pgio_header *hdr =
  297. container_of(task, struct nfs_pgio_header, task);
  298. dprintk("%s enter\n", __func__);
  299. if (likely(!hdr->pnfs_error)) {
  300. struct pnfs_block_layout *bl = BLK_LSEG2EXT(hdr->lseg);
  301. u64 start = hdr->args.offset & (loff_t)PAGE_MASK;
  302. u64 end = (hdr->args.offset + hdr->args.count +
  303. PAGE_SIZE - 1) & (loff_t)PAGE_MASK;
  304. u64 lwb = hdr->args.offset + hdr->args.count;
  305. ext_tree_mark_written(bl, start >> SECTOR_SHIFT,
  306. (end - start) >> SECTOR_SHIFT, lwb);
  307. }
  308. pnfs_ld_write_done(hdr);
  309. }
  310. /* Called when last of bios associated with a bl_write_pagelist call finishes */
  311. static void bl_end_par_io_write(void *data)
  312. {
  313. struct nfs_pgio_header *hdr = data;
  314. hdr->task.tk_status = hdr->pnfs_error;
  315. hdr->verf.committed = NFS_FILE_SYNC;
  316. INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup);
  317. schedule_work(&hdr->task.u.tk_work);
  318. }
  319. static enum pnfs_try_status
  320. bl_write_pagelist(struct nfs_pgio_header *header, int sync)
  321. {
  322. struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
  323. struct pnfs_block_dev_map map = { .start = NFS4_MAX_UINT64 };
  324. struct bio *bio = NULL;
  325. struct pnfs_block_extent be;
  326. sector_t isect, extent_length = 0;
  327. struct parallel_io *par = NULL;
  328. loff_t offset = header->args.offset;
  329. size_t count = header->args.count;
  330. struct page **pages = header->args.pages;
  331. int pg_index = header->args.pgbase >> PAGE_SHIFT;
  332. unsigned int pg_len;
  333. struct blk_plug plug;
  334. int i;
  335. dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
  336. /* At this point, header->page_aray is a (sequential) list of nfs_pages.
  337. * We want to write each, and if there is an error set pnfs_error
  338. * to have it redone using nfs.
  339. */
  340. par = alloc_parallel(header);
  341. if (!par)
  342. return PNFS_NOT_ATTEMPTED;
  343. par->pnfs_callback = bl_end_par_io_write;
  344. blk_start_plug(&plug);
  345. /* we always write out the whole page */
  346. offset = offset & (loff_t)PAGE_MASK;
  347. isect = offset >> SECTOR_SHIFT;
  348. for (i = pg_index; i < header->page_array.npages; i++) {
  349. if (extent_length <= 0) {
  350. /* We've used up the previous extent */
  351. bio = bl_submit_bio(bio);
  352. /* Get the next one */
  353. if (!ext_tree_lookup(bl, isect, &be, true)) {
  354. header->pnfs_error = -EINVAL;
  355. goto out;
  356. }
  357. extent_length = be.be_length - (isect - be.be_f_offset);
  358. }
  359. pg_len = PAGE_SIZE;
  360. bio = do_add_page_to_bio(bio, header->page_array.npages - i,
  361. WRITE, isect, pages[i], &map, &be,
  362. bl_end_io_write, par,
  363. 0, &pg_len);
  364. if (IS_ERR(bio)) {
  365. header->pnfs_error = PTR_ERR(bio);
  366. bio = NULL;
  367. goto out;
  368. }
  369. offset += pg_len;
  370. count -= pg_len;
  371. isect += (pg_len >> SECTOR_SHIFT);
  372. extent_length -= (pg_len >> SECTOR_SHIFT);
  373. }
  374. header->res.count = header->args.count;
  375. out:
  376. bl_submit_bio(bio);
  377. blk_finish_plug(&plug);
  378. put_parallel(par);
  379. return PNFS_ATTEMPTED;
  380. }
  381. static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
  382. {
  383. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  384. int err;
  385. dprintk("%s enter\n", __func__);
  386. err = ext_tree_remove(bl, true, 0, LLONG_MAX);
  387. WARN_ON(err);
  388. kfree(bl);
  389. }
  390. static struct pnfs_layout_hdr *__bl_alloc_layout_hdr(struct inode *inode,
  391. gfp_t gfp_flags, bool is_scsi_layout)
  392. {
  393. struct pnfs_block_layout *bl;
  394. dprintk("%s enter\n", __func__);
  395. bl = kzalloc(sizeof(*bl), gfp_flags);
  396. if (!bl)
  397. return NULL;
  398. bl->bl_ext_rw = RB_ROOT;
  399. bl->bl_ext_ro = RB_ROOT;
  400. spin_lock_init(&bl->bl_ext_lock);
  401. bl->bl_scsi_layout = is_scsi_layout;
  402. return &bl->bl_layout;
  403. }
  404. static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
  405. gfp_t gfp_flags)
  406. {
  407. return __bl_alloc_layout_hdr(inode, gfp_flags, false);
  408. }
  409. static struct pnfs_layout_hdr *sl_alloc_layout_hdr(struct inode *inode,
  410. gfp_t gfp_flags)
  411. {
  412. return __bl_alloc_layout_hdr(inode, gfp_flags, true);
  413. }
  414. static void bl_free_lseg(struct pnfs_layout_segment *lseg)
  415. {
  416. dprintk("%s enter\n", __func__);
  417. kfree(lseg);
  418. }
  419. /* Tracks info needed to ensure extents in layout obey constraints of spec */
  420. struct layout_verification {
  421. u32 mode; /* R or RW */
  422. u64 start; /* Expected start of next non-COW extent */
  423. u64 inval; /* Start of INVAL coverage */
  424. u64 cowread; /* End of COW read coverage */
  425. };
  426. /* Verify the extent meets the layout requirements of the pnfs-block draft,
  427. * section 2.3.1.
  428. */
  429. static int verify_extent(struct pnfs_block_extent *be,
  430. struct layout_verification *lv)
  431. {
  432. if (lv->mode == IOMODE_READ) {
  433. if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
  434. be->be_state == PNFS_BLOCK_INVALID_DATA)
  435. return -EIO;
  436. if (be->be_f_offset != lv->start)
  437. return -EIO;
  438. lv->start += be->be_length;
  439. return 0;
  440. }
  441. /* lv->mode == IOMODE_RW */
  442. if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
  443. if (be->be_f_offset != lv->start)
  444. return -EIO;
  445. if (lv->cowread > lv->start)
  446. return -EIO;
  447. lv->start += be->be_length;
  448. lv->inval = lv->start;
  449. return 0;
  450. } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  451. if (be->be_f_offset != lv->start)
  452. return -EIO;
  453. lv->start += be->be_length;
  454. return 0;
  455. } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
  456. if (be->be_f_offset > lv->start)
  457. return -EIO;
  458. if (be->be_f_offset < lv->inval)
  459. return -EIO;
  460. if (be->be_f_offset < lv->cowread)
  461. return -EIO;
  462. /* It looks like you might want to min this with lv->start,
  463. * but you really don't.
  464. */
  465. lv->inval = lv->inval + be->be_length;
  466. lv->cowread = be->be_f_offset + be->be_length;
  467. return 0;
  468. } else
  469. return -EIO;
  470. }
  471. static int decode_sector_number(__be32 **rp, sector_t *sp)
  472. {
  473. uint64_t s;
  474. *rp = xdr_decode_hyper(*rp, &s);
  475. if (s & 0x1ff) {
  476. printk(KERN_WARNING "NFS: %s: sector not aligned\n", __func__);
  477. return -1;
  478. }
  479. *sp = s >> SECTOR_SHIFT;
  480. return 0;
  481. }
  482. static int
  483. bl_alloc_extent(struct xdr_stream *xdr, struct pnfs_layout_hdr *lo,
  484. struct layout_verification *lv, struct list_head *extents,
  485. gfp_t gfp_mask)
  486. {
  487. struct pnfs_block_extent *be;
  488. struct nfs4_deviceid id;
  489. int error;
  490. __be32 *p;
  491. p = xdr_inline_decode(xdr, 28 + NFS4_DEVICEID4_SIZE);
  492. if (!p)
  493. return -EIO;
  494. be = kzalloc(sizeof(*be), GFP_NOFS);
  495. if (!be)
  496. return -ENOMEM;
  497. memcpy(&id, p, NFS4_DEVICEID4_SIZE);
  498. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  499. error = -EIO;
  500. be->be_device = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), &id,
  501. lo->plh_lc_cred, gfp_mask);
  502. if (!be->be_device)
  503. goto out_free_be;
  504. /*
  505. * The next three values are read in as bytes, but stored in the
  506. * extent structure in 512-byte granularity.
  507. */
  508. if (decode_sector_number(&p, &be->be_f_offset) < 0)
  509. goto out_put_deviceid;
  510. if (decode_sector_number(&p, &be->be_length) < 0)
  511. goto out_put_deviceid;
  512. if (decode_sector_number(&p, &be->be_v_offset) < 0)
  513. goto out_put_deviceid;
  514. be->be_state = be32_to_cpup(p++);
  515. error = verify_extent(be, lv);
  516. if (error) {
  517. dprintk("%s: extent verification failed\n", __func__);
  518. goto out_put_deviceid;
  519. }
  520. list_add_tail(&be->be_list, extents);
  521. return 0;
  522. out_put_deviceid:
  523. nfs4_put_deviceid_node(be->be_device);
  524. out_free_be:
  525. kfree(be);
  526. return error;
  527. }
  528. static struct pnfs_layout_segment *
  529. bl_alloc_lseg(struct pnfs_layout_hdr *lo, struct nfs4_layoutget_res *lgr,
  530. gfp_t gfp_mask)
  531. {
  532. struct layout_verification lv = {
  533. .mode = lgr->range.iomode,
  534. .start = lgr->range.offset >> SECTOR_SHIFT,
  535. .inval = lgr->range.offset >> SECTOR_SHIFT,
  536. .cowread = lgr->range.offset >> SECTOR_SHIFT,
  537. };
  538. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  539. struct pnfs_layout_segment *lseg;
  540. struct xdr_buf buf;
  541. struct xdr_stream xdr;
  542. struct page *scratch;
  543. int status, i;
  544. uint32_t count;
  545. __be32 *p;
  546. LIST_HEAD(extents);
  547. dprintk("---> %s\n", __func__);
  548. lseg = kzalloc(sizeof(*lseg), gfp_mask);
  549. if (!lseg)
  550. return ERR_PTR(-ENOMEM);
  551. status = -ENOMEM;
  552. scratch = alloc_page(gfp_mask);
  553. if (!scratch)
  554. goto out;
  555. xdr_init_decode_pages(&xdr, &buf,
  556. lgr->layoutp->pages, lgr->layoutp->len);
  557. xdr_set_scratch_buffer(&xdr, page_address(scratch), PAGE_SIZE);
  558. status = -EIO;
  559. p = xdr_inline_decode(&xdr, 4);
  560. if (unlikely(!p))
  561. goto out_free_scratch;
  562. count = be32_to_cpup(p++);
  563. dprintk("%s: number of extents %d\n", __func__, count);
  564. /*
  565. * Decode individual extents, putting them in temporary staging area
  566. * until whole layout is decoded to make error recovery easier.
  567. */
  568. for (i = 0; i < count; i++) {
  569. status = bl_alloc_extent(&xdr, lo, &lv, &extents, gfp_mask);
  570. if (status)
  571. goto process_extents;
  572. }
  573. if (lgr->range.offset + lgr->range.length !=
  574. lv.start << SECTOR_SHIFT) {
  575. dprintk("%s Final length mismatch\n", __func__);
  576. status = -EIO;
  577. goto process_extents;
  578. }
  579. if (lv.start < lv.cowread) {
  580. dprintk("%s Final uncovered COW extent\n", __func__);
  581. status = -EIO;
  582. }
  583. process_extents:
  584. while (!list_empty(&extents)) {
  585. struct pnfs_block_extent *be =
  586. list_first_entry(&extents, struct pnfs_block_extent,
  587. be_list);
  588. list_del(&be->be_list);
  589. if (!status)
  590. status = ext_tree_insert(bl, be);
  591. if (status) {
  592. nfs4_put_deviceid_node(be->be_device);
  593. kfree(be);
  594. }
  595. }
  596. out_free_scratch:
  597. __free_page(scratch);
  598. out:
  599. dprintk("%s returns %d\n", __func__, status);
  600. if (status) {
  601. kfree(lseg);
  602. return ERR_PTR(status);
  603. }
  604. return lseg;
  605. }
  606. static void
  607. bl_return_range(struct pnfs_layout_hdr *lo,
  608. struct pnfs_layout_range *range)
  609. {
  610. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  611. sector_t offset = range->offset >> SECTOR_SHIFT, end;
  612. if (range->offset % 8) {
  613. dprintk("%s: offset %lld not block size aligned\n",
  614. __func__, range->offset);
  615. return;
  616. }
  617. if (range->length != NFS4_MAX_UINT64) {
  618. if (range->length % 8) {
  619. dprintk("%s: length %lld not block size aligned\n",
  620. __func__, range->length);
  621. return;
  622. }
  623. end = offset + (range->length >> SECTOR_SHIFT);
  624. } else {
  625. end = round_down(NFS4_MAX_UINT64, PAGE_SIZE);
  626. }
  627. ext_tree_remove(bl, range->iomode & IOMODE_RW, offset, end);
  628. }
  629. static int
  630. bl_prepare_layoutcommit(struct nfs4_layoutcommit_args *arg)
  631. {
  632. return ext_tree_prepare_commit(arg);
  633. }
  634. static void
  635. bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
  636. {
  637. ext_tree_mark_committed(&lcdata->args, lcdata->res.status);
  638. }
  639. static int
  640. bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
  641. {
  642. dprintk("%s enter\n", __func__);
  643. if (server->pnfs_blksize == 0) {
  644. dprintk("%s Server did not return blksize\n", __func__);
  645. return -EINVAL;
  646. }
  647. if (server->pnfs_blksize > PAGE_SIZE) {
  648. printk(KERN_ERR "%s: pNFS blksize %d not supported.\n",
  649. __func__, server->pnfs_blksize);
  650. return -EINVAL;
  651. }
  652. return 0;
  653. }
  654. static bool
  655. is_aligned_req(struct nfs_pageio_descriptor *pgio,
  656. struct nfs_page *req, unsigned int alignment, bool is_write)
  657. {
  658. /*
  659. * Always accept buffered writes, higher layers take care of the
  660. * right alignment.
  661. */
  662. if (pgio->pg_dreq == NULL)
  663. return true;
  664. if (!IS_ALIGNED(req->wb_offset, alignment))
  665. return false;
  666. if (IS_ALIGNED(req->wb_bytes, alignment))
  667. return true;
  668. if (is_write &&
  669. (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode))) {
  670. /*
  671. * If the write goes up to the inode size, just write
  672. * the full page. Data past the inode size is
  673. * guaranteed to be zeroed by the higher level client
  674. * code, and this behaviour is mandated by RFC 5663
  675. * section 2.3.2.
  676. */
  677. return true;
  678. }
  679. return false;
  680. }
  681. static void
  682. bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  683. {
  684. if (!is_aligned_req(pgio, req, SECTOR_SIZE, false)) {
  685. nfs_pageio_reset_read_mds(pgio);
  686. return;
  687. }
  688. pnfs_generic_pg_init_read(pgio, req);
  689. }
  690. /*
  691. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  692. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  693. */
  694. static size_t
  695. bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  696. struct nfs_page *req)
  697. {
  698. if (!is_aligned_req(pgio, req, SECTOR_SIZE, false))
  699. return 0;
  700. return pnfs_generic_pg_test(pgio, prev, req);
  701. }
  702. /*
  703. * Return the number of contiguous bytes for a given inode
  704. * starting at page frame idx.
  705. */
  706. static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
  707. {
  708. struct address_space *mapping = inode->i_mapping;
  709. pgoff_t end;
  710. /* Optimize common case that writes from 0 to end of file */
  711. end = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  712. if (end != inode->i_mapping->nrpages) {
  713. rcu_read_lock();
  714. end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
  715. rcu_read_unlock();
  716. }
  717. if (!end)
  718. return i_size_read(inode) - (idx << PAGE_SHIFT);
  719. else
  720. return (end - idx) << PAGE_SHIFT;
  721. }
  722. static void
  723. bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  724. {
  725. u64 wb_size;
  726. if (!is_aligned_req(pgio, req, PAGE_SIZE, true)) {
  727. nfs_pageio_reset_write_mds(pgio);
  728. return;
  729. }
  730. if (pgio->pg_dreq == NULL)
  731. wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
  732. req->wb_index);
  733. else
  734. wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  735. pnfs_generic_pg_init_write(pgio, req, wb_size);
  736. }
  737. /*
  738. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  739. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  740. */
  741. static size_t
  742. bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  743. struct nfs_page *req)
  744. {
  745. if (!is_aligned_req(pgio, req, PAGE_SIZE, true))
  746. return 0;
  747. return pnfs_generic_pg_test(pgio, prev, req);
  748. }
  749. static const struct nfs_pageio_ops bl_pg_read_ops = {
  750. .pg_init = bl_pg_init_read,
  751. .pg_test = bl_pg_test_read,
  752. .pg_doio = pnfs_generic_pg_readpages,
  753. .pg_cleanup = pnfs_generic_pg_cleanup,
  754. };
  755. static const struct nfs_pageio_ops bl_pg_write_ops = {
  756. .pg_init = bl_pg_init_write,
  757. .pg_test = bl_pg_test_write,
  758. .pg_doio = pnfs_generic_pg_writepages,
  759. .pg_cleanup = pnfs_generic_pg_cleanup,
  760. };
  761. static struct pnfs_layoutdriver_type blocklayout_type = {
  762. .id = LAYOUT_BLOCK_VOLUME,
  763. .name = "LAYOUT_BLOCK_VOLUME",
  764. .owner = THIS_MODULE,
  765. .flags = PNFS_LAYOUTRET_ON_SETATTR |
  766. PNFS_READ_WHOLE_PAGE,
  767. .read_pagelist = bl_read_pagelist,
  768. .write_pagelist = bl_write_pagelist,
  769. .alloc_layout_hdr = bl_alloc_layout_hdr,
  770. .free_layout_hdr = bl_free_layout_hdr,
  771. .alloc_lseg = bl_alloc_lseg,
  772. .free_lseg = bl_free_lseg,
  773. .return_range = bl_return_range,
  774. .prepare_layoutcommit = bl_prepare_layoutcommit,
  775. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  776. .set_layoutdriver = bl_set_layoutdriver,
  777. .alloc_deviceid_node = bl_alloc_deviceid_node,
  778. .free_deviceid_node = bl_free_deviceid_node,
  779. .pg_read_ops = &bl_pg_read_ops,
  780. .pg_write_ops = &bl_pg_write_ops,
  781. .sync = pnfs_generic_sync,
  782. };
  783. static struct pnfs_layoutdriver_type scsilayout_type = {
  784. .id = LAYOUT_SCSI,
  785. .name = "LAYOUT_SCSI",
  786. .owner = THIS_MODULE,
  787. .flags = PNFS_LAYOUTRET_ON_SETATTR |
  788. PNFS_READ_WHOLE_PAGE,
  789. .read_pagelist = bl_read_pagelist,
  790. .write_pagelist = bl_write_pagelist,
  791. .alloc_layout_hdr = sl_alloc_layout_hdr,
  792. .free_layout_hdr = bl_free_layout_hdr,
  793. .alloc_lseg = bl_alloc_lseg,
  794. .free_lseg = bl_free_lseg,
  795. .return_range = bl_return_range,
  796. .prepare_layoutcommit = bl_prepare_layoutcommit,
  797. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  798. .set_layoutdriver = bl_set_layoutdriver,
  799. .alloc_deviceid_node = bl_alloc_deviceid_node,
  800. .free_deviceid_node = bl_free_deviceid_node,
  801. .pg_read_ops = &bl_pg_read_ops,
  802. .pg_write_ops = &bl_pg_write_ops,
  803. .sync = pnfs_generic_sync,
  804. };
  805. static int __init nfs4blocklayout_init(void)
  806. {
  807. int ret;
  808. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  809. ret = bl_init_pipefs();
  810. if (ret)
  811. goto out;
  812. ret = pnfs_register_layoutdriver(&blocklayout_type);
  813. if (ret)
  814. goto out_cleanup_pipe;
  815. ret = pnfs_register_layoutdriver(&scsilayout_type);
  816. if (ret)
  817. goto out_unregister_block;
  818. return 0;
  819. out_unregister_block:
  820. pnfs_unregister_layoutdriver(&blocklayout_type);
  821. out_cleanup_pipe:
  822. bl_cleanup_pipefs();
  823. out:
  824. return ret;
  825. }
  826. static void __exit nfs4blocklayout_exit(void)
  827. {
  828. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  829. __func__);
  830. pnfs_unregister_layoutdriver(&scsilayout_type);
  831. pnfs_unregister_layoutdriver(&blocklayout_type);
  832. bl_cleanup_pipefs();
  833. }
  834. MODULE_ALIAS("nfs-layouttype4-3");
  835. module_init(nfs4blocklayout_init);
  836. module_exit(nfs4blocklayout_exit);