xfs_buf.c 38 KB

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