xfs_buf.c 42 KB

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