xfs_buf.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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. __func__, 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_flags |= _XBF_PAGE_LOCKED;
  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_flags & _XBF_PAGE_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. __func__);
  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 ((!is_vmalloc_addr(addr))) {
  625. return virt_to_page(addr);
  626. } else {
  627. return vmalloc_to_page(addr);
  628. }
  629. }
  630. int
  631. xfs_buf_associate_memory(
  632. xfs_buf_t *bp,
  633. void *mem,
  634. size_t len)
  635. {
  636. int rval;
  637. int i = 0;
  638. unsigned long pageaddr;
  639. unsigned long offset;
  640. size_t buflen;
  641. int page_count;
  642. pageaddr = (unsigned long)mem & PAGE_CACHE_MASK;
  643. offset = (unsigned long)mem - pageaddr;
  644. buflen = PAGE_CACHE_ALIGN(len + offset);
  645. page_count = buflen >> PAGE_CACHE_SHIFT;
  646. /* Free any previous set of page pointers */
  647. if (bp->b_pages)
  648. _xfs_buf_free_pages(bp);
  649. bp->b_pages = NULL;
  650. bp->b_addr = mem;
  651. rval = _xfs_buf_get_pages(bp, page_count, 0);
  652. if (rval)
  653. return rval;
  654. bp->b_offset = offset;
  655. for (i = 0; i < bp->b_page_count; i++) {
  656. bp->b_pages[i] = mem_to_page((void *)pageaddr);
  657. pageaddr += PAGE_CACHE_SIZE;
  658. }
  659. bp->b_count_desired = len;
  660. bp->b_buffer_length = buflen;
  661. bp->b_flags |= XBF_MAPPED;
  662. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  663. return 0;
  664. }
  665. xfs_buf_t *
  666. xfs_buf_get_noaddr(
  667. size_t len,
  668. xfs_buftarg_t *target)
  669. {
  670. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  671. int error, i;
  672. xfs_buf_t *bp;
  673. bp = xfs_buf_allocate(0);
  674. if (unlikely(bp == NULL))
  675. goto fail;
  676. _xfs_buf_initialize(bp, target, 0, len, 0);
  677. error = _xfs_buf_get_pages(bp, page_count, 0);
  678. if (error)
  679. goto fail_free_buf;
  680. for (i = 0; i < page_count; i++) {
  681. bp->b_pages[i] = alloc_page(GFP_KERNEL);
  682. if (!bp->b_pages[i])
  683. goto fail_free_mem;
  684. }
  685. bp->b_flags |= _XBF_PAGES;
  686. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  687. if (unlikely(error)) {
  688. printk(KERN_WARNING "%s: failed to map pages\n",
  689. __func__);
  690. goto fail_free_mem;
  691. }
  692. xfs_buf_unlock(bp);
  693. XB_TRACE(bp, "no_daddr", len);
  694. return bp;
  695. fail_free_mem:
  696. while (--i >= 0)
  697. __free_page(bp->b_pages[i]);
  698. _xfs_buf_free_pages(bp);
  699. fail_free_buf:
  700. xfs_buf_deallocate(bp);
  701. fail:
  702. return NULL;
  703. }
  704. /*
  705. * Increment reference count on buffer, to hold the buffer concurrently
  706. * with another thread which may release (free) the buffer asynchronously.
  707. * Must hold the buffer already to call this function.
  708. */
  709. void
  710. xfs_buf_hold(
  711. xfs_buf_t *bp)
  712. {
  713. atomic_inc(&bp->b_hold);
  714. XB_TRACE(bp, "hold", 0);
  715. }
  716. /*
  717. * Releases a hold on the specified buffer. If the
  718. * the hold count is 1, calls xfs_buf_free.
  719. */
  720. void
  721. xfs_buf_rele(
  722. xfs_buf_t *bp)
  723. {
  724. xfs_bufhash_t *hash = bp->b_hash;
  725. XB_TRACE(bp, "rele", bp->b_relse);
  726. if (unlikely(!hash)) {
  727. ASSERT(!bp->b_relse);
  728. if (atomic_dec_and_test(&bp->b_hold))
  729. xfs_buf_free(bp);
  730. return;
  731. }
  732. if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
  733. if (bp->b_relse) {
  734. atomic_inc(&bp->b_hold);
  735. spin_unlock(&hash->bh_lock);
  736. (*(bp->b_relse)) (bp);
  737. } else if (bp->b_flags & XBF_FS_MANAGED) {
  738. spin_unlock(&hash->bh_lock);
  739. } else {
  740. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  741. list_del_init(&bp->b_hash_list);
  742. spin_unlock(&hash->bh_lock);
  743. xfs_buf_free(bp);
  744. }
  745. } else {
  746. /*
  747. * Catch reference count leaks
  748. */
  749. ASSERT(atomic_read(&bp->b_hold) >= 0);
  750. }
  751. }
  752. /*
  753. * Mutual exclusion on buffers. Locking model:
  754. *
  755. * Buffers associated with inodes for which buffer locking
  756. * is not enabled are not protected by semaphores, and are
  757. * assumed to be exclusively owned by the caller. There is a
  758. * spinlock in the buffer, used by the caller when concurrent
  759. * access is possible.
  760. */
  761. /*
  762. * Locks a buffer object, if it is not already locked.
  763. * Note that this in no way locks the underlying pages, so it is only
  764. * useful for synchronizing concurrent use of buffer objects, not for
  765. * synchronizing independent access to the underlying pages.
  766. */
  767. int
  768. xfs_buf_cond_lock(
  769. xfs_buf_t *bp)
  770. {
  771. int locked;
  772. locked = down_trylock(&bp->b_sema) == 0;
  773. if (locked) {
  774. XB_SET_OWNER(bp);
  775. }
  776. XB_TRACE(bp, "cond_lock", (long)locked);
  777. return locked ? 0 : -EBUSY;
  778. }
  779. #if defined(DEBUG) || defined(XFS_BLI_TRACE)
  780. int
  781. xfs_buf_lock_value(
  782. xfs_buf_t *bp)
  783. {
  784. return bp->b_sema.count;
  785. }
  786. #endif
  787. /*
  788. * Locks a buffer object.
  789. * Note that this in no way locks the underlying pages, so it is only
  790. * useful for synchronizing concurrent use of buffer objects, not for
  791. * synchronizing independent access to the underlying pages.
  792. */
  793. void
  794. xfs_buf_lock(
  795. xfs_buf_t *bp)
  796. {
  797. XB_TRACE(bp, "lock", 0);
  798. if (atomic_read(&bp->b_io_remaining))
  799. blk_run_address_space(bp->b_target->bt_mapping);
  800. down(&bp->b_sema);
  801. XB_SET_OWNER(bp);
  802. XB_TRACE(bp, "locked", 0);
  803. }
  804. /*
  805. * Releases the lock on the buffer object.
  806. * If the buffer is marked delwri but is not queued, do so before we
  807. * unlock the buffer as we need to set flags correctly. We also need to
  808. * take a reference for the delwri queue because the unlocker is going to
  809. * drop their's and they don't know we just queued it.
  810. */
  811. void
  812. xfs_buf_unlock(
  813. xfs_buf_t *bp)
  814. {
  815. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  816. atomic_inc(&bp->b_hold);
  817. bp->b_flags |= XBF_ASYNC;
  818. xfs_buf_delwri_queue(bp, 0);
  819. }
  820. XB_CLEAR_OWNER(bp);
  821. up(&bp->b_sema);
  822. XB_TRACE(bp, "unlock", 0);
  823. }
  824. /*
  825. * Pinning Buffer Storage in Memory
  826. * Ensure that no attempt to force a buffer to disk will succeed.
  827. */
  828. void
  829. xfs_buf_pin(
  830. xfs_buf_t *bp)
  831. {
  832. atomic_inc(&bp->b_pin_count);
  833. XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
  834. }
  835. void
  836. xfs_buf_unpin(
  837. xfs_buf_t *bp)
  838. {
  839. if (atomic_dec_and_test(&bp->b_pin_count))
  840. wake_up_all(&bp->b_waiters);
  841. XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
  842. }
  843. int
  844. xfs_buf_ispin(
  845. xfs_buf_t *bp)
  846. {
  847. return atomic_read(&bp->b_pin_count);
  848. }
  849. STATIC void
  850. xfs_buf_wait_unpin(
  851. xfs_buf_t *bp)
  852. {
  853. DECLARE_WAITQUEUE (wait, current);
  854. if (atomic_read(&bp->b_pin_count) == 0)
  855. return;
  856. add_wait_queue(&bp->b_waiters, &wait);
  857. for (;;) {
  858. set_current_state(TASK_UNINTERRUPTIBLE);
  859. if (atomic_read(&bp->b_pin_count) == 0)
  860. break;
  861. if (atomic_read(&bp->b_io_remaining))
  862. blk_run_address_space(bp->b_target->bt_mapping);
  863. schedule();
  864. }
  865. remove_wait_queue(&bp->b_waiters, &wait);
  866. set_current_state(TASK_RUNNING);
  867. }
  868. /*
  869. * Buffer Utility Routines
  870. */
  871. STATIC void
  872. xfs_buf_iodone_work(
  873. struct work_struct *work)
  874. {
  875. xfs_buf_t *bp =
  876. container_of(work, xfs_buf_t, b_iodone_work);
  877. /*
  878. * We can get an EOPNOTSUPP to ordered writes. Here we clear the
  879. * ordered flag and reissue them. Because we can't tell the higher
  880. * layers directly that they should not issue ordered I/O anymore, they
  881. * need to check if the ordered flag was cleared during I/O completion.
  882. */
  883. if ((bp->b_error == EOPNOTSUPP) &&
  884. (bp->b_flags & (XBF_ORDERED|XBF_ASYNC)) == (XBF_ORDERED|XBF_ASYNC)) {
  885. XB_TRACE(bp, "ordered_retry", bp->b_iodone);
  886. bp->b_flags &= ~XBF_ORDERED;
  887. xfs_buf_iorequest(bp);
  888. } else if (bp->b_iodone)
  889. (*(bp->b_iodone))(bp);
  890. else if (bp->b_flags & XBF_ASYNC)
  891. xfs_buf_relse(bp);
  892. }
  893. void
  894. xfs_buf_ioend(
  895. xfs_buf_t *bp,
  896. int schedule)
  897. {
  898. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_READ_AHEAD);
  899. if (bp->b_error == 0)
  900. bp->b_flags |= XBF_DONE;
  901. XB_TRACE(bp, "iodone", bp->b_iodone);
  902. if ((bp->b_iodone) || (bp->b_flags & XBF_ASYNC)) {
  903. if (schedule) {
  904. INIT_WORK(&bp->b_iodone_work, xfs_buf_iodone_work);
  905. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  906. } else {
  907. xfs_buf_iodone_work(&bp->b_iodone_work);
  908. }
  909. } else {
  910. up(&bp->b_iodonesema);
  911. }
  912. }
  913. void
  914. xfs_buf_ioerror(
  915. xfs_buf_t *bp,
  916. int error)
  917. {
  918. ASSERT(error >= 0 && error <= 0xffff);
  919. bp->b_error = (unsigned short)error;
  920. XB_TRACE(bp, "ioerror", (unsigned long)error);
  921. }
  922. /*
  923. * Initiate I/O on a buffer, based on the flags supplied.
  924. * The b_iodone routine in the buffer supplied will only be called
  925. * when all of the subsidiary I/O requests, if any, have been completed.
  926. */
  927. int
  928. xfs_buf_iostart(
  929. xfs_buf_t *bp,
  930. xfs_buf_flags_t flags)
  931. {
  932. int status = 0;
  933. XB_TRACE(bp, "iostart", (unsigned long)flags);
  934. if (flags & XBF_DELWRI) {
  935. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC);
  936. bp->b_flags |= flags & (XBF_DELWRI | XBF_ASYNC);
  937. xfs_buf_delwri_queue(bp, 1);
  938. return 0;
  939. }
  940. bp->b_flags &= ~(XBF_READ | XBF_WRITE | XBF_ASYNC | XBF_DELWRI | \
  941. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  942. bp->b_flags |= flags & (XBF_READ | XBF_WRITE | XBF_ASYNC | \
  943. XBF_READ_AHEAD | _XBF_RUN_QUEUES);
  944. BUG_ON(bp->b_bn == XFS_BUF_DADDR_NULL);
  945. /* For writes allow an alternate strategy routine to precede
  946. * the actual I/O request (which may not be issued at all in
  947. * a shutdown situation, for example).
  948. */
  949. status = (flags & XBF_WRITE) ?
  950. xfs_buf_iostrategy(bp) : xfs_buf_iorequest(bp);
  951. /* Wait for I/O if we are not an async request.
  952. * Note: async I/O request completion will release the buffer,
  953. * and that can already be done by this point. So using the
  954. * buffer pointer from here on, after async I/O, is invalid.
  955. */
  956. if (!status && !(flags & XBF_ASYNC))
  957. status = xfs_buf_iowait(bp);
  958. return status;
  959. }
  960. STATIC_INLINE void
  961. _xfs_buf_ioend(
  962. xfs_buf_t *bp,
  963. int schedule)
  964. {
  965. if (atomic_dec_and_test(&bp->b_io_remaining) == 1) {
  966. bp->b_flags &= ~_XBF_PAGE_LOCKED;
  967. xfs_buf_ioend(bp, schedule);
  968. }
  969. }
  970. STATIC void
  971. xfs_buf_bio_end_io(
  972. struct bio *bio,
  973. int error)
  974. {
  975. xfs_buf_t *bp = (xfs_buf_t *)bio->bi_private;
  976. unsigned int blocksize = bp->b_target->bt_bsize;
  977. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  978. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  979. bp->b_error = EIO;
  980. do {
  981. struct page *page = bvec->bv_page;
  982. ASSERT(!PagePrivate(page));
  983. if (unlikely(bp->b_error)) {
  984. if (bp->b_flags & XBF_READ)
  985. ClearPageUptodate(page);
  986. } else if (blocksize >= PAGE_CACHE_SIZE) {
  987. SetPageUptodate(page);
  988. } else if (!PagePrivate(page) &&
  989. (bp->b_flags & _XBF_PAGE_CACHE)) {
  990. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  991. }
  992. if (--bvec >= bio->bi_io_vec)
  993. prefetchw(&bvec->bv_page->flags);
  994. if (bp->b_flags & _XBF_PAGE_LOCKED)
  995. unlock_page(page);
  996. } while (bvec >= bio->bi_io_vec);
  997. _xfs_buf_ioend(bp, 1);
  998. bio_put(bio);
  999. }
  1000. STATIC void
  1001. _xfs_buf_ioapply(
  1002. xfs_buf_t *bp)
  1003. {
  1004. int rw, map_i, total_nr_pages, nr_pages;
  1005. struct bio *bio;
  1006. int offset = bp->b_offset;
  1007. int size = bp->b_count_desired;
  1008. sector_t sector = bp->b_bn;
  1009. unsigned int blocksize = bp->b_target->bt_bsize;
  1010. total_nr_pages = bp->b_page_count;
  1011. map_i = 0;
  1012. if (bp->b_flags & XBF_ORDERED) {
  1013. ASSERT(!(bp->b_flags & XBF_READ));
  1014. rw = WRITE_BARRIER;
  1015. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1016. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1017. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1018. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1019. } else {
  1020. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1021. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1022. }
  1023. /* Special code path for reading a sub page size buffer in --
  1024. * we populate up the whole page, and hence the other metadata
  1025. * in the same page. This optimization is only valid when the
  1026. * filesystem block size is not smaller than the page size.
  1027. */
  1028. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1029. ((bp->b_flags & (XBF_READ|_XBF_PAGE_LOCKED)) ==
  1030. (XBF_READ|_XBF_PAGE_LOCKED)) &&
  1031. (blocksize >= PAGE_CACHE_SIZE)) {
  1032. bio = bio_alloc(GFP_NOIO, 1);
  1033. bio->bi_bdev = bp->b_target->bt_bdev;
  1034. bio->bi_sector = sector - (offset >> BBSHIFT);
  1035. bio->bi_end_io = xfs_buf_bio_end_io;
  1036. bio->bi_private = bp;
  1037. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1038. size = 0;
  1039. atomic_inc(&bp->b_io_remaining);
  1040. goto submit_io;
  1041. }
  1042. next_chunk:
  1043. atomic_inc(&bp->b_io_remaining);
  1044. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1045. if (nr_pages > total_nr_pages)
  1046. nr_pages = total_nr_pages;
  1047. bio = bio_alloc(GFP_NOIO, nr_pages);
  1048. bio->bi_bdev = bp->b_target->bt_bdev;
  1049. bio->bi_sector = sector;
  1050. bio->bi_end_io = xfs_buf_bio_end_io;
  1051. bio->bi_private = bp;
  1052. for (; size && nr_pages; nr_pages--, map_i++) {
  1053. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1054. if (nbytes > size)
  1055. nbytes = size;
  1056. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1057. if (rbytes < nbytes)
  1058. break;
  1059. offset = 0;
  1060. sector += nbytes >> BBSHIFT;
  1061. size -= nbytes;
  1062. total_nr_pages--;
  1063. }
  1064. submit_io:
  1065. if (likely(bio->bi_size)) {
  1066. submit_bio(rw, bio);
  1067. if (size)
  1068. goto next_chunk;
  1069. } else {
  1070. bio_put(bio);
  1071. xfs_buf_ioerror(bp, EIO);
  1072. }
  1073. }
  1074. int
  1075. xfs_buf_iorequest(
  1076. xfs_buf_t *bp)
  1077. {
  1078. XB_TRACE(bp, "iorequest", 0);
  1079. if (bp->b_flags & XBF_DELWRI) {
  1080. xfs_buf_delwri_queue(bp, 1);
  1081. return 0;
  1082. }
  1083. if (bp->b_flags & XBF_WRITE) {
  1084. xfs_buf_wait_unpin(bp);
  1085. }
  1086. xfs_buf_hold(bp);
  1087. /* Set the count to 1 initially, this will stop an I/O
  1088. * completion callout which happens before we have started
  1089. * all the I/O from calling xfs_buf_ioend too early.
  1090. */
  1091. atomic_set(&bp->b_io_remaining, 1);
  1092. _xfs_buf_ioapply(bp);
  1093. _xfs_buf_ioend(bp, 0);
  1094. xfs_buf_rele(bp);
  1095. return 0;
  1096. }
  1097. /*
  1098. * Waits for I/O to complete on the buffer supplied.
  1099. * It returns immediately if no I/O is pending.
  1100. * It returns the I/O error code, if any, or 0 if there was no error.
  1101. */
  1102. int
  1103. xfs_buf_iowait(
  1104. xfs_buf_t *bp)
  1105. {
  1106. XB_TRACE(bp, "iowait", 0);
  1107. if (atomic_read(&bp->b_io_remaining))
  1108. blk_run_address_space(bp->b_target->bt_mapping);
  1109. down(&bp->b_iodonesema);
  1110. XB_TRACE(bp, "iowaited", (long)bp->b_error);
  1111. return bp->b_error;
  1112. }
  1113. xfs_caddr_t
  1114. xfs_buf_offset(
  1115. xfs_buf_t *bp,
  1116. size_t offset)
  1117. {
  1118. struct page *page;
  1119. if (bp->b_flags & XBF_MAPPED)
  1120. return XFS_BUF_PTR(bp) + offset;
  1121. offset += bp->b_offset;
  1122. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1123. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1124. }
  1125. /*
  1126. * Move data into or out of a buffer.
  1127. */
  1128. void
  1129. xfs_buf_iomove(
  1130. xfs_buf_t *bp, /* buffer to process */
  1131. size_t boff, /* starting buffer offset */
  1132. size_t bsize, /* length to copy */
  1133. caddr_t data, /* data address */
  1134. xfs_buf_rw_t mode) /* read/write/zero flag */
  1135. {
  1136. size_t bend, cpoff, csize;
  1137. struct page *page;
  1138. bend = boff + bsize;
  1139. while (boff < bend) {
  1140. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1141. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1142. csize = min_t(size_t,
  1143. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1144. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1145. switch (mode) {
  1146. case XBRW_ZERO:
  1147. memset(page_address(page) + cpoff, 0, csize);
  1148. break;
  1149. case XBRW_READ:
  1150. memcpy(data, page_address(page) + cpoff, csize);
  1151. break;
  1152. case XBRW_WRITE:
  1153. memcpy(page_address(page) + cpoff, data, csize);
  1154. }
  1155. boff += csize;
  1156. data += csize;
  1157. }
  1158. }
  1159. /*
  1160. * Handling of buffer targets (buftargs).
  1161. */
  1162. /*
  1163. * Wait for any bufs with callbacks that have been submitted but
  1164. * have not yet returned... walk the hash list for the target.
  1165. */
  1166. void
  1167. xfs_wait_buftarg(
  1168. xfs_buftarg_t *btp)
  1169. {
  1170. xfs_buf_t *bp, *n;
  1171. xfs_bufhash_t *hash;
  1172. uint i;
  1173. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1174. hash = &btp->bt_hash[i];
  1175. again:
  1176. spin_lock(&hash->bh_lock);
  1177. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  1178. ASSERT(btp == bp->b_target);
  1179. if (!(bp->b_flags & XBF_FS_MANAGED)) {
  1180. spin_unlock(&hash->bh_lock);
  1181. /*
  1182. * Catch superblock reference count leaks
  1183. * immediately
  1184. */
  1185. BUG_ON(bp->b_bn == 0);
  1186. delay(100);
  1187. goto again;
  1188. }
  1189. }
  1190. spin_unlock(&hash->bh_lock);
  1191. }
  1192. }
  1193. /*
  1194. * Allocate buffer hash table for a given target.
  1195. * For devices containing metadata (i.e. not the log/realtime devices)
  1196. * we need to allocate a much larger hash table.
  1197. */
  1198. STATIC void
  1199. xfs_alloc_bufhash(
  1200. xfs_buftarg_t *btp,
  1201. int external)
  1202. {
  1203. unsigned int i;
  1204. btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
  1205. btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
  1206. btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
  1207. sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
  1208. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1209. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1210. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1211. }
  1212. }
  1213. STATIC void
  1214. xfs_free_bufhash(
  1215. xfs_buftarg_t *btp)
  1216. {
  1217. kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t));
  1218. btp->bt_hash = NULL;
  1219. }
  1220. /*
  1221. * buftarg list for delwrite queue processing
  1222. */
  1223. static LIST_HEAD(xfs_buftarg_list);
  1224. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1225. STATIC void
  1226. xfs_register_buftarg(
  1227. xfs_buftarg_t *btp)
  1228. {
  1229. spin_lock(&xfs_buftarg_lock);
  1230. list_add(&btp->bt_list, &xfs_buftarg_list);
  1231. spin_unlock(&xfs_buftarg_lock);
  1232. }
  1233. STATIC void
  1234. xfs_unregister_buftarg(
  1235. xfs_buftarg_t *btp)
  1236. {
  1237. spin_lock(&xfs_buftarg_lock);
  1238. list_del(&btp->bt_list);
  1239. spin_unlock(&xfs_buftarg_lock);
  1240. }
  1241. void
  1242. xfs_free_buftarg(
  1243. xfs_buftarg_t *btp,
  1244. int external)
  1245. {
  1246. xfs_flush_buftarg(btp, 1);
  1247. xfs_blkdev_issue_flush(btp);
  1248. if (external)
  1249. xfs_blkdev_put(btp->bt_bdev);
  1250. xfs_free_bufhash(btp);
  1251. iput(btp->bt_mapping->host);
  1252. /* Unregister the buftarg first so that we don't get a
  1253. * wakeup finding a non-existent task
  1254. */
  1255. xfs_unregister_buftarg(btp);
  1256. kthread_stop(btp->bt_task);
  1257. kmem_free(btp, sizeof(*btp));
  1258. }
  1259. STATIC int
  1260. xfs_setsize_buftarg_flags(
  1261. xfs_buftarg_t *btp,
  1262. unsigned int blocksize,
  1263. unsigned int sectorsize,
  1264. int verbose)
  1265. {
  1266. btp->bt_bsize = blocksize;
  1267. btp->bt_sshift = ffs(sectorsize) - 1;
  1268. btp->bt_smask = sectorsize - 1;
  1269. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1270. printk(KERN_WARNING
  1271. "XFS: Cannot set_blocksize to %u on device %s\n",
  1272. sectorsize, XFS_BUFTARG_NAME(btp));
  1273. return EINVAL;
  1274. }
  1275. if (verbose &&
  1276. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1277. printk(KERN_WARNING
  1278. "XFS: %u byte sectors in use on device %s. "
  1279. "This is suboptimal; %u or greater is ideal.\n",
  1280. sectorsize, XFS_BUFTARG_NAME(btp),
  1281. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1282. }
  1283. return 0;
  1284. }
  1285. /*
  1286. * When allocating the initial buffer target we have not yet
  1287. * read in the superblock, so don't know what sized sectors
  1288. * are being used is at this early stage. Play safe.
  1289. */
  1290. STATIC int
  1291. xfs_setsize_buftarg_early(
  1292. xfs_buftarg_t *btp,
  1293. struct block_device *bdev)
  1294. {
  1295. return xfs_setsize_buftarg_flags(btp,
  1296. PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
  1297. }
  1298. int
  1299. xfs_setsize_buftarg(
  1300. xfs_buftarg_t *btp,
  1301. unsigned int blocksize,
  1302. unsigned int sectorsize)
  1303. {
  1304. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1305. }
  1306. STATIC int
  1307. xfs_mapping_buftarg(
  1308. xfs_buftarg_t *btp,
  1309. struct block_device *bdev)
  1310. {
  1311. struct backing_dev_info *bdi;
  1312. struct inode *inode;
  1313. struct address_space *mapping;
  1314. static const struct address_space_operations mapping_aops = {
  1315. .sync_page = block_sync_page,
  1316. .migratepage = fail_migrate_page,
  1317. };
  1318. inode = new_inode(bdev->bd_inode->i_sb);
  1319. if (!inode) {
  1320. printk(KERN_WARNING
  1321. "XFS: Cannot allocate mapping inode for device %s\n",
  1322. XFS_BUFTARG_NAME(btp));
  1323. return ENOMEM;
  1324. }
  1325. inode->i_mode = S_IFBLK;
  1326. inode->i_bdev = bdev;
  1327. inode->i_rdev = bdev->bd_dev;
  1328. bdi = blk_get_backing_dev_info(bdev);
  1329. if (!bdi)
  1330. bdi = &default_backing_dev_info;
  1331. mapping = &inode->i_data;
  1332. mapping->a_ops = &mapping_aops;
  1333. mapping->backing_dev_info = bdi;
  1334. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1335. btp->bt_mapping = mapping;
  1336. return 0;
  1337. }
  1338. STATIC int
  1339. xfs_alloc_delwrite_queue(
  1340. xfs_buftarg_t *btp)
  1341. {
  1342. int error = 0;
  1343. INIT_LIST_HEAD(&btp->bt_list);
  1344. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1345. spin_lock_init(&btp->bt_delwrite_lock);
  1346. btp->bt_flags = 0;
  1347. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
  1348. if (IS_ERR(btp->bt_task)) {
  1349. error = PTR_ERR(btp->bt_task);
  1350. goto out_error;
  1351. }
  1352. xfs_register_buftarg(btp);
  1353. out_error:
  1354. return error;
  1355. }
  1356. xfs_buftarg_t *
  1357. xfs_alloc_buftarg(
  1358. struct block_device *bdev,
  1359. int external)
  1360. {
  1361. xfs_buftarg_t *btp;
  1362. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1363. btp->bt_dev = bdev->bd_dev;
  1364. btp->bt_bdev = bdev;
  1365. if (xfs_setsize_buftarg_early(btp, bdev))
  1366. goto error;
  1367. if (xfs_mapping_buftarg(btp, bdev))
  1368. goto error;
  1369. if (xfs_alloc_delwrite_queue(btp))
  1370. goto error;
  1371. xfs_alloc_bufhash(btp, external);
  1372. return btp;
  1373. error:
  1374. kmem_free(btp, sizeof(*btp));
  1375. return NULL;
  1376. }
  1377. /*
  1378. * Delayed write buffer handling
  1379. */
  1380. STATIC void
  1381. xfs_buf_delwri_queue(
  1382. xfs_buf_t *bp,
  1383. int unlock)
  1384. {
  1385. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1386. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1387. XB_TRACE(bp, "delwri_q", (long)unlock);
  1388. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1389. spin_lock(dwlk);
  1390. /* If already in the queue, dequeue and place at tail */
  1391. if (!list_empty(&bp->b_list)) {
  1392. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1393. if (unlock)
  1394. atomic_dec(&bp->b_hold);
  1395. list_del(&bp->b_list);
  1396. }
  1397. bp->b_flags |= _XBF_DELWRI_Q;
  1398. list_add_tail(&bp->b_list, dwq);
  1399. bp->b_queuetime = jiffies;
  1400. spin_unlock(dwlk);
  1401. if (unlock)
  1402. xfs_buf_unlock(bp);
  1403. }
  1404. void
  1405. xfs_buf_delwri_dequeue(
  1406. xfs_buf_t *bp)
  1407. {
  1408. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1409. int dequeued = 0;
  1410. spin_lock(dwlk);
  1411. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1412. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1413. list_del_init(&bp->b_list);
  1414. dequeued = 1;
  1415. }
  1416. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1417. spin_unlock(dwlk);
  1418. if (dequeued)
  1419. xfs_buf_rele(bp);
  1420. XB_TRACE(bp, "delwri_dq", (long)dequeued);
  1421. }
  1422. STATIC void
  1423. xfs_buf_runall_queues(
  1424. struct workqueue_struct *queue)
  1425. {
  1426. flush_workqueue(queue);
  1427. }
  1428. STATIC int
  1429. xfsbufd_wakeup(
  1430. int priority,
  1431. gfp_t mask)
  1432. {
  1433. xfs_buftarg_t *btp;
  1434. spin_lock(&xfs_buftarg_lock);
  1435. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1436. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1437. continue;
  1438. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1439. wake_up_process(btp->bt_task);
  1440. }
  1441. spin_unlock(&xfs_buftarg_lock);
  1442. return 0;
  1443. }
  1444. /*
  1445. * Move as many buffers as specified to the supplied list
  1446. * idicating if we skipped any buffers to prevent deadlocks.
  1447. */
  1448. STATIC int
  1449. xfs_buf_delwri_split(
  1450. xfs_buftarg_t *target,
  1451. struct list_head *list,
  1452. unsigned long age)
  1453. {
  1454. xfs_buf_t *bp, *n;
  1455. struct list_head *dwq = &target->bt_delwrite_queue;
  1456. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1457. int skipped = 0;
  1458. int force;
  1459. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1460. INIT_LIST_HEAD(list);
  1461. spin_lock(dwlk);
  1462. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1463. XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
  1464. ASSERT(bp->b_flags & XBF_DELWRI);
  1465. if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
  1466. if (!force &&
  1467. time_before(jiffies, bp->b_queuetime + age)) {
  1468. xfs_buf_unlock(bp);
  1469. break;
  1470. }
  1471. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1472. _XBF_RUN_QUEUES);
  1473. bp->b_flags |= XBF_WRITE;
  1474. list_move_tail(&bp->b_list, list);
  1475. } else
  1476. skipped++;
  1477. }
  1478. spin_unlock(dwlk);
  1479. return skipped;
  1480. }
  1481. STATIC int
  1482. xfsbufd(
  1483. void *data)
  1484. {
  1485. struct list_head tmp;
  1486. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1487. int count;
  1488. xfs_buf_t *bp;
  1489. current->flags |= PF_MEMALLOC;
  1490. set_freezable();
  1491. do {
  1492. if (unlikely(freezing(current))) {
  1493. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1494. refrigerator();
  1495. } else {
  1496. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1497. }
  1498. schedule_timeout_interruptible(
  1499. xfs_buf_timer_centisecs * msecs_to_jiffies(10));
  1500. xfs_buf_delwri_split(target, &tmp,
  1501. xfs_buf_age_centisecs * msecs_to_jiffies(10));
  1502. count = 0;
  1503. while (!list_empty(&tmp)) {
  1504. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1505. ASSERT(target == bp->b_target);
  1506. list_del_init(&bp->b_list);
  1507. xfs_buf_iostrategy(bp);
  1508. count++;
  1509. }
  1510. if (as_list_len > 0)
  1511. purge_addresses();
  1512. if (count)
  1513. blk_run_address_space(target->bt_mapping);
  1514. } while (!kthread_should_stop());
  1515. return 0;
  1516. }
  1517. /*
  1518. * Go through all incore buffers, and release buffers if they belong to
  1519. * the given device. This is used in filesystem error handling to
  1520. * preserve the consistency of its metadata.
  1521. */
  1522. int
  1523. xfs_flush_buftarg(
  1524. xfs_buftarg_t *target,
  1525. int wait)
  1526. {
  1527. struct list_head tmp;
  1528. xfs_buf_t *bp, *n;
  1529. int pincount = 0;
  1530. xfs_buf_runall_queues(xfsdatad_workqueue);
  1531. xfs_buf_runall_queues(xfslogd_workqueue);
  1532. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1533. pincount = xfs_buf_delwri_split(target, &tmp, 0);
  1534. /*
  1535. * Dropped the delayed write list lock, now walk the temporary list
  1536. */
  1537. list_for_each_entry_safe(bp, n, &tmp, b_list) {
  1538. ASSERT(target == bp->b_target);
  1539. if (wait)
  1540. bp->b_flags &= ~XBF_ASYNC;
  1541. else
  1542. list_del_init(&bp->b_list);
  1543. xfs_buf_iostrategy(bp);
  1544. }
  1545. if (wait)
  1546. blk_run_address_space(target->bt_mapping);
  1547. /*
  1548. * Remaining list items must be flushed before returning
  1549. */
  1550. while (!list_empty(&tmp)) {
  1551. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1552. list_del_init(&bp->b_list);
  1553. xfs_iowait(bp);
  1554. xfs_buf_relse(bp);
  1555. }
  1556. return pincount;
  1557. }
  1558. int __init
  1559. xfs_buf_init(void)
  1560. {
  1561. #ifdef XFS_BUF_TRACE
  1562. xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP);
  1563. #endif
  1564. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1565. KM_ZONE_HWALIGN, NULL);
  1566. if (!xfs_buf_zone)
  1567. goto out_free_trace_buf;
  1568. xfslogd_workqueue = create_workqueue("xfslogd");
  1569. if (!xfslogd_workqueue)
  1570. goto out_free_buf_zone;
  1571. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1572. if (!xfsdatad_workqueue)
  1573. goto out_destroy_xfslogd_workqueue;
  1574. register_shrinker(&xfs_buf_shake);
  1575. return 0;
  1576. out_destroy_xfslogd_workqueue:
  1577. destroy_workqueue(xfslogd_workqueue);
  1578. out_free_buf_zone:
  1579. kmem_zone_destroy(xfs_buf_zone);
  1580. out_free_trace_buf:
  1581. #ifdef XFS_BUF_TRACE
  1582. ktrace_free(xfs_buf_trace_buf);
  1583. #endif
  1584. return -ENOMEM;
  1585. }
  1586. void
  1587. xfs_buf_terminate(void)
  1588. {
  1589. unregister_shrinker(&xfs_buf_shake);
  1590. destroy_workqueue(xfsdatad_workqueue);
  1591. destroy_workqueue(xfslogd_workqueue);
  1592. kmem_zone_destroy(xfs_buf_zone);
  1593. #ifdef XFS_BUF_TRACE
  1594. ktrace_free(xfs_buf_trace_buf);
  1595. #endif
  1596. }
  1597. #ifdef CONFIG_KDB_MODULES
  1598. struct list_head *
  1599. xfs_get_buftarg_list(void)
  1600. {
  1601. return &xfs_buftarg_list;
  1602. }
  1603. #endif