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 = __getname(); 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 __putname(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, S_IFDIR | mode, 0, 216 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 /* Allocate PATH_MAX bytes. */ 307 de = __getname(); 308 if (!de) 309 return -ENOMEM; 310 311 /* Translate dentry->d_name into unicode form. */ 312 err = fill_name_de(sbi, de, &dentry->d_name, NULL); 313 if (err < 0) 314 goto out; 315 316 if (is_same) { 317 /* Reuse 'de'. */ 318 new_de = de; 319 } else { 320 /* Translate new_dentry->d_name into unicode form. */ 321 new_de = Add2Ptr(de, 2048); 322 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL); 323 if (err < 0) 324 goto out; 325 } 326 327 ni_lock_dir(dir_ni); 328 ni_lock(ni); 329 if (dir_ni != new_dir_ni) 330 ni_lock_dir2(new_dir_ni); 331 332 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de); 333 if (!err) { 334 simple_rename_timestamp(dir, dentry, new_dir, new_dentry); 335 mark_inode_dirty(inode); 336 mark_inode_dirty(dir); 337 if (dir != new_dir) 338 mark_inode_dirty(new_dir); 339 340 if (IS_DIRSYNC(dir)) 341 ntfs_sync_inode(dir); 342 343 if (IS_DIRSYNC(new_dir)) 344 ntfs_sync_inode(inode); 345 } 346 347 if (dir_ni != new_dir_ni) 348 ni_unlock(new_dir_ni); 349 ni_unlock(ni); 350 ni_unlock(dir_ni); 351 out: 352 __putname(de); 353 return err; 354 } 355 356 struct dentry *ntfs3_get_parent(struct dentry *child) 357 { 358 struct inode *inode = d_inode(child); 359 struct ntfs_inode *ni = ntfs_i(inode); 360 361 struct ATTR_LIST_ENTRY *le = NULL; 362 struct ATTRIB *attr = NULL; 363 struct ATTR_FILE_NAME *fname; 364 365 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL, 366 NULL))) { 367 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 368 if (!fname) 369 continue; 370 371 return d_obtain_alias( 372 ntfs_iget5(inode->i_sb, &fname->home, NULL)); 373 } 374 375 return ERR_PTR(-ENOENT); 376 } 377 378 /* 379 * dentry_operations::d_hash 380 */ 381 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name) 382 { 383 struct ntfs_sb_info *sbi; 384 const char *n = name->name; 385 unsigned int len = name->len; 386 unsigned long hash; 387 struct cpu_str *uni; 388 unsigned int c; 389 int err; 390 391 /* First try fast implementation. */ 392 hash = init_name_hash(dentry); 393 394 for (;;) { 395 if (!len--) { 396 name->hash = end_name_hash(hash); 397 return 0; 398 } 399 400 c = *n++; 401 if (c >= 0x80) 402 break; 403 404 hash = partial_name_hash(toupper(c), hash); 405 } 406 407 /* 408 * Try slow way with current upcase table 409 */ 410 uni = kmem_cache_alloc(names_cachep, GFP_NOWAIT); 411 if (!uni) 412 return -ENOMEM; 413 414 sbi = dentry->d_sb->s_fs_info; 415 416 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN, 417 UTF16_HOST_ENDIAN); 418 if (err < 0) 419 goto out; 420 421 if (!err) { 422 err = -EINVAL; 423 goto out; 424 } 425 426 hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase, 427 init_name_hash(dentry)); 428 name->hash = end_name_hash(hash); 429 err = 0; 430 431 out: 432 kmem_cache_free(names_cachep, uni); 433 return err; 434 } 435 436 /* 437 * dentry_operations::d_compare 438 */ 439 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1, 440 const char *str, const struct qstr *name) 441 { 442 struct ntfs_sb_info *sbi; 443 int ret; 444 const char *n1 = str; 445 const char *n2 = name->name; 446 unsigned int len2 = name->len; 447 unsigned int lm = min(len1, len2); 448 unsigned char c1, c2; 449 struct cpu_str *uni1; 450 struct le_str *uni2; 451 452 /* First try fast implementation. */ 453 for (;;) { 454 if (!lm--) 455 return len1 != len2; 456 457 if ((c1 = *n1++) == (c2 = *n2++)) 458 continue; 459 460 if (c1 >= 0x80 || c2 >= 0x80) 461 break; 462 463 if (toupper(c1) != toupper(c2)) 464 return 1; 465 } 466 467 /* 468 * Try slow way with current upcase table 469 */ 470 sbi = dentry->d_sb->s_fs_info; 471 uni1 = __getname(); 472 if (!uni1) 473 return -ENOMEM; 474 475 ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN, 476 UTF16_HOST_ENDIAN); 477 if (ret < 0) 478 goto out; 479 480 if (!ret) { 481 ret = -EINVAL; 482 goto out; 483 } 484 485 uni2 = Add2Ptr(uni1, 2048); 486 487 ret = ntfs_nls_to_utf16(sbi, name->name, name->len, 488 (struct cpu_str *)uni2, NTFS_NAME_LEN, 489 UTF16_LITTLE_ENDIAN); 490 if (ret < 0) 491 goto out; 492 493 if (!ret) { 494 ret = -EINVAL; 495 goto out; 496 } 497 498 ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1; 499 500 out: 501 __putname(uni1); 502 return ret; 503 } 504 505 // clang-format off 506 const struct inode_operations ntfs_dir_inode_operations = { 507 .lookup = ntfs_lookup, 508 .create = ntfs_create, 509 .link = ntfs_link, 510 .unlink = ntfs_unlink, 511 .symlink = ntfs_symlink, 512 .mkdir = ntfs_mkdir, 513 .rmdir = ntfs_rmdir, 514 .mknod = ntfs_mknod, 515 .rename = ntfs_rename, 516 .get_acl = ntfs_get_acl, 517 .set_acl = ntfs_set_acl, 518 .setattr = ntfs_setattr, 519 .getattr = ntfs_getattr, 520 .listxattr = ntfs_listxattr, 521 .fiemap = ntfs_fiemap, 522 }; 523 524 const struct inode_operations ntfs_special_inode_operations = { 525 .setattr = ntfs_setattr, 526 .getattr = ntfs_getattr, 527 .listxattr = ntfs_listxattr, 528 .get_acl = ntfs_get_acl, 529 .set_acl = ntfs_set_acl, 530 }; 531 532 const struct dentry_operations ntfs_dentry_ops = { 533 .d_hash = ntfs_d_hash, 534 .d_compare = ntfs_d_compare, 535 }; 536 537 // clang-format on 538