xfs_buf.c 41 KB

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