]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/erofs/dir.c
Merge branch 'for-linus-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/ibft
[linux.git] / drivers / staging / erofs / dir.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/dir.c
4  *
5  * Copyright (C) 2017-2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #include "internal.h"
14
15 static const unsigned char erofs_filetype_table[EROFS_FT_MAX] = {
16         [EROFS_FT_UNKNOWN]      = DT_UNKNOWN,
17         [EROFS_FT_REG_FILE]     = DT_REG,
18         [EROFS_FT_DIR]          = DT_DIR,
19         [EROFS_FT_CHRDEV]       = DT_CHR,
20         [EROFS_FT_BLKDEV]       = DT_BLK,
21         [EROFS_FT_FIFO]         = DT_FIFO,
22         [EROFS_FT_SOCK]         = DT_SOCK,
23         [EROFS_FT_SYMLINK]      = DT_LNK,
24 };
25
26 static void debug_one_dentry(unsigned char d_type, const char *de_name,
27                              unsigned int de_namelen)
28 {
29 #ifdef CONFIG_EROFS_FS_DEBUG
30         /* since the on-disk name could not have the trailing '\0' */
31         unsigned char dbg_namebuf[EROFS_NAME_LEN + 1];
32
33         memcpy(dbg_namebuf, de_name, de_namelen);
34         dbg_namebuf[de_namelen] = '\0';
35
36         debugln("found dirent %s de_len %u d_type %d", dbg_namebuf,
37                 de_namelen, d_type);
38 #endif
39 }
40
41 static int erofs_fill_dentries(struct dir_context *ctx,
42                                void *dentry_blk, unsigned int *ofs,
43                                unsigned int nameoff, unsigned int maxsize)
44 {
45         struct erofs_dirent *de = dentry_blk + *ofs;
46         const struct erofs_dirent *end = dentry_blk + nameoff;
47
48         while (de < end) {
49                 const char *de_name;
50                 unsigned int de_namelen;
51                 unsigned char d_type;
52
53                 if (de->file_type < EROFS_FT_MAX)
54                         d_type = erofs_filetype_table[de->file_type];
55                 else
56                         d_type = DT_UNKNOWN;
57
58                 nameoff = le16_to_cpu(de->nameoff);
59                 de_name = (char *)dentry_blk + nameoff;
60
61                 /* the last dirent in the block? */
62                 if (de + 1 >= end)
63                         de_namelen = strnlen(de_name, maxsize - nameoff);
64                 else
65                         de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
66
67                 /* a corrupted entry is found */
68                 if (unlikely(nameoff + de_namelen > maxsize ||
69                              de_namelen > EROFS_NAME_LEN)) {
70                         DBG_BUGON(1);
71                         return -EIO;
72                 }
73
74                 debug_one_dentry(d_type, de_name, de_namelen);
75                 if (!dir_emit(ctx, de_name, de_namelen,
76                               le64_to_cpu(de->nid), d_type))
77                         /* stopped by some reason */
78                         return 1;
79                 ++de;
80                 *ofs += sizeof(struct erofs_dirent);
81         }
82         *ofs = maxsize;
83         return 0;
84 }
85
86 static int erofs_readdir(struct file *f, struct dir_context *ctx)
87 {
88         struct inode *dir = file_inode(f);
89         struct address_space *mapping = dir->i_mapping;
90         const size_t dirsize = i_size_read(dir);
91         unsigned int i = ctx->pos / EROFS_BLKSIZ;
92         unsigned int ofs = ctx->pos % EROFS_BLKSIZ;
93         int err = 0;
94         bool initial = true;
95
96         while (ctx->pos < dirsize) {
97                 struct page *dentry_page;
98                 struct erofs_dirent *de;
99                 unsigned int nameoff, maxsize;
100
101                 dentry_page = read_mapping_page(mapping, i, NULL);
102                 if (IS_ERR(dentry_page))
103                         continue;
104
105                 de = (struct erofs_dirent *)kmap(dentry_page);
106
107                 nameoff = le16_to_cpu(de->nameoff);
108
109                 if (unlikely(nameoff < sizeof(struct erofs_dirent) ||
110                              nameoff >= PAGE_SIZE)) {
111                         errln("%s, invalid de[0].nameoff %u",
112                               __func__, nameoff);
113
114                         err = -EIO;
115                         goto skip_this;
116                 }
117
118                 maxsize = min_t(unsigned int,
119                                 dirsize - ctx->pos + ofs, PAGE_SIZE);
120
121                 /* search dirents at the arbitrary position */
122                 if (unlikely(initial)) {
123                         initial = false;
124
125                         ofs = roundup(ofs, sizeof(struct erofs_dirent));
126                         if (unlikely(ofs >= nameoff))
127                                 goto skip_this;
128                 }
129
130                 err = erofs_fill_dentries(ctx, de, &ofs, nameoff, maxsize);
131 skip_this:
132                 kunmap(dentry_page);
133
134                 put_page(dentry_page);
135
136                 ctx->pos = blknr_to_addr(i) + ofs;
137
138                 if (unlikely(err))
139                         break;
140                 ++i;
141                 ofs = 0;
142         }
143         return err < 0 ? err : 0;
144 }
145
146 const struct file_operations erofs_dir_fops = {
147         .llseek         = generic_file_llseek,
148         .read           = generic_read_dir,
149         .iterate_shared = erofs_readdir,
150 };
151