blocklayout.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 "blocklayout.h"
  39. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>");
  42. MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver");
  43. struct dentry *bl_device_pipe;
  44. wait_queue_head_t bl_wq;
  45. static void print_page(struct page *page)
  46. {
  47. dprintk("PRINTPAGE page %p\n", page);
  48. dprintk(" PagePrivate %d\n", PagePrivate(page));
  49. dprintk(" PageUptodate %d\n", PageUptodate(page));
  50. dprintk(" PageError %d\n", PageError(page));
  51. dprintk(" PageDirty %d\n", PageDirty(page));
  52. dprintk(" PageReferenced %d\n", PageReferenced(page));
  53. dprintk(" PageLocked %d\n", PageLocked(page));
  54. dprintk(" PageWriteback %d\n", PageWriteback(page));
  55. dprintk(" PageMappedToDisk %d\n", PageMappedToDisk(page));
  56. dprintk("\n");
  57. }
  58. /* Given the be associated with isect, determine if page data needs to be
  59. * initialized.
  60. */
  61. static int is_hole(struct pnfs_block_extent *be, sector_t isect)
  62. {
  63. if (be->be_state == PNFS_BLOCK_NONE_DATA)
  64. return 1;
  65. else if (be->be_state != PNFS_BLOCK_INVALID_DATA)
  66. return 0;
  67. else
  68. return !bl_is_sector_init(be->be_inval, isect);
  69. }
  70. /* Given the be associated with isect, determine if page data can be
  71. * written to disk.
  72. */
  73. static int is_writable(struct pnfs_block_extent *be, sector_t isect)
  74. {
  75. return (be->be_state == PNFS_BLOCK_READWRITE_DATA ||
  76. be->be_state == PNFS_BLOCK_INVALID_DATA);
  77. }
  78. /* The data we are handed might be spread across several bios. We need
  79. * to track when the last one is finished.
  80. */
  81. struct parallel_io {
  82. struct kref refcnt;
  83. struct rpc_call_ops call_ops;
  84. void (*pnfs_callback) (void *data);
  85. void *data;
  86. };
  87. static inline struct parallel_io *alloc_parallel(void *data)
  88. {
  89. struct parallel_io *rv;
  90. rv = kmalloc(sizeof(*rv), GFP_NOFS);
  91. if (rv) {
  92. rv->data = data;
  93. kref_init(&rv->refcnt);
  94. }
  95. return rv;
  96. }
  97. static inline void get_parallel(struct parallel_io *p)
  98. {
  99. kref_get(&p->refcnt);
  100. }
  101. static void destroy_parallel(struct kref *kref)
  102. {
  103. struct parallel_io *p = container_of(kref, struct parallel_io, refcnt);
  104. dprintk("%s enter\n", __func__);
  105. p->pnfs_callback(p->data);
  106. kfree(p);
  107. }
  108. static inline void put_parallel(struct parallel_io *p)
  109. {
  110. kref_put(&p->refcnt, destroy_parallel);
  111. }
  112. static struct bio *
  113. bl_submit_bio(int rw, struct bio *bio)
  114. {
  115. if (bio) {
  116. get_parallel(bio->bi_private);
  117. dprintk("%s submitting %s bio %u@%llu\n", __func__,
  118. rw == READ ? "read" : "write",
  119. bio->bi_size, (unsigned long long)bio->bi_sector);
  120. submit_bio(rw, bio);
  121. }
  122. return NULL;
  123. }
  124. static struct bio *bl_alloc_init_bio(int npg, sector_t isect,
  125. struct pnfs_block_extent *be,
  126. void (*end_io)(struct bio *, int err),
  127. struct parallel_io *par)
  128. {
  129. struct bio *bio;
  130. bio = bio_alloc(GFP_NOIO, npg);
  131. if (!bio)
  132. return NULL;
  133. bio->bi_sector = isect - be->be_f_offset + be->be_v_offset;
  134. bio->bi_bdev = be->be_mdev;
  135. bio->bi_end_io = end_io;
  136. bio->bi_private = par;
  137. return bio;
  138. }
  139. static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
  140. sector_t isect, struct page *page,
  141. struct pnfs_block_extent *be,
  142. void (*end_io)(struct bio *, int err),
  143. struct parallel_io *par)
  144. {
  145. retry:
  146. if (!bio) {
  147. bio = bl_alloc_init_bio(npg, isect, be, end_io, par);
  148. if (!bio)
  149. return ERR_PTR(-ENOMEM);
  150. }
  151. if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
  152. bio = bl_submit_bio(rw, bio);
  153. goto retry;
  154. }
  155. return bio;
  156. }
  157. static void bl_set_lo_fail(struct pnfs_layout_segment *lseg)
  158. {
  159. if (lseg->pls_range.iomode == IOMODE_RW) {
  160. dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
  161. set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
  162. } else {
  163. dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
  164. set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
  165. }
  166. }
  167. /* This is basically copied from mpage_end_io_read */
  168. static void bl_end_io_read(struct bio *bio, int err)
  169. {
  170. struct parallel_io *par = bio->bi_private;
  171. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  172. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  173. struct nfs_read_data *rdata = (struct nfs_read_data *)par->data;
  174. do {
  175. struct page *page = bvec->bv_page;
  176. if (--bvec >= bio->bi_io_vec)
  177. prefetchw(&bvec->bv_page->flags);
  178. if (uptodate)
  179. SetPageUptodate(page);
  180. } while (bvec >= bio->bi_io_vec);
  181. if (!uptodate) {
  182. if (!rdata->pnfs_error)
  183. rdata->pnfs_error = -EIO;
  184. bl_set_lo_fail(rdata->lseg);
  185. }
  186. bio_put(bio);
  187. put_parallel(par);
  188. }
  189. static void bl_read_cleanup(struct work_struct *work)
  190. {
  191. struct rpc_task *task;
  192. struct nfs_read_data *rdata;
  193. dprintk("%s enter\n", __func__);
  194. task = container_of(work, struct rpc_task, u.tk_work);
  195. rdata = container_of(task, struct nfs_read_data, task);
  196. pnfs_ld_read_done(rdata);
  197. }
  198. static void
  199. bl_end_par_io_read(void *data)
  200. {
  201. struct nfs_read_data *rdata = data;
  202. INIT_WORK(&rdata->task.u.tk_work, bl_read_cleanup);
  203. schedule_work(&rdata->task.u.tk_work);
  204. }
  205. /* We don't want normal .rpc_call_done callback used, so we replace it
  206. * with this stub.
  207. */
  208. static void bl_rpc_do_nothing(struct rpc_task *task, void *calldata)
  209. {
  210. return;
  211. }
  212. static enum pnfs_try_status
  213. bl_read_pagelist(struct nfs_read_data *rdata)
  214. {
  215. int i, hole;
  216. struct bio *bio = NULL;
  217. struct pnfs_block_extent *be = NULL, *cow_read = NULL;
  218. sector_t isect, extent_length = 0;
  219. struct parallel_io *par;
  220. loff_t f_offset = rdata->args.offset;
  221. size_t count = rdata->args.count;
  222. struct page **pages = rdata->args.pages;
  223. int pg_index = rdata->args.pgbase >> PAGE_CACHE_SHIFT;
  224. dprintk("%s enter nr_pages %u offset %lld count %Zd\n", __func__,
  225. rdata->npages, f_offset, count);
  226. par = alloc_parallel(rdata);
  227. if (!par)
  228. goto use_mds;
  229. par->call_ops = *rdata->mds_ops;
  230. par->call_ops.rpc_call_done = bl_rpc_do_nothing;
  231. par->pnfs_callback = bl_end_par_io_read;
  232. /* At this point, we can no longer jump to use_mds */
  233. isect = (sector_t) (f_offset >> SECTOR_SHIFT);
  234. /* Code assumes extents are page-aligned */
  235. for (i = pg_index; i < rdata->npages; i++) {
  236. if (!extent_length) {
  237. /* We've used up the previous extent */
  238. bl_put_extent(be);
  239. bl_put_extent(cow_read);
  240. bio = bl_submit_bio(READ, bio);
  241. /* Get the next one */
  242. be = bl_find_get_extent(BLK_LSEG2EXT(rdata->lseg),
  243. isect, &cow_read);
  244. if (!be) {
  245. rdata->pnfs_error = -EIO;
  246. goto out;
  247. }
  248. extent_length = be->be_length -
  249. (isect - be->be_f_offset);
  250. if (cow_read) {
  251. sector_t cow_length = cow_read->be_length -
  252. (isect - cow_read->be_f_offset);
  253. extent_length = min(extent_length, cow_length);
  254. }
  255. }
  256. hole = is_hole(be, isect);
  257. if (hole && !cow_read) {
  258. bio = bl_submit_bio(READ, bio);
  259. /* Fill hole w/ zeroes w/o accessing device */
  260. dprintk("%s Zeroing page for hole\n", __func__);
  261. zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  262. print_page(pages[i]);
  263. SetPageUptodate(pages[i]);
  264. } else {
  265. struct pnfs_block_extent *be_read;
  266. be_read = (hole && cow_read) ? cow_read : be;
  267. bio = bl_add_page_to_bio(bio, rdata->npages - i, READ,
  268. isect, pages[i], be_read,
  269. bl_end_io_read, par);
  270. if (IS_ERR(bio)) {
  271. rdata->pnfs_error = PTR_ERR(bio);
  272. goto out;
  273. }
  274. }
  275. isect += PAGE_CACHE_SECTORS;
  276. extent_length -= PAGE_CACHE_SECTORS;
  277. }
  278. if ((isect << SECTOR_SHIFT) >= rdata->inode->i_size) {
  279. rdata->res.eof = 1;
  280. rdata->res.count = rdata->inode->i_size - f_offset;
  281. } else {
  282. rdata->res.count = (isect << SECTOR_SHIFT) - f_offset;
  283. }
  284. out:
  285. bl_put_extent(be);
  286. bl_put_extent(cow_read);
  287. bl_submit_bio(READ, bio);
  288. put_parallel(par);
  289. return PNFS_ATTEMPTED;
  290. use_mds:
  291. dprintk("Giving up and using normal NFS\n");
  292. return PNFS_NOT_ATTEMPTED;
  293. }
  294. static void mark_extents_written(struct pnfs_block_layout *bl,
  295. __u64 offset, __u32 count)
  296. {
  297. sector_t isect, end;
  298. struct pnfs_block_extent *be;
  299. dprintk("%s(%llu, %u)\n", __func__, offset, count);
  300. if (count == 0)
  301. return;
  302. isect = (offset & (long)(PAGE_CACHE_MASK)) >> SECTOR_SHIFT;
  303. end = (offset + count + PAGE_CACHE_SIZE - 1) & (long)(PAGE_CACHE_MASK);
  304. end >>= SECTOR_SHIFT;
  305. while (isect < end) {
  306. sector_t len;
  307. be = bl_find_get_extent(bl, isect, NULL);
  308. BUG_ON(!be); /* FIXME */
  309. len = min(end, be->be_f_offset + be->be_length) - isect;
  310. if (be->be_state == PNFS_BLOCK_INVALID_DATA)
  311. bl_mark_for_commit(be, isect, len); /* What if fails? */
  312. isect += len;
  313. bl_put_extent(be);
  314. }
  315. }
  316. static void bl_end_io_write_zero(struct bio *bio, int err)
  317. {
  318. struct parallel_io *par = bio->bi_private;
  319. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  320. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  321. struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
  322. do {
  323. struct page *page = bvec->bv_page;
  324. if (--bvec >= bio->bi_io_vec)
  325. prefetchw(&bvec->bv_page->flags);
  326. /* This is the zeroing page we added */
  327. end_page_writeback(page);
  328. page_cache_release(page);
  329. } while (bvec >= bio->bi_io_vec);
  330. if (!uptodate) {
  331. if (!wdata->pnfs_error)
  332. wdata->pnfs_error = -EIO;
  333. bl_set_lo_fail(wdata->lseg);
  334. }
  335. bio_put(bio);
  336. put_parallel(par);
  337. }
  338. /* This is basically copied from mpage_end_io_read */
  339. static void bl_end_io_write(struct bio *bio, int err)
  340. {
  341. struct parallel_io *par = bio->bi_private;
  342. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  343. struct nfs_write_data *wdata = (struct nfs_write_data *)par->data;
  344. if (!uptodate) {
  345. if (!wdata->pnfs_error)
  346. wdata->pnfs_error = -EIO;
  347. bl_set_lo_fail(wdata->lseg);
  348. }
  349. bio_put(bio);
  350. put_parallel(par);
  351. }
  352. /* Function scheduled for call during bl_end_par_io_write,
  353. * it marks sectors as written and extends the commitlist.
  354. */
  355. static void bl_write_cleanup(struct work_struct *work)
  356. {
  357. struct rpc_task *task;
  358. struct nfs_write_data *wdata;
  359. dprintk("%s enter\n", __func__);
  360. task = container_of(work, struct rpc_task, u.tk_work);
  361. wdata = container_of(task, struct nfs_write_data, task);
  362. if (!wdata->pnfs_error) {
  363. /* Marks for LAYOUTCOMMIT */
  364. mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
  365. wdata->args.offset, wdata->args.count);
  366. }
  367. pnfs_ld_write_done(wdata);
  368. }
  369. /* Called when last of bios associated with a bl_write_pagelist call finishes */
  370. static void bl_end_par_io_write(void *data)
  371. {
  372. struct nfs_write_data *wdata = data;
  373. wdata->task.tk_status = 0;
  374. wdata->verf.committed = NFS_FILE_SYNC;
  375. INIT_WORK(&wdata->task.u.tk_work, bl_write_cleanup);
  376. schedule_work(&wdata->task.u.tk_work);
  377. }
  378. /* FIXME STUB - mark intersection of layout and page as bad, so is not
  379. * used again.
  380. */
  381. static void mark_bad_read(void)
  382. {
  383. return;
  384. }
  385. /*
  386. * map_block: map a requested I/0 block (isect) into an offset in the LVM
  387. * block_device
  388. */
  389. static void
  390. map_block(struct buffer_head *bh, sector_t isect, struct pnfs_block_extent *be)
  391. {
  392. dprintk("%s enter be=%p\n", __func__, be);
  393. set_buffer_mapped(bh);
  394. bh->b_bdev = be->be_mdev;
  395. bh->b_blocknr = (isect - be->be_f_offset + be->be_v_offset) >>
  396. (be->be_mdev->bd_inode->i_blkbits - SECTOR_SHIFT);
  397. dprintk("%s isect %llu, bh->b_blocknr %ld, using bsize %Zd\n",
  398. __func__, (unsigned long long)isect, (long)bh->b_blocknr,
  399. bh->b_size);
  400. return;
  401. }
  402. /* Given an unmapped page, zero it or read in page for COW, page is locked
  403. * by caller.
  404. */
  405. static int
  406. init_page_for_write(struct page *page, struct pnfs_block_extent *cow_read)
  407. {
  408. struct buffer_head *bh = NULL;
  409. int ret = 0;
  410. sector_t isect;
  411. dprintk("%s enter, %p\n", __func__, page);
  412. BUG_ON(PageUptodate(page));
  413. if (!cow_read) {
  414. zero_user_segment(page, 0, PAGE_SIZE);
  415. SetPageUptodate(page);
  416. goto cleanup;
  417. }
  418. bh = alloc_page_buffers(page, PAGE_CACHE_SIZE, 0);
  419. if (!bh) {
  420. ret = -ENOMEM;
  421. goto cleanup;
  422. }
  423. isect = (sector_t) page->index << PAGE_CACHE_SECTOR_SHIFT;
  424. map_block(bh, isect, cow_read);
  425. if (!bh_uptodate_or_lock(bh))
  426. ret = bh_submit_read(bh);
  427. if (ret)
  428. goto cleanup;
  429. SetPageUptodate(page);
  430. cleanup:
  431. bl_put_extent(cow_read);
  432. if (bh)
  433. free_buffer_head(bh);
  434. if (ret) {
  435. /* Need to mark layout with bad read...should now
  436. * just use nfs4 for reads and writes.
  437. */
  438. mark_bad_read();
  439. }
  440. return ret;
  441. }
  442. static enum pnfs_try_status
  443. bl_write_pagelist(struct nfs_write_data *wdata, int sync)
  444. {
  445. int i, ret, npg_zero, pg_index, last = 0;
  446. struct bio *bio = NULL;
  447. struct pnfs_block_extent *be = NULL, *cow_read = NULL;
  448. sector_t isect, last_isect = 0, extent_length = 0;
  449. struct parallel_io *par;
  450. loff_t offset = wdata->args.offset;
  451. size_t count = wdata->args.count;
  452. struct page **pages = wdata->args.pages;
  453. struct page *page;
  454. pgoff_t index;
  455. u64 temp;
  456. int npg_per_block =
  457. NFS_SERVER(wdata->inode)->pnfs_blksize >> PAGE_CACHE_SHIFT;
  458. dprintk("%s enter, %Zu@%lld\n", __func__, count, offset);
  459. /* At this point, wdata->pages is a (sequential) list of nfs_pages.
  460. * We want to write each, and if there is an error set pnfs_error
  461. * to have it redone using nfs.
  462. */
  463. par = alloc_parallel(wdata);
  464. if (!par)
  465. return PNFS_NOT_ATTEMPTED;
  466. par->call_ops = *wdata->mds_ops;
  467. par->call_ops.rpc_call_done = bl_rpc_do_nothing;
  468. par->pnfs_callback = bl_end_par_io_write;
  469. /* At this point, have to be more careful with error handling */
  470. isect = (sector_t) ((offset & (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
  471. be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg), isect, &cow_read);
  472. if (!be || !is_writable(be, isect)) {
  473. dprintk("%s no matching extents!\n", __func__);
  474. wdata->pnfs_error = -EINVAL;
  475. goto out;
  476. }
  477. /* First page inside INVALID extent */
  478. if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  479. temp = offset >> PAGE_CACHE_SHIFT;
  480. npg_zero = do_div(temp, npg_per_block);
  481. isect = (sector_t) (((offset - npg_zero * PAGE_CACHE_SIZE) &
  482. (long)PAGE_CACHE_MASK) >> SECTOR_SHIFT);
  483. extent_length = be->be_length - (isect - be->be_f_offset);
  484. fill_invalid_ext:
  485. dprintk("%s need to zero %d pages\n", __func__, npg_zero);
  486. for (;npg_zero > 0; npg_zero--) {
  487. /* page ref released in bl_end_io_write_zero */
  488. index = isect >> PAGE_CACHE_SECTOR_SHIFT;
  489. dprintk("%s zero %dth page: index %lu isect %llu\n",
  490. __func__, npg_zero, index,
  491. (unsigned long long)isect);
  492. page =
  493. find_or_create_page(wdata->inode->i_mapping, index,
  494. GFP_NOFS);
  495. if (!page) {
  496. dprintk("%s oom\n", __func__);
  497. wdata->pnfs_error = -ENOMEM;
  498. goto out;
  499. }
  500. /* PageDirty: Other will write this out
  501. * PageWriteback: Other is writing this out
  502. * PageUptodate: It was read before
  503. * sector_initialized: already written out
  504. */
  505. if (PageDirty(page) || PageWriteback(page) ||
  506. bl_is_sector_init(be->be_inval, isect)) {
  507. print_page(page);
  508. unlock_page(page);
  509. page_cache_release(page);
  510. goto next_page;
  511. }
  512. if (!PageUptodate(page)) {
  513. /* New page, readin or zero it */
  514. init_page_for_write(page, cow_read);
  515. }
  516. set_page_writeback(page);
  517. unlock_page(page);
  518. ret = bl_mark_sectors_init(be->be_inval, isect,
  519. PAGE_CACHE_SECTORS,
  520. NULL);
  521. if (unlikely(ret)) {
  522. dprintk("%s bl_mark_sectors_init fail %d\n",
  523. __func__, ret);
  524. end_page_writeback(page);
  525. page_cache_release(page);
  526. wdata->pnfs_error = ret;
  527. goto out;
  528. }
  529. bio = bl_add_page_to_bio(bio, npg_zero, WRITE,
  530. isect, page, be,
  531. bl_end_io_write_zero, par);
  532. if (IS_ERR(bio)) {
  533. wdata->pnfs_error = PTR_ERR(bio);
  534. goto out;
  535. }
  536. /* FIXME: This should be done in bi_end_io */
  537. mark_extents_written(BLK_LSEG2EXT(wdata->lseg),
  538. page->index << PAGE_CACHE_SHIFT,
  539. PAGE_CACHE_SIZE);
  540. next_page:
  541. isect += PAGE_CACHE_SECTORS;
  542. extent_length -= PAGE_CACHE_SECTORS;
  543. }
  544. if (last)
  545. goto write_done;
  546. }
  547. bio = bl_submit_bio(WRITE, bio);
  548. /* Middle pages */
  549. pg_index = wdata->args.pgbase >> PAGE_CACHE_SHIFT;
  550. for (i = pg_index; i < wdata->npages; i++) {
  551. if (!extent_length) {
  552. /* We've used up the previous extent */
  553. bl_put_extent(be);
  554. bio = bl_submit_bio(WRITE, bio);
  555. /* Get the next one */
  556. be = bl_find_get_extent(BLK_LSEG2EXT(wdata->lseg),
  557. isect, NULL);
  558. if (!be || !is_writable(be, isect)) {
  559. wdata->pnfs_error = -EINVAL;
  560. goto out;
  561. }
  562. extent_length = be->be_length -
  563. (isect - be->be_f_offset);
  564. }
  565. if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  566. ret = bl_mark_sectors_init(be->be_inval, isect,
  567. PAGE_CACHE_SECTORS,
  568. NULL);
  569. if (unlikely(ret)) {
  570. dprintk("%s bl_mark_sectors_init fail %d\n",
  571. __func__, ret);
  572. wdata->pnfs_error = ret;
  573. goto out;
  574. }
  575. }
  576. bio = bl_add_page_to_bio(bio, wdata->npages - i, WRITE,
  577. isect, pages[i], be,
  578. bl_end_io_write, par);
  579. if (IS_ERR(bio)) {
  580. wdata->pnfs_error = PTR_ERR(bio);
  581. goto out;
  582. }
  583. isect += PAGE_CACHE_SECTORS;
  584. last_isect = isect;
  585. extent_length -= PAGE_CACHE_SECTORS;
  586. }
  587. /* Last page inside INVALID extent */
  588. if (be->be_state == PNFS_BLOCK_INVALID_DATA) {
  589. bio = bl_submit_bio(WRITE, bio);
  590. temp = last_isect >> PAGE_CACHE_SECTOR_SHIFT;
  591. npg_zero = npg_per_block - do_div(temp, npg_per_block);
  592. if (npg_zero < npg_per_block) {
  593. last = 1;
  594. goto fill_invalid_ext;
  595. }
  596. }
  597. write_done:
  598. wdata->res.count = (last_isect << SECTOR_SHIFT) - (offset);
  599. if (count < wdata->res.count) {
  600. wdata->res.count = count;
  601. }
  602. out:
  603. bl_put_extent(be);
  604. bl_submit_bio(WRITE, bio);
  605. put_parallel(par);
  606. return PNFS_ATTEMPTED;
  607. }
  608. /* FIXME - range ignored */
  609. static void
  610. release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range)
  611. {
  612. int i;
  613. struct pnfs_block_extent *be;
  614. spin_lock(&bl->bl_ext_lock);
  615. for (i = 0; i < EXTENT_LISTS; i++) {
  616. while (!list_empty(&bl->bl_extents[i])) {
  617. be = list_first_entry(&bl->bl_extents[i],
  618. struct pnfs_block_extent,
  619. be_node);
  620. list_del(&be->be_node);
  621. bl_put_extent(be);
  622. }
  623. }
  624. spin_unlock(&bl->bl_ext_lock);
  625. }
  626. static void
  627. release_inval_marks(struct pnfs_inval_markings *marks)
  628. {
  629. struct pnfs_inval_tracking *pos, *temp;
  630. list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) {
  631. list_del(&pos->it_link);
  632. kfree(pos);
  633. }
  634. return;
  635. }
  636. static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo)
  637. {
  638. struct pnfs_block_layout *bl = BLK_LO2EXT(lo);
  639. dprintk("%s enter\n", __func__);
  640. release_extents(bl, NULL);
  641. release_inval_marks(&bl->bl_inval);
  642. kfree(bl);
  643. }
  644. static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode,
  645. gfp_t gfp_flags)
  646. {
  647. struct pnfs_block_layout *bl;
  648. dprintk("%s enter\n", __func__);
  649. bl = kzalloc(sizeof(*bl), gfp_flags);
  650. if (!bl)
  651. return NULL;
  652. spin_lock_init(&bl->bl_ext_lock);
  653. INIT_LIST_HEAD(&bl->bl_extents[0]);
  654. INIT_LIST_HEAD(&bl->bl_extents[1]);
  655. INIT_LIST_HEAD(&bl->bl_commit);
  656. INIT_LIST_HEAD(&bl->bl_committing);
  657. bl->bl_count = 0;
  658. bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT;
  659. BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize);
  660. return &bl->bl_layout;
  661. }
  662. static void bl_free_lseg(struct pnfs_layout_segment *lseg)
  663. {
  664. dprintk("%s enter\n", __func__);
  665. kfree(lseg);
  666. }
  667. /* We pretty much ignore lseg, and store all data layout wide, so we
  668. * can correctly merge.
  669. */
  670. static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo,
  671. struct nfs4_layoutget_res *lgr,
  672. gfp_t gfp_flags)
  673. {
  674. struct pnfs_layout_segment *lseg;
  675. int status;
  676. dprintk("%s enter\n", __func__);
  677. lseg = kzalloc(sizeof(*lseg), gfp_flags);
  678. if (!lseg)
  679. return ERR_PTR(-ENOMEM);
  680. status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags);
  681. if (status) {
  682. /* We don't want to call the full-blown bl_free_lseg,
  683. * since on error extents were not touched.
  684. */
  685. kfree(lseg);
  686. return ERR_PTR(status);
  687. }
  688. return lseg;
  689. }
  690. static void
  691. bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr,
  692. const struct nfs4_layoutcommit_args *arg)
  693. {
  694. dprintk("%s enter\n", __func__);
  695. encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg);
  696. }
  697. static void
  698. bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata)
  699. {
  700. struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout;
  701. dprintk("%s enter\n", __func__);
  702. clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status);
  703. }
  704. static void free_blk_mountid(struct block_mount_id *mid)
  705. {
  706. if (mid) {
  707. struct pnfs_block_dev *dev;
  708. spin_lock(&mid->bm_lock);
  709. while (!list_empty(&mid->bm_devlist)) {
  710. dev = list_first_entry(&mid->bm_devlist,
  711. struct pnfs_block_dev,
  712. bm_node);
  713. list_del(&dev->bm_node);
  714. bl_free_block_dev(dev);
  715. }
  716. spin_unlock(&mid->bm_lock);
  717. kfree(mid);
  718. }
  719. }
  720. /* This is mostly copied from the filelayout's get_device_info function.
  721. * It seems much of this should be at the generic pnfs level.
  722. */
  723. static struct pnfs_block_dev *
  724. nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh,
  725. struct nfs4_deviceid *d_id)
  726. {
  727. struct pnfs_device *dev;
  728. struct pnfs_block_dev *rv = NULL;
  729. u32 max_resp_sz;
  730. int max_pages;
  731. struct page **pages = NULL;
  732. int i, rc;
  733. /*
  734. * Use the session max response size as the basis for setting
  735. * GETDEVICEINFO's maxcount
  736. */
  737. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  738. max_pages = max_resp_sz >> PAGE_SHIFT;
  739. dprintk("%s max_resp_sz %u max_pages %d\n",
  740. __func__, max_resp_sz, max_pages);
  741. dev = kmalloc(sizeof(*dev), GFP_NOFS);
  742. if (!dev) {
  743. dprintk("%s kmalloc failed\n", __func__);
  744. return NULL;
  745. }
  746. pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS);
  747. if (pages == NULL) {
  748. kfree(dev);
  749. return NULL;
  750. }
  751. for (i = 0; i < max_pages; i++) {
  752. pages[i] = alloc_page(GFP_NOFS);
  753. if (!pages[i])
  754. goto out_free;
  755. }
  756. memcpy(&dev->dev_id, d_id, sizeof(*d_id));
  757. dev->layout_type = LAYOUT_BLOCK_VOLUME;
  758. dev->pages = pages;
  759. dev->pgbase = 0;
  760. dev->pglen = PAGE_SIZE * max_pages;
  761. dev->mincount = 0;
  762. dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data);
  763. rc = nfs4_proc_getdeviceinfo(server, dev);
  764. dprintk("%s getdevice info returns %d\n", __func__, rc);
  765. if (rc)
  766. goto out_free;
  767. rv = nfs4_blk_decode_device(server, dev);
  768. out_free:
  769. for (i = 0; i < max_pages; i++)
  770. __free_page(pages[i]);
  771. kfree(pages);
  772. kfree(dev);
  773. return rv;
  774. }
  775. static int
  776. bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh)
  777. {
  778. struct block_mount_id *b_mt_id = NULL;
  779. struct pnfs_devicelist *dlist = NULL;
  780. struct pnfs_block_dev *bdev;
  781. LIST_HEAD(block_disklist);
  782. int status = 0, i;
  783. dprintk("%s enter\n", __func__);
  784. if (server->pnfs_blksize == 0) {
  785. dprintk("%s Server did not return blksize\n", __func__);
  786. return -EINVAL;
  787. }
  788. b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS);
  789. if (!b_mt_id) {
  790. status = -ENOMEM;
  791. goto out_error;
  792. }
  793. /* Initialize nfs4 block layout mount id */
  794. spin_lock_init(&b_mt_id->bm_lock);
  795. INIT_LIST_HEAD(&b_mt_id->bm_devlist);
  796. dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS);
  797. if (!dlist) {
  798. status = -ENOMEM;
  799. goto out_error;
  800. }
  801. dlist->eof = 0;
  802. while (!dlist->eof) {
  803. status = nfs4_proc_getdevicelist(server, fh, dlist);
  804. if (status)
  805. goto out_error;
  806. dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n",
  807. __func__, dlist->num_devs, dlist->eof);
  808. for (i = 0; i < dlist->num_devs; i++) {
  809. bdev = nfs4_blk_get_deviceinfo(server, fh,
  810. &dlist->dev_id[i]);
  811. if (!bdev) {
  812. status = -ENODEV;
  813. goto out_error;
  814. }
  815. spin_lock(&b_mt_id->bm_lock);
  816. list_add(&bdev->bm_node, &b_mt_id->bm_devlist);
  817. spin_unlock(&b_mt_id->bm_lock);
  818. }
  819. }
  820. dprintk("%s SUCCESS\n", __func__);
  821. server->pnfs_ld_data = b_mt_id;
  822. out_return:
  823. kfree(dlist);
  824. return status;
  825. out_error:
  826. free_blk_mountid(b_mt_id);
  827. goto out_return;
  828. }
  829. static int
  830. bl_clear_layoutdriver(struct nfs_server *server)
  831. {
  832. struct block_mount_id *b_mt_id = server->pnfs_ld_data;
  833. dprintk("%s enter\n", __func__);
  834. free_blk_mountid(b_mt_id);
  835. dprintk("%s RETURNS\n", __func__);
  836. return 0;
  837. }
  838. static const struct nfs_pageio_ops bl_pg_read_ops = {
  839. .pg_init = pnfs_generic_pg_init_read,
  840. .pg_test = pnfs_generic_pg_test,
  841. .pg_doio = pnfs_generic_pg_readpages,
  842. };
  843. static const struct nfs_pageio_ops bl_pg_write_ops = {
  844. .pg_init = pnfs_generic_pg_init_write,
  845. .pg_test = pnfs_generic_pg_test,
  846. .pg_doio = pnfs_generic_pg_writepages,
  847. };
  848. static struct pnfs_layoutdriver_type blocklayout_type = {
  849. .id = LAYOUT_BLOCK_VOLUME,
  850. .name = "LAYOUT_BLOCK_VOLUME",
  851. .read_pagelist = bl_read_pagelist,
  852. .write_pagelist = bl_write_pagelist,
  853. .alloc_layout_hdr = bl_alloc_layout_hdr,
  854. .free_layout_hdr = bl_free_layout_hdr,
  855. .alloc_lseg = bl_alloc_lseg,
  856. .free_lseg = bl_free_lseg,
  857. .encode_layoutcommit = bl_encode_layoutcommit,
  858. .cleanup_layoutcommit = bl_cleanup_layoutcommit,
  859. .set_layoutdriver = bl_set_layoutdriver,
  860. .clear_layoutdriver = bl_clear_layoutdriver,
  861. .pg_read_ops = &bl_pg_read_ops,
  862. .pg_write_ops = &bl_pg_write_ops,
  863. };
  864. static const struct rpc_pipe_ops bl_upcall_ops = {
  865. .upcall = bl_pipe_upcall,
  866. .downcall = bl_pipe_downcall,
  867. .destroy_msg = bl_pipe_destroy_msg,
  868. };
  869. static int __init nfs4blocklayout_init(void)
  870. {
  871. struct vfsmount *mnt;
  872. struct path path;
  873. int ret;
  874. dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__);
  875. ret = pnfs_register_layoutdriver(&blocklayout_type);
  876. if (ret)
  877. goto out;
  878. init_waitqueue_head(&bl_wq);
  879. mnt = rpc_get_mount();
  880. if (IS_ERR(mnt)) {
  881. ret = PTR_ERR(mnt);
  882. goto out_remove;
  883. }
  884. ret = vfs_path_lookup(mnt->mnt_root,
  885. mnt,
  886. NFS_PIPE_DIRNAME, 0, &path);
  887. if (ret)
  888. goto out_remove;
  889. bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL,
  890. &bl_upcall_ops, 0);
  891. if (IS_ERR(bl_device_pipe)) {
  892. ret = PTR_ERR(bl_device_pipe);
  893. goto out_remove;
  894. }
  895. out:
  896. return ret;
  897. out_remove:
  898. pnfs_unregister_layoutdriver(&blocklayout_type);
  899. return ret;
  900. }
  901. static void __exit nfs4blocklayout_exit(void)
  902. {
  903. dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n",
  904. __func__);
  905. pnfs_unregister_layoutdriver(&blocklayout_type);
  906. rpc_unlink(bl_device_pipe);
  907. }
  908. MODULE_ALIAS("nfs-layouttype4-3");
  909. module_init(nfs4blocklayout_init);
  910. module_exit(nfs4blocklayout_exit);