xfs_buf.c 42 KB

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