xfs_buf.c 40 KB

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