Merge tag 'sched-core-2023-04-27' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / fs / ntfs3 / namei.c
CommitLineData
4342306f
KK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 */
7
4342306f 8#include <linux/fs.h>
4342306f 9#include <linux/nls.h>
a3a956c7 10#include <linux/ctype.h>
2b108260 11#include <linux/posix_acl.h>
4342306f
KK
12
13#include "debug.h"
14#include "ntfs.h"
15#include "ntfs_fs.h"
16
17/*
e8b8e97f 18 * fill_name_de - Format NTFS_DE in @buf.
4342306f
KK
19 */
20int 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 {
e8b8e97f 45 /* Convert input string to unicode. */
4342306f
KK
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
fa3cacf5 56 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
4342306f
KK
57 e->key_size = cpu_to_le16(data_size);
58 e->flags = 0;
59 e->res = 0;
60
61 return 0;
62}
63
64/*
e8b8e97f 65 * ntfs_lookup - inode_operations::lookup
4342306f
KK
66 */
67static 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(ni);
85 inode = dir_search_u(dir, uni, NULL);
86 ni_unlock(ni);
87 }
88 __putname(uni);
89 }
90
91 return d_splice_alias(inode, dentry);
92}
93
94/*
e8b8e97f 95 * ntfs_create - inode_operations::create
4342306f 96 */
6c960e68 97static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
4342306f
KK
98 struct dentry *dentry, umode_t mode, bool excl)
99{
4342306f
KK
100 struct inode *inode;
101
700b7940 102 inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode,
4342306f
KK
103 0, NULL, 0, NULL);
104
4342306f
KK
105 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
106}
107
108/*
109 * ntfs_mknod
110 *
111 * inode_operations::mknod
112 */
5ebb29be 113static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
4342306f
KK
114 struct dentry *dentry, umode_t mode, dev_t rdev)
115{
4342306f
KK
116 struct inode *inode;
117
700b7940 118 inode = ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev,
4342306f
KK
119 NULL, 0, NULL);
120
4342306f
KK
121 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
122}
123
124/*
e8b8e97f 125 * ntfs_link - inode_operations::link
4342306f
KK
126 */
127static 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
4342306f
KK
143 inc_nlink(inode);
144 ihold(inode);
145
146 err = ntfs_link_inode(inode, de);
78ab59fe 147
4342306f 148 if (!err) {
78ab59fe
KK
149 dir->i_ctime = dir->i_mtime = inode->i_ctime =
150 current_time(dir);
4342306f
KK
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/*
e8b8e97f 167 * ntfs_unlink - inode_operations::unlink
4342306f
KK
168 */
169static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
170{
171 struct ntfs_inode *ni = ntfs_i(dir);
172 int err;
173
174 ni_lock_dir(ni);
175
176 err = ntfs_unlink_inode(dir, dentry);
177
178 ni_unlock(ni);
179
180 return err;
181}
182
183/*
e8b8e97f 184 * ntfs_symlink - inode_operations::symlink
4342306f 185 */
7a77db95 186static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
4342306f
KK
187 struct dentry *dentry, const char *symname)
188{
189 u32 size = strlen(symname);
190 struct inode *inode;
4342306f 191
700b7940 192 inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777,
4342306f
KK
193 0, symname, size, NULL);
194
4342306f
KK
195 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
196}
197
198/*
e8b8e97f 199 * ntfs_mkdir- inode_operations::mkdir
4342306f 200 */
c54bd91e 201static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
4342306f
KK
202 struct dentry *dentry, umode_t mode)
203{
204 struct inode *inode;
4342306f 205
700b7940 206 inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode,
4342306f
KK
207 0, NULL, 0, NULL);
208
4342306f
KK
209 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
210}
211
212/*
6700eabb 213 * ntfs_rmdir - inode_operations::rmdir
4342306f
KK
214 */
215static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
216{
217 struct ntfs_inode *ni = ntfs_i(dir);
218 int err;
219
220 ni_lock_dir(ni);
221
222 err = ntfs_unlink_inode(dir, dentry);
223
224 ni_unlock(ni);
225
226 return err;
227}
228
229/*
e8b8e97f 230 * ntfs_rename - inode_operations::rename
4342306f 231 */
e18275ae 232static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
78ab59fe 233 struct dentry *dentry, struct inode *new_dir,
4342306f
KK
234 struct dentry *new_dentry, u32 flags)
235{
236 int err;
78ab59fe 237 struct super_block *sb = dir->i_sb;
4342306f 238 struct ntfs_sb_info *sbi = sb->s_fs_info;
78ab59fe 239 struct ntfs_inode *dir_ni = ntfs_i(dir);
4342306f 240 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
78ab59fe
KK
241 struct inode *inode = d_inode(dentry);
242 struct ntfs_inode *ni = ntfs_i(inode);
243 struct inode *new_inode = d_inode(new_dentry);
244 struct NTFS_DE *de, *new_de;
245 bool is_same, is_bad;
246 /*
247 * de - memory of PATH_MAX bytes:
248 * [0-1024) - original name (dentry->d_name)
249 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
250 * [2048-3072) - new name (new_dentry->d_name)
251 */
4342306f
KK
252 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
253 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
254 1024);
255 static_assert(PATH_MAX >= 4 * 1024);
256
257 if (flags & ~RENAME_NOREPLACE)
258 return -EINVAL;
259
78ab59fe
KK
260 is_same = dentry->d_name.len == new_dentry->d_name.len &&
261 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
262 dentry->d_name.len);
4342306f 263
78ab59fe 264 if (is_same && dir == new_dir) {
e8b8e97f 265 /* Nothing to do. */
78ab59fe 266 return 0;
4342306f
KK
267 }
268
78ab59fe
KK
269 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
270 /* Should we print an error? */
271 return -EINVAL;
4342306f
KK
272 }
273
274 if (new_inode) {
e8b8e97f 275 /* Target name exists. Unlink it. */
4342306f
KK
276 dget(new_dentry);
277 ni_lock_dir(new_dir_ni);
278 err = ntfs_unlink_inode(new_dir, new_dentry);
279 ni_unlock(new_dir_ni);
280 dput(new_dentry);
281 if (err)
78ab59fe 282 return err;
4342306f
KK
283 }
284
e8b8e97f 285 /* Allocate PATH_MAX bytes. */
78ab59fe
KK
286 de = __getname();
287 if (!de)
288 return -ENOMEM;
4342306f 289
78ab59fe
KK
290 /* Translate dentry->d_name into unicode form. */
291 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
4342306f 292 if (err < 0)
78ab59fe 293 goto out;
4342306f
KK
294
295 if (is_same) {
78ab59fe
KK
296 /* Reuse 'de'. */
297 new_de = de;
4342306f 298 } else {
78ab59fe
KK
299 /* Translate new_dentry->d_name into unicode form. */
300 new_de = Add2Ptr(de, 2048);
4342306f
KK
301 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
302 if (err < 0)
78ab59fe 303 goto out;
4342306f
KK
304 }
305
78ab59fe
KK
306 ni_lock_dir(dir_ni);
307 ni_lock(ni);
0ad9dfcb
KK
308 if (dir_ni != new_dir_ni)
309 ni_lock_dir2(new_dir_ni);
78ab59fe
KK
310
311 is_bad = false;
312 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
313 if (is_bad) {
314 /* Restore after failed rename failed too. */
c12df45e 315 _ntfs_bad_inode(inode);
78ab59fe
KK
316 } else if (!err) {
317 inode->i_ctime = dir->i_ctime = dir->i_mtime =
318 current_time(dir);
319 mark_inode_dirty(inode);
320 mark_inode_dirty(dir);
321 if (dir != new_dir) {
322 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
323 mark_inode_dirty(new_dir);
4342306f 324 }
4342306f 325
78ab59fe
KK
326 if (IS_DIRSYNC(dir))
327 ntfs_sync_inode(dir);
4342306f 328
78ab59fe
KK
329 if (IS_DIRSYNC(new_dir))
330 ntfs_sync_inode(inode);
4342306f
KK
331 }
332
0ad9dfcb
KK
333 if (dir_ni != new_dir_ni)
334 ni_unlock(new_dir_ni);
78ab59fe
KK
335 ni_unlock(ni);
336 ni_unlock(dir_ni);
4342306f 337out:
78ab59fe 338 __putname(de);
4342306f
KK
339 return err;
340}
341
2b108260
KK
342/*
343 * ntfs_atomic_open
344 *
345 * inode_operations::atomic_open
346 */
347static int ntfs_atomic_open(struct inode *dir, struct dentry *dentry,
348 struct file *file, u32 flags, umode_t mode)
349{
350 int err;
351 struct inode *inode;
352 struct ntfs_fnd *fnd = NULL;
353 struct ntfs_inode *ni = ntfs_i(dir);
354 struct dentry *d = NULL;
355 struct cpu_str *uni = __getname();
356 bool locked = false;
357
358 if (!uni)
359 return -ENOMEM;
360
361 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
362 dentry->d_name.len, uni, NTFS_NAME_LEN,
363 UTF16_HOST_ENDIAN);
364 if (err < 0)
365 goto out;
366
367#ifdef CONFIG_NTFS3_FS_POSIX_ACL
368 if (IS_POSIXACL(dir)) {
369 /*
370 * Load in cache current acl to avoid ni_lock(dir):
371 * ntfs_create_inode -> ntfs_init_acl -> posix_acl_create ->
372 * ntfs_get_acl -> ntfs_get_acl_ex -> ni_lock
373 */
6022ec6e 374 struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
2b108260
KK
375
376 if (IS_ERR(p)) {
377 err = PTR_ERR(p);
378 goto out;
379 }
380 posix_acl_release(p);
381 }
382#endif
383
384 if (d_in_lookup(dentry)) {
385 ni_lock_dir(ni);
386 locked = true;
387 fnd = fnd_get();
388 if (!fnd) {
389 err = -ENOMEM;
390 goto out1;
391 }
392
393 d = d_splice_alias(dir_search_u(dir, uni, fnd), dentry);
394 if (IS_ERR(d)) {
395 err = PTR_ERR(d);
396 d = NULL;
397 goto out2;
398 }
399
400 if (d)
401 dentry = d;
402 }
403
404 if (!(flags & O_CREAT) || d_really_is_positive(dentry)) {
405 err = finish_no_open(file, d);
406 goto out2;
407 }
408
409 file->f_mode |= FMODE_CREATED;
410
411 /*
412 * fnd contains tree's path to insert to.
413 * If fnd is not NULL then dir is locked.
414 */
415
416 /*
417 * Unfortunately I don't know how to get here correct 'struct nameidata *nd'
700b7940 418 * or 'struct mnt_idmap *idmap'.
2b108260
KK
419 * See atomic_open in fs/namei.c.
420 * This is why xfstest/633 failed.
700b7940 421 * Looks like ntfs_atomic_open must accept 'struct mnt_idmap *idmap' as argument.
2b108260
KK
422 */
423
700b7940 424 inode = ntfs_create_inode(&nop_mnt_idmap, dir, dentry, uni, mode, 0,
2b108260
KK
425 NULL, 0, fnd);
426 err = IS_ERR(inode) ? PTR_ERR(inode)
427 : finish_open(file, dentry, ntfs_file_open);
428 dput(d);
429
430out2:
431 fnd_put(fnd);
432out1:
433 if (locked)
434 ni_unlock(ni);
435out:
436 __putname(uni);
437 return err;
438}
439
4342306f
KK
440struct dentry *ntfs3_get_parent(struct dentry *child)
441{
442 struct inode *inode = d_inode(child);
443 struct ntfs_inode *ni = ntfs_i(inode);
444
445 struct ATTR_LIST_ENTRY *le = NULL;
446 struct ATTRIB *attr = NULL;
447 struct ATTR_FILE_NAME *fname;
448
449 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
450 NULL))) {
451 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
452 if (!fname)
453 continue;
454
455 return d_obtain_alias(
456 ntfs_iget5(inode->i_sb, &fname->home, NULL));
457 }
458
459 return ERR_PTR(-ENOENT);
460}
461
a3a956c7
KK
462/*
463 * dentry_operations::d_hash
464 */
465static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
466{
467 struct ntfs_sb_info *sbi;
468 const char *n = name->name;
469 unsigned int len = name->len;
470 unsigned long hash;
471 struct cpu_str *uni;
472 unsigned int c;
473 int err;
474
475 /* First try fast implementation. */
476 hash = init_name_hash(dentry);
477
478 for (;;) {
479 if (!len--) {
480 name->hash = end_name_hash(hash);
481 return 0;
482 }
483
484 c = *n++;
485 if (c >= 0x80)
486 break;
487
488 hash = partial_name_hash(toupper(c), hash);
489 }
490
491 /*
492 * Try slow way with current upcase table
493 */
494 uni = __getname();
495 if (!uni)
496 return -ENOMEM;
497
498 sbi = dentry->d_sb->s_fs_info;
499
500 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
501 UTF16_HOST_ENDIAN);
502 if (err < 0)
503 goto out;
504
505 if (!err) {
506 err = -EINVAL;
507 goto out;
508 }
509
510 hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
511 init_name_hash(dentry));
512 name->hash = end_name_hash(hash);
513 err = 0;
514
515out:
516 __putname(uni);
517 return err;
518}
519
520/*
521 * dentry_operations::d_compare
522 */
523static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
524 const char *str, const struct qstr *name)
525{
526 struct ntfs_sb_info *sbi;
527 int ret;
528 const char *n1 = str;
529 const char *n2 = name->name;
530 unsigned int len2 = name->len;
531 unsigned int lm = min(len1, len2);
532 unsigned char c1, c2;
9144b438
KK
533 struct cpu_str *uni1;
534 struct le_str *uni2;
a3a956c7
KK
535
536 /* First try fast implementation. */
537 for (;;) {
0d6d7c61 538 if (!lm--)
75b5e472 539 return len1 != len2;
a3a956c7
KK
540
541 if ((c1 = *n1++) == (c2 = *n2++))
542 continue;
543
544 if (c1 >= 0x80 || c2 >= 0x80)
545 break;
546
0d6d7c61
NC
547 if (toupper(c1) != toupper(c2))
548 return 1;
a3a956c7
KK
549 }
550
551 /*
552 * Try slow way with current upcase table
553 */
554 sbi = dentry->d_sb->s_fs_info;
555 uni1 = __getname();
556 if (!uni1)
557 return -ENOMEM;
558
559 ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
560 UTF16_HOST_ENDIAN);
561 if (ret < 0)
562 goto out;
563
564 if (!ret) {
565 ret = -EINVAL;
566 goto out;
567 }
568
569 uni2 = Add2Ptr(uni1, 2048);
570
9144b438
KK
571 ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
572 (struct cpu_str *)uni2, NTFS_NAME_LEN,
573 UTF16_LITTLE_ENDIAN);
a3a956c7
KK
574 if (ret < 0)
575 goto out;
576
577 if (!ret) {
578 ret = -EINVAL;
579 goto out;
580 }
581
9144b438 582 ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
a3a956c7
KK
583
584out:
585 __putname(uni1);
586 return ret;
587}
588
4342306f
KK
589// clang-format off
590const struct inode_operations ntfs_dir_inode_operations = {
591 .lookup = ntfs_lookup,
592 .create = ntfs_create,
593 .link = ntfs_link,
594 .unlink = ntfs_unlink,
595 .symlink = ntfs_symlink,
596 .mkdir = ntfs_mkdir,
597 .rmdir = ntfs_rmdir,
598 .mknod = ntfs_mknod,
599 .rename = ntfs_rename,
600 .permission = ntfs_permission,
cac2f8b8 601 .get_inode_acl = ntfs_get_acl,
4342306f
KK
602 .set_acl = ntfs_set_acl,
603 .setattr = ntfs3_setattr,
604 .getattr = ntfs_getattr,
605 .listxattr = ntfs_listxattr,
2b108260 606 .atomic_open = ntfs_atomic_open,
4342306f
KK
607 .fiemap = ntfs_fiemap,
608};
609
610const struct inode_operations ntfs_special_inode_operations = {
611 .setattr = ntfs3_setattr,
612 .getattr = ntfs_getattr,
613 .listxattr = ntfs_listxattr,
cac2f8b8 614 .get_inode_acl = ntfs_get_acl,
4342306f
KK
615 .set_acl = ntfs_set_acl,
616};
a3a956c7
KK
617
618const struct dentry_operations ntfs_dentry_ops = {
619 .d_hash = ntfs_d_hash,
620 .d_compare = ntfs_d_compare,
621};
622
4342306f 623// clang-format on