xfs_aops.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir.h"
  25. #include "xfs_dir2.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dir_sf.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_error.h"
  40. #include "xfs_rw.h"
  41. #include "xfs_iomap.h"
  42. #include <linux/mpage.h>
  43. #include <linux/pagevec.h>
  44. #include <linux/writeback.h>
  45. STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
  46. #if defined(XFS_RW_TRACE)
  47. void
  48. xfs_page_trace(
  49. int tag,
  50. struct inode *inode,
  51. struct page *page,
  52. int mask)
  53. {
  54. xfs_inode_t *ip;
  55. bhv_desc_t *bdp;
  56. vnode_t *vp = LINVFS_GET_VP(inode);
  57. loff_t isize = i_size_read(inode);
  58. loff_t offset = page_offset(page);
  59. int delalloc = -1, unmapped = -1, unwritten = -1;
  60. if (page_has_buffers(page))
  61. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  62. bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
  63. ip = XFS_BHVTOI(bdp);
  64. if (!ip->i_rwtrace)
  65. return;
  66. ktrace_enter(ip->i_rwtrace,
  67. (void *)((unsigned long)tag),
  68. (void *)ip,
  69. (void *)inode,
  70. (void *)page,
  71. (void *)((unsigned long)mask),
  72. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  73. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  74. (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
  75. (void *)((unsigned long)(isize & 0xffffffff)),
  76. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  77. (void *)((unsigned long)(offset & 0xffffffff)),
  78. (void *)((unsigned long)delalloc),
  79. (void *)((unsigned long)unmapped),
  80. (void *)((unsigned long)unwritten),
  81. (void *)NULL,
  82. (void *)NULL);
  83. }
  84. #else
  85. #define xfs_page_trace(tag, inode, page, mask)
  86. #endif
  87. /*
  88. * Schedule IO completion handling on a xfsdatad if this was
  89. * the final hold on this ioend.
  90. */
  91. STATIC void
  92. xfs_finish_ioend(
  93. xfs_ioend_t *ioend)
  94. {
  95. if (atomic_dec_and_test(&ioend->io_remaining))
  96. queue_work(xfsdatad_workqueue, &ioend->io_work);
  97. }
  98. /*
  99. * We're now finished for good with this ioend structure.
  100. * Update the page state via the associated buffer_heads,
  101. * release holds on the inode and bio, and finally free
  102. * up memory. Do not use the ioend after this.
  103. */
  104. STATIC void
  105. xfs_destroy_ioend(
  106. xfs_ioend_t *ioend)
  107. {
  108. struct buffer_head *bh, *next;
  109. for (bh = ioend->io_buffer_head; bh; bh = next) {
  110. next = bh->b_private;
  111. bh->b_end_io(bh, ioend->io_uptodate);
  112. }
  113. vn_iowake(ioend->io_vnode);
  114. mempool_free(ioend, xfs_ioend_pool);
  115. }
  116. /*
  117. * Buffered IO write completion for delayed allocate extents.
  118. * TODO: Update ondisk isize now that we know the file data
  119. * has been flushed (i.e. the notorious "NULL file" problem).
  120. */
  121. STATIC void
  122. xfs_end_bio_delalloc(
  123. void *data)
  124. {
  125. xfs_ioend_t *ioend = data;
  126. xfs_destroy_ioend(ioend);
  127. }
  128. /*
  129. * Buffered IO write completion for regular, written extents.
  130. */
  131. STATIC void
  132. xfs_end_bio_written(
  133. void *data)
  134. {
  135. xfs_ioend_t *ioend = data;
  136. xfs_destroy_ioend(ioend);
  137. }
  138. /*
  139. * IO write completion for unwritten extents.
  140. *
  141. * Issue transactions to convert a buffer range from unwritten
  142. * to written extents.
  143. */
  144. STATIC void
  145. xfs_end_bio_unwritten(
  146. void *data)
  147. {
  148. xfs_ioend_t *ioend = data;
  149. vnode_t *vp = ioend->io_vnode;
  150. xfs_off_t offset = ioend->io_offset;
  151. size_t size = ioend->io_size;
  152. int error;
  153. if (ioend->io_uptodate)
  154. VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
  155. xfs_destroy_ioend(ioend);
  156. }
  157. /*
  158. * Allocate and initialise an IO completion structure.
  159. * We need to track unwritten extent write completion here initially.
  160. * We'll need to extend this for updating the ondisk inode size later
  161. * (vs. incore size).
  162. */
  163. STATIC xfs_ioend_t *
  164. xfs_alloc_ioend(
  165. struct inode *inode,
  166. unsigned int type)
  167. {
  168. xfs_ioend_t *ioend;
  169. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  170. /*
  171. * Set the count to 1 initially, which will prevent an I/O
  172. * completion callback from happening before we have started
  173. * all the I/O from calling the completion routine too early.
  174. */
  175. atomic_set(&ioend->io_remaining, 1);
  176. ioend->io_uptodate = 1; /* cleared if any I/O fails */
  177. ioend->io_list = NULL;
  178. ioend->io_type = type;
  179. ioend->io_vnode = LINVFS_GET_VP(inode);
  180. ioend->io_buffer_head = NULL;
  181. ioend->io_buffer_tail = NULL;
  182. atomic_inc(&ioend->io_vnode->v_iocount);
  183. ioend->io_offset = 0;
  184. ioend->io_size = 0;
  185. if (type == IOMAP_UNWRITTEN)
  186. INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
  187. else if (type == IOMAP_DELAY)
  188. INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
  189. else
  190. INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
  191. return ioend;
  192. }
  193. STATIC int
  194. xfs_map_blocks(
  195. struct inode *inode,
  196. loff_t offset,
  197. ssize_t count,
  198. xfs_iomap_t *mapp,
  199. int flags)
  200. {
  201. vnode_t *vp = LINVFS_GET_VP(inode);
  202. int error, nmaps = 1;
  203. VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
  204. if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
  205. VMODIFY(vp);
  206. return -error;
  207. }
  208. STATIC inline int
  209. xfs_iomap_valid(
  210. xfs_iomap_t *iomapp,
  211. loff_t offset)
  212. {
  213. return offset >= iomapp->iomap_offset &&
  214. offset < iomapp->iomap_offset + iomapp->iomap_bsize;
  215. }
  216. /*
  217. * BIO completion handler for buffered IO.
  218. */
  219. STATIC int
  220. xfs_end_bio(
  221. struct bio *bio,
  222. unsigned int bytes_done,
  223. int error)
  224. {
  225. xfs_ioend_t *ioend = bio->bi_private;
  226. if (bio->bi_size)
  227. return 1;
  228. ASSERT(ioend);
  229. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  230. /* Toss bio and pass work off to an xfsdatad thread */
  231. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  232. ioend->io_uptodate = 0;
  233. bio->bi_private = NULL;
  234. bio->bi_end_io = NULL;
  235. bio_put(bio);
  236. xfs_finish_ioend(ioend);
  237. return 0;
  238. }
  239. STATIC void
  240. xfs_submit_ioend_bio(
  241. xfs_ioend_t *ioend,
  242. struct bio *bio)
  243. {
  244. atomic_inc(&ioend->io_remaining);
  245. bio->bi_private = ioend;
  246. bio->bi_end_io = xfs_end_bio;
  247. submit_bio(WRITE, bio);
  248. ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
  249. bio_put(bio);
  250. }
  251. STATIC struct bio *
  252. xfs_alloc_ioend_bio(
  253. struct buffer_head *bh)
  254. {
  255. struct bio *bio;
  256. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  257. do {
  258. bio = bio_alloc(GFP_NOIO, nvecs);
  259. nvecs >>= 1;
  260. } while (!bio);
  261. ASSERT(bio->bi_private == NULL);
  262. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  263. bio->bi_bdev = bh->b_bdev;
  264. bio_get(bio);
  265. return bio;
  266. }
  267. STATIC void
  268. xfs_start_buffer_writeback(
  269. struct buffer_head *bh)
  270. {
  271. ASSERT(buffer_mapped(bh));
  272. ASSERT(buffer_locked(bh));
  273. ASSERT(!buffer_delay(bh));
  274. ASSERT(!buffer_unwritten(bh));
  275. mark_buffer_async_write(bh);
  276. set_buffer_uptodate(bh);
  277. clear_buffer_dirty(bh);
  278. }
  279. STATIC void
  280. xfs_start_page_writeback(
  281. struct page *page,
  282. struct writeback_control *wbc,
  283. int clear_dirty,
  284. int buffers)
  285. {
  286. ASSERT(PageLocked(page));
  287. ASSERT(!PageWriteback(page));
  288. set_page_writeback(page);
  289. if (clear_dirty)
  290. clear_page_dirty(page);
  291. unlock_page(page);
  292. if (!buffers) {
  293. end_page_writeback(page);
  294. wbc->pages_skipped++; /* We didn't write this page */
  295. }
  296. }
  297. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  298. {
  299. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  300. }
  301. /*
  302. * Submit all of the bios for all of the ioends we have saved up,
  303. * covering the initial writepage page and also any probed pages.
  304. */
  305. STATIC void
  306. xfs_submit_ioend(
  307. xfs_ioend_t *ioend)
  308. {
  309. xfs_ioend_t *next;
  310. struct buffer_head *bh;
  311. struct bio *bio;
  312. sector_t lastblock = 0;
  313. do {
  314. next = ioend->io_list;
  315. bio = NULL;
  316. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  317. xfs_start_buffer_writeback(bh);
  318. if (!bio) {
  319. retry:
  320. bio = xfs_alloc_ioend_bio(bh);
  321. } else if (bh->b_blocknr != lastblock + 1) {
  322. xfs_submit_ioend_bio(ioend, bio);
  323. goto retry;
  324. }
  325. if (bio_add_buffer(bio, bh) != bh->b_size) {
  326. xfs_submit_ioend_bio(ioend, bio);
  327. goto retry;
  328. }
  329. lastblock = bh->b_blocknr;
  330. }
  331. if (bio)
  332. xfs_submit_ioend_bio(ioend, bio);
  333. xfs_finish_ioend(ioend);
  334. } while ((ioend = next) != NULL);
  335. }
  336. /*
  337. * Cancel submission of all buffer_heads so far in this endio.
  338. * Toss the endio too. Only ever called for the initial page
  339. * in a writepage request, so only ever one page.
  340. */
  341. STATIC void
  342. xfs_cancel_ioend(
  343. xfs_ioend_t *ioend)
  344. {
  345. xfs_ioend_t *next;
  346. struct buffer_head *bh, *next_bh;
  347. do {
  348. next = ioend->io_list;
  349. bh = ioend->io_buffer_head;
  350. do {
  351. next_bh = bh->b_private;
  352. clear_buffer_async_write(bh);
  353. unlock_buffer(bh);
  354. } while ((bh = next_bh) != NULL);
  355. vn_iowake(ioend->io_vnode);
  356. mempool_free(ioend, xfs_ioend_pool);
  357. } while ((ioend = next) != NULL);
  358. }
  359. /*
  360. * Test to see if we've been building up a completion structure for
  361. * earlier buffers -- if so, we try to append to this ioend if we
  362. * can, otherwise we finish off any current ioend and start another.
  363. * Return true if we've finished the given ioend.
  364. */
  365. STATIC void
  366. xfs_add_to_ioend(
  367. struct inode *inode,
  368. struct buffer_head *bh,
  369. unsigned int p_offset,
  370. unsigned int type,
  371. xfs_ioend_t **result,
  372. int need_ioend)
  373. {
  374. xfs_ioend_t *ioend = *result;
  375. if (!ioend || need_ioend || type != ioend->io_type) {
  376. xfs_ioend_t *previous = *result;
  377. xfs_off_t offset;
  378. offset = (xfs_off_t)bh->b_page->index << PAGE_CACHE_SHIFT;
  379. offset += p_offset;
  380. ioend = xfs_alloc_ioend(inode, type);
  381. ioend->io_offset = offset;
  382. ioend->io_buffer_head = bh;
  383. ioend->io_buffer_tail = bh;
  384. if (previous)
  385. previous->io_list = ioend;
  386. *result = ioend;
  387. } else {
  388. ioend->io_buffer_tail->b_private = bh;
  389. ioend->io_buffer_tail = bh;
  390. }
  391. bh->b_private = NULL;
  392. ioend->io_size += bh->b_size;
  393. }
  394. STATIC void
  395. xfs_map_at_offset(
  396. struct buffer_head *bh,
  397. loff_t offset,
  398. int block_bits,
  399. xfs_iomap_t *iomapp)
  400. {
  401. xfs_daddr_t bn;
  402. int sector_shift;
  403. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  404. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  405. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  406. sector_shift = block_bits - BBSHIFT;
  407. bn = (iomapp->iomap_bn >> sector_shift) +
  408. ((offset - iomapp->iomap_offset) >> block_bits);
  409. ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
  410. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  411. lock_buffer(bh);
  412. bh->b_blocknr = bn;
  413. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  414. set_buffer_mapped(bh);
  415. clear_buffer_delay(bh);
  416. clear_buffer_unwritten(bh);
  417. }
  418. /*
  419. * Look for a page at index which is unlocked and not mapped
  420. * yet - clustering for mmap write case.
  421. */
  422. STATIC unsigned int
  423. xfs_probe_unmapped_page(
  424. struct page *page,
  425. unsigned int pg_offset)
  426. {
  427. int ret = 0;
  428. if (PageWriteback(page))
  429. return 0;
  430. if (page->mapping && PageDirty(page)) {
  431. if (page_has_buffers(page)) {
  432. struct buffer_head *bh, *head;
  433. bh = head = page_buffers(page);
  434. do {
  435. if (buffer_mapped(bh) || !buffer_uptodate(bh))
  436. break;
  437. ret += bh->b_size;
  438. if (ret >= pg_offset)
  439. break;
  440. } while ((bh = bh->b_this_page) != head);
  441. } else
  442. ret = PAGE_CACHE_SIZE;
  443. }
  444. return ret;
  445. }
  446. STATIC size_t
  447. xfs_probe_unmapped_cluster(
  448. struct inode *inode,
  449. struct page *startpage,
  450. struct buffer_head *bh,
  451. struct buffer_head *head)
  452. {
  453. struct pagevec pvec;
  454. pgoff_t tindex, tlast, tloff;
  455. size_t total = 0;
  456. int done = 0, i;
  457. /* First sum forwards in this page */
  458. do {
  459. if (buffer_mapped(bh))
  460. return total;
  461. total += bh->b_size;
  462. } while ((bh = bh->b_this_page) != head);
  463. /* if we reached the end of the page, sum forwards in following pages */
  464. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  465. tindex = startpage->index + 1;
  466. /* Prune this back to avoid pathological behavior */
  467. tloff = min(tlast, startpage->index + 64);
  468. pagevec_init(&pvec, 0);
  469. while (!done && tindex <= tloff) {
  470. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  471. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  472. break;
  473. for (i = 0; i < pagevec_count(&pvec); i++) {
  474. struct page *page = pvec.pages[i];
  475. size_t pg_offset, len = 0;
  476. if (tindex == tlast) {
  477. pg_offset =
  478. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  479. if (!pg_offset) {
  480. done = 1;
  481. break;
  482. }
  483. } else
  484. pg_offset = PAGE_CACHE_SIZE;
  485. if (page->index == tindex && !TestSetPageLocked(page)) {
  486. len = xfs_probe_unmapped_page(page, pg_offset);
  487. unlock_page(page);
  488. }
  489. if (!len) {
  490. done = 1;
  491. break;
  492. }
  493. total += len;
  494. tindex++;
  495. }
  496. pagevec_release(&pvec);
  497. cond_resched();
  498. }
  499. return total;
  500. }
  501. /*
  502. * Test if a given page is suitable for writing as part of an unwritten
  503. * or delayed allocate extent.
  504. */
  505. STATIC int
  506. xfs_is_delayed_page(
  507. struct page *page,
  508. unsigned int type)
  509. {
  510. if (PageWriteback(page))
  511. return 0;
  512. if (page->mapping && page_has_buffers(page)) {
  513. struct buffer_head *bh, *head;
  514. int acceptable = 0;
  515. bh = head = page_buffers(page);
  516. do {
  517. if (buffer_unwritten(bh))
  518. acceptable = (type == IOMAP_UNWRITTEN);
  519. else if (buffer_delay(bh))
  520. acceptable = (type == IOMAP_DELAY);
  521. else
  522. break;
  523. } while ((bh = bh->b_this_page) != head);
  524. if (acceptable)
  525. return 1;
  526. }
  527. return 0;
  528. }
  529. /*
  530. * Allocate & map buffers for page given the extent map. Write it out.
  531. * except for the original page of a writepage, this is called on
  532. * delalloc/unwritten pages only, for the original page it is possible
  533. * that the page has no mapping at all.
  534. */
  535. STATIC int
  536. xfs_convert_page(
  537. struct inode *inode,
  538. struct page *page,
  539. loff_t tindex,
  540. xfs_iomap_t *mp,
  541. xfs_ioend_t **ioendp,
  542. struct writeback_control *wbc,
  543. int startio,
  544. int all_bh)
  545. {
  546. struct buffer_head *bh, *head;
  547. xfs_off_t end_offset;
  548. unsigned long p_offset;
  549. unsigned int type;
  550. int bbits = inode->i_blkbits;
  551. int len, page_dirty;
  552. int count = 0, done = 0, uptodate = 1;
  553. xfs_off_t offset = page_offset(page);
  554. if (page->index != tindex)
  555. goto fail;
  556. if (TestSetPageLocked(page))
  557. goto fail;
  558. if (PageWriteback(page))
  559. goto fail_unlock_page;
  560. if (page->mapping != inode->i_mapping)
  561. goto fail_unlock_page;
  562. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  563. goto fail_unlock_page;
  564. /*
  565. * page_dirty is initially a count of buffers on the page before
  566. * EOF and is decrememted as we move each into a cleanable state.
  567. *
  568. * Derivation:
  569. *
  570. * End offset is the highest offset that this page should represent.
  571. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  572. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  573. * hence give us the correct page_dirty count. On any other page,
  574. * it will be zero and in that case we need page_dirty to be the
  575. * count of buffers on the page.
  576. */
  577. end_offset = min_t(unsigned long long,
  578. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  579. i_size_read(inode));
  580. len = 1 << inode->i_blkbits;
  581. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  582. PAGE_CACHE_SIZE);
  583. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  584. page_dirty = p_offset / len;
  585. p_offset = 0;
  586. bh = head = page_buffers(page);
  587. do {
  588. if (offset >= end_offset)
  589. break;
  590. if (!buffer_uptodate(bh))
  591. uptodate = 0;
  592. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  593. done = 1;
  594. continue;
  595. }
  596. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  597. if (buffer_unwritten(bh))
  598. type = IOMAP_UNWRITTEN;
  599. else
  600. type = IOMAP_DELAY;
  601. if (!xfs_iomap_valid(mp, offset)) {
  602. done = 1;
  603. continue;
  604. }
  605. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  606. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  607. xfs_map_at_offset(bh, offset, bbits, mp);
  608. if (startio) {
  609. xfs_add_to_ioend(inode, bh, p_offset,
  610. type, ioendp, done);
  611. } else {
  612. set_buffer_dirty(bh);
  613. unlock_buffer(bh);
  614. mark_buffer_dirty(bh);
  615. }
  616. page_dirty--;
  617. count++;
  618. } else {
  619. type = 0;
  620. if (buffer_mapped(bh) && all_bh && startio) {
  621. lock_buffer(bh);
  622. xfs_add_to_ioend(inode, bh, p_offset,
  623. type, ioendp, done);
  624. count++;
  625. page_dirty--;
  626. } else {
  627. done = 1;
  628. }
  629. }
  630. } while (offset += len, p_offset += len,
  631. (bh = bh->b_this_page) != head);
  632. if (uptodate && bh == head)
  633. SetPageUptodate(page);
  634. if (startio) {
  635. if (count)
  636. wbc->nr_to_write--;
  637. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  638. }
  639. return done;
  640. fail_unlock_page:
  641. unlock_page(page);
  642. fail:
  643. return 1;
  644. }
  645. /*
  646. * Convert & write out a cluster of pages in the same extent as defined
  647. * by mp and following the start page.
  648. */
  649. STATIC void
  650. xfs_cluster_write(
  651. struct inode *inode,
  652. pgoff_t tindex,
  653. xfs_iomap_t *iomapp,
  654. xfs_ioend_t **ioendp,
  655. struct writeback_control *wbc,
  656. int startio,
  657. int all_bh,
  658. pgoff_t tlast)
  659. {
  660. struct pagevec pvec;
  661. int done = 0, i;
  662. pagevec_init(&pvec, 0);
  663. while (!done && tindex <= tlast) {
  664. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  665. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  666. break;
  667. for (i = 0; i < pagevec_count(&pvec); i++) {
  668. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  669. iomapp, ioendp, wbc, startio, all_bh);
  670. if (done)
  671. break;
  672. }
  673. pagevec_release(&pvec);
  674. cond_resched();
  675. }
  676. }
  677. /*
  678. * Calling this without startio set means we are being asked to make a dirty
  679. * page ready for freeing it's buffers. When called with startio set then
  680. * we are coming from writepage.
  681. *
  682. * When called with startio set it is important that we write the WHOLE
  683. * page if possible.
  684. * The bh->b_state's cannot know if any of the blocks or which block for
  685. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  686. * only vaild if the page itself isn't completely uptodate. Some layers
  687. * may clear the page dirty flag prior to calling write page, under the
  688. * assumption the entire page will be written out; by not writing out the
  689. * whole page the page can be reused before all valid dirty data is
  690. * written out. Note: in the case of a page that has been dirty'd by
  691. * mapwrite and but partially setup by block_prepare_write the
  692. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  693. * valid state, thus the whole page must be written out thing.
  694. */
  695. STATIC int
  696. xfs_page_state_convert(
  697. struct inode *inode,
  698. struct page *page,
  699. struct writeback_control *wbc,
  700. int startio,
  701. int unmapped) /* also implies page uptodate */
  702. {
  703. struct buffer_head *bh, *head;
  704. xfs_iomap_t iomap;
  705. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  706. loff_t offset;
  707. unsigned long p_offset = 0;
  708. unsigned int type;
  709. __uint64_t end_offset;
  710. pgoff_t end_index, last_index, tlast;
  711. int flags, len, err, iomap_valid = 0, uptodate = 1;
  712. int page_dirty, count = 0, trylock_flag = 0;
  713. /* wait for other IO threads? */
  714. if (startio && wbc->sync_mode != WB_SYNC_NONE)
  715. trylock_flag |= BMAPI_TRYLOCK;
  716. /* Is this page beyond the end of the file? */
  717. offset = i_size_read(inode);
  718. end_index = offset >> PAGE_CACHE_SHIFT;
  719. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  720. if (page->index >= end_index) {
  721. if ((page->index >= end_index + 1) ||
  722. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  723. if (startio)
  724. unlock_page(page);
  725. return 0;
  726. }
  727. }
  728. /*
  729. * page_dirty is initially a count of buffers on the page before
  730. * EOF and is decrememted as we move each into a cleanable state.
  731. *
  732. * Derivation:
  733. *
  734. * End offset is the highest offset that this page should represent.
  735. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  736. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  737. * hence give us the correct page_dirty count. On any other page,
  738. * it will be zero and in that case we need page_dirty to be the
  739. * count of buffers on the page.
  740. */
  741. end_offset = min_t(unsigned long long,
  742. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  743. len = 1 << inode->i_blkbits;
  744. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  745. PAGE_CACHE_SIZE);
  746. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  747. page_dirty = p_offset / len;
  748. bh = head = page_buffers(page);
  749. offset = page_offset(page);
  750. /* TODO: cleanup count and page_dirty */
  751. do {
  752. if (offset >= end_offset)
  753. break;
  754. if (!buffer_uptodate(bh))
  755. uptodate = 0;
  756. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  757. /*
  758. * the iomap is actually still valid, but the ioend
  759. * isn't. shouldn't happen too often.
  760. */
  761. iomap_valid = 0;
  762. continue;
  763. }
  764. if (iomap_valid)
  765. iomap_valid = xfs_iomap_valid(&iomap, offset);
  766. /*
  767. * First case, map an unwritten extent and prepare for
  768. * extent state conversion transaction on completion.
  769. *
  770. * Second case, allocate space for a delalloc buffer.
  771. * We can return EAGAIN here in the release page case.
  772. */
  773. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  774. if (buffer_unwritten(bh)) {
  775. type = IOMAP_UNWRITTEN;
  776. flags = BMAPI_WRITE|BMAPI_IGNSTATE;
  777. } else {
  778. type = IOMAP_DELAY;
  779. flags = BMAPI_ALLOCATE;
  780. if (!startio)
  781. flags |= trylock_flag;
  782. }
  783. if (!iomap_valid) {
  784. err = xfs_map_blocks(inode, offset, len, &iomap,
  785. flags);
  786. if (err)
  787. goto error;
  788. iomap_valid = xfs_iomap_valid(&iomap, offset);
  789. }
  790. if (iomap_valid) {
  791. xfs_map_at_offset(bh, offset,
  792. inode->i_blkbits, &iomap);
  793. if (startio) {
  794. xfs_add_to_ioend(inode, bh, p_offset,
  795. type, &ioend,
  796. !iomap_valid);
  797. } else {
  798. set_buffer_dirty(bh);
  799. unlock_buffer(bh);
  800. mark_buffer_dirty(bh);
  801. }
  802. page_dirty--;
  803. count++;
  804. }
  805. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  806. (unmapped || startio)) {
  807. type = 0;
  808. if (!buffer_mapped(bh)) {
  809. /*
  810. * Getting here implies an unmapped buffer
  811. * was found, and we are in a path where we
  812. * need to write the whole page out.
  813. */
  814. if (!iomap_valid) {
  815. int size;
  816. size = xfs_probe_unmapped_cluster(
  817. inode, page, bh, head);
  818. err = xfs_map_blocks(inode, offset,
  819. size, &iomap,
  820. BMAPI_WRITE|BMAPI_MMAP);
  821. if (err)
  822. goto error;
  823. iomap_valid = xfs_iomap_valid(&iomap,
  824. offset);
  825. }
  826. if (iomap_valid) {
  827. xfs_map_at_offset(bh, offset,
  828. inode->i_blkbits,
  829. &iomap);
  830. if (startio) {
  831. xfs_add_to_ioend(inode,
  832. bh, p_offset, type,
  833. &ioend, !iomap_valid);
  834. } else {
  835. set_buffer_dirty(bh);
  836. unlock_buffer(bh);
  837. mark_buffer_dirty(bh);
  838. }
  839. page_dirty--;
  840. count++;
  841. }
  842. } else if (startio) {
  843. if (buffer_uptodate(bh) &&
  844. !test_and_set_bit(BH_Lock, &bh->b_state)) {
  845. ASSERT(buffer_mapped(bh));
  846. xfs_add_to_ioend(inode,
  847. bh, p_offset, type,
  848. &ioend, !iomap_valid);
  849. page_dirty--;
  850. count++;
  851. } else {
  852. iomap_valid = 0;
  853. }
  854. } else {
  855. iomap_valid = 0;
  856. }
  857. }
  858. if (!iohead)
  859. iohead = ioend;
  860. } while (offset += len, ((bh = bh->b_this_page) != head));
  861. if (uptodate && bh == head)
  862. SetPageUptodate(page);
  863. if (startio)
  864. xfs_start_page_writeback(page, wbc, 1, count);
  865. if (ioend && iomap_valid) {
  866. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  867. PAGE_CACHE_SHIFT;
  868. tlast = min_t(pgoff_t, offset, last_index);
  869. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  870. wbc, startio, unmapped, tlast);
  871. }
  872. if (iohead)
  873. xfs_submit_ioend(iohead);
  874. return page_dirty;
  875. error:
  876. if (iohead)
  877. xfs_cancel_ioend(iohead);
  878. /*
  879. * If it's delalloc and we have nowhere to put it,
  880. * throw it away, unless the lower layers told
  881. * us to try again.
  882. */
  883. if (err != -EAGAIN) {
  884. if (!unmapped)
  885. block_invalidatepage(page, 0);
  886. ClearPageUptodate(page);
  887. }
  888. return err;
  889. }
  890. STATIC int
  891. __linvfs_get_block(
  892. struct inode *inode,
  893. sector_t iblock,
  894. unsigned long blocks,
  895. struct buffer_head *bh_result,
  896. int create,
  897. int direct,
  898. bmapi_flags_t flags)
  899. {
  900. vnode_t *vp = LINVFS_GET_VP(inode);
  901. xfs_iomap_t iomap;
  902. xfs_off_t offset;
  903. ssize_t size;
  904. int retpbbm = 1;
  905. int error;
  906. offset = (xfs_off_t)iblock << inode->i_blkbits;
  907. if (blocks)
  908. size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
  909. (xfs_off_t)blocks << inode->i_blkbits);
  910. else
  911. size = 1 << inode->i_blkbits;
  912. VOP_BMAP(vp, offset, size,
  913. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  914. if (error)
  915. return -error;
  916. if (retpbbm == 0)
  917. return 0;
  918. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  919. xfs_daddr_t bn;
  920. xfs_off_t delta;
  921. /* For unwritten extents do not report a disk address on
  922. * the read case (treat as if we're reading into a hole).
  923. */
  924. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  925. delta = offset - iomap.iomap_offset;
  926. delta >>= inode->i_blkbits;
  927. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  928. bn += delta;
  929. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  930. bh_result->b_blocknr = bn;
  931. set_buffer_mapped(bh_result);
  932. }
  933. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  934. if (direct)
  935. bh_result->b_private = inode;
  936. set_buffer_unwritten(bh_result);
  937. set_buffer_delay(bh_result);
  938. }
  939. }
  940. /* If this is a realtime file, data might be on a new device */
  941. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  942. /* If we previously allocated a block out beyond eof and
  943. * we are now coming back to use it then we will need to
  944. * flag it as new even if it has a disk address.
  945. */
  946. if (create &&
  947. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  948. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
  949. set_buffer_new(bh_result);
  950. if (iomap.iomap_flags & IOMAP_DELAY) {
  951. BUG_ON(direct);
  952. if (create) {
  953. set_buffer_uptodate(bh_result);
  954. set_buffer_mapped(bh_result);
  955. set_buffer_delay(bh_result);
  956. }
  957. }
  958. if (blocks) {
  959. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  960. offset = min_t(xfs_off_t,
  961. iomap.iomap_bsize - iomap.iomap_delta,
  962. (xfs_off_t)blocks << inode->i_blkbits);
  963. bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
  964. }
  965. return 0;
  966. }
  967. int
  968. linvfs_get_block(
  969. struct inode *inode,
  970. sector_t iblock,
  971. struct buffer_head *bh_result,
  972. int create)
  973. {
  974. return __linvfs_get_block(inode, iblock, 0, bh_result,
  975. create, 0, BMAPI_WRITE);
  976. }
  977. STATIC int
  978. linvfs_get_blocks_direct(
  979. struct inode *inode,
  980. sector_t iblock,
  981. unsigned long max_blocks,
  982. struct buffer_head *bh_result,
  983. int create)
  984. {
  985. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  986. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  987. }
  988. STATIC void
  989. linvfs_end_io_direct(
  990. struct kiocb *iocb,
  991. loff_t offset,
  992. ssize_t size,
  993. void *private)
  994. {
  995. xfs_ioend_t *ioend = iocb->private;
  996. /*
  997. * Non-NULL private data means we need to issue a transaction to
  998. * convert a range from unwritten to written extents. This needs
  999. * to happen from process contect but aio+dio I/O completion
  1000. * happens from irq context so we need to defer it to a workqueue.
  1001. * This is not nessecary for synchronous direct I/O, but we do
  1002. * it anyway to keep the code uniform and simpler.
  1003. *
  1004. * The core direct I/O code might be changed to always call the
  1005. * completion handler in the future, in which case all this can
  1006. * go away.
  1007. */
  1008. if (private && size > 0) {
  1009. ioend->io_offset = offset;
  1010. ioend->io_size = size;
  1011. xfs_finish_ioend(ioend);
  1012. } else {
  1013. ASSERT(size >= 0);
  1014. xfs_destroy_ioend(ioend);
  1015. }
  1016. /*
  1017. * blockdev_direct_IO can return an error even afer the I/O
  1018. * completion handler was called. Thus we need to protect
  1019. * against double-freeing.
  1020. */
  1021. iocb->private = NULL;
  1022. }
  1023. STATIC ssize_t
  1024. linvfs_direct_IO(
  1025. int rw,
  1026. struct kiocb *iocb,
  1027. const struct iovec *iov,
  1028. loff_t offset,
  1029. unsigned long nr_segs)
  1030. {
  1031. struct file *file = iocb->ki_filp;
  1032. struct inode *inode = file->f_mapping->host;
  1033. vnode_t *vp = LINVFS_GET_VP(inode);
  1034. xfs_iomap_t iomap;
  1035. int maps = 1;
  1036. int error;
  1037. ssize_t ret;
  1038. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1039. if (error)
  1040. return -error;
  1041. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1042. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1043. iomap.iomap_target->bt_bdev,
  1044. iov, offset, nr_segs,
  1045. linvfs_get_blocks_direct,
  1046. linvfs_end_io_direct);
  1047. if (unlikely(ret <= 0 && iocb->private))
  1048. xfs_destroy_ioend(iocb->private);
  1049. return ret;
  1050. }
  1051. STATIC sector_t
  1052. linvfs_bmap(
  1053. struct address_space *mapping,
  1054. sector_t block)
  1055. {
  1056. struct inode *inode = (struct inode *)mapping->host;
  1057. vnode_t *vp = LINVFS_GET_VP(inode);
  1058. int error;
  1059. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1060. VOP_RWLOCK(vp, VRWLOCK_READ);
  1061. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1062. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1063. return generic_block_bmap(mapping, block, linvfs_get_block);
  1064. }
  1065. STATIC int
  1066. linvfs_readpage(
  1067. struct file *unused,
  1068. struct page *page)
  1069. {
  1070. return mpage_readpage(page, linvfs_get_block);
  1071. }
  1072. STATIC int
  1073. linvfs_readpages(
  1074. struct file *unused,
  1075. struct address_space *mapping,
  1076. struct list_head *pages,
  1077. unsigned nr_pages)
  1078. {
  1079. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1080. }
  1081. STATIC void
  1082. xfs_count_page_state(
  1083. struct page *page,
  1084. int *delalloc,
  1085. int *unmapped,
  1086. int *unwritten)
  1087. {
  1088. struct buffer_head *bh, *head;
  1089. *delalloc = *unmapped = *unwritten = 0;
  1090. bh = head = page_buffers(page);
  1091. do {
  1092. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  1093. (*unmapped) = 1;
  1094. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  1095. clear_buffer_unwritten(bh);
  1096. else if (buffer_unwritten(bh))
  1097. (*unwritten) = 1;
  1098. else if (buffer_delay(bh))
  1099. (*delalloc) = 1;
  1100. } while ((bh = bh->b_this_page) != head);
  1101. }
  1102. /*
  1103. * writepage: Called from one of two places:
  1104. *
  1105. * 1. we are flushing a delalloc buffer head.
  1106. *
  1107. * 2. we are writing out a dirty page. Typically the page dirty
  1108. * state is cleared before we get here. In this case is it
  1109. * conceivable we have no buffer heads.
  1110. *
  1111. * For delalloc space on the page we need to allocate space and
  1112. * flush it. For unmapped buffer heads on the page we should
  1113. * allocate space if the page is uptodate. For any other dirty
  1114. * buffer heads on the page we should flush them.
  1115. *
  1116. * If we detect that a transaction would be required to flush
  1117. * the page, we have to check the process flags first, if we
  1118. * are already in a transaction or disk I/O during allocations
  1119. * is off, we need to fail the writepage and redirty the page.
  1120. */
  1121. STATIC int
  1122. linvfs_writepage(
  1123. struct page *page,
  1124. struct writeback_control *wbc)
  1125. {
  1126. int error;
  1127. int need_trans;
  1128. int delalloc, unmapped, unwritten;
  1129. struct inode *inode = page->mapping->host;
  1130. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1131. /*
  1132. * We need a transaction if:
  1133. * 1. There are delalloc buffers on the page
  1134. * 2. The page is uptodate and we have unmapped buffers
  1135. * 3. The page is uptodate and we have no buffers
  1136. * 4. There are unwritten buffers on the page
  1137. */
  1138. if (!page_has_buffers(page)) {
  1139. unmapped = 1;
  1140. need_trans = 1;
  1141. } else {
  1142. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1143. if (!PageUptodate(page))
  1144. unmapped = 0;
  1145. need_trans = delalloc + unmapped + unwritten;
  1146. }
  1147. /*
  1148. * If we need a transaction and the process flags say
  1149. * we are already in a transaction, or no IO is allowed
  1150. * then mark the page dirty again and leave the page
  1151. * as is.
  1152. */
  1153. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1154. goto out_fail;
  1155. /*
  1156. * Delay hooking up buffer heads until we have
  1157. * made our go/no-go decision.
  1158. */
  1159. if (!page_has_buffers(page))
  1160. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1161. /*
  1162. * Convert delayed allocate, unwritten or unmapped space
  1163. * to real space and flush out to disk.
  1164. */
  1165. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1166. if (error == -EAGAIN)
  1167. goto out_fail;
  1168. if (unlikely(error < 0))
  1169. goto out_unlock;
  1170. return 0;
  1171. out_fail:
  1172. redirty_page_for_writepage(wbc, page);
  1173. unlock_page(page);
  1174. return 0;
  1175. out_unlock:
  1176. unlock_page(page);
  1177. return error;
  1178. }
  1179. STATIC int
  1180. linvfs_invalidate_page(
  1181. struct page *page,
  1182. unsigned long offset)
  1183. {
  1184. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1185. page->mapping->host, page, offset);
  1186. return block_invalidatepage(page, offset);
  1187. }
  1188. /*
  1189. * Called to move a page into cleanable state - and from there
  1190. * to be released. Possibly the page is already clean. We always
  1191. * have buffer heads in this call.
  1192. *
  1193. * Returns 0 if the page is ok to release, 1 otherwise.
  1194. *
  1195. * Possible scenarios are:
  1196. *
  1197. * 1. We are being called to release a page which has been written
  1198. * to via regular I/O. buffer heads will be dirty and possibly
  1199. * delalloc. If no delalloc buffer heads in this case then we
  1200. * can just return zero.
  1201. *
  1202. * 2. We are called to release a page which has been written via
  1203. * mmap, all we need to do is ensure there is no delalloc
  1204. * state in the buffer heads, if not we can let the caller
  1205. * free them and we should come back later via writepage.
  1206. */
  1207. STATIC int
  1208. linvfs_release_page(
  1209. struct page *page,
  1210. gfp_t gfp_mask)
  1211. {
  1212. struct inode *inode = page->mapping->host;
  1213. int dirty, delalloc, unmapped, unwritten;
  1214. struct writeback_control wbc = {
  1215. .sync_mode = WB_SYNC_ALL,
  1216. .nr_to_write = 1,
  1217. };
  1218. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1219. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1220. if (!delalloc && !unwritten)
  1221. goto free_buffers;
  1222. if (!(gfp_mask & __GFP_FS))
  1223. return 0;
  1224. /* If we are already inside a transaction or the thread cannot
  1225. * do I/O, we cannot release this page.
  1226. */
  1227. if (PFLAGS_TEST_FSTRANS())
  1228. return 0;
  1229. /*
  1230. * Convert delalloc space to real space, do not flush the
  1231. * data out to disk, that will be done by the caller.
  1232. * Never need to allocate space here - we will always
  1233. * come back to writepage in that case.
  1234. */
  1235. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1236. if (dirty == 0 && !unwritten)
  1237. goto free_buffers;
  1238. return 0;
  1239. free_buffers:
  1240. return try_to_free_buffers(page);
  1241. }
  1242. STATIC int
  1243. linvfs_prepare_write(
  1244. struct file *file,
  1245. struct page *page,
  1246. unsigned int from,
  1247. unsigned int to)
  1248. {
  1249. return block_prepare_write(page, from, to, linvfs_get_block);
  1250. }
  1251. struct address_space_operations linvfs_aops = {
  1252. .readpage = linvfs_readpage,
  1253. .readpages = linvfs_readpages,
  1254. .writepage = linvfs_writepage,
  1255. .sync_page = block_sync_page,
  1256. .releasepage = linvfs_release_page,
  1257. .invalidatepage = linvfs_invalidate_page,
  1258. .prepare_write = linvfs_prepare_write,
  1259. .commit_write = generic_commit_write,
  1260. .bmap = linvfs_bmap,
  1261. .direct_IO = linvfs_direct_IO,
  1262. };