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:50 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-03-16 01:22:46 +00:00
|
|
|
static void *find_target_block_classic(struct erofs_buf *target,
|
|
|
|
struct inode *dir,
|
|
|
|
struct erofs_qstr *name,
|
|
|
|
int *_ndirents)
|
2018-07-26 12:21:50 +00:00
|
|
|
{
|
2018-09-10 19:41:14 +00:00
|
|
|
unsigned int startprfx, endprfx;
|
2019-02-01 12:16:31 +00:00
|
|
|
int head, back;
|
2022-03-16 01:22:46 +00:00
|
|
|
void *candidate = ERR_PTR(-ENOENT);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
startprfx = endprfx = 0;
|
|
|
|
head = 0;
|
2019-09-04 02:09:05 +00:00
|
|
|
back = erofs_inode_datablocks(dir) - 1;
|
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
|
|
|
|
2022-03-16 01:22:46 +00:00
|
|
|
de = erofs_bread(&buf, dir, mid, EROFS_KMAP);
|
|
|
|
if (!IS_ERR(de)) {
|
2019-02-01 12:16:31 +00:00
|
|
|
const int nameoff = nameoff_from_disk(de->nameoff,
|
|
|
|
EROFS_BLKSIZ);
|
|
|
|
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)
|
|
|
|
dname.end = (u8 *)de + EROFS_BLKSIZ;
|
|
|
|
else
|
|
|
|
dname.end = (u8 *)de +
|
|
|
|
nameoff_from_disk(de[1].nameoff,
|
|
|
|
EROFS_BLKSIZ);
|
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
|
|
|
|
2019-08-29 16:38:27 +00:00
|
|
|
if (!diff) {
|
2019-02-01 12:16:31 +00:00
|
|
|
*_ndirents = 0;
|
|
|
|
goto out;
|
2018-07-26 12:21:50 +00:00
|
|
|
} else if (diff > 0) {
|
|
|
|
head = mid + 1;
|
|
|
|
startprfx = matched;
|
|
|
|
|
2019-02-12 03:24:22 +00:00
|
|
|
if (!IS_ERR(candidate))
|
2022-03-16 01:22:46 +00:00
|
|
|
erofs_put_metabuf(target);
|
|
|
|
*target = buf;
|
|
|
|
candidate = de;
|
2019-02-01 12:16:31 +00:00
|
|
|
*_ndirents = ndirents;
|
2018-07-26 12:21:50 +00:00
|
|
|
} else {
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int erofs_namei(struct inode *dir,
|
2019-02-01 12:16:31 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
ndirents = 0;
|
2018-07-26 12:21:50 +00:00
|
|
|
|
2022-03-16 01:22:46 +00:00
|
|
|
de = find_target_block_classic(&buf, dir, &qn, &ndirents);
|
|
|
|
if (IS_ERR(de))
|
|
|
|
return PTR_ERR(de);
|
2018-07-26 12:21:50 +00:00
|
|
|
|
|
|
|
/* the target page has been mapped */
|
2019-02-01 12:16:31 +00:00
|
|
|
if (ndirents)
|
2022-03-16 01:22:46 +00:00
|
|
|
de = find_target_dirent(&qn, (u8 *)de, EROFS_BLKSIZ, 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
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: i_mutex is already held by vfs */
|
|
|
|
static struct dentry *erofs_lookup(struct inode *dir,
|
2019-03-18 23:58:41 +00:00
|
|
|
struct dentry *dentry,
|
|
|
|
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;
|
|
|
|
|
|
|
|
DBG_BUGON(!d_really_is_negative(dentry));
|
|
|
|
/* dentry must be unhashed in lookup, no need to worry about */
|
|
|
|
DBG_BUGON(!d_unhashed(dentry));
|
|
|
|
|
2018-07-26 12:21:55 +00:00
|
|
|
trace_erofs_lookup(dir, dentry, flags);
|
|
|
|
|
2018-07-26 12:21:50 +00:00
|
|
|
/* file name exceeds fs limit */
|
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);
|
|
|
|
|
|
|
|
/* false uninitialized warnings on gcc 4.8.x */
|
|
|
|
err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
|
|
|
|
|
|
|
|
if (err == -ENOENT) {
|
|
|
|
/* negative dentry */
|
|
|
|
inode = NULL;
|
2019-08-29 16:38:27 +00:00
|
|
|
} else if (err) {
|
2018-10-10 20:37:39 +00:00
|
|
|
inode = ERR_PTR(err);
|
|
|
|
} else {
|
2021-01-06 04:03:16 +00:00
|
|
|
erofs_dbg("%s, %pd (nid %llu) found, d_type %u", __func__,
|
|
|
|
dentry, nid, d_type);
|
2019-08-16 07:11:42 +00:00
|
|
|
inode = erofs_iget(dir->i_sb, nid, d_type == FT_DIR);
|
2018-10-10 20:37:39 +00:00
|
|
|
}
|
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,
|
2019-01-29 08:35:20 +00:00
|
|
|
.get_acl = erofs_get_acl,
|
2021-08-13 05:29:31 +00:00
|
|
|
.fiemap = erofs_fiemap,
|
2018-07-26 12:21:50 +00:00
|
|
|
};
|