Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * i_block.c --- Manage the i_block field for i_blocks
      3  *
      4  * Copyright (C) 2008 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Library
      8  * General Public License, version 2.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #if HAVE_UNISTD_H
     14 #include <unistd.h>
     15 #endif
     16 #include <time.h>
     17 #include <string.h>
     18 #if HAVE_SYS_STAT_H
     19 #include <sys/stat.h>
     20 #endif
     21 #if HAVE_SYS_TYPES_H
     22 #include <sys/types.h>
     23 #endif
     24 #include <errno.h>
     25 
     26 #include "ext2_fs.h"
     27 #include "ext2fs.h"
     28 
     29 errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
     30 				 blk64_t num_blocks)
     31 {
     32 	unsigned long long b = inode->i_blocks;
     33 
     34 	if (!(fs->super->s_feature_ro_compat &
     35 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
     36 	    !(inode->i_flags & EXT4_HUGE_FILE_FL))
     37 	    num_blocks *= fs->blocksize / 512;
     38 
     39 	b += num_blocks;
     40 
     41 	if (fs->super->s_feature_ro_compat &
     42 	    EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
     43 		b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
     44 		inode->osd2.linux2.l_i_blocks_hi = b >> 32;
     45 	} else if (b > 0xFFFFFFFF)
     46 		return EOVERFLOW;
     47 	inode->i_blocks = b & 0xFFFFFFFF;
     48 	return 0;
     49 }
     50 
     51 errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
     52 				 blk64_t num_blocks)
     53 {
     54 	unsigned long long b = inode->i_blocks;
     55 
     56 	if (!(fs->super->s_feature_ro_compat &
     57 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
     58 	    !(inode->i_flags & EXT4_HUGE_FILE_FL))
     59 	    num_blocks *= fs->blocksize / 512;
     60 
     61 	if (num_blocks > b)
     62 		return EOVERFLOW;
     63 
     64 	b -= num_blocks;
     65 
     66 	if (fs->super->s_feature_ro_compat &
     67 	    EXT4_FEATURE_RO_COMPAT_HUGE_FILE) {
     68 		b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
     69 		inode->osd2.linux2.l_i_blocks_hi = b >> 32;
     70 	}
     71 	inode->i_blocks = b & 0xFFFFFFFF;
     72 	return 0;
     73 }
     74 
     75 errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b)
     76 {
     77 	if (!(fs->super->s_feature_ro_compat &
     78 	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
     79 	    !(inode->i_flags & EXT4_HUGE_FILE_FL))
     80 		b *= fs->blocksize / 512;
     81 
     82 	inode->i_blocks = b & 0xFFFFFFFF;
     83 	if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
     84 		inode->osd2.linux2.l_i_blocks_hi = b >> 32;
     85 	else if (b >> 32)
     86 		return EOVERFLOW;
     87 	return 0;
     88 }
     89