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