xfs_buf.c 47 KB

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