xfs_buf.c 41 KB

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