xfs_buf.c 42 KB

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