xfs_buf.c 41 KB

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