1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/staging/erofs/namei.c
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
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.
16 #include <trace/events/erofs.h>
19 const unsigned char *name;
20 const unsigned char *end;
23 /* based on the end of qn is accurate and it must have the trailing '\0' */
24 static inline int dirnamecmp(const struct erofs_qstr *qn,
25 const struct erofs_qstr *qd,
26 unsigned int *matched)
28 unsigned int i = *matched;
31 * on-disk error, let's only BUG_ON in the debugging mode.
32 * otherwise, it will return 1 to just skip the invalid name
33 * and go on (in consideration of the lookup performance).
35 DBG_BUGON(qd->name > qd->end);
37 /* qd could not have trailing '\0' */
38 /* However it is absolutely safe if < qd->end */
39 while (qd->name + i < qd->end && qd->name[i] != '\0') {
40 if (qn->name[i] != qd->name[i]) {
42 return qn->name[i] > qd->name[i] ? 1 : -1;
47 /* See comments in __d_alloc on the terminating NUL character */
48 return qn->name[i] == '\0' ? 0 : 1;
51 #define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1))
53 static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
55 unsigned int dirblksize,
59 unsigned int startprfx, endprfx;
60 struct erofs_dirent *const de = (struct erofs_dirent *)data;
62 /* since the 1st dirent has been evaluated previously */
65 startprfx = endprfx = 0;
67 while (head <= back) {
68 const int mid = head + (back - head) / 2;
69 const int nameoff = nameoff_from_disk(de[mid].nameoff,
71 unsigned int matched = min(startprfx, endprfx);
72 struct erofs_qstr dname = {
73 .name = data + nameoff,
74 .end = unlikely(mid >= ndirents - 1) ?
76 data + nameoff_from_disk(de[mid + 1].nameoff,
80 /* string comparison without already matched prefix */
81 int ret = dirnamecmp(name, &dname, &matched);
94 return ERR_PTR(-ENOENT);
97 static struct page *find_target_block_classic(struct inode *dir,
98 struct erofs_qstr *name,
101 unsigned int startprfx, endprfx;
103 struct address_space *const mapping = dir->i_mapping;
104 struct page *candidate = ERR_PTR(-ENOENT);
106 startprfx = endprfx = 0;
108 back = inode_datablocks(dir) - 1;
110 while (head <= back) {
111 const int mid = head + (back - head) / 2;
112 struct page *page = read_mapping_page(mapping, mid, NULL);
115 struct erofs_dirent *de = kmap_atomic(page);
116 const int nameoff = nameoff_from_disk(de->nameoff,
118 const int ndirents = nameoff / sizeof(*de);
120 unsigned int matched;
121 struct erofs_qstr dname;
123 if (unlikely(!ndirents)) {
127 page = ERR_PTR(-EIO);
131 matched = min(startprfx, endprfx);
133 dname.name = (u8 *)de + nameoff;
135 dname.end = (u8 *)de + EROFS_BLKSIZ;
137 dname.end = (u8 *)de +
138 nameoff_from_disk(de[1].nameoff,
141 /* string comparison without already matched prefix */
142 diff = dirnamecmp(name, &dname, &matched);
145 if (unlikely(!diff)) {
148 } else if (diff > 0) {
152 if (!IS_ERR(candidate))
155 *_ndirents = ndirents;
164 out: /* free if the candidate is valid */
165 if (!IS_ERR(candidate))
172 int erofs_namei(struct inode *dir,
174 erofs_nid_t *nid, unsigned int *d_type)
179 struct erofs_dirent *de;
180 struct erofs_qstr qn;
182 if (unlikely(!dir->i_size))
185 qn.name = name->name;
186 qn.end = name->name + name->len;
189 page = find_target_block_classic(dir, &qn, &ndirents);
192 return PTR_ERR(page);
194 data = kmap_atomic(page);
195 /* the target page has been mapped */
197 de = find_target_dirent(&qn, data, EROFS_BLKSIZ, ndirents);
199 de = (struct erofs_dirent *)data;
202 *nid = le64_to_cpu(de->nid);
203 *d_type = de->file_type;
209 return PTR_ERR_OR_ZERO(de);
212 /* NOTE: i_mutex is already held by vfs */
213 static struct dentry *erofs_lookup(struct inode *dir,
214 struct dentry *dentry,
222 DBG_BUGON(!d_really_is_negative(dentry));
223 /* dentry must be unhashed in lookup, no need to worry about */
224 DBG_BUGON(!d_unhashed(dentry));
226 trace_erofs_lookup(dir, dentry, flags);
228 /* file name exceeds fs limit */
229 if (unlikely(dentry->d_name.len > EROFS_NAME_LEN))
230 return ERR_PTR(-ENAMETOOLONG);
232 /* false uninitialized warnings on gcc 4.8.x */
233 err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
235 if (err == -ENOENT) {
236 /* negative dentry */
238 } else if (unlikely(err)) {
239 inode = ERR_PTR(err);
241 debugln("%s, %s (nid %llu) found, d_type %u", __func__,
242 dentry->d_name.name, nid, d_type);
243 inode = erofs_iget(dir->i_sb, nid, d_type == EROFS_FT_DIR);
245 return d_splice_alias(inode, dentry);
248 const struct inode_operations erofs_dir_iops = {
249 .lookup = erofs_lookup,
250 .getattr = erofs_getattr,
251 #ifdef CONFIG_EROFS_FS_XATTR
252 .listxattr = erofs_listxattr,
254 .get_acl = erofs_get_acl,