xfs_buf.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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. STATIC 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. * Create a skeletal pagebuf (no pages associated with it).
  614. */
  615. xfs_buf_t *
  616. pagebuf_lookup(
  617. xfs_buftarg_t *target,
  618. loff_t ioff,
  619. size_t isize,
  620. page_buf_flags_t flags)
  621. {
  622. xfs_buf_t *pb;
  623. pb = pagebuf_allocate(flags);
  624. if (pb) {
  625. _pagebuf_initialize(pb, target, ioff, isize, flags);
  626. }
  627. return pb;
  628. }
  629. /*
  630. * If we are not low on memory then do the readahead in a deadlock
  631. * safe manner.
  632. */
  633. void
  634. pagebuf_readahead(
  635. xfs_buftarg_t *target,
  636. loff_t ioff,
  637. size_t isize,
  638. page_buf_flags_t flags)
  639. {
  640. struct backing_dev_info *bdi;
  641. bdi = target->pbr_mapping->backing_dev_info;
  642. if (bdi_read_congested(bdi))
  643. return;
  644. flags |= (PBF_TRYLOCK|PBF_ASYNC|PBF_READ_AHEAD);
  645. xfs_buf_read_flags(target, ioff, isize, flags);
  646. }
  647. xfs_buf_t *
  648. pagebuf_get_empty(
  649. size_t len,
  650. xfs_buftarg_t *target)
  651. {
  652. xfs_buf_t *pb;
  653. pb = pagebuf_allocate(0);
  654. if (pb)
  655. _pagebuf_initialize(pb, target, 0, len, 0);
  656. return pb;
  657. }
  658. static inline struct page *
  659. mem_to_page(
  660. void *addr)
  661. {
  662. if (((unsigned long)addr < VMALLOC_START) ||
  663. ((unsigned long)addr >= VMALLOC_END)) {
  664. return virt_to_page(addr);
  665. } else {
  666. return vmalloc_to_page(addr);
  667. }
  668. }
  669. int
  670. pagebuf_associate_memory(
  671. xfs_buf_t *pb,
  672. void *mem,
  673. size_t len)
  674. {
  675. int rval;
  676. int i = 0;
  677. size_t ptr;
  678. size_t end, end_cur;
  679. off_t offset;
  680. int page_count;
  681. page_count = PAGE_CACHE_ALIGN(len) >> PAGE_CACHE_SHIFT;
  682. offset = (off_t) mem - ((off_t)mem & PAGE_CACHE_MASK);
  683. if (offset && (len > PAGE_CACHE_SIZE))
  684. page_count++;
  685. /* Free any previous set of page pointers */
  686. if (pb->pb_pages)
  687. _pagebuf_free_pages(pb);
  688. pb->pb_pages = NULL;
  689. pb->pb_addr = mem;
  690. rval = _pagebuf_get_pages(pb, page_count, 0);
  691. if (rval)
  692. return rval;
  693. pb->pb_offset = offset;
  694. ptr = (size_t) mem & PAGE_CACHE_MASK;
  695. end = PAGE_CACHE_ALIGN((size_t) mem + len);
  696. end_cur = end;
  697. /* set up first page */
  698. pb->pb_pages[0] = mem_to_page(mem);
  699. ptr += PAGE_CACHE_SIZE;
  700. pb->pb_page_count = ++i;
  701. while (ptr < end) {
  702. pb->pb_pages[i] = mem_to_page((void *)ptr);
  703. pb->pb_page_count = ++i;
  704. ptr += PAGE_CACHE_SIZE;
  705. }
  706. pb->pb_locked = 0;
  707. pb->pb_count_desired = pb->pb_buffer_length = len;
  708. pb->pb_flags |= PBF_MAPPED;
  709. return 0;
  710. }
  711. xfs_buf_t *
  712. pagebuf_get_no_daddr(
  713. size_t len,
  714. xfs_buftarg_t *target)
  715. {
  716. size_t malloc_len = len;
  717. xfs_buf_t *bp;
  718. void *data;
  719. int error;
  720. bp = pagebuf_allocate(0);
  721. if (unlikely(bp == NULL))
  722. goto fail;
  723. _pagebuf_initialize(bp, target, 0, len, PBF_FORCEIO);
  724. try_again:
  725. data = kmem_alloc(malloc_len, KM_SLEEP | KM_MAYFAIL);
  726. if (unlikely(data == NULL))
  727. goto fail_free_buf;
  728. /* check whether alignment matches.. */
  729. if ((__psunsigned_t)data !=
  730. ((__psunsigned_t)data & ~target->pbr_smask)) {
  731. /* .. else double the size and try again */
  732. kmem_free(data, malloc_len);
  733. malloc_len <<= 1;
  734. goto try_again;
  735. }
  736. error = pagebuf_associate_memory(bp, data, len);
  737. if (error)
  738. goto fail_free_mem;
  739. bp->pb_flags |= _PBF_KMEM_ALLOC;
  740. pagebuf_unlock(bp);
  741. PB_TRACE(bp, "no_daddr", data);
  742. return bp;
  743. fail_free_mem:
  744. kmem_free(data, malloc_len);
  745. fail_free_buf:
  746. pagebuf_free(bp);
  747. fail:
  748. return NULL;
  749. }
  750. /*
  751. * pagebuf_hold
  752. *
  753. * Increment reference count on buffer, to hold the buffer concurrently
  754. * with another thread which may release (free) the buffer asynchronously.
  755. *
  756. * Must hold the buffer already to call this function.
  757. */
  758. void
  759. pagebuf_hold(
  760. xfs_buf_t *pb)
  761. {
  762. atomic_inc(&pb->pb_hold);
  763. PB_TRACE(pb, "hold", 0);
  764. }
  765. /*
  766. * pagebuf_rele
  767. *
  768. * pagebuf_rele releases a hold on the specified buffer. If the
  769. * the hold count is 1, pagebuf_rele calls pagebuf_free.
  770. */
  771. void
  772. pagebuf_rele(
  773. xfs_buf_t *pb)
  774. {
  775. xfs_bufhash_t *hash = pb->pb_hash;
  776. PB_TRACE(pb, "rele", pb->pb_relse);
  777. /*
  778. * pagebuf_lookup buffers are not hashed, not delayed write,
  779. * and don't have their own release routines. Special case.
  780. */
  781. if (unlikely(!hash)) {
  782. ASSERT(!pb->pb_relse);
  783. if (atomic_dec_and_test(&pb->pb_hold))
  784. xfs_buf_free(pb);
  785. return;
  786. }
  787. if (atomic_dec_and_lock(&pb->pb_hold, &hash->bh_lock)) {
  788. int do_free = 1;
  789. if (pb->pb_relse) {
  790. atomic_inc(&pb->pb_hold);
  791. spin_unlock(&hash->bh_lock);
  792. (*(pb->pb_relse)) (pb);
  793. spin_lock(&hash->bh_lock);
  794. do_free = 0;
  795. }
  796. if (pb->pb_flags & PBF_DELWRI) {
  797. pb->pb_flags |= PBF_ASYNC;
  798. atomic_inc(&pb->pb_hold);
  799. pagebuf_delwri_queue(pb, 0);
  800. do_free = 0;
  801. } else if (pb->pb_flags & PBF_FS_MANAGED) {
  802. do_free = 0;
  803. }
  804. if (do_free) {
  805. list_del_init(&pb->pb_hash_list);
  806. spin_unlock(&hash->bh_lock);
  807. pagebuf_free(pb);
  808. } else {
  809. spin_unlock(&hash->bh_lock);
  810. }
  811. }
  812. }
  813. /*
  814. * Mutual exclusion on buffers. Locking model:
  815. *
  816. * Buffers associated with inodes for which buffer locking
  817. * is not enabled are not protected by semaphores, and are
  818. * assumed to be exclusively owned by the caller. There is a
  819. * spinlock in the buffer, used by the caller when concurrent
  820. * access is possible.
  821. */
  822. /*
  823. * pagebuf_cond_lock
  824. *
  825. * pagebuf_cond_lock locks a buffer object, if it is not already locked.
  826. * Note that this in no way
  827. * locks the underlying pages, so it is only useful for synchronizing
  828. * concurrent use of page buffer objects, not for synchronizing independent
  829. * access to the underlying pages.
  830. */
  831. int
  832. pagebuf_cond_lock( /* lock buffer, if not locked */
  833. /* returns -EBUSY if locked) */
  834. xfs_buf_t *pb)
  835. {
  836. int locked;
  837. locked = down_trylock(&pb->pb_sema) == 0;
  838. if (locked) {
  839. PB_SET_OWNER(pb);
  840. }
  841. PB_TRACE(pb, "cond_lock", (long)locked);
  842. return(locked ? 0 : -EBUSY);
  843. }
  844. #if defined(DEBUG) || defined(XFS_BLI_TRACE)
  845. /*
  846. * pagebuf_lock_value
  847. *
  848. * Return lock value for a pagebuf
  849. */
  850. int
  851. pagebuf_lock_value(
  852. xfs_buf_t *pb)
  853. {
  854. return(atomic_read(&pb->pb_sema.count));
  855. }
  856. #endif
  857. /*
  858. * pagebuf_lock
  859. *
  860. * pagebuf_lock locks a buffer object. Note that this in no way
  861. * locks the underlying pages, so it is only useful for synchronizing
  862. * concurrent use of page buffer objects, not for synchronizing independent
  863. * access to the underlying pages.
  864. */
  865. int
  866. pagebuf_lock(
  867. xfs_buf_t *pb)
  868. {
  869. PB_TRACE(pb, "lock", 0);
  870. if (atomic_read(&pb->pb_io_remaining))
  871. blk_run_address_space(pb->pb_target->pbr_mapping);
  872. down(&pb->pb_sema);
  873. PB_SET_OWNER(pb);
  874. PB_TRACE(pb, "locked", 0);
  875. return 0;
  876. }
  877. /*
  878. * pagebuf_unlock
  879. *
  880. * pagebuf_unlock releases the lock on the buffer object created by
  881. * pagebuf_lock or pagebuf_cond_lock (not any
  882. * pinning of underlying pages created by pagebuf_pin).
  883. */
  884. void
  885. pagebuf_unlock( /* unlock buffer */
  886. xfs_buf_t *pb) /* buffer to unlock */
  887. {
  888. PB_CLEAR_OWNER(pb);
  889. up(&pb->pb_sema);
  890. PB_TRACE(pb, "unlock", 0);
  891. }
  892. /*
  893. * Pinning Buffer Storage in Memory
  894. */
  895. /*
  896. * pagebuf_pin
  897. *
  898. * pagebuf_pin locks all of the memory represented by a buffer in
  899. * memory. Multiple calls to pagebuf_pin and pagebuf_unpin, for
  900. * the same or different buffers affecting a given page, will
  901. * properly count the number of outstanding "pin" requests. The
  902. * buffer may be released after the pagebuf_pin and a different
  903. * buffer used when calling pagebuf_unpin, if desired.
  904. * pagebuf_pin should be used by the file system when it wants be
  905. * assured that no attempt will be made to force the affected
  906. * memory to disk. It does not assure that a given logical page
  907. * will not be moved to a different physical page.
  908. */
  909. void
  910. pagebuf_pin(
  911. xfs_buf_t *pb)
  912. {
  913. atomic_inc(&pb->pb_pin_count);
  914. PB_TRACE(pb, "pin", (long)pb->pb_pin_count.counter);
  915. }
  916. /*
  917. * pagebuf_unpin
  918. *
  919. * pagebuf_unpin reverses the locking of memory performed by
  920. * pagebuf_pin. Note that both functions affected the logical
  921. * pages associated with the buffer, not the buffer itself.
  922. */
  923. void
  924. pagebuf_unpin(
  925. xfs_buf_t *pb)
  926. {
  927. if (atomic_dec_and_test(&pb->pb_pin_count)) {
  928. wake_up_all(&pb->pb_waiters);
  929. }
  930. PB_TRACE(pb, "unpin", (long)pb->pb_pin_count.counter);
  931. }
  932. int
  933. pagebuf_ispin(
  934. xfs_buf_t *pb)
  935. {
  936. return atomic_read(&pb->pb_pin_count);
  937. }
  938. /*
  939. * pagebuf_wait_unpin
  940. *
  941. * pagebuf_wait_unpin waits until all of the memory associated
  942. * with the buffer is not longer locked in memory. It returns
  943. * immediately if none of the affected pages are locked.
  944. */
  945. static inline void
  946. _pagebuf_wait_unpin(
  947. xfs_buf_t *pb)
  948. {
  949. DECLARE_WAITQUEUE (wait, current);
  950. if (atomic_read(&pb->pb_pin_count) == 0)
  951. return;
  952. add_wait_queue(&pb->pb_waiters, &wait);
  953. for (;;) {
  954. set_current_state(TASK_UNINTERRUPTIBLE);
  955. if (atomic_read(&pb->pb_pin_count) == 0)
  956. break;
  957. if (atomic_read(&pb->pb_io_remaining))
  958. blk_run_address_space(pb->pb_target->pbr_mapping);
  959. schedule();
  960. }
  961. remove_wait_queue(&pb->pb_waiters, &wait);
  962. set_current_state(TASK_RUNNING);
  963. }
  964. /*
  965. * Buffer Utility Routines
  966. */
  967. /*
  968. * pagebuf_iodone
  969. *
  970. * pagebuf_iodone marks a buffer for which I/O is in progress
  971. * done with respect to that I/O. The pb_iodone routine, if
  972. * present, will be called as a side-effect.
  973. */
  974. STATIC void
  975. pagebuf_iodone_work(
  976. void *v)
  977. {
  978. xfs_buf_t *bp = (xfs_buf_t *)v;
  979. if (bp->pb_iodone)
  980. (*(bp->pb_iodone))(bp);
  981. else if (bp->pb_flags & PBF_ASYNC)
  982. xfs_buf_relse(bp);
  983. }
  984. void
  985. pagebuf_iodone(
  986. xfs_buf_t *pb,
  987. int dataio,
  988. int schedule)
  989. {
  990. pb->pb_flags &= ~(PBF_READ | PBF_WRITE);
  991. if (pb->pb_error == 0) {
  992. pb->pb_flags &= ~(PBF_PARTIAL | PBF_NONE);
  993. }
  994. PB_TRACE(pb, "iodone", pb->pb_iodone);
  995. if ((pb->pb_iodone) || (pb->pb_flags & PBF_ASYNC)) {
  996. if (schedule) {
  997. INIT_WORK(&pb->pb_iodone_work, pagebuf_iodone_work, pb);
  998. queue_work(dataio ? xfsdatad_workqueue :
  999. xfslogd_workqueue, &pb->pb_iodone_work);
  1000. } else {
  1001. pagebuf_iodone_work(pb);
  1002. }
  1003. } else {
  1004. up(&pb->pb_iodonesema);
  1005. }
  1006. }
  1007. /*
  1008. * pagebuf_ioerror
  1009. *
  1010. * pagebuf_ioerror sets the error code for a buffer.
  1011. */
  1012. void
  1013. pagebuf_ioerror( /* mark/clear buffer error flag */
  1014. xfs_buf_t *pb, /* buffer to mark */
  1015. int error) /* error to store (0 if none) */
  1016. {
  1017. ASSERT(error >= 0 && error <= 0xffff);
  1018. pb->pb_error = (unsigned short)error;
  1019. PB_TRACE(pb, "ioerror", (unsigned long)error);
  1020. }
  1021. /*
  1022. * pagebuf_iostart
  1023. *
  1024. * pagebuf_iostart initiates I/O on a buffer, based on the flags supplied.
  1025. * If necessary, it will arrange for any disk space allocation required,
  1026. * and it will break up the request if the block mappings require it.
  1027. * The pb_iodone routine in the buffer supplied will only be called
  1028. * when all of the subsidiary I/O requests, if any, have been completed.
  1029. * pagebuf_iostart calls the pagebuf_ioinitiate routine or
  1030. * pagebuf_iorequest, if the former routine is not defined, to start
  1031. * the I/O on a given low-level request.
  1032. */
  1033. int
  1034. pagebuf_iostart( /* start I/O on a buffer */
  1035. xfs_buf_t *pb, /* buffer to start */
  1036. page_buf_flags_t flags) /* PBF_LOCK, PBF_ASYNC, PBF_READ, */
  1037. /* PBF_WRITE, PBF_DELWRI, */
  1038. /* PBF_DONT_BLOCK */
  1039. {
  1040. int status = 0;
  1041. PB_TRACE(pb, "iostart", (unsigned long)flags);
  1042. if (flags & PBF_DELWRI) {
  1043. pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC);
  1044. pb->pb_flags |= flags & (PBF_DELWRI | PBF_ASYNC);
  1045. pagebuf_delwri_queue(pb, 1);
  1046. return status;
  1047. }
  1048. pb->pb_flags &= ~(PBF_READ | PBF_WRITE | PBF_ASYNC | PBF_DELWRI | \
  1049. PBF_READ_AHEAD | _PBF_RUN_QUEUES);
  1050. pb->pb_flags |= flags & (PBF_READ | PBF_WRITE | PBF_ASYNC | \
  1051. PBF_READ_AHEAD | _PBF_RUN_QUEUES);
  1052. BUG_ON(pb->pb_bn == XFS_BUF_DADDR_NULL);
  1053. /* For writes allow an alternate strategy routine to precede
  1054. * the actual I/O request (which may not be issued at all in
  1055. * a shutdown situation, for example).
  1056. */
  1057. status = (flags & PBF_WRITE) ?
  1058. pagebuf_iostrategy(pb) : pagebuf_iorequest(pb);
  1059. /* Wait for I/O if we are not an async request.
  1060. * Note: async I/O request completion will release the buffer,
  1061. * and that can already be done by this point. So using the
  1062. * buffer pointer from here on, after async I/O, is invalid.
  1063. */
  1064. if (!status && !(flags & PBF_ASYNC))
  1065. status = pagebuf_iowait(pb);
  1066. return status;
  1067. }
  1068. /*
  1069. * Helper routine for pagebuf_iorequest
  1070. */
  1071. STATIC __inline__ int
  1072. _pagebuf_iolocked(
  1073. xfs_buf_t *pb)
  1074. {
  1075. ASSERT(pb->pb_flags & (PBF_READ|PBF_WRITE));
  1076. if (pb->pb_flags & PBF_READ)
  1077. return pb->pb_locked;
  1078. return 0;
  1079. }
  1080. STATIC __inline__ void
  1081. _pagebuf_iodone(
  1082. xfs_buf_t *pb,
  1083. int schedule)
  1084. {
  1085. if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
  1086. pb->pb_locked = 0;
  1087. pagebuf_iodone(pb, (pb->pb_flags & PBF_FS_DATAIOD), schedule);
  1088. }
  1089. }
  1090. STATIC int
  1091. bio_end_io_pagebuf(
  1092. struct bio *bio,
  1093. unsigned int bytes_done,
  1094. int error)
  1095. {
  1096. xfs_buf_t *pb = (xfs_buf_t *)bio->bi_private;
  1097. unsigned int blocksize = pb->pb_target->pbr_bsize;
  1098. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  1099. if (bio->bi_size)
  1100. return 1;
  1101. if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1102. pb->pb_error = EIO;
  1103. do {
  1104. struct page *page = bvec->bv_page;
  1105. if (unlikely(pb->pb_error)) {
  1106. if (pb->pb_flags & PBF_READ)
  1107. ClearPageUptodate(page);
  1108. SetPageError(page);
  1109. } else if (blocksize == PAGE_CACHE_SIZE) {
  1110. SetPageUptodate(page);
  1111. } else if (!PagePrivate(page) &&
  1112. (pb->pb_flags & _PBF_PAGE_CACHE)) {
  1113. set_page_region(page, bvec->bv_offset, bvec->bv_len);
  1114. }
  1115. if (--bvec >= bio->bi_io_vec)
  1116. prefetchw(&bvec->bv_page->flags);
  1117. if (_pagebuf_iolocked(pb)) {
  1118. unlock_page(page);
  1119. }
  1120. } while (bvec >= bio->bi_io_vec);
  1121. _pagebuf_iodone(pb, 1);
  1122. bio_put(bio);
  1123. return 0;
  1124. }
  1125. STATIC void
  1126. _pagebuf_ioapply(
  1127. xfs_buf_t *pb)
  1128. {
  1129. int i, rw, map_i, total_nr_pages, nr_pages;
  1130. struct bio *bio;
  1131. int offset = pb->pb_offset;
  1132. int size = pb->pb_count_desired;
  1133. sector_t sector = pb->pb_bn;
  1134. unsigned int blocksize = pb->pb_target->pbr_bsize;
  1135. int locking = _pagebuf_iolocked(pb);
  1136. total_nr_pages = pb->pb_page_count;
  1137. map_i = 0;
  1138. if (pb->pb_flags & _PBF_RUN_QUEUES) {
  1139. pb->pb_flags &= ~_PBF_RUN_QUEUES;
  1140. rw = (pb->pb_flags & PBF_READ) ? READ_SYNC : WRITE_SYNC;
  1141. } else {
  1142. rw = (pb->pb_flags & PBF_READ) ? READ : WRITE;
  1143. }
  1144. /* Special code path for reading a sub page size pagebuf in --
  1145. * we populate up the whole page, and hence the other metadata
  1146. * in the same page. This optimization is only valid when the
  1147. * filesystem block size and the page size are equal.
  1148. */
  1149. if ((pb->pb_buffer_length < PAGE_CACHE_SIZE) &&
  1150. (pb->pb_flags & PBF_READ) && locking &&
  1151. (blocksize == PAGE_CACHE_SIZE)) {
  1152. bio = bio_alloc(GFP_NOIO, 1);
  1153. bio->bi_bdev = pb->pb_target->pbr_bdev;
  1154. bio->bi_sector = sector - (offset >> BBSHIFT);
  1155. bio->bi_end_io = bio_end_io_pagebuf;
  1156. bio->bi_private = pb;
  1157. bio_add_page(bio, pb->pb_pages[0], PAGE_CACHE_SIZE, 0);
  1158. size = 0;
  1159. atomic_inc(&pb->pb_io_remaining);
  1160. goto submit_io;
  1161. }
  1162. /* Lock down the pages which we need to for the request */
  1163. if (locking && (pb->pb_flags & PBF_WRITE) && (pb->pb_locked == 0)) {
  1164. for (i = 0; size; i++) {
  1165. int nbytes = PAGE_CACHE_SIZE - offset;
  1166. struct page *page = pb->pb_pages[i];
  1167. if (nbytes > size)
  1168. nbytes = size;
  1169. lock_page(page);
  1170. size -= nbytes;
  1171. offset = 0;
  1172. }
  1173. offset = pb->pb_offset;
  1174. size = pb->pb_count_desired;
  1175. }
  1176. next_chunk:
  1177. atomic_inc(&pb->pb_io_remaining);
  1178. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1179. if (nr_pages > total_nr_pages)
  1180. nr_pages = total_nr_pages;
  1181. bio = bio_alloc(GFP_NOIO, nr_pages);
  1182. bio->bi_bdev = pb->pb_target->pbr_bdev;
  1183. bio->bi_sector = sector;
  1184. bio->bi_end_io = bio_end_io_pagebuf;
  1185. bio->bi_private = pb;
  1186. for (; size && nr_pages; nr_pages--, map_i++) {
  1187. int nbytes = PAGE_CACHE_SIZE - offset;
  1188. if (nbytes > size)
  1189. nbytes = size;
  1190. if (bio_add_page(bio, pb->pb_pages[map_i],
  1191. nbytes, offset) < nbytes)
  1192. break;
  1193. offset = 0;
  1194. sector += nbytes >> BBSHIFT;
  1195. size -= nbytes;
  1196. total_nr_pages--;
  1197. }
  1198. submit_io:
  1199. if (likely(bio->bi_size)) {
  1200. submit_bio(rw, bio);
  1201. if (size)
  1202. goto next_chunk;
  1203. } else {
  1204. bio_put(bio);
  1205. pagebuf_ioerror(pb, EIO);
  1206. }
  1207. }
  1208. /*
  1209. * pagebuf_iorequest -- the core I/O request routine.
  1210. */
  1211. int
  1212. pagebuf_iorequest( /* start real I/O */
  1213. xfs_buf_t *pb) /* buffer to convey to device */
  1214. {
  1215. PB_TRACE(pb, "iorequest", 0);
  1216. if (pb->pb_flags & PBF_DELWRI) {
  1217. pagebuf_delwri_queue(pb, 1);
  1218. return 0;
  1219. }
  1220. if (pb->pb_flags & PBF_WRITE) {
  1221. _pagebuf_wait_unpin(pb);
  1222. }
  1223. pagebuf_hold(pb);
  1224. /* Set the count to 1 initially, this will stop an I/O
  1225. * completion callout which happens before we have started
  1226. * all the I/O from calling pagebuf_iodone too early.
  1227. */
  1228. atomic_set(&pb->pb_io_remaining, 1);
  1229. _pagebuf_ioapply(pb);
  1230. _pagebuf_iodone(pb, 0);
  1231. pagebuf_rele(pb);
  1232. return 0;
  1233. }
  1234. /*
  1235. * pagebuf_iowait
  1236. *
  1237. * pagebuf_iowait waits for I/O to complete on the buffer supplied.
  1238. * It returns immediately if no I/O is pending. In any case, it returns
  1239. * the error code, if any, or 0 if there is no error.
  1240. */
  1241. int
  1242. pagebuf_iowait(
  1243. xfs_buf_t *pb)
  1244. {
  1245. PB_TRACE(pb, "iowait", 0);
  1246. if (atomic_read(&pb->pb_io_remaining))
  1247. blk_run_address_space(pb->pb_target->pbr_mapping);
  1248. down(&pb->pb_iodonesema);
  1249. PB_TRACE(pb, "iowaited", (long)pb->pb_error);
  1250. return pb->pb_error;
  1251. }
  1252. caddr_t
  1253. pagebuf_offset(
  1254. xfs_buf_t *pb,
  1255. size_t offset)
  1256. {
  1257. struct page *page;
  1258. offset += pb->pb_offset;
  1259. page = pb->pb_pages[offset >> PAGE_CACHE_SHIFT];
  1260. return (caddr_t) page_address(page) + (offset & (PAGE_CACHE_SIZE - 1));
  1261. }
  1262. /*
  1263. * pagebuf_iomove
  1264. *
  1265. * Move data into or out of a buffer.
  1266. */
  1267. void
  1268. pagebuf_iomove(
  1269. xfs_buf_t *pb, /* buffer to process */
  1270. size_t boff, /* starting buffer offset */
  1271. size_t bsize, /* length to copy */
  1272. caddr_t data, /* data address */
  1273. page_buf_rw_t mode) /* read/write flag */
  1274. {
  1275. size_t bend, cpoff, csize;
  1276. struct page *page;
  1277. bend = boff + bsize;
  1278. while (boff < bend) {
  1279. page = pb->pb_pages[page_buf_btoct(boff + pb->pb_offset)];
  1280. cpoff = page_buf_poff(boff + pb->pb_offset);
  1281. csize = min_t(size_t,
  1282. PAGE_CACHE_SIZE-cpoff, pb->pb_count_desired-boff);
  1283. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1284. switch (mode) {
  1285. case PBRW_ZERO:
  1286. memset(page_address(page) + cpoff, 0, csize);
  1287. break;
  1288. case PBRW_READ:
  1289. memcpy(data, page_address(page) + cpoff, csize);
  1290. break;
  1291. case PBRW_WRITE:
  1292. memcpy(page_address(page) + cpoff, data, csize);
  1293. }
  1294. boff += csize;
  1295. data += csize;
  1296. }
  1297. }
  1298. /*
  1299. * Handling of buftargs.
  1300. */
  1301. /*
  1302. * Wait for any bufs with callbacks that have been submitted but
  1303. * have not yet returned... walk the hash list for the target.
  1304. */
  1305. void
  1306. xfs_wait_buftarg(
  1307. xfs_buftarg_t *btp)
  1308. {
  1309. xfs_buf_t *bp, *n;
  1310. xfs_bufhash_t *hash;
  1311. uint i;
  1312. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1313. hash = &btp->bt_hash[i];
  1314. again:
  1315. spin_lock(&hash->bh_lock);
  1316. list_for_each_entry_safe(bp, n, &hash->bh_list, pb_hash_list) {
  1317. ASSERT(btp == bp->pb_target);
  1318. if (!(bp->pb_flags & PBF_FS_MANAGED)) {
  1319. spin_unlock(&hash->bh_lock);
  1320. delay(100);
  1321. goto again;
  1322. }
  1323. }
  1324. spin_unlock(&hash->bh_lock);
  1325. }
  1326. }
  1327. /*
  1328. * Allocate buffer hash table for a given target.
  1329. * For devices containing metadata (i.e. not the log/realtime devices)
  1330. * we need to allocate a much larger hash table.
  1331. */
  1332. STATIC void
  1333. xfs_alloc_bufhash(
  1334. xfs_buftarg_t *btp,
  1335. int external)
  1336. {
  1337. unsigned int i;
  1338. btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
  1339. btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
  1340. btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
  1341. sizeof(xfs_bufhash_t), KM_SLEEP);
  1342. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1343. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1344. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1345. }
  1346. }
  1347. STATIC void
  1348. xfs_free_bufhash(
  1349. xfs_buftarg_t *btp)
  1350. {
  1351. kmem_free(btp->bt_hash,
  1352. (1 << btp->bt_hashshift) * sizeof(xfs_bufhash_t));
  1353. btp->bt_hash = NULL;
  1354. }
  1355. void
  1356. xfs_free_buftarg(
  1357. xfs_buftarg_t *btp,
  1358. int external)
  1359. {
  1360. xfs_flush_buftarg(btp, 1);
  1361. if (external)
  1362. xfs_blkdev_put(btp->pbr_bdev);
  1363. xfs_free_bufhash(btp);
  1364. iput(btp->pbr_mapping->host);
  1365. kmem_free(btp, sizeof(*btp));
  1366. }
  1367. STATIC int
  1368. xfs_setsize_buftarg_flags(
  1369. xfs_buftarg_t *btp,
  1370. unsigned int blocksize,
  1371. unsigned int sectorsize,
  1372. int verbose)
  1373. {
  1374. btp->pbr_bsize = blocksize;
  1375. btp->pbr_sshift = ffs(sectorsize) - 1;
  1376. btp->pbr_smask = sectorsize - 1;
  1377. if (set_blocksize(btp->pbr_bdev, sectorsize)) {
  1378. printk(KERN_WARNING
  1379. "XFS: Cannot set_blocksize to %u on device %s\n",
  1380. sectorsize, XFS_BUFTARG_NAME(btp));
  1381. return EINVAL;
  1382. }
  1383. if (verbose &&
  1384. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1385. printk(KERN_WARNING
  1386. "XFS: %u byte sectors in use on device %s. "
  1387. "This is suboptimal; %u or greater is ideal.\n",
  1388. sectorsize, XFS_BUFTARG_NAME(btp),
  1389. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1390. }
  1391. return 0;
  1392. }
  1393. /*
  1394. * When allocating the initial buffer target we have not yet
  1395. * read in the superblock, so don't know what sized sectors
  1396. * are being used is at this early stage. Play safe.
  1397. */
  1398. STATIC int
  1399. xfs_setsize_buftarg_early(
  1400. xfs_buftarg_t *btp,
  1401. struct block_device *bdev)
  1402. {
  1403. return xfs_setsize_buftarg_flags(btp,
  1404. PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
  1405. }
  1406. int
  1407. xfs_setsize_buftarg(
  1408. xfs_buftarg_t *btp,
  1409. unsigned int blocksize,
  1410. unsigned int sectorsize)
  1411. {
  1412. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1413. }
  1414. STATIC int
  1415. xfs_mapping_buftarg(
  1416. xfs_buftarg_t *btp,
  1417. struct block_device *bdev)
  1418. {
  1419. struct backing_dev_info *bdi;
  1420. struct inode *inode;
  1421. struct address_space *mapping;
  1422. static struct address_space_operations mapping_aops = {
  1423. .sync_page = block_sync_page,
  1424. };
  1425. inode = new_inode(bdev->bd_inode->i_sb);
  1426. if (!inode) {
  1427. printk(KERN_WARNING
  1428. "XFS: Cannot allocate mapping inode for device %s\n",
  1429. XFS_BUFTARG_NAME(btp));
  1430. return ENOMEM;
  1431. }
  1432. inode->i_mode = S_IFBLK;
  1433. inode->i_bdev = bdev;
  1434. inode->i_rdev = bdev->bd_dev;
  1435. bdi = blk_get_backing_dev_info(bdev);
  1436. if (!bdi)
  1437. bdi = &default_backing_dev_info;
  1438. mapping = &inode->i_data;
  1439. mapping->a_ops = &mapping_aops;
  1440. mapping->backing_dev_info = bdi;
  1441. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1442. btp->pbr_mapping = mapping;
  1443. return 0;
  1444. }
  1445. xfs_buftarg_t *
  1446. xfs_alloc_buftarg(
  1447. struct block_device *bdev,
  1448. int external)
  1449. {
  1450. xfs_buftarg_t *btp;
  1451. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1452. btp->pbr_dev = bdev->bd_dev;
  1453. btp->pbr_bdev = bdev;
  1454. if (xfs_setsize_buftarg_early(btp, bdev))
  1455. goto error;
  1456. if (xfs_mapping_buftarg(btp, bdev))
  1457. goto error;
  1458. xfs_alloc_bufhash(btp, external);
  1459. return btp;
  1460. error:
  1461. kmem_free(btp, sizeof(*btp));
  1462. return NULL;
  1463. }
  1464. /*
  1465. * Pagebuf delayed write buffer handling
  1466. */
  1467. STATIC LIST_HEAD(pbd_delwrite_queue);
  1468. STATIC DEFINE_SPINLOCK(pbd_delwrite_lock);
  1469. STATIC void
  1470. pagebuf_delwri_queue(
  1471. xfs_buf_t *pb,
  1472. int unlock)
  1473. {
  1474. PB_TRACE(pb, "delwri_q", (long)unlock);
  1475. ASSERT(pb->pb_flags & PBF_DELWRI);
  1476. spin_lock(&pbd_delwrite_lock);
  1477. /* If already in the queue, dequeue and place at tail */
  1478. if (!list_empty(&pb->pb_list)) {
  1479. if (unlock) {
  1480. atomic_dec(&pb->pb_hold);
  1481. }
  1482. list_del(&pb->pb_list);
  1483. }
  1484. list_add_tail(&pb->pb_list, &pbd_delwrite_queue);
  1485. pb->pb_queuetime = jiffies;
  1486. spin_unlock(&pbd_delwrite_lock);
  1487. if (unlock)
  1488. pagebuf_unlock(pb);
  1489. }
  1490. void
  1491. pagebuf_delwri_dequeue(
  1492. xfs_buf_t *pb)
  1493. {
  1494. int dequeued = 0;
  1495. spin_lock(&pbd_delwrite_lock);
  1496. if ((pb->pb_flags & PBF_DELWRI) && !list_empty(&pb->pb_list)) {
  1497. list_del_init(&pb->pb_list);
  1498. dequeued = 1;
  1499. }
  1500. pb->pb_flags &= ~PBF_DELWRI;
  1501. spin_unlock(&pbd_delwrite_lock);
  1502. if (dequeued)
  1503. pagebuf_rele(pb);
  1504. PB_TRACE(pb, "delwri_dq", (long)dequeued);
  1505. }
  1506. STATIC void
  1507. pagebuf_runall_queues(
  1508. struct workqueue_struct *queue)
  1509. {
  1510. flush_workqueue(queue);
  1511. }
  1512. /* Defines for pagebuf daemon */
  1513. STATIC DECLARE_COMPLETION(xfsbufd_done);
  1514. STATIC struct task_struct *xfsbufd_task;
  1515. STATIC int xfsbufd_active;
  1516. STATIC int xfsbufd_force_flush;
  1517. STATIC int xfsbufd_force_sleep;
  1518. STATIC int
  1519. xfsbufd_wakeup(
  1520. int priority,
  1521. unsigned int mask)
  1522. {
  1523. if (xfsbufd_force_sleep)
  1524. return 0;
  1525. xfsbufd_force_flush = 1;
  1526. barrier();
  1527. wake_up_process(xfsbufd_task);
  1528. return 0;
  1529. }
  1530. STATIC int
  1531. xfsbufd(
  1532. void *data)
  1533. {
  1534. struct list_head tmp;
  1535. unsigned long age;
  1536. xfs_buftarg_t *target;
  1537. xfs_buf_t *pb, *n;
  1538. /* Set up the thread */
  1539. daemonize("xfsbufd");
  1540. current->flags |= PF_MEMALLOC;
  1541. xfsbufd_task = current;
  1542. xfsbufd_active = 1;
  1543. barrier();
  1544. INIT_LIST_HEAD(&tmp);
  1545. do {
  1546. if (unlikely(freezing(current))) {
  1547. xfsbufd_force_sleep = 1;
  1548. refrigerator();
  1549. } else {
  1550. xfsbufd_force_sleep = 0;
  1551. }
  1552. set_current_state(TASK_INTERRUPTIBLE);
  1553. schedule_timeout((xfs_buf_timer_centisecs * HZ) / 100);
  1554. age = (xfs_buf_age_centisecs * HZ) / 100;
  1555. spin_lock(&pbd_delwrite_lock);
  1556. list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
  1557. PB_TRACE(pb, "walkq1", (long)pagebuf_ispin(pb));
  1558. ASSERT(pb->pb_flags & PBF_DELWRI);
  1559. if (!pagebuf_ispin(pb) && !pagebuf_cond_lock(pb)) {
  1560. if (!xfsbufd_force_flush &&
  1561. time_before(jiffies,
  1562. pb->pb_queuetime + age)) {
  1563. pagebuf_unlock(pb);
  1564. break;
  1565. }
  1566. pb->pb_flags &= ~PBF_DELWRI;
  1567. pb->pb_flags |= PBF_WRITE;
  1568. list_move(&pb->pb_list, &tmp);
  1569. }
  1570. }
  1571. spin_unlock(&pbd_delwrite_lock);
  1572. while (!list_empty(&tmp)) {
  1573. pb = list_entry(tmp.next, xfs_buf_t, pb_list);
  1574. target = pb->pb_target;
  1575. list_del_init(&pb->pb_list);
  1576. pagebuf_iostrategy(pb);
  1577. blk_run_address_space(target->pbr_mapping);
  1578. }
  1579. if (as_list_len > 0)
  1580. purge_addresses();
  1581. xfsbufd_force_flush = 0;
  1582. } while (xfsbufd_active);
  1583. complete_and_exit(&xfsbufd_done, 0);
  1584. }
  1585. /*
  1586. * Go through all incore buffers, and release buffers if they belong to
  1587. * the given device. This is used in filesystem error handling to
  1588. * preserve the consistency of its metadata.
  1589. */
  1590. int
  1591. xfs_flush_buftarg(
  1592. xfs_buftarg_t *target,
  1593. int wait)
  1594. {
  1595. struct list_head tmp;
  1596. xfs_buf_t *pb, *n;
  1597. int pincount = 0;
  1598. pagebuf_runall_queues(xfsdatad_workqueue);
  1599. pagebuf_runall_queues(xfslogd_workqueue);
  1600. INIT_LIST_HEAD(&tmp);
  1601. spin_lock(&pbd_delwrite_lock);
  1602. list_for_each_entry_safe(pb, n, &pbd_delwrite_queue, pb_list) {
  1603. if (pb->pb_target != target)
  1604. continue;
  1605. ASSERT(pb->pb_flags & PBF_DELWRI);
  1606. PB_TRACE(pb, "walkq2", (long)pagebuf_ispin(pb));
  1607. if (pagebuf_ispin(pb)) {
  1608. pincount++;
  1609. continue;
  1610. }
  1611. pb->pb_flags &= ~PBF_DELWRI;
  1612. pb->pb_flags |= PBF_WRITE;
  1613. list_move(&pb->pb_list, &tmp);
  1614. }
  1615. spin_unlock(&pbd_delwrite_lock);
  1616. /*
  1617. * Dropped the delayed write list lock, now walk the temporary list
  1618. */
  1619. list_for_each_entry_safe(pb, n, &tmp, pb_list) {
  1620. if (wait)
  1621. pb->pb_flags &= ~PBF_ASYNC;
  1622. else
  1623. list_del_init(&pb->pb_list);
  1624. pagebuf_lock(pb);
  1625. pagebuf_iostrategy(pb);
  1626. }
  1627. /*
  1628. * Remaining list items must be flushed before returning
  1629. */
  1630. while (!list_empty(&tmp)) {
  1631. pb = list_entry(tmp.next, xfs_buf_t, pb_list);
  1632. list_del_init(&pb->pb_list);
  1633. xfs_iowait(pb);
  1634. xfs_buf_relse(pb);
  1635. }
  1636. if (wait)
  1637. blk_run_address_space(target->pbr_mapping);
  1638. return pincount;
  1639. }
  1640. STATIC int
  1641. xfs_buf_daemons_start(void)
  1642. {
  1643. int error = -ENOMEM;
  1644. xfslogd_workqueue = create_workqueue("xfslogd");
  1645. if (!xfslogd_workqueue)
  1646. goto out;
  1647. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1648. if (!xfsdatad_workqueue)
  1649. goto out_destroy_xfslogd_workqueue;
  1650. error = kernel_thread(xfsbufd, NULL, CLONE_FS|CLONE_FILES);
  1651. if (error < 0)
  1652. goto out_destroy_xfsdatad_workqueue;
  1653. return 0;
  1654. out_destroy_xfsdatad_workqueue:
  1655. destroy_workqueue(xfsdatad_workqueue);
  1656. out_destroy_xfslogd_workqueue:
  1657. destroy_workqueue(xfslogd_workqueue);
  1658. out:
  1659. return error;
  1660. }
  1661. /*
  1662. * Note: do not mark as __exit, it is called from pagebuf_terminate.
  1663. */
  1664. STATIC void
  1665. xfs_buf_daemons_stop(void)
  1666. {
  1667. xfsbufd_active = 0;
  1668. barrier();
  1669. wait_for_completion(&xfsbufd_done);
  1670. destroy_workqueue(xfslogd_workqueue);
  1671. destroy_workqueue(xfsdatad_workqueue);
  1672. }
  1673. /*
  1674. * Initialization and Termination
  1675. */
  1676. int __init
  1677. pagebuf_init(void)
  1678. {
  1679. int error = -ENOMEM;
  1680. pagebuf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buf");
  1681. if (!pagebuf_zone)
  1682. goto out;
  1683. #ifdef PAGEBUF_TRACE
  1684. pagebuf_trace_buf = ktrace_alloc(PAGEBUF_TRACE_SIZE, KM_SLEEP);
  1685. #endif
  1686. error = xfs_buf_daemons_start();
  1687. if (error)
  1688. goto out_free_buf_zone;
  1689. pagebuf_shake = kmem_shake_register(xfsbufd_wakeup);
  1690. if (!pagebuf_shake) {
  1691. error = -ENOMEM;
  1692. goto out_stop_daemons;
  1693. }
  1694. return 0;
  1695. out_stop_daemons:
  1696. xfs_buf_daemons_stop();
  1697. out_free_buf_zone:
  1698. #ifdef PAGEBUF_TRACE
  1699. ktrace_free(pagebuf_trace_buf);
  1700. #endif
  1701. kmem_zone_destroy(pagebuf_zone);
  1702. out:
  1703. return error;
  1704. }
  1705. /*
  1706. * pagebuf_terminate.
  1707. *
  1708. * Note: do not mark as __exit, this is also called from the __init code.
  1709. */
  1710. void
  1711. pagebuf_terminate(void)
  1712. {
  1713. xfs_buf_daemons_stop();
  1714. #ifdef PAGEBUF_TRACE
  1715. ktrace_free(pagebuf_trace_buf);
  1716. #endif
  1717. kmem_zone_destroy(pagebuf_zone);
  1718. kmem_shake_deregister(pagebuf_shake);
  1719. }