the_nilfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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_writer_refcount, -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. mutex_init(&nilfs->ns_writer_mutex);
  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_clear(nilfs->ns_sufile);
  137. nilfs_mdt_destroy(nilfs->ns_sufile);
  138. nilfs_mdt_clear(nilfs->ns_cpfile);
  139. nilfs_mdt_destroy(nilfs->ns_cpfile);
  140. nilfs_mdt_clear(nilfs->ns_dat);
  141. nilfs_mdt_destroy(nilfs->ns_dat);
  142. /* XXX: how and when to clear nilfs->ns_gc_dat? */
  143. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  144. }
  145. if (nilfs_init(nilfs)) {
  146. nilfs_destroy_gccache(nilfs);
  147. brelse(nilfs->ns_sbh[0]);
  148. brelse(nilfs->ns_sbh[1]);
  149. }
  150. kfree(nilfs);
  151. }
  152. static int nilfs_load_super_root(struct the_nilfs *nilfs,
  153. struct nilfs_sb_info *sbi, sector_t sr_block)
  154. {
  155. static struct lock_class_key dat_lock_key;
  156. struct buffer_head *bh_sr;
  157. struct nilfs_super_root *raw_sr;
  158. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  159. unsigned dat_entry_size, segment_usage_size, checkpoint_size;
  160. unsigned inode_size;
  161. int err;
  162. err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
  163. if (unlikely(err))
  164. return err;
  165. down_read(&nilfs->ns_sem);
  166. dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
  167. checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
  168. segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
  169. up_read(&nilfs->ns_sem);
  170. inode_size = nilfs->ns_inode_size;
  171. err = -ENOMEM;
  172. nilfs->ns_dat = nilfs_mdt_new(
  173. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  174. if (unlikely(!nilfs->ns_dat))
  175. goto failed;
  176. nilfs->ns_gc_dat = nilfs_mdt_new(
  177. nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
  178. if (unlikely(!nilfs->ns_gc_dat))
  179. goto failed_dat;
  180. nilfs->ns_cpfile = nilfs_mdt_new(
  181. nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
  182. if (unlikely(!nilfs->ns_cpfile))
  183. goto failed_gc_dat;
  184. nilfs->ns_sufile = nilfs_mdt_new(
  185. nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
  186. if (unlikely(!nilfs->ns_sufile))
  187. goto failed_cpfile;
  188. err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
  189. if (unlikely(err))
  190. goto failed_sufile;
  191. err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
  192. if (unlikely(err))
  193. goto failed_sufile;
  194. lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key);
  195. lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key);
  196. nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
  197. nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
  198. sizeof(struct nilfs_cpfile_header));
  199. nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
  200. sizeof(struct nilfs_sufile_header));
  201. err = nilfs_mdt_read_inode_direct(
  202. nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
  203. if (unlikely(err))
  204. goto failed_sufile;
  205. err = nilfs_mdt_read_inode_direct(
  206. nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
  207. if (unlikely(err))
  208. goto failed_sufile;
  209. err = nilfs_mdt_read_inode_direct(
  210. nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
  211. if (unlikely(err))
  212. goto failed_sufile;
  213. raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
  214. nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
  215. failed:
  216. brelse(bh_sr);
  217. return err;
  218. failed_sufile:
  219. nilfs_mdt_destroy(nilfs->ns_sufile);
  220. failed_cpfile:
  221. nilfs_mdt_destroy(nilfs->ns_cpfile);
  222. failed_gc_dat:
  223. nilfs_mdt_destroy(nilfs->ns_gc_dat);
  224. failed_dat:
  225. nilfs_mdt_destroy(nilfs->ns_dat);
  226. goto failed;
  227. }
  228. static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
  229. {
  230. memset(ri, 0, sizeof(*ri));
  231. INIT_LIST_HEAD(&ri->ri_used_segments);
  232. }
  233. static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
  234. {
  235. nilfs_dispose_segment_list(&ri->ri_used_segments);
  236. }
  237. /**
  238. * load_nilfs - load and recover the nilfs
  239. * @nilfs: the_nilfs structure to be released
  240. * @sbi: nilfs_sb_info used to recover past segment
  241. *
  242. * load_nilfs() searches and load the latest super root,
  243. * attaches the last segment, and does recovery if needed.
  244. * The caller must call this exclusively for simultaneous mounts.
  245. */
  246. int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  247. {
  248. struct nilfs_recovery_info ri;
  249. unsigned int s_flags = sbi->s_super->s_flags;
  250. int really_read_only = bdev_read_only(nilfs->ns_bdev);
  251. unsigned valid_fs;
  252. int err = 0;
  253. nilfs_init_recovery_info(&ri);
  254. down_write(&nilfs->ns_sem);
  255. valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
  256. up_write(&nilfs->ns_sem);
  257. if (!valid_fs && (s_flags & MS_RDONLY)) {
  258. printk(KERN_INFO "NILFS: INFO: recovery "
  259. "required for readonly filesystem.\n");
  260. if (really_read_only) {
  261. printk(KERN_ERR "NILFS: write access "
  262. "unavailable, cannot proceed.\n");
  263. err = -EROFS;
  264. goto failed;
  265. }
  266. printk(KERN_INFO "NILFS: write access will "
  267. "be enabled during recovery.\n");
  268. sbi->s_super->s_flags &= ~MS_RDONLY;
  269. }
  270. err = nilfs_search_super_root(nilfs, sbi, &ri);
  271. if (unlikely(err)) {
  272. printk(KERN_ERR "NILFS: error searching super root.\n");
  273. goto failed;
  274. }
  275. err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
  276. if (unlikely(err)) {
  277. printk(KERN_ERR "NILFS: error loading super root.\n");
  278. goto failed;
  279. }
  280. if (!valid_fs) {
  281. err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
  282. if (unlikely(err)) {
  283. nilfs_mdt_destroy(nilfs->ns_cpfile);
  284. nilfs_mdt_destroy(nilfs->ns_sufile);
  285. nilfs_mdt_destroy(nilfs->ns_dat);
  286. goto failed;
  287. }
  288. if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
  289. sbi->s_super->s_dirt = 1;
  290. }
  291. set_nilfs_loaded(nilfs);
  292. failed:
  293. nilfs_clear_recovery_info(&ri);
  294. sbi->s_super->s_flags = s_flags;
  295. return err;
  296. }
  297. static unsigned long long nilfs_max_size(unsigned int blkbits)
  298. {
  299. unsigned int max_bits;
  300. unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
  301. max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
  302. if (max_bits < 64)
  303. res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
  304. return res;
  305. }
  306. static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
  307. struct nilfs_super_block *sbp)
  308. {
  309. if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
  310. printk(KERN_ERR "NILFS: revision mismatch "
  311. "(superblock rev.=%d.%d, current rev.=%d.%d). "
  312. "Please check the version of mkfs.nilfs.\n",
  313. le32_to_cpu(sbp->s_rev_level),
  314. le16_to_cpu(sbp->s_minor_rev_level),
  315. NILFS_CURRENT_REV, NILFS_MINOR_REV);
  316. return -EINVAL;
  317. }
  318. nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
  319. if (nilfs->ns_sbsize > BLOCK_SIZE)
  320. return -EINVAL;
  321. nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
  322. nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
  323. nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
  324. if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
  325. printk(KERN_ERR "NILFS: too short segment. \n");
  326. return -EINVAL;
  327. }
  328. nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
  329. nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
  330. nilfs->ns_r_segments_percentage =
  331. le32_to_cpu(sbp->s_r_segments_percentage);
  332. nilfs->ns_nrsvsegs =
  333. max_t(unsigned long, NILFS_MIN_NRSVSEGS,
  334. DIV_ROUND_UP(nilfs->ns_nsegments *
  335. nilfs->ns_r_segments_percentage, 100));
  336. nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
  337. return 0;
  338. }
  339. static int nilfs_valid_sb(struct nilfs_super_block *sbp)
  340. {
  341. static unsigned char sum[4];
  342. const int sumoff = offsetof(struct nilfs_super_block, s_sum);
  343. size_t bytes;
  344. u32 crc;
  345. if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
  346. return 0;
  347. bytes = le16_to_cpu(sbp->s_bytes);
  348. if (bytes > BLOCK_SIZE)
  349. return 0;
  350. crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
  351. sumoff);
  352. crc = crc32_le(crc, sum, 4);
  353. crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
  354. bytes - sumoff - 4);
  355. return crc == le32_to_cpu(sbp->s_sum);
  356. }
  357. static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
  358. {
  359. return offset < ((le64_to_cpu(sbp->s_nsegments) *
  360. le32_to_cpu(sbp->s_blocks_per_segment)) <<
  361. (le32_to_cpu(sbp->s_log_block_size) + 10));
  362. }
  363. static void nilfs_release_super_block(struct the_nilfs *nilfs)
  364. {
  365. int i;
  366. for (i = 0; i < 2; i++) {
  367. if (nilfs->ns_sbp[i]) {
  368. brelse(nilfs->ns_sbh[i]);
  369. nilfs->ns_sbh[i] = NULL;
  370. nilfs->ns_sbp[i] = NULL;
  371. }
  372. }
  373. }
  374. void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
  375. {
  376. brelse(nilfs->ns_sbh[0]);
  377. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  378. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  379. nilfs->ns_sbh[1] = NULL;
  380. nilfs->ns_sbp[1] = NULL;
  381. }
  382. void nilfs_swap_super_block(struct the_nilfs *nilfs)
  383. {
  384. struct buffer_head *tsbh = nilfs->ns_sbh[0];
  385. struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
  386. nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
  387. nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
  388. nilfs->ns_sbh[1] = tsbh;
  389. nilfs->ns_sbp[1] = tsbp;
  390. }
  391. static int nilfs_load_super_block(struct the_nilfs *nilfs,
  392. struct super_block *sb, int blocksize,
  393. struct nilfs_super_block **sbpp)
  394. {
  395. struct nilfs_super_block **sbp = nilfs->ns_sbp;
  396. struct buffer_head **sbh = nilfs->ns_sbh;
  397. u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
  398. int valid[2], swp = 0;
  399. sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
  400. &sbh[0]);
  401. sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
  402. if (!sbp[0]) {
  403. if (!sbp[1]) {
  404. printk(KERN_ERR "NILFS: unable to read superblock\n");
  405. return -EIO;
  406. }
  407. printk(KERN_WARNING
  408. "NILFS warning: unable to read primary superblock\n");
  409. } else if (!sbp[1])
  410. printk(KERN_WARNING
  411. "NILFS warning: unable to read secondary superblock\n");
  412. valid[0] = nilfs_valid_sb(sbp[0]);
  413. valid[1] = nilfs_valid_sb(sbp[1]);
  414. swp = valid[1] &&
  415. (!valid[0] ||
  416. le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
  417. if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
  418. brelse(sbh[1]);
  419. sbh[1] = NULL;
  420. sbp[1] = NULL;
  421. swp = 0;
  422. }
  423. if (!valid[swp]) {
  424. nilfs_release_super_block(nilfs);
  425. printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
  426. sb->s_id);
  427. return -EINVAL;
  428. }
  429. if (swp) {
  430. printk(KERN_WARNING "NILFS warning: broken superblock. "
  431. "using spare superblock.\n");
  432. nilfs_swap_super_block(nilfs);
  433. }
  434. nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
  435. nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
  436. nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
  437. *sbpp = sbp[0];
  438. return 0;
  439. }
  440. /**
  441. * init_nilfs - initialize a NILFS instance.
  442. * @nilfs: the_nilfs structure
  443. * @sbi: nilfs_sb_info
  444. * @sb: super block
  445. * @data: mount options
  446. *
  447. * init_nilfs() performs common initialization per block device (e.g.
  448. * reading the super block, getting disk layout information, initializing
  449. * shared fields in the_nilfs). It takes on some portion of the jobs
  450. * typically done by a fill_super() routine. This division arises from
  451. * the nature that multiple NILFS instances may be simultaneously
  452. * mounted on a device.
  453. * For multiple mounts on the same device, only the first mount
  454. * invokes these tasks.
  455. *
  456. * Return Value: On success, 0 is returned. On error, a negative error
  457. * code is returned.
  458. */
  459. int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
  460. {
  461. struct super_block *sb = sbi->s_super;
  462. struct nilfs_super_block *sbp;
  463. struct backing_dev_info *bdi;
  464. int blocksize;
  465. int err;
  466. down_write(&nilfs->ns_sem);
  467. if (nilfs_init(nilfs)) {
  468. /* Load values from existing the_nilfs */
  469. sbp = nilfs->ns_sbp[0];
  470. err = nilfs_store_magic_and_option(sb, sbp, data);
  471. if (err)
  472. goto out;
  473. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  474. if (sb->s_blocksize != blocksize &&
  475. !sb_set_blocksize(sb, blocksize)) {
  476. printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
  477. blocksize);
  478. err = -EINVAL;
  479. }
  480. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  481. goto out;
  482. }
  483. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  484. if (!blocksize) {
  485. printk(KERN_ERR "NILFS: unable to set blocksize\n");
  486. err = -EINVAL;
  487. goto out;
  488. }
  489. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  490. if (err)
  491. goto out;
  492. err = nilfs_store_magic_and_option(sb, sbp, data);
  493. if (err)
  494. goto failed_sbh;
  495. blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
  496. if (sb->s_blocksize != blocksize) {
  497. int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
  498. if (blocksize < hw_blocksize) {
  499. printk(KERN_ERR
  500. "NILFS: blocksize %d too small for device "
  501. "(sector-size = %d).\n",
  502. blocksize, hw_blocksize);
  503. err = -EINVAL;
  504. goto failed_sbh;
  505. }
  506. nilfs_release_super_block(nilfs);
  507. sb_set_blocksize(sb, blocksize);
  508. err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
  509. if (err)
  510. goto out;
  511. /* not failed_sbh; sbh is released automatically
  512. when reloading fails. */
  513. }
  514. nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
  515. err = nilfs_store_disk_layout(nilfs, sbp);
  516. if (err)
  517. goto failed_sbh;
  518. sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
  519. nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
  520. bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
  521. if (!bdi)
  522. bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
  523. nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
  524. /* Finding last segment */
  525. nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
  526. nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
  527. nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
  528. nilfs->ns_seg_seq = nilfs->ns_last_seq;
  529. nilfs->ns_segnum =
  530. nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
  531. nilfs->ns_cno = nilfs->ns_last_cno + 1;
  532. if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
  533. printk(KERN_ERR "NILFS invalid last segment number.\n");
  534. err = -EINVAL;
  535. goto failed_sbh;
  536. }
  537. /* Dummy values */
  538. nilfs->ns_free_segments_count =
  539. nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
  540. /* Initialize gcinode cache */
  541. err = nilfs_init_gccache(nilfs);
  542. if (err)
  543. goto failed_sbh;
  544. set_nilfs_init(nilfs);
  545. err = 0;
  546. out:
  547. up_write(&nilfs->ns_sem);
  548. return err;
  549. failed_sbh:
  550. nilfs_release_super_block(nilfs);
  551. goto out;
  552. }
  553. int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
  554. {
  555. struct inode *dat = nilfs_dat_inode(nilfs);
  556. unsigned long ncleansegs;
  557. int err;
  558. down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  559. err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
  560. up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
  561. if (likely(!err))
  562. *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
  563. return err;
  564. }
  565. int nilfs_near_disk_full(struct the_nilfs *nilfs)
  566. {
  567. struct inode *sufile = nilfs->ns_sufile;
  568. unsigned long ncleansegs, nincsegs;
  569. int ret;
  570. ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
  571. if (likely(!ret)) {
  572. nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
  573. nilfs->ns_blocks_per_segment + 1;
  574. if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
  575. ret++;
  576. }
  577. return ret;
  578. }
  579. /**
  580. * nilfs_find_sbinfo - find existing nilfs_sb_info structure
  581. * @nilfs: nilfs object
  582. * @rw_mount: mount type (non-zero value for read/write mount)
  583. * @cno: checkpoint number (zero for read-only mount)
  584. *
  585. * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
  586. * @rw_mount and @cno (in case of snapshots) matched. If no instance
  587. * was found, NULL is returned. Although the super block instance can
  588. * be unmounted after this function returns, the nilfs_sb_info struct
  589. * is kept on memory until nilfs_put_sbinfo() is called.
  590. */
  591. struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
  592. int rw_mount, __u64 cno)
  593. {
  594. struct nilfs_sb_info *sbi;
  595. down_read(&nilfs->ns_super_sem);
  596. /*
  597. * The SNAPSHOT flag and sb->s_flags are supposed to be
  598. * protected with nilfs->ns_super_sem.
  599. */
  600. sbi = nilfs->ns_current;
  601. if (rw_mount) {
  602. if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
  603. goto found; /* read/write mount */
  604. else
  605. goto out;
  606. } else if (cno == 0) {
  607. if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
  608. goto found; /* read-only mount */
  609. else
  610. goto out;
  611. }
  612. list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
  613. if (nilfs_test_opt(sbi, SNAPSHOT) &&
  614. sbi->s_snapshot_cno == cno)
  615. goto found; /* snapshot mount */
  616. }
  617. out:
  618. up_read(&nilfs->ns_super_sem);
  619. return NULL;
  620. found:
  621. atomic_inc(&sbi->s_count);
  622. up_read(&nilfs->ns_super_sem);
  623. return sbi;
  624. }
  625. int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
  626. int snapshot_mount)
  627. {
  628. struct nilfs_sb_info *sbi;
  629. int ret = 0;
  630. down_read(&nilfs->ns_super_sem);
  631. if (cno == 0 || cno > nilfs->ns_cno)
  632. goto out_unlock;
  633. list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
  634. if (sbi->s_snapshot_cno == cno &&
  635. (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
  636. /* exclude read-only mounts */
  637. ret++;
  638. break;
  639. }
  640. }
  641. /* for protecting recent checkpoints */
  642. if (cno >= nilfs_last_cno(nilfs))
  643. ret++;
  644. out_unlock:
  645. up_read(&nilfs->ns_super_sem);
  646. return ret;
  647. }