the_nilfs.c 24 KB

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