Home | History | Annotate | Line # | Download | only in lfs_cleanerd
fdfs.c revision 1.1
      1 /* $NetBSD: fdfs.c,v 1.1 2006/03/30 19:10:13 perseant Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Buffer cache routines for a file-descriptor backed filesystem.
     41  * This is part of lfs_cleanerd so there is also a "segment pointer" that
     42  * we can make buffers out of without duplicating memory or reading the data
     43  * again.
     44  */
     45 
     46 #include <err.h>
     47 #include <fcntl.h>
     48 #include <time.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 #include <sys/syslog.h>
     55 #include <sys/param.h>
     56 #include <sys/mount.h>
     57 #include <sys/stat.h>
     58 
     59 #include "vnode.h"
     60 #include "bufcache.h"
     61 #include "fdfs.h"
     62 
     63 /*
     64  * Return a "vnode" interface to a given file descriptor.
     65  */
     66 struct uvnode *
     67 fd_vget(int fd, int bsize, int segsize, int nseg)
     68 {
     69 	struct fdfs *fs;
     70 	struct uvnode *vp;
     71 	int i;
     72 
     73 	fs = (struct fdfs *)malloc(sizeof(*fs));
     74 	if (fs == NULL)
     75 		return NULL;
     76 	if (segsize > 0) {
     77 		fs->fd_bufp = (struct fd_buf *)malloc(nseg *
     78 						      sizeof(struct fd_buf));
     79 		if (fs->fd_bufp == NULL) {
     80 			free(fs);
     81 			return NULL;
     82 		}
     83 		for (i = 0; i < nseg; i++) {
     84 			fs->fd_bufp[i].start = 0x0;
     85 			fs->fd_bufp[i].end = 0x0;
     86 			fs->fd_bufp[i].buf = (char *)malloc(segsize);
     87 			if (fs->fd_bufp[i].buf == NULL) {
     88 				while (--i >= 0)
     89 					free(fs->fd_bufp[i].buf);
     90 				free(fs->fd_bufp);
     91 				free(fs);
     92 				return NULL;
     93 			}
     94 		}
     95 	} else
     96 		fs->fd_bufp = NULL;
     97 
     98 	fs->fd_fd = fd;
     99 	fs->fd_bufc = nseg;
    100 	fs->fd_bufi = 0;
    101 	fs->fd_bsize = bsize;
    102 	fs->fd_ssize = segsize;
    103 
    104 	vp = (struct uvnode *) malloc(sizeof(*vp));
    105 	memset(vp, 0, sizeof(*vp));
    106 	vp->v_fd = fd;
    107 	vp->v_fs = fs;
    108 	vp->v_usecount = 0;
    109 	vp->v_strategy_op = fd_vop_strategy;
    110 	vp->v_bwrite_op = fd_vop_bwrite;
    111 	vp->v_bmap_op = fd_vop_bmap;
    112 	LIST_INIT(&vp->v_cleanblkhd);
    113 	LIST_INIT(&vp->v_dirtyblkhd);
    114 	vp->v_data = NULL;
    115 
    116 	return vp;
    117 }
    118 
    119 /*
    120  * Deallocate a vnode.
    121  */
    122 void
    123 fd_reclaim(struct uvnode *vp)
    124 {
    125 	int i;
    126 	struct ubuf *bp;
    127 	struct fdfs *fs;
    128 
    129 	LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
    130 		bremfree(bp);
    131 		buf_destroy(bp);
    132 	}
    133 	LIST_FOREACH(bp, &vp->v_cleanblkhd, b_vnbufs) {
    134 		bremfree(bp);
    135 		buf_destroy(bp);
    136 	}
    137 
    138 	fs = (struct fdfs *)vp->v_fs;
    139 	for (i = 0; i < fs->fd_bufc; i++)
    140 		free(fs->fd_bufp[i].buf);
    141 	free(fs->fd_bufp);
    142 	free(fs);
    143 	memset(vp, 0, sizeof(vp));
    144 }
    145 
    146 /*
    147  * We won't be using that last segment after all.
    148  */
    149 void
    150 fd_release(struct uvnode *vp)
    151 {
    152 	--((struct fdfs *)vp->v_fs)->fd_bufi;
    153 }
    154 
    155 /*
    156  * Reset buffer pointer to first buffer.
    157  */
    158 void
    159 fd_release_all(struct uvnode *vp)
    160 {
    161 	((struct fdfs *)vp->v_fs)->fd_bufi = 0;
    162 }
    163 
    164 /*
    165  * Prepare a segment buffer which we will expect to read from.
    166  */
    167 int
    168 fd_preload(struct uvnode *vp, daddr_t start)
    169 {
    170 	struct fdfs *fs = (struct fdfs *)vp->v_fs;
    171 	struct fd_buf *t;
    172 	int r;
    173 
    174 	/* We might need to allocate more buffers. */
    175 	if (fs->fd_bufi == fs->fd_bufc) {
    176 		fs->fd_bufc = fs->fd_bufi + 1;
    177 		syslog(LOG_DEBUG, "increasing number of segment buffers to %d",
    178 			fs->fd_bufc);
    179 		t = realloc(fs->fd_bufp, fs->fd_bufc * sizeof(struct fd_buf));
    180 		if (t == NULL)
    181 			return -1;
    182 		fs->fd_bufp = t;
    183 		fs->fd_bufp[fs->fd_bufi].start = 0x0;
    184 		fs->fd_bufp[fs->fd_bufi].end = 0x0;
    185 		fs->fd_bufp[fs->fd_bufi].buf = (char *)malloc(fs->fd_ssize);
    186 		if (fs->fd_bufp[fs->fd_bufi].buf == NULL) {
    187 			--fs->fd_bufc;
    188 			free(fs->fd_bufp[fs->fd_bufc].buf);
    189 		}
    190 	}
    191 
    192 	/* Read the current buffer. */
    193 	fs->fd_bufp[fs->fd_bufi].start = start;
    194 	fs->fd_bufp[fs->fd_bufi].end =	 start + fs->fd_ssize / fs->fd_bsize;
    195 
    196 	if ((r = pread(fs->fd_fd, fs->fd_bufp[fs->fd_bufi].buf,
    197 		       (size_t)fs->fd_ssize, start * fs->fd_bsize)) < 0) {
    198 		syslog(LOG_ERR, "preload to segment buffer %d", fs->fd_bufi);
    199 		return r;
    200 	}
    201 
    202 	fs->fd_bufi = fs->fd_bufi + 1;
    203 	return 0;
    204 }
    205 
    206 /*
    207  * Get a pointer to a block contained in one of the segment buffers,
    208  * as if from bread() but avoiding the buffer cache.
    209  */
    210 char *
    211 fd_ptrget(struct uvnode *vp, daddr_t bn)
    212 {
    213 	int i;
    214 	struct fdfs *fs;
    215 
    216 	fs = (struct fdfs *)vp->v_fs;
    217 	for (i = 0; i < fs->fd_bufc; i++) {
    218 		if (bn >= fs->fd_bufp[i].start && bn < fs->fd_bufp[i].end) {
    219 			return fs->fd_bufp[i].buf +
    220 				(bn - fs->fd_bufp[i].start) * fs->fd_bsize;
    221 		}
    222 	}
    223 	return NULL;
    224 }
    225 
    226 /*
    227  * Strategy routine.  We can read from the segment buffer if requested.
    228  */
    229 int
    230 fd_vop_strategy(struct ubuf * bp)
    231 {
    232 	struct fdfs *fs;
    233 	char *cp;
    234 	int count;
    235 
    236 	fs = (struct fdfs *)bp->b_vp->v_fs;
    237 	if (bp->b_flags & B_READ) {
    238 		if ((cp = fd_ptrget(bp->b_vp, bp->b_blkno)) != NULL) {
    239 			free(bp->b_data);
    240 			bp->b_data = cp;
    241 			bp->b_flags |= (B_DONTFREE | B_DONE);
    242 			return 0;
    243 		}
    244 		count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    245 			      bp->b_blkno * fs->fd_bsize);
    246 		if (count == bp->b_bcount)
    247 			bp->b_flags |= B_DONE;
    248 	} else {
    249 		count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
    250 			       bp->b_blkno * fs->fd_bsize);
    251 		if (count == 0) {
    252 			perror("pwrite");
    253 			return -1;
    254 		}
    255 		bp->b_flags &= ~B_DELWRI;
    256 		reassignbuf(bp, bp->b_vp);
    257 	}
    258 	return 0;
    259 }
    260 
    261 /*
    262  * Delayed write.
    263  */
    264 int
    265 fd_vop_bwrite(struct ubuf * bp)
    266 {
    267 	bp->b_flags |= B_DELWRI;
    268 	reassignbuf(bp, bp->b_vp);
    269 	brelse(bp);
    270 	return 0;
    271 }
    272 
    273 /*
    274  * Map lbn to disk address.  Since we are using the file
    275  * descriptor as the "disk", the disk address is meaningless
    276  * and we just return the block address.
    277  */
    278 int
    279 fd_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
    280 {
    281 	*daddrp = lbn;
    282 	return 0;
    283 }
    284