the_nilfs.c 21 KB

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