xfs_buf.c 38 KB

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