xfs_buf.c 44 KB

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