xfs_aops.c 40 KB

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