xfs_buf.c 42 KB

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