revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / plugin / disk_format / disk_format40.c
blob17718f095bbb5708e1af0e4d86b83603edcadf8e
1 /* Copyright 2002, 2003 by Hans Reiser, licensing governed by reiser4/README */
3 #include "../../debug.h"
4 #include "../../dformat.h"
5 #include "../../key.h"
6 #include "../node/node.h"
7 #include "../space/space_allocator.h"
8 #include "disk_format40.h"
9 #include "../plugin.h"
10 #include "../../txnmgr.h"
11 #include "../../jnode.h"
12 #include "../../tree.h"
13 #include "../../super.h"
14 #include "../../wander.h"
15 #include "../../inode.h"
16 #include "../../ktxnmgrd.h"
17 #include "../../status_flags.h"
19 #include <linux/types.h> /* for __u?? */
20 #include <linux/fs.h> /* for struct super_block */
21 #include <linux/buffer_head.h>
23 /* reiser 4.0 default disk layout */
25 /* Amount of free blocks needed to perform release_format40 when fs gets
26 mounted RW: 1 for SB, 1 for non-leaves in overwrite set, 2 for tx header
27 & tx record. */
28 #define RELEASE_RESERVED 4
30 /* The greatest supported format40 version number */
31 #define FORMAT40_VERSION PLUGIN_LIBRARY_VERSION
33 /* This flag indicates that backup should be updated
34 (the update is performed by fsck) */
35 #define FORMAT40_UPDATE_BACKUP (1 << 31)
37 /* functions to access fields of format40_disk_super_block */
38 static __u64 get_format40_block_count(const format40_disk_super_block * sb)
40 return le64_to_cpu(get_unaligned(&sb->block_count));
43 static __u64 get_format40_free_blocks(const format40_disk_super_block * sb)
45 return le64_to_cpu(get_unaligned(&sb->free_blocks));
48 static __u64 get_format40_root_block(const format40_disk_super_block * sb)
50 return le64_to_cpu(get_unaligned(&sb->root_block));
53 static __u16 get_format40_tree_height(const format40_disk_super_block * sb)
55 return le16_to_cpu(get_unaligned(&sb->tree_height));
58 static __u64 get_format40_file_count(const format40_disk_super_block * sb)
60 return le64_to_cpu(get_unaligned(&sb->file_count));
63 static __u64 get_format40_oid(const format40_disk_super_block * sb)
65 return le64_to_cpu(get_unaligned(&sb->oid));
68 static __u32 get_format40_mkfs_id(const format40_disk_super_block * sb)
70 return le32_to_cpu(get_unaligned(&sb->mkfs_id));
73 static __u64 get_format40_flags(const format40_disk_super_block * sb)
75 return le64_to_cpu(get_unaligned(&sb->flags));
78 static __u32 get_format40_version(const format40_disk_super_block * sb)
80 return le32_to_cpu(get_unaligned(&sb->version)) &
81 ~FORMAT40_UPDATE_BACKUP;
84 static int update_backup_version(const format40_disk_super_block * sb)
86 return (le32_to_cpu(get_unaligned(&sb->version)) &
87 FORMAT40_UPDATE_BACKUP);
90 static int update_disk_version(const format40_disk_super_block * sb)
92 return (get_format40_version(sb) < FORMAT40_VERSION);
95 static int incomplete_compatibility(const format40_disk_super_block * sb)
97 return (get_format40_version(sb) > FORMAT40_VERSION);
100 static format40_super_info *get_sb_info(struct super_block *super)
102 return &get_super_private(super)->u.format40;
105 static int consult_diskmap(struct super_block *s)
107 format40_super_info *info;
108 journal_location *jloc;
110 info = get_sb_info(s);
111 jloc = &get_super_private(s)->jloc;
112 /* Default format-specific locations, if there is nothing in
113 * diskmap */
114 jloc->footer = FORMAT40_JOURNAL_FOOTER_BLOCKNR;
115 jloc->header = FORMAT40_JOURNAL_HEADER_BLOCKNR;
116 info->loc.super = FORMAT40_OFFSET / s->s_blocksize;
117 #ifdef CONFIG_REISER4_BADBLOCKS
118 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID, FORMAT40_JF,
119 &jloc->footer);
120 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID, FORMAT40_JH,
121 &jloc->header);
122 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID, FORMAT40_SUPER,
123 &info->loc.super);
124 #endif
125 return 0;
128 /* find any valid super block of disk_format40 (even if the first
129 super block is destroyed), will change block numbers of actual journal header/footer (jf/jh)
130 if needed */
131 static struct buffer_head *find_a_disk_format40_super_block(struct super_block
134 struct buffer_head *super_bh;
135 format40_disk_super_block *disk_sb;
136 format40_super_info *info;
138 assert("umka-487", s != NULL);
140 info = get_sb_info(s);
142 super_bh = sb_bread(s, info->loc.super);
143 if (super_bh == NULL)
144 return ERR_PTR(RETERR(-EIO));
146 disk_sb = (format40_disk_super_block *) super_bh->b_data;
147 if (strncmp(disk_sb->magic, FORMAT40_MAGIC, sizeof(FORMAT40_MAGIC))) {
148 brelse(super_bh);
149 return ERR_PTR(RETERR(-EINVAL));
152 reiser4_set_block_count(s, le64_to_cpu(get_unaligned(&disk_sb->block_count)));
153 reiser4_set_data_blocks(s, le64_to_cpu(get_unaligned(&disk_sb->block_count)) -
154 le64_to_cpu(get_unaligned(&disk_sb->free_blocks)));
155 reiser4_set_free_blocks(s, le64_to_cpu(get_unaligned(&disk_sb->free_blocks)));
157 return super_bh;
160 /* find the most recent version of super block. This is called after journal is
161 replayed */
162 static struct buffer_head *read_super_block(struct super_block *s UNUSED_ARG)
164 /* Here the most recent superblock copy has to be read. However, as
165 journal replay isn't complete, we are using
166 find_a_disk_format40_super_block() function. */
167 return find_a_disk_format40_super_block(s);
170 static int get_super_jnode(struct super_block *s)
172 reiser4_super_info_data *sbinfo = get_super_private(s);
173 jnode *sb_jnode;
174 int ret;
176 sb_jnode = reiser4_alloc_io_head(&get_sb_info(s)->loc.super);
178 ret = jload(sb_jnode);
180 if (ret) {
181 reiser4_drop_io_head(sb_jnode);
182 return ret;
185 pin_jnode_data(sb_jnode);
186 jrelse(sb_jnode);
188 sbinfo->u.format40.sb_jnode = sb_jnode;
190 return 0;
193 static void done_super_jnode(struct super_block *s)
195 jnode *sb_jnode = get_super_private(s)->u.format40.sb_jnode;
197 if (sb_jnode) {
198 unpin_jnode_data(sb_jnode);
199 reiser4_drop_io_head(sb_jnode);
203 typedef enum format40_init_stage {
204 NONE_DONE = 0,
205 CONSULT_DISKMAP,
206 FIND_A_SUPER,
207 INIT_JOURNAL_INFO,
208 INIT_STATUS,
209 JOURNAL_REPLAY,
210 READ_SUPER,
211 KEY_CHECK,
212 INIT_OID,
213 INIT_TREE,
214 JOURNAL_RECOVER,
215 INIT_SA,
216 INIT_JNODE,
217 ALL_DONE
218 } format40_init_stage;
220 static format40_disk_super_block *copy_sb(const struct buffer_head *super_bh)
222 format40_disk_super_block *sb_copy;
224 sb_copy = kmalloc(sizeof(format40_disk_super_block),
225 reiser4_ctx_gfp_mask_get());
226 if (sb_copy == NULL)
227 return ERR_PTR(RETERR(-ENOMEM));
228 memcpy(sb_copy, ((format40_disk_super_block *) super_bh->b_data),
229 sizeof(format40_disk_super_block));
230 return sb_copy;
233 static int check_key_format(const format40_disk_super_block *sb_copy)
235 if (!equi(REISER4_LARGE_KEY,
236 get_format40_flags(sb_copy) & (1 << FORMAT40_LARGE_KEYS))) {
237 warning("nikita-3228", "Key format mismatch. "
238 "Only %s keys are supported.",
239 REISER4_LARGE_KEY ? "large" : "small");
240 return RETERR(-EINVAL);
242 return 0;
246 * try_init_format40
247 * @super:
248 * @stage:
251 static int try_init_format40(struct super_block *super,
252 format40_init_stage *stage)
254 int result;
255 struct buffer_head *super_bh;
256 reiser4_super_info_data *sbinfo;
257 format40_disk_super_block *sb_copy;
258 tree_level height;
259 reiser4_block_nr root_block;
260 node_plugin *nplug;
262 assert("vs-475", super != NULL);
263 assert("vs-474", get_super_private(super));
265 *stage = NONE_DONE;
267 result = consult_diskmap(super);
268 if (result)
269 return result;
270 *stage = CONSULT_DISKMAP;
272 super_bh = find_a_disk_format40_super_block(super);
273 if (IS_ERR(super_bh))
274 return PTR_ERR(super_bh);
275 brelse(super_bh);
276 *stage = FIND_A_SUPER;
278 /* ok, we are sure that filesystem format is a format40 format */
280 /* map jnodes for journal control blocks (header, footer) to disk */
281 result = reiser4_init_journal_info(super);
282 if (result)
283 return result;
284 *stage = INIT_JOURNAL_INFO;
286 /* ok, we are sure that filesystem format is a format40 format */
287 /* Now check it's state */
288 result = reiser4_status_init(FORMAT40_STATUS_BLOCKNR);
289 if (result != 0 && result != -EINVAL)
290 /* -EINVAL means there is no magic, so probably just old
291 * fs. */
292 return result;
293 *stage = INIT_STATUS;
295 result = reiser4_status_query(NULL, NULL);
296 if (result == REISER4_STATUS_MOUNT_WARN)
297 notice("vpf-1363", "Warning: mounting %s with errors.",
298 super->s_id);
299 if (result == REISER4_STATUS_MOUNT_RO)
300 notice("vpf-1364", "Warning: mounting %s with fatal errors,"
301 " forcing read-only mount.", super->s_id);
302 result = reiser4_journal_replay(super);
303 if (result)
304 return result;
305 *stage = JOURNAL_REPLAY;
307 super_bh = read_super_block(super);
308 if (IS_ERR(super_bh))
309 return PTR_ERR(super_bh);
310 *stage = READ_SUPER;
312 /* allocate and make a copy of format40_disk_super_block */
313 sb_copy = copy_sb(super_bh);
314 brelse(super_bh);
316 if (IS_ERR(sb_copy))
317 return PTR_ERR(sb_copy);
318 printk("reiser4: %s: found disk format 4.0.%u.\n",
319 super->s_id,
320 get_format40_version(sb_copy));
321 if (incomplete_compatibility(sb_copy))
322 printk("reiser4: Warning: The last completely supported "
323 "version of disk format40 is %u. Some objects of "
324 "the semantic tree can be unaccessible.\n",
325 FORMAT40_VERSION);
326 /* make sure that key format of kernel and filesystem match */
327 result = check_key_format(sb_copy);
328 if (result) {
329 kfree(sb_copy);
330 return result;
332 *stage = KEY_CHECK;
334 result = oid_init_allocator(super, get_format40_file_count(sb_copy),
335 get_format40_oid(sb_copy));
336 if (result) {
337 kfree(sb_copy);
338 return result;
340 *stage = INIT_OID;
342 /* get things necessary to init reiser4_tree */
343 root_block = get_format40_root_block(sb_copy);
344 height = get_format40_tree_height(sb_copy);
345 nplug = node_plugin_by_id(NODE40_ID);
347 /* initialize reiser4_super_info_data */
348 sbinfo = get_super_private(super);
349 assert("", sbinfo->tree.super == super);
350 /* init reiser4_tree for the filesystem */
351 result = reiser4_init_tree(&sbinfo->tree, &root_block, height, nplug);
352 if (result) {
353 kfree(sb_copy);
354 return result;
356 *stage = INIT_TREE;
359 * initialize reiser4_super_info_data with data from format40 super
360 * block
362 sbinfo->default_uid = 0;
363 sbinfo->default_gid = 0;
364 sbinfo->mkfs_id = get_format40_mkfs_id(sb_copy);
365 /* number of blocks in filesystem and reserved space */
366 reiser4_set_block_count(super, get_format40_block_count(sb_copy));
367 sbinfo->blocks_free = get_format40_free_blocks(sb_copy);
368 sbinfo->version = get_format40_version(sb_copy);
369 kfree(sb_copy);
371 if (update_backup_version(sb_copy))
372 printk("reiser4: Warning: metadata backup is not updated. "
373 "Please run 'fsck.reiser4 --fix' on %s.\n",
374 super->s_id);
376 sbinfo->fsuid = 0;
377 sbinfo->fs_flags |= (1 << REISER4_ADG); /* hard links for directories
378 * are not supported */
379 sbinfo->fs_flags |= (1 << REISER4_ONE_NODE_PLUGIN); /* all nodes in
380 * layout 40 are
381 * of one
382 * plugin */
383 /* sbinfo->tmgr is initialized already */
385 /* recover sb data which were logged separately from sb block */
387 /* NOTE-NIKITA: reiser4_journal_recover_sb_data() calls
388 * oid_init_allocator() and reiser4_set_free_blocks() with new
389 * data. What's the reason to call them above? */
390 result = reiser4_journal_recover_sb_data(super);
391 if (result != 0)
392 return result;
393 *stage = JOURNAL_RECOVER;
396 * Set number of used blocks. The number of used blocks is not stored
397 * neither in on-disk super block nor in the journal footer blocks. At
398 * this moment actual values of total blocks and free block counters
399 * are set in the reiser4 super block (in-memory structure) and we can
400 * calculate number of used blocks from them.
402 reiser4_set_data_blocks(super,
403 reiser4_block_count(super) -
404 reiser4_free_blocks(super));
406 #if REISER4_DEBUG
407 sbinfo->min_blocks_used = 16 /* reserved area */ +
408 2 /* super blocks */ +
409 2 /* journal footer and header */ ;
410 #endif
412 /* init disk space allocator */
413 result = sa_init_allocator(reiser4_get_space_allocator(super),
414 super, NULL);
415 if (result)
416 return result;
417 *stage = INIT_SA;
419 result = get_super_jnode(super);
420 if (result == 0)
421 *stage = ALL_DONE;
422 return result;
425 /* plugin->u.format.get_ready */
426 int init_format_format40(struct super_block *s, void *data UNUSED_ARG)
428 int result;
429 format40_init_stage stage;
431 result = try_init_format40(s, &stage);
432 switch (stage) {
433 case ALL_DONE:
434 assert("nikita-3458", result == 0);
435 break;
436 case INIT_JNODE:
437 done_super_jnode(s);
438 case INIT_SA:
439 sa_destroy_allocator(reiser4_get_space_allocator(s), s);
440 case JOURNAL_RECOVER:
441 case INIT_TREE:
442 reiser4_done_tree(&get_super_private(s)->tree);
443 case INIT_OID:
444 case KEY_CHECK:
445 case READ_SUPER:
446 case JOURNAL_REPLAY:
447 case INIT_STATUS:
448 reiser4_status_finish();
449 case INIT_JOURNAL_INFO:
450 reiser4_done_journal_info(s);
451 case FIND_A_SUPER:
452 case CONSULT_DISKMAP:
453 case NONE_DONE:
454 break;
455 default:
456 impossible("nikita-3457", "init stage: %i", stage);
459 if (!rofs_super(s) && reiser4_free_blocks(s) < RELEASE_RESERVED)
460 return RETERR(-ENOSPC);
462 return result;
465 static void pack_format40_super(const struct super_block *s, char *data)
467 format40_disk_super_block *super_data =
468 (format40_disk_super_block *) data;
470 reiser4_super_info_data *sbinfo = get_super_private(s);
472 assert("zam-591", data != NULL);
474 put_unaligned(cpu_to_le64(reiser4_free_committed_blocks(s)),
475 &super_data->free_blocks);
477 put_unaligned(cpu_to_le64(sbinfo->tree.root_block),
478 &super_data->root_block);
480 put_unaligned(cpu_to_le64(oid_next(s)),
481 &super_data->oid);
483 put_unaligned(cpu_to_le64(oids_used(s)),
484 &super_data->file_count);
486 put_unaligned(cpu_to_le16(sbinfo->tree.height),
487 &super_data->tree_height);
489 if (update_disk_version(super_data)) {
490 __u32 version = FORMAT40_VERSION | FORMAT40_UPDATE_BACKUP;
492 put_unaligned(cpu_to_le32(version), &super_data->version);
496 /* plugin->u.format.log_super
497 return a jnode which should be added to transaction when the super block
498 gets logged */
499 jnode *log_super_format40(struct super_block *s)
501 jnode *sb_jnode;
503 sb_jnode = get_super_private(s)->u.format40.sb_jnode;
505 jload(sb_jnode);
507 pack_format40_super(s, jdata(sb_jnode));
509 jrelse(sb_jnode);
511 return sb_jnode;
514 /* plugin->u.format.release */
515 int release_format40(struct super_block *s)
517 int ret;
518 reiser4_super_info_data *sbinfo;
520 sbinfo = get_super_private(s);
521 assert("zam-579", sbinfo != NULL);
523 if (!rofs_super(s)) {
524 ret = reiser4_capture_super_block(s);
525 if (ret != 0)
526 warning("vs-898",
527 "reiser4_capture_super_block failed: %d",
528 ret);
530 ret = txnmgr_force_commit_all(s, 1);
531 if (ret != 0)
532 warning("jmacd-74438", "txn_force failed: %d", ret);
534 all_grabbed2free();
537 sa_destroy_allocator(&sbinfo->space_allocator, s);
538 reiser4_done_journal_info(s);
539 done_super_jnode(s);
541 rcu_barrier();
542 reiser4_done_tree(&sbinfo->tree);
543 /* call finish_rcu(), because some znode were "released" in
544 * reiser4_done_tree(). */
545 rcu_barrier();
547 return 0;
550 #define FORMAT40_ROOT_LOCALITY 41
551 #define FORMAT40_ROOT_OBJECTID 42
553 /* plugin->u.format.root_dir_key */
554 const reiser4_key *root_dir_key_format40(const struct super_block *super
555 UNUSED_ARG)
557 static const reiser4_key FORMAT40_ROOT_DIR_KEY = {
558 .el = {
559 __constant_cpu_to_le64((FORMAT40_ROOT_LOCALITY << 4) | KEY_SD_MINOR),
560 #if REISER4_LARGE_KEY
561 ON_LARGE_KEY(0ull,)
562 #endif
563 __constant_cpu_to_le64(FORMAT40_ROOT_OBJECTID),
564 0ull
568 return &FORMAT40_ROOT_DIR_KEY;
571 /* plugin->u.format.check_open.
572 Check the opened object for validness. For now it checks for the valid oid &
573 locality only, can be improved later and it its work may depend on the mount
574 options. */
575 int check_open_format40(const struct inode *object)
577 oid_t max, oid;
579 max = oid_next(object->i_sb) - 1;
581 /* Check the oid. */
582 oid = get_inode_oid(object);
583 if (oid > max) {
584 warning("vpf-1360", "The object with the oid %llu "
585 "greater then the max used oid %llu found.",
586 (unsigned long long)oid, (unsigned long long)max);
588 return RETERR(-EIO);
591 /* Check the locality. */
592 oid = reiser4_inode_data(object)->locality_id;
593 if (oid > max) {
594 warning("vpf-1361", "The object with the locality %llu "
595 "greater then the max used oid %llu found.",
596 (unsigned long long)oid, (unsigned long long)max);
598 return RETERR(-EIO);
601 return 0;
604 /* plugin->u.format.version_update.
605 Perform all version update operations from the on-disk
606 format40_disk_super_block.version on disk to FORMAT40_VERSION.
608 int version_update_format40(struct super_block *super) {
609 txn_handle * trans;
610 lock_handle lh;
611 txn_atom *atom;
612 int ret;
614 /* Nothing to do if RO mount or the on-disk version is not less. */
615 if (super->s_flags & MS_RDONLY)
616 return 0;
618 if (get_super_private(super)->version >= FORMAT40_VERSION)
619 return 0;
621 printk("reiser4: Updating disk format to 4.0.%u. The reiser4 metadata "
622 "backup is left unchanged. Please run 'fsck.reiser4 --fix' "
623 "on %s to update it too.\n", FORMAT40_VERSION, super->s_id);
625 /* Mark the uber znode dirty to call log_super on write_logs. */
626 init_lh(&lh);
627 ret = get_uber_znode(reiser4_get_tree(super), ZNODE_WRITE_LOCK,
628 ZNODE_LOCK_HIPRI, &lh);
629 if (ret != 0)
630 return ret;
632 znode_make_dirty(lh.node);
633 done_lh(&lh);
635 /* Update the backup blocks. */
637 /* Force write_logs immediately. */
638 trans = get_current_context()->trans;
639 atom = get_current_atom_locked();
640 assert("vpf-1906", atom != NULL);
642 spin_lock_txnh(trans);
643 return force_commit_atom(trans);
646 /* Make Linus happy.
647 Local variables:
648 c-indentation-style: "K&R"
649 mode-name: "LC"
650 c-basic-offset: 8
651 tab-width: 8
652 fill-column: 120
653 scroll-step: 1
654 End: