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