xfs_buf.c 40 KB

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