blocklayout.c 23 KB

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