the_nilfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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/random.h>
  28. #include <linux/crc32.h>
  29. #include "nilfs.h"
  30. #include "segment.h"
  31. #include "alloc.h"
  32. #include "cpfile.h"
  33. #include "sufile.h"
  34. #include "dat.h"
  35. #include "segbuf.h"
  36. static int nilfs_valid_sb(struct nilfs_super_block *sbp);
  37. void nilfs_set_last_segment(struct the_nilfs *nilfs,
  38. sector_t start_blocknr, u64 seq, __u64 cno)
  39. {
  40. spin_lock(&nilfs->ns_last_segment_lock);
  41. nilfs->ns_last_pseg = start_blocknr;
  42. nilfs->ns_last_seq = seq;
  43. nilfs->ns_last_cno = cno;
  44. if (!nilfs_sb_dirty(nilfs)) {
  45. if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
  46. goto stay_cursor;
  47. set_nilfs_sb_dirty(nilfs);
  48. }
  49. nilfs->ns_prev_seq = nilfs->ns_last_seq;
  50. stay_cursor:
  51. spin_unlock(&nilfs->ns_last_segment_lock);
  52. }
  53. /**
  54. * alloc_nilfs - allocate a nilfs object
  55. * @bdev: block device to which the_nilfs is related
  56. *
  57. * Return Value: On success, pointer to the_nilfs is returned.
  58. * On error, NULL is returned.
  59. */
  60. struct the_nilfs *alloc_nilfs(struct block_device *bdev)
  61. {
  62. struct the_nilfs *nilfs;
  63. nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
  64. if (!nilfs)
  65. return NULL;
  66. nilfs->ns_bdev = bdev;
  67. atomic_set(&nilfs->ns_ndirtyblks, 0);
  68. init_rwsem(&nilfs->ns_sem);
  69. mutex_init(&nilfs->ns_snapshot_mount_mutex);
  70. INIT_LIST_HEAD(&nilfs->ns_dirty_files);
  71. INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
  72. spin_lock_init(&nilfs->ns_inode_lock);
  73. spin_lock_init(&nilfs->ns_next_gen_lock);
  74. spin_lock_init(&nilfs->ns_last_segment_lock);
  75. nilfs->ns_cptree = RB_ROOT;
  76. spin_lock_init(&nilfs->ns_cptree_lock);
  77. init_rwsem(&nilfs->ns_segctor_sem);
  78. nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
  79. return nilfs;
  80. }
  81. /**
  82. * destroy_nilfs - destroy nilfs object
  83. * @nilfs: nilfs object to be released
  84. */
  85. void destroy_nilfs(struct the_nilfs *nilfs)
  86. {
  87. might_sleep();
  88. if (nilfs_init(nilfs)) {
  89. nilfs_sysfs_delete_device_group(nilfs);
  90. brelse(nilfs->ns_sbh[0]);
  91. brelse(nilfs->ns_sbh[1]);
  92. }
  93. kfree(nilfs);
  94. }
  95. static int nilfs_load_super_root(struct the_nilfs *nilfs,
  96. struct super_block *sb, sector_t sr_block)
  97. {
  98. struct buffer_head *bh_sr;
  99. struct nilfs_super_root *raw_sr;
  100. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  101. struct nilfs_inode *rawi;
  102. unsigned dat_entry_size, segment_usage_size, checkpoint_size;
  103. unsigned inode_size;
  104. int err;
  105. err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
  106. if (unlikely(err))
  107. return err;
  108. down_read(&nilfs->ns_sem);
  109. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  110. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  111. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  112. up_read(&nilfs->ns_sem);
  113. inode_size = nilfs->ns_inode_size;
  114. rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
  115. err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
  116. if (err)
  117. goto failed;
  118. rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
  119. err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
  120. if (err)
  121. goto failed_dat;
  122. rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
  123. err = nilfs_sufile_read(sb, segment_usage_size, rawi,
  124. &nilfs->ns_sufile);
  125. if (err)
  126. goto failed_cpfile;
  127. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  128. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  129. failed:
  130. brelse(bh_sr);
  131. return err;
  132. failed_cpfile:
  133. iput(nilfs->ns_cpfile);
  134. failed_dat:
  135. iput(nilfs->ns_dat);
  136. goto failed;
  137. }
  138. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  139. {
  140. memset(ri, 0, sizeof(*ri));
  141. INIT_LIST_HEAD(&ri->ri_used_segments);
  142. }
  143. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  144. {
  145. nilfs_dispose_segment_list(&ri->ri_used_segments);
  146. }
  147. /**
  148. * nilfs_store_log_cursor - load log cursor from a super block
  149. * @nilfs: nilfs object
  150. * @sbp: buffer storing super block to be read
  151. *
  152. * nilfs_store_log_cursor() reads the last position of the log
  153. * containing a super root from a given super block, and initializes
  154. * relevant information on the nilfs object preparatory for log
  155. * scanning and recovery.
  156. */
  157. static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
  158. struct nilfs_super_block *sbp)
  159. {
  160. int ret = 0;
  161. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  162. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  163. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  164. nilfs->ns_prev_seq = nilfs->ns_last_seq;
  165. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  166. nilfs->ns_segnum =
  167. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  168. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  169. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  170. printk(KERN_ERR "NILFS invalid last segment number.\n");
  171. ret = -EINVAL;
  172. }
  173. return ret;
  174. }
  175. /**
  176. * load_nilfs - load and recover the nilfs
  177. * @nilfs: the_nilfs structure to be released
  178. * @sb: super block isntance used to recover past segment
  179. *
  180. * load_nilfs() searches and load the latest super root,
  181. * attaches the last segment, and does recovery if needed.
  182. * The caller must call this exclusively for simultaneous mounts.
  183. */
  184. int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
  185. {
  186. struct nilfs_recovery_info ri;
  187. unsigned int s_flags = sb->s_flags;
  188. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  189. int valid_fs = nilfs_valid_fs(nilfs);
  190. int err;
  191. if (!valid_fs) {
  192. printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
  193. if (s_flags & MS_RDONLY) {
  194. printk(KERN_INFO "NILFS: INFO: recovery "
  195. "required for readonly filesystem.\n");
  196. printk(KERN_INFO "NILFS: write access will "
  197. "be enabled during recovery.\n");
  198. }
  199. }
  200. nilfs_init_recovery_info(&ri);
  201. err = nilfs_search_super_root(nilfs, &ri);
  202. if (unlikely(err)) {
  203. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  204. int blocksize;
  205. if (err != -EINVAL)
  206. goto scan_error;
  207. if (!nilfs_valid_sb(sbp[1])) {
  208. printk(KERN_WARNING
  209. "NILFS warning: unable to fall back to spare"
  210. "super block\n");
  211. goto scan_error;
  212. }
  213. printk(KERN_INFO
  214. "NILFS: try rollback from an earlier position\n");
  215. /*
  216. * restore super block with its spare and reconfigure
  217. * relevant states of the nilfs object.
  218. */
  219. memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
  220. nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
  221. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  222. /* verify consistency between two super blocks */
  223. blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
  224. if (blocksize != nilfs->ns_blocksize) {
  225. printk(KERN_WARNING
  226. "NILFS warning: blocksize differs between "
  227. "two super blocks (%d != %d)\n",
  228. blocksize, nilfs->ns_blocksize);
  229. goto scan_error;
  230. }
  231. err = nilfs_store_log_cursor(nilfs, sbp[0]);
  232. if (err)
  233. goto scan_error;
  234. /* drop clean flag to allow roll-forward and recovery */
  235. nilfs->ns_mount_state &= ~NILFS_VALID_FS;
  236. valid_fs = 0;
  237. err = nilfs_search_super_root(nilfs, &ri);
  238. if (err)
  239. goto scan_error;
  240. }
  241. err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
  242. if (unlikely(err)) {
  243. printk(KERN_ERR "NILFS: error loading super root.\n");
  244. goto failed;
  245. }
  246. if (valid_fs)
  247. goto skip_recovery;
  248. if (s_flags & MS_RDONLY) {
  249. __u64 features;
  250. if (nilfs_test_opt(nilfs, NORECOVERY)) {
  251. printk(KERN_INFO "NILFS: norecovery option specified. "
  252. "skipping roll-forward recovery\n");
  253. goto skip_recovery;
  254. }
  255. features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
  256. ~NILFS_FEATURE_COMPAT_RO_SUPP;
  257. if (features) {
  258. printk(KERN_ERR "NILFS: couldn't proceed with "
  259. "recovery because of unsupported optional "
  260. "features (%llx)\n",
  261. (unsigned long long)features);
  262. err = -EROFS;
  263. goto failed_unload;
  264. }
  265. if (really_read_only) {
  266. printk(KERN_ERR "NILFS: write access "
  267. "unavailable, cannot proceed.\n");
  268. err = -EROFS;
  269. goto failed_unload;
  270. }
  271. sb->s_flags &= ~MS_RDONLY;
  272. } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
  273. printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
  274. "option was specified for a read/write mount\n");
  275. err = -EINVAL;
  276. goto failed_unload;
  277. }
  278. err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
  279. if (err)
  280. goto failed_unload;
  281. down_write(&nilfs->ns_sem);
  282. nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
  283. err = nilfs_cleanup_super(sb);
  284. up_write(&nilfs->ns_sem);
  285. if (err) {
  286. printk(KERN_ERR "NILFS: failed to update super block. "
  287. "recovery unfinished.\n");
  288. goto failed_unload;
  289. }
  290. printk(KERN_INFO "NILFS: recovery complete.\n");
  291. skip_recovery:
  292. nilfs_clear_recovery_info(&ri);
  293. sb->s_flags = s_flags;
  294. return 0;
  295. scan_error:
  296. printk(KERN_ERR "NILFS: error searching super root.\n");
  297. goto failed;
  298. failed_unload:
  299. iput(nilfs->ns_cpfile);
  300. iput(nilfs->ns_sufile);
  301. iput(nilfs->ns_dat);
  302. failed:
  303. nilfs_clear_recovery_info(&ri);
  304. sb->s_flags = s_flags;
  305. return err;
  306. }
  307. static unsigned long long nilfs_max_size(unsigned int blkbits)
  308. {
  309. unsigned int max_bits;
  310. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  311. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  312. if (max_bits < 64)
  313. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  314. return res;
  315. }
  316. /**
  317. * nilfs_nrsvsegs - calculate the number of reserved segments
  318. * @nilfs: nilfs object
  319. * @nsegs: total number of segments
  320. */
  321. unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
  322. {
  323. return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  324. DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
  325. 100));
  326. }
  327. void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
  328. {
  329. nilfs->ns_nsegments = nsegs;
  330. nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
  331. }
  332. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  333. struct nilfs_super_block *sbp)
  334. {
  335. if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
  336. printk(KERN_ERR "NILFS: unsupported revision "
  337. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  338. "Please check the version of mkfs.nilfs.\n",
  339. le32_to_cpu(sbp->s_rev_level),
  340. le16_to_cpu(sbp->s_minor_rev_level),
  341. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  342. return -EINVAL;
  343. }
  344. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  345. if (nilfs->ns_sbsize > BLOCK_SIZE)
  346. return -EINVAL;
  347. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  348. if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
  349. printk(KERN_ERR "NILFS: too large inode size: %d bytes.\n",
  350. nilfs->ns_inode_size);
  351. return -EINVAL;
  352. } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
  353. printk(KERN_ERR "NILFS: too small inode size: %d bytes.\n",
  354. nilfs->ns_inode_size);
  355. return -EINVAL;
  356. }
  357. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  358. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  359. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  360. printk(KERN_ERR "NILFS: too short segment.\n");
  361. return -EINVAL;
  362. }
  363. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  364. nilfs->ns_r_segments_percentage =
  365. le32_to_cpu(sbp->s_r_segments_percentage);
  366. if (nilfs->ns_r_segments_percentage < 1 ||
  367. nilfs->ns_r_segments_percentage > 99) {
  368. printk(KERN_ERR "NILFS: invalid reserved segments percentage.\n");
  369. return -EINVAL;
  370. }
  371. nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
  372. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  373. return 0;
  374. }
  375. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  376. {
  377. static unsigned char sum[4];
  378. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  379. size_t bytes;
  380. u32 crc;
  381. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  382. return 0;
  383. bytes = le16_to_cpu(sbp->s_bytes);
  384. if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
  385. return 0;
  386. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  387. sumoff);
  388. crc = crc32_le(crc, sum, 4);
  389. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  390. bytes - sumoff - 4);
  391. return crc == le32_to_cpu(sbp->s_sum);
  392. }
  393. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  394. {
  395. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  396. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  397. (le32_to_cpu(sbp->s_log_block_size) + 10));
  398. }
  399. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  400. {
  401. int i;
  402. for (i = 0; i < 2; i++) {
  403. if (nilfs->ns_sbp[i]) {
  404. brelse(nilfs->ns_sbh[i]);
  405. nilfs->ns_sbh[i] = NULL;
  406. nilfs->ns_sbp[i] = NULL;
  407. }
  408. }
  409. }
  410. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  411. {
  412. brelse(nilfs->ns_sbh[0]);
  413. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  414. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  415. nilfs->ns_sbh[1] = NULL;
  416. nilfs->ns_sbp[1] = NULL;
  417. }
  418. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  419. {
  420. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  421. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  422. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  423. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  424. nilfs->ns_sbh[1] = tsbh;
  425. nilfs->ns_sbp[1] = tsbp;
  426. }
  427. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  428. struct super_block *sb, int blocksize,
  429. struct nilfs_super_block **sbpp)
  430. {
  431. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  432. struct buffer_head **sbh = nilfs->ns_sbh;
  433. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  434. int valid[2], swp = 0;
  435. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  436. &sbh[0]);
  437. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  438. if (!sbp[0]) {
  439. if (!sbp[1]) {
  440. printk(KERN_ERR "NILFS: unable to read superblock\n");
  441. return -EIO;
  442. }
  443. printk(KERN_WARNING
  444. "NILFS warning: unable to read primary superblock "
  445. "(blocksize = %d)\n", blocksize);
  446. } else if (!sbp[1]) {
  447. printk(KERN_WARNING
  448. "NILFS warning: unable to read secondary superblock "
  449. "(blocksize = %d)\n", blocksize);
  450. }
  451. /*
  452. * Compare two super blocks and set 1 in swp if the secondary
  453. * super block is valid and newer. Otherwise, set 0 in swp.
  454. */
  455. valid[0] = nilfs_valid_sb(sbp[0]);
  456. valid[1] = nilfs_valid_sb(sbp[1]);
  457. swp = valid[1] && (!valid[0] ||
  458. le64_to_cpu(sbp[1]->s_last_cno) >
  459. le64_to_cpu(sbp[0]->s_last_cno));
  460. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  461. brelse(sbh[1]);
  462. sbh[1] = NULL;
  463. sbp[1] = NULL;
  464. valid[1] = 0;
  465. swp = 0;
  466. }
  467. if (!valid[swp]) {
  468. nilfs_release_super_block(nilfs);
  469. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  470. sb->s_id);
  471. return -EINVAL;
  472. }
  473. if (!valid[!swp])
  474. printk(KERN_WARNING "NILFS warning: broken superblock. "
  475. "using spare superblock (blocksize = %d).\n", blocksize);
  476. if (swp)
  477. nilfs_swap_super_block(nilfs);
  478. nilfs->ns_sbwcount = 0;
  479. nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
  480. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  481. *sbpp = sbp[0];
  482. return 0;
  483. }
  484. /**
  485. * init_nilfs - initialize a NILFS instance.
  486. * @nilfs: the_nilfs structure
  487. * @sb: super block
  488. * @data: mount options
  489. *
  490. * init_nilfs() performs common initialization per block device (e.g.
  491. * reading the super block, getting disk layout information, initializing
  492. * shared fields in the_nilfs).
  493. *
  494. * Return Value: On success, 0 is returned. On error, a negative error
  495. * code is returned.
  496. */
  497. int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
  498. {
  499. struct nilfs_super_block *sbp;
  500. int blocksize;
  501. int err;
  502. down_write(&nilfs->ns_sem);
  503. blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
  504. if (!blocksize) {
  505. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  506. err = -EINVAL;
  507. goto out;
  508. }
  509. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  510. if (err)
  511. goto out;
  512. err = nilfs_store_magic_and_option(sb, sbp, data);
  513. if (err)
  514. goto failed_sbh;
  515. err = nilfs_check_feature_compatibility(sb, sbp);
  516. if (err)
  517. goto failed_sbh;
  518. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  519. if (blocksize < NILFS_MIN_BLOCK_SIZE ||
  520. blocksize > NILFS_MAX_BLOCK_SIZE) {
  521. printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
  522. "filesystem blocksize %d\n", blocksize);
  523. err = -EINVAL;
  524. goto failed_sbh;
  525. }
  526. if (sb->s_blocksize != blocksize) {
  527. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  528. if (blocksize < hw_blocksize) {
  529. printk(KERN_ERR
  530. "NILFS: blocksize %d too small for device "
  531. "(sector-size = %d).\n",
  532. blocksize, hw_blocksize);
  533. err = -EINVAL;
  534. goto failed_sbh;
  535. }
  536. nilfs_release_super_block(nilfs);
  537. sb_set_blocksize(sb, blocksize);
  538. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  539. if (err)
  540. goto out;
  541. /* not failed_sbh; sbh is released automatically
  542. when reloading fails. */
  543. }
  544. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  545. nilfs->ns_blocksize = blocksize;
  546. get_random_bytes(&nilfs->ns_next_generation,
  547. sizeof(nilfs->ns_next_generation));
  548. err = nilfs_store_disk_layout(nilfs, sbp);
  549. if (err)
  550. goto failed_sbh;
  551. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  552. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  553. err = nilfs_store_log_cursor(nilfs, sbp);
  554. if (err)
  555. goto failed_sbh;
  556. err = nilfs_sysfs_create_device_group(sb);
  557. if (err)
  558. goto failed_sbh;
  559. set_nilfs_init(nilfs);
  560. err = 0;
  561. out:
  562. up_write(&nilfs->ns_sem);
  563. return err;
  564. failed_sbh:
  565. nilfs_release_super_block(nilfs);
  566. goto out;
  567. }
  568. int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
  569. size_t nsegs)
  570. {
  571. sector_t seg_start, seg_end;
  572. sector_t start = 0, nblocks = 0;
  573. unsigned int sects_per_block;
  574. __u64 *sn;
  575. int ret = 0;
  576. sects_per_block = (1 << nilfs->ns_blocksize_bits) /
  577. bdev_logical_block_size(nilfs->ns_bdev);
  578. for (sn = segnump; sn < segnump + nsegs; sn++) {
  579. nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
  580. if (!nblocks) {
  581. start = seg_start;
  582. nblocks = seg_end - seg_start + 1;
  583. } else if (start + nblocks == seg_start) {
  584. nblocks += seg_end - seg_start + 1;
  585. } else {
  586. ret = blkdev_issue_discard(nilfs->ns_bdev,
  587. start * sects_per_block,
  588. nblocks * sects_per_block,
  589. GFP_NOFS, 0);
  590. if (ret < 0)
  591. return ret;
  592. nblocks = 0;
  593. }
  594. }
  595. if (nblocks)
  596. ret = blkdev_issue_discard(nilfs->ns_bdev,
  597. start * sects_per_block,
  598. nblocks * sects_per_block,
  599. GFP_NOFS, 0);
  600. return ret;
  601. }
  602. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  603. {
  604. unsigned long ncleansegs;
  605. down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  606. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  607. up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
  608. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  609. return 0;
  610. }
  611. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  612. {
  613. unsigned long ncleansegs, nincsegs;
  614. ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
  615. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  616. nilfs->ns_blocks_per_segment + 1;
  617. return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
  618. }
  619. struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
  620. {
  621. struct rb_node *n;
  622. struct nilfs_root *root;
  623. spin_lock(&nilfs->ns_cptree_lock);
  624. n = nilfs->ns_cptree.rb_node;
  625. while (n) {
  626. root = rb_entry(n, struct nilfs_root, rb_node);
  627. if (cno < root->cno) {
  628. n = n->rb_left;
  629. } else if (cno > root->cno) {
  630. n = n->rb_right;
  631. } else {
  632. atomic_inc(&root->count);
  633. spin_unlock(&nilfs->ns_cptree_lock);
  634. return root;
  635. }
  636. }
  637. spin_unlock(&nilfs->ns_cptree_lock);
  638. return NULL;
  639. }
  640. struct nilfs_root *
  641. nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
  642. {
  643. struct rb_node **p, *parent;
  644. struct nilfs_root *root, *new;
  645. int err;
  646. root = nilfs_lookup_root(nilfs, cno);
  647. if (root)
  648. return root;
  649. new = kzalloc(sizeof(*root), GFP_KERNEL);
  650. if (!new)
  651. return NULL;
  652. spin_lock(&nilfs->ns_cptree_lock);
  653. p = &nilfs->ns_cptree.rb_node;
  654. parent = NULL;
  655. while (*p) {
  656. parent = *p;
  657. root = rb_entry(parent, struct nilfs_root, rb_node);
  658. if (cno < root->cno) {
  659. p = &(*p)->rb_left;
  660. } else if (cno > root->cno) {
  661. p = &(*p)->rb_right;
  662. } else {
  663. atomic_inc(&root->count);
  664. spin_unlock(&nilfs->ns_cptree_lock);
  665. kfree(new);
  666. return root;
  667. }
  668. }
  669. new->cno = cno;
  670. new->ifile = NULL;
  671. new->nilfs = nilfs;
  672. atomic_set(&new->count, 1);
  673. atomic64_set(&new->inodes_count, 0);
  674. atomic64_set(&new->blocks_count, 0);
  675. rb_link_node(&new->rb_node, parent, p);
  676. rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
  677. spin_unlock(&nilfs->ns_cptree_lock);
  678. err = nilfs_sysfs_create_snapshot_group(new);
  679. if (err) {
  680. kfree(new);
  681. new = NULL;
  682. }
  683. return new;
  684. }
  685. void nilfs_put_root(struct nilfs_root *root)
  686. {
  687. if (atomic_dec_and_test(&root->count)) {
  688. struct the_nilfs *nilfs = root->nilfs;
  689. nilfs_sysfs_delete_snapshot_group(root);
  690. spin_lock(&nilfs->ns_cptree_lock);
  691. rb_erase(&root->rb_node, &nilfs->ns_cptree);
  692. spin_unlock(&nilfs->ns_cptree_lock);
  693. iput(root->ifile);
  694. kfree(root);
  695. }
  696. }