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 kiocb *iocb,
  142. loff_t offset,
  143. ssize_t size,
  144. void *private)
  145. {
  146. struct inode *inode = iocb->ki_filp->f_dentry->d_inode;
  147. ASSERT(!private || inode == (struct inode *)private);
  148. /* private indicates an unwritten extent lay beneath this IO */
  149. if (private && size > 0) {
  150. vnode_t *vp = LINVFS_GET_VP(inode);
  151. int error;
  152. VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
  153. }
  154. }
  155. STATIC int
  156. xfs_map_blocks(
  157. struct inode *inode,
  158. loff_t offset,
  159. ssize_t count,
  160. xfs_iomap_t *mapp,
  161. int flags)
  162. {
  163. vnode_t *vp = LINVFS_GET_VP(inode);
  164. int error, nmaps = 1;
  165. VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
  166. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  167. VMODIFY(vp);
  168. return -error;
  169. }
  170. /*
  171. * Finds the corresponding mapping in block @map array of the
  172. * given @offset within a @page.
  173. */
  174. STATIC xfs_iomap_t *
  175. xfs_offset_to_map(
  176. struct page *page,
  177. xfs_iomap_t *iomapp,
  178. unsigned long offset)
  179. {
  180. loff_t full_offset; /* offset from start of file */
  181. ASSERT(offset < PAGE_CACHE_SIZE);
  182. full_offset = page->index; /* NB: using 64bit number */
  183. full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
  184. full_offset += offset; /* offset from page start */
  185. if (full_offset < iomapp->iomap_offset)
  186. return NULL;
  187. if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
  188. return iomapp;
  189. return NULL;
  190. }
  191. STATIC void
  192. xfs_map_at_offset(
  193. struct page *page,
  194. struct buffer_head *bh,
  195. unsigned long offset,
  196. int block_bits,
  197. xfs_iomap_t *iomapp)
  198. {
  199. xfs_daddr_t bn;
  200. loff_t delta;
  201. int sector_shift;
  202. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  203. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  204. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  205. delta = page->index;
  206. delta <<= PAGE_CACHE_SHIFT;
  207. delta += offset;
  208. delta -= iomapp->iomap_offset;
  209. delta >>= block_bits;
  210. sector_shift = block_bits - BBSHIFT;
  211. bn = iomapp->iomap_bn >> sector_shift;
  212. bn += delta;
  213. BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
  214. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  215. lock_buffer(bh);
  216. bh->b_blocknr = bn;
  217. bh->b_bdev = iomapp->iomap_target->pbr_bdev;
  218. set_buffer_mapped(bh);
  219. clear_buffer_delay(bh);
  220. }
  221. /*
  222. * Look for a page at index which is unlocked and contains our
  223. * unwritten extent flagged buffers at its head. Returns page
  224. * locked and with an extra reference count, and length of the
  225. * unwritten extent component on this page that we can write,
  226. * in units of filesystem blocks.
  227. */
  228. STATIC struct page *
  229. xfs_probe_unwritten_page(
  230. struct address_space *mapping,
  231. pgoff_t index,
  232. xfs_iomap_t *iomapp,
  233. xfs_buf_t *pb,
  234. unsigned long max_offset,
  235. unsigned long *fsbs,
  236. unsigned int bbits)
  237. {
  238. struct page *page;
  239. page = find_trylock_page(mapping, index);
  240. if (!page)
  241. return NULL;
  242. if (PageWriteback(page))
  243. goto out;
  244. if (page->mapping && page_has_buffers(page)) {
  245. struct buffer_head *bh, *head;
  246. unsigned long p_offset = 0;
  247. *fsbs = 0;
  248. bh = head = page_buffers(page);
  249. do {
  250. if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
  251. break;
  252. if (!xfs_offset_to_map(page, iomapp, p_offset))
  253. break;
  254. if (p_offset >= max_offset)
  255. break;
  256. xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
  257. set_buffer_unwritten_io(bh);
  258. bh->b_private = pb;
  259. p_offset += bh->b_size;
  260. (*fsbs)++;
  261. } while ((bh = bh->b_this_page) != head);
  262. if (p_offset)
  263. return page;
  264. }
  265. out:
  266. unlock_page(page);
  267. return NULL;
  268. }
  269. /*
  270. * Look for a page at index which is unlocked and not mapped
  271. * yet - clustering for mmap write case.
  272. */
  273. STATIC unsigned int
  274. xfs_probe_unmapped_page(
  275. struct address_space *mapping,
  276. pgoff_t index,
  277. unsigned int pg_offset)
  278. {
  279. struct page *page;
  280. int ret = 0;
  281. page = find_trylock_page(mapping, index);
  282. if (!page)
  283. return 0;
  284. if (PageWriteback(page))
  285. goto out;
  286. if (page->mapping && PageDirty(page)) {
  287. if (page_has_buffers(page)) {
  288. struct buffer_head *bh, *head;
  289. bh = head = page_buffers(page);
  290. do {
  291. if (buffer_mapped(bh) || !buffer_uptodate(bh))
  292. break;
  293. ret += bh->b_size;
  294. if (ret >= pg_offset)
  295. break;
  296. } while ((bh = bh->b_this_page) != head);
  297. } else
  298. ret = PAGE_CACHE_SIZE;
  299. }
  300. out:
  301. unlock_page(page);
  302. return ret;
  303. }
  304. STATIC unsigned int
  305. xfs_probe_unmapped_cluster(
  306. struct inode *inode,
  307. struct page *startpage,
  308. struct buffer_head *bh,
  309. struct buffer_head *head)
  310. {
  311. pgoff_t tindex, tlast, tloff;
  312. unsigned int pg_offset, len, total = 0;
  313. struct address_space *mapping = inode->i_mapping;
  314. /* First sum forwards in this page */
  315. do {
  316. if (buffer_mapped(bh))
  317. break;
  318. total += bh->b_size;
  319. } while ((bh = bh->b_this_page) != head);
  320. /* If we reached the end of the page, sum forwards in
  321. * following pages.
  322. */
  323. if (bh == head) {
  324. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  325. /* Prune this back to avoid pathological behavior */
  326. tloff = min(tlast, startpage->index + 64);
  327. for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
  328. len = xfs_probe_unmapped_page(mapping, tindex,
  329. PAGE_CACHE_SIZE);
  330. if (!len)
  331. return total;
  332. total += len;
  333. }
  334. if (tindex == tlast &&
  335. (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  336. total += xfs_probe_unmapped_page(mapping,
  337. tindex, pg_offset);
  338. }
  339. }
  340. return total;
  341. }
  342. /*
  343. * Probe for a given page (index) in the inode and test if it is delayed
  344. * and without unwritten buffers. Returns page locked and with an extra
  345. * reference count.
  346. */
  347. STATIC struct page *
  348. xfs_probe_delalloc_page(
  349. struct inode *inode,
  350. pgoff_t index)
  351. {
  352. struct page *page;
  353. page = find_trylock_page(inode->i_mapping, index);
  354. if (!page)
  355. return NULL;
  356. if (PageWriteback(page))
  357. goto out;
  358. if (page->mapping && page_has_buffers(page)) {
  359. struct buffer_head *bh, *head;
  360. int acceptable = 0;
  361. bh = head = page_buffers(page);
  362. do {
  363. if (buffer_unwritten(bh)) {
  364. acceptable = 0;
  365. break;
  366. } else if (buffer_delay(bh)) {
  367. acceptable = 1;
  368. }
  369. } while ((bh = bh->b_this_page) != head);
  370. if (acceptable)
  371. return page;
  372. }
  373. out:
  374. unlock_page(page);
  375. return NULL;
  376. }
  377. STATIC int
  378. xfs_map_unwritten(
  379. struct inode *inode,
  380. struct page *start_page,
  381. struct buffer_head *head,
  382. struct buffer_head *curr,
  383. unsigned long p_offset,
  384. int block_bits,
  385. xfs_iomap_t *iomapp,
  386. struct writeback_control *wbc,
  387. int startio,
  388. int all_bh)
  389. {
  390. struct buffer_head *bh = curr;
  391. xfs_iomap_t *tmp;
  392. xfs_buf_t *pb;
  393. loff_t offset, size;
  394. unsigned long nblocks = 0;
  395. offset = start_page->index;
  396. offset <<= PAGE_CACHE_SHIFT;
  397. offset += p_offset;
  398. /* get an "empty" pagebuf to manage IO completion
  399. * Proper values will be set before returning */
  400. pb = pagebuf_lookup(iomapp->iomap_target, 0, 0, 0);
  401. if (!pb)
  402. return -EAGAIN;
  403. /* Take a reference to the inode to prevent it from
  404. * being reclaimed while we have outstanding unwritten
  405. * extent IO on it.
  406. */
  407. if ((igrab(inode)) != inode) {
  408. pagebuf_free(pb);
  409. return -EAGAIN;
  410. }
  411. /* Set the count to 1 initially, this will stop an I/O
  412. * completion callout which happens before we have started
  413. * all the I/O from calling pagebuf_iodone too early.
  414. */
  415. atomic_set(&pb->pb_io_remaining, 1);
  416. /* First map forwards in the page consecutive buffers
  417. * covering this unwritten extent
  418. */
  419. do {
  420. if (!buffer_unwritten(bh))
  421. break;
  422. tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
  423. if (!tmp)
  424. break;
  425. xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
  426. set_buffer_unwritten_io(bh);
  427. bh->b_private = pb;
  428. p_offset += bh->b_size;
  429. nblocks++;
  430. } while ((bh = bh->b_this_page) != head);
  431. atomic_add(nblocks, &pb->pb_io_remaining);
  432. /* If we reached the end of the page, map forwards in any
  433. * following pages which are also covered by this extent.
  434. */
  435. if (bh == head) {
  436. struct address_space *mapping = inode->i_mapping;
  437. pgoff_t tindex, tloff, tlast;
  438. unsigned long bs;
  439. unsigned int pg_offset, bbits = inode->i_blkbits;
  440. struct page *page;
  441. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  442. tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
  443. tloff = min(tlast, tloff);
  444. for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
  445. page = xfs_probe_unwritten_page(mapping,
  446. tindex, iomapp, pb,
  447. PAGE_CACHE_SIZE, &bs, bbits);
  448. if (!page)
  449. break;
  450. nblocks += bs;
  451. atomic_add(bs, &pb->pb_io_remaining);
  452. xfs_convert_page(inode, page, iomapp, wbc, pb,
  453. startio, all_bh);
  454. /* stop if converting the next page might add
  455. * enough blocks that the corresponding byte
  456. * count won't fit in our ulong page buf length */
  457. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  458. goto enough;
  459. }
  460. if (tindex == tlast &&
  461. (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
  462. page = xfs_probe_unwritten_page(mapping,
  463. tindex, iomapp, pb,
  464. pg_offset, &bs, bbits);
  465. if (page) {
  466. nblocks += bs;
  467. atomic_add(bs, &pb->pb_io_remaining);
  468. xfs_convert_page(inode, page, iomapp, wbc, pb,
  469. startio, all_bh);
  470. if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
  471. goto enough;
  472. }
  473. }
  474. }
  475. enough:
  476. size = nblocks; /* NB: using 64bit number here */
  477. size <<= block_bits; /* convert fsb's to byte range */
  478. XFS_BUF_DATAIO(pb);
  479. XFS_BUF_ASYNC(pb);
  480. XFS_BUF_SET_SIZE(pb, size);
  481. XFS_BUF_SET_COUNT(pb, size);
  482. XFS_BUF_SET_OFFSET(pb, offset);
  483. XFS_BUF_SET_FSPRIVATE(pb, LINVFS_GET_VP(inode));
  484. XFS_BUF_SET_IODONE_FUNC(pb, linvfs_unwritten_convert);
  485. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  486. pagebuf_iodone(pb, 1, 1);
  487. }
  488. return 0;
  489. }
  490. STATIC void
  491. xfs_submit_page(
  492. struct page *page,
  493. struct writeback_control *wbc,
  494. struct buffer_head *bh_arr[],
  495. int bh_count,
  496. int probed_page,
  497. int clear_dirty)
  498. {
  499. struct buffer_head *bh;
  500. int i;
  501. BUG_ON(PageWriteback(page));
  502. if (bh_count)
  503. set_page_writeback(page);
  504. if (clear_dirty)
  505. clear_page_dirty(page);
  506. unlock_page(page);
  507. if (bh_count) {
  508. for (i = 0; i < bh_count; i++) {
  509. bh = bh_arr[i];
  510. mark_buffer_async_write(bh);
  511. if (buffer_unwritten(bh))
  512. set_buffer_unwritten_io(bh);
  513. set_buffer_uptodate(bh);
  514. clear_buffer_dirty(bh);
  515. }
  516. for (i = 0; i < bh_count; i++)
  517. submit_bh(WRITE, bh_arr[i]);
  518. if (probed_page && clear_dirty)
  519. wbc->nr_to_write--; /* Wrote an "extra" page */
  520. }
  521. }
  522. /*
  523. * Allocate & map buffers for page given the extent map. Write it out.
  524. * except for the original page of a writepage, this is called on
  525. * delalloc/unwritten pages only, for the original page it is possible
  526. * that the page has no mapping at all.
  527. */
  528. STATIC void
  529. xfs_convert_page(
  530. struct inode *inode,
  531. struct page *page,
  532. xfs_iomap_t *iomapp,
  533. struct writeback_control *wbc,
  534. void *private,
  535. int startio,
  536. int all_bh)
  537. {
  538. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  539. xfs_iomap_t *mp = iomapp, *tmp;
  540. unsigned long offset, end_offset;
  541. int index = 0;
  542. int bbits = inode->i_blkbits;
  543. int len, page_dirty;
  544. end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
  545. /*
  546. * page_dirty is initially a count of buffers on the page before
  547. * EOF and is decrememted as we move each into a cleanable state.
  548. */
  549. len = 1 << inode->i_blkbits;
  550. end_offset = max(end_offset, PAGE_CACHE_SIZE);
  551. end_offset = roundup(end_offset, len);
  552. page_dirty = end_offset / len;
  553. offset = 0;
  554. bh = head = page_buffers(page);
  555. do {
  556. if (offset >= end_offset)
  557. break;
  558. if (!(PageUptodate(page) || buffer_uptodate(bh)))
  559. continue;
  560. if (buffer_mapped(bh) && all_bh &&
  561. !(buffer_unwritten(bh) || buffer_delay(bh))) {
  562. if (startio) {
  563. lock_buffer(bh);
  564. bh_arr[index++] = bh;
  565. page_dirty--;
  566. }
  567. continue;
  568. }
  569. tmp = xfs_offset_to_map(page, mp, offset);
  570. if (!tmp)
  571. continue;
  572. ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
  573. ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
  574. /* If this is a new unwritten extent buffer (i.e. one
  575. * that we haven't passed in private data for, we must
  576. * now map this buffer too.
  577. */
  578. if (buffer_unwritten(bh) && !bh->b_end_io) {
  579. ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
  580. xfs_map_unwritten(inode, page, head, bh, offset,
  581. bbits, tmp, wbc, startio, all_bh);
  582. } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
  583. xfs_map_at_offset(page, bh, offset, bbits, tmp);
  584. if (buffer_unwritten(bh)) {
  585. set_buffer_unwritten_io(bh);
  586. bh->b_private = private;
  587. ASSERT(private);
  588. }
  589. }
  590. if (startio) {
  591. bh_arr[index++] = bh;
  592. } else {
  593. set_buffer_dirty(bh);
  594. unlock_buffer(bh);
  595. mark_buffer_dirty(bh);
  596. }
  597. page_dirty--;
  598. } while (offset += len, (bh = bh->b_this_page) != head);
  599. if (startio && index) {
  600. xfs_submit_page(page, wbc, bh_arr, index, 1, !page_dirty);
  601. } else {
  602. unlock_page(page);
  603. }
  604. }
  605. /*
  606. * Convert & write out a cluster of pages in the same extent as defined
  607. * by mp and following the start page.
  608. */
  609. STATIC void
  610. xfs_cluster_write(
  611. struct inode *inode,
  612. pgoff_t tindex,
  613. xfs_iomap_t *iomapp,
  614. struct writeback_control *wbc,
  615. int startio,
  616. int all_bh,
  617. pgoff_t tlast)
  618. {
  619. struct page *page;
  620. for (; tindex <= tlast; tindex++) {
  621. page = xfs_probe_delalloc_page(inode, tindex);
  622. if (!page)
  623. break;
  624. xfs_convert_page(inode, page, iomapp, wbc, NULL,
  625. startio, all_bh);
  626. }
  627. }
  628. /*
  629. * Calling this without startio set means we are being asked to make a dirty
  630. * page ready for freeing it's buffers. When called with startio set then
  631. * we are coming from writepage.
  632. *
  633. * When called with startio set it is important that we write the WHOLE
  634. * page if possible.
  635. * The bh->b_state's cannot know if any of the blocks or which block for
  636. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  637. * only vaild if the page itself isn't completely uptodate. Some layers
  638. * may clear the page dirty flag prior to calling write page, under the
  639. * assumption the entire page will be written out; by not writing out the
  640. * whole page the page can be reused before all valid dirty data is
  641. * written out. Note: in the case of a page that has been dirty'd by
  642. * mapwrite and but partially setup by block_prepare_write the
  643. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  644. * valid state, thus the whole page must be written out thing.
  645. */
  646. STATIC int
  647. xfs_page_state_convert(
  648. struct inode *inode,
  649. struct page *page,
  650. struct writeback_control *wbc,
  651. int startio,
  652. int unmapped) /* also implies page uptodate */
  653. {
  654. struct buffer_head *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
  655. xfs_iomap_t *iomp, iomap;
  656. loff_t offset;
  657. unsigned long p_offset = 0;
  658. __uint64_t end_offset;
  659. pgoff_t end_index, last_index, tlast;
  660. int len, err, i, cnt = 0, uptodate = 1;
  661. int flags;
  662. int page_dirty;
  663. /* wait for other IO threads? */
  664. flags = (startio && wbc->sync_mode != WB_SYNC_NONE) ? 0 : BMAPI_TRYLOCK;
  665. /* Is this page beyond the end of the file? */
  666. offset = i_size_read(inode);
  667. end_index = offset >> PAGE_CACHE_SHIFT;
  668. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  669. if (page->index >= end_index) {
  670. if ((page->index >= end_index + 1) ||
  671. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  672. err = -EIO;
  673. goto error;
  674. }
  675. }
  676. end_offset = min_t(unsigned long long,
  677. (loff_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  678. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  679. /*
  680. * page_dirty is initially a count of buffers on the page before
  681. * EOF and is decrememted as we move each into a cleanable state.
  682. */
  683. len = 1 << inode->i_blkbits;
  684. p_offset = max(p_offset, PAGE_CACHE_SIZE);
  685. p_offset = roundup(p_offset, len);
  686. page_dirty = p_offset / len;
  687. iomp = NULL;
  688. p_offset = 0;
  689. bh = head = page_buffers(page);
  690. do {
  691. if (offset >= end_offset)
  692. break;
  693. if (!buffer_uptodate(bh))
  694. uptodate = 0;
  695. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
  696. continue;
  697. if (iomp) {
  698. iomp = xfs_offset_to_map(page, &iomap, p_offset);
  699. }
  700. /*
  701. * First case, map an unwritten extent and prepare for
  702. * extent state conversion transaction on completion.
  703. */
  704. if (buffer_unwritten(bh)) {
  705. if (!startio)
  706. continue;
  707. if (!iomp) {
  708. err = xfs_map_blocks(inode, offset, len, &iomap,
  709. BMAPI_READ|BMAPI_IGNSTATE);
  710. if (err) {
  711. goto error;
  712. }
  713. iomp = xfs_offset_to_map(page, &iomap,
  714. p_offset);
  715. }
  716. if (iomp) {
  717. if (!bh->b_end_io) {
  718. err = xfs_map_unwritten(inode, page,
  719. head, bh, p_offset,
  720. inode->i_blkbits, iomp,
  721. wbc, startio, unmapped);
  722. if (err) {
  723. goto error;
  724. }
  725. } else {
  726. set_bit(BH_Lock, &bh->b_state);
  727. }
  728. BUG_ON(!buffer_locked(bh));
  729. bh_arr[cnt++] = bh;
  730. page_dirty--;
  731. }
  732. /*
  733. * Second case, allocate space for a delalloc buffer.
  734. * We can return EAGAIN here in the release page case.
  735. */
  736. } else if (buffer_delay(bh)) {
  737. if (!iomp) {
  738. err = xfs_map_blocks(inode, offset, len, &iomap,
  739. BMAPI_ALLOCATE | flags);
  740. if (err) {
  741. goto error;
  742. }
  743. iomp = xfs_offset_to_map(page, &iomap,
  744. p_offset);
  745. }
  746. if (iomp) {
  747. xfs_map_at_offset(page, bh, p_offset,
  748. inode->i_blkbits, iomp);
  749. if (startio) {
  750. bh_arr[cnt++] = bh;
  751. } else {
  752. set_buffer_dirty(bh);
  753. unlock_buffer(bh);
  754. mark_buffer_dirty(bh);
  755. }
  756. page_dirty--;
  757. }
  758. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  759. (unmapped || startio)) {
  760. if (!buffer_mapped(bh)) {
  761. int size;
  762. /*
  763. * Getting here implies an unmapped buffer
  764. * was found, and we are in a path where we
  765. * need to write the whole page out.
  766. */
  767. if (!iomp) {
  768. size = xfs_probe_unmapped_cluster(
  769. inode, page, bh, head);
  770. err = xfs_map_blocks(inode, offset,
  771. size, &iomap,
  772. BMAPI_WRITE|BMAPI_MMAP);
  773. if (err) {
  774. goto error;
  775. }
  776. iomp = xfs_offset_to_map(page, &iomap,
  777. p_offset);
  778. }
  779. if (iomp) {
  780. xfs_map_at_offset(page,
  781. bh, p_offset,
  782. inode->i_blkbits, iomp);
  783. if (startio) {
  784. bh_arr[cnt++] = bh;
  785. } else {
  786. set_buffer_dirty(bh);
  787. unlock_buffer(bh);
  788. mark_buffer_dirty(bh);
  789. }
  790. page_dirty--;
  791. }
  792. } else if (startio) {
  793. if (buffer_uptodate(bh) &&
  794. !test_and_set_bit(BH_Lock, &bh->b_state)) {
  795. bh_arr[cnt++] = bh;
  796. page_dirty--;
  797. }
  798. }
  799. }
  800. } while (offset += len, p_offset += len,
  801. ((bh = bh->b_this_page) != head));
  802. if (uptodate && bh == head)
  803. SetPageUptodate(page);
  804. if (startio) {
  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. };