xfs_buf.c 44 KB

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