Whamcloud - gitweb
libext2fs: move the alignment field from unix_io to the io_manager
[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                 ((blk64_t)group * fs->super->s_blocks_per_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         if (fs->super->s_desc_size >= EXT2_MIN_DESC_SIZE_64BIT)
191                 return (struct ext2_group_desc *)
192                         ((struct ext4_group_desc *) gdp + group);
193         else
194                 return (struct ext2_group_desc *) gdp + group;
195 }
196
197 /* Do the same but as an ext4 group desc for internal use here */
198 static struct ext4_group_desc *ext4fs_group_desc(ext2_filsys fs,
199                                           struct opaque_ext2_group_desc *gdp,
200                                           dgrp_t group)
201 {
202         return (struct ext4_group_desc *)ext2fs_group_desc(fs, gdp, group);
203 }
204
205 /*
206  * Return the block bitmap block of a group
207  */
208 blk64_t ext2fs_block_bitmap_loc(ext2_filsys fs, dgrp_t group)
209 {
210         struct ext4_group_desc *gdp;
211
212         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
213         return gdp->bg_block_bitmap |
214                 (fs->super->s_feature_incompat
215                  & EXT4_FEATURE_INCOMPAT_64BIT ?
216                  (__u64)gdp->bg_block_bitmap_hi << 32 : 0);
217 }
218
219 /*
220  * Set the block bitmap block of a group
221  */
222 void ext2fs_block_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
223 {
224         struct ext4_group_desc *gdp;
225
226         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
227         gdp->bg_block_bitmap = blk;
228         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
229                 gdp->bg_block_bitmap_hi = (__u64) blk >> 32;
230 }
231
232 /*
233  * Return the inode bitmap block of a group
234  */
235 blk64_t ext2fs_inode_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_inode_bitmap |
241                 (fs->super->s_feature_incompat
242                  & EXT4_FEATURE_INCOMPAT_64BIT ?
243                  (__u64) gdp->bg_inode_bitmap_hi << 32 : 0);
244 }
245
246 /*
247  * Set the inode bitmap block of a group
248  */
249 void ext2fs_inode_bitmap_loc_set(ext2_filsys fs, dgrp_t group, blk64_t blk)
250 {
251         struct ext4_group_desc *gdp;
252
253         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
254         gdp->bg_inode_bitmap = blk;
255         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
256                 gdp->bg_inode_bitmap_hi = (__u64) blk >> 32;
257 }
258
259 /*
260  * Return the inode table block of a group
261  */
262 blk64_t ext2fs_inode_table_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_table |
268                 (fs->super->s_feature_incompat
269                  & EXT4_FEATURE_INCOMPAT_64BIT ?
270                  (__u64) gdp->bg_inode_table_hi << 32 : 0);
271 }
272
273 /*
274  * Set the inode table block of a group
275  */
276 void ext2fs_inode_table_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_table = blk;
282         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
283                 gdp->bg_inode_table_hi = (__u64) blk >> 32;
284 }
285
286 /*
287  * Return the free blocks count of a group
288  */
289 __u32 ext2fs_bg_free_blocks_count(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_free_blocks_count |
295                 (fs->super->s_feature_incompat
296                  & EXT4_FEATURE_INCOMPAT_64BIT ?
297                  (__u32) gdp->bg_free_blocks_count_hi << 16 : 0);
298 }
299
300 /*
301  * Set the free blocks count of a group
302  */
303 void ext2fs_bg_free_blocks_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
304 {
305         struct ext4_group_desc *gdp;
306
307         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
308         gdp->bg_free_blocks_count = n;
309
310         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
311                 gdp->bg_free_blocks_count_hi = (__u32) n >> 16;
312 }
313
314 /*
315  * Return the free inodes count of a group
316  */
317 __u32 ext2fs_bg_free_inodes_count(ext2_filsys fs, dgrp_t group)
318 {
319         struct ext4_group_desc *gdp;
320
321         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
322         return gdp->bg_free_inodes_count |
323                 (fs->super->s_feature_incompat
324                  & EXT4_FEATURE_INCOMPAT_64BIT ?
325                  (__u32) gdp->bg_free_inodes_count_hi << 16 : 0);
326 }
327
328 /*
329  * Set the free inodes count of a group
330  */
331 void ext2fs_bg_free_inodes_count_set(ext2_filsys fs, dgrp_t group, __u32 n)
332 {
333         struct ext4_group_desc *gdp;
334
335         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
336         gdp->bg_free_inodes_count = n;
337         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
338                 gdp->bg_free_inodes_count_hi = (__u32) n >> 16;
339 }
340
341 /*
342  * Return the used dirs count of a group
343  */
344 __u32 ext2fs_bg_used_dirs_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_used_dirs_count |
350                 (fs->super->s_feature_incompat
351                  & EXT4_FEATURE_INCOMPAT_64BIT ?
352                  (__u32) gdp->bg_used_dirs_count_hi << 16 : 0);
353 }
354
355 /*
356  * Set the used dirs count of a group
357  */
358 void ext2fs_bg_used_dirs_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_used_dirs_count = n;
364         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
365                 gdp->bg_used_dirs_count_hi = (__u32) n >> 16;
366 }
367
368 /*
369  * Return the unused inodes count of a group
370  */
371 __u32 ext2fs_bg_itable_unused(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_itable_unused |
377                 (fs->super->s_feature_incompat
378                  & EXT4_FEATURE_INCOMPAT_64BIT ?
379                  (__u32) gdp->bg_itable_unused_hi << 16 : 0);
380 }
381
382 /*
383  * Set the unused inodes count of a group
384  */
385 void ext2fs_bg_itable_unused_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_itable_unused = n;
391         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
392                 gdp->bg_itable_unused_hi = (__u32) n >> 16;
393 }
394
395 /*
396  * Get the flags for this block group
397  */
398 __u16 ext2fs_bg_flags(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_flags;
404 }
405
406 /*
407  * Zero out the flags for this block group
408  */
409 void ext2fs_bg_flags_zap(ext2_filsys fs, dgrp_t group)
410 {
411         struct ext4_group_desc *gdp;
412
413         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
414         gdp->bg_flags = 0;
415         return;
416 }
417
418 /*
419  * Get the value of a particular flag for this block group
420  */
421 int ext2fs_bg_flags_test(ext2_filsys fs, dgrp_t group, __u16 bg_flag)
422 {
423         struct ext4_group_desc *gdp;
424
425         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
426         return gdp->bg_flags & bg_flag;
427 }
428
429 /*
430  * Set a flag or set of flags for this block group
431  */
432 void ext2fs_bg_flags_set(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
433 {
434         struct ext4_group_desc *gdp;
435
436         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
437         gdp->bg_flags |= bg_flags;
438         return;
439 }
440
441 /*
442  * Clear a flag or set of flags for this block group
443  */
444 void ext2fs_bg_flags_clear(ext2_filsys fs, dgrp_t group, __u16 bg_flags)
445 {
446         struct ext4_group_desc *gdp;
447
448         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
449         gdp->bg_flags &= ~bg_flags;
450         return;
451 }
452
453 /*
454  * Get the checksum for this block group
455  */
456 __u16 ext2fs_bg_checksum(ext2_filsys fs, dgrp_t group)
457 {
458         struct ext4_group_desc *gdp;
459
460         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
461         return gdp->bg_checksum;
462 }
463
464 /*
465  * Set the checksum for this block group to a previously calculated value
466  */
467 void ext2fs_bg_checksum_set(ext2_filsys fs, dgrp_t group, __u16 checksum)
468 {
469         struct ext4_group_desc *gdp;
470
471         gdp = ext4fs_group_desc(fs, fs->group_desc, group);
472         gdp->bg_checksum = checksum;
473         return;
474 }
475
476 /*
477  * Get the acl block of a file
478  */
479 blk64_t ext2fs_file_acl_block(ext2_filsys fs, const struct ext2_inode *inode)
480 {
481         blk64_t blk = inode->i_file_acl;
482
483         if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
484                 blk |= ((__u64) inode->osd2.linux2.l_i_file_acl_high) << 32;
485         return blk;
486 }
487
488 /*
489  * Set the acl block of a file
490  */
491 void ext2fs_file_acl_block_set(ext2_filsys fs, struct ext2_inode *inode,
492                                blk64_t blk)
493 {
494         inode->i_file_acl = blk;
495         if (fs && fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
496                 inode->osd2.linux2.l_i_file_acl_high = (__u64) blk >> 32;
497 }
498