xfs_aops.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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. xfs_off_t 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. ioend = xfs_alloc_ioend(inode, type);
  378. ioend->io_offset = offset;
  379. ioend->io_buffer_head = bh;
  380. ioend->io_buffer_tail = bh;
  381. if (previous)
  382. previous->io_list = ioend;
  383. *result = ioend;
  384. } else {
  385. ioend->io_buffer_tail->b_private = bh;
  386. ioend->io_buffer_tail = bh;
  387. }
  388. bh->b_private = NULL;
  389. ioend->io_size += bh->b_size;
  390. }
  391. STATIC void
  392. xfs_map_at_offset(
  393. struct buffer_head *bh,
  394. loff_t offset,
  395. int block_bits,
  396. xfs_iomap_t *iomapp)
  397. {
  398. xfs_daddr_t bn;
  399. int sector_shift;
  400. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  401. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  402. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  403. sector_shift = block_bits - BBSHIFT;
  404. bn = (iomapp->iomap_bn >> sector_shift) +
  405. ((offset - iomapp->iomap_offset) >> block_bits);
  406. ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
  407. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  408. lock_buffer(bh);
  409. bh->b_blocknr = bn;
  410. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  411. set_buffer_mapped(bh);
  412. clear_buffer_delay(bh);
  413. clear_buffer_unwritten(bh);
  414. }
  415. /*
  416. * Look for a page at index that is suitable for clustering.
  417. */
  418. STATIC unsigned int
  419. xfs_probe_page(
  420. struct page *page,
  421. unsigned int pg_offset,
  422. int mapped)
  423. {
  424. int ret = 0;
  425. if (PageWriteback(page))
  426. return 0;
  427. if (page->mapping && PageDirty(page)) {
  428. if (page_has_buffers(page)) {
  429. struct buffer_head *bh, *head;
  430. bh = head = page_buffers(page);
  431. do {
  432. if (!buffer_uptodate(bh))
  433. break;
  434. if (mapped != buffer_mapped(bh))
  435. break;
  436. ret += bh->b_size;
  437. if (ret >= pg_offset)
  438. break;
  439. } while ((bh = bh->b_this_page) != head);
  440. } else
  441. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  442. }
  443. return ret;
  444. }
  445. STATIC size_t
  446. xfs_probe_cluster(
  447. struct inode *inode,
  448. struct page *startpage,
  449. struct buffer_head *bh,
  450. struct buffer_head *head,
  451. int mapped)
  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 (mapped != 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_page(page, pg_offset, mapped);
  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 if (buffer_mapped(bh))
  522. acceptable = (type == 0);
  523. else
  524. break;
  525. } while ((bh = bh->b_this_page) != head);
  526. if (acceptable)
  527. return 1;
  528. }
  529. return 0;
  530. }
  531. /*
  532. * Allocate & map buffers for page given the extent map. Write it out.
  533. * except for the original page of a writepage, this is called on
  534. * delalloc/unwritten pages only, for the original page it is possible
  535. * that the page has no mapping at all.
  536. */
  537. STATIC int
  538. xfs_convert_page(
  539. struct inode *inode,
  540. struct page *page,
  541. loff_t tindex,
  542. xfs_iomap_t *mp,
  543. xfs_ioend_t **ioendp,
  544. struct writeback_control *wbc,
  545. int startio,
  546. int all_bh)
  547. {
  548. struct buffer_head *bh, *head;
  549. xfs_off_t end_offset;
  550. unsigned long p_offset;
  551. unsigned int type;
  552. int bbits = inode->i_blkbits;
  553. int len, page_dirty;
  554. int count = 0, done = 0, uptodate = 1;
  555. xfs_off_t offset = page_offset(page);
  556. if (page->index != tindex)
  557. goto fail;
  558. if (TestSetPageLocked(page))
  559. goto fail;
  560. if (PageWriteback(page))
  561. goto fail_unlock_page;
  562. if (page->mapping != inode->i_mapping)
  563. goto fail_unlock_page;
  564. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  565. goto fail_unlock_page;
  566. /*
  567. * page_dirty is initially a count of buffers on the page before
  568. * EOF and is decrememted as we move each into a cleanable state.
  569. *
  570. * Derivation:
  571. *
  572. * End offset is the highest offset that this page should represent.
  573. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  574. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  575. * hence give us the correct page_dirty count. On any other page,
  576. * it will be zero and in that case we need page_dirty to be the
  577. * count of buffers on the page.
  578. */
  579. end_offset = min_t(unsigned long long,
  580. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  581. i_size_read(inode));
  582. len = 1 << inode->i_blkbits;
  583. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  584. PAGE_CACHE_SIZE);
  585. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  586. page_dirty = p_offset / len;
  587. bh = head = page_buffers(page);
  588. do {
  589. if (offset >= end_offset)
  590. break;
  591. if (!buffer_uptodate(bh))
  592. uptodate = 0;
  593. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  594. done = 1;
  595. continue;
  596. }
  597. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  598. if (buffer_unwritten(bh))
  599. type = IOMAP_UNWRITTEN;
  600. else
  601. type = IOMAP_DELAY;
  602. if (!xfs_iomap_valid(mp, offset)) {
  603. done = 1;
  604. continue;
  605. }
  606. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  607. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  608. xfs_map_at_offset(bh, offset, bbits, mp);
  609. if (startio) {
  610. xfs_add_to_ioend(inode, bh, offset,
  611. type, ioendp, done);
  612. } else {
  613. set_buffer_dirty(bh);
  614. unlock_buffer(bh);
  615. mark_buffer_dirty(bh);
  616. }
  617. page_dirty--;
  618. count++;
  619. } else {
  620. type = 0;
  621. if (buffer_mapped(bh) && all_bh && startio) {
  622. lock_buffer(bh);
  623. xfs_add_to_ioend(inode, bh, offset,
  624. type, ioendp, done);
  625. count++;
  626. page_dirty--;
  627. } else {
  628. done = 1;
  629. }
  630. }
  631. } while (offset += len, (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. ssize_t size, len;
  712. int flags, err, iomap_valid = 0, uptodate = 1;
  713. int page_dirty, count = 0, trylock_flag = 0;
  714. int all_bh = unmapped;
  715. /* wait for other IO threads? */
  716. if (startio && wbc->sync_mode != WB_SYNC_NONE)
  717. trylock_flag |= BMAPI_TRYLOCK;
  718. /* Is this page beyond the end of the file? */
  719. offset = i_size_read(inode);
  720. end_index = offset >> PAGE_CACHE_SHIFT;
  721. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  722. if (page->index >= end_index) {
  723. if ((page->index >= end_index + 1) ||
  724. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  725. if (startio)
  726. unlock_page(page);
  727. return 0;
  728. }
  729. }
  730. /*
  731. * page_dirty is initially a count of buffers on the page before
  732. * EOF and is decrememted as we move each into a cleanable state.
  733. *
  734. * Derivation:
  735. *
  736. * End offset is the highest offset that this page should represent.
  737. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  738. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  739. * hence give us the correct page_dirty count. On any other page,
  740. * it will be zero and in that case we need page_dirty to be the
  741. * count of buffers on the page.
  742. */
  743. end_offset = min_t(unsigned long long,
  744. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  745. len = 1 << inode->i_blkbits;
  746. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  747. PAGE_CACHE_SIZE);
  748. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  749. page_dirty = p_offset / len;
  750. bh = head = page_buffers(page);
  751. offset = page_offset(page);
  752. flags = -1;
  753. type = 0;
  754. /* TODO: cleanup count and page_dirty */
  755. do {
  756. if (offset >= end_offset)
  757. break;
  758. if (!buffer_uptodate(bh))
  759. uptodate = 0;
  760. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  761. /*
  762. * the iomap is actually still valid, but the ioend
  763. * isn't. shouldn't happen too often.
  764. */
  765. iomap_valid = 0;
  766. continue;
  767. }
  768. if (iomap_valid)
  769. iomap_valid = xfs_iomap_valid(&iomap, offset);
  770. /*
  771. * First case, map an unwritten extent and prepare for
  772. * extent state conversion transaction on completion.
  773. *
  774. * Second case, allocate space for a delalloc buffer.
  775. * We can return EAGAIN here in the release page case.
  776. *
  777. * Third case, an unmapped buffer was found, and we are
  778. * in a path where we need to write the whole page out.
  779. */
  780. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  781. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  782. !buffer_mapped(bh) && (unmapped || startio))) {
  783. /*
  784. * Make sure we don't use a read-only iomap
  785. */
  786. if (flags == BMAPI_READ)
  787. iomap_valid = 0;
  788. if (buffer_unwritten(bh)) {
  789. type = IOMAP_UNWRITTEN;
  790. flags = BMAPI_WRITE|BMAPI_IGNSTATE;
  791. } else if (buffer_delay(bh)) {
  792. type = IOMAP_DELAY;
  793. flags = BMAPI_ALLOCATE;
  794. if (!startio)
  795. flags |= trylock_flag;
  796. } else {
  797. type = IOMAP_NEW;
  798. flags = BMAPI_WRITE|BMAPI_MMAP;
  799. }
  800. if (!iomap_valid) {
  801. if (type == IOMAP_NEW) {
  802. size = xfs_probe_cluster(inode,
  803. page, bh, head, 0);
  804. } else {
  805. size = len;
  806. }
  807. err = xfs_map_blocks(inode, offset, size,
  808. &iomap, flags);
  809. if (err)
  810. goto error;
  811. iomap_valid = xfs_iomap_valid(&iomap, offset);
  812. }
  813. if (iomap_valid) {
  814. xfs_map_at_offset(bh, offset,
  815. inode->i_blkbits, &iomap);
  816. if (startio) {
  817. xfs_add_to_ioend(inode, bh, offset,
  818. type, &ioend,
  819. !iomap_valid);
  820. } else {
  821. set_buffer_dirty(bh);
  822. unlock_buffer(bh);
  823. mark_buffer_dirty(bh);
  824. }
  825. page_dirty--;
  826. count++;
  827. }
  828. } else if (buffer_uptodate(bh) && startio) {
  829. /*
  830. * we got here because the buffer is already mapped.
  831. * That means it must already have extents allocated
  832. * underneath it. Map the extent by reading it.
  833. */
  834. if (!iomap_valid || type != 0) {
  835. flags = BMAPI_READ;
  836. size = xfs_probe_cluster(inode, page, bh,
  837. head, 1);
  838. err = xfs_map_blocks(inode, offset, size,
  839. &iomap, flags);
  840. if (err)
  841. goto error;
  842. iomap_valid = xfs_iomap_valid(&iomap, offset);
  843. }
  844. type = 0;
  845. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  846. ASSERT(buffer_mapped(bh));
  847. if (iomap_valid)
  848. all_bh = 1;
  849. xfs_add_to_ioend(inode, bh, offset, type,
  850. &ioend, !iomap_valid);
  851. page_dirty--;
  852. count++;
  853. } else {
  854. iomap_valid = 0;
  855. }
  856. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  857. (unmapped || startio)) {
  858. iomap_valid = 0;
  859. }
  860. if (!iohead)
  861. iohead = ioend;
  862. } while (offset += len, ((bh = bh->b_this_page) != head));
  863. if (uptodate && bh == head)
  864. SetPageUptodate(page);
  865. if (startio)
  866. xfs_start_page_writeback(page, wbc, 1, count);
  867. if (ioend && iomap_valid) {
  868. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  869. PAGE_CACHE_SHIFT;
  870. tlast = min_t(pgoff_t, offset, last_index);
  871. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  872. wbc, startio, all_bh, tlast);
  873. }
  874. if (iohead)
  875. xfs_submit_ioend(iohead);
  876. return page_dirty;
  877. error:
  878. if (iohead)
  879. xfs_cancel_ioend(iohead);
  880. /*
  881. * If it's delalloc and we have nowhere to put it,
  882. * throw it away, unless the lower layers told
  883. * us to try again.
  884. */
  885. if (err != -EAGAIN) {
  886. if (!unmapped)
  887. block_invalidatepage(page, 0);
  888. ClearPageUptodate(page);
  889. }
  890. return err;
  891. }
  892. STATIC int
  893. __linvfs_get_block(
  894. struct inode *inode,
  895. sector_t iblock,
  896. unsigned long blocks,
  897. struct buffer_head *bh_result,
  898. int create,
  899. int direct,
  900. bmapi_flags_t flags)
  901. {
  902. vnode_t *vp = LINVFS_GET_VP(inode);
  903. xfs_iomap_t iomap;
  904. xfs_off_t offset;
  905. ssize_t size;
  906. int retpbbm = 1;
  907. int error;
  908. offset = (xfs_off_t)iblock << inode->i_blkbits;
  909. if (blocks)
  910. size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
  911. (xfs_off_t)blocks << inode->i_blkbits);
  912. else
  913. size = 1 << inode->i_blkbits;
  914. VOP_BMAP(vp, offset, size,
  915. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  916. if (error)
  917. return -error;
  918. if (retpbbm == 0)
  919. return 0;
  920. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  921. xfs_daddr_t bn;
  922. xfs_off_t delta;
  923. /* For unwritten extents do not report a disk address on
  924. * the read case (treat as if we're reading into a hole).
  925. */
  926. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  927. delta = offset - iomap.iomap_offset;
  928. delta >>= inode->i_blkbits;
  929. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  930. bn += delta;
  931. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  932. bh_result->b_blocknr = bn;
  933. set_buffer_mapped(bh_result);
  934. }
  935. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  936. if (direct)
  937. bh_result->b_private = inode;
  938. set_buffer_unwritten(bh_result);
  939. set_buffer_delay(bh_result);
  940. }
  941. }
  942. /* If this is a realtime file, data might be on a new device */
  943. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  944. /* If we previously allocated a block out beyond eof and
  945. * we are now coming back to use it then we will need to
  946. * flag it as new even if it has a disk address.
  947. */
  948. if (create &&
  949. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  950. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
  951. set_buffer_new(bh_result);
  952. if (iomap.iomap_flags & IOMAP_DELAY) {
  953. BUG_ON(direct);
  954. if (create) {
  955. set_buffer_uptodate(bh_result);
  956. set_buffer_mapped(bh_result);
  957. set_buffer_delay(bh_result);
  958. }
  959. }
  960. if (blocks) {
  961. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  962. offset = min_t(xfs_off_t,
  963. iomap.iomap_bsize - iomap.iomap_delta,
  964. (xfs_off_t)blocks << inode->i_blkbits);
  965. bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
  966. }
  967. return 0;
  968. }
  969. int
  970. linvfs_get_block(
  971. struct inode *inode,
  972. sector_t iblock,
  973. struct buffer_head *bh_result,
  974. int create)
  975. {
  976. return __linvfs_get_block(inode, iblock, 0, bh_result,
  977. create, 0, BMAPI_WRITE);
  978. }
  979. STATIC int
  980. linvfs_get_blocks_direct(
  981. struct inode *inode,
  982. sector_t iblock,
  983. unsigned long max_blocks,
  984. struct buffer_head *bh_result,
  985. int create)
  986. {
  987. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  988. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  989. }
  990. STATIC void
  991. linvfs_end_io_direct(
  992. struct kiocb *iocb,
  993. loff_t offset,
  994. ssize_t size,
  995. void *private)
  996. {
  997. xfs_ioend_t *ioend = iocb->private;
  998. /*
  999. * Non-NULL private data means we need to issue a transaction to
  1000. * convert a range from unwritten to written extents. This needs
  1001. * to happen from process contect but aio+dio I/O completion
  1002. * happens from irq context so we need to defer it to a workqueue.
  1003. * This is not nessecary for synchronous direct I/O, but we do
  1004. * it anyway to keep the code uniform and simpler.
  1005. *
  1006. * The core direct I/O code might be changed to always call the
  1007. * completion handler in the future, in which case all this can
  1008. * go away.
  1009. */
  1010. if (private && size > 0) {
  1011. ioend->io_offset = offset;
  1012. ioend->io_size = size;
  1013. xfs_finish_ioend(ioend);
  1014. } else {
  1015. ASSERT(size >= 0);
  1016. xfs_destroy_ioend(ioend);
  1017. }
  1018. /*
  1019. * blockdev_direct_IO can return an error even afer the I/O
  1020. * completion handler was called. Thus we need to protect
  1021. * against double-freeing.
  1022. */
  1023. iocb->private = NULL;
  1024. }
  1025. STATIC ssize_t
  1026. linvfs_direct_IO(
  1027. int rw,
  1028. struct kiocb *iocb,
  1029. const struct iovec *iov,
  1030. loff_t offset,
  1031. unsigned long nr_segs)
  1032. {
  1033. struct file *file = iocb->ki_filp;
  1034. struct inode *inode = file->f_mapping->host;
  1035. vnode_t *vp = LINVFS_GET_VP(inode);
  1036. xfs_iomap_t iomap;
  1037. int maps = 1;
  1038. int error;
  1039. ssize_t ret;
  1040. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1041. if (error)
  1042. return -error;
  1043. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1044. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1045. iomap.iomap_target->bt_bdev,
  1046. iov, offset, nr_segs,
  1047. linvfs_get_blocks_direct,
  1048. linvfs_end_io_direct);
  1049. if (unlikely(ret <= 0 && iocb->private))
  1050. xfs_destroy_ioend(iocb->private);
  1051. return ret;
  1052. }
  1053. STATIC sector_t
  1054. linvfs_bmap(
  1055. struct address_space *mapping,
  1056. sector_t block)
  1057. {
  1058. struct inode *inode = (struct inode *)mapping->host;
  1059. vnode_t *vp = LINVFS_GET_VP(inode);
  1060. int error;
  1061. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1062. VOP_RWLOCK(vp, VRWLOCK_READ);
  1063. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1064. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1065. return generic_block_bmap(mapping, block, linvfs_get_block);
  1066. }
  1067. STATIC int
  1068. linvfs_readpage(
  1069. struct file *unused,
  1070. struct page *page)
  1071. {
  1072. return mpage_readpage(page, linvfs_get_block);
  1073. }
  1074. STATIC int
  1075. linvfs_readpages(
  1076. struct file *unused,
  1077. struct address_space *mapping,
  1078. struct list_head *pages,
  1079. unsigned nr_pages)
  1080. {
  1081. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1082. }
  1083. STATIC void
  1084. xfs_count_page_state(
  1085. struct page *page,
  1086. int *delalloc,
  1087. int *unmapped,
  1088. int *unwritten)
  1089. {
  1090. struct buffer_head *bh, *head;
  1091. *delalloc = *unmapped = *unwritten = 0;
  1092. bh = head = page_buffers(page);
  1093. do {
  1094. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  1095. (*unmapped) = 1;
  1096. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  1097. clear_buffer_unwritten(bh);
  1098. else if (buffer_unwritten(bh))
  1099. (*unwritten) = 1;
  1100. else if (buffer_delay(bh))
  1101. (*delalloc) = 1;
  1102. } while ((bh = bh->b_this_page) != head);
  1103. }
  1104. /*
  1105. * writepage: Called from one of two places:
  1106. *
  1107. * 1. we are flushing a delalloc buffer head.
  1108. *
  1109. * 2. we are writing out a dirty page. Typically the page dirty
  1110. * state is cleared before we get here. In this case is it
  1111. * conceivable we have no buffer heads.
  1112. *
  1113. * For delalloc space on the page we need to allocate space and
  1114. * flush it. For unmapped buffer heads on the page we should
  1115. * allocate space if the page is uptodate. For any other dirty
  1116. * buffer heads on the page we should flush them.
  1117. *
  1118. * If we detect that a transaction would be required to flush
  1119. * the page, we have to check the process flags first, if we
  1120. * are already in a transaction or disk I/O during allocations
  1121. * is off, we need to fail the writepage and redirty the page.
  1122. */
  1123. STATIC int
  1124. linvfs_writepage(
  1125. struct page *page,
  1126. struct writeback_control *wbc)
  1127. {
  1128. int error;
  1129. int need_trans;
  1130. int delalloc, unmapped, unwritten;
  1131. struct inode *inode = page->mapping->host;
  1132. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1133. /*
  1134. * We need a transaction if:
  1135. * 1. There are delalloc buffers on the page
  1136. * 2. The page is uptodate and we have unmapped buffers
  1137. * 3. The page is uptodate and we have no buffers
  1138. * 4. There are unwritten buffers on the page
  1139. */
  1140. if (!page_has_buffers(page)) {
  1141. unmapped = 1;
  1142. need_trans = 1;
  1143. } else {
  1144. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1145. if (!PageUptodate(page))
  1146. unmapped = 0;
  1147. need_trans = delalloc + unmapped + unwritten;
  1148. }
  1149. /*
  1150. * If we need a transaction and the process flags say
  1151. * we are already in a transaction, or no IO is allowed
  1152. * then mark the page dirty again and leave the page
  1153. * as is.
  1154. */
  1155. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1156. goto out_fail;
  1157. /*
  1158. * Delay hooking up buffer heads until we have
  1159. * made our go/no-go decision.
  1160. */
  1161. if (!page_has_buffers(page))
  1162. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1163. /*
  1164. * Convert delayed allocate, unwritten or unmapped space
  1165. * to real space and flush out to disk.
  1166. */
  1167. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1168. if (error == -EAGAIN)
  1169. goto out_fail;
  1170. if (unlikely(error < 0))
  1171. goto out_unlock;
  1172. return 0;
  1173. out_fail:
  1174. redirty_page_for_writepage(wbc, page);
  1175. unlock_page(page);
  1176. return 0;
  1177. out_unlock:
  1178. unlock_page(page);
  1179. return error;
  1180. }
  1181. STATIC int
  1182. linvfs_invalidate_page(
  1183. struct page *page,
  1184. unsigned long offset)
  1185. {
  1186. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1187. page->mapping->host, page, offset);
  1188. return block_invalidatepage(page, offset);
  1189. }
  1190. /*
  1191. * Called to move a page into cleanable state - and from there
  1192. * to be released. Possibly the page is already clean. We always
  1193. * have buffer heads in this call.
  1194. *
  1195. * Returns 0 if the page is ok to release, 1 otherwise.
  1196. *
  1197. * Possible scenarios are:
  1198. *
  1199. * 1. We are being called to release a page which has been written
  1200. * to via regular I/O. buffer heads will be dirty and possibly
  1201. * delalloc. If no delalloc buffer heads in this case then we
  1202. * can just return zero.
  1203. *
  1204. * 2. We are called to release a page which has been written via
  1205. * mmap, all we need to do is ensure there is no delalloc
  1206. * state in the buffer heads, if not we can let the caller
  1207. * free them and we should come back later via writepage.
  1208. */
  1209. STATIC int
  1210. linvfs_release_page(
  1211. struct page *page,
  1212. gfp_t gfp_mask)
  1213. {
  1214. struct inode *inode = page->mapping->host;
  1215. int dirty, delalloc, unmapped, unwritten;
  1216. struct writeback_control wbc = {
  1217. .sync_mode = WB_SYNC_ALL,
  1218. .nr_to_write = 1,
  1219. };
  1220. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1221. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1222. if (!delalloc && !unwritten)
  1223. goto free_buffers;
  1224. if (!(gfp_mask & __GFP_FS))
  1225. return 0;
  1226. /* If we are already inside a transaction or the thread cannot
  1227. * do I/O, we cannot release this page.
  1228. */
  1229. if (PFLAGS_TEST_FSTRANS())
  1230. return 0;
  1231. /*
  1232. * Convert delalloc space to real space, do not flush the
  1233. * data out to disk, that will be done by the caller.
  1234. * Never need to allocate space here - we will always
  1235. * come back to writepage in that case.
  1236. */
  1237. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1238. if (dirty == 0 && !unwritten)
  1239. goto free_buffers;
  1240. return 0;
  1241. free_buffers:
  1242. return try_to_free_buffers(page);
  1243. }
  1244. STATIC int
  1245. linvfs_prepare_write(
  1246. struct file *file,
  1247. struct page *page,
  1248. unsigned int from,
  1249. unsigned int to)
  1250. {
  1251. return block_prepare_write(page, from, to, linvfs_get_block);
  1252. }
  1253. struct address_space_operations linvfs_aops = {
  1254. .readpage = linvfs_readpage,
  1255. .readpages = linvfs_readpages,
  1256. .writepage = linvfs_writepage,
  1257. .sync_page = block_sync_page,
  1258. .releasepage = linvfs_release_page,
  1259. .invalidatepage = linvfs_invalidate_page,
  1260. .prepare_write = linvfs_prepare_write,
  1261. .commit_write = generic_commit_write,
  1262. .bmap = linvfs_bmap,
  1263. .direct_IO = linvfs_direct_IO,
  1264. };