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 ? fs->blocksize >> 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 fs block count
91 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
93 return super->s_blocks_count |
94 (ext2fs_has_feature_64bit(super) ?
95 (__u64) super->s_blocks_count_hi << 32 : 0);
99 * Set the fs block count
101 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
103 super->s_blocks_count = blk;
104 if (ext2fs_has_feature_64bit(super))
105 super->s_blocks_count_hi = (__u64) blk >> 32;
109 * Add to the current fs block count
111 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
114 tmp = ext2fs_blocks_count(super) + blk;
115 ext2fs_blocks_count_set(super, tmp);
119 * Return the fs reserved block count
121 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
123 return super->s_r_blocks_count |
124 (ext2fs_has_feature_64bit(super) ?
125 (__u64) super->s_r_blocks_count_hi << 32 : 0);
129 * Set the fs reserved block count
131 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
133 super->s_r_blocks_count = blk;
134 if (ext2fs_has_feature_64bit(super))
135 super->s_r_blocks_count_hi = (__u64) blk >> 32;
139 * Add to the current reserved fs block count
141 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
144 tmp = ext2fs_r_blocks_count(super) + blk;
145 ext2fs_r_blocks_count_set(super, tmp);
149 * Return the fs free block count
151 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
153 return super->s_free_blocks_count |
154 (ext2fs_has_feature_64bit(super) ?
155 (__u64) super->s_free_blocks_hi << 32 : 0);
159 * Set the fs free block count
161 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
163 super->s_free_blocks_count = blk;
164 if (ext2fs_has_feature_64bit(super))
165 super->s_free_blocks_hi = (__u64) blk >> 32;
169 * Add to the current free fs block count
171 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
174 tmp = ext2fs_free_blocks_count(super) + blk;
175 ext2fs_free_blocks_count_set(super, tmp);
179 * Get a pointer to a block group descriptor. We need the explicit
180 * pointer to the group desc for code that swaps block group
181 * descriptors before writing them out, as it wants to make a copy and
184 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
185 struct opaque_ext2_group_desc *gdp,
188 return (struct ext2_group_desc *)((char *)gdp +
189 group * EXT2_DESC_SIZE(fs->super));
192 /* Do the same but as an ext4 group desc for internal use here */
193 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
194 struct opaque_ext2_group_desc *gdp,
197 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
201 * Return the block bitmap checksum of a group
203 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
205 struct ext4_group_desc *gdp;
208 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
209 csum = gdp->bg_block_bitmap_csum_lo;
210 if (fs->super->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
211 csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
216 * Return the block bitmap block of a group
218 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
220 struct ext4_group_desc *gdp;
222 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
223 return gdp->bg_block_bitmap |
224 (ext2fs_has_feature_64bit(fs->super) ?
225 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
229 * Set the block bitmap block of a group
231 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
233 struct ext4_group_desc *gdp;
235 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
236 gdp->bg_block_bitmap = blk;
237 if (ext2fs_has_feature_64bit(fs->super))
238 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
242 * Return the inode bitmap checksum of a group
244 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
246 struct ext4_group_desc *gdp;
249 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
250 csum = gdp->bg_inode_bitmap_csum_lo;
251 if (fs->super->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
252 csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
257 * Return the inode bitmap block of a group
259 blk64_t ext2fs_inode_bitmap_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_bitmap |
265 (ext2fs_has_feature_64bit(fs->super) ?
266 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
270 * Set the inode bitmap block of a group
272 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
274 struct ext4_group_desc *gdp;
276 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
277 gdp->bg_inode_bitmap = blk;
278 if (ext2fs_has_feature_64bit(fs->super))
279 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
283 * Return the inode table block of a group
285 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
287 struct ext4_group_desc *gdp;
289 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
290 return gdp->bg_inode_table |
291 (ext2fs_has_feature_64bit(fs->super) ?
292 (__u64) gdp->bg_inode_table_hi << 32 : 0);
296 * Set the inode table block of a group
298 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
300 struct ext4_group_desc *gdp;
302 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
303 gdp->bg_inode_table = blk;
304 if (ext2fs_has_feature_64bit(fs->super))
305 gdp->bg_inode_table_hi = (__u64) blk >> 32;
309 * Return the free blocks count of a group
311 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
313 struct ext4_group_desc *gdp;
315 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
316 return gdp->bg_free_blocks_count |
317 (ext2fs_has_feature_64bit(fs->super) ?
318 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
322 * Set the free blocks count of a group
324 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
326 struct ext4_group_desc *gdp;
328 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
329 gdp->bg_free_blocks_count = n;
331 if (ext2fs_has_feature_64bit(fs->super))
332 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
336 * Return the free inodes count of a group
338 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
340 struct ext4_group_desc *gdp;
342 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
343 return gdp->bg_free_inodes_count |
344 (ext2fs_has_feature_64bit(fs->super) ?
345 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
349 * Set the free inodes count of a group
351 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
353 struct ext4_group_desc *gdp;
355 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
356 gdp->bg_free_inodes_count = n;
357 if (ext2fs_has_feature_64bit(fs->super))
358 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
362 * Return the used dirs count of a group
364 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
366 struct ext4_group_desc *gdp;
368 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
369 return gdp->bg_used_dirs_count |
370 (ext2fs_has_feature_64bit(fs->super) ?
371 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
375 * Set the used dirs count of a group
377 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
379 struct ext4_group_desc *gdp;
381 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
382 gdp->bg_used_dirs_count = n;
383 if (ext2fs_has_feature_64bit(fs->super))
384 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
388 * Return the unused inodes count of a group
390 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
392 struct ext4_group_desc *gdp;
394 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
395 return gdp->bg_itable_unused |
396 (ext2fs_has_feature_64bit(fs->super) ?
397 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
401 * Set the unused inodes count of a group
403 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
405 struct ext4_group_desc *gdp;
407 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
408 gdp->bg_itable_unused = n;
409 if (ext2fs_has_feature_64bit(fs->super))
410 gdp->bg_itable_unused_hi = (__u32) n >> 16;
414 * Get the flags for this block group
416 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
418 struct ext4_group_desc *gdp;
420 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
421 return gdp->bg_flags;
425 * Zero out the flags for this block group
427 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
429 struct ext4_group_desc *gdp;
431 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
437 * Get the value of a particular flag for this block group
439 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
441 struct ext4_group_desc *gdp;
443 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
444 return gdp->bg_flags & bg_flag;
448 * Set a flag or set of flags for this block group
450 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
452 struct ext4_group_desc *gdp;
454 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
455 gdp->bg_flags |= bg_flags;
460 * Clear a flag or set of flags for this block group
462 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
464 struct ext4_group_desc *gdp;
466 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
467 gdp->bg_flags &= ~bg_flags;
472 * Get the checksum for this block group
474 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
476 struct ext4_group_desc *gdp;
478 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
479 return gdp->bg_checksum;
483 * Set the checksum for this block group to a previously calculated value
485 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
487 struct ext4_group_desc *gdp;
489 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
490 gdp->bg_checksum = checksum;
495 * Get the acl block of a file
497 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
499 blk64_t blk = inode->i_file_acl;
501 if (fs && ext2fs_has_feature_64bit(fs->super))
502 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
507 * Set the acl block of a file
509 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
512 inode->i_file_acl = blk;
513 if (fs && ext2fs_has_feature_64bit(fs->super))
514 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
518 * Set the size of the inode
520 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
523 /* Only regular files get to be larger than 4GB */
524 if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
525 return EXT2_ET_FILE_TOO_BIG;
527 /* If we're writing a large file, set the large_file flag */
528 if (LINUX_S_ISREG(inode->i_mode) &&
529 ext2fs_needs_large_file_feature(size) &&
530 (!ext2fs_has_feature_large_file(fs->super) ||
531 fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
532 ext2fs_set_feature_large_file(fs->super);
533 ext2fs_update_dynamic_rev(fs);
534 ext2fs_mark_super_dirty(fs);
537 inode->i_size = size & 0xffffffff;
538 inode->i_size_high = (size >> 32);