]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/blknum.c
ead23281987ebc3aa1232afd4e4f5c9d674ee416
[thirdparty/e2fsprogs.git] / lib / ext2fs / blknum.c
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 */
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 */
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 */
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 */
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 */
68 blk64_t ext2fs_inode_data_blocks2(ext2_filsys fs,
69 struct ext2_inode *inode)
70 {
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);
76 }
77
78 /*
79 * Return the inode i_blocks count
80 */
81 blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
82 struct ext2_inode *inode)
83 {
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));
88 }
89
90 /*
91 * Return the fs block count
92 */
93 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
94 {
95 return super->s_blocks_count |
96 (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
97 (__u64) super->s_blocks_count_hi << 32 : 0);
98 }
99
100 /*
101 * Set the fs block count
102 */
103 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
104 {
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;
108 }
109
110 /*
111 * Add to the current fs block count
112 */
113 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
114 {
115 blk64_t tmp;
116 tmp = ext2fs_blocks_count(super) + blk;
117 ext2fs_blocks_count_set(super, tmp);
118 }
119
120 /*
121 * Return the fs reserved block count
122 */
123 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
124 {
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);
128 }
129
130 /*
131 * Set the fs reserved block count
132 */
133 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
134 {
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;
138 }
139
140 /*
141 * Add to the current reserved fs block count
142 */
143 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
144 {
145 blk64_t tmp;
146 tmp = ext2fs_r_blocks_count(super) + blk;
147 ext2fs_r_blocks_count_set(super, tmp);
148 }
149
150 /*
151 * Return the fs free block count
152 */
153 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
154 {
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);
158 }
159
160 /*
161 * Set the fs free block count
162 */
163 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
164 {
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;
168 }
169
170 /*
171 * Add to the current free fs block count
172 */
173 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
174 {
175 blk64_t tmp;
176 tmp = ext2fs_free_blocks_count(super) + blk;
177 ext2fs_free_blocks_count_set(super, tmp);
178 }
179
180 /*
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
184 * do the swap there.
185 */
186 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
187 struct opaque_ext2_group_desc *gdp,
188 dgrp_t group)
189 {
190 return (struct ext2_group_desc *)((char *)gdp +
191 group * EXT2_DESC_SIZE(fs->super));
192 }
193
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,
197 dgrp_t group)
198 {
199 return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
200 }
201
202 /*
203 * Return the block bitmap checksum of a group
204 */
205 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
206 {
207 struct ext4_group_desc *gdp;
208 __u32 csum;
209
210 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
211 csum = gdp->bg_block_bitmap_csum_lo;
212 if (fs->super->s_desc_size >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
213 csum |= ((__u32)gdp->bg_block_bitmap_csum_hi << 16);
214 return csum;
215 }
216
217 /*
218 * Return the block bitmap block of a group
219 */
220 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
221 {
222 struct ext4_group_desc *gdp;
223
224 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
225 return gdp->bg_block_bitmap |
226 (fs->super->s_feature_incompat
227 & EXT4_FEATURE_INCOMPAT_64BIT ?
228 (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
229 }
230
231 /*
232 * Set the block bitmap block of a group
233 */
234 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
235 {
236 struct ext4_group_desc *gdp;
237
238 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
239 gdp->bg_block_bitmap = blk;
240 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
241 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
242 }
243
244 /*
245 * Return the inode bitmap checksum of a group
246 */
247 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
248 {
249 struct ext4_group_desc *gdp;
250 __u32 csum;
251
252 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
253 csum = gdp->bg_inode_bitmap_csum_lo;
254 if (fs->super->s_desc_size >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
255 csum |= ((__u32)gdp->bg_inode_bitmap_csum_hi << 16);
256 return csum;
257 }
258
259 /*
260 * Return the inode bitmap block of a group
261 */
262 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
263 {
264 struct ext4_group_desc *gdp;
265
266 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
267 return gdp->bg_inode_bitmap |
268 (fs->super->s_feature_incompat
269 & EXT4_FEATURE_INCOMPAT_64BIT ?
270 (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
271 }
272
273 /*
274 * Set the inode bitmap block of a group
275 */
276 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
277 {
278 struct ext4_group_desc *gdp;
279
280 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
281 gdp->bg_inode_bitmap = blk;
282 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
283 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
284 }
285
286 /*
287 * Return the inode table block of a group
288 */
289 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
290 {
291 struct ext4_group_desc *gdp;
292
293 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
294 return gdp->bg_inode_table |
295 (fs->super->s_feature_incompat
296 & EXT4_FEATURE_INCOMPAT_64BIT ?
297 (__u64) gdp->bg_inode_table_hi << 32 : 0);
298 }
299
300 /*
301 * Set the inode table block of a group
302 */
303 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
304 {
305 struct ext4_group_desc *gdp;
306
307 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
308 gdp->bg_inode_table = blk;
309 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
310 gdp->bg_inode_table_hi = (__u64) blk >> 32;
311 }
312
313 /*
314 * Return the free blocks count of a group
315 */
316 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
317 {
318 struct ext4_group_desc *gdp;
319
320 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
321 return gdp->bg_free_blocks_count |
322 (fs->super->s_feature_incompat
323 & EXT4_FEATURE_INCOMPAT_64BIT ?
324 (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
325 }
326
327 /*
328 * Set the free blocks count of a group
329 */
330 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
331 {
332 struct ext4_group_desc *gdp;
333
334 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
335 gdp->bg_free_blocks_count = n;
336
337 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
338 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
339 }
340
341 /*
342 * Return the free inodes count of a group
343 */
344 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
345 {
346 struct ext4_group_desc *gdp;
347
348 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
349 return gdp->bg_free_inodes_count |
350 (fs->super->s_feature_incompat
351 & EXT4_FEATURE_INCOMPAT_64BIT ?
352 (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
353 }
354
355 /*
356 * Set the free inodes count of a group
357 */
358 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
359 {
360 struct ext4_group_desc *gdp;
361
362 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
363 gdp->bg_free_inodes_count = n;
364 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
365 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
366 }
367
368 /*
369 * Return the used dirs count of a group
370 */
371 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
372 {
373 struct ext4_group_desc *gdp;
374
375 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
376 return gdp->bg_used_dirs_count |
377 (fs->super->s_feature_incompat
378 & EXT4_FEATURE_INCOMPAT_64BIT ?
379 (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
380 }
381
382 /*
383 * Set the used dirs count of a group
384 */
385 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
386 {
387 struct ext4_group_desc *gdp;
388
389 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
390 gdp->bg_used_dirs_count = n;
391 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
392 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
393 }
394
395 /*
396 * Return the unused inodes count of a group
397 */
398 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
399 {
400 struct ext4_group_desc *gdp;
401
402 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
403 return gdp->bg_itable_unused |
404 (fs->super->s_feature_incompat
405 & EXT4_FEATURE_INCOMPAT_64BIT ?
406 (__u32) gdp->bg_itable_unused_hi << 16 : 0);
407 }
408
409 /*
410 * Set the unused inodes count of a group
411 */
412 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
413 {
414 struct ext4_group_desc *gdp;
415
416 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
417 gdp->bg_itable_unused = n;
418 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
419 gdp->bg_itable_unused_hi = (__u32) n >> 16;
420 }
421
422 /*
423 * Get the flags for this block group
424 */
425 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
426 {
427 struct ext4_group_desc *gdp;
428
429 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
430 return gdp->bg_flags;
431 }
432
433 /*
434 * Zero out the flags for this block group
435 */
436 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
437 {
438 struct ext4_group_desc *gdp;
439
440 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
441 gdp->bg_flags = 0;
442 return;
443 }
444
445 /*
446 * Get the value of a particular flag for this block group
447 */
448 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
449 {
450 struct ext4_group_desc *gdp;
451
452 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
453 return gdp->bg_flags & bg_flag;
454 }
455
456 /*
457 * Set a flag or set of flags for this block group
458 */
459 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
460 {
461 struct ext4_group_desc *gdp;
462
463 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
464 gdp->bg_flags |= bg_flags;
465 return;
466 }
467
468 /*
469 * Clear a flag or set of flags for this block group
470 */
471 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
472 {
473 struct ext4_group_desc *gdp;
474
475 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
476 gdp->bg_flags &= ~bg_flags;
477 return;
478 }
479
480 /*
481 * Get the checksum for this block group
482 */
483 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
484 {
485 struct ext4_group_desc *gdp;
486
487 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
488 return gdp->bg_checksum;
489 }
490
491 /*
492 * Set the checksum for this block group to a previously calculated value
493 */
494 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
495 {
496 struct ext4_group_desc *gdp;
497
498 gdp = ext4fs_group_desc(fs, fs->group_desc, group);
499 gdp->bg_checksum = checksum;
500 return;
501 }
502
503 /*
504 * Get the acl block of a file
505 */
506 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
507 {
508 blk64_t blk = inode->i_file_acl;
509
510 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
511 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
512 return blk;
513 }
514
515 /*
516 * Set the acl block of a file
517 */
518 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
519 blk64_t blk)
520 {
521 inode->i_file_acl = blk;
522 if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
523 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
524 }
525