xfs_buf.c 42 KB

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