• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
debug_one_dentry(unsigned char d_type,const char * de_name,unsigned int de_namelen)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 
erofs_fill_dentries(struct inode * dir,struct dir_context * ctx,void * dentry_blk,struct erofs_dirent * de,unsigned int nameoff,unsigned int maxsize)24 static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
25 			       void *dentry_blk, struct erofs_dirent *de,
26 			       unsigned int nameoff, unsigned int maxsize)
27 {
28 	const struct erofs_dirent *end = dentry_blk + nameoff;
29 
30 	while (de < end) {
31 		const char *de_name;
32 		unsigned int de_namelen;
33 		unsigned char d_type;
34 
35 		d_type = fs_ftype_to_dtype(de->file_type);
36 
37 		nameoff = le16_to_cpu(de->nameoff);
38 		de_name = (char *)dentry_blk + nameoff;
39 
40 		/* the last dirent in the block? */
41 		if (de + 1 >= end)
42 			de_namelen = strnlen(de_name, maxsize - nameoff);
43 		else
44 			de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
45 
46 		/* a corrupted entry is found */
47 		if (nameoff + de_namelen > maxsize ||
48 		    de_namelen > EROFS_NAME_LEN) {
49 			erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
50 				  EROFS_I(dir)->nid);
51 			DBG_BUGON(1);
52 			return -EFSCORRUPTED;
53 		}
54 
55 		debug_one_dentry(d_type, de_name, de_namelen);
56 		if (!dir_emit(ctx, de_name, de_namelen,
57 			      le64_to_cpu(de->nid), d_type))
58 			/* stopped by some reason */
59 			return 1;
60 		++de;
61 		ctx->pos += sizeof(struct erofs_dirent);
62 	}
63 	return 0;
64 }
65 
erofs_readdir(struct file * f,struct dir_context * ctx)66 static int erofs_readdir(struct file *f, struct dir_context *ctx)
67 {
68 	struct inode *dir = file_inode(f);
69 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
70 	struct super_block *sb = dir->i_sb;
71 	unsigned long bsz = sb->s_blocksize;
72 	const size_t dirsize = i_size_read(dir);
73 	unsigned int i = erofs_blknr(sb, ctx->pos);
74 	unsigned int ofs = erofs_blkoff(sb, ctx->pos);
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(sb, "fail to readdir of logical block %u of nid %llu",
85 				  i, EROFS_I(dir)->nid);
86 			err = PTR_ERR(de);
87 			break;
88 		}
89 
90 		nameoff = le16_to_cpu(de->nameoff);
91 		if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
92 			erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
93 				  nameoff, EROFS_I(dir)->nid);
94 			err = -EFSCORRUPTED;
95 			break;
96 		}
97 
98 		maxsize = min_t(unsigned int, dirsize - ctx->pos + ofs, bsz);
99 
100 		/* search dirents at the arbitrary position */
101 		if (initial) {
102 			initial = false;
103 
104 			ofs = roundup(ofs, sizeof(struct erofs_dirent));
105 			ctx->pos = erofs_pos(sb, i) + ofs;
106 			if (ofs >= nameoff)
107 				goto skip_this;
108 		}
109 
110 		err = erofs_fill_dentries(dir, ctx, de, (void *)de + ofs,
111 					  nameoff, maxsize);
112 		if (err)
113 			break;
114 skip_this:
115 		ctx->pos = erofs_pos(sb, i) + maxsize;
116 		++i;
117 		ofs = 0;
118 	}
119 	erofs_put_metabuf(&buf);
120 	return err < 0 ? err : 0;
121 }
122 
123 const struct file_operations erofs_dir_fops = {
124 	.llseek		= generic_file_llseek,
125 	.read		= generic_read_dir,
126 	.iterate_shared	= erofs_readdir,
127 };
128