xfs_aops.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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_dir2.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dir2_sf.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_error.h"
  38. #include "xfs_rw.h"
  39. #include "xfs_iomap.h"
  40. #include "xfs_vnodeops.h"
  41. #include <linux/mpage.h>
  42. #include <linux/pagevec.h>
  43. #include <linux/writeback.h>
  44. /*
  45. * Prime number of hash buckets since address is used as the key.
  46. */
  47. #define NVSYNC 37
  48. #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
  49. static wait_queue_head_t xfs_ioend_wq[NVSYNC];
  50. void __init
  51. xfs_ioend_init(void)
  52. {
  53. int i;
  54. for (i = 0; i < NVSYNC; i++)
  55. init_waitqueue_head(&xfs_ioend_wq[i]);
  56. }
  57. void
  58. xfs_ioend_wait(
  59. xfs_inode_t *ip)
  60. {
  61. wait_queue_head_t *wq = to_ioend_wq(ip);
  62. wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
  63. }
  64. STATIC void
  65. xfs_ioend_wake(
  66. xfs_inode_t *ip)
  67. {
  68. if (atomic_dec_and_test(&ip->i_iocount))
  69. wake_up(to_ioend_wq(ip));
  70. }
  71. STATIC void
  72. xfs_count_page_state(
  73. struct page *page,
  74. int *delalloc,
  75. int *unmapped,
  76. int *unwritten)
  77. {
  78. struct buffer_head *bh, *head;
  79. *delalloc = *unmapped = *unwritten = 0;
  80. bh = head = page_buffers(page);
  81. do {
  82. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  83. (*unmapped) = 1;
  84. else if (buffer_unwritten(bh))
  85. (*unwritten) = 1;
  86. else if (buffer_delay(bh))
  87. (*delalloc) = 1;
  88. } while ((bh = bh->b_this_page) != head);
  89. }
  90. #if defined(XFS_RW_TRACE)
  91. void
  92. xfs_page_trace(
  93. int tag,
  94. struct inode *inode,
  95. struct page *page,
  96. unsigned long pgoff)
  97. {
  98. xfs_inode_t *ip;
  99. loff_t isize = i_size_read(inode);
  100. loff_t offset = page_offset(page);
  101. int delalloc = -1, unmapped = -1, unwritten = -1;
  102. if (page_has_buffers(page))
  103. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  104. ip = XFS_I(inode);
  105. if (!ip->i_rwtrace)
  106. return;
  107. ktrace_enter(ip->i_rwtrace,
  108. (void *)((unsigned long)tag),
  109. (void *)ip,
  110. (void *)inode,
  111. (void *)page,
  112. (void *)pgoff,
  113. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  114. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  115. (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
  116. (void *)((unsigned long)(isize & 0xffffffff)),
  117. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  118. (void *)((unsigned long)(offset & 0xffffffff)),
  119. (void *)((unsigned long)delalloc),
  120. (void *)((unsigned long)unmapped),
  121. (void *)((unsigned long)unwritten),
  122. (void *)((unsigned long)current_pid()),
  123. (void *)NULL);
  124. }
  125. #else
  126. #define xfs_page_trace(tag, inode, page, pgoff)
  127. #endif
  128. STATIC struct block_device *
  129. xfs_find_bdev_for_inode(
  130. struct xfs_inode *ip)
  131. {
  132. struct xfs_mount *mp = ip->i_mount;
  133. if (XFS_IS_REALTIME_INODE(ip))
  134. return mp->m_rtdev_targp->bt_bdev;
  135. else
  136. return mp->m_ddev_targp->bt_bdev;
  137. }
  138. /*
  139. * Schedule IO completion handling on a xfsdatad if this was
  140. * the final hold on this ioend. If we are asked to wait,
  141. * flush the workqueue.
  142. */
  143. STATIC void
  144. xfs_finish_ioend(
  145. xfs_ioend_t *ioend,
  146. int wait)
  147. {
  148. if (atomic_dec_and_test(&ioend->io_remaining)) {
  149. queue_work(xfsdatad_workqueue, &ioend->io_work);
  150. if (wait)
  151. flush_workqueue(xfsdatad_workqueue);
  152. }
  153. }
  154. /*
  155. * We're now finished for good with this ioend structure.
  156. * Update the page state via the associated buffer_heads,
  157. * release holds on the inode and bio, and finally free
  158. * up memory. Do not use the ioend after this.
  159. */
  160. STATIC void
  161. xfs_destroy_ioend(
  162. xfs_ioend_t *ioend)
  163. {
  164. struct buffer_head *bh, *next;
  165. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  166. for (bh = ioend->io_buffer_head; bh; bh = next) {
  167. next = bh->b_private;
  168. bh->b_end_io(bh, !ioend->io_error);
  169. }
  170. /*
  171. * Volume managers supporting multiple paths can send back ENODEV
  172. * when the final path disappears. In this case continuing to fill
  173. * the page cache with dirty data which cannot be written out is
  174. * evil, so prevent that.
  175. */
  176. if (unlikely(ioend->io_error == -ENODEV)) {
  177. xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
  178. __FILE__, __LINE__);
  179. }
  180. xfs_ioend_wake(ip);
  181. mempool_free(ioend, xfs_ioend_pool);
  182. }
  183. /*
  184. * Update on-disk file size now that data has been written to disk.
  185. * The current in-memory file size is i_size. If a write is beyond
  186. * eof i_new_size will be the intended file size until i_size is
  187. * updated. If this write does not extend all the way to the valid
  188. * file size then restrict this update to the end of the write.
  189. */
  190. STATIC void
  191. xfs_setfilesize(
  192. xfs_ioend_t *ioend)
  193. {
  194. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  195. xfs_fsize_t isize;
  196. xfs_fsize_t bsize;
  197. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  198. ASSERT(ioend->io_type != IOMAP_READ);
  199. if (unlikely(ioend->io_error))
  200. return;
  201. bsize = ioend->io_offset + ioend->io_size;
  202. xfs_ilock(ip, XFS_ILOCK_EXCL);
  203. isize = MAX(ip->i_size, ip->i_new_size);
  204. isize = MIN(isize, bsize);
  205. if (ip->i_d.di_size < isize) {
  206. ip->i_d.di_size = isize;
  207. ip->i_update_core = 1;
  208. ip->i_update_size = 1;
  209. xfs_mark_inode_dirty_sync(ip);
  210. }
  211. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  212. }
  213. /*
  214. * Buffered IO write completion for delayed allocate extents.
  215. */
  216. STATIC void
  217. xfs_end_bio_delalloc(
  218. struct work_struct *work)
  219. {
  220. xfs_ioend_t *ioend =
  221. container_of(work, xfs_ioend_t, io_work);
  222. xfs_setfilesize(ioend);
  223. xfs_destroy_ioend(ioend);
  224. }
  225. /*
  226. * Buffered IO write completion for regular, written extents.
  227. */
  228. STATIC void
  229. xfs_end_bio_written(
  230. struct work_struct *work)
  231. {
  232. xfs_ioend_t *ioend =
  233. container_of(work, xfs_ioend_t, io_work);
  234. xfs_setfilesize(ioend);
  235. xfs_destroy_ioend(ioend);
  236. }
  237. /*
  238. * IO write completion for unwritten extents.
  239. *
  240. * Issue transactions to convert a buffer range from unwritten
  241. * to written extents.
  242. */
  243. STATIC void
  244. xfs_end_bio_unwritten(
  245. struct work_struct *work)
  246. {
  247. xfs_ioend_t *ioend =
  248. container_of(work, xfs_ioend_t, io_work);
  249. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  250. xfs_off_t offset = ioend->io_offset;
  251. size_t size = ioend->io_size;
  252. if (likely(!ioend->io_error)) {
  253. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  254. int error;
  255. error = xfs_iomap_write_unwritten(ip, offset, size);
  256. if (error)
  257. ioend->io_error = error;
  258. }
  259. xfs_setfilesize(ioend);
  260. }
  261. xfs_destroy_ioend(ioend);
  262. }
  263. /*
  264. * IO read completion for regular, written extents.
  265. */
  266. STATIC void
  267. xfs_end_bio_read(
  268. struct work_struct *work)
  269. {
  270. xfs_ioend_t *ioend =
  271. container_of(work, xfs_ioend_t, io_work);
  272. xfs_destroy_ioend(ioend);
  273. }
  274. /*
  275. * Allocate and initialise an IO completion structure.
  276. * We need to track unwritten extent write completion here initially.
  277. * We'll need to extend this for updating the ondisk inode size later
  278. * (vs. incore size).
  279. */
  280. STATIC xfs_ioend_t *
  281. xfs_alloc_ioend(
  282. struct inode *inode,
  283. unsigned int type)
  284. {
  285. xfs_ioend_t *ioend;
  286. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  287. /*
  288. * Set the count to 1 initially, which will prevent an I/O
  289. * completion callback from happening before we have started
  290. * all the I/O from calling the completion routine too early.
  291. */
  292. atomic_set(&ioend->io_remaining, 1);
  293. ioend->io_error = 0;
  294. ioend->io_list = NULL;
  295. ioend->io_type = type;
  296. ioend->io_inode = inode;
  297. ioend->io_buffer_head = NULL;
  298. ioend->io_buffer_tail = NULL;
  299. atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
  300. ioend->io_offset = 0;
  301. ioend->io_size = 0;
  302. if (type == IOMAP_UNWRITTEN)
  303. INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten);
  304. else if (type == IOMAP_DELAY)
  305. INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc);
  306. else if (type == IOMAP_READ)
  307. INIT_WORK(&ioend->io_work, xfs_end_bio_read);
  308. else
  309. INIT_WORK(&ioend->io_work, xfs_end_bio_written);
  310. return ioend;
  311. }
  312. STATIC int
  313. xfs_map_blocks(
  314. struct inode *inode,
  315. loff_t offset,
  316. ssize_t count,
  317. xfs_iomap_t *mapp,
  318. int flags)
  319. {
  320. int nmaps = 1;
  321. return -xfs_iomap(XFS_I(inode), offset, count, flags, mapp, &nmaps);
  322. }
  323. STATIC_INLINE int
  324. xfs_iomap_valid(
  325. xfs_iomap_t *iomapp,
  326. loff_t offset)
  327. {
  328. return offset >= iomapp->iomap_offset &&
  329. offset < iomapp->iomap_offset + iomapp->iomap_bsize;
  330. }
  331. /*
  332. * BIO completion handler for buffered IO.
  333. */
  334. STATIC void
  335. xfs_end_bio(
  336. struct bio *bio,
  337. int error)
  338. {
  339. xfs_ioend_t *ioend = bio->bi_private;
  340. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  341. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  342. /* Toss bio and pass work off to an xfsdatad thread */
  343. bio->bi_private = NULL;
  344. bio->bi_end_io = NULL;
  345. bio_put(bio);
  346. xfs_finish_ioend(ioend, 0);
  347. }
  348. STATIC void
  349. xfs_submit_ioend_bio(
  350. xfs_ioend_t *ioend,
  351. struct bio *bio)
  352. {
  353. atomic_inc(&ioend->io_remaining);
  354. bio->bi_private = ioend;
  355. bio->bi_end_io = xfs_end_bio;
  356. submit_bio(WRITE, bio);
  357. ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
  358. bio_put(bio);
  359. }
  360. STATIC struct bio *
  361. xfs_alloc_ioend_bio(
  362. struct buffer_head *bh)
  363. {
  364. struct bio *bio;
  365. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  366. do {
  367. bio = bio_alloc(GFP_NOIO, nvecs);
  368. nvecs >>= 1;
  369. } while (!bio);
  370. ASSERT(bio->bi_private == NULL);
  371. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  372. bio->bi_bdev = bh->b_bdev;
  373. bio_get(bio);
  374. return bio;
  375. }
  376. STATIC void
  377. xfs_start_buffer_writeback(
  378. struct buffer_head *bh)
  379. {
  380. ASSERT(buffer_mapped(bh));
  381. ASSERT(buffer_locked(bh));
  382. ASSERT(!buffer_delay(bh));
  383. ASSERT(!buffer_unwritten(bh));
  384. mark_buffer_async_write(bh);
  385. set_buffer_uptodate(bh);
  386. clear_buffer_dirty(bh);
  387. }
  388. STATIC void
  389. xfs_start_page_writeback(
  390. struct page *page,
  391. int clear_dirty,
  392. int buffers)
  393. {
  394. ASSERT(PageLocked(page));
  395. ASSERT(!PageWriteback(page));
  396. if (clear_dirty)
  397. clear_page_dirty_for_io(page);
  398. set_page_writeback(page);
  399. unlock_page(page);
  400. /* If no buffers on the page are to be written, finish it here */
  401. if (!buffers)
  402. end_page_writeback(page);
  403. }
  404. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  405. {
  406. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  407. }
  408. /*
  409. * Submit all of the bios for all of the ioends we have saved up, covering the
  410. * initial writepage page and also any probed pages.
  411. *
  412. * Because we may have multiple ioends spanning a page, we need to start
  413. * writeback on all the buffers before we submit them for I/O. If we mark the
  414. * buffers as we got, then we can end up with a page that only has buffers
  415. * marked async write and I/O complete on can occur before we mark the other
  416. * buffers async write.
  417. *
  418. * The end result of this is that we trip a bug in end_page_writeback() because
  419. * we call it twice for the one page as the code in end_buffer_async_write()
  420. * assumes that all buffers on the page are started at the same time.
  421. *
  422. * The fix is two passes across the ioend list - one to start writeback on the
  423. * buffer_heads, and then submit them for I/O on the second pass.
  424. */
  425. STATIC void
  426. xfs_submit_ioend(
  427. xfs_ioend_t *ioend)
  428. {
  429. xfs_ioend_t *head = ioend;
  430. xfs_ioend_t *next;
  431. struct buffer_head *bh;
  432. struct bio *bio;
  433. sector_t lastblock = 0;
  434. /* Pass 1 - start writeback */
  435. do {
  436. next = ioend->io_list;
  437. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  438. xfs_start_buffer_writeback(bh);
  439. }
  440. } while ((ioend = next) != NULL);
  441. /* Pass 2 - submit I/O */
  442. ioend = head;
  443. do {
  444. next = ioend->io_list;
  445. bio = NULL;
  446. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  447. if (!bio) {
  448. retry:
  449. bio = xfs_alloc_ioend_bio(bh);
  450. } else if (bh->b_blocknr != lastblock + 1) {
  451. xfs_submit_ioend_bio(ioend, bio);
  452. goto retry;
  453. }
  454. if (bio_add_buffer(bio, bh) != bh->b_size) {
  455. xfs_submit_ioend_bio(ioend, bio);
  456. goto retry;
  457. }
  458. lastblock = bh->b_blocknr;
  459. }
  460. if (bio)
  461. xfs_submit_ioend_bio(ioend, bio);
  462. xfs_finish_ioend(ioend, 0);
  463. } while ((ioend = next) != NULL);
  464. }
  465. /*
  466. * Cancel submission of all buffer_heads so far in this endio.
  467. * Toss the endio too. Only ever called for the initial page
  468. * in a writepage request, so only ever one page.
  469. */
  470. STATIC void
  471. xfs_cancel_ioend(
  472. xfs_ioend_t *ioend)
  473. {
  474. xfs_ioend_t *next;
  475. struct buffer_head *bh, *next_bh;
  476. do {
  477. next = ioend->io_list;
  478. bh = ioend->io_buffer_head;
  479. do {
  480. next_bh = bh->b_private;
  481. clear_buffer_async_write(bh);
  482. unlock_buffer(bh);
  483. } while ((bh = next_bh) != NULL);
  484. xfs_ioend_wake(XFS_I(ioend->io_inode));
  485. mempool_free(ioend, xfs_ioend_pool);
  486. } while ((ioend = next) != NULL);
  487. }
  488. /*
  489. * Test to see if we've been building up a completion structure for
  490. * earlier buffers -- if so, we try to append to this ioend if we
  491. * can, otherwise we finish off any current ioend and start another.
  492. * Return true if we've finished the given ioend.
  493. */
  494. STATIC void
  495. xfs_add_to_ioend(
  496. struct inode *inode,
  497. struct buffer_head *bh,
  498. xfs_off_t offset,
  499. unsigned int type,
  500. xfs_ioend_t **result,
  501. int need_ioend)
  502. {
  503. xfs_ioend_t *ioend = *result;
  504. if (!ioend || need_ioend || type != ioend->io_type) {
  505. xfs_ioend_t *previous = *result;
  506. ioend = xfs_alloc_ioend(inode, type);
  507. ioend->io_offset = offset;
  508. ioend->io_buffer_head = bh;
  509. ioend->io_buffer_tail = bh;
  510. if (previous)
  511. previous->io_list = ioend;
  512. *result = ioend;
  513. } else {
  514. ioend->io_buffer_tail->b_private = bh;
  515. ioend->io_buffer_tail = bh;
  516. }
  517. bh->b_private = NULL;
  518. ioend->io_size += bh->b_size;
  519. }
  520. STATIC void
  521. xfs_map_buffer(
  522. struct buffer_head *bh,
  523. xfs_iomap_t *mp,
  524. xfs_off_t offset,
  525. uint block_bits)
  526. {
  527. sector_t bn;
  528. ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
  529. bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
  530. ((offset - mp->iomap_offset) >> block_bits);
  531. ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
  532. bh->b_blocknr = bn;
  533. set_buffer_mapped(bh);
  534. }
  535. STATIC void
  536. xfs_map_at_offset(
  537. struct buffer_head *bh,
  538. loff_t offset,
  539. int block_bits,
  540. xfs_iomap_t *iomapp)
  541. {
  542. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  543. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  544. lock_buffer(bh);
  545. xfs_map_buffer(bh, iomapp, offset, block_bits);
  546. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  547. set_buffer_mapped(bh);
  548. clear_buffer_delay(bh);
  549. clear_buffer_unwritten(bh);
  550. }
  551. /*
  552. * Look for a page at index that is suitable for clustering.
  553. */
  554. STATIC unsigned int
  555. xfs_probe_page(
  556. struct page *page,
  557. unsigned int pg_offset,
  558. int mapped)
  559. {
  560. int ret = 0;
  561. if (PageWriteback(page))
  562. return 0;
  563. if (page->mapping && PageDirty(page)) {
  564. if (page_has_buffers(page)) {
  565. struct buffer_head *bh, *head;
  566. bh = head = page_buffers(page);
  567. do {
  568. if (!buffer_uptodate(bh))
  569. break;
  570. if (mapped != buffer_mapped(bh))
  571. break;
  572. ret += bh->b_size;
  573. if (ret >= pg_offset)
  574. break;
  575. } while ((bh = bh->b_this_page) != head);
  576. } else
  577. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  578. }
  579. return ret;
  580. }
  581. STATIC size_t
  582. xfs_probe_cluster(
  583. struct inode *inode,
  584. struct page *startpage,
  585. struct buffer_head *bh,
  586. struct buffer_head *head,
  587. int mapped)
  588. {
  589. struct pagevec pvec;
  590. pgoff_t tindex, tlast, tloff;
  591. size_t total = 0;
  592. int done = 0, i;
  593. /* First sum forwards in this page */
  594. do {
  595. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  596. return total;
  597. total += bh->b_size;
  598. } while ((bh = bh->b_this_page) != head);
  599. /* if we reached the end of the page, sum forwards in following pages */
  600. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  601. tindex = startpage->index + 1;
  602. /* Prune this back to avoid pathological behavior */
  603. tloff = min(tlast, startpage->index + 64);
  604. pagevec_init(&pvec, 0);
  605. while (!done && tindex <= tloff) {
  606. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  607. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  608. break;
  609. for (i = 0; i < pagevec_count(&pvec); i++) {
  610. struct page *page = pvec.pages[i];
  611. size_t pg_offset, pg_len = 0;
  612. if (tindex == tlast) {
  613. pg_offset =
  614. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  615. if (!pg_offset) {
  616. done = 1;
  617. break;
  618. }
  619. } else
  620. pg_offset = PAGE_CACHE_SIZE;
  621. if (page->index == tindex && trylock_page(page)) {
  622. pg_len = xfs_probe_page(page, pg_offset, mapped);
  623. unlock_page(page);
  624. }
  625. if (!pg_len) {
  626. done = 1;
  627. break;
  628. }
  629. total += pg_len;
  630. tindex++;
  631. }
  632. pagevec_release(&pvec);
  633. cond_resched();
  634. }
  635. return total;
  636. }
  637. /*
  638. * Test if a given page is suitable for writing as part of an unwritten
  639. * or delayed allocate extent.
  640. */
  641. STATIC int
  642. xfs_is_delayed_page(
  643. struct page *page,
  644. unsigned int type)
  645. {
  646. if (PageWriteback(page))
  647. return 0;
  648. if (page->mapping && page_has_buffers(page)) {
  649. struct buffer_head *bh, *head;
  650. int acceptable = 0;
  651. bh = head = page_buffers(page);
  652. do {
  653. if (buffer_unwritten(bh))
  654. acceptable = (type == IOMAP_UNWRITTEN);
  655. else if (buffer_delay(bh))
  656. acceptable = (type == IOMAP_DELAY);
  657. else if (buffer_dirty(bh) && buffer_mapped(bh))
  658. acceptable = (type == IOMAP_NEW);
  659. else
  660. break;
  661. } while ((bh = bh->b_this_page) != head);
  662. if (acceptable)
  663. return 1;
  664. }
  665. return 0;
  666. }
  667. /*
  668. * Allocate & map buffers for page given the extent map. Write it out.
  669. * except for the original page of a writepage, this is called on
  670. * delalloc/unwritten pages only, for the original page it is possible
  671. * that the page has no mapping at all.
  672. */
  673. STATIC int
  674. xfs_convert_page(
  675. struct inode *inode,
  676. struct page *page,
  677. loff_t tindex,
  678. xfs_iomap_t *mp,
  679. xfs_ioend_t **ioendp,
  680. struct writeback_control *wbc,
  681. int startio,
  682. int all_bh)
  683. {
  684. struct buffer_head *bh, *head;
  685. xfs_off_t end_offset;
  686. unsigned long p_offset;
  687. unsigned int type;
  688. int bbits = inode->i_blkbits;
  689. int len, page_dirty;
  690. int count = 0, done = 0, uptodate = 1;
  691. xfs_off_t offset = page_offset(page);
  692. if (page->index != tindex)
  693. goto fail;
  694. if (!trylock_page(page))
  695. goto fail;
  696. if (PageWriteback(page))
  697. goto fail_unlock_page;
  698. if (page->mapping != inode->i_mapping)
  699. goto fail_unlock_page;
  700. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  701. goto fail_unlock_page;
  702. /*
  703. * page_dirty is initially a count of buffers on the page before
  704. * EOF and is decremented as we move each into a cleanable state.
  705. *
  706. * Derivation:
  707. *
  708. * End offset is the highest offset that this page should represent.
  709. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  710. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  711. * hence give us the correct page_dirty count. On any other page,
  712. * it will be zero and in that case we need page_dirty to be the
  713. * count of buffers on the page.
  714. */
  715. end_offset = min_t(unsigned long long,
  716. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  717. i_size_read(inode));
  718. len = 1 << inode->i_blkbits;
  719. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  720. PAGE_CACHE_SIZE);
  721. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  722. page_dirty = p_offset / len;
  723. bh = head = page_buffers(page);
  724. do {
  725. if (offset >= end_offset)
  726. break;
  727. if (!buffer_uptodate(bh))
  728. uptodate = 0;
  729. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  730. done = 1;
  731. continue;
  732. }
  733. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  734. if (buffer_unwritten(bh))
  735. type = IOMAP_UNWRITTEN;
  736. else
  737. type = IOMAP_DELAY;
  738. if (!xfs_iomap_valid(mp, offset)) {
  739. done = 1;
  740. continue;
  741. }
  742. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  743. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  744. xfs_map_at_offset(bh, offset, bbits, mp);
  745. if (startio) {
  746. xfs_add_to_ioend(inode, bh, offset,
  747. type, ioendp, done);
  748. } else {
  749. set_buffer_dirty(bh);
  750. unlock_buffer(bh);
  751. mark_buffer_dirty(bh);
  752. }
  753. page_dirty--;
  754. count++;
  755. } else {
  756. type = IOMAP_NEW;
  757. if (buffer_mapped(bh) && all_bh && startio) {
  758. lock_buffer(bh);
  759. xfs_add_to_ioend(inode, bh, offset,
  760. type, ioendp, done);
  761. count++;
  762. page_dirty--;
  763. } else {
  764. done = 1;
  765. }
  766. }
  767. } while (offset += len, (bh = bh->b_this_page) != head);
  768. if (uptodate && bh == head)
  769. SetPageUptodate(page);
  770. if (startio) {
  771. if (count) {
  772. struct backing_dev_info *bdi;
  773. bdi = inode->i_mapping->backing_dev_info;
  774. wbc->nr_to_write--;
  775. if (bdi_write_congested(bdi)) {
  776. wbc->encountered_congestion = 1;
  777. done = 1;
  778. } else if (wbc->nr_to_write <= 0) {
  779. done = 1;
  780. }
  781. }
  782. xfs_start_page_writeback(page, !page_dirty, count);
  783. }
  784. return done;
  785. fail_unlock_page:
  786. unlock_page(page);
  787. fail:
  788. return 1;
  789. }
  790. /*
  791. * Convert & write out a cluster of pages in the same extent as defined
  792. * by mp and following the start page.
  793. */
  794. STATIC void
  795. xfs_cluster_write(
  796. struct inode *inode,
  797. pgoff_t tindex,
  798. xfs_iomap_t *iomapp,
  799. xfs_ioend_t **ioendp,
  800. struct writeback_control *wbc,
  801. int startio,
  802. int all_bh,
  803. pgoff_t tlast)
  804. {
  805. struct pagevec pvec;
  806. int done = 0, i;
  807. pagevec_init(&pvec, 0);
  808. while (!done && tindex <= tlast) {
  809. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  810. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  811. break;
  812. for (i = 0; i < pagevec_count(&pvec); i++) {
  813. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  814. iomapp, ioendp, wbc, startio, all_bh);
  815. if (done)
  816. break;
  817. }
  818. pagevec_release(&pvec);
  819. cond_resched();
  820. }
  821. }
  822. /*
  823. * Calling this without startio set means we are being asked to make a dirty
  824. * page ready for freeing it's buffers. When called with startio set then
  825. * we are coming from writepage.
  826. *
  827. * When called with startio set it is important that we write the WHOLE
  828. * page if possible.
  829. * The bh->b_state's cannot know if any of the blocks or which block for
  830. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  831. * only valid if the page itself isn't completely uptodate. Some layers
  832. * may clear the page dirty flag prior to calling write page, under the
  833. * assumption the entire page will be written out; by not writing out the
  834. * whole page the page can be reused before all valid dirty data is
  835. * written out. Note: in the case of a page that has been dirty'd by
  836. * mapwrite and but partially setup by block_prepare_write the
  837. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  838. * valid state, thus the whole page must be written out thing.
  839. */
  840. STATIC int
  841. xfs_page_state_convert(
  842. struct inode *inode,
  843. struct page *page,
  844. struct writeback_control *wbc,
  845. int startio,
  846. int unmapped) /* also implies page uptodate */
  847. {
  848. struct buffer_head *bh, *head;
  849. xfs_iomap_t iomap;
  850. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  851. loff_t offset;
  852. unsigned long p_offset = 0;
  853. unsigned int type;
  854. __uint64_t end_offset;
  855. pgoff_t end_index, last_index, tlast;
  856. ssize_t size, len;
  857. int flags, err, iomap_valid = 0, uptodate = 1;
  858. int page_dirty, count = 0;
  859. int trylock = 0;
  860. int all_bh = unmapped;
  861. if (startio) {
  862. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  863. trylock |= BMAPI_TRYLOCK;
  864. }
  865. /* Is this page beyond the end of the file? */
  866. offset = i_size_read(inode);
  867. end_index = offset >> PAGE_CACHE_SHIFT;
  868. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  869. if (page->index >= end_index) {
  870. if ((page->index >= end_index + 1) ||
  871. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  872. if (startio)
  873. unlock_page(page);
  874. return 0;
  875. }
  876. }
  877. /*
  878. * page_dirty is initially a count of buffers on the page before
  879. * EOF and is decremented as we move each into a cleanable state.
  880. *
  881. * Derivation:
  882. *
  883. * End offset is the highest offset that this page should represent.
  884. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  885. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  886. * hence give us the correct page_dirty count. On any other page,
  887. * it will be zero and in that case we need page_dirty to be the
  888. * count of buffers on the page.
  889. */
  890. end_offset = min_t(unsigned long long,
  891. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  892. len = 1 << inode->i_blkbits;
  893. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  894. PAGE_CACHE_SIZE);
  895. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  896. page_dirty = p_offset / len;
  897. bh = head = page_buffers(page);
  898. offset = page_offset(page);
  899. flags = BMAPI_READ;
  900. type = IOMAP_NEW;
  901. /* TODO: cleanup count and page_dirty */
  902. do {
  903. if (offset >= end_offset)
  904. break;
  905. if (!buffer_uptodate(bh))
  906. uptodate = 0;
  907. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  908. /*
  909. * the iomap is actually still valid, but the ioend
  910. * isn't. shouldn't happen too often.
  911. */
  912. iomap_valid = 0;
  913. continue;
  914. }
  915. if (iomap_valid)
  916. iomap_valid = xfs_iomap_valid(&iomap, offset);
  917. /*
  918. * First case, map an unwritten extent and prepare for
  919. * extent state conversion transaction on completion.
  920. *
  921. * Second case, allocate space for a delalloc buffer.
  922. * We can return EAGAIN here in the release page case.
  923. *
  924. * Third case, an unmapped buffer was found, and we are
  925. * in a path where we need to write the whole page out.
  926. */
  927. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  928. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  929. !buffer_mapped(bh) && (unmapped || startio))) {
  930. int new_ioend = 0;
  931. /*
  932. * Make sure we don't use a read-only iomap
  933. */
  934. if (flags == BMAPI_READ)
  935. iomap_valid = 0;
  936. if (buffer_unwritten(bh)) {
  937. type = IOMAP_UNWRITTEN;
  938. flags = BMAPI_WRITE | BMAPI_IGNSTATE;
  939. } else if (buffer_delay(bh)) {
  940. type = IOMAP_DELAY;
  941. flags = BMAPI_ALLOCATE | trylock;
  942. } else {
  943. type = IOMAP_NEW;
  944. flags = BMAPI_WRITE | BMAPI_MMAP;
  945. }
  946. if (!iomap_valid) {
  947. /*
  948. * if we didn't have a valid mapping then we
  949. * need to ensure that we put the new mapping
  950. * in a new ioend structure. This needs to be
  951. * done to ensure that the ioends correctly
  952. * reflect the block mappings at io completion
  953. * for unwritten extent conversion.
  954. */
  955. new_ioend = 1;
  956. if (type == IOMAP_NEW) {
  957. size = xfs_probe_cluster(inode,
  958. page, bh, head, 0);
  959. } else {
  960. size = len;
  961. }
  962. err = xfs_map_blocks(inode, offset, size,
  963. &iomap, flags);
  964. if (err)
  965. goto error;
  966. iomap_valid = xfs_iomap_valid(&iomap, offset);
  967. }
  968. if (iomap_valid) {
  969. xfs_map_at_offset(bh, offset,
  970. inode->i_blkbits, &iomap);
  971. if (startio) {
  972. xfs_add_to_ioend(inode, bh, offset,
  973. type, &ioend,
  974. new_ioend);
  975. } else {
  976. set_buffer_dirty(bh);
  977. unlock_buffer(bh);
  978. mark_buffer_dirty(bh);
  979. }
  980. page_dirty--;
  981. count++;
  982. }
  983. } else if (buffer_uptodate(bh) && startio) {
  984. /*
  985. * we got here because the buffer is already mapped.
  986. * That means it must already have extents allocated
  987. * underneath it. Map the extent by reading it.
  988. */
  989. if (!iomap_valid || flags != BMAPI_READ) {
  990. flags = BMAPI_READ;
  991. size = xfs_probe_cluster(inode, page, bh,
  992. head, 1);
  993. err = xfs_map_blocks(inode, offset, size,
  994. &iomap, flags);
  995. if (err)
  996. goto error;
  997. iomap_valid = xfs_iomap_valid(&iomap, offset);
  998. }
  999. /*
  1000. * We set the type to IOMAP_NEW in case we are doing a
  1001. * small write at EOF that is extending the file but
  1002. * without needing an allocation. We need to update the
  1003. * file size on I/O completion in this case so it is
  1004. * the same case as having just allocated a new extent
  1005. * that we are writing into for the first time.
  1006. */
  1007. type = IOMAP_NEW;
  1008. if (trylock_buffer(bh)) {
  1009. ASSERT(buffer_mapped(bh));
  1010. if (iomap_valid)
  1011. all_bh = 1;
  1012. xfs_add_to_ioend(inode, bh, offset, type,
  1013. &ioend, !iomap_valid);
  1014. page_dirty--;
  1015. count++;
  1016. } else {
  1017. iomap_valid = 0;
  1018. }
  1019. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  1020. (unmapped || startio)) {
  1021. iomap_valid = 0;
  1022. }
  1023. if (!iohead)
  1024. iohead = ioend;
  1025. } while (offset += len, ((bh = bh->b_this_page) != head));
  1026. if (uptodate && bh == head)
  1027. SetPageUptodate(page);
  1028. if (startio)
  1029. xfs_start_page_writeback(page, 1, count);
  1030. if (ioend && iomap_valid) {
  1031. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  1032. PAGE_CACHE_SHIFT;
  1033. tlast = min_t(pgoff_t, offset, last_index);
  1034. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  1035. wbc, startio, all_bh, tlast);
  1036. }
  1037. if (iohead)
  1038. xfs_submit_ioend(iohead);
  1039. return page_dirty;
  1040. error:
  1041. if (iohead)
  1042. xfs_cancel_ioend(iohead);
  1043. /*
  1044. * If it's delalloc and we have nowhere to put it,
  1045. * throw it away, unless the lower layers told
  1046. * us to try again.
  1047. */
  1048. if (err != -EAGAIN) {
  1049. if (!unmapped)
  1050. block_invalidatepage(page, 0);
  1051. ClearPageUptodate(page);
  1052. }
  1053. return err;
  1054. }
  1055. /*
  1056. * writepage: Called from one of two places:
  1057. *
  1058. * 1. we are flushing a delalloc buffer head.
  1059. *
  1060. * 2. we are writing out a dirty page. Typically the page dirty
  1061. * state is cleared before we get here. In this case is it
  1062. * conceivable we have no buffer heads.
  1063. *
  1064. * For delalloc space on the page we need to allocate space and
  1065. * flush it. For unmapped buffer heads on the page we should
  1066. * allocate space if the page is uptodate. For any other dirty
  1067. * buffer heads on the page we should flush them.
  1068. *
  1069. * If we detect that a transaction would be required to flush
  1070. * the page, we have to check the process flags first, if we
  1071. * are already in a transaction or disk I/O during allocations
  1072. * is off, we need to fail the writepage and redirty the page.
  1073. */
  1074. STATIC int
  1075. xfs_vm_writepage(
  1076. struct page *page,
  1077. struct writeback_control *wbc)
  1078. {
  1079. int error;
  1080. int need_trans;
  1081. int delalloc, unmapped, unwritten;
  1082. struct inode *inode = page->mapping->host;
  1083. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1084. /*
  1085. * We need a transaction if:
  1086. * 1. There are delalloc buffers on the page
  1087. * 2. The page is uptodate and we have unmapped buffers
  1088. * 3. The page is uptodate and we have no buffers
  1089. * 4. There are unwritten buffers on the page
  1090. */
  1091. if (!page_has_buffers(page)) {
  1092. unmapped = 1;
  1093. need_trans = 1;
  1094. } else {
  1095. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1096. if (!PageUptodate(page))
  1097. unmapped = 0;
  1098. need_trans = delalloc + unmapped + unwritten;
  1099. }
  1100. /*
  1101. * If we need a transaction and the process flags say
  1102. * we are already in a transaction, or no IO is allowed
  1103. * then mark the page dirty again and leave the page
  1104. * as is.
  1105. */
  1106. if (current_test_flags(PF_FSTRANS) && need_trans)
  1107. goto out_fail;
  1108. /*
  1109. * Delay hooking up buffer heads until we have
  1110. * made our go/no-go decision.
  1111. */
  1112. if (!page_has_buffers(page))
  1113. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1114. /*
  1115. * Convert delayed allocate, unwritten or unmapped space
  1116. * to real space and flush out to disk.
  1117. */
  1118. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1119. if (error == -EAGAIN)
  1120. goto out_fail;
  1121. if (unlikely(error < 0))
  1122. goto out_unlock;
  1123. return 0;
  1124. out_fail:
  1125. redirty_page_for_writepage(wbc, page);
  1126. unlock_page(page);
  1127. return 0;
  1128. out_unlock:
  1129. unlock_page(page);
  1130. return error;
  1131. }
  1132. STATIC int
  1133. xfs_vm_writepages(
  1134. struct address_space *mapping,
  1135. struct writeback_control *wbc)
  1136. {
  1137. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1138. return generic_writepages(mapping, wbc);
  1139. }
  1140. /*
  1141. * Called to move a page into cleanable state - and from there
  1142. * to be released. Possibly the page is already clean. We always
  1143. * have buffer heads in this call.
  1144. *
  1145. * Returns 0 if the page is ok to release, 1 otherwise.
  1146. *
  1147. * Possible scenarios are:
  1148. *
  1149. * 1. We are being called to release a page which has been written
  1150. * to via regular I/O. buffer heads will be dirty and possibly
  1151. * delalloc. If no delalloc buffer heads in this case then we
  1152. * can just return zero.
  1153. *
  1154. * 2. We are called to release a page which has been written via
  1155. * mmap, all we need to do is ensure there is no delalloc
  1156. * state in the buffer heads, if not we can let the caller
  1157. * free them and we should come back later via writepage.
  1158. */
  1159. STATIC int
  1160. xfs_vm_releasepage(
  1161. struct page *page,
  1162. gfp_t gfp_mask)
  1163. {
  1164. struct inode *inode = page->mapping->host;
  1165. int dirty, delalloc, unmapped, unwritten;
  1166. struct writeback_control wbc = {
  1167. .sync_mode = WB_SYNC_ALL,
  1168. .nr_to_write = 1,
  1169. };
  1170. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, 0);
  1171. if (!page_has_buffers(page))
  1172. return 0;
  1173. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1174. if (!delalloc && !unwritten)
  1175. goto free_buffers;
  1176. if (!(gfp_mask & __GFP_FS))
  1177. return 0;
  1178. /* If we are already inside a transaction or the thread cannot
  1179. * do I/O, we cannot release this page.
  1180. */
  1181. if (current_test_flags(PF_FSTRANS))
  1182. return 0;
  1183. /*
  1184. * Convert delalloc space to real space, do not flush the
  1185. * data out to disk, that will be done by the caller.
  1186. * Never need to allocate space here - we will always
  1187. * come back to writepage in that case.
  1188. */
  1189. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1190. if (dirty == 0 && !unwritten)
  1191. goto free_buffers;
  1192. return 0;
  1193. free_buffers:
  1194. return try_to_free_buffers(page);
  1195. }
  1196. STATIC int
  1197. __xfs_get_blocks(
  1198. struct inode *inode,
  1199. sector_t iblock,
  1200. struct buffer_head *bh_result,
  1201. int create,
  1202. int direct,
  1203. bmapi_flags_t flags)
  1204. {
  1205. xfs_iomap_t iomap;
  1206. xfs_off_t offset;
  1207. ssize_t size;
  1208. int niomap = 1;
  1209. int error;
  1210. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1211. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1212. size = bh_result->b_size;
  1213. if (!create && direct && offset >= i_size_read(inode))
  1214. return 0;
  1215. error = xfs_iomap(XFS_I(inode), offset, size,
  1216. create ? flags : BMAPI_READ, &iomap, &niomap);
  1217. if (error)
  1218. return -error;
  1219. if (niomap == 0)
  1220. return 0;
  1221. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  1222. /*
  1223. * For unwritten extents do not report a disk address on
  1224. * the read case (treat as if we're reading into a hole).
  1225. */
  1226. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1227. xfs_map_buffer(bh_result, &iomap, offset,
  1228. inode->i_blkbits);
  1229. }
  1230. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1231. if (direct)
  1232. bh_result->b_private = inode;
  1233. set_buffer_unwritten(bh_result);
  1234. }
  1235. }
  1236. /*
  1237. * If this is a realtime file, data may be on a different device.
  1238. * to that pointed to from the buffer_head b_bdev currently.
  1239. */
  1240. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  1241. /*
  1242. * If we previously allocated a block out beyond eof and we are now
  1243. * coming back to use it then we will need to flag it as new even if it
  1244. * has a disk address.
  1245. *
  1246. * With sub-block writes into unwritten extents we also need to mark
  1247. * the buffer as new so that the unwritten parts of the buffer gets
  1248. * correctly zeroed.
  1249. */
  1250. if (create &&
  1251. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1252. (offset >= i_size_read(inode)) ||
  1253. (iomap.iomap_flags & (IOMAP_NEW|IOMAP_UNWRITTEN))))
  1254. set_buffer_new(bh_result);
  1255. if (iomap.iomap_flags & IOMAP_DELAY) {
  1256. BUG_ON(direct);
  1257. if (create) {
  1258. set_buffer_uptodate(bh_result);
  1259. set_buffer_mapped(bh_result);
  1260. set_buffer_delay(bh_result);
  1261. }
  1262. }
  1263. if (direct || size > (1 << inode->i_blkbits)) {
  1264. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  1265. offset = min_t(xfs_off_t,
  1266. iomap.iomap_bsize - iomap.iomap_delta, size);
  1267. bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
  1268. }
  1269. return 0;
  1270. }
  1271. int
  1272. xfs_get_blocks(
  1273. struct inode *inode,
  1274. sector_t iblock,
  1275. struct buffer_head *bh_result,
  1276. int create)
  1277. {
  1278. return __xfs_get_blocks(inode, iblock,
  1279. bh_result, create, 0, BMAPI_WRITE);
  1280. }
  1281. STATIC int
  1282. xfs_get_blocks_direct(
  1283. struct inode *inode,
  1284. sector_t iblock,
  1285. struct buffer_head *bh_result,
  1286. int create)
  1287. {
  1288. return __xfs_get_blocks(inode, iblock,
  1289. bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1290. }
  1291. STATIC void
  1292. xfs_end_io_direct(
  1293. struct kiocb *iocb,
  1294. loff_t offset,
  1295. ssize_t size,
  1296. void *private)
  1297. {
  1298. xfs_ioend_t *ioend = iocb->private;
  1299. /*
  1300. * Non-NULL private data means we need to issue a transaction to
  1301. * convert a range from unwritten to written extents. This needs
  1302. * to happen from process context but aio+dio I/O completion
  1303. * happens from irq context so we need to defer it to a workqueue.
  1304. * This is not necessary for synchronous direct I/O, but we do
  1305. * it anyway to keep the code uniform and simpler.
  1306. *
  1307. * Well, if only it were that simple. Because synchronous direct I/O
  1308. * requires extent conversion to occur *before* we return to userspace,
  1309. * we have to wait for extent conversion to complete. Look at the
  1310. * iocb that has been passed to us to determine if this is AIO or
  1311. * not. If it is synchronous, tell xfs_finish_ioend() to kick the
  1312. * workqueue and wait for it to complete.
  1313. *
  1314. * The core direct I/O code might be changed to always call the
  1315. * completion handler in the future, in which case all this can
  1316. * go away.
  1317. */
  1318. ioend->io_offset = offset;
  1319. ioend->io_size = size;
  1320. if (ioend->io_type == IOMAP_READ) {
  1321. xfs_finish_ioend(ioend, 0);
  1322. } else if (private && size > 0) {
  1323. xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
  1324. } else {
  1325. /*
  1326. * A direct I/O write ioend starts it's life in unwritten
  1327. * state in case they map an unwritten extent. This write
  1328. * didn't map an unwritten extent so switch it's completion
  1329. * handler.
  1330. */
  1331. INIT_WORK(&ioend->io_work, xfs_end_bio_written);
  1332. xfs_finish_ioend(ioend, 0);
  1333. }
  1334. /*
  1335. * blockdev_direct_IO can return an error even after the I/O
  1336. * completion handler was called. Thus we need to protect
  1337. * against double-freeing.
  1338. */
  1339. iocb->private = NULL;
  1340. }
  1341. STATIC ssize_t
  1342. xfs_vm_direct_IO(
  1343. int rw,
  1344. struct kiocb *iocb,
  1345. const struct iovec *iov,
  1346. loff_t offset,
  1347. unsigned long nr_segs)
  1348. {
  1349. struct file *file = iocb->ki_filp;
  1350. struct inode *inode = file->f_mapping->host;
  1351. struct block_device *bdev;
  1352. ssize_t ret;
  1353. bdev = xfs_find_bdev_for_inode(XFS_I(inode));
  1354. if (rw == WRITE) {
  1355. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1356. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1357. bdev, iov, offset, nr_segs,
  1358. xfs_get_blocks_direct,
  1359. xfs_end_io_direct);
  1360. } else {
  1361. iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
  1362. ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
  1363. bdev, iov, offset, nr_segs,
  1364. xfs_get_blocks_direct,
  1365. xfs_end_io_direct);
  1366. }
  1367. if (unlikely(ret != -EIOCBQUEUED && iocb->private))
  1368. xfs_destroy_ioend(iocb->private);
  1369. return ret;
  1370. }
  1371. STATIC int
  1372. xfs_vm_write_begin(
  1373. struct file *file,
  1374. struct address_space *mapping,
  1375. loff_t pos,
  1376. unsigned len,
  1377. unsigned flags,
  1378. struct page **pagep,
  1379. void **fsdata)
  1380. {
  1381. *pagep = NULL;
  1382. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1383. xfs_get_blocks);
  1384. }
  1385. STATIC sector_t
  1386. xfs_vm_bmap(
  1387. struct address_space *mapping,
  1388. sector_t block)
  1389. {
  1390. struct inode *inode = (struct inode *)mapping->host;
  1391. struct xfs_inode *ip = XFS_I(inode);
  1392. xfs_itrace_entry(XFS_I(inode));
  1393. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1394. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1395. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1396. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1397. }
  1398. STATIC int
  1399. xfs_vm_readpage(
  1400. struct file *unused,
  1401. struct page *page)
  1402. {
  1403. return mpage_readpage(page, xfs_get_blocks);
  1404. }
  1405. STATIC int
  1406. xfs_vm_readpages(
  1407. struct file *unused,
  1408. struct address_space *mapping,
  1409. struct list_head *pages,
  1410. unsigned nr_pages)
  1411. {
  1412. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1413. }
  1414. STATIC void
  1415. xfs_vm_invalidatepage(
  1416. struct page *page,
  1417. unsigned long offset)
  1418. {
  1419. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1420. page->mapping->host, page, offset);
  1421. block_invalidatepage(page, offset);
  1422. }
  1423. const struct address_space_operations xfs_address_space_operations = {
  1424. .readpage = xfs_vm_readpage,
  1425. .readpages = xfs_vm_readpages,
  1426. .writepage = xfs_vm_writepage,
  1427. .writepages = xfs_vm_writepages,
  1428. .sync_page = block_sync_page,
  1429. .releasepage = xfs_vm_releasepage,
  1430. .invalidatepage = xfs_vm_invalidatepage,
  1431. .write_begin = xfs_vm_write_begin,
  1432. .write_end = generic_write_end,
  1433. .bmap = xfs_vm_bmap,
  1434. .direct_IO = xfs_vm_direct_IO,
  1435. .migratepage = buffer_migrate_page,
  1436. .is_partially_uptodate = block_is_partially_uptodate,
  1437. };