-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnamei.c
More file actions
1883 lines (1671 loc) · 53.3 KB
/
namei.c
File metadata and controls
1883 lines (1671 loc) · 53.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* NTFS kernel directory inode operations.
*
* Copyright (c) 2001-2006 Anton Altaparmakov
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include <linux/exportfs.h>
#include <linux/iversion.h>
#include "ntfs.h"
#include "time.h"
#include "index.h"
#include "reparse.h"
#include "object_id.h"
#include "ea.h"
static const __le16 aux_name_le[3] = {
cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X')
};
static const __le16 con_name_le[3] = {
cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N')
};
static const __le16 com_name_le[3] = {
cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M')
};
static const __le16 lpt_name_le[3] = {
cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T')
};
static const __le16 nul_name_le[3] = {
cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L')
};
static const __le16 prn_name_le[3] = {
cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N')
};
static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len)
{
int i;
for (i = 0; i < wc_len; i++) {
u16 c = le16_to_cpu(wc[i]);
if (c < 0x0020 ||
c == 0x0022 || c == 0x002A || c == 0x002F ||
c == 0x003A || c == 0x003C || c == 0x003E ||
c == 0x003F || c == 0x005C || c == 0x007C)
return -EINVAL;
}
return 0;
}
static int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
const __le16 *wc,
unsigned int wc_len)
{
if (ntfs_check_bad_char(wc, wc_len))
return -EINVAL;
if (!NVolCheckWindowsNames(vol))
return 0;
/* Check for trailing space or dot. */
if (wc_len > 0 &&
(wc[wc_len - 1] == cpu_to_le16(' ') ||
wc[wc_len - 1] == cpu_to_le16('.')))
return -EINVAL;
if (wc_len == 3 || (wc_len > 3 && wc[3] == cpu_to_le16('.'))) {
__le16 *upcase = vol->upcase;
u32 size = vol->upcase_len;
if (ntfs_are_names_equal(wc, 3, aux_name_le, 3, IGNORE_CASE, upcase, size) ||
ntfs_are_names_equal(wc, 3, con_name_le, 3, IGNORE_CASE, upcase, size) ||
ntfs_are_names_equal(wc, 3, nul_name_le, 3, IGNORE_CASE, upcase, size) ||
ntfs_are_names_equal(wc, 3, prn_name_le, 3, IGNORE_CASE, upcase, size))
return -EINVAL;
}
if (wc_len == 4 || (wc_len > 4 && wc[4] == cpu_to_le16('.'))) {
__le16 *upcase = vol->upcase;
u32 size = vol->upcase_len, port;
if (ntfs_are_names_equal(wc, 3, com_name_le, 3, IGNORE_CASE, upcase, size) ||
ntfs_are_names_equal(wc, 3, lpt_name_le, 3, IGNORE_CASE, upcase, size)) {
port = le16_to_cpu(wc[3]);
if (port >= '1' && port <= '9')
return -EINVAL;
}
}
return 0;
}
/*
* ntfs_lookup - find the inode represented by a dentry in a directory inode
* @dir_ino: directory inode in which to look for the inode
* @dent: dentry representing the inode to look for
* @flags: lookup flags
*
* In short, ntfs_lookup() looks for the inode represented by the dentry @dent
* in the directory inode @dir_ino and if found attaches the inode to the
* dentry @dent.
*
* In more detail, the dentry @dent specifies which inode to look for by
* supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
* converts the name to Unicode and walks the contents of the directory inode
* @dir_ino looking for the converted Unicode name. If the name is found in the
* directory, the corresponding inode is loaded by calling ntfs_iget() on its
* inode number and the inode is associated with the dentry @dent via a call to
* d_splice_alias().
*
* If the name is not found in the directory, a NULL inode is inserted into the
* dentry @dent via a call to d_add(). The dentry is then termed a negative
* dentry.
*
* Only if an actual error occurs, do we return an error via ERR_PTR().
*
* In order to handle the case insensitivity issues of NTFS with regards to the
* dcache and the dcache requiring only one dentry per directory, we deal with
* dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
* a case sensitive dcache. This means that we get the full benefit of dcache
* speed when the file/directory is looked up with the same case as returned by
* ->ntfs_readdir() but that a lookup for any other case (or for the short file
* name) will not find anything in dcache and will enter ->ntfs_lookup()
* instead, where we search the directory for a fully matching file name
* (including case) and if that is not found, we search for a file name that
* matches with different case and if that has non-POSIX semantics we return
* that. We actually do only one search (case sensitive) and keep tabs on
* whether we have found a case insensitive match in the process.
*
* To simplify matters for us, we do not treat the short vs long filenames as
* two hard links but instead if the lookup matches a short filename, we
* return the dentry for the corresponding long filename instead.
*
* There are three cases we need to distinguish here:
*
* 1) @dent perfectly matches (i.e. including case) a directory entry with a
* file name in the WIN32 or POSIX namespaces. In this case
* ntfs_lookup_inode_by_name() will return with name set to NULL and we
* just d_splice_alias() @dent.
* 2) @dent matches (not including case) a directory entry with a file name in
* the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
* with name set to point to a kmalloc()ed ntfs_name structure containing
* the properly cased little endian Unicode name. We convert the name to the
* current NLS code page, search if a dentry with this name already exists
* and if so return that instead of @dent. At this point things are
* complicated by the possibility of 'disconnected' dentries due to NFS
* which we deal with appropriately (see the code comments). The VFS will
* then destroy the old @dent and use the one we returned. If a dentry is
* not found, we allocate a new one, d_splice_alias() it, and return it as
* above.
* 3) @dent matches either perfectly or not (i.e. we don't care about case) a
* directory entry with a file name in the DOS namespace. In this case
* ntfs_lookup_inode_by_name() will return with name set to point to a
* kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
* of the inode. We use the mft reference to read the inode and to find the
* file name in the WIN32 namespace corresponding to the matched short file
* name. We then convert the name to the current NLS code page, and proceed
* searching for a dentry with this name, etc, as in case 2), above.
*
* Locking: Caller must hold i_mutex on the directory.
*/
static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
unsigned int flags)
{
struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
struct inode *dent_inode;
__le16 *uname;
struct ntfs_name *name = NULL;
u64 mref;
unsigned long dent_ino;
int uname_len;
ntfs_debug("Looking up %pd in directory inode 0x%lx.",
dent, dir_ino->i_ino);
/* Convert the name of the dentry to Unicode. */
uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
if (uname_len != -ENAMETOOLONG)
ntfs_debug("Failed to convert name to Unicode.");
return ERR_PTR(uname_len);
}
mutex_lock(&NTFS_I(dir_ino)->mrec_lock);
mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
&name);
mutex_unlock(&NTFS_I(dir_ino)->mrec_lock);
kmem_cache_free(ntfs_name_cache, uname);
if (!IS_ERR_MREF(mref)) {
dent_ino = MREF(mref);
ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
dent_inode = ntfs_iget(vol->sb, dent_ino);
if (!IS_ERR(dent_inode)) {
/* Consistency check. */
if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no ||
dent_ino == FILE_MFT) {
/* Perfect WIN32/POSIX match. -- Case 1. */
if (!name) {
ntfs_debug("Done. (Case 1.)");
return d_splice_alias(dent_inode, dent);
}
/*
* We are too indented. Handle imperfect
* matches and short file names further below.
*/
goto handle_name;
}
ntfs_error(vol->sb,
"Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EIO. Run chkdsk.",
dent_ino, MSEQNO(mref),
NTFS_I(dent_inode)->seq_no);
iput(dent_inode);
dent_inode = ERR_PTR(-EIO);
} else
ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with error code %li.",
dent_ino, PTR_ERR(dent_inode));
kfree(name);
/* Return the error code. */
return ERR_CAST(dent_inode);
}
kfree(name);
/* It is guaranteed that @name is no longer allocated at this point. */
if (MREF_ERR(mref) == -ENOENT) {
ntfs_debug("Entry was not found, adding negative dentry.");
/* The dcache will handle negative entries. */
d_add(dent, NULL);
ntfs_debug("Done.");
return NULL;
}
ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error code %i.",
-MREF_ERR(mref));
return ERR_PTR(MREF_ERR(mref));
handle_name:
{
struct mft_record *m;
struct ntfs_attr_search_ctx *ctx;
struct ntfs_inode *ni = NTFS_I(dent_inode);
int err;
struct qstr nls_name;
nls_name.name = NULL;
if (name->type != FILE_NAME_DOS) { /* Case 2. */
ntfs_debug("Case 2.");
nls_name.len = (unsigned int)ntfs_ucstonls(vol,
(__le16 *)&name->name, name->len,
(unsigned char **)&nls_name.name, 0);
kfree(name);
} else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */
struct file_name_attr *fn;
ntfs_debug("Case 3.");
kfree(name);
/* Find the WIN32 name corresponding to the matched DOS name. */
ni = NTFS_I(dent_inode);
m = map_mft_record(ni);
if (IS_ERR(m)) {
err = PTR_ERR(m);
m = NULL;
ctx = NULL;
goto err_out;
}
ctx = ntfs_attr_get_search_ctx(ni, m);
if (unlikely(!ctx)) {
err = -ENOMEM;
goto err_out;
}
do {
struct attr_record *a;
u32 val_len;
err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
NULL, 0, ctx);
if (unlikely(err)) {
ntfs_error(vol->sb,
"Inode corrupt: No WIN32 namespace counterpart to DOS file name. Run chkdsk.");
if (err == -ENOENT)
err = -EIO;
goto err_out;
}
/* Consistency checks. */
a = ctx->attr;
if (a->non_resident || a->flags)
goto eio_err_out;
val_len = le32_to_cpu(a->data.resident.value_length);
if (le16_to_cpu(a->data.resident.value_offset) +
val_len > le32_to_cpu(a->length))
goto eio_err_out;
fn = (struct file_name_attr *)((u8 *)ctx->attr + le16_to_cpu(
ctx->attr->data.resident.value_offset));
if ((u32)(fn->file_name_length * sizeof(__le16) +
sizeof(struct file_name_attr)) > val_len)
goto eio_err_out;
} while (fn->file_name_type != FILE_NAME_WIN32);
/* Convert the found WIN32 name to current NLS code page. */
nls_name.len = (unsigned int)ntfs_ucstonls(vol,
(__le16 *)&fn->file_name, fn->file_name_length,
(unsigned char **)&nls_name.name, 0);
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(ni);
}
m = NULL;
ctx = NULL;
/* Check if a conversion error occurred. */
if ((int)nls_name.len < 0) {
err = (int)nls_name.len;
goto err_out;
}
nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len);
dent = d_add_ci(dent, dent_inode, &nls_name);
kfree(nls_name.name);
return dent;
eio_err_out:
ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
err = -EIO;
err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
if (m)
unmap_mft_record(ni);
iput(dent_inode);
ntfs_error(vol->sb, "Failed, returning error code %i.", err);
return ERR_PTR(err);
}
}
static int ntfs_sd_add_everyone(struct ntfs_inode *ni)
{
struct security_descriptor_relative *sd;
struct ntfs_acl *acl;
struct ntfs_ace *ace;
struct ntfs_sid *sid;
int ret, sd_len;
/* Create SECURITY_DESCRIPTOR attribute (everyone has full access). */
/*
* Calculate security descriptor length. We have 2 sub-authorities in
* owner and group SIDs, So add 8 bytes to every SID.
*/
sd_len = sizeof(struct security_descriptor_relative) + 2 *
(sizeof(struct ntfs_sid) + 8) + sizeof(struct ntfs_acl) +
sizeof(struct ntfs_ace) + 4;
sd = kmalloc(sd_len, GFP_NOFS);
if (!sd)
return -1;
sd->revision = 1;
sd->control = SE_DACL_PRESENT | SE_SELF_RELATIVE;
sid = (struct ntfs_sid *)((u8 *)sd + sizeof(struct security_descriptor_relative));
sid->revision = 1;
sid->sub_authority_count = 2;
sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
sid->identifier_authority.value[5] = 5;
sd->owner = cpu_to_le32((u8 *)sid - (u8 *)sd);
sid = (struct ntfs_sid *)((u8 *)sid + sizeof(struct ntfs_sid) + 8);
sid->revision = 1;
sid->sub_authority_count = 2;
sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
sid->identifier_authority.value[5] = 5;
sd->group = cpu_to_le32((u8 *)sid - (u8 *)sd);
acl = (struct ntfs_acl *)((u8 *)sid + sizeof(struct ntfs_sid) + 8);
acl->revision = 2;
acl->size = cpu_to_le16(sizeof(struct ntfs_acl) + sizeof(struct ntfs_ace) + 4);
acl->ace_count = cpu_to_le16(1);
sd->dacl = cpu_to_le32((u8 *)acl - (u8 *)sd);
ace = (struct ntfs_ace *)((u8 *)acl + sizeof(struct ntfs_acl));
ace->type = ACCESS_ALLOWED_ACE_TYPE;
ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE;
ace->size = cpu_to_le16(sizeof(struct ntfs_ace) + 4);
ace->mask = cpu_to_le32(0x1f01ff);
ace->sid.revision = 1;
ace->sid.sub_authority_count = 1;
ace->sid.sub_authority[0] = 0;
ace->sid.identifier_authority.value[5] = 1;
ret = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0, (u8 *)sd,
sd_len);
if (ret)
ntfs_error(ni->vol->sb, "Failed to add SECURITY_DESCRIPTOR\n");
kfree(sd);
return ret;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
__le16 *name, u8 name_len, mode_t mode, dev_t dev,
__le16 *target, int target_len)
#else
static struct ntfs_inode *__ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
__le16 *name, u8 name_len, mode_t mode, dev_t dev,
__le16 *target, int target_len)
#endif
{
struct ntfs_inode *dir_ni = NTFS_I(dir);
struct ntfs_volume *vol = dir_ni->vol;
struct ntfs_inode *ni;
bool rollback_data = false, rollback_sd = false, rollback_reparse = false;
struct file_name_attr *fn = NULL;
struct standard_information *si = NULL;
int err = 0, fn_len, si_len;
struct inode *vi;
struct mft_record *ni_mrec, *dni_mrec;
struct super_block *sb = dir_ni->vol->sb;
__le64 parent_mft_ref;
u64 child_mft_ref;
__le16 ea_size;
vi = new_inode(vol->sb);
if (!vi)
return ERR_PTR(-ENOMEM);
ntfs_init_big_inode(vi);
ni = NTFS_I(vi);
ni->vol = dir_ni->vol;
ni->name_len = 0;
ni->name = NULL;
/*
* Set the appropriate mode, attribute type, and name. For
* directories, also setup the index values to the defaults.
*/
if (S_ISDIR(mode)) {
mode &= ~vol->dmask;
NInoSetMstProtected(ni);
ni->itype.index.block_size = 4096;
ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
ni->itype.index.collation_rule = COLLATION_FILE_NAME;
if (vol->cluster_size <= ni->itype.index.block_size) {
ni->itype.index.vcn_size = vol->cluster_size;
ni->itype.index.vcn_size_bits =
vol->cluster_size_bits;
} else {
ni->itype.index.vcn_size = vol->sector_size;
ni->itype.index.vcn_size_bits =
vol->sector_size_bits;
}
} else {
mode &= ~vol->fmask;
}
if (IS_RDONLY(vi))
mode &= ~0222;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
inode_init_owner(idmap, vi, dir, mode);
#else
inode_init_owner(mnt_userns, vi, dir, mode);
#endif
mode = vi->i_mode;
#ifdef CONFIG_NTFS_FS_POSIX_ACL
if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
err = ntfs_init_acl(idmap, vi, dir);
if (err)
goto err_out;
} else
#endif
{
vi->i_flags |= S_NOSEC;
}
if (uid_valid(vol->uid))
vi->i_uid = vol->uid;
if (gid_valid(vol->gid))
vi->i_gid = vol->gid;
/*
* Set the file size to 0, the ntfs inode sizes are set to 0 by
* the call to ntfs_init_big_inode() below.
*/
vi->i_size = 0;
vi->i_blocks = 0;
inode_inc_iversion(vi);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
simple_inode_init_ts(vi);
ni->i_crtime = inode_get_ctime(vi);
#else
ni->i_crtime = vi->i_mtime = vi->i_atime = inode_set_ctime_current(vi);
#endif
#else
ni->i_crtime = vi->i_mtime = vi->i_atime = vi->i_ctime = current_time(vi);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
inode_set_mtime_to_ts(dir, ni->i_crtime);
inode_set_ctime_to_ts(dir, ni->i_crtime);
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
inode_set_ctime_to_ts(dir, ni->i_crtime);
dir->i_mtime = ni->i_crtime;
#else
dir->i_mtime = dir->i_ctime = ni->i_crtime;
#endif
#endif
mark_inode_dirty(dir);
err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL,
&ni_mrec);
if (err) {
iput(vi);
return ERR_PTR(err);
}
/*
* Prevent iget and writeback from finding this inode.
* Caller must call d_instantiate_new instead of d_instantiate.
*/
spin_lock(&vi->i_lock);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
inode_state_set(vi, I_NEW | I_CREATING);
#else
vi->i_state = I_NEW | I_CREATING;
#endif
spin_unlock(&vi->i_lock);
/* Add the inode to the inode hash for the superblock. */
vi->i_ino = ni->mft_no;
inode_set_iversion(vi, 1);
insert_inode_hash(vi);
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
if (NInoBeingDeleted(dir_ni)) {
err = -ENOENT;
goto err_out;
}
dni_mrec = map_mft_record(dir_ni);
if (IS_ERR(dni_mrec)) {
ntfs_error(dir_ni->vol->sb, "failed to map mft record for file %ld.\n",
dir_ni->mft_no);
err = -EIO;
goto err_out;
}
parent_mft_ref = MK_LE_MREF(dir_ni->mft_no,
le16_to_cpu(dni_mrec->sequence_number));
unmap_mft_record(dir_ni);
/*
* Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION
* version 1.2, windows will upgrade it to version 3 if needed.
*/
si_len = offsetof(struct standard_information, file_attributes) +
sizeof(__le32) + 12;
si = kzalloc(si_len, GFP_NOFS);
if (!si) {
err = -ENOMEM;
goto err_out;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime);
#else
si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime);
#endif
si->last_mft_change_time = si->last_access_time = si->creation_time;
if (!S_ISREG(mode) && !S_ISDIR(mode))
si->file_attributes = FILE_ATTR_SYSTEM;
/* Add STANDARD_INFORMATION to inode. */
err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si,
si_len);
if (err) {
ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n");
goto err_out;
}
err = ntfs_sd_add_everyone(ni);
if (err)
goto err_out;
rollback_sd = true;
if (S_ISDIR(mode)) {
struct index_root *ir = NULL;
struct index_entry *ie;
int ir_len, index_len;
/* Create struct index_root attribute. */
index_len = sizeof(struct index_header) + sizeof(struct index_entry_header);
ir_len = offsetof(struct index_root, index) + index_len;
ir = kzalloc(ir_len, GFP_NOFS);
if (!ir) {
err = -ENOMEM;
goto err_out;
}
ir->type = AT_FILE_NAME;
ir->collation_rule = COLLATION_FILE_NAME;
ir->index_block_size = cpu_to_le32(ni->vol->index_record_size);
if (ni->vol->cluster_size <= ni->vol->index_record_size)
ir->clusters_per_index_block =
NTFS_B_TO_CLU(vol, ni->vol->index_record_size);
else
ir->clusters_per_index_block =
ni->vol->index_record_size >> ni->vol->sector_size_bits;
ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header));
ir->index.index_length = cpu_to_le32(index_len);
ir->index.allocated_size = cpu_to_le32(index_len);
ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root));
ie->length = cpu_to_le16(sizeof(struct index_entry_header));
ie->key_length = 0;
ie->flags = INDEX_ENTRY_END;
/* Add struct index_root attribute to inode. */
err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len);
if (err) {
kfree(ir);
ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n");
goto err_out;
}
kfree(ir);
err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4);
if (err)
goto err_out;
} else {
/* Add DATA attribute to inode. */
err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0);
if (err) {
ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n");
goto err_out;
}
rollback_data = true;
err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
if (err)
goto err_out;
if (S_ISLNK(mode)) {
err = ntfs_reparse_set_wsl_symlink(ni, target, target_len);
if (!err)
rollback_reparse = true;
} else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) ||
S_ISFIFO(mode)) {
si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN;
ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN;
err = ntfs_reparse_set_wsl_not_symlink(ni, mode);
if (!err)
rollback_reparse = true;
}
if (err)
goto err_out;
}
err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size,
NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE);
if (err)
goto err_out;
/* Create FILE_NAME attribute. */
fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
fn = kzalloc(fn_len, GFP_NOFS);
if (!fn) {
err = -ENOMEM;
goto err_out;
}
fn->file_attributes |= ni->flags;
fn->parent_directory = parent_mft_ref;
fn->file_name_length = name_len;
fn->file_name_type = FILE_NAME_POSIX;
fn->type.ea.packed_ea_size = ea_size;
if (S_ISDIR(mode)) {
fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
fn->allocated_size = fn->data_size = 0;
} else {
fn->data_size = cpu_to_le64(ni->data_size);
fn->allocated_size = cpu_to_le64(ni->allocated_size);
}
if (!S_ISREG(mode) && !S_ISDIR(mode)) {
fn->file_attributes = FILE_ATTR_SYSTEM;
if (rollback_reparse)
fn->file_attributes |= FILE_ATTR_REPARSE_POINT;
}
if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
fn->file_attributes |= FILE_ATTR_HIDDEN;
fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime);
fn->last_mft_change_time = fn->last_access_time = fn->creation_time;
memcpy(fn->file_name, name, name_len * sizeof(__le16));
/* Add FILE_NAME attribute to inode. */
err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
if (err) {
ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
goto err_out;
}
child_mft_ref = MK_MREF(ni->mft_no,
le16_to_cpu(ni_mrec->sequence_number));
/* Set hard links count and directory flag. */
ni_mrec->link_count = cpu_to_le16(1);
mark_mft_record_dirty(ni);
/* Add FILE_NAME attribute to index. */
err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref);
if (err) {
ntfs_debug("Failed to add entry to the index");
goto err_out;
}
unmap_mft_record(ni);
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
ni->flags = fn->file_attributes;
/* Set the sequence number. */
vi->i_generation = ni->seq_no;
set_nlink(vi, 1);
ntfs_set_vfs_operations(vi, mode, dev);
/* Done! */
kfree(fn);
kfree(si);
ntfs_debug("Done.\n");
return ni;
err_out:
if (rollback_sd)
ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0);
if (rollback_data)
ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0);
if (rollback_reparse)
ntfs_delete_reparse_index(ni);
/*
* Free extent MFT records (should not exist any with current
* ntfs_create implementation, but for any case if something will be
* changed in the future).
*/
while (ni->nr_extents != 0) {
int err2;
err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos));
if (err2)
ntfs_error(sb,
"Failed to free extent MFT record. Leaving inconsistent metadata.\n");
ntfs_inode_close(*(ni->ext.extent_ntfs_inos));
}
if (ntfs_mft_record_free(ni->vol, ni))
ntfs_error(sb,
"Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n");
unmap_mft_record(ni);
kfree(fn);
kfree(si);
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
remove_inode_hash(vi);
discard_new_inode(vi);
return ERR_PTR(err);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl)
#else
static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl)
#endif
{
struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
struct ntfs_inode *ni;
__le16 *uname;
int uname_len, err;
if (NVolShutdown(vol))
return -EIO;
uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
&uname, NTFS_MAX_NAME_LEN);
if (uname_len < 0) {
if (uname_len != -ENAMETOOLONG)
ntfs_error(vol->sb, "Failed to convert name to unicode.");
return uname_len;
}
err = ntfs_check_bad_windows_name(vol, uname, uname_len);
if (err) {
kmem_cache_free(ntfs_name_cache, uname);
return err;
}
if (!(vol->vol_flags & VOLUME_IS_DIRTY))
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0);
#else
ni = __ntfs_create(mnt_userns, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0);
#endif
kmem_cache_free(ntfs_name_cache, uname);
if (IS_ERR(ni))
return PTR_ERR(ni);
d_instantiate_new(dentry, VFS_I(ni));
return 0;
}
static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn)
{
int link_count;
int ret;
struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino;
struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec;
ret = ntfs_check_empty_dir(ni, ni_mrec);
if (!ret || ret != -ENOTEMPTY)
return ret;
link_count = le16_to_cpu(ni_mrec->link_count);
/*
* Directory is non-empty, so we can unlink only if there is more than
* one "real" hard link, i.e. links aren't different DOS and WIN32 names
*/
if ((link_count == 1) ||
(link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) {
ret = -ENOTEMPTY;
ntfs_debug("Non-empty directory without hard links\n");
goto no_hardlink;
}
ret = 0;
no_hardlink:
return ret;
}
static int ntfs_test_inode_attr(struct inode *vi, void *data)
{
struct ntfs_inode *ni = NTFS_I(vi);
unsigned long mft_no = (unsigned long)data;
if (ni->mft_no != mft_no)
return 0;
if (NInoAttr(ni) || ni->nr_extents == -1)
return 1;
else
return 0;
}
/*
* ntfs_delete - delete file or directory from ntfs volume
* @ni: ntfs inode for object to delte
* @dir_ni: ntfs inode for directory in which delete object
* @name: unicode name of the object to delete
* @name_len: length of the name in unicode characters
* @need_lock: whether mrec lock is needed or not
*
* Delete the specified name from the directory index @dir_ni and decrement
* the link count of the target inode @ni.
*
* Return 0 on success and -errno on error.
*/
static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
__le16 *name, u8 name_len, bool need_lock)
{
struct ntfs_attr_search_ctx *actx = NULL;
struct file_name_attr *fn = NULL;
bool looking_for_dos_name = false, looking_for_win32_name = false;
bool case_sensitive_match = true;
int err = 0;
struct mft_record *ni_mrec;
struct super_block *sb;
bool link_count_zero = false;
ntfs_debug("Entering.\n");
if (need_lock == true) {
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
}
sb = dir_ni->vol->sb;
if (ni->nr_extents == -1)
ni = ni->ext.base_ntfs_ino;
if (dir_ni->nr_extents == -1)
dir_ni = dir_ni->ext.base_ntfs_ino;
/*
* Search for FILE_NAME attribute with such name. If it's in POSIX or
* WIN32_AND_DOS namespace, then simply remove it from index and inode.
* If filename in DOS or in WIN32 namespace, then remove DOS name first,
* only then remove WIN32 name.
*/
actx = ntfs_attr_get_search_ctx(ni, NULL);
if (!actx) {
ntfs_error(sb, "%s, Failed to get search context", __func__);
if (need_lock) {
mutex_unlock(&dir_ni->mrec_lock);
mutex_unlock(&ni->mrec_lock);
}
return -ENOMEM;
}
search:
while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE,
0, NULL, 0, actx)) == 0) {
#ifdef DEBUG
unsigned char *s;
#endif
bool case_sensitive = IGNORE_CASE;
fn = (struct file_name_attr *)((u8 *)actx->attr +
le16_to_cpu(actx->attr->data.resident.value_offset));
#ifdef DEBUG
s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length);
ntfs_debug("name: '%s' type: %d dos: %d win32: %d case: %d\n",
s, fn->file_name_type,
looking_for_dos_name, looking_for_win32_name,
case_sensitive_match);
ntfs_attr_name_free(&s);
#endif
if (looking_for_dos_name) {
if (fn->file_name_type == FILE_NAME_DOS)
break;
continue;
}
if (looking_for_win32_name) {
if (fn->file_name_type == FILE_NAME_WIN32)
break;
continue;
}
/* Ignore hard links from other directories */
if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) {
ntfs_debug("MFT record numbers don't match (%lu != %lu)\n",
dir_ni->mft_no,
MREF_LE(fn->parent_directory));
continue;
}
if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match)
case_sensitive = CASE_SENSITIVE;
if (ntfs_names_are_equal(fn->file_name, fn->file_name_length,
name, name_len, case_sensitive,
ni->vol->upcase, ni->vol->upcase_len)) {
if (fn->file_name_type == FILE_NAME_WIN32) {
looking_for_dos_name = true;
ntfs_attr_reinit_search_ctx(actx);
continue;
}
if (fn->file_name_type == FILE_NAME_DOS)
looking_for_dos_name = true;
break;
}
}
if (err) {
/*
* If case sensitive search failed, then try once again
* ignoring case.
*/
if (err == -ENOENT && case_sensitive_match) {
case_sensitive_match = false;
ntfs_attr_reinit_search_ctx(actx);
goto search;
}
goto err_out;
}
err = ntfs_check_unlinkable_dir(actx, fn);
if (err)
goto err_out;
err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length));
if (err)
goto err_out;
err = ntfs_attr_record_rm(actx);
if (err)
goto err_out;
ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec;
ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1);
drop_nlink(VFS_I(ni));