libf2fs.c revision 0fbdf6cbd4dfc633454eba2f841c123789e297ad
1/** 2 * libf2fs.c 3 * 4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * Dual licensed under the GPL or LGPL version 2 licenses. 8 */ 9#define _LARGEFILE64_SOURCE 10 11#include <stdio.h> 12#include <stdlib.h> 13#include <string.h> 14#include <errno.h> 15#include <unistd.h> 16#include <fcntl.h> 17#include <mntent.h> 18#include <time.h> 19#include <sys/stat.h> 20#include <sys/mount.h> 21#include <sys/ioctl.h> 22#include <linux/hdreg.h> 23 24#include <f2fs_fs.h> 25 26void ASCIIToUNICODE(u_int16_t *out_buf, u_int8_t *in_buf) 27{ 28 u_int8_t *pchTempPtr = in_buf; 29 u_int16_t *pwTempPtr = out_buf; 30 31 while (*pchTempPtr != '\0') { 32 *pwTempPtr = (u_int16_t)*pchTempPtr; 33 pchTempPtr++; 34 pwTempPtr++; 35 } 36 *pwTempPtr = '\0'; 37 return; 38} 39 40int log_base_2(u_int32_t num) 41{ 42 int ret = 0; 43 if (num <= 0 || (num & (num - 1)) != 0) 44 return -1; 45 46 while (num >>= 1) 47 ret++; 48 return ret; 49} 50 51/* 52 * f2fs bit operations 53 */ 54static const int bits_in_byte[256] = { 55 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 56 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 57 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 58 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 59 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 60 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 61 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 62 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 63 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 64 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 65 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 66 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 67 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 68 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 69 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 70 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, 71}; 72 73int get_bits_in_byte(unsigned char n) 74{ 75 return bits_in_byte[n]; 76} 77 78int set_bit(unsigned int nr,void * addr) 79{ 80 int mask, retval; 81 unsigned char *ADDR = (unsigned char *) addr; 82 83 ADDR += nr >> 3; 84 mask = 1 << ((nr & 0x07)); 85 retval = mask & *ADDR; 86 *ADDR |= mask; 87 return retval; 88} 89 90int clear_bit(unsigned int nr, void * addr) 91{ 92 int mask, retval; 93 unsigned char *ADDR = (unsigned char *) addr; 94 95 ADDR += nr >> 3; 96 mask = 1 << ((nr & 0x07)); 97 retval = mask & *ADDR; 98 *ADDR &= ~mask; 99 return retval; 100} 101 102int test_bit(unsigned int nr, const void * addr) 103{ 104 const __u32 *p = (const __u32 *)addr; 105 106 nr = nr ^ 0; 107 108 return ((1 << (nr & 31)) & (p[nr >> 5])) != 0; 109} 110 111int f2fs_test_bit(unsigned int nr, const char *p) 112{ 113 int mask; 114 char *addr = (char *)p; 115 116 addr += (nr >> 3); 117 mask = 1 << (7 - (nr & 0x07)); 118 return (mask & *addr) != 0; 119} 120 121int f2fs_set_bit(unsigned int nr, char *addr) 122{ 123 int mask; 124 int ret; 125 126 addr += (nr >> 3); 127 mask = 1 << (7 - (nr & 0x07)); 128 ret = mask & *addr; 129 *addr |= mask; 130 return ret; 131} 132 133int f2fs_clear_bit(unsigned int nr, char *addr) 134{ 135 int mask; 136 int ret; 137 138 addr += (nr >> 3); 139 mask = 1 << (7 - (nr & 0x07)); 140 ret = mask & *addr; 141 *addr &= ~mask; 142 return ret; 143} 144 145static inline unsigned long __ffs(unsigned long word) 146{ 147 int num = 0; 148 149#if BITS_PER_LONG == 64 150 if ((word & 0xffffffff) == 0) { 151 num += 32; 152 word >>= 32; 153 } 154#endif 155 if ((word & 0xffff) == 0) { 156 num += 16; 157 word >>= 16; 158 } 159 if ((word & 0xff) == 0) { 160 num += 8; 161 word >>= 8; 162 } 163 if ((word & 0xf) == 0) { 164 num += 4; 165 word >>= 4; 166 } 167 if ((word & 0x3) == 0) { 168 num += 2; 169 word >>= 2; 170 } 171 if ((word & 0x1) == 0) 172 num += 1; 173 return num; 174} 175 176unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 177 unsigned long offset) 178{ 179 const unsigned long *p = addr + BIT_WORD(offset); 180 unsigned long result = offset & ~(BITS_PER_LONG-1); 181 unsigned long tmp; 182 183 if (offset >= size) 184 return size; 185 size -= result; 186 offset %= BITS_PER_LONG; 187 if (offset) { 188 tmp = *(p++); 189 tmp &= (~0UL << offset); 190 if (size < BITS_PER_LONG) 191 goto found_first; 192 if (tmp) 193 goto found_middle; 194 size -= BITS_PER_LONG; 195 result += BITS_PER_LONG; 196 } 197 while (size & ~(BITS_PER_LONG-1)) { 198 if ((tmp = *(p++))) 199 goto found_middle; 200 result += BITS_PER_LONG; 201 size -= BITS_PER_LONG; 202 } 203 if (!size) 204 return result; 205 tmp = *p; 206 207found_first: 208 tmp &= (~0UL >> (BITS_PER_LONG - size)); 209 if (tmp == 0UL) /* Are any bits set? */ 210 return result + size; /* Nope. */ 211found_middle: 212 return result + __ffs(tmp); 213} 214 215/* 216 * Hashing code adapted from ext3 217 */ 218#define DELTA 0x9E3779B9 219 220static void TEA_transform(unsigned int buf[4], unsigned int const in[]) 221{ 222 __u32 sum = 0; 223 __u32 b0 = buf[0], b1 = buf[1]; 224 __u32 a = in[0], b = in[1], c = in[2], d = in[3]; 225 int n = 16; 226 227 do { 228 sum += DELTA; 229 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); 230 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); 231 } while (--n); 232 233 buf[0] += b0; 234 buf[1] += b1; 235 236} 237 238static void str2hashbuf(const char *msg, int len, unsigned int *buf, int num) 239{ 240 unsigned pad, val; 241 int i; 242 243 pad = (__u32)len | ((__u32)len << 8); 244 pad |= pad << 16; 245 246 val = pad; 247 if (len > num * 4) 248 len = num * 4; 249 for (i = 0; i < len; i++) { 250 if ((i % 4) == 0) 251 val = pad; 252 val = msg[i] + (val << 8); 253 if ((i % 4) == 3) { 254 *buf++ = val; 255 val = pad; 256 num--; 257 } 258 } 259 if (--num >= 0) 260 *buf++ = val; 261 while (--num >= 0) 262 *buf++ = pad; 263 264} 265 266/** 267 * Return hash value of directory entry 268 * @param name dentry name 269 * @param len name lenth 270 * @return return on success hash value, errno on failure 271 */ 272f2fs_hash_t f2fs_dentry_hash(const char *name, int len) 273{ 274 __u32 hash; 275 f2fs_hash_t f2fs_hash; 276 const char *p; 277 __u32 in[8], buf[4]; 278 279 /* special hash codes for special dentries */ 280 if (name[0] == '.') { 281 if (name[1] == '\0') { 282 f2fs_hash = F2FS_DOT_HASH; 283 goto exit; 284 } 285 if (name[1] == '.' && name[2] == '\0') { 286 f2fs_hash = F2FS_DDOT_HASH; 287 goto exit; 288 } 289 } 290 291 /* Initialize the default seed for the hash checksum functions */ 292 buf[0] = 0x67452301; 293 buf[1] = 0xefcdab89; 294 buf[2] = 0x98badcfe; 295 buf[3] = 0x10325476; 296 297 p = name; 298 while (len > 0) { 299 str2hashbuf(p, len, in, 4); 300 TEA_transform(buf, in); 301 len -= 16; 302 p += 16; 303 } 304 hash = buf[0]; 305 306 f2fs_hash = hash; 307exit: 308 f2fs_hash &= ~F2FS_HASH_COL_BIT; 309 310 return f2fs_hash; 311} 312 313unsigned int addrs_per_inode(struct f2fs_inode *i) 314{ 315 if (i->i_inline & F2FS_INLINE_XATTR) 316 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS; 317 return DEF_ADDRS_PER_INODE; 318} 319 320/* 321 * CRC32 322 */ 323#define CRCPOLY_LE 0xedb88320 324 325u_int32_t f2fs_cal_crc32(u_int32_t crc, void *buf, int len) 326{ 327 int i; 328 unsigned char *p = (unsigned char *)buf; 329 while (len--) { 330 crc ^= *p++; 331 for (i = 0; i < 8; i++) 332 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); 333 } 334 return crc; 335} 336 337int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len) 338{ 339 u_int32_t cal_crc = 0; 340 341 cal_crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, buf, len); 342 343 if (cal_crc != blk_crc) { 344 DBG(0,"CRC validation failed: cal_crc = %u \ 345 blk_crc = %u buff_size = 0x%x", 346 cal_crc, blk_crc, len); 347 return -1; 348 } 349 return 0; 350} 351 352/* 353 * device information 354 */ 355void f2fs_init_configuration(struct f2fs_configuration *c) 356{ 357 c->sector_size = DEFAULT_SECTOR_SIZE; 358 c->sectors_per_blk = DEFAULT_SECTORS_PER_BLOCK; 359 c->blks_per_seg = DEFAULT_BLOCKS_PER_SEGMENT; 360 361 /* calculated by overprovision ratio */ 362 c->reserved_segments = 48; 363 c->overprovision = 5; 364 c->segs_per_sec = 1; 365 c->secs_per_zone = 1; 366 c->heap = 1; 367 c->vol_label = ""; 368 c->device_name = NULL; 369 c->trim = 1; 370} 371 372static int is_mounted(const char *mpt, const char *device) 373{ 374 FILE *file = NULL; 375 struct mntent *mnt = NULL; 376 377 file = setmntent(mpt, "r"); 378 if (file == NULL) 379 return 0; 380 381 while ((mnt = getmntent(file)) != NULL) { 382 if (!strcmp(device, mnt->mnt_fsname)) 383 break; 384 } 385 endmntent(file); 386 return mnt ? 1 : 0; 387} 388 389int f2fs_dev_is_umounted(struct f2fs_configuration *c) 390{ 391 struct stat st_buf; 392 int ret = 0; 393 394 ret = is_mounted(MOUNTED, c->device_name); 395 if (ret) { 396 MSG(0, "\tError: Not available on mounted device!\n"); 397 return -1; 398 } 399 400 /* 401 * if failed due to /etc/mtab file not present 402 * try with /proc/mounts. 403 */ 404 ret = is_mounted("/proc/mounts", c->device_name); 405 if (ret) { 406 MSG(0, "\tError: Not available on mounted device!\n"); 407 return -1; 408 } 409 410 /* 411 * If f2fs is umounted with -l, the process can still use 412 * the file system. In this case, we should not format. 413 */ 414 if (stat(c->device_name, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) { 415 int fd = open(c->device_name, O_RDONLY | O_EXCL); 416 417 if (fd >= 0) { 418 close(fd); 419 } else if (errno == EBUSY) { 420 MSG(0, "\tError: In use by the system!\n"); 421 return -1; 422 } 423 } 424 return 0; 425} 426 427int f2fs_get_device_info(struct f2fs_configuration *c) 428{ 429 int32_t fd = 0; 430 uint32_t sector_size; 431 struct stat stat_buf; 432 struct hd_geometry geom; 433 434 fd = open(c->device_name, O_RDWR); 435 if (fd < 0) { 436 MSG(0, "\tError: Failed to open the device!\n"); 437 return -1; 438 } 439 c->fd = fd; 440 441 if (fstat(fd, &stat_buf) < 0 ) { 442 MSG(0, "\tError: Failed to get the device stat!\n"); 443 return -1; 444 } 445 446 if (S_ISREG(stat_buf.st_mode)) { 447 c->total_sectors = stat_buf.st_size / c->sector_size; 448 } else if (S_ISBLK(stat_buf.st_mode)) { 449 if (ioctl(fd, BLKSSZGET, §or_size) < 0) { 450 MSG(0, "\tError: Using the default sector size\n"); 451 } else { 452 if (c->sector_size < sector_size) { 453 MSG(0, "\tError: Cannot set the sector size to:" 454 " %d as the device does not support" 455 "\nSetting the sector size to : %d\n", 456 c->sector_size, sector_size); 457 c->sector_size = sector_size; 458 c->sectors_per_blk = PAGE_SIZE / sector_size; 459 } 460 } 461 462 if (ioctl(fd, BLKGETSIZE, &c->total_sectors) < 0) { 463 MSG(0, "\tError: Cannot get the device size\n"); 464 return -1; 465 } 466 467 if (ioctl(fd, HDIO_GETGEO, &geom) < 0) 468 c->start_sector = 0; 469 else 470 c->start_sector = geom.start; 471 } else { 472 MSG(0, "\tError: Volume type is not supported!!!\n"); 473 return -1; 474 } 475 476 MSG(0, "Info: sector size = %u\n", c->sector_size); 477 MSG(0, "Info: total sectors = %"PRIu64" (in 512bytes)\n", 478 c->total_sectors); 479 if (c->total_sectors < 480 (F2FS_MIN_VOLUME_SIZE / DEFAULT_SECTOR_SIZE)) { 481 MSG(0, "Error: Min volume size supported is %d\n", 482 F2FS_MIN_VOLUME_SIZE); 483 return -1; 484 } 485 486 return 0; 487} 488 489