xfs_buf.c 41 KB

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