-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathea.c
More file actions
968 lines (824 loc) · 22.6 KB
/
ea.c
File metadata and controls
968 lines (824 loc) · 22.6 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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Pocessing of EA's
*
* Part of this file is based on code from the NTFS-3G.
*
* Copyright (c) 2014-2021 Jean-Pierre Andre
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include <linux/fs.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
#include <linux/xattr.h>
#include "layout.h"
#include "attrib.h"
#include "index.h"
#include "dir.h"
#include "ea.h"
static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off,
s64 ea_size, bool need_truncate)
{
struct inode *ea_vi;
int err = 0;
s64 written;
ea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0);
if (IS_ERR(ea_vi))
return PTR_ERR(ea_vi);
written = ntfs_inode_attr_pwrite(ea_vi, ea_off, ea_size, value, false);
if (written != ea_size)
err = -EIO;
else {
struct ntfs_inode *ea_ni = NTFS_I(ea_vi);
if (need_truncate && ea_ni->data_size > ea_off + ea_size)
ntfs_attr_truncate(ea_ni, ea_off + ea_size);
mark_mft_record_dirty(ni);
}
iput(ea_vi);
return err;
}
static int ntfs_ea_lookup(char *ea_buf, s64 ea_buf_size, const char *name,
int name_len, s64 *ea_offset, s64 *ea_size)
{
const struct ea_attr *p_ea;
s64 offset;
unsigned int next;
if (ea_buf_size < sizeof(struct ea_attr))
goto out;
offset = 0;
do {
p_ea = (const struct ea_attr *)&ea_buf[offset];
next = le32_to_cpu(p_ea->next_entry_offset);
if (offset + next > ea_buf_size ||
((1 + p_ea->ea_name_length) > (ea_buf_size - offset)))
break;
if (p_ea->ea_name_length == name_len &&
!memcmp(p_ea->ea_name, name, name_len)) {
*ea_offset = offset;
if (next)
*ea_size = next;
else {
unsigned int ea_len = 1 + p_ea->ea_name_length +
le16_to_cpu(p_ea->ea_value_length);
if ((ea_buf_size - offset) < ea_len)
goto out;
*ea_size = ALIGN(struct_size(p_ea, ea_name,
1 + p_ea->ea_name_length +
le16_to_cpu(p_ea->ea_value_length)), 4);
}
if (ea_buf_size < *ea_offset + *ea_size)
goto out;
return 0;
}
offset += next;
} while (next > 0 && offset < ea_buf_size &&
sizeof(struct ea_attr) < (ea_buf_size - offset));
out:
return -ENOENT;
}
/*
* Return the existing EA
*
* The EA_INFORMATION is not examined and the consistency of the
* existing EA is not checked.
*
* If successful, the full attribute is returned unchanged
* and its size is returned.
* If the designated buffer is too small, the needed size is
* returned, and the buffer is left unchanged.
* If there is an error, a negative value is returned and errno
* is set according to the error.
*/
static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
void *buffer, size_t size)
{
struct ntfs_inode *ni = NTFS_I(inode);
const struct ea_attr *p_ea;
char *ea_buf;
s64 ea_off, ea_size, all_ea_size, ea_info_size;
int err;
u32 ea_info_qlen;
u16 ea_value_len;
struct ea_information *p_ea_info;
if (!NInoHasEA(ni))
return -ENODATA;
p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
&ea_info_size);
if (!p_ea_info || ea_info_size != sizeof(struct ea_information)) {
kvfree(p_ea_info);
return -ENODATA;
}
ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length);
kvfree(p_ea_info);
ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);
if (!ea_buf)
return -ENODATA;
err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off,
&ea_size);
if (!err) {
p_ea = (struct ea_attr *)&ea_buf[ea_off];
ea_value_len = le16_to_cpu(p_ea->ea_value_length);
if (!buffer) {
kvfree(ea_buf);
return ea_value_len;
}
if (ea_value_len > size) {
err = -ERANGE;
goto free_ea_buf;
}
memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1],
ea_value_len);
kvfree(ea_buf);
return ea_value_len;
}
err = -ENODATA;
free_ea_buf:
kvfree(ea_buf);
return err;
}
static inline int ea_packed_size(const struct ea_attr *p_ea)
{
/*
* 4 bytes for header (flags and lengths) + name length + 1 +
* value length.
*/
return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length);
}
/*
* Set a new EA, and set EA_INFORMATION accordingly
*
* This is roughly the same as ZwSetEaFile() on Windows, however
* the "offset to next" of the last EA should not be cleared.
*
* Consistency of the new EA is first checked.
*
* EA_INFORMATION is set first, and it is restored to its former
* state if setting EA fails.
*/
static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
const void *value, size_t val_size, int flags,
__le16 *packed_ea_size)
{
struct ntfs_inode *ni = NTFS_I(inode);
struct ea_information *p_ea_info = NULL;
int ea_packed, err = 0;
struct ea_attr *p_ea;
u32 ea_info_qsize = 0;
char *ea_buf = NULL;
size_t new_ea_size = ALIGN(struct_size(p_ea, ea_name, 1 + name_len + val_size), 4);
s64 ea_off, ea_info_size, all_ea_size, ea_size;
if (name_len > 255)
return -ENAMETOOLONG;
if (ntfs_attr_exist(ni, AT_EA_INFORMATION, AT_UNNAMED, 0)) {
p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
&ea_info_size);
if (!p_ea_info || ea_info_size != sizeof(struct ea_information))
goto out;
ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);
if (!ea_buf) {
ea_info_qsize = 0;
kvfree(p_ea_info);
goto create_ea_info;
}
ea_info_qsize = le32_to_cpu(p_ea_info->ea_query_length);
} else {
create_ea_info:
p_ea_info = kzalloc(sizeof(struct ea_information), GFP_NOFS);
if (!p_ea_info)
return -ENOMEM;
ea_info_qsize = 0;
err = ntfs_attr_add(ni, AT_EA_INFORMATION, AT_UNNAMED, 0,
(char *)p_ea_info, sizeof(struct ea_information));
if (err)
goto out;
if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
if (err)
goto out;
}
goto alloc_new_ea;
}
if (ea_info_qsize > all_ea_size) {
err = -EIO;
goto out;
}
err = ntfs_ea_lookup(ea_buf, ea_info_qsize, name, name_len, &ea_off,
&ea_size);
if (ea_info_qsize && !err) {
if (flags & XATTR_CREATE) {
err = -EEXIST;
goto out;
}
p_ea = (struct ea_attr *)(ea_buf + ea_off);
if (val_size &&
le16_to_cpu(p_ea->ea_value_length) == val_size &&
!memcmp(p_ea->ea_name + p_ea->ea_name_length + 1, value,
val_size))
goto out;
le16_add_cpu(&p_ea_info->ea_length, 0 - ea_packed_size(p_ea));
if (p_ea->flags & NEED_EA)
le16_add_cpu(&p_ea_info->need_ea_count, -1);
memmove((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size));
ea_info_qsize -= ea_size;
p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize);
err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0,
sizeof(struct ea_information), false);
if (err)
goto out;
err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, ea_info_qsize, true);
if (err)
goto out;
if ((flags & XATTR_REPLACE) && !val_size) {
/* Remove xattr. */
goto out;
}
} else {
if (flags & XATTR_REPLACE) {
err = -ENODATA;
goto out;
}
}
kvfree(ea_buf);
alloc_new_ea:
ea_buf = kzalloc(new_ea_size, GFP_NOFS);
if (!ea_buf) {
err = -ENOMEM;
goto out;
}
/*
* EA and REPARSE_POINT compatibility not checked any more,
* required by Windows 10, but having both may lead to
* problems with earlier versions.
*/
p_ea = (struct ea_attr *)ea_buf;
memcpy(p_ea->ea_name, name, name_len);
p_ea->ea_name_length = name_len;
p_ea->ea_name[name_len] = 0;
memcpy(p_ea->ea_name + name_len + 1, value, val_size);
p_ea->ea_value_length = cpu_to_le16(val_size);
p_ea->next_entry_offset = cpu_to_le32(new_ea_size);
ea_packed = le16_to_cpu(p_ea_info->ea_length) + ea_packed_size(p_ea);
p_ea_info->ea_length = cpu_to_le16(ea_packed);
p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize + new_ea_size);
if (ea_packed > 0xffff ||
ntfs_attr_size_bounds_check(ni->vol, AT_EA, new_ea_size)) {
err = -EFBIG;
goto out;
}
/*
* no EA or EA_INFORMATION : add them
*/
if (!ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
err = ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, (char *)p_ea,
new_ea_size);
if (err)
goto out;
} else {
err = ntfs_write_ea(ni, AT_EA, (char *)p_ea, ea_info_qsize,
new_ea_size, false);
if (err)
goto out;
}
err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0,
sizeof(struct ea_information), false);
if (err)
goto out;
if (packed_ea_size)
*packed_ea_size = p_ea_info->ea_length;
mark_mft_record_dirty(ni);
out:
if (ea_info_qsize > 0)
NInoSetHasEA(ni);
else
NInoClearHasEA(ni);
kvfree(ea_buf);
kvfree(p_ea_info);
return err;
}
/*
* Check for the presence of an EA "$LXDEV" (used by WSL)
* and return its value as a device address
*/
int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags)
{
int err;
__le32 v;
if (!(flags & NTFS_VOL_UID)) {
/* Load uid to lxuid EA */
err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,
sizeof(v));
if (err < 0)
return err;
i_uid_write(inode, le32_to_cpu(v));
}
if (!(flags & NTFS_VOL_UID)) {
/* Load gid to lxgid EA */
err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,
sizeof(v));
if (err < 0)
return err;
i_gid_write(inode, le32_to_cpu(v));
}
/* Load mode to lxmod EA */
err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v));
if (err > 0) {
inode->i_mode = le32_to_cpu(v);
} else {
/* Everyone gets all permissions. */
inode->i_mode |= 0777;
}
/* Load mode to lxdev EA */
err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v));
if (err > 0)
*rdevp = le32_to_cpu(v);
err = 0;
return err;
}
int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size,
unsigned int flags)
{
__le32 v;
int err;
if (flags & NTFS_EA_UID) {
/* Store uid to lxuid EA */
v = cpu_to_le32(i_uid_read(inode));
err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,
sizeof(v), 0, ea_size);
if (err)
return err;
}
if (flags & NTFS_EA_GID) {
/* Store gid to lxgid EA */
v = cpu_to_le32(i_gid_read(inode));
err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,
sizeof(v), 0, ea_size);
if (err)
return err;
}
if (flags & NTFS_EA_MODE) {
/* Store mode to lxmod EA */
v = cpu_to_le32(inode->i_mode);
err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v,
sizeof(v), 0, ea_size);
if (err)
return err;
}
if (rdev) {
v = cpu_to_le32(rdev);
err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v),
0, ea_size);
}
return err;
}
ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
struct inode *inode = d_inode(dentry);
struct ntfs_inode *ni = NTFS_I(inode);
const struct ea_attr *p_ea;
s64 offset, ea_buf_size, ea_info_size;
int next, err = 0, ea_size;
u32 ea_info_qsize;
char *ea_buf = NULL;
ssize_t ret = 0;
struct ea_information *ea_info;
if (!NInoHasEA(ni))
return 0;
mutex_lock(&NTFS_I(inode)->mrec_lock);
ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
&ea_info_size);
if (!ea_info || ea_info_size != sizeof(struct ea_information))
goto out;
ea_info_qsize = le32_to_cpu(ea_info->ea_query_length);
ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &ea_buf_size);
if (!ea_buf)
goto out;
if (ea_info_qsize > ea_buf_size)
goto out;
if (ea_buf_size < sizeof(struct ea_attr))
goto out;
offset = 0;
do {
p_ea = (const struct ea_attr *)&ea_buf[offset];
next = le32_to_cpu(p_ea->next_entry_offset);
if (next)
ea_size = next;
else
ea_size = ALIGN(struct_size(p_ea, ea_name,
1 + p_ea->ea_name_length +
le16_to_cpu(p_ea->ea_value_length)),
4);
if (buffer) {
if (offset + ea_size > ea_info_qsize)
break;
if (ret + p_ea->ea_name_length + 1 > size) {
err = -ERANGE;
goto out;
}
if (p_ea->ea_name_length + 1 > (ea_info_qsize - offset))
break;
memcpy(buffer + ret, p_ea->ea_name, p_ea->ea_name_length);
buffer[ret + p_ea->ea_name_length] = 0;
}
ret += p_ea->ea_name_length + 1;
offset += ea_size;
} while (next > 0 && offset < ea_info_qsize &&
sizeof(struct ea_attr) < (ea_info_qsize - offset));
out:
mutex_unlock(&NTFS_I(inode)->mrec_lock);
kvfree(ea_info);
kvfree(ea_buf);
return err ? err : ret;
}
// clang-format off
#define SYSTEM_DOS_ATTRIB "system.dos_attrib"
#define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib"
#define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be"
// clang-format on
static int ntfs_getxattr(const struct xattr_handler *handler,
struct dentry *unused, struct inode *inode, const char *name,
void *buffer, size_t size)
{
struct ntfs_inode *ni = NTFS_I(inode);
int err;
if (NVolShutdown(ni->vol))
return -EIO;
if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
if (!buffer) {
err = sizeof(u8);
} else if (size < sizeof(u8)) {
err = -ENODATA;
} else {
err = sizeof(u8);
*(u8 *)buffer = (u8)(le32_to_cpu(ni->flags) & 0x3F);
}
goto out;
}
if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
if (!buffer) {
err = sizeof(u32);
} else if (size < sizeof(u32)) {
err = -ENODATA;
} else {
err = sizeof(u32);
*(u32 *)buffer = le32_to_cpu(ni->flags);
if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
*(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer);
}
goto out;
}
mutex_lock(&ni->mrec_lock);
err = ntfs_get_ea(inode, name, strlen(name), buffer, size);
mutex_unlock(&ni->mrec_lock);
out:
return err;
}
static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
{
struct ntfs_attr_search_ctx *ctx;
struct mft_record *m;
struct attr_record *a;
__le16 new_aflags;
int mp_size, mp_ofs, name_ofs, arec_size, err;
m = map_mft_record(ni);
if (IS_ERR(m))
return PTR_ERR(m);
ctx = ntfs_attr_get_search_ctx(ni, m);
if (!ctx) {
err = -ENOMEM;
goto err_out;
}
err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
CASE_SENSITIVE, 0, NULL, 0, ctx);
if (err) {
err = -EINVAL;
goto err_out;
}
a = ctx->attr;
new_aflags = ctx->attr->flags;
if (fattr & FILE_ATTR_SPARSE_FILE)
new_aflags |= ATTR_IS_SPARSE;
else
new_aflags &= ~ATTR_IS_SPARSE;
if (fattr & FILE_ATTR_COMPRESSED)
new_aflags |= ATTR_IS_COMPRESSED;
else
new_aflags &= ~ATTR_IS_COMPRESSED;
if (new_aflags == a->flags)
return 0;
if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) ==
(ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
pr_err("file can't be sparsed and compressed\n");
err = -EOPNOTSUPP;
goto err_out;
}
if (!a->non_resident)
goto out;
if (a->data.non_resident.data_size) {
pr_err("Can't change sparsed/compressed for non-empty file");
err = -EOPNOTSUPP;
goto err_out;
}
if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))
name_ofs = (offsetof(struct attr_record,
data.non_resident.compressed_size) +
sizeof(a->data.non_resident.compressed_size) + 7) & ~7;
else
name_ofs = (offsetof(struct attr_record,
data.non_resident.compressed_size) + 7) & ~7;
mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1);
if (unlikely(mp_size < 0)) {
err = mp_size;
ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err);
goto err_out;
}
mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
arec_size = (mp_ofs + mp_size + 7) & ~7;
err = ntfs_attr_record_resize(m, a, arec_size);
if (unlikely(err))
goto err_out;
if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
a->data.non_resident.compression_unit = 0;
if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3)
a->data.non_resident.compression_unit = 4;
a->data.non_resident.compressed_size = 0;
ni->itype.compressed.size = 0;
if (a->data.non_resident.compression_unit) {
ni->itype.compressed.block_size = 1U <<
(a->data.non_resident.compression_unit +
ni->vol->cluster_size_bits);
ni->itype.compressed.block_size_bits =
ffs(ni->itype.compressed.block_size) -
1;
ni->itype.compressed.block_clusters = 1U <<
a->data.non_resident.compression_unit;
} else {
ni->itype.compressed.block_size = 0;
ni->itype.compressed.block_size_bits = 0;
ni->itype.compressed.block_clusters = 0;
}
if (new_aflags & ATTR_IS_SPARSE) {
NInoSetSparse(ni);
ni->flags |= FILE_ATTR_SPARSE_FILE;
}
if (new_aflags & ATTR_IS_COMPRESSED) {
NInoSetCompressed(ni);
ni->flags |= FILE_ATTR_COMPRESSED;
}
} else {
ni->flags &= ~(FILE_ATTR_SPARSE_FILE | FILE_ATTR_COMPRESSED);
a->data.non_resident.compression_unit = 0;
NInoClearSparse(ni);
NInoClearCompressed(ni);
}
a->name_offset = cpu_to_le16(name_ofs);
a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
out:
a->flags = new_aflags;
mark_mft_record_dirty(ctx->ntfs_ino);
err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(ni);
return err;
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(6, 3, 0)
static int ntfs_setxattr(const struct xattr_handler *handler,
struct mnt_idmap *idmap, struct dentry *unused,
struct inode *inode, const char *name, const void *value,
size_t size, int flags)
#else
static int ntfs_setxattr(const struct xattr_handler *handler,
struct user_namespace *mnt_userns, struct dentry *unused,
struct inode *inode, const char *name, const void *value,
size_t size, int flags)
#endif
{
struct ntfs_inode *ni = NTFS_I(inode);
int err;
__le32 fattr;
if (NVolShutdown(ni->vol))
return -EIO;
if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
if (sizeof(u8) != size) {
err = -EINVAL;
goto out;
}
fattr = cpu_to_le32(*(u8 *)value);
goto set_fattr;
}
if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
if (size != sizeof(u32)) {
err = -EINVAL;
goto out;
}
if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value));
else
fattr = cpu_to_le32(*(u32 *)value);
if (S_ISREG(inode->i_mode)) {
mutex_lock(&ni->mrec_lock);
err = ntfs_new_attr_flags(ni, fattr);
mutex_unlock(&ni->mrec_lock);
if (err)
goto out;
}
set_fattr:
if (S_ISDIR(inode->i_mode))
fattr |= FILE_ATTR_DIRECTORY;
else
fattr &= ~FILE_ATTR_DIRECTORY;
if (ni->flags != fattr) {
ni->flags = fattr;
if (fattr & FILE_ATTR_READONLY)
inode->i_mode &= ~0222;
else
inode->i_mode |= 0222;
NInoSetFileNameDirty(ni);
mark_inode_dirty(inode);
}
err = 0;
goto out;
}
mutex_lock(&ni->mrec_lock);
err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL);
mutex_unlock(&ni->mrec_lock);
out:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
inode_set_ctime_current(inode);
#else
inode->i_ctime = current_time(inode);
#endif
mark_inode_dirty(inode);
return err;
}
static bool ntfs_xattr_user_list(struct dentry *dentry)
{
return true;
}
// clang-format off
static const struct xattr_handler ntfs_other_xattr_handler = {
.prefix = "",
.get = ntfs_getxattr,
.set = ntfs_setxattr,
.list = ntfs_xattr_user_list,
};
const struct xattr_handler * const ntfs_xattr_handlers[] = {
&ntfs_other_xattr_handler,
NULL,
};
// clang-format on
#ifdef CONFIG_NTFS_FS_POSIX_ACL
struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
int type)
{
struct inode *inode = d_inode(dentry);
struct ntfs_inode *ni = NTFS_I(inode);
const char *name;
size_t name_len;
struct posix_acl *acl;
int err;
void *buf;
buf = kmalloc(PATH_MAX, GFP_KERNEL);
if (!buf)
return ERR_PTR(-ENOMEM);
/* Possible values of 'type' was already checked above. */
if (type == ACL_TYPE_ACCESS) {
name = XATTR_NAME_POSIX_ACL_ACCESS;
name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
} else {
name = XATTR_NAME_POSIX_ACL_DEFAULT;
name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
}
mutex_lock(&ni->mrec_lock);
err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX);
mutex_unlock(&ni->mrec_lock);
/* Translate extended attribute to acl. */
if (err >= 0)
acl = posix_acl_from_xattr(&init_user_ns, buf, err);
else if (err == -ENODATA)
acl = NULL;
else
acl = ERR_PTR(err);
if (!IS_ERR(acl))
set_cached_acl(inode, type, acl);
kfree(buf);
return acl;
}
static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
struct inode *inode, struct posix_acl *acl,
int type, bool init_acl)
{
const char *name;
size_t size, name_len;
void *value;
int err;
int flags;
umode_t mode;
if (S_ISLNK(inode->i_mode))
return -EOPNOTSUPP;
mode = inode->i_mode;
switch (type) {
case ACL_TYPE_ACCESS:
/* Do not change i_mode if we are in init_acl */
if (acl && !init_acl) {
err = posix_acl_update_mode(idmap, inode, &mode, &acl);
if (err)
return err;
}
name = XATTR_NAME_POSIX_ACL_ACCESS;
name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
break;
case ACL_TYPE_DEFAULT:
if (!S_ISDIR(inode->i_mode))
return acl ? -EACCES : 0;
name = XATTR_NAME_POSIX_ACL_DEFAULT;
name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
break;
default:
return -EINVAL;
}
if (!acl) {
/* Remove xattr if it can be presented via mode. */
size = 0;
value = NULL;
flags = XATTR_REPLACE;
} else {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(7, 0, 0)
value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
if (!value)
return -ENOMEM;
#else
size = posix_acl_xattr_size(acl->a_count);
value = kmalloc(size, GFP_NOFS);
if (!value)
return -ENOMEM;
err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
if (err < 0)
goto out;
#endif
flags = 0;
}
mutex_lock(&NTFS_I(inode)->mrec_lock);
err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL);
mutex_unlock(&NTFS_I(inode)->mrec_lock);
if (err == -ENODATA && !size)
err = 0; /* Removing non existed xattr. */
if (!err) {
__le16 ea_size = 0;
umode_t old_mode = inode->i_mode;
inode->i_mode = mode;
mutex_lock(&NTFS_I(inode)->mrec_lock);
err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE);
if (err) {
ntfs_set_ea(inode, name, name_len, NULL, 0,
XATTR_REPLACE, NULL);
mutex_unlock(&NTFS_I(inode)->mrec_lock);
inode->i_mode = old_mode;
goto out;
}
mutex_unlock(&NTFS_I(inode)->mrec_lock);
set_cached_acl(inode, type, acl);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
inode_set_ctime_current(inode);
#else
inode->i_ctime = current_time(inode);
#endif
mark_inode_dirty(inode);
}
out:
kfree(value);
return err;
}
int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
struct posix_acl *acl, int type)
{
return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false);
}
int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode,
struct inode *dir)
{
struct posix_acl *default_acl, *acl;
int err;
err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
if (err)
return err;
if (default_acl) {
err = ntfs_set_acl_ex(idmap, inode, default_acl,
ACL_TYPE_DEFAULT, true);
posix_acl_release(default_acl);
} else {
inode->i_default_acl = NULL;
}
if (acl) {
if (!err)
err = ntfs_set_acl_ex(idmap, inode, acl,
ACL_TYPE_ACCESS, true);
posix_acl_release(acl);
} else {
inode->i_acl = NULL;
}
return err;
}
#endif