blocklayout.c 30 KB

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