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
17 * Return the group # of a block
19 dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk)
21 return (blk - fs->super->s_first_data_block) /
22 fs->super->s_blocks_per_group;
26 * Return the first block (inclusive) in a group
28 blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group)
30 return fs->super->s_first_data_block +
31 ((blk64_t)group * fs->super->s_blocks_per_group);
35 * Return the last block (inclusive) in a group
37 blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group)
39 return (group == fs->group_desc_count - 1 ?
40 ext2fs_blocks_count(fs->super) - 1 :
41 ext2fs_group_first_block2(fs, group) +
42 (fs->super->s_blocks_per_group - 1));
46 * Return the inode data block count
48 blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
49 struct ext2_inode *inode)
51 return (inode->i_blocks |
52 ((fs->super->s_feature_ro_compat &
53 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ?
54 (__u64) inode->osd2.linux2.l_i_blocks_hi << 32 : 0)) -
55 (inode->i_file_acl ? fs->blocksize >> 9 : 0);
59 * Return the inode i_blocks count
61 blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
62 struct ext2_inode *inode)
64 return (inode->i_blocks |
65 ((fs->super->s_feature_ro_compat &
66 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ?
67 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
71 * Return the fs block count
73 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
75 return super->s_blocks_count |
76 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
77 (__u64) super->s_blocks_count_hi << 32 : 0);
81 * Set the fs block count
83 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
85 super->s_blocks_count = blk;
86 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
87 super->s_blocks_count_hi = (__u64) blk >> 32;
91 * Add to the current fs block count
93 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
96 tmp = ext2fs_blocks_count(super) + blk;
97 ext2fs_blocks_count_set(super, tmp);
101 * Return the fs reserved block count
103 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
105 return super->s_r_blocks_count |
106 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
107 (__u64) super->s_r_blocks_count_hi << 32 : 0);
111 * Set the fs reserved block count
113 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
115 super->s_r_blocks_count = blk;
116 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
117 super->s_r_blocks_count_hi = (__u64) blk >> 32;
121 * Add to the current reserved fs block count
123 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
126 tmp = ext2fs_r_blocks_count(super) + blk;
127 ext2fs_r_blocks_count_set(super, tmp);
131 * Return the fs free block count
133 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
135 return super->s_free_blocks_count |
136 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
137 (__u64) super->s_free_blocks_hi << 32 : 0);
141 * Set the fs free block count
143 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
145 super->s_free_blocks_count = blk;
146 if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
147 super->s_free_blocks_hi = (__u64) blk >> 32;
151 * Add to the current free fs block count
153 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
156 tmp = ext2fs_free_blocks_count(super) + blk;
157 ext2fs_free_blocks_count_set(super, tmp);
161 * Get a pointer to a block group descriptor. We need the explicit
162 * pointer to the group desc for code that swaps block group
163 * descriptors before writing them out, as it wants to make a copy and
166 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
167 struct opaque_ext2_group_desc *gdp,
170 if (fs->super->s_desc_size >= EXT2_MIN_DESC_SIZE_64BIT)
171 return (struct ext2_group_desc *)
172 ((struct ext4_group_desc *) gdp + group);
174 return (struct ext2_group_desc *) gdp + group;
177 /* Do the same but as an ext4 group desc for internal use here */
178 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
179 struct opaque_ext2_group_desc *gdp,
182 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
186 * Return the block bitmap block of a group
188 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
190 struct ext4_group_desc *gdp;
192 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
193 return gdp->bg_block_bitmap |
194 (fs->super->s_feature_incompat
195 & EXT4_FEATURE_INCOMPAT_64BIT ?
196 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
200 * Set the block bitmap block of a group
202 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
204 struct ext4_group_desc *gdp;
206 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
207 gdp->bg_block_bitmap = blk;
208 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
209 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
213 * Return the inode bitmap block of a group
215 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
217 struct ext4_group_desc *gdp;
219 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
220 return gdp->bg_inode_bitmap |
221 (fs->super->s_feature_incompat
222 & EXT4_FEATURE_INCOMPAT_64BIT ?
223 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
227 * Set the inode bitmap block of a group
229 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
231 struct ext4_group_desc *gdp;
233 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
234 gdp->bg_inode_bitmap = blk;
235 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
236 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
240 * Return the inode table block of a group
242 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
244 struct ext4_group_desc *gdp;
246 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
247 return gdp->bg_inode_table |
248 (fs->super->s_feature_incompat
249 & EXT4_FEATURE_INCOMPAT_64BIT ?
250 (__u64) gdp->bg_inode_table_hi << 32 : 0);
254 * Set the inode table block of a group
256 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
258 struct ext4_group_desc *gdp;
260 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
261 gdp->bg_inode_table = blk;
262 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
263 gdp->bg_inode_table_hi = (__u64) blk >> 32;
267 * Return the free blocks count of a group
269 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
271 struct ext4_group_desc *gdp;
273 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
274 return gdp->bg_free_blocks_count |
275 (fs->super->s_feature_incompat
276 & EXT4_FEATURE_INCOMPAT_64BIT ?
277 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
281 * Set the free blocks count of a group
283 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
285 struct ext4_group_desc *gdp;
287 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
288 gdp->bg_free_blocks_count = n;
290 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
291 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
295 * Return the free inodes count of a group
297 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
299 struct ext4_group_desc *gdp;
301 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
302 return gdp->bg_free_inodes_count |
303 (fs->super->s_feature_incompat
304 & EXT4_FEATURE_INCOMPAT_64BIT ?
305 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
309 * Set the free inodes count of a group
311 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
313 struct ext4_group_desc *gdp;
315 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
316 gdp->bg_free_inodes_count = n;
317 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
318 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
322 * Return the used dirs count of a group
324 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
326 struct ext4_group_desc *gdp;
328 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
329 return gdp->bg_used_dirs_count |
330 (fs->super->s_feature_incompat
331 & EXT4_FEATURE_INCOMPAT_64BIT ?
332 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
336 * Set the used dirs count of a group
338 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
340 struct ext4_group_desc *gdp;
342 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
343 gdp->bg_used_dirs_count = n;
344 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
345 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
349 * Return the unused inodes count of a group
351 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
353 struct ext4_group_desc *gdp;
355 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
356 return gdp->bg_itable_unused |
357 (fs->super->s_feature_incompat
358 & EXT4_FEATURE_INCOMPAT_64BIT ?
359 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
363 * Set the unused inodes count of a group
365 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
367 struct ext4_group_desc *gdp;
369 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
370 gdp->bg_itable_unused = n;
371 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
372 gdp->bg_itable_unused_hi = (__u32) n >> 16;
376 * Get the flags for this block group
378 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
380 struct ext4_group_desc *gdp;
382 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
383 return gdp->bg_flags;
387 * Zero out the flags for this block group
389 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
391 struct ext4_group_desc *gdp;
393 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
399 * Get the value of a particular flag for this block group
401 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
403 struct ext4_group_desc *gdp;
405 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
406 return gdp->bg_flags & bg_flag;
410 * Set a flag or set of flags for this block group
412 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
414 struct ext4_group_desc *gdp;
416 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
417 gdp->bg_flags |= bg_flags;
422 * Clear a flag or set of flags for this block group
424 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
426 struct ext4_group_desc *gdp;
428 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
429 gdp->bg_flags &= ~bg_flags;
434 * Get the checksum for this block group
436 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
438 struct ext4_group_desc *gdp;
440 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
441 return gdp->bg_checksum;
445 * Set the checksum for this block group to a previously calculated value
447 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
449 struct ext4_group_desc *gdp;
451 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
452 gdp->bg_checksum = checksum;
457 * Get the acl block of a file
459 * XXX Ignoring 64-bit file system flag - most places where this is
460 * called don't have access to the fs struct, and the high bits should
461 * be 0 in the non-64-bit case anyway.
463 blk64_t ext2fs_file_acl_block(const struct ext2_inode *inode)
465 return (inode->i_file_acl |
466 (__u64) inode->osd2.linux2.l_i_file_acl_high << 32);
470 * Set the acl block of a file
472 void ext2fs_file_acl_block_set(struct ext2_inode *inode, blk64_t blk)
474 inode->i_file_acl = blk;
475 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;