meta_io.c 19 KB

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