xfs_buf.c 41 KB

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