xfs_buf.c 45 KB

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