xfs_buf.c 41 KB

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