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