xfs_buf.c 38 KB

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