mdt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. #include <trace/events/nilfs2.h>
  35. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  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);
  53. memset(kaddr + bh_offset(bh), 0, i_blocksize(inode));
  54. if (init_block)
  55. init_block(inode, bh, kaddr);
  56. flush_dcache_page(bh->b_page);
  57. kunmap_atomic(kaddr);
  58. set_buffer_uptodate(bh);
  59. mark_buffer_dirty(bh);
  60. nilfs_mdt_mark_dirty(inode);
  61. trace_nilfs2_mdt_insert_new_block(inode, inode->i_ino, block);
  62. return 0;
  63. }
  64. static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
  65. struct buffer_head **out_bh,
  66. void (*init_block)(struct inode *,
  67. struct buffer_head *,
  68. void *))
  69. {
  70. struct super_block *sb = inode->i_sb;
  71. struct nilfs_transaction_info ti;
  72. struct buffer_head *bh;
  73. int err;
  74. nilfs_transaction_begin(sb, &ti, 0);
  75. err = -ENOMEM;
  76. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  77. if (unlikely(!bh))
  78. goto failed_unlock;
  79. err = -EEXIST;
  80. if (buffer_uptodate(bh))
  81. goto failed_bh;
  82. wait_on_buffer(bh);
  83. if (buffer_uptodate(bh))
  84. goto failed_bh;
  85. bh->b_bdev = sb->s_bdev;
  86. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  87. if (likely(!err)) {
  88. get_bh(bh);
  89. *out_bh = bh;
  90. }
  91. failed_bh:
  92. unlock_page(bh->b_page);
  93. page_cache_release(bh->b_page);
  94. brelse(bh);
  95. failed_unlock:
  96. if (likely(!err))
  97. err = nilfs_transaction_commit(sb);
  98. else
  99. nilfs_transaction_abort(sb);
  100. return err;
  101. }
  102. static int
  103. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  104. int mode, struct buffer_head **out_bh)
  105. {
  106. struct buffer_head *bh;
  107. __u64 blknum = 0;
  108. int ret = -ENOMEM;
  109. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  110. if (unlikely(!bh))
  111. goto failed;
  112. ret = -EEXIST; /* internal code */
  113. if (buffer_uptodate(bh))
  114. goto out;
  115. if (mode == READA) {
  116. if (!trylock_buffer(bh)) {
  117. ret = -EBUSY;
  118. goto failed_bh;
  119. }
  120. } else /* mode == READ */
  121. lock_buffer(bh);
  122. if (buffer_uptodate(bh)) {
  123. unlock_buffer(bh);
  124. goto out;
  125. }
  126. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
  127. if (unlikely(ret)) {
  128. unlock_buffer(bh);
  129. goto failed_bh;
  130. }
  131. map_bh(bh, inode->i_sb, (sector_t)blknum);
  132. bh->b_end_io = end_buffer_read_sync;
  133. get_bh(bh);
  134. submit_bh(mode, bh);
  135. ret = 0;
  136. trace_nilfs2_mdt_submit_block(inode, inode->i_ino, blkoff, mode);
  137. out:
  138. get_bh(bh);
  139. *out_bh = bh;
  140. failed_bh:
  141. unlock_page(bh->b_page);
  142. page_cache_release(bh->b_page);
  143. brelse(bh);
  144. failed:
  145. return ret;
  146. }
  147. static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
  148. int readahead, struct buffer_head **out_bh)
  149. {
  150. struct buffer_head *first_bh, *bh;
  151. unsigned long blkoff;
  152. int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
  153. int err;
  154. err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
  155. if (err == -EEXIST) /* internal code */
  156. goto out;
  157. if (unlikely(err))
  158. goto failed;
  159. if (readahead) {
  160. blkoff = block + 1;
  161. for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
  162. err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
  163. if (likely(!err || err == -EEXIST))
  164. brelse(bh);
  165. else if (err != -EBUSY)
  166. break;
  167. /* abort readahead if bmap lookup failed */
  168. if (!buffer_locked(first_bh))
  169. goto out_no_wait;
  170. }
  171. }
  172. wait_on_buffer(first_bh);
  173. out_no_wait:
  174. err = -EIO;
  175. if (!buffer_uptodate(first_bh))
  176. goto failed_bh;
  177. out:
  178. *out_bh = first_bh;
  179. return 0;
  180. failed_bh:
  181. brelse(first_bh);
  182. failed:
  183. return err;
  184. }
  185. /**
  186. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  187. * @inode: inode of the meta data file
  188. * @blkoff: block offset
  189. * @create: create flag
  190. * @init_block: initializer used for newly allocated block
  191. * @out_bh: output of a pointer to the buffer_head
  192. *
  193. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  194. * a new buffer if @create is not zero. On success, the returned buffer is
  195. * assured to be either existing or formatted using a buffer lock on success.
  196. * @out_bh is substituted only when zero is returned.
  197. *
  198. * Return Value: On success, it returns 0. On error, the following negative
  199. * error code is returned.
  200. *
  201. * %-ENOMEM - Insufficient memory available.
  202. *
  203. * %-EIO - I/O error
  204. *
  205. * %-ENOENT - the specified block does not exist (hole block)
  206. *
  207. * %-EROFS - Read only filesystem (for create mode)
  208. */
  209. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  210. void (*init_block)(struct inode *,
  211. struct buffer_head *, void *),
  212. struct buffer_head **out_bh)
  213. {
  214. int ret;
  215. /* Should be rewritten with merging nilfs_mdt_read_block() */
  216. retry:
  217. ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
  218. if (!create || ret != -ENOENT)
  219. return ret;
  220. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  221. if (unlikely(ret == -EEXIST)) {
  222. /* create = 0; */ /* limit read-create loop retries */
  223. goto retry;
  224. }
  225. return ret;
  226. }
  227. /**
  228. * nilfs_mdt_find_block - find and get a buffer on meta data file.
  229. * @inode: inode of the meta data file
  230. * @start: start block offset (inclusive)
  231. * @end: end block offset (inclusive)
  232. * @blkoff: block offset
  233. * @out_bh: place to store a pointer to buffer_head struct
  234. *
  235. * nilfs_mdt_find_block() looks up an existing block in range of
  236. * [@start, @end] and stores pointer to a buffer head of the block to
  237. * @out_bh, and block offset to @blkoff, respectively. @out_bh and
  238. * @blkoff are substituted only when zero is returned.
  239. *
  240. * Return Value: On success, it returns 0. On error, the following negative
  241. * error code is returned.
  242. *
  243. * %-ENOMEM - Insufficient memory available.
  244. *
  245. * %-EIO - I/O error
  246. *
  247. * %-ENOENT - no block was found in the range
  248. */
  249. int nilfs_mdt_find_block(struct inode *inode, unsigned long start,
  250. unsigned long end, unsigned long *blkoff,
  251. struct buffer_head **out_bh)
  252. {
  253. __u64 next;
  254. int ret;
  255. if (unlikely(start > end))
  256. return -ENOENT;
  257. ret = nilfs_mdt_read_block(inode, start, true, out_bh);
  258. if (!ret) {
  259. *blkoff = start;
  260. goto out;
  261. }
  262. if (unlikely(ret != -ENOENT || start == ULONG_MAX))
  263. goto out;
  264. ret = nilfs_bmap_seek_key(NILFS_I(inode)->i_bmap, start + 1, &next);
  265. if (!ret) {
  266. if (next <= end) {
  267. ret = nilfs_mdt_read_block(inode, next, true, out_bh);
  268. if (!ret)
  269. *blkoff = next;
  270. } else {
  271. ret = -ENOENT;
  272. }
  273. }
  274. out:
  275. return ret;
  276. }
  277. /**
  278. * nilfs_mdt_delete_block - make a hole on the meta data file.
  279. * @inode: inode of the meta data file
  280. * @block: block offset
  281. *
  282. * Return Value: On success, zero is returned.
  283. * On error, one of the following negative error code is returned.
  284. *
  285. * %-ENOMEM - Insufficient memory available.
  286. *
  287. * %-EIO - I/O error
  288. */
  289. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  290. {
  291. struct nilfs_inode_info *ii = NILFS_I(inode);
  292. int err;
  293. err = nilfs_bmap_delete(ii->i_bmap, block);
  294. if (!err || err == -ENOENT) {
  295. nilfs_mdt_mark_dirty(inode);
  296. nilfs_mdt_forget_block(inode, block);
  297. }
  298. return err;
  299. }
  300. /**
  301. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  302. * @inode: inode of the meta data file
  303. * @block: block offset
  304. *
  305. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  306. * tries to release the page including the buffer from a page cache.
  307. *
  308. * Return Value: On success, 0 is returned. On error, one of the following
  309. * negative error code is returned.
  310. *
  311. * %-EBUSY - page has an active buffer.
  312. *
  313. * %-ENOENT - page cache has no page addressed by the offset.
  314. */
  315. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  316. {
  317. pgoff_t index = (pgoff_t)block >>
  318. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  319. struct page *page;
  320. unsigned long first_block;
  321. int ret = 0;
  322. int still_dirty;
  323. page = find_lock_page(inode->i_mapping, index);
  324. if (!page)
  325. return -ENOENT;
  326. wait_on_page_writeback(page);
  327. first_block = (unsigned long)index <<
  328. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  329. if (page_has_buffers(page)) {
  330. struct buffer_head *bh;
  331. bh = nilfs_page_get_nth_block(page, block - first_block);
  332. nilfs_forget_buffer(bh);
  333. }
  334. still_dirty = PageDirty(page);
  335. unlock_page(page);
  336. page_cache_release(page);
  337. if (still_dirty ||
  338. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  339. ret = -EBUSY;
  340. return ret;
  341. }
  342. /**
  343. * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
  344. * @inode: inode of the meta data file
  345. * @block: block offset
  346. *
  347. * Return Value: On success, it returns 0. On error, the following negative
  348. * error code is returned.
  349. *
  350. * %-ENOMEM - Insufficient memory available.
  351. *
  352. * %-EIO - I/O error
  353. *
  354. * %-ENOENT - the specified block does not exist (hole block)
  355. */
  356. int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
  357. {
  358. struct buffer_head *bh;
  359. int err;
  360. err = nilfs_mdt_read_block(inode, block, 0, &bh);
  361. if (unlikely(err))
  362. return err;
  363. mark_buffer_dirty(bh);
  364. nilfs_mdt_mark_dirty(inode);
  365. brelse(bh);
  366. return 0;
  367. }
  368. int nilfs_mdt_fetch_dirty(struct inode *inode)
  369. {
  370. struct nilfs_inode_info *ii = NILFS_I(inode);
  371. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  372. set_bit(NILFS_I_DIRTY, &ii->i_state);
  373. return 1;
  374. }
  375. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  376. }
  377. static int
  378. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  379. {
  380. struct inode *inode = page->mapping->host;
  381. struct super_block *sb;
  382. int err = 0;
  383. if (inode && (inode->i_sb->s_flags & MS_RDONLY)) {
  384. /*
  385. * It means that filesystem was remounted in read-only
  386. * mode because of error or metadata corruption. But we
  387. * have dirty pages that try to be flushed in background.
  388. * So, here we simply discard this dirty page.
  389. */
  390. nilfs_clear_dirty_page(page, false);
  391. unlock_page(page);
  392. return -EROFS;
  393. }
  394. redirty_page_for_writepage(wbc, page);
  395. unlock_page(page);
  396. if (!inode)
  397. return 0;
  398. sb = inode->i_sb;
  399. if (wbc->sync_mode == WB_SYNC_ALL)
  400. err = nilfs_construct_segment(sb);
  401. else if (wbc->for_reclaim)
  402. nilfs_flush_segment(sb, inode->i_ino);
  403. return err;
  404. }
  405. static const struct address_space_operations def_mdt_aops = {
  406. .writepage = nilfs_mdt_write_page,
  407. };
  408. static const struct inode_operations def_mdt_iops;
  409. static const struct file_operations def_mdt_fops;
  410. int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz)
  411. {
  412. struct nilfs_mdt_info *mi;
  413. mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
  414. if (!mi)
  415. return -ENOMEM;
  416. init_rwsem(&mi->mi_sem);
  417. inode->i_private = mi;
  418. inode->i_mode = S_IFREG;
  419. mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
  420. inode->i_op = &def_mdt_iops;
  421. inode->i_fop = &def_mdt_fops;
  422. inode->i_mapping->a_ops = &def_mdt_aops;
  423. return 0;
  424. }
  425. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
  426. unsigned header_size)
  427. {
  428. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  429. mi->mi_entry_size = entry_size;
  430. mi->mi_entries_per_block = i_blocksize(inode) / entry_size;
  431. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  432. }
  433. /**
  434. * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file
  435. * @inode: inode of the metadata file
  436. * @shadow: shadow mapping
  437. */
  438. int nilfs_mdt_setup_shadow_map(struct inode *inode,
  439. struct nilfs_shadow_map *shadow)
  440. {
  441. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  442. INIT_LIST_HEAD(&shadow->frozen_buffers);
  443. address_space_init_once(&shadow->frozen_data);
  444. nilfs_mapping_init(&shadow->frozen_data, inode);
  445. address_space_init_once(&shadow->frozen_btnodes);
  446. nilfs_mapping_init(&shadow->frozen_btnodes, inode);
  447. mi->mi_shadow = shadow;
  448. return 0;
  449. }
  450. /**
  451. * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map
  452. * @inode: inode of the metadata file
  453. */
  454. int nilfs_mdt_save_to_shadow_map(struct inode *inode)
  455. {
  456. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  457. struct nilfs_inode_info *ii = NILFS_I(inode);
  458. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  459. int ret;
  460. ret = nilfs_copy_dirty_pages(&shadow->frozen_data, inode->i_mapping);
  461. if (ret)
  462. goto out;
  463. ret = nilfs_copy_dirty_pages(&shadow->frozen_btnodes,
  464. &ii->i_btnode_cache);
  465. if (ret)
  466. goto out;
  467. nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store);
  468. out:
  469. return ret;
  470. }
  471. int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh)
  472. {
  473. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  474. struct buffer_head *bh_frozen;
  475. struct page *page;
  476. int blkbits = inode->i_blkbits;
  477. page = grab_cache_page(&shadow->frozen_data, bh->b_page->index);
  478. if (!page)
  479. return -ENOMEM;
  480. if (!page_has_buffers(page))
  481. create_empty_buffers(page, 1 << blkbits, 0);
  482. bh_frozen = nilfs_page_get_nth_block(page, bh_offset(bh) >> blkbits);
  483. if (!buffer_uptodate(bh_frozen))
  484. nilfs_copy_buffer(bh_frozen, bh);
  485. if (list_empty(&bh_frozen->b_assoc_buffers)) {
  486. list_add_tail(&bh_frozen->b_assoc_buffers,
  487. &shadow->frozen_buffers);
  488. set_buffer_nilfs_redirected(bh);
  489. } else {
  490. brelse(bh_frozen); /* already frozen */
  491. }
  492. unlock_page(page);
  493. page_cache_release(page);
  494. return 0;
  495. }
  496. struct buffer_head *
  497. nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh)
  498. {
  499. struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow;
  500. struct buffer_head *bh_frozen = NULL;
  501. struct page *page;
  502. int n;
  503. page = find_lock_page(&shadow->frozen_data, bh->b_page->index);
  504. if (page) {
  505. if (page_has_buffers(page)) {
  506. n = bh_offset(bh) >> inode->i_blkbits;
  507. bh_frozen = nilfs_page_get_nth_block(page, n);
  508. }
  509. unlock_page(page);
  510. page_cache_release(page);
  511. }
  512. return bh_frozen;
  513. }
  514. static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow)
  515. {
  516. struct list_head *head = &shadow->frozen_buffers;
  517. struct buffer_head *bh;
  518. while (!list_empty(head)) {
  519. bh = list_first_entry(head, struct buffer_head,
  520. b_assoc_buffers);
  521. list_del_init(&bh->b_assoc_buffers);
  522. brelse(bh); /* drop ref-count to make it releasable */
  523. }
  524. }
  525. /**
  526. * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state
  527. * @inode: inode of the metadata file
  528. */
  529. void nilfs_mdt_restore_from_shadow_map(struct inode *inode)
  530. {
  531. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  532. struct nilfs_inode_info *ii = NILFS_I(inode);
  533. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  534. down_write(&mi->mi_sem);
  535. if (mi->mi_palloc_cache)
  536. nilfs_palloc_clear_cache(inode);
  537. nilfs_clear_dirty_pages(inode->i_mapping, true);
  538. nilfs_copy_back_pages(inode->i_mapping, &shadow->frozen_data);
  539. nilfs_clear_dirty_pages(&ii->i_btnode_cache, true);
  540. nilfs_copy_back_pages(&ii->i_btnode_cache, &shadow->frozen_btnodes);
  541. nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store);
  542. up_write(&mi->mi_sem);
  543. }
  544. /**
  545. * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches
  546. * @inode: inode of the metadata file
  547. */
  548. void nilfs_mdt_clear_shadow_map(struct inode *inode)
  549. {
  550. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  551. struct nilfs_shadow_map *shadow = mi->mi_shadow;
  552. down_write(&mi->mi_sem);
  553. nilfs_release_frozen_buffers(shadow);
  554. truncate_inode_pages(&shadow->frozen_data, 0);
  555. truncate_inode_pages(&shadow->frozen_btnodes, 0);
  556. up_write(&mi->mi_sem);
  557. }