| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/fs.h> |
| 9 |
#include <linux/nls.h> |
| 10 |
#include <linux/ctype.h> |
| 11 |
#include <linux/posix_acl.h> |
| 12 |
|
| 13 |
#include "debug.h" |
| 14 |
#include "ntfs.h" |
| 15 |
#include "ntfs_fs.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* fill_name_de - Format NTFS_DE in @buf. |
| 19 |
*/ |
| 20 |
int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name, |
| 21 |
const struct cpu_str *uni) |
| 22 |
{ |
| 23 |
int err; |
| 24 |
struct NTFS_DE *e = buf; |
| 25 |
u16 data_size; |
| 26 |
struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1); |
| 27 |
|
| 28 |
#ifndef CONFIG_NTFS3_64BIT_CLUSTER |
| 29 |
e->ref.high = fname->home.high = 0; |
| 30 |
#endif |
| 31 |
if (uni) { |
| 32 |
#ifdef __BIG_ENDIAN |
| 33 |
int ulen = uni->len; |
| 34 |
__le16 *uname = fname->name; |
| 35 |
const u16 *name_cpu = uni->name; |
| 36 |
|
| 37 |
while (ulen--) |
| 38 |
*uname++ = cpu_to_le16(*name_cpu++); |
| 39 |
#else |
| 40 |
memcpy(fname->name, uni->name, uni->len * sizeof(u16)); |
| 41 |
#endif |
| 42 |
fname->name_len = uni->len; |
| 43 |
|
| 44 |
} else { |
| 45 |
/* Convert input string to unicode. */ |
| 46 |
err = ntfs_nls_to_utf16(sbi, name->name, name->len, |
| 47 |
(struct cpu_str *)&fname->name_len, |
| 48 |
NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN); |
| 49 |
if (err < 0) |
| 50 |
return err; |
| 51 |
} |
| 52 |
|
| 53 |
fname->type = FILE_NAME_POSIX; |
| 54 |
data_size = fname_full_size(fname); |
| 55 |
|
| 56 |
e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE)); |
| 57 |
e->key_size = cpu_to_le16(data_size); |
| 58 |
e->flags = 0; |
| 59 |
e->res = 0; |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* ntfs_lookup - inode_operations::lookup |
| 66 |
*/ |
| 67 |
static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry, |
| 68 |
u32 flags) |
| 69 |
{ |
| 70 |
struct ntfs_inode *ni = ntfs_i(dir); |
| 71 |
struct cpu_str *uni = kmalloc(PATH_MAX, GFP_KERNEL); |
| 72 |
struct inode *inode; |
| 73 |
int err; |
| 74 |
|
| 75 |
if (!uni) |
| 76 |
inode = ERR_PTR(-ENOMEM); |
| 77 |
else { |
| 78 |
err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name, |
| 79 |
dentry->d_name.len, uni, NTFS_NAME_LEN, |
| 80 |
UTF16_HOST_ENDIAN); |
| 81 |
if (err < 0) |
| 82 |
inode = ERR_PTR(err); |
| 83 |
else { |
| 84 |
ni_lock_dir(ni); |
| 85 |
inode = dir_search_u(dir, uni, NULL); |
| 86 |
ni_unlock(ni); |
| 87 |
} |
| 88 |
kfree(uni); |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Check for a null pointer |
| 93 |
* If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL. |
| 94 |
* This causes null pointer dereference in d_splice_alias(). |
| 95 |
*/ |
| 96 |
if (!IS_ERR_OR_NULL(inode) && !inode->i_op) { |
| 97 |
iput(inode); |
| 98 |
inode = ERR_PTR(-EINVAL); |
| 99 |
} |
| 100 |
|
| 101 |
return d_splice_alias(inode, dentry); |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* ntfs_create - inode_operations::create |
| 106 |
*/ |
| 107 |
static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir, |
| 108 |
struct dentry *dentry, umode_t mode, bool excl) |
| 109 |
{ |
| 110 |
return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0, |
| 111 |
NULL, 0, NULL); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* ntfs_mknod - inode_operations::mknod |
| 116 |
*/ |
| 117 |
static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir, |
| 118 |
struct dentry *dentry, umode_t mode, dev_t rdev) |
| 119 |
{ |
| 120 |
return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0, |
| 121 |
NULL); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* ntfs_link - inode_operations::link |
| 126 |
*/ |
| 127 |
static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de) |
| 128 |
{ |
| 129 |
int err; |
| 130 |
struct inode *inode = d_inode(ode); |
| 131 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 132 |
|
| 133 |
if (S_ISDIR(inode->i_mode)) |
| 134 |
return -EPERM; |
| 135 |
|
| 136 |
if (inode->i_nlink >= NTFS_LINK_MAX) |
| 137 |
return -EMLINK; |
| 138 |
|
| 139 |
ni_lock_dir(ntfs_i(dir)); |
| 140 |
if (inode != dir) |
| 141 |
ni_lock(ni); |
| 142 |
|
| 143 |
inc_nlink(inode); |
| 144 |
ihold(inode); |
| 145 |
|
| 146 |
err = ntfs_link_inode(inode, de); |
| 147 |
|
| 148 |
if (!err) { |
| 149 |
inode_set_ctime_current(inode); |
| 150 |
inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); |
| 151 |
mark_inode_dirty(inode); |
| 152 |
mark_inode_dirty(dir); |
| 153 |
d_instantiate(de, inode); |
| 154 |
} else { |
| 155 |
drop_nlink(inode); |
| 156 |
iput(inode); |
| 157 |
} |
| 158 |
|
| 159 |
if (inode != dir) |
| 160 |
ni_unlock(ni); |
| 161 |
ni_unlock(ntfs_i(dir)); |
| 162 |
|
| 163 |
return err; |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* ntfs_unlink - inode_operations::unlink |
| 168 |
*/ |
| 169 |
static int ntfs_unlink(struct inode *dir, struct dentry *dentry) |
| 170 |
{ |
| 171 |
struct ntfs_inode *ni = ntfs_i(dir); |
| 172 |
int err; |
| 173 |
|
| 174 |
/* Avoid any operation if inode is bad. */ |
| 175 |
if (unlikely(is_bad_ni(ni))) |
| 176 |
return -EINVAL; |
| 177 |
|
| 178 |
if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) |
| 179 |
return -EIO; |
| 180 |
|
| 181 |
ni_lock_dir(ni); |
| 182 |
|
| 183 |
err = ntfs_unlink_inode(dir, dentry); |
| 184 |
|
| 185 |
ni_unlock(ni); |
| 186 |
|
| 187 |
return err; |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
* ntfs_symlink - inode_operations::symlink |
| 192 |
*/ |
| 193 |
static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir, |
| 194 |
struct dentry *dentry, const char *symname) |
| 195 |
{ |
| 196 |
u32 size = strlen(symname); |
| 197 |
|
| 198 |
/* Avoid any operation if inode is bad. */ |
| 199 |
if (unlikely(is_bad_ni(ntfs_i(dir)))) |
| 200 |
return -EINVAL; |
| 201 |
|
| 202 |
if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) |
| 203 |
return -EIO; |
| 204 |
|
| 205 |
return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0, |
| 206 |
symname, size, NULL); |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
* ntfs_mkdir - inode_operations::mkdir |
| 211 |
*/ |
| 212 |
static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
| 213 |
struct dentry *dentry, umode_t mode) |
| 214 |
{ |
| 215 |
return ERR_PTR(ntfs_create_inode(idmap, dir, dentry, NULL, |
| 216 |
S_IFDIR | mode, 0, NULL, 0, NULL)); |
| 217 |
} |
| 218 |
|
| 219 |
/* |
| 220 |
* ntfs_rmdir - inode_operations::rmdir |
| 221 |
*/ |
| 222 |
static int ntfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 223 |
{ |
| 224 |
struct ntfs_inode *ni = ntfs_i(dir); |
| 225 |
int err; |
| 226 |
|
| 227 |
/* Avoid any operation if inode is bad. */ |
| 228 |
if (unlikely(is_bad_ni(ni))) |
| 229 |
return -EINVAL; |
| 230 |
|
| 231 |
if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) |
| 232 |
return -EIO; |
| 233 |
|
| 234 |
ni_lock_dir(ni); |
| 235 |
|
| 236 |
err = ntfs_unlink_inode(dir, dentry); |
| 237 |
|
| 238 |
ni_unlock(ni); |
| 239 |
|
| 240 |
return err; |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* ntfs_rename - inode_operations::rename |
| 245 |
*/ |
| 246 |
static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir, |
| 247 |
struct dentry *dentry, struct inode *new_dir, |
| 248 |
struct dentry *new_dentry, u32 flags) |
| 249 |
{ |
| 250 |
int err; |
| 251 |
struct super_block *sb = dir->i_sb; |
| 252 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 253 |
struct ntfs_inode *dir_ni = ntfs_i(dir); |
| 254 |
struct ntfs_inode *new_dir_ni = ntfs_i(new_dir); |
| 255 |
struct inode *inode = d_inode(dentry); |
| 256 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 257 |
struct inode *new_inode = d_inode(new_dentry); |
| 258 |
struct NTFS_DE *de, *new_de; |
| 259 |
bool is_same; |
| 260 |
/* |
| 261 |
* de - memory of PATH_MAX bytes: |
| 262 |
* [0-1024) - original name (dentry->d_name) |
| 263 |
* [1024-2048) - paired to original name, usually DOS variant of dentry->d_name |
| 264 |
* [2048-3072) - new name (new_dentry->d_name) |
| 265 |
*/ |
| 266 |
static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024); |
| 267 |
static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) < |
| 268 |
1024); |
| 269 |
static_assert(PATH_MAX >= 4 * 1024); |
| 270 |
|
| 271 |
/* Avoid any operation if inode is bad. */ |
| 272 |
if (unlikely(is_bad_ni(ni))) |
| 273 |
return -EINVAL; |
| 274 |
|
| 275 |
if (unlikely(ntfs3_forced_shutdown(sb))) |
| 276 |
return -EIO; |
| 277 |
|
| 278 |
if (flags & ~RENAME_NOREPLACE) |
| 279 |
return -EINVAL; |
| 280 |
|
| 281 |
is_same = dentry->d_name.len == new_dentry->d_name.len && |
| 282 |
!memcmp(dentry->d_name.name, new_dentry->d_name.name, |
| 283 |
dentry->d_name.len); |
| 284 |
|
| 285 |
if (is_same && dir == new_dir) { |
| 286 |
/* Nothing to do. */ |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
if (ntfs_is_meta_file(sbi, inode->i_ino)) { |
| 291 |
/* Should we print an error? */ |
| 292 |
return -EINVAL; |
| 293 |
} |
| 294 |
|
| 295 |
if (new_inode) { |
| 296 |
/* Target name exists. Unlink it. */ |
| 297 |
dget(new_dentry); |
| 298 |
ni_lock_dir(new_dir_ni); |
| 299 |
err = ntfs_unlink_inode(new_dir, new_dentry); |
| 300 |
ni_unlock(new_dir_ni); |
| 301 |
dput(new_dentry); |
| 302 |
if (err) |
| 303 |
return err; |
| 304 |
} |
| 305 |
|
| 306 |
de = kmalloc(PATH_MAX, GFP_KERNEL); |
| 307 |
if (!de) |
| 308 |
return -ENOMEM; |
| 309 |
|
| 310 |
/* Translate dentry->d_name into unicode form. */ |
| 311 |
err = fill_name_de(sbi, de, &dentry->d_name, NULL); |
| 312 |
if (err < 0) |
| 313 |
goto out; |
| 314 |
|
| 315 |
if (is_same) { |
| 316 |
/* Reuse 'de'. */ |
| 317 |
new_de = de; |
| 318 |
} else { |
| 319 |
/* Translate new_dentry->d_name into unicode form. */ |
| 320 |
new_de = Add2Ptr(de, 2048); |
| 321 |
err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL); |
| 322 |
if (err < 0) |
| 323 |
goto out; |
| 324 |
} |
| 325 |
|
| 326 |
ni_lock_dir(dir_ni); |
| 327 |
ni_lock(ni); |
| 328 |
if (dir_ni != new_dir_ni) |
| 329 |
ni_lock_dir2(new_dir_ni); |
| 330 |
|
| 331 |
err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de); |
| 332 |
if (!err) { |
| 333 |
simple_rename_timestamp(dir, dentry, new_dir, new_dentry); |
| 334 |
mark_inode_dirty(inode); |
| 335 |
mark_inode_dirty(dir); |
| 336 |
if (dir != new_dir) |
| 337 |
mark_inode_dirty(new_dir); |
| 338 |
|
| 339 |
if (IS_DIRSYNC(dir)) |
| 340 |
ntfs_sync_inode(dir); |
| 341 |
|
| 342 |
if (IS_DIRSYNC(new_dir)) |
| 343 |
ntfs_sync_inode(inode); |
| 344 |
} |
| 345 |
|
| 346 |
if (dir_ni != new_dir_ni) |
| 347 |
ni_unlock(new_dir_ni); |
| 348 |
ni_unlock(ni); |
| 349 |
ni_unlock(dir_ni); |
| 350 |
out: |
| 351 |
kfree(de); |
| 352 |
return err; |
| 353 |
} |
| 354 |
|
| 355 |
struct dentry *ntfs3_get_parent(struct dentry *child) |
| 356 |
{ |
| 357 |
struct inode *inode = d_inode(child); |
| 358 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 359 |
|
| 360 |
struct ATTR_LIST_ENTRY *le = NULL; |
| 361 |
struct ATTRIB *attr = NULL; |
| 362 |
struct ATTR_FILE_NAME *fname; |
| 363 |
|
| 364 |
while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL, |
| 365 |
NULL))) { |
| 366 |
fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); |
| 367 |
if (!fname) |
| 368 |
continue; |
| 369 |
|
| 370 |
return d_obtain_alias( |
| 371 |
ntfs_iget5(inode->i_sb, &fname->home, NULL)); |
| 372 |
} |
| 373 |
|
| 374 |
return ERR_PTR(-ENOENT); |
| 375 |
} |
| 376 |
|
| 377 |
/* |
| 378 |
* dentry_operations::d_hash |
| 379 |
*/ |
| 380 |
static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name) |
| 381 |
{ |
| 382 |
struct ntfs_sb_info *sbi; |
| 383 |
const char *n = name->name; |
| 384 |
unsigned int len = name->len; |
| 385 |
unsigned long hash; |
| 386 |
struct cpu_str *uni; |
| 387 |
unsigned int c; |
| 388 |
int err; |
| 389 |
|
| 390 |
/* First try fast implementation. */ |
| 391 |
hash = init_name_hash(dentry); |
| 392 |
|
| 393 |
for (;;) { |
| 394 |
if (!len--) { |
| 395 |
name->hash = end_name_hash(hash); |
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
c = *n++; |
| 400 |
if (c >= 0x80) |
| 401 |
break; |
| 402 |
|
| 403 |
hash = partial_name_hash(toupper(c), hash); |
| 404 |
} |
| 405 |
|
| 406 |
/* |
| 407 |
* Try slow way with current upcase table |
| 408 |
*/ |
| 409 |
uni = kmalloc(PATH_MAX, GFP_NOWAIT); |
| 410 |
if (!uni) |
| 411 |
return -ENOMEM; |
| 412 |
|
| 413 |
sbi = dentry->d_sb->s_fs_info; |
| 414 |
|
| 415 |
err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN, |
| 416 |
UTF16_HOST_ENDIAN); |
| 417 |
if (err < 0) |
| 418 |
goto out; |
| 419 |
|
| 420 |
if (!err) { |
| 421 |
err = -EINVAL; |
| 422 |
goto out; |
| 423 |
} |
| 424 |
|
| 425 |
hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase, |
| 426 |
init_name_hash(dentry)); |
| 427 |
name->hash = end_name_hash(hash); |
| 428 |
err = 0; |
| 429 |
|
| 430 |
out: |
| 431 |
kfree(uni); |
| 432 |
return err; |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
* dentry_operations::d_compare |
| 437 |
*/ |
| 438 |
static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1, |
| 439 |
const char *str, const struct qstr *name) |
| 440 |
{ |
| 441 |
struct ntfs_sb_info *sbi; |
| 442 |
int ret; |
| 443 |
const char *n1 = str; |
| 444 |
const char *n2 = name->name; |
| 445 |
unsigned int len2 = name->len; |
| 446 |
unsigned int lm = min(len1, len2); |
| 447 |
unsigned char c1, c2; |
| 448 |
struct cpu_str *uni1; |
| 449 |
struct le_str *uni2; |
| 450 |
|
| 451 |
/* First try fast implementation. */ |
| 452 |
for (;;) { |
| 453 |
if (!lm--) |
| 454 |
return len1 != len2; |
| 455 |
|
| 456 |
if ((c1 = *n1++) == (c2 = *n2++)) |
| 457 |
continue; |
| 458 |
|
| 459 |
if (c1 >= 0x80 || c2 >= 0x80) |
| 460 |
break; |
| 461 |
|
| 462 |
if (toupper(c1) != toupper(c2)) |
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
/* |
| 467 |
* Try slow way with current upcase table |
| 468 |
*/ |
| 469 |
sbi = dentry->d_sb->s_fs_info; |
| 470 |
uni1 = kmalloc(PATH_MAX, GFP_NOWAIT); |
| 471 |
if (!uni1) |
| 472 |
return -ENOMEM; |
| 473 |
|
| 474 |
ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN, |
| 475 |
UTF16_HOST_ENDIAN); |
| 476 |
if (ret < 0) |
| 477 |
goto out; |
| 478 |
|
| 479 |
if (!ret) { |
| 480 |
ret = -EINVAL; |
| 481 |
goto out; |
| 482 |
} |
| 483 |
|
| 484 |
uni2 = Add2Ptr(uni1, 2048); |
| 485 |
|
| 486 |
ret = ntfs_nls_to_utf16(sbi, name->name, name->len, |
| 487 |
(struct cpu_str *)uni2, NTFS_NAME_LEN, |
| 488 |
UTF16_LITTLE_ENDIAN); |
| 489 |
if (ret < 0) |
| 490 |
goto out; |
| 491 |
|
| 492 |
if (!ret) { |
| 493 |
ret = -EINVAL; |
| 494 |
goto out; |
| 495 |
} |
| 496 |
|
| 497 |
ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1; |
| 498 |
|
| 499 |
out: |
| 500 |
kfree(uni1); |
| 501 |
return ret; |
| 502 |
} |
| 503 |
|
| 504 |
// clang-format off |
| 505 |
const struct inode_operations ntfs_dir_inode_operations = { |
| 506 |
.lookup = ntfs_lookup, |
| 507 |
.create = ntfs_create, |
| 508 |
.link = ntfs_link, |
| 509 |
.unlink = ntfs_unlink, |
| 510 |
.symlink = ntfs_symlink, |
| 511 |
.mkdir = ntfs_mkdir, |
| 512 |
.rmdir = ntfs_rmdir, |
| 513 |
.mknod = ntfs_mknod, |
| 514 |
.rename = ntfs_rename, |
| 515 |
.get_acl = ntfs_get_acl, |
| 516 |
.set_acl = ntfs_set_acl, |
| 517 |
.setattr = ntfs_setattr, |
| 518 |
.getattr = ntfs_getattr, |
| 519 |
.listxattr = ntfs_listxattr, |
| 520 |
.fiemap = ntfs_fiemap, |
| 521 |
}; |
| 522 |
|
| 523 |
const struct inode_operations ntfs_special_inode_operations = { |
| 524 |
.setattr = ntfs_setattr, |
| 525 |
.getattr = ntfs_getattr, |
| 526 |
.listxattr = ntfs_listxattr, |
| 527 |
.get_acl = ntfs_get_acl, |
| 528 |
.set_acl = ntfs_set_acl, |
| 529 |
}; |
| 530 |
|
| 531 |
const struct dentry_operations ntfs_dentry_ops = { |
| 532 |
.d_hash = ntfs_d_hash, |
| 533 |
.d_compare = ntfs_d_compare, |
| 534 |
}; |
| 535 |
|
| 536 |
// clang-format on |