xref: /linux/fs/erofs/dir.c (revision 0a94608f0f7de9b1135ffea3546afe68eafef57f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2022, Alibaba Cloud
6  */
7 #include "internal.h"
8 
9 static void debug_one_dentry(unsigned char d_type, const char *de_name,
10 			     unsigned int de_namelen)
11 {
12 #ifdef CONFIG_EROFS_FS_DEBUG
13 	/* since the on-disk name could not have the trailing '\0' */
14 	unsigned char dbg_namebuf[EROFS_NAME_LEN + 1];
15 
16 	memcpy(dbg_namebuf, de_name, de_namelen);
17 	dbg_namebuf[de_namelen] = '\0';
18 
19 	erofs_dbg("found dirent %s de_len %u d_type %d", dbg_namebuf,
20 		  de_namelen, d_type);
21 #endif
22 }
23 
24 static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
25 			       void *dentry_blk, unsigned int *ofs,
26 			       unsigned int nameoff, unsigned int maxsize)
27 {
28 	struct erofs_dirent *de = dentry_blk + *ofs;
29 	const struct erofs_dirent *end = dentry_blk + nameoff;
30 
31 	while (de < end) {
32 		const char *de_name;
33 		unsigned int de_namelen;
34 		unsigned char d_type;
35 
36 		d_type = fs_ftype_to_dtype(de->file_type);
37 
38 		nameoff = le16_to_cpu(de->nameoff);
39 		de_name = (char *)dentry_blk + nameoff;
40 
41 		/* the last dirent in the block? */
42 		if (de + 1 >= end)
43 			de_namelen = strnlen(de_name, maxsize - nameoff);
44 		else
45 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
46 
47 		/* a corrupted entry is found */
48 		if (nameoff + de_namelen > maxsize ||
49 		    de_namelen > EROFS_NAME_LEN) {
50 			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
51 				  EROFS_I(dir)->nid);
52 			DBG_BUGON(1);
53 			return -EFSCORRUPTED;
54 		}
55 
56 		debug_one_dentry(d_type, de_name, de_namelen);
57 		if (!dir_emit(ctx, de_name, de_namelen,
58 			      le64_to_cpu(de->nid), d_type))
59 			/* stopped by some reason */
60 			return 1;
61 		++de;
62 		*ofs += sizeof(struct erofs_dirent);
63 	}
64 	*ofs = maxsize;
65 	return 0;
66 }
67 
68 static int erofs_readdir(struct file *f, struct dir_context *ctx)
69 {
70 	struct inode *dir = file_inode(f);
71 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
72 	const size_t dirsize = i_size_read(dir);
73 	unsigned int i = ctx->pos / EROFS_BLKSIZ;
74 	unsigned int ofs = ctx->pos % EROFS_BLKSIZ;
75 	int err = 0;
76 	bool initial = true;
77 
78 	while (ctx->pos < dirsize) {
79 		struct erofs_dirent *de;
80 		unsigned int nameoff, maxsize;
81 
82 		de = erofs_bread(&buf, dir, i, EROFS_KMAP);
83 		if (IS_ERR(de)) {
84 			erofs_err(dir->i_sb,
85 				  "fail to readdir of logical block %u of nid %llu",
86 				  i, EROFS_I(dir)->nid);
87 			err = PTR_ERR(de);
88 			break;
89 		}
90 
91 		nameoff = le16_to_cpu(de->nameoff);
92 		if (nameoff < sizeof(struct erofs_dirent) ||
93 		    nameoff >= PAGE_SIZE) {
94 			erofs_err(dir->i_sb,
95 				  "invalid de[0].nameoff %u @ nid %llu",
96 				  nameoff, EROFS_I(dir)->nid);
97 			err = -EFSCORRUPTED;
98 			goto skip_this;
99 		}
100 
101 		maxsize = min_t(unsigned int,
102 				dirsize - ctx->pos + ofs, PAGE_SIZE);
103 
104 		/* search dirents at the arbitrary position */
105 		if (initial) {
106 			initial = false;
107 
108 			ofs = roundup(ofs, sizeof(struct erofs_dirent));
109 			if (ofs >= nameoff)
110 				goto skip_this;
111 		}
112 
113 		err = erofs_fill_dentries(dir, ctx, de, &ofs,
114 					  nameoff, maxsize);
115 skip_this:
116 		ctx->pos = blknr_to_addr(i) + ofs;
117 
118 		if (err)
119 			break;
120 		++i;
121 		ofs = 0;
122 	}
123 	erofs_put_metabuf(&buf);
124 	return err < 0 ? err : 0;
125 }
126 
127 const struct file_operations erofs_dir_fops = {
128 	.llseek		= generic_file_llseek,
129 	.read		= generic_read_dir,
130 	.iterate_shared	= erofs_readdir,
131 };
132