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