the_nilfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * the_nilfs.c - the_nilfs shared structure.
  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. */
  23. #include <linux/buffer_head.h>
  24. #include <linux/slab.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/crc32.h>
  28. #include "nilfs.h"
  29. #include "segment.h"
  30. #include "alloc.h"
  31. #include "cpfile.h"
  32. #include "sufile.h"
  33. #include "dat.h"
  34. #include "seglist.h"
  35. #include "segbuf.h"
  36. static LIST_HEAD(nilfs_objects);
  37. static DEFINE_SPINLOCK(nilfs_lock);
  38. void nilfs_set_last_segment(struct the_nilfs *nilfs,
  39. sector_t start_blocknr, u64 seq, __u64 cno)
  40. {
  41. spin_lock(&nilfs->ns_last_segment_lock);
  42. nilfs->ns_last_pseg = start_blocknr;
  43. nilfs->ns_last_seq = seq;
  44. nilfs->ns_last_cno = cno;
  45. spin_unlock(&nilfs->ns_last_segment_lock);
  46. }
  47. /**
  48. * alloc_nilfs - allocate the_nilfs structure
  49. * @bdev: block device to which the_nilfs is related
  50. *
  51. * alloc_nilfs() allocates memory for the_nilfs and
  52. * initializes its reference count and locks.
  53. *
  54. * Return Value: On success, pointer to the_nilfs is returned.
  55. * On error, NULL is returned.
  56. */
  57. static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
  58. {
  59. struct the_nilfs *nilfs;
  60. nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
  61. if (!nilfs)
  62. return NULL;
  63. nilfs->ns_bdev = bdev;
  64. atomic_set(&nilfs->ns_count, 1);
  65. atomic_set(&nilfs->ns_writer_refcount, -1);
  66. atomic_set(&nilfs->ns_ndirtyblks, 0);
  67. init_rwsem(&nilfs->ns_sem);
  68. init_rwsem(&nilfs->ns_super_sem);
  69. mutex_init(&nilfs->ns_mount_mutex);
  70. mutex_init(&nilfs->ns_writer_mutex);
  71. INIT_LIST_HEAD(&nilfs->ns_list);
  72. INIT_LIST_HEAD(&nilfs->ns_supers);
  73. spin_lock_init(&nilfs->ns_last_segment_lock);
  74. nilfs->ns_gc_inodes_h = NULL;
  75. init_rwsem(&nilfs->ns_segctor_sem);
  76. return nilfs;
  77. }
  78. /**
  79. * find_or_create_nilfs - find or create nilfs object
  80. * @bdev: block device to which the_nilfs is related
  81. *
  82. * find_nilfs() looks up an existent nilfs object created on the
  83. * device and gets the reference count of the object. If no nilfs object
  84. * is found on the device, a new nilfs object is allocated.
  85. *
  86. * Return Value: On success, pointer to the nilfs object is returned.
  87. * On error, NULL is returned.
  88. */
  89. struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
  90. {
  91. struct the_nilfs *nilfs, *new = NULL;
  92. retry:
  93. spin_lock(&nilfs_lock);
  94. list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
  95. if (nilfs->ns_bdev == bdev) {
  96. get_nilfs(nilfs);
  97. spin_unlock(&nilfs_lock);
  98. if (new)
  99. put_nilfs(new);
  100. return nilfs; /* existing object */
  101. }
  102. }
  103. if (new) {
  104. list_add_tail(&new->ns_list, &nilfs_objects);
  105. spin_unlock(&nilfs_lock);
  106. return new; /* new object */
  107. }
  108. spin_unlock(&nilfs_lock);
  109. new = alloc_nilfs(bdev);
  110. if (new)
  111. goto retry;
  112. return NULL; /* insufficient memory */
  113. }
  114. /**
  115. * put_nilfs - release a reference to the_nilfs
  116. * @nilfs: the_nilfs structure to be released
  117. *
  118. * put_nilfs() decrements a reference counter of the_nilfs.
  119. * If the reference count reaches zero, the_nilfs is freed.
  120. */
  121. void put_nilfs(struct the_nilfs *nilfs)
  122. {
  123. spin_lock(&nilfs_lock);
  124. if (!atomic_dec_and_test(&nilfs->ns_count)) {
  125. spin_unlock(&nilfs_lock);
  126. return;
  127. }
  128. list_del_init(&nilfs->ns_list);
  129. spin_unlock(&nilfs_lock);
  130. /*
  131. * Increment of ns_count never occurs below because the caller
  132. * of get_nilfs() holds at least one reference to the_nilfs.
  133. * Thus its exclusion control is not required here.
  134. */
  135. might_sleep();
  136. if (nilfs_loaded(nilfs)) {
  137. nilfs_mdt_clear(nilfs->ns_sufile);
  138. nilfs_mdt_destroy(nilfs->ns_sufile);
  139. nilfs_mdt_clear(nilfs->ns_cpfile);
  140. nilfs_mdt_destroy(nilfs->ns_cpfile);
  141. nilfs_mdt_clear(nilfs->ns_dat);
  142. nilfs_mdt_destroy(nilfs->ns_dat);
  143. /* XXX: how and when to clear nilfs->ns_gc_dat? */
  144. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  145. }
  146. if (nilfs_init(nilfs)) {
  147. nilfs_destroy_gccache(nilfs);
  148. brelse(nilfs->ns_sbh[0]);
  149. brelse(nilfs->ns_sbh[1]);
  150. }
  151. kfree(nilfs);
  152. }
  153. static int nilfs_load_super_root(struct the_nilfs *nilfs,
  154. struct nilfs_sb_info *sbi, sector_t sr_block)
  155. {
  156. static struct lock_class_key dat_lock_key;
  157. struct buffer_head *bh_sr;
  158. struct nilfs_super_root *raw_sr;
  159. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  160. unsigned dat_entry_size, segment_usage_size, checkpoint_size;
  161. unsigned inode_size;
  162. int err;
  163. err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
  164. if (unlikely(err))
  165. return err;
  166. down_read(&nilfs->ns_sem);
  167. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  168. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  169. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  170. up_read(&nilfs->ns_sem);
  171. inode_size = nilfs->ns_inode_size;
  172. err = -ENOMEM;
  173. nilfs->ns_dat = nilfs_mdt_new(
  174. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  175. if (unlikely(!nilfs->ns_dat))
  176. goto failed;
  177. nilfs->ns_gc_dat = nilfs_mdt_new(
  178. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  179. if (unlikely(!nilfs->ns_gc_dat))
  180. goto failed_dat;
  181. nilfs->ns_cpfile = nilfs_mdt_new(
  182. nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
  183. if (unlikely(!nilfs->ns_cpfile))
  184. goto failed_gc_dat;
  185. nilfs->ns_sufile = nilfs_mdt_new(
  186. nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
  187. if (unlikely(!nilfs->ns_sufile))
  188. goto failed_cpfile;
  189. err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
  190. if (unlikely(err))
  191. goto failed_sufile;
  192. err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
  193. if (unlikely(err))
  194. goto failed_sufile;
  195. lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key);
  196. lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key);
  197. nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
  198. nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
  199. sizeof(struct nilfs_cpfile_header));
  200. nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
  201. sizeof(struct nilfs_sufile_header));
  202. err = nilfs_mdt_read_inode_direct(
  203. nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
  204. if (unlikely(err))
  205. goto failed_sufile;
  206. err = nilfs_mdt_read_inode_direct(
  207. nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
  208. if (unlikely(err))
  209. goto failed_sufile;
  210. err = nilfs_mdt_read_inode_direct(
  211. nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
  212. if (unlikely(err))
  213. goto failed_sufile;
  214. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  215. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  216. failed:
  217. brelse(bh_sr);
  218. return err;
  219. failed_sufile:
  220. nilfs_mdt_destroy(nilfs->ns_sufile);
  221. failed_cpfile:
  222. nilfs_mdt_destroy(nilfs->ns_cpfile);
  223. failed_gc_dat:
  224. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  225. failed_dat:
  226. nilfs_mdt_destroy(nilfs->ns_dat);
  227. goto failed;
  228. }
  229. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  230. {
  231. memset(ri, 0, sizeof(*ri));
  232. INIT_LIST_HEAD(&ri->ri_used_segments);
  233. }
  234. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  235. {
  236. nilfs_dispose_segment_list(&ri->ri_used_segments);
  237. }
  238. /**
  239. * load_nilfs - load and recover the nilfs
  240. * @nilfs: the_nilfs structure to be released
  241. * @sbi: nilfs_sb_info used to recover past segment
  242. *
  243. * load_nilfs() searches and load the latest super root,
  244. * attaches the last segment, and does recovery if needed.
  245. * The caller must call this exclusively for simultaneous mounts.
  246. */
  247. int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  248. {
  249. struct nilfs_recovery_info ri;
  250. unsigned int s_flags = sbi->s_super->s_flags;
  251. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  252. unsigned valid_fs;
  253. int err = 0;
  254. nilfs_init_recovery_info(&ri);
  255. down_write(&nilfs->ns_sem);
  256. valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
  257. up_write(&nilfs->ns_sem);
  258. if (!valid_fs && (s_flags & MS_RDONLY)) {
  259. printk(KERN_INFO "NILFS: INFO: recovery "
  260. "required for readonly filesystem.\n");
  261. if (really_read_only) {
  262. printk(KERN_ERR "NILFS: write access "
  263. "unavailable, cannot proceed.\n");
  264. err = -EROFS;
  265. goto failed;
  266. }
  267. printk(KERN_INFO "NILFS: write access will "
  268. "be enabled during recovery.\n");
  269. sbi->s_super->s_flags &= ~MS_RDONLY;
  270. }
  271. err = nilfs_search_super_root(nilfs, sbi, &ri);
  272. if (unlikely(err)) {
  273. printk(KERN_ERR "NILFS: error searching super root.\n");
  274. goto failed;
  275. }
  276. err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
  277. if (unlikely(err)) {
  278. printk(KERN_ERR "NILFS: error loading super root.\n");
  279. goto failed;
  280. }
  281. if (!valid_fs) {
  282. err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
  283. if (unlikely(err)) {
  284. nilfs_mdt_destroy(nilfs->ns_cpfile);
  285. nilfs_mdt_destroy(nilfs->ns_sufile);
  286. nilfs_mdt_destroy(nilfs->ns_dat);
  287. goto failed;
  288. }
  289. if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
  290. sbi->s_super->s_dirt = 1;
  291. }
  292. set_nilfs_loaded(nilfs);
  293. failed:
  294. nilfs_clear_recovery_info(&ri);
  295. sbi->s_super->s_flags = s_flags;
  296. return err;
  297. }
  298. static unsigned long long nilfs_max_size(unsigned int blkbits)
  299. {
  300. unsigned int max_bits;
  301. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  302. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  303. if (max_bits < 64)
  304. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  305. return res;
  306. }
  307. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  308. struct nilfs_super_block *sbp)
  309. {
  310. if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
  311. printk(KERN_ERR "NILFS: revision mismatch "
  312. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  313. "Please check the version of mkfs.nilfs.\n",
  314. le32_to_cpu(sbp->s_rev_level),
  315. le16_to_cpu(sbp->s_minor_rev_level),
  316. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  317. return -EINVAL;
  318. }
  319. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  320. if (nilfs->ns_sbsize > BLOCK_SIZE)
  321. return -EINVAL;
  322. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  323. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  324. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  325. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  326. printk(KERN_ERR "NILFS: too short segment. \n");
  327. return -EINVAL;
  328. }
  329. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  330. nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
  331. nilfs->ns_r_segments_percentage =
  332. le32_to_cpu(sbp->s_r_segments_percentage);
  333. nilfs->ns_nrsvsegs =
  334. max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  335. DIV_ROUND_UP(nilfs->ns_nsegments *
  336. nilfs->ns_r_segments_percentage, 100));
  337. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  338. return 0;
  339. }
  340. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  341. {
  342. static unsigned char sum[4];
  343. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  344. size_t bytes;
  345. u32 crc;
  346. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  347. return 0;
  348. bytes = le16_to_cpu(sbp->s_bytes);
  349. if (bytes > BLOCK_SIZE)
  350. return 0;
  351. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  352. sumoff);
  353. crc = crc32_le(crc, sum, 4);
  354. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  355. bytes - sumoff - 4);
  356. return crc == le32_to_cpu(sbp->s_sum);
  357. }
  358. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  359. {
  360. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  361. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  362. (le32_to_cpu(sbp->s_log_block_size) + 10));
  363. }
  364. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  365. {
  366. int i;
  367. for (i = 0; i < 2; i++) {
  368. if (nilfs->ns_sbp[i]) {
  369. brelse(nilfs->ns_sbh[i]);
  370. nilfs->ns_sbh[i] = NULL;
  371. nilfs->ns_sbp[i] = NULL;
  372. }
  373. }
  374. }
  375. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  376. {
  377. brelse(nilfs->ns_sbh[0]);
  378. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  379. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  380. nilfs->ns_sbh[1] = NULL;
  381. nilfs->ns_sbp[1] = NULL;
  382. }
  383. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  384. {
  385. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  386. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  387. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  388. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  389. nilfs->ns_sbh[1] = tsbh;
  390. nilfs->ns_sbp[1] = tsbp;
  391. }
  392. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  393. struct super_block *sb, int blocksize,
  394. struct nilfs_super_block **sbpp)
  395. {
  396. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  397. struct buffer_head **sbh = nilfs->ns_sbh;
  398. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  399. int valid[2], swp = 0;
  400. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  401. &sbh[0]);
  402. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  403. if (!sbp[0]) {
  404. if (!sbp[1]) {
  405. printk(KERN_ERR "NILFS: unable to read superblock\n");
  406. return -EIO;
  407. }
  408. printk(KERN_WARNING
  409. "NILFS warning: unable to read primary superblock\n");
  410. } else if (!sbp[1])
  411. printk(KERN_WARNING
  412. "NILFS warning: unable to read secondary superblock\n");
  413. valid[0] = nilfs_valid_sb(sbp[0]);
  414. valid[1] = nilfs_valid_sb(sbp[1]);
  415. swp = valid[1] &&
  416. (!valid[0] ||
  417. le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
  418. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  419. brelse(sbh[1]);
  420. sbh[1] = NULL;
  421. sbp[1] = NULL;
  422. swp = 0;
  423. }
  424. if (!valid[swp]) {
  425. nilfs_release_super_block(nilfs);
  426. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  427. sb->s_id);
  428. return -EINVAL;
  429. }
  430. if (swp) {
  431. printk(KERN_WARNING "NILFS warning: broken superblock. "
  432. "using spare superblock.\n");
  433. nilfs_swap_super_block(nilfs);
  434. }
  435. nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
  436. nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
  437. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  438. *sbpp = sbp[0];
  439. return 0;
  440. }
  441. /**
  442. * init_nilfs - initialize a NILFS instance.
  443. * @nilfs: the_nilfs structure
  444. * @sbi: nilfs_sb_info
  445. * @sb: super block
  446. * @data: mount options
  447. *
  448. * init_nilfs() performs common initialization per block device (e.g.
  449. * reading the super block, getting disk layout information, initializing
  450. * shared fields in the_nilfs). It takes on some portion of the jobs
  451. * typically done by a fill_super() routine. This division arises from
  452. * the nature that multiple NILFS instances may be simultaneously
  453. * mounted on a device.
  454. * For multiple mounts on the same device, only the first mount
  455. * invokes these tasks.
  456. *
  457. * Return Value: On success, 0 is returned. On error, a negative error
  458. * code is returned.
  459. */
  460. int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
  461. {
  462. struct super_block *sb = sbi->s_super;
  463. struct nilfs_super_block *sbp;
  464. struct backing_dev_info *bdi;
  465. int blocksize;
  466. int err;
  467. down_write(&nilfs->ns_sem);
  468. if (nilfs_init(nilfs)) {
  469. /* Load values from existing the_nilfs */
  470. sbp = nilfs->ns_sbp[0];
  471. err = nilfs_store_magic_and_option(sb, sbp, data);
  472. if (err)
  473. goto out;
  474. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  475. if (sb->s_blocksize != blocksize &&
  476. !sb_set_blocksize(sb, blocksize)) {
  477. printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
  478. blocksize);
  479. err = -EINVAL;
  480. }
  481. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  482. goto out;
  483. }
  484. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  485. if (!blocksize) {
  486. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  487. err = -EINVAL;
  488. goto out;
  489. }
  490. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  491. if (err)
  492. goto out;
  493. err = nilfs_store_magic_and_option(sb, sbp, data);
  494. if (err)
  495. goto failed_sbh;
  496. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  497. if (sb->s_blocksize != blocksize) {
  498. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  499. if (blocksize < hw_blocksize) {
  500. printk(KERN_ERR
  501. "NILFS: blocksize %d too small for device "
  502. "(sector-size = %d).\n",
  503. blocksize, hw_blocksize);
  504. err = -EINVAL;
  505. goto failed_sbh;
  506. }
  507. nilfs_release_super_block(nilfs);
  508. sb_set_blocksize(sb, blocksize);
  509. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  510. if (err)
  511. goto out;
  512. /* not failed_sbh; sbh is released automatically
  513. when reloading fails. */
  514. }
  515. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  516. err = nilfs_store_disk_layout(nilfs, sbp);
  517. if (err)
  518. goto failed_sbh;
  519. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  520. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  521. bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
  522. if (!bdi)
  523. bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
  524. nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
  525. /* Finding last segment */
  526. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  527. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  528. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  529. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  530. nilfs->ns_segnum =
  531. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  532. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  533. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  534. printk(KERN_ERR "NILFS invalid last segment number.\n");
  535. err = -EINVAL;
  536. goto failed_sbh;
  537. }
  538. /* Dummy values */
  539. nilfs->ns_free_segments_count =
  540. nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
  541. /* Initialize gcinode cache */
  542. err = nilfs_init_gccache(nilfs);
  543. if (err)
  544. goto failed_sbh;
  545. set_nilfs_init(nilfs);
  546. err = 0;
  547. out:
  548. up_write(&nilfs->ns_sem);
  549. return err;
  550. failed_sbh:
  551. nilfs_release_super_block(nilfs);
  552. goto out;
  553. }
  554. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  555. {
  556. struct inode *dat = nilfs_dat_inode(nilfs);
  557. unsigned long ncleansegs;
  558. int err;
  559. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  560. err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
  561. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  562. if (likely(!err))
  563. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  564. return err;
  565. }
  566. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  567. {
  568. struct inode *sufile = nilfs->ns_sufile;
  569. unsigned long ncleansegs, nincsegs;
  570. int ret;
  571. ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
  572. if (likely(!ret)) {
  573. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  574. nilfs->ns_blocks_per_segment + 1;
  575. if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
  576. ret++;
  577. }
  578. return ret;
  579. }
  580. /**
  581. * nilfs_find_sbinfo - find existing nilfs_sb_info structure
  582. * @nilfs: nilfs object
  583. * @rw_mount: mount type (non-zero value for read/write mount)
  584. * @cno: checkpoint number (zero for read-only mount)
  585. *
  586. * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
  587. * @rw_mount and @cno (in case of snapshots) matched. If no instance
  588. * was found, NULL is returned. Although the super block instance can
  589. * be unmounted after this function returns, the nilfs_sb_info struct
  590. * is kept on memory until nilfs_put_sbinfo() is called.
  591. */
  592. struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
  593. int rw_mount, __u64 cno)
  594. {
  595. struct nilfs_sb_info *sbi;
  596. down_read(&nilfs->ns_super_sem);
  597. /*
  598. * The SNAPSHOT flag and sb->s_flags are supposed to be
  599. * protected with nilfs->ns_super_sem.
  600. */
  601. sbi = nilfs->ns_current;
  602. if (rw_mount) {
  603. if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
  604. goto found; /* read/write mount */
  605. else
  606. goto out;
  607. } else if (cno == 0) {
  608. if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
  609. goto found; /* read-only mount */
  610. else
  611. goto out;
  612. }
  613. list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
  614. if (nilfs_test_opt(sbi, SNAPSHOT) &&
  615. sbi->s_snapshot_cno == cno)
  616. goto found; /* snapshot mount */
  617. }
  618. out:
  619. up_read(&nilfs->ns_super_sem);
  620. return NULL;
  621. found:
  622. atomic_inc(&sbi->s_count);
  623. up_read(&nilfs->ns_super_sem);
  624. return sbi;
  625. }
  626. int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
  627. int snapshot_mount)
  628. {
  629. struct nilfs_sb_info *sbi;
  630. int ret = 0;
  631. down_read(&nilfs->ns_super_sem);
  632. if (cno == 0 || cno > nilfs->ns_cno)
  633. goto out_unlock;
  634. list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
  635. if (sbi->s_snapshot_cno == cno &&
  636. (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
  637. /* exclude read-only mounts */
  638. ret++;
  639. break;
  640. }
  641. }
  642. /* for protecting recent checkpoints */
  643. if (cno >= nilfs_last_cno(nilfs))
  644. ret++;
  645. out_unlock:
  646. up_read(&nilfs->ns_super_sem);
  647. return ret;
  648. }