xfs_aops.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_inum.h"
  34. #include "xfs_log.h"
  35. #include "xfs_sb.h"
  36. #include "xfs_dir.h"
  37. #include "xfs_dir2.h"
  38. #include "xfs_trans.h"
  39. #include "xfs_dmapi.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_bmap_btree.h"
  42. #include "xfs_alloc_btree.h"
  43. #include "xfs_ialloc_btree.h"
  44. #include "xfs_alloc.h"
  45. #include "xfs_btree.h"
  46. #include "xfs_attr_sf.h"
  47. #include "xfs_dir_sf.h"
  48. #include "xfs_dir2_sf.h"
  49. #include "xfs_dinode.h"
  50. #include "xfs_inode.h"
  51. #include "xfs_error.h"
  52. #include "xfs_rw.h"
  53. #include "xfs_iomap.h"
  54. #include <linux/mpage.h>
  55. #include <linux/writeback.h>
  56. STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
  57. STATIC void xfs_convert_page(struct inode *, struct page *, xfs_iomap_t *,
  58. struct writeback_control *wbc, void *, int, int);
  59. #if defined(XFS_RW_TRACE)
  60. void
  61. xfs_page_trace(
  62. int tag,
  63. struct inode *inode,
  64. struct page *page,
  65. int mask)
  66. {
  67. xfs_inode_t *ip;
  68. bhv_desc_t *bdp;
  69. vnode_t *vp = LINVFS_GET_VP(inode);
  70. loff_t isize = i_size_read(inode);
  71. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  72. int delalloc = -1, unmapped = -1, unwritten = -1;
  73. if (page_has_buffers(page))
  74. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  75. bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
  76. ip = XFS_BHVTOI(bdp);
  77. if (!ip->i_rwtrace)
  78. return;
  79. ktrace_enter(ip->i_rwtrace,
  80. (void *)((unsigned long)tag),
  81. (void *)ip,
  82. (void *)inode,
  83. (void *)page,
  84. (void *)((unsigned long)mask),
  85. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  86. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  87. (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
  88. (void *)((unsigned long)(isize & 0xffffffff)),
  89. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  90. (void *)((unsigned long)(offset & 0xffffffff)),
  91. (void *)((unsigned long)delalloc),
  92. (void *)((unsigned long)unmapped),
  93. (void *)((unsigned long)unwritten),
  94. (void *)NULL,
  95. (void *)NULL);
  96. }
  97. #else
  98. #define xfs_page_trace(tag, inode, page, mask)
  99. #endif
  100. void
  101. linvfs_unwritten_done(
  102. struct buffer_head *bh,
  103. int uptodate)
  104. {
  105. xfs_buf_t *pb = (xfs_buf_t *)bh->b_private;
  106. ASSERT(buffer_unwritten(bh));
  107. bh->b_end_io = NULL;
  108. clear_buffer_unwritten(bh);
  109. if (!uptodate)
  110. pagebuf_ioerror(pb, EIO);
  111. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  112. pagebuf_iodone(pb, 1, 1);
  113. }
  114. end_buffer_async_write(bh, uptodate);
  115. }
  116. /*
  117. * Issue transactions to convert a buffer range from unwritten
  118. * to written extents (buffered IO).
  119. */
  120. STATIC void
  121. linvfs_unwritten_convert(
  122. xfs_buf_t *bp)
  123. {
  124. vnode_t *vp = XFS_BUF_FSPRIVATE(bp, vnode_t *);
  125. int error;
  126. BUG_ON(atomic_read(&bp->pb_hold) < 1);
  127. VOP_BMAP(vp, XFS_BUF_OFFSET(bp), XFS_BUF_SIZE(bp),
  128. BMAPI_UNWRITTEN, NULL, NULL, error);
  129. XFS_BUF_SET_FSPRIVATE(bp, NULL);
  130. XFS_BUF_CLR_IODONE_FUNC(bp);
  131. XFS_BUF_UNDATAIO(bp);
  132. iput(LINVFS_GET_IP(vp));
  133. pagebuf_iodone(bp, 0, 0);
  134. }
  135. /*
  136. * Issue transactions to convert a buffer range from unwritten
  137. * to written extents (direct IO).
  138. */
  139. STATIC void
  140. linvfs_unwritten_convert_direct(
  141. struct inode *inode,
  142. loff_t offset,
  143. ssize_t size,
  144. void *private)
  145. {
  146. ASSERT(!private || inode == (struct inode *)private);
  147. /* private indicates an unwritten extent lay beneath this IO */
  148. if (private && size > 0) {
  149. vnode_t *vp = LINVFS_GET_VP(inode);
  150. int error;
  151. VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
  152. }
  153. }
  154. STATIC int
  155. xfs_map_blocks(
  156. struct inode *inode,
  157. loff_t offset,
  158. ssize_t count,
  159. xfs_iomap_t *mapp,
  160. int flags)
  161. {
  162. vnode_t *vp = LINVFS_GET_VP(inode);
  163. int error, nmaps = 1;
  164. VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
  165. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  166. VMODIFY(vp);
  167. return -error;
  168. }
  169. /*
  170. * Finds the corresponding mapping in block @map array of the
  171. * given @offset within a @page.
  172. */
  173. STATIC xfs_iomap_t *
  174. xfs_offset_to_map(
  175. struct page *page,
  176. xfs_iomap_t *iomapp,
  177. unsigned long offset)
  178. {
  179. loff_t full_offset; /* offset from start of file */
  180. ASSERT(offset < PAGE_CACHE_SIZE);
  181. full_offset = page->index; /* NB: using 64bit number */
  182. full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
  183. full_offset += offset; /* offset from page start */
  184. if (full_offset < iomapp->iomap_offset)
  185. return NULL;
  186. if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
  187. return iomapp;
  188. return NULL;
  189. }
  190. STATIC void
  191. xfs_map_at_offset(
  192. struct page *page,
  193. struct buffer_head *bh,
  194. unsigned long offset,
  195. int block_bits,
  196. xfs_iomap_t *iomapp)
  197. {
  198. xfs_daddr_t bn;
  199. loff_t delta;
  200. int sector_shift;
  201. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  202. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  203. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  204. delta = page->index;
  205. delta <<= PAGE_CACHE_SHIFT;
  206. delta += offset;
  207. delta -= iomapp->iomap_offset;
  208. delta >>= block_bits;
  209. sector_shift = block_bits - BBSHIFT;
  210. bn = iomapp->iomap_bn >> sector_shift;
  211. bn += delta;
  212. BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
  213. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  214. lock_buffer(bh);
  215. bh->b_blocknr = bn;
  216. bh->b_bdev = iomapp->iomap_target->pbr_bdev;
  217. set_buffer_mapped(bh);
  218. clear_buffer_delay(bh);
  219. }
  220. /*
  221. * Look for a page at index which is unlocked and contains our
  222. * unwritten extent flagged buffers at its head. Returns page
  223. * locked and with an extra reference count, and length of the
  224. * unwritten extent component on this page that we can write,
  225. * in units of filesystem blocks.
  226. */
  227. STATIC struct page *
  228. xfs_probe_unwritten_page(
  229. struct address_space *mapping,
  230. pgoff_t index,
  231. xfs_iomap_t *iomapp,
  232. xfs_buf_t *pb,
  233. unsigned long max_offset,
  234. unsigned long *fsbs,
  235. unsigned int bbits)
  236. {
  237. struct page *page;
  238. page = find_trylock_page(mapping, index);
  239. if (!page)
  240. return NULL;
  241. if (PageWriteback(page))
  242. goto out;
  243. if (page->mapping && page_has_buffers(page)) {
  244. struct buffer_head *bh, *head;
  245. unsigned long p_offset = 0;
  246. *fsbs = 0;
  247. bh = head = page_buffers(page);
  248. do {
  249. if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
  250. break;
  251. if (!xfs_offset_to_map(page, iomapp, p_offset))
  252. break;
  253. if (p_offset >= max_offset)
  254. break;
  255. xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
  256. set_buffer_unwritten_io(bh);
  257. bh->b_private = pb;
  258. p_offset += bh->b_size;
  259. (*fsbs)++;
  260. } while ((bh = bh->b_this_page) != head);
  261. if (p_offset)
  262. return page;
  263. }
  264. out:
  265. unlock_page(page);
  266. return NULL;
  267. }
  268. /*
  269. * Look for a page at index which is unlocked and not mapped
  270. * yet - clustering for mmap write case.
  271. */
  272. STATIC unsigned int
  273. xfs_probe_unmapped_page(
  274. struct address_space *mapping,
  275. pgoff_t index,
  276. unsigned int pg_offset)
  277. {
  278. struct page *page;
  279. int ret = 0;
  280. page = find_trylock_page(mapping, index);
  281. if (!page)
  282. return 0;
  283. if (PageWriteback(page))
  284. goto out;
  285. if (page->mapping && PageDirty(page)) {
  286. if (page_has_buffers(page)) {
  287. struct buffer_head *bh, *head;
  288. bh = head = page_buffers(page);
  289. do {
  290. if (buffer_mapped(bh) || !buffer_uptodate(bh))
  291. break;
  292. ret += bh->b_size;
  293. if (ret >= pg_offset)
  294. break;
  295. } while ((bh = bh->b_this_page) != head);
  296. } else
  297. ret = PAGE_CACHE_SIZE;
  298. }
  299. out:
  300. unlock_page(page);
  301. return ret;
  302. }
  303. STATIC unsigned int
  304. xfs_probe_unmapped_cluster(
  305. struct inode *inode,
  306. struct page *startpage,
  307. struct buffer_head *bh,
  308. struct buffer_head *head)
  309. {
  310. pgoff_t tindex, tlast, tloff;
  311. unsigned int pg_offset, len, total = 0;
  312. struct address_space *mapping = inode->i_mapping;
  313. /* First sum forwards in this page */
  314. do {
  315. if (buffer_mapped(bh))
  316. break;
  317. total += bh->b_size;
  318. } while ((bh = bh->b_this_page) != head);
  319. /* If we reached the end of the page, sum forwards in
  320. * following pages.
  321. */
  322. if (bh == head) {
  323. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  324. /* Prune this back to avoid pathological behavior */
  325. tloff = min(tlast, startpage->index + 64);
  326. for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
  327. len = xfs_probe_unmapped_page(mapping, tindex,
  328. PAGE_CACHE_SIZE);
  329. if (!len)
  330. return total;
  331. total += len;
  332. }
  333. if (tindex == tlast &&
  334. (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  335. total += xfs_probe_unmapped_page(mapping,
  336. tindex, pg_offset);
  337. }
  338. }
  339. return total;
  340. }
  341. /*
  342. * Probe for a given page (index) in the inode and test if it is delayed
  343. * and without unwritten buffers. Returns page locked and with an extra
  344. * reference count.
  345. */
  346. STATIC struct page *
  347. xfs_probe_delalloc_page(
  348. struct inode *inode,
  349. pgoff_t index)
  350. {
  351. struct page *page;
  352. page = find_trylock_page(inode->i_mapping, index);
  353. if (!page)
  354. return NULL;
  355. if (PageWriteback(page))
  356. goto out;
  357. if (page->mapping && page_has_buffers(page)) {
  358. struct buffer_head *bh, *head;
  359. int acceptable = 0;
  360. bh = head = page_buffers(page);
  361. do {
  362. if (buffer_unwritten(bh)) {
  363. acceptable = 0;
  364. break;
  365. } else if (buffer_delay(bh)) {
  366. acceptable = 1;
  367. }
  368. } while ((bh = bh->b_this_page) != head);
  369. if (acceptable)
  370. return page;
  371. }
  372. out:
  373. unlock_page(page);
  374. return NULL;
  375. }
  376. STATIC int
  377. xfs_map_unwritten(
  378. struct inode *inode,
  379. struct page *start_page,
  380. struct buffer_head *head,
  381. struct buffer_head *curr,
  382. unsigned long p_offset,
  383. int block_bits,
  384. xfs_iomap_t *iomapp,
  385. struct writeback_control *wbc,
  386. int startio,
  387. int all_bh)
  388. {
  389. struct buffer_head *bh = curr;
  390. xfs_iomap_t *tmp;
  391. xfs_buf_t *pb;
  392. loff_t offset, size;
  393. unsigned long nblocks = 0;
  394. offset = start_page->index;
  395. offset <<= PAGE_CACHE_SHIFT;
  396. offset += p_offset;
  397. /* get an "empty" pagebuf to manage IO completion
  398. * Proper values will be set before returning */
  399. pb = pagebuf_lookup(iomapp->iomap_target, 0, 0, 0);
  400. if (!pb)
  401. return -EAGAIN;
  402. /* Take a reference to the inode to prevent it from
  403. * being reclaimed while we have outstanding unwritten
  404. * extent IO on it.
  405. */
  406. if ((igrab(inode)) != inode) {
  407. pagebuf_free(pb);
  408. return -EAGAIN;
  409. }
  410. /* Set the count to 1 initially, this will stop an I/O
  411. * completion callout which happens before we have started
  412. * all the I/O from calling pagebuf_iodone too early.
  413. */
  414. atomic_set(&pb->pb_io_remaining, 1);
  415. /* First map forwards in the page consecutive buffers
  416. * covering this unwritten extent
  417. */
  418. do {
  419. if (!buffer_unwritten(bh))
  420. break;
  421. tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
  422. if (!tmp)
  423. break;
  424. xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
  425. set_buffer_unwritten_io(bh);
  426. bh->b_private = pb;
  427. p_offset += bh->b_size;
  428. nblocks++;
  429. } while ((bh = bh->b_this_page) != head);
  430. atomic_add(nblocks, &pb->pb_io_remaining);
  431. /* If we reached the end of the page, map forwards in any
  432. * following pages which are also covered by this extent.
  433. */
  434. if (bh == head) {
  435. struct address_space *mapping = inode->i_mapping;
  436. pgoff_t tindex, tloff, tlast;
  437. unsigned long bs;
  438. unsigned int pg_offset, bbits = inode->i_blkbits;
  439. struct page *page;
  440. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  441. tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
  442. tloff = min(tlast, tloff);
  443. for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
  444. page = xfs_probe_unwritten_page(mapping,
  445. tindex, iomapp, pb,
  446. PAGE_CACHE_SIZE, &bs, bbits);
  447. if (!page)
  448. break;
  449. nblocks += bs;
  450. atomic_add(bs, &pb->pb_io_remaining);
  451. xfs_convert_page(inode, page, iomapp, wbc, pb,
  452. startio, all_bh);
  453. /* stop if converting the next page might add
  454. * enough blocks that the corresponding byte
  455. * count won't fit in our ulong page buf length */
  456. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  457. goto enough;
  458. }
  459. if (tindex == tlast &&
  460. (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
  461. page = xfs_probe_unwritten_page(mapping,
  462. tindex, iomapp, pb,
  463. pg_offset, &bs, bbits);
  464. if (page) {
  465. nblocks += bs;
  466. atomic_add(bs, &pb->pb_io_remaining);
  467. xfs_convert_page(inode, page, iomapp, wbc, pb,
  468. startio, all_bh);
  469. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  470. goto enough;
  471. }
  472. }
  473. }
  474. enough:
  475. size = nblocks; /* NB: using 64bit number here */
  476. size <<= block_bits; /* convert fsb's to byte range */
  477. XFS_BUF_DATAIO(pb);
  478. XFS_BUF_ASYNC(pb);
  479. XFS_BUF_SET_SIZE(pb, size);
  480. XFS_BUF_SET_COUNT(pb, size);
  481. XFS_BUF_SET_OFFSET(pb, offset);
  482. XFS_BUF_SET_FSPRIVATE(pb, LINVFS_GET_VP(inode));
  483. XFS_BUF_SET_IODONE_FUNC(pb, linvfs_unwritten_convert);
  484. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  485. pagebuf_iodone(pb, 1, 1);
  486. }
  487. return 0;
  488. }
  489. STATIC void
  490. xfs_submit_page(
  491. struct page *page,
  492. struct writeback_control *wbc,
  493. struct buffer_head *bh_arr[],
  494. int bh_count,
  495. int probed_page,
  496. int clear_dirty)
  497. {
  498. struct buffer_head *bh;
  499. int i;
  500. BUG_ON(PageWriteback(page));
  501. if (bh_count)
  502. set_page_writeback(page);
  503. if (clear_dirty)
  504. clear_page_dirty(page);
  505. unlock_page(page);
  506. if (bh_count) {
  507. for (i = 0; i < bh_count; i++) {
  508. bh = bh_arr[i];
  509. mark_buffer_async_write(bh);
  510. if (buffer_unwritten(bh))
  511. set_buffer_unwritten_io(bh);
  512. set_buffer_uptodate(bh);
  513. clear_buffer_dirty(bh);
  514. }
  515. for (i = 0; i < bh_count; i++)
  516. submit_bh(WRITE, bh_arr[i]);
  517. if (probed_page && clear_dirty)
  518. wbc->nr_to_write--; /* Wrote an "extra" page */
  519. }
  520. }
  521. /*
  522. * Allocate & map buffers for page given the extent map. Write it out.
  523. * except for the original page of a writepage, this is called on
  524. * delalloc/unwritten pages only, for the original page it is possible
  525. * that the page has no mapping at all.
  526. */
  527. STATIC void
  528. xfs_convert_page(
  529. struct inode *inode,
  530. struct page *page,
  531. xfs_iomap_t *iomapp,
  532. struct writeback_control *wbc,
  533. void *private,
  534. int startio,
  535. int all_bh)
  536. {
  537. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  538. xfs_iomap_t *mp = iomapp, *tmp;
  539. unsigned long offset, end_offset;
  540. int index = 0;
  541. int bbits = inode->i_blkbits;
  542. int len, page_dirty;
  543. end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
  544. /*
  545. * page_dirty is initially a count of buffers on the page before
  546. * EOF and is decrememted as we move each into a cleanable state.
  547. */
  548. len = 1 << inode->i_blkbits;
  549. end_offset = max(end_offset, PAGE_CACHE_SIZE);
  550. end_offset = roundup(end_offset, len);
  551. page_dirty = end_offset / len;
  552. offset = 0;
  553. bh = head = page_buffers(page);
  554. do {
  555. if (offset >= end_offset)
  556. break;
  557. if (!(PageUptodate(page) || buffer_uptodate(bh)))
  558. continue;
  559. if (buffer_mapped(bh) && all_bh &&
  560. !(buffer_unwritten(bh) || buffer_delay(bh))) {
  561. if (startio) {
  562. lock_buffer(bh);
  563. bh_arr[index++] = bh;
  564. page_dirty--;
  565. }
  566. continue;
  567. }
  568. tmp = xfs_offset_to_map(page, mp, offset);
  569. if (!tmp)
  570. continue;
  571. ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
  572. ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
  573. /* If this is a new unwritten extent buffer (i.e. one
  574. * that we haven't passed in private data for, we must
  575. * now map this buffer too.
  576. */
  577. if (buffer_unwritten(bh) && !bh->b_end_io) {
  578. ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
  579. xfs_map_unwritten(inode, page, head, bh, offset,
  580. bbits, tmp, wbc, startio, all_bh);
  581. } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
  582. xfs_map_at_offset(page, bh, offset, bbits, tmp);
  583. if (buffer_unwritten(bh)) {
  584. set_buffer_unwritten_io(bh);
  585. bh->b_private = private;
  586. ASSERT(private);
  587. }
  588. }
  589. if (startio) {
  590. bh_arr[index++] = bh;
  591. } else {
  592. set_buffer_dirty(bh);
  593. unlock_buffer(bh);
  594. mark_buffer_dirty(bh);
  595. }
  596. page_dirty--;
  597. } while (offset += len, (bh = bh->b_this_page) != head);
  598. if (startio && index) {
  599. xfs_submit_page(page, wbc, bh_arr, index, 1, !page_dirty);
  600. } else {
  601. unlock_page(page);
  602. }
  603. }
  604. /*
  605. * Convert & write out a cluster of pages in the same extent as defined
  606. * by mp and following the start page.
  607. */
  608. STATIC void
  609. xfs_cluster_write(
  610. struct inode *inode,
  611. pgoff_t tindex,
  612. xfs_iomap_t *iomapp,
  613. struct writeback_control *wbc,
  614. int startio,
  615. int all_bh,
  616. pgoff_t tlast)
  617. {
  618. struct page *page;
  619. for (; tindex <= tlast; tindex++) {
  620. page = xfs_probe_delalloc_page(inode, tindex);
  621. if (!page)
  622. break;
  623. xfs_convert_page(inode, page, iomapp, wbc, NULL,
  624. startio, all_bh);
  625. }
  626. }
  627. /*
  628. * Calling this without startio set means we are being asked to make a dirty
  629. * page ready for freeing it's buffers. When called with startio set then
  630. * we are coming from writepage.
  631. *
  632. * When called with startio set it is important that we write the WHOLE
  633. * page if possible.
  634. * The bh->b_state's cannot know if any of the blocks or which block for
  635. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  636. * only vaild if the page itself isn't completely uptodate. Some layers
  637. * may clear the page dirty flag prior to calling write page, under the
  638. * assumption the entire page will be written out; by not writing out the
  639. * whole page the page can be reused before all valid dirty data is
  640. * written out. Note: in the case of a page that has been dirty'd by
  641. * mapwrite and but partially setup by block_prepare_write the
  642. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  643. * valid state, thus the whole page must be written out thing.
  644. */
  645. STATIC int
  646. xfs_page_state_convert(
  647. struct inode *inode,
  648. struct page *page,
  649. struct writeback_control *wbc,
  650. int startio,
  651. int unmapped) /* also implies page uptodate */
  652. {
  653. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  654. xfs_iomap_t *iomp, iomap;
  655. loff_t offset;
  656. unsigned long p_offset = 0;
  657. __uint64_t end_offset;
  658. pgoff_t end_index, last_index, tlast;
  659. int len, err, i, cnt = 0, uptodate = 1;
  660. int flags;
  661. int page_dirty;
  662. /* wait for other IO threads? */
  663. flags = (startio && wbc->sync_mode != WB_SYNC_NONE) ? 0 : BMAPI_TRYLOCK;
  664. /* Is this page beyond the end of the file? */
  665. offset = i_size_read(inode);
  666. end_index = offset >> PAGE_CACHE_SHIFT;
  667. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  668. if (page->index >= end_index) {
  669. if ((page->index >= end_index + 1) ||
  670. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  671. err = -EIO;
  672. goto error;
  673. }
  674. }
  675. end_offset = min_t(unsigned long long,
  676. (loff_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  677. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  678. /*
  679. * page_dirty is initially a count of buffers on the page before
  680. * EOF and is decrememted as we move each into a cleanable state.
  681. */
  682. len = 1 << inode->i_blkbits;
  683. p_offset = max(p_offset, PAGE_CACHE_SIZE);
  684. p_offset = roundup(p_offset, len);
  685. page_dirty = p_offset / len;
  686. iomp = NULL;
  687. p_offset = 0;
  688. bh = head = page_buffers(page);
  689. do {
  690. if (offset >= end_offset)
  691. break;
  692. if (!buffer_uptodate(bh))
  693. uptodate = 0;
  694. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
  695. continue;
  696. if (iomp) {
  697. iomp = xfs_offset_to_map(page, &iomap, p_offset);
  698. }
  699. /*
  700. * First case, map an unwritten extent and prepare for
  701. * extent state conversion transaction on completion.
  702. */
  703. if (buffer_unwritten(bh)) {
  704. if (!startio)
  705. continue;
  706. if (!iomp) {
  707. err = xfs_map_blocks(inode, offset, len, &iomap,
  708. BMAPI_READ|BMAPI_IGNSTATE);
  709. if (err) {
  710. goto error;
  711. }
  712. iomp = xfs_offset_to_map(page, &iomap,
  713. p_offset);
  714. }
  715. if (iomp) {
  716. if (!bh->b_end_io) {
  717. err = xfs_map_unwritten(inode, page,
  718. head, bh, p_offset,
  719. inode->i_blkbits, iomp,
  720. wbc, startio, unmapped);
  721. if (err) {
  722. goto error;
  723. }
  724. } else {
  725. set_bit(BH_Lock, &bh->b_state);
  726. }
  727. BUG_ON(!buffer_locked(bh));
  728. bh_arr[cnt++] = bh;
  729. page_dirty--;
  730. }
  731. /*
  732. * Second case, allocate space for a delalloc buffer.
  733. * We can return EAGAIN here in the release page case.
  734. */
  735. } else if (buffer_delay(bh)) {
  736. if (!iomp) {
  737. err = xfs_map_blocks(inode, offset, len, &iomap,
  738. BMAPI_ALLOCATE | flags);
  739. if (err) {
  740. goto error;
  741. }
  742. iomp = xfs_offset_to_map(page, &iomap,
  743. p_offset);
  744. }
  745. if (iomp) {
  746. xfs_map_at_offset(page, bh, p_offset,
  747. inode->i_blkbits, iomp);
  748. if (startio) {
  749. bh_arr[cnt++] = bh;
  750. } else {
  751. set_buffer_dirty(bh);
  752. unlock_buffer(bh);
  753. mark_buffer_dirty(bh);
  754. }
  755. page_dirty--;
  756. }
  757. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  758. (unmapped || startio)) {
  759. if (!buffer_mapped(bh)) {
  760. int size;
  761. /*
  762. * Getting here implies an unmapped buffer
  763. * was found, and we are in a path where we
  764. * need to write the whole page out.
  765. */
  766. if (!iomp) {
  767. size = xfs_probe_unmapped_cluster(
  768. inode, page, bh, head);
  769. err = xfs_map_blocks(inode, offset,
  770. size, &iomap,
  771. BMAPI_WRITE|BMAPI_MMAP);
  772. if (err) {
  773. goto error;
  774. }
  775. iomp = xfs_offset_to_map(page, &iomap,
  776. p_offset);
  777. }
  778. if (iomp) {
  779. xfs_map_at_offset(page,
  780. bh, p_offset,
  781. inode->i_blkbits, iomp);
  782. if (startio) {
  783. bh_arr[cnt++] = bh;
  784. } else {
  785. set_buffer_dirty(bh);
  786. unlock_buffer(bh);
  787. mark_buffer_dirty(bh);
  788. }
  789. page_dirty--;
  790. }
  791. } else if (startio) {
  792. if (buffer_uptodate(bh) &&
  793. !test_and_set_bit(BH_Lock, &bh->b_state)) {
  794. bh_arr[cnt++] = bh;
  795. page_dirty--;
  796. }
  797. }
  798. }
  799. } while (offset += len, p_offset += len,
  800. ((bh = bh->b_this_page) != head));
  801. if (uptodate && bh == head)
  802. SetPageUptodate(page);
  803. if (startio) {
  804. WARN_ON(page_dirty);
  805. xfs_submit_page(page, wbc, bh_arr, cnt, 0, !page_dirty);
  806. }
  807. if (iomp) {
  808. offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
  809. PAGE_CACHE_SHIFT;
  810. tlast = min_t(pgoff_t, offset, last_index);
  811. xfs_cluster_write(inode, page->index + 1, iomp, wbc,
  812. startio, unmapped, tlast);
  813. }
  814. return page_dirty;
  815. error:
  816. for (i = 0; i < cnt; i++) {
  817. unlock_buffer(bh_arr[i]);
  818. }
  819. /*
  820. * If it's delalloc and we have nowhere to put it,
  821. * throw it away, unless the lower layers told
  822. * us to try again.
  823. */
  824. if (err != -EAGAIN) {
  825. if (!unmapped) {
  826. block_invalidatepage(page, 0);
  827. }
  828. ClearPageUptodate(page);
  829. }
  830. return err;
  831. }
  832. STATIC int
  833. __linvfs_get_block(
  834. struct inode *inode,
  835. sector_t iblock,
  836. unsigned long blocks,
  837. struct buffer_head *bh_result,
  838. int create,
  839. int direct,
  840. bmapi_flags_t flags)
  841. {
  842. vnode_t *vp = LINVFS_GET_VP(inode);
  843. xfs_iomap_t iomap;
  844. int retpbbm = 1;
  845. int error;
  846. ssize_t size;
  847. loff_t offset = (loff_t)iblock << inode->i_blkbits;
  848. if (blocks)
  849. size = blocks << inode->i_blkbits;
  850. else
  851. size = 1 << inode->i_blkbits;
  852. VOP_BMAP(vp, offset, size,
  853. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  854. if (error)
  855. return -error;
  856. if (retpbbm == 0)
  857. return 0;
  858. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  859. xfs_daddr_t bn;
  860. loff_t delta;
  861. /* For unwritten extents do not report a disk address on
  862. * the read case (treat as if we're reading into a hole).
  863. */
  864. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  865. delta = offset - iomap.iomap_offset;
  866. delta >>= inode->i_blkbits;
  867. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  868. bn += delta;
  869. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  870. bh_result->b_blocknr = bn;
  871. set_buffer_mapped(bh_result);
  872. }
  873. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  874. if (direct)
  875. bh_result->b_private = inode;
  876. set_buffer_unwritten(bh_result);
  877. set_buffer_delay(bh_result);
  878. }
  879. }
  880. /* If this is a realtime file, data might be on a new device */
  881. bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
  882. /* If we previously allocated a block out beyond eof and
  883. * we are now coming back to use it then we will need to
  884. * flag it as new even if it has a disk address.
  885. */
  886. if (create &&
  887. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  888. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) {
  889. set_buffer_new(bh_result);
  890. }
  891. if (iomap.iomap_flags & IOMAP_DELAY) {
  892. BUG_ON(direct);
  893. if (create) {
  894. set_buffer_uptodate(bh_result);
  895. set_buffer_mapped(bh_result);
  896. set_buffer_delay(bh_result);
  897. }
  898. }
  899. if (blocks) {
  900. bh_result->b_size = (ssize_t)min(
  901. (loff_t)(iomap.iomap_bsize - iomap.iomap_delta),
  902. (loff_t)(blocks << inode->i_blkbits));
  903. }
  904. return 0;
  905. }
  906. int
  907. linvfs_get_block(
  908. struct inode *inode,
  909. sector_t iblock,
  910. struct buffer_head *bh_result,
  911. int create)
  912. {
  913. return __linvfs_get_block(inode, iblock, 0, bh_result,
  914. create, 0, BMAPI_WRITE);
  915. }
  916. STATIC int
  917. linvfs_get_blocks_direct(
  918. struct inode *inode,
  919. sector_t iblock,
  920. unsigned long max_blocks,
  921. struct buffer_head *bh_result,
  922. int create)
  923. {
  924. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  925. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  926. }
  927. STATIC ssize_t
  928. linvfs_direct_IO(
  929. int rw,
  930. struct kiocb *iocb,
  931. const struct iovec *iov,
  932. loff_t offset,
  933. unsigned long nr_segs)
  934. {
  935. struct file *file = iocb->ki_filp;
  936. struct inode *inode = file->f_mapping->host;
  937. vnode_t *vp = LINVFS_GET_VP(inode);
  938. xfs_iomap_t iomap;
  939. int maps = 1;
  940. int error;
  941. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  942. if (error)
  943. return -error;
  944. return blockdev_direct_IO_own_locking(rw, iocb, inode,
  945. iomap.iomap_target->pbr_bdev,
  946. iov, offset, nr_segs,
  947. linvfs_get_blocks_direct,
  948. linvfs_unwritten_convert_direct);
  949. }
  950. STATIC sector_t
  951. linvfs_bmap(
  952. struct address_space *mapping,
  953. sector_t block)
  954. {
  955. struct inode *inode = (struct inode *)mapping->host;
  956. vnode_t *vp = LINVFS_GET_VP(inode);
  957. int error;
  958. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  959. VOP_RWLOCK(vp, VRWLOCK_READ);
  960. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  961. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  962. return generic_block_bmap(mapping, block, linvfs_get_block);
  963. }
  964. STATIC int
  965. linvfs_readpage(
  966. struct file *unused,
  967. struct page *page)
  968. {
  969. return mpage_readpage(page, linvfs_get_block);
  970. }
  971. STATIC int
  972. linvfs_readpages(
  973. struct file *unused,
  974. struct address_space *mapping,
  975. struct list_head *pages,
  976. unsigned nr_pages)
  977. {
  978. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  979. }
  980. STATIC void
  981. xfs_count_page_state(
  982. struct page *page,
  983. int *delalloc,
  984. int *unmapped,
  985. int *unwritten)
  986. {
  987. struct buffer_head *bh, *head;
  988. *delalloc = *unmapped = *unwritten = 0;
  989. bh = head = page_buffers(page);
  990. do {
  991. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  992. (*unmapped) = 1;
  993. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  994. clear_buffer_unwritten(bh);
  995. else if (buffer_unwritten(bh))
  996. (*unwritten) = 1;
  997. else if (buffer_delay(bh))
  998. (*delalloc) = 1;
  999. } while ((bh = bh->b_this_page) != head);
  1000. }
  1001. /*
  1002. * writepage: Called from one of two places:
  1003. *
  1004. * 1. we are flushing a delalloc buffer head.
  1005. *
  1006. * 2. we are writing out a dirty page. Typically the page dirty
  1007. * state is cleared before we get here. In this case is it
  1008. * conceivable we have no buffer heads.
  1009. *
  1010. * For delalloc space on the page we need to allocate space and
  1011. * flush it. For unmapped buffer heads on the page we should
  1012. * allocate space if the page is uptodate. For any other dirty
  1013. * buffer heads on the page we should flush them.
  1014. *
  1015. * If we detect that a transaction would be required to flush
  1016. * the page, we have to check the process flags first, if we
  1017. * are already in a transaction or disk I/O during allocations
  1018. * is off, we need to fail the writepage and redirty the page.
  1019. */
  1020. STATIC int
  1021. linvfs_writepage(
  1022. struct page *page,
  1023. struct writeback_control *wbc)
  1024. {
  1025. int error;
  1026. int need_trans;
  1027. int delalloc, unmapped, unwritten;
  1028. struct inode *inode = page->mapping->host;
  1029. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1030. /*
  1031. * We need a transaction if:
  1032. * 1. There are delalloc buffers on the page
  1033. * 2. The page is uptodate and we have unmapped buffers
  1034. * 3. The page is uptodate and we have no buffers
  1035. * 4. There are unwritten buffers on the page
  1036. */
  1037. if (!page_has_buffers(page)) {
  1038. unmapped = 1;
  1039. need_trans = 1;
  1040. } else {
  1041. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1042. if (!PageUptodate(page))
  1043. unmapped = 0;
  1044. need_trans = delalloc + unmapped + unwritten;
  1045. }
  1046. /*
  1047. * If we need a transaction and the process flags say
  1048. * we are already in a transaction, or no IO is allowed
  1049. * then mark the page dirty again and leave the page
  1050. * as is.
  1051. */
  1052. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1053. goto out_fail;
  1054. /*
  1055. * Delay hooking up buffer heads until we have
  1056. * made our go/no-go decision.
  1057. */
  1058. if (!page_has_buffers(page))
  1059. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1060. /*
  1061. * Convert delayed allocate, unwritten or unmapped space
  1062. * to real space and flush out to disk.
  1063. */
  1064. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1065. if (error == -EAGAIN)
  1066. goto out_fail;
  1067. if (unlikely(error < 0))
  1068. goto out_unlock;
  1069. return 0;
  1070. out_fail:
  1071. redirty_page_for_writepage(wbc, page);
  1072. unlock_page(page);
  1073. return 0;
  1074. out_unlock:
  1075. unlock_page(page);
  1076. return error;
  1077. }
  1078. /*
  1079. * Called to move a page into cleanable state - and from there
  1080. * to be released. Possibly the page is already clean. We always
  1081. * have buffer heads in this call.
  1082. *
  1083. * Returns 0 if the page is ok to release, 1 otherwise.
  1084. *
  1085. * Possible scenarios are:
  1086. *
  1087. * 1. We are being called to release a page which has been written
  1088. * to via regular I/O. buffer heads will be dirty and possibly
  1089. * delalloc. If no delalloc buffer heads in this case then we
  1090. * can just return zero.
  1091. *
  1092. * 2. We are called to release a page which has been written via
  1093. * mmap, all we need to do is ensure there is no delalloc
  1094. * state in the buffer heads, if not we can let the caller
  1095. * free them and we should come back later via writepage.
  1096. */
  1097. STATIC int
  1098. linvfs_release_page(
  1099. struct page *page,
  1100. int gfp_mask)
  1101. {
  1102. struct inode *inode = page->mapping->host;
  1103. int dirty, delalloc, unmapped, unwritten;
  1104. struct writeback_control wbc = {
  1105. .sync_mode = WB_SYNC_ALL,
  1106. .nr_to_write = 1,
  1107. };
  1108. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1109. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1110. if (!delalloc && !unwritten)
  1111. goto free_buffers;
  1112. if (!(gfp_mask & __GFP_FS))
  1113. return 0;
  1114. /* If we are already inside a transaction or the thread cannot
  1115. * do I/O, we cannot release this page.
  1116. */
  1117. if (PFLAGS_TEST_FSTRANS())
  1118. return 0;
  1119. /*
  1120. * Convert delalloc space to real space, do not flush the
  1121. * data out to disk, that will be done by the caller.
  1122. * Never need to allocate space here - we will always
  1123. * come back to writepage in that case.
  1124. */
  1125. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1126. if (dirty == 0 && !unwritten)
  1127. goto free_buffers;
  1128. return 0;
  1129. free_buffers:
  1130. return try_to_free_buffers(page);
  1131. }
  1132. STATIC int
  1133. linvfs_prepare_write(
  1134. struct file *file,
  1135. struct page *page,
  1136. unsigned int from,
  1137. unsigned int to)
  1138. {
  1139. return block_prepare_write(page, from, to, linvfs_get_block);
  1140. }
  1141. struct address_space_operations linvfs_aops = {
  1142. .readpage = linvfs_readpage,
  1143. .readpages = linvfs_readpages,
  1144. .writepage = linvfs_writepage,
  1145. .sync_page = block_sync_page,
  1146. .releasepage = linvfs_release_page,
  1147. .prepare_write = linvfs_prepare_write,
  1148. .commit_write = generic_commit_write,
  1149. .bmap = linvfs_bmap,
  1150. .direct_IO = linvfs_direct_IO,
  1151. };