Merge tag 'kvm-arm-for-v4.7-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / fs / nilfs2 / the_nilfs.c
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 * Written by Ryusuke Konishi.
17 *
18 */
19
20 #include <linux/buffer_head.h>
21 #include <linux/slab.h>
22 #include <linux/blkdev.h>
23 #include <linux/backing-dev.h>
24 #include <linux/random.h>
25 #include <linux/crc32.h>
26 #include "nilfs.h"
27 #include "segment.h"
28 #include "alloc.h"
29 #include "cpfile.h"
30 #include "sufile.h"
31 #include "dat.h"
32 #include "segbuf.h"
33
34
35 static int nilfs_valid_sb(struct nilfs_super_block *sbp);
36
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
45 if (!nilfs_sb_dirty(nilfs)) {
46 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
47 goto stay_cursor;
48
49 set_nilfs_sb_dirty(nilfs);
50 }
51 nilfs->ns_prev_seq = nilfs->ns_last_seq;
52
53 stay_cursor:
54 spin_unlock(&nilfs->ns_last_segment_lock);
55 }
56
57 /**
58 * alloc_nilfs - allocate a nilfs object
59 * @bdev: block device to which the_nilfs is related
60 *
61 * Return Value: On success, pointer to the_nilfs is returned.
62 * On error, NULL is returned.
63 */
64 struct the_nilfs *alloc_nilfs(struct block_device *bdev)
65 {
66 struct the_nilfs *nilfs;
67
68 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
69 if (!nilfs)
70 return NULL;
71
72 nilfs->ns_bdev = bdev;
73 atomic_set(&nilfs->ns_ndirtyblks, 0);
74 init_rwsem(&nilfs->ns_sem);
75 mutex_init(&nilfs->ns_snapshot_mount_mutex);
76 INIT_LIST_HEAD(&nilfs->ns_dirty_files);
77 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
78 spin_lock_init(&nilfs->ns_inode_lock);
79 spin_lock_init(&nilfs->ns_next_gen_lock);
80 spin_lock_init(&nilfs->ns_last_segment_lock);
81 nilfs->ns_cptree = RB_ROOT;
82 spin_lock_init(&nilfs->ns_cptree_lock);
83 init_rwsem(&nilfs->ns_segctor_sem);
84 nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
85
86 return nilfs;
87 }
88
89 /**
90 * destroy_nilfs - destroy nilfs object
91 * @nilfs: nilfs object to be released
92 */
93 void destroy_nilfs(struct the_nilfs *nilfs)
94 {
95 might_sleep();
96 if (nilfs_init(nilfs)) {
97 nilfs_sysfs_delete_device_group(nilfs);
98 brelse(nilfs->ns_sbh[0]);
99 brelse(nilfs->ns_sbh[1]);
100 }
101 kfree(nilfs);
102 }
103
104 static int nilfs_load_super_root(struct the_nilfs *nilfs,
105 struct super_block *sb, sector_t sr_block)
106 {
107 struct buffer_head *bh_sr;
108 struct nilfs_super_root *raw_sr;
109 struct nilfs_super_block **sbp = nilfs->ns_sbp;
110 struct nilfs_inode *rawi;
111 unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
112 unsigned int inode_size;
113 int err;
114
115 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
116 if (unlikely(err))
117 return err;
118
119 down_read(&nilfs->ns_sem);
120 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
121 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
122 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
123 up_read(&nilfs->ns_sem);
124
125 inode_size = nilfs->ns_inode_size;
126
127 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
128 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
129 if (err)
130 goto failed;
131
132 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
133 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
134 if (err)
135 goto failed_dat;
136
137 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
138 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
139 &nilfs->ns_sufile);
140 if (err)
141 goto failed_cpfile;
142
143 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
144 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
145
146 failed:
147 brelse(bh_sr);
148 return err;
149
150 failed_cpfile:
151 iput(nilfs->ns_cpfile);
152
153 failed_dat:
154 iput(nilfs->ns_dat);
155 goto failed;
156 }
157
158 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
159 {
160 memset(ri, 0, sizeof(*ri));
161 INIT_LIST_HEAD(&ri->ri_used_segments);
162 }
163
164 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
165 {
166 nilfs_dispose_segment_list(&ri->ri_used_segments);
167 }
168
169 /**
170 * nilfs_store_log_cursor - load log cursor from a super block
171 * @nilfs: nilfs object
172 * @sbp: buffer storing super block to be read
173 *
174 * nilfs_store_log_cursor() reads the last position of the log
175 * containing a super root from a given super block, and initializes
176 * relevant information on the nilfs object preparatory for log
177 * scanning and recovery.
178 */
179 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
180 struct nilfs_super_block *sbp)
181 {
182 int ret = 0;
183
184 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
185 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
186 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
187
188 nilfs->ns_prev_seq = nilfs->ns_last_seq;
189 nilfs->ns_seg_seq = nilfs->ns_last_seq;
190 nilfs->ns_segnum =
191 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
192 nilfs->ns_cno = nilfs->ns_last_cno + 1;
193 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
194 printk(KERN_ERR "NILFS invalid last segment number.\n");
195 ret = -EINVAL;
196 }
197 return ret;
198 }
199
200 /**
201 * load_nilfs - load and recover the nilfs
202 * @nilfs: the_nilfs structure to be released
203 * @sb: super block isntance used to recover past segment
204 *
205 * load_nilfs() searches and load the latest super root,
206 * attaches the last segment, and does recovery if needed.
207 * The caller must call this exclusively for simultaneous mounts.
208 */
209 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
210 {
211 struct nilfs_recovery_info ri;
212 unsigned int s_flags = sb->s_flags;
213 int really_read_only = bdev_read_only(nilfs->ns_bdev);
214 int valid_fs = nilfs_valid_fs(nilfs);
215 int err;
216
217 if (!valid_fs) {
218 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
219 if (s_flags & MS_RDONLY) {
220 printk(KERN_INFO "NILFS: INFO: recovery "
221 "required for readonly filesystem.\n");
222 printk(KERN_INFO "NILFS: write access will "
223 "be enabled during recovery.\n");
224 }
225 }
226
227 nilfs_init_recovery_info(&ri);
228
229 err = nilfs_search_super_root(nilfs, &ri);
230 if (unlikely(err)) {
231 struct nilfs_super_block **sbp = nilfs->ns_sbp;
232 int blocksize;
233
234 if (err != -EINVAL)
235 goto scan_error;
236
237 if (!nilfs_valid_sb(sbp[1])) {
238 printk(KERN_WARNING
239 "NILFS warning: unable to fall back to spare"
240 "super block\n");
241 goto scan_error;
242 }
243 printk(KERN_INFO
244 "NILFS: try rollback from an earlier position\n");
245
246 /*
247 * restore super block with its spare and reconfigure
248 * relevant states of the nilfs object.
249 */
250 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
251 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
252 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
253
254 /* verify consistency between two super blocks */
255 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
256 if (blocksize != nilfs->ns_blocksize) {
257 printk(KERN_WARNING
258 "NILFS warning: blocksize differs between "
259 "two super blocks (%d != %d)\n",
260 blocksize, nilfs->ns_blocksize);
261 goto scan_error;
262 }
263
264 err = nilfs_store_log_cursor(nilfs, sbp[0]);
265 if (err)
266 goto scan_error;
267
268 /* drop clean flag to allow roll-forward and recovery */
269 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
270 valid_fs = 0;
271
272 err = nilfs_search_super_root(nilfs, &ri);
273 if (err)
274 goto scan_error;
275 }
276
277 err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
278 if (unlikely(err)) {
279 printk(KERN_ERR "NILFS: error loading super root.\n");
280 goto failed;
281 }
282
283 if (valid_fs)
284 goto skip_recovery;
285
286 if (s_flags & MS_RDONLY) {
287 __u64 features;
288
289 if (nilfs_test_opt(nilfs, NORECOVERY)) {
290 printk(KERN_INFO "NILFS: norecovery option specified. "
291 "skipping roll-forward recovery\n");
292 goto skip_recovery;
293 }
294 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
295 ~NILFS_FEATURE_COMPAT_RO_SUPP;
296 if (features) {
297 printk(KERN_ERR "NILFS: couldn't proceed with "
298 "recovery because of unsupported optional "
299 "features (%llx)\n",
300 (unsigned long long)features);
301 err = -EROFS;
302 goto failed_unload;
303 }
304 if (really_read_only) {
305 printk(KERN_ERR "NILFS: write access "
306 "unavailable, cannot proceed.\n");
307 err = -EROFS;
308 goto failed_unload;
309 }
310 sb->s_flags &= ~MS_RDONLY;
311 } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
312 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
313 "option was specified for a read/write mount\n");
314 err = -EINVAL;
315 goto failed_unload;
316 }
317
318 err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
319 if (err)
320 goto failed_unload;
321
322 down_write(&nilfs->ns_sem);
323 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
324 err = nilfs_cleanup_super(sb);
325 up_write(&nilfs->ns_sem);
326
327 if (err) {
328 printk(KERN_ERR "NILFS: failed to update super block. "
329 "recovery unfinished.\n");
330 goto failed_unload;
331 }
332 printk(KERN_INFO "NILFS: recovery complete.\n");
333
334 skip_recovery:
335 nilfs_clear_recovery_info(&ri);
336 sb->s_flags = s_flags;
337 return 0;
338
339 scan_error:
340 printk(KERN_ERR "NILFS: error searching super root.\n");
341 goto failed;
342
343 failed_unload:
344 iput(nilfs->ns_cpfile);
345 iput(nilfs->ns_sufile);
346 iput(nilfs->ns_dat);
347
348 failed:
349 nilfs_clear_recovery_info(&ri);
350 sb->s_flags = s_flags;
351 return err;
352 }
353
354 static unsigned long long nilfs_max_size(unsigned int blkbits)
355 {
356 unsigned int max_bits;
357 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
358
359 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
360 if (max_bits < 64)
361 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
362 return res;
363 }
364
365 /**
366 * nilfs_nrsvsegs - calculate the number of reserved segments
367 * @nilfs: nilfs object
368 * @nsegs: total number of segments
369 */
370 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
371 {
372 return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
373 DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
374 100));
375 }
376
377 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
378 {
379 nilfs->ns_nsegments = nsegs;
380 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
381 }
382
383 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
384 struct nilfs_super_block *sbp)
385 {
386 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
387 printk(KERN_ERR "NILFS: unsupported revision "
388 "(superblock rev.=%d.%d, current rev.=%d.%d). "
389 "Please check the version of mkfs.nilfs.\n",
390 le32_to_cpu(sbp->s_rev_level),
391 le16_to_cpu(sbp->s_minor_rev_level),
392 NILFS_CURRENT_REV, NILFS_MINOR_REV);
393 return -EINVAL;
394 }
395 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
396 if (nilfs->ns_sbsize > BLOCK_SIZE)
397 return -EINVAL;
398
399 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
400 if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
401 printk(KERN_ERR "NILFS: too large inode size: %d bytes.\n",
402 nilfs->ns_inode_size);
403 return -EINVAL;
404 } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
405 printk(KERN_ERR "NILFS: too small inode size: %d bytes.\n",
406 nilfs->ns_inode_size);
407 return -EINVAL;
408 }
409
410 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
411
412 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
413 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
414 printk(KERN_ERR "NILFS: too short segment.\n");
415 return -EINVAL;
416 }
417
418 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
419 nilfs->ns_r_segments_percentage =
420 le32_to_cpu(sbp->s_r_segments_percentage);
421 if (nilfs->ns_r_segments_percentage < 1 ||
422 nilfs->ns_r_segments_percentage > 99) {
423 printk(KERN_ERR "NILFS: invalid reserved segments percentage.\n");
424 return -EINVAL;
425 }
426
427 nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
428 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
429 return 0;
430 }
431
432 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
433 {
434 static unsigned char sum[4];
435 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
436 size_t bytes;
437 u32 crc;
438
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 < sumoff + 4 || 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
452 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
453 {
454 return offset < ((le64_to_cpu(sbp->s_nsegments) *
455 le32_to_cpu(sbp->s_blocks_per_segment)) <<
456 (le32_to_cpu(sbp->s_log_block_size) + 10));
457 }
458
459 static void nilfs_release_super_block(struct the_nilfs *nilfs)
460 {
461 int i;
462
463 for (i = 0; i < 2; i++) {
464 if (nilfs->ns_sbp[i]) {
465 brelse(nilfs->ns_sbh[i]);
466 nilfs->ns_sbh[i] = NULL;
467 nilfs->ns_sbp[i] = NULL;
468 }
469 }
470 }
471
472 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
473 {
474 brelse(nilfs->ns_sbh[0]);
475 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
476 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
477 nilfs->ns_sbh[1] = NULL;
478 nilfs->ns_sbp[1] = NULL;
479 }
480
481 void nilfs_swap_super_block(struct the_nilfs *nilfs)
482 {
483 struct buffer_head *tsbh = nilfs->ns_sbh[0];
484 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
485
486 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
487 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
488 nilfs->ns_sbh[1] = tsbh;
489 nilfs->ns_sbp[1] = tsbp;
490 }
491
492 static int nilfs_load_super_block(struct the_nilfs *nilfs,
493 struct super_block *sb, int blocksize,
494 struct nilfs_super_block **sbpp)
495 {
496 struct nilfs_super_block **sbp = nilfs->ns_sbp;
497 struct buffer_head **sbh = nilfs->ns_sbh;
498 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
499 int valid[2], swp = 0;
500
501 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
502 &sbh[0]);
503 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
504
505 if (!sbp[0]) {
506 if (!sbp[1]) {
507 printk(KERN_ERR "NILFS: unable to read superblock\n");
508 return -EIO;
509 }
510 printk(KERN_WARNING
511 "NILFS warning: unable to read primary superblock "
512 "(blocksize = %d)\n", blocksize);
513 } else if (!sbp[1]) {
514 printk(KERN_WARNING
515 "NILFS warning: unable to read secondary superblock "
516 "(blocksize = %d)\n", blocksize);
517 }
518
519 /*
520 * Compare two super blocks and set 1 in swp if the secondary
521 * super block is valid and newer. Otherwise, set 0 in swp.
522 */
523 valid[0] = nilfs_valid_sb(sbp[0]);
524 valid[1] = nilfs_valid_sb(sbp[1]);
525 swp = valid[1] && (!valid[0] ||
526 le64_to_cpu(sbp[1]->s_last_cno) >
527 le64_to_cpu(sbp[0]->s_last_cno));
528
529 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
530 brelse(sbh[1]);
531 sbh[1] = NULL;
532 sbp[1] = NULL;
533 valid[1] = 0;
534 swp = 0;
535 }
536 if (!valid[swp]) {
537 nilfs_release_super_block(nilfs);
538 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
539 sb->s_id);
540 return -EINVAL;
541 }
542
543 if (!valid[!swp])
544 printk(KERN_WARNING "NILFS warning: broken superblock. "
545 "using spare superblock (blocksize = %d).\n", blocksize);
546 if (swp)
547 nilfs_swap_super_block(nilfs);
548
549 nilfs->ns_sbwcount = 0;
550 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
551 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
552 *sbpp = sbp[0];
553 return 0;
554 }
555
556 /**
557 * init_nilfs - initialize a NILFS instance.
558 * @nilfs: the_nilfs structure
559 * @sb: super block
560 * @data: mount options
561 *
562 * init_nilfs() performs common initialization per block device (e.g.
563 * reading the super block, getting disk layout information, initializing
564 * shared fields in the_nilfs).
565 *
566 * Return Value: On success, 0 is returned. On error, a negative error
567 * code is returned.
568 */
569 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
570 {
571 struct nilfs_super_block *sbp;
572 int blocksize;
573 int err;
574
575 down_write(&nilfs->ns_sem);
576
577 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
578 if (!blocksize) {
579 printk(KERN_ERR "NILFS: unable to set blocksize\n");
580 err = -EINVAL;
581 goto out;
582 }
583 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
584 if (err)
585 goto out;
586
587 err = nilfs_store_magic_and_option(sb, sbp, data);
588 if (err)
589 goto failed_sbh;
590
591 err = nilfs_check_feature_compatibility(sb, sbp);
592 if (err)
593 goto failed_sbh;
594
595 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
596 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
597 blocksize > NILFS_MAX_BLOCK_SIZE) {
598 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
599 "filesystem blocksize %d\n", blocksize);
600 err = -EINVAL;
601 goto failed_sbh;
602 }
603 if (sb->s_blocksize != blocksize) {
604 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
605
606 if (blocksize < hw_blocksize) {
607 printk(KERN_ERR
608 "NILFS: blocksize %d too small for device "
609 "(sector-size = %d).\n",
610 blocksize, hw_blocksize);
611 err = -EINVAL;
612 goto failed_sbh;
613 }
614 nilfs_release_super_block(nilfs);
615 sb_set_blocksize(sb, blocksize);
616
617 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
618 if (err)
619 goto out;
620 /*
621 * Not to failed_sbh; sbh is released automatically
622 * when reloading fails.
623 */
624 }
625 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
626 nilfs->ns_blocksize = blocksize;
627
628 get_random_bytes(&nilfs->ns_next_generation,
629 sizeof(nilfs->ns_next_generation));
630
631 err = nilfs_store_disk_layout(nilfs, sbp);
632 if (err)
633 goto failed_sbh;
634
635 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
636
637 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
638
639 err = nilfs_store_log_cursor(nilfs, sbp);
640 if (err)
641 goto failed_sbh;
642
643 err = nilfs_sysfs_create_device_group(sb);
644 if (err)
645 goto failed_sbh;
646
647 set_nilfs_init(nilfs);
648 err = 0;
649 out:
650 up_write(&nilfs->ns_sem);
651 return err;
652
653 failed_sbh:
654 nilfs_release_super_block(nilfs);
655 goto out;
656 }
657
658 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
659 size_t nsegs)
660 {
661 sector_t seg_start, seg_end;
662 sector_t start = 0, nblocks = 0;
663 unsigned int sects_per_block;
664 __u64 *sn;
665 int ret = 0;
666
667 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
668 bdev_logical_block_size(nilfs->ns_bdev);
669 for (sn = segnump; sn < segnump + nsegs; sn++) {
670 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
671
672 if (!nblocks) {
673 start = seg_start;
674 nblocks = seg_end - seg_start + 1;
675 } else if (start + nblocks == seg_start) {
676 nblocks += seg_end - seg_start + 1;
677 } else {
678 ret = blkdev_issue_discard(nilfs->ns_bdev,
679 start * sects_per_block,
680 nblocks * sects_per_block,
681 GFP_NOFS, 0);
682 if (ret < 0)
683 return ret;
684 nblocks = 0;
685 }
686 }
687 if (nblocks)
688 ret = blkdev_issue_discard(nilfs->ns_bdev,
689 start * sects_per_block,
690 nblocks * sects_per_block,
691 GFP_NOFS, 0);
692 return ret;
693 }
694
695 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
696 {
697 unsigned long ncleansegs;
698
699 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
700 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
701 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
702 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
703 return 0;
704 }
705
706 int nilfs_near_disk_full(struct the_nilfs *nilfs)
707 {
708 unsigned long ncleansegs, nincsegs;
709
710 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
711 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
712 nilfs->ns_blocks_per_segment + 1;
713
714 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
715 }
716
717 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
718 {
719 struct rb_node *n;
720 struct nilfs_root *root;
721
722 spin_lock(&nilfs->ns_cptree_lock);
723 n = nilfs->ns_cptree.rb_node;
724 while (n) {
725 root = rb_entry(n, struct nilfs_root, rb_node);
726
727 if (cno < root->cno) {
728 n = n->rb_left;
729 } else if (cno > root->cno) {
730 n = n->rb_right;
731 } else {
732 atomic_inc(&root->count);
733 spin_unlock(&nilfs->ns_cptree_lock);
734 return root;
735 }
736 }
737 spin_unlock(&nilfs->ns_cptree_lock);
738
739 return NULL;
740 }
741
742 struct nilfs_root *
743 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
744 {
745 struct rb_node **p, *parent;
746 struct nilfs_root *root, *new;
747 int err;
748
749 root = nilfs_lookup_root(nilfs, cno);
750 if (root)
751 return root;
752
753 new = kzalloc(sizeof(*root), GFP_KERNEL);
754 if (!new)
755 return NULL;
756
757 spin_lock(&nilfs->ns_cptree_lock);
758
759 p = &nilfs->ns_cptree.rb_node;
760 parent = NULL;
761
762 while (*p) {
763 parent = *p;
764 root = rb_entry(parent, struct nilfs_root, rb_node);
765
766 if (cno < root->cno) {
767 p = &(*p)->rb_left;
768 } else if (cno > root->cno) {
769 p = &(*p)->rb_right;
770 } else {
771 atomic_inc(&root->count);
772 spin_unlock(&nilfs->ns_cptree_lock);
773 kfree(new);
774 return root;
775 }
776 }
777
778 new->cno = cno;
779 new->ifile = NULL;
780 new->nilfs = nilfs;
781 atomic_set(&new->count, 1);
782 atomic64_set(&new->inodes_count, 0);
783 atomic64_set(&new->blocks_count, 0);
784
785 rb_link_node(&new->rb_node, parent, p);
786 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
787
788 spin_unlock(&nilfs->ns_cptree_lock);
789
790 err = nilfs_sysfs_create_snapshot_group(new);
791 if (err) {
792 kfree(new);
793 new = NULL;
794 }
795
796 return new;
797 }
798
799 void nilfs_put_root(struct nilfs_root *root)
800 {
801 if (atomic_dec_and_test(&root->count)) {
802 struct the_nilfs *nilfs = root->nilfs;
803
804 nilfs_sysfs_delete_snapshot_group(root);
805
806 spin_lock(&nilfs->ns_cptree_lock);
807 rb_erase(&root->rb_node, &nilfs->ns_cptree);
808 spin_unlock(&nilfs->ns_cptree_lock);
809 iput(root->ifile);
810
811 kfree(root);
812 }
813 }
This page took 0.068928 seconds and 6 git commands to generate.