2 * blknum.c --- Functions to handle blk64_t and high/low 64-bit block
5 * Copyright IBM Corporation, 2007
6 * Author Jose R. Santos <jrs@us.ibm.com>
9 * This file may be redistributed under the terms of the GNU Public
18 * Return the group # of a block
20 dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk)
22 return (blk - fs->super->s_first_data_block) /
23 fs->super->s_blocks_per_group;
27 * Return the first block (inclusive) in a group
29 blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group)
31 return fs->super->s_first_data_block +
32 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
36 * Return the last block (inclusive) in a group
38 blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group)
40 return (group == fs->group_desc_count - 1 ?
41 ext2fs_blocks_count(fs->super) - 1 :
42 ext2fs_group_first_block2(fs, group) +
43 (fs->super->s_blocks_per_group - 1));
47 * Return the number of blocks in a group
49 int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group)
53 if (group == fs->group_desc_count - 1) {
54 num_blocks = (ext2fs_blocks_count(fs->super) -
55 fs->super->s_first_data_block) %
56 fs->super->s_blocks_per_group;
58 num_blocks = fs->super->s_blocks_per_group;
60 num_blocks = fs->super->s_blocks_per_group;
66 * Return the inode data block count
68 blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
69 struct ext2_inode *inode)
71 return (inode->i_blocks |
72 (ext2fs_has_feature_huge_file(fs->super) ?
73 (__u64) inode->osd2.linux2.l_i_blocks_hi << 32 : 0)) -
74 (inode->i_file_acl ? EXT2_CLUSTER_SIZE(fs->super) >> 9 : 0);
78 * Return the inode i_blocks count
80 blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
81 struct ext2_inode *inode)
83 return (inode->i_blocks |
84 (ext2fs_has_feature_huge_file(fs->super) ?
85 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
89 * Return the inode i_blocks in stat (512 byte) units
91 blk64_t ext2fs_get_stat_i_blocks(ext2_filsys fs,
92 struct ext2_inode *inode)
94 blk64_t ret = inode->i_blocks;
96 if (ext2fs_has_feature_huge_file(fs->super)) {
97 ret += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
98 if (inode->i_flags & EXT4_HUGE_FILE_FL)
99 ret *= (fs->blocksize / 512);
105 * Return the fs block count
107 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
109 return super->s_blocks_count |
110 (ext2fs_has_feature_64bit(super) ?
111 (__u64) super->s_blocks_count_hi << 32 : 0);
115 * Set the fs block count
117 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
119 super->s_blocks_count = blk;
120 if (ext2fs_has_feature_64bit(super))
121 super->s_blocks_count_hi = (__u64) blk >> 32;
125 * Add to the current fs block count
127 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
130 tmp = ext2fs_blocks_count(super) + blk;
131 ext2fs_blocks_count_set(super, tmp);
135 * Return the fs reserved block count
137 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
139 return super->s_r_blocks_count |
140 (ext2fs_has_feature_64bit(super) ?
141 (__u64) super->s_r_blocks_count_hi << 32 : 0);
145 * Set the fs reserved block count
147 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
149 super->s_r_blocks_count = blk;
150 if (ext2fs_has_feature_64bit(super))
151 super->s_r_blocks_count_hi = (__u64) blk >> 32;
155 * Add to the current reserved fs block count
157 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
160 tmp = ext2fs_r_blocks_count(super) + blk;
161 ext2fs_r_blocks_count_set(super, tmp);
165 * Return the fs free block count
167 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
169 return super->s_free_blocks_count |
170 (ext2fs_has_feature_64bit(super) ?
171 (__u64) super->s_free_blocks_hi << 32 : 0);
175 * Set the fs free block count
177 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
179 super->s_free_blocks_count = blk;
180 if (ext2fs_has_feature_64bit(super))
181 super->s_free_blocks_hi = (__u64) blk >> 32;
185 * Add to the current free fs block count
187 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
190 tmp = ext2fs_free_blocks_count(super) + blk;
191 ext2fs_free_blocks_count_set(super, tmp);
195 * Get a pointer to a block group descriptor. We need the explicit
196 * pointer to the group desc for code that swaps block group
197 * descriptors before writing them out, as it wants to make a copy and
200 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
201 struct opaque_ext2_group_desc *gdp,
204 int desc_size = EXT2_DESC_SIZE(fs->super) & ~7;
206 return (struct ext2_group_desc *)((char *)gdp + group * desc_size);
209 /* Do the same but as an ext4 group desc for internal use here */
210 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
211 struct opaque_ext2_group_desc *gdp,
214 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
218 * Return the block bitmap checksum of a group
220 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
222 struct ext4_group_desc *gdp;
225 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
226 csum = gdp->bg_block_bitmap_csum_lo;
227 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
228 csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
233 * Return the block bitmap block of a group
235 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
237 struct ext4_group_desc *gdp;
239 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
240 return gdp->bg_block_bitmap |
241 (ext2fs_has_feature_64bit(fs->super) ?
242 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
246 * Set the block bitmap block of a group
248 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
250 struct ext4_group_desc *gdp;
252 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
253 gdp->bg_block_bitmap = blk;
254 if (ext2fs_has_feature_64bit(fs->super))
255 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
259 * Return the inode bitmap checksum of a group
261 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
263 struct ext4_group_desc *gdp;
266 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
267 csum = gdp->bg_inode_bitmap_csum_lo;
268 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
269 csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
274 * Return the inode bitmap block of a group
276 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
278 struct ext4_group_desc *gdp;
280 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
281 return gdp->bg_inode_bitmap |
282 (ext2fs_has_feature_64bit(fs->super) ?
283 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
287 * Set the inode bitmap block of a group
289 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
291 struct ext4_group_desc *gdp;
293 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
294 gdp->bg_inode_bitmap = blk;
295 if (ext2fs_has_feature_64bit(fs->super))
296 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
300 * Return the inode table block of a group
302 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
304 struct ext4_group_desc *gdp;
306 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
307 return gdp->bg_inode_table |
308 (ext2fs_has_feature_64bit(fs->super) ?
309 (__u64) gdp->bg_inode_table_hi << 32 : 0);
313 * Set the inode table block of a group
315 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
317 struct ext4_group_desc *gdp;
319 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
320 gdp->bg_inode_table = blk;
321 if (ext2fs_has_feature_64bit(fs->super))
322 gdp->bg_inode_table_hi = (__u64) blk >> 32;
326 * Return the free blocks count of a group
328 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
330 struct ext4_group_desc *gdp;
332 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
333 return gdp->bg_free_blocks_count |
334 (ext2fs_has_feature_64bit(fs->super) ?
335 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
339 * Set the free blocks count of a group
341 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
343 struct ext4_group_desc *gdp;
345 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
346 gdp->bg_free_blocks_count = n;
348 if (ext2fs_has_feature_64bit(fs->super))
349 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
353 * Return the free inodes count of a group
355 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
357 struct ext4_group_desc *gdp;
359 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
360 return gdp->bg_free_inodes_count |
361 (ext2fs_has_feature_64bit(fs->super) ?
362 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
366 * Set the free inodes count of a group
368 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
370 struct ext4_group_desc *gdp;
372 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
373 gdp->bg_free_inodes_count = n;
374 if (ext2fs_has_feature_64bit(fs->super))
375 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
379 * Return the used dirs count of a group
381 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
383 struct ext4_group_desc *gdp;
385 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
386 return gdp->bg_used_dirs_count |
387 (ext2fs_has_feature_64bit(fs->super) ?
388 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
392 * Set the used dirs count of a group
394 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
396 struct ext4_group_desc *gdp;
398 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
399 gdp->bg_used_dirs_count = n;
400 if (ext2fs_has_feature_64bit(fs->super))
401 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
405 * Return the unused inodes count of a group
407 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
409 struct ext4_group_desc *gdp;
411 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
412 return gdp->bg_itable_unused |
413 (ext2fs_has_feature_64bit(fs->super) ?
414 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
418 * Set the unused inodes count of a group
420 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
422 struct ext4_group_desc *gdp;
424 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
425 gdp->bg_itable_unused = n;
426 if (ext2fs_has_feature_64bit(fs->super))
427 gdp->bg_itable_unused_hi = (__u32) n >> 16;
431 * Get the flags for this block group
433 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
435 struct ext4_group_desc *gdp;
437 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
438 return gdp->bg_flags;
442 * Zero out the flags for this block group
444 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
446 struct ext4_group_desc *gdp;
448 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
454 * Get the value of a particular flag for this block group
456 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
458 struct ext4_group_desc *gdp;
460 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
461 return gdp->bg_flags & bg_flag;
465 * Set a flag or set of flags for this block group
467 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
469 struct ext4_group_desc *gdp;
471 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
472 gdp->bg_flags |= bg_flags;
477 * Clear a flag or set of flags for this block group
479 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
481 struct ext4_group_desc *gdp;
483 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
484 gdp->bg_flags &= ~bg_flags;
489 * Get the checksum for this block group
491 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
493 struct ext4_group_desc *gdp;
495 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
496 return gdp->bg_checksum;
500 * Set the checksum for this block group to a previously calculated value
502 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
504 struct ext4_group_desc *gdp;
506 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
507 gdp->bg_checksum = checksum;
512 * Get the acl block of a file
514 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
516 blk64_t blk = inode->i_file_acl;
518 if (fs && ext2fs_has_feature_64bit(fs->super))
519 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
524 * Set the acl block of a file
526 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
529 inode->i_file_acl = blk;
530 if (fs && ext2fs_has_feature_64bit(fs->super))
531 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
535 * Set the size of the inode
537 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
540 /* Only regular files get to be larger than 4GB */
541 if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
542 return EXT2_ET_FILE_TOO_BIG;
544 /* If we're writing a large file, set the large_file flag */
545 if (LINUX_S_ISREG(inode->i_mode) &&
546 ext2fs_needs_large_file_feature(size) &&
547 (!ext2fs_has_feature_large_file(fs->super) ||
548 fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
549 ext2fs_set_feature_large_file(fs->super);
550 ext2fs_update_dynamic_rev(fs);
551 ext2fs_mark_super_dirty(fs);
554 inode->i_size = size & 0xffffffff;
555 inode->i_size_high = (size >> 32);