Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * inode.c --- utility routines to read and write inodes |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 17 | #if HAVE_SYS_STAT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | #include <sys/stat.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 19 | #endif |
| 20 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 22 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 24 | #if EXT2_FLAT_INCLUDES |
| 25 | #include "ext2_fs.h" |
| 26 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 28 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 30 | #include "ext2fsP.h" |
| 31 | |
| 32 | struct ext2_struct_inode_scan { |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 33 | errcode_t magic; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 34 | ext2_filsys fs; |
| 35 | ino_t current_inode; |
| 36 | blk_t current_block; |
| 37 | dgrp_t current_group; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 38 | ino_t inodes_left; |
| 39 | blk_t blocks_left; |
| 40 | dgrp_t groups_left; |
| 41 | blk_t inode_buffer_blocks; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 42 | char * inode_buffer; |
| 43 | int inode_size; |
| 44 | char * ptr; |
| 45 | int bytes_left; |
| 46 | char *temp_buffer; |
| 47 | errcode_t (*done_group)(ext2_filsys fs, |
| 48 | ext2_inode_scan scan, |
| 49 | dgrp_t group, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 50 | void * priv_data); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 51 | void * done_group_data; |
| 52 | int bad_block_ptr; |
| 53 | int scan_flags; |
| 54 | int reserved[6]; |
| 55 | }; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 56 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 57 | static errcode_t create_icache(ext2_filsys fs) |
| 58 | { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 59 | errcode_t retval; |
| 60 | int i; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 61 | |
| 62 | if (fs->icache) |
| 63 | return 0; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 64 | retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), |
| 65 | (void **) &fs->icache); |
| 66 | if (retval) |
| 67 | return retval; |
| 68 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 69 | memset(fs->icache, 0, sizeof(struct ext2_inode_cache)); |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 70 | retval = ext2fs_get_mem(fs->blocksize, (void **) &fs->icache->buffer); |
| 71 | if (retval) { |
| 72 | ext2fs_free_mem((void **) &fs->icache); |
| 73 | return retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 74 | } |
| 75 | fs->icache->buffer_blk = 0; |
| 76 | fs->icache->cache_last = -1; |
| 77 | fs->icache->cache_size = 4; |
| 78 | fs->icache->refcount = 1; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 79 | retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent) |
| 80 | * fs->icache->cache_size, |
| 81 | (void **) &fs->icache->cache); |
| 82 | if (retval) { |
| 83 | ext2fs_free_mem((void **) &fs->icache->buffer); |
| 84 | ext2fs_free_mem((void **) &fs->icache); |
| 85 | return retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 86 | } |
| 87 | for (i=0; i < fs->icache->cache_size; i++) |
| 88 | fs->icache->cache[i].ino = 0; |
| 89 | return 0; |
| 90 | } |
| 91 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks, |
| 93 | ext2_inode_scan *ret_scan) |
| 94 | { |
| 95 | ext2_inode_scan scan; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 96 | errcode_t retval; |
Theodore Ts'o | d163b09 | 1997-10-03 17:42:28 +0000 | [diff] [blame] | 97 | errcode_t (*save_get_blocks)(ext2_filsys f, ino_t ino, blk_t *blocks); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 99 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 100 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 101 | /* |
| 102 | * If fs->badblocks isn't set, then set it --- since the inode |
| 103 | * scanning functions require it. |
| 104 | */ |
| 105 | if (fs->badblocks == 0) { |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 106 | /* |
| 107 | * Temporarly save fs->get_blocks and set it to zero, |
| 108 | * for compatibility with old e2fsck's. |
| 109 | */ |
| 110 | save_get_blocks = fs->get_blocks; |
| 111 | fs->get_blocks = 0; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 112 | retval = ext2fs_read_bb_inode(fs, &fs->badblocks); |
| 113 | if (retval && fs->badblocks) { |
| 114 | badblocks_list_free(fs->badblocks); |
| 115 | fs->badblocks = 0; |
| 116 | } |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 117 | fs->get_blocks = save_get_blocks; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 120 | retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), |
| 121 | (void **) &scan); |
| 122 | if (retval) |
| 123 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 124 | memset(scan, 0, sizeof(struct ext2_struct_inode_scan)); |
| 125 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 126 | scan->magic = EXT2_ET_MAGIC_INODE_SCAN; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 127 | scan->fs = fs; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 128 | scan->inode_size = EXT2_INODE_SIZE(fs->super); |
| 129 | scan->bytes_left = 0; |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 130 | scan->current_group = 0; |
| 131 | scan->groups_left = fs->group_desc_count - 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 132 | scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8; |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 133 | scan->current_block = scan->fs-> |
| 134 | group_desc[scan->current_group].bg_inode_table; |
| 135 | scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super); |
| 136 | scan->blocks_left = scan->fs->inode_blocks_per_group; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 137 | retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks * |
| 138 | fs->blocksize), |
| 139 | (void **) &scan->inode_buffer); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 140 | scan->done_group = 0; |
| 141 | scan->done_group_data = 0; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 142 | scan->bad_block_ptr = 0; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 143 | if (retval) { |
| 144 | ext2fs_free_mem((void **) &scan); |
| 145 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | } |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 147 | retval = ext2fs_get_mem(scan->inode_size, |
| 148 | (void **) &scan->temp_buffer); |
| 149 | if (retval) { |
| 150 | ext2fs_free_mem((void **) &scan->inode_buffer); |
| 151 | ext2fs_free_mem((void **) &scan); |
| 152 | return retval; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 153 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 154 | if (scan->fs->badblocks && scan->fs->badblocks->num) |
| 155 | scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 156 | *ret_scan = scan; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | void ext2fs_close_inode_scan(ext2_inode_scan scan) |
| 161 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 162 | if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) |
| 163 | return; |
| 164 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 165 | ext2fs_free_mem((void **) &scan->inode_buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 166 | scan->inode_buffer = NULL; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 167 | ext2fs_free_mem((void **) &scan->temp_buffer); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 168 | scan->temp_buffer = NULL; |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 169 | ext2fs_free_mem((void **) &scan); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 173 | void ext2fs_set_inode_callback(ext2_inode_scan scan, |
| 174 | errcode_t (*done_group)(ext2_filsys fs, |
| 175 | ext2_inode_scan scan, |
| 176 | dgrp_t group, |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 177 | void * priv_data), |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 178 | void *done_group_data) |
| 179 | { |
| 180 | if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) |
| 181 | return; |
| 182 | |
| 183 | scan->done_group = done_group; |
| 184 | scan->done_group_data = done_group_data; |
| 185 | } |
| 186 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 187 | int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags, |
| 188 | int clear_flags) |
| 189 | { |
| 190 | int old_flags; |
| 191 | |
| 192 | if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN)) |
| 193 | return 0; |
| 194 | |
| 195 | old_flags = scan->scan_flags; |
| 196 | scan->scan_flags &= ~clear_flags; |
| 197 | scan->scan_flags |= set_flags; |
| 198 | return old_flags; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * This function is called by ext2fs_get_next_inode when it needs to |
| 203 | * get ready to read in a new blockgroup. |
| 204 | */ |
| 205 | static errcode_t get_next_blockgroup(ext2_inode_scan scan) |
| 206 | { |
| 207 | scan->current_group++; |
| 208 | scan->groups_left--; |
| 209 | |
| 210 | scan->current_block = scan->fs-> |
| 211 | group_desc[scan->current_group].bg_inode_table; |
| 212 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 213 | scan->current_inode = scan->current_group * |
| 214 | EXT2_INODES_PER_GROUP(scan->fs->super); |
| 215 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 216 | scan->bytes_left = 0; |
| 217 | scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super); |
| 218 | scan->blocks_left = scan->fs->inode_blocks_per_group; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan, |
| 223 | int group) |
| 224 | { |
| 225 | scan->current_group = group - 1; |
| 226 | scan->groups_left = scan->fs->group_desc_count - group; |
| 227 | return get_next_blockgroup(scan); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * This function is called by get_next_blocks() to check for bad |
| 232 | * blocks in the inode table. |
| 233 | * |
| 234 | * This function assumes that badblocks_list->list is sorted in |
| 235 | * increasing order. |
| 236 | */ |
| 237 | static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan, |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 238 | blk_t *num_blocks) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 239 | { |
| 240 | blk_t blk = scan->current_block; |
| 241 | badblocks_list bb = scan->fs->badblocks; |
| 242 | |
| 243 | /* |
| 244 | * If the inode table is missing, then obviously there are no |
| 245 | * bad blocks. :-) |
| 246 | */ |
| 247 | if (blk == 0) |
| 248 | return 0; |
| 249 | |
| 250 | /* |
| 251 | * If the current block is greater than the bad block listed |
| 252 | * in the bad block list, then advance the pointer until this |
| 253 | * is no longer the case. If we run out of bad blocks, then |
| 254 | * we don't need to do any more checking! |
| 255 | */ |
| 256 | while (blk > bb->list[scan->bad_block_ptr]) { |
| 257 | if (++scan->bad_block_ptr >= bb->num) { |
| 258 | scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * If the current block is equal to the bad block listed in |
| 265 | * the bad block list, then handle that one block specially. |
| 266 | * (We could try to handle runs of bad blocks, but that |
| 267 | * only increases CPU efficiency by a small amount, at the |
| 268 | * expense of a huge expense of code complexity, and for an |
| 269 | * uncommon case at that.) |
| 270 | */ |
| 271 | if (blk == bb->list[scan->bad_block_ptr]) { |
| 272 | scan->scan_flags |= EXT2_SF_BAD_INODE_BLK; |
| 273 | *num_blocks = 1; |
| 274 | if (++scan->bad_block_ptr >= bb->num) |
| 275 | scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS; |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * If there is a bad block in the range that we're about to |
| 281 | * read in, adjust the number of blocks to read so that we we |
| 282 | * don't read in the bad block. (Then the next block to read |
| 283 | * will be the bad block, which is handled in the above case.) |
| 284 | */ |
| 285 | if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr]) |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 286 | *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * This function is called by ext2fs_get_next_inode when it needs to |
| 293 | * read in more blocks from the current blockgroup's inode table. |
| 294 | */ |
| 295 | static errcode_t get_next_blocks(ext2_inode_scan scan) |
| 296 | { |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 297 | blk_t num_blocks; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 298 | errcode_t retval; |
| 299 | |
| 300 | /* |
| 301 | * Figure out how many blocks to read; we read at most |
| 302 | * inode_buffer_blocks, and perhaps less if there aren't that |
| 303 | * many blocks left to read. |
| 304 | */ |
| 305 | num_blocks = scan->inode_buffer_blocks; |
| 306 | if (num_blocks > scan->blocks_left) |
| 307 | num_blocks = scan->blocks_left; |
| 308 | |
| 309 | /* |
| 310 | * If the past block "read" was a bad block, then mark the |
| 311 | * left-over extra bytes as also being bad. |
| 312 | */ |
| 313 | if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) { |
| 314 | if (scan->bytes_left) |
| 315 | scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES; |
| 316 | scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Do inode bad block processing, if necessary. |
| 321 | */ |
| 322 | if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) { |
| 323 | retval = check_for_inode_bad_blocks(scan, &num_blocks); |
| 324 | if (retval) |
| 325 | return retval; |
| 326 | } |
| 327 | |
| 328 | if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) || |
| 329 | (scan->current_block == 0)) { |
| 330 | memset(scan->inode_buffer, 0, |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 331 | (size_t) num_blocks * scan->fs->blocksize); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 332 | } else { |
| 333 | retval = io_channel_read_blk(scan->fs->io, |
| 334 | scan->current_block, |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 335 | (int) num_blocks, |
| 336 | scan->inode_buffer); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 337 | if (retval) |
| 338 | return EXT2_ET_NEXT_INODE_READ; |
| 339 | } |
| 340 | scan->ptr = scan->inode_buffer; |
| 341 | scan->bytes_left = num_blocks * scan->fs->blocksize; |
| 342 | |
| 343 | scan->blocks_left -= num_blocks; |
| 344 | if (scan->current_block) |
| 345 | scan->current_block += num_blocks; |
| 346 | return 0; |
| 347 | } |
| 348 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 349 | #if 0 |
| 350 | /* |
| 351 | * Returns 1 if the entire inode_buffer has a non-zero size and |
| 352 | * contains all zeros. (Not just deleted inodes, since that means |
| 353 | * that part of the inode table was used at one point; we want all |
| 354 | * zeros, which means that the inode table is pristine.) |
| 355 | */ |
| 356 | static inline int is_empty_scan(ext2_inode_scan scan) |
| 357 | { |
| 358 | int i; |
| 359 | |
| 360 | if (scan->bytes_left == 0) |
| 361 | return 0; |
| 362 | |
| 363 | for (i=0; i < scan->bytes_left; i++) |
| 364 | if (scan->ptr[i]) |
| 365 | return 0; |
| 366 | return 1; |
| 367 | } |
| 368 | #endif |
| 369 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 370 | errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ino_t *ino, |
| 371 | struct ext2_inode *inode) |
| 372 | { |
| 373 | errcode_t retval; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 374 | int extra_bytes = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 375 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 376 | EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN); |
| 377 | |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 378 | /* |
| 379 | * Do we need to start reading a new block group? |
| 380 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 381 | if (scan->inodes_left <= 0) { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 382 | force_new_group: |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 383 | if (scan->done_group) { |
| 384 | retval = (scan->done_group) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 385 | (scan->fs, scan, scan->current_group, |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 386 | scan->done_group_data); |
| 387 | if (retval) |
| 388 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 389 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 390 | if (scan->groups_left <= 0) { |
| 391 | *ino = 0; |
| 392 | return 0; |
| 393 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 394 | retval = get_next_blockgroup(scan); |
| 395 | if (retval) |
| 396 | return retval; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 397 | } |
Theodore Ts'o | 218a486 | 1998-02-21 01:41:39 +0000 | [diff] [blame] | 398 | /* |
| 399 | * This is done outside the above if statement so that the |
| 400 | * check can be done for block group #0. |
| 401 | */ |
| 402 | if (scan->current_block == 0) { |
| 403 | if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) { |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 404 | goto force_new_group; |
Theodore Ts'o | 218a486 | 1998-02-21 01:41:39 +0000 | [diff] [blame] | 405 | } else |
| 406 | return EXT2_ET_MISSING_INODE_TABLE; |
| 407 | } |
| 408 | |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * Have we run out of space in the inode buffer? If so, we |
| 412 | * need to read in more blocks. |
| 413 | */ |
| 414 | if (scan->bytes_left < scan->inode_size) { |
| 415 | memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left); |
| 416 | extra_bytes = scan->bytes_left; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 417 | |
| 418 | retval = get_next_blocks(scan); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 419 | if (retval) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 420 | return retval; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 421 | #if 0 |
| 422 | /* |
| 423 | * XXX test Need check for used inode somehow. |
| 424 | * (Note: this is hard.) |
| 425 | */ |
| 426 | if (is_empty_scan(scan)) |
| 427 | goto force_new_group; |
| 428 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 429 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 430 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 431 | retval = 0; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 432 | if (extra_bytes) { |
| 433 | memcpy(scan->temp_buffer+extra_bytes, scan->ptr, |
| 434 | scan->inode_size - extra_bytes); |
| 435 | scan->ptr += scan->inode_size - extra_bytes; |
| 436 | scan->bytes_left -= scan->inode_size - extra_bytes; |
| 437 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 438 | if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 439 | (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 440 | ext2fs_swap_inode(scan->fs, inode, |
| 441 | (struct ext2_inode *) scan->temp_buffer, 0); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 442 | else |
| 443 | *inode = *((struct ext2_inode *) scan->temp_buffer); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 444 | if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES) |
| 445 | retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; |
| 446 | scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 447 | } else { |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 448 | if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 449 | (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 450 | ext2fs_swap_inode(scan->fs, inode, |
| 451 | (struct ext2_inode *) scan->ptr, 0); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 452 | else |
| 453 | *inode = *((struct ext2_inode *) scan->ptr); |
| 454 | scan->ptr += scan->inode_size; |
| 455 | scan->bytes_left -= scan->inode_size; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 456 | if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) |
| 457 | retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 458 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 459 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 460 | scan->inodes_left--; |
| 461 | scan->current_inode++; |
| 462 | *ino = scan->current_inode; |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 463 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Functions to read and write a single inode. |
| 468 | */ |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 469 | errcode_t ext2fs_read_inode (ext2_filsys fs, ino_t ino, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 470 | struct ext2_inode * inode) |
| 471 | { |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 472 | unsigned long group, block, block_nr, offset; |
| 473 | char *ptr; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 474 | errcode_t retval; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 475 | int clen, length, i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 476 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 477 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 478 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 479 | /* Check to see if user has an override function */ |
| 480 | if (fs->read_inode) { |
| 481 | retval = (fs->read_inode)(fs, ino, inode); |
| 482 | if (retval != EXT2_ET_CALLBACK_NOTHANDLED) |
| 483 | return retval; |
| 484 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 485 | /* Create inode cache if not present */ |
| 486 | if (!fs->icache) { |
| 487 | retval = create_icache(fs); |
| 488 | if (retval) |
| 489 | return retval; |
| 490 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 491 | /* Check to see if it's in the inode cache */ |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 492 | for (i=0; i < fs->icache->cache_size; i++) { |
| 493 | if (fs->icache->cache[i].ino == ino) { |
| 494 | *inode = fs->icache->cache[i].inode; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | } |
Theodore Ts'o | 665f710 | 1999-01-08 13:33:39 +0000 | [diff] [blame^] | 498 | if ((ino == 0) || (ino > fs->super->s_inodes_count)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 499 | return EXT2_ET_BAD_INODE_NUM; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 500 | group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 501 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * |
| 502 | EXT2_INODE_SIZE(fs->super); |
| 503 | block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 504 | if (!fs->group_desc[(unsigned)group].bg_inode_table) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 505 | return EXT2_ET_MISSING_INODE_TABLE; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 506 | block_nr = fs->group_desc[(unsigned)group].bg_inode_table + block; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 507 | if (block_nr != fs->icache->buffer_blk) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 508 | retval = io_channel_read_blk(fs->io, block_nr, 1, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 509 | fs->icache->buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 510 | if (retval) |
| 511 | return retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 512 | fs->icache->buffer_blk = block_nr; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 513 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 514 | offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 515 | ptr = ((char *) fs->icache->buffer) + (unsigned) offset; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 516 | |
| 517 | memset(inode, 0, sizeof(struct ext2_inode)); |
| 518 | length = EXT2_INODE_SIZE(fs->super); |
| 519 | if (length > sizeof(struct ext2_inode)) |
| 520 | length = sizeof(struct ext2_inode); |
| 521 | |
| 522 | if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) { |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 523 | clen = (int) (EXT2_BLOCK_SIZE(fs->super) - offset); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 524 | memcpy((char *) inode, ptr, clen); |
| 525 | length -= clen; |
| 526 | |
| 527 | retval = io_channel_read_blk(fs->io, block_nr+1, 1, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 528 | fs->icache->buffer); |
| 529 | if (retval) { |
| 530 | fs->icache->buffer_blk = 0; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 531 | return retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 532 | } |
| 533 | fs->icache->buffer_blk = block_nr+1; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 534 | |
| 535 | memcpy(((char *) inode) + clen, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 536 | fs->icache->buffer, length); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 537 | } else |
| 538 | memcpy((char *) inode, ptr, length); |
| 539 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 540 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 541 | (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 542 | ext2fs_swap_inode(fs, inode, inode, 0); |
| 543 | |
| 544 | /* Update the inode cache */ |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 545 | fs->icache->cache_last = (fs->icache->cache_last + 1) % |
| 546 | fs->icache->cache_size; |
| 547 | fs->icache->cache[fs->icache->cache_last].ino = ino; |
| 548 | fs->icache->cache[fs->icache->cache_last].inode = *inode; |
| 549 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 553 | errcode_t ext2fs_write_inode(ext2_filsys fs, ino_t ino, |
| 554 | struct ext2_inode * inode) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 555 | { |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 556 | unsigned long group, block, block_nr, offset; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 557 | errcode_t retval; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 558 | struct ext2_inode temp_inode; |
| 559 | char *ptr; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 560 | int clen, length, i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 561 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 562 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 563 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 564 | /* Check to see if user provided an override function */ |
| 565 | if (fs->write_inode) { |
| 566 | retval = (fs->write_inode)(fs, ino, inode); |
| 567 | if (retval != EXT2_ET_CALLBACK_NOTHANDLED) |
| 568 | return retval; |
| 569 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 570 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 571 | /* Check to see if the inode cache needs to be updated */ |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 572 | if (fs->icache) { |
| 573 | for (i=0; i < fs->icache->cache_size; i++) { |
| 574 | if (fs->icache->cache[i].ino == ino) { |
| 575 | fs->icache->cache[i].inode = *inode; |
| 576 | break; |
| 577 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 578 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 579 | } else { |
| 580 | retval = create_icache(fs); |
| 581 | if (retval) |
| 582 | return retval; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 583 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 584 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 585 | if (!(fs->flags & EXT2_FLAG_RW)) |
| 586 | return EXT2_ET_RO_FILSYS; |
| 587 | |
Theodore Ts'o | 665f710 | 1999-01-08 13:33:39 +0000 | [diff] [blame^] | 588 | if ((ino == 0) || (ino > fs->super->s_inodes_count)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 589 | return EXT2_ET_BAD_INODE_NUM; |
| 590 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 591 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 592 | (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 593 | ext2fs_swap_inode(fs, &temp_inode, inode, 1); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 594 | else |
| 595 | memcpy(&temp_inode, inode, sizeof(struct ext2_inode)); |
| 596 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 597 | group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 598 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) * |
| 599 | EXT2_INODE_SIZE(fs->super); |
| 600 | block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super); |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 601 | if (!fs->group_desc[(unsigned) group].bg_inode_table) |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 602 | return EXT2_ET_MISSING_INODE_TABLE; |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 603 | block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 604 | offset &= (EXT2_BLOCK_SIZE(fs->super) - 1); |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 605 | ptr = (char *) fs->icache->buffer + (unsigned) offset; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 606 | |
| 607 | length = EXT2_INODE_SIZE(fs->super); |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 608 | clen = length; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 609 | if (length > sizeof(struct ext2_inode)) |
| 610 | length = sizeof(struct ext2_inode); |
| 611 | |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 612 | if (fs->icache->buffer_blk != block_nr) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 613 | retval = io_channel_read_blk(fs->io, block_nr, 1, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 614 | fs->icache->buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 615 | if (retval) |
| 616 | return retval; |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 617 | fs->icache->buffer_blk = block_nr; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 618 | } |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 619 | |
| 620 | if ((offset + length) > EXT2_BLOCK_SIZE(fs->super)) { |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 621 | clen = (int) (EXT2_BLOCK_SIZE(fs->super) - offset); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 622 | length -= clen; |
| 623 | } else { |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 624 | length = 0; |
| 625 | } |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 626 | memcpy(ptr, &temp_inode, clen); |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 627 | retval = io_channel_write_blk(fs->io, block_nr, 1, fs->icache->buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 628 | if (retval) |
| 629 | return retval; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 630 | |
| 631 | if (length) { |
| 632 | retval = io_channel_read_blk(fs->io, ++block_nr, 1, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 633 | fs->icache->buffer); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 634 | if (retval) { |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 635 | fs->icache->buffer_blk = 0; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 636 | return retval; |
| 637 | } |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 638 | fs->icache->buffer_blk = block_nr; |
| 639 | memcpy(fs->icache->buffer, ((char *) &temp_inode) + clen, |
| 640 | length); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 641 | |
| 642 | retval = io_channel_write_blk(fs->io, block_nr, 1, |
Theodore Ts'o | a29f4d3 | 1997-04-29 21:26:48 +0000 | [diff] [blame] | 643 | fs->icache->buffer); |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 644 | if (retval) |
| 645 | return retval; |
| 646 | } |
| 647 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 648 | fs->flags |= EXT2_FLAG_CHANGED; |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | errcode_t ext2fs_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks) |
| 653 | { |
| 654 | struct ext2_inode inode; |
| 655 | int i; |
| 656 | errcode_t retval; |
| 657 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 658 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 659 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 660 | if (ino > fs->super->s_inodes_count) |
| 661 | return EXT2_ET_BAD_INODE_NUM; |
| 662 | |
| 663 | if (fs->get_blocks) { |
| 664 | if (!(*fs->get_blocks)(fs, ino, blocks)) |
| 665 | return 0; |
| 666 | } |
| 667 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 668 | if (retval) |
| 669 | return retval; |
| 670 | for (i=0; i < EXT2_N_BLOCKS; i++) |
| 671 | blocks[i] = inode.i_block[i]; |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | errcode_t ext2fs_check_directory(ext2_filsys fs, ino_t ino) |
| 676 | { |
| 677 | struct ext2_inode inode; |
| 678 | errcode_t retval; |
| 679 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 680 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 681 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 682 | if (ino > fs->super->s_inodes_count) |
| 683 | return EXT2_ET_BAD_INODE_NUM; |
| 684 | |
Theodore Ts'o | d163b09 | 1997-10-03 17:42:28 +0000 | [diff] [blame] | 685 | if (fs->check_directory) { |
| 686 | retval = (fs->check_directory)(fs, ino); |
| 687 | if (retval != EXT2_ET_CALLBACK_NOTHANDLED) |
| 688 | return retval; |
| 689 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 690 | retval = ext2fs_read_inode(fs, ino, &inode); |
| 691 | if (retval) |
| 692 | return retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 693 | if (!LINUX_S_ISDIR(inode.i_mode)) |
Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 694 | return EXT2_ET_NO_DIRECTORY; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |