xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_snap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/systm.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/vnode.h>
32*0Sstevel@tonic-gate #include <sys/file.h>
33*0Sstevel@tonic-gate #include <sys/buf.h>
34*0Sstevel@tonic-gate #include <sys/ddi.h>
35*0Sstevel@tonic-gate #include <sys/errno.h>
36*0Sstevel@tonic-gate #include <sys/cmn_err.h>
37*0Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
38*0Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
39*0Sstevel@tonic-gate #include <sys/fs/ufs_snap.h>
40*0Sstevel@tonic-gate #include <sys/fssnap_if.h>
41*0Sstevel@tonic-gate #include <sys/sysmacros.h>
42*0Sstevel@tonic-gate #include <sys/modctl.h>
43*0Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
44*0Sstevel@tonic-gate #include <sys/debug.h>
45*0Sstevel@tonic-gate #include <sys/kmem.h>
46*0Sstevel@tonic-gate #include <sys/inttypes.h>
47*0Sstevel@tonic-gate #include <sys/vfs.h>
48*0Sstevel@tonic-gate #include <sys/disp.h>
49*0Sstevel@tonic-gate #include <sys/atomic.h>
50*0Sstevel@tonic-gate #include <sys/conf.h>
51*0Sstevel@tonic-gate #include <sys/param.h>
52*0Sstevel@tonic-gate #include <sys/policy.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static int ufs_snap_init_backfile(int  *, int, vnode_t ***, struct ufsvfs *);
55*0Sstevel@tonic-gate static void release_backing_vnodes(vnode_t ***, int);
56*0Sstevel@tonic-gate static int ufs_snap_find_candidates(void *, struct ufsvfs *, int);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * Create a snapshot on a file system
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate int
ufs_snap_create(struct vnode * vp,struct fiosnapcreate_multi * fiosnapp,cred_t * cr)62*0Sstevel@tonic-gate ufs_snap_create(struct vnode *vp, struct fiosnapcreate_multi *fiosnapp,
63*0Sstevel@tonic-gate     cred_t *cr)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	int		error = 0;
66*0Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
67*0Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
68*0Sstevel@tonic-gate 	vnode_t		**bfvpp = NULL;
69*0Sstevel@tonic-gate 	struct lockfs	lf;
70*0Sstevel@tonic-gate 	void		*snapid = NULL;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	u_offset_t	nchunks;
73*0Sstevel@tonic-gate 	uint_t		chunksize, fragsperchunk;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	/*
76*0Sstevel@tonic-gate 	 * Only privilege processes can create a snapshot for now.  This
77*0Sstevel@tonic-gate 	 * would be better if it was based on the permissions of the device
78*0Sstevel@tonic-gate 	 * file.
79*0Sstevel@tonic-gate 	 */
80*0Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
81*0Sstevel@tonic-gate 		return (EPERM);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/*
84*0Sstevel@tonic-gate 	 * There is no reason to make a snapshot of a read-only file system
85*0Sstevel@tonic-gate 	 */
86*0Sstevel@tonic-gate 	if (fs->fs_ronly) {
87*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EREADONLY;
88*0Sstevel@tonic-gate 		return (EROFS);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/*
92*0Sstevel@tonic-gate 	 * Initialize the backing files to store old data.  This assumes any
93*0Sstevel@tonic-gate 	 * preallocation and setup has been done already.
94*0Sstevel@tonic-gate 	 * ufs_snap_init_backfile() allocates and returns a pointer to
95*0Sstevel@tonic-gate 	 * a null-terminated array of vnodes in bfvpp.
96*0Sstevel@tonic-gate 	 */
97*0Sstevel@tonic-gate 	error = ufs_snap_init_backfile(fiosnapp->backfiledesc,
98*0Sstevel@tonic-gate 	    fiosnapp->backfilecount, &bfvpp, ufsvfsp);
99*0Sstevel@tonic-gate 	if (error) {
100*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EBACKFILE;
101*0Sstevel@tonic-gate 		return (error);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * File system must be write locked to prevent updates while
106*0Sstevel@tonic-gate 	 * the snapshot is being established.
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 	if ((error = ufs_fiolfss(vp, &lf)) != 0) {
109*0Sstevel@tonic-gate 		release_backing_vnodes(&bfvpp, fiosnapp->backfilecount);
110*0Sstevel@tonic-gate 		return (error);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
114*0Sstevel@tonic-gate 		release_backing_vnodes(&bfvpp, fiosnapp->backfilecount);
115*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EULOCK;
116*0Sstevel@tonic-gate 		return (EINVAL);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
120*0Sstevel@tonic-gate 	lf.lf_flags = 0;
121*0Sstevel@tonic-gate 	lf.lf_comment = NULL;
122*0Sstevel@tonic-gate 	if ((error = ufs_fiolfs(vp, &lf, 1)) != 0) {
123*0Sstevel@tonic-gate 		release_backing_vnodes(&bfvpp, fiosnapp->backfilecount);
124*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EWLOCK;
125*0Sstevel@tonic-gate 		return (EINVAL);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	/*
129*0Sstevel@tonic-gate 	 * File system must be fairly consistent to enable snapshots
130*0Sstevel@tonic-gate 	 */
131*0Sstevel@tonic-gate 	if (fs->fs_clean != FSACTIVE &&
132*0Sstevel@tonic-gate 	    fs->fs_clean != FSSTABLE &&
133*0Sstevel@tonic-gate 	    fs->fs_clean != FSCLEAN &&
134*0Sstevel@tonic-gate 	    fs->fs_clean != FSLOG) {
135*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_ECLEAN;
136*0Sstevel@tonic-gate 		error = EINVAL;
137*0Sstevel@tonic-gate 		goto unlockout;
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	/*
141*0Sstevel@tonic-gate 	 * Only one snapshot is allowed per file system, so error if
142*0Sstevel@tonic-gate 	 * a snapshot is already enabled.
143*0Sstevel@tonic-gate 	 */
144*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot) {
145*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EBUSY;
146*0Sstevel@tonic-gate 		error = EBUSY;
147*0Sstevel@tonic-gate 		goto unlockout;
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/* Tell bio.c how to call our strategy routine.  XXX ugly hack */
151*0Sstevel@tonic-gate 	if (bio_snapshot_strategy == NULL)
152*0Sstevel@tonic-gate 		bio_snapshot_strategy =
153*0Sstevel@tonic-gate 		    (void (*) (void *, buf_t *))fssnap_strategy;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * use chunk size that is passed in, or the file system
157*0Sstevel@tonic-gate 	 * block size if it is zero.  For most cases, the file system
158*0Sstevel@tonic-gate 	 * block size will be reasonably efficient.  A larger
159*0Sstevel@tonic-gate 	 * chunksize uses less memory but may potentially induce more
160*0Sstevel@tonic-gate 	 * I/O copying the larger chunks aside.
161*0Sstevel@tonic-gate 	 */
162*0Sstevel@tonic-gate 	if (fiosnapp->chunksize != 0)
163*0Sstevel@tonic-gate 		chunksize = fiosnapp->chunksize;
164*0Sstevel@tonic-gate 	else
165*0Sstevel@tonic-gate 		chunksize = fs->fs_bsize * 4;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	/*
169*0Sstevel@tonic-gate 	 * compute the number of chunks in this whole file system.  Since
170*0Sstevel@tonic-gate 	 * the UFS allocation bitmaps are in units of fragments, we first
171*0Sstevel@tonic-gate 	 * compute the number of fragments per chunk.  Things work out
172*0Sstevel@tonic-gate 	 * nicer if the chunk size is a power-of-two multiple of the
173*0Sstevel@tonic-gate 	 * fragment size.
174*0Sstevel@tonic-gate 	 */
175*0Sstevel@tonic-gate 	if ((chunksize < fs->fs_fsize) || (chunksize % fs->fs_fsize != 0)) {
176*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_ECHUNKSZ;
177*0Sstevel@tonic-gate 		error = EINVAL;
178*0Sstevel@tonic-gate 		goto unlockout;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	fragsperchunk = chunksize >> fs->fs_fshift;
181*0Sstevel@tonic-gate 	nchunks = (fs->fs_size + fragsperchunk) / fragsperchunk;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/*
184*0Sstevel@tonic-gate 	 * Create and initialize snapshot state and allocate/initialize
185*0Sstevel@tonic-gate 	 * translation table.  This does the real work of taking the snapshot.
186*0Sstevel@tonic-gate 	 */
187*0Sstevel@tonic-gate 	snapid = fssnap_create(nchunks, chunksize, fiosnapp->maxsize, vp,
188*0Sstevel@tonic-gate 	    fiosnapp->backfilecount, bfvpp, fiosnapp->backfilename,
189*0Sstevel@tonic-gate 	    fiosnapp->backfilesize);
190*0Sstevel@tonic-gate 	if (snapid == NULL) {
191*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_ECREATE;
192*0Sstevel@tonic-gate 		error = EINVAL;
193*0Sstevel@tonic-gate 		goto unlockout;
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	error = ufs_snap_find_candidates(snapid, ufsvfsp, chunksize);
197*0Sstevel@tonic-gate 	fiosnapp->snapshotnumber = fssnap_create_done(snapid);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (error) {
200*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufs_snap_create: failed scanning bitmaps, "
201*0Sstevel@tonic-gate 		    "error = %d.", error);
202*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EBITMAP;
203*0Sstevel@tonic-gate 		goto unlockout;
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	ufsvfsp->vfs_snapshot = snapid;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate unlockout:
209*0Sstevel@tonic-gate 	/*
210*0Sstevel@tonic-gate 	 * Unlock the file system
211*0Sstevel@tonic-gate 	 */
212*0Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
213*0Sstevel@tonic-gate 	lf.lf_flags = 0;
214*0Sstevel@tonic-gate 	if ((ufs_fiolfs(vp, &lf, 1) != 0) && !error) {
215*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_ENOULOCK;
216*0Sstevel@tonic-gate 		error = EINVAL;
217*0Sstevel@tonic-gate 	} else {
218*0Sstevel@tonic-gate 		fiosnapp->error = 0;
219*0Sstevel@tonic-gate 	}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/* clean up the snapshot if an error occurred. */
222*0Sstevel@tonic-gate 	if (error && snapid != NULL)
223*0Sstevel@tonic-gate 		(void) fssnap_delete(&snapid);
224*0Sstevel@tonic-gate 	else if (error && bfvpp != NULL)
225*0Sstevel@tonic-gate 		release_backing_vnodes(&bfvpp, fiosnapp->backfilecount);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	return (error);
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate static int
ufs_snap_init_backfile(int * filedesc,int count,vnode_t *** vppp,struct ufsvfs * ufsvfsp)231*0Sstevel@tonic-gate ufs_snap_init_backfile(int *filedesc, int count, vnode_t ***vppp,
232*0Sstevel@tonic-gate     struct ufsvfs *ufsvfsp)
233*0Sstevel@tonic-gate {
234*0Sstevel@tonic-gate 	file_t *fp;
235*0Sstevel@tonic-gate 	vnode_t **vpp;
236*0Sstevel@tonic-gate 	int i;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	vpp = (vnode_t **)kmem_zalloc((count  + 1) * sizeof (vnode_t *),
239*0Sstevel@tonic-gate 	    KM_SLEEP);
240*0Sstevel@tonic-gate 	*vppp = vpp;
241*0Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
242*0Sstevel@tonic-gate 		if ((fp = getf(*filedesc)) == NULL) {
243*0Sstevel@tonic-gate 			release_backing_vnodes(vppp, count);
244*0Sstevel@tonic-gate 			*vppp = NULL;
245*0Sstevel@tonic-gate 			return (EBADF);
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		ASSERT(fp->f_vnode != NULL);
249*0Sstevel@tonic-gate 		VN_HOLD(fp->f_vnode);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 		*vpp = fp->f_vnode;
252*0Sstevel@tonic-gate 		releasef(*filedesc);
253*0Sstevel@tonic-gate 		filedesc++;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		/* make sure the backing file is on a different file system */
256*0Sstevel@tonic-gate 		if ((*vpp)->v_vfsp == ufsvfsp->vfs_vfs) {
257*0Sstevel@tonic-gate 			release_backing_vnodes(vppp, count);
258*0Sstevel@tonic-gate 			*vppp = NULL;
259*0Sstevel@tonic-gate 			return (EINVAL);
260*0Sstevel@tonic-gate 		}
261*0Sstevel@tonic-gate 		vpp++;
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 	return (0);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate static void
release_backing_vnodes(vnode_t *** bvppp,int count)267*0Sstevel@tonic-gate release_backing_vnodes(vnode_t ***bvppp, int count)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	vnode_t **vpp;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	vpp = *bvppp;
272*0Sstevel@tonic-gate 	while (*vpp) {
273*0Sstevel@tonic-gate 		VN_RELE(*vpp);
274*0Sstevel@tonic-gate 		*vpp++ = NULL;
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 	kmem_free(*bvppp, (count + 1) * sizeof (vnode_t *));
277*0Sstevel@tonic-gate 	*bvppp = NULL;
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate static int
ufs_snap_find_candidates(void * snapid,struct ufsvfs * ufsvfsp,int chunksize)281*0Sstevel@tonic-gate ufs_snap_find_candidates(void *snapid, struct ufsvfs *ufsvfsp, int chunksize)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
284*0Sstevel@tonic-gate 	struct buf	*cgbp;	/* cylinder group buffer */
285*0Sstevel@tonic-gate 	struct cg	*cgp;	/* cylinder group data */
286*0Sstevel@tonic-gate 	ulong_t		cg;
287*0Sstevel@tonic-gate 	ulong_t		cgbase;
288*0Sstevel@tonic-gate 	ulong_t		chunk;
289*0Sstevel@tonic-gate 	uchar_t		*blksfree;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	ulong_t		curfrag;
292*0Sstevel@tonic-gate 	int		error = 0;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	/*
295*0Sstevel@tonic-gate 	 * read through each ufs cylinder group and fetch the fragment
296*0Sstevel@tonic-gate 	 * allocation bitmap.  UFS indicates a fragment is allocated by
297*0Sstevel@tonic-gate 	 * a zero bit (not a one bit) in the fragment offset.
298*0Sstevel@tonic-gate 	 */
299*0Sstevel@tonic-gate 	cgbase = 0LL;
300*0Sstevel@tonic-gate 	for (cg = 0; cg < fs->fs_ncg; cg++) {
301*0Sstevel@tonic-gate 		/* read the cylinder group in */
302*0Sstevel@tonic-gate 		cgbp = BREAD(ufsvfsp->vfs_dev,
303*0Sstevel@tonic-gate 		    (daddr_t)fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
304*0Sstevel@tonic-gate 		if ((error = geterror(cgbp)) != 0) {
305*0Sstevel@tonic-gate 			brelse(cgbp);
306*0Sstevel@tonic-gate 			goto errout;
307*0Sstevel@tonic-gate 		}
308*0Sstevel@tonic-gate 		cgp = cgbp->b_un.b_cg;
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 		/* check the magic number */
311*0Sstevel@tonic-gate 		if (cgp->cg_magic != CG_MAGIC) {
312*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "ufs_snap_find_candidates: cg %lu "
313*0Sstevel@tonic-gate 			    "magic number (0x%x) does not match expected "
314*0Sstevel@tonic-gate 			    "magic number (0x%x)", cg, cgp->cg_magic, CG_MAGIC);
315*0Sstevel@tonic-gate 			error = EIO;
316*0Sstevel@tonic-gate 			goto errout;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		blksfree = cg_blksfree(cgp);
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 		/*
322*0Sstevel@tonic-gate 		 * go through the allocation bitmap and set the
323*0Sstevel@tonic-gate 		 * corresponding bit in the candidate map.
324*0Sstevel@tonic-gate 		 */
325*0Sstevel@tonic-gate 		for (curfrag = 0; curfrag < cgp->cg_ndblk; curfrag++) {
326*0Sstevel@tonic-gate 			if (isclr(blksfree, curfrag)) {
327*0Sstevel@tonic-gate 				/*
328*0Sstevel@tonic-gate 				 * this assumes chunksize is a multiple of
329*0Sstevel@tonic-gate 				 * the fragment size
330*0Sstevel@tonic-gate 				 */
331*0Sstevel@tonic-gate 				chunk = (ulong_t)((cgbase + curfrag) /
332*0Sstevel@tonic-gate 				    (chunksize >> fs->fs_fshift));
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 				fssnap_set_candidate(snapid, chunk);
335*0Sstevel@tonic-gate 				/*
336*0Sstevel@tonic-gate 				 * no need to scan the rest of this chunk since
337*0Sstevel@tonic-gate 				 * it is already marked, so skip to the next
338*0Sstevel@tonic-gate 				 */
339*0Sstevel@tonic-gate 				curfrag += ((chunksize >> fs->fs_fshift) -
340*0Sstevel@tonic-gate 				    ((cgbase + curfrag) %
341*0Sstevel@tonic-gate 				    (chunksize >> fs->fs_fshift))) - 1;
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 		cgbase += cgp->cg_ndblk;
346*0Sstevel@tonic-gate 		ASSERT(cgbase <= fs->fs_size);
347*0Sstevel@tonic-gate 		brelse(cgbp);
348*0Sstevel@tonic-gate 	} /* cylinder group loop */
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	ASSERT(cgbase == fs->fs_size);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate errout:
353*0Sstevel@tonic-gate 	return (error);
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate int
ufs_snap_delete(struct vnode * vp,struct fiosnapdelete * fiosnapp,cred_t * cr)358*0Sstevel@tonic-gate ufs_snap_delete(struct vnode *vp, struct fiosnapdelete *fiosnapp, cred_t *cr)
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
361*0Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	/*
364*0Sstevel@tonic-gate 	 * Initialize fields in the user's buffer
365*0Sstevel@tonic-gate 	 */
366*0Sstevel@tonic-gate 	fiosnapp->error = 0;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/*
369*0Sstevel@tonic-gate 	 * No snapshot exists, we're done.
370*0Sstevel@tonic-gate 	 */
371*0Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot == NULL)
372*0Sstevel@tonic-gate 		return (ENOENT);
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	/*
375*0Sstevel@tonic-gate 	 * must have sufficient privileges.
376*0Sstevel@tonic-gate 	 */
377*0Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
378*0Sstevel@tonic-gate 		return (EPERM);
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	/*
381*0Sstevel@tonic-gate 	 * Readonly file system
382*0Sstevel@tonic-gate 	 */
383*0Sstevel@tonic-gate 	if (fs->fs_ronly) {
384*0Sstevel@tonic-gate 		fiosnapp->error = FIOCOW_EREADONLY;
385*0Sstevel@tonic-gate 		return (EROFS);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	/* free the data structures and clear the vfs_snapshot field. */
389*0Sstevel@tonic-gate 	fiosnapp->snapshotnumber = fssnap_delete(&ufsvfsp->vfs_snapshot);
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	if (fiosnapp->snapshotnumber == -1)
392*0Sstevel@tonic-gate 		return (EINVAL);
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	return (0);
395*0Sstevel@tonic-gate }
396