xref: /freebsd-src/sys/ufs/ufs/ufs_gjournal.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1 /*-
2  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ufs.h"
31 
32 #ifdef UFS_GJOURNAL
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/vnode.h>
38 #include <sys/lock.h>
39 #include <sys/mount.h>
40 #include <sys/mutex.h>
41 
42 #include <ufs/ufs/extattr.h>
43 #include <ufs/ufs/quota.h>
44 #include <ufs/ufs/inode.h>
45 #include <ufs/ufs/ufsmount.h>
46 #include <ufs/ufs/gjournal.h>
47 
48 #include <ufs/ffs/fs.h>
49 #include <ufs/ffs/ffs_extern.h>
50 
51 /*
52  * Change the number of unreferenced inodes.
53  */
54 static int
55 ufs_gjournal_modref(struct vnode *vp, int count)
56 {
57 	struct cg *cgp;
58 	struct buf *bp;
59 	ufs2_daddr_t cgbno;
60 	int error, cg;
61 	struct cdev *dev;
62 	struct inode *ip;
63 	struct ufsmount *ump;
64 	struct fs *fs;
65 	struct vnode *devvp;
66 	ino_t ino;
67 
68 	ip = VTOI(vp);
69 	ump = ip->i_ump;
70 	fs = ip->i_fs;
71 	devvp = ip->i_devvp;
72 	ino = ip->i_number;
73 
74 	cg = ino_to_cg(fs, ino);
75 	if (devvp->v_type != VCHR) {
76 		/* devvp is a snapshot */
77 		dev = VTOI(devvp)->i_devvp->v_rdev;
78 		cgbno = fragstoblks(fs, cgtod(fs, cg));
79 	} else {
80 		/* devvp is a normal disk device */
81 		dev = devvp->v_rdev;
82 		cgbno = fsbtodb(fs, cgtod(fs, cg));
83 	}
84 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
85 		panic("ufs_gjournal_modref: range: dev = %s, ino = %lu, fs = %s",
86 		    devtoname(dev), (u_long)ino, fs->fs_fsmnt);
87 	if ((error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp))) {
88 		brelse(bp);
89 		return (error);
90 	}
91 	cgp = (struct cg *)bp->b_data;
92 	if (!cg_chkmagic(cgp)) {
93 		brelse(bp);
94 		return (0);
95 	}
96 	bp->b_xflags |= BX_BKGRDWRITE;
97 	cgp->cg_unrefs += count;
98 	UFS_LOCK(ump);
99 	fs->fs_unrefs += count;
100 	fs->fs_fmod = 1;
101 	ACTIVECLEAR(fs, cg);
102 	UFS_UNLOCK(ump);
103 	bdwrite(bp);
104 	return (0);
105 }
106 
107 void
108 ufs_gjournal_orphan(struct vnode *vp)
109 {
110 	struct inode *ip;
111 
112 	if (vp->v_mount->mnt_gjprovider == NULL)
113 		return;
114 	if (vp->v_usecount < 2 || (vp->v_vflag & VV_DELETED))
115 		return;
116 	ip = VTOI(vp);
117 	if ((vp->v_type == VDIR && ip->i_nlink > 2) ||
118 	    (vp->v_type != VDIR && ip->i_nlink > 1)) {
119 		return;
120 	}
121 	vp->v_vflag |= VV_DELETED;
122 
123 	ufs_gjournal_modref(vp, 1);
124 }
125 
126 void
127 ufs_gjournal_close(struct vnode *vp)
128 {
129 	struct inode *ip;
130 
131 	if (vp->v_mount->mnt_gjprovider == NULL)
132 		return;
133 	if (!(vp->v_vflag & VV_DELETED))
134 		return;
135 	ip = VTOI(vp);
136 	if (ip->i_nlink > 0)
137 		return;
138 	ufs_gjournal_modref(vp, -1);
139 }
140 
141 #endif /* UFS_GJOURNAL */
142