Whamcloud - gitweb
libext2fs: fix offset code in undo_write_tdb
[tools/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                 (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);
75 }
76
77 /*
78  * Return the inode i_blocks count
79  */
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 fs block count
90  */
91 blk64_t ext2fs_blocks_count(struct ext2_super_block *super)
92 {
93         return super->s_blocks_count |
94                 (ext2fs_has_feature_64bit(super) ?
95                 (__u64) super->s_blocks_count_hi << 32 : 0);
96 }
97
98 /*
99  * Set the fs block count
100  */
101 void ext2fs_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
102 {
103         super->s_blocks_count = blk;
104         if (ext2fs_has_feature_64bit(super))
105                 super->s_blocks_count_hi = (__u64) blk >> 32;
106 }
107
108 /*
109  * Add to the current fs block count
110  */
111 void ext2fs_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
112 {
113         blk64_t tmp;
114         tmp = ext2fs_blocks_count(super) + blk;
115         ext2fs_blocks_count_set(super, tmp);
116 }
117
118 /*
119  * Return the fs reserved block count
120  */
121 blk64_t ext2fs_r_blocks_count(struct ext2_super_block *super)
122 {
123         return super->s_r_blocks_count |
124                 (ext2fs_has_feature_64bit(super) ?
125                 (__u64) super->s_r_blocks_count_hi << 32 : 0);
126 }
127
128 /*
129  * Set the fs reserved block count
130  */
131 void ext2fs_r_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
132 {
133         super->s_r_blocks_count = blk;
134         if (ext2fs_has_feature_64bit(super))
135                 super->s_r_blocks_count_hi = (__u64) blk >> 32;
136 }
137
138 /*
139  * Add to the current reserved fs block count
140  */
141 void ext2fs_r_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
142 {
143         blk64_t tmp;
144         tmp = ext2fs_r_blocks_count(super) + blk;
145         ext2fs_r_blocks_count_set(super, tmp);
146 }
147
148 /*
149  * Return the fs free block count
150  */
151 blk64_t ext2fs_free_blocks_count(struct ext2_super_block *super)
152 {
153         return super->s_free_blocks_count |
154                 (ext2fs_has_feature_64bit(super) ?
155                 (__u64) super->s_free_blocks_hi << 32 : 0);
156 }
157
158 /*
159  * Set the fs free block count
160  */
161 void ext2fs_free_blocks_count_set(struct ext2_super_block *super, blk64_t blk)
162 {
163         super->s_free_blocks_count = blk;
164         if (ext2fs_has_feature_64bit(super))
165                 super->s_free_blocks_hi = (__u64) blk >> 32;
166 }
167
168 /*
169  * Add to the current free fs block count
170  */
171 void ext2fs_free_blocks_count_add(struct ext2_super_block *super, blk64_t blk)
172 {
173         blk64_t tmp;
174         tmp = ext2fs_free_blocks_count(super) + blk;
175         ext2fs_free_blocks_count_set(super, tmp);
176 }
177
178 /*
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
182  * do the swap there.
183  */
184 struct ext2_group_desc *ext2fs_group_desc(ext2_filsys fs,
185                                           struct opaque_ext2_group_desc *gdp,
186                                           dgrp_t group)
187 {
188         return (struct ext2_group_desc *)((char *)gdp +
189                                           group * EXT2_DESC_SIZE(fs->super));
190 }
191
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,
195                                           dgrp_t group)
196 {
197         return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
198 }
199
200 /*
201  * Return the block bitmap checksum of a group
202  */
203 __u32 ext2fs_block_bitmap_checksum(ext2_filsys fs, dgrp_t group)
204 {
205         struct ext4_group_desc *gdp;
206         __u32 csum;
207
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);
212         return csum;
213 }
214
215 /*
216  * Return the block bitmap block of a group
217  */
218 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
219 {
220         struct ext4_group_desc *gdp;
221
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);
226 }
227
228 /*
229  * Set the block bitmap block of a group
230  */
231 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
232 {
233         struct ext4_group_desc *gdp;
234
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;
239 }
240
241 /*
242  * Return the inode bitmap checksum of a group
243  */
244 __u32 ext2fs_inode_bitmap_checksum(ext2_filsys fs, dgrp_t group)
245 {
246         struct ext4_group_desc *gdp;
247         __u32 csum;
248
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);
253         return csum;
254 }
255
256 /*
257  * Return the inode bitmap block of a group
258  */
259 blk64_t ext2fs_inode_bitmap_loc(ext2_filsys fs, dgrp_t group)
260 {
261         struct ext4_group_desc *gdp;
262
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);
267 }
268
269 /*
270  * Set the inode bitmap block of a group
271  */
272 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
273 {
274         struct ext4_group_desc *gdp;
275
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;
280 }
281
282 /*
283  * Return the inode table block of a group
284  */
285 blk64_t ext2fs_inode_table_loc(ext2_filsys fs, dgrp_t group)
286 {
287         struct ext4_group_desc *gdp;
288
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);
293 }
294
295 /*
296  * Set the inode table block of a group
297  */
298 void ext2fs_inode_table_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
299 {
300         struct ext4_group_desc *gdp;
301
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;
306 }
307
308 /*
309  * Return the free blocks count of a group
310  */
311 __u32 ext2fs_bg_free_blocks_count(ext2_filsys fs, dgrp_t group)
312 {
313         struct ext4_group_desc *gdp;
314
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);
319 }
320
321 /*
322  * Set the free blocks count of a group
323  */
324 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
325 {
326         struct ext4_group_desc *gdp;
327
328         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
329         gdp->bg_free_blocks_count = n;
330
331         if (ext2fs_has_feature_64bit(fs->super))
332                 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
333 }
334
335 /*
336  * Return the free inodes count of a group
337  */
338 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
339 {
340         struct ext4_group_desc *gdp;
341
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);
346 }
347
348 /*
349  * Set the free inodes count of a group
350  */
351 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
352 {
353         struct ext4_group_desc *gdp;
354
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;
359 }
360
361 /*
362  * Return the used dirs count of a group
363  */
364 __u32 ext2fs_bg_used_dirs_count(ext2_filsys fs, dgrp_t group)
365 {
366         struct ext4_group_desc *gdp;
367
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);
372 }
373
374 /*
375  * Set the used dirs count of a group
376  */
377 void ext2fs_bg_used_dirs_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
378 {
379         struct ext4_group_desc *gdp;
380
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;
385 }
386
387 /*
388  * Return the unused inodes count of a group
389  */
390 __u32 ext2fs_bg_itable_unused(ext2_filsys fs, dgrp_t group)
391 {
392         struct ext4_group_desc *gdp;
393
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);
398 }
399
400 /*
401  * Set the unused inodes count of a group
402  */
403 void ext2fs_bg_itable_unused_set(ext2_filsys fs, dgrp_t group, __u32 n)
404 {
405         struct ext4_group_desc *gdp;
406
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;
411 }
412
413 /*
414  * Get the flags for this block group
415  */
416 __u16 ext2fs_bg_flags(ext2_filsys fs, dgrp_t group)
417 {
418         struct ext4_group_desc *gdp;
419
420         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
421         return gdp->bg_flags;
422 }
423
424 /*
425  * Zero out the flags for this block group
426  */
427 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
428 {
429         struct ext4_group_desc *gdp;
430
431         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
432         gdp->bg_flags = 0;
433         return;
434 }
435
436 /*
437  * Get the value of a particular flag for this block group
438  */
439 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
440 {
441         struct ext4_group_desc *gdp;
442
443         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
444         return gdp->bg_flags & bg_flag;
445 }
446
447 /*
448  * Set a flag or set of flags for this block group
449  */
450 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
451 {
452         struct ext4_group_desc *gdp;
453
454         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
455         gdp->bg_flags |= bg_flags;
456         return;
457 }
458
459 /*
460  * Clear a flag or set of flags for this block group
461  */
462 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
463 {
464         struct ext4_group_desc *gdp;
465
466         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
467         gdp->bg_flags &= ~bg_flags;
468         return;
469 }
470
471 /*
472  * Get the checksum for this block group
473  */
474 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
475 {
476         struct ext4_group_desc *gdp;
477
478         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
479         return gdp->bg_checksum;
480 }
481
482 /*
483  * Set the checksum for this block group to a previously calculated value
484  */
485 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
486 {
487         struct ext4_group_desc *gdp;
488
489         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
490         gdp->bg_checksum = checksum;
491         return;
492 }
493
494 /*
495  * Get the acl block of a file
496  */
497 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
498 {
499         blk64_t blk = inode->i_file_acl;
500
501         if (fs && ext2fs_has_feature_64bit(fs->super))
502                 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
503         return blk;
504 }
505
506 /*
507  * Set the acl block of a file
508  */
509 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
510                                blk64_t blk)
511 {
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;
515 }
516
517 /*
518  * Set the size of the inode
519  */
520 errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
521                                 ext2_off64_t size)
522 {
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;
526
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);
535         }
536
537         inode->i_size = size & 0xffffffff;
538         inode->i_size_high = (size >> 32);
539
540         return 0;
541 }
542