inode.c (3998B)
1 /* $Id: //depot/blt/srv/vfs/drivers/ffs/inode.c#2 $ 2 ** 3 ** Copyright 1999 Sidney Cammeresi 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. Redistributions in binary form must reproduce the above copyright 12 ** notice, this list of conditions, and the following disclaimer in the 13 ** documentation and/or other materials provided with the distribution. 14 ** 3. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include "vfs-int.h" 33 34 #ifndef VFS_SANDBOX 35 #include <blt/blkdev.h> 36 #else 37 #include "../../sandbox/blkdev.h" 38 #endif 39 40 #include "ffs.h" 41 #include "ffs-blt.h" 42 #include "dinode.h" 43 #include "dir.h" 44 45 int ffs_read_vnode (struct vnode *vnode) 46 { 47 char *buf; 48 int block, offset; 49 struct ffs_super *fs; 50 struct ffs_super_data *data; 51 struct ffs_dinode *di; 52 53 #ifdef FFS_DEBUG 54 printf ("ffs_read_vnode %lld\n", vnode->v_vnid); 55 #endif 56 data = vnode->v_sb->sb_data; 57 fs = data->sbbuf; 58 vnode->v_data = di = malloc (sizeof (struct ffs_dinode)); 59 buf = malloc (BLKSIZE); 60 block = fsbtodb (fs, ino_to_fsba (fs, (int) vnode->v_vnid)); 61 offset = ino_to_fsbo (fs, (int) vnode->v_vnid); 62 blk_read (data->dev, buf, block, BLKSIZE / data->dev->blksize); 63 memcpy (di, (struct ffs_dinode *) buf + offset, sizeof (struct ffs_dinode)); 64 free (buf); 65 return 0; 66 } 67 68 void ffs_drop_vnode (struct vnode *vnode) 69 { 70 #ifdef FFS_DEBUG 71 printf ("ffs_drop_vnode %lld\n", vnode->v_vnid); 72 #endif 73 free (vnode->v_data); 74 } 75 76 static struct vnode *ffs_walk_one (struct vnode *parent, const char *path) 77 { 78 char *buf; 79 int i, offset; 80 struct ffs_super *fs; 81 struct ffs_super_data *data; 82 struct ffs_dinode *di; 83 struct ffs_direct *direct; 84 85 #ifdef FFS_DEBUG 86 printf ("ffs_walk_one %s\n", path); 87 #endif 88 data = parent->v_sb->sb_data; 89 fs = data->sbbuf; 90 di = parent->v_data; 91 buf = malloc (BLKSIZE); 92 93 for (i = 0; i < NDADDR; i++) 94 if (di->di_db[i]) 95 { 96 blk_read (data->dev, buf, fsbtodb (fs, di->di_db[i]), 97 BLKSIZE / data->dev->blksize); 98 for (offset = 0; offset < BLKSIZE; offset += direct->d_reclen) 99 { 100 direct = (struct ffs_direct *) (buf + offset); 101 if (!strcmp (direct->d_name, path)) 102 return vget (parent->v_sb, direct->d_ino); 103 } 104 } 105 printf ("ffs_walk_one: failage 1!\n"); 106 107 for (i = 0; i < NIADDR; i++) 108 if (di->di_ib[i]) 109 { 110 printf ("ffs_walk_one: indirect %d\n", di->di_ib[i]); 111 } 112 113 free (buf); 114 printf ("ffs_walk_one: failage 2!\n"); 115 return NULL; 116 } 117 118 struct vnode *ffs_walk (struct vnode *parent, const char *path) 119 { 120 char *name; 121 int i, j, len; 122 struct vnode *vn, *vnnext; 123 124 #ifdef FFS_DEBUG 125 printf ("ffs_walk %s\n", path); 126 #endif 127 vn = parent; 128 name = malloc ((len = strlen (path)) + 1); 129 strcpy (name, path); 130 131 for (i = 0; i < strlen (path); i = j + 1, vn = vnnext) 132 { 133 for (j = i; (name[j] != '/') && (j < len); j++) ; 134 name[j] = 0; 135 vnnext = ffs_walk_one (vn, name + i); 136 if (vn != parent) 137 vput (vn); 138 } 139 return vn; 140 } 141