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