blocklayout.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. ext_tree_mark_written(bl, start >> SECTOR_SHIFT,
  305. (end - start) >> SECTOR_SHIFT);
  306. }
  307. pnfs_ld_write_done(hdr);
  308. }
  309. /* Called when last of bios associated with a bl_write_pagelist call finishes */
  310. static void bl_end_par_io_write(void *data)
  311. {
  312. struct nfs_pgio_header *hdr = data;
  313. hdr->task.tk_status = hdr->pnfs_error;
  314. hdr->verf.committed = NFS_FILE_SYNC;
  315. INIT_WORK(&hdr->task.u.tk_work, bl_write_cleanup);
  316. schedule_work(&hdr->task.u.tk_work);
  317. }
  318. static enum pnfs_try_status
  319. bl_write_pagelist(struct nfs_pgio_header *header, int sync)
  320. {
  321. struct pnfs_block_layout *bl = BLK_LSEG2EXT(header->lseg);
  322. struct pnfs_block_dev_map map = { .start = NFS4_MAX_UINT64 };
  323. struct bio *bio = NULL;
  324. struct pnfs_block_extent be;
  325. sector_t isect, extent_length = 0;
  326. struct parallel_io *par = NULL;
  327. loff_t offset = header->args.offset;
  328. size_t count = header->args.count;
  329. struct page **pages = header->args.pages;
  330. int pg_index = header->args.pgbase >> PAGE_SHIFT;
  331. unsigned int pg_len;
  332. struct blk_plug plug;
  333. int i;
  334. dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
  335. /* At this point, header->page_aray is a (sequential) list of nfs_pages.
  336. * We want to write each, and if there is an error set pnfs_error
  337. * to have it redone using nfs.
  338. */
  339. par = alloc_parallel(header);
  340. if (!par)
  341. return PNFS_NOT_ATTEMPTED;
  342. par->pnfs_callback = bl_end_par_io_write;
  343. blk_start_plug(&plug);
  344. /* we always write out the whole page */
  345. offset = offset & (loff_t)PAGE_MASK;
  346. isect = offset >> SECTOR_SHIFT;
  347. for (i = pg_index; i < header->page_array.npages; i++) {
  348. if (extent_length <= 0) {
  349. /* We've used up the previous extent */
  350. bio = bl_submit_bio(bio);
  351. /* Get the next one */
  352. if (!ext_tree_lookup(bl, isect, &be, true)) {
  353. header->pnfs_error = -EINVAL;
  354. goto out;
  355. }
  356. extent_length = be.be_length - (isect - be.be_f_offset);
  357. }
  358. pg_len = PAGE_SIZE;
  359. bio = do_add_page_to_bio(bio, header->page_array.npages - i,
  360. WRITE, isect, pages[i], &map, &be,
  361. bl_end_io_write, par,
  362. 0, &pg_len);
  363. if (IS_ERR(bio)) {
  364. header->pnfs_error = PTR_ERR(bio);
  365. bio = NULL;
  366. goto out;
  367. }
  368. offset += pg_len;
  369. count -= pg_len;
  370. isect += (pg_len >> SECTOR_SHIFT);
  371. extent_length -= (pg_len >> SECTOR_SHIFT);
  372. }
  373. header->res.count = header->args.count;
  374. out:
  375. bl_submit_bio(bio);
  376. blk_finish_plug(&plug);
  377. put_parallel(par);
  378. return PNFS_ATTEMPTED;
  379. }
  380. static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
  381. {
  382. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  383. int err;
  384. dprintk("%s enter\n", __func__);
  385. err = ext_tree_remove(bl, true, 0, LLONG_MAX);
  386. WARN_ON(err);
  387. kfree(bl);
  388. }
  389. static struct pnfs_layout_hdr *__bl_alloc_layout_hdr(struct inode *inode,
  390. gfp_t gfp_flags, bool is_scsi_layout)
  391. {
  392. struct pnfs_block_layout *bl;
  393. dprintk("%s enter\n", __func__);
  394. bl = kzalloc(sizeof(*bl), gfp_flags);
  395. if (!bl)
  396. return NULL;
  397. bl->bl_ext_rw = RB_ROOT;
  398. bl->bl_ext_ro = RB_ROOT;
  399. spin_lock_init(&bl->bl_ext_lock);
  400. bl->bl_scsi_layout = is_scsi_layout;
  401. return &bl->bl_layout;
  402. }
  403. static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
  404. gfp_t gfp_flags)
  405. {
  406. return __bl_alloc_layout_hdr(inode, gfp_flags, false);
  407. }
  408. static struct pnfs_layout_hdr *sl_alloc_layout_hdr(struct inode *inode,
  409. gfp_t gfp_flags)
  410. {
  411. return __bl_alloc_layout_hdr(inode, gfp_flags, true);
  412. }
  413. static void bl_free_lseg(struct pnfs_layout_segment *lseg)
  414. {
  415. dprintk("%s enter\n", __func__);
  416. kfree(lseg);
  417. }
  418. /* Tracks info needed to ensure extents in layout obey constraints of spec */
  419. struct layout_verification {
  420. u32 mode; /* R or RW */
  421. u64 start; /* Expected start of next non-COW extent */
  422. u64 inval; /* Start of INVAL coverage */
  423. u64 cowread; /* End of COW read coverage */
  424. };
  425. /* Verify the extent meets the layout requirements of the pnfs-block draft,
  426. * section 2.3.1.
  427. */
  428. static int verify_extent(struct pnfs_block_extent *be,
  429. struct layout_verification *lv)
  430. {
  431. if (lv->mode == IOMODE_READ) {
  432. if (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
  433. be->be_state == PNFS_BLOCK_INVALID_DATA)
  434. return -EIO;
  435. if (be->be_f_offset != lv->start)
  436. return -EIO;
  437. lv->start += be->be_length;
  438. return 0;
  439. }
  440. /* lv->mode == IOMODE_RW */
  441. if (be->be_state == PNFS_BLOCK_READWRITE_DATA) {
  442. if (be->be_f_offset != lv->start)
  443. return -EIO;
  444. if (lv->cowread > lv->start)
  445. return -EIO;
  446. lv->start += be->be_length;
  447. lv->inval = lv->start;
  448. return 0;
  449. } else if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  450. if (be->be_f_offset != lv->start)
  451. return -EIO;
  452. lv->start += be->be_length;
  453. return 0;
  454. } else if (be->be_state == PNFS_BLOCK_READ_DATA) {
  455. if (be->be_f_offset > lv->start)
  456. return -EIO;
  457. if (be->be_f_offset < lv->inval)
  458. return -EIO;
  459. if (be->be_f_offset < lv->cowread)
  460. return -EIO;
  461. /* It looks like you might want to min this with lv->start,
  462. * but you really don't.
  463. */
  464. lv->inval = lv->inval + be->be_length;
  465. lv->cowread = be->be_f_offset + be->be_length;
  466. return 0;
  467. } else
  468. return -EIO;
  469. }
  470. static int decode_sector_number(__be32 **rp, sector_t *sp)
  471. {
  472. uint64_t s;
  473. *rp = xdr_decode_hyper(*rp, &s);
  474. if (s & 0x1ff) {
  475. printk(KERN_WARNING "NFS: %s: sector not aligned\n", __func__);
  476. return -1;
  477. }
  478. *sp = s >> SECTOR_SHIFT;
  479. return 0;
  480. }
  481. static int
  482. bl_alloc_extent(struct xdr_stream *xdr, struct pnfs_layout_hdr *lo,
  483. struct layout_verification *lv, struct list_head *extents,
  484. gfp_t gfp_mask)
  485. {
  486. struct pnfs_block_extent *be;
  487. struct nfs4_deviceid id;
  488. int error;
  489. __be32 *p;
  490. p = xdr_inline_decode(xdr, 28 + NFS4_DEVICEID4_SIZE);
  491. if (!p)
  492. return -EIO;
  493. be = kzalloc(sizeof(*be), GFP_NOFS);
  494. if (!be)
  495. return -ENOMEM;
  496. memcpy(&id, p, NFS4_DEVICEID4_SIZE);
  497. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  498. error = -EIO;
  499. be->be_device = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), &id,
  500. lo->plh_lc_cred, gfp_mask);
  501. if (!be->be_device)
  502. goto out_free_be;
  503. /*
  504. * The next three values are read in as bytes, but stored in the
  505. * extent structure in 512-byte granularity.
  506. */
  507. if (decode_sector_number(&p, &be->be_f_offset) < 0)
  508. goto out_put_deviceid;
  509. if (decode_sector_number(&p, &be->be_length) < 0)
  510. goto out_put_deviceid;
  511. if (decode_sector_number(&p, &be->be_v_offset) < 0)
  512. goto out_put_deviceid;
  513. be->be_state = be32_to_cpup(p++);
  514. error = verify_extent(be, lv);
  515. if (error) {
  516. dprintk("%s: extent verification failed\n", __func__);
  517. goto out_put_deviceid;
  518. }
  519. list_add_tail(&be->be_list, extents);
  520. return 0;
  521. out_put_deviceid:
  522. nfs4_put_deviceid_node(be->be_device);
  523. out_free_be:
  524. kfree(be);
  525. return error;
  526. }
  527. static struct pnfs_layout_segment *
  528. bl_alloc_lseg(struct pnfs_layout_hdr *lo, struct nfs4_layoutget_res *lgr,
  529. gfp_t gfp_mask)
  530. {
  531. struct layout_verification lv = {
  532. .mode = lgr->range.iomode,
  533. .start = lgr->range.offset >> SECTOR_SHIFT,
  534. .inval = lgr->range.offset >> SECTOR_SHIFT,
  535. .cowread = lgr->range.offset >> SECTOR_SHIFT,
  536. };
  537. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  538. struct pnfs_layout_segment *lseg;
  539. struct xdr_buf buf;
  540. struct xdr_stream xdr;
  541. struct page *scratch;
  542. int status, i;
  543. uint32_t count;
  544. __be32 *p;
  545. LIST_HEAD(extents);
  546. dprintk("---> %s\n", __func__);
  547. lseg = kzalloc(sizeof(*lseg), gfp_mask);
  548. if (!lseg)
  549. return ERR_PTR(-ENOMEM);
  550. status = -ENOMEM;
  551. scratch = alloc_page(gfp_mask);
  552. if (!scratch)
  553. goto out;
  554. xdr_init_decode_pages(&xdr, &buf,
  555. lgr->layoutp->pages, lgr->layoutp->len);
  556. xdr_set_scratch_buffer(&xdr, page_address(scratch), PAGE_SIZE);
  557. status = -EIO;
  558. p = xdr_inline_decode(&xdr, 4);
  559. if (unlikely(!p))
  560. goto out_free_scratch;
  561. count = be32_to_cpup(p++);
  562. dprintk("%s: number of extents %d\n", __func__, count);
  563. /*
  564. * Decode individual extents, putting them in temporary staging area
  565. * until whole layout is decoded to make error recovery easier.
  566. */
  567. for (i = 0; i < count; i++) {
  568. status = bl_alloc_extent(&xdr, lo, &lv, &extents, gfp_mask);
  569. if (status)
  570. goto process_extents;
  571. }
  572. if (lgr->range.offset + lgr->range.length !=
  573. lv.start << SECTOR_SHIFT) {
  574. dprintk("%s Final length mismatch\n", __func__);
  575. status = -EIO;
  576. goto process_extents;
  577. }
  578. if (lv.start < lv.cowread) {
  579. dprintk("%s Final uncovered COW extent\n", __func__);
  580. status = -EIO;
  581. }
  582. process_extents:
  583. while (!list_empty(&extents)) {
  584. struct pnfs_block_extent *be =
  585. list_first_entry(&extents, struct pnfs_block_extent,
  586. be_list);
  587. list_del(&be->be_list);
  588. if (!status)
  589. status = ext_tree_insert(bl, be);
  590. if (status) {
  591. nfs4_put_deviceid_node(be->be_device);
  592. kfree(be);
  593. }
  594. }
  595. out_free_scratch:
  596. __free_page(scratch);
  597. out:
  598. dprintk("%s returns %d\n", __func__, status);
  599. if (status) {
  600. kfree(lseg);
  601. return ERR_PTR(status);
  602. }
  603. return lseg;
  604. }
  605. static void
  606. bl_return_range(struct pnfs_layout_hdr *lo,
  607. struct pnfs_layout_range *range)
  608. {
  609. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  610. sector_t offset = range->offset >> SECTOR_SHIFT, end;
  611. if (range->offset % 8) {
  612. dprintk("%s: offset %lld not block size aligned\n",
  613. __func__, range->offset);
  614. return;
  615. }
  616. if (range->length != NFS4_MAX_UINT64) {
  617. if (range->length % 8) {
  618. dprintk("%s: length %lld not block size aligned\n",
  619. __func__, range->length);
  620. return;
  621. }
  622. end = offset + (range->length >> SECTOR_SHIFT);
  623. } else {
  624. end = round_down(NFS4_MAX_UINT64, PAGE_SIZE);
  625. }
  626. ext_tree_remove(bl, range->iomode & IOMODE_RW, offset, end);
  627. }
  628. static int
  629. bl_prepare_layoutcommit(struct nfs4_layoutcommit_args *arg)
  630. {
  631. return ext_tree_prepare_commit(arg);
  632. }
  633. static void
  634. bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
  635. {
  636. ext_tree_mark_committed(&lcdata->args, lcdata->res.status);
  637. }
  638. static int
  639. bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
  640. {
  641. dprintk("%s enter\n", __func__);
  642. if (server->pnfs_blksize == 0) {
  643. dprintk("%s Server did not return blksize\n", __func__);
  644. return -EINVAL;
  645. }
  646. if (server->pnfs_blksize > PAGE_SIZE) {
  647. printk(KERN_ERR "%s: pNFS blksize %d not supported.\n",
  648. __func__, server->pnfs_blksize);
  649. return -EINVAL;
  650. }
  651. return 0;
  652. }
  653. static bool
  654. is_aligned_req(struct nfs_pageio_descriptor *pgio,
  655. struct nfs_page *req, unsigned int alignment, bool is_write)
  656. {
  657. /*
  658. * Always accept buffered writes, higher layers take care of the
  659. * right alignment.
  660. */
  661. if (pgio->pg_dreq == NULL)
  662. return true;
  663. if (!IS_ALIGNED(req->wb_offset, alignment))
  664. return false;
  665. if (IS_ALIGNED(req->wb_bytes, alignment))
  666. return true;
  667. if (is_write &&
  668. (req_offset(req) + req->wb_bytes == i_size_read(pgio->pg_inode))) {
  669. /*
  670. * If the write goes up to the inode size, just write
  671. * the full page. Data past the inode size is
  672. * guaranteed to be zeroed by the higher level client
  673. * code, and this behaviour is mandated by RFC 5663
  674. * section 2.3.2.
  675. */
  676. return true;
  677. }
  678. return false;
  679. }
  680. static void
  681. bl_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  682. {
  683. if (!is_aligned_req(pgio, req, SECTOR_SIZE, false)) {
  684. nfs_pageio_reset_read_mds(pgio);
  685. return;
  686. }
  687. pnfs_generic_pg_init_read(pgio, req);
  688. }
  689. /*
  690. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  691. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  692. */
  693. static size_t
  694. bl_pg_test_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  695. struct nfs_page *req)
  696. {
  697. if (!is_aligned_req(pgio, req, SECTOR_SIZE, false))
  698. return 0;
  699. return pnfs_generic_pg_test(pgio, prev, req);
  700. }
  701. /*
  702. * Return the number of contiguous bytes for a given inode
  703. * starting at page frame idx.
  704. */
  705. static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx)
  706. {
  707. struct address_space *mapping = inode->i_mapping;
  708. pgoff_t end;
  709. /* Optimize common case that writes from 0 to end of file */
  710. end = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  711. if (end != inode->i_mapping->nrpages) {
  712. rcu_read_lock();
  713. end = page_cache_next_hole(mapping, idx + 1, ULONG_MAX);
  714. rcu_read_unlock();
  715. }
  716. if (!end)
  717. return i_size_read(inode) - (idx << PAGE_SHIFT);
  718. else
  719. return (end - idx) << PAGE_SHIFT;
  720. }
  721. static void
  722. bl_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
  723. {
  724. u64 wb_size;
  725. if (!is_aligned_req(pgio, req, PAGE_SIZE, true)) {
  726. nfs_pageio_reset_write_mds(pgio);
  727. return;
  728. }
  729. if (pgio->pg_dreq == NULL)
  730. wb_size = pnfs_num_cont_bytes(pgio->pg_inode,
  731. req->wb_index);
  732. else
  733. wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
  734. pnfs_generic_pg_init_write(pgio, req, wb_size);
  735. }
  736. /*
  737. * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
  738. * of bytes (maximum @req->wb_bytes) that can be coalesced.
  739. */
  740. static size_t
  741. bl_pg_test_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
  742. struct nfs_page *req)
  743. {
  744. if (!is_aligned_req(pgio, req, PAGE_SIZE, true))
  745. return 0;
  746. return pnfs_generic_pg_test(pgio, prev, req);
  747. }
  748. static const struct nfs_pageio_ops bl_pg_read_ops = {
  749. .pg_init = bl_pg_init_read,
  750. .pg_test = bl_pg_test_read,
  751. .pg_doio = pnfs_generic_pg_readpages,
  752. .pg_cleanup = pnfs_generic_pg_cleanup,
  753. };
  754. static const struct nfs_pageio_ops bl_pg_write_ops = {
  755. .pg_init = bl_pg_init_write,
  756. .pg_test = bl_pg_test_write,
  757. .pg_doio = pnfs_generic_pg_writepages,
  758. .pg_cleanup = pnfs_generic_pg_cleanup,
  759. };
  760. static struct pnfs_layoutdriver_type blocklayout_type = {
  761. .id = LAYOUT_BLOCK_VOLUME,
  762. .name = "LAYOUT_BLOCK_VOLUME",
  763. .owner = THIS_MODULE,
  764. .flags = PNFS_LAYOUTRET_ON_SETATTR |
  765. PNFS_READ_WHOLE_PAGE,
  766. .read_pagelist = bl_read_pagelist,
  767. .write_pagelist = bl_write_pagelist,
  768. .alloc_layout_hdr = bl_alloc_layout_hdr,
  769. .free_layout_hdr = bl_free_layout_hdr,
  770. .alloc_lseg = bl_alloc_lseg,
  771. .free_lseg = bl_free_lseg,
  772. .return_range = bl_return_range,
  773. .prepare_layoutcommit = bl_prepare_layoutcommit,
  774. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  775. .set_layoutdriver = bl_set_layoutdriver,
  776. .alloc_deviceid_node = bl_alloc_deviceid_node,
  777. .free_deviceid_node = bl_free_deviceid_node,
  778. .pg_read_ops = &bl_pg_read_ops,
  779. .pg_write_ops = &bl_pg_write_ops,
  780. .sync = pnfs_generic_sync,
  781. };
  782. static struct pnfs_layoutdriver_type scsilayout_type = {
  783. .id = LAYOUT_SCSI,
  784. .name = "LAYOUT_SCSI",
  785. .owner = THIS_MODULE,
  786. .flags = PNFS_LAYOUTRET_ON_SETATTR |
  787. PNFS_READ_WHOLE_PAGE,
  788. .read_pagelist = bl_read_pagelist,
  789. .write_pagelist = bl_write_pagelist,
  790. .alloc_layout_hdr = sl_alloc_layout_hdr,
  791. .free_layout_hdr = bl_free_layout_hdr,
  792. .alloc_lseg = bl_alloc_lseg,
  793. .free_lseg = bl_free_lseg,
  794. .return_range = bl_return_range,
  795. .prepare_layoutcommit = bl_prepare_layoutcommit,
  796. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  797. .set_layoutdriver = bl_set_layoutdriver,
  798. .alloc_deviceid_node = bl_alloc_deviceid_node,
  799. .free_deviceid_node = bl_free_deviceid_node,
  800. .pg_read_ops = &bl_pg_read_ops,
  801. .pg_write_ops = &bl_pg_write_ops,
  802. .sync = pnfs_generic_sync,
  803. };
  804. static int __init nfs4blocklayout_init(void)
  805. {
  806. int ret;
  807. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  808. ret = bl_init_pipefs();
  809. if (ret)
  810. goto out;
  811. ret = pnfs_register_layoutdriver(&blocklayout_type);
  812. if (ret)
  813. goto out_cleanup_pipe;
  814. ret = pnfs_register_layoutdriver(&scsilayout_type);
  815. if (ret)
  816. goto out_unregister_block;
  817. return 0;
  818. out_unregister_block:
  819. pnfs_unregister_layoutdriver(&blocklayout_type);
  820. out_cleanup_pipe:
  821. bl_cleanup_pipefs();
  822. out:
  823. return ret;
  824. }
  825. static void __exit nfs4blocklayout_exit(void)
  826. {
  827. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  828. __func__);
  829. pnfs_unregister_layoutdriver(&scsilayout_type);
  830. pnfs_unregister_layoutdriver(&blocklayout_type);
  831. bl_cleanup_pipefs();
  832. }
  833. MODULE_ALIAS("nfs-layouttype4-3");
  834. module_init(nfs4blocklayout_init);
  835. module_exit(nfs4blocklayout_exit);