Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2017-2018 HUAWEI, Inc.
0004  *             https://www.huawei.com/
0005  * Copyright (C) 2022, Alibaba Cloud
0006  */
0007 #include "xattr.h"
0008 
0009 #include <trace/events/erofs.h>
0010 
0011 struct erofs_qstr {
0012     const unsigned char *name;
0013     const unsigned char *end;
0014 };
0015 
0016 /* based on the end of qn is accurate and it must have the trailing '\0' */
0017 static inline int erofs_dirnamecmp(const struct erofs_qstr *qn,
0018                    const struct erofs_qstr *qd,
0019                    unsigned int *matched)
0020 {
0021     unsigned int i = *matched;
0022 
0023     /*
0024      * on-disk error, let's only BUG_ON in the debugging mode.
0025      * otherwise, it will return 1 to just skip the invalid name
0026      * and go on (in consideration of the lookup performance).
0027      */
0028     DBG_BUGON(qd->name > qd->end);
0029 
0030     /* qd could not have trailing '\0' */
0031     /* However it is absolutely safe if < qd->end */
0032     while (qd->name + i < qd->end && qd->name[i] != '\0') {
0033         if (qn->name[i] != qd->name[i]) {
0034             *matched = i;
0035             return qn->name[i] > qd->name[i] ? 1 : -1;
0036         }
0037         ++i;
0038     }
0039     *matched = i;
0040     /* See comments in __d_alloc on the terminating NUL character */
0041     return qn->name[i] == '\0' ? 0 : 1;
0042 }
0043 
0044 #define nameoff_from_disk(off, sz)  (le16_to_cpu(off) & ((sz) - 1))
0045 
0046 static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,
0047                            u8 *data,
0048                            unsigned int dirblksize,
0049                            const int ndirents)
0050 {
0051     int head, back;
0052     unsigned int startprfx, endprfx;
0053     struct erofs_dirent *const de = (struct erofs_dirent *)data;
0054 
0055     /* since the 1st dirent has been evaluated previously */
0056     head = 1;
0057     back = ndirents - 1;
0058     startprfx = endprfx = 0;
0059 
0060     while (head <= back) {
0061         const int mid = head + (back - head) / 2;
0062         const int nameoff = nameoff_from_disk(de[mid].nameoff,
0063                               dirblksize);
0064         unsigned int matched = min(startprfx, endprfx);
0065         struct erofs_qstr dname = {
0066             .name = data + nameoff,
0067             .end = mid >= ndirents - 1 ?
0068                 data + dirblksize :
0069                 data + nameoff_from_disk(de[mid + 1].nameoff,
0070                              dirblksize)
0071         };
0072 
0073         /* string comparison without already matched prefix */
0074         int ret = erofs_dirnamecmp(name, &dname, &matched);
0075 
0076         if (!ret) {
0077             return de + mid;
0078         } else if (ret > 0) {
0079             head = mid + 1;
0080             startprfx = matched;
0081         } else {
0082             back = mid - 1;
0083             endprfx = matched;
0084         }
0085     }
0086 
0087     return ERR_PTR(-ENOENT);
0088 }
0089 
0090 static void *find_target_block_classic(struct erofs_buf *target,
0091                        struct inode *dir,
0092                        struct erofs_qstr *name,
0093                        int *_ndirents)
0094 {
0095     unsigned int startprfx, endprfx;
0096     int head, back;
0097     void *candidate = ERR_PTR(-ENOENT);
0098 
0099     startprfx = endprfx = 0;
0100     head = 0;
0101     back = erofs_inode_datablocks(dir) - 1;
0102 
0103     while (head <= back) {
0104         const int mid = head + (back - head) / 2;
0105         struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
0106         struct erofs_dirent *de;
0107 
0108         de = erofs_bread(&buf, dir, mid, EROFS_KMAP);
0109         if (!IS_ERR(de)) {
0110             const int nameoff = nameoff_from_disk(de->nameoff,
0111                                   EROFS_BLKSIZ);
0112             const int ndirents = nameoff / sizeof(*de);
0113             int diff;
0114             unsigned int matched;
0115             struct erofs_qstr dname;
0116 
0117             if (!ndirents) {
0118                 erofs_put_metabuf(&buf);
0119                 erofs_err(dir->i_sb,
0120                       "corrupted dir block %d @ nid %llu",
0121                       mid, EROFS_I(dir)->nid);
0122                 DBG_BUGON(1);
0123                 de = ERR_PTR(-EFSCORRUPTED);
0124                 goto out;
0125             }
0126 
0127             matched = min(startprfx, endprfx);
0128 
0129             dname.name = (u8 *)de + nameoff;
0130             if (ndirents == 1)
0131                 dname.end = (u8 *)de + EROFS_BLKSIZ;
0132             else
0133                 dname.end = (u8 *)de +
0134                     nameoff_from_disk(de[1].nameoff,
0135                               EROFS_BLKSIZ);
0136 
0137             /* string comparison without already matched prefix */
0138             diff = erofs_dirnamecmp(name, &dname, &matched);
0139 
0140             if (!diff) {
0141                 *_ndirents = 0;
0142                 goto out;
0143             } else if (diff > 0) {
0144                 head = mid + 1;
0145                 startprfx = matched;
0146 
0147                 if (!IS_ERR(candidate))
0148                     erofs_put_metabuf(target);
0149                 *target = buf;
0150                 candidate = de;
0151                 *_ndirents = ndirents;
0152             } else {
0153                 erofs_put_metabuf(&buf);
0154 
0155                 back = mid - 1;
0156                 endprfx = matched;
0157             }
0158             continue;
0159         }
0160 out:        /* free if the candidate is valid */
0161         if (!IS_ERR(candidate))
0162             erofs_put_metabuf(target);
0163         return de;
0164     }
0165     return candidate;
0166 }
0167 
0168 int erofs_namei(struct inode *dir, const struct qstr *name, erofs_nid_t *nid,
0169         unsigned int *d_type)
0170 {
0171     int ndirents;
0172     struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
0173     struct erofs_dirent *de;
0174     struct erofs_qstr qn;
0175 
0176     if (!dir->i_size)
0177         return -ENOENT;
0178 
0179     qn.name = name->name;
0180     qn.end = name->name + name->len;
0181 
0182     ndirents = 0;
0183 
0184     de = find_target_block_classic(&buf, dir, &qn, &ndirents);
0185     if (IS_ERR(de))
0186         return PTR_ERR(de);
0187 
0188     /* the target page has been mapped */
0189     if (ndirents)
0190         de = find_target_dirent(&qn, (u8 *)de, EROFS_BLKSIZ, ndirents);
0191 
0192     if (!IS_ERR(de)) {
0193         *nid = le64_to_cpu(de->nid);
0194         *d_type = de->file_type;
0195     }
0196     erofs_put_metabuf(&buf);
0197     return PTR_ERR_OR_ZERO(de);
0198 }
0199 
0200 /* NOTE: i_mutex is already held by vfs */
0201 static struct dentry *erofs_lookup(struct inode *dir,
0202                    struct dentry *dentry,
0203                    unsigned int flags)
0204 {
0205     int err;
0206     erofs_nid_t nid;
0207     unsigned int d_type;
0208     struct inode *inode;
0209 
0210     DBG_BUGON(!d_really_is_negative(dentry));
0211     /* dentry must be unhashed in lookup, no need to worry about */
0212     DBG_BUGON(!d_unhashed(dentry));
0213 
0214     trace_erofs_lookup(dir, dentry, flags);
0215 
0216     /* file name exceeds fs limit */
0217     if (dentry->d_name.len > EROFS_NAME_LEN)
0218         return ERR_PTR(-ENAMETOOLONG);
0219 
0220     /* false uninitialized warnings on gcc 4.8.x */
0221     err = erofs_namei(dir, &dentry->d_name, &nid, &d_type);
0222 
0223     if (err == -ENOENT) {
0224         /* negative dentry */
0225         inode = NULL;
0226     } else if (err) {
0227         inode = ERR_PTR(err);
0228     } else {
0229         erofs_dbg("%s, %pd (nid %llu) found, d_type %u", __func__,
0230               dentry, nid, d_type);
0231         inode = erofs_iget(dir->i_sb, nid, d_type == FT_DIR);
0232     }
0233     return d_splice_alias(inode, dentry);
0234 }
0235 
0236 const struct inode_operations erofs_dir_iops = {
0237     .lookup = erofs_lookup,
0238     .getattr = erofs_getattr,
0239     .listxattr = erofs_listxattr,
0240     .get_acl = erofs_get_acl,
0241     .fiemap = erofs_fiemap,
0242 };