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