1 /**
2  * D header file for the linux/fs.h interface.
3  *
4  * This file has definitions for some important file table structures
5  * and constants and structures used by various generic file system
6  * ioctl's.
7  *
8  * Copyright: The D Language Foundation 2021.
9  * License : $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
10  * Authors : Luís Ferreira
11  */
12 module core.sys.linux.fs;
13 
14 version (linux):
15 
16 public import core.sys.posix.sys.ioctl;
17 
18 import core.stdc.config : c_ulong, c_long;
19 
20 extern (C):
21 @nogc:
22 nothrow:
23 
24 enum INR_OPEN_CUR = 1024; /// Initial setting for nfile rlimits
25 enum INR_OPEN_MAX = 4096; /// Hard limit for nfile rlimits
26 
27 enum BLOCK_SIZE_BITS = 10; ///
28 enum BLOCK_SIZE = 1 << BLOCK_SIZE_BITS; ///
29 
30 enum
31 {
32     SEEK_SET = 0, /// seek relative to beginning of file
33     SEEK_CUR = 1, /// seek relative to current file position
34     SEEK_END = 2, /// seek relative to end of file
35     SEEK_DATA = 3, /// seek to the next data
36     SEEK_HOLE = 4, /// seek to the next hole
37     SEEK_MAX = SEEK_HOLE, ///
38 }
39 
40 enum
41 {
42     RENAME_NOREPLACE = 1 << 0, /// Don't overwrite target
43     RENAME_EXCHANGE = 1 << 1, /// Exchange source and dest
44     RENAME_WHITEOUT = 1 << 2, /// Whiteout source
45 }
46 
47 struct file_clone_range
48 {
49     long src_fd;
50     ulong src_offset;
51     ulong src_length;
52     ulong dest_offset;
53 }
54 
55 struct fstrim_range
56 {
57     ulong start;
58     ulong len;
59     ulong minlen;
60 }
61 
62 /**
63  * extent-same (dedupe) ioctls; these MUST match the btrfs ioctl definitions
64  */
65 enum
66 {
67     FILE_DEDUPE_RANGE_SAME = 0,
68     FILE_DEDUPE_RANGE_DIFFERS = 1,
69 }
70 
71 /**
72  * from struct btrfs_ioctl_file_extent_same_info
73  */
74 struct file_dedupe_range_info
75 {
76     long dest_fd; /// in - destination file
77     ulong dest_offset; /// in - start of extent in destination
78     ulong bytes_deduped; /// out - total # of bytes we were able to dedupe from this file.
79     /** status of this dedupe operation:
80      * < 0 for error
81      * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds
82      * == FILE_DEDUPE_RANGE_DIFFERS if data differs
83      */
84     int status;
85     uint reserved; /// must be zero
86 }
87 
88 /**
89  * from struct btrfs_ioctl_file_extent_same_args
90  */
91 struct file_dedupe_range
92 {
93     ulong src_offset; /// in - start of extent in source
94     ulong src_length; /// in - length of extent
95     ushort dest_count; /// in - total elements in info array
96     ushort reserved1; /// must be zero
97     uint reserved2; /// must be zero
98     file_dedupe_range_info[0] info;
99 }
100 
101 /**
102  * And dynamically-tunable limits and defaults:
103  */
104 struct files_stat_struct
105 {
106     c_ulong nr_files; /// read only
107     c_ulong nr_free_files; /// read only
108     c_ulong max_files; /// tunable
109 }
110 
111 struct inodes_stat_t
112 {
113     c_long nr_inodes;
114     c_long nr_unused;
115     c_long[5] dummy; /// padding for sysctl ABI compatibility
116 }
117 
118 enum NR_FILE = 8192;
119 
120 /**
121  * Structure for FS_IOC_FSGETXATTR[A] and FS_IOC_FSSETXATTR.
122  */
123 struct fsxattr
124 {
125     uint fsx_xflags;
126     uint fsx_extsize;
127     uint fsx_nextents;
128     uint fsx_projid; /// project identifier
129     uint fsx_cowextsize; /// CoW extsize
130     ubyte[8] fsx_pad;
131 }
132 
133 /*
134  * Flags for the fsx_xflags field
135  */
136 enum {
137     S_XFLAG_REALTIME = 0x00000001, /// data in realtime volume
138     S_XFLAG_PREALLOC = 0x00000002, /// preallocated file extents
139     S_XFLAG_IMMUTABLE = 0x00000008, /// file cannot be modified
140     S_XFLAG_APPEND = 0x00000010, /// all writes append
141     S_XFLAG_SYNC = 0x00000020, /// all writes synchronous
142     S_XFLAG_NOATIME = 0x00000040, /// do not update access time
143     S_XFLAG_NODUMP = 0x00000080, /// do not include in backups
144     S_XFLAG_RTINHERIT = 0x00000100, /// create with rt bit set
145     S_XFLAG_PROJINHERIT = 0x00000200, /// create with parents projid
146     S_XFLAG_NOSYMLINKS = 0x00000400, /// disallow symlink creation
147     S_XFLAG_EXTSIZE = 0x00000800, /// extent size allocator hint
148     S_XFLAG_EXTSZINHERIT = 0x00001000, /// inherit inode extent size
149     S_XFLAG_NODEFRAG = 0x00002000, /// do not defragment
150     S_XFLAG_FILESTREAM = 0x00004000, /// use filestream allocator
151     S_XFLAG_DAX = 0x00008000, /// use DAX for IO
152     S_XFLAG_COWEXTSIZE = 0x00010000, /// CoW extent size allocator hint
153     S_XFLAG_HASATTR = 0x80000000, /// no DIFLAG for this
154 }
155 
156 static if (__traits(compiles, _IO(1, 2)))
157 {
158     enum BLKROSET = _IO(0x12, 93); /// set device read-only
159     enum BLKROGET = _IO(0x12, 94); /// get read-only status
160     enum BLKRRPART = _IO(0x12, 95); /// re-read partition table
161     enum BLKGETSIZE = _IO(0x12, 96); /// return device size
162     enum BLKFLSBUF = _IO(0x12, 97); /// flush buffer cache
163     enum BLKRASET = _IO(0x12, 98); /// set read ahead for block device
164     enum BLKRAGET = _IO(0x12, 99); /// get current read ahead setting
165     enum BLKFRASET = _IO(0x12, 100); /// set filesystem
166     enum BLKFRAGET = _IO(0x12, 101); /// get filesystem
167     enum BLKSECTSET = _IO(0x12, 102); /// set max sectors per request
168     enum BLKSECTGET = _IO(0x12, 103); /// get max sectors per request
169     enum BLKSSZGET = _IO(0x12, 104); /// get block device sector size
170 
171 
172     enum BLKBSZGET = _IOR!size_t(0x12, 112);
173     enum BLKBSZSET = _IOW!size_t(0x12, 113);
174     enum BLKGETSIZE64 = _IOR!size_t(0x12, 114);
175     enum BLKTRACESTART = _IO(0x12, 116);
176     enum BLKTRACESTOP = _IO(0x12, 117);
177     enum BLKTRACETEARDOWN = _IO(0x12, 118);
178     enum BLKDISCARD = _IO(0x12, 119);
179     enum BLKIOMIN = _IO(0x12, 120);
180     enum BLKIOOPT = _IO(0x12, 121);
181     enum BLKALIGNOFF = _IO(0x12, 122);
182     enum BLKPBSZGET = _IO(0x12, 123);
183     enum BLKDISCARDZEROES = _IO(0x12, 124);
184     enum BLKSECDISCARD = _IO(0x12, 125);
185     enum BLKROTATIONAL = _IO(0x12, 126);
186     enum BLKZEROOUT = _IO(0x12, 127);
187 
188     enum BMAP_IOCTL = 1; /// obsolete - kept for compatibility
189     enum FIBMAP = _IO(0x00, 1); /// bmap access
190     enum FIGETBSZ = _IO(0x00, 2); /// get the block size used for bmap
191 }
192 
193 enum FSLABEL_MAX = 256; /// Max chars for the interface; each fs may differ
194 
195 /**
196  * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)
197  *
198  * Note: for historical reasons, these flags were originally used and
199  * defined for use by ext2/ext3, and then other file systems started
200  * using these flags so they wouldn't need to write their own version
201  * of chattr/lsattr (which was shipped as part of e2fsprogs).  You
202  * should think twice before trying to use these flags in new
203  * contexts, or trying to assign these flags, since they are used both
204  * as the UAPI and the on-disk encoding for ext2/3/4.  Also, we are
205  * almost out of 32-bit flags.  :-)
206  *
207  * We have recently hoisted FS_IOC_FSGETXATTR / FS_IOC_FSSETXATTR from
208  * XFS to the generic FS level interface.  This uses a structure that
209  * has padding and hence has more room to grow, so it may be more
210  * appropriate for many new use cases.
211  */
212 enum {
213     FS_SECRM_FL = 0x00000001, /// Secure deletion
214     FS_UNRM_FL = 0x00000002, /// Undelete
215     FS_COMPR_FL = 0x00000004, /// Compress file
216     FS_SYNC_FL = 0x00000008, /// Synchronous updates
217     FS_IMMUTABLE_FL = 0x00000010, /// Immutable file
218     FS_APPEND_FL = 0x00000020, /// writes to file may only append
219     FS_NODUMP_FL = 0x00000040, /// do not dump file
220     FS_NOATIME_FL = 0x00000080, /// do not update atime
221     FS_DIRTY_FL = 0x00000100, /// Reserved for compression usage
222     FS_COMPRBLK_FL = 0x00000200, /// One or more compressed clusters
223     FS_NOCOMP_FL = 0x00000400, /// Don't compress
224     FS_ENCRYPT_FL = 0x00000800, /// Encrypted file
225     FS_BTREE_FL = 0x00001000, /// btree format dir
226     FS_INDEX_FL = 0x00001000, /// hash-indexed directory
227     FS_IMAGIC_FL = 0x00002000, /// AFS directory
228     FS_JOURNAL_DATA_FL = 0x00004000, /// Reserved for ext3
229     FS_NOTAIL_FL = 0x00008000, /// file tail should not be merged
230     FS_DIRSYNC_FL = 0x00010000, /// dirsync behaviour (directories only)
231     FS_TOPDIR_FL = 0x00020000, /// Top of directory hierarchie
232     FS_HUGE_FILE_FL = 0x00040000, /// Reserved for ext4
233     FS_EXTENT_FL = 0x00080000, /// Extents
234     FS_VERITY_FL = 0x00100000, /// Verity protected inode
235     FS_EA_INODE_FL = 0x00200000, /// Inode used for large EA
236     FS_EOFBLOCKS_FL = 0x00400000, /// Reserved for ext4
237     FS_NOCOW_FL = 0x00800000, /// Do not cow file
238     FS_DAX_FL = 0x02000000, /// Inode is DAX
239     FS_INLINE_DATA_FL = 0x10000000, /// Reserved for ext4
240     FS_PROJINHERIT_FL = 0x20000000, /// Create with parents projid
241     FS_CASEFOLD_FL = 0x40000000, /// Folder is case insensitive
242     FS_RESERVED_FL = 0x80000000, /// reserved for ext2 lib
243 }
244 
245 enum FS_FL_USER_VISIBLE = 0x0003DFFF; /// User visible flags
246 enum FS_FL_USER_MODIFIABLE = 0x000380FF; /// User modifiable flags
247 
248 enum SYNC_FILE_RANGE_WAIT_BEFORE = 1;
249 enum SYNC_FILE_RANGE_WRITE = 2;
250 enum SYNC_FILE_RANGE_WAIT_AFTER = 4;
251 enum SYNC_FILE_RANGE_WRITE_AND_WAIT = SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WAIT_AFTER;
252 
253 alias __kernel_rwf_t = int;
254 
255 /**
256  * Flags for preadv2/pwritev2:
257  */
258 enum : __kernel_rwf_t {
259     RWF_HIPRI = 0x00000001, /// high priority request, poll if possible
260     RWF_DSYNC = 0x00000002, /// per-IO O_DSYNC
261     RWF_SYNC = 0x00000004, /// per-IO O_SYNC
262     RWF_NOWAIT = 0x00000008, /// per-IO, return -EAGAIN if operation would block
263     RWF_APPEND = 0x00000010, /// per-IO O_APPEND
264 }
265 
266 /// mask of flags supported by the kernel
267 enum RWF_SUPPORTED = RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT | RWF_APPEND;