xfs_buf.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include <linux/stddef.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/init.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/bio.h>
  26. #include <linux/sysctl.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/percpu.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/hash.h>
  32. #include <linux/kthread.h>
  33. #include <linux/migrate.h>
  34. #include <linux/backing-dev.h>
  35. #include <linux/freezer.h>
  36. static kmem_zone_t *xfs_buf_zone;
  37. static kmem_shaker_t xfs_buf_shake;
  38. STATIC int xfsbufd(void *);
  39. STATIC int xfsbufd_wakeup(int, gfp_t);
  40. STATIC void xfs_buf_delwri_queue(xfs_buf_t *, int);
  41. static struct workqueue_struct *xfslogd_workqueue;
  42. struct workqueue_struct *xfsdatad_workqueue;
  43. #ifdef XFS_BUF_TRACE
  44. void
  45. xfs_buf_trace(
  46. xfs_buf_t *bp,
  47. char *id,
  48. void *data,
  49. void *ra)
  50. {
  51. ktrace_enter(xfs_buf_trace_buf,
  52. bp, id,
  53. (void *)(unsigned long)bp->b_flags,
  54. (void *)(unsigned long)bp->b_hold.counter,
  55. (void *)(unsigned long)bp->b_sema.count.counter,
  56. (void *)current,
  57. data, ra,
  58. (void *)(unsigned long)((bp->b_file_offset>>32) & 0xffffffff),
  59. (void *)(unsigned long)(bp->b_file_offset & 0xffffffff),
  60. (void *)(unsigned long)bp->b_buffer_length,
  61. NULL, NULL, NULL, NULL, NULL);
  62. }
  63. ktrace_t *xfs_buf_trace_buf;
  64. #define XFS_BUF_TRACE_SIZE 4096
  65. #define XB_TRACE(bp, id, data) \
  66. xfs_buf_trace(bp, id, (void *)data, (void *)__builtin_return_address(0))
  67. #else
  68. #define XB_TRACE(bp, id, data) do { } while (0)
  69. #endif
  70. #ifdef XFS_BUF_LOCK_TRACKING
  71. # define XB_SET_OWNER(bp) ((bp)->b_last_holder = current->pid)
  72. # define XB_CLEAR_OWNER(bp) ((bp)->b_last_holder = -1)
  73. # define XB_GET_OWNER(bp) ((bp)->b_last_holder)
  74. #else
  75. # define XB_SET_OWNER(bp) do { } while (0)
  76. # define XB_CLEAR_OWNER(bp) do { } while (0)
  77. # define XB_GET_OWNER(bp) do { } while (0)
  78. #endif
  79. #define xb_to_gfp(flags) \
  80. ((((flags) & XBF_READ_AHEAD) ? __GFP_NORETRY : \
  81. ((flags) & XBF_DONT_BLOCK) ? GFP_NOFS : GFP_KERNEL) | __GFP_NOWARN)
  82. #define xb_to_km(flags) \
  83. (((flags) & XBF_DONT_BLOCK) ? KM_NOFS : KM_SLEEP)
  84. #define xfs_buf_allocate(flags) \
  85. kmem_zone_alloc(xfs_buf_zone, xb_to_km(flags))
  86. #define xfs_buf_deallocate(bp) \
  87. kmem_zone_free(xfs_buf_zone, (bp));
  88. /*
  89. * Page Region interfaces.
  90. *
  91. * For pages in filesystems where the blocksize is smaller than the
  92. * pagesize, we use the page->private field (long) to hold a bitmap
  93. * of uptodate regions within the page.
  94. *
  95. * Each such region is "bytes per page / bits per long" bytes long.
  96. *
  97. * NBPPR == number-of-bytes-per-page-region
  98. * BTOPR == bytes-to-page-region (rounded up)
  99. * BTOPRT == bytes-to-page-region-truncated (rounded down)
  100. */
  101. #if (BITS_PER_LONG == 32)
  102. #define PRSHIFT (PAGE_CACHE_SHIFT - 5) /* (32 == 1<<5) */
  103. #elif (BITS_PER_LONG == 64)
  104. #define PRSHIFT (PAGE_CACHE_SHIFT - 6) /* (64 == 1<<6) */
  105. #else
  106. #error BITS_PER_LONG must be 32 or 64
  107. #endif
  108. #define NBPPR (PAGE_CACHE_SIZE/BITS_PER_LONG)
  109. #define BTOPR(b) (((unsigned int)(b) + (NBPPR - 1)) >> PRSHIFT)
  110. #define BTOPRT(b) (((unsigned int)(b) >> PRSHIFT))
  111. STATIC unsigned long
  112. page_region_mask(
  113. size_t offset,
  114. size_t length)
  115. {
  116. unsigned long mask;
  117. int first, final;
  118. first = BTOPR(offset);
  119. final = BTOPRT(offset + length - 1);
  120. first = min(first, final);
  121. mask = ~0UL;
  122. mask <<= BITS_PER_LONG - (final - first);
  123. mask >>= BITS_PER_LONG - (final);
  124. ASSERT(offset + length <= PAGE_CACHE_SIZE);
  125. ASSERT((final - first) < BITS_PER_LONG && (final - first) >= 0);
  126. return mask;
  127. }
  128. STATIC_INLINE void
  129. set_page_region(
  130. struct page *page,
  131. size_t offset,
  132. size_t length)
  133. {
  134. set_page_private(page,
  135. page_private(page) | page_region_mask(offset, length));
  136. if (page_private(page) == ~0UL)
  137. SetPageUptodate(page);
  138. }
  139. STATIC_INLINE int
  140. test_page_region(
  141. struct page *page,
  142. size_t offset,
  143. size_t length)
  144. {
  145. unsigned long mask = page_region_mask(offset, length);
  146. return (mask && (page_private(page) & mask) == mask);
  147. }
  148. /*
  149. * Mapping of multi-page buffers into contiguous virtual space
  150. */
  151. typedef struct a_list {
  152. void *vm_addr;
  153. struct a_list *next;
  154. } a_list_t;
  155. static a_list_t *as_free_head;
  156. static int as_list_len;
  157. static DEFINE_SPINLOCK(as_lock);
  158. /*
  159. * Try to batch vunmaps because they are costly.
  160. */
  161. STATIC void
  162. free_address(
  163. void *addr)
  164. {
  165. a_list_t *aentry;
  166. aentry = kmalloc(sizeof(a_list_t), GFP_NOWAIT);
  167. if (likely(aentry)) {
  168. spin_lock(&as_lock);
  169. aentry->next = as_free_head;
  170. aentry->vm_addr = addr;
  171. as_free_head = aentry;
  172. as_list_len++;
  173. spin_unlock(&as_lock);
  174. } else {
  175. vunmap(addr);
  176. }
  177. }
  178. STATIC void
  179. purge_addresses(void)
  180. {
  181. a_list_t *aentry, *old;
  182. if (as_free_head == NULL)
  183. return;
  184. spin_lock(&as_lock);
  185. aentry = as_free_head;
  186. as_free_head = NULL;
  187. as_list_len = 0;
  188. spin_unlock(&as_lock);
  189. while ((old = aentry) != NULL) {
  190. vunmap(aentry->vm_addr);
  191. aentry = aentry->next;
  192. kfree(old);
  193. }
  194. }
  195. /*
  196. * Internal xfs_buf_t object manipulation
  197. */
  198. STATIC void
  199. _xfs_buf_initialize(
  200. xfs_buf_t *bp,
  201. xfs_buftarg_t *target,
  202. xfs_off_t range_base,
  203. size_t range_length,
  204. xfs_buf_flags_t flags)
  205. {
  206. /*
  207. * We don't want certain flags to appear in b_flags.
  208. */
  209. flags &= ~(XBF_LOCK|XBF_MAPPED|XBF_DONT_BLOCK|XBF_READ_AHEAD);
  210. memset(bp, 0, sizeof(xfs_buf_t));
  211. atomic_set(&bp->b_hold, 1);
  212. init_MUTEX_LOCKED(&bp->b_iodonesema);
  213. INIT_LIST_HEAD(&bp->b_list);
  214. INIT_LIST_HEAD(&bp->b_hash_list);
  215. init_MUTEX_LOCKED(&bp->b_sema); /* held, no waiters */
  216. XB_SET_OWNER(bp);
  217. bp->b_target = target;
  218. bp->b_file_offset = range_base;
  219. /*
  220. * Set buffer_length and count_desired to the same value initially.
  221. * I/O routines should use count_desired, which will be the same in
  222. * most cases but may be reset (e.g. XFS recovery).
  223. */
  224. bp->b_buffer_length = bp->b_count_desired = range_length;
  225. bp->b_flags = flags;
  226. bp->b_bn = XFS_BUF_DADDR_NULL;
  227. atomic_set(&bp->b_pin_count, 0);
  228. init_waitqueue_head(&bp->b_waiters);
  229. XFS_STATS_INC(xb_create);
  230. XB_TRACE(bp, "initialize", target);
  231. }
  232. /*
  233. * Allocate a page array capable of holding a specified number
  234. * of pages, and point the page buf at it.
  235. */
  236. STATIC int
  237. _xfs_buf_get_pages(
  238. xfs_buf_t *bp,
  239. int page_count,
  240. xfs_buf_flags_t flags)
  241. {
  242. /* Make sure that we have a page list */
  243. if (bp->b_pages == NULL) {
  244. bp->b_offset = xfs_buf_poff(bp->b_file_offset);
  245. bp->b_page_count = page_count;
  246. if (page_count <= XB_PAGES) {
  247. bp->b_pages = bp->b_page_array;
  248. } else {
  249. bp->b_pages = kmem_alloc(sizeof(struct page *) *
  250. page_count, xb_to_km(flags));
  251. if (bp->b_pages == NULL)
  252. return -ENOMEM;
  253. }
  254. memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Frees b_pages if it was allocated.
  260. */
  261. STATIC void
  262. _xfs_buf_free_pages(
  263. xfs_buf_t *bp)
  264. {
  265. if (bp->b_pages != bp->b_page_array) {
  266. kmem_free(bp->b_pages,
  267. bp->b_page_count * sizeof(struct page *));
  268. }
  269. }
  270. /*
  271. * Releases the specified buffer.
  272. *
  273. * The modification state of any associated pages is left unchanged.
  274. * The buffer most not be on any hash - use xfs_buf_rele instead for
  275. * hashed and refcounted buffers
  276. */
  277. void
  278. xfs_buf_free(
  279. xfs_buf_t *bp)
  280. {
  281. XB_TRACE(bp, "free", 0);
  282. ASSERT(list_empty(&bp->b_hash_list));
  283. if (bp->b_flags & (_XBF_PAGE_CACHE|_XBF_PAGES)) {
  284. uint i;
  285. if ((bp->b_flags & XBF_MAPPED) && (bp->b_page_count > 1))
  286. free_address(bp->b_addr - bp->b_offset);
  287. for (i = 0; i < bp->b_page_count; i++) {
  288. struct page *page = bp->b_pages[i];
  289. if (bp->b_flags & _XBF_PAGE_CACHE)
  290. ASSERT(!PagePrivate(page));
  291. page_cache_release(page);
  292. }
  293. _xfs_buf_free_pages(bp);
  294. }
  295. xfs_buf_deallocate(bp);
  296. }
  297. /*
  298. * Finds all pages for buffer in question and builds it's page list.
  299. */
  300. STATIC int
  301. _xfs_buf_lookup_pages(
  302. xfs_buf_t *bp,
  303. uint flags)
  304. {
  305. struct address_space *mapping = bp->b_target->bt_mapping;
  306. size_t blocksize = bp->b_target->bt_bsize;
  307. size_t size = bp->b_count_desired;
  308. size_t nbytes, offset;
  309. gfp_t gfp_mask = xb_to_gfp(flags);
  310. unsigned short page_count, i;
  311. pgoff_t first;
  312. xfs_off_t end;
  313. int error;
  314. end = bp->b_file_offset + bp->b_buffer_length;
  315. page_count = xfs_buf_btoc(end) - xfs_buf_btoct(bp->b_file_offset);
  316. error = _xfs_buf_get_pages(bp, page_count, flags);
  317. if (unlikely(error))
  318. return error;
  319. bp->b_flags |= _XBF_PAGE_CACHE;
  320. offset = bp->b_offset;
  321. first = bp->b_file_offset >> PAGE_CACHE_SHIFT;
  322. for (i = 0; i < bp->b_page_count; i++) {
  323. struct page *page;
  324. uint retries = 0;
  325. retry:
  326. page = find_or_create_page(mapping, first + i, gfp_mask);
  327. if (unlikely(page == NULL)) {
  328. if (flags & XBF_READ_AHEAD) {
  329. bp->b_page_count = i;
  330. for (i = 0; i < bp->b_page_count; i++)
  331. unlock_page(bp->b_pages[i]);
  332. return -ENOMEM;
  333. }
  334. /*
  335. * This could deadlock.
  336. *
  337. * But until all the XFS lowlevel code is revamped to
  338. * handle buffer allocation failures we can't do much.
  339. */
  340. if (!(++retries % 100))
  341. printk(KERN_ERR
  342. "XFS: possible memory allocation "
  343. "deadlock in %s (mode:0x%x)\n",
  344. __FUNCTION__, gfp_mask);
  345. XFS_STATS_INC(xb_page_retries);
  346. xfsbufd_wakeup(0, gfp_mask);
  347. congestion_wait(WRITE, HZ/50);
  348. goto retry;
  349. }
  350. XFS_STATS_INC(xb_page_found);
  351. nbytes = min_t(size_t, size, PAGE_CACHE_SIZE - offset);
  352. size -= nbytes;
  353. ASSERT(!PagePrivate(page));
  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. xfs_off_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. xfs_off_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 & (xfs_off_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. xfs_off_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. xfs_off_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. xfs_off_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. unsigned long page_count = PAGE_ALIGN(len) >> PAGE_SHIFT;
  664. int error, i;
  665. xfs_buf_t *bp;
  666. bp = xfs_buf_allocate(0);
  667. if (unlikely(bp == NULL))
  668. goto fail;
  669. _xfs_buf_initialize(bp, target, 0, len, 0);
  670. error = _xfs_buf_get_pages(bp, page_count, 0);
  671. if (error)
  672. goto fail_free_buf;
  673. for (i = 0; i < page_count; i++) {
  674. bp->b_pages[i] = alloc_page(GFP_KERNEL);
  675. if (!bp->b_pages[i])
  676. goto fail_free_mem;
  677. }
  678. bp->b_flags |= _XBF_PAGES;
  679. error = _xfs_buf_map_pages(bp, XBF_MAPPED);
  680. if (unlikely(error)) {
  681. printk(KERN_WARNING "%s: failed to map pages\n",
  682. __FUNCTION__);
  683. goto fail_free_mem;
  684. }
  685. xfs_buf_unlock(bp);
  686. XB_TRACE(bp, "no_daddr", len);
  687. return bp;
  688. fail_free_mem:
  689. while (--i >= 0)
  690. __free_page(bp->b_pages[i]);
  691. _xfs_buf_free_pages(bp);
  692. fail_free_buf:
  693. xfs_buf_deallocate(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 (unlikely(!hash)) {
  720. ASSERT(!bp->b_relse);
  721. if (atomic_dec_and_test(&bp->b_hold))
  722. xfs_buf_free(bp);
  723. return;
  724. }
  725. if (atomic_dec_and_lock(&bp->b_hold, &hash->bh_lock)) {
  726. if (bp->b_relse) {
  727. atomic_inc(&bp->b_hold);
  728. spin_unlock(&hash->bh_lock);
  729. (*(bp->b_relse)) (bp);
  730. } else if (bp->b_flags & XBF_FS_MANAGED) {
  731. spin_unlock(&hash->bh_lock);
  732. } else {
  733. ASSERT(!(bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)));
  734. list_del_init(&bp->b_hash_list);
  735. spin_unlock(&hash->bh_lock);
  736. xfs_buf_free(bp);
  737. }
  738. } else {
  739. /*
  740. * Catch reference count leaks
  741. */
  742. ASSERT(atomic_read(&bp->b_hold) >= 0);
  743. }
  744. }
  745. /*
  746. * Mutual exclusion on buffers. Locking model:
  747. *
  748. * Buffers associated with inodes for which buffer locking
  749. * is not enabled are not protected by semaphores, and are
  750. * assumed to be exclusively owned by the caller. There is a
  751. * spinlock in the buffer, used by the caller when concurrent
  752. * access is possible.
  753. */
  754. /*
  755. * Locks a buffer object, if it is not already locked.
  756. * Note that this in no way locks the underlying pages, so it is only
  757. * useful for synchronizing concurrent use of buffer objects, not for
  758. * synchronizing independent access to the underlying pages.
  759. */
  760. int
  761. xfs_buf_cond_lock(
  762. xfs_buf_t *bp)
  763. {
  764. int locked;
  765. locked = down_trylock(&bp->b_sema) == 0;
  766. if (locked) {
  767. XB_SET_OWNER(bp);
  768. }
  769. XB_TRACE(bp, "cond_lock", (long)locked);
  770. return locked ? 0 : -EBUSY;
  771. }
  772. #if defined(DEBUG) || defined(XFS_BLI_TRACE)
  773. int
  774. xfs_buf_lock_value(
  775. xfs_buf_t *bp)
  776. {
  777. return atomic_read(&bp->b_sema.count);
  778. }
  779. #endif
  780. /*
  781. * Locks a buffer object.
  782. * Note that this in no way locks the underlying pages, so it is only
  783. * useful for synchronizing concurrent use of buffer objects, not for
  784. * synchronizing independent access to the underlying pages.
  785. */
  786. void
  787. xfs_buf_lock(
  788. xfs_buf_t *bp)
  789. {
  790. XB_TRACE(bp, "lock", 0);
  791. if (atomic_read(&bp->b_io_remaining))
  792. blk_run_address_space(bp->b_target->bt_mapping);
  793. down(&bp->b_sema);
  794. XB_SET_OWNER(bp);
  795. XB_TRACE(bp, "locked", 0);
  796. }
  797. /*
  798. * Releases the lock on the buffer object.
  799. * If the buffer is marked delwri but is not queued, do so before we
  800. * unlock the buffer as we need to set flags correctly. We also need to
  801. * take a reference for the delwri queue because the unlocker is going to
  802. * drop their's and they don't know we just queued it.
  803. */
  804. void
  805. xfs_buf_unlock(
  806. xfs_buf_t *bp)
  807. {
  808. if ((bp->b_flags & (XBF_DELWRI|_XBF_DELWRI_Q)) == XBF_DELWRI) {
  809. atomic_inc(&bp->b_hold);
  810. bp->b_flags |= XBF_ASYNC;
  811. xfs_buf_delwri_queue(bp, 0);
  812. }
  813. XB_CLEAR_OWNER(bp);
  814. up(&bp->b_sema);
  815. XB_TRACE(bp, "unlock", 0);
  816. }
  817. /*
  818. * Pinning Buffer Storage in Memory
  819. * Ensure that no attempt to force a buffer to disk will succeed.
  820. */
  821. void
  822. xfs_buf_pin(
  823. xfs_buf_t *bp)
  824. {
  825. atomic_inc(&bp->b_pin_count);
  826. XB_TRACE(bp, "pin", (long)bp->b_pin_count.counter);
  827. }
  828. void
  829. xfs_buf_unpin(
  830. xfs_buf_t *bp)
  831. {
  832. if (atomic_dec_and_test(&bp->b_pin_count))
  833. wake_up_all(&bp->b_waiters);
  834. XB_TRACE(bp, "unpin", (long)bp->b_pin_count.counter);
  835. }
  836. int
  837. xfs_buf_ispin(
  838. xfs_buf_t *bp)
  839. {
  840. return atomic_read(&bp->b_pin_count);
  841. }
  842. STATIC void
  843. xfs_buf_wait_unpin(
  844. xfs_buf_t *bp)
  845. {
  846. DECLARE_WAITQUEUE (wait, current);
  847. if (atomic_read(&bp->b_pin_count) == 0)
  848. return;
  849. add_wait_queue(&bp->b_waiters, &wait);
  850. for (;;) {
  851. set_current_state(TASK_UNINTERRUPTIBLE);
  852. if (atomic_read(&bp->b_pin_count) == 0)
  853. break;
  854. if (atomic_read(&bp->b_io_remaining))
  855. blk_run_address_space(bp->b_target->bt_mapping);
  856. schedule();
  857. }
  858. remove_wait_queue(&bp->b_waiters, &wait);
  859. set_current_state(TASK_RUNNING);
  860. }
  861. /*
  862. * Buffer Utility Routines
  863. */
  864. STATIC void
  865. xfs_buf_iodone_work(
  866. struct work_struct *work)
  867. {
  868. xfs_buf_t *bp =
  869. container_of(work, xfs_buf_t, b_iodone_work);
  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);
  887. queue_work(xfslogd_workqueue, &bp->b_iodone_work);
  888. } else {
  889. xfs_buf_iodone_work(&bp->b_iodone_work);
  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. ASSERT(!PagePrivate(page));
  977. if (unlikely(bp->b_error)) {
  978. if (bp->b_flags & XBF_READ)
  979. ClearPageUptodate(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_ORDERED) {
  1010. ASSERT(!(bp->b_flags & XBF_READ));
  1011. rw = WRITE_BARRIER;
  1012. } else if (bp->b_flags & _XBF_RUN_QUEUES) {
  1013. ASSERT(!(bp->b_flags & XBF_READ_AHEAD));
  1014. bp->b_flags &= ~_XBF_RUN_QUEUES;
  1015. rw = (bp->b_flags & XBF_WRITE) ? WRITE_SYNC : READ_SYNC;
  1016. } else {
  1017. rw = (bp->b_flags & XBF_WRITE) ? WRITE :
  1018. (bp->b_flags & XBF_READ_AHEAD) ? READA : READ;
  1019. }
  1020. /* Special code path for reading a sub page size buffer in --
  1021. * we populate up the whole page, and hence the other metadata
  1022. * in the same page. This optimization is only valid when the
  1023. * filesystem block size is not smaller than the page size.
  1024. */
  1025. if ((bp->b_buffer_length < PAGE_CACHE_SIZE) &&
  1026. (bp->b_flags & XBF_READ) && locking &&
  1027. (blocksize >= PAGE_CACHE_SIZE)) {
  1028. bio = bio_alloc(GFP_NOIO, 1);
  1029. bio->bi_bdev = bp->b_target->bt_bdev;
  1030. bio->bi_sector = sector - (offset >> BBSHIFT);
  1031. bio->bi_end_io = xfs_buf_bio_end_io;
  1032. bio->bi_private = bp;
  1033. bio_add_page(bio, bp->b_pages[0], PAGE_CACHE_SIZE, 0);
  1034. size = 0;
  1035. atomic_inc(&bp->b_io_remaining);
  1036. goto submit_io;
  1037. }
  1038. /* Lock down the pages which we need to for the request */
  1039. if (locking && (bp->b_flags & XBF_WRITE) && (bp->b_locked == 0)) {
  1040. for (i = 0; size; i++) {
  1041. int nbytes = PAGE_CACHE_SIZE - offset;
  1042. struct page *page = bp->b_pages[i];
  1043. if (nbytes > size)
  1044. nbytes = size;
  1045. lock_page(page);
  1046. size -= nbytes;
  1047. offset = 0;
  1048. }
  1049. offset = bp->b_offset;
  1050. size = bp->b_count_desired;
  1051. }
  1052. next_chunk:
  1053. atomic_inc(&bp->b_io_remaining);
  1054. nr_pages = BIO_MAX_SECTORS >> (PAGE_SHIFT - BBSHIFT);
  1055. if (nr_pages > total_nr_pages)
  1056. nr_pages = total_nr_pages;
  1057. bio = bio_alloc(GFP_NOIO, nr_pages);
  1058. bio->bi_bdev = bp->b_target->bt_bdev;
  1059. bio->bi_sector = sector;
  1060. bio->bi_end_io = xfs_buf_bio_end_io;
  1061. bio->bi_private = bp;
  1062. for (; size && nr_pages; nr_pages--, map_i++) {
  1063. int rbytes, nbytes = PAGE_CACHE_SIZE - offset;
  1064. if (nbytes > size)
  1065. nbytes = size;
  1066. rbytes = bio_add_page(bio, bp->b_pages[map_i], nbytes, offset);
  1067. if (rbytes < nbytes)
  1068. break;
  1069. offset = 0;
  1070. sector += nbytes >> BBSHIFT;
  1071. size -= nbytes;
  1072. total_nr_pages--;
  1073. }
  1074. submit_io:
  1075. if (likely(bio->bi_size)) {
  1076. submit_bio(rw, bio);
  1077. if (size)
  1078. goto next_chunk;
  1079. } else {
  1080. bio_put(bio);
  1081. xfs_buf_ioerror(bp, EIO);
  1082. }
  1083. }
  1084. int
  1085. xfs_buf_iorequest(
  1086. xfs_buf_t *bp)
  1087. {
  1088. XB_TRACE(bp, "iorequest", 0);
  1089. if (bp->b_flags & XBF_DELWRI) {
  1090. xfs_buf_delwri_queue(bp, 1);
  1091. return 0;
  1092. }
  1093. if (bp->b_flags & XBF_WRITE) {
  1094. xfs_buf_wait_unpin(bp);
  1095. }
  1096. xfs_buf_hold(bp);
  1097. /* Set the count to 1 initially, this will stop an I/O
  1098. * completion callout which happens before we have started
  1099. * all the I/O from calling xfs_buf_ioend too early.
  1100. */
  1101. atomic_set(&bp->b_io_remaining, 1);
  1102. _xfs_buf_ioapply(bp);
  1103. _xfs_buf_ioend(bp, 0);
  1104. xfs_buf_rele(bp);
  1105. return 0;
  1106. }
  1107. /*
  1108. * Waits for I/O to complete on the buffer supplied.
  1109. * It returns immediately if no I/O is pending.
  1110. * It returns the I/O error code, if any, or 0 if there was no error.
  1111. */
  1112. int
  1113. xfs_buf_iowait(
  1114. xfs_buf_t *bp)
  1115. {
  1116. XB_TRACE(bp, "iowait", 0);
  1117. if (atomic_read(&bp->b_io_remaining))
  1118. blk_run_address_space(bp->b_target->bt_mapping);
  1119. down(&bp->b_iodonesema);
  1120. XB_TRACE(bp, "iowaited", (long)bp->b_error);
  1121. return bp->b_error;
  1122. }
  1123. xfs_caddr_t
  1124. xfs_buf_offset(
  1125. xfs_buf_t *bp,
  1126. size_t offset)
  1127. {
  1128. struct page *page;
  1129. if (bp->b_flags & XBF_MAPPED)
  1130. return XFS_BUF_PTR(bp) + offset;
  1131. offset += bp->b_offset;
  1132. page = bp->b_pages[offset >> PAGE_CACHE_SHIFT];
  1133. return (xfs_caddr_t)page_address(page) + (offset & (PAGE_CACHE_SIZE-1));
  1134. }
  1135. /*
  1136. * Move data into or out of a buffer.
  1137. */
  1138. void
  1139. xfs_buf_iomove(
  1140. xfs_buf_t *bp, /* buffer to process */
  1141. size_t boff, /* starting buffer offset */
  1142. size_t bsize, /* length to copy */
  1143. caddr_t data, /* data address */
  1144. xfs_buf_rw_t mode) /* read/write/zero flag */
  1145. {
  1146. size_t bend, cpoff, csize;
  1147. struct page *page;
  1148. bend = boff + bsize;
  1149. while (boff < bend) {
  1150. page = bp->b_pages[xfs_buf_btoct(boff + bp->b_offset)];
  1151. cpoff = xfs_buf_poff(boff + bp->b_offset);
  1152. csize = min_t(size_t,
  1153. PAGE_CACHE_SIZE-cpoff, bp->b_count_desired-boff);
  1154. ASSERT(((csize + cpoff) <= PAGE_CACHE_SIZE));
  1155. switch (mode) {
  1156. case XBRW_ZERO:
  1157. memset(page_address(page) + cpoff, 0, csize);
  1158. break;
  1159. case XBRW_READ:
  1160. memcpy(data, page_address(page) + cpoff, csize);
  1161. break;
  1162. case XBRW_WRITE:
  1163. memcpy(page_address(page) + cpoff, data, csize);
  1164. }
  1165. boff += csize;
  1166. data += csize;
  1167. }
  1168. }
  1169. /*
  1170. * Handling of buffer targets (buftargs).
  1171. */
  1172. /*
  1173. * Wait for any bufs with callbacks that have been submitted but
  1174. * have not yet returned... walk the hash list for the target.
  1175. */
  1176. void
  1177. xfs_wait_buftarg(
  1178. xfs_buftarg_t *btp)
  1179. {
  1180. xfs_buf_t *bp, *n;
  1181. xfs_bufhash_t *hash;
  1182. uint i;
  1183. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1184. hash = &btp->bt_hash[i];
  1185. again:
  1186. spin_lock(&hash->bh_lock);
  1187. list_for_each_entry_safe(bp, n, &hash->bh_list, b_hash_list) {
  1188. ASSERT(btp == bp->b_target);
  1189. if (!(bp->b_flags & XBF_FS_MANAGED)) {
  1190. spin_unlock(&hash->bh_lock);
  1191. /*
  1192. * Catch superblock reference count leaks
  1193. * immediately
  1194. */
  1195. BUG_ON(bp->b_bn == 0);
  1196. delay(100);
  1197. goto again;
  1198. }
  1199. }
  1200. spin_unlock(&hash->bh_lock);
  1201. }
  1202. }
  1203. /*
  1204. * Allocate buffer hash table for a given target.
  1205. * For devices containing metadata (i.e. not the log/realtime devices)
  1206. * we need to allocate a much larger hash table.
  1207. */
  1208. STATIC void
  1209. xfs_alloc_bufhash(
  1210. xfs_buftarg_t *btp,
  1211. int external)
  1212. {
  1213. unsigned int i;
  1214. btp->bt_hashshift = external ? 3 : 8; /* 8 or 256 buckets */
  1215. btp->bt_hashmask = (1 << btp->bt_hashshift) - 1;
  1216. btp->bt_hash = kmem_zalloc((1 << btp->bt_hashshift) *
  1217. sizeof(xfs_bufhash_t), KM_SLEEP | KM_LARGE);
  1218. for (i = 0; i < (1 << btp->bt_hashshift); i++) {
  1219. spin_lock_init(&btp->bt_hash[i].bh_lock);
  1220. INIT_LIST_HEAD(&btp->bt_hash[i].bh_list);
  1221. }
  1222. }
  1223. STATIC void
  1224. xfs_free_bufhash(
  1225. xfs_buftarg_t *btp)
  1226. {
  1227. kmem_free(btp->bt_hash, (1<<btp->bt_hashshift) * sizeof(xfs_bufhash_t));
  1228. btp->bt_hash = NULL;
  1229. }
  1230. /*
  1231. * buftarg list for delwrite queue processing
  1232. */
  1233. static LIST_HEAD(xfs_buftarg_list);
  1234. static DEFINE_SPINLOCK(xfs_buftarg_lock);
  1235. STATIC void
  1236. xfs_register_buftarg(
  1237. xfs_buftarg_t *btp)
  1238. {
  1239. spin_lock(&xfs_buftarg_lock);
  1240. list_add(&btp->bt_list, &xfs_buftarg_list);
  1241. spin_unlock(&xfs_buftarg_lock);
  1242. }
  1243. STATIC void
  1244. xfs_unregister_buftarg(
  1245. xfs_buftarg_t *btp)
  1246. {
  1247. spin_lock(&xfs_buftarg_lock);
  1248. list_del(&btp->bt_list);
  1249. spin_unlock(&xfs_buftarg_lock);
  1250. }
  1251. void
  1252. xfs_free_buftarg(
  1253. xfs_buftarg_t *btp,
  1254. int external)
  1255. {
  1256. xfs_flush_buftarg(btp, 1);
  1257. if (external)
  1258. xfs_blkdev_put(btp->bt_bdev);
  1259. xfs_free_bufhash(btp);
  1260. iput(btp->bt_mapping->host);
  1261. /* Unregister the buftarg first so that we don't get a
  1262. * wakeup finding a non-existent task
  1263. */
  1264. xfs_unregister_buftarg(btp);
  1265. kthread_stop(btp->bt_task);
  1266. kmem_free(btp, sizeof(*btp));
  1267. }
  1268. STATIC int
  1269. xfs_setsize_buftarg_flags(
  1270. xfs_buftarg_t *btp,
  1271. unsigned int blocksize,
  1272. unsigned int sectorsize,
  1273. int verbose)
  1274. {
  1275. btp->bt_bsize = blocksize;
  1276. btp->bt_sshift = ffs(sectorsize) - 1;
  1277. btp->bt_smask = sectorsize - 1;
  1278. if (set_blocksize(btp->bt_bdev, sectorsize)) {
  1279. printk(KERN_WARNING
  1280. "XFS: Cannot set_blocksize to %u on device %s\n",
  1281. sectorsize, XFS_BUFTARG_NAME(btp));
  1282. return EINVAL;
  1283. }
  1284. if (verbose &&
  1285. (PAGE_CACHE_SIZE / BITS_PER_LONG) > sectorsize) {
  1286. printk(KERN_WARNING
  1287. "XFS: %u byte sectors in use on device %s. "
  1288. "This is suboptimal; %u or greater is ideal.\n",
  1289. sectorsize, XFS_BUFTARG_NAME(btp),
  1290. (unsigned int)PAGE_CACHE_SIZE / BITS_PER_LONG);
  1291. }
  1292. return 0;
  1293. }
  1294. /*
  1295. * When allocating the initial buffer target we have not yet
  1296. * read in the superblock, so don't know what sized sectors
  1297. * are being used is at this early stage. Play safe.
  1298. */
  1299. STATIC int
  1300. xfs_setsize_buftarg_early(
  1301. xfs_buftarg_t *btp,
  1302. struct block_device *bdev)
  1303. {
  1304. return xfs_setsize_buftarg_flags(btp,
  1305. PAGE_CACHE_SIZE, bdev_hardsect_size(bdev), 0);
  1306. }
  1307. int
  1308. xfs_setsize_buftarg(
  1309. xfs_buftarg_t *btp,
  1310. unsigned int blocksize,
  1311. unsigned int sectorsize)
  1312. {
  1313. return xfs_setsize_buftarg_flags(btp, blocksize, sectorsize, 1);
  1314. }
  1315. STATIC int
  1316. xfs_mapping_buftarg(
  1317. xfs_buftarg_t *btp,
  1318. struct block_device *bdev)
  1319. {
  1320. struct backing_dev_info *bdi;
  1321. struct inode *inode;
  1322. struct address_space *mapping;
  1323. static const struct address_space_operations mapping_aops = {
  1324. .sync_page = block_sync_page,
  1325. .migratepage = fail_migrate_page,
  1326. };
  1327. inode = new_inode(bdev->bd_inode->i_sb);
  1328. if (!inode) {
  1329. printk(KERN_WARNING
  1330. "XFS: Cannot allocate mapping inode for device %s\n",
  1331. XFS_BUFTARG_NAME(btp));
  1332. return ENOMEM;
  1333. }
  1334. inode->i_mode = S_IFBLK;
  1335. inode->i_bdev = bdev;
  1336. inode->i_rdev = bdev->bd_dev;
  1337. bdi = blk_get_backing_dev_info(bdev);
  1338. if (!bdi)
  1339. bdi = &default_backing_dev_info;
  1340. mapping = &inode->i_data;
  1341. mapping->a_ops = &mapping_aops;
  1342. mapping->backing_dev_info = bdi;
  1343. mapping_set_gfp_mask(mapping, GFP_NOFS);
  1344. btp->bt_mapping = mapping;
  1345. return 0;
  1346. }
  1347. STATIC int
  1348. xfs_alloc_delwrite_queue(
  1349. xfs_buftarg_t *btp)
  1350. {
  1351. int error = 0;
  1352. INIT_LIST_HEAD(&btp->bt_list);
  1353. INIT_LIST_HEAD(&btp->bt_delwrite_queue);
  1354. spinlock_init(&btp->bt_delwrite_lock, "delwri_lock");
  1355. btp->bt_flags = 0;
  1356. btp->bt_task = kthread_run(xfsbufd, btp, "xfsbufd");
  1357. if (IS_ERR(btp->bt_task)) {
  1358. error = PTR_ERR(btp->bt_task);
  1359. goto out_error;
  1360. }
  1361. xfs_register_buftarg(btp);
  1362. out_error:
  1363. return error;
  1364. }
  1365. xfs_buftarg_t *
  1366. xfs_alloc_buftarg(
  1367. struct block_device *bdev,
  1368. int external)
  1369. {
  1370. xfs_buftarg_t *btp;
  1371. btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
  1372. btp->bt_dev = bdev->bd_dev;
  1373. btp->bt_bdev = bdev;
  1374. if (xfs_setsize_buftarg_early(btp, bdev))
  1375. goto error;
  1376. if (xfs_mapping_buftarg(btp, bdev))
  1377. goto error;
  1378. if (xfs_alloc_delwrite_queue(btp))
  1379. goto error;
  1380. xfs_alloc_bufhash(btp, external);
  1381. return btp;
  1382. error:
  1383. kmem_free(btp, sizeof(*btp));
  1384. return NULL;
  1385. }
  1386. /*
  1387. * Delayed write buffer handling
  1388. */
  1389. STATIC void
  1390. xfs_buf_delwri_queue(
  1391. xfs_buf_t *bp,
  1392. int unlock)
  1393. {
  1394. struct list_head *dwq = &bp->b_target->bt_delwrite_queue;
  1395. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1396. XB_TRACE(bp, "delwri_q", (long)unlock);
  1397. ASSERT((bp->b_flags&(XBF_DELWRI|XBF_ASYNC)) == (XBF_DELWRI|XBF_ASYNC));
  1398. spin_lock(dwlk);
  1399. /* If already in the queue, dequeue and place at tail */
  1400. if (!list_empty(&bp->b_list)) {
  1401. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1402. if (unlock)
  1403. atomic_dec(&bp->b_hold);
  1404. list_del(&bp->b_list);
  1405. }
  1406. bp->b_flags |= _XBF_DELWRI_Q;
  1407. list_add_tail(&bp->b_list, dwq);
  1408. bp->b_queuetime = jiffies;
  1409. spin_unlock(dwlk);
  1410. if (unlock)
  1411. xfs_buf_unlock(bp);
  1412. }
  1413. void
  1414. xfs_buf_delwri_dequeue(
  1415. xfs_buf_t *bp)
  1416. {
  1417. spinlock_t *dwlk = &bp->b_target->bt_delwrite_lock;
  1418. int dequeued = 0;
  1419. spin_lock(dwlk);
  1420. if ((bp->b_flags & XBF_DELWRI) && !list_empty(&bp->b_list)) {
  1421. ASSERT(bp->b_flags & _XBF_DELWRI_Q);
  1422. list_del_init(&bp->b_list);
  1423. dequeued = 1;
  1424. }
  1425. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q);
  1426. spin_unlock(dwlk);
  1427. if (dequeued)
  1428. xfs_buf_rele(bp);
  1429. XB_TRACE(bp, "delwri_dq", (long)dequeued);
  1430. }
  1431. STATIC void
  1432. xfs_buf_runall_queues(
  1433. struct workqueue_struct *queue)
  1434. {
  1435. flush_workqueue(queue);
  1436. }
  1437. STATIC int
  1438. xfsbufd_wakeup(
  1439. int priority,
  1440. gfp_t mask)
  1441. {
  1442. xfs_buftarg_t *btp;
  1443. spin_lock(&xfs_buftarg_lock);
  1444. list_for_each_entry(btp, &xfs_buftarg_list, bt_list) {
  1445. if (test_bit(XBT_FORCE_SLEEP, &btp->bt_flags))
  1446. continue;
  1447. set_bit(XBT_FORCE_FLUSH, &btp->bt_flags);
  1448. wake_up_process(btp->bt_task);
  1449. }
  1450. spin_unlock(&xfs_buftarg_lock);
  1451. return 0;
  1452. }
  1453. /*
  1454. * Move as many buffers as specified to the supplied list
  1455. * idicating if we skipped any buffers to prevent deadlocks.
  1456. */
  1457. STATIC int
  1458. xfs_buf_delwri_split(
  1459. xfs_buftarg_t *target,
  1460. struct list_head *list,
  1461. unsigned long age)
  1462. {
  1463. xfs_buf_t *bp, *n;
  1464. struct list_head *dwq = &target->bt_delwrite_queue;
  1465. spinlock_t *dwlk = &target->bt_delwrite_lock;
  1466. int skipped = 0;
  1467. int force;
  1468. force = test_and_clear_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1469. INIT_LIST_HEAD(list);
  1470. spin_lock(dwlk);
  1471. list_for_each_entry_safe(bp, n, dwq, b_list) {
  1472. XB_TRACE(bp, "walkq1", (long)xfs_buf_ispin(bp));
  1473. ASSERT(bp->b_flags & XBF_DELWRI);
  1474. if (!xfs_buf_ispin(bp) && !xfs_buf_cond_lock(bp)) {
  1475. if (!force &&
  1476. time_before(jiffies, bp->b_queuetime + age)) {
  1477. xfs_buf_unlock(bp);
  1478. break;
  1479. }
  1480. bp->b_flags &= ~(XBF_DELWRI|_XBF_DELWRI_Q|
  1481. _XBF_RUN_QUEUES);
  1482. bp->b_flags |= XBF_WRITE;
  1483. list_move_tail(&bp->b_list, list);
  1484. } else
  1485. skipped++;
  1486. }
  1487. spin_unlock(dwlk);
  1488. return skipped;
  1489. }
  1490. STATIC int
  1491. xfsbufd(
  1492. void *data)
  1493. {
  1494. struct list_head tmp;
  1495. xfs_buftarg_t *target = (xfs_buftarg_t *)data;
  1496. int count;
  1497. xfs_buf_t *bp;
  1498. current->flags |= PF_MEMALLOC;
  1499. do {
  1500. if (unlikely(freezing(current))) {
  1501. set_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1502. refrigerator();
  1503. } else {
  1504. clear_bit(XBT_FORCE_SLEEP, &target->bt_flags);
  1505. }
  1506. schedule_timeout_interruptible(
  1507. xfs_buf_timer_centisecs * msecs_to_jiffies(10));
  1508. xfs_buf_delwri_split(target, &tmp,
  1509. xfs_buf_age_centisecs * msecs_to_jiffies(10));
  1510. count = 0;
  1511. while (!list_empty(&tmp)) {
  1512. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1513. ASSERT(target == bp->b_target);
  1514. list_del_init(&bp->b_list);
  1515. xfs_buf_iostrategy(bp);
  1516. count++;
  1517. }
  1518. if (as_list_len > 0)
  1519. purge_addresses();
  1520. if (count)
  1521. blk_run_address_space(target->bt_mapping);
  1522. } while (!kthread_should_stop());
  1523. return 0;
  1524. }
  1525. /*
  1526. * Go through all incore buffers, and release buffers if they belong to
  1527. * the given device. This is used in filesystem error handling to
  1528. * preserve the consistency of its metadata.
  1529. */
  1530. int
  1531. xfs_flush_buftarg(
  1532. xfs_buftarg_t *target,
  1533. int wait)
  1534. {
  1535. struct list_head tmp;
  1536. xfs_buf_t *bp, *n;
  1537. int pincount = 0;
  1538. xfs_buf_runall_queues(xfsdatad_workqueue);
  1539. xfs_buf_runall_queues(xfslogd_workqueue);
  1540. set_bit(XBT_FORCE_FLUSH, &target->bt_flags);
  1541. pincount = xfs_buf_delwri_split(target, &tmp, 0);
  1542. /*
  1543. * Dropped the delayed write list lock, now walk the temporary list
  1544. */
  1545. list_for_each_entry_safe(bp, n, &tmp, b_list) {
  1546. ASSERT(target == bp->b_target);
  1547. if (wait)
  1548. bp->b_flags &= ~XBF_ASYNC;
  1549. else
  1550. list_del_init(&bp->b_list);
  1551. xfs_buf_iostrategy(bp);
  1552. }
  1553. if (wait)
  1554. blk_run_address_space(target->bt_mapping);
  1555. /*
  1556. * Remaining list items must be flushed before returning
  1557. */
  1558. while (!list_empty(&tmp)) {
  1559. bp = list_entry(tmp.next, xfs_buf_t, b_list);
  1560. list_del_init(&bp->b_list);
  1561. xfs_iowait(bp);
  1562. xfs_buf_relse(bp);
  1563. }
  1564. return pincount;
  1565. }
  1566. int __init
  1567. xfs_buf_init(void)
  1568. {
  1569. #ifdef XFS_BUF_TRACE
  1570. xfs_buf_trace_buf = ktrace_alloc(XFS_BUF_TRACE_SIZE, KM_SLEEP);
  1571. #endif
  1572. xfs_buf_zone = kmem_zone_init_flags(sizeof(xfs_buf_t), "xfs_buf",
  1573. KM_ZONE_HWALIGN, NULL);
  1574. if (!xfs_buf_zone)
  1575. goto out_free_trace_buf;
  1576. xfslogd_workqueue = create_workqueue("xfslogd");
  1577. if (!xfslogd_workqueue)
  1578. goto out_free_buf_zone;
  1579. xfsdatad_workqueue = create_workqueue("xfsdatad");
  1580. if (!xfsdatad_workqueue)
  1581. goto out_destroy_xfslogd_workqueue;
  1582. xfs_buf_shake = kmem_shake_register(xfsbufd_wakeup);
  1583. if (!xfs_buf_shake)
  1584. goto out_destroy_xfsdatad_workqueue;
  1585. return 0;
  1586. out_destroy_xfsdatad_workqueue:
  1587. destroy_workqueue(xfsdatad_workqueue);
  1588. out_destroy_xfslogd_workqueue:
  1589. destroy_workqueue(xfslogd_workqueue);
  1590. out_free_buf_zone:
  1591. kmem_zone_destroy(xfs_buf_zone);
  1592. out_free_trace_buf:
  1593. #ifdef XFS_BUF_TRACE
  1594. ktrace_free(xfs_buf_trace_buf);
  1595. #endif
  1596. return -ENOMEM;
  1597. }
  1598. void
  1599. xfs_buf_terminate(void)
  1600. {
  1601. kmem_shake_deregister(xfs_buf_shake);
  1602. destroy_workqueue(xfsdatad_workqueue);
  1603. destroy_workqueue(xfslogd_workqueue);
  1604. kmem_zone_destroy(xfs_buf_zone);
  1605. #ifdef XFS_BUF_TRACE
  1606. ktrace_free(xfs_buf_trace_buf);
  1607. #endif
  1608. }
  1609. #ifdef CONFIG_KDB_MODULES
  1610. struct list_head *
  1611. xfs_get_buftarg_list(void)
  1612. {
  1613. return &xfs_buftarg_list;
  1614. }
  1615. #endif