meta_io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. set_v2bd(bh, NULL);
  125. }
  126. bh = bh->b_this_page;
  127. }
  128. while (bh != head);
  129. out:
  130. return try_to_free_buffers(page);
  131. }
  132. static struct address_space_operations aspace_aops = {
  133. .writepage = gfs2_aspace_writepage,
  134. .releasepage = gfs2_aspace_releasepage,
  135. };
  136. /**
  137. * gfs2_aspace_get - Create and initialize a struct inode structure
  138. * @sdp: the filesystem the aspace is in
  139. *
  140. * Right now a struct inode is just a struct inode. Maybe Linux
  141. * will supply a more lightweight address space construct (that works)
  142. * in the future.
  143. *
  144. * Make sure pages/buffers in this aspace aren't in high memory.
  145. *
  146. * Returns: the aspace
  147. */
  148. struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
  149. {
  150. struct inode *aspace;
  151. aspace = new_inode(sdp->sd_vfs);
  152. if (aspace) {
  153. mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
  154. aspace->i_mapping->a_ops = &aspace_aops;
  155. aspace->i_size = ~0ULL;
  156. set_v2ip(aspace, NULL);
  157. insert_inode_hash(aspace);
  158. }
  159. return aspace;
  160. }
  161. void gfs2_aspace_put(struct inode *aspace)
  162. {
  163. remove_inode_hash(aspace);
  164. iput(aspace);
  165. }
  166. /**
  167. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  168. * @sdp: the filesystem
  169. * @tr: the part of the AIL
  170. *
  171. */
  172. void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  173. {
  174. struct gfs2_bufdata *bd, *s;
  175. struct buffer_head *bh;
  176. int retry;
  177. do {
  178. retry = 0;
  179. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  180. bd_ail_st_list) {
  181. bh = bd->bd_bh;
  182. gfs2_assert(sdp, bd->bd_ail == ai);
  183. if (!buffer_busy(bh)) {
  184. if (!buffer_uptodate(bh))
  185. gfs2_io_error_bh(sdp, bh);
  186. list_move(&bd->bd_ail_st_list,
  187. &ai->ai_ail2_list);
  188. continue;
  189. }
  190. if (!buffer_dirty(bh))
  191. continue;
  192. list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  193. gfs2_log_unlock(sdp);
  194. wait_on_buffer(bh);
  195. ll_rw_block(WRITE, 1, &bh);
  196. gfs2_log_lock(sdp);
  197. retry = 1;
  198. break;
  199. }
  200. } while (retry);
  201. }
  202. /**
  203. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  204. * @sdp: the filesystem
  205. * @ai: the AIL entry
  206. *
  207. */
  208. int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
  209. {
  210. struct gfs2_bufdata *bd, *s;
  211. struct buffer_head *bh;
  212. list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
  213. bd_ail_st_list) {
  214. bh = bd->bd_bh;
  215. gfs2_assert(sdp, bd->bd_ail == ai);
  216. if (buffer_busy(bh)) {
  217. if (flags & DIO_ALL)
  218. continue;
  219. else
  220. break;
  221. }
  222. if (!buffer_uptodate(bh))
  223. gfs2_io_error_bh(sdp, bh);
  224. list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
  225. }
  226. return list_empty(&ai->ai_ail1_list);
  227. }
  228. /**
  229. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  230. * @sdp: the filesystem
  231. * @ai: the AIL entry
  232. *
  233. */
  234. void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
  235. {
  236. struct list_head *head = &ai->ai_ail2_list;
  237. struct gfs2_bufdata *bd;
  238. while (!list_empty(head)) {
  239. bd = list_entry(head->prev, struct gfs2_bufdata,
  240. bd_ail_st_list);
  241. gfs2_assert(sdp, bd->bd_ail == ai);
  242. bd->bd_ail = NULL;
  243. list_del(&bd->bd_ail_st_list);
  244. list_del(&bd->bd_ail_gl_list);
  245. atomic_dec(&bd->bd_gl->gl_ail_count);
  246. brelse(bd->bd_bh);
  247. }
  248. }
  249. /**
  250. * ail_empty_gl - remove all buffers for a given lock from the AIL
  251. * @gl: the glock
  252. *
  253. * None of the buffers should be dirty, locked, or pinned.
  254. */
  255. void gfs2_ail_empty_gl(struct gfs2_glock *gl)
  256. {
  257. struct gfs2_sbd *sdp = gl->gl_sbd;
  258. unsigned int blocks;
  259. struct list_head *head = &gl->gl_ail_list;
  260. struct gfs2_bufdata *bd;
  261. struct buffer_head *bh;
  262. uint64_t blkno;
  263. int error;
  264. blocks = atomic_read(&gl->gl_ail_count);
  265. if (!blocks)
  266. return;
  267. error = gfs2_trans_begin(sdp, 0, blocks);
  268. if (gfs2_assert_withdraw(sdp, !error))
  269. return;
  270. gfs2_log_lock(sdp);
  271. while (!list_empty(head)) {
  272. bd = list_entry(head->next, struct gfs2_bufdata,
  273. bd_ail_gl_list);
  274. bh = bd->bd_bh;
  275. blkno = bh->b_blocknr;
  276. gfs2_assert_withdraw(sdp, !buffer_busy(bh));
  277. bd->bd_ail = NULL;
  278. list_del(&bd->bd_ail_st_list);
  279. list_del(&bd->bd_ail_gl_list);
  280. atomic_dec(&gl->gl_ail_count);
  281. brelse(bh);
  282. gfs2_log_unlock(sdp);
  283. gfs2_trans_add_revoke(sdp, blkno);
  284. gfs2_log_lock(sdp);
  285. }
  286. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  287. gfs2_log_unlock(sdp);
  288. gfs2_trans_end(sdp);
  289. gfs2_log_flush(sdp);
  290. }
  291. /**
  292. * gfs2_meta_inval - Invalidate all buffers associated with a glock
  293. * @gl: the glock
  294. *
  295. */
  296. void gfs2_meta_inval(struct gfs2_glock *gl)
  297. {
  298. struct gfs2_sbd *sdp = gl->gl_sbd;
  299. struct inode *aspace = gl->gl_aspace;
  300. struct address_space *mapping = gl->gl_aspace->i_mapping;
  301. gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
  302. atomic_inc(&aspace->i_writecount);
  303. truncate_inode_pages(mapping, 0);
  304. atomic_dec(&aspace->i_writecount);
  305. gfs2_assert_withdraw(sdp, !mapping->nrpages);
  306. }
  307. /**
  308. * gfs2_meta_sync - Sync all buffers associated with a glock
  309. * @gl: The glock
  310. * @flags: DIO_START | DIO_WAIT
  311. *
  312. */
  313. void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
  314. {
  315. struct address_space *mapping = gl->gl_aspace->i_mapping;
  316. int error = 0;
  317. if (flags & DIO_START)
  318. filemap_fdatawrite(mapping);
  319. if (!error && (flags & DIO_WAIT))
  320. error = filemap_fdatawait(mapping);
  321. if (error)
  322. gfs2_io_error(gl->gl_sbd);
  323. }
  324. /**
  325. * getbuf - Get a buffer with a given address space
  326. * @sdp: the filesystem
  327. * @aspace: the address space
  328. * @blkno: the block number (filesystem scope)
  329. * @create: 1 if the buffer should be created
  330. *
  331. * Returns: the buffer
  332. */
  333. static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
  334. uint64_t blkno, int create)
  335. {
  336. struct page *page;
  337. struct buffer_head *bh;
  338. unsigned int shift;
  339. unsigned long index;
  340. unsigned int bufnum;
  341. shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  342. index = blkno >> shift; /* convert block to page */
  343. bufnum = blkno - (index << shift); /* block buf index within page */
  344. if (create) {
  345. for (;;) {
  346. page = grab_cache_page(aspace->i_mapping, index);
  347. if (page)
  348. break;
  349. yield();
  350. }
  351. } else {
  352. page = find_lock_page(aspace->i_mapping, index);
  353. if (!page)
  354. return NULL;
  355. }
  356. if (!page_has_buffers(page))
  357. create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  358. /* Locate header for our buffer within our page */
  359. for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  360. /* Do nothing */;
  361. get_bh(bh);
  362. if (!buffer_mapped(bh))
  363. map_bh(bh, sdp->sd_vfs, blkno);
  364. unlock_page(page);
  365. mark_page_accessed(page);
  366. page_cache_release(page);
  367. return bh;
  368. }
  369. static void meta_prep_new(struct buffer_head *bh)
  370. {
  371. struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  372. lock_buffer(bh);
  373. clear_buffer_dirty(bh);
  374. set_buffer_uptodate(bh);
  375. unlock_buffer(bh);
  376. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  377. }
  378. /**
  379. * gfs2_meta_new - Get a block
  380. * @gl: The glock associated with this block
  381. * @blkno: The block number
  382. *
  383. * Returns: The buffer
  384. */
  385. struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
  386. {
  387. struct buffer_head *bh;
  388. bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  389. meta_prep_new(bh);
  390. return bh;
  391. }
  392. /**
  393. * gfs2_meta_read - Read a block from disk
  394. * @gl: The glock covering the block
  395. * @blkno: The block number
  396. * @flags: flags to gfs2_dreread()
  397. * @bhp: the place where the buffer is returned (NULL on failure)
  398. *
  399. * Returns: errno
  400. */
  401. int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
  402. struct buffer_head **bhp)
  403. {
  404. int error;
  405. *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
  406. error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
  407. if (error)
  408. brelse(*bhp);
  409. return error;
  410. }
  411. /**
  412. * gfs2_meta_reread - Reread a block from disk
  413. * @sdp: the filesystem
  414. * @bh: The block to read
  415. * @flags: Flags that control the read
  416. *
  417. * Returns: errno
  418. */
  419. int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
  420. {
  421. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  422. return -EIO;
  423. if (flags & DIO_FORCE)
  424. clear_buffer_uptodate(bh);
  425. if ((flags & DIO_START) && !buffer_uptodate(bh))
  426. ll_rw_block(READ, 1, &bh);
  427. if (flags & DIO_WAIT) {
  428. wait_on_buffer(bh);
  429. if (!buffer_uptodate(bh)) {
  430. struct gfs2_trans *tr = get_transaction;
  431. if (tr && tr->tr_touched)
  432. gfs2_io_error_bh(sdp, bh);
  433. return -EIO;
  434. }
  435. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  436. return -EIO;
  437. }
  438. return 0;
  439. }
  440. /**
  441. * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
  442. * @gl: the glock the buffer belongs to
  443. * @bh: The buffer to be attached to
  444. * @meta: Flag to indicate whether its metadata or not
  445. */
  446. void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
  447. 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. memset(bd, 0, sizeof(struct gfs2_bufdata));
  459. bd->bd_bh = bh;
  460. bd->bd_gl = gl;
  461. INIT_LIST_HEAD(&bd->bd_list_tr);
  462. if (meta) {
  463. lops_init_le(&bd->bd_le, &gfs2_buf_lops);
  464. } else {
  465. lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
  466. get_bh(bh);
  467. }
  468. set_v2bd(bh, bd);
  469. if (meta)
  470. unlock_page(bh->b_page);
  471. }
  472. /**
  473. * gfs2_pin - Pin a buffer in memory
  474. * @sdp: the filesystem the buffer belongs to
  475. * @bh: The buffer to be pinned
  476. *
  477. */
  478. void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  479. {
  480. struct gfs2_bufdata *bd = get_v2bd(bh);
  481. gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
  482. if (test_set_buffer_pinned(bh))
  483. gfs2_assert_withdraw(sdp, 0);
  484. wait_on_buffer(bh);
  485. /* If this buffer is in the AIL and it has already been written
  486. to in-place disk block, remove it from the AIL. */
  487. gfs2_log_lock(sdp);
  488. if (bd->bd_ail && !buffer_in_io(bh))
  489. list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
  490. gfs2_log_unlock(sdp);
  491. clear_buffer_dirty(bh);
  492. wait_on_buffer(bh);
  493. if (!buffer_uptodate(bh))
  494. gfs2_io_error_bh(sdp, bh);
  495. get_bh(bh);
  496. }
  497. /**
  498. * gfs2_unpin - Unpin a buffer
  499. * @sdp: the filesystem the buffer belongs to
  500. * @bh: The buffer to unpin
  501. * @ai:
  502. *
  503. */
  504. void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  505. struct gfs2_ail *ai)
  506. {
  507. struct gfs2_bufdata *bd = get_v2bd(bh);
  508. gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
  509. if (!buffer_pinned(bh))
  510. gfs2_assert_withdraw(sdp, 0);
  511. mark_buffer_dirty(bh);
  512. clear_buffer_pinned(bh);
  513. gfs2_log_lock(sdp);
  514. if (bd->bd_ail) {
  515. list_del(&bd->bd_ail_st_list);
  516. brelse(bh);
  517. } else {
  518. struct gfs2_glock *gl = bd->bd_gl;
  519. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  520. atomic_inc(&gl->gl_ail_count);
  521. }
  522. bd->bd_ail = ai;
  523. list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
  524. gfs2_log_unlock(sdp);
  525. }
  526. /**
  527. * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
  528. * @ip: the inode who owns the buffers
  529. * @bstart: the first buffer in the run
  530. * @blen: the number of buffers in the run
  531. *
  532. */
  533. void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  534. {
  535. struct gfs2_sbd *sdp = ip->i_sbd;
  536. struct inode *aspace = ip->i_gl->gl_aspace;
  537. struct buffer_head *bh;
  538. while (blen) {
  539. bh = getbuf(sdp, aspace, bstart, NO_CREATE);
  540. if (bh) {
  541. struct gfs2_bufdata *bd = get_v2bd(bh);
  542. if (test_clear_buffer_pinned(bh)) {
  543. gfs2_log_lock(sdp);
  544. list_del_init(&bd->bd_le.le_list);
  545. gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
  546. sdp->sd_log_num_buf--;
  547. gfs2_log_unlock(sdp);
  548. get_transaction->tr_num_buf_rm++;
  549. brelse(bh);
  550. }
  551. if (bd) {
  552. gfs2_log_lock(sdp);
  553. if (bd->bd_ail) {
  554. uint64_t blkno = bh->b_blocknr;
  555. bd->bd_ail = NULL;
  556. list_del(&bd->bd_ail_st_list);
  557. list_del(&bd->bd_ail_gl_list);
  558. atomic_dec(&bd->bd_gl->gl_ail_count);
  559. brelse(bh);
  560. gfs2_log_unlock(sdp);
  561. gfs2_trans_add_revoke(sdp, blkno);
  562. } else
  563. gfs2_log_unlock(sdp);
  564. }
  565. lock_buffer(bh);
  566. clear_buffer_dirty(bh);
  567. clear_buffer_uptodate(bh);
  568. unlock_buffer(bh);
  569. brelse(bh);
  570. }
  571. bstart++;
  572. blen--;
  573. }
  574. }
  575. /**
  576. * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
  577. * @ip: The GFS2 inode
  578. *
  579. * This releases buffers that are in the most-recently-used array of
  580. * blocks used for indirect block addressing for this inode.
  581. */
  582. void gfs2_meta_cache_flush(struct gfs2_inode *ip)
  583. {
  584. struct buffer_head **bh_slot;
  585. unsigned int x;
  586. spin_lock(&ip->i_spin);
  587. for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
  588. bh_slot = &ip->i_cache[x];
  589. if (!*bh_slot)
  590. break;
  591. brelse(*bh_slot);
  592. *bh_slot = NULL;
  593. }
  594. spin_unlock(&ip->i_spin);
  595. }
  596. /**
  597. * gfs2_meta_indirect_buffer - Get a metadata buffer
  598. * @ip: The GFS2 inode
  599. * @height: The level of this buf in the metadata (indir addr) tree (if any)
  600. * @num: The block number (device relative) of the buffer
  601. * @new: Non-zero if we may create a new buffer
  602. * @bhp: the buffer is returned here
  603. *
  604. * Try to use the gfs2_inode's MRU metadata tree cache.
  605. *
  606. * Returns: errno
  607. */
  608. int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
  609. int new, struct buffer_head **bhp)
  610. {
  611. struct buffer_head *bh, **bh_slot = ip->i_cache + height;
  612. int error;
  613. spin_lock(&ip->i_spin);
  614. bh = *bh_slot;
  615. if (bh) {
  616. if (bh->b_blocknr == num)
  617. get_bh(bh);
  618. else
  619. bh = NULL;
  620. }
  621. spin_unlock(&ip->i_spin);
  622. if (bh) {
  623. if (new)
  624. meta_prep_new(bh);
  625. else {
  626. error = gfs2_meta_reread(ip->i_sbd, bh,
  627. DIO_START | DIO_WAIT);
  628. if (error) {
  629. brelse(bh);
  630. return error;
  631. }
  632. }
  633. } else {
  634. if (new)
  635. bh = gfs2_meta_new(ip->i_gl, num);
  636. else {
  637. error = gfs2_meta_read(ip->i_gl, num,
  638. DIO_START | DIO_WAIT, &bh);
  639. if (error)
  640. return error;
  641. }
  642. spin_lock(&ip->i_spin);
  643. if (*bh_slot != bh) {
  644. brelse(*bh_slot);
  645. *bh_slot = bh;
  646. get_bh(bh);
  647. }
  648. spin_unlock(&ip->i_spin);
  649. }
  650. if (new) {
  651. if (gfs2_assert_warn(ip->i_sbd, height)) {
  652. brelse(bh);
  653. return -EIO;
  654. }
  655. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  656. gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  657. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  658. } else if (gfs2_metatype_check(ip->i_sbd, bh,
  659. (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
  660. brelse(bh);
  661. return -EIO;
  662. }
  663. *bhp = bh;
  664. return 0;
  665. }
  666. /**
  667. * gfs2_meta_ra - start readahead on an extent of a file
  668. * @gl: the glock the blocks belong to
  669. * @dblock: the starting disk block
  670. * @extlen: the number of blocks in the extent
  671. *
  672. */
  673. void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
  674. {
  675. struct gfs2_sbd *sdp = gl->gl_sbd;
  676. struct inode *aspace = gl->gl_aspace;
  677. struct buffer_head *first_bh, *bh;
  678. uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
  679. 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. }