2019-07-31 15:57:31 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2018-07-26 12:21:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017-2018 HUAWEI, Inc.
|
2020-07-13 13:09:44 +00:00
|
|
|
* https://www.huawei.com/
|
2022-03-16 01:22:46 +00:00
|
|
|
* Copyright (C) 2022, Alibaba Cloud
|
2018-07-26 12:21:50 +00:00
|
|
|
*/
|
2018-07-26 12:21:52 +00:00
|
|
|
#include "xattr.h"
|
2018-07-26 12:21:55 +00:00
|
|
|
#include <trace/events/erofs.h>
|
|
|
|
|
2019-02-01 12:16:31 +00:00
|
|
|
struct erofs_qstr {
|
|
|
|
const unsigned char *name;
|
|
|
|
const unsigned char *end;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* based on the end of qn is accurate and it must have the trailing '\0' */
|
2019-09-04 02:09:05 +00:00
|
|
|
static inline int erofs_dirnamecmp(const struct erofs_qstr *qn,
|
|
|
|
const struct erofs_qstr *qd,
|
|
|
|
unsigned int *matched)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
2019-02-01 12:16:31 +00:00
|
|
|
unsigned int i = *matched;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* on-disk error, let's only BUG_ON in the debugging mode.
|
|
|
|
* otherwise, it will return 1 to just skip the invalid name
|
|
|
|
* and go on (in consideration of the lookup performance).
|
|
|
|
*/
|
|
|
|
DBG_BUGON(qd->name > qd->end);
|
|
|
|
|
|
|
|
/* qd could not have trailing '\0' */
|
|
|
|
/* However it is absolutely safe if < qd->end */
|
|
|
|
while (qd->name + i < qd->end && qd->name[i] != '\0') {
|
|
|
|
if (qn->name[i] != qd->name[i]) {
|
|
|
|
*matched = i;
|
|
|
|
return qn->name[i] > qd->name[i] ? 1 : -1;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
2019-02-01 12:16:31 +00:00
|
|
|
++i;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
2019-02-01 12:16:31 +00:00
|
|
|
*matched = i;
|
|
|
|
/* See comments in __d_alloc on the terminating NUL character */
|
|
|
|
return qn->name[i] == '\0' ? 0 : 1;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 12:16:31 +00:00
|
|
|
#define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1))
|
|
|
|
|
|
|
|
static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
|
|
|
|
u8 *data,
|
|
|
|
unsigned int dirblksize,
|
|
|
|
const int ndirents)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
2019-02-01 12:16:31 +00:00
|
|
|
int head, back;
|
2018-09-10 19:41:14 +00:00
|
|
|
unsigned int startprfx, endprfx;
|
2018-07-26 12:21:50 +00:00
|
|
|
struct erofs_dirent *const de = (struct erofs_dirent *)data;
|
|
|
|
|
2019-02-01 12:16:31 +00:00
|
|
|
/* since the 1st dirent has been evaluated previously */
|
|
|
|
head = 1;
|
2018-07-26 12:21:50 +00:00
|
|
|
back = ndirents - 1;
|
|
|
|
startprfx = endprfx = 0;
|
|
|
|
|
|
|
|
while (head <= back) {
|
2019-02-01 12:16:31 +00:00
|
|
|
const int mid = head + (back - head) / 2;
|
|
|
|
const int nameoff = nameoff_from_disk(de[mid].nameoff,
|
|
|
|
dirblksize);
|
2018-09-10 19:41:14 +00:00
|
|
|
unsigned int matched = min(startprfx, endprfx);
|
2019-02-01 12:16:31 +00:00
|
|
|
struct erofs_qstr dname = {
|
|
|
|
.name = data + nameoff,
|
2019-08-29 16:38:27 +00:00
|
|
|
.end = mid >= ndirents - 1 ?
|
2019-02-01 12:16:31 +00:00
|
|
|
data + dirblksize :
|
|
|
|
data + nameoff_from_disk(de[mid + 1].nameoff,
|
|
|
|
dirblksize)
|
|
|
|
};
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
/* string comparison without already matched prefix */
|
2019-09-04 02:09:05 +00:00
|
|
|
int ret = erofs_dirnamecmp(name, &dname, &matched);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2019-08-29 16:38:27 +00:00
|
|
|
if (!ret) {
|
2018-07-26 12:21:50 +00:00
|
|
|
return de + mid;
|
2019-02-01 12:16:31 +00:00
|
|
|
} else if (ret > 0) {
|
2018-07-26 12:21:50 +00:00
|
|
|
head = mid + 1;
|
|
|
|
startprfx = matched;
|
2019-02-01 12:16:31 +00:00
|
|
|
} else {
|
2018-07-26 12:21:50 +00:00
|
|
|
back = mid - 1;
|
|
|
|
endprfx = matched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
}
|
|
|
|
|
2023-02-04 09:30:35 +00:00
|
|
|
static void *erofs_find_target_block(struct erofs_buf *target,
|
|
|
|
struct inode *dir, struct erofs_qstr *name, int *_ndirents)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
2023-03-13 13:53:08 +00:00
|
|
|
unsigned int bsz = i_blocksize(dir);
|
|
|
|
int head = 0, back = erofs_iblks(dir) - 1;
|
2023-02-04 09:30:35 +00:00
|
|
|
unsigned int startprfx = 0, endprfx = 0;
|
2022-03-16 01:22:46 +00:00
|
|
|
void *candidate = ERR_PTR(-ENOENT);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
while (head <= back) {
|
2019-02-01 12:16:31 +00:00
|
|
|
const int mid = head + (back - head) / 2;
|
2022-03-16 01:22:46 +00:00
|
|
|
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
|
|
|
|
struct erofs_dirent *de;
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2023-04-07 14:17:04 +00:00
|
|
|
buf.inode = dir;
|
2024-04-07 07:04:50 +00:00
|
|
|
de = erofs_bread(&buf, erofs_pos(dir->i_sb, mid), EROFS_KMAP);
|
2022-03-16 01:22:46 +00:00
|
|
|
if (!IS_ERR(de)) {
|
2023-03-13 13:53:08 +00:00
|
|
|
const int nameoff = nameoff_from_disk(de->nameoff, bsz);
|
2019-02-01 12:16:31 +00:00
|
|
|
const int ndirents = nameoff / sizeof(*de);
|
|
|
|
int diff;
|
|
|
|
unsigned int matched;
|
|
|
|
struct erofs_qstr dname;
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2019-08-29 16:38:27 +00:00
|
|
|
if (!ndirents) {
|
2022-03-16 01:22:46 +00:00
|
|
|
erofs_put_metabuf(&buf);
|
2019-09-04 02:09:09 +00:00
|
|
|
erofs_err(dir->i_sb,
|
|
|
|
"corrupted dir block %d @ nid %llu",
|
|
|
|
mid, EROFS_I(dir)->nid);
|
2019-08-14 10:37:03 +00:00
|
|
|
DBG_BUGON(1);
|
2022-03-16 01:22:46 +00:00
|
|
|
de = ERR_PTR(-EFSCORRUPTED);
|
2019-02-01 12:16:31 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
matched = min(startprfx, endprfx);
|
|
|
|
|
|
|
|
dname.name = (u8 *)de + nameoff;
|
2019-02-01 12:16:31 +00:00
|
|
|
if (ndirents == 1)
|
2023-03-13 13:53:08 +00:00
|
|
|
dname.end = (u8 *)de + bsz;
|
2019-02-01 12:16:31 +00:00
|
|
|
else
|
|
|
|
dname.end = (u8 *)de +
|
2023-03-13 13:53:08 +00:00
|
|
|
nameoff_from_disk(de[1].nameoff, bsz);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
/* string comparison without already matched prefix */
|
2019-09-04 02:09:05 +00:00
|
|
|
diff = erofs_dirnamecmp(name, &dname, &matched);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2024-02-21 21:03:47 +00:00
|
|
|
if (diff < 0) {
|
2022-03-16 01:22:46 +00:00
|
|
|
erofs_put_metabuf(&buf);
|
2018-07-26 12:21:50 +00:00
|
|
|
back = mid - 1;
|
|
|
|
endprfx = matched;
|
2024-02-21 21:03:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ERR(candidate))
|
|
|
|
erofs_put_metabuf(target);
|
|
|
|
*target = buf;
|
|
|
|
if (!diff) {
|
|
|
|
*_ndirents = 0;
|
|
|
|
return de;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
2024-02-21 21:03:47 +00:00
|
|
|
head = mid + 1;
|
|
|
|
startprfx = matched;
|
|
|
|
candidate = de;
|
|
|
|
*_ndirents = ndirents;
|
2019-02-01 12:16:31 +00:00
|
|
|
continue;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
2019-02-01 12:16:31 +00:00
|
|
|
out: /* free if the candidate is valid */
|
|
|
|
if (!IS_ERR(candidate))
|
2022-03-16 01:22:46 +00:00
|
|
|
erofs_put_metabuf(target);
|
|
|
|
return de;
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
|
2022-04-25 04:07:12 +00:00
|
|
|
int erofs_namei(struct inode *dir, const struct qstr *name, erofs_nid_t *nid,
|
|
|
|
unsigned int *d_type)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
2019-02-01 12:16:31 +00:00
|
|
|
int ndirents;
|
2022-03-16 01:22:46 +00:00
|
|
|
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
|
2018-07-26 12:21:50 +00:00
|
|
|
struct erofs_dirent *de;
|
2019-02-01 12:16:31 +00:00
|
|
|
struct erofs_qstr qn;
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2019-08-29 16:38:27 +00:00
|
|
|
if (!dir->i_size)
|
2018-07-26 12:21:50 +00:00
|
|
|
return -ENOENT;
|
|
|
|
|
2019-02-01 12:16:31 +00:00
|
|
|
qn.name = name->name;
|
|
|
|
qn.end = name->name + name->len;
|
2023-04-07 14:17:04 +00:00
|
|
|
buf.inode = dir;
|
2019-02-01 12:16:31 +00:00
|
|
|
|
|
|
|
ndirents = 0;
|
2023-02-04 09:30:35 +00:00
|
|
|
de = erofs_find_target_block(&buf, dir, &qn, &ndirents);
|
2022-03-16 01:22:46 +00:00
|
|
|
if (IS_ERR(de))
|
|
|
|
return PTR_ERR(de);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2019-02-01 12:16:31 +00:00
|
|
|
if (ndirents)
|
2023-03-13 13:53:08 +00:00
|
|
|
de = find_target_dirent(&qn, (u8 *)de, i_blocksize(dir),
|
|
|
|
ndirents);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2019-02-12 03:24:22 +00:00
|
|
|
if (!IS_ERR(de)) {
|
2018-07-26 12:21:50 +00:00
|
|
|
*nid = le64_to_cpu(de->nid);
|
|
|
|
*d_type = de->file_type;
|
|
|
|
}
|
2022-03-16 01:22:46 +00:00
|
|
|
erofs_put_metabuf(&buf);
|
2018-07-30 01:51:01 +00:00
|
|
|
return PTR_ERR_OR_ZERO(de);
|
2018-07-26 12:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 06:36:06 +00:00
|
|
|
static struct dentry *erofs_lookup(struct inode *dir, struct dentry *dentry,
|
2019-03-18 23:58:41 +00:00
|
|
|
unsigned int flags)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
erofs_nid_t nid;
|
2018-09-10 19:41:14 +00:00
|
|
|
unsigned int d_type;
|
2018-07-26 12:21:50 +00:00
|
|
|
struct inode *inode;
|
|
|
|
|
2018-07-26 12:21:55 +00:00
|
|
|
trace_erofs_lookup(dir, dentry, flags);
|
|
|
|
|
2019-08-29 16:38:27 +00:00
|
|
|
if (dentry->d_name.len > EROFS_NAME_LEN)
|
2018-07-26 12:21:50 +00:00
|
|
|
return ERR_PTR(-ENAMETOOLONG);
|
|
|
|
|
|
|
|
err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
|
|
|
|
|
2023-04-14 08:30:26 +00:00
|
|
|
if (err == -ENOENT)
|
2018-07-26 12:21:50 +00:00
|
|
|
/* negative dentry */
|
|
|
|
inode = NULL;
|
2023-04-14 08:30:26 +00:00
|
|
|
else if (err)
|
2018-10-10 20:37:39 +00:00
|
|
|
inode = ERR_PTR(err);
|
2023-04-14 08:30:26 +00:00
|
|
|
else
|
2022-09-27 06:36:07 +00:00
|
|
|
inode = erofs_iget(dir->i_sb, nid);
|
2018-07-26 12:21:50 +00:00
|
|
|
return d_splice_alias(inode, dentry);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct inode_operations erofs_dir_iops = {
|
|
|
|
.lookup = erofs_lookup,
|
2019-05-28 03:19:42 +00:00
|
|
|
.getattr = erofs_getattr,
|
2018-07-26 12:21:52 +00:00
|
|
|
.listxattr = erofs_listxattr,
|
2022-09-22 15:17:00 +00:00
|
|
|
.get_inode_acl = erofs_get_acl,
|
2021-08-13 05:29:31 +00:00
|
|
|
.fiemap = erofs_fiemap,
|
2018-07-26 12:21:50 +00:00
|
|
|
};
|