Newer
Older
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
From 2f38cb2c0640a7d53062b57aab3bf8620fc49237 Mon Sep 17 00:00:00 2001
From: Umer Saleem <usaleem@ixsystems.com>
Date: Fri, 18 Feb 2022 21:21:20 +0500
Subject: [PATCH] Expose additional file level attributes
ZFS allows to update and retrieve additional file level attributes for
FreeBSD. This commit allows additional file level attributes to be
updated and retrieved for Linux. These include the flags stored in the
upper half of z_pflags only.
Two new IOCTLs have been added for this purpose. ZFS_IOC_GETDOSFLAGS
can be used to retieve the attributes, while ZFS_IOC_SETDOSFLAGS can
be used to update the attributes.
Attributes that are allowed to be updated include ZFS_IMMUTABLE,
ZFS_APPENDONLY, ZFS_NOUNLINK, ZFS_ARCHIVE, ZFS_NODUMP, ZFS_SYSTEM,
ZFS_HIDDEN, ZFS_READONLY, ZFS_REPARSE, ZFS_OFFLINE and ZFS_SPARSE.
Flags can be or'd together while calling ZFS_IOC_SETDOSFLAGS.
Signed-off-by: Umer Saleem <usaleem@ixsystems.com>
---
configure.ac | 3 +
include/sys/fs/zfs.h | 35 +++
include/sys/zfs_znode.h | 2 +-
module/os/linux/zfs/zpl_file.c | 125 +++++++++--
tests/runfiles/common.run | 2 +-
tests/runfiles/freebsd.run | 4 -
tests/runfiles/linux.run | 4 +
tests/zfs-tests/cmd/Makefile.am | 2 +
.../cmd/read_dos_attributes/.gitignore | 1 +
.../cmd/read_dos_attributes/Makefile.am | 6 +
.../read_dos_attributes/read_dos_attributes.c | 167 +++++++++++++++
.../cmd/write_dos_attributes/.gitignore | 1 +
.../cmd/write_dos_attributes/Makefile.am | 6 +
.../write_dos_attributes.c | 201 ++++++++++++++++++
tests/zfs-tests/include/commands.cfg | 2 +
tests/zfs-tests/tests/functional/Makefile.am | 1 +
.../tests/functional/acl/off/Makefile.am | 2 -
.../tests/functional/acl/off/dosmode.ksh | 97 +++++----
.../acl/off/dosmode_readonly_write.c | 11 +
.../functional/dos_attributes/Makefile.am | 8 +
.../functional/dos_attributes/cleanup.ksh | 34 +++
.../dos_attributes/read_dos_attrs_001.ksh | 60 ++++++
.../tests/functional/dos_attributes/setup.ksh | 35 +++
.../dos_attributes/write_dos_attrs_001.ksh | 61 ++++++
24 files changed, 806 insertions(+), 64 deletions(-)
create mode 100644 tests/zfs-tests/cmd/read_dos_attributes/.gitignore
create mode 100644 tests/zfs-tests/cmd/read_dos_attributes/Makefile.am
create mode 100644 tests/zfs-tests/cmd/read_dos_attributes/read_dos_attributes.c
create mode 100644 tests/zfs-tests/cmd/write_dos_attributes/.gitignore
create mode 100644 tests/zfs-tests/cmd/write_dos_attributes/Makefile.am
create mode 100644 tests/zfs-tests/cmd/write_dos_attributes/write_dos_attributes.c
create mode 100644 tests/zfs-tests/tests/functional/dos_attributes/Makefile.am
create mode 100755 tests/zfs-tests/tests/functional/dos_attributes/cleanup.ksh
create mode 100755 tests/zfs-tests/tests/functional/dos_attributes/read_dos_attrs_001.ksh
create mode 100755 tests/zfs-tests/tests/functional/dos_attributes/setup.ksh
create mode 100755 tests/zfs-tests/tests/functional/dos_attributes/write_dos_attrs_001.ksh
diff --git a/configure.ac b/configure.ac
index 7037c06b225..990958bafa1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -226,12 +226,14 @@ AC_CONFIG_FILES([
tests/zfs-tests/cmd/randfree_file/Makefile
tests/zfs-tests/cmd/randwritecomp/Makefile
tests/zfs-tests/cmd/readmmap/Makefile
+ tests/zfs-tests/cmd/read_dos_attributes/Makefile
tests/zfs-tests/cmd/rename_dir/Makefile
tests/zfs-tests/cmd/rm_lnkcnt_zero_file/Makefile
tests/zfs-tests/cmd/send_doall/Makefile
tests/zfs-tests/cmd/stride_dd/Makefile
tests/zfs-tests/cmd/threadsappend/Makefile
tests/zfs-tests/cmd/user_ns_exec/Makefile
+ tests/zfs-tests/cmd/write_dos_attributes/Makefile
tests/zfs-tests/cmd/xattrtest/Makefile
tests/zfs-tests/include/Makefile
tests/zfs-tests/tests/Makefile
@@ -333,6 +335,7 @@ AC_CONFIG_FILES([
tests/zfs-tests/tests/functional/deadman/Makefile
tests/zfs-tests/tests/functional/delegate/Makefile
tests/zfs-tests/tests/functional/devices/Makefile
+ tests/zfs-tests/tests/functional/dos_attributes/Makefile
tests/zfs-tests/tests/functional/events/Makefile
tests/zfs-tests/tests/functional/exec/Makefile
tests/zfs-tests/tests/functional/fallocate/Makefile
diff --git a/include/sys/fs/zfs.h b/include/sys/fs/zfs.h
index 6e85c4b7b39..ab29b4e2ef3 100644
--- a/include/sys/fs/zfs.h
+++ b/include/sys/fs/zfs.h
@@ -1459,6 +1459,41 @@ typedef enum zfs_ioc {
*/
#define BLKZNAME _IOR(0x12, 125, char[ZFS_MAX_DATASET_NAME_LEN])
+#ifdef __linux__
+
+/*
+ * IOCTLs to update and retrieve additional file level attributes on
+ * Linux.
+ */
+#define ZFS_IOC_GETDOSFLAGS _IOR(0x83, 1, uint64_t)
+#define ZFS_IOC_SETDOSFLAGS _IOW(0x83, 2, uint64_t)
+
+/*
+ * Additional file level attributes, that are stored
+ * in the upper half of z_pflags
+ */
+#define ZFS_READONLY 0x0000000100000000ull
+#define ZFS_HIDDEN 0x0000000200000000ull
+#define ZFS_SYSTEM 0x0000000400000000ull
+#define ZFS_ARCHIVE 0x0000000800000000ull
+#define ZFS_IMMUTABLE 0x0000001000000000ull
+#define ZFS_NOUNLINK 0x0000002000000000ull
+#define ZFS_APPENDONLY 0x0000004000000000ull
+#define ZFS_NODUMP 0x0000008000000000ull
+#define ZFS_OPAQUE 0x0000010000000000ull
+#define ZFS_AV_QUARANTINED 0x0000020000000000ull
+#define ZFS_AV_MODIFIED 0x0000040000000000ull
+#define ZFS_REPARSE 0x0000080000000000ull
+#define ZFS_OFFLINE 0x0000100000000000ull
+#define ZFS_SPARSE 0x0000200000000000ull
+
+#define ZFS_DOS_FL_USER_VISIBLE (ZFS_IMMUTABLE | ZFS_APPENDONLY | \
+ ZFS_NOUNLINK | ZFS_ARCHIVE | ZFS_NODUMP | ZFS_SYSTEM | \
+ ZFS_HIDDEN | ZFS_READONLY | ZFS_REPARSE | ZFS_OFFLINE | \
+ ZFS_SPARSE)
+
+#endif
+
/*
* ZFS-specific error codes used for returning descriptive errors
* to the userland through zfs ioctls.
diff --git a/include/sys/zfs_znode.h b/include/sys/zfs_znode.h
index e20c18cc2b6..127fd8736ff 100644
--- a/include/sys/zfs_znode.h
+++ b/include/sys/zfs_znode.h
@@ -37,7 +37,7 @@ extern "C" {
/*
* Additional file level attributes, that are stored
- * in the upper half of zp_flags
+ * in the upper half of z_pflags
*/
#define ZFS_READONLY 0x0000000100000000ull
#define ZFS_HIDDEN 0x0000000200000000ull
diff --git a/module/os/linux/zfs/zpl_file.c b/module/os/linux/zfs/zpl_file.c
index 982b424197e..f78e50262af 100644
--- a/module/os/linux/zfs/zpl_file.c
+++ b/module/os/linux/zfs/zpl_file.c
@@ -905,21 +905,24 @@ __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
xva_init(xva);
xoap = xva_getxoptattr(xva);
- XVA_SET_REQ(xva, XAT_IMMUTABLE);
- if (ioctl_flags & FS_IMMUTABLE_FL)
- xoap->xoa_immutable = B_TRUE;
-
- XVA_SET_REQ(xva, XAT_APPENDONLY);
- if (ioctl_flags & FS_APPEND_FL)
- xoap->xoa_appendonly = B_TRUE;
-
- XVA_SET_REQ(xva, XAT_NODUMP);
- if (ioctl_flags & FS_NODUMP_FL)
- xoap->xoa_nodump = B_TRUE;
-
- XVA_SET_REQ(xva, XAT_PROJINHERIT);
- if (ioctl_flags & ZFS_PROJINHERIT_FL)
- xoap->xoa_projinherit = B_TRUE;
+#define FLAG_CHANGE(iflag, zflag, xflag, xfield) do { \
+ if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) || \
+ ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) { \
+ XVA_SET_REQ(xva, (xflag)); \
+ (xfield) = ((ioctl_flags & (iflag)) != 0); \
+ } \
+} while (0)
+
+ FLAG_CHANGE(FS_IMMUTABLE_FL, ZFS_IMMUTABLE, XAT_IMMUTABLE,
+ xoap->xoa_immutable);
+ FLAG_CHANGE(FS_APPEND_FL, ZFS_APPENDONLY, XAT_APPENDONLY,
+ xoap->xoa_appendonly);
+ FLAG_CHANGE(FS_NODUMP_FL, ZFS_NODUMP, XAT_NODUMP,
+ xoap->xoa_nodump);
+ FLAG_CHANGE(ZFS_PROJINHERIT_FL, ZFS_PROJINHERIT, XAT_PROJINHERIT,
+ xoap->xoa_projinherit);
+
+#undef FLAG_CHANGE
return (0);
}
@@ -998,6 +1001,94 @@ zpl_ioctl_setxattr(struct file *filp, void __user *arg)
return (err);
}
+/*
+ * Expose Additional File Level Attributes of ZFS.
+ */
+static int
+zpl_ioctl_getdosflags(struct file *filp, void __user *arg)
+{
+ struct inode *ip = file_inode(filp);
+ uint64_t dosflags = ITOZ(ip)->z_pflags;
+ dosflags &= ZFS_DOS_FL_USER_VISIBLE;
+ int err = copy_to_user(arg, &dosflags, sizeof (dosflags));
+
+ return (err);
+}
+
+static int
+__zpl_ioctl_setdosflags(struct inode *ip, uint64_t ioctl_flags, xvattr_t *xva)
+{
+ uint64_t zfs_flags = ITOZ(ip)->z_pflags;
+ xoptattr_t *xoap;
+
+ if (ioctl_flags & (~ZFS_DOS_FL_USER_VISIBLE))
+ return (-EOPNOTSUPP);
+
+ if ((fchange(ioctl_flags, zfs_flags, ZFS_IMMUTABLE, ZFS_IMMUTABLE) ||
+ fchange(ioctl_flags, zfs_flags, ZFS_APPENDONLY, ZFS_APPENDONLY)) &&
+ !capable(CAP_LINUX_IMMUTABLE))
+ return (-EPERM);
+
+ if (!zpl_inode_owner_or_capable(kcred->user_ns, ip))
+ return (-EACCES);
+
+ xva_init(xva);
+ xoap = xva_getxoptattr(xva);
+
+#define FLAG_CHANGE(iflag, xflag, xfield) do { \
+ if (((ioctl_flags & (iflag)) && !(zfs_flags & (iflag))) || \
+ ((zfs_flags & (iflag)) && !(ioctl_flags & (iflag)))) { \
+ XVA_SET_REQ(xva, (xflag)); \
+ (xfield) = ((ioctl_flags & (iflag)) != 0); \
+ } \
+} while (0)
+
+ FLAG_CHANGE(ZFS_IMMUTABLE, XAT_IMMUTABLE, xoap->xoa_immutable);
+ FLAG_CHANGE(ZFS_APPENDONLY, XAT_APPENDONLY, xoap->xoa_appendonly);
+ FLAG_CHANGE(ZFS_NODUMP, XAT_NODUMP, xoap->xoa_nodump);
+ FLAG_CHANGE(ZFS_READONLY, XAT_READONLY, xoap->xoa_readonly);
+ FLAG_CHANGE(ZFS_HIDDEN, XAT_HIDDEN, xoap->xoa_hidden);
+ FLAG_CHANGE(ZFS_SYSTEM, XAT_SYSTEM, xoap->xoa_system);
+ FLAG_CHANGE(ZFS_ARCHIVE, XAT_ARCHIVE, xoap->xoa_archive);
+ FLAG_CHANGE(ZFS_NOUNLINK, XAT_NOUNLINK, xoap->xoa_nounlink);
+ FLAG_CHANGE(ZFS_REPARSE, XAT_REPARSE, xoap->xoa_reparse);
+ FLAG_CHANGE(ZFS_OFFLINE, XAT_OFFLINE, xoap->xoa_offline);
+ FLAG_CHANGE(ZFS_SPARSE, XAT_SPARSE, xoap->xoa_sparse);
+
+#undef FLAG_CHANGE
+
+ return (0);
+}
+
+/*
+ * Set Additional File Level Attributes of ZFS.
+ */
+static int
+zpl_ioctl_setdosflags(struct file *filp, void __user *arg)
+{
+ struct inode *ip = file_inode(filp);
+ uint64_t dosflags;
+ cred_t *cr = CRED();
+ xvattr_t xva;
+ int err;
+ fstrans_cookie_t cookie;
+
+ if (copy_from_user(&dosflags, arg, sizeof (dosflags)))
+ return (-EFAULT);
+
+ err = __zpl_ioctl_setdosflags(ip, dosflags, &xva);
+ if (err)
+ return (err);
+
+ crhold(cr);
+ cookie = spl_fstrans_mark();
+ err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
+ spl_fstrans_unmark(cookie);
+ crfree(cr);
+
+ return (err);
+}
+
static long
zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
@@ -1012,6 +1103,10 @@ zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return (zpl_ioctl_getxattr(filp, (void *)arg));
case ZFS_IOC_FSSETXATTR:
return (zpl_ioctl_setxattr(filp, (void *)arg));
+ case ZFS_IOC_GETDOSFLAGS:
+ return (zpl_ioctl_getdosflags(filp, (void *)arg));
+ case ZFS_IOC_SETDOSFLAGS:
+ return (zpl_ioctl_setdosflags(filp, (void *)arg));
default:
return (-ENOTTY);
}
diff --git a/tests/runfiles/common.run b/tests/runfiles/common.run
index 9f11284ad62..eb0d451351f 100644
--- a/tests/runfiles/common.run
+++ b/tests/runfiles/common.run
@@ -29,7 +29,7 @@ outputdir = /var/tmp/test_results
tags = ['functional']
[tests/functional/acl/off]
-tests = ['posixmode']
+tests = ['dosmode', 'posixmode']
tags = ['functional', 'acl']
[tests/functional/alloc_class]
diff --git a/tests/runfiles/freebsd.run b/tests/runfiles/freebsd.run
index 153b204b493..c7ca1d769fc 100644
--- a/tests/runfiles/freebsd.run
+++ b/tests/runfiles/freebsd.run
@@ -22,10 +22,6 @@ failsafe = callbacks/zfs_failsafe
outputdir = /var/tmp/test_results
tags = ['functional']
-[tests/functional/acl/off:FreeBSD]
-tests = ['dosmode']
-tags = ['functional', 'acl']
-
[tests/functional/cli_root/zfs_jail:FreeBSD]
tests = ['zfs_jail_001_pos']
tags = ['functional', 'cli_root', 'zfs_jail']
diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run
index 7186d5d67ac..8412a7ea5a9 100644
--- a/tests/runfiles/linux.run
+++ b/tests/runfiles/linux.run
@@ -149,6 +149,10 @@ tests = ['projectid_001_pos', 'projectid_002_pos', 'projectid_003_pos',
'projecttree_001_pos', 'projecttree_002_pos', 'projecttree_003_neg']
tags = ['functional', 'projectquota']
+[tests/functional/dos_attributes:Linux]
+tests = ['read_dos_attrs_001', 'write_dos_attrs_001']
+tags = ['functional', 'dos_attributes']
+
[tests/functional/rsend:Linux]
tests = ['send_realloc_dnode_size', 'send_encrypted_files']
tags = ['functional', 'rsend']
diff --git a/tests/zfs-tests/cmd/Makefile.am b/tests/zfs-tests/cmd/Makefile.am
index 2470397a90a..88d8c8cf08b 100644
--- a/tests/zfs-tests/cmd/Makefile.am
+++ b/tests/zfs-tests/cmd/Makefile.am
@@ -34,6 +34,8 @@ if BUILD_LINUX
SUBDIRS += \
getversion \
randfree_file \
+ read_dos_attributes \
user_ns_exec \
+ write_dos_attributes \
xattrtest
endif
diff --git a/tests/zfs-tests/cmd/read_dos_attributes/.gitignore b/tests/zfs-tests/cmd/read_dos_attributes/.gitignore
new file mode 100644
index 00000000000..52584e4a738
--- /dev/null
+++ b/tests/zfs-tests/cmd/read_dos_attributes/.gitignore
@@ -0,0 +1 @@
+/read_dos_attributes
diff --git a/tests/zfs-tests/cmd/read_dos_attributes/Makefile.am b/tests/zfs-tests/cmd/read_dos_attributes/Makefile.am
new file mode 100644
index 00000000000..69412f05f65
--- /dev/null
+++ b/tests/zfs-tests/cmd/read_dos_attributes/Makefile.am
@@ -0,0 +1,6 @@
+include $(top_srcdir)/config/Rules.am
+
+pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/bin
+
+pkgexec_PROGRAMS = read_dos_attributes
+read_dos_attributes_SOURCES = read_dos_attributes.c
diff --git a/tests/zfs-tests/cmd/read_dos_attributes/read_dos_attributes.c b/tests/zfs-tests/cmd/read_dos_attributes/read_dos_attributes.c
new file mode 100644
index 00000000000..ed0906c36a6
--- /dev/null
+++ b/tests/zfs-tests/cmd/read_dos_attributes/read_dos_attributes.c
@@ -0,0 +1,167 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source. A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2022 iXsystems, Inc.
+ */
+
+/*
+ * FreeBSD allows to update and retreive additional file level attributes.
+ * For Linux, two IOCTLs have been added to update and retrieve additional
+ * level attributes.
+ *
+ * This application reads additional file level attributes on a given
+ * file and prints FreeBSD keywords that map to respective attributes.
+ *
+ * Usage: 'read_dos_attributes filepath'
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <err.h>
+#include <sys/fs/zfs.h>
+#include <string.h>
+
+#define SU_ARCH_SHORT "arch"
+#define SU_ARCH_FULL "archived"
+#define SU_NODUMP "nodump"
+#define SU_APPEND_SHORT "sappnd"
+#define SU_APPEND_FULL "sappend"
+#define SU_IMMUTABLE "schg"
+#define SU_IMMUTABLE_SHORT "schange"
+#define SU_IMMUTABLE_FULL "simmutable"
+#define SU_UNLINK_SHORT "sunlnk"
+#define SU_UNLINK_FULL "sunlink"
+#define U_APPEND_SHORT "uappnd"
+#define U_APPEND_FULL "uappend"
+#define U_ARCH_SHORT "uarch"
+#define U_ARCH_FULL "uarchive"
+#define U_IMMUTABLE "uchg"
+#define U_IMMUTABLE_SHORT "uchange"
+#define U_IMMUTABLE_FULL "uimmutable"
+#define U_HIDDEN_SHORT "hidden"
+#define U_HIDDEN_FULL "uhidden"
+#define U_OFFLINE_SHORT "offline"
+#define U_OFFLINE_FULL "uoffline"
+#define U_RDONLY "rdonly"
+#define U_RDONLY_SHORT "urdonly"
+#define U_RDONLY_FULL "readonly"
+#define U_SPARSE_SHORT "sparse"
+#define U_SPARSE_FULL "usparse"
+#define U_SYSTEM_SHORT "system"
+#define U_SYSTEM_FULL "usystem"
+#define U_REPARSE_SHORT "reparse"
+#define U_REPARSE_FULL "ureparse"
+#define U_UNLINK_SHORT "uunlnk"
+#define U_UNLINK_FULL "uunlink"
+#define UNSET_NODUMP "dump"
+
+#define NO_ATTRIBUTE "-"
+
+#define SEPARATOR ","
+
+#define BUFFER_SIZE 0x200
+
+void attribute_to_str(uint64_t attributes, char *buff);
+
+void
+attribute_to_str(uint64_t attributes, char *buff)
+{
+ if (attributes & ZFS_ARCHIVE) {
+ strcat(buff, U_ARCH_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_APPENDONLY) {
+ strcat(buff, U_APPEND_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_IMMUTABLE) {
+ strcat(buff, U_IMMUTABLE_FULL);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_NOUNLINK) {
+ strcat(buff, U_UNLINK_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_NODUMP) {
+ strcat(buff, SU_NODUMP);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_HIDDEN) {
+ strcat(buff, U_HIDDEN_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_OFFLINE) {
+ strcat(buff, U_OFFLINE_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_READONLY) {
+ strcat(buff, U_RDONLY);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_SPARSE) {
+ strcat(buff, U_SPARSE_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_SYSTEM) {
+ strcat(buff, U_SYSTEM_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (attributes & ZFS_REPARSE) {
+ strcat(buff, U_REPARSE_SHORT);
+ strcat(buff, SEPARATOR);
+ }
+
+ if (buff[0] == '\0')
+ strcat(buff, NO_ATTRIBUTE);
+ else
+ buff[strlen(buff) - 1] = '\0';
+}
+
+int
+main(int argc, const char * const argv[])
+{
+ if (argc != 2)
+ errx(EXIT_FAILURE, "Usage: %s filepath", argv[0]);
+
+ int fd = open(argv[1], O_RDWR | O_APPEND);
+ if (fd < 0)
+ err(EXIT_FAILURE, "Failed to open %s", argv[1]);
+
+ uint64_t dosflags = 0;
+ if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &dosflags) == -1)
+ err(EXIT_FAILURE, "ZFS_IOC_GETDOSFLAGS failed");
+
+ (void) close(fd);
+
+ char buffer[BUFFER_SIZE];
+ memset(buffer, 0, BUFFER_SIZE);
+
+ (void) attribute_to_str(dosflags, buffer);
+
+ (void) printf("%s\n", buffer);
+
+ return (EXIT_SUCCESS);
+}
diff --git a/tests/zfs-tests/cmd/write_dos_attributes/.gitignore b/tests/zfs-tests/cmd/write_dos_attributes/.gitignore
new file mode 100644
index 00000000000..f3949ac8282
--- /dev/null
+++ b/tests/zfs-tests/cmd/write_dos_attributes/.gitignore
@@ -0,0 +1 @@
+/write_dos_attributes
diff --git a/tests/zfs-tests/cmd/write_dos_attributes/Makefile.am b/tests/zfs-tests/cmd/write_dos_attributes/Makefile.am
new file mode 100644
index 00000000000..c297fd49e0e
--- /dev/null
+++ b/tests/zfs-tests/cmd/write_dos_attributes/Makefile.am
@@ -0,0 +1,6 @@
+include $(top_srcdir)/config/Rules.am
+
+pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/bin
+
+pkgexec_PROGRAMS = write_dos_attributes
+write_dos_attributes_SOURCES = write_dos_attributes.c
diff --git a/tests/zfs-tests/cmd/write_dos_attributes/write_dos_attributes.c b/tests/zfs-tests/cmd/write_dos_attributes/write_dos_attributes.c
new file mode 100644
index 00000000000..c373d3b1531
--- /dev/null
+++ b/tests/zfs-tests/cmd/write_dos_attributes/write_dos_attributes.c
@@ -0,0 +1,201 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source. A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2022 iXsystems, Inc.
+ */
+
+/*
+ * FreeBSD allows to update and retreive additional file level attributes.
+ * For Linux, two IOCTLs have been added to update and retrieve additional
+ * level attributes.
+ *
+ * This application updates additional file level attributes on a given
+ * file. FreeBSD keywords can be used to specify the flag.
+ *
+ * Usage: 'write_dos_attributes flag filepath'
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <err.h>
+#include <sys/fs/zfs.h>
+#include <string.h>
+#include <ctype.h>
+
+#define SU_ARCH_SHORT "arch"
+#define SU_ARCH_FULL "archived"
+#define SU_NODUMP "nodump"
+#define SU_APPEND_SHORT "sappnd"
+#define SU_APPEND_FULL "sappend"
+#define SU_IMMUTABLE "schg"
+#define SU_IMMUTABLE_SHORT "schange"
+#define SU_IMMUTABLE_FULL "simmutable"
+#define SU_UNLINK_SHORT "sunlnk"
+#define SU_UNLINK_FULL "sunlink"
+#define U_APPEND_SHORT "uappnd"
+#define U_APPEND_FULL "uappend"
+#define U_ARCH_SHORT "uarch"
+#define U_ARCH_FULL "uarchive"
+#define U_IMMUTABLE "uchg"
+#define U_IMMUTABLE_SHORT "uchange"
+#define U_IMMUTABLE_FULL "uimmutable"
+#define U_HIDDEN_SHORT "hidden"
+#define U_HIDDEN_FULL "uhidden"
+#define U_OFFLINE_SHORT "offline"
+#define U_OFFLINE_FULL "uoffline"
+#define U_RDONLY "rdonly"
+#define U_RDONLY_SHORT "urdonly"
+#define U_RDONLY_FULL "readonly"
+#define U_SPARSE_SHORT "sparse"
+#define U_SPARSE_FULL "usparse"
+#define U_SYSTEM_SHORT "system"
+#define U_SYSTEM_FULL "usystem"
+#define U_REPARSE_SHORT "reparse"
+#define U_REPARSE_FULL "ureparse"
+#define U_UNLINK_SHORT "uunlnk"
+#define U_UNLINK_FULL "uunlink"
+#define UNSET_NODUMP "dump"
+
+#define IS_NO(s) (s[0] == 'n' && s[1] == 'o')
+
+uint64_t str_to_attribute(char *str);
+
+uint64_t
+str_to_attribute(char *str)
+{
+ if ((strcmp(str, SU_ARCH_SHORT) == 0) ||
+ (strcmp(str, SU_ARCH_FULL) == 0) ||
+ (strcmp(str, U_ARCH_SHORT) == 0) ||
+ (strcmp(str, U_ARCH_FULL) == 0))
+ return (ZFS_ARCHIVE);
+
+ else if ((strcmp(str, SU_APPEND_SHORT) == 0) ||
+ (strcmp(str, SU_APPEND_FULL) == 0) ||
+ (strcmp(str, U_APPEND_SHORT) == 0) ||
+ (strcmp(str, U_APPEND_FULL) == 0))
+ return (ZFS_APPENDONLY);
+
+ else if ((strcmp(str, SU_IMMUTABLE) == 0) ||
+ (strcmp(str, SU_IMMUTABLE_SHORT) == 0) ||
+ (strcmp(str, SU_IMMUTABLE_FULL) == 0))
+ return (ZFS_IMMUTABLE);
+
+ else if ((strcmp(str, SU_UNLINK_SHORT) == 0) ||
+ (strcmp(str, SU_UNLINK_FULL) == 0) ||
+ (strcmp(str, U_UNLINK_SHORT) == 0) ||
+ (strcmp(str, SU_UNLINK_FULL) == 0))
+ return (ZFS_NOUNLINK);
+
+ else if ((strcmp(str, U_HIDDEN_SHORT) == 0) ||
+ (strcmp(str, U_HIDDEN_FULL) == 0))
+ return (ZFS_HIDDEN);
+
+ else if ((strcmp(str, U_OFFLINE_SHORT) == 0) ||
+ (strcmp(str, U_OFFLINE_FULL) == 0))
+ return (ZFS_OFFLINE);
+
+ else if ((strcmp(str, U_RDONLY) == 0) ||
+ (strcmp(str, U_RDONLY_SHORT) == 0) ||
+ (strcmp(str, U_RDONLY_FULL) == 0))
+ return (ZFS_READONLY);
+
+ else if ((strcmp(str, U_SPARSE_SHORT) == 0) ||
+ (strcmp(str, U_SPARSE_FULL) == 0))
+ return (ZFS_SPARSE);
+
+ else if ((strcmp(str, U_SYSTEM_SHORT) == 0) ||
+ (strcmp(str, U_SYSTEM_FULL) == 0))
+ return (ZFS_SYSTEM);
+
+ else if ((strcmp(str, U_REPARSE_SHORT) == 0) ||
+ (strcmp(str, U_REPARSE_FULL) == 0))
+ return (ZFS_REPARSE);
+
+ return (-1);
+}
+
+int
+main(int argc, const char * const argv[])
+{
+ if (argc != 3)
+ errx(EXIT_FAILURE, "Usage: %s flag filepath", argv[0]);
+
+ uint8_t unset, unset_all;
+ uint64_t attribute, dosflags;
+ char *flag = strdup(argv[1]);
+ unset = unset_all = 0;
+ attribute = dosflags = 0;
+
+ // convert the flag to lower case
+ for (int i = 0; i < strlen(argv[1]); ++i)
+ flag[i] = tolower((unsigned char) flag[i]);
+
+ // check if flag starts with 'no'
+ if (IS_NO(flag)) {
+ if (strcmp(flag, SU_NODUMP) == 0) {
+ attribute = ZFS_NODUMP;
+ } else {
+ attribute = str_to_attribute(flag + 2);
+ unset = 1;
+ }
+ }
+ // check if '0' was passed
+ else if (strcmp(flag, "0") == 0) {
+ unset_all = 1;
+ }
+ // check if the flag is 'dump'
+ else if (strcmp(flag, UNSET_NODUMP) == 0) {
+ attribute = ZFS_NODUMP;
+ unset = 1;
+ } else {
+ attribute = str_to_attribute(flag);
+ }
+
+ if (attribute == -1)
+ errx(EXIT_FAILURE, "Invalid Flag %s", argv[1]);
+
+ int fd = open(argv[2], O_RDWR | O_APPEND);
+ if (fd < 0)
+ err(EXIT_FAILURE, "Failed to open %s", argv[2]);
+
+ if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &dosflags) == -1)
+ err(EXIT_FAILURE, "ZFS_IOC_GETDOSFLAGS failed");
+
+ if (unset == 0 && attribute != 0)
+ attribute |= dosflags;
+ else if (unset == 1 && attribute != 0)
+ attribute = dosflags & (~attribute);
+ else if (unset_all == 1)
+ attribute = 0;
+
+ // set the attribute/s
+ if (ioctl(fd, ZFS_IOC_SETDOSFLAGS, &attribute) == -1)
+ err(EXIT_FAILURE, "ZFS_IOC_SETDOSFLAGS failed");
+
+ // get the attributes to confirm
+ dosflags = -1;
+ if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &dosflags) == -1)
+ err(EXIT_FAILURE, "ZFS_IOC_GETDOSFLAGS failed");
+
+ (void) close(fd);
+
+ if (dosflags != attribute)
+ errx(EXIT_FAILURE, "Could not set %s attribute", argv[1]);
+
+ (void) printf("New Dos Flags: 0x%llx\n", (u_longlong_t)dosflags);
+
+ return (EXIT_SUCCESS);
+}
diff --git a/tests/zfs-tests/include/commands.cfg b/tests/zfs-tests/include/commands.cfg
index fa3d12d669f..b247a67ff66 100644
--- a/tests/zfs-tests/include/commands.cfg
+++ b/tests/zfs-tests/include/commands.cfg
@@ -215,10 +215,12 @@ export ZFSTEST_FILES='badsend
randfree_file
randwritecomp
readmmap
+ read_dos_attributes
rename_dir
rm_lnkcnt_zero_file
send_doall
threadsappend
user_ns_exec
+ write_dos_attributes
xattrtest
stride_dd'
diff --git a/tests/zfs-tests/tests/functional/Makefile.am b/tests/zfs-tests/tests/functional/Makefile.am
index e71172b8e86..9164650e341 100644
--- a/tests/zfs-tests/tests/functional/Makefile.am
+++ b/tests/zfs-tests/tests/functional/Makefile.am
@@ -21,6 +21,7 @@ SUBDIRS = \
deadman \
delegate \
devices \
+ dos_attributes \
events \
exec \
fallocate \
diff --git a/tests/zfs-tests/tests/functional/acl/off/Makefile.am b/tests/zfs-tests/tests/functional/acl/off/Makefile.am
index 36aa13dd03f..ae6a9c69d93 100644
--- a/tests/zfs-tests/tests/functional/acl/off/Makefile.am
+++ b/tests/zfs-tests/tests/functional/acl/off/Makefile.am
@@ -10,7 +10,5 @@ dist_pkgdata_SCRIPTS = \
pkgexecdir = $(datadir)/@PACKAGE@/zfs-tests/tests/functional/acl/off
-if BUILD_FREEBSD
pkgexec_PROGRAMS = dosmode_readonly_write
dosmode_readonly_write_SOURCES = dosmode_readonly_write.c
-endif
diff --git a/tests/zfs-tests/tests/functional/acl/off/dosmode.ksh b/tests/zfs-tests/tests/functional/acl/off/dosmode.ksh
index e232dfd525c..585aa025390 100755
--- a/tests/zfs-tests/tests/functional/acl/off/dosmode.ksh
+++ b/tests/zfs-tests/tests/functional/acl/off/dosmode.ksh
@@ -31,8 +31,6 @@
# DESCRIPTION:
# Verify that DOS mode flags function correctly.
#
-# These flags are not currently exposed on Linux, so the test is
-# only useful on FreeBSD.
#
# STRATEGY:
# 1. ARCHIVE
@@ -56,7 +54,13 @@ function hasflag
typeset flag=$1
typeset path=$2
- ls -lo $path | awk '{ gsub(",", "\n", $5); print $5 }' | grep -qxF $flag
+ if is_linux; then
+ read_dos_attributes $path | awk \
+ '{ gsub(",", "\n", $1); print $1 }' | grep -qxF $flag
+ else
+ ls -lo $path | awk '{ gsub(",", "\n", $5); print $5 }' | \
+ grep -qxF $flag
+ fi
}
log_assert "Verify DOS mode flags function correctly"
@@ -67,6 +71,12 @@ testfile=$TESTDIR/testfile
owner=$ZFS_ACL_STAFF1
other=$ZFS_ACL_STAFF2
+if is_linux; then
+ changeflags=write_dos_attributes
+else
+ changeflags=chflags
+fi
+
#
# ARCHIVE
#
@@ -75,36 +85,40 @@ other=$ZFS_ACL_STAFF2
#
log_must touch $testfile
log_must hasflag uarch $testfile
-log_must chflags nouarch $testfile
+log_must $changeflags nouarch $testfile
log_must hasflag - $testfile
log_must touch $testfile
-log_must hasflag uarch $testfile
+if ! is_linux; then
+ log_must hasflag uarch $testfile
+fi
log_must rm $testfile
log_must user_run $owner touch $testfile
log_must hasflag uarch $testfile
-log_must user_run $owner chflags nouarch $testfile
-log_mustnot user_run $other chflags uarch $testfile
+log_must user_run $owner $changeflags nouarch $testfile
+log_mustnot user_run $other $changeflags uarch $testfile
log_must hasflag - $testfile
log_must user_run $owner touch $testfile
-log_mustnot user_run $other chflags nouarch $testfile
-log_must hasflag uarch $testfile
+log_mustnot user_run $other $changeflags nouarch $testfile
+if ! is_linux; then
+ log_must hasflag uarch $testfile
+fi
log_must user_run $owner rm $testfile
#
# HIDDEN
#
log_must touch $testfile
-log_must chflags hidden $testfile
+log_must $changeflags hidden $testfile
log_must hasflag hidden $testfile
-log_must chflags 0 $testfile
+log_must $changeflags 0 $testfile
log_must hasflag - $testfile
log_must rm $testfile
log_must user_run $owner touch $testfile
-log_must user_run $owner chflags hidden $testfile
-log_mustnot user_run $other chflags nohidden $testfile
+log_must user_run $owner $changeflags hidden $testfile
+log_mustnot user_run $other $changeflags nohidden $testfile
log_must hasflag hidden $testfile
-log_must user_run $owner chflags 0 $testfile
-log_mustnot user_run $other chflags hidden $testfile
+log_must user_run $owner $changeflags 0 $testfile
+log_mustnot user_run $other $changeflags hidden $testfile
log_must hasflag - $testfile
log_must user_run $owner rm $testfile
@@ -113,17 +127,17 @@ log_must user_run $owner rm $testfile
# OFFLINE
#
log_must touch $testfile
-log_must chflags offline $testfile
+log_must $changeflags offline $testfile
log_must hasflag offline $testfile
-log_must chflags 0 $testfile
+log_must $changeflags 0 $testfile
log_must hasflag - $testfile
log_must rm $testfile
log_must user_run $owner touch $testfile
-log_must user_run $owner chflags offline $testfile
-log_mustnot user_run $other chflags nooffline $testfile
+log_must user_run $owner $changeflags offline $testfile
+log_mustnot user_run $other $changeflags nooffline $testfile
log_must hasflag offline $testfile
-log_must user_run $owner chflags 0 $testfile
-log_mustnot user_run $other chflags offline $testfile
+log_must user_run $owner $changeflags 0 $testfile
+log_mustnot user_run $other $changeflags offline $testfile
log_must hasflag - $testfile
log_must user_run $owner rm $testfile
@@ -134,21 +148,23 @@ log_must user_run $owner rm $testfile
# but root is always allowed the operation.
#
log_must touch $testfile
-log_must chflags rdonly $testfile
+log_must $changeflags rdonly $testfile
log_must hasflag rdonly $testfile
log_must eval "echo 'root write allowed' >> $testfile"
log_must cat $testfile
-log_must chflags 0 $testfile
-log_must hasflag - $tesfile
+log_must $changeflags 0 $testfile
+log_must hasflag - $testfile
log_must rm $testfile
# It is required to still be able to write to an fd that was opened RW before
# READONLY is set. We have a special test program for that.
log_must user_run $owner touch $testfile
-log_mustnot user_run $other chflags rdonly $testfile
+log_mustnot user_run $other $changeflags rdonly $testfile
log_must user_run $owner $tests_base/dosmode_readonly_write $testfile
-log_mustnot user_run $other chflags nordonly $testfile
+log_mustnot user_run $other $changeflags nordonly $testfile
log_must hasflag rdonly $testfile
-log_mustnot user_run $owner "echo 'user write forbidden' >> $testfile"
+if ! is_linux; then
+ log_mustnot user_run $owner "echo 'user write forbidden' >> $testfile"
+fi
log_must eval "echo 'root write allowed' >> $testfile"
# We are still allowed to read and remove the file when READONLY is set.
log_must user_run $owner cat $testfile
@@ -157,24 +173,23 @@ log_must user_run $owner rm $testfile
#
# REPARSE
#
-# FIXME: does not work, not sure if broken or testing wrong
-#
+# not allowed to be changed
#
# SPARSE
#
log_must truncate -s 1m $testfile
-log_must chflags sparse $testfile
+log_must $changeflags sparse $testfile
log_must hasflag sparse $testfile
-log_must chflags 0 $testfile
+log_must $changeflags 0 $testfile
log_must hasflag - $testfile
log_must rm $testfile
log_must user_run $owner truncate -s 1m $testfile
-log_must user_run $owner chflags sparse $testfile
-log_mustnot user_run $other chflags nosparse $testfile
+log_must user_run $owner $changeflags sparse $testfile
+log_mustnot user_run $other $changeflags nosparse $testfile
log_must hasflag sparse $testfile
-log_must user_run $owner chflags 0 $testfile
-log_mustnot user_run $other chflags sparse $testfile
+log_must user_run $owner $changeflags 0 $testfile
+log_mustnot user_run $other $changeflags sparse $testfile
log_must hasflag - $testfile
log_must user_run $owner rm $testfile
@@ -182,17 +197,17 @@ log_must user_run $owner rm $testfile
# SYSTEM
#
log_must touch $testfile
-log_must chflags system $testfile
+log_must $changeflags system $testfile
log_must hasflag system $testfile
-log_must chflags 0 $testfile
+log_must $changeflags 0 $testfile
log_must hasflag - $testfile
log_must rm $testfile