the_nilfs.c 21 KB

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