blocklayout.c 31 KB

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