xfs_buf.c 41 KB

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