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 block of a group
205 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
207 struct ext4_group_desc *gdp;
209 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
210 return gdp->bg_block_bitmap |
211 (fs->super->s_feature_incompat
212 & EXT4_FEATURE_INCOMPAT_64BIT ?
213 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
217 * Set the block bitmap block of a group
219 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
221 struct ext4_group_desc *gdp;
223 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
224 gdp->bg_block_bitmap = blk;
225 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
226 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
230 * Return the inode bitmap block of a group
232 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
234 struct ext4_group_desc *gdp;
236 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
237 return gdp->bg_inode_bitmap |
238 (fs->super->s_feature_incompat
239 & EXT4_FEATURE_INCOMPAT_64BIT ?
240 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
244 * Set the inode bitmap block of a group
246 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
248 struct ext4_group_desc *gdp;
250 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
251 gdp->bg_inode_bitmap = blk;
252 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
253 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
257 * Return the inode table block of a group
259 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
261 struct ext4_group_desc *gdp;
263 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
264 return gdp->bg_inode_table |
265 (fs->super->s_feature_incompat
266 & EXT4_FEATURE_INCOMPAT_64BIT ?
267 (__u64) gdp->bg_inode_table_hi << 32 : 0);
271 * Set the inode table block of a group
273 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
275 struct ext4_group_desc *gdp;
277 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
278 gdp->bg_inode_table = blk;
279 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
280 gdp->bg_inode_table_hi = (__u64) blk >> 32;
284 * Return the free blocks count of a group
286 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
288 struct ext4_group_desc *gdp;
290 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
291 return gdp->bg_free_blocks_count |
292 (fs->super->s_feature_incompat
293 & EXT4_FEATURE_INCOMPAT_64BIT ?
294 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
298 * Set the free blocks count of a group
300 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
302 struct ext4_group_desc *gdp;
304 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
305 gdp->bg_free_blocks_count = n;
307 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
308 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
312 * Return the free inodes count of a group
314 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
316 struct ext4_group_desc *gdp;
318 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
319 return gdp->bg_free_inodes_count |
320 (fs->super->s_feature_incompat
321 & EXT4_FEATURE_INCOMPAT_64BIT ?
322 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
326 * Set the free inodes count of a group
328 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
330 struct ext4_group_desc *gdp;
332 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
333 gdp->bg_free_inodes_count = n;
334 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
335 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
339 * Return the used dirs count of a group
341 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
343 struct ext4_group_desc *gdp;
345 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
346 return gdp->bg_used_dirs_count |
347 (fs->super->s_feature_incompat
348 & EXT4_FEATURE_INCOMPAT_64BIT ?
349 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
353 * Set the used dirs count of a group
355 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
357 struct ext4_group_desc *gdp;
359 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
360 gdp->bg_used_dirs_count = n;
361 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
362 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
366 * Return the unused inodes count of a group
368 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
370 struct ext4_group_desc *gdp;
372 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
373 return gdp->bg_itable_unused |
374 (fs->super->s_feature_incompat
375 & EXT4_FEATURE_INCOMPAT_64BIT ?
376 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
380 * Set the unused inodes count of a group
382 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
384 struct ext4_group_desc *gdp;
386 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
387 gdp->bg_itable_unused = n;
388 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
389 gdp->bg_itable_unused_hi = (__u32) n >> 16;
393 * Get the flags for this block group
395 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
397 struct ext4_group_desc *gdp;
399 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
400 return gdp->bg_flags;
404 * Zero out the flags for this block group
406 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
408 struct ext4_group_desc *gdp;
410 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
416 * Get the value of a particular flag for this block group
418 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
420 struct ext4_group_desc *gdp;
422 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
423 return gdp->bg_flags & bg_flag;
427 * Set a flag or set of flags for this block group
429 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
431 struct ext4_group_desc *gdp;
433 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
434 gdp->bg_flags |= bg_flags;
439 * Clear a flag or set of flags for this block group
441 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
443 struct ext4_group_desc *gdp;
445 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
446 gdp->bg_flags &= ~bg_flags;
451 * Get the checksum for this block group
453 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
455 struct ext4_group_desc *gdp;
457 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
458 return gdp->bg_checksum;
462 * Set the checksum for this block group to a previously calculated value
464 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
466 struct ext4_group_desc *gdp;
468 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
469 gdp->bg_checksum = checksum;
474 * Get the acl block of a file
476 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
478 blk64_t blk = inode->i_file_acl;
480 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
481 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
486 * Set the acl block of a file
488 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
491 inode->i_file_acl = blk;
492 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
493 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
497 * Set the size of the inode
499 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
502 /* Only regular files get to be larger than 4GB */
503 if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
504 return EXT2_ET_FILE_TOO_BIG;
506 /* If we're writing a large file, set the large_file flag */
507 if (LINUX_S_ISREG(inode->i_mode) &&
508 ext2fs_needs_large_file_feature(size) &&
509 (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
510 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
511 fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
512 fs->super->s_feature_ro_compat |=
513 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
514 ext2fs_update_dynamic_rev(fs);
515 ext2fs_mark_super_dirty(fs);
518 inode->i_size = size & 0xffffffff;
519 inode->i_size_high = (size >> 32);