mdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * mdt.c - meta data file for NILFS
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>
  21. */
  22. #include <linux/buffer_head.h>
  23. #include <linux/mpage.h>
  24. #include <linux/mm.h>
  25. #include <linux/writeback.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/swap.h>
  28. #include <linux/slab.h>
  29. #include "nilfs.h"
  30. #include "btnode.h"
  31. #include "segment.h"
  32. #include "page.h"
  33. #include "mdt.h"
  34. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  35. #define INIT_UNUSED_INODE_FIELDS
  36. static int
  37. nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
  38. struct buffer_head *bh,
  39. void (*init_block)(struct inode *,
  40. struct buffer_head *, void *))
  41. {
  42. struct nilfs_inode_info *ii = NILFS_I(inode);
  43. void *kaddr;
  44. int ret;
  45. /* Caller exclude read accesses using page lock */
  46. /* set_buffer_new(bh); */
  47. bh->b_blocknr = 0;
  48. ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
  49. if (unlikely(ret))
  50. return ret;
  51. set_buffer_mapped(bh);
  52. kaddr = kmap_atomic(bh->b_page, KM_USER0);
  53. memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
  54. if (init_block)
  55. init_block(inode, bh, kaddr);
  56. flush_dcache_page(bh->b_page);
  57. kunmap_atomic(kaddr, KM_USER0);
  58. set_buffer_uptodate(bh);
  59. nilfs_mark_buffer_dirty(bh);
  60. nilfs_mdt_mark_dirty(inode);
  61. return 0;
  62. }
  63. static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
  64. struct buffer_head **out_bh,
  65. void (*init_block)(struct inode *,
  66. struct buffer_head *,
  67. void *))
  68. {
  69. struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
  70. struct super_block *sb = inode->i_sb;
  71. struct nilfs_transaction_info ti;
  72. struct buffer_head *bh;
  73. int err;
  74. if (!sb) {
  75. /*
  76. * Make sure this function is not called from any
  77. * read-only context.
  78. */
  79. if (!nilfs->ns_writer) {
  80. WARN_ON(1);
  81. err = -EROFS;
  82. goto out;
  83. }
  84. sb = nilfs->ns_writer->s_super;
  85. }
  86. nilfs_transaction_begin(sb, &ti, 0);
  87. err = -ENOMEM;
  88. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  89. if (unlikely(!bh))
  90. goto failed_unlock;
  91. err = -EEXIST;
  92. if (buffer_uptodate(bh))
  93. goto failed_bh;
  94. wait_on_buffer(bh);
  95. if (buffer_uptodate(bh))
  96. goto failed_bh;
  97. bh->b_bdev = nilfs->ns_bdev;
  98. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  99. if (likely(!err)) {
  100. get_bh(bh);
  101. *out_bh = bh;
  102. }
  103. failed_bh:
  104. unlock_page(bh->b_page);
  105. page_cache_release(bh->b_page);
  106. brelse(bh);
  107. failed_unlock:
  108. if (likely(!err))
  109. err = nilfs_transaction_commit(sb);
  110. else
  111. nilfs_transaction_abort(sb);
  112. out:
  113. return err;
  114. }
  115. static int
  116. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  117. int mode, struct buffer_head **out_bh)
  118. {
  119. struct buffer_head *bh;
  120. __u64 blknum = 0;
  121. int ret = -ENOMEM;
  122. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  123. if (unlikely(!bh))
  124. goto failed;
  125. ret = -EEXIST; /* internal code */
  126. if (buffer_uptodate(bh))
  127. goto out;
  128. if (mode == READA) {
  129. if (!trylock_buffer(bh)) {
  130. ret = -EBUSY;
  131. goto failed_bh;
  132. }
  133. } else /* mode == READ */
  134. lock_buffer(bh);
  135. if (buffer_uptodate(bh)) {
  136. unlock_buffer(bh);
  137. goto out;
  138. }
  139. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
  140. if (unlikely(ret)) {
  141. unlock_buffer(bh);
  142. goto failed_bh;
  143. }
  144. bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
  145. bh->b_blocknr = (sector_t)blknum;
  146. set_buffer_mapped(bh);
  147. bh->b_end_io = end_buffer_read_sync;
  148. get_bh(bh);
  149. submit_bh(mode, bh);
  150. ret = 0;
  151. out:
  152. get_bh(bh);
  153. *out_bh = bh;
  154. failed_bh:
  155. unlock_page(bh->b_page);
  156. page_cache_release(bh->b_page);
  157. brelse(bh);
  158. failed:
  159. return ret;
  160. }
  161. static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
  162. int readahead, struct buffer_head **out_bh)
  163. {
  164. struct buffer_head *first_bh, *bh;
  165. unsigned long blkoff;
  166. int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
  167. int err;
  168. err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
  169. if (err == -EEXIST) /* internal code */
  170. goto out;
  171. if (unlikely(err))
  172. goto failed;
  173. if (readahead) {
  174. blkoff = block + 1;
  175. for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
  176. err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
  177. if (likely(!err || err == -EEXIST))
  178. brelse(bh);
  179. else if (err != -EBUSY)
  180. break;
  181. /* abort readahead if bmap lookup failed */
  182. if (!buffer_locked(first_bh))
  183. goto out_no_wait;
  184. }
  185. }
  186. wait_on_buffer(first_bh);
  187. out_no_wait:
  188. err = -EIO;
  189. if (!buffer_uptodate(first_bh))
  190. goto failed_bh;
  191. out:
  192. *out_bh = first_bh;
  193. return 0;
  194. failed_bh:
  195. brelse(first_bh);
  196. failed:
  197. return err;
  198. }
  199. /**
  200. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  201. * @inode: inode of the meta data file
  202. * @blkoff: block offset
  203. * @create: create flag
  204. * @init_block: initializer used for newly allocated block
  205. * @out_bh: output of a pointer to the buffer_head
  206. *
  207. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  208. * a new buffer if @create is not zero. On success, the returned buffer is
  209. * assured to be either existing or formatted using a buffer lock on success.
  210. * @out_bh is substituted only when zero is returned.
  211. *
  212. * Return Value: On success, it returns 0. On error, the following negative
  213. * error code is returned.
  214. *
  215. * %-ENOMEM - Insufficient memory available.
  216. *
  217. * %-EIO - I/O error
  218. *
  219. * %-ENOENT - the specified block does not exist (hole block)
  220. *
  221. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  222. *
  223. * %-EROFS - Read only filesystem (for create mode)
  224. */
  225. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  226. void (*init_block)(struct inode *,
  227. struct buffer_head *, void *),
  228. struct buffer_head **out_bh)
  229. {
  230. int ret;
  231. /* Should be rewritten with merging nilfs_mdt_read_block() */
  232. retry:
  233. ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
  234. if (!create || ret != -ENOENT)
  235. return ret;
  236. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  237. if (unlikely(ret == -EEXIST)) {
  238. /* create = 0; */ /* limit read-create loop retries */
  239. goto retry;
  240. }
  241. return ret;
  242. }
  243. /**
  244. * nilfs_mdt_delete_block - make a hole on the meta data file.
  245. * @inode: inode of the meta data file
  246. * @block: block offset
  247. *
  248. * Return Value: On success, zero is returned.
  249. * On error, one of the following negative error code is returned.
  250. *
  251. * %-ENOMEM - Insufficient memory available.
  252. *
  253. * %-EIO - I/O error
  254. *
  255. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  256. */
  257. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  258. {
  259. struct nilfs_inode_info *ii = NILFS_I(inode);
  260. int err;
  261. err = nilfs_bmap_delete(ii->i_bmap, block);
  262. if (!err || err == -ENOENT) {
  263. nilfs_mdt_mark_dirty(inode);
  264. nilfs_mdt_forget_block(inode, block);
  265. }
  266. return err;
  267. }
  268. /**
  269. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  270. * @inode: inode of the meta data file
  271. * @block: block offset
  272. *
  273. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  274. * tries to release the page including the buffer from a page cache.
  275. *
  276. * Return Value: On success, 0 is returned. On error, one of the following
  277. * negative error code is returned.
  278. *
  279. * %-EBUSY - page has an active buffer.
  280. *
  281. * %-ENOENT - page cache has no page addressed by the offset.
  282. */
  283. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  284. {
  285. pgoff_t index = (pgoff_t)block >>
  286. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  287. struct page *page;
  288. unsigned long first_block;
  289. int ret = 0;
  290. int still_dirty;
  291. page = find_lock_page(inode->i_mapping, index);
  292. if (!page)
  293. return -ENOENT;
  294. wait_on_page_writeback(page);
  295. first_block = (unsigned long)index <<
  296. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  297. if (page_has_buffers(page)) {
  298. struct buffer_head *bh;
  299. bh = nilfs_page_get_nth_block(page, block - first_block);
  300. nilfs_forget_buffer(bh);
  301. }
  302. still_dirty = PageDirty(page);
  303. unlock_page(page);
  304. page_cache_release(page);
  305. if (still_dirty ||
  306. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  307. ret = -EBUSY;
  308. return ret;
  309. }
  310. /**
  311. * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
  312. * @inode: inode of the meta data file
  313. * @block: block offset
  314. *
  315. * Return Value: On success, it returns 0. On error, the following negative
  316. * error code is returned.
  317. *
  318. * %-ENOMEM - Insufficient memory available.
  319. *
  320. * %-EIO - I/O error
  321. *
  322. * %-ENOENT - the specified block does not exist (hole block)
  323. *
  324. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  325. */
  326. int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
  327. {
  328. struct buffer_head *bh;
  329. int err;
  330. err = nilfs_mdt_read_block(inode, block, 0, &bh);
  331. if (unlikely(err))
  332. return err;
  333. nilfs_mark_buffer_dirty(bh);
  334. nilfs_mdt_mark_dirty(inode);
  335. brelse(bh);
  336. return 0;
  337. }
  338. int nilfs_mdt_fetch_dirty(struct inode *inode)
  339. {
  340. struct nilfs_inode_info *ii = NILFS_I(inode);
  341. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  342. set_bit(NILFS_I_DIRTY, &ii->i_state);
  343. return 1;
  344. }
  345. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  346. }
  347. static int
  348. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  349. {
  350. struct inode *inode;
  351. struct super_block *sb;
  352. struct the_nilfs *nilfs;
  353. struct nilfs_sb_info *writer = NULL;
  354. int err = 0;
  355. redirty_page_for_writepage(wbc, page);
  356. unlock_page(page);
  357. inode = page->mapping->host;
  358. if (!inode)
  359. return 0;
  360. sb = inode->i_sb;
  361. nilfs = NILFS_MDT(inode)->mi_nilfs;
  362. if (!sb) {
  363. down_read(&nilfs->ns_writer_sem);
  364. writer = nilfs->ns_writer;
  365. if (!writer) {
  366. up_read(&nilfs->ns_writer_sem);
  367. return -EROFS;
  368. }
  369. sb = writer->s_super;
  370. }
  371. if (wbc->sync_mode == WB_SYNC_ALL)
  372. err = nilfs_construct_segment(sb);
  373. else if (wbc->for_reclaim)
  374. nilfs_flush_segment(sb, inode->i_ino);
  375. if (writer)
  376. up_read(&nilfs->ns_writer_sem);
  377. return err;
  378. }
  379. static const struct address_space_operations def_mdt_aops = {
  380. .writepage = nilfs_mdt_write_page,
  381. .sync_page = block_sync_page,
  382. };
  383. static const struct inode_operations def_mdt_iops;
  384. static const struct file_operations def_mdt_fops;
  385. int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
  386. {
  387. struct nilfs_mdt_info *mi;
  388. mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
  389. if (!mi)
  390. return -ENOMEM;
  391. mi->mi_nilfs = NILFS_I_NILFS(inode);
  392. init_rwsem(&mi->mi_sem);
  393. inode->i_private = mi;
  394. inode->i_mode = S_IFREG;
  395. mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
  396. inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
  397. inode->i_op = &def_mdt_iops;
  398. inode->i_fop = &def_mdt_fops;
  399. inode->i_mapping->a_ops = &def_mdt_aops;
  400. return 0;
  401. }
  402. /*
  403. * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
  404. * ifile, or gcinodes. This allows the B-tree code and segment constructor
  405. * to treat them like regular files, and this helps to simplify the
  406. * implementation.
  407. * On the other hand, some of the pseudo inodes have an irregular point:
  408. * They don't have valid inode->i_sb pointer because their lifetimes are
  409. * longer than those of the super block structs; they may continue for
  410. * several consecutive mounts/umounts. This would need discussions.
  411. */
  412. /**
  413. * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
  414. * @nilfs: nilfs object
  415. * @sb: super block instance the metadata file belongs to
  416. * @ino: inode number
  417. */
  418. struct inode *
  419. nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
  420. ino_t ino)
  421. {
  422. struct inode *inode = nilfs_alloc_inode_common(nilfs);
  423. if (!inode)
  424. return NULL;
  425. else {
  426. struct address_space * const mapping = &inode->i_data;
  427. inode->i_sb = sb; /* sb may be NULL for some meta data files */
  428. inode->i_blkbits = nilfs->ns_blocksize_bits;
  429. inode->i_flags = 0;
  430. atomic_set(&inode->i_count, 1);
  431. inode->i_nlink = 1;
  432. inode->i_ino = ino;
  433. #ifdef INIT_UNUSED_INODE_FIELDS
  434. atomic_set(&inode->i_writecount, 0);
  435. inode->i_size = 0;
  436. inode->i_blocks = 0;
  437. inode->i_bytes = 0;
  438. inode->i_generation = 0;
  439. #ifdef CONFIG_QUOTA
  440. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  441. #endif
  442. inode->i_pipe = NULL;
  443. inode->i_bdev = NULL;
  444. inode->i_cdev = NULL;
  445. inode->i_rdev = 0;
  446. #ifdef CONFIG_SECURITY
  447. inode->i_security = NULL;
  448. #endif
  449. inode->dirtied_when = 0;
  450. INIT_LIST_HEAD(&inode->i_list);
  451. INIT_LIST_HEAD(&inode->i_sb_list);
  452. inode->i_state = 0;
  453. #endif
  454. spin_lock_init(&inode->i_lock);
  455. mutex_init(&inode->i_mutex);
  456. init_rwsem(&inode->i_alloc_sem);
  457. mapping->host = NULL; /* instead of inode */
  458. mapping->flags = 0;
  459. mapping->assoc_mapping = NULL;
  460. inode->i_mapping = mapping;
  461. }
  462. return inode;
  463. }
  464. struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
  465. ino_t ino, size_t objsz)
  466. {
  467. struct inode *inode;
  468. inode = nilfs_mdt_new_common(nilfs, sb, ino);
  469. if (!inode)
  470. return NULL;
  471. if (nilfs_mdt_init(inode, NILFS_MDT_GFP, objsz) < 0) {
  472. nilfs_destroy_inode(inode);
  473. return NULL;
  474. }
  475. return inode;
  476. }
  477. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
  478. unsigned header_size)
  479. {
  480. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  481. mi->mi_entry_size = entry_size;
  482. mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
  483. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  484. }
  485. static const struct address_space_operations shadow_map_aops = {
  486. .sync_page = block_sync_page,
  487. };
  488. /**
  489. * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
  490. * @inode: inode of the metadata file
  491. * @shadow: shadow mapping
  492. */
  493. int nilfs_mdt_setup_shadow_map(struct inode *inode,
  494. struct nilfs_shadow_map *shadow)
  495. {
  496. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  497. struct backing_dev_info *bdi = NILFS_I_NILFS(inode)->ns_bdi;
  498. INIT_LIST_HEAD(&shadow->frozen_buffers);
  499. nilfs_mapping_init_once(&shadow->frozen_data);
  500. nilfs_mapping_init(&shadow->frozen_data, bdi, &shadow_map_aops);
  501. nilfs_mapping_init_once(&shadow->frozen_btnodes);
  502. nilfs_mapping_init(&shadow->frozen_btnodes, bdi, &shadow_map_aops);
  503. mi->mi_shadow = shadow;
  504. return 0;
  505. }
  506. /**
  507. * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
  508. * @inode: inode of the metadata file
  509. */
  510. int nilfs_mdt_save_to_shadow_map(struct inode *inode)
  511. {
  512. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  513. struct nilfs_inode_info *ii = NILFS_I(inode);
  514. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  515. int ret;
  516. ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
  517. if (ret)
  518. goto out;
  519. ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
  520. &ii->i_btnode_cache);
  521. if (ret)
  522. goto out;
  523. nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
  524. out:
  525. return ret;
  526. }
  527. int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
  528. {
  529. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  530. struct buffer_head *bh_frozen;
  531. struct page *page;
  532. int blkbits = inode->i_blkbits;
  533. int ret = -ENOMEM;
  534. page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
  535. if (!page)
  536. return ret;
  537. if (!page_has_buffers(page))
  538. create_empty_buffers(page, 1 << blkbits, 0);
  539. bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
  540. if (bh_frozen) {
  541. if (!buffer_uptodate(bh_frozen))
  542. nilfs_copy_buffer(bh_frozen, bh);
  543. if (list_empty(&bh_frozen->b_assoc_buffers)) {
  544. list_add_tail(&bh_frozen->b_assoc_buffers,
  545. &shadow->frozen_buffers);
  546. set_buffer_nilfs_redirected(bh);
  547. } else {
  548. brelse(bh_frozen); /* already frozen */
  549. }
  550. ret = 0;
  551. }
  552. unlock_page(page);
  553. page_cache_release(page);
  554. return ret;
  555. }
  556. struct buffer_head *
  557. nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
  558. {
  559. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  560. struct buffer_head *bh_frozen = NULL;
  561. struct page *page;
  562. int n;
  563. page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
  564. if (page) {
  565. if (page_has_buffers(page)) {
  566. n = bh_offset(bh) >> inode->i_blkbits;
  567. bh_frozen = nilfs_page_get_nth_block(page, n);
  568. }
  569. unlock_page(page);
  570. page_cache_release(page);
  571. }
  572. return bh_frozen;
  573. }
  574. static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
  575. {
  576. struct list_head *head = &shadow->frozen_buffers;
  577. struct buffer_head *bh;
  578. while (!list_empty(head)) {
  579. bh = list_first_entry(head, struct buffer_head,
  580. b_assoc_buffers);
  581. list_del_init(&bh->b_assoc_buffers);
  582. brelse(bh); /* drop ref-count to make it releasable */
  583. }
  584. }
  585. /**
  586. * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
  587. * @inode: inode of the metadata file
  588. */
  589. void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
  590. {
  591. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  592. struct nilfs_inode_info *ii = NILFS_I(inode);
  593. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  594. down_write(&mi->mi_sem);
  595. if (mi->mi_palloc_cache)
  596. nilfs_palloc_clear_cache(inode);
  597. nilfs_clear_dirty_pages(inode->i_mapping);
  598. nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
  599. nilfs_clear_dirty_pages(&ii->i_btnode_cache);
  600. nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
  601. nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
  602. up_write(&mi->mi_sem);
  603. }
  604. /**
  605. * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
  606. * @inode: inode of the metadata file
  607. */
  608. void nilfs_mdt_clear_shadow_map(struct inode *inode)
  609. {
  610. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  611. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  612. down_write(&mi->mi_sem);
  613. nilfs_release_frozen_buffers(shadow);
  614. truncate_inode_pages(&shadow->frozen_data, 0);
  615. truncate_inode_pages(&shadow->frozen_btnodes, 0);
  616. up_write(&mi->mi_sem);
  617. }
  618. static void nilfs_mdt_clear(struct inode *inode)
  619. {
  620. struct nilfs_inode_info *ii = NILFS_I(inode);
  621. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  622. truncate_inode_pages(inode->i_mapping, 0);
  623. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  624. nilfs_bmap_clear(ii->i_bmap);
  625. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  626. }
  627. void nilfs_mdt_destroy(struct inode *inode)
  628. {
  629. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  630. if (mdi->mi_palloc_cache)
  631. nilfs_palloc_destroy_cache(inode);
  632. nilfs_mdt_clear(inode);
  633. nilfs_destroy_inode(inode);
  634. }