Gao Xiang | 29b24f6 | 2019-07-31 23:57:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 2 | /* |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 3 | * Copyright (C) 2017-2018 HUAWEI, Inc. |
Alexander A. Klimov | 592e7cd | 2020-07-13 15:09:44 +0200 | [diff] [blame] | 4 | * https://www.huawei.com/ |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 5 | * Created by Gao Xiang <gaoxiang25@huawei.com> |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 6 | */ |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 7 | #include "xattr.h" |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 8 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 9 | #include <trace/events/erofs.h> |
| 10 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 11 | struct erofs_qstr { |
| 12 | const unsigned char *name; |
| 13 | const unsigned char *end; |
| 14 | }; |
| 15 | |
| 16 | /* based on the end of qn is accurate and it must have the trailing '\0' */ |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 17 | static inline int erofs_dirnamecmp(const struct erofs_qstr *qn, |
| 18 | const struct erofs_qstr *qd, |
| 19 | unsigned int *matched) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 20 | { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 21 | unsigned int i = *matched; |
| 22 | |
| 23 | /* |
| 24 | * on-disk error, let's only BUG_ON in the debugging mode. |
| 25 | * otherwise, it will return 1 to just skip the invalid name |
| 26 | * and go on (in consideration of the lookup performance). |
| 27 | */ |
| 28 | DBG_BUGON(qd->name > qd->end); |
| 29 | |
| 30 | /* qd could not have trailing '\0' */ |
| 31 | /* However it is absolutely safe if < qd->end */ |
| 32 | while (qd->name + i < qd->end && qd->name[i] != '\0') { |
| 33 | if (qn->name[i] != qd->name[i]) { |
| 34 | *matched = i; |
| 35 | return qn->name[i] > qd->name[i] ? 1 : -1; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 36 | } |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 37 | ++i; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 38 | } |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 39 | *matched = i; |
| 40 | /* See comments in __d_alloc on the terminating NUL character */ |
| 41 | return qn->name[i] == '\0' ? 0 : 1; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 44 | #define nameoff_from_disk(off, sz) (le16_to_cpu(off) & ((sz) - 1)) |
| 45 | |
| 46 | static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name, |
| 47 | u8 *data, |
| 48 | unsigned int dirblksize, |
| 49 | const int ndirents) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 50 | { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 51 | int head, back; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 52 | unsigned int startprfx, endprfx; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 53 | struct erofs_dirent *const de = (struct erofs_dirent *)data; |
| 54 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 55 | /* since the 1st dirent has been evaluated previously */ |
| 56 | head = 1; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 57 | back = ndirents - 1; |
| 58 | startprfx = endprfx = 0; |
| 59 | |
| 60 | while (head <= back) { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 61 | const int mid = head + (back - head) / 2; |
| 62 | const int nameoff = nameoff_from_disk(de[mid].nameoff, |
| 63 | dirblksize); |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 64 | unsigned int matched = min(startprfx, endprfx); |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 65 | struct erofs_qstr dname = { |
| 66 | .name = data + nameoff, |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 67 | .end = mid >= ndirents - 1 ? |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 68 | data + dirblksize : |
| 69 | data + nameoff_from_disk(de[mid + 1].nameoff, |
| 70 | dirblksize) |
| 71 | }; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 72 | |
| 73 | /* string comparison without already matched prefix */ |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 74 | int ret = erofs_dirnamecmp(name, &dname, &matched); |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 75 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 76 | if (!ret) { |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 77 | return de + mid; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 78 | } else if (ret > 0) { |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 79 | head = mid + 1; |
| 80 | startprfx = matched; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 81 | } else { |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 82 | back = mid - 1; |
| 83 | endprfx = matched; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return ERR_PTR(-ENOENT); |
| 88 | } |
| 89 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 90 | static struct page *find_target_block_classic(struct inode *dir, |
| 91 | struct erofs_qstr *name, |
| 92 | int *_ndirents) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 93 | { |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 94 | unsigned int startprfx, endprfx; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 95 | int head, back; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 96 | struct address_space *const mapping = dir->i_mapping; |
| 97 | struct page *candidate = ERR_PTR(-ENOENT); |
| 98 | |
| 99 | startprfx = endprfx = 0; |
| 100 | head = 0; |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 101 | back = erofs_inode_datablocks(dir) - 1; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 102 | |
| 103 | while (head <= back) { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 104 | const int mid = head + (back - head) / 2; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 105 | struct page *page = read_mapping_page(mapping, mid, NULL); |
| 106 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 107 | if (!IS_ERR(page)) { |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 108 | struct erofs_dirent *de = kmap_atomic(page); |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 109 | const int nameoff = nameoff_from_disk(de->nameoff, |
| 110 | EROFS_BLKSIZ); |
| 111 | const int ndirents = nameoff / sizeof(*de); |
| 112 | int diff; |
| 113 | unsigned int matched; |
| 114 | struct erofs_qstr dname; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 115 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 116 | if (!ndirents) { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 117 | kunmap_atomic(de); |
| 118 | put_page(page); |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 119 | erofs_err(dir->i_sb, |
| 120 | "corrupted dir block %d @ nid %llu", |
| 121 | mid, EROFS_I(dir)->nid); |
Gao Xiang | a6b9b1d | 2019-08-14 18:37:03 +0800 | [diff] [blame] | 122 | DBG_BUGON(1); |
| 123 | page = ERR_PTR(-EFSCORRUPTED); |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 124 | goto out; |
| 125 | } |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 126 | |
| 127 | matched = min(startprfx, endprfx); |
| 128 | |
| 129 | dname.name = (u8 *)de + nameoff; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 130 | if (ndirents == 1) |
| 131 | dname.end = (u8 *)de + EROFS_BLKSIZ; |
| 132 | else |
| 133 | dname.end = (u8 *)de + |
| 134 | nameoff_from_disk(de[1].nameoff, |
| 135 | EROFS_BLKSIZ); |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 136 | |
| 137 | /* string comparison without already matched prefix */ |
Gao Xiang | 99634bf | 2019-09-04 10:09:05 +0800 | [diff] [blame] | 138 | diff = erofs_dirnamecmp(name, &dname, &matched); |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 139 | kunmap_atomic(de); |
| 140 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 141 | if (!diff) { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 142 | *_ndirents = 0; |
| 143 | goto out; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 144 | } else if (diff > 0) { |
| 145 | head = mid + 1; |
| 146 | startprfx = matched; |
| 147 | |
Chengguang Xu | 7fadcdc | 2019-02-12 11:24:22 +0800 | [diff] [blame] | 148 | if (!IS_ERR(candidate)) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 149 | put_page(candidate); |
| 150 | candidate = page; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 151 | *_ndirents = ndirents; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 152 | } else { |
| 153 | put_page(page); |
| 154 | |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 155 | back = mid - 1; |
| 156 | endprfx = matched; |
| 157 | } |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 158 | continue; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 159 | } |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 160 | out: /* free if the candidate is valid */ |
| 161 | if (!IS_ERR(candidate)) |
| 162 | put_page(candidate); |
| 163 | return page; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 164 | } |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 165 | return candidate; |
| 166 | } |
| 167 | |
| 168 | int erofs_namei(struct inode *dir, |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 169 | struct qstr *name, |
| 170 | erofs_nid_t *nid, unsigned int *d_type) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 171 | { |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 172 | int ndirents; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 173 | struct page *page; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 174 | void *data; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 175 | struct erofs_dirent *de; |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 176 | struct erofs_qstr qn; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 177 | |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 178 | if (!dir->i_size) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 179 | return -ENOENT; |
| 180 | |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 181 | qn.name = name->name; |
| 182 | qn.end = name->name + name->len; |
| 183 | |
| 184 | ndirents = 0; |
| 185 | page = find_target_block_classic(dir, &qn, &ndirents); |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 186 | |
Chengguang Xu | 7fadcdc | 2019-02-12 11:24:22 +0800 | [diff] [blame] | 187 | if (IS_ERR(page)) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 188 | return PTR_ERR(page); |
| 189 | |
| 190 | data = kmap_atomic(page); |
| 191 | /* the target page has been mapped */ |
Gao Xiang | 419d6ef | 2019-02-01 20:16:31 +0800 | [diff] [blame] | 192 | if (ndirents) |
| 193 | de = find_target_dirent(&qn, data, EROFS_BLKSIZ, ndirents); |
| 194 | else |
| 195 | de = (struct erofs_dirent *)data; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 196 | |
Chengguang Xu | 7fadcdc | 2019-02-12 11:24:22 +0800 | [diff] [blame] | 197 | if (!IS_ERR(de)) { |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 198 | *nid = le64_to_cpu(de->nid); |
| 199 | *d_type = de->file_type; |
| 200 | } |
| 201 | |
| 202 | kunmap_atomic(data); |
| 203 | put_page(page); |
| 204 | |
Gao Xiang | 38c6aa2 | 2018-07-30 09:51:01 +0800 | [diff] [blame] | 205 | return PTR_ERR_OR_ZERO(de); |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* NOTE: i_mutex is already held by vfs */ |
| 209 | static struct dentry *erofs_lookup(struct inode *dir, |
Julian Merida | 447a362 | 2019-03-18 20:58:41 -0300 | [diff] [blame] | 210 | struct dentry *dentry, |
| 211 | unsigned int flags) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 212 | { |
| 213 | int err; |
| 214 | erofs_nid_t nid; |
Thomas Weißschuh | 7dd68b1 | 2018-09-10 21:41:14 +0200 | [diff] [blame] | 215 | unsigned int d_type; |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 216 | struct inode *inode; |
| 217 | |
| 218 | DBG_BUGON(!d_really_is_negative(dentry)); |
| 219 | /* dentry must be unhashed in lookup, no need to worry about */ |
| 220 | DBG_BUGON(!d_unhashed(dentry)); |
| 221 | |
Chao Yu | 13f06f4 | 2018-07-26 20:21:55 +0800 | [diff] [blame] | 222 | trace_erofs_lookup(dir, dentry, flags); |
| 223 | |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 224 | /* file name exceeds fs limit */ |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 225 | if (dentry->d_name.len > EROFS_NAME_LEN) |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 226 | return ERR_PTR(-ENAMETOOLONG); |
| 227 | |
| 228 | /* false uninitialized warnings on gcc 4.8.x */ |
| 229 | err = erofs_namei(dir, &dentry->d_name, &nid, &d_type); |
| 230 | |
| 231 | if (err == -ENOENT) { |
| 232 | /* negative dentry */ |
| 233 | inode = NULL; |
Gao Xiang | 8d8a09b | 2019-08-30 00:38:27 +0800 | [diff] [blame] | 234 | } else if (err) { |
Al Viro | 8300807 | 2018-10-10 16:37:39 -0400 | [diff] [blame] | 235 | inode = ERR_PTR(err); |
| 236 | } else { |
Gao Xiang | 4f761fa | 2019-09-04 10:09:09 +0800 | [diff] [blame] | 237 | erofs_dbg("%s, %s (nid %llu) found, d_type %u", __func__, |
| 238 | dentry->d_name.name, nid, d_type); |
Gao Xiang | 1d819c5 | 2019-08-16 15:11:42 +0800 | [diff] [blame] | 239 | inode = erofs_iget(dir->i_sb, nid, d_type == FT_DIR); |
Al Viro | 8300807 | 2018-10-10 16:37:39 -0400 | [diff] [blame] | 240 | } |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 241 | return d_splice_alias(inode, dentry); |
| 242 | } |
| 243 | |
| 244 | const struct inode_operations erofs_dir_iops = { |
| 245 | .lookup = erofs_lookup, |
Gao Xiang | 89f27ed | 2019-05-28 11:19:42 +0800 | [diff] [blame] | 246 | .getattr = erofs_getattr, |
Gao Xiang | b17500a | 2018-07-26 20:21:52 +0800 | [diff] [blame] | 247 | .listxattr = erofs_listxattr, |
Gao Xiang | 516c115c | 2019-01-29 16:35:20 +0800 | [diff] [blame] | 248 | .get_acl = erofs_get_acl, |
Gao Xiang | d72d1ce | 2018-07-26 20:21:50 +0800 | [diff] [blame] | 249 | }; |
| 250 | |