GNU Linux-libre 4.14.319-gnu1
[releases.git] / fs / ext2 / ext2.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 1992, 1993, 1994, 1995
4  * Remy Card (card@masi.ibp.fr)
5  * Laboratoire MASI - Institut Blaise Pascal
6  * Universite Pierre et Marie Curie (Paris VI)
7  *
8  *  from
9  *
10  *  linux/include/linux/minix_fs.h
11  *
12  *  Copyright (C) 1991, 1992  Linus Torvalds
13  */
14 #include <linux/fs.h>
15 #include <linux/ext2_fs.h>
16 #include <linux/blockgroup_lock.h>
17 #include <linux/percpu_counter.h>
18 #include <linux/rbtree.h>
19
20 /* XXX Here for now... not interested in restructing headers JUST now */
21
22 /* data type for block offset of block group */
23 typedef int ext2_grpblk_t;
24
25 /* data type for filesystem-wide blocks number */
26 typedef unsigned long ext2_fsblk_t;
27
28 #define E2FSBLK "%lu"
29
30 struct ext2_reserve_window {
31         ext2_fsblk_t            _rsv_start;     /* First byte reserved */
32         ext2_fsblk_t            _rsv_end;       /* Last byte reserved or 0 */
33 };
34
35 struct ext2_reserve_window_node {
36         struct rb_node          rsv_node;
37         __u32                   rsv_goal_size;
38         __u32                   rsv_alloc_hit;
39         struct ext2_reserve_window      rsv_window;
40 };
41
42 struct ext2_block_alloc_info {
43         /* information about reservation window */
44         struct ext2_reserve_window_node rsv_window_node;
45         /*
46          * was i_next_alloc_block in ext2_inode_info
47          * is the logical (file-relative) number of the
48          * most-recently-allocated block in this file.
49          * We use this for detecting linearly ascending allocation requests.
50          */
51         __u32                   last_alloc_logical_block;
52         /*
53          * Was i_next_alloc_goal in ext2_inode_info
54          * is the *physical* companion to i_next_alloc_block.
55          * it the the physical block number of the block which was most-recentl
56          * allocated to this file.  This give us the goal (target) for the next
57          * allocation when we detect linearly ascending requests.
58          */
59         ext2_fsblk_t            last_alloc_physical_block;
60 };
61
62 #define rsv_start rsv_window._rsv_start
63 #define rsv_end rsv_window._rsv_end
64
65 struct mb_cache;
66
67 /*
68  * second extended-fs super-block data in memory
69  */
70 struct ext2_sb_info {
71         unsigned long s_frag_size;      /* Size of a fragment in bytes */
72         unsigned long s_frags_per_block;/* Number of fragments per block */
73         unsigned long s_inodes_per_block;/* Number of inodes per block */
74         unsigned long s_frags_per_group;/* Number of fragments in a group */
75         unsigned long s_blocks_per_group;/* Number of blocks in a group */
76         unsigned long s_inodes_per_group;/* Number of inodes in a group */
77         unsigned long s_itb_per_group;  /* Number of inode table blocks per group */
78         unsigned long s_gdb_count;      /* Number of group descriptor blocks */
79         unsigned long s_desc_per_block; /* Number of group descriptors per block */
80         unsigned long s_groups_count;   /* Number of groups in the fs */
81         unsigned long s_overhead_last;  /* Last calculated overhead */
82         unsigned long s_blocks_last;    /* Last seen block count */
83         struct buffer_head * s_sbh;     /* Buffer containing the super block */
84         struct ext2_super_block * s_es; /* Pointer to the super block in the buffer */
85         struct buffer_head ** s_group_desc;
86         unsigned long  s_mount_opt;
87         unsigned long s_sb_block;
88         kuid_t s_resuid;
89         kgid_t s_resgid;
90         unsigned short s_mount_state;
91         unsigned short s_pad;
92         int s_addr_per_block_bits;
93         int s_desc_per_block_bits;
94         int s_inode_size;
95         int s_first_ino;
96         spinlock_t s_next_gen_lock;
97         u32 s_next_generation;
98         unsigned long s_dir_count;
99         u8 *s_debts;
100         struct percpu_counter s_freeblocks_counter;
101         struct percpu_counter s_freeinodes_counter;
102         struct percpu_counter s_dirs_counter;
103         struct blockgroup_lock *s_blockgroup_lock;
104         /* root of the per fs reservation window tree */
105         spinlock_t s_rsv_window_lock;
106         struct rb_root s_rsv_window_root;
107         struct ext2_reserve_window_node s_rsv_window_head;
108         /*
109          * s_lock protects against concurrent modifications of s_mount_state,
110          * s_blocks_last, s_overhead_last and the content of superblock's
111          * buffer pointed to by sbi->s_es.
112          *
113          * Note: It is used in ext2_show_options() to provide a consistent view
114          * of the mount options.
115          */
116         spinlock_t s_lock;
117         struct mb_cache *s_ea_block_cache;
118         struct dax_device *s_daxdev;
119 };
120
121 static inline spinlock_t *
122 sb_bgl_lock(struct ext2_sb_info *sbi, unsigned int block_group)
123 {
124         return bgl_lock_ptr(sbi->s_blockgroup_lock, block_group);
125 }
126
127 /*
128  * Define EXT2FS_DEBUG to produce debug messages
129  */
130 #undef EXT2FS_DEBUG
131
132 /*
133  * Define EXT2_RESERVATION to reserve data blocks for expanding files
134  */
135 #define EXT2_DEFAULT_RESERVE_BLOCKS     8
136 /*max window size: 1024(direct blocks) + 3([t,d]indirect blocks) */
137 #define EXT2_MAX_RESERVE_BLOCKS         1027
138 #define EXT2_RESERVE_WINDOW_NOT_ALLOCATED 0
139 /*
140  * The second extended file system version
141  */
142 #define EXT2FS_DATE             "95/08/09"
143 #define EXT2FS_VERSION          "0.5b"
144
145 /*
146  * Debug code
147  */
148 #ifdef EXT2FS_DEBUG
149 #       define ext2_debug(f, a...)      { \
150                                         printk ("EXT2-fs DEBUG (%s, %d): %s:", \
151                                                 __FILE__, __LINE__, __func__); \
152                                         printk (f, ## a); \
153                                         }
154 #else
155 #       define ext2_debug(f, a...)      /**/
156 #endif
157
158 /*
159  * Special inode numbers
160  */
161 #define EXT2_BAD_INO             1      /* Bad blocks inode */
162 #define EXT2_ROOT_INO            2      /* Root inode */
163 #define EXT2_BOOT_LOADER_INO     5      /* Boot loader inode */
164 #define EXT2_UNDEL_DIR_INO       6      /* Undelete directory inode */
165
166 /* First non-reserved inode for old ext2 filesystems */
167 #define EXT2_GOOD_OLD_FIRST_INO 11
168
169 static inline struct ext2_sb_info *EXT2_SB(struct super_block *sb)
170 {
171         return sb->s_fs_info;
172 }
173
174 /*
175  * Macro-instructions used to manage several block sizes
176  */
177 #define EXT2_MIN_BLOCK_SIZE             1024
178 #define EXT2_MAX_BLOCK_SIZE             4096
179 #define EXT2_MIN_BLOCK_LOG_SIZE           10
180 #define EXT2_MAX_BLOCK_LOG_SIZE           16
181 #define EXT2_BLOCK_SIZE(s)              ((s)->s_blocksize)
182 #define EXT2_ADDR_PER_BLOCK(s)          (EXT2_BLOCK_SIZE(s) / sizeof (__u32))
183 #define EXT2_BLOCK_SIZE_BITS(s)         ((s)->s_blocksize_bits)
184 #define EXT2_ADDR_PER_BLOCK_BITS(s)     (EXT2_SB(s)->s_addr_per_block_bits)
185 #define EXT2_INODE_SIZE(s)              (EXT2_SB(s)->s_inode_size)
186 #define EXT2_FIRST_INO(s)               (EXT2_SB(s)->s_first_ino)
187
188 /*
189  * Macro-instructions used to manage fragments
190  */
191 #define EXT2_MIN_FRAG_SIZE              1024
192 #define EXT2_MAX_FRAG_SIZE              4096
193 #define EXT2_MIN_FRAG_LOG_SIZE            10
194 #define EXT2_FRAG_SIZE(s)               (EXT2_SB(s)->s_frag_size)
195 #define EXT2_FRAGS_PER_BLOCK(s)         (EXT2_SB(s)->s_frags_per_block)
196
197 /*
198  * Structure of a blocks group descriptor
199  */
200 struct ext2_group_desc
201 {
202         __le32  bg_block_bitmap;                /* Blocks bitmap block */
203         __le32  bg_inode_bitmap;                /* Inodes bitmap block */
204         __le32  bg_inode_table;         /* Inodes table block */
205         __le16  bg_free_blocks_count;   /* Free blocks count */
206         __le16  bg_free_inodes_count;   /* Free inodes count */
207         __le16  bg_used_dirs_count;     /* Directories count */
208         __le16  bg_pad;
209         __le32  bg_reserved[3];
210 };
211
212 /*
213  * Macro-instructions used to manage group descriptors
214  */
215 #define EXT2_BLOCKS_PER_GROUP(s)        (EXT2_SB(s)->s_blocks_per_group)
216 #define EXT2_DESC_PER_BLOCK(s)          (EXT2_SB(s)->s_desc_per_block)
217 #define EXT2_INODES_PER_GROUP(s)        (EXT2_SB(s)->s_inodes_per_group)
218 #define EXT2_DESC_PER_BLOCK_BITS(s)     (EXT2_SB(s)->s_desc_per_block_bits)
219
220 /*
221  * Constants relative to the data blocks
222  */
223 #define EXT2_NDIR_BLOCKS                12
224 #define EXT2_IND_BLOCK                  EXT2_NDIR_BLOCKS
225 #define EXT2_DIND_BLOCK                 (EXT2_IND_BLOCK + 1)
226 #define EXT2_TIND_BLOCK                 (EXT2_DIND_BLOCK + 1)
227 #define EXT2_N_BLOCKS                   (EXT2_TIND_BLOCK + 1)
228
229 /*
230  * Inode flags (GETFLAGS/SETFLAGS)
231  */
232 #define EXT2_SECRM_FL                   FS_SECRM_FL     /* Secure deletion */
233 #define EXT2_UNRM_FL                    FS_UNRM_FL      /* Undelete */
234 #define EXT2_COMPR_FL                   FS_COMPR_FL     /* Compress file */
235 #define EXT2_SYNC_FL                    FS_SYNC_FL      /* Synchronous updates */
236 #define EXT2_IMMUTABLE_FL               FS_IMMUTABLE_FL /* Immutable file */
237 #define EXT2_APPEND_FL                  FS_APPEND_FL    /* writes to file may only append */
238 #define EXT2_NODUMP_FL                  FS_NODUMP_FL    /* do not dump file */
239 #define EXT2_NOATIME_FL                 FS_NOATIME_FL   /* do not update atime */
240 /* Reserved for compression usage... */
241 #define EXT2_DIRTY_FL                   FS_DIRTY_FL
242 #define EXT2_COMPRBLK_FL                FS_COMPRBLK_FL  /* One or more compressed clusters */
243 #define EXT2_NOCOMP_FL                  FS_NOCOMP_FL    /* Don't compress */
244 #define EXT2_ECOMPR_FL                  FS_ECOMPR_FL    /* Compression error */
245 /* End compression flags --- maybe not all used */      
246 #define EXT2_BTREE_FL                   FS_BTREE_FL     /* btree format dir */
247 #define EXT2_INDEX_FL                   FS_INDEX_FL     /* hash-indexed directory */
248 #define EXT2_IMAGIC_FL                  FS_IMAGIC_FL    /* AFS directory */
249 #define EXT2_JOURNAL_DATA_FL            FS_JOURNAL_DATA_FL /* Reserved for ext3 */
250 #define EXT2_NOTAIL_FL                  FS_NOTAIL_FL    /* file tail should not be merged */
251 #define EXT2_DIRSYNC_FL                 FS_DIRSYNC_FL   /* dirsync behaviour (directories only) */
252 #define EXT2_TOPDIR_FL                  FS_TOPDIR_FL    /* Top of directory hierarchies*/
253 #define EXT2_RESERVED_FL                FS_RESERVED_FL  /* reserved for ext2 lib */
254
255 #define EXT2_FL_USER_VISIBLE            FS_FL_USER_VISIBLE      /* User visible flags */
256 #define EXT2_FL_USER_MODIFIABLE         FS_FL_USER_MODIFIABLE   /* User modifiable flags */
257
258 /* Flags that should be inherited by new inodes from their parent. */
259 #define EXT2_FL_INHERITED (EXT2_SECRM_FL | EXT2_UNRM_FL | EXT2_COMPR_FL |\
260                            EXT2_SYNC_FL | EXT2_NODUMP_FL |\
261                            EXT2_NOATIME_FL | EXT2_COMPRBLK_FL |\
262                            EXT2_NOCOMP_FL | EXT2_JOURNAL_DATA_FL |\
263                            EXT2_NOTAIL_FL | EXT2_DIRSYNC_FL)
264
265 /* Flags that are appropriate for regular files (all but dir-specific ones). */
266 #define EXT2_REG_FLMASK (~(EXT2_DIRSYNC_FL | EXT2_TOPDIR_FL))
267
268 /* Flags that are appropriate for non-directories/regular files. */
269 #define EXT2_OTHER_FLMASK (EXT2_NODUMP_FL | EXT2_NOATIME_FL)
270
271 /* Mask out flags that are inappropriate for the given type of inode. */
272 static inline __u32 ext2_mask_flags(umode_t mode, __u32 flags)
273 {
274         if (S_ISDIR(mode))
275                 return flags;
276         else if (S_ISREG(mode))
277                 return flags & EXT2_REG_FLMASK;
278         else
279                 return flags & EXT2_OTHER_FLMASK;
280 }
281
282 /*
283  * ioctl commands
284  */
285 #define EXT2_IOC_GETFLAGS               FS_IOC_GETFLAGS
286 #define EXT2_IOC_SETFLAGS               FS_IOC_SETFLAGS
287 #define EXT2_IOC_GETVERSION             FS_IOC_GETVERSION
288 #define EXT2_IOC_SETVERSION             FS_IOC_SETVERSION
289 #define EXT2_IOC_GETRSVSZ               _IOR('f', 5, long)
290 #define EXT2_IOC_SETRSVSZ               _IOW('f', 6, long)
291
292 /*
293  * ioctl commands in 32 bit emulation
294  */
295 #define EXT2_IOC32_GETFLAGS             FS_IOC32_GETFLAGS
296 #define EXT2_IOC32_SETFLAGS             FS_IOC32_SETFLAGS
297 #define EXT2_IOC32_GETVERSION           FS_IOC32_GETVERSION
298 #define EXT2_IOC32_SETVERSION           FS_IOC32_SETVERSION
299
300 /*
301  * Structure of an inode on the disk
302  */
303 struct ext2_inode {
304         __le16  i_mode;         /* File mode */
305         __le16  i_uid;          /* Low 16 bits of Owner Uid */
306         __le32  i_size;         /* Size in bytes */
307         __le32  i_atime;        /* Access time */
308         __le32  i_ctime;        /* Creation time */
309         __le32  i_mtime;        /* Modification time */
310         __le32  i_dtime;        /* Deletion Time */
311         __le16  i_gid;          /* Low 16 bits of Group Id */
312         __le16  i_links_count;  /* Links count */
313         __le32  i_blocks;       /* Blocks count */
314         __le32  i_flags;        /* File flags */
315         union {
316                 struct {
317                         __le32  l_i_reserved1;
318                 } linux1;
319                 struct {
320                         __le32  h_i_translator;
321                 } hurd1;
322                 struct {
323                         __le32  m_i_reserved1;
324                 } masix1;
325         } osd1;                         /* OS dependent 1 */
326         __le32  i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
327         __le32  i_generation;   /* File version (for NFS) */
328         __le32  i_file_acl;     /* File ACL */
329         __le32  i_dir_acl;      /* Directory ACL */
330         __le32  i_faddr;        /* Fragment address */
331         union {
332                 struct {
333                         __u8    l_i_frag;       /* Fragment number */
334                         __u8    l_i_fsize;      /* Fragment size */
335                         __u16   i_pad1;
336                         __le16  l_i_uid_high;   /* these 2 fields    */
337                         __le16  l_i_gid_high;   /* were reserved2[0] */
338                         __u32   l_i_reserved2;
339                 } linux2;
340                 struct {
341                         __u8    h_i_frag;       /* Fragment number */
342                         __u8    h_i_fsize;      /* Fragment size */
343                         __le16  h_i_mode_high;
344                         __le16  h_i_uid_high;
345                         __le16  h_i_gid_high;
346                         __le32  h_i_author;
347                 } hurd2;
348                 struct {
349                         __u8    m_i_frag;       /* Fragment number */
350                         __u8    m_i_fsize;      /* Fragment size */
351                         __u16   m_pad1;
352                         __u32   m_i_reserved2[2];
353                 } masix2;
354         } osd2;                         /* OS dependent 2 */
355 };
356
357 #define i_size_high     i_dir_acl
358
359 #define i_reserved1     osd1.linux1.l_i_reserved1
360 #define i_frag          osd2.linux2.l_i_frag
361 #define i_fsize         osd2.linux2.l_i_fsize
362 #define i_uid_low       i_uid
363 #define i_gid_low       i_gid
364 #define i_uid_high      osd2.linux2.l_i_uid_high
365 #define i_gid_high      osd2.linux2.l_i_gid_high
366 #define i_reserved2     osd2.linux2.l_i_reserved2
367
368 /*
369  * File system states
370  */
371 #define EXT2_VALID_FS                   0x0001  /* Unmounted cleanly */
372 #define EXT2_ERROR_FS                   0x0002  /* Errors detected */
373 #define EFSCORRUPTED                    EUCLEAN /* Filesystem is corrupted */
374
375 /*
376  * Mount flags
377  */
378 #define EXT2_MOUNT_CHECK                0x000001  /* Do mount-time checks */
379 #define EXT2_MOUNT_OLDALLOC             0x000002  /* Don't use the new Orlov allocator */
380 #define EXT2_MOUNT_GRPID                0x000004  /* Create files with directory's group */
381 #define EXT2_MOUNT_DEBUG                0x000008  /* Some debugging messages */
382 #define EXT2_MOUNT_ERRORS_CONT          0x000010  /* Continue on errors */
383 #define EXT2_MOUNT_ERRORS_RO            0x000020  /* Remount fs ro on errors */
384 #define EXT2_MOUNT_ERRORS_PANIC         0x000040  /* Panic on errors */
385 #define EXT2_MOUNT_MINIX_DF             0x000080  /* Mimics the Minix statfs */
386 #define EXT2_MOUNT_NOBH                 0x000100  /* No buffer_heads */
387 #define EXT2_MOUNT_NO_UID32             0x000200  /* Disable 32-bit UIDs */
388 #define EXT2_MOUNT_XATTR_USER           0x004000  /* Extended user attributes */
389 #define EXT2_MOUNT_POSIX_ACL            0x008000  /* POSIX Access Control Lists */
390 #define EXT2_MOUNT_XIP                  0x010000  /* Obsolete, use DAX */
391 #define EXT2_MOUNT_USRQUOTA             0x020000  /* user quota */
392 #define EXT2_MOUNT_GRPQUOTA             0x040000  /* group quota */
393 #define EXT2_MOUNT_RESERVATION          0x080000  /* Preallocation */
394 #ifdef CONFIG_FS_DAX
395 #define EXT2_MOUNT_DAX                  0x100000  /* Direct Access */
396 #else
397 #define EXT2_MOUNT_DAX                  0
398 #endif
399
400
401 #define clear_opt(o, opt)               o &= ~EXT2_MOUNT_##opt
402 #define set_opt(o, opt)                 o |= EXT2_MOUNT_##opt
403 #define test_opt(sb, opt)               (EXT2_SB(sb)->s_mount_opt & \
404                                          EXT2_MOUNT_##opt)
405 /*
406  * Maximal mount counts between two filesystem checks
407  */
408 #define EXT2_DFL_MAX_MNT_COUNT          20      /* Allow 20 mounts */
409 #define EXT2_DFL_CHECKINTERVAL          0       /* Don't use interval check */
410
411 /*
412  * Behaviour when detecting errors
413  */
414 #define EXT2_ERRORS_CONTINUE            1       /* Continue execution */
415 #define EXT2_ERRORS_RO                  2       /* Remount fs read-only */
416 #define EXT2_ERRORS_PANIC               3       /* Panic */
417 #define EXT2_ERRORS_DEFAULT             EXT2_ERRORS_CONTINUE
418
419 /*
420  * Structure of the super block
421  */
422 struct ext2_super_block {
423         __le32  s_inodes_count;         /* Inodes count */
424         __le32  s_blocks_count;         /* Blocks count */
425         __le32  s_r_blocks_count;       /* Reserved blocks count */
426         __le32  s_free_blocks_count;    /* Free blocks count */
427         __le32  s_free_inodes_count;    /* Free inodes count */
428         __le32  s_first_data_block;     /* First Data Block */
429         __le32  s_log_block_size;       /* Block size */
430         __le32  s_log_frag_size;        /* Fragment size */
431         __le32  s_blocks_per_group;     /* # Blocks per group */
432         __le32  s_frags_per_group;      /* # Fragments per group */
433         __le32  s_inodes_per_group;     /* # Inodes per group */
434         __le32  s_mtime;                /* Mount time */
435         __le32  s_wtime;                /* Write time */
436         __le16  s_mnt_count;            /* Mount count */
437         __le16  s_max_mnt_count;        /* Maximal mount count */
438         __le16  s_magic;                /* Magic signature */
439         __le16  s_state;                /* File system state */
440         __le16  s_errors;               /* Behaviour when detecting errors */
441         __le16  s_minor_rev_level;      /* minor revision level */
442         __le32  s_lastcheck;            /* time of last check */
443         __le32  s_checkinterval;        /* max. time between checks */
444         __le32  s_creator_os;           /* OS */
445         __le32  s_rev_level;            /* Revision level */
446         __le16  s_def_resuid;           /* Default uid for reserved blocks */
447         __le16  s_def_resgid;           /* Default gid for reserved blocks */
448         /*
449          * These fields are for EXT2_DYNAMIC_REV superblocks only.
450          *
451          * Note: the difference between the compatible feature set and
452          * the incompatible feature set is that if there is a bit set
453          * in the incompatible feature set that the kernel doesn't
454          * know about, it should refuse to mount the filesystem.
455          * 
456          * e2fsck's requirements are more strict; if it doesn't know
457          * about a feature in either the compatible or incompatible
458          * feature set, it must abort and not try to meddle with
459          * things it doesn't understand...
460          */
461         __le32  s_first_ino;            /* First non-reserved inode */
462         __le16   s_inode_size;          /* size of inode structure */
463         __le16  s_block_group_nr;       /* block group # of this superblock */
464         __le32  s_feature_compat;       /* compatible feature set */
465         __le32  s_feature_incompat;     /* incompatible feature set */
466         __le32  s_feature_ro_compat;    /* readonly-compatible feature set */
467         __u8    s_uuid[16];             /* 128-bit uuid for volume */
468         char    s_volume_name[16];      /* volume name */
469         char    s_last_mounted[64];     /* directory where last mounted */
470         __le32  s_algorithm_usage_bitmap; /* For compression */
471         /*
472          * Performance hints.  Directory preallocation should only
473          * happen if the EXT2_COMPAT_PREALLOC flag is on.
474          */
475         __u8    s_prealloc_blocks;      /* Nr of blocks to try to preallocate*/
476         __u8    s_prealloc_dir_blocks;  /* Nr to preallocate for dirs */
477         __u16   s_padding1;
478         /*
479          * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
480          */
481         __u8    s_journal_uuid[16];     /* uuid of journal superblock */
482         __u32   s_journal_inum;         /* inode number of journal file */
483         __u32   s_journal_dev;          /* device number of journal file */
484         __u32   s_last_orphan;          /* start of list of inodes to delete */
485         __u32   s_hash_seed[4];         /* HTREE hash seed */
486         __u8    s_def_hash_version;     /* Default hash version to use */
487         __u8    s_reserved_char_pad;
488         __u16   s_reserved_word_pad;
489         __le32  s_default_mount_opts;
490         __le32  s_first_meta_bg;        /* First metablock block group */
491         __u32   s_reserved[190];        /* Padding to the end of the block */
492 };
493
494 /*
495  * Codes for operating systems
496  */
497 #define EXT2_OS_LINUX           0
498 #define EXT2_OS_HURD            1
499 #define EXT2_OS_MASIX           2
500 #define EXT2_OS_FREEBSD         3
501 #define EXT2_OS_LITES           4
502
503 /*
504  * Revision levels
505  */
506 #define EXT2_GOOD_OLD_REV       0       /* The good old (original) format */
507 #define EXT2_DYNAMIC_REV        1       /* V2 format w/ dynamic inode sizes */
508
509 #define EXT2_CURRENT_REV        EXT2_GOOD_OLD_REV
510 #define EXT2_MAX_SUPP_REV       EXT2_DYNAMIC_REV
511
512 #define EXT2_GOOD_OLD_INODE_SIZE 128
513
514 /*
515  * Feature set definitions
516  */
517
518 #define EXT2_HAS_COMPAT_FEATURE(sb,mask)                        \
519         ( EXT2_SB(sb)->s_es->s_feature_compat & cpu_to_le32(mask) )
520 #define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask)                     \
521         ( EXT2_SB(sb)->s_es->s_feature_ro_compat & cpu_to_le32(mask) )
522 #define EXT2_HAS_INCOMPAT_FEATURE(sb,mask)                      \
523         ( EXT2_SB(sb)->s_es->s_feature_incompat & cpu_to_le32(mask) )
524 #define EXT2_SET_COMPAT_FEATURE(sb,mask)                        \
525         EXT2_SB(sb)->s_es->s_feature_compat |= cpu_to_le32(mask)
526 #define EXT2_SET_RO_COMPAT_FEATURE(sb,mask)                     \
527         EXT2_SB(sb)->s_es->s_feature_ro_compat |= cpu_to_le32(mask)
528 #define EXT2_SET_INCOMPAT_FEATURE(sb,mask)                      \
529         EXT2_SB(sb)->s_es->s_feature_incompat |= cpu_to_le32(mask)
530 #define EXT2_CLEAR_COMPAT_FEATURE(sb,mask)                      \
531         EXT2_SB(sb)->s_es->s_feature_compat &= ~cpu_to_le32(mask)
532 #define EXT2_CLEAR_RO_COMPAT_FEATURE(sb,mask)                   \
533         EXT2_SB(sb)->s_es->s_feature_ro_compat &= ~cpu_to_le32(mask)
534 #define EXT2_CLEAR_INCOMPAT_FEATURE(sb,mask)                    \
535         EXT2_SB(sb)->s_es->s_feature_incompat &= ~cpu_to_le32(mask)
536
537 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC        0x0001
538 #define EXT2_FEATURE_COMPAT_IMAGIC_INODES       0x0002
539 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x0004
540 #define EXT2_FEATURE_COMPAT_EXT_ATTR            0x0008
541 #define EXT2_FEATURE_COMPAT_RESIZE_INO          0x0010
542 #define EXT2_FEATURE_COMPAT_DIR_INDEX           0x0020
543 #define EXT2_FEATURE_COMPAT_ANY                 0xffffffff
544
545 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER     0x0001
546 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE       0x0002
547 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR        0x0004
548 #define EXT2_FEATURE_RO_COMPAT_ANY              0xffffffff
549
550 #define EXT2_FEATURE_INCOMPAT_COMPRESSION       0x0001
551 #define EXT2_FEATURE_INCOMPAT_FILETYPE          0x0002
552 #define EXT3_FEATURE_INCOMPAT_RECOVER           0x0004
553 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x0008
554 #define EXT2_FEATURE_INCOMPAT_META_BG           0x0010
555 #define EXT2_FEATURE_INCOMPAT_ANY               0xffffffff
556
557 #define EXT2_FEATURE_COMPAT_SUPP        EXT2_FEATURE_COMPAT_EXT_ATTR
558 #define EXT2_FEATURE_INCOMPAT_SUPP      (EXT2_FEATURE_INCOMPAT_FILETYPE| \
559                                          EXT2_FEATURE_INCOMPAT_META_BG)
560 #define EXT2_FEATURE_RO_COMPAT_SUPP     (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER| \
561                                          EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
562                                          EXT2_FEATURE_RO_COMPAT_BTREE_DIR)
563 #define EXT2_FEATURE_RO_COMPAT_UNSUPPORTED      ~EXT2_FEATURE_RO_COMPAT_SUPP
564 #define EXT2_FEATURE_INCOMPAT_UNSUPPORTED       ~EXT2_FEATURE_INCOMPAT_SUPP
565
566 /*
567  * Default values for user and/or group using reserved blocks
568  */
569 #define EXT2_DEF_RESUID         0
570 #define EXT2_DEF_RESGID         0
571
572 /*
573  * Default mount options
574  */
575 #define EXT2_DEFM_DEBUG         0x0001
576 #define EXT2_DEFM_BSDGROUPS     0x0002
577 #define EXT2_DEFM_XATTR_USER    0x0004
578 #define EXT2_DEFM_ACL           0x0008
579 #define EXT2_DEFM_UID16         0x0010
580     /* Not used by ext2, but reserved for use by ext3 */
581 #define EXT3_DEFM_JMODE         0x0060 
582 #define EXT3_DEFM_JMODE_DATA    0x0020
583 #define EXT3_DEFM_JMODE_ORDERED 0x0040
584 #define EXT3_DEFM_JMODE_WBACK   0x0060
585
586 /*
587  * Structure of a directory entry
588  */
589
590 struct ext2_dir_entry {
591         __le32  inode;                  /* Inode number */
592         __le16  rec_len;                /* Directory entry length */
593         __le16  name_len;               /* Name length */
594         char    name[];                 /* File name, up to EXT2_NAME_LEN */
595 };
596
597 /*
598  * The new version of the directory entry.  Since EXT2 structures are
599  * stored in intel byte order, and the name_len field could never be
600  * bigger than 255 chars, it's safe to reclaim the extra byte for the
601  * file_type field.
602  */
603 struct ext2_dir_entry_2 {
604         __le32  inode;                  /* Inode number */
605         __le16  rec_len;                /* Directory entry length */
606         __u8    name_len;               /* Name length */
607         __u8    file_type;
608         char    name[];                 /* File name, up to EXT2_NAME_LEN */
609 };
610
611 /*
612  * Ext2 directory file types.  Only the low 3 bits are used.  The
613  * other bits are reserved for now.
614  */
615 enum {
616         EXT2_FT_UNKNOWN         = 0,
617         EXT2_FT_REG_FILE        = 1,
618         EXT2_FT_DIR             = 2,
619         EXT2_FT_CHRDEV          = 3,
620         EXT2_FT_BLKDEV          = 4,
621         EXT2_FT_FIFO            = 5,
622         EXT2_FT_SOCK            = 6,
623         EXT2_FT_SYMLINK         = 7,
624         EXT2_FT_MAX
625 };
626
627 /*
628  * EXT2_DIR_PAD defines the directory entries boundaries
629  *
630  * NOTE: It must be a multiple of 4
631  */
632 #define EXT2_DIR_PAD                    4
633 #define EXT2_DIR_ROUND                  (EXT2_DIR_PAD - 1)
634 #define EXT2_DIR_REC_LEN(name_len)      (((name_len) + 8 + EXT2_DIR_ROUND) & \
635                                          ~EXT2_DIR_ROUND)
636 #define EXT2_MAX_REC_LEN                ((1<<16)-1)
637
638 static inline void verify_offsets(void)
639 {
640 #define A(x,y) BUILD_BUG_ON(x != offsetof(struct ext2_super_block, y));
641         A(EXT2_SB_MAGIC_OFFSET, s_magic);
642         A(EXT2_SB_BLOCKS_OFFSET, s_blocks_count);
643         A(EXT2_SB_BSIZE_OFFSET, s_log_block_size);
644 #undef A
645 }
646
647 /*
648  * ext2 mount options
649  */
650 struct ext2_mount_options {
651         unsigned long s_mount_opt;
652         kuid_t s_resuid;
653         kgid_t s_resgid;
654 };
655
656 /*
657  * second extended file system inode data in memory
658  */
659 struct ext2_inode_info {
660         __le32  i_data[15];
661         __u32   i_flags;
662         __u32   i_faddr;
663         __u8    i_frag_no;
664         __u8    i_frag_size;
665         __u16   i_state;
666         __u32   i_file_acl;
667         __u32   i_dir_acl;
668         __u32   i_dtime;
669
670         /*
671          * i_block_group is the number of the block group which contains
672          * this file's inode.  Constant across the lifetime of the inode,
673          * it is used for making block allocation decisions - we try to
674          * place a file's data blocks near its inode block, and new inodes
675          * near to their parent directory's inode.
676          */
677         __u32   i_block_group;
678
679         /* block reservation info */
680         struct ext2_block_alloc_info *i_block_alloc_info;
681
682         __u32   i_dir_start_lookup;
683 #ifdef CONFIG_EXT2_FS_XATTR
684         /*
685          * Extended attributes can be read independently of the main file
686          * data. Taking i_mutex even when reading would cause contention
687          * between readers of EAs and writers of regular file data, so
688          * instead we synchronize on xattr_sem when reading or changing
689          * EAs.
690          */
691         struct rw_semaphore xattr_sem;
692 #endif
693         rwlock_t i_meta_lock;
694 #ifdef CONFIG_FS_DAX
695         struct rw_semaphore dax_sem;
696 #endif
697
698         /*
699          * truncate_mutex is for serialising ext2_truncate() against
700          * ext2_getblock().  It also protects the internals of the inode's
701          * reservation data structures: ext2_reserve_window and
702          * ext2_reserve_window_node.
703          */
704         struct mutex truncate_mutex;
705         struct inode    vfs_inode;
706         struct list_head i_orphan;      /* unlinked but open inodes */
707 #ifdef CONFIG_QUOTA
708         struct dquot *i_dquot[MAXQUOTAS];
709 #endif
710 };
711
712 #ifdef CONFIG_FS_DAX
713 #define dax_sem_down_write(ext2_inode)  down_write(&(ext2_inode)->dax_sem)
714 #define dax_sem_up_write(ext2_inode)    up_write(&(ext2_inode)->dax_sem)
715 #else
716 #define dax_sem_down_write(ext2_inode)
717 #define dax_sem_up_write(ext2_inode)
718 #endif
719
720 /*
721  * Inode dynamic state flags
722  */
723 #define EXT2_STATE_NEW                  0x00000001 /* inode is newly created */
724
725
726 /*
727  * Function prototypes
728  */
729
730 /*
731  * Ok, these declarations are also in <linux/kernel.h> but none of the
732  * ext2 source programs needs to include it so they are duplicated here.
733  */
734
735 static inline struct ext2_inode_info *EXT2_I(struct inode *inode)
736 {
737         return container_of(inode, struct ext2_inode_info, vfs_inode);
738 }
739
740 /* balloc.c */
741 extern int ext2_bg_has_super(struct super_block *sb, int group);
742 extern unsigned long ext2_bg_num_gdb(struct super_block *sb, int group);
743 extern ext2_fsblk_t ext2_new_block(struct inode *, unsigned long, int *);
744 extern ext2_fsblk_t ext2_new_blocks(struct inode *, unsigned long,
745                                 unsigned long *, int *);
746 extern int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
747                                  unsigned int count);
748 extern void ext2_free_blocks (struct inode *, unsigned long,
749                               unsigned long);
750 extern unsigned long ext2_count_free_blocks (struct super_block *);
751 extern unsigned long ext2_count_dirs (struct super_block *);
752 extern void ext2_check_blocks_bitmap (struct super_block *);
753 extern struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
754                                                     unsigned int block_group,
755                                                     struct buffer_head ** bh);
756 extern void ext2_discard_reservation (struct inode *);
757 extern int ext2_should_retry_alloc(struct super_block *sb, int *retries);
758 extern void ext2_init_block_alloc_info(struct inode *);
759 extern void ext2_rsv_window_add(struct super_block *sb, struct ext2_reserve_window_node *rsv);
760
761 /* dir.c */
762 extern int ext2_add_link (struct dentry *, struct inode *);
763 extern ino_t ext2_inode_by_name(struct inode *, const struct qstr *);
764 extern int ext2_make_empty(struct inode *, struct inode *);
765 extern struct ext2_dir_entry_2 * ext2_find_entry (struct inode *,const struct qstr *, struct page **);
766 extern int ext2_delete_entry (struct ext2_dir_entry_2 *, struct page *);
767 extern int ext2_empty_dir (struct inode *);
768 extern struct ext2_dir_entry_2 * ext2_dotdot (struct inode *, struct page **);
769 extern void ext2_set_link(struct inode *, struct ext2_dir_entry_2 *, struct page *, struct inode *, int);
770
771 /* ialloc.c */
772 extern struct inode * ext2_new_inode (struct inode *, umode_t, const struct qstr *);
773 extern void ext2_free_inode (struct inode *);
774 extern unsigned long ext2_count_free_inodes (struct super_block *);
775 extern void ext2_check_inodes_bitmap (struct super_block *);
776 extern unsigned long ext2_count_free (struct buffer_head *, unsigned);
777
778 /* inode.c */
779 extern struct inode *ext2_iget (struct super_block *, unsigned long);
780 extern int ext2_write_inode (struct inode *, struct writeback_control *);
781 extern void ext2_evict_inode(struct inode *);
782 extern int ext2_get_block(struct inode *, sector_t, struct buffer_head *, int);
783 extern int ext2_setattr (struct dentry *, struct iattr *);
784 extern void ext2_set_inode_flags(struct inode *inode);
785 extern int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
786                        u64 start, u64 len);
787
788 /* ioctl.c */
789 extern long ext2_ioctl(struct file *, unsigned int, unsigned long);
790 extern long ext2_compat_ioctl(struct file *, unsigned int, unsigned long);
791
792 /* namei.c */
793 struct dentry *ext2_get_parent(struct dentry *child);
794
795 /* super.c */
796 extern __printf(3, 4)
797 void ext2_error(struct super_block *, const char *, const char *, ...);
798 extern __printf(3, 4)
799 void ext2_msg(struct super_block *, const char *, const char *, ...);
800 extern void ext2_update_dynamic_rev (struct super_block *sb);
801 extern void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
802                             int wait);
803
804 /*
805  * Inodes and files operations
806  */
807
808 /* dir.c */
809 extern const struct file_operations ext2_dir_operations;
810
811 /* file.c */
812 extern int ext2_fsync(struct file *file, loff_t start, loff_t end,
813                       int datasync);
814 extern const struct inode_operations ext2_file_inode_operations;
815 extern const struct file_operations ext2_file_operations;
816
817 /* inode.c */
818 extern const struct address_space_operations ext2_aops;
819 extern const struct address_space_operations ext2_nobh_aops;
820 extern const struct iomap_ops ext2_iomap_ops;
821
822 /* namei.c */
823 extern const struct inode_operations ext2_dir_inode_operations;
824 extern const struct inode_operations ext2_special_inode_operations;
825
826 /* symlink.c */
827 extern const struct inode_operations ext2_fast_symlink_inode_operations;
828 extern const struct inode_operations ext2_symlink_inode_operations;
829
830 static inline ext2_fsblk_t
831 ext2_group_first_block_no(struct super_block *sb, unsigned long group_no)
832 {
833         return group_no * (ext2_fsblk_t)EXT2_BLOCKS_PER_GROUP(sb) +
834                 le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block);
835 }
836
837 #define ext2_set_bit    __test_and_set_bit_le
838 #define ext2_clear_bit  __test_and_clear_bit_le
839 #define ext2_test_bit   test_bit_le
840 #define ext2_find_first_zero_bit        find_first_zero_bit_le
841 #define ext2_find_next_zero_bit         find_next_zero_bit_le