xref: /freebsd-13-stable/sys/ufs/ufs/inode.h (revision 6f87402a02f0801942fe8f0d9a8f7f3e04dfbb01)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)inode.h	8.9 (Berkeley) 5/14/95
37  */
38 
39 #ifndef _UFS_UFS_INODE_H_
40 #define	_UFS_UFS_INODE_H_
41 
42 #include <sys/lock.h>
43 #include <sys/queue.h>
44 #include <ufs/ufs/dinode.h>
45 #include <sys/seqc.h>
46 #ifdef DIAGNOSTIC
47 #include <sys/stack.h>
48 #endif
49 
50 /*
51  * This must agree with the definition in <ufs/ufs/dir.h>.
52  */
53 #define	doff_t		int32_t
54 
55 #ifdef DIAGNOSTIC
56 struct iown_tracker {
57 	struct thread	*tr_owner;
58 	struct stack	tr_st;
59 	struct stack	tr_unlock;
60 	int		tr_gen;
61 };
62 #endif
63 
64 /*
65  * The inode is used to describe each active (or recently active) file in the
66  * UFS filesystem. It is composed of two types of information. The first part
67  * is the information that is needed only while the file is active (such as
68  * the identity of the file and linkage to speed its lookup). The second part
69  * is the permanent meta-data associated with the file which is read in
70  * from the permanent dinode from long term storage when the file becomes
71  * active, and is put back when the file is no longer being used.
72  *
73  * An inode may only be changed while holding either the exclusive
74  * vnode lock or the shared vnode lock and the vnode interlock. We use
75  * the latter only for "read" and "get" operations that require
76  * changing i_flag, or a timestamp. This locking protocol allows executing
77  * those operations without having to upgrade the vnode lock from shared to
78  * exclusive.
79  */
80 struct inode {
81 	TAILQ_ENTRY(inode) i_nextsnap; /* Snapshot file list. */
82 	struct vnode	*i_vnode; /* Vnode associated with this inode. */
83 	struct ufsmount	*i_ump; /* Ufsmount point associated with this inode. */
84 	struct dquot	*i_dquot[MAXQUOTAS]; /* Dquot structures. */
85 	union {
86 		struct dirhash *dirhash; /* Hashing for large directories. */
87 		daddr_t *snapblklist;    /* Collect expunged snapshot blocks. */
88 	} i_un;
89 	/*
90 	 * The real copy of the on-disk inode.
91 	 */
92 	union dinodep i_dp;	/* On-disk dinode */
93 
94 	ino_t	  i_number;	/* The identity of the inode. */
95 	uint32_t  i_flag;	/* flags, see below */
96 	int32_t	  i_effnlink;	/* i_nlink when I/O completes */
97 
98 	/*
99 	 * Side effects; used during directory lookup.
100 	 */
101 	int32_t	  i_count;	/* Size of free slot in directory. */
102 	doff_t	  i_endoff;	/* End of useful stuff in directory. */
103 	doff_t	  i_diroff;	/* Offset in dir, where we found last entry. */
104 	doff_t	  i_offset;	/* Offset of free space in directory. */
105 #ifdef DIAGNOSTIC
106 	int			i_lock_gen;
107 	struct iown_tracker	i_count_tracker;
108 	struct iown_tracker	i_endoff_tracker;
109 	struct iown_tracker	i_offset_tracker;
110 #endif
111 
112 	int	i_nextclustercg; /* last cg searched for cluster */
113 
114 	/*
115 	 * Data for extended attribute modification.
116  	 */
117 	uint8_t	  *i_ea_area;	/* Pointer to malloced copy of EA area */
118 	unsigned  i_ea_len;	/* Length of i_ea_area */
119 	int	  i_ea_error;	/* First errno in transaction */
120 	int	  i_ea_refs;	/* Number of users of EA area */
121 
122 	/*
123 	 * Copies from the on-disk dinode itself.
124 	 */
125 	uint64_t i_size;	/* File byte count. */
126 	uint64_t i_gen;		/* Generation number. */
127 	uint32_t i_flags;	/* Status flags (chflags). */
128 	uint32_t i_uid;		/* File owner. */
129 	uint32_t i_gid;		/* File group. */
130 	int32_t  i_nlink;	/* File link count. */
131 	uint16_t i_mode;	/* IFMT, permissions; see below. */
132 };
133 /*
134  * These flags are kept in i_flag.
135  */
136 #define	IN_ACCESS	0x0001		/* Access time update request. */
137 #define	IN_CHANGE	0x0002		/* Inode change time update request. */
138 #define	IN_UPDATE	0x0004		/* Modification time update request. */
139 #define	IN_MODIFIED	0x0008		/* Inode has been modified. */
140 #define	IN_NEEDSYNC	0x0010		/* Inode requires fsync. */
141 #define	IN_LAZYMOD	0x0020		/* Modified, but don't write yet. */
142 #define	IN_LAZYACCESS	0x0040		/* Process IN_ACCESS after the
143 					   suspension finished */
144 #define	IN_EA_LOCKED	0x0080		/* Extended attributes locked */
145 #define	IN_EA_LOCKWAIT	0x0100		/* Want extended attributes lock */
146 #define	IN_TRUNCATED	0x0200		/* Journaled truncation pending. */
147 #define	IN_UFS2		0x0400		/* UFS2 vs UFS1 */
148 #define	IN_IBLKDATA	0x0800		/* datasync requires inode block
149 					   update */
150 #define	IN_SIZEMOD	0x1000		/* Inode size has been modified */
151 #define	IN_ENDOFF	0x2000		/* Free space at the end of directory,
152 					   try to truncate when possible */
153 
154 #define PRINT_INODE_FLAGS "\20\20b16\17b15\16b14\15sizemod" \
155 	"\14iblkdata\13is_ufs2\12truncated\11ea_lockwait\10ea_locked" \
156 	"\7lazyaccess\6lazymod\5needsync\4modified\3update\2change\1access"
157 
158 #define UFS_INODE_FLAG_LAZY_MASK	\
159 	(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_LAZYMOD | \
160 	 IN_LAZYACCESS)
161 /*
162  * Some flags can persist a vnode transitioning to 0 hold count and being tkaen
163  * off the list.
164  */
165 #define UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE \
166 	(UFS_INODE_FLAG_LAZY_MASK & ~(IN_LAZYMOD | IN_LAZYACCESS))
167 
168 #define UFS_INODE_SET_MODE(ip, mode) do {			\
169 	struct inode *_ip = (ip);				\
170 	int _mode = (mode);					\
171 								\
172 	ASSERT_VOP_IN_SEQC(ITOV(_ip));				\
173 	atomic_store_short(&(_ip)->i_mode, _mode);		\
174 } while (0)
175 
176 #define UFS_INODE_SET_FLAG(ip, flags) do {			\
177 	struct inode *_ip = (ip);				\
178 	struct vnode *_vp = ITOV(_ip);				\
179 	int _flags = (flags);					\
180 								\
181 	_ip->i_flag |= _flags;					\
182 	if (_flags & UFS_INODE_FLAG_LAZY_MASK)			\
183 		vlazy(_vp);					\
184 } while (0)
185 
186 #define UFS_INODE_SET_FLAG_SHARED(ip, flags) do {		\
187 	struct inode *_ip = (ip);				\
188 	struct vnode *_vp = ITOV(_ip);				\
189 	int _flags = (flags);					\
190 								\
191 	ASSERT_VI_UNLOCKED(_vp, __func__);			\
192 	if ((_ip->i_flag & (_flags)) != _flags) {		\
193 		VI_LOCK(_vp);					\
194 		_ip->i_flag |= _flags;				\
195 		if (_flags & UFS_INODE_FLAG_LAZY_MASK)		\
196 			vlazy(_vp);				\
197 		VI_UNLOCK(_vp);					\
198 	}							\
199 } while (0)
200 
201 #define	i_dirhash i_un.dirhash
202 #define	i_snapblklist i_un.snapblklist
203 #define	i_din1 i_dp.dp1
204 #define	i_din2 i_dp.dp2
205 
206 #ifdef _KERNEL
207 
208 #define	ITOUMP(ip)	((ip)->i_ump)
209 #define	ITODEV(ip)	(ITOUMP(ip)->um_dev)
210 #define	ITODEVVP(ip)	(ITOUMP(ip)->um_devvp)
211 #define	ITOFS(ip)	(ITOUMP(ip)->um_fs)
212 #define	ITOVFS(ip)	((ip)->i_vnode->v_mount)
213 
214 static inline _Bool
I_IS_UFS1(const struct inode * ip)215 I_IS_UFS1(const struct inode *ip)
216 {
217 
218 	return ((ip->i_flag & IN_UFS2) == 0);
219 }
220 
221 static inline _Bool
I_IS_UFS2(const struct inode * ip)222 I_IS_UFS2(const struct inode *ip)
223 {
224 
225 	return ((ip->i_flag & IN_UFS2) != 0);
226 }
227 
228 /*
229  * The DIP macro is used to access fields in the dinode that are
230  * not cached in the inode itself.
231  */
232 #define	DIP(ip, field)	(I_IS_UFS1(ip) ? (ip)->i_din1->d##field : \
233     (ip)->i_din2->d##field)
234 #define	DIP_SET(ip, field, val) do {				\
235 	if (I_IS_UFS1(ip))					\
236 		(ip)->i_din1->d##field = (val); 		\
237 	else							\
238 		(ip)->i_din2->d##field = (val); 		\
239 	} while (0)
240 #define	DIP_SET_NLINK(ip, val) do {					\
241 	KASSERT(ip->i_nlink >= 0, ("%s:%d %s(): setting negative "	\
242 	    "nlink value %d for inode %jd\n", __FILE__, __LINE__,	\
243 	    __FUNCTION__, (ip)->i_nlink, (ip)->i_number));		\
244 	DIP_SET(ip, i_nlink, val);					\
245 	} while (0)
246 
247 #define	IS_SNAPSHOT(ip)		((ip)->i_flags & SF_SNAPSHOT)
248 #define	IS_UFS(vp)		((vp)->v_data != NULL)
249 
250 /*
251  * Structure used to pass around logical block paths generated by
252  * ufs_getlbns and used by truncate and bmap code.
253  */
254 struct indir {
255 	ufs2_daddr_t in_lbn;		/* Logical block number. */
256 	int	in_off;			/* Offset in buffer. */
257 };
258 
259 /* Convert between inode pointers and vnode pointers. */
260 #define	VTOI(vp)	((struct inode *)(vp)->v_data)
261 #define	VTOI_SMR(vp)	((struct inode *)vn_load_v_data_smr(vp))
262 #define	ITOV(ip)	((ip)->i_vnode)
263 
264 /* Determine if soft dependencies are being done */
265 #define	MOUNTEDSOFTDEP(mp)	(((mp)->mnt_flag & MNT_SOFTDEP) != 0)
266 #define	DOINGSOFTDEP(vp)	MOUNTEDSOFTDEP((vp)->v_mount)
267 #define	MOUNTEDSUJ(mp)		(((mp)->mnt_flag & (MNT_SOFTDEP | MNT_SUJ)) == \
268     (MNT_SOFTDEP | MNT_SUJ))
269 #define	DOINGSUJ(vp)		MOUNTEDSUJ((vp)->v_mount)
270 
271 /* This overlays the fid structure (see mount.h). */
272 struct ufid {
273 	uint16_t ufid_len;	/* Length of structure. */
274 	uint16_t ufid_pad;	/* Force 32-bit alignment. */
275 	uint32_t  ufid_ino;	/* File number (ino). */
276 	uint32_t  ufid_gen;	/* Generation number. */
277 };
278 
279 #ifdef DIAGNOSTIC
280 void ufs_init_trackers(struct inode *ip);
281 void ufs_unlock_tracker(struct inode *ip);
282 
283 doff_t ufs_get_i_offset(struct inode *ip, const char *file, int line);
284 void ufs_set_i_offset(struct inode *ip, doff_t off, const char *file, int line);
285 #define	I_OFFSET(ip)		ufs_get_i_offset(ip, __FILE__, __LINE__)
286 #define	SET_I_OFFSET(ip, off)	ufs_set_i_offset(ip, off, __FILE__, __LINE__)
287 
288 int32_t ufs_get_i_count(struct inode *ip, const char *file, int line);
289 void ufs_set_i_count(struct inode *ip, int32_t cnt, const char *file, int line);
290 #define	I_COUNT(ip)		ufs_get_i_count(ip, __FILE__, __LINE__)
291 #define	SET_I_COUNT(ip, cnt)	ufs_set_i_count(ip, cnt, __FILE__, __LINE__)
292 
293 doff_t ufs_get_i_endoff(struct inode *ip, const char *file, int line);
294 void ufs_set_i_endoff(struct inode *ip, doff_t off, const char *file, int line);
295 #define	I_ENDOFF(ip)		ufs_get_i_endoff(ip, __FILE__, __LINE__)
296 #define	SET_I_ENDOFF(ip, off)	ufs_set_i_endoff(ip, off, __FILE__, __LINE__)
297 
298 #else
299 #define	I_OFFSET(ip)		((ip)->i_offset)
300 #define	SET_I_OFFSET(ip, off)	((ip)->i_offset = (off))
301 #define	I_COUNT(ip)		((ip)->i_count)
302 #define	SET_I_COUNT(ip, cnt)	((ip)->i_count = cnt)
303 #define	I_ENDOFF(ip)		((ip)->i_endoff)
304 #define	SET_I_ENDOFF(ip, off)	((ip)->i_endoff = off)
305 #endif
306 
307 #endif /* _KERNEL */
308 
309 #endif /* !_UFS_UFS_INODE_H_ */
310