mdt.c 14 KB

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