xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 55807)
1 /*
2  * Copyright (c) 1986, 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_inode.c	7.77 (Berkeley) 08/01/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/mount.h>
13 #include <sys/proc.h>
14 #include <sys/file.h>
15 #include <sys/buf.h>
16 #include <sys/vnode.h>
17 #include <sys/kernel.h>
18 #include <sys/malloc.h>
19 
20 #include <vm/vm.h>
21 
22 #include <ufs/ufs/quota.h>
23 #include <ufs/ufs/inode.h>
24 #include <ufs/ufs/ufsmount.h>
25 #include <ufs/ufs/ufs_extern.h>
26 
27 #include <ufs/lfs/lfs.h>
28 #include <ufs/lfs/lfs_extern.h>
29 
30 int
31 lfs_init()
32 {
33 	return (ufs_init());
34 }
35 
36 /* Search a block for a specific dinode. */
37 struct dinode *
38 lfs_ifind(fs, ino, dip)
39 	struct lfs *fs;
40 	ino_t ino;
41 	register struct dinode *dip;
42 {
43 	register int cnt;
44 	register struct dinode *ldip;
45 
46 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
47 		if (ldip->di_inum == ino)
48 			return (ldip);
49 
50 	panic("lfs_ifind: dinode %u not found", ino);
51 	/* NOTREACHED */
52 }
53 
54 int
55 lfs_update(ap)
56 	struct vop_update_args /* {
57 		struct vnode *a_vp;
58 		struct timeval *a_ta;
59 		struct timeval *a_tm;
60 		int a_waitfor;
61 	} */ *ap;
62 {
63 	struct vnode *vp = ap->a_vp;
64 	struct inode *ip;
65 
66 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
67 		return (0);
68 	ip = VTOI(vp);
69 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
70 		return (0);
71 	if (ip->i_flag&IACC)
72 		ip->i_atime.ts_sec = ap->a_ta->tv_sec;
73 	if (ip->i_flag&IUPD) {
74 		ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
75 		(ip)->i_modrev++;
76 	}
77 	if (ip->i_flag&ICHG)
78 		ip->i_ctime.ts_sec = time.tv_sec;
79 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
80 
81 	/* Push back the vnode and any dirty blocks it may have. */
82 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
83 }
84 
85 /* Update segment usage information when removing a block. */
86 #define UPDATE_SEGUSE \
87 	if (lastseg != -1) { \
88 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
89 		if ((num << fs->lfs_bshift) > sup->su_nbytes) \
90 			panic("lfs_truncate: negative bytes in segment %d\n", \
91 			    lastseg); \
92 		sup->su_nbytes -= num << fs->lfs_bshift; \
93 		LFS_UBWRITE(sup_bp); \
94 		blocksreleased += num; \
95 	}
96 
97 #define SEGDEC { \
98 	if (daddr != UNASSIGNED) { \
99 		if (lastseg != (seg = datosn(fs, daddr))) { \
100 			UPDATE_SEGUSE; \
101 			num = 1; \
102 			lastseg = seg; \
103 		} else \
104 			++num; \
105 	} \
106 }
107 
108 /*
109  * Truncate the inode ip to at most length size.  Update segment usage
110  * table information.
111  */
112 /* ARGSUSED */
113 int
114 lfs_truncate(ap)
115 	struct vop_truncate_args /* {
116 		struct vnode *a_vp;
117 		off_t a_length;
118 		int a_flags;
119 		struct ucred *a_cred;
120 		struct proc *a_p;
121 	} */ *ap;
122 {
123 	register INDIR *inp;
124 	register int i;
125 	register daddr_t *daddrp;
126 	register struct vnode *vp = ap->a_vp;
127 	off_t length = ap->a_length;
128 	struct buf *bp, *sup_bp;
129 	struct timeval tv;
130 	struct ifile *ifp;
131 	struct inode *ip;
132 	struct lfs *fs;
133 	INDIR a[NIADDR + 2], a_end[NIADDR + 2];
134 	SEGUSE *sup;
135 	daddr_t daddr, lastblock, lbn, olastblock;
136 	long off, blocksreleased;
137 	int e1, e2, depth, lastseg, num, offset, seg, size;
138 
139 	ip = VTOI(vp);
140 	tv = time;
141 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
142 #ifdef DIAGNOSTIC
143 		if (length != 0)
144 			panic("lfs_truncate: partial truncate of symlink");
145 #endif
146 		bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
147 		ip->i_size = 0;
148 		ip->i_flag |= ICHG|IUPD;
149 		return (VOP_UPDATE(vp, &tv, &tv, 0));
150 	}
151 	vnode_pager_setsize(vp, (u_long)length);
152 
153 	fs = ip->i_lfs;
154 
155 	/* If length is larger than the file, just update the times. */
156 	if (ip->i_size <= length) {
157 		ip->i_flag |= ICHG|IUPD;
158 		return (VOP_UPDATE(vp, &tv, &tv, 0));
159 	}
160 
161 	/*
162 	 * Calculate index into inode's block list of last direct and indirect
163 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
164 	 * file is truncated to 0.
165 	 */
166 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
167 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
168 
169 	/*
170 	 * Update the size of the file. If the file is not being truncated to
171 	 * a block boundry, the contents of the partial block following the end
172 	 * of the file must be zero'ed in case it ever become accessable again
173 	 * because of subsequent file growth.
174 	 */
175 	offset = blkoff(fs, length);
176 	if (offset == 0)
177 		ip->i_size = length;
178 	else {
179 		lbn = lblkno(fs, length);
180 #ifdef QUOTA
181 		if (e1 = getinoquota(ip))
182 			return (e1);
183 #endif
184 		if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
185 			return (e1);
186 		ip->i_size = length;
187 		size = blksize(fs);
188 		(void)vnode_pager_uncache(vp);
189 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
190 		allocbuf(bp, size);
191 		LFS_UBWRITE(bp);
192 	}
193 	/*
194 	 * Modify sup->su_nbyte counters for each deleted block; keep track
195 	 * of number of blocks removed for ip->i_blocks.
196 	 */
197 	blocksreleased = 0;
198 	num = 0;
199 	lastseg = -1;
200 
201 	for (lbn = olastblock; lbn >= lastblock;) {
202 		lfs_bmaparray(vp, lbn, &daddr, a, &depth);
203 		if (lbn == olastblock)
204 			for (i = NIADDR + 2; i--;)
205 				a_end[i] = a[i];
206 		switch (depth) {
207 		case 0:				/* Direct block. */
208 			daddr = ip->i_db[lbn];
209 			SEGDEC;
210 			ip->i_db[lbn] = 0;
211 			--lbn;
212 			break;
213 #ifdef DIAGNOSTIC
214 		case 1:				/* An indirect block. */
215 			panic("lfs_truncate: lfs_bmaparray returned depth 1");
216 			/* NOTREACHED */
217 #endif
218 		default:			/* Chain of indirect blocks. */
219 			inp = a + --depth;
220 			if (inp->in_off > 0 && lbn != lastblock) {
221 				lbn -= inp->in_off < lbn - lastblock ?
222 				    inp->in_off : lbn - lastblock;
223 				break;
224 			}
225 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
226 			    --inp, --depth) {
227 				/*
228 				 * XXX
229 				 * The indirect block may not yet exist, so
230 				 * bread will create one just so we can free
231 				 * it.
232 				 */
233 				if (bread(vp,
234 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
235 					panic("lfs_truncate: bread bno %d",
236 					    inp->in_lbn);
237 				daddrp = bp->b_un.b_daddr + inp->in_off;
238 				for (i = inp->in_off;
239 				    i++ <= a_end[depth].in_off;) {
240 					daddr = *daddrp++;
241 					SEGDEC;
242 				}
243 				a_end[depth].in_off = NINDIR(fs) - 1;
244 				if (inp->in_off == 0)
245 					brelse (bp);
246 				else {
247 					bzero(bp->b_un.b_daddr + inp->in_off,
248 					    fs->lfs_bsize -
249 					    inp->in_off * sizeof(daddr_t));
250 					LFS_UBWRITE(bp);
251 				}
252 			}
253 			if (depth == 0 && a[1].in_off == 0) {
254 				off = a[0].in_off;
255 				daddr = ip->i_ib[off];
256 				SEGDEC;
257 				ip->i_ib[off] = 0;
258 			}
259 			if (lbn == lastblock || lbn <= NDADDR)
260 				--lbn;
261 			else {
262 				lbn -= NINDIR(fs);
263 				if (lbn < lastblock)
264 					lbn = lastblock;
265 			}
266 		}
267 	}
268 	UPDATE_SEGUSE;
269 
270 	/* If truncating the file to 0, update the version number. */
271 	if (length == 0) {
272 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
273 		++ifp->if_version;
274 		LFS_UBWRITE(bp);
275 	}
276 
277 	ip->i_blocks -= btodb(blocksreleased << fs->lfs_bshift);
278 	fs->lfs_bfree +=  btodb(blocksreleased << fs->lfs_bshift);
279 #ifdef DIAGNOSTIC
280 	if (ip->i_blocks < 0)
281 		panic("lfs_truncate: block count < 0");
282 #endif
283 	ip->i_flag |= ICHG|IUPD;
284 	e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p);
285 	e2 = VOP_UPDATE(vp, &tv, &tv, 0);
286 	return (e1 ? e1 : e2 ? e2 : 0);
287 }
288