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 ((fs->super->s_feature_ro_compat &
73 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ?
74 (__u64) inode->osd2.linux2.l_i_blocks_hi << 32 : 0)) -
75 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
79 * Return the inode i_blocks count
81 blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
82 struct ext2_inode *inode)
84 return (inode->i_blocks |
85 ((fs->super->s_feature_ro_compat &
86 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ?
87 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
91 * Return the fs block count
93 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
95 return super->s_blocks_count |
96 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
97 (__u64) super->s_blocks_count_hi << 32 : 0);
101 * Set the fs block count
103 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
105 super->s_blocks_count = blk;
106 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
107 super->s_blocks_count_hi = (__u64) blk >> 32;
111 * Add to the current fs block count
113 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
116 tmp = ext2fs_blocks_count(super) + blk;
117 ext2fs_blocks_count_set(super, tmp);
121 * Return the fs reserved block count
123 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
125 return super->s_r_blocks_count |
126 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
127 (__u64) super->s_r_blocks_count_hi << 32 : 0);
131 * Set the fs reserved block count
133 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
135 super->s_r_blocks_count = blk;
136 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
137 super->s_r_blocks_count_hi = (__u64) blk >> 32;
141 * Add to the current reserved fs block count
143 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
146 tmp = ext2fs_r_blocks_count(super) + blk;
147 ext2fs_r_blocks_count_set(super, tmp);
151 * Return the fs free block count
153 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
155 return super->s_free_blocks_count |
156 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
157 (__u64) super->s_free_blocks_hi << 32 : 0);
161 * Set the fs free block count
163 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
165 super->s_free_blocks_count = blk;
166 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
167 super->s_free_blocks_hi = (__u64) blk >> 32;
171 * Add to the current free fs block count
173 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
176 tmp = ext2fs_free_blocks_count(super) + blk;
177 ext2fs_free_blocks_count_set(super, tmp);
181 * Get a pointer to a block group descriptor. We need the explicit
182 * pointer to the group desc for code that swaps block group
183 * descriptors before writing them out, as it wants to make a copy and
186 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
187 struct opaque_ext2_group_desc *gdp,
190 return (struct ext2_group_desc *)((char *)gdp +
191 group * EXT2_DESC_SIZE(fs->super));
194 /* Do the same but as an ext4 group desc for internal use here */
195 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
196 struct opaque_ext2_group_desc *gdp,
199 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
203 * Return the block bitmap checksum of a group
205 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
207 struct ext4_group_desc *gdp;
210 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
211 csum = gdp->bg_block_bitmap_csum_lo;
212 if (fs->super->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
213 csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
218 * Return the block bitmap block of a group
220 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
222 struct ext4_group_desc *gdp;
224 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
225 return gdp->bg_block_bitmap |
226 (fs->super->s_feature_incompat
227 & EXT4_FEATURE_INCOMPAT_64BIT ?
228 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
232 * Set the block bitmap block of a group
234 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
236 struct ext4_group_desc *gdp;
238 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
239 gdp->bg_block_bitmap = blk;
240 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
241 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
245 * Return the inode bitmap checksum of a group
247 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
249 struct ext4_group_desc *gdp;
252 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
253 csum = gdp->bg_inode_bitmap_csum_lo;
254 if (fs->super->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
255 csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
260 * Return the inode bitmap block of a group
262 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
264 struct ext4_group_desc *gdp;
266 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
267 return gdp->bg_inode_bitmap |
268 (fs->super->s_feature_incompat
269 & EXT4_FEATURE_INCOMPAT_64BIT ?
270 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
274 * Set the inode bitmap block of a group
276 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
278 struct ext4_group_desc *gdp;
280 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
281 gdp->bg_inode_bitmap = blk;
282 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
283 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
287 * Return the inode table block of a group
289 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
291 struct ext4_group_desc *gdp;
293 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
294 return gdp->bg_inode_table |
295 (fs->super->s_feature_incompat
296 & EXT4_FEATURE_INCOMPAT_64BIT ?
297 (__u64) gdp->bg_inode_table_hi << 32 : 0);
301 * Set the inode table block of a group
303 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
305 struct ext4_group_desc *gdp;
307 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
308 gdp->bg_inode_table = blk;
309 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
310 gdp->bg_inode_table_hi = (__u64) blk >> 32;
314 * Return the free blocks count of a group
316 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
318 struct ext4_group_desc *gdp;
320 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
321 return gdp->bg_free_blocks_count |
322 (fs->super->s_feature_incompat
323 & EXT4_FEATURE_INCOMPAT_64BIT ?
324 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
328 * Set the free blocks count of a group
330 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
332 struct ext4_group_desc *gdp;
334 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
335 gdp->bg_free_blocks_count = n;
337 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
338 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
342 * Return the free inodes count of a group
344 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
346 struct ext4_group_desc *gdp;
348 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
349 return gdp->bg_free_inodes_count |
350 (fs->super->s_feature_incompat
351 & EXT4_FEATURE_INCOMPAT_64BIT ?
352 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
356 * Set the free inodes count of a group
358 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
360 struct ext4_group_desc *gdp;
362 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
363 gdp->bg_free_inodes_count = n;
364 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
365 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
369 * Return the used dirs count of a group
371 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
373 struct ext4_group_desc *gdp;
375 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
376 return gdp->bg_used_dirs_count |
377 (fs->super->s_feature_incompat
378 & EXT4_FEATURE_INCOMPAT_64BIT ?
379 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
383 * Set the used dirs count of a group
385 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
387 struct ext4_group_desc *gdp;
389 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
390 gdp->bg_used_dirs_count = n;
391 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
392 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
396 * Return the unused inodes count of a group
398 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
400 struct ext4_group_desc *gdp;
402 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
403 return gdp->bg_itable_unused |
404 (fs->super->s_feature_incompat
405 & EXT4_FEATURE_INCOMPAT_64BIT ?
406 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
410 * Set the unused inodes count of a group
412 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
414 struct ext4_group_desc *gdp;
416 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
417 gdp->bg_itable_unused = n;
418 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
419 gdp->bg_itable_unused_hi = (__u32) n >> 16;
423 * Get the flags for this block group
425 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
427 struct ext4_group_desc *gdp;
429 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
430 return gdp->bg_flags;
434 * Zero out the flags for this block group
436 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
438 struct ext4_group_desc *gdp;
440 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
446 * Get the value of a particular flag for this block group
448 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
450 struct ext4_group_desc *gdp;
452 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
453 return gdp->bg_flags & bg_flag;
457 * Set a flag or set of flags for this block group
459 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
461 struct ext4_group_desc *gdp;
463 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
464 gdp->bg_flags |= bg_flags;
469 * Clear a flag or set of flags for this block group
471 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
473 struct ext4_group_desc *gdp;
475 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
476 gdp->bg_flags &= ~bg_flags;
481 * Get the checksum for this block group
483 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
485 struct ext4_group_desc *gdp;
487 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
488 return gdp->bg_checksum;
492 * Set the checksum for this block group to a previously calculated value
494 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
496 struct ext4_group_desc *gdp;
498 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
499 gdp->bg_checksum = checksum;
504 * Get the acl block of a file
506 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
508 blk64_t blk = inode->i_file_acl;
510 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
511 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
516 * Set the acl block of a file
518 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
521 inode->i_file_acl = blk;
522 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
523 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
527 * Set the size of the inode
529 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
532 /* Only regular files get to be larger than 4GB */
533 if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
534 return EXT2_ET_FILE_TOO_BIG;
536 /* If we're writing a large file, set the large_file flag */
537 if (LINUX_S_ISREG(inode->i_mode) &&
538 ext2fs_needs_large_file_feature(size) &&
539 (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
540 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
541 fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
542 fs->super->s_feature_ro_compat |=
543 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
544 ext2fs_update_dynamic_rev(fs);
545 ext2fs_mark_super_dirty(fs);
548 inode->i_size = size & 0xffffffff;
549 inode->i_size_high = (size >> 32);