xfs_buf.c 41 KB

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