the_nilfs.c 23 KB

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