xfs_buf.c 41 KB

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