• 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, 0);
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 #ifdef S_IFREG
478 		mode |= S_IFREG;
479 #else
480 		ASSERT(0);
481 #endif
482 		size = 0;
483 	} else if (de->file_type == F2FS_FT_SYMLINK) {
484 		ASSERT(de->link);
485 #ifdef S_IFLNK
486 		mode |= S_IFLNK;
487 #else
488 		ASSERT(0);
489 #endif
490 		size = strlen(de->link);
491 		if (size + 1 > MAX_INLINE_DATA(node_blk))
492 			blocks++;
493 	} else {
494 		ASSERT(0);
495 	}
496 
497 	node_blk->i.i_mode = cpu_to_le16(mode);
498 	node_blk->i.i_advise = 0;
499 	node_blk->i.i_uid = cpu_to_le32(de->uid);
500 	node_blk->i.i_gid = cpu_to_le32(de->gid);
501 	node_blk->i.i_links = cpu_to_le32(links);
502 	node_blk->i.i_size = cpu_to_le32(size);
503 	node_blk->i.i_blocks = cpu_to_le32(blocks);
504 	node_blk->i.i_atime = cpu_to_le64(de->mtime);
505 	node_blk->i.i_ctime = cpu_to_le64(de->mtime);
506 	node_blk->i.i_mtime = cpu_to_le64(de->mtime);
507 	node_blk->i.i_atime_nsec = 0;
508 	node_blk->i.i_ctime_nsec = 0;
509 	node_blk->i.i_mtime_nsec = 0;
510 	node_blk->i.i_generation = 0;
511 	if (de->file_type == F2FS_FT_DIR)
512 		node_blk->i.i_current_depth = cpu_to_le32(1);
513 	else
514 		node_blk->i.i_current_depth = cpu_to_le32(0);
515 	node_blk->i.i_xattr_nid = 0;
516 	node_blk->i.i_flags = 0;
517 	node_blk->i.i_inline = F2FS_INLINE_XATTR;
518 	node_blk->i.i_pino = cpu_to_le32(de->pino);
519 	node_blk->i.i_namelen = cpu_to_le32(de->len);
520 	memcpy(node_blk->i.i_name, de->name, de->len);
521 	node_blk->i.i_name[de->len] = 0;
522 
523 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
524 		node_blk->i.i_inline |= F2FS_EXTRA_ATTR;
525 		node_blk->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
526 	}
527 
528 	set_file_temperature(sbi, node_blk, de->name);
529 
530 	node_blk->footer.ino = cpu_to_le32(de->ino);
531 	node_blk->footer.nid = cpu_to_le32(de->ino);
532 	node_blk->footer.flag = 0;
533 	node_blk->footer.cp_ver = ckpt->checkpoint_ver;
534 	set_cold_node(node_blk, S_ISDIR(mode));
535 
536 	if (S_ISDIR(mode)) {
537 		make_empty_dir(sbi, node_blk);
538 	} else if (S_ISLNK(mode)) {
539 		page_symlink(sbi, node_blk, de->link, size);
540 
541 		free(de->link);
542 		de->link = NULL;
543 	}
544 
545 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
546 		node_blk->i.i_inode_checksum =
547 			cpu_to_le32(f2fs_inode_chksum(node_blk));
548 }
549 
convert_inline_dentry(struct f2fs_sb_info * sbi,struct f2fs_node * node,block_t p_blkaddr)550 int convert_inline_dentry(struct f2fs_sb_info *sbi, struct f2fs_node *node,
551 							block_t p_blkaddr)
552 {
553 	struct f2fs_inode *inode = &(node->i);
554 	unsigned int dir_level = node->i.i_dir_level;
555 	nid_t ino = le32_to_cpu(node->footer.ino);
556 	char inline_data[MAX_INLINE_DATA(node)];
557 	struct dnode_of_data dn;
558 	struct f2fs_dentry_ptr d;
559 	unsigned long bit_pos = 0;
560 	int ret = 0;
561 
562 	if (!(inode->i_inline & F2FS_INLINE_DENTRY))
563 		return 0;
564 
565 	memcpy(inline_data, inline_data_addr(node), MAX_INLINE_DATA(node));
566 	memset(inline_data_addr(node), 0, MAX_INLINE_DATA(node));
567 	inode->i_inline &= ~F2FS_INLINE_DENTRY;
568 
569 	ret = dev_write_block(node, p_blkaddr);
570 	ASSERT(ret >= 0);
571 
572 	memset(&dn, 0, sizeof(dn));
573 	if (!dir_level) {
574 		struct f2fs_dentry_block *dentry_blk;
575 		struct f2fs_dentry_ptr src, dst;
576 
577 		dentry_blk = calloc(BLOCK_SZ, 1);
578 		ASSERT(dentry_blk);
579 
580 		set_new_dnode(&dn, node, NULL, ino);
581 		get_dnode_of_data(sbi, &dn, 0, ALLOC_NODE);
582 		if (dn.data_blkaddr == NULL_ADDR)
583 			new_data_block(sbi, dentry_blk, &dn, CURSEG_HOT_DATA);
584 
585 		make_dentry_ptr(&src, node, (void *)inline_data, 2);
586 		make_dentry_ptr(&dst, NULL, (void *)dentry_blk, 1);
587 
588 		 /* copy data from inline dentry block to new dentry block */
589 		memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
590 		memset(dst.bitmap + src.nr_bitmap, 0,
591 					dst.nr_bitmap - src.nr_bitmap);
592 
593 		memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
594 		memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
595 
596 		ret = dev_write_block(dentry_blk, dn.data_blkaddr);
597 		ASSERT(ret >= 0);
598 
599 		MSG(1, "%s: copy inline entry to block\n", __func__);
600 
601 		free(dentry_blk);
602 		return ret;
603 	}
604 
605 	make_empty_dir(sbi, node);
606 	make_dentry_ptr(&d, node, (void *)inline_data, 2);
607 
608 	while (bit_pos < (unsigned long)d.max) {
609 		struct f2fs_dir_entry *de;
610 		const unsigned char *filename;
611 		int namelen;
612 
613 		if (!test_bit_le(bit_pos, d.bitmap)) {
614 			bit_pos++;
615 			continue;
616 		}
617 
618 		de = &d.dentry[bit_pos];
619 		if (!de->name_len) {
620 			bit_pos++;
621 			continue;
622 		}
623 
624 		filename = d.filename[bit_pos];
625 		namelen = le32_to_cpu(de->name_len);
626 
627 		if (is_dot_dotdot(filename, namelen)) {
628 			bit_pos += GET_DENTRY_SLOTS(namelen);
629 			continue;
630 		}
631 
632 		ret = f2fs_add_link(sbi, node, filename, namelen,
633 				le32_to_cpu(de->ino),
634 				de->file_type, p_blkaddr, 0);
635 		if (ret)
636 			MSG(0, "Convert file \"%s\" ERR=%d\n", filename, ret);
637 		else
638 			MSG(1, "%s: add inline entry to block\n", __func__);
639 
640 		bit_pos += GET_DENTRY_SLOTS(namelen);
641 	}
642 
643 	return 0;
644 }
645 
cmp_from_devino(const void * a,const void * b)646 static int cmp_from_devino(const void *a, const void *b) {
647 	u64 devino_a = ((struct hardlink_cache_entry*) a)->from_devino;
648 	u64 devino_b = ((struct hardlink_cache_entry*) b)->from_devino;
649 
650 	return (devino_a > devino_b) - (devino_a < devino_b);
651 }
652 
f2fs_search_hardlink(struct f2fs_sb_info * sbi,struct dentry * de)653 struct hardlink_cache_entry *f2fs_search_hardlink(struct f2fs_sb_info *sbi,
654 						struct dentry *de)
655 {
656 	struct hardlink_cache_entry *find_hardlink = NULL;
657 	struct hardlink_cache_entry *found_hardlink = NULL;
658 	void *search_result;
659 
660 	/* This might be a hardlink, try to find it in the cache */
661 	find_hardlink = calloc(1, sizeof(struct hardlink_cache_entry));
662 	find_hardlink->from_devino = de->from_devino;
663 
664 	search_result = tsearch(find_hardlink, &(sbi->hardlink_cache),
665 				cmp_from_devino);
666 	ASSERT(search_result != 0);
667 
668 	found_hardlink = *(struct hardlink_cache_entry**) search_result;
669 	ASSERT(find_hardlink->from_devino == found_hardlink->from_devino);
670 
671 	/* If it was already in the cache, free the entry we just created */
672 	if (found_hardlink != find_hardlink)
673 		free(find_hardlink);
674 
675 	return found_hardlink;
676 }
677 
f2fs_create(struct f2fs_sb_info * sbi,struct dentry * de)678 int f2fs_create(struct f2fs_sb_info *sbi, struct dentry *de)
679 {
680 	struct f2fs_node *parent, *child;
681 	struct hardlink_cache_entry *found_hardlink = NULL;
682 	struct node_info ni, hardlink_ni;
683 	struct f2fs_summary sum;
684 	block_t blkaddr = NULL_ADDR;
685 	int ret;
686 
687 	/* Find if there is a */
688 	get_node_info(sbi, de->pino, &ni);
689 	if (ni.blk_addr == NULL_ADDR) {
690 		MSG(0, "No parent directory pino=%x\n", de->pino);
691 		return -1;
692 	}
693 
694 	if (de->from_devino)
695 		found_hardlink = f2fs_search_hardlink(sbi, de);
696 
697 	parent = calloc(BLOCK_SZ, 1);
698 	ASSERT(parent);
699 
700 	ret = dev_read_block(parent, ni.blk_addr);
701 	ASSERT(ret >= 0);
702 
703 	/* Must convert inline dentry before the following opertions */
704 	ret = convert_inline_dentry(sbi, parent, ni.blk_addr);
705 	if (ret) {
706 		MSG(0, "Convert inline dentry for pino=%x failed.\n", de->pino);
707 		return -1;
708 	}
709 
710 	ret = f2fs_find_entry(sbi, parent, de);
711 	if (ret) {
712 		MSG(0, "Skip the existing \"%s\" pino=%x ERR=%d\n",
713 					de->name, de->pino, ret);
714 		if (de->file_type == F2FS_FT_REG_FILE)
715 			de->ino = 0;
716 		goto free_parent_dir;
717 	}
718 
719 	child = calloc(BLOCK_SZ, 1);
720 	ASSERT(child);
721 
722 	if (found_hardlink && found_hardlink->to_ino) {
723 		/*
724 		 * If we found this devino in the cache, we're creating a
725 		 * hard link.
726 		 */
727 		get_node_info(sbi, found_hardlink->to_ino, &hardlink_ni);
728 		if (hardlink_ni.blk_addr == NULL_ADDR) {
729 			MSG(1, "No original inode for hard link to_ino=%x\n",
730 				found_hardlink->to_ino);
731 			return -1;
732 		}
733 
734 		/* Use previously-recorded inode */
735 		de->ino = found_hardlink->to_ino;
736 		blkaddr = hardlink_ni.blk_addr;
737 		MSG(1, "Info: Creating \"%s\" as hard link to inode %d\n",
738 				de->path, de->ino);
739 	} else {
740 		f2fs_alloc_nid(sbi, &de->ino);
741 	}
742 
743 	init_inode_block(sbi, child, de);
744 
745 	ret = f2fs_add_link(sbi, parent, child->i.i_name,
746 				le32_to_cpu(child->i.i_namelen),
747 				le32_to_cpu(child->footer.ino),
748 				map_de_type(le16_to_cpu(child->i.i_mode)),
749 				ni.blk_addr, 1);
750 	if (ret) {
751 		MSG(0, "Skip the existing \"%s\" pino=%x ERR=%d\n",
752 					de->name, de->pino, ret);
753 		goto free_child_dir;
754 	}
755 
756 	if (found_hardlink) {
757 		if (!found_hardlink->to_ino) {
758 			MSG(2, "Adding inode %d from %s to hardlink cache\n",
759 				de->ino, de->path);
760 			found_hardlink->to_ino = de->ino;
761 		} else {
762 			/* Replace child with original block */
763 			free(child);
764 
765 			child = calloc(BLOCK_SZ, 1);
766 			ASSERT(child);
767 
768 			ret = dev_read_block(child, blkaddr);
769 			ASSERT(ret >= 0);
770 
771 			/* Increment links and skip to writing block */
772 			child->i.i_links = cpu_to_le32(
773 					le32_to_cpu(child->i.i_links) + 1);
774 			MSG(2, "Number of links on inode %d is now %d\n",
775 				de->ino, le32_to_cpu(child->i.i_links));
776 			goto write_child_dir;
777 		}
778 	}
779 
780 	/* write child */
781 	set_summary(&sum, de->ino, 0, ni.version);
782 	ret = reserve_new_block(sbi, &blkaddr, &sum, CURSEG_HOT_NODE, 1);
783 	ASSERT(!ret);
784 
785 	/* update nat info */
786 	update_nat_blkaddr(sbi, de->ino, de->ino, blkaddr);
787 
788 write_child_dir:
789 	ret = dev_write_block(child, blkaddr);
790 	ASSERT(ret >= 0);
791 
792 	update_free_segments(sbi);
793 	MSG(1, "Info: Create %s -> %s\n"
794 		"  -- ino=%x, type=%x, mode=%x, uid=%x, "
795 		"gid=%x, cap=%"PRIx64", size=%lu, link=%u "
796 		"blocks=%"PRIx64" pino=%x\n",
797 		de->full_path, de->path,
798 		de->ino, de->file_type, de->mode,
799 		de->uid, de->gid, de->capabilities, de->size,
800 		le32_to_cpu(child->i.i_links),
801 		le64_to_cpu(child->i.i_blocks),
802 		de->pino);
803 free_child_dir:
804 	free(child);
805 free_parent_dir:
806 	free(parent);
807 	return 0;
808 }
809 
f2fs_mkdir(struct f2fs_sb_info * sbi,struct dentry * de)810 int f2fs_mkdir(struct f2fs_sb_info *sbi, struct dentry *de)
811 {
812 	return f2fs_create(sbi, de);
813 }
814 
f2fs_symlink(struct f2fs_sb_info * sbi,struct dentry * de)815 int f2fs_symlink(struct f2fs_sb_info *sbi, struct dentry *de)
816 {
817 	return f2fs_create(sbi, de);
818 }
819 
f2fs_find_path(struct f2fs_sb_info * sbi,char * path,nid_t * ino)820 int f2fs_find_path(struct f2fs_sb_info *sbi, char *path, nid_t *ino)
821 {
822 	struct f2fs_node *parent;
823 	struct node_info ni;
824 	struct dentry de;
825 	int err = 0;
826 	int ret;
827 	char *p;
828 
829 	if (path[0] != '/')
830 		return -ENOENT;
831 
832 	*ino = F2FS_ROOT_INO(sbi);
833 	parent = calloc(BLOCK_SZ, 1);
834 	ASSERT(parent);
835 
836 	p = strtok(path, "/");
837 	while (p) {
838 		de.name = (const u8 *)p;
839 		de.len = strlen(p);
840 
841 		get_node_info(sbi, *ino, &ni);
842 		if (ni.blk_addr == NULL_ADDR) {
843 			err = -ENOENT;
844 			goto err;
845 		}
846 		ret = dev_read_block(parent, ni.blk_addr);
847 		ASSERT(ret >= 0);
848 
849 		ret = f2fs_find_entry(sbi, parent, &de);
850 		if (!ret) {
851 			err = -ENOENT;
852 			goto err;
853 		}
854 
855 		*ino = de.ino;
856 		p = strtok(NULL, "/");
857 	}
858 err:
859 	free(parent);
860 	return err;
861 }
862