1 // SPDX-License-Identifier: GPL-2.0-only
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>
11 #include <trace/events/erofs.h>
14 const unsigned char *name;
15 const unsigned char *end;
18 /* based on the end of qn is accurate and it must have the trailing '\0' */
19 static inline int dirnamecmp(const struct erofs_qstr *qn,
20 const struct erofs_qstr *qd,
21 unsigned int *matched)
23 unsigned int i = *matched;
26 * on-disk error, let's only BUG_ON in the debugging mode.
27 * otherwise, it will return 1 to just skip the invalid name
28 * and go on (in consideration of the lookup performance).
30 DBG_BUGON(qd->name > qd->end);
32 /* qd could not have trailing '\0' */
33 /* However it is absolutely safe if < qd->end */
34 while (qd->name + i < qd->end && qd->name[i] != '\0') {
35 if (qn->name[i] != qd->name[i]) {
37 return qn->name[i] > qd->name[i] ? 1 : -1;
42 /* See comments in __d_alloc on the terminating NUL character */
43 return qn->name[i] == '\0' ? 0 : 1;
46 #define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1))
48 static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
50 unsigned int dirblksize,
54 unsigned int startprfx, endprfx;
55 struct erofs_dirent *const de = (struct erofs_dirent *)data;
57 /* since the 1st dirent has been evaluated previously */
60 startprfx = endprfx = 0;
62 while (head <= back) {
63 const int mid = head + (back - head) / 2;
64 const int nameoff = nameoff_from_disk(de[mid].nameoff,
66 unsigned int matched = min(startprfx, endprfx);
67 struct erofs_qstr dname = {
68 .name = data + nameoff,
69 .end = unlikely(mid >= ndirents - 1) ?
71 data + nameoff_from_disk(de[mid + 1].nameoff,
75 /* string comparison without already matched prefix */
76 int ret = dirnamecmp(name, &dname, &matched);
89 return ERR_PTR(-ENOENT);
92 static struct page *find_target_block_classic(struct inode *dir,
93 struct erofs_qstr *name,
96 unsigned int startprfx, endprfx;
98 struct address_space *const mapping = dir->i_mapping;
99 struct page *candidate = ERR_PTR(-ENOENT);
101 startprfx = endprfx = 0;
103 back = inode_datablocks(dir) - 1;
105 while (head <= back) {
106 const int mid = head + (back - head) / 2;
107 struct page *page = read_mapping_page(mapping, mid, NULL);
110 struct erofs_dirent *de = kmap_atomic(page);
111 const int nameoff = nameoff_from_disk(de->nameoff,
113 const int ndirents = nameoff / sizeof(*de);
115 unsigned int matched;
116 struct erofs_qstr dname;
118 if (unlikely(!ndirents)) {
121 errln("corrupted dir block %d @ nid %llu",
122 mid, EROFS_V(dir)->nid);
124 page = ERR_PTR(-EFSCORRUPTED);
128 matched = min(startprfx, endprfx);
130 dname.name = (u8 *)de + nameoff;
132 dname.end = (u8 *)de + EROFS_BLKSIZ;
134 dname.end = (u8 *)de +
135 nameoff_from_disk(de[1].nameoff,
138 /* string comparison without already matched prefix */
139 diff = dirnamecmp(name, &dname, &matched);
142 if (unlikely(!diff)) {
145 } else if (diff > 0) {
149 if (!IS_ERR(candidate))
152 *_ndirents = ndirents;
161 out: /* free if the candidate is valid */
162 if (!IS_ERR(candidate))
169 int erofs_namei(struct inode *dir,
171 erofs_nid_t *nid, unsigned int *d_type)
176 struct erofs_dirent *de;
177 struct erofs_qstr qn;
179 if (unlikely(!dir->i_size))
182 qn.name = name->name;
183 qn.end = name->name + name->len;
186 page = find_target_block_classic(dir, &qn, &ndirents);
189 return PTR_ERR(page);
191 data = kmap_atomic(page);
192 /* the target page has been mapped */
194 de = find_target_dirent(&qn, data, EROFS_BLKSIZ, ndirents);
196 de = (struct erofs_dirent *)data;
199 *nid = le64_to_cpu(de->nid);
200 *d_type = de->file_type;
206 return PTR_ERR_OR_ZERO(de);
209 /* NOTE: i_mutex is already held by vfs */
210 static struct dentry *erofs_lookup(struct inode *dir,
211 struct dentry *dentry,
219 DBG_BUGON(!d_really_is_negative(dentry));
220 /* dentry must be unhashed in lookup, no need to worry about */
221 DBG_BUGON(!d_unhashed(dentry));
223 trace_erofs_lookup(dir, dentry, flags);
225 /* file name exceeds fs limit */
226 if (unlikely(dentry->d_name.len > EROFS_NAME_LEN))
227 return ERR_PTR(-ENAMETOOLONG);
229 /* false uninitialized warnings on gcc 4.8.x */
230 err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
232 if (err == -ENOENT) {
233 /* negative dentry */
235 } else if (unlikely(err)) {
236 inode = ERR_PTR(err);
238 debugln("%s, %s (nid %llu) found, d_type %u", __func__,
239 dentry->d_name.name, nid, d_type);
240 inode = erofs_iget(dir->i_sb, nid, d_type == FT_DIR);
242 return d_splice_alias(inode, dentry);
245 const struct inode_operations erofs_dir_iops = {
246 .lookup = erofs_lookup,
247 .getattr = erofs_getattr,
248 #ifdef CONFIG_EROFS_FS_XATTR
249 .listxattr = erofs_listxattr,
251 .get_acl = erofs_get_acl,