xfs_buf.c 38 KB

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