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