1 /*
2 * blknum.c --- Functions to handle blk64_t and high/low 64-bit block
3 * number.
4 *
5 * Copyright IBM Corporation, 2007
6 * Author Jose R. Santos <jrs@us.ibm.com>
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
14 #include "config.h"
15 #include "ext2fs.h"
16
17 /*
18 * Return the group # of a block
19 */
ext2fs_group_of_blk2(ext2_filsys fs,blk64_t blk)20 dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk)
21 {
22 return (blk - fs->super->s_first_data_block) /
23 fs->super->s_blocks_per_group;
24 }
25
26 /*
27 * Return the first block (inclusive) in a group
28 */
ext2fs_group_first_block2(ext2_filsys fs,dgrp_t group)29 blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group)
30 {
31 return fs->super->s_first_data_block +
32 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
33 }
34
35 /*
36 * Return the last block (inclusive) in a group
37 */
ext2fs_group_last_block2(ext2_filsys fs,dgrp_t group)38 blk64_t ext2fs_group_last_block2(ext2_filsys fs, dgrp_t group)
39 {
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));
44 }
45
46 /*
47 * Return the number of blocks in a group
48 */
ext2fs_group_blocks_count(ext2_filsys fs,dgrp_t group)49 int ext2fs_group_blocks_count(ext2_filsys fs, dgrp_t group)
50 {
51 int num_blocks;
52
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;
57 if (!num_blocks)
58 num_blocks = fs->super->s_blocks_per_group;
59 } else
60 num_blocks = fs->super->s_blocks_per_group;
61
62 return num_blocks;
63 }
64
65 /*
66 * Return the inode data block count
67 */
ext2fs_inode_data_blocks2(ext2_filsys fs,struct ext2_inode * inode)68 blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
69 struct ext2_inode *inode)
70 {
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 ? EXT2_CLUSTER_SIZE(fs->super) >> 9 : 0);
75 }
76
77 /*
78 * Return the inode i_blocks count
79 */
ext2fs_inode_i_blocks(ext2_filsys fs,struct ext2_inode * inode)80 blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
81 struct ext2_inode *inode)
82 {
83 return (inode->i_blocks |
84 (ext2fs_has_feature_huge_file(fs->super) ?
85 (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
86 }
87
88 /*
89 * Return the inode i_blocks in stat (512 byte) units
90 */
ext2fs_get_stat_i_blocks(ext2_filsys fs,struct ext2_inode * inode)91 blk64_t ext2fs_get_stat_i_blocks(ext2_filsys fs,
92 struct ext2_inode *inode)
93 {
94 blk64_t ret = inode->i_blocks;
95
96 if (ext2fs_has_feature_huge_file(fs->super)) {
97 ret += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
98 if (inode->i_flags & EXT4_HUGE_FILE_FL)
99 ret *= (fs->blocksize / 512);
100 }
101 return ret;
102 }
103
104 /*
105 * Return the fs block count
106 */
ext2fs_blocks_count(struct ext2_super_block * super)107 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
108 {
109 return super->s_blocks_count |
110 (ext2fs_has_feature_64bit(super) ?
111 (__u64) super->s_blocks_count_hi << 32 : 0);
112 }
113
114 /*
115 * Set the fs block count
116 */
ext2fs_blocks_count_set(struct ext2_super_block * super,blk64_t blk)117 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
118 {
119 super->s_blocks_count = blk;
120 if (ext2fs_has_feature_64bit(super))
121 super->s_blocks_count_hi = (__u64) blk >> 32;
122 }
123
124 /*
125 * Add to the current fs block count
126 */
ext2fs_blocks_count_add(struct ext2_super_block * super,blk64_t blk)127 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
128 {
129 blk64_t tmp;
130 tmp = ext2fs_blocks_count(super) + blk;
131 ext2fs_blocks_count_set(super, tmp);
132 }
133
134 /*
135 * Return the fs reserved block count
136 */
ext2fs_r_blocks_count(struct ext2_super_block * super)137 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
138 {
139 return super->s_r_blocks_count |
140 (ext2fs_has_feature_64bit(super) ?
141 (__u64) super->s_r_blocks_count_hi << 32 : 0);
142 }
143
144 /*
145 * Set the fs reserved block count
146 */
ext2fs_r_blocks_count_set(struct ext2_super_block * super,blk64_t blk)147 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
148 {
149 super->s_r_blocks_count = blk;
150 if (ext2fs_has_feature_64bit(super))
151 super->s_r_blocks_count_hi = (__u64) blk >> 32;
152 }
153
154 /*
155 * Add to the current reserved fs block count
156 */
ext2fs_r_blocks_count_add(struct ext2_super_block * super,blk64_t blk)157 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
158 {
159 blk64_t tmp;
160 tmp = ext2fs_r_blocks_count(super) + blk;
161 ext2fs_r_blocks_count_set(super, tmp);
162 }
163
164 /*
165 * Return the fs free block count
166 */
ext2fs_free_blocks_count(struct ext2_super_block * super)167 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
168 {
169 return super->s_free_blocks_count |
170 (ext2fs_has_feature_64bit(super) ?
171 (__u64) super->s_free_blocks_hi << 32 : 0);
172 }
173
174 /*
175 * Set the fs free block count
176 */
ext2fs_free_blocks_count_set(struct ext2_super_block * super,blk64_t blk)177 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
178 {
179 super->s_free_blocks_count = blk;
180 if (ext2fs_has_feature_64bit(super))
181 super->s_free_blocks_hi = (__u64) blk >> 32;
182 }
183
184 /*
185 * Add to the current free fs block count
186 */
ext2fs_free_blocks_count_add(struct ext2_super_block * super,blk64_t blk)187 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
188 {
189 blk64_t tmp;
190 tmp = ext2fs_free_blocks_count(super) + blk;
191 ext2fs_free_blocks_count_set(super, tmp);
192 }
193
194 /*
195 * Get a pointer to a block group descriptor. We need the explicit
196 * pointer to the group desc for code that swaps block group
197 * descriptors before writing them out, as it wants to make a copy and
198 * do the swap there.
199 */
ext2fs_group_desc(ext2_filsys fs,struct opaque_ext2_group_desc * gdp,dgrp_t group)200 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
201 struct opaque_ext2_group_desc *gdp,
202 dgrp_t group)
203 {
204 int desc_size = EXT2_DESC_SIZE(fs->super) & ~7;
205
206 return (struct ext2_group_desc *)((char *)gdp + group * desc_size);
207 }
208
209 /* Do the same but as an ext4 group desc for internal use here */
ext4fs_group_desc(ext2_filsys fs,struct opaque_ext2_group_desc * gdp,dgrp_t group)210 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
211 struct opaque_ext2_group_desc *gdp,
212 dgrp_t group)
213 {
214 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
215 }
216
217 /*
218 * Return the block bitmap checksum of a group
219 */
ext2fs_block_bitmap_checksum(ext2_filsys fs,dgrp_t group)220 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
221 {
222 struct ext4_group_desc *gdp;
223 __u32 csum;
224
225 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
226 csum = gdp->bg_block_bitmap_csum_lo;
227 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
228 csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
229 return csum;
230 }
231
232 /*
233 * Return the block bitmap block of a group
234 */
ext2fs_block_bitmap_loc(ext2_filsys fs,dgrp_t group)235 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
236 {
237 struct ext4_group_desc *gdp;
238
239 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
240 return gdp->bg_block_bitmap |
241 (ext2fs_has_feature_64bit(fs->super) ?
242 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
243 }
244
245 /*
246 * Set the block bitmap block of a group
247 */
ext2fs_block_bitmap_loc_set(ext2_filsys fs,dgrp_t group,blk64_t blk)248 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
249 {
250 struct ext4_group_desc *gdp;
251
252 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
253 gdp->bg_block_bitmap = blk;
254 if (ext2fs_has_feature_64bit(fs->super))
255 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
256 }
257
258 /*
259 * Return the inode bitmap checksum of a group
260 */
ext2fs_inode_bitmap_checksum(ext2_filsys fs,dgrp_t group)261 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
262 {
263 struct ext4_group_desc *gdp;
264 __u32 csum;
265
266 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
267 csum = gdp->bg_inode_bitmap_csum_lo;
268 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
269 csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
270 return csum;
271 }
272
273 /*
274 * Return the inode bitmap block of a group
275 */
ext2fs_inode_bitmap_loc(ext2_filsys fs,dgrp_t group)276 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
277 {
278 struct ext4_group_desc *gdp;
279
280 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
281 return gdp->bg_inode_bitmap |
282 (ext2fs_has_feature_64bit(fs->super) ?
283 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
284 }
285
286 /*
287 * Set the inode bitmap block of a group
288 */
ext2fs_inode_bitmap_loc_set(ext2_filsys fs,dgrp_t group,blk64_t blk)289 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
290 {
291 struct ext4_group_desc *gdp;
292
293 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
294 gdp->bg_inode_bitmap = blk;
295 if (ext2fs_has_feature_64bit(fs->super))
296 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
297 }
298
299 /*
300 * Return the inode table block of a group
301 */
ext2fs_inode_table_loc(ext2_filsys fs,dgrp_t group)302 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
303 {
304 struct ext4_group_desc *gdp;
305
306 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
307 return gdp->bg_inode_table |
308 (ext2fs_has_feature_64bit(fs->super) ?
309 (__u64) gdp->bg_inode_table_hi << 32 : 0);
310 }
311
312 /*
313 * Set the inode table block of a group
314 */
ext2fs_inode_table_loc_set(ext2_filsys fs,dgrp_t group,blk64_t blk)315 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
316 {
317 struct ext4_group_desc *gdp;
318
319 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
320 gdp->bg_inode_table = blk;
321 if (ext2fs_has_feature_64bit(fs->super))
322 gdp->bg_inode_table_hi = (__u64) blk >> 32;
323 }
324
325 /*
326 * Return the free blocks count of a group
327 */
ext2fs_bg_free_blocks_count(ext2_filsys fs,dgrp_t group)328 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
329 {
330 struct ext4_group_desc *gdp;
331
332 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
333 return gdp->bg_free_blocks_count |
334 (ext2fs_has_feature_64bit(fs->super) ?
335 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
336 }
337
338 /*
339 * Set the free blocks count of a group
340 */
ext2fs_bg_free_blocks_count_set(ext2_filsys fs,dgrp_t group,__u32 n)341 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
342 {
343 struct ext4_group_desc *gdp;
344
345 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
346 gdp->bg_free_blocks_count = n;
347
348 if (ext2fs_has_feature_64bit(fs->super))
349 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
350 }
351
352 /*
353 * Return the free inodes count of a group
354 */
ext2fs_bg_free_inodes_count(ext2_filsys fs,dgrp_t group)355 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
356 {
357 struct ext4_group_desc *gdp;
358
359 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
360 return gdp->bg_free_inodes_count |
361 (ext2fs_has_feature_64bit(fs->super) ?
362 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
363 }
364
365 /*
366 * Set the free inodes count of a group
367 */
ext2fs_bg_free_inodes_count_set(ext2_filsys fs,dgrp_t group,__u32 n)368 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
369 {
370 struct ext4_group_desc *gdp;
371
372 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
373 gdp->bg_free_inodes_count = n;
374 if (ext2fs_has_feature_64bit(fs->super))
375 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
376 }
377
378 /*
379 * Return the used dirs count of a group
380 */
ext2fs_bg_used_dirs_count(ext2_filsys fs,dgrp_t group)381 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
382 {
383 struct ext4_group_desc *gdp;
384
385 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
386 return gdp->bg_used_dirs_count |
387 (ext2fs_has_feature_64bit(fs->super) ?
388 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
389 }
390
391 /*
392 * Set the used dirs count of a group
393 */
ext2fs_bg_used_dirs_count_set(ext2_filsys fs,dgrp_t group,__u32 n)394 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
395 {
396 struct ext4_group_desc *gdp;
397
398 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
399 gdp->bg_used_dirs_count = n;
400 if (ext2fs_has_feature_64bit(fs->super))
401 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
402 }
403
404 /*
405 * Return the unused inodes count of a group
406 */
ext2fs_bg_itable_unused(ext2_filsys fs,dgrp_t group)407 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
408 {
409 struct ext4_group_desc *gdp;
410
411 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
412 return gdp->bg_itable_unused |
413 (ext2fs_has_feature_64bit(fs->super) ?
414 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
415 }
416
417 /*
418 * Set the unused inodes count of a group
419 */
ext2fs_bg_itable_unused_set(ext2_filsys fs,dgrp_t group,__u32 n)420 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
421 {
422 struct ext4_group_desc *gdp;
423
424 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
425 gdp->bg_itable_unused = n;
426 if (ext2fs_has_feature_64bit(fs->super))
427 gdp->bg_itable_unused_hi = (__u32) n >> 16;
428 }
429
430 /*
431 * Get the flags for this block group
432 */
ext2fs_bg_flags(ext2_filsys fs,dgrp_t group)433 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
434 {
435 struct ext4_group_desc *gdp;
436
437 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
438 return gdp->bg_flags;
439 }
440
441 /*
442 * Zero out the flags for this block group
443 */
ext2fs_bg_flags_zap(ext2_filsys fs,dgrp_t group)444 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
445 {
446 struct ext4_group_desc *gdp;
447
448 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
449 gdp->bg_flags = 0;
450 return;
451 }
452
453 /*
454 * Get the value of a particular flag for this block group
455 */
ext2fs_bg_flags_test(ext2_filsys fs,dgrp_t group,__u16 bg_flag)456 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
457 {
458 struct ext4_group_desc *gdp;
459
460 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
461 return gdp->bg_flags & bg_flag;
462 }
463
464 /*
465 * Set a flag or set of flags for this block group
466 */
ext2fs_bg_flags_set(ext2_filsys fs,dgrp_t group,__u16 bg_flags)467 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
468 {
469 struct ext4_group_desc *gdp;
470
471 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
472 gdp->bg_flags |= bg_flags;
473 return;
474 }
475
476 /*
477 * Clear a flag or set of flags for this block group
478 */
ext2fs_bg_flags_clear(ext2_filsys fs,dgrp_t group,__u16 bg_flags)479 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
480 {
481 struct ext4_group_desc *gdp;
482
483 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
484 gdp->bg_flags &= ~bg_flags;
485 return;
486 }
487
488 /*
489 * Get the checksum for this block group
490 */
ext2fs_bg_checksum(ext2_filsys fs,dgrp_t group)491 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
492 {
493 struct ext4_group_desc *gdp;
494
495 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
496 return gdp->bg_checksum;
497 }
498
499 /*
500 * Set the checksum for this block group to a previously calculated value
501 */
ext2fs_bg_checksum_set(ext2_filsys fs,dgrp_t group,__u16 checksum)502 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
503 {
504 struct ext4_group_desc *gdp;
505
506 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
507 gdp->bg_checksum = checksum;
508 return;
509 }
510
511 /*
512 * Get the acl block of a file
513 */
ext2fs_file_acl_block(ext2_filsys fs,const struct ext2_inode * inode)514 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
515 {
516 blk64_t blk = inode->i_file_acl;
517
518 if (fs && ext2fs_has_feature_64bit(fs->super))
519 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
520 return blk;
521 }
522
523 /*
524 * Set the acl block of a file
525 */
ext2fs_file_acl_block_set(ext2_filsys fs,struct ext2_inode * inode,blk64_t blk)526 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
527 blk64_t blk)
528 {
529 inode->i_file_acl = blk;
530 if (fs && ext2fs_has_feature_64bit(fs->super))
531 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
532 }
533
534 /*
535 * Set the size of the inode
536 */
ext2fs_inode_size_set(ext2_filsys fs,struct ext2_inode * inode,ext2_off64_t size)537 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
538 ext2_off64_t size)
539 {
540 if (size < 0)
541 return EINVAL;
542
543 /* Only regular files get to be larger than 4GB */
544 if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
545 return EXT2_ET_FILE_TOO_BIG;
546
547 /* If we're writing a large file, set the large_file flag */
548 if (LINUX_S_ISREG(inode->i_mode) &&
549 ext2fs_needs_large_file_feature(size) &&
550 (!ext2fs_has_feature_large_file(fs->super) ||
551 fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
552 ext2fs_set_feature_large_file(fs->super);
553 ext2fs_update_dynamic_rev(fs);
554 ext2fs_mark_super_dirty(fs);
555 }
556
557 inode->i_size = size & 0xffffffff;
558 inode->i_size_high = (size >> 32);
559
560 return 0;
561 }
562
563