blocklayout.c 38 KB

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