]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nilfs2/the_nilfs.c
nilfs2: simplify handling of active state of segments
[net-next-2.6.git] / fs / nilfs2 / the_nilfs.c
CommitLineData
8a9d2191
RK
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
24#include <linux/buffer_head.h>
25#include <linux/slab.h>
26#include <linux/blkdev.h>
27#include <linux/backing-dev.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 "seglist.h"
35#include "segbuf.h"
36
37void 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/**
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 */
57struct the_nilfs *alloc_nilfs(struct block_device *bdev)
58{
59 struct the_nilfs *nilfs;
60
61 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
62 if (!nilfs)
63 return NULL;
64
65 nilfs->ns_bdev = bdev;
66 atomic_set(&nilfs->ns_count, 1);
67 atomic_set(&nilfs->ns_writer_refcount, -1);
68 atomic_set(&nilfs->ns_ndirtyblks, 0);
69 init_rwsem(&nilfs->ns_sem);
70 mutex_init(&nilfs->ns_writer_mutex);
71 INIT_LIST_HEAD(&nilfs->ns_supers);
72 spin_lock_init(&nilfs->ns_last_segment_lock);
73 nilfs->ns_gc_inodes_h = NULL;
8a9d2191 74 init_rwsem(&nilfs->ns_segctor_sem);
8a9d2191
RK
75
76 return nilfs;
77}
78
79/**
80 * put_nilfs - release a reference to the_nilfs
81 * @nilfs: the_nilfs structure to be released
82 *
83 * put_nilfs() decrements a reference counter of the_nilfs.
84 * If the reference count reaches zero, the_nilfs is freed.
85 */
86void put_nilfs(struct the_nilfs *nilfs)
87{
88 if (!atomic_dec_and_test(&nilfs->ns_count))
89 return;
90 /*
91 * Increment of ns_count never occur below because the caller
92 * of get_nilfs() holds at least one reference to the_nilfs.
93 * Thus its exclusion control is not required here.
94 */
95 might_sleep();
96 if (nilfs_loaded(nilfs)) {
8a9d2191
RK
97 nilfs_mdt_clear(nilfs->ns_sufile);
98 nilfs_mdt_destroy(nilfs->ns_sufile);
99 nilfs_mdt_clear(nilfs->ns_cpfile);
100 nilfs_mdt_destroy(nilfs->ns_cpfile);
101 nilfs_mdt_clear(nilfs->ns_dat);
102 nilfs_mdt_destroy(nilfs->ns_dat);
103 /* XXX: how and when to clear nilfs->ns_gc_dat? */
104 nilfs_mdt_destroy(nilfs->ns_gc_dat);
105 }
106 if (nilfs_init(nilfs)) {
107 nilfs_destroy_gccache(nilfs);
108 brelse(nilfs->ns_sbh);
109 }
110 kfree(nilfs);
111}
112
113static int nilfs_load_super_root(struct the_nilfs *nilfs,
114 struct nilfs_sb_info *sbi, sector_t sr_block)
115{
116 struct buffer_head *bh_sr;
117 struct nilfs_super_root *raw_sr;
118 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
119 unsigned inode_size;
120 int err;
121
122 err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
123 if (unlikely(err))
124 return err;
125
126 down_read(&nilfs->ns_sem);
127 dat_entry_size = le16_to_cpu(nilfs->ns_sbp->s_dat_entry_size);
128 checkpoint_size = le16_to_cpu(nilfs->ns_sbp->s_checkpoint_size);
129 segment_usage_size = le16_to_cpu(nilfs->ns_sbp->s_segment_usage_size);
130 up_read(&nilfs->ns_sem);
131
132 inode_size = nilfs->ns_inode_size;
133
134 err = -ENOMEM;
135 nilfs->ns_dat = nilfs_mdt_new(
136 nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
137 if (unlikely(!nilfs->ns_dat))
138 goto failed;
139
140 nilfs->ns_gc_dat = nilfs_mdt_new(
141 nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
142 if (unlikely(!nilfs->ns_gc_dat))
143 goto failed_dat;
144
145 nilfs->ns_cpfile = nilfs_mdt_new(
146 nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
147 if (unlikely(!nilfs->ns_cpfile))
148 goto failed_gc_dat;
149
150 nilfs->ns_sufile = nilfs_mdt_new(
151 nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
152 if (unlikely(!nilfs->ns_sufile))
153 goto failed_cpfile;
154
155 err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
156 if (unlikely(err))
157 goto failed_sufile;
158
159 err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
160 if (unlikely(err))
161 goto failed_sufile;
162
163 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
164 nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
165 sizeof(struct nilfs_cpfile_header));
166 nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
167 sizeof(struct nilfs_sufile_header));
168
169 err = nilfs_mdt_read_inode_direct(
170 nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
171 if (unlikely(err))
172 goto failed_sufile;
173
174 err = nilfs_mdt_read_inode_direct(
175 nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
176 if (unlikely(err))
177 goto failed_sufile;
178
179 err = nilfs_mdt_read_inode_direct(
180 nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
181 if (unlikely(err))
182 goto failed_sufile;
183
184 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
185 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
186
187 failed:
188 brelse(bh_sr);
189 return err;
190
191 failed_sufile:
192 nilfs_mdt_destroy(nilfs->ns_sufile);
193
194 failed_cpfile:
195 nilfs_mdt_destroy(nilfs->ns_cpfile);
196
197 failed_gc_dat:
198 nilfs_mdt_destroy(nilfs->ns_gc_dat);
199
200 failed_dat:
201 nilfs_mdt_destroy(nilfs->ns_dat);
202 goto failed;
203}
204
205static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
206{
207 memset(ri, 0, sizeof(*ri));
208 INIT_LIST_HEAD(&ri->ri_used_segments);
209}
210
211static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
212{
213 nilfs_dispose_segment_list(&ri->ri_used_segments);
214}
215
216/**
217 * load_nilfs - load and recover the nilfs
218 * @nilfs: the_nilfs structure to be released
219 * @sbi: nilfs_sb_info used to recover past segment
220 *
221 * load_nilfs() searches and load the latest super root,
222 * attaches the last segment, and does recovery if needed.
223 * The caller must call this exclusively for simultaneous mounts.
224 */
225int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
226{
227 struct nilfs_recovery_info ri;
228 unsigned int s_flags = sbi->s_super->s_flags;
229 int really_read_only = bdev_read_only(nilfs->ns_bdev);
230 unsigned valid_fs;
231 int err = 0;
232
233 nilfs_init_recovery_info(&ri);
234
235 down_write(&nilfs->ns_sem);
236 valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
237 up_write(&nilfs->ns_sem);
238
239 if (!valid_fs && (s_flags & MS_RDONLY)) {
240 printk(KERN_INFO "NILFS: INFO: recovery "
241 "required for readonly filesystem.\n");
242 if (really_read_only) {
243 printk(KERN_ERR "NILFS: write access "
244 "unavailable, cannot proceed.\n");
245 err = -EROFS;
246 goto failed;
247 }
248 printk(KERN_INFO "NILFS: write access will "
249 "be enabled during recovery.\n");
250 sbi->s_super->s_flags &= ~MS_RDONLY;
251 }
252
253 err = nilfs_search_super_root(nilfs, sbi, &ri);
254 if (unlikely(err)) {
255 printk(KERN_ERR "NILFS: error searching super root.\n");
256 goto failed;
257 }
258
259 err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
260 if (unlikely(err)) {
261 printk(KERN_ERR "NILFS: error loading super root.\n");
262 goto failed;
263 }
264
265 if (!valid_fs) {
266 err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
267 if (unlikely(err)) {
268 nilfs_mdt_destroy(nilfs->ns_cpfile);
269 nilfs_mdt_destroy(nilfs->ns_sufile);
270 nilfs_mdt_destroy(nilfs->ns_dat);
271 goto failed;
272 }
273 if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED) {
274 down_write(&nilfs->ns_sem);
275 nilfs_update_last_segment(sbi, 0);
276 up_write(&nilfs->ns_sem);
277 }
278 }
279
280 set_nilfs_loaded(nilfs);
281
282 failed:
283 nilfs_clear_recovery_info(&ri);
284 sbi->s_super->s_flags = s_flags;
285 return err;
286}
287
288static unsigned long long nilfs_max_size(unsigned int blkbits)
289{
290 unsigned int max_bits;
291 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
292
293 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
294 if (max_bits < 64)
295 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
296 return res;
297}
298
299static int
300nilfs_store_disk_layout(struct the_nilfs *nilfs, struct super_block *sb,
301 struct nilfs_super_block *sbp)
302{
303 if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
304 printk(KERN_ERR "NILFS: revision mismatch "
305 "(superblock rev.=%d.%d, current rev.=%d.%d). "
306 "Please check the version of mkfs.nilfs.\n",
307 le32_to_cpu(sbp->s_rev_level),
308 le16_to_cpu(sbp->s_minor_rev_level),
309 NILFS_CURRENT_REV, NILFS_MINOR_REV);
310 return -EINVAL;
311 }
312 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
313 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
314
315 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
316 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
317 printk(KERN_ERR "NILFS: too short segment. \n");
318 return -EINVAL;
319 }
320
321 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
322 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
323 nilfs->ns_r_segments_percentage =
324 le32_to_cpu(sbp->s_r_segments_percentage);
325 nilfs->ns_nrsvsegs =
326 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
327 DIV_ROUND_UP(nilfs->ns_nsegments *
328 nilfs->ns_r_segments_percentage, 100));
329 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
330 return 0;
331}
332
333/**
334 * init_nilfs - initialize a NILFS instance.
335 * @nilfs: the_nilfs structure
336 * @sbi: nilfs_sb_info
337 * @sb: super block
338 * @data: mount options
339 *
340 * init_nilfs() performs common initialization per block device (e.g.
341 * reading the super block, getting disk layout information, initializing
342 * shared fields in the_nilfs). It takes on some portion of the jobs
343 * typically done by a fill_super() routine. This division arises from
344 * the nature that multiple NILFS instances may be simultaneously
345 * mounted on a device.
346 * For multiple mounts on the same device, only the first mount
347 * invokes these tasks.
348 *
349 * Return Value: On success, 0 is returned. On error, a negative error
350 * code is returned.
351 */
352int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
353{
354 struct super_block *sb = sbi->s_super;
355 struct buffer_head *sbh;
356 struct nilfs_super_block *sbp;
357 struct backing_dev_info *bdi;
358 int blocksize;
359 int err = 0;
360
361 down_write(&nilfs->ns_sem);
362 if (nilfs_init(nilfs)) {
363 /* Load values from existing the_nilfs */
364 sbp = nilfs->ns_sbp;
365 err = nilfs_store_magic_and_option(sb, sbp, data);
366 if (err)
367 goto out;
368
369 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
370 if (sb->s_blocksize != blocksize &&
371 !sb_set_blocksize(sb, blocksize)) {
372 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
373 blocksize);
374 err = -EINVAL;
375 }
376 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
377 goto out;
378 }
379
380 sbp = nilfs_load_super_block(sb, &sbh);
381 if (!sbp) {
382 err = -EINVAL;
383 goto out;
384 }
385 err = nilfs_store_magic_and_option(sb, sbp, data);
386 if (err)
387 goto failed_sbh;
388
389 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
390 if (sb->s_blocksize != blocksize) {
391 sbp = nilfs_reload_super_block(sb, &sbh, blocksize);
392 if (!sbp) {
393 err = -EINVAL;
394 goto out;
395 /* not failed_sbh; sbh is released automatically
396 when reloading fails. */
397 }
398 }
399 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
400
401 err = nilfs_store_disk_layout(nilfs, sb, sbp);
402 if (err)
403 goto failed_sbh;
404
405 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
406
407 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
408 nilfs->ns_sbh = sbh;
409 nilfs->ns_sbp = sbp;
410
411 bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
412 if (!bdi)
413 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
414 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
415
416 /* Finding last segment */
417 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
418 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
419 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
420
421 nilfs->ns_seg_seq = nilfs->ns_last_seq;
422 nilfs->ns_segnum =
423 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
424 nilfs->ns_cno = nilfs->ns_last_cno + 1;
425 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
426 printk(KERN_ERR "NILFS invalid last segment number.\n");
427 err = -EINVAL;
428 goto failed_sbh;
429 }
430 /* Dummy values */
431 nilfs->ns_free_segments_count =
432 nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
433
434 /* Initialize gcinode cache */
435 err = nilfs_init_gccache(nilfs);
436 if (err)
437 goto failed_sbh;
438
439 set_nilfs_init(nilfs);
440 err = 0;
441 out:
442 up_write(&nilfs->ns_sem);
443 return err;
444
445 failed_sbh:
446 brelse(sbh);
447 goto out;
448}
449
450int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
451{
452 struct inode *dat = nilfs_dat_inode(nilfs);
453 unsigned long ncleansegs;
454 int err;
455
456 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
457 err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
458 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
459 if (likely(!err))
460 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
461 return err;
462}
463
8a9d2191
RK
464int nilfs_near_disk_full(struct the_nilfs *nilfs)
465{
466 struct inode *sufile = nilfs->ns_sufile;
467 unsigned long ncleansegs, nincsegs;
468 int ret;
469
470 ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
471 if (likely(!ret)) {
472 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
473 nilfs->ns_blocks_per_segment + 1;
474 if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
475 ret++;
476 }
477 return ret;
478}
479
480int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
481 int snapshot_mount)
482{
483 struct nilfs_sb_info *sbi;
484 int ret = 0;
485
486 down_read(&nilfs->ns_sem);
487 if (cno == 0 || cno > nilfs->ns_cno)
488 goto out_unlock;
489
490 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
491 if (sbi->s_snapshot_cno == cno &&
492 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
493 /* exclude read-only mounts */
494 ret++;
495 break;
496 }
497 }
498 /* for protecting recent checkpoints */
499 if (cno >= nilfs_last_cno(nilfs))
500 ret++;
501
502 out_unlock:
503 up_read(&nilfs->ns_sem);
504 return ret;
505}