blob: 2776bb832127d79d55c91da1b0a1a6fe4705a27e [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiang3aa8ec72018-07-26 20:21:49 +08002/*
Gao Xiang3aa8ec72018-07-26 20:21:49 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang3aa8ec72018-07-26 20:21:49 +08005 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiang3aa8ec72018-07-26 20:21:49 +08006 */
7#include "internal.h"
8
Gao Xiang33bac912019-03-29 04:14:58 +08009static 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
Gao Xiang4f761fa2019-09-04 10:09:09 +080019 erofs_dbg("found dirent %s de_len %u d_type %d", dbg_namebuf,
20 de_namelen, d_type);
Gao Xiang33bac912019-03-29 04:14:58 +080021#endif
22}
23
Gao Xianga6b9b1d2019-08-14 18:37:03 +080024static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
Bhanusree Polacbebe5d2019-02-19 14:55:25 +053025 void *dentry_blk, unsigned int *ofs,
26 unsigned int nameoff, unsigned int maxsize)
Gao Xiang3aa8ec72018-07-26 20:21:49 +080027{
Colin Ian King5c8004c2019-06-17 13:55:29 +010028 struct erofs_dirent *de = dentry_blk + *ofs;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080029 const struct erofs_dirent *end = dentry_blk + nameoff;
30
Gao Xiang3aa8ec72018-07-26 20:21:49 +080031 while (de < end) {
32 const char *de_name;
Gao Xiang33bac912019-03-29 04:14:58 +080033 unsigned int de_namelen;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080034 unsigned char d_type;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080035
Gao Xiang1d819c52019-08-16 15:11:42 +080036 d_type = fs_ftype_to_dtype(de->file_type);
Gao Xiang3aa8ec72018-07-26 20:21:49 +080037
38 nameoff = le16_to_cpu(de->nameoff);
39 de_name = (char *)dentry_blk + nameoff;
40
Gao Xiang33bac912019-03-29 04:14:58 +080041 /* 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;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080046
Gao Xiang8b987bc2018-12-05 21:23:13 +080047 /* a corrupted entry is found */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080048 if (nameoff + de_namelen > maxsize ||
49 de_namelen > EROFS_NAME_LEN) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080050 erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
51 EROFS_I(dir)->nid);
Gao Xiang8b987bc2018-12-05 21:23:13 +080052 DBG_BUGON(1);
Gao Xianga6b9b1d2019-08-14 18:37:03 +080053 return -EFSCORRUPTED;
Gao Xiang8b987bc2018-12-05 21:23:13 +080054 }
Gao Xiang3aa8ec72018-07-26 20:21:49 +080055
Gao Xiang33bac912019-03-29 04:14:58 +080056 debug_one_dentry(d_type, de_name, de_namelen);
Gao Xiang3aa8ec72018-07-26 20:21:49 +080057 if (!dir_emit(ctx, de_name, de_namelen,
Aaron Strahlberger019ec6c2018-12-11 11:54:41 +010058 le64_to_cpu(de->nid), d_type))
59 /* stopped by some reason */
Gao Xiang3aa8ec72018-07-26 20:21:49 +080060 return 1;
61 ++de;
62 *ofs += sizeof(struct erofs_dirent);
63 }
64 *ofs = maxsize;
65 return 0;
66}
67
68static int erofs_readdir(struct file *f, struct dir_context *ctx)
69{
70 struct inode *dir = file_inode(f);
71 struct address_space *mapping = dir->i_mapping;
72 const size_t dirsize = i_size_read(dir);
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020073 unsigned int i = ctx->pos / EROFS_BLKSIZ;
74 unsigned int ofs = ctx->pos % EROFS_BLKSIZ;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080075 int err = 0;
76 bool initial = true;
77
78 while (ctx->pos < dirsize) {
79 struct page *dentry_page;
80 struct erofs_dirent *de;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020081 unsigned int nameoff, maxsize;
Gao Xiang3aa8ec72018-07-26 20:21:49 +080082
83 dentry_page = read_mapping_page(mapping, i, NULL);
Gao Xiangacb383f2019-08-18 20:54:57 +080084 if (dentry_page == ERR_PTR(-ENOMEM)) {
85 err = -ENOMEM;
86 break;
87 } else if (IS_ERR(dentry_page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +080088 erofs_err(dir->i_sb,
89 "fail to readdir of logical block %u of nid %llu",
90 i, EROFS_I(dir)->nid);
Gao Xiangacb383f2019-08-18 20:54:57 +080091 err = -EFSCORRUPTED;
92 break;
93 }
Gao Xiang3aa8ec72018-07-26 20:21:49 +080094
Gao Xiang3aa8ec72018-07-26 20:21:49 +080095 de = (struct erofs_dirent *)kmap(dentry_page);
96
97 nameoff = le16_to_cpu(de->nameoff);
98
Gao Xiang8d8a09b2019-08-30 00:38:27 +080099 if (nameoff < sizeof(struct erofs_dirent) ||
100 nameoff >= PAGE_SIZE) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800101 erofs_err(dir->i_sb,
102 "invalid de[0].nameoff %u @ nid %llu",
103 nameoff, EROFS_I(dir)->nid);
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800104 err = -EFSCORRUPTED;
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800105 goto skip_this;
106 }
107
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200108 maxsize = min_t(unsigned int,
109 dirsize - ctx->pos + ofs, PAGE_SIZE);
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800110
111 /* search dirents at the arbitrary position */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800112 if (initial) {
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800113 initial = false;
114
115 ofs = roundup(ofs, sizeof(struct erofs_dirent));
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800116 if (ofs >= nameoff)
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800117 goto skip_this;
118 }
119
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800120 err = erofs_fill_dentries(dir, ctx, de, &ofs,
121 nameoff, maxsize);
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800122skip_this:
123 kunmap(dentry_page);
124
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800125 put_page(dentry_page);
126
127 ctx->pos = blknr_to_addr(i) + ofs;
128
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800129 if (err)
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800130 break;
131 ++i;
132 ofs = 0;
133 }
134 return err < 0 ? err : 0;
135}
136
137const struct file_operations erofs_dir_fops = {
138 .llseek = generic_file_llseek,
139 .read = generic_read_dir,
Gao Xiangbee15682019-02-21 10:34:11 +0800140 .iterate_shared = erofs_readdir,
Gao Xiang3aa8ec72018-07-26 20:21:49 +0800141};
142