📄 fsys_reiserfs.c
字号:
{ journal_table += j_len - i; goto found; } } } else { /* This is the end of cached journal marker. The remaining * transactions are still on disk. */ struct reiserfs_journal_desc desc; struct reiserfs_journal_commit commit; if (! journal_read (desc_block, sizeof (desc), (char *) &desc)) return 0; j_len = desc.j_len; while (i < j_len && i < JOURNAL_TRANS_HALF) if (desc.j_realblock[i++] == blockNr) goto found; if (j_len >= JOURNAL_TRANS_HALF) { int commit_block = (desc_block + 1 + j_len) & journal_mask; if (! journal_read (commit_block, sizeof (commit), (char *) &commit)) return 0; while (i < j_len) if (commit.j_realblock[i++ - JOURNAL_TRANS_HALF] == blockNr) goto found; } } goto not_found; found: translatedNr = INFO->journal_block + ((desc_block + i) & journal_mask);#ifdef REISERDEBUG printf ("block_read: block %d is mapped to journal block %d.\n", blockNr, translatedNr - INFO->journal_block);#endif /* We must continue the search, as this block may be overwritten * in later transactions. */ not_found: desc_block = (desc_block + 2 + j_len) & journal_mask; } return devread (translatedNr << INFO->blocksize_shift, start, len, buffer);}/* Init the journal data structure. We try to cache as much as * possible in the JOURNAL_START-JOURNAL_END space, but if it is full * we can still read the rest from the disk on demand. * * The first number of valid transactions and the descriptor block of the * first valid transaction are held in INFO. The transactions are all * adjacent, but we must take care of the journal wrap around. */static intjournal_init (void){ unsigned int block_count = INFO->journal_block_count; unsigned int desc_block; unsigned int commit_block; unsigned int next_trans_id; struct reiserfs_journal_header header; struct reiserfs_journal_desc desc; struct reiserfs_journal_commit commit; __u32 *journal_table = JOURNAL_START; journal_read (block_count, sizeof (header), (char *) &header); desc_block = header.j_first_unflushed_offset; if (desc_block >= block_count) return 0; INFO->journal_first_desc = desc_block; next_trans_id = header.j_last_flush_trans_id + 1;#ifdef REISERDEBUG printf ("journal_init: last flushed %d\n", header.j_last_flush_trans_id);#endif while (1) { journal_read (desc_block, sizeof (desc), (char *) &desc); if (substring (JOURNAL_DESC_MAGIC, desc.j_magic) > 0 || desc.j_trans_id != next_trans_id || desc.j_mount_id != header.j_mount_id) /* no more valid transactions */ break; commit_block = (desc_block + desc.j_len + 1) & (block_count - 1); journal_read (commit_block, sizeof (commit), (char *) &commit); if (desc.j_trans_id != commit.j_trans_id || desc.j_len != commit.j_len) /* no more valid transactions */ break; #ifdef REISERDEBUG printf ("Found valid transaction %d/%d at %d.\n", desc.j_trans_id, desc.j_mount_id, desc_block);#endif next_trans_id++; if (journal_table < JOURNAL_END) { if ((journal_table + 1 + desc.j_len) >= JOURNAL_END) { /* The table is almost full; mark the end of the cached * journal.*/ *journal_table = 0xffffffff; journal_table = JOURNAL_END; } else { int i; /* Cache the length and the realblock numbers in the table. * The block number of descriptor can easily be computed. * and need not to be stored here. */ *journal_table++ = desc.j_len; for (i = 0; i < desc.j_len && i < JOURNAL_TRANS_HALF; i++) { *journal_table++ = desc.j_realblock[i];#ifdef REISERDEBUG printf ("block %d is in journal %d.\n", desc.j_realblock[i], desc_block);#endif } for ( ; i < desc.j_len; i++) { *journal_table++ = commit.j_realblock[i-JOURNAL_TRANS_HALF];#ifdef REISERDEBUG printf ("block %d is in journal %d.\n", commit.j_realblock[i-JOURNAL_TRANS_HALF], desc_block);#endif } } } desc_block = (commit_block + 1) & (block_count - 1); }#ifdef REISERDEBUG printf ("Transaction %d/%d at %d isn't valid.\n", desc.j_trans_id, desc.j_mount_id, desc_block);#endif INFO->journal_transactions = next_trans_id - header.j_last_flush_trans_id - 1; return errnum == 0;}/* check filesystem types and read superblock into memory buffer */intreiserfs_mount (void){ struct reiserfs_super_block super; int superblock = REISERFS_DISK_OFFSET_IN_BYTES >> SECTOR_BITS; if (part_length < superblock + (sizeof (super) >> SECTOR_BITS) || ! devread (superblock, 0, sizeof (struct reiserfs_super_block), (char *) &super) || (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0 && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0) || (/* check that this is not a copy inside the journal log */ super.s_journal_block * super.s_blocksize <= REISERFS_DISK_OFFSET_IN_BYTES)) { /* Try old super block position */ superblock = REISERFS_OLD_DISK_OFFSET_IN_BYTES >> SECTOR_BITS; if (part_length < superblock + (sizeof (super) >> SECTOR_BITS) || ! devread (superblock, 0, sizeof (struct reiserfs_super_block), (char *) &super)) return 0; if (substring (REISER3FS_SUPER_MAGIC_STRING, super.s_magic) > 0 && substring (REISER2FS_SUPER_MAGIC_STRING, super.s_magic) > 0 && substring (REISERFS_SUPER_MAGIC_STRING, super.s_magic) > 0) { /* pre journaling super block ? */ if (substring (REISERFS_SUPER_MAGIC_STRING, (char*) ((int) &super + 20)) > 0) return 0; super.s_blocksize = REISERFS_OLD_BLOCKSIZE; super.s_journal_block = 0; super.s_version = 0; } } /* check the version number. */ if (super.s_version > REISERFS_MAX_SUPPORTED_VERSION) return 0; INFO->version = super.s_version; INFO->blocksize = super.s_blocksize; INFO->fullblocksize_shift = log2 (super.s_blocksize); INFO->blocksize_shift = INFO->fullblocksize_shift - SECTOR_BITS; INFO->cached_slots = (FSYSREISER_CACHE_SIZE >> INFO->fullblocksize_shift) - 1;#ifdef REISERDEBUG printf ("reiserfs_mount: version=%d, blocksize=%d\n", INFO->version, INFO->blocksize);#endif /* REISERDEBUG */ /* Clear node cache. */ memset (INFO->blocks, 0, sizeof (INFO->blocks)); if (super.s_blocksize < FSYSREISER_MIN_BLOCKSIZE || super.s_blocksize > FSYSREISER_MAX_BLOCKSIZE || (SECTOR_SIZE << INFO->blocksize_shift) != super.s_blocksize) return 0; /* Initialize journal code. If something fails we end with zero * journal_transactions, so we don't access the journal at all. */ INFO->journal_transactions = 0; if (super.s_journal_block != 0 && super.s_journal_dev == 0) { INFO->journal_block = super.s_journal_block; INFO->journal_block_count = super.s_journal_size; if (is_power_of_two (INFO->journal_block_count)) journal_init (); /* Read in super block again, maybe it is in the journal */ block_read (superblock >> INFO->blocksize_shift, 0, sizeof (struct reiserfs_super_block), (char *) &super); } if (! block_read (super.s_root_block, 0, INFO->blocksize, (char*) ROOT)) return 0; INFO->tree_depth = BLOCKHEAD (ROOT)->blk_level; #ifdef REISERDEBUG printf ("root read_in: block=%d, depth=%d\n", super.s_root_block, INFO->tree_depth);#endif /* REISERDEBUG */ if (INFO->tree_depth >= MAX_HEIGHT) return 0; if (INFO->tree_depth == DISK_LEAF_NODE_LEVEL) { /* There is only one node in the whole filesystem, * which is simultanously leaf and root */ memcpy (LEAF, ROOT, INFO->blocksize); } return 1;}/***************** TREE ACCESSING METHODS *****************************//* I assume you are familiar with the ReiserFS tree, if not go to * http://www.namesys.com/content_table.html * * My tree node cache is organized as following * 0 ROOT node * 1 LEAF node (if the ROOT is also a LEAF it is copied here * 2-n other nodes on current path from bottom to top. * if there is not enough space in the cache, the top most are * omitted. * * I have only two methods to find a key in the tree: * search_stat(dir_id, objectid) searches for the stat entry (always * the first entry) of an object. * next_key() gets the next key in tree order. * * This means, that I can only sequential reads of files are * efficient, but this really doesn't hurt for grub. *//* Read in the node at the current path and depth into the node cache. * You must set INFO->blocks[depth] before. */static char *read_tree_node (unsigned int blockNr, int depth){ char* cache = CACHE(depth); int num_cached = INFO->cached_slots; if (depth < num_cached) { /* This is the cached part of the path. Check if same block is * needed. */ if (blockNr == INFO->blocks[depth]) return cache; } else cache = CACHE(num_cached);#ifdef REISERDEBUG printf (" next read_in: block=%d (depth=%d)\n", blockNr, depth);#endif /* REISERDEBUG */ if (! block_read (blockNr, 0, INFO->blocksize, cache)) return 0; /* Make sure it has the right node level */ if (BLOCKHEAD (cache)->blk_level != depth) { errnum = ERR_FSYS_CORRUPT; return 0; } INFO->blocks[depth] = blockNr; return cache;}/* Get the next key, i.e. the key following the last retrieved key in * tree order. INFO->current_ih and * INFO->current_info are adapted accordingly. */static intnext_key (void){ int depth; struct item_head *ih = INFO->current_ih + 1; char *cache; #ifdef REISERDEBUG printf ("next_key:\n old ih: key %d:%d:%d:%d version:%d\n", INFO->current_ih->ih_key.k_dir_id, INFO->current_ih->ih_key.k_objectid, INFO->current_ih->ih_key.u.v1.k_offset, INFO->current_ih->ih_key.u.v1.k_uniqueness, INFO->current_ih->ih_version);#endif /* REISERDEBUG */ if (ih == &ITEMHEAD[BLOCKHEAD (LEAF)->blk_nr_item]) { depth = DISK_LEAF_NODE_LEVEL; /* The last item, was the last in the leaf node. * Read in the next block */ do { if (depth == INFO->tree_depth) { /* There are no more keys at all. * Return a dummy item with MAX_KEY */ ih = (struct item_head *) &BLOCKHEAD (LEAF)->blk_right_delim_key; goto found; } depth++;#ifdef REISERDEBUG printf (" depth=%d, i=%d\n", depth, INFO->next_key_nr[depth]);#endif /* REISERDEBUG */ } while (INFO->next_key_nr[depth] == 0); if (depth == INFO->tree_depth) cache = ROOT; else if (depth <= INFO->cached_slots) cache = CACHE (depth); else { cache = read_tree_node (INFO->blocks[depth], depth); if (! cache) return 0; } do { int nr_item = BLOCKHEAD (cache)->blk_nr_item; int key_nr = INFO->next_key_nr[depth]++;#ifdef REISERDEBUG printf (" depth=%d, i=%d/%d\n", depth, key_nr, nr_item);#endif /* REISERDEBUG */ if (key_nr == nr_item) /* This is the last item in this block, set the next_key_nr to 0 */ INFO->next_key_nr[depth] = 0; cache = read_tree_node (DC (cache)[key_nr].dc_block_number, --depth); if (! cache) return 0; } while (depth > DISK_LEAF_NODE_LEVEL); ih = ITEMHEAD; } found: INFO->current_ih = ih; INFO->current_item = &LEAF[ih->ih_item_location];#ifdef REISERDEBUG printf (" new ih: key %d:%d:%d:%d version:%d\n", INFO->current_ih->ih_key.k_dir_id, INFO->current_ih->ih_key.k_objectid, INFO->current_ih->ih_key.u.v1.k_offset, INFO->current_ih->ih_key.u.v1.k_uniqueness, INFO->current_ih->ih_version);#endif /* REISERDEBUG */ return 1;}/* preconditions: reiserfs_mount already executed, therefore * INFO block is valid * returns: 0 if error (errnum is set), * nonzero iff we were able to find the key successfully. * postconditions: on a nonzero return, the current_ih and * current_item fields describe the key that equals the * searched key. INFO->next_key contains the next key after * the searched key. * side effects: messes around with the cache. */static intsearch_stat (__u32 dir_id, __u32 objectid) { char *cache; int depth; int nr_item; int i; struct item_head *ih;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -