xfs_buf.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /*
  2. * Copyright (c) 2000-2006 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 <linux/stddef.h>
  20. #include <linux/errno.h>
  21. #include <linux/gfp.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/init.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/bio.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/percpu.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/hash.h>
  32. #include <linux/kthread.h>
  33. #include <linux/migrate.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/freezer.h>
  36. #include "xfs_log_format.h"
  37. #include "xfs_trans_resv.h"
  38. #include "xfs_sb.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_mount.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_log.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. static struct workqueue_struct *xfslogd_workqueue;
  45. #ifdef XFS_BUF_LOCK_TRACKING
  46. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  47. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  48. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  49. #else
  50. # define XB_SET_OWNER(bp) do { } while (0)
  51. # define XB_CLEAR_OWNER(bp) do { } while (0)
  52. # define XB_GET_OWNER(bp) do { } while (0)
  53. #endif
  54. #define xb_to_gfp(flags) \
  55. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : GFP_NOFS) | __GFP_NOWARN)
  56. static inline int
  57. xfs_buf_is_vmapped(
  58. struct xfs_buf *bp)
  59. {
  60. /*
  61. * Return true if the buffer is vmapped.
  62. *
  63. * b_addr is null if the buffer is not mapped, but the code is clever
  64. * enough to know it doesn't have to map a single page, so the check has
  65. * to be both for b_addr and bp->b_page_count > 1.
  66. */
  67. return bp->b_addr && bp->b_page_count > 1;
  68. }
  69. static inline int
  70. xfs_buf_vmap_len(
  71. struct xfs_buf *bp)
  72. {
  73. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  74. }
  75. /*
  76. * When we mark a buffer stale, we remove the buffer from the LRU and clear the
  77. * b_lru_ref count so that the buffer is freed immediately when the buffer
  78. * reference count falls to zero. If the buffer is already on the LRU, we need
  79. * to remove the reference that LRU holds on the buffer.
  80. *
  81. * This prevents build-up of stale buffers on the LRU.
  82. */
  83. void
  84. xfs_buf_stale(
  85. struct xfs_buf *bp)
  86. {
  87. ASSERT(xfs_buf_islocked(bp));
  88. bp->b_flags |= XBF_STALE;
  89. /*
  90. * Clear the delwri status so that a delwri queue walker will not
  91. * flush this buffer to disk now that it is stale. The delwri queue has
  92. * a reference to the buffer, so this is safe to do.
  93. */
  94. bp->b_flags &= ~_XBF_DELWRI_Q;
  95. spin_lock(&bp->b_lock);
  96. atomic_set(&bp->b_lru_ref, 0);
  97. if (!(bp->b_state & XFS_BSTATE_DISPOSE) &&
  98. (list_lru_del(&bp->b_target->bt_lru, &bp->b_lru)))
  99. atomic_dec(&bp->b_hold);
  100. ASSERT(atomic_read(&bp->b_hold) >= 1);
  101. spin_unlock(&bp->b_lock);
  102. }
  103. static int
  104. xfs_buf_get_maps(
  105. struct xfs_buf *bp,
  106. int map_count)
  107. {
  108. ASSERT(bp->b_maps == NULL);
  109. bp->b_map_count = map_count;
  110. if (map_count == 1) {
  111. bp->b_maps = &bp->__b_map;
  112. return 0;
  113. }
  114. bp->b_maps = kmem_zalloc(map_count * sizeof(struct xfs_buf_map),
  115. KM_NOFS);
  116. if (!bp->b_maps)
  117. return ENOMEM;
  118. return 0;
  119. }
  120. /*
  121. * Frees b_pages if it was allocated.
  122. */
  123. static void
  124. xfs_buf_free_maps(
  125. struct xfs_buf *bp)
  126. {
  127. if (bp->b_maps != &bp->__b_map) {
  128. kmem_free(bp->b_maps);
  129. bp->b_maps = NULL;
  130. }
  131. }
  132. struct xfs_buf *
  133. _xfs_buf_alloc(
  134. struct xfs_buftarg *target,
  135. struct xfs_buf_map *map,
  136. int nmaps,
  137. xfs_buf_flags_t flags)
  138. {
  139. struct xfs_buf *bp;
  140. int error;
  141. int i;
  142. bp = kmem_zone_zalloc(xfs_buf_zone, KM_NOFS);
  143. if (unlikely(!bp))
  144. return NULL;
  145. /*
  146. * We don't want certain flags to appear in b_flags unless they are
  147. * specifically set by later operations on the buffer.
  148. */
  149. flags &= ~(XBF_UNMAPPED | XBF_TRYLOCK | XBF_ASYNC | XBF_READ_AHEAD);
  150. atomic_set(&bp->b_hold, 1);
  151. atomic_set(&bp->b_lru_ref, 1);
  152. init_completion(&bp->b_iowait);
  153. INIT_LIST_HEAD(&bp->b_lru);
  154. INIT_LIST_HEAD(&bp->b_list);
  155. RB_CLEAR_NODE(&bp->b_rbnode);
  156. sema_init(&bp->b_sema, 0); /* held, no waiters */
  157. spin_lock_init(&bp->b_lock);
  158. XB_SET_OWNER(bp);
  159. bp->b_target = target;
  160. bp->b_flags = flags;
  161. /*
  162. * Set length and io_length to the same value initially.
  163. * I/O routines should use io_length, which will be the same in
  164. * most cases but may be reset (e.g. XFS recovery).
  165. */
  166. error = xfs_buf_get_maps(bp, nmaps);
  167. if (error) {
  168. kmem_zone_free(xfs_buf_zone, bp);
  169. return NULL;
  170. }
  171. bp->b_bn = map[0].bm_bn;
  172. bp->b_length = 0;
  173. for (i = 0; i < nmaps; i++) {
  174. bp->b_maps[i].bm_bn = map[i].bm_bn;
  175. bp->b_maps[i].bm_len = map[i].bm_len;
  176. bp->b_length += map[i].bm_len;
  177. }
  178. bp->b_io_length = bp->b_length;
  179. atomic_set(&bp->b_pin_count, 0);
  180. init_waitqueue_head(&bp->b_waiters);
  181. XFS_STATS_INC(xb_create);
  182. trace_xfs_buf_init(bp, _RET_IP_);
  183. return bp;
  184. }
  185. /*
  186. * Allocate a page array capable of holding a specified number
  187. * of pages, and point the page buf at it.
  188. */
  189. STATIC int
  190. _xfs_buf_get_pages(
  191. xfs_buf_t *bp,
  192. int page_count,
  193. xfs_buf_flags_t flags)
  194. {
  195. /* Make sure that we have a page list */
  196. if (bp->b_pages == NULL) {
  197. bp->b_page_count = page_count;
  198. if (page_count <= XB_PAGES) {
  199. bp->b_pages = bp->b_page_array;
  200. } else {
  201. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  202. page_count, KM_NOFS);
  203. if (bp->b_pages == NULL)
  204. return -ENOMEM;
  205. }
  206. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  207. }
  208. return 0;
  209. }
  210. /*
  211. * Frees b_pages if it was allocated.
  212. */
  213. STATIC void
  214. _xfs_buf_free_pages(
  215. xfs_buf_t *bp)
  216. {
  217. if (bp->b_pages != bp->b_page_array) {
  218. kmem_free(bp->b_pages);
  219. bp->b_pages = NULL;
  220. }
  221. }
  222. /*
  223. * Releases the specified buffer.
  224. *
  225. * The modification state of any associated pages is left unchanged.
  226. * The buffer must not be on any hash - use xfs_buf_rele instead for
  227. * hashed and refcounted buffers
  228. */
  229. void
  230. xfs_buf_free(
  231. xfs_buf_t *bp)
  232. {
  233. trace_xfs_buf_free(bp, _RET_IP_);
  234. ASSERT(list_empty(&bp->b_lru));
  235. if (bp->b_flags & _XBF_PAGES) {
  236. uint i;
  237. if (xfs_buf_is_vmapped(bp))
  238. vm_unmap_ram(bp->b_addr - bp->b_offset,
  239. bp->b_page_count);
  240. for (i = 0; i < bp->b_page_count; i++) {
  241. struct page *page = bp->b_pages[i];
  242. __free_page(page);
  243. }
  244. } else if (bp->b_flags & _XBF_KMEM)
  245. kmem_free(bp->b_addr);
  246. _xfs_buf_free_pages(bp);
  247. xfs_buf_free_maps(bp);
  248. kmem_zone_free(xfs_buf_zone, bp);
  249. }
  250. /*
  251. * Allocates all the pages for buffer in question and builds it's page list.
  252. */
  253. STATIC int
  254. xfs_buf_allocate_memory(
  255. xfs_buf_t *bp,
  256. uint flags)
  257. {
  258. size_t size;
  259. size_t nbytes, offset;
  260. gfp_t gfp_mask = xb_to_gfp(flags);
  261. unsigned short page_count, i;
  262. xfs_off_t start, end;
  263. int error;
  264. /*
  265. * for buffers that are contained within a single page, just allocate
  266. * the memory from the heap - there's no need for the complexity of
  267. * page arrays to keep allocation down to order 0.
  268. */
  269. size = BBTOB(bp->b_length);
  270. if (size < PAGE_SIZE) {
  271. bp->b_addr = kmem_alloc(size, KM_NOFS);
  272. if (!bp->b_addr) {
  273. /* low memory - use alloc_page loop instead */
  274. goto use_alloc_page;
  275. }
  276. if (((unsigned long)(bp->b_addr + size - 1) & PAGE_MASK) !=
  277. ((unsigned long)bp->b_addr & PAGE_MASK)) {
  278. /* b_addr spans two pages - use alloc_page instead */
  279. kmem_free(bp->b_addr);
  280. bp->b_addr = NULL;
  281. goto use_alloc_page;
  282. }
  283. bp->b_offset = offset_in_page(bp->b_addr);
  284. bp->b_pages = bp->b_page_array;
  285. bp->b_pages[0] = virt_to_page(bp->b_addr);
  286. bp->b_page_count = 1;
  287. bp->b_flags |= _XBF_KMEM;
  288. return 0;
  289. }
  290. use_alloc_page:
  291. start = BBTOB(bp->b_maps[0].bm_bn) >> PAGE_SHIFT;
  292. end = (BBTOB(bp->b_maps[0].bm_bn + bp->b_length) + PAGE_SIZE - 1)
  293. >> PAGE_SHIFT;
  294. page_count = end - start;
  295. error = _xfs_buf_get_pages(bp, page_count, flags);
  296. if (unlikely(error))
  297. return error;
  298. offset = bp->b_offset;
  299. bp->b_flags |= _XBF_PAGES;
  300. for (i = 0; i < bp->b_page_count; i++) {
  301. struct page *page;
  302. uint retries = 0;
  303. retry:
  304. page = alloc_page(gfp_mask);
  305. if (unlikely(page == NULL)) {
  306. if (flags & XBF_READ_AHEAD) {
  307. bp->b_page_count = i;
  308. error = ENOMEM;
  309. goto out_free_pages;
  310. }
  311. /*
  312. * This could deadlock.
  313. *
  314. * But until all the XFS lowlevel code is revamped to
  315. * handle buffer allocation failures we can't do much.
  316. */
  317. if (!(++retries % 100))
  318. xfs_err(NULL,
  319. "possible memory allocation deadlock in %s (mode:0x%x)",
  320. __func__, gfp_mask);
  321. XFS_STATS_INC(xb_page_retries);
  322. congestion_wait(BLK_RW_ASYNC, HZ/50);
  323. goto retry;
  324. }
  325. XFS_STATS_INC(xb_page_found);
  326. nbytes = min_t(size_t, size, PAGE_SIZE - offset);
  327. size -= nbytes;
  328. bp->b_pages[i] = page;
  329. offset = 0;
  330. }
  331. return 0;
  332. out_free_pages:
  333. for (i = 0; i < bp->b_page_count; i++)
  334. __free_page(bp->b_pages[i]);
  335. return error;
  336. }
  337. /*
  338. * Map buffer into kernel address-space if necessary.
  339. */
  340. STATIC int
  341. _xfs_buf_map_pages(
  342. xfs_buf_t *bp,
  343. uint flags)
  344. {
  345. ASSERT(bp->b_flags & _XBF_PAGES);
  346. if (bp->b_page_count == 1) {
  347. /* A single page buffer is always mappable */
  348. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  349. } else if (flags & XBF_UNMAPPED) {
  350. bp->b_addr = NULL;
  351. } else {
  352. int retried = 0;
  353. do {
  354. bp->b_addr = vm_map_ram(bp->b_pages, bp->b_page_count,
  355. -1, PAGE_KERNEL);
  356. if (bp->b_addr)
  357. break;
  358. vm_unmap_aliases();
  359. } while (retried++ <= 1);
  360. if (!bp->b_addr)
  361. return -ENOMEM;
  362. bp->b_addr += bp->b_offset;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * Finding and Reading Buffers
  368. */
  369. /*
  370. * Look up, and creates if absent, a lockable buffer for
  371. * a given range of an inode. The buffer is returned
  372. * locked. No I/O is implied by this call.
  373. */
  374. xfs_buf_t *
  375. _xfs_buf_find(
  376. struct xfs_buftarg *btp,
  377. struct xfs_buf_map *map,
  378. int nmaps,
  379. xfs_buf_flags_t flags,
  380. xfs_buf_t *new_bp)
  381. {
  382. size_t numbytes;
  383. struct xfs_perag *pag;
  384. struct rb_node **rbp;
  385. struct rb_node *parent;
  386. xfs_buf_t *bp;
  387. xfs_daddr_t blkno = map[0].bm_bn;
  388. xfs_daddr_t eofs;
  389. int numblks = 0;
  390. int i;
  391. for (i = 0; i < nmaps; i++)
  392. numblks += map[i].bm_len;
  393. numbytes = BBTOB(numblks);
  394. /* Check for IOs smaller than the sector size / not sector aligned */
  395. ASSERT(!(numbytes < (1 << btp->bt_sshift)));
  396. ASSERT(!(BBTOB(blkno) & (xfs_off_t)btp->bt_smask));
  397. /*
  398. * Corrupted block numbers can get through to here, unfortunately, so we
  399. * have to check that the buffer falls within the filesystem bounds.
  400. */
  401. eofs = XFS_FSB_TO_BB(btp->bt_mount, btp->bt_mount->m_sb.sb_dblocks);
  402. if (blkno >= eofs) {
  403. /*
  404. * XXX (dgc): we should really be returning EFSCORRUPTED here,
  405. * but none of the higher level infrastructure supports
  406. * returning a specific error on buffer lookup failures.
  407. */
  408. xfs_alert(btp->bt_mount,
  409. "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
  410. __func__, blkno, eofs);
  411. WARN_ON(1);
  412. return NULL;
  413. }
  414. /* get tree root */
  415. pag = xfs_perag_get(btp->bt_mount,
  416. xfs_daddr_to_agno(btp->bt_mount, blkno));
  417. /* walk tree */
  418. spin_lock(&pag->pag_buf_lock);
  419. rbp = &pag->pag_buf_tree.rb_node;
  420. parent = NULL;
  421. bp = NULL;
  422. while (*rbp) {
  423. parent = *rbp;
  424. bp = rb_entry(parent, struct xfs_buf, b_rbnode);
  425. if (blkno < bp->b_bn)
  426. rbp = &(*rbp)->rb_left;
  427. else if (blkno > bp->b_bn)
  428. rbp = &(*rbp)->rb_right;
  429. else {
  430. /*
  431. * found a block number match. If the range doesn't
  432. * match, the only way this is allowed is if the buffer
  433. * in the cache is stale and the transaction that made
  434. * it stale has not yet committed. i.e. we are
  435. * reallocating a busy extent. Skip this buffer and
  436. * continue searching to the right for an exact match.
  437. */
  438. if (bp->b_length != numblks) {
  439. ASSERT(bp->b_flags & XBF_STALE);
  440. rbp = &(*rbp)->rb_right;
  441. continue;
  442. }
  443. atomic_inc(&bp->b_hold);
  444. goto found;
  445. }
  446. }
  447. /* No match found */
  448. if (new_bp) {
  449. rb_link_node(&new_bp->b_rbnode, parent, rbp);
  450. rb_insert_color(&new_bp->b_rbnode, &pag->pag_buf_tree);
  451. /* the buffer keeps the perag reference until it is freed */
  452. new_bp->b_pag = pag;
  453. spin_unlock(&pag->pag_buf_lock);
  454. } else {
  455. XFS_STATS_INC(xb_miss_locked);
  456. spin_unlock(&pag->pag_buf_lock);
  457. xfs_perag_put(pag);
  458. }
  459. return new_bp;
  460. found:
  461. spin_unlock(&pag->pag_buf_lock);
  462. xfs_perag_put(pag);
  463. if (!xfs_buf_trylock(bp)) {
  464. if (flags & XBF_TRYLOCK) {
  465. xfs_buf_rele(bp);
  466. XFS_STATS_INC(xb_busy_locked);
  467. return NULL;
  468. }
  469. xfs_buf_lock(bp);
  470. XFS_STATS_INC(xb_get_locked_waited);
  471. }
  472. /*
  473. * if the buffer is stale, clear all the external state associated with
  474. * it. We need to keep flags such as how we allocated the buffer memory
  475. * intact here.
  476. */
  477. if (bp->b_flags & XBF_STALE) {
  478. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  479. ASSERT(bp->b_iodone == NULL);
  480. bp->b_flags &= _XBF_KMEM | _XBF_PAGES;
  481. bp->b_ops = NULL;
  482. }
  483. trace_xfs_buf_find(bp, flags, _RET_IP_);
  484. XFS_STATS_INC(xb_get_locked);
  485. return bp;
  486. }
  487. /*
  488. * Assembles a buffer covering the specified range. The code is optimised for
  489. * cache hits, as metadata intensive workloads will see 3 orders of magnitude
  490. * more hits than misses.
  491. */
  492. struct xfs_buf *
  493. xfs_buf_get_map(
  494. struct xfs_buftarg *target,
  495. struct xfs_buf_map *map,
  496. int nmaps,
  497. xfs_buf_flags_t flags)
  498. {
  499. struct xfs_buf *bp;
  500. struct xfs_buf *new_bp;
  501. int error = 0;
  502. bp = _xfs_buf_find(target, map, nmaps, flags, NULL);
  503. if (likely(bp))
  504. goto found;
  505. new_bp = _xfs_buf_alloc(target, map, nmaps, flags);
  506. if (unlikely(!new_bp))
  507. return NULL;
  508. error = xfs_buf_allocate_memory(new_bp, flags);
  509. if (error) {
  510. xfs_buf_free(new_bp);
  511. return NULL;
  512. }
  513. bp = _xfs_buf_find(target, map, nmaps, flags, new_bp);
  514. if (!bp) {
  515. xfs_buf_free(new_bp);
  516. return NULL;
  517. }
  518. if (bp != new_bp)
  519. xfs_buf_free(new_bp);
  520. found:
  521. if (!bp->b_addr) {
  522. error = _xfs_buf_map_pages(bp, flags);
  523. if (unlikely(error)) {
  524. xfs_warn(target->bt_mount,
  525. "%s: failed to map pagesn", __func__);
  526. xfs_buf_relse(bp);
  527. return NULL;
  528. }
  529. }
  530. XFS_STATS_INC(xb_get);
  531. trace_xfs_buf_get(bp, flags, _RET_IP_);
  532. return bp;
  533. }
  534. STATIC int
  535. _xfs_buf_read(
  536. xfs_buf_t *bp,
  537. xfs_buf_flags_t flags)
  538. {
  539. ASSERT(!(flags & XBF_WRITE));
  540. ASSERT(bp->b_maps[0].bm_bn != XFS_BUF_DADDR_NULL);
  541. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_READ_AHEAD);
  542. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | XBF_READ_AHEAD);
  543. xfs_buf_iorequest(bp);
  544. if (flags & XBF_ASYNC)
  545. return 0;
  546. return xfs_buf_iowait(bp);
  547. }
  548. xfs_buf_t *
  549. xfs_buf_read_map(
  550. struct xfs_buftarg *target,
  551. struct xfs_buf_map *map,
  552. int nmaps,
  553. xfs_buf_flags_t flags,
  554. const struct xfs_buf_ops *ops)
  555. {
  556. struct xfs_buf *bp;
  557. flags |= XBF_READ;
  558. bp = xfs_buf_get_map(target, map, nmaps, flags);
  559. if (bp) {
  560. trace_xfs_buf_read(bp, flags, _RET_IP_);
  561. if (!XFS_BUF_ISDONE(bp)) {
  562. XFS_STATS_INC(xb_get_read);
  563. bp->b_ops = ops;
  564. _xfs_buf_read(bp, flags);
  565. } else if (flags & XBF_ASYNC) {
  566. /*
  567. * Read ahead call which is already satisfied,
  568. * drop the buffer
  569. */
  570. xfs_buf_relse(bp);
  571. return NULL;
  572. } else {
  573. /* We do not want read in the flags */
  574. bp->b_flags &= ~XBF_READ;
  575. }
  576. }
  577. return bp;
  578. }
  579. /*
  580. * If we are not low on memory then do the readahead in a deadlock
  581. * safe manner.
  582. */
  583. void
  584. xfs_buf_readahead_map(
  585. struct xfs_buftarg *target,
  586. struct xfs_buf_map *map,
  587. int nmaps,
  588. const struct xfs_buf_ops *ops)
  589. {
  590. if (bdi_read_congested(target->bt_bdi))
  591. return;
  592. xfs_buf_read_map(target, map, nmaps,
  593. XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD, ops);
  594. }
  595. /*
  596. * Read an uncached buffer from disk. Allocates and returns a locked
  597. * buffer containing the disk contents or nothing.
  598. */
  599. struct xfs_buf *
  600. xfs_buf_read_uncached(
  601. struct xfs_buftarg *target,
  602. xfs_daddr_t daddr,
  603. size_t numblks,
  604. int flags,
  605. const struct xfs_buf_ops *ops)
  606. {
  607. struct xfs_buf *bp;
  608. bp = xfs_buf_get_uncached(target, numblks, flags);
  609. if (!bp)
  610. return NULL;
  611. /* set up the buffer for a read IO */
  612. ASSERT(bp->b_map_count == 1);
  613. bp->b_bn = daddr;
  614. bp->b_maps[0].bm_bn = daddr;
  615. bp->b_flags |= XBF_READ;
  616. bp->b_ops = ops;
  617. xfsbdstrat(target->bt_mount, bp);
  618. xfs_buf_iowait(bp);
  619. return bp;
  620. }
  621. /*
  622. * Return a buffer allocated as an empty buffer and associated to external
  623. * memory via xfs_buf_associate_memory() back to it's empty state.
  624. */
  625. void
  626. xfs_buf_set_empty(
  627. struct xfs_buf *bp,
  628. size_t numblks)
  629. {
  630. if (bp->b_pages)
  631. _xfs_buf_free_pages(bp);
  632. bp->b_pages = NULL;
  633. bp->b_page_count = 0;
  634. bp->b_addr = NULL;
  635. bp->b_length = numblks;
  636. bp->b_io_length = numblks;
  637. ASSERT(bp->b_map_count == 1);
  638. bp->b_bn = XFS_BUF_DADDR_NULL;
  639. bp->b_maps[0].bm_bn = XFS_BUF_DADDR_NULL;
  640. bp->b_maps[0].bm_len = bp->b_length;
  641. }
  642. static inline struct page *
  643. mem_to_page(
  644. void *addr)
  645. {
  646. if ((!is_vmalloc_addr(addr))) {
  647. return virt_to_page(addr);
  648. } else {
  649. return vmalloc_to_page(addr);
  650. }
  651. }
  652. int
  653. xfs_buf_associate_memory(
  654. xfs_buf_t *bp,
  655. void *mem,
  656. size_t len)
  657. {
  658. int rval;
  659. int i = 0;
  660. unsigned long pageaddr;
  661. unsigned long offset;
  662. size_t buflen;
  663. int page_count;
  664. pageaddr = (unsigned long)mem & PAGE_MASK;
  665. offset = (unsigned long)mem - pageaddr;
  666. buflen = PAGE_ALIGN(len + offset);
  667. page_count = buflen >> PAGE_SHIFT;
  668. /* Free any previous set of page pointers */
  669. if (bp->b_pages)
  670. _xfs_buf_free_pages(bp);
  671. bp->b_pages = NULL;
  672. bp->b_addr = mem;
  673. rval = _xfs_buf_get_pages(bp, page_count, 0);
  674. if (rval)
  675. return rval;
  676. bp->b_offset = offset;
  677. for (i = 0; i < bp->b_page_count; i++) {
  678. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  679. pageaddr += PAGE_SIZE;
  680. }
  681. bp->b_io_length = BTOBB(len);
  682. bp->b_length = BTOBB(buflen);
  683. return 0;
  684. }
  685. xfs_buf_t *
  686. xfs_buf_get_uncached(
  687. struct xfs_buftarg *target,
  688. size_t numblks,
  689. int flags)
  690. {
  691. unsigned long page_count;
  692. int error, i;
  693. struct xfs_buf *bp;
  694. DEFINE_SINGLE_BUF_MAP(map, XFS_BUF_DADDR_NULL, numblks);
  695. bp = _xfs_buf_alloc(target, &map, 1, 0);
  696. if (unlikely(bp == NULL))
  697. goto fail;
  698. page_count = PAGE_ALIGN(numblks << BBSHIFT) >> PAGE_SHIFT;
  699. error = _xfs_buf_get_pages(bp, page_count, 0);
  700. if (error)
  701. goto fail_free_buf;
  702. for (i = 0; i < page_count; i++) {
  703. bp->b_pages[i] = alloc_page(xb_to_gfp(flags));
  704. if (!bp->b_pages[i])
  705. goto fail_free_mem;
  706. }
  707. bp->b_flags |= _XBF_PAGES;
  708. error = _xfs_buf_map_pages(bp, 0);
  709. if (unlikely(error)) {
  710. xfs_warn(target->bt_mount,
  711. "%s: failed to map pages", __func__);
  712. goto fail_free_mem;
  713. }
  714. trace_xfs_buf_get_uncached(bp, _RET_IP_);
  715. return bp;
  716. fail_free_mem:
  717. while (--i >= 0)
  718. __free_page(bp->b_pages[i]);
  719. _xfs_buf_free_pages(bp);
  720. fail_free_buf:
  721. xfs_buf_free_maps(bp);
  722. kmem_zone_free(xfs_buf_zone, bp);
  723. fail:
  724. return NULL;
  725. }
  726. /*
  727. * Increment reference count on buffer, to hold the buffer concurrently
  728. * with another thread which may release (free) the buffer asynchronously.
  729. * Must hold the buffer already to call this function.
  730. */
  731. void
  732. xfs_buf_hold(
  733. xfs_buf_t *bp)
  734. {
  735. trace_xfs_buf_hold(bp, _RET_IP_);
  736. atomic_inc(&bp->b_hold);
  737. }
  738. /*
  739. * Releases a hold on the specified buffer. If the
  740. * the hold count is 1, calls xfs_buf_free.
  741. */
  742. void
  743. xfs_buf_rele(
  744. xfs_buf_t *bp)
  745. {
  746. struct xfs_perag *pag = bp->b_pag;
  747. trace_xfs_buf_rele(bp, _RET_IP_);
  748. if (!pag) {
  749. ASSERT(list_empty(&bp->b_lru));
  750. ASSERT(RB_EMPTY_NODE(&bp->b_rbnode));
  751. if (atomic_dec_and_test(&bp->b_hold))
  752. xfs_buf_free(bp);
  753. return;
  754. }
  755. ASSERT(!RB_EMPTY_NODE(&bp->b_rbnode));
  756. ASSERT(atomic_read(&bp->b_hold) > 0);
  757. if (atomic_dec_and_lock(&bp->b_hold, &pag->pag_buf_lock)) {
  758. spin_lock(&bp->b_lock);
  759. if (!(bp->b_flags & XBF_STALE) && atomic_read(&bp->b_lru_ref)) {
  760. /*
  761. * If the buffer is added to the LRU take a new
  762. * reference to the buffer for the LRU and clear the
  763. * (now stale) dispose list state flag
  764. */
  765. if (list_lru_add(&bp->b_target->bt_lru, &bp->b_lru)) {
  766. bp->b_state &= ~XFS_BSTATE_DISPOSE;
  767. atomic_inc(&bp->b_hold);
  768. }
  769. spin_unlock(&bp->b_lock);
  770. spin_unlock(&pag->pag_buf_lock);
  771. } else {
  772. /*
  773. * most of the time buffers will already be removed from
  774. * the LRU, so optimise that case by checking for the
  775. * XFS_BSTATE_DISPOSE flag indicating the last list the
  776. * buffer was on was the disposal list
  777. */
  778. if (!(bp->b_state & XFS_BSTATE_DISPOSE)) {
  779. list_lru_del(&bp->b_target->bt_lru, &bp->b_lru);
  780. } else {
  781. ASSERT(list_empty(&bp->b_lru));
  782. }
  783. spin_unlock(&bp->b_lock);
  784. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  785. rb_erase(&bp->b_rbnode, &pag->pag_buf_tree);
  786. spin_unlock(&pag->pag_buf_lock);
  787. xfs_perag_put(pag);
  788. xfs_buf_free(bp);
  789. }
  790. }
  791. }
  792. /*
  793. * Lock a buffer object, if it is not already locked.
  794. *
  795. * If we come across a stale, pinned, locked buffer, we know that we are
  796. * being asked to lock a buffer that has been reallocated. Because it is
  797. * pinned, we know that the log has not been pushed to disk and hence it
  798. * will still be locked. Rather than continuing to have trylock attempts
  799. * fail until someone else pushes the log, push it ourselves before
  800. * returning. This means that the xfsaild will not get stuck trying
  801. * to push on stale inode buffers.
  802. */
  803. int
  804. xfs_buf_trylock(
  805. struct xfs_buf *bp)
  806. {
  807. int locked;
  808. locked = down_trylock(&bp->b_sema) == 0;
  809. if (locked)
  810. XB_SET_OWNER(bp);
  811. trace_xfs_buf_trylock(bp, _RET_IP_);
  812. return locked;
  813. }
  814. /*
  815. * Lock a buffer object.
  816. *
  817. * If we come across a stale, pinned, locked buffer, we know that we
  818. * are being asked to lock a buffer that has been reallocated. Because
  819. * it is pinned, we know that the log has not been pushed to disk and
  820. * hence it will still be locked. Rather than sleeping until someone
  821. * else pushes the log, push it ourselves before trying to get the lock.
  822. */
  823. void
  824. xfs_buf_lock(
  825. struct xfs_buf *bp)
  826. {
  827. trace_xfs_buf_lock(bp, _RET_IP_);
  828. if (atomic_read(&bp->b_pin_count) && (bp->b_flags & XBF_STALE))
  829. xfs_log_force(bp->b_target->bt_mount, 0);
  830. down(&bp->b_sema);
  831. XB_SET_OWNER(bp);
  832. trace_xfs_buf_lock_done(bp, _RET_IP_);
  833. }
  834. void
  835. xfs_buf_unlock(
  836. struct xfs_buf *bp)
  837. {
  838. XB_CLEAR_OWNER(bp);
  839. up(&bp->b_sema);
  840. trace_xfs_buf_unlock(bp, _RET_IP_);
  841. }
  842. STATIC void
  843. xfs_buf_wait_unpin(
  844. xfs_buf_t *bp)
  845. {
  846. DECLARE_WAITQUEUE (wait, current);
  847. if (atomic_read(&bp->b_pin_count) == 0)
  848. return;
  849. add_wait_queue(&bp->b_waiters, &wait);
  850. for (;;) {
  851. set_current_state(TASK_UNINTERRUPTIBLE);
  852. if (atomic_read(&bp->b_pin_count) == 0)
  853. break;
  854. io_schedule();
  855. }
  856. remove_wait_queue(&bp->b_waiters, &wait);
  857. set_current_state(TASK_RUNNING);
  858. }
  859. /*
  860. * Buffer Utility Routines
  861. */
  862. STATIC void
  863. xfs_buf_iodone_work(
  864. struct work_struct *work)
  865. {
  866. struct xfs_buf *bp =
  867. container_of(work, xfs_buf_t, b_iodone_work);
  868. bool read = !!(bp->b_flags & XBF_READ);
  869. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  870. /* only validate buffers that were read without errors */
  871. if (read && bp->b_ops && !bp->b_error && (bp->b_flags & XBF_DONE))
  872. bp->b_ops->verify_read(bp);
  873. if (bp->b_iodone)
  874. (*(bp->b_iodone))(bp);
  875. else if (bp->b_flags & XBF_ASYNC)
  876. xfs_buf_relse(bp);
  877. else {
  878. ASSERT(read && bp->b_ops);
  879. complete(&bp->b_iowait);
  880. }
  881. }
  882. void
  883. xfs_buf_ioend(
  884. struct xfs_buf *bp,
  885. int schedule)
  886. {
  887. bool read = !!(bp->b_flags & XBF_READ);
  888. trace_xfs_buf_iodone(bp, _RET_IP_);
  889. if (bp->b_error == 0)
  890. bp->b_flags |= XBF_DONE;
  891. if (bp->b_iodone || (read && bp->b_ops) || (bp->b_flags & XBF_ASYNC)) {
  892. if (schedule) {
  893. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  894. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  895. } else {
  896. xfs_buf_iodone_work(&bp->b_iodone_work);
  897. }
  898. } else {
  899. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  900. complete(&bp->b_iowait);
  901. }
  902. }
  903. void
  904. xfs_buf_ioerror(
  905. xfs_buf_t *bp,
  906. int error)
  907. {
  908. ASSERT(error >= 0 && error <= 0xffff);
  909. bp->b_error = (unsigned short)error;
  910. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  911. }
  912. void
  913. xfs_buf_ioerror_alert(
  914. struct xfs_buf *bp,
  915. const char *func)
  916. {
  917. xfs_alert(bp->b_target->bt_mount,
  918. "metadata I/O error: block 0x%llx (\"%s\") error %d numblks %d",
  919. (__uint64_t)XFS_BUF_ADDR(bp), func, bp->b_error, bp->b_length);
  920. }
  921. /*
  922. * Called when we want to stop a buffer from getting written or read.
  923. * We attach the EIO error, muck with its flags, and call xfs_buf_ioend
  924. * so that the proper iodone callbacks get called.
  925. */
  926. STATIC int
  927. xfs_bioerror(
  928. xfs_buf_t *bp)
  929. {
  930. #ifdef XFSERRORDEBUG
  931. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  932. #endif
  933. /*
  934. * No need to wait until the buffer is unpinned, we aren't flushing it.
  935. */
  936. xfs_buf_ioerror(bp, EIO);
  937. /*
  938. * We're calling xfs_buf_ioend, so delete XBF_DONE flag.
  939. */
  940. XFS_BUF_UNREAD(bp);
  941. XFS_BUF_UNDONE(bp);
  942. xfs_buf_stale(bp);
  943. xfs_buf_ioend(bp, 0);
  944. return EIO;
  945. }
  946. /*
  947. * Same as xfs_bioerror, except that we are releasing the buffer
  948. * here ourselves, and avoiding the xfs_buf_ioend call.
  949. * This is meant for userdata errors; metadata bufs come with
  950. * iodone functions attached, so that we can track down errors.
  951. */
  952. STATIC int
  953. xfs_bioerror_relse(
  954. struct xfs_buf *bp)
  955. {
  956. int64_t fl = bp->b_flags;
  957. /*
  958. * No need to wait until the buffer is unpinned.
  959. * We aren't flushing it.
  960. *
  961. * chunkhold expects B_DONE to be set, whether
  962. * we actually finish the I/O or not. We don't want to
  963. * change that interface.
  964. */
  965. XFS_BUF_UNREAD(bp);
  966. XFS_BUF_DONE(bp);
  967. xfs_buf_stale(bp);
  968. bp->b_iodone = NULL;
  969. if (!(fl & XBF_ASYNC)) {
  970. /*
  971. * Mark b_error and B_ERROR _both_.
  972. * Lot's of chunkcache code assumes that.
  973. * There's no reason to mark error for
  974. * ASYNC buffers.
  975. */
  976. xfs_buf_ioerror(bp, EIO);
  977. complete(&bp->b_iowait);
  978. } else {
  979. xfs_buf_relse(bp);
  980. }
  981. return EIO;
  982. }
  983. STATIC int
  984. xfs_bdstrat_cb(
  985. struct xfs_buf *bp)
  986. {
  987. if (XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  988. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  989. /*
  990. * Metadata write that didn't get logged but
  991. * written delayed anyway. These aren't associated
  992. * with a transaction, and can be ignored.
  993. */
  994. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  995. return xfs_bioerror_relse(bp);
  996. else
  997. return xfs_bioerror(bp);
  998. }
  999. xfs_buf_iorequest(bp);
  1000. return 0;
  1001. }
  1002. int
  1003. xfs_bwrite(
  1004. struct xfs_buf *bp)
  1005. {
  1006. int error;
  1007. ASSERT(xfs_buf_islocked(bp));
  1008. bp->b_flags |= XBF_WRITE;
  1009. bp->b_flags &= ~(XBF_ASYNC | XBF_READ | _XBF_DELWRI_Q);
  1010. xfs_bdstrat_cb(bp);
  1011. error = xfs_buf_iowait(bp);
  1012. if (error) {
  1013. xfs_force_shutdown(bp->b_target->bt_mount,
  1014. SHUTDOWN_META_IO_ERROR);
  1015. }
  1016. return error;
  1017. }
  1018. /*
  1019. * Wrapper around bdstrat so that we can stop data from going to disk in case
  1020. * we are shutting down the filesystem. Typically user data goes thru this
  1021. * path; one of the exceptions is the superblock.
  1022. */
  1023. void
  1024. xfsbdstrat(
  1025. struct xfs_mount *mp,
  1026. struct xfs_buf *bp)
  1027. {
  1028. if (XFS_FORCED_SHUTDOWN(mp)) {
  1029. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1030. xfs_bioerror_relse(bp);
  1031. return;
  1032. }
  1033. xfs_buf_iorequest(bp);
  1034. }
  1035. STATIC void
  1036. _xfs_buf_ioend(
  1037. xfs_buf_t *bp,
  1038. int schedule)
  1039. {
  1040. if (atomic_dec_and_test(&bp->b_io_remaining) == 1)
  1041. xfs_buf_ioend(bp, schedule);
  1042. }
  1043. STATIC void
  1044. xfs_buf_bio_end_io(
  1045. struct bio *bio,
  1046. int error)
  1047. {
  1048. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1049. /*
  1050. * don't overwrite existing errors - otherwise we can lose errors on
  1051. * buffers that require multiple bios to complete.
  1052. */
  1053. if (!bp->b_error)
  1054. xfs_buf_ioerror(bp, -error);
  1055. if (!bp->b_error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1056. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1057. _xfs_buf_ioend(bp, 1);
  1058. bio_put(bio);
  1059. }
  1060. static void
  1061. xfs_buf_ioapply_map(
  1062. struct xfs_buf *bp,
  1063. int map,
  1064. int *buf_offset,
  1065. int *count,
  1066. int rw)
  1067. {
  1068. int page_index;
  1069. int total_nr_pages = bp->b_page_count;
  1070. int nr_pages;
  1071. struct bio *bio;
  1072. sector_t sector = bp->b_maps[map].bm_bn;
  1073. int size;
  1074. int offset;
  1075. total_nr_pages = bp->b_page_count;
  1076. /* skip the pages in the buffer before the start offset */
  1077. page_index = 0;
  1078. offset = *buf_offset;
  1079. while (offset >= PAGE_SIZE) {
  1080. page_index++;
  1081. offset -= PAGE_SIZE;
  1082. }
  1083. /*
  1084. * Limit the IO size to the length of the current vector, and update the
  1085. * remaining IO count for the next time around.
  1086. */
  1087. size = min_t(int, BBTOB(bp->b_maps[map].bm_len), *count);
  1088. *count -= size;
  1089. *buf_offset += size;
  1090. next_chunk:
  1091. atomic_inc(&bp->b_io_remaining);
  1092. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1093. if (nr_pages > total_nr_pages)
  1094. nr_pages = total_nr_pages;
  1095. bio = bio_alloc(GFP_NOIO, nr_pages);
  1096. bio->bi_bdev = bp->b_target->bt_bdev;
  1097. bio->bi_sector = sector;
  1098. bio->bi_end_io = xfs_buf_bio_end_io;
  1099. bio->bi_private = bp;
  1100. for (; size && nr_pages; nr_pages--, page_index++) {
  1101. int rbytes, nbytes = PAGE_SIZE - offset;
  1102. if (nbytes > size)
  1103. nbytes = size;
  1104. rbytes = bio_add_page(bio, bp->b_pages[page_index], nbytes,
  1105. offset);
  1106. if (rbytes < nbytes)
  1107. break;
  1108. offset = 0;
  1109. sector += BTOBB(nbytes);
  1110. size -= nbytes;
  1111. total_nr_pages--;
  1112. }
  1113. if (likely(bio->bi_size)) {
  1114. if (xfs_buf_is_vmapped(bp)) {
  1115. flush_kernel_vmap_range(bp->b_addr,
  1116. xfs_buf_vmap_len(bp));
  1117. }
  1118. submit_bio(rw, bio);
  1119. if (size)
  1120. goto next_chunk;
  1121. } else {
  1122. /*
  1123. * This is guaranteed not to be the last io reference count
  1124. * because the caller (xfs_buf_iorequest) holds a count itself.
  1125. */
  1126. atomic_dec(&bp->b_io_remaining);
  1127. xfs_buf_ioerror(bp, EIO);
  1128. bio_put(bio);
  1129. }
  1130. }
  1131. STATIC void
  1132. _xfs_buf_ioapply(
  1133. struct xfs_buf *bp)
  1134. {
  1135. struct blk_plug plug;
  1136. int rw;
  1137. int offset;
  1138. int size;
  1139. int i;
  1140. /*
  1141. * Make sure we capture only current IO errors rather than stale errors
  1142. * left over from previous use of the buffer (e.g. failed readahead).
  1143. */
  1144. bp->b_error = 0;
  1145. if (bp->b_flags & XBF_WRITE) {
  1146. if (bp->b_flags & XBF_SYNCIO)
  1147. rw = WRITE_SYNC;
  1148. else
  1149. rw = WRITE;
  1150. if (bp->b_flags & XBF_FUA)
  1151. rw |= REQ_FUA;
  1152. if (bp->b_flags & XBF_FLUSH)
  1153. rw |= REQ_FLUSH;
  1154. /*
  1155. * Run the write verifier callback function if it exists. If
  1156. * this function fails it will mark the buffer with an error and
  1157. * the IO should not be dispatched.
  1158. */
  1159. if (bp->b_ops) {
  1160. bp->b_ops->verify_write(bp);
  1161. if (bp->b_error) {
  1162. xfs_force_shutdown(bp->b_target->bt_mount,
  1163. SHUTDOWN_CORRUPT_INCORE);
  1164. return;
  1165. }
  1166. }
  1167. } else if (bp->b_flags & XBF_READ_AHEAD) {
  1168. rw = READA;
  1169. } else {
  1170. rw = READ;
  1171. }
  1172. /* we only use the buffer cache for meta-data */
  1173. rw |= REQ_META;
  1174. /*
  1175. * Walk all the vectors issuing IO on them. Set up the initial offset
  1176. * into the buffer and the desired IO size before we start -
  1177. * _xfs_buf_ioapply_vec() will modify them appropriately for each
  1178. * subsequent call.
  1179. */
  1180. offset = bp->b_offset;
  1181. size = BBTOB(bp->b_io_length);
  1182. blk_start_plug(&plug);
  1183. for (i = 0; i < bp->b_map_count; i++) {
  1184. xfs_buf_ioapply_map(bp, i, &offset, &size, rw);
  1185. if (bp->b_error)
  1186. break;
  1187. if (size <= 0)
  1188. break; /* all done */
  1189. }
  1190. blk_finish_plug(&plug);
  1191. }
  1192. void
  1193. xfs_buf_iorequest(
  1194. xfs_buf_t *bp)
  1195. {
  1196. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1197. ASSERT(!(bp->b_flags & _XBF_DELWRI_Q));
  1198. if (bp->b_flags & XBF_WRITE)
  1199. xfs_buf_wait_unpin(bp);
  1200. xfs_buf_hold(bp);
  1201. /* Set the count to 1 initially, this will stop an I/O
  1202. * completion callout which happens before we have started
  1203. * all the I/O from calling xfs_buf_ioend too early.
  1204. */
  1205. atomic_set(&bp->b_io_remaining, 1);
  1206. _xfs_buf_ioapply(bp);
  1207. _xfs_buf_ioend(bp, 1);
  1208. xfs_buf_rele(bp);
  1209. }
  1210. /*
  1211. * Waits for I/O to complete on the buffer supplied. It returns immediately if
  1212. * no I/O is pending or there is already a pending error on the buffer. It
  1213. * returns the I/O error code, if any, or 0 if there was no error.
  1214. */
  1215. int
  1216. xfs_buf_iowait(
  1217. xfs_buf_t *bp)
  1218. {
  1219. trace_xfs_buf_iowait(bp, _RET_IP_);
  1220. if (!bp->b_error)
  1221. wait_for_completion(&bp->b_iowait);
  1222. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1223. return bp->b_error;
  1224. }
  1225. xfs_caddr_t
  1226. xfs_buf_offset(
  1227. xfs_buf_t *bp,
  1228. size_t offset)
  1229. {
  1230. struct page *page;
  1231. if (bp->b_addr)
  1232. return bp->b_addr + offset;
  1233. offset += bp->b_offset;
  1234. page = bp->b_pages[offset >> PAGE_SHIFT];
  1235. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_SIZE-1));
  1236. }
  1237. /*
  1238. * Move data into or out of a buffer.
  1239. */
  1240. void
  1241. xfs_buf_iomove(
  1242. xfs_buf_t *bp, /* buffer to process */
  1243. size_t boff, /* starting buffer offset */
  1244. size_t bsize, /* length to copy */
  1245. void *data, /* data address */
  1246. xfs_buf_rw_t mode) /* read/write/zero flag */
  1247. {
  1248. size_t bend;
  1249. bend = boff + bsize;
  1250. while (boff < bend) {
  1251. struct page *page;
  1252. int page_index, page_offset, csize;
  1253. page_index = (boff + bp->b_offset) >> PAGE_SHIFT;
  1254. page_offset = (boff + bp->b_offset) & ~PAGE_MASK;
  1255. page = bp->b_pages[page_index];
  1256. csize = min_t(size_t, PAGE_SIZE - page_offset,
  1257. BBTOB(bp->b_io_length) - boff);
  1258. ASSERT((csize + page_offset) <= PAGE_SIZE);
  1259. switch (mode) {
  1260. case XBRW_ZERO:
  1261. memset(page_address(page) + page_offset, 0, csize);
  1262. break;
  1263. case XBRW_READ:
  1264. memcpy(data, page_address(page) + page_offset, csize);
  1265. break;
  1266. case XBRW_WRITE:
  1267. memcpy(page_address(page) + page_offset, data, csize);
  1268. }
  1269. boff += csize;
  1270. data += csize;
  1271. }
  1272. }
  1273. /*
  1274. * Handling of buffer targets (buftargs).
  1275. */
  1276. /*
  1277. * Wait for any bufs with callbacks that have been submitted but have not yet
  1278. * returned. These buffers will have an elevated hold count, so wait on those
  1279. * while freeing all the buffers only held by the LRU.
  1280. */
  1281. static enum lru_status
  1282. xfs_buftarg_wait_rele(
  1283. struct list_head *item,
  1284. spinlock_t *lru_lock,
  1285. void *arg)
  1286. {
  1287. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1288. struct list_head *dispose = arg;
  1289. if (atomic_read(&bp->b_hold) > 1) {
  1290. /* need to wait, so skip it this pass */
  1291. trace_xfs_buf_wait_buftarg(bp, _RET_IP_);
  1292. return LRU_SKIP;
  1293. }
  1294. if (!spin_trylock(&bp->b_lock))
  1295. return LRU_SKIP;
  1296. /*
  1297. * clear the LRU reference count so the buffer doesn't get
  1298. * ignored in xfs_buf_rele().
  1299. */
  1300. atomic_set(&bp->b_lru_ref, 0);
  1301. bp->b_state |= XFS_BSTATE_DISPOSE;
  1302. list_move(item, dispose);
  1303. spin_unlock(&bp->b_lock);
  1304. return LRU_REMOVED;
  1305. }
  1306. void
  1307. xfs_wait_buftarg(
  1308. struct xfs_buftarg *btp)
  1309. {
  1310. LIST_HEAD(dispose);
  1311. int loop = 0;
  1312. /* loop until there is nothing left on the lru list. */
  1313. while (list_lru_count(&btp->bt_lru)) {
  1314. list_lru_walk(&btp->bt_lru, xfs_buftarg_wait_rele,
  1315. &dispose, LONG_MAX);
  1316. while (!list_empty(&dispose)) {
  1317. struct xfs_buf *bp;
  1318. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1319. list_del_init(&bp->b_lru);
  1320. xfs_buf_rele(bp);
  1321. }
  1322. if (loop++ != 0)
  1323. delay(100);
  1324. }
  1325. }
  1326. static enum lru_status
  1327. xfs_buftarg_isolate(
  1328. struct list_head *item,
  1329. spinlock_t *lru_lock,
  1330. void *arg)
  1331. {
  1332. struct xfs_buf *bp = container_of(item, struct xfs_buf, b_lru);
  1333. struct list_head *dispose = arg;
  1334. /*
  1335. * we are inverting the lru lock/bp->b_lock here, so use a trylock.
  1336. * If we fail to get the lock, just skip it.
  1337. */
  1338. if (!spin_trylock(&bp->b_lock))
  1339. return LRU_SKIP;
  1340. /*
  1341. * Decrement the b_lru_ref count unless the value is already
  1342. * zero. If the value is already zero, we need to reclaim the
  1343. * buffer, otherwise it gets another trip through the LRU.
  1344. */
  1345. if (!atomic_add_unless(&bp->b_lru_ref, -1, 0)) {
  1346. spin_unlock(&bp->b_lock);
  1347. return LRU_ROTATE;
  1348. }
  1349. bp->b_state |= XFS_BSTATE_DISPOSE;
  1350. list_move(item, dispose);
  1351. spin_unlock(&bp->b_lock);
  1352. return LRU_REMOVED;
  1353. }
  1354. static unsigned long
  1355. xfs_buftarg_shrink_scan(
  1356. struct shrinker *shrink,
  1357. struct shrink_control *sc)
  1358. {
  1359. struct xfs_buftarg *btp = container_of(shrink,
  1360. struct xfs_buftarg, bt_shrinker);
  1361. LIST_HEAD(dispose);
  1362. unsigned long freed;
  1363. unsigned long nr_to_scan = sc->nr_to_scan;
  1364. freed = list_lru_walk_node(&btp->bt_lru, sc->nid, xfs_buftarg_isolate,
  1365. &dispose, &nr_to_scan);
  1366. while (!list_empty(&dispose)) {
  1367. struct xfs_buf *bp;
  1368. bp = list_first_entry(&dispose, struct xfs_buf, b_lru);
  1369. list_del_init(&bp->b_lru);
  1370. xfs_buf_rele(bp);
  1371. }
  1372. return freed;
  1373. }
  1374. static unsigned long
  1375. xfs_buftarg_shrink_count(
  1376. struct shrinker *shrink,
  1377. struct shrink_control *sc)
  1378. {
  1379. struct xfs_buftarg *btp = container_of(shrink,
  1380. struct xfs_buftarg, bt_shrinker);
  1381. return list_lru_count_node(&btp->bt_lru, sc->nid);
  1382. }
  1383. void
  1384. xfs_free_buftarg(
  1385. struct xfs_mount *mp,
  1386. struct xfs_buftarg *btp)
  1387. {
  1388. unregister_shrinker(&btp->bt_shrinker);
  1389. list_lru_destroy(&btp->bt_lru);
  1390. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1391. xfs_blkdev_issue_flush(btp);
  1392. kmem_free(btp);
  1393. }
  1394. STATIC int
  1395. xfs_setsize_buftarg_flags(
  1396. xfs_buftarg_t *btp,
  1397. unsigned int blocksize,
  1398. unsigned int sectorsize,
  1399. int verbose)
  1400. {
  1401. btp->bt_bsize = blocksize;
  1402. btp->bt_sshift = ffs(sectorsize) - 1;
  1403. btp->bt_smask = sectorsize - 1;
  1404. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1405. char name[BDEVNAME_SIZE];
  1406. bdevname(btp->bt_bdev, name);
  1407. xfs_warn(btp->bt_mount,
  1408. "Cannot set_blocksize to %u on device %s",
  1409. sectorsize, name);
  1410. return EINVAL;
  1411. }
  1412. return 0;
  1413. }
  1414. /*
  1415. * When allocating the initial buffer target we have not yet
  1416. * read in the superblock, so don't know what sized sectors
  1417. * are being used at this early stage. Play safe.
  1418. */
  1419. STATIC int
  1420. xfs_setsize_buftarg_early(
  1421. xfs_buftarg_t *btp,
  1422. struct block_device *bdev)
  1423. {
  1424. return xfs_setsize_buftarg_flags(btp,
  1425. PAGE_SIZE, bdev_logical_block_size(bdev), 0);
  1426. }
  1427. int
  1428. xfs_setsize_buftarg(
  1429. xfs_buftarg_t *btp,
  1430. unsigned int blocksize,
  1431. unsigned int sectorsize)
  1432. {
  1433. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1434. }
  1435. xfs_buftarg_t *
  1436. xfs_alloc_buftarg(
  1437. struct xfs_mount *mp,
  1438. struct block_device *bdev,
  1439. int external,
  1440. const char *fsname)
  1441. {
  1442. xfs_buftarg_t *btp;
  1443. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP | KM_NOFS);
  1444. btp->bt_mount = mp;
  1445. btp->bt_dev = bdev->bd_dev;
  1446. btp->bt_bdev = bdev;
  1447. btp->bt_bdi = blk_get_backing_dev_info(bdev);
  1448. if (!btp->bt_bdi)
  1449. goto error;
  1450. if (xfs_setsize_buftarg_early(btp, bdev))
  1451. goto error;
  1452. if (list_lru_init(&btp->bt_lru))
  1453. goto error;
  1454. btp->bt_shrinker.count_objects = xfs_buftarg_shrink_count;
  1455. btp->bt_shrinker.scan_objects = xfs_buftarg_shrink_scan;
  1456. btp->bt_shrinker.seeks = DEFAULT_SEEKS;
  1457. btp->bt_shrinker.flags = SHRINKER_NUMA_AWARE;
  1458. register_shrinker(&btp->bt_shrinker);
  1459. return btp;
  1460. error:
  1461. kmem_free(btp);
  1462. return NULL;
  1463. }
  1464. /*
  1465. * Add a buffer to the delayed write list.
  1466. *
  1467. * This queues a buffer for writeout if it hasn't already been. Note that
  1468. * neither this routine nor the buffer list submission functions perform
  1469. * any internal synchronization. It is expected that the lists are thread-local
  1470. * to the callers.
  1471. *
  1472. * Returns true if we queued up the buffer, or false if it already had
  1473. * been on the buffer list.
  1474. */
  1475. bool
  1476. xfs_buf_delwri_queue(
  1477. struct xfs_buf *bp,
  1478. struct list_head *list)
  1479. {
  1480. ASSERT(xfs_buf_islocked(bp));
  1481. ASSERT(!(bp->b_flags & XBF_READ));
  1482. /*
  1483. * If the buffer is already marked delwri it already is queued up
  1484. * by someone else for imediate writeout. Just ignore it in that
  1485. * case.
  1486. */
  1487. if (bp->b_flags & _XBF_DELWRI_Q) {
  1488. trace_xfs_buf_delwri_queued(bp, _RET_IP_);
  1489. return false;
  1490. }
  1491. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1492. /*
  1493. * If a buffer gets written out synchronously or marked stale while it
  1494. * is on a delwri list we lazily remove it. To do this, the other party
  1495. * clears the _XBF_DELWRI_Q flag but otherwise leaves the buffer alone.
  1496. * It remains referenced and on the list. In a rare corner case it
  1497. * might get readded to a delwri list after the synchronous writeout, in
  1498. * which case we need just need to re-add the flag here.
  1499. */
  1500. bp->b_flags |= _XBF_DELWRI_Q;
  1501. if (list_empty(&bp->b_list)) {
  1502. atomic_inc(&bp->b_hold);
  1503. list_add_tail(&bp->b_list, list);
  1504. }
  1505. return true;
  1506. }
  1507. /*
  1508. * Compare function is more complex than it needs to be because
  1509. * the return value is only 32 bits and we are doing comparisons
  1510. * on 64 bit values
  1511. */
  1512. static int
  1513. xfs_buf_cmp(
  1514. void *priv,
  1515. struct list_head *a,
  1516. struct list_head *b)
  1517. {
  1518. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1519. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1520. xfs_daddr_t diff;
  1521. diff = ap->b_maps[0].bm_bn - bp->b_maps[0].bm_bn;
  1522. if (diff < 0)
  1523. return -1;
  1524. if (diff > 0)
  1525. return 1;
  1526. return 0;
  1527. }
  1528. static int
  1529. __xfs_buf_delwri_submit(
  1530. struct list_head *buffer_list,
  1531. struct list_head *io_list,
  1532. bool wait)
  1533. {
  1534. struct blk_plug plug;
  1535. struct xfs_buf *bp, *n;
  1536. int pinned = 0;
  1537. list_for_each_entry_safe(bp, n, buffer_list, b_list) {
  1538. if (!wait) {
  1539. if (xfs_buf_ispinned(bp)) {
  1540. pinned++;
  1541. continue;
  1542. }
  1543. if (!xfs_buf_trylock(bp))
  1544. continue;
  1545. } else {
  1546. xfs_buf_lock(bp);
  1547. }
  1548. /*
  1549. * Someone else might have written the buffer synchronously or
  1550. * marked it stale in the meantime. In that case only the
  1551. * _XBF_DELWRI_Q flag got cleared, and we have to drop the
  1552. * reference and remove it from the list here.
  1553. */
  1554. if (!(bp->b_flags & _XBF_DELWRI_Q)) {
  1555. list_del_init(&bp->b_list);
  1556. xfs_buf_relse(bp);
  1557. continue;
  1558. }
  1559. list_move_tail(&bp->b_list, io_list);
  1560. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1561. }
  1562. list_sort(NULL, io_list, xfs_buf_cmp);
  1563. blk_start_plug(&plug);
  1564. list_for_each_entry_safe(bp, n, io_list, b_list) {
  1565. bp->b_flags &= ~(_XBF_DELWRI_Q | XBF_ASYNC);
  1566. bp->b_flags |= XBF_WRITE;
  1567. if (!wait) {
  1568. bp->b_flags |= XBF_ASYNC;
  1569. list_del_init(&bp->b_list);
  1570. }
  1571. xfs_bdstrat_cb(bp);
  1572. }
  1573. blk_finish_plug(&plug);
  1574. return pinned;
  1575. }
  1576. /*
  1577. * Write out a buffer list asynchronously.
  1578. *
  1579. * This will take the @buffer_list, write all non-locked and non-pinned buffers
  1580. * out and not wait for I/O completion on any of the buffers. This interface
  1581. * is only safely useable for callers that can track I/O completion by higher
  1582. * level means, e.g. AIL pushing as the @buffer_list is consumed in this
  1583. * function.
  1584. */
  1585. int
  1586. xfs_buf_delwri_submit_nowait(
  1587. struct list_head *buffer_list)
  1588. {
  1589. LIST_HEAD (io_list);
  1590. return __xfs_buf_delwri_submit(buffer_list, &io_list, false);
  1591. }
  1592. /*
  1593. * Write out a buffer list synchronously.
  1594. *
  1595. * This will take the @buffer_list, write all buffers out and wait for I/O
  1596. * completion on all of the buffers. @buffer_list is consumed by the function,
  1597. * so callers must have some other way of tracking buffers if they require such
  1598. * functionality.
  1599. */
  1600. int
  1601. xfs_buf_delwri_submit(
  1602. struct list_head *buffer_list)
  1603. {
  1604. LIST_HEAD (io_list);
  1605. int error = 0, error2;
  1606. struct xfs_buf *bp;
  1607. __xfs_buf_delwri_submit(buffer_list, &io_list, true);
  1608. /* Wait for IO to complete. */
  1609. while (!list_empty(&io_list)) {
  1610. bp = list_first_entry(&io_list, struct xfs_buf, b_list);
  1611. list_del_init(&bp->b_list);
  1612. error2 = xfs_buf_iowait(bp);
  1613. xfs_buf_relse(bp);
  1614. if (!error)
  1615. error = error2;
  1616. }
  1617. return error;
  1618. }
  1619. int __init
  1620. xfs_buf_init(void)
  1621. {
  1622. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1623. KM_ZONE_HWALIGN, NULL);
  1624. if (!xfs_buf_zone)
  1625. goto out;
  1626. xfslogd_workqueue = alloc_workqueue("xfslogd",
  1627. WQ_MEM_RECLAIM | WQ_HIGHPRI, 1);
  1628. if (!xfslogd_workqueue)
  1629. goto out_free_buf_zone;
  1630. return 0;
  1631. out_free_buf_zone:
  1632. kmem_zone_destroy(xfs_buf_zone);
  1633. out:
  1634. return -ENOMEM;
  1635. }
  1636. void
  1637. xfs_buf_terminate(void)
  1638. {
  1639. destroy_workqueue(xfslogd_workqueue);
  1640. kmem_zone_destroy(xfs_buf_zone);
  1641. }