xfs_buf.c 37 KB

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