Home | History | Annotate | Line # | Download | only in dump
snapshot.c revision 1.4.4.1
      1  1.4.4.1   bouyer /*	$NetBSD: snapshot.c,v 1.4.4.1 2011/06/18 17:00:25 bouyer Exp $	*/
      2      1.1  hannken 
      3      1.1  hannken /*-
      4      1.2  hannken  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5      1.1  hannken  * All rights reserved.
      6      1.1  hannken  *
      7      1.1  hannken  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  hannken  * by Juergen Hannken-Illjes.
      9      1.1  hannken  *
     10      1.1  hannken  * Redistribution and use in source and binary forms, with or without
     11      1.1  hannken  * modification, are permitted provided that the following conditions
     12      1.1  hannken  * are met:
     13      1.1  hannken  * 1. Redistributions of source code must retain the above copyright
     14      1.1  hannken  *    notice, this list of conditions and the following disclaimer.
     15      1.1  hannken  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  hannken  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  hannken  *    documentation and/or other materials provided with the distribution.
     18      1.1  hannken  *
     19      1.1  hannken  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  hannken  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  hannken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  hannken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  hannken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  hannken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  hannken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  hannken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  hannken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  hannken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  hannken  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  hannken  */
     31      1.1  hannken 
     32      1.1  hannken #include <sys/param.h>
     33      1.1  hannken #include <sys/ioctl.h>
     34      1.1  hannken #include <sys/mount.h>
     35      1.1  hannken #include <sys/stat.h>
     36      1.1  hannken 
     37      1.1  hannken #include <dev/fssvar.h>
     38      1.1  hannken 
     39      1.1  hannken #include <errno.h>
     40      1.1  hannken #include <fcntl.h>
     41      1.1  hannken #include <stdio.h>
     42      1.1  hannken #include <stdlib.h>
     43      1.3  hannken #include <string.h>
     44      1.1  hannken #include <unistd.h>
     45      1.1  hannken 
     46      1.1  hannken #include "snapshot.h"
     47      1.1  hannken 
     48      1.1  hannken /*
     49      1.1  hannken  * Create a snapshot of the file system currently mounted on the first argument
     50      1.1  hannken  * using the second argument as backing store and return an open file
     51      1.1  hannken  * descriptor for the snapshot.  If the second argument is NULL, use the first
     52      1.1  hannken  * as backing store.  If the third argument is not NULL, it gets the time the
     53      1.3  hannken  * snapshot was created.  If the fourth argument is not NULL, it gets the
     54      1.3  hannken  * snapshot device path.
     55      1.1  hannken  */
     56      1.1  hannken int
     57      1.3  hannken snap_open(char *mountpoint, char *backup, time_t *snap_date, char **snap_dev)
     58      1.1  hannken {
     59  1.4.4.1   bouyer 	int i, fd, israw, fsinternal, dounlink;
     60      1.1  hannken 	char path[MAXPATHLEN], fss_dev[14];
     61      1.1  hannken 	dev_t mountdev;
     62      1.1  hannken 	struct fss_set fss;
     63      1.1  hannken 	struct fss_get fsg;
     64      1.1  hannken 	struct stat sb;
     65      1.1  hannken 	struct statvfs fsb;
     66      1.1  hannken 
     67      1.1  hannken 	dounlink = 0;
     68      1.1  hannken 	fd = -1;
     69      1.1  hannken 
     70      1.1  hannken 	fss.fss_mount = mountpoint;
     71      1.1  hannken 	fss.fss_bstore = backup ? backup : fss.fss_mount;
     72      1.1  hannken 	fss.fss_csize = 0;
     73      1.1  hannken 
     74      1.1  hannken 	if (stat(fss.fss_mount, &sb) < 0)
     75      1.1  hannken 		goto fail;
     76      1.1  hannken 	mountdev = sb.st_dev;
     77      1.1  hannken 
     78      1.1  hannken 	/*
     79      1.1  hannken 	 * Prepare the backing store. `backup' is either a raw device,
     80      1.1  hannken 	 * a file or a directory.  If it is a file, it must not exist.
     81      1.1  hannken 	 */
     82      1.1  hannken 	israw = 0;
     83      1.1  hannken 	if (stat(fss.fss_bstore, &sb) == 0) {
     84      1.1  hannken 		if (S_ISDIR(sb.st_mode)) {
     85      1.1  hannken 			snprintf(path, sizeof(path),
     86      1.1  hannken 			    "%s/XXXXXXXXXX", fss.fss_bstore);
     87      1.1  hannken 			fd = mkstemp(path);
     88      1.1  hannken 			fss.fss_bstore = path;
     89      1.1  hannken 			dounlink = 1;
     90      1.1  hannken 		} else if (S_ISCHR(sb.st_mode)) {
     91      1.1  hannken 			fd = open(fss.fss_bstore, O_RDWR);
     92      1.1  hannken 			israw = 1;
     93      1.1  hannken 		} else
     94      1.1  hannken 			goto fail;
     95      1.1  hannken 	} else {
     96      1.1  hannken 		fd = open(fss.fss_bstore, O_CREAT|O_EXCL|O_WRONLY, 0600);
     97      1.1  hannken 		dounlink = 1;
     98      1.1  hannken 	}
     99      1.1  hannken 	if (fd < 0)
    100      1.1  hannken 		goto fail;
    101      1.1  hannken 
    102      1.1  hannken 	if (fstat(fd, &sb) < 0)
    103      1.1  hannken 		goto fail;
    104      1.1  hannken 	fsinternal = (!israw && sb.st_dev == mountdev);
    105      1.1  hannken 
    106      1.1  hannken 	/*
    107      1.1  hannken 	 * If the backing store is a plain file and the snapshot
    108      1.1  hannken 	 * is not file system internal, truncate to file system
    109      1.1  hannken 	 * free space.
    110      1.1  hannken 	 */
    111      1.1  hannken 	if (!israw && !fsinternal) {
    112      1.1  hannken 		if (statvfs(fss.fss_bstore, &fsb) < 0)
    113      1.1  hannken 			goto fail;
    114      1.1  hannken 		if (ftruncate(fd, (off_t)fsb.f_frsize*fsb.f_bavail) < 0)
    115      1.1  hannken 			goto fail;
    116      1.1  hannken 	}
    117      1.1  hannken 
    118      1.1  hannken 	if (close(fd) < 0)
    119      1.1  hannken 		goto fail;
    120      1.1  hannken 
    121  1.4.4.1   bouyer 	fss.fss_flags = FSS_UNCONFIG_ON_CLOSE;
    122  1.4.4.1   bouyer 	if (dounlink)
    123  1.4.4.1   bouyer 		fss.fss_flags |= FSS_UNLINK_ON_CREATE;
    124      1.1  hannken 	/*
    125      1.1  hannken 	 * Create the snapshot on the first free snapshot device.
    126      1.1  hannken 	 */
    127      1.1  hannken 	for (i = 0; ; i++) {
    128      1.1  hannken 		snprintf(fss_dev, sizeof(fss_dev), "/dev/rfss%d", i);
    129      1.1  hannken 		if ((fd = open(fss_dev, O_RDWR, 0)) < 0)
    130      1.1  hannken 			goto fail;
    131      1.1  hannken 
    132      1.1  hannken 		if (ioctl(fd, FSSIOCSET, &fss) < 0) {
    133      1.1  hannken 			if (errno != EBUSY)
    134      1.1  hannken 				goto fail;
    135      1.1  hannken 			close(fd);
    136      1.1  hannken 			fd = -1;
    137      1.1  hannken 			continue;
    138      1.1  hannken 		}
    139  1.4.4.1   bouyer 		dounlink = 0;
    140      1.1  hannken 
    141      1.3  hannken 		if (snap_dev != NULL) {
    142      1.3  hannken 			*snap_dev = strdup(fss_dev);
    143      1.3  hannken 			if (*snap_dev == NULL) {
    144      1.3  hannken 				ioctl(fd, FSSIOCCLR);
    145      1.3  hannken 				goto fail;
    146      1.3  hannken 			}
    147      1.3  hannken 		}
    148      1.3  hannken 
    149  1.4.4.1   bouyer 		if (ioctl(fd, FSSIOCGET, &fsg) < 0) {
    150      1.1  hannken 			ioctl(fd, FSSIOCCLR);
    151      1.1  hannken 			goto fail;
    152      1.1  hannken 		}
    153      1.1  hannken 
    154      1.1  hannken 		if (snap_date != NULL)
    155      1.1  hannken 			*snap_date = fsg.fsg_time.tv_sec;
    156      1.1  hannken 		return fd;
    157      1.1  hannken 	}
    158      1.1  hannken 
    159      1.1  hannken fail:
    160      1.1  hannken 	if (dounlink)
    161      1.1  hannken 		unlink(fss.fss_bstore);
    162      1.1  hannken 	if (fd >= 0)
    163      1.1  hannken 		close(fd);
    164      1.1  hannken 
    165      1.1  hannken 	return -1;
    166      1.1  hannken }
    167