meta_io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/mm.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/writeback.h>
  17. #include <linux/swap.h>
  18. #include <linux/delay.h>
  19. #include <asm/semaphore.h>
  20. #include "gfs2.h"
  21. #include "glock.h"
  22. #include "glops.h"
  23. #include "inode.h"
  24. #include "log.h"
  25. #include "lops.h"
  26. #include "meta_io.h"
  27. #include "rgrp.h"
  28. #include "trans.h"
  29. #define buffer_busy(bh) \
  30. ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
  31. #define buffer_in_io(bh) \
  32. ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
  33. static int aspace_get_block(struct inode *inode, sector_t lblock,
  34. struct buffer_head *bh_result, int create)
  35. {
  36. gfs2_assert_warn(get_v2sdp(inode->i_sb), 0);
  37. return -EOPNOTSUPP;
  38. }
  39. static int gfs2_aspace_writepage(struct page *page,
  40. struct writeback_control *wbc)
  41. {
  42. return block_write_full_page(page, aspace_get_block, wbc);
  43. }
  44. /**
  45. * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
  46. * @bh: the buffer we're stuck on
  47. *
  48. */
  49. static void stuck_releasepage(struct buffer_head *bh)
  50. {
  51. struct gfs2_sbd *sdp = get_v2sdp(bh->b_page->mapping->host->i_sb);
  52. struct gfs2_bufdata *bd = get_v2bd(bh);
  53. struct gfs2_glock *gl;
  54. fs_warn(sdp, "stuck in gfs2_releasepage()\n");
  55. fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
  56. (uint64_t)bh->b_blocknr, atomic_read(&bh->b_count));
  57. fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
  58. fs_warn(sdp, "get_v2bd(bh) = %s\n", (bd) ? "!NULL" : "NULL");
  59. if (!bd)
  60. return;
  61. gl = bd->bd_gl;
  62. fs_warn(sdp, "gl = (%u, %llu)\n",
  63. gl->gl_name.ln_type, gl->gl_name.ln_number);
  64. fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
  65. (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
  66. (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
  67. if (gl->gl_ops == &gfs2_inode_glops) {
  68. struct gfs2_inode *ip = get_gl2ip(gl);
  69. unsigned int x;
  70. if (!ip)
  71. return;
  72. fs_warn(sdp, "ip = %llu %llu\n",
  73. ip->i_num.no_formal_ino, ip->i_num.no_addr);
  74. fs_warn(sdp, "ip->i_count = %d, ip->i_vnode = %s\n",
  75. atomic_read(&ip->i_count),
  76. (ip->i_vnode) ? "!NULL" : "NULL");
  77. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
  78. fs_warn(sdp, "ip->i_cache[%u] = %s\n",
  79. x, (ip->i_cache[x]) ? "!NULL" : "NULL");
  80. }
  81. }
  82. /**
  83. * gfs2_aspace_releasepage - free the metadata associated with a page
  84. * @page: the page that's being released
  85. * @gfp_mask: passed from Linux VFS, ignored by us
  86. *
  87. * Call try_to_free_buffers() if the buffers in this page can be
  88. * released.
  89. *
  90. * Returns: 0
  91. */
  92. static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask)
  93. {
  94. struct inode *aspace = page->mapping->host;
  95. struct gfs2_sbd *sdp = get_v2sdp(aspace->i_sb);
  96. struct buffer_head *bh, *head;
  97. struct gfs2_bufdata *bd;
  98. unsigned long t;
  99. if (!page_has_buffers(page))
  100. goto out;
  101. head = bh = page_buffers(page);
  102. do {
  103. t = jiffies;
  104. while (atomic_read(&bh->b_count)) {
  105. if (atomic_read(&aspace->i_writecount)) {
  106. if (time_after_eq(jiffies, t +
  107. gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
  108. stuck_releasepage(bh);
  109. t = jiffies;
  110. }
  111. yield();
  112. continue;
  113. }
  114. return 0;
  115. }
  116. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  117. bd = get_v2bd(bh);
  118. if (bd) {
  119. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  120. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  121. gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
  122. gfs2_assert_warn(sdp, !bd->bd_ail);
  123. kmem_cache_free(gfs2_bufdata_cachep, bd);
  124. atomic_dec(&sdp->sd_bufdata_count);
  125. set_v2bd(bh, NULL);
  126. }
  127. bh = bh->b_this_page;
  128. }
  129. while (bh != head);
  130. out:
  131. return try_to_free_buffers(page);
  132. }
  133. static struct address_space_operations aspace_aops = {
  134. .writepage = gfs2_aspace_writepage,
  135. .releasepage = gfs2_aspace_releasepage,
  136. };
  137. /**
  138. * gfs2_aspace_get - Create and initialize a struct inode structure
  139. * @sdp: the filesystem the aspace is in
  140. *
  141. * Right now a struct inode is just a struct inode. Maybe Linux
  142. * will supply a more lightweight address space construct (that works)
  143. * in the future.
  144. *
  145. * Make sure pages/buffers in this aspace aren't in high memory.
  146. *
  147. * Returns: the aspace
  148. */
  149. struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
  150. {
  151. struct inode *aspace;
  152. aspace = new_inode(sdp->sd_vfs);
  153. if (aspace) {
  154. mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
  155. aspace->i_mapping->a_ops = &aspace_aops;
  156. aspace->i_size = ~0ULL;
  157. set_v2ip(aspace, NULL);
  158. insert_inode_hash(aspace);
  159. }
  160. return aspace;
  161. }
  162. void gfs2_aspace_put(struct inode *aspace)
  163. {
  164. remove_inode_hash(aspace);
  165. iput(aspace);
  166. }
  167. /**
  168. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  169. * @sdp: the filesystem
  170. * @tr: the part of the AIL
  171. *
  172. */
  173. void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  174. {
  175. struct gfs2_bufdata *bd, *s;
  176. struct buffer_head *bh;
  177. int retry;
  178. do {
  179. retry = 0;
  180. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  181. bd_ail_st_list) {
  182. bh = bd->bd_bh;
  183. gfs2_assert(sdp, bd->bd_ail == ai);
  184. if (!buffer_busy(bh)) {
  185. if (!buffer_uptodate(bh))
  186. gfs2_io_error_bh(sdp, bh);
  187. list_move(&bd->bd_ail_st_list,
  188. &ai->ai_ail2_list);
  189. continue;
  190. }
  191. if (!buffer_dirty(bh))
  192. continue;
  193. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  194. gfs2_log_unlock(sdp);
  195. wait_on_buffer(bh);
  196. ll_rw_block(WRITE, 1, &bh);
  197. gfs2_log_lock(sdp);
  198. retry = 1;
  199. break;
  200. }
  201. } while (retry);
  202. }
  203. /**
  204. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  205. * @sdp: the filesystem
  206. * @ai: the AIL entry
  207. *
  208. */
  209. int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  210. {
  211. struct gfs2_bufdata *bd, *s;
  212. struct buffer_head *bh;
  213. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  214. bd_ail_st_list) {
  215. bh = bd->bd_bh;
  216. gfs2_assert(sdp, bd->bd_ail == ai);
  217. if (buffer_busy(bh)) {
  218. if (flags & DIO_ALL)
  219. continue;
  220. else
  221. break;
  222. }
  223. if (!buffer_uptodate(bh))
  224. gfs2_io_error_bh(sdp, bh);
  225. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  226. }
  227. return list_empty(&ai->ai_ail1_list);
  228. }
  229. /**
  230. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  231. * @sdp: the filesystem
  232. * @ai: the AIL entry
  233. *
  234. */
  235. void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  236. {
  237. struct list_head *head = &ai->ai_ail2_list;
  238. struct gfs2_bufdata *bd;
  239. while (!list_empty(head)) {
  240. bd = list_entry(head->prev, struct gfs2_bufdata,
  241. bd_ail_st_list);
  242. gfs2_assert(sdp, bd->bd_ail == ai);
  243. bd->bd_ail = NULL;
  244. list_del(&bd->bd_ail_st_list);
  245. list_del(&bd->bd_ail_gl_list);
  246. atomic_dec(&bd->bd_gl->gl_ail_count);
  247. brelse(bd->bd_bh);
  248. }
  249. }
  250. /**
  251. * ail_empty_gl - remove all buffers for a given lock from the AIL
  252. * @gl: the glock
  253. *
  254. * None of the buffers should be dirty, locked, or pinned.
  255. */
  256. void gfs2_ail_empty_gl(struct gfs2_glock *gl)
  257. {
  258. struct gfs2_sbd *sdp = gl->gl_sbd;
  259. unsigned int blocks;
  260. struct list_head *head = &gl->gl_ail_list;
  261. struct gfs2_bufdata *bd;
  262. struct buffer_head *bh;
  263. uint64_t blkno;
  264. int error;
  265. blocks = atomic_read(&gl->gl_ail_count);
  266. if (!blocks)
  267. return;
  268. error = gfs2_trans_begin(sdp, 0, blocks);
  269. if (gfs2_assert_withdraw(sdp, !error))
  270. return;
  271. gfs2_log_lock(sdp);
  272. while (!list_empty(head)) {
  273. bd = list_entry(head->next, struct gfs2_bufdata,
  274. bd_ail_gl_list);
  275. bh = bd->bd_bh;
  276. blkno = bh->b_blocknr;
  277. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  278. bd->bd_ail = NULL;
  279. list_del(&bd->bd_ail_st_list);
  280. list_del(&bd->bd_ail_gl_list);
  281. atomic_dec(&gl->gl_ail_count);
  282. brelse(bh);
  283. gfs2_log_unlock(sdp);
  284. gfs2_trans_add_revoke(sdp, blkno);
  285. gfs2_log_lock(sdp);
  286. }
  287. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  288. gfs2_log_unlock(sdp);
  289. gfs2_trans_end(sdp);
  290. gfs2_log_flush(sdp);
  291. }
  292. /**
  293. * gfs2_meta_inval - Invalidate all buffers associated with a glock
  294. * @gl: the glock
  295. *
  296. */
  297. void gfs2_meta_inval(struct gfs2_glock *gl)
  298. {
  299. struct gfs2_sbd *sdp = gl->gl_sbd;
  300. struct inode *aspace = gl->gl_aspace;
  301. struct address_space *mapping = gl->gl_aspace->i_mapping;
  302. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  303. atomic_inc(&aspace->i_writecount);
  304. truncate_inode_pages(mapping, 0);
  305. atomic_dec(&aspace->i_writecount);
  306. gfs2_assert_withdraw(sdp, !mapping->nrpages);
  307. }
  308. /**
  309. * gfs2_meta_sync - Sync all buffers associated with a glock
  310. * @gl: The glock
  311. * @flags: DIO_START | DIO_WAIT
  312. *
  313. */
  314. void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
  315. {
  316. struct address_space *mapping = gl->gl_aspace->i_mapping;
  317. int error = 0;
  318. if (flags & DIO_START)
  319. filemap_fdatawrite(mapping);
  320. if (!error && (flags & DIO_WAIT))
  321. error = filemap_fdatawait(mapping);
  322. if (error)
  323. gfs2_io_error(gl->gl_sbd);
  324. }
  325. /**
  326. * getbuf - Get a buffer with a given address space
  327. * @sdp: the filesystem
  328. * @aspace: the address space
  329. * @blkno: the block number (filesystem scope)
  330. * @create: 1 if the buffer should be created
  331. *
  332. * Returns: the buffer
  333. */
  334. static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
  335. uint64_t blkno, int create)
  336. {
  337. struct page *page;
  338. struct buffer_head *bh;
  339. unsigned int shift;
  340. unsigned long index;
  341. unsigned int bufnum;
  342. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  343. index = blkno >> shift; /* convert block to page */
  344. bufnum = blkno - (index << shift); /* block buf index within page */
  345. if (create) {
  346. for (;;) {
  347. page = grab_cache_page(aspace->i_mapping, index);
  348. if (page)
  349. break;
  350. yield();
  351. }
  352. } else {
  353. page = find_lock_page(aspace->i_mapping, index);
  354. if (!page)
  355. return NULL;
  356. }
  357. if (!page_has_buffers(page))
  358. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  359. /* Locate header for our buffer within our page */
  360. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  361. /* Do nothing */;
  362. get_bh(bh);
  363. if (!buffer_mapped(bh))
  364. map_bh(bh, sdp->sd_vfs, blkno);
  365. unlock_page(page);
  366. mark_page_accessed(page);
  367. page_cache_release(page);
  368. return bh;
  369. }
  370. static void meta_prep_new(struct buffer_head *bh)
  371. {
  372. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  373. lock_buffer(bh);
  374. clear_buffer_dirty(bh);
  375. set_buffer_uptodate(bh);
  376. unlock_buffer(bh);
  377. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  378. }
  379. /**
  380. * gfs2_meta_new - Get a block
  381. * @gl: The glock associated with this block
  382. * @blkno: The block number
  383. *
  384. * Returns: The buffer
  385. */
  386. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
  387. {
  388. struct buffer_head *bh;
  389. bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  390. meta_prep_new(bh);
  391. return bh;
  392. }
  393. /**
  394. * gfs2_meta_read - Read a block from disk
  395. * @gl: The glock covering the block
  396. * @blkno: The block number
  397. * @flags: flags to gfs2_dreread()
  398. * @bhp: the place where the buffer is returned (NULL on failure)
  399. *
  400. * Returns: errno
  401. */
  402. int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
  403. struct buffer_head **bhp)
  404. {
  405. int error;
  406. *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  407. error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
  408. if (error)
  409. brelse(*bhp);
  410. return error;
  411. }
  412. /**
  413. * gfs2_meta_reread - Reread a block from disk
  414. * @sdp: the filesystem
  415. * @bh: The block to read
  416. * @flags: Flags that control the read
  417. *
  418. * Returns: errno
  419. */
  420. int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
  421. {
  422. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  423. return -EIO;
  424. if (flags & DIO_FORCE)
  425. clear_buffer_uptodate(bh);
  426. if ((flags & DIO_START) && !buffer_uptodate(bh))
  427. ll_rw_block(READ, 1, &bh);
  428. if (flags & DIO_WAIT) {
  429. wait_on_buffer(bh);
  430. if (!buffer_uptodate(bh)) {
  431. struct gfs2_trans *tr = get_transaction;
  432. if (tr && tr->tr_touched)
  433. gfs2_io_error_bh(sdp, bh);
  434. return -EIO;
  435. }
  436. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  437. return -EIO;
  438. }
  439. return 0;
  440. }
  441. /**
  442. * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
  443. * @gl: the glock the buffer belongs to
  444. * @bh: The buffer to be attached to
  445. * @meta: Flag to indicate whether its metadata or not
  446. */
  447. void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
  448. {
  449. struct gfs2_bufdata *bd;
  450. if (meta)
  451. lock_page(bh->b_page);
  452. if (get_v2bd(bh)) {
  453. if (meta)
  454. unlock_page(bh->b_page);
  455. return;
  456. }
  457. bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
  458. atomic_inc(&gl->gl_sbd->sd_bufdata_count);
  459. memset(bd, 0, sizeof(struct gfs2_bufdata));
  460. bd->bd_bh = bh;
  461. bd->bd_gl = gl;
  462. INIT_LIST_HEAD(&bd->bd_list_tr);
  463. if (meta) {
  464. lops_init_le(&bd->bd_le, &gfs2_buf_lops);
  465. } else {
  466. lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
  467. get_bh(bh);
  468. }
  469. set_v2bd(bh, bd);
  470. if (meta)
  471. unlock_page(bh->b_page);
  472. }
  473. /**
  474. * gfs2_pin - Pin a buffer in memory
  475. * @sdp: the filesystem the buffer belongs to
  476. * @bh: The buffer to be pinned
  477. *
  478. */
  479. void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  480. {
  481. struct gfs2_bufdata *bd = get_v2bd(bh);
  482. gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  483. if (test_set_buffer_pinned(bh))
  484. gfs2_assert_withdraw(sdp, 0);
  485. wait_on_buffer(bh);
  486. /* If this buffer is in the AIL and it has already been written
  487. to in-place disk block, remove it from the AIL. */
  488. gfs2_log_lock(sdp);
  489. if (bd->bd_ail && !buffer_in_io(bh))
  490. list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
  491. gfs2_log_unlock(sdp);
  492. clear_buffer_dirty(bh);
  493. wait_on_buffer(bh);
  494. if (!buffer_uptodate(bh))
  495. gfs2_io_error_bh(sdp, bh);
  496. get_bh(bh);
  497. }
  498. /**
  499. * gfs2_unpin - Unpin a buffer
  500. * @sdp: the filesystem the buffer belongs to
  501. * @bh: The buffer to unpin
  502. * @ai:
  503. *
  504. */
  505. void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  506. struct gfs2_ail *ai)
  507. {
  508. struct gfs2_bufdata *bd = get_v2bd(bh);
  509. gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
  510. if (!buffer_pinned(bh))
  511. gfs2_assert_withdraw(sdp, 0);
  512. mark_buffer_dirty(bh);
  513. clear_buffer_pinned(bh);
  514. gfs2_log_lock(sdp);
  515. if (bd->bd_ail) {
  516. list_del(&bd->bd_ail_st_list);
  517. brelse(bh);
  518. } else {
  519. struct gfs2_glock *gl = bd->bd_gl;
  520. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  521. atomic_inc(&gl->gl_ail_count);
  522. }
  523. bd->bd_ail = ai;
  524. list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  525. gfs2_log_unlock(sdp);
  526. }
  527. /**
  528. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  529. * @ip: the inode who owns the buffers
  530. * @bstart: the first buffer in the run
  531. * @blen: the number of buffers in the run
  532. *
  533. */
  534. void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  535. {
  536. struct gfs2_sbd *sdp = ip->i_sbd;
  537. struct inode *aspace = ip->i_gl->gl_aspace;
  538. struct buffer_head *bh;
  539. while (blen) {
  540. bh = getbuf(sdp, aspace, bstart, NO_CREATE);
  541. if (bh) {
  542. struct gfs2_bufdata *bd = get_v2bd(bh);
  543. if (test_clear_buffer_pinned(bh)) {
  544. gfs2_log_lock(sdp);
  545. list_del_init(&bd->bd_le.le_list);
  546. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  547. sdp->sd_log_num_buf--;
  548. gfs2_log_unlock(sdp);
  549. get_transaction->tr_num_buf_rm++;
  550. brelse(bh);
  551. }
  552. if (bd) {
  553. gfs2_log_lock(sdp);
  554. if (bd->bd_ail) {
  555. uint64_t blkno = bh->b_blocknr;
  556. bd->bd_ail = NULL;
  557. list_del(&bd->bd_ail_st_list);
  558. list_del(&bd->bd_ail_gl_list);
  559. atomic_dec(&bd->bd_gl->gl_ail_count);
  560. brelse(bh);
  561. gfs2_log_unlock(sdp);
  562. gfs2_trans_add_revoke(sdp, blkno);
  563. } else
  564. gfs2_log_unlock(sdp);
  565. }
  566. lock_buffer(bh);
  567. clear_buffer_dirty(bh);
  568. clear_buffer_uptodate(bh);
  569. unlock_buffer(bh);
  570. brelse(bh);
  571. }
  572. bstart++;
  573. blen--;
  574. }
  575. }
  576. /**
  577. * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
  578. * @ip: The GFS2 inode
  579. *
  580. * This releases buffers that are in the most-recently-used array of
  581. * blocks used for indirect block addressing for this inode.
  582. */
  583. void gfs2_meta_cache_flush(struct gfs2_inode *ip)
  584. {
  585. struct buffer_head **bh_slot;
  586. unsigned int x;
  587. spin_lock(&ip->i_spin);
  588. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
  589. bh_slot = &ip->i_cache[x];
  590. if (!*bh_slot)
  591. break;
  592. brelse(*bh_slot);
  593. *bh_slot = NULL;
  594. }
  595. spin_unlock(&ip->i_spin);
  596. }
  597. /**
  598. * gfs2_meta_indirect_buffer - Get a metadata buffer
  599. * @ip: The GFS2 inode
  600. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  601. * @num: The block number (device relative) of the buffer
  602. * @new: Non-zero if we may create a new buffer
  603. * @bhp: the buffer is returned here
  604. *
  605. * Try to use the gfs2_inode's MRU metadata tree cache.
  606. *
  607. * Returns: errno
  608. */
  609. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
  610. int new, struct buffer_head **bhp)
  611. {
  612. struct buffer_head *bh, **bh_slot = ip->i_cache + height;
  613. int error;
  614. spin_lock(&ip->i_spin);
  615. bh = *bh_slot;
  616. if (bh) {
  617. if (bh->b_blocknr == num)
  618. get_bh(bh);
  619. else
  620. bh = NULL;
  621. }
  622. spin_unlock(&ip->i_spin);
  623. if (bh) {
  624. if (new)
  625. meta_prep_new(bh);
  626. else {
  627. error = gfs2_meta_reread(ip->i_sbd, bh,
  628. DIO_START | DIO_WAIT);
  629. if (error) {
  630. brelse(bh);
  631. return error;
  632. }
  633. }
  634. } else {
  635. if (new)
  636. bh = gfs2_meta_new(ip->i_gl, num);
  637. else {
  638. error = gfs2_meta_read(ip->i_gl, num,
  639. DIO_START | DIO_WAIT, &bh);
  640. if (error)
  641. return error;
  642. }
  643. spin_lock(&ip->i_spin);
  644. if (*bh_slot != bh) {
  645. brelse(*bh_slot);
  646. *bh_slot = bh;
  647. get_bh(bh);
  648. }
  649. spin_unlock(&ip->i_spin);
  650. }
  651. if (new) {
  652. if (gfs2_assert_warn(ip->i_sbd, height)) {
  653. brelse(bh);
  654. return -EIO;
  655. }
  656. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  657. gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  658. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  659. } else if (gfs2_metatype_check(ip->i_sbd, bh,
  660. (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
  661. brelse(bh);
  662. return -EIO;
  663. }
  664. *bhp = bh;
  665. return 0;
  666. }
  667. /**
  668. * gfs2_meta_ra - start readahead on an extent of a file
  669. * @gl: the glock the blocks belong to
  670. * @dblock: the starting disk block
  671. * @extlen: the number of blocks in the extent
  672. *
  673. */
  674. void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
  675. {
  676. struct gfs2_sbd *sdp = gl->gl_sbd;
  677. struct inode *aspace = gl->gl_aspace;
  678. struct buffer_head *first_bh, *bh;
  679. uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
  680. int error;
  681. if (!extlen || !max_ra)
  682. return;
  683. if (extlen > max_ra)
  684. extlen = max_ra;
  685. first_bh = getbuf(sdp, aspace, dblock, CREATE);
  686. if (buffer_uptodate(first_bh))
  687. goto out;
  688. if (!buffer_locked(first_bh)) {
  689. error = gfs2_meta_reread(sdp, first_bh, DIO_START);
  690. if (error)
  691. goto out;
  692. }
  693. dblock++;
  694. extlen--;
  695. while (extlen) {
  696. bh = getbuf(sdp, aspace, dblock, CREATE);
  697. if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
  698. error = gfs2_meta_reread(sdp, bh, DIO_START);
  699. brelse(bh);
  700. if (error)
  701. goto out;
  702. } else
  703. brelse(bh);
  704. dblock++;
  705. extlen--;
  706. if (buffer_uptodate(first_bh))
  707. break;
  708. }
  709. out:
  710. brelse(first_bh);
  711. }
  712. /**
  713. * gfs2_meta_syncfs - sync all the buffers in a filesystem
  714. * @sdp: the filesystem
  715. *
  716. */
  717. void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
  718. {
  719. gfs2_log_flush(sdp);
  720. for (;;) {
  721. gfs2_ail1_start(sdp, DIO_ALL);
  722. if (gfs2_ail1_empty(sdp, DIO_ALL))
  723. break;
  724. msleep(100);
  725. }
  726. }