mdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "segment.h"
  31. #include "page.h"
  32. #include "mdt.h"
  33. #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1)
  34. #define INIT_UNUSED_INODE_FIELDS
  35. static int
  36. nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block,
  37. struct buffer_head *bh,
  38. void (*init_block)(struct inode *,
  39. struct buffer_head *, void *))
  40. {
  41. struct nilfs_inode_info *ii = NILFS_I(inode);
  42. void *kaddr;
  43. int ret;
  44. /* Caller exclude read accesses using page lock */
  45. /* set_buffer_new(bh); */
  46. bh->b_blocknr = 0;
  47. ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh);
  48. if (unlikely(ret))
  49. return ret;
  50. set_buffer_mapped(bh);
  51. kaddr = kmap_atomic(bh->b_page, KM_USER0);
  52. memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits);
  53. if (init_block)
  54. init_block(inode, bh, kaddr);
  55. flush_dcache_page(bh->b_page);
  56. kunmap_atomic(kaddr, KM_USER0);
  57. set_buffer_uptodate(bh);
  58. nilfs_mark_buffer_dirty(bh);
  59. nilfs_mdt_mark_dirty(inode);
  60. return 0;
  61. }
  62. static int nilfs_mdt_create_block(struct inode *inode, unsigned long block,
  63. struct buffer_head **out_bh,
  64. void (*init_block)(struct inode *,
  65. struct buffer_head *,
  66. void *))
  67. {
  68. struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
  69. struct super_block *sb = inode->i_sb;
  70. struct nilfs_transaction_info ti;
  71. struct buffer_head *bh;
  72. int err;
  73. if (!sb) {
  74. /*
  75. * Make sure this function is not called from any
  76. * read-only context.
  77. */
  78. if (!nilfs->ns_writer) {
  79. WARN_ON(1);
  80. err = -EROFS;
  81. goto out;
  82. }
  83. sb = nilfs->ns_writer->s_super;
  84. }
  85. nilfs_transaction_begin(sb, &ti, 0);
  86. err = -ENOMEM;
  87. bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0);
  88. if (unlikely(!bh))
  89. goto failed_unlock;
  90. err = -EEXIST;
  91. if (buffer_uptodate(bh))
  92. goto failed_bh;
  93. wait_on_buffer(bh);
  94. if (buffer_uptodate(bh))
  95. goto failed_bh;
  96. bh->b_bdev = nilfs->ns_bdev;
  97. err = nilfs_mdt_insert_new_block(inode, block, bh, init_block);
  98. if (likely(!err)) {
  99. get_bh(bh);
  100. *out_bh = bh;
  101. }
  102. failed_bh:
  103. unlock_page(bh->b_page);
  104. page_cache_release(bh->b_page);
  105. brelse(bh);
  106. failed_unlock:
  107. if (likely(!err))
  108. err = nilfs_transaction_commit(sb);
  109. else
  110. nilfs_transaction_abort(sb);
  111. out:
  112. return err;
  113. }
  114. static int
  115. nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff,
  116. int mode, struct buffer_head **out_bh)
  117. {
  118. struct buffer_head *bh;
  119. __u64 blknum = 0;
  120. int ret = -ENOMEM;
  121. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  122. if (unlikely(!bh))
  123. goto failed;
  124. ret = -EEXIST; /* internal code */
  125. if (buffer_uptodate(bh))
  126. goto out;
  127. if (mode == READA) {
  128. if (!trylock_buffer(bh)) {
  129. ret = -EBUSY;
  130. goto failed_bh;
  131. }
  132. } else /* mode == READ */
  133. lock_buffer(bh);
  134. if (buffer_uptodate(bh)) {
  135. unlock_buffer(bh);
  136. goto out;
  137. }
  138. ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum);
  139. if (unlikely(ret)) {
  140. unlock_buffer(bh);
  141. goto failed_bh;
  142. }
  143. bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev;
  144. bh->b_blocknr = (sector_t)blknum;
  145. set_buffer_mapped(bh);
  146. bh->b_end_io = end_buffer_read_sync;
  147. get_bh(bh);
  148. submit_bh(mode, bh);
  149. ret = 0;
  150. out:
  151. get_bh(bh);
  152. *out_bh = bh;
  153. failed_bh:
  154. unlock_page(bh->b_page);
  155. page_cache_release(bh->b_page);
  156. brelse(bh);
  157. failed:
  158. return ret;
  159. }
  160. static int nilfs_mdt_read_block(struct inode *inode, unsigned long block,
  161. int readahead, struct buffer_head **out_bh)
  162. {
  163. struct buffer_head *first_bh, *bh;
  164. unsigned long blkoff;
  165. int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS;
  166. int err;
  167. err = nilfs_mdt_submit_block(inode, block, READ, &first_bh);
  168. if (err == -EEXIST) /* internal code */
  169. goto out;
  170. if (unlikely(err))
  171. goto failed;
  172. if (readahead) {
  173. blkoff = block + 1;
  174. for (i = 0; i < nr_ra_blocks; i++, blkoff++) {
  175. err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh);
  176. if (likely(!err || err == -EEXIST))
  177. brelse(bh);
  178. else if (err != -EBUSY)
  179. break;
  180. /* abort readahead if bmap lookup failed */
  181. if (!buffer_locked(first_bh))
  182. goto out_no_wait;
  183. }
  184. }
  185. wait_on_buffer(first_bh);
  186. out_no_wait:
  187. err = -EIO;
  188. if (!buffer_uptodate(first_bh))
  189. goto failed_bh;
  190. out:
  191. *out_bh = first_bh;
  192. return 0;
  193. failed_bh:
  194. brelse(first_bh);
  195. failed:
  196. return err;
  197. }
  198. /**
  199. * nilfs_mdt_get_block - read or create a buffer on meta data file.
  200. * @inode: inode of the meta data file
  201. * @blkoff: block offset
  202. * @create: create flag
  203. * @init_block: initializer used for newly allocated block
  204. * @out_bh: output of a pointer to the buffer_head
  205. *
  206. * nilfs_mdt_get_block() looks up the specified buffer and tries to create
  207. * a new buffer if @create is not zero. On success, the returned buffer is
  208. * assured to be either existing or formatted using a buffer lock on success.
  209. * @out_bh is substituted only when zero is returned.
  210. *
  211. * Return Value: On success, it returns 0. On error, the following negative
  212. * error code is returned.
  213. *
  214. * %-ENOMEM - Insufficient memory available.
  215. *
  216. * %-EIO - I/O error
  217. *
  218. * %-ENOENT - the specified block does not exist (hole block)
  219. *
  220. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  221. *
  222. * %-EROFS - Read only filesystem (for create mode)
  223. */
  224. int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create,
  225. void (*init_block)(struct inode *,
  226. struct buffer_head *, void *),
  227. struct buffer_head **out_bh)
  228. {
  229. int ret;
  230. /* Should be rewritten with merging nilfs_mdt_read_block() */
  231. retry:
  232. ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh);
  233. if (!create || ret != -ENOENT)
  234. return ret;
  235. ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block);
  236. if (unlikely(ret == -EEXIST)) {
  237. /* create = 0; */ /* limit read-create loop retries */
  238. goto retry;
  239. }
  240. return ret;
  241. }
  242. /**
  243. * nilfs_mdt_delete_block - make a hole on the meta data file.
  244. * @inode: inode of the meta data file
  245. * @block: block offset
  246. *
  247. * Return Value: On success, zero is returned.
  248. * On error, one of the following negative error code is returned.
  249. *
  250. * %-ENOMEM - Insufficient memory available.
  251. *
  252. * %-EIO - I/O error
  253. *
  254. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  255. */
  256. int nilfs_mdt_delete_block(struct inode *inode, unsigned long block)
  257. {
  258. struct nilfs_inode_info *ii = NILFS_I(inode);
  259. int err;
  260. err = nilfs_bmap_delete(ii->i_bmap, block);
  261. if (!err || err == -ENOENT) {
  262. nilfs_mdt_mark_dirty(inode);
  263. nilfs_mdt_forget_block(inode, block);
  264. }
  265. return err;
  266. }
  267. /**
  268. * nilfs_mdt_forget_block - discard dirty state and try to remove the page
  269. * @inode: inode of the meta data file
  270. * @block: block offset
  271. *
  272. * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and
  273. * tries to release the page including the buffer from a page cache.
  274. *
  275. * Return Value: On success, 0 is returned. On error, one of the following
  276. * negative error code is returned.
  277. *
  278. * %-EBUSY - page has an active buffer.
  279. *
  280. * %-ENOENT - page cache has no page addressed by the offset.
  281. */
  282. int nilfs_mdt_forget_block(struct inode *inode, unsigned long block)
  283. {
  284. pgoff_t index = (pgoff_t)block >>
  285. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  286. struct page *page;
  287. unsigned long first_block;
  288. int ret = 0;
  289. int still_dirty;
  290. page = find_lock_page(inode->i_mapping, index);
  291. if (!page)
  292. return -ENOENT;
  293. wait_on_page_writeback(page);
  294. first_block = (unsigned long)index <<
  295. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  296. if (page_has_buffers(page)) {
  297. struct buffer_head *bh;
  298. bh = nilfs_page_get_nth_block(page, block - first_block);
  299. nilfs_forget_buffer(bh);
  300. }
  301. still_dirty = PageDirty(page);
  302. unlock_page(page);
  303. page_cache_release(page);
  304. if (still_dirty ||
  305. invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0)
  306. ret = -EBUSY;
  307. return ret;
  308. }
  309. /**
  310. * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty.
  311. * @inode: inode of the meta data file
  312. * @block: block offset
  313. *
  314. * Return Value: On success, it returns 0. On error, the following negative
  315. * error code is returned.
  316. *
  317. * %-ENOMEM - Insufficient memory available.
  318. *
  319. * %-EIO - I/O error
  320. *
  321. * %-ENOENT - the specified block does not exist (hole block)
  322. *
  323. * %-EINVAL - bmap is broken. (the caller should call nilfs_error())
  324. */
  325. int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block)
  326. {
  327. struct buffer_head *bh;
  328. int err;
  329. err = nilfs_mdt_read_block(inode, block, 0, &bh);
  330. if (unlikely(err))
  331. return err;
  332. nilfs_mark_buffer_dirty(bh);
  333. nilfs_mdt_mark_dirty(inode);
  334. brelse(bh);
  335. return 0;
  336. }
  337. int nilfs_mdt_fetch_dirty(struct inode *inode)
  338. {
  339. struct nilfs_inode_info *ii = NILFS_I(inode);
  340. if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) {
  341. set_bit(NILFS_I_DIRTY, &ii->i_state);
  342. return 1;
  343. }
  344. return test_bit(NILFS_I_DIRTY, &ii->i_state);
  345. }
  346. static int
  347. nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc)
  348. {
  349. struct inode *inode = container_of(page->mapping,
  350. struct inode, i_data);
  351. struct super_block *sb = inode->i_sb;
  352. struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs;
  353. struct nilfs_sb_info *writer = NULL;
  354. int err = 0;
  355. redirty_page_for_writepage(wbc, page);
  356. unlock_page(page);
  357. if (page->mapping->assoc_mapping)
  358. return 0; /* Do not request flush for shadow page cache */
  359. if (!sb) {
  360. down_read(&nilfs->ns_writer_sem);
  361. writer = nilfs->ns_writer;
  362. if (!writer) {
  363. up_read(&nilfs->ns_writer_sem);
  364. return -EROFS;
  365. }
  366. sb = writer->s_super;
  367. }
  368. if (wbc->sync_mode == WB_SYNC_ALL)
  369. err = nilfs_construct_segment(sb);
  370. else if (wbc->for_reclaim)
  371. nilfs_flush_segment(sb, inode->i_ino);
  372. if (writer)
  373. up_read(&nilfs->ns_writer_sem);
  374. return err;
  375. }
  376. static const struct address_space_operations def_mdt_aops = {
  377. .writepage = nilfs_mdt_write_page,
  378. .sync_page = block_sync_page,
  379. };
  380. static const struct inode_operations def_mdt_iops;
  381. static const struct file_operations def_mdt_fops;
  382. /*
  383. * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile,
  384. * ifile, or gcinodes. This allows the B-tree code and segment constructor
  385. * to treat them like regular files, and this helps to simplify the
  386. * implementation.
  387. * On the other hand, some of the pseudo inodes have an irregular point:
  388. * They don't have valid inode->i_sb pointer because their lifetimes are
  389. * longer than those of the super block structs; they may continue for
  390. * several consecutive mounts/umounts. This would need discussions.
  391. */
  392. /**
  393. * nilfs_mdt_new_common - allocate a pseudo inode for metadata file
  394. * @nilfs: nilfs object
  395. * @sb: super block instance the metadata file belongs to
  396. * @ino: inode number
  397. * @gfp_mask: gfp mask for data pages
  398. * @objsz: size of the private object attached to inode->i_private
  399. */
  400. struct inode *
  401. nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb,
  402. ino_t ino, gfp_t gfp_mask, size_t objsz)
  403. {
  404. struct inode *inode = nilfs_alloc_inode_common(nilfs);
  405. if (!inode)
  406. return NULL;
  407. else {
  408. struct address_space * const mapping = &inode->i_data;
  409. struct nilfs_mdt_info *mi;
  410. mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS);
  411. if (!mi) {
  412. nilfs_destroy_inode(inode);
  413. return NULL;
  414. }
  415. mi->mi_nilfs = nilfs;
  416. init_rwsem(&mi->mi_sem);
  417. inode->i_sb = sb; /* sb may be NULL for some meta data files */
  418. inode->i_blkbits = nilfs->ns_blocksize_bits;
  419. inode->i_flags = 0;
  420. atomic_set(&inode->i_count, 1);
  421. inode->i_nlink = 1;
  422. inode->i_ino = ino;
  423. inode->i_mode = S_IFREG;
  424. inode->i_private = mi;
  425. #ifdef INIT_UNUSED_INODE_FIELDS
  426. atomic_set(&inode->i_writecount, 0);
  427. inode->i_size = 0;
  428. inode->i_blocks = 0;
  429. inode->i_bytes = 0;
  430. inode->i_generation = 0;
  431. #ifdef CONFIG_QUOTA
  432. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  433. #endif
  434. inode->i_pipe = NULL;
  435. inode->i_bdev = NULL;
  436. inode->i_cdev = NULL;
  437. inode->i_rdev = 0;
  438. #ifdef CONFIG_SECURITY
  439. inode->i_security = NULL;
  440. #endif
  441. inode->dirtied_when = 0;
  442. INIT_LIST_HEAD(&inode->i_list);
  443. INIT_LIST_HEAD(&inode->i_sb_list);
  444. inode->i_state = 0;
  445. #endif
  446. spin_lock_init(&inode->i_lock);
  447. mutex_init(&inode->i_mutex);
  448. init_rwsem(&inode->i_alloc_sem);
  449. mapping->host = NULL; /* instead of inode */
  450. mapping->flags = 0;
  451. mapping_set_gfp_mask(mapping, gfp_mask);
  452. mapping->assoc_mapping = NULL;
  453. mapping->backing_dev_info = nilfs->ns_bdi;
  454. inode->i_mapping = mapping;
  455. }
  456. return inode;
  457. }
  458. struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb,
  459. ino_t ino, size_t objsz)
  460. {
  461. struct inode *inode;
  462. inode = nilfs_mdt_new_common(nilfs, sb, ino, NILFS_MDT_GFP, objsz);
  463. if (!inode)
  464. return NULL;
  465. inode->i_op = &def_mdt_iops;
  466. inode->i_fop = &def_mdt_fops;
  467. inode->i_mapping->a_ops = &def_mdt_aops;
  468. return inode;
  469. }
  470. void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size,
  471. unsigned header_size)
  472. {
  473. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  474. mi->mi_entry_size = entry_size;
  475. mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size;
  476. mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size);
  477. }
  478. void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow)
  479. {
  480. shadow->i_mapping->assoc_mapping = orig->i_mapping;
  481. NILFS_I(shadow)->i_btnode_cache.assoc_mapping =
  482. &NILFS_I(orig)->i_btnode_cache;
  483. }
  484. static void nilfs_mdt_clear(struct inode *inode)
  485. {
  486. struct nilfs_inode_info *ii = NILFS_I(inode);
  487. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  488. truncate_inode_pages(inode->i_mapping, 0);
  489. if (test_bit(NILFS_I_BMAP, &ii->i_state))
  490. nilfs_bmap_clear(ii->i_bmap);
  491. nilfs_btnode_cache_clear(&ii->i_btnode_cache);
  492. }
  493. void nilfs_mdt_destroy(struct inode *inode)
  494. {
  495. struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
  496. if (mdi->mi_palloc_cache)
  497. nilfs_palloc_destroy_cache(inode);
  498. nilfs_mdt_clear(inode);
  499. kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
  500. kfree(mdi);
  501. nilfs_destroy_inode(inode);
  502. }