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 ((blk64_t)group * fs->super->s_blocks_per_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 if (fs->super->s_desc_size >= EXT2_MIN_DESC_SIZE_64BIT)
191 return (struct ext2_group_desc *)
192 ((struct ext4_group_desc *) gdp + group);
194 return (struct ext2_group_desc *) gdp + group;
197 /* Do the same but as an ext4 group desc for internal use here */
198 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
199 struct opaque_ext2_group_desc *gdp,
202 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
206 * Return the block bitmap block of a group
208 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
210 struct ext4_group_desc *gdp;
212 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
213 return gdp->bg_block_bitmap |
214 (fs->super->s_feature_incompat
215 & EXT4_FEATURE_INCOMPAT_64BIT ?
216 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
220 * Set the block bitmap block of a group
222 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
224 struct ext4_group_desc *gdp;
226 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
227 gdp->bg_block_bitmap = blk;
228 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
229 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
233 * Return the inode bitmap block of a group
235 blk64_t ext2fs_inode_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_inode_bitmap |
241 (fs->super->s_feature_incompat
242 & EXT4_FEATURE_INCOMPAT_64BIT ?
243 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
247 * Set the inode bitmap block of a group
249 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
251 struct ext4_group_desc *gdp;
253 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
254 gdp->bg_inode_bitmap = blk;
255 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
256 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
260 * Return the inode table block of a group
262 blk64_t ext2fs_inode_table_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_table |
268 (fs->super->s_feature_incompat
269 & EXT4_FEATURE_INCOMPAT_64BIT ?
270 (__u64) gdp->bg_inode_table_hi << 32 : 0);
274 * Set the inode table block of a group
276 void ext2fs_inode_table_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_table = blk;
282 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
283 gdp->bg_inode_table_hi = (__u64) blk >> 32;
287 * Return the free blocks count of a group
289 __u32 ext2fs_bg_free_blocks_count(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_free_blocks_count |
295 (fs->super->s_feature_incompat
296 & EXT4_FEATURE_INCOMPAT_64BIT ?
297 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
301 * Set the free blocks count of a group
303 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
305 struct ext4_group_desc *gdp;
307 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
308 gdp->bg_free_blocks_count = n;
310 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
311 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
315 * Return the free inodes count of a group
317 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
319 struct ext4_group_desc *gdp;
321 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
322 return gdp->bg_free_inodes_count |
323 (fs->super->s_feature_incompat
324 & EXT4_FEATURE_INCOMPAT_64BIT ?
325 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
329 * Set the free inodes count of a group
331 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
333 struct ext4_group_desc *gdp;
335 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
336 gdp->bg_free_inodes_count = n;
337 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
338 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
342 * Return the used dirs count of a group
344 __u32 ext2fs_bg_used_dirs_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_used_dirs_count |
350 (fs->super->s_feature_incompat
351 & EXT4_FEATURE_INCOMPAT_64BIT ?
352 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
356 * Set the used dirs count of a group
358 void ext2fs_bg_used_dirs_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_used_dirs_count = n;
364 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
365 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
369 * Return the unused inodes count of a group
371 __u32 ext2fs_bg_itable_unused(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_itable_unused |
377 (fs->super->s_feature_incompat
378 & EXT4_FEATURE_INCOMPAT_64BIT ?
379 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
383 * Set the unused inodes count of a group
385 void ext2fs_bg_itable_unused_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_itable_unused = n;
391 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
392 gdp->bg_itable_unused_hi = (__u32) n >> 16;
396 * Get the flags for this block group
398 __u16 ext2fs_bg_flags(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_flags;
407 * Zero out the flags for this block group
409 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
411 struct ext4_group_desc *gdp;
413 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
419 * Get the value of a particular flag for this block group
421 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
423 struct ext4_group_desc *gdp;
425 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
426 return gdp->bg_flags & bg_flag;
430 * Set a flag or set of flags for this block group
432 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
434 struct ext4_group_desc *gdp;
436 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
437 gdp->bg_flags |= bg_flags;
442 * Clear a flag or set of flags for this block group
444 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
446 struct ext4_group_desc *gdp;
448 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
449 gdp->bg_flags &= ~bg_flags;
454 * Get the checksum for this block group
456 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
458 struct ext4_group_desc *gdp;
460 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
461 return gdp->bg_checksum;
465 * Set the checksum for this block group to a previously calculated value
467 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
469 struct ext4_group_desc *gdp;
471 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
472 gdp->bg_checksum = checksum;
477 * Get the acl block of a file
479 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
481 blk64_t blk = inode->i_file_acl;
483 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
484 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
489 * Set the acl block of a file
491 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
494 inode->i_file_acl = blk;
495 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
496 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;