/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/priv_names.h>
#include <sys/fssnap_if.h>
/*
* This module implements the file system snapshot code, which provides a
* point-in-time image of a file system for the purposes of online backup.
* There are essentially two parts to this project: the driver half and the
* file system half. The driver half is a pseudo device driver called
* "fssnap" that represents the snapshot. Each snapshot is assigned a
* number that corresponds to the minor number of the device, and a control
* device with a high minor number is used to initiate snapshot creation and
* deletion. For all practical purposes the driver half acts like a
* read-only disk device whose contents are exactly the same as the master
* file system at the time the snapshot was created.
*
* The file system half provides interfaces necessary for performing the
* file system dependent operations required to create and delete snapshots
* and a special driver strategy routine that must always be used by the file
* system for snapshots to work correctly.
*
* When a snapshot is to be created, the user utility will send an ioctl to
* the control device of the driver half specifying the file system to be
* snapshotted, the file descriptor of a backing-store file which is used to
* hold old data before it is overwritten, and other snapshot parameters.
* This ioctl is passed on to the file system specified in the original
* ioctl request. The file system is expected to be able to flush
* everything out to make the file system consistent and lock it to ensure
* no changes occur while the snapshot is being created. It then calls
* fssnap_create() to create state for a new snapshot, from which an opaque
* handle is returned with the snapshot locked. Next, the file system must
* populate the "candidate bitmap", which tells the snapshot code which
* "chunks" should be considered for copy-on-write (a chunk is the unit of
* granularity used for copy-on-write, which is independent of the device
* and file system block sizes). This is typically done by scanning the
* file system allocation bitmaps to determine which chunks contain
* allocated blocks in the file system at the time the snapshot was created.
* If a chunk has no allocated blocks, it does not need to be copied before
* being written to. Once the candidate bitmap is populated with
* fssnap_set_candidate(), the file system calls fssnap_create_done() to
* complete the snapshot creation and unlock the snapshot. The file system
* may now be unlocked and modifications to it resumed.
*
* Once a snapshot is created, the file system must perform all writes
* through a special strategy routine, fssnap_strategy(). This strategy
* routine determines whether the chunks contained by the write must be
* copied before being overwritten by consulting the candidate bitmap
* described above, and the "hastrans bitmap" which tells it whether the chunk
* has been copied already or not. If the chunk is a candidate but has not
* been copied, it reads the old data in and adds it to a queue. The
* old data can then be overwritten with the new data. An asynchronous
* task queue is dispatched for each old chunk read in which writes the old
* data to the backing file specified at snapshot creation time. The
* backing file is a sparse file the same size as the file system that
* contains the old data at the offset that data originally had in the
* file system. If the queue containing in-memory chunks gets too large,
* writes to the file system may be throttled by a semaphore until the
* task queues have a chance to push some of the chunks to the backing file.
*
* With the candidate bitmap, the hastrans bitmap, the data on the master
* file system, and the old data in memory and in the backing file, the
* snapshot pseudo-driver can piece together the original file system
* information to satisfy read requests. If the requested chunk is not a
* candidate, it returns a zeroed buffer. If the chunk is a candidate but
* has not been copied it reads it from the master file system. If it is a
* candidate and has been copied, it either copies the data from the
* in-memory queue or it reads it in from the backing file. The result is
* a replication of the original file system that can be backed up, mounted,
* or manipulated by other file system utilities that work on a read-only
* device.
*
* This module is divided into three roughly logical sections:
*
* representing the snapshot itself. These routines are
* prefixed with "snap_".
*
* - The library routines that are defined in fssnap_if.h that
* are used by file systems that use this snapshot implementation.
* These functions are prefixed with "fssnap_" and are called through
* a function vector from the file system.
*
* - The helper routines used by the snapshot driver and the fssnap
* library routines for managing the translation table and other
* useful functions. These routines are all static and are
* prefixed with either "fssnap_" or "transtbl_" if they
* are specifically used for translation table activities.
*/
static int num_snapshots = 0;
/* "tunable" parameters */
/* static function prototypes */
/* snapshot driver */
/* fssnap interface implementations (see fssnap_if.h) */
static void fssnap_strategy_impl(void *, struct buf *);
static void fssnap_set_candidate_impl(void *, chunknumber_t);
static int fssnap_is_candidate_impl(void *, u_offset_t);
static int fssnap_create_done_impl(void *);
static int fssnap_delete_impl(void *);
/* fssnap interface support routines */
static void fssnap_write_taskq(void *);
static void fssnap_create_kstats(snapshot_id_t *, int, const char *,
const char *);
static int fssnap_update_kstat_num(kstat_t *, int);
static void fssnap_delete_kstats(struct cow_info *);
/* translation table prototypes */
static void transtbl_free(cow_map_t *);
/* ************************************************************************ */
/* Device and Module Structures */
nodev, /* no snap_dump */
nodev, /* no snap_write */
nodev, /* no snap_devmap */
nodev, /* no snap_mmap */
nodev, /* no snap_segmap */
NULL, /* streamtab */
nodev, /* async I/O read entry point */
nodev /* async I/O write entry point */
};
0, /* ref count */
nulldev, /* snap_identify obsolete */
nulldev, /* no snap_probe */
nodev, /* no snap_reset */
nulldev, /* no snap_power() */
ddi_quiesce_not_needed, /* quiesce */
};
extern struct mod_ops mod_driverops;
&mod_driverops, /* Type of module. This is a driver */
"snapshot driver", /* Name of the module */
&snap_ops,
};
&md,
};
static void *statep;
int
_init(void)
{
int error;
if (error) {
return (error);
}
if (error) {
return (error);
}
/*
* Fill in the snapshot operations vector for file systems
* (defined in fssnap_if.c)
*/
/*
* Initialize the fssnap highwater kstat
*/
KSTAT_TYPE_NAMED, 1, 0);
} else {
}
return (0);
}
int
{
}
int
_fini(void)
{
int error;
if (error)
return (error);
/*
* delete the fssnap highwater kstat
*/
/* Clear out the file system operations vector */
return (0);
}
/* ************************************************************************ */
/*
* Snapshot Driver Routines
*
* This section implements the snapshot character and block drivers. The
* device will appear to be a consistent read-only file system to
* applications that wish to back it up or mount it. The snapshot driver
* communicates with the file system through the translation table, which
* tells the snapshot driver where to find the data necessary to piece
* together the frozen file system. The data may either be on the master
* device (no translation exists), in memory (a translation exists but has
* not been flushed to the backing store), or in the backing store file.
* The read request may require the snapshot driver to retrieve data from
* several different places and piece it together to look like a single
* contiguous read.
*
* The device minor number corresponds to the snapshot number in the list of
* snapshot identifiers. The soft state for each minor number is simply a
* pointer to the snapshot id, which holds all of the snapshot state. One
* minor number is designated as the control device. All snapshot create
* and delete requests go through the control device to ensure this module
* is properly loaded and attached before the file system starts calling
* routines defined here.
*/
/*
* snap_getinfo() - snapshot driver getinfo(9E) routine
*
*/
/*ARGSUSED*/
static int
{
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = fssnap_dip;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
*result = 0; /* we only have one instance */
return (DDI_SUCCESS);
}
return (DDI_FAILURE);
}
/*
* snap_attach() - snapshot driver attach(9E) routine
*
* sets up snapshot control device and control state. The control state
* is a pointer to an "anonymous" snapshot_id for tracking opens and closes
*/
static int
{
int error;
switch (cmd) {
case DDI_ATTACH:
/* create the control device */
if (error == DDI_FAILURE) {
return (DDI_FAILURE);
}
fssnap_dip = dip;
/* the control sid is not linked into the snapshot list */
return (DDI_SUCCESS);
case DDI_PM_RESUME:
return (DDI_SUCCESS);
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* snap_detach() - snapshot driver detach(9E) routine
*
* destroys snapshot control device and control state. If any snapshots
* are active (ie. num_snapshots != 0), the device will refuse to detach.
*/
static int
{
switch (cmd) {
case DDI_DETACH:
/* do not detach if the device is active */
if ((num_snapshots != 0) ||
return (DDI_FAILURE);
}
/* free up the snapshot list */
}
/* delete the control device */
fssnap_dip = NULL;
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* snap_open() - snapshot driver open(9E) routine
*
* marks the snapshot id as busy so it will not be recycled when deleted
* until the snapshot is closed.
*/
/* ARGSUSED */
static int
{
/* snapshots are read-only */
return (EROFS);
if (minor == SNAP_CTL_MINOR) {
/* control device must be opened exclusively */
return (EINVAL);
return (EBUSY);
}
return (0);
}
return (ENXIO);
return (EAGAIN);
}
/* check to see if this snapshot has been killed on us */
if (SID_INACTIVE(sidp)) {
minor);
return (ENXIO);
}
switch (otyp) {
case OTYP_CHR:
break;
case OTYP_BLK:
break;
default:
return (EINVAL);
}
/*
* at this point if a valid snapshot was found then it has
* been marked busy and we can use it.
*/
return (0);
}
/*
* snap_close() - snapshot driver close(9E) routine
*
* unsets the busy bits in the snapshot id. If the snapshot has been
* deleted while the snapshot device was open, the close call will clean
* up the remaining state information.
*/
/* ARGSUSED */
static int
{
/* if this is the control device, close it and return */
if (minor == SNAP_CTL_MINOR) {
return (0);
}
"snapshot %d.", minor);
return (ENXIO);
}
/* Mark the snapshot as not being busy anymore */
switch (otyp) {
case OTYP_CHR:
break;
case OTYP_BLK:
break;
default:
return (EINVAL);
}
if (SID_AVAILABLE(sidp)) {
/*
* if this is the last close on a snapshot that has been
* deleted, then free up the soft state. The snapdelete
* ioctl does not free this when the device is in use so
* we do it here after the last reference goes away.
*/
/* remove the device nodes */
/* delete the state structure */
}
return (0);
}
/*
* snap_read() - snapshot driver read(9E) routine
*
* reads data from the snapshot by calling snap_strategy() through physio()
*/
/* ARGSUSED */
static int
{
"snap_read: could not find state for snapshot %d.", minor);
return (ENXIO);
}
}
/*
* snap_strategy() - snapshot driver strategy(9E) routine
*
* cycles through each chunk in the requested buffer and calls
* snap_getchunk() on each chunk to retrieve it from the appropriate
* place. Once all of the parts are put together the requested buffer
* is returned. The snapshot driver is read-only, so a write is invalid.
*/
static int
{
int error = 0;
/* snapshot device is read-only */
return (0);
}
"snap_strategy: could not find state for snapshot %d.",
minor);
return (0);
}
if (SID_INACTIVE(sidp)) {
return (0);
}
/* reqptr is the current DEV_BSIZE offset into the device */
/* chunk is the chunk containing reqptr */
/* len is the length of the request (in the current chunk) in bytes */
/* off is the byte offset into the current chunk */
/*
* EINVAL means the user tried to go out of range.
* Anything else means it's likely that we're
* confused.
*/
"calling snap_getchunk, chunk = %llu, "
"offset = %d, len = %d, resid = %lu, "
"error = %d.",
}
return (0);
}
}
return (0);
}
/*
* snap_getchunk() - helper function for snap_strategy()
*
* gets the requested data from the appropriate place and fills in the
* buffer. chunk is the chunk number of the request, offset is the
* offset into that chunk and must be less than the chunk size. len is
* the length of the request starting at offset, and must not exceed a
* chunk boundary. buffer is the address to copy the data to. len
* bytes are copied into the buffer starting at the location specified.
*
* A chunk is located according to the following algorithm:
* - If the chunk does not have a translation or is not a candidate
* for translation, it is read straight from the master device.
* - If the chunk does have a translation, then it is either on
* disk or in memory:
* o If it is in memory the requested data is simply copied out
* of the in-memory buffer.
* o If it is in the backing store, it is read from there.
*
* This function does the real work of the snapshot driver.
*/
static int
{
int error = 0;
char *newbuffer;
int newlen = 0;
int partial = 0;
/*
* Check if the chunk number is out of range and if so bail out
*/
return (EINVAL);
}
/*
* If the chunk is not a candidate for translation, then the chunk
* was not allocated when the snapshot was taken. Since it does
* not contain data associated with this snapshot, just return a
* zero buffer instead.
*/
return (0);
}
/*
* if the chunk is a candidate for translation but a
* translation does not exist, then read through to the
* original file system. The rwlock is held until the read
* completes if it hasn't been translated to make sure the
* file system does not translate the block before we
* access it. If it has already been translated we don't
* need the lock, because the translation will never go away.
*/
/*
* Reading into the buffer saves having to do a copy,
* but gets tricky if the request size is not a
* multiple of DEV_BSIZE. However, we are filling the
* buffer left to right, so future reads will write
* over any extra data we might have read.
*/
if (partial) {
/*
* Partial block read in progress.
* This is bad as modules further down the line
* assume buf's are exact multiples of DEV_BSIZE
* and we end up with fewer, or zero, bytes read.
* To get round this we need to round up to the
* nearest full block read and then return only
* len bytes.
*/
}
(void) bdev_strategy(snapbuf);
if (partial) {
/*
* Partial block read. Now we need to bcopy the
* correct number of bytes back into the
* supplied buffer, and tidy up our temp
* buffer.
*/
}
return (error);
}
/*
* finally, if the chunk is a candidate for translation and it
* has been translated, then we clone the chunk of the buffer
* that was copied aside by the file system.
* The cmap_rwlock does not need to be held after we know the
* data has already been copied. Once a chunk has been copied
* to the backing file, it is stable read only data.
*/
/* check whether the data is in memory or in the backing file */
/* already in memory */
} else {
int bf_index;
/*
* can cause deadlock with writer if we don't drop the
* cmap_rwlock before trying to get the backing store file
* vnode rwlock.
*/
/* read buffer from backing file */
}
return (error);
}
/*
* snap_print() - snapshot driver print(9E) routine
*
* prints the device identification string.
*/
static int
{
"snap_print: could not find state for snapshot %d.", minor);
return (ENXIO);
}
return (0);
}
/*
* snap_prop_op() - snapshot driver prop_op(9E) routine
*
* get 32-bit and 64-bit values for size (character driver) and nblocks
* (block driver).
*/
static int
{
int minor;
int error;
/*
* If this is the control device just check for .conf properties,
* if the wildcard DDI_DEV_T_ANY was passed in via the dev_t
* just fall back to the defaults.
*/
/* check to see if there is a master device plumbed */
"snap_prop_op: could not find state for "
"snapshot %d.", minor);
return (DDI_PROP_NOT_FOUND);
}
/* hold master device and pass operation down */
/* get size information from the master device. */
if (error == DDI_PROP_SUCCESS)
return (error);
}
/* master device did not service the request, try framework */
}
/*
* snap_ioctl() - snapshot driver ioctl(9E) routine
*
* only applies to the control device. The control device accepts two
* ioctl requests: create a snapshot or delete a snapshot. In either
* case, the vnode for the requested file system is extracted, and the
* request is passed on to the file system via the same ioctl. The file
* system is responsible for doing the things necessary for creating or
* destroying a snapshot, including any file system specific operations
* that must be performed as well as setting up and deleting the snapshot
* state through the fssnap interfaces.
*/
static int
int *rvalp)
{
int error = 0;
if (minor != SNAP_CTL_MINOR) {
return (EINVAL);
}
switch (cmd) {
case _FIOSNAPSHOTCREATE:
{
return (EFAULT);
/* get vnode for file system mount point */
return (EBADF);
/* pass ioctl request to file system */
break;
}
case _FIOSNAPSHOTCREATE_MULTI:
{
return (EFAULT);
/* get vnode for file system mount point */
return (EBADF);
/* pass ioctl request to file system */
break;
}
case _FIOSNAPSHOTDELETE:
{
return (EFAULT);
/* get vnode for file system mount point */
return (EBADF);
/*
* pseudo device:
* or
* mount point:
* fssnap -d [/mntpt]
* Note that minor is verified to be equal to SNAP_CTL_MINOR
* at this point which is an invalid minor number.
*/
/* pseudo device: */
break;
}
/* Mount point: */
} else {
break;
}
}
}
/* Verify minor got set correctly above */
if (minor == SNAP_CTL_MINOR) {
return (EINVAL);
}
/*
* Create dummy vfs entry
* to use as a locking semaphore across the IOCTL
* for mount in progress cases...
*/
(vfs_devismounted(dev))) {
return (EBUSY);
}
/*
* Nobody mounted but do not release mount in progress lock
* until IOCTL complete to prohibit a mount sneaking
* in
*/
break;
}
default:
return (EINVAL);
}
return (error);
}
/* ************************************************************************ */
/*
* Translation Table Routines
*
* These support routines implement a simple doubly linked list
* to keep track of chunks that are currently in memory. The maximum
* size of the list is determined by the fssnap_max_mem_chunks variable.
* The cmap_rwlock is used to protect the linkage of the list.
*/
/*
* transtbl_add() - add a node to the translation table
*
* allocates a new node and points it at the buffer passed in. The node
* is added to the beginning of the doubly linked list and the head of
* the list is moved. The cmap_rwlock must be held as a writer through
* this operation.
*/
static cow_map_node_t *
{
/*
* insert new translations at the beginning so cmn_table is always
* the first node.
*/
return (cmnode);
}
/*
* transtbl_get() - look up a node in the translation table
*
* called by the snapshot driver to find data that has been translated.
* The lookup is done by the chunk number, and the node is returned.
* If the node was not found, NULL is returned.
*/
static cow_map_node_t *
{
/* search the translation table */
return (cmn);
}
/* not found */
return (NULL);
}
/*
* transtbl_delete() - delete a node from the translation table
*
* called when a node's data has been written out to disk. The
* cmap_rwlock must be held as a writer for this operation. If the node
* being deleted is the head of the list, then the head is moved to the
* next node. Both the node's data and the node itself are freed.
*/
static void
{
/* if the head of the list is being deleted, then move the head up */
}
/* make previous node's next pointer skip over current node */
}
/* make next node's previous pointer skip over current node */
}
/* free the data and the node */
}
/*
* transtbl_free() - free the entire translation table
*
* called when the snapshot is deleted. This frees all of the nodes in
* the translation table (but not the bitmaps).
*/
static void
{
}
}
/* ************************************************************************ */
/*
* Interface Implementation Routines
*
* The following functions implement snapshot interface routines that are
* called by the file system to create, delete, and use a snapshot. The
* interfaces are defined in fssnap_if.c and are filled in by this driver
* when it is loaded. This technique allows the file system to depend on
* the interface module without having to load the full implementation and
* snapshot device drivers.
*/
/*
* fssnap_strategy_impl() - strategy routine called by the file system
*
* called by the file system to handle copy-on-write when necessary. All
* reads and writes that the file system performs should go through this
* function. If the file system calls the underlying device's strategy
* routine without going through fssnap_strategy() (eg. by calling
* bdev_strategy()), the snapshot may not be consistent.
*
* This function starts by doing significant sanity checking to insure
* the snapshot was not deleted out from under it or deleted and then
* recreated. To do this, it checks the actual pointer passed into it
* (ie. the handle held by the file system). NOTE that the parameter is
* a POINTER TO A POINTER to the snapshot id. Once the snapshot id is
* locked, it knows things are ok and that this snapshot is really for
* this file system.
*
* If the request is a write, fssnap_translate() is called to determine
* whether a copy-on-write is required. If it is a read, the read is
* simply passed on to the underlying device.
*/
static void
{
int error;
/* read requests are always passed through */
(void) bdev_strategy(bp);
return;
}
/*
* Because we were not able to take the snapshot read lock BEFORE
* checking for a snapshot back in the file system, things may have
* drastically changed out from under us. For instance, the snapshot
* may have been deleted, deleted and recreated, or worse yet, deleted
* for this file system but now the snapshot number is in use by another
* file system.
*
* Having a pointer to the file system's snapshot id pointer allows us
* to sanity check most of this, though it assumes the file system is
* keeping track of a pointer to the snapshot_id somewhere.
*/
/*
* if this file system's snapshot was disabled, just pass the
* request through.
*/
(void) bdev_strategy(bp);
return;
}
/*
* Once we have the reader lock the snapshot will not magically go
* away. But things may have changed on us before this so double check.
*/
/*
* if an error was founds somewhere the DELETE flag will be
* set to indicate the snapshot should be deleted and no new
* translations should occur.
*/
(void) fssnap_delete_impl(sidpp);
(void) bdev_strategy(bp);
return;
}
/*
* If the file system is no longer pointing to the snapshot we were
* called with, then it should not attempt to translate this buffer as
* it may be going to a snapshot for a different file system.
* Even if the file system snapshot pointer is still the same, the
* snapshot may have been disabled before we got the reader lock.
*/
(void) bdev_strategy(bp);
return;
}
/*
* At this point we're sure the snapshot will not go away while the
* reader lock is held, and we are reasonably certain that we are
* writing to the correct snapshot.
*/
/*
* fssnap_translate can release the reader lock if it
* has to wait for a semaphore. In this case it is possible
* for the snapshot to be deleted in this time frame. If this
* happens just sent the buf thru to the filesystems device.
*/
(void) bdev_strategy(bp);
return;
}
}
}
/*
* fssnap_translate() - helper function for fssnap_strategy()
*
* performs the actual copy-on-write for write requests, if required.
* This function does the real work of the file system side of things.
*
* It first checks the candidate bitmap to quickly determine whether any
* action is necessary. If the candidate bitmap indicates the chunk was
* allocated when the snapshot was created, then it checks to see whether
* a translation already exists. If a translation already exists then no
* action is required. If the chunk is a candidate for copy-on-write,
* and a translation does not already exist, then the chunk is read in
* and a node is added to the translation table.
*
* Once all of the chunks in the request range have been copied (if they
* needed to be), then the original request can be satisfied and the old
* data can be overwritten.
*/
static int
{
int error;
int throttle_write = 0;
/* make sure the snapshot is active */
/*
* Do not throttle the writes of the fssnap taskq thread and
* the log roll (trans_roll) thread. Furthermore the writes to
* the on-disk log are also not subject to throttling.
* The fssnap_write_taskq thread's write can block on the throttling
* semaphore which leads to self-deadlock as this same thread
* releases the throttling semaphore after completing the IO.
* If the trans_roll thread's write is throttled then we can deadlock
* because the fssnap_taskq_thread which releases the throttling
* semaphore can block waiting for log space which can only be
* released by the trans_roll thread.
*/
/*
* Iterate through all chunks covered by this write and perform the
* copy-aside if necessary. Once all chunks have been safely
* stowed away, the new data may be written in a single sweep.
*
* For each chunk in the range, the following sequence is performed:
* - Is the chunk a candidate for translation?
* o If not, then no translation is necessary, continue
* - If it is a candidate, then does it already have a translation?
* o If so, then no translation is necessary, continue
* - If it is a candidate, but does not yet have a translation,
* then read the old data and schedule an asynchronous taskq
* to write the old data to the backing file.
*
* Once this has been performed over the entire range of chunks, then
* it is safe to overwrite the data that is there.
*
* Note that no lock is required to check the candidate bitmap because
* it never changes once the snapshot is created. The reader lock is
* taken to check the hastrans bitmap since it may change. If it
* turns out a copy is required, then the lock is upgraded to a
* writer, and the bitmap is re-checked as it may have changed while
* the lock was released. Finally, the write lock is held while
* reading the old data to make sure it is not translated out from
* under us.
*
* This locking mechanism should be sufficient to handle multiple
* threads writing to overlapping chunks simultaneously.
*/
/*
* If the cowchunk is outside of the range of our
* candidate maps, then simply break out of the
* loop and pass the I/O through to bdev_strategy.
* This would occur if the file system has grown
* larger since the snapshot was taken.
*/
break;
/*
* If no disk blocks were allocated in this chunk when the
* snapshot was created then no copy-on-write will be
* required. Since this bitmap is read-only no locks are
* necessary.
*/
continue;
}
/*
* If a translation already exists, the data can be written
* through since the old data has already been saved off.
*/
continue;
}
/*
* Throttle translations if there are too many outstanding
* chunks in memory. The semaphore is sema_v'd by the taskq.
*
* You can't keep the sid_rwlock if you would go to sleep.
* This will result in deadlock when someone tries to delete
* the snapshot (wants the sid_rwlock as a writer, but can't
* get it).
*/
if (throttle_write) {
/*
* Now since we released the sid_rwlock the state may
* have transitioned underneath us. so check that again.
*/
return (ENXIO);
}
}
}
/*
* Acquire the lock as a writer and check to see if a
* translation has been added in the meantime.
*/
if (throttle_write)
continue; /* go to the next chunk */
}
/*
* read a full chunk of data from the requested offset rounded
* down to the nearest chunk size.
*/
(void) bdev_strategy(oldbp);
/*
* It's ok to bail in the middle of translating the range
* because the extra copy-asides will not hurt anything
* (except by using extra space in the backing store).
*/
"old data for snapshot %d, chunk %llu, disk block "
if (throttle_write)
return (error);
}
/*
* add the node to the translation table and save a reference
* to pass to the taskq for writing out to the backing file
*/
/*
* Add a reference to the snapshot id so the lower level
* processing (ie. the taskq) can get back to the state
* information.
*/
/*
* schedule the asynchronous write to the backing file
*/
}
/*
* Write new data in place of the old data. At this point all of the
* chunks touched by this write have been copied aside and so the new
* data can be written out all at once.
*/
(void) bdev_strategy(wbp);
return (0);
}
/*
* fssnap_write_taskq() - write in-memory translations to the backing file
*
* writes in-memory translations to the backing file asynchronously. A
* task is dispatched each time a new translation is created. The task
* writes the data to the backing file and removes it from the memory
* list. The throttling semaphore is released only if the particular
* translation was throttled in fssnap_translate.
*/
static void
{
int error;
int bf_index;
/*
* The sid_rwlock does not need to be held here because the taskqs
* are destroyed explicitly by fssnap_delete (with the sid_rwlock
* held as a writer). taskq_destroy() will flush all of the tasks
* out before fssnap_delete frees up all of the structures.
*/
/* if the snapshot was disabled from under us, drop the request. */
if (SID_INACTIVE(sidp)) {
if (release_sem)
return;
}
if ((cmap->cmap_maxsize != 0) &&
"reached the maximum backing file size specified (%llu "
cmap->cmap_maxsize);
if (release_sem)
return;
}
/* perform the write */
"backing file. DELETING SNAPSHOT %d, backing file path "
if (release_sem)
return;
}
/*
* now remove the node and buffer from memory
*/
/* Allow more translations */
if (release_sem)
}
/*
* fssnap_create_impl() - called from the file system to create a new snapshot
*
* allocates and initializes the structures needed for a new snapshot.
* This is called by the file system when it receives an ioctl request to
* create a new snapshot. An unused snapshot identifier is either found
* or created, and eventually returned as the opaque handle the file
* system will use to identify this snapshot. The snapshot number
* associated with the snapshot identifier is the same as the minor
* number for the snapshot device that is used to access that snapshot.
*
* The snapshot can not be used until the candidate bitmap is populated
* by the file system (see fssnap_set_candidate_impl()), and the file
* system finishes the setup process by calling fssnap_create_done().
* Nearly all of the snapshot locks are held for the duration of the
* create, and are not released until fssnap_create_done is called().
*/
static void *
{
int lastsnap;
/*
* Sanity check the parameters we care about
* (we don't care about the informational parameters)
*/
if ((nchunks == 0) ||
return (NULL);
}
/*
* Look for unused snapshot identifiers. Snapshot ids are never
* freed, but deleted snapshot ids will be recycled as needed.
*/
lastsnap = 0;
/*
* The sid_rwlock is taken as a reader initially so that
* activity on each snapshot is not stalled while searching
* for a free snapshot id.
*/
/*
* If the snapshot has been deleted and nobody is using the
* snapshot device than we can reuse this snapshot_id. If
* the snapshot is marked to be deleted (SID_DELETE), then
* it hasn't been deleted yet so don't reuse it.
*/
if (SID_AVAILABLE(sidp))
break; /* This spot is unused, so take it */
}
/*
* add a new snapshot identifier if there are no deleted
* entries. Since it doesn't matter what order the entries
* are in we can just add it to the beginning of the list.
*/
if (sidp) {
/* someone else grabbed it as a writer, try again */
goto findagain;
}
} else {
/* Create a new node if we didn't find an unused one */
}
/* The root vnode is held until snap_delete_impl() is called */
/* allocate and initialize structures */
/*
* Initialize task queues for this snapshot. Only a small number
* of threads are required because they will be serialized on the
*/
/* don't allow tasks to start until after everything is ready */
/* initialize translation table */
SEMA_DEFAULT, NULL);
/*
* allocate one bit per chunk for the bitmaps, round up
*/
/* initialize kstats for this snapshot */
/*
* return with snapshot id rwlock held as a writer until
* fssnap_create_done is called
*/
return (sidp);
}
/*
* fssnap_set_candidate_impl() - mark a chunk as a candidate for copy-on-write
*
* sets a bit in the candidate bitmap that indicates that a chunk is a
* candidate for copy-on-write. Typically, chunks that are allocated on
* the file system at the time the snapshot is taken are candidates,
* while chunks that have no allocated data do not need to be copied.
* Chunks containing metadata must be marked as candidates as well.
*/
static void
{
/* simple bitmap operation for now */
}
/*
* fssnap_is_candidate_impl() - check whether a chunk is a candidate
*
* returns 0 if the chunk is not a candidate and 1 if the chunk is a
* candidate. This can be used by the file system to change behavior for
* chunks that might induce a copy-on-write. The offset is specified in
* bytes since the chunk size may not be known by the file system.
*/
static int
{
/* simple bitmap operation for now */
}
/*
* fssnap_create_done_impl() - complete the snapshot setup process
*
* called when the file system is done populating the candidate bitmap
* and it is ready to start using the snapshot. This routine releases
* the snapshot locks, allows taskq tasks to start processing, and
* creates the device minor nodes associated with the snapshot.
*/
static int
{
/* sid rwlock and cmap rwlock should be taken from fssnap_create */
/* allocate state structure and find new snapshot id */
"snap_ioctl: create: could not allocate "
"state for snapshot %d.", snapnumber);
snapnumber = -1;
goto out;
}
/* create minor node based on snapshot number */
"block minor node for snapshot %d.", snapnumber);
snapnumber = -1;
goto out;
}
"character minor node for snapshot %d.", snapnumber);
snapnumber = -1;
}
out:
/* let the taskq threads start processing */
return (snapnumber);
}
/*
* fssnap_delete_impl() - delete a snapshot
*
* used when a snapshot is no longer needed. This is called by the file
* system when it receives an ioctl request to delete a snapshot. It is
* also called internally when error conditions such as disk full, errors
* writing to the backing file, or backing file maxsize exceeded occur.
* If the snapshot device is busy when the delete request is received,
* all state will be deleted except for the soft state and device files
* associated with the snapshot; they will be deleted when the snapshot
* device is closed.
*
* NOTE this function takes a POINTER TO A POINTER to the snapshot id,
* and expects to be able to set the handle held by the file system to
* NULL. This depends on the file system checking that variable for NULL
* before calling fssnap_strategy().
*/
static int
{
/*
* sidp is guaranteed to be valid if sidpp is valid because
* the snapshot list is append-only.
*/
return (-1);
}
/*
* double check that the snapshot is still valid for THIS file system
*/
return (-1);
}
/*
* Now we know the snapshot is still valid and will not go away
* because we have the write lock. Once the state is transitioned
* to "disabling", the sid_rwlock can be released. Any pending I/O
* waiting for the lock as a reader will check for this state and
* abort without touching data that may be getting freed.
*/
}
/*
* This is pointing into file system specific data! The assumption is
* that fssnap_strategy() gets called from the file system based on
* whether this reference to the snapshot_id is NULL or not. So
* setting this to NULL should disable snapshots for the file system.
*/
/* remove cowinfo */
return (-1);
}
/* destroy task queues first so they don't reference freed data. */
}
}
/* remove cmap */
if (cmap->cmap_candidate)
if (cmap->cmap_hastrans)
if (cmap->cmap_table)
while (cmap->cmap_waiters) {
}
/* remove kstats */
"fssnap_delete_impl: could not find state for snapshot %d.",
}
/*
* Leave the node in the list marked DISABLED so it can be reused
* and avoid many race conditions. Return the snapshot number
* that was deleted.
*/
/*
* If the snapshot is not busy, free the device info now. Otherwise
* the device nodes are freed in snap_close() when the device is
* closed. The sid will not be reused until the device is not busy.
*/
if (SID_AVAILABLE(sidp)) {
/* remove the device nodes */
/* delete the state structure */
}
return (snapnumber);
}
/*
* fssnap_create_kstats() - allocate and initialize snapshot kstats
*
*/
static void
const char *mountpoint, const char *backfilename)
{
/* update the high water mark */
if (fssnap_highwater_kstat == NULL) {
"high water mark kstat.");
return;
}
/* initialize the mount point kstat */
if (mountpoint != NULL) {
"create mount point kstat");
} else {
strlen(mountpoint));
}
} else {
"specified.");
}
/* initialize the backing file kstat */
if (backfilename == NULL) {
} else {
} else {
"create backing file name kstat");
}
}
/* initialize numeric kstats */
"misc", KSTAT_TYPE_NAMED,
sizeof (struct cow_kstat_num) / sizeof (kstat_named_t),
0);
"numeric kstats");
return;
}
/* initialize the static kstats */
}
/*
* fssnap_update_kstat_num() - update a numerical snapshot kstat value
*
*/
int
{
if (rw == KSTAT_WRITE)
return (EACCES);
/* state */
else if (SID_INACTIVE(sidp))
else
/* bfsize */
return (0);
}
/*
* fssnap_delete_kstats() - deallocate snapshot kstats
*
*/
void
{
}
}
}
}