xfs_buf.c 42 KB

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