• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * dir.c
3  *
4  * Many parts of codes are copied from Linux kernel/fs/f2fs.
5  *
6  * Copyright (C) 2015 Huawei Ltd.
7  * Witten by:
8  *   Hou Pengyang <houpengyang@huawei.com>
9  *   Liu Shuoran <liushuoran@huawei.com>
10  *   Jaegeuk Kim <jaegeuk@kernel.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16 #include "fsck.h"
17 #include "node.h"
18 #include <search.h>
19 
room_for_filename(const u8 * bitmap,int slots,int max_slots)20 static int room_for_filename(const u8 *bitmap, int slots, int max_slots)
21 {
22 	int bit_start = 0;
23 	int zero_start, zero_end;
24 next:
25 	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
26 	if (zero_start >= max_slots)
27 		return max_slots;
28 
29 	zero_end = find_next_bit_le(bitmap, max_slots, zero_start + 1);
30 
31 	if (zero_end - zero_start >= slots)
32 		return zero_start;
33 	bit_start = zero_end;
34 	goto next;
35 
36 }
37 
make_dentry_ptr(struct f2fs_dentry_ptr * d,struct f2fs_node * node_blk,void * src,int type)38 void make_dentry_ptr(struct f2fs_dentry_ptr *d, struct f2fs_node *node_blk,
39 							void *src, int type)
40 {
41 	if (type == 1) {
42 		struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
43 		d->max = NR_DENTRY_IN_BLOCK;
44 		d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
45 		d->bitmap = t->dentry_bitmap;
46 		d->dentry = t->dentry;
47 		d->filename = t->filename;
48 	} else {
49 		int entry_cnt = NR_INLINE_DENTRY(node_blk);
50 		int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(node_blk);
51 		int reserved_size = INLINE_RESERVED_SIZE(node_blk);
52 
53 		d->max = entry_cnt;
54 		d->nr_bitmap = bitmap_size;
55 		d->bitmap = (u8 *)src;
56 		d->dentry = (struct f2fs_dir_entry *)
57 				((char *)src + bitmap_size + reserved_size);
58 		d->filename = (__u8 (*)[F2FS_SLOT_LEN])((char *)src +
59 				bitmap_size + reserved_size +
60 				SIZE_OF_DIR_ENTRY * entry_cnt);
61 	}
62 }
63 
find_target_dentry(const u8 * name,unsigned int len,f2fs_hash_t namehash,int * max_slots,struct f2fs_dentry_ptr * d)64 static struct f2fs_dir_entry *find_target_dentry(const u8 *name,
65 		unsigned int len, f2fs_hash_t namehash, int *max_slots,
66 		struct f2fs_dentry_ptr *d)
67 {
68 	struct f2fs_dir_entry *de;
69 	unsigned long bit_pos = 0;
70 	int max_len = 0;
71 
72 	if (max_slots)
73 		*max_slots = 0;
74 	while (bit_pos < (unsigned long)d->max) {
75 		if (!test_bit_le(bit_pos, d->bitmap)) {
76 			bit_pos++;
77 			max_len++;
78 			continue;
79 		}
80 
81 		de = &d->dentry[bit_pos];
82 		if (le16_to_cpu(de->name_len) == len &&
83 			de->hash_code == namehash &&
84 			!memcmp(d->filename[bit_pos], name, len)) {
85 			goto found;
86 		}
87 
88 		if (max_slots && max_len > *max_slots)
89 			*max_slots = max_len;
90 		max_len = 0;
91 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
92 	}
93 	de = NULL;
94 found:
95 	if (max_slots && max_len > *max_slots)
96 		*max_slots = max_len;
97 	return de;
98 }
99 
find_in_block(void * block,const u8 * name,int len,f2fs_hash_t namehash,int * max_slots)100 static struct f2fs_dir_entry *find_in_block(void *block,
101 		const u8 *name, int len, f2fs_hash_t namehash,
102 		int *max_slots)
103 {
104 	struct f2fs_dentry_ptr d;
105 
106 	make_dentry_ptr(&d, NULL, block, 1);
107 	return find_target_dentry(name, len, namehash, max_slots, &d);
108 }
109 
find_in_level(struct f2fs_sb_info * sbi,struct f2fs_node * dir,unsigned int level,struct dentry * de)110 static int find_in_level(struct f2fs_sb_info *sbi, struct f2fs_node *dir,
111 		unsigned int level, struct dentry *de)
112 {
113 	unsigned int nbucket, nblock;
114 	unsigned int bidx, end_block;
115 	struct f2fs_dir_entry *dentry = NULL;
116 	struct dnode_of_data dn;
117 	void *dentry_blk;
118 	int max_slots = 214;
119 	nid_t ino = le32_to_cpu(dir->footer.ino);
120 	f2fs_hash_t namehash;
121 	unsigned int dir_level = dir->i.i_dir_level;
122 	int ret = 0;
123 
124 	namehash = f2fs_dentry_hash(get_encoding(sbi), IS_CASEFOLDED(&dir->i),
125 					de->name, de->len);
126 
127 	nbucket = dir_buckets(level, dir_level);
128 	nblock = bucket_blocks(level);
129 
130 	bidx = dir_block_index(level, dir_level, le32_to_cpu(namehash) % nbucket);
131 	end_block = bidx + nblock;
132 
133 	dentry_blk = calloc(BLOCK_SZ, 1);
134 	ASSERT(dentry_blk);
135 
136 	memset(&dn, 0, sizeof(dn));
137 	for (; bidx < end_block; bidx++) {
138 
139 		/* Firstly, we should know direct node of target data blk */
140 		if (dn.node_blk && dn.node_blk != dn.inode_blk)
141 			free(dn.node_blk);
142 
143 		set_new_dnode(&dn, dir, NULL, ino);
144 		get_dnode_of_data(sbi, &dn, bidx, LOOKUP_NODE);
145 		if (dn.data_blkaddr == NULL_ADDR)
146 			continue;
147 
148 		ret = dev_read_block(dentry_blk, dn.data_blkaddr);
149 		ASSERT(ret >= 0);
150 
151 		dentry = find_in_block(dentry_blk, de->name, de->len,
152 						namehash, &max_slots);
153 		if (dentry) {
154 			ret = 1;
155 			de->ino = le32_to_cpu(dentry->ino);
156 			break;
157 		}
158 	}
159 
160 	if (dn.node_blk && dn.node_blk != dn.inode_blk)
161 		free(dn.node_blk);
162 	free(dentry_blk);
163 
164 	return ret;
165 }
166 
f2fs_find_entry(struct f2fs_sb_info * sbi,struct f2fs_node * dir,struct dentry * de)167 static int f2fs_find_entry(struct f2fs_sb_info *sbi,
168 				struct f2fs_node *dir, struct dentry *de)
169 {
170 	unsigned int max_depth;
171 	unsigned int level;
172 
173 	max_depth = le32_to_cpu(dir->i.i_current_depth);
174 	for (level = 0; level < max_depth; level ++) {
175 		if (find_in_level(sbi, dir, level, de))
176 			return 1;
177 	}
178 	return 0;
179 }
180 
181 /* return ino if file exists, otherwise return 0 */
f2fs_lookup(struct f2fs_sb_info * sbi,struct f2fs_node * dir,u8 * name,int len)182 nid_t f2fs_lookup(struct f2fs_sb_info *sbi, struct f2fs_node *dir,
183 				u8 *name, int len)
184 {
185 	int err;
186 	struct dentry de = {
187 		.name = name,
188 		.len = len,
189 	};
190 
191 	err = f2fs_find_entry(sbi, dir, &de);
192 	if (err == 1)
193 		return de.ino;
194 	else
195 		return 0;
196 }
197 
f2fs_update_dentry(nid_t ino,int file_type,struct f2fs_dentry_ptr * d,const unsigned char * name,int len,f2fs_hash_t name_hash,unsigned int bit_pos)198 static void f2fs_update_dentry(nid_t ino, int file_type,
199 		struct f2fs_dentry_ptr *d,
200 		const unsigned char *name, int len, f2fs_hash_t name_hash,
201 		unsigned int bit_pos)
202 {
203 	struct f2fs_dir_entry *de;
204 	int slots = GET_DENTRY_SLOTS(len);
205 	int i;
206 
207 	de = &d->dentry[bit_pos];
208 	de->name_len = cpu_to_le16(len);
209 	de->hash_code = name_hash;
210 	memcpy(d->filename[bit_pos], name, len);
211 	d->filename[bit_pos][len] = 0;
212 	de->ino = cpu_to_le32(ino);
213 	de->file_type = file_type;
214 	for (i = 0; i < slots; i++)
215 		test_and_set_bit_le(bit_pos + i, d->bitmap);
216 }
217 
218 /*
219  * f2fs_add_link - Add a new file(dir) to parent dir.
220  */
f2fs_add_link(struct f2fs_sb_info * sbi,struct f2fs_node * parent,const unsigned char * name,int name_len,nid_t ino,int file_type,block_t p_blkaddr,int inc_link)221 int f2fs_add_link(struct f2fs_sb_info *sbi, struct f2fs_node *parent,
222 			const unsigned char *name, int name_len, nid_t ino,
223 			int file_type, block_t p_blkaddr, int inc_link)
224 {
225 	int level = 0, current_depth, bit_pos;
226 	int nbucket, nblock, bidx, block;
227 	int slots = GET_DENTRY_SLOTS(name_len);
228 	f2fs_hash_t dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
229 						IS_CASEFOLDED(&parent->i),
230 						name, name_len);
231 	struct f2fs_dentry_block *dentry_blk;
232 	struct f2fs_dentry_ptr d;
233 	struct dnode_of_data dn;
234 	nid_t pino = le32_to_cpu(parent->footer.ino);
235 	unsigned int dir_level = parent->i.i_dir_level;
236 	int ret;
237 
238 	if (parent == NULL)
239 		return -EINVAL;
240 
241 	if (!pino) {
242 		ERR_MSG("Wrong parent ino:%d \n", pino);
243 		return -EINVAL;
244 	}
245 
246 	dentry_blk = calloc(BLOCK_SZ, 1);
247 	ASSERT(dentry_blk);
248 
249 	current_depth = le32_to_cpu(parent->i.i_current_depth);
250 start:
251 	if (current_depth == MAX_DIR_HASH_DEPTH) {
252 		free(dentry_blk);
253 		ERR_MSG("\tError: MAX_DIR_HASH\n");
254 		return -ENOSPC;
255 	}
256 
257 	/* Need a new dentry block */
258 	if (level == current_depth)
259 		++current_depth;
260 
261 	nbucket = dir_buckets(level, dir_level);
262 	nblock = bucket_blocks(level);
263 	bidx = dir_block_index(level, dir_level, le32_to_cpu(dentry_hash) % nbucket);
264 
265 	memset(&dn, 0, sizeof(dn));
266 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
267 
268 		/* Firstly, we should know the direct node of target data blk */
269 		if (dn.node_blk && dn.node_blk != dn.inode_blk)
270 			free(dn.node_blk);
271 
272 		set_new_dnode(&dn, parent, NULL, pino);
273 		get_dnode_of_data(sbi, &dn, block, ALLOC_NODE);
274 
275 		if (dn.data_blkaddr == NULL_ADDR) {
276 			new_data_block(sbi, dentry_blk, &dn, CURSEG_HOT_DATA);
277 		} else {
278 			ret = dev_read_block(dentry_blk, dn.data_blkaddr);
279 			ASSERT(ret >= 0);
280 		}
281 		bit_pos = room_for_filename(dentry_blk->dentry_bitmap,
282 				slots, NR_DENTRY_IN_BLOCK);
283 
284 		if (bit_pos < NR_DENTRY_IN_BLOCK)
285 			goto add_dentry;
286 	}
287 	level ++;
288 	goto start;
289 
290 add_dentry:
291 	make_dentry_ptr(&d, NULL, (void *)dentry_blk, 1);
292 	f2fs_update_dentry(ino, file_type, &d, name, name_len, dentry_hash, bit_pos);
293 
294 	ret = dev_write_block(dentry_blk, dn.data_blkaddr);
295 	ASSERT(ret >= 0);
296 
297 	/*
298 	 * Parent inode needs updating, because its inode info may be changed.
299 	 * such as i_current_depth and i_blocks.
300 	 */
301 	if (parent->i.i_current_depth != cpu_to_le32(current_depth)) {
302 		parent->i.i_current_depth = cpu_to_le32(current_depth);
303 		dn.idirty = 1;
304 	}
305 
306 	/* Update parent's i_links info*/
307 	if (inc_link && (file_type == F2FS_FT_DIR)){
308 		u32 links = le32_to_cpu(parent->i.i_links);
309 		parent->i.i_links = cpu_to_le32(links + 1);
310 		dn.idirty = 1;
311 	}
312 
313 	if ((__u64)((block + 1) * F2FS_BLKSIZE) >
314 					le64_to_cpu(parent->i.i_size)) {
315 		parent->i.i_size = cpu_to_le64((block + 1) * F2FS_BLKSIZE);
316 		dn.idirty = 1;
317 	}
318 
319 	if (dn.ndirty) {
320 		ret = dev_write_block(dn.node_blk, dn.node_blkaddr);
321 		ASSERT(ret >= 0);
322 	}
323 
324 	if (dn.idirty) {
325 		ASSERT(parent == dn.inode_blk);
326 		ret = write_inode(dn.inode_blk, p_blkaddr);
327 		ASSERT(ret >= 0);
328 	}
329 
330 	if (dn.node_blk != dn.inode_blk)
331 		free(dn.node_blk);
332 	free(dentry_blk);
333 	return 0;
334 }
335 
make_empty_dir(struct f2fs_sb_info * sbi,struct f2fs_node * inode)336 static void make_empty_dir(struct f2fs_sb_info *sbi, struct f2fs_node *inode)
337 {
338 	struct f2fs_dentry_block *dent_blk;
339 	nid_t ino = le32_to_cpu(inode->footer.ino);
340 	nid_t pino = le32_to_cpu(inode->i.i_pino);
341 	struct f2fs_summary sum;
342 	struct node_info ni;
343 	block_t blkaddr = NULL_ADDR;
344 	int ret;
345 
346 	get_node_info(sbi, ino, &ni);
347 
348 	dent_blk = calloc(BLOCK_SZ, 1);
349 	ASSERT(dent_blk);
350 
351 	dent_blk->dentry[0].hash_code = 0;
352 	dent_blk->dentry[0].ino = cpu_to_le32(ino);
353 	dent_blk->dentry[0].name_len = cpu_to_le16(1);
354 	dent_blk->dentry[0].file_type = F2FS_FT_DIR;
355 	memcpy(dent_blk->filename[0], ".", 1);
356 
357 	dent_blk->dentry[1].hash_code = 0;
358 	dent_blk->dentry[1].ino = cpu_to_le32(pino);
359 	dent_blk->dentry[1].name_len = cpu_to_le16(2);
360 	dent_blk->dentry[1].file_type = F2FS_FT_DIR;
361 	memcpy(dent_blk->filename[1], "..", 2);
362 
363 	test_and_set_bit_le(0, dent_blk->dentry_bitmap);
364 	test_and_set_bit_le(1, dent_blk->dentry_bitmap);
365 
366 	set_summary(&sum, ino, 0, ni.version);
367 	ret = reserve_new_block(sbi, &blkaddr, &sum, CURSEG_HOT_DATA, 0);
368 	ASSERT(!ret);
369 
370 	ret = dev_write_block(dent_blk, blkaddr);
371 	ASSERT(ret >= 0);
372 
373 	inode->i.i_addr[get_extra_isize(inode)] = cpu_to_le32(blkaddr);
374 	free(dent_blk);
375 }
376 
page_symlink(struct f2fs_sb_info * sbi,struct f2fs_node * inode,const char * symname,int symlen)377 static void page_symlink(struct f2fs_sb_info *sbi, struct f2fs_node *inode,
378 					const char *symname, int symlen)
379 {
380 	nid_t ino = le32_to_cpu(inode->footer.ino);
381 	struct f2fs_summary sum;
382 	struct node_info ni;
383 	char *data_blk;
384 	block_t blkaddr = NULL_ADDR;
385 	int ret;
386 
387 	get_node_info(sbi, ino, &ni);
388 
389 	/* store into inline_data */
390 	if ((unsigned long)(symlen + 1) <= MAX_INLINE_DATA(inode)) {
391 		inode->i.i_inline |= F2FS_INLINE_DATA;
392 		inode->i.i_inline |= F2FS_DATA_EXIST;
393 		memcpy(inline_data_addr(inode), symname, symlen);
394 		return;
395 	}
396 
397 	data_blk = calloc(BLOCK_SZ, 1);
398 	ASSERT(data_blk);
399 
400 	memcpy(data_blk, symname, symlen);
401 
402 	set_summary(&sum, ino, 0, ni.version);
403 	ret = reserve_new_block(sbi, &blkaddr, &sum, CURSEG_WARM_DATA, 1);
404 	ASSERT(!ret);
405 
406 	ret = dev_write_block(data_blk, blkaddr);
407 	ASSERT(ret >= 0);
408 
409 	inode->i.i_addr[get_extra_isize(inode)] = cpu_to_le32(blkaddr);
410 	free(data_blk);
411 }
412 
is_extension_exist(const char * s,const char * sub)413 static inline int is_extension_exist(const char *s,
414 					const char *sub)
415 {
416 	unsigned int slen = strlen(s);
417 	unsigned int  sublen = strlen(sub);
418 	int i;
419 
420 	/*
421 	 * filename format of multimedia file should be defined as:
422 	 * "filename + '.' + extension + (optional: '.' + temp extension)".
423 	 */
424 	if (slen < sublen + 2)
425 		return 0;
426 
427 	for (i = 1; i < slen - sublen; i++) {
428 		if (s[i] != '.')
429 			continue;
430 		if (!strncasecmp(s + i + 1, sub, sublen))
431 			return 1;
432 	}
433 
434 	return 0;
435 }
436 
set_file_temperature(struct f2fs_sb_info * sbi,struct f2fs_node * node_blk,const unsigned char * name)437 static void set_file_temperature(struct f2fs_sb_info *sbi,
438 				struct f2fs_node *node_blk,
439 				const unsigned char *name)
440 {
441 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
442 	int i, cold_count, hot_count;
443 
444 	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
445 	hot_count = sbi->raw_super->hot_ext_count;
446 
447 	for (i = 0; i < cold_count + hot_count; i++) {
448 		if (is_extension_exist((const char *)name,
449 					(const char *)extlist[i]))
450 			break;
451 	}
452 
453 	if (i == cold_count + hot_count)
454 		return;
455 
456 	if (i < cold_count)
457 		node_blk->i.i_advise |= FADVISE_COLD_BIT;
458 	else
459 		node_blk->i.i_advise |= FADVISE_HOT_BIT;
460 }
461 
init_inode_block(struct f2fs_sb_info * sbi,struct f2fs_node * node_blk,struct dentry * de)462 static void init_inode_block(struct f2fs_sb_info *sbi,
463 		struct f2fs_node *node_blk, struct dentry *de)
464 {
465 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
466 	mode_t mode = de->mode;
467 	int links = 1;
468 	unsigned int size;
469 	int blocks = 1;
470 
471 	if (de->file_type == F2FS_FT_DIR) {
472 		mode |= S_IFDIR;
473 		size = 4096;
474 		links++;
475 		blocks++;
476 	} else if (de->file_type == F2FS_FT_REG_FILE) {
477 		mode |= S_IFREG;
478 		size = 0;
479 	} else if (de->file_type == F2FS_FT_SYMLINK) {
480 		ASSERT(de->link);
481 		mode |= S_IFLNK;
482 		size = strlen(de->link);
483 		if (size + 1 > MAX_INLINE_DATA(node_blk))
484 			blocks++;
485 	} else {
486 		ASSERT(0);
487 	}
488 
489 	node_blk->i.i_mode = cpu_to_le16(mode);
490 	node_blk->i.i_advise = 0;
491 	node_blk->i.i_uid = cpu_to_le32(de->uid);
492 	node_blk->i.i_gid = cpu_to_le32(de->gid);
493 	node_blk->i.i_links = cpu_to_le32(links);
494 	node_blk->i.i_size = cpu_to_le32(size);
495 	node_blk->i.i_blocks = cpu_to_le32(blocks);
496 	node_blk->i.i_atime = cpu_to_le64(de->mtime);
497 	node_blk->i.i_ctime = cpu_to_le64(de->mtime);
498 	node_blk->i.i_mtime = cpu_to_le64(de->mtime);
499 	node_blk->i.i_atime_nsec = 0;
500 	node_blk->i.i_ctime_nsec = 0;
501 	node_blk->i.i_mtime_nsec = 0;
502 	node_blk->i.i_generation = 0;
503 	if (de->file_type == F2FS_FT_DIR)
504 		node_blk->i.i_current_depth = cpu_to_le32(1);
505 	else
506 		node_blk->i.i_current_depth = cpu_to_le32(0);
507 	node_blk->i.i_xattr_nid = 0;
508 	node_blk->i.i_flags = 0;
509 	node_blk->i.i_inline = F2FS_INLINE_XATTR;
510 	node_blk->i.i_pino = cpu_to_le32(de->pino);
511 	node_blk->i.i_namelen = cpu_to_le32(de->len);
512 	memcpy(node_blk->i.i_name, de->name, de->len);
513 	node_blk->i.i_name[de->len] = 0;
514 
515 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
516 		node_blk->i.i_inline |= F2FS_EXTRA_ATTR;
517 		node_blk->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
518 	}
519 
520 	set_file_temperature(sbi, node_blk, de->name);
521 
522 	node_blk->footer.ino = cpu_to_le32(de->ino);
523 	node_blk->footer.nid = cpu_to_le32(de->ino);
524 	node_blk->footer.flag = 0;
525 	node_blk->footer.cp_ver = ckpt->checkpoint_ver;
526 	set_cold_node(node_blk, S_ISDIR(mode));
527 
528 	if (S_ISDIR(mode)) {
529 		make_empty_dir(sbi, node_blk);
530 	} else if (S_ISLNK(mode)) {
531 		page_symlink(sbi, node_blk, de->link, size);
532 
533 		free(de->link);
534 		de->link = NULL;
535 	}
536 
537 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
538 		node_blk->i.i_inode_checksum =
539 			cpu_to_le32(f2fs_inode_chksum(node_blk));
540 }
541 
convert_inline_dentry(struct f2fs_sb_info * sbi,struct f2fs_node * node,block_t p_blkaddr)542 int convert_inline_dentry(struct f2fs_sb_info *sbi, struct f2fs_node *node,
543 							block_t p_blkaddr)
544 {
545 	struct f2fs_inode *inode = &(node->i);
546 	unsigned int dir_level = node->i.i_dir_level;
547 	nid_t ino = le32_to_cpu(node->footer.ino);
548 	char inline_data[MAX_INLINE_DATA(node)];
549 	struct dnode_of_data dn;
550 	struct f2fs_dentry_ptr d;
551 	unsigned long bit_pos = 0;
552 	int ret = 0;
553 
554 	if (!(inode->i_inline & F2FS_INLINE_DENTRY))
555 		return 0;
556 
557 	memcpy(inline_data, inline_data_addr(node), MAX_INLINE_DATA(node));
558 	memset(inline_data_addr(node), 0, MAX_INLINE_DATA(node));
559 	inode->i_inline &= ~F2FS_INLINE_DENTRY;
560 
561 	ret = dev_write_block(node, p_blkaddr);
562 	ASSERT(ret >= 0);
563 
564 	memset(&dn, 0, sizeof(dn));
565 	if (!dir_level) {
566 		struct f2fs_dentry_block *dentry_blk;
567 		struct f2fs_dentry_ptr src, dst;
568 
569 		dentry_blk = calloc(BLOCK_SZ, 1);
570 		ASSERT(dentry_blk);
571 
572 		set_new_dnode(&dn, node, NULL, ino);
573 		get_dnode_of_data(sbi, &dn, 0, ALLOC_NODE);
574 		if (dn.data_blkaddr == NULL_ADDR)
575 			new_data_block(sbi, dentry_blk, &dn, CURSEG_HOT_DATA);
576 
577 		make_dentry_ptr(&src, node, (void *)inline_data, 2);
578 		make_dentry_ptr(&dst, NULL, (void *)dentry_blk, 1);
579 
580 		 /* copy data from inline dentry block to new dentry block */
581 		memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
582 		memset(dst.bitmap + src.nr_bitmap, 0,
583 					dst.nr_bitmap - src.nr_bitmap);
584 
585 		memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
586 		memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
587 
588 		ret = dev_write_block(dentry_blk, dn.data_blkaddr);
589 		ASSERT(ret >= 0);
590 
591 		MSG(1, "%s: copy inline entry to block\n", __func__);
592 
593 		free(dentry_blk);
594 		return ret;
595 	}
596 
597 	make_empty_dir(sbi, node);
598 	make_dentry_ptr(&d, node, (void *)inline_data, 2);
599 
600 	while (bit_pos < (unsigned long)d.max) {
601 		struct f2fs_dir_entry *de;
602 		const unsigned char *filename;
603 		int namelen;
604 
605 		if (!test_bit_le(bit_pos, d.bitmap)) {
606 			bit_pos++;
607 			continue;
608 		}
609 
610 		de = &d.dentry[bit_pos];
611 		if (!de->name_len) {
612 			bit_pos++;
613 			continue;
614 		}
615 
616 		filename = d.filename[bit_pos];
617 		namelen = le32_to_cpu(de->name_len);
618 
619 		if (is_dot_dotdot(filename, namelen)) {
620 			bit_pos += GET_DENTRY_SLOTS(namelen);
621 			continue;
622 		}
623 
624 		ret = f2fs_add_link(sbi, node, filename, namelen,
625 				le32_to_cpu(de->ino),
626 				de->file_type, p_blkaddr, 0);
627 		if (ret)
628 			MSG(0, "Convert file \"%s\" ERR=%d\n", filename, ret);
629 		else
630 			MSG(1, "%s: add inline entry to block\n", __func__);
631 
632 		bit_pos += GET_DENTRY_SLOTS(namelen);
633 	}
634 
635 	return 0;
636 }
637 
cmp_from_devino(const void * a,const void * b)638 static int cmp_from_devino(const void *a, const void *b) {
639 	u64 devino_a = ((struct hardlink_cache_entry*) a)->from_devino;
640 	u64 devino_b = ((struct hardlink_cache_entry*) b)->from_devino;
641 
642 	return (devino_a > devino_b) - (devino_a < devino_b);
643 }
644 
f2fs_search_hardlink(struct f2fs_sb_info * sbi,struct dentry * de)645 struct hardlink_cache_entry *f2fs_search_hardlink(struct f2fs_sb_info *sbi,
646 						struct dentry *de)
647 {
648 	struct hardlink_cache_entry *find_hardlink = NULL;
649 	struct hardlink_cache_entry *found_hardlink = NULL;
650 	void *search_result;
651 
652 	/* This might be a hardlink, try to find it in the cache */
653 	find_hardlink = calloc(1, sizeof(struct hardlink_cache_entry));
654 	find_hardlink->from_devino = de->from_devino;
655 
656 	search_result = tsearch(find_hardlink, &(sbi->hardlink_cache),
657 				cmp_from_devino);
658 	ASSERT(search_result != 0);
659 
660 	found_hardlink = *(struct hardlink_cache_entry**) search_result;
661 	ASSERT(find_hardlink->from_devino == found_hardlink->from_devino);
662 
663 	/* If it was already in the cache, free the entry we just created */
664 	if (found_hardlink != find_hardlink)
665 		free(find_hardlink);
666 
667 	return found_hardlink;
668 }
669 
f2fs_create(struct f2fs_sb_info * sbi,struct dentry * de)670 int f2fs_create(struct f2fs_sb_info *sbi, struct dentry *de)
671 {
672 	struct f2fs_node *parent, *child;
673 	struct hardlink_cache_entry *found_hardlink = NULL;
674 	struct node_info ni, hardlink_ni;
675 	struct f2fs_summary sum;
676 	block_t blkaddr = NULL_ADDR;
677 	int ret;
678 
679 	/* Find if there is a */
680 	get_node_info(sbi, de->pino, &ni);
681 	if (ni.blk_addr == NULL_ADDR) {
682 		MSG(0, "No parent directory pino=%x\n", de->pino);
683 		return -1;
684 	}
685 
686 	if (de->from_devino)
687 		found_hardlink = f2fs_search_hardlink(sbi, de);
688 
689 	parent = calloc(BLOCK_SZ, 1);
690 	ASSERT(parent);
691 
692 	ret = dev_read_block(parent, ni.blk_addr);
693 	ASSERT(ret >= 0);
694 
695 	/* Must convert inline dentry before the following opertions */
696 	ret = convert_inline_dentry(sbi, parent, ni.blk_addr);
697 	if (ret) {
698 		MSG(0, "Convert inline dentry for pino=%x failed.\n", de->pino);
699 		return -1;
700 	}
701 
702 	ret = f2fs_find_entry(sbi, parent, de);
703 	if (ret) {
704 		MSG(0, "Skip the existing \"%s\" pino=%x ERR=%d\n",
705 					de->name, de->pino, ret);
706 		if (de->file_type == F2FS_FT_REG_FILE)
707 			de->ino = 0;
708 		goto free_parent_dir;
709 	}
710 
711 	child = calloc(BLOCK_SZ, 1);
712 	ASSERT(child);
713 
714 	if (found_hardlink && found_hardlink->to_ino) {
715 		/*
716 		 * If we found this devino in the cache, we're creating a
717 		 * hard link.
718 		 */
719 		get_node_info(sbi, found_hardlink->to_ino, &hardlink_ni);
720 		if (hardlink_ni.blk_addr == NULL_ADDR) {
721 			MSG(1, "No original inode for hard link to_ino=%x\n",
722 				found_hardlink->to_ino);
723 			return -1;
724 		}
725 
726 		/* Use previously-recorded inode */
727 		de->ino = found_hardlink->to_ino;
728 		blkaddr = hardlink_ni.blk_addr;
729 		MSG(1, "Info: Creating \"%s\" as hard link to inode %d\n",
730 				de->path, de->ino);
731 	} else {
732 		f2fs_alloc_nid(sbi, &de->ino);
733 	}
734 
735 	init_inode_block(sbi, child, de);
736 
737 	ret = f2fs_add_link(sbi, parent, child->i.i_name,
738 				le32_to_cpu(child->i.i_namelen),
739 				le32_to_cpu(child->footer.ino),
740 				map_de_type(le16_to_cpu(child->i.i_mode)),
741 				ni.blk_addr, 1);
742 	if (ret) {
743 		MSG(0, "Skip the existing \"%s\" pino=%x ERR=%d\n",
744 					de->name, de->pino, ret);
745 		goto free_child_dir;
746 	}
747 
748 	if (found_hardlink) {
749 		if (!found_hardlink->to_ino) {
750 			MSG(2, "Adding inode %d from %s to hardlink cache\n",
751 				de->ino, de->path);
752 			found_hardlink->to_ino = de->ino;
753 		} else {
754 			/* Replace child with original block */
755 			free(child);
756 
757 			child = calloc(BLOCK_SZ, 1);
758 			ASSERT(child);
759 
760 			ret = dev_read_block(child, blkaddr);
761 			ASSERT(ret >= 0);
762 
763 			/* Increment links and skip to writing block */
764 			child->i.i_links = cpu_to_le32(
765 					le32_to_cpu(child->i.i_links) + 1);
766 			MSG(2, "Number of links on inode %d is now %d\n",
767 				de->ino, le32_to_cpu(child->i.i_links));
768 			goto write_child_dir;
769 		}
770 	}
771 
772 	/* write child */
773 	set_summary(&sum, de->ino, 0, ni.version);
774 	ret = reserve_new_block(sbi, &blkaddr, &sum, CURSEG_HOT_NODE, 1);
775 	ASSERT(!ret);
776 
777 	/* update nat info */
778 	update_nat_blkaddr(sbi, de->ino, de->ino, blkaddr);
779 
780 write_child_dir:
781 	ret = dev_write_block(child, blkaddr);
782 	ASSERT(ret >= 0);
783 
784 	update_free_segments(sbi);
785 	MSG(1, "Info: Create %s -> %s\n"
786 		"  -- ino=%x, type=%x, mode=%x, uid=%x, "
787 		"gid=%x, cap=%"PRIx64", size=%lu, link=%u "
788 		"blocks=%"PRIx64" pino=%x\n",
789 		de->full_path, de->path,
790 		de->ino, de->file_type, de->mode,
791 		de->uid, de->gid, de->capabilities, de->size,
792 		le32_to_cpu(child->i.i_links),
793 		le64_to_cpu(child->i.i_blocks),
794 		de->pino);
795 free_child_dir:
796 	free(child);
797 free_parent_dir:
798 	free(parent);
799 	return 0;
800 }
801 
f2fs_mkdir(struct f2fs_sb_info * sbi,struct dentry * de)802 int f2fs_mkdir(struct f2fs_sb_info *sbi, struct dentry *de)
803 {
804 	return f2fs_create(sbi, de);
805 }
806 
f2fs_symlink(struct f2fs_sb_info * sbi,struct dentry * de)807 int f2fs_symlink(struct f2fs_sb_info *sbi, struct dentry *de)
808 {
809 	return f2fs_create(sbi, de);
810 }
811 
f2fs_find_path(struct f2fs_sb_info * sbi,char * path,nid_t * ino)812 int f2fs_find_path(struct f2fs_sb_info *sbi, char *path, nid_t *ino)
813 {
814 	struct f2fs_node *parent;
815 	struct node_info ni;
816 	struct dentry de;
817 	int err = 0;
818 	int ret;
819 	char *p;
820 
821 	if (path[0] != '/')
822 		return -ENOENT;
823 
824 	*ino = F2FS_ROOT_INO(sbi);
825 	parent = calloc(BLOCK_SZ, 1);
826 	ASSERT(parent);
827 
828 	p = strtok(path, "/");
829 	while (p) {
830 		de.name = (const u8 *)p;
831 		de.len = strlen(p);
832 
833 		get_node_info(sbi, *ino, &ni);
834 		if (ni.blk_addr == NULL_ADDR) {
835 			err = -ENOENT;
836 			goto err;
837 		}
838 		ret = dev_read_block(parent, ni.blk_addr);
839 		ASSERT(ret >= 0);
840 
841 		ret = f2fs_find_entry(sbi, parent, &de);
842 		if (!ret) {
843 			err = -ENOENT;
844 			goto err;
845 		}
846 
847 		*ino = de.ino;
848 		p = strtok(NULL, "/");
849 	}
850 err:
851 	free(parent);
852 	return err;
853 }
854