xfs_buf.c 38 KB

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