xfs_buf.c 41 KB

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