xfs_buf.c 42 KB

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