libf2fs.c revision 5005fb18214a234e944dcb74a10b8719f672add2
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\n",
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->total_sectors = 0;
358	c->sector_size = DEFAULT_SECTOR_SIZE;
359	c->sectors_per_blk = DEFAULT_SECTORS_PER_BLOCK;
360	c->blks_per_seg = DEFAULT_BLOCKS_PER_SEGMENT;
361
362	/* calculated by overprovision ratio */
363	c->reserved_segments = 48;
364	c->overprovision = 5;
365	c->segs_per_sec = 1;
366	c->secs_per_zone = 1;
367	c->heap = 1;
368	c->vol_label = "";
369	c->device_name = NULL;
370	c->trim = 1;
371}
372
373static int is_mounted(const char *mpt, const char *device)
374{
375	FILE *file = NULL;
376	struct mntent *mnt = NULL;
377
378	file = setmntent(mpt, "r");
379	if (file == NULL)
380		return 0;
381
382	while ((mnt = getmntent(file)) != NULL) {
383		if (!strcmp(device, mnt->mnt_fsname))
384			break;
385	}
386	endmntent(file);
387	return mnt ? 1 : 0;
388}
389
390int f2fs_dev_is_umounted(struct f2fs_configuration *c)
391{
392	struct stat st_buf;
393	int ret = 0;
394
395	ret = is_mounted(MOUNTED, c->device_name);
396	if (ret) {
397		MSG(0, "\tError: Not available on mounted device!\n");
398		return -1;
399	}
400
401	/*
402	 * if failed due to /etc/mtab file not present
403	 * try with /proc/mounts.
404	 */
405	ret = is_mounted("/proc/mounts", c->device_name);
406	if (ret) {
407		MSG(0, "\tError: Not available on mounted device!\n");
408		return -1;
409	}
410
411	/*
412	 * If f2fs is umounted with -l, the process can still use
413	 * the file system. In this case, we should not format.
414	 */
415	if (stat(c->device_name, &st_buf) == 0 && S_ISBLK(st_buf.st_mode)) {
416		int fd = open(c->device_name, O_RDONLY | O_EXCL);
417
418		if (fd >= 0) {
419			close(fd);
420		} else if (errno == EBUSY) {
421			MSG(0, "\tError: In use by the system!\n");
422			return -1;
423		}
424	}
425	return 0;
426}
427
428int f2fs_get_device_info(struct f2fs_configuration *c)
429{
430	int32_t fd = 0;
431	uint32_t sector_size;
432	struct stat stat_buf;
433	struct hd_geometry geom;
434	u_int64_t wanted_total_sectors = c->total_sectors;
435
436	fd = open(c->device_name, O_RDWR);
437	if (fd < 0) {
438		MSG(0, "\tError: Failed to open the device!\n");
439		return -1;
440	}
441	c->fd = fd;
442
443	if (fstat(fd, &stat_buf) < 0 ) {
444		MSG(0, "\tError: Failed to get the device stat!\n");
445		return -1;
446	}
447
448	if (S_ISREG(stat_buf.st_mode)) {
449		c->total_sectors = stat_buf.st_size / c->sector_size;
450	} else if (S_ISBLK(stat_buf.st_mode)) {
451		if (ioctl(fd, BLKSSZGET, &sector_size) < 0) {
452			MSG(0, "\tError: Using the default sector size\n");
453		} else {
454			if (c->sector_size < sector_size) {
455				MSG(0, "\tError: Cannot set the sector size to:"
456					" %d as the device does not support"
457					"\nSetting the sector size to : %d\n",
458					c->sector_size, sector_size);
459				c->sector_size = sector_size;
460				c->sectors_per_blk = PAGE_SIZE / sector_size;
461			}
462		}
463
464		if (ioctl(fd, BLKGETSIZE, &c->total_sectors) < 0) {
465			MSG(0, "\tError: Cannot get the device size\n");
466			return -1;
467		}
468
469		if (ioctl(fd, HDIO_GETGEO, &geom) < 0)
470			c->start_sector = 0;
471		else
472			c->start_sector = geom.start;
473	} else {
474		MSG(0, "\tError: Volume type is not supported!!!\n");
475		return -1;
476	}
477	if (wanted_total_sectors && wanted_total_sectors < c->total_sectors) {
478		MSG(0, "Info: total device sectors = %"PRIu64" (in 512bytes)\n",
479					c->total_sectors);
480		c->total_sectors = wanted_total_sectors;
481
482	}
483	MSG(0, "Info: sector size = %u\n", c->sector_size);
484	MSG(0, "Info: total sectors = %"PRIu64" (in 512bytes)\n",
485					c->total_sectors);
486	if (c->total_sectors <
487			(F2FS_MIN_VOLUME_SIZE / DEFAULT_SECTOR_SIZE)) {
488		MSG(0, "Error: Min volume size supported is %d\n",
489				F2FS_MIN_VOLUME_SIZE);
490		return -1;
491	}
492
493	return 0;
494}
495
496