xfs_buf.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  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/slab.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 <linux/list_sort.h>
  37. #include "xfs_sb.h"
  38. #include "xfs_inum.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_dmapi.h"
  41. #include "xfs_mount.h"
  42. #include "xfs_trace.h"
  43. static kmem_zone_t *xfs_buf_zone;
  44. STATIC int xfsbufd(void *);
  45. STATIC int xfsbufd_wakeup(int, gfp_t);
  46. STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
  47. static struct shrinker xfs_buf_shake = {
  48. .shrink = xfsbufd_wakeup,
  49. .seeks = DEFAULT_SEEKS,
  50. };
  51. static struct workqueue_struct *xfslogd_workqueue;
  52. struct workqueue_struct *xfsdatad_workqueue;
  53. struct workqueue_struct *xfsconvertd_workqueue;
  54. #ifdef XFS_BUF_LOCK_TRACKING
  55. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  56. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  57. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  58. #else
  59. # define XB_SET_OWNER(bp) do { } while (0)
  60. # define XB_CLEAR_OWNER(bp) do { } while (0)
  61. # define XB_GET_OWNER(bp) do { } while (0)
  62. #endif
  63. #define xb_to_gfp(flags) \
  64. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  65. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  66. #define xb_to_km(flags) \
  67. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  68. #define xfs_buf_allocate(flags) \
  69. kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
  70. #define xfs_buf_deallocate(bp) \
  71. kmem_zone_free(xfs_buf_zone, (bp));
  72. static inline int
  73. xfs_buf_is_vmapped(
  74. struct xfs_buf *bp)
  75. {
  76. /*
  77. * Return true if the buffer is vmapped.
  78. *
  79. * The XBF_MAPPED flag is set if the buffer should be mapped, but the
  80. * code is clever enough to know it doesn't have to map a single page,
  81. * so the check has to be both for XBF_MAPPED and bp->b_page_count > 1.
  82. */
  83. return (bp->b_flags & XBF_MAPPED) && bp->b_page_count > 1;
  84. }
  85. static inline int
  86. xfs_buf_vmap_len(
  87. struct xfs_buf *bp)
  88. {
  89. return (bp->b_page_count * PAGE_SIZE) - bp->b_offset;
  90. }
  91. /*
  92. * Page Region interfaces.
  93. *
  94. * For pages in filesystems where the blocksize is smaller than the
  95. * pagesize, we use the page->private field (long) to hold a bitmap
  96. * of uptodate regions within the page.
  97. *
  98. * Each such region is "bytes per page / bits per long" bytes long.
  99. *
  100. * NBPPR == number-of-bytes-per-page-region
  101. * BTOPR == bytes-to-page-region (rounded up)
  102. * BTOPRT == bytes-to-page-region-truncated (rounded down)
  103. */
  104. #if (BITS_PER_LONG == 32)
  105. #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
  106. #elif (BITS_PER_LONG == 64)
  107. #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
  108. #else
  109. #error BITS_PER_LONG must be 32 or 64
  110. #endif
  111. #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
  112. #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
  113. #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
  114. STATIC unsigned long
  115. page_region_mask(
  116. size_t offset,
  117. size_t length)
  118. {
  119. unsigned long mask;
  120. int first, final;
  121. first = BTOPR(offset);
  122. final = BTOPRT(offset + length - 1);
  123. first = min(first, final);
  124. mask = ~0UL;
  125. mask <<= BITS_PER_LONG - (final - first);
  126. mask >>= BITS_PER_LONG - (final);
  127. ASSERT(offset + length <= PAGE_CACHE_SIZE);
  128. ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
  129. return mask;
  130. }
  131. STATIC void
  132. set_page_region(
  133. struct page *page,
  134. size_t offset,
  135. size_t length)
  136. {
  137. set_page_private(page,
  138. page_private(page) | page_region_mask(offset, length));
  139. if (page_private(page) == ~0UL)
  140. SetPageUptodate(page);
  141. }
  142. STATIC int
  143. test_page_region(
  144. struct page *page,
  145. size_t offset,
  146. size_t length)
  147. {
  148. unsigned long mask = page_region_mask(offset, length);
  149. return (mask && (page_private(page) & mask) == mask);
  150. }
  151. /*
  152. * Internal xfs_buf_t object manipulation
  153. */
  154. STATIC void
  155. _xfs_buf_initialize(
  156. xfs_buf_t *bp,
  157. xfs_buftarg_t *target,
  158. xfs_off_t range_base,
  159. size_t range_length,
  160. xfs_buf_flags_t flags)
  161. {
  162. /*
  163. * We don't want certain flags to appear in b_flags.
  164. */
  165. flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  166. memset(bp, 0, sizeof(xfs_buf_t));
  167. atomic_set(&bp->b_hold, 1);
  168. init_completion(&bp->b_iowait);
  169. INIT_LIST_HEAD(&bp->b_list);
  170. INIT_LIST_HEAD(&bp->b_hash_list);
  171. init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
  172. XB_SET_OWNER(bp);
  173. bp->b_target = target;
  174. bp->b_file_offset = range_base;
  175. /*
  176. * Set buffer_length and count_desired to the same value initially.
  177. * I/O routines should use count_desired, which will be the same in
  178. * most cases but may be reset (e.g. XFS recovery).
  179. */
  180. bp->b_buffer_length = bp->b_count_desired = range_length;
  181. bp->b_flags = flags;
  182. bp->b_bn = XFS_BUF_DADDR_NULL;
  183. atomic_set(&bp->b_pin_count, 0);
  184. init_waitqueue_head(&bp->b_waiters);
  185. XFS_STATS_INC(xb_create);
  186. trace_xfs_buf_init(bp, _RET_IP_);
  187. }
  188. /*
  189. * Allocate a page array capable of holding a specified number
  190. * of pages, and point the page buf at it.
  191. */
  192. STATIC int
  193. _xfs_buf_get_pages(
  194. xfs_buf_t *bp,
  195. int page_count,
  196. xfs_buf_flags_t flags)
  197. {
  198. /* Make sure that we have a page list */
  199. if (bp->b_pages == NULL) {
  200. bp->b_offset = xfs_buf_poff(bp->b_file_offset);
  201. bp->b_page_count = page_count;
  202. if (page_count <= XB_PAGES) {
  203. bp->b_pages = bp->b_page_array;
  204. } else {
  205. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  206. page_count, xb_to_km(flags));
  207. if (bp->b_pages == NULL)
  208. return -ENOMEM;
  209. }
  210. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Frees b_pages if it was allocated.
  216. */
  217. STATIC void
  218. _xfs_buf_free_pages(
  219. xfs_buf_t *bp)
  220. {
  221. if (bp->b_pages != bp->b_page_array) {
  222. kmem_free(bp->b_pages);
  223. bp->b_pages = NULL;
  224. }
  225. }
  226. /*
  227. * Releases the specified buffer.
  228. *
  229. * The modification state of any associated pages is left unchanged.
  230. * The buffer most not be on any hash - use xfs_buf_rele instead for
  231. * hashed and refcounted buffers
  232. */
  233. void
  234. xfs_buf_free(
  235. xfs_buf_t *bp)
  236. {
  237. trace_xfs_buf_free(bp, _RET_IP_);
  238. ASSERT(list_empty(&bp->b_hash_list));
  239. if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
  240. uint i;
  241. if (xfs_buf_is_vmapped(bp))
  242. vunmap(bp->b_addr - bp->b_offset);
  243. for (i = 0; i < bp->b_page_count; i++) {
  244. struct page *page = bp->b_pages[i];
  245. if (bp->b_flags & _XBF_PAGE_CACHE)
  246. ASSERT(!PagePrivate(page));
  247. page_cache_release(page);
  248. }
  249. }
  250. _xfs_buf_free_pages(bp);
  251. xfs_buf_deallocate(bp);
  252. }
  253. /*
  254. * Finds all pages for buffer in question and builds it's page list.
  255. */
  256. STATIC int
  257. _xfs_buf_lookup_pages(
  258. xfs_buf_t *bp,
  259. uint flags)
  260. {
  261. struct address_space *mapping = bp->b_target->bt_mapping;
  262. size_t blocksize = bp->b_target->bt_bsize;
  263. size_t size = bp->b_count_desired;
  264. size_t nbytes, offset;
  265. gfp_t gfp_mask = xb_to_gfp(flags);
  266. unsigned short page_count, i;
  267. pgoff_t first;
  268. xfs_off_t end;
  269. int error;
  270. end = bp->b_file_offset + bp->b_buffer_length;
  271. page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
  272. error = _xfs_buf_get_pages(bp, page_count, flags);
  273. if (unlikely(error))
  274. return error;
  275. bp->b_flags |= _XBF_PAGE_CACHE;
  276. offset = bp->b_offset;
  277. first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
  278. for (i = 0; i < bp->b_page_count; i++) {
  279. struct page *page;
  280. uint retries = 0;
  281. retry:
  282. page = find_or_create_page(mapping, first + i, gfp_mask);
  283. if (unlikely(page == NULL)) {
  284. if (flags & XBF_READ_AHEAD) {
  285. bp->b_page_count = i;
  286. for (i = 0; i < bp->b_page_count; i++)
  287. unlock_page(bp->b_pages[i]);
  288. return -ENOMEM;
  289. }
  290. /*
  291. * This could deadlock.
  292. *
  293. * But until all the XFS lowlevel code is revamped to
  294. * handle buffer allocation failures we can't do much.
  295. */
  296. if (!(++retries % 100))
  297. printk(KERN_ERR
  298. "XFS: possible memory allocation "
  299. "deadlock in %s (mode:0x%x)\n",
  300. __func__, gfp_mask);
  301. XFS_STATS_INC(xb_page_retries);
  302. xfsbufd_wakeup(0, gfp_mask);
  303. congestion_wait(BLK_RW_ASYNC, HZ/50);
  304. goto retry;
  305. }
  306. XFS_STATS_INC(xb_page_found);
  307. nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
  308. size -= nbytes;
  309. ASSERT(!PagePrivate(page));
  310. if (!PageUptodate(page)) {
  311. page_count--;
  312. if (blocksize >= PAGE_CACHE_SIZE) {
  313. if (flags & XBF_READ)
  314. bp->b_flags |= _XBF_PAGE_LOCKED;
  315. } else if (!PagePrivate(page)) {
  316. if (test_page_region(page, offset, nbytes))
  317. page_count++;
  318. }
  319. }
  320. bp->b_pages[i] = page;
  321. offset = 0;
  322. }
  323. if (!(bp->b_flags & _XBF_PAGE_LOCKED)) {
  324. for (i = 0; i < bp->b_page_count; i++)
  325. unlock_page(bp->b_pages[i]);
  326. }
  327. if (page_count == bp->b_page_count)
  328. bp->b_flags |= XBF_DONE;
  329. return error;
  330. }
  331. /*
  332. * Map buffer into kernel address-space if nessecary.
  333. */
  334. STATIC int
  335. _xfs_buf_map_pages(
  336. xfs_buf_t *bp,
  337. uint flags)
  338. {
  339. /* A single page buffer is always mappable */
  340. if (bp->b_page_count == 1) {
  341. bp->b_addr = page_address(bp->b_pages[0]) + bp->b_offset;
  342. bp->b_flags |= XBF_MAPPED;
  343. } else if (flags & XBF_MAPPED) {
  344. bp->b_addr = vmap(bp->b_pages, bp->b_page_count,
  345. VM_MAP, PAGE_KERNEL);
  346. if (unlikely(bp->b_addr == NULL))
  347. return -ENOMEM;
  348. bp->b_addr += bp->b_offset;
  349. bp->b_flags |= XBF_MAPPED;
  350. }
  351. return 0;
  352. }
  353. /*
  354. * Finding and Reading Buffers
  355. */
  356. /*
  357. * Look up, and creates if absent, a lockable buffer for
  358. * a given range of an inode. The buffer is returned
  359. * locked. If other overlapping buffers exist, they are
  360. * released before the new buffer is created and locked,
  361. * which may imply that this call will block until those buffers
  362. * are unlocked. No I/O is implied by this call.
  363. */
  364. xfs_buf_t *
  365. _xfs_buf_find(
  366. xfs_buftarg_t *btp, /* block device target */
  367. xfs_off_t ioff, /* starting offset of range */
  368. size_t isize, /* length of range */
  369. xfs_buf_flags_t flags,
  370. xfs_buf_t *new_bp)
  371. {
  372. xfs_off_t range_base;
  373. size_t range_length;
  374. xfs_bufhash_t *hash;
  375. xfs_buf_t *bp, *n;
  376. range_base = (ioff << BBSHIFT);
  377. range_length = (isize << BBSHIFT);
  378. /* Check for IOs smaller than the sector size / not sector aligned */
  379. ASSERT(!(range_length < (1 << btp->bt_sshift)));
  380. ASSERT(!(range_base & (xfs_off_t)btp->bt_smask));
  381. hash = &btp->bt_hash[hash_long((unsigned long)ioff, btp->bt_hashshift)];
  382. spin_lock(&hash->bh_lock);
  383. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  384. ASSERT(btp == bp->b_target);
  385. if (bp->b_file_offset == range_base &&
  386. bp->b_buffer_length == range_length) {
  387. /*
  388. * If we look at something, bring it to the
  389. * front of the list for next time.
  390. */
  391. atomic_inc(&bp->b_hold);
  392. list_move(&bp->b_hash_list, &hash->bh_list);
  393. goto found;
  394. }
  395. }
  396. /* No match found */
  397. if (new_bp) {
  398. _xfs_buf_initialize(new_bp, btp, range_base,
  399. range_length, flags);
  400. new_bp->b_hash = hash;
  401. list_add(&new_bp->b_hash_list, &hash->bh_list);
  402. } else {
  403. XFS_STATS_INC(xb_miss_locked);
  404. }
  405. spin_unlock(&hash->bh_lock);
  406. return new_bp;
  407. found:
  408. spin_unlock(&hash->bh_lock);
  409. /* Attempt to get the semaphore without sleeping,
  410. * if this does not work then we need to drop the
  411. * spinlock and do a hard attempt on the semaphore.
  412. */
  413. if (down_trylock(&bp->b_sema)) {
  414. if (!(flags & XBF_TRYLOCK)) {
  415. /* wait for buffer ownership */
  416. xfs_buf_lock(bp);
  417. XFS_STATS_INC(xb_get_locked_waited);
  418. } else {
  419. /* We asked for a trylock and failed, no need
  420. * to look at file offset and length here, we
  421. * know that this buffer at least overlaps our
  422. * buffer and is locked, therefore our buffer
  423. * either does not exist, or is this buffer.
  424. */
  425. xfs_buf_rele(bp);
  426. XFS_STATS_INC(xb_busy_locked);
  427. return NULL;
  428. }
  429. } else {
  430. /* trylock worked */
  431. XB_SET_OWNER(bp);
  432. }
  433. if (bp->b_flags & XBF_STALE) {
  434. ASSERT((bp->b_flags & _XBF_DELWRI_Q) == 0);
  435. bp->b_flags &= XBF_MAPPED;
  436. }
  437. trace_xfs_buf_find(bp, flags, _RET_IP_);
  438. XFS_STATS_INC(xb_get_locked);
  439. return bp;
  440. }
  441. /*
  442. * Assembles a buffer covering the specified range.
  443. * Storage in memory for all portions of the buffer will be allocated,
  444. * although backing storage may not be.
  445. */
  446. xfs_buf_t *
  447. xfs_buf_get(
  448. xfs_buftarg_t *target,/* target for buffer */
  449. xfs_off_t ioff, /* starting offset of range */
  450. size_t isize, /* length of range */
  451. xfs_buf_flags_t flags)
  452. {
  453. xfs_buf_t *bp, *new_bp;
  454. int error = 0, i;
  455. new_bp = xfs_buf_allocate(flags);
  456. if (unlikely(!new_bp))
  457. return NULL;
  458. bp = _xfs_buf_find(target, ioff, isize, flags, new_bp);
  459. if (bp == new_bp) {
  460. error = _xfs_buf_lookup_pages(bp, flags);
  461. if (error)
  462. goto no_buffer;
  463. } else {
  464. xfs_buf_deallocate(new_bp);
  465. if (unlikely(bp == NULL))
  466. return NULL;
  467. }
  468. for (i = 0; i < bp->b_page_count; i++)
  469. mark_page_accessed(bp->b_pages[i]);
  470. if (!(bp->b_flags & XBF_MAPPED)) {
  471. error = _xfs_buf_map_pages(bp, flags);
  472. if (unlikely(error)) {
  473. printk(KERN_WARNING "%s: failed to map pages\n",
  474. __func__);
  475. goto no_buffer;
  476. }
  477. }
  478. XFS_STATS_INC(xb_get);
  479. /*
  480. * Always fill in the block number now, the mapped cases can do
  481. * their own overlay of this later.
  482. */
  483. bp->b_bn = ioff;
  484. bp->b_count_desired = bp->b_buffer_length;
  485. trace_xfs_buf_get(bp, flags, _RET_IP_);
  486. return bp;
  487. no_buffer:
  488. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  489. xfs_buf_unlock(bp);
  490. xfs_buf_rele(bp);
  491. return NULL;
  492. }
  493. STATIC int
  494. _xfs_buf_read(
  495. xfs_buf_t *bp,
  496. xfs_buf_flags_t flags)
  497. {
  498. int status;
  499. ASSERT(!(flags & (XBF_DELWRI|XBF_WRITE)));
  500. ASSERT(bp->b_bn != XFS_BUF_DADDR_NULL);
  501. bp->b_flags &= ~(XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  502. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  503. bp->b_flags |= flags & (XBF_READ | XBF_ASYNC | \
  504. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  505. status = xfs_buf_iorequest(bp);
  506. if (!status && !(flags & XBF_ASYNC))
  507. status = xfs_buf_iowait(bp);
  508. return status;
  509. }
  510. xfs_buf_t *
  511. xfs_buf_read(
  512. xfs_buftarg_t *target,
  513. xfs_off_t ioff,
  514. size_t isize,
  515. xfs_buf_flags_t flags)
  516. {
  517. xfs_buf_t *bp;
  518. flags |= XBF_READ;
  519. bp = xfs_buf_get(target, ioff, isize, flags);
  520. if (bp) {
  521. trace_xfs_buf_read(bp, flags, _RET_IP_);
  522. if (!XFS_BUF_ISDONE(bp)) {
  523. XFS_STATS_INC(xb_get_read);
  524. _xfs_buf_read(bp, flags);
  525. } else if (flags & XBF_ASYNC) {
  526. /*
  527. * Read ahead call which is already satisfied,
  528. * drop the buffer
  529. */
  530. goto no_buffer;
  531. } else {
  532. /* We do not want read in the flags */
  533. bp->b_flags &= ~XBF_READ;
  534. }
  535. }
  536. return bp;
  537. no_buffer:
  538. if (flags & (XBF_LOCK | XBF_TRYLOCK))
  539. xfs_buf_unlock(bp);
  540. xfs_buf_rele(bp);
  541. return NULL;
  542. }
  543. /*
  544. * If we are not low on memory then do the readahead in a deadlock
  545. * safe manner.
  546. */
  547. void
  548. xfs_buf_readahead(
  549. xfs_buftarg_t *target,
  550. xfs_off_t ioff,
  551. size_t isize,
  552. xfs_buf_flags_t flags)
  553. {
  554. struct backing_dev_info *bdi;
  555. bdi = target->bt_mapping->backing_dev_info;
  556. if (bdi_read_congested(bdi))
  557. return;
  558. flags |= (XBF_TRYLOCK|XBF_ASYNC|XBF_READ_AHEAD);
  559. xfs_buf_read(target, ioff, isize, flags);
  560. }
  561. xfs_buf_t *
  562. xfs_buf_get_empty(
  563. size_t len,
  564. xfs_buftarg_t *target)
  565. {
  566. xfs_buf_t *bp;
  567. bp = xfs_buf_allocate(0);
  568. if (bp)
  569. _xfs_buf_initialize(bp, target, 0, len, 0);
  570. return bp;
  571. }
  572. static inline struct page *
  573. mem_to_page(
  574. void *addr)
  575. {
  576. if ((!is_vmalloc_addr(addr))) {
  577. return virt_to_page(addr);
  578. } else {
  579. return vmalloc_to_page(addr);
  580. }
  581. }
  582. int
  583. xfs_buf_associate_memory(
  584. xfs_buf_t *bp,
  585. void *mem,
  586. size_t len)
  587. {
  588. int rval;
  589. int i = 0;
  590. unsigned long pageaddr;
  591. unsigned long offset;
  592. size_t buflen;
  593. int page_count;
  594. pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
  595. offset = (unsigned long)mem - pageaddr;
  596. buflen = PAGE_CACHE_ALIGN(len + offset);
  597. page_count = buflen >> PAGE_CACHE_SHIFT;
  598. /* Free any previous set of page pointers */
  599. if (bp->b_pages)
  600. _xfs_buf_free_pages(bp);
  601. bp->b_pages = NULL;
  602. bp->b_addr = mem;
  603. rval = _xfs_buf_get_pages(bp, page_count, XBF_DONT_BLOCK);
  604. if (rval)
  605. return rval;
  606. bp->b_offset = offset;
  607. for (i = 0; i < bp->b_page_count; i++) {
  608. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  609. pageaddr += PAGE_CACHE_SIZE;
  610. }
  611. bp->b_count_desired = len;
  612. bp->b_buffer_length = buflen;
  613. bp->b_flags |= XBF_MAPPED;
  614. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  615. return 0;
  616. }
  617. xfs_buf_t *
  618. xfs_buf_get_noaddr(
  619. size_t len,
  620. xfs_buftarg_t *target)
  621. {
  622. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  623. int error, i;
  624. xfs_buf_t *bp;
  625. bp = xfs_buf_allocate(0);
  626. if (unlikely(bp == NULL))
  627. goto fail;
  628. _xfs_buf_initialize(bp, target, 0, len, 0);
  629. error = _xfs_buf_get_pages(bp, page_count, 0);
  630. if (error)
  631. goto fail_free_buf;
  632. for (i = 0; i < page_count; i++) {
  633. bp->b_pages[i] = alloc_page(GFP_KERNEL);
  634. if (!bp->b_pages[i])
  635. goto fail_free_mem;
  636. }
  637. bp->b_flags |= _XBF_PAGES;
  638. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  639. if (unlikely(error)) {
  640. printk(KERN_WARNING "%s: failed to map pages\n",
  641. __func__);
  642. goto fail_free_mem;
  643. }
  644. xfs_buf_unlock(bp);
  645. trace_xfs_buf_get_noaddr(bp, _RET_IP_);
  646. return bp;
  647. fail_free_mem:
  648. while (--i >= 0)
  649. __free_page(bp->b_pages[i]);
  650. _xfs_buf_free_pages(bp);
  651. fail_free_buf:
  652. xfs_buf_deallocate(bp);
  653. fail:
  654. return NULL;
  655. }
  656. /*
  657. * Increment reference count on buffer, to hold the buffer concurrently
  658. * with another thread which may release (free) the buffer asynchronously.
  659. * Must hold the buffer already to call this function.
  660. */
  661. void
  662. xfs_buf_hold(
  663. xfs_buf_t *bp)
  664. {
  665. trace_xfs_buf_hold(bp, _RET_IP_);
  666. atomic_inc(&bp->b_hold);
  667. }
  668. /*
  669. * Releases a hold on the specified buffer. If the
  670. * the hold count is 1, calls xfs_buf_free.
  671. */
  672. void
  673. xfs_buf_rele(
  674. xfs_buf_t *bp)
  675. {
  676. xfs_bufhash_t *hash = bp->b_hash;
  677. trace_xfs_buf_rele(bp, _RET_IP_);
  678. if (unlikely(!hash)) {
  679. ASSERT(!bp->b_relse);
  680. if (atomic_dec_and_test(&bp->b_hold))
  681. xfs_buf_free(bp);
  682. return;
  683. }
  684. ASSERT(atomic_read(&bp->b_hold) > 0);
  685. if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
  686. if (bp->b_relse) {
  687. atomic_inc(&bp->b_hold);
  688. spin_unlock(&hash->bh_lock);
  689. (*(bp->b_relse)) (bp);
  690. } else if (bp->b_flags & XBF_FS_MANAGED) {
  691. spin_unlock(&hash->bh_lock);
  692. } else {
  693. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  694. list_del_init(&bp->b_hash_list);
  695. spin_unlock(&hash->bh_lock);
  696. xfs_buf_free(bp);
  697. }
  698. }
  699. }
  700. /*
  701. * Mutual exclusion on buffers. Locking model:
  702. *
  703. * Buffers associated with inodes for which buffer locking
  704. * is not enabled are not protected by semaphores, and are
  705. * assumed to be exclusively owned by the caller. There is a
  706. * spinlock in the buffer, used by the caller when concurrent
  707. * access is possible.
  708. */
  709. /*
  710. * Locks a buffer object, if it is not already locked.
  711. * Note that this in no way locks the underlying pages, so it is only
  712. * useful for synchronizing concurrent use of buffer objects, not for
  713. * synchronizing independent access to the underlying pages.
  714. */
  715. int
  716. xfs_buf_cond_lock(
  717. xfs_buf_t *bp)
  718. {
  719. int locked;
  720. locked = down_trylock(&bp->b_sema) == 0;
  721. if (locked)
  722. XB_SET_OWNER(bp);
  723. trace_xfs_buf_cond_lock(bp, _RET_IP_);
  724. return locked ? 0 : -EBUSY;
  725. }
  726. int
  727. xfs_buf_lock_value(
  728. xfs_buf_t *bp)
  729. {
  730. return bp->b_sema.count;
  731. }
  732. /*
  733. * Locks a buffer object.
  734. * Note that this in no way locks the underlying pages, so it is only
  735. * useful for synchronizing concurrent use of buffer objects, not for
  736. * synchronizing independent access to the underlying pages.
  737. */
  738. void
  739. xfs_buf_lock(
  740. xfs_buf_t *bp)
  741. {
  742. trace_xfs_buf_lock(bp, _RET_IP_);
  743. if (atomic_read(&bp->b_io_remaining))
  744. blk_run_address_space(bp->b_target->bt_mapping);
  745. down(&bp->b_sema);
  746. XB_SET_OWNER(bp);
  747. trace_xfs_buf_lock_done(bp, _RET_IP_);
  748. }
  749. /*
  750. * Releases the lock on the buffer object.
  751. * If the buffer is marked delwri but is not queued, do so before we
  752. * unlock the buffer as we need to set flags correctly. We also need to
  753. * take a reference for the delwri queue because the unlocker is going to
  754. * drop their's and they don't know we just queued it.
  755. */
  756. void
  757. xfs_buf_unlock(
  758. xfs_buf_t *bp)
  759. {
  760. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  761. atomic_inc(&bp->b_hold);
  762. bp->b_flags |= XBF_ASYNC;
  763. xfs_buf_delwri_queue(bp, 0);
  764. }
  765. XB_CLEAR_OWNER(bp);
  766. up(&bp->b_sema);
  767. trace_xfs_buf_unlock(bp, _RET_IP_);
  768. }
  769. /*
  770. * Pinning Buffer Storage in Memory
  771. * Ensure that no attempt to force a buffer to disk will succeed.
  772. */
  773. void
  774. xfs_buf_pin(
  775. xfs_buf_t *bp)
  776. {
  777. trace_xfs_buf_pin(bp, _RET_IP_);
  778. atomic_inc(&bp->b_pin_count);
  779. }
  780. void
  781. xfs_buf_unpin(
  782. xfs_buf_t *bp)
  783. {
  784. trace_xfs_buf_unpin(bp, _RET_IP_);
  785. if (atomic_dec_and_test(&bp->b_pin_count))
  786. wake_up_all(&bp->b_waiters);
  787. }
  788. int
  789. xfs_buf_ispin(
  790. xfs_buf_t *bp)
  791. {
  792. return atomic_read(&bp->b_pin_count);
  793. }
  794. STATIC void
  795. xfs_buf_wait_unpin(
  796. xfs_buf_t *bp)
  797. {
  798. DECLARE_WAITQUEUE (wait, current);
  799. if (atomic_read(&bp->b_pin_count) == 0)
  800. return;
  801. add_wait_queue(&bp->b_waiters, &wait);
  802. for (;;) {
  803. set_current_state(TASK_UNINTERRUPTIBLE);
  804. if (atomic_read(&bp->b_pin_count) == 0)
  805. break;
  806. if (atomic_read(&bp->b_io_remaining))
  807. blk_run_address_space(bp->b_target->bt_mapping);
  808. schedule();
  809. }
  810. remove_wait_queue(&bp->b_waiters, &wait);
  811. set_current_state(TASK_RUNNING);
  812. }
  813. /*
  814. * Buffer Utility Routines
  815. */
  816. STATIC void
  817. xfs_buf_iodone_work(
  818. struct work_struct *work)
  819. {
  820. xfs_buf_t *bp =
  821. container_of(work, xfs_buf_t, b_iodone_work);
  822. /*
  823. * We can get an EOPNOTSUPP to ordered writes. Here we clear the
  824. * ordered flag and reissue them. Because we can't tell the higher
  825. * layers directly that they should not issue ordered I/O anymore, they
  826. * need to check if the _XFS_BARRIER_FAILED flag was set during I/O completion.
  827. */
  828. if ((bp->b_error == EOPNOTSUPP) &&
  829. (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
  830. trace_xfs_buf_ordered_retry(bp, _RET_IP_);
  831. bp->b_flags &= ~XBF_ORDERED;
  832. bp->b_flags |= _XFS_BARRIER_FAILED;
  833. xfs_buf_iorequest(bp);
  834. } else if (bp->b_iodone)
  835. (*(bp->b_iodone))(bp);
  836. else if (bp->b_flags & XBF_ASYNC)
  837. xfs_buf_relse(bp);
  838. }
  839. void
  840. xfs_buf_ioend(
  841. xfs_buf_t *bp,
  842. int schedule)
  843. {
  844. trace_xfs_buf_iodone(bp, _RET_IP_);
  845. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  846. if (bp->b_error == 0)
  847. bp->b_flags |= XBF_DONE;
  848. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  849. if (schedule) {
  850. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  851. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  852. } else {
  853. xfs_buf_iodone_work(&bp->b_iodone_work);
  854. }
  855. } else {
  856. complete(&bp->b_iowait);
  857. }
  858. }
  859. void
  860. xfs_buf_ioerror(
  861. xfs_buf_t *bp,
  862. int error)
  863. {
  864. ASSERT(error >= 0 && error <= 0xffff);
  865. bp->b_error = (unsigned short)error;
  866. trace_xfs_buf_ioerror(bp, error, _RET_IP_);
  867. }
  868. int
  869. xfs_bwrite(
  870. struct xfs_mount *mp,
  871. struct xfs_buf *bp)
  872. {
  873. int iowait = (bp->b_flags & XBF_ASYNC) == 0;
  874. int error = 0;
  875. bp->b_strat = xfs_bdstrat_cb;
  876. bp->b_mount = mp;
  877. bp->b_flags |= XBF_WRITE;
  878. if (!iowait)
  879. bp->b_flags |= _XBF_RUN_QUEUES;
  880. xfs_buf_delwri_dequeue(bp);
  881. xfs_buf_iostrategy(bp);
  882. if (iowait) {
  883. error = xfs_buf_iowait(bp);
  884. if (error)
  885. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  886. xfs_buf_relse(bp);
  887. }
  888. return error;
  889. }
  890. void
  891. xfs_bdwrite(
  892. void *mp,
  893. struct xfs_buf *bp)
  894. {
  895. trace_xfs_buf_bdwrite(bp, _RET_IP_);
  896. bp->b_strat = xfs_bdstrat_cb;
  897. bp->b_mount = mp;
  898. bp->b_flags &= ~XBF_READ;
  899. bp->b_flags |= (XBF_DELWRI | XBF_ASYNC);
  900. xfs_buf_delwri_queue(bp, 1);
  901. }
  902. /*
  903. * Called when we want to stop a buffer from getting written or read.
  904. * We attach the EIO error, muck with its flags, and call biodone
  905. * so that the proper iodone callbacks get called.
  906. */
  907. STATIC int
  908. xfs_bioerror(
  909. xfs_buf_t *bp)
  910. {
  911. #ifdef XFSERRORDEBUG
  912. ASSERT(XFS_BUF_ISREAD(bp) || bp->b_iodone);
  913. #endif
  914. /*
  915. * No need to wait until the buffer is unpinned, we aren't flushing it.
  916. */
  917. XFS_BUF_ERROR(bp, EIO);
  918. /*
  919. * We're calling biodone, so delete XBF_DONE flag.
  920. */
  921. XFS_BUF_UNREAD(bp);
  922. XFS_BUF_UNDELAYWRITE(bp);
  923. XFS_BUF_UNDONE(bp);
  924. XFS_BUF_STALE(bp);
  925. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  926. xfs_biodone(bp);
  927. return EIO;
  928. }
  929. /*
  930. * Same as xfs_bioerror, except that we are releasing the buffer
  931. * here ourselves, and avoiding the biodone call.
  932. * This is meant for userdata errors; metadata bufs come with
  933. * iodone functions attached, so that we can track down errors.
  934. */
  935. STATIC int
  936. xfs_bioerror_relse(
  937. struct xfs_buf *bp)
  938. {
  939. int64_t fl = XFS_BUF_BFLAGS(bp);
  940. /*
  941. * No need to wait until the buffer is unpinned.
  942. * We aren't flushing it.
  943. *
  944. * chunkhold expects B_DONE to be set, whether
  945. * we actually finish the I/O or not. We don't want to
  946. * change that interface.
  947. */
  948. XFS_BUF_UNREAD(bp);
  949. XFS_BUF_UNDELAYWRITE(bp);
  950. XFS_BUF_DONE(bp);
  951. XFS_BUF_STALE(bp);
  952. XFS_BUF_CLR_IODONE_FUNC(bp);
  953. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  954. if (!(fl & XBF_ASYNC)) {
  955. /*
  956. * Mark b_error and B_ERROR _both_.
  957. * Lot's of chunkcache code assumes that.
  958. * There's no reason to mark error for
  959. * ASYNC buffers.
  960. */
  961. XFS_BUF_ERROR(bp, EIO);
  962. XFS_BUF_FINISH_IOWAIT(bp);
  963. } else {
  964. xfs_buf_relse(bp);
  965. }
  966. return EIO;
  967. }
  968. /*
  969. * All xfs metadata buffers except log state machine buffers
  970. * get this attached as their b_bdstrat callback function.
  971. * This is so that we can catch a buffer
  972. * after prematurely unpinning it to forcibly shutdown the filesystem.
  973. */
  974. int
  975. xfs_bdstrat_cb(
  976. struct xfs_buf *bp)
  977. {
  978. if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
  979. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  980. /*
  981. * Metadata write that didn't get logged but
  982. * written delayed anyway. These aren't associated
  983. * with a transaction, and can be ignored.
  984. */
  985. if (!bp->b_iodone && !XFS_BUF_ISREAD(bp))
  986. return xfs_bioerror_relse(bp);
  987. else
  988. return xfs_bioerror(bp);
  989. }
  990. xfs_buf_iorequest(bp);
  991. return 0;
  992. }
  993. /*
  994. * Wrapper around bdstrat so that we can stop data from going to disk in case
  995. * we are shutting down the filesystem. Typically user data goes thru this
  996. * path; one of the exceptions is the superblock.
  997. */
  998. void
  999. xfsbdstrat(
  1000. struct xfs_mount *mp,
  1001. struct xfs_buf *bp)
  1002. {
  1003. if (XFS_FORCED_SHUTDOWN(mp)) {
  1004. trace_xfs_bdstrat_shut(bp, _RET_IP_);
  1005. xfs_bioerror_relse(bp);
  1006. return;
  1007. }
  1008. xfs_buf_iorequest(bp);
  1009. }
  1010. STATIC void
  1011. _xfs_buf_ioend(
  1012. xfs_buf_t *bp,
  1013. int schedule)
  1014. {
  1015. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  1016. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  1017. xfs_buf_ioend(bp, schedule);
  1018. }
  1019. }
  1020. STATIC void
  1021. xfs_buf_bio_end_io(
  1022. struct bio *bio,
  1023. int error)
  1024. {
  1025. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  1026. unsigned int blocksize = bp->b_target->bt_bsize;
  1027. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1028. xfs_buf_ioerror(bp, -error);
  1029. if (!error && xfs_buf_is_vmapped(bp) && (bp->b_flags & XBF_READ))
  1030. invalidate_kernel_vmap_range(bp->b_addr, xfs_buf_vmap_len(bp));
  1031. do {
  1032. struct page *page = bvec->bv_page;
  1033. ASSERT(!PagePrivate(page));
  1034. if (unlikely(bp->b_error)) {
  1035. if (bp->b_flags & XBF_READ)
  1036. ClearPageUptodate(page);
  1037. } else if (blocksize >= PAGE_CACHE_SIZE) {
  1038. SetPageUptodate(page);
  1039. } else if (!PagePrivate(page) &&
  1040. (bp->b_flags & _XBF_PAGE_CACHE)) {
  1041. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1042. }
  1043. if (--bvec >= bio->bi_io_vec)
  1044. prefetchw(&bvec->bv_page->flags);
  1045. if (bp->b_flags & _XBF_PAGE_LOCKED)
  1046. unlock_page(page);
  1047. } while (bvec >= bio->bi_io_vec);
  1048. _xfs_buf_ioend(bp, 1);
  1049. bio_put(bio);
  1050. }
  1051. STATIC void
  1052. _xfs_buf_ioapply(
  1053. xfs_buf_t *bp)
  1054. {
  1055. int rw, map_i, total_nr_pages, nr_pages;
  1056. struct bio *bio;
  1057. int offset = bp->b_offset;
  1058. int size = bp->b_count_desired;
  1059. sector_t sector = bp->b_bn;
  1060. unsigned int blocksize = bp->b_target->bt_bsize;
  1061. total_nr_pages = bp->b_page_count;
  1062. map_i = 0;
  1063. if (bp->b_flags & XBF_ORDERED) {
  1064. ASSERT(!(bp->b_flags & XBF_READ));
  1065. rw = WRITE_BARRIER;
  1066. } else if (bp->b_flags & XBF_LOG_BUFFER) {
  1067. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1068. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1069. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1070. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1071. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1072. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1073. rw = (bp->b_flags & XBF_WRITE) ? WRITE_META : READ_META;
  1074. } else {
  1075. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1076. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1077. }
  1078. /* Special code path for reading a sub page size buffer in --
  1079. * we populate up the whole page, and hence the other metadata
  1080. * in the same page. This optimization is only valid when the
  1081. * filesystem block size is not smaller than the page size.
  1082. */
  1083. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1084. ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
  1085. (XBF_READ|_XBF_PAGE_LOCKED)) &&
  1086. (blocksize >= PAGE_CACHE_SIZE)) {
  1087. bio = bio_alloc(GFP_NOIO, 1);
  1088. bio->bi_bdev = bp->b_target->bt_bdev;
  1089. bio->bi_sector = sector - (offset >> BBSHIFT);
  1090. bio->bi_end_io = xfs_buf_bio_end_io;
  1091. bio->bi_private = bp;
  1092. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1093. size = 0;
  1094. atomic_inc(&bp->b_io_remaining);
  1095. goto submit_io;
  1096. }
  1097. next_chunk:
  1098. atomic_inc(&bp->b_io_remaining);
  1099. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1100. if (nr_pages > total_nr_pages)
  1101. nr_pages = total_nr_pages;
  1102. bio = bio_alloc(GFP_NOIO, nr_pages);
  1103. bio->bi_bdev = bp->b_target->bt_bdev;
  1104. bio->bi_sector = sector;
  1105. bio->bi_end_io = xfs_buf_bio_end_io;
  1106. bio->bi_private = bp;
  1107. for (; size && nr_pages; nr_pages--, map_i++) {
  1108. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1109. if (nbytes > size)
  1110. nbytes = size;
  1111. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1112. if (rbytes < nbytes)
  1113. break;
  1114. offset = 0;
  1115. sector += nbytes >> BBSHIFT;
  1116. size -= nbytes;
  1117. total_nr_pages--;
  1118. }
  1119. submit_io:
  1120. if (likely(bio->bi_size)) {
  1121. if (xfs_buf_is_vmapped(bp)) {
  1122. flush_kernel_vmap_range(bp->b_addr,
  1123. xfs_buf_vmap_len(bp));
  1124. }
  1125. submit_bio(rw, bio);
  1126. if (size)
  1127. goto next_chunk;
  1128. } else {
  1129. bio_put(bio);
  1130. xfs_buf_ioerror(bp, EIO);
  1131. }
  1132. }
  1133. int
  1134. xfs_buf_iorequest(
  1135. xfs_buf_t *bp)
  1136. {
  1137. trace_xfs_buf_iorequest(bp, _RET_IP_);
  1138. if (bp->b_flags & XBF_DELWRI) {
  1139. xfs_buf_delwri_queue(bp, 1);
  1140. return 0;
  1141. }
  1142. if (bp->b_flags & XBF_WRITE) {
  1143. xfs_buf_wait_unpin(bp);
  1144. }
  1145. xfs_buf_hold(bp);
  1146. /* Set the count to 1 initially, this will stop an I/O
  1147. * completion callout which happens before we have started
  1148. * all the I/O from calling xfs_buf_ioend too early.
  1149. */
  1150. atomic_set(&bp->b_io_remaining, 1);
  1151. _xfs_buf_ioapply(bp);
  1152. _xfs_buf_ioend(bp, 0);
  1153. xfs_buf_rele(bp);
  1154. return 0;
  1155. }
  1156. /*
  1157. * Waits for I/O to complete on the buffer supplied.
  1158. * It returns immediately if no I/O is pending.
  1159. * It returns the I/O error code, if any, or 0 if there was no error.
  1160. */
  1161. int
  1162. xfs_buf_iowait(
  1163. xfs_buf_t *bp)
  1164. {
  1165. trace_xfs_buf_iowait(bp, _RET_IP_);
  1166. if (atomic_read(&bp->b_io_remaining))
  1167. blk_run_address_space(bp->b_target->bt_mapping);
  1168. wait_for_completion(&bp->b_iowait);
  1169. trace_xfs_buf_iowait_done(bp, _RET_IP_);
  1170. return bp->b_error;
  1171. }
  1172. xfs_caddr_t
  1173. xfs_buf_offset(
  1174. xfs_buf_t *bp,
  1175. size_t offset)
  1176. {
  1177. struct page *page;
  1178. if (bp->b_flags & XBF_MAPPED)
  1179. return XFS_BUF_PTR(bp) + offset;
  1180. offset += bp->b_offset;
  1181. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1182. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1183. }
  1184. /*
  1185. * Move data into or out of a buffer.
  1186. */
  1187. void
  1188. xfs_buf_iomove(
  1189. xfs_buf_t *bp, /* buffer to process */
  1190. size_t boff, /* starting buffer offset */
  1191. size_t bsize, /* length to copy */
  1192. void *data, /* data address */
  1193. xfs_buf_rw_t mode) /* read/write/zero flag */
  1194. {
  1195. size_t bend, cpoff, csize;
  1196. struct page *page;
  1197. bend = boff + bsize;
  1198. while (boff < bend) {
  1199. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1200. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1201. csize = min_t(size_t,
  1202. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1203. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1204. switch (mode) {
  1205. case XBRW_ZERO:
  1206. memset(page_address(page) + cpoff, 0, csize);
  1207. break;
  1208. case XBRW_READ:
  1209. memcpy(data, page_address(page) + cpoff, csize);
  1210. break;
  1211. case XBRW_WRITE:
  1212. memcpy(page_address(page) + cpoff, data, csize);
  1213. }
  1214. boff += csize;
  1215. data += csize;
  1216. }
  1217. }
  1218. /*
  1219. * Handling of buffer targets (buftargs).
  1220. */
  1221. /*
  1222. * Wait for any bufs with callbacks that have been submitted but
  1223. * have not yet returned... walk the hash list for the target.
  1224. */
  1225. void
  1226. xfs_wait_buftarg(
  1227. xfs_buftarg_t *btp)
  1228. {
  1229. xfs_buf_t *bp, *n;
  1230. xfs_bufhash_t *hash;
  1231. uint i;
  1232. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1233. hash = &btp->bt_hash[i];
  1234. again:
  1235. spin_lock(&hash->bh_lock);
  1236. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  1237. ASSERT(btp == bp->b_target);
  1238. if (!(bp->b_flags & XBF_FS_MANAGED)) {
  1239. spin_unlock(&hash->bh_lock);
  1240. /*
  1241. * Catch superblock reference count leaks
  1242. * immediately
  1243. */
  1244. BUG_ON(bp->b_bn == 0);
  1245. delay(100);
  1246. goto again;
  1247. }
  1248. }
  1249. spin_unlock(&hash->bh_lock);
  1250. }
  1251. }
  1252. /*
  1253. * Allocate buffer hash table for a given target.
  1254. * For devices containing metadata (i.e. not the log/realtime devices)
  1255. * we need to allocate a much larger hash table.
  1256. */
  1257. STATIC void
  1258. xfs_alloc_bufhash(
  1259. xfs_buftarg_t *btp,
  1260. int external)
  1261. {
  1262. unsigned int i;
  1263. btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
  1264. btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
  1265. btp->bt_hash = kmem_zalloc_large((1 << btp->bt_hashshift) *
  1266. sizeof(xfs_bufhash_t));
  1267. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1268. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1269. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1270. }
  1271. }
  1272. STATIC void
  1273. xfs_free_bufhash(
  1274. xfs_buftarg_t *btp)
  1275. {
  1276. kmem_free_large(btp->bt_hash);
  1277. btp->bt_hash = NULL;
  1278. }
  1279. /*
  1280. * buftarg list for delwrite queue processing
  1281. */
  1282. static LIST_HEAD(xfs_buftarg_list);
  1283. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1284. STATIC void
  1285. xfs_register_buftarg(
  1286. xfs_buftarg_t *btp)
  1287. {
  1288. spin_lock(&xfs_buftarg_lock);
  1289. list_add(&btp->bt_list, &xfs_buftarg_list);
  1290. spin_unlock(&xfs_buftarg_lock);
  1291. }
  1292. STATIC void
  1293. xfs_unregister_buftarg(
  1294. xfs_buftarg_t *btp)
  1295. {
  1296. spin_lock(&xfs_buftarg_lock);
  1297. list_del(&btp->bt_list);
  1298. spin_unlock(&xfs_buftarg_lock);
  1299. }
  1300. void
  1301. xfs_free_buftarg(
  1302. struct xfs_mount *mp,
  1303. struct xfs_buftarg *btp)
  1304. {
  1305. xfs_flush_buftarg(btp, 1);
  1306. if (mp->m_flags & XFS_MOUNT_BARRIER)
  1307. xfs_blkdev_issue_flush(btp);
  1308. xfs_free_bufhash(btp);
  1309. iput(btp->bt_mapping->host);
  1310. /* Unregister the buftarg first so that we don't get a
  1311. * wakeup finding a non-existent task
  1312. */
  1313. xfs_unregister_buftarg(btp);
  1314. kthread_stop(btp->bt_task);
  1315. kmem_free(btp);
  1316. }
  1317. STATIC int
  1318. xfs_setsize_buftarg_flags(
  1319. xfs_buftarg_t *btp,
  1320. unsigned int blocksize,
  1321. unsigned int sectorsize,
  1322. int verbose)
  1323. {
  1324. btp->bt_bsize = blocksize;
  1325. btp->bt_sshift = ffs(sectorsize) - 1;
  1326. btp->bt_smask = sectorsize - 1;
  1327. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1328. printk(KERN_WARNING
  1329. "XFS: Cannot set_blocksize to %u on device %s\n",
  1330. sectorsize, XFS_BUFTARG_NAME(btp));
  1331. return EINVAL;
  1332. }
  1333. if (verbose &&
  1334. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1335. printk(KERN_WARNING
  1336. "XFS: %u byte sectors in use on device %s. "
  1337. "This is suboptimal; %u or greater is ideal.\n",
  1338. sectorsize, XFS_BUFTARG_NAME(btp),
  1339. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * When allocating the initial buffer target we have not yet
  1345. * read in the superblock, so don't know what sized sectors
  1346. * are being used is at this early stage. Play safe.
  1347. */
  1348. STATIC int
  1349. xfs_setsize_buftarg_early(
  1350. xfs_buftarg_t *btp,
  1351. struct block_device *bdev)
  1352. {
  1353. return xfs_setsize_buftarg_flags(btp,
  1354. PAGE_CACHE_SIZE, bdev_logical_block_size(bdev), 0);
  1355. }
  1356. int
  1357. xfs_setsize_buftarg(
  1358. xfs_buftarg_t *btp,
  1359. unsigned int blocksize,
  1360. unsigned int sectorsize)
  1361. {
  1362. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1363. }
  1364. STATIC int
  1365. xfs_mapping_buftarg(
  1366. xfs_buftarg_t *btp,
  1367. struct block_device *bdev)
  1368. {
  1369. struct backing_dev_info *bdi;
  1370. struct inode *inode;
  1371. struct address_space *mapping;
  1372. static const struct address_space_operations mapping_aops = {
  1373. .sync_page = block_sync_page,
  1374. .migratepage = fail_migrate_page,
  1375. };
  1376. inode = new_inode(bdev->bd_inode->i_sb);
  1377. if (!inode) {
  1378. printk(KERN_WARNING
  1379. "XFS: Cannot allocate mapping inode for device %s\n",
  1380. XFS_BUFTARG_NAME(btp));
  1381. return ENOMEM;
  1382. }
  1383. inode->i_mode = S_IFBLK;
  1384. inode->i_bdev = bdev;
  1385. inode->i_rdev = bdev->bd_dev;
  1386. bdi = blk_get_backing_dev_info(bdev);
  1387. if (!bdi)
  1388. bdi = &default_backing_dev_info;
  1389. mapping = &inode->i_data;
  1390. mapping->a_ops = &mapping_aops;
  1391. mapping->backing_dev_info = bdi;
  1392. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1393. btp->bt_mapping = mapping;
  1394. return 0;
  1395. }
  1396. STATIC int
  1397. xfs_alloc_delwrite_queue(
  1398. xfs_buftarg_t *btp)
  1399. {
  1400. int error = 0;
  1401. INIT_LIST_HEAD(&btp->bt_list);
  1402. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1403. spin_lock_init(&btp->bt_delwrite_lock);
  1404. btp->bt_flags = 0;
  1405. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
  1406. if (IS_ERR(btp->bt_task)) {
  1407. error = PTR_ERR(btp->bt_task);
  1408. goto out_error;
  1409. }
  1410. xfs_register_buftarg(btp);
  1411. out_error:
  1412. return error;
  1413. }
  1414. xfs_buftarg_t *
  1415. xfs_alloc_buftarg(
  1416. struct block_device *bdev,
  1417. int external)
  1418. {
  1419. xfs_buftarg_t *btp;
  1420. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1421. btp->bt_dev = bdev->bd_dev;
  1422. btp->bt_bdev = bdev;
  1423. if (xfs_setsize_buftarg_early(btp, bdev))
  1424. goto error;
  1425. if (xfs_mapping_buftarg(btp, bdev))
  1426. goto error;
  1427. if (xfs_alloc_delwrite_queue(btp))
  1428. goto error;
  1429. xfs_alloc_bufhash(btp, external);
  1430. return btp;
  1431. error:
  1432. kmem_free(btp);
  1433. return NULL;
  1434. }
  1435. /*
  1436. * Delayed write buffer handling
  1437. */
  1438. STATIC void
  1439. xfs_buf_delwri_queue(
  1440. xfs_buf_t *bp,
  1441. int unlock)
  1442. {
  1443. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1444. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1445. trace_xfs_buf_delwri_queue(bp, _RET_IP_);
  1446. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1447. spin_lock(dwlk);
  1448. /* If already in the queue, dequeue and place at tail */
  1449. if (!list_empty(&bp->b_list)) {
  1450. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1451. if (unlock)
  1452. atomic_dec(&bp->b_hold);
  1453. list_del(&bp->b_list);
  1454. }
  1455. if (list_empty(dwq)) {
  1456. /* start xfsbufd as it is about to have something to do */
  1457. wake_up_process(bp->b_target->bt_task);
  1458. }
  1459. bp->b_flags |= _XBF_DELWRI_Q;
  1460. list_add_tail(&bp->b_list, dwq);
  1461. bp->b_queuetime = jiffies;
  1462. spin_unlock(dwlk);
  1463. if (unlock)
  1464. xfs_buf_unlock(bp);
  1465. }
  1466. void
  1467. xfs_buf_delwri_dequeue(
  1468. xfs_buf_t *bp)
  1469. {
  1470. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1471. int dequeued = 0;
  1472. spin_lock(dwlk);
  1473. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1474. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1475. list_del_init(&bp->b_list);
  1476. dequeued = 1;
  1477. }
  1478. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1479. spin_unlock(dwlk);
  1480. if (dequeued)
  1481. xfs_buf_rele(bp);
  1482. trace_xfs_buf_delwri_dequeue(bp, _RET_IP_);
  1483. }
  1484. /*
  1485. * If a delwri buffer needs to be pushed before it has aged out, then promote
  1486. * it to the head of the delwri queue so that it will be flushed on the next
  1487. * xfsbufd run. We do this by resetting the queuetime of the buffer to be older
  1488. * than the age currently needed to flush the buffer. Hence the next time the
  1489. * xfsbufd sees it is guaranteed to be considered old enough to flush.
  1490. */
  1491. void
  1492. xfs_buf_delwri_promote(
  1493. struct xfs_buf *bp)
  1494. {
  1495. struct xfs_buftarg *btp = bp->b_target;
  1496. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10) + 1;
  1497. ASSERT(bp->b_flags & XBF_DELWRI);
  1498. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1499. /*
  1500. * Check the buffer age before locking the delayed write queue as we
  1501. * don't need to promote buffers that are already past the flush age.
  1502. */
  1503. if (bp->b_queuetime < jiffies - age)
  1504. return;
  1505. bp->b_queuetime = jiffies - age;
  1506. spin_lock(&btp->bt_delwrite_lock);
  1507. list_move(&bp->b_list, &btp->bt_delwrite_queue);
  1508. spin_unlock(&btp->bt_delwrite_lock);
  1509. }
  1510. STATIC void
  1511. xfs_buf_runall_queues(
  1512. struct workqueue_struct *queue)
  1513. {
  1514. flush_workqueue(queue);
  1515. }
  1516. STATIC int
  1517. xfsbufd_wakeup(
  1518. int priority,
  1519. gfp_t mask)
  1520. {
  1521. xfs_buftarg_t *btp;
  1522. spin_lock(&xfs_buftarg_lock);
  1523. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1524. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1525. continue;
  1526. if (list_empty(&btp->bt_delwrite_queue))
  1527. continue;
  1528. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1529. wake_up_process(btp->bt_task);
  1530. }
  1531. spin_unlock(&xfs_buftarg_lock);
  1532. return 0;
  1533. }
  1534. /*
  1535. * Move as many buffers as specified to the supplied list
  1536. * idicating if we skipped any buffers to prevent deadlocks.
  1537. */
  1538. STATIC int
  1539. xfs_buf_delwri_split(
  1540. xfs_buftarg_t *target,
  1541. struct list_head *list,
  1542. unsigned long age)
  1543. {
  1544. xfs_buf_t *bp, *n;
  1545. struct list_head *dwq = &target->bt_delwrite_queue;
  1546. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1547. int skipped = 0;
  1548. int force;
  1549. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1550. INIT_LIST_HEAD(list);
  1551. spin_lock(dwlk);
  1552. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1553. trace_xfs_buf_delwri_split(bp, _RET_IP_);
  1554. ASSERT(bp->b_flags & XBF_DELWRI);
  1555. if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
  1556. if (!force &&
  1557. time_before(jiffies, bp->b_queuetime + age)) {
  1558. xfs_buf_unlock(bp);
  1559. break;
  1560. }
  1561. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1562. _XBF_RUN_QUEUES);
  1563. bp->b_flags |= XBF_WRITE;
  1564. list_move_tail(&bp->b_list, list);
  1565. } else
  1566. skipped++;
  1567. }
  1568. spin_unlock(dwlk);
  1569. return skipped;
  1570. }
  1571. /*
  1572. * Compare function is more complex than it needs to be because
  1573. * the return value is only 32 bits and we are doing comparisons
  1574. * on 64 bit values
  1575. */
  1576. static int
  1577. xfs_buf_cmp(
  1578. void *priv,
  1579. struct list_head *a,
  1580. struct list_head *b)
  1581. {
  1582. struct xfs_buf *ap = container_of(a, struct xfs_buf, b_list);
  1583. struct xfs_buf *bp = container_of(b, struct xfs_buf, b_list);
  1584. xfs_daddr_t diff;
  1585. diff = ap->b_bn - bp->b_bn;
  1586. if (diff < 0)
  1587. return -1;
  1588. if (diff > 0)
  1589. return 1;
  1590. return 0;
  1591. }
  1592. void
  1593. xfs_buf_delwri_sort(
  1594. xfs_buftarg_t *target,
  1595. struct list_head *list)
  1596. {
  1597. list_sort(NULL, list, xfs_buf_cmp);
  1598. }
  1599. STATIC int
  1600. xfsbufd(
  1601. void *data)
  1602. {
  1603. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1604. current->flags |= PF_MEMALLOC;
  1605. set_freezable();
  1606. do {
  1607. long age = xfs_buf_age_centisecs * msecs_to_jiffies(10);
  1608. long tout = xfs_buf_timer_centisecs * msecs_to_jiffies(10);
  1609. int count = 0;
  1610. struct list_head tmp;
  1611. if (unlikely(freezing(current))) {
  1612. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1613. refrigerator();
  1614. } else {
  1615. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1616. }
  1617. /* sleep for a long time if there is nothing to do. */
  1618. if (list_empty(&target->bt_delwrite_queue))
  1619. tout = MAX_SCHEDULE_TIMEOUT;
  1620. schedule_timeout_interruptible(tout);
  1621. xfs_buf_delwri_split(target, &tmp, age);
  1622. list_sort(NULL, &tmp, xfs_buf_cmp);
  1623. while (!list_empty(&tmp)) {
  1624. struct xfs_buf *bp;
  1625. bp = list_first_entry(&tmp, struct xfs_buf, b_list);
  1626. list_del_init(&bp->b_list);
  1627. xfs_buf_iostrategy(bp);
  1628. count++;
  1629. }
  1630. if (count)
  1631. blk_run_address_space(target->bt_mapping);
  1632. } while (!kthread_should_stop());
  1633. return 0;
  1634. }
  1635. /*
  1636. * Go through all incore buffers, and release buffers if they belong to
  1637. * the given device. This is used in filesystem error handling to
  1638. * preserve the consistency of its metadata.
  1639. */
  1640. int
  1641. xfs_flush_buftarg(
  1642. xfs_buftarg_t *target,
  1643. int wait)
  1644. {
  1645. xfs_buf_t *bp;
  1646. int pincount = 0;
  1647. LIST_HEAD(tmp_list);
  1648. LIST_HEAD(wait_list);
  1649. xfs_buf_runall_queues(xfsconvertd_workqueue);
  1650. xfs_buf_runall_queues(xfsdatad_workqueue);
  1651. xfs_buf_runall_queues(xfslogd_workqueue);
  1652. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1653. pincount = xfs_buf_delwri_split(target, &tmp_list, 0);
  1654. /*
  1655. * Dropped the delayed write list lock, now walk the temporary list.
  1656. * All I/O is issued async and then if we need to wait for completion
  1657. * we do that after issuing all the IO.
  1658. */
  1659. list_sort(NULL, &tmp_list, xfs_buf_cmp);
  1660. while (!list_empty(&tmp_list)) {
  1661. bp = list_first_entry(&tmp_list, struct xfs_buf, b_list);
  1662. ASSERT(target == bp->b_target);
  1663. list_del_init(&bp->b_list);
  1664. if (wait) {
  1665. bp->b_flags &= ~XBF_ASYNC;
  1666. list_add(&bp->b_list, &wait_list);
  1667. }
  1668. xfs_buf_iostrategy(bp);
  1669. }
  1670. if (wait) {
  1671. /* Expedite and wait for IO to complete. */
  1672. blk_run_address_space(target->bt_mapping);
  1673. while (!list_empty(&wait_list)) {
  1674. bp = list_first_entry(&wait_list, struct xfs_buf, b_list);
  1675. list_del_init(&bp->b_list);
  1676. xfs_iowait(bp);
  1677. xfs_buf_relse(bp);
  1678. }
  1679. }
  1680. return pincount;
  1681. }
  1682. int __init
  1683. xfs_buf_init(void)
  1684. {
  1685. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1686. KM_ZONE_HWALIGN, NULL);
  1687. if (!xfs_buf_zone)
  1688. goto out;
  1689. xfslogd_workqueue = create_workqueue("xfslogd");
  1690. if (!xfslogd_workqueue)
  1691. goto out_free_buf_zone;
  1692. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1693. if (!xfsdatad_workqueue)
  1694. goto out_destroy_xfslogd_workqueue;
  1695. xfsconvertd_workqueue = create_workqueue("xfsconvertd");
  1696. if (!xfsconvertd_workqueue)
  1697. goto out_destroy_xfsdatad_workqueue;
  1698. register_shrinker(&xfs_buf_shake);
  1699. return 0;
  1700. out_destroy_xfsdatad_workqueue:
  1701. destroy_workqueue(xfsdatad_workqueue);
  1702. out_destroy_xfslogd_workqueue:
  1703. destroy_workqueue(xfslogd_workqueue);
  1704. out_free_buf_zone:
  1705. kmem_zone_destroy(xfs_buf_zone);
  1706. out:
  1707. return -ENOMEM;
  1708. }
  1709. void
  1710. xfs_buf_terminate(void)
  1711. {
  1712. unregister_shrinker(&xfs_buf_shake);
  1713. destroy_workqueue(xfsconvertd_workqueue);
  1714. destroy_workqueue(xfsdatad_workqueue);
  1715. destroy_workqueue(xfslogd_workqueue);
  1716. kmem_zone_destroy(xfs_buf_zone);
  1717. }
  1718. #ifdef CONFIG_KDB_MODULES
  1719. struct list_head *
  1720. xfs_get_buftarg_list(void)
  1721. {
  1722. return &xfs_buftarg_list;
  1723. }
  1724. #endif