Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | /* * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* Copyright (c) 1998, Apple Computer, Inc. All rights reserved. */ /* * Change History: * * 17-Aug-1999 Pat Dirks New today. * */ #include <mach/mach_types.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/file.h> #include <sys/stat.h> #include <sys/proc.h> #include <sys/conf.h> #include <sys/mount_internal.h> #include <sys/vnode_internal.h> #include <sys/malloc.h> #include <sys/dirent.h> #include <sys/namei.h> #include <sys/attr.h> #include <sys/time.h> #include <sys/uio_internal.h> #include <sys/vm.h> #include <sys/errno.h> #include <vfs/vfs_support.h> #include "synthfs.h" struct synthfs_direntry_head { u_int32_t d_fileno; /* file number of entry */ u_int16_t d_reclen; /* length of this record */ u_int8_t d_type; /* file type, see below */ u_int8_t d_namlen; /* length of string in d_name */ }; #define PATHSEPARATOR '/' #define ROOTDIRID 2 static int synthfs_insertnode(struct synthfsnode *newnode_sp, struct synthfsnode *parent_sp) { struct timeval now; DBG_ASSERT(parent_sp->s_type == SYNTHFS_DIRECTORY); TAILQ_INSERT_TAIL(&parent_sp->s_u.d.d_subnodes, newnode_sp, s_sibling); ++parent_sp->s_u.d.d_entrycount; newnode_sp->s_parent = parent_sp; parent_sp->s_nodeflags |= IN_CHANGE | IN_MODIFIED; microtime(&now); synthfs_update(STOV(parent_sp), &now, &now, 0); return 0; } static int synthfs_newnode(mount_t mp, vnode_t dp, const char *name, unsigned long nodeid, mode_t mode, __unused proc_t p, enum vtype vtype, vnode_t *vpp) { int result; struct synthfsnode *sp; struct vnode *vp; struct timeval now; char *nodename; struct vnode_fsparam vfsp; MALLOC(sp, struct synthfsnode *, sizeof(struct synthfsnode), M_SYNTHFS, M_WAITOK); if (name == NULL) { MALLOC(nodename, char *, 1, M_TEMP, M_WAITOK); nodename[0] = 0; } else { MALLOC(nodename, char *, strlen(name) + 1, M_TEMP, M_WAITOK); strcpy(nodename, name); }; /* Initialize the relevant synthfsnode fields: */ bzero(sp, sizeof(*sp)); sp->s_nodeid = nodeid; /* Initialize all times from a consistent snapshot of the clock: */ microtime(&now); sp->s_createtime = now; sp->s_accesstime = now; sp->s_modificationtime = now; sp->s_changetime = now; sp->s_name = nodename; sp->s_mode = mode; //bzero(&vfsp, sizeof(struct vnode_fsparam)); vfsp.vnfs_mp = mp; vfsp.vnfs_vtype = vtype; vfsp.vnfs_str = "synthfs"; vfsp.vnfs_dvp = 0; vfsp.vnfs_fsnode = sp; vfsp.vnfs_cnp = 0; vfsp.vnfs_vops = synthfs_vnodeop_p; vfsp.vnfs_rdev = 0; vfsp.vnfs_filesize = 0; vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE; vfsp.vnfs_marksystem = 0; vfsp.vnfs_markroot = 0; result = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &vp); if (result != 0) { DBG_VOP(("getnewvnode failed with error code %d\n", result)); FREE(nodename, M_TEMP); FREE(sp, M_TEMP); return result; } vnode_ref(vp); sp->s_vp = vp; /* If there's a parent directory, update its subnode structures to insert this new node: */ if (dp) { result = synthfs_insertnode(sp, VTOS(dp)); }; *vpp = vp; return result; } int synthfs_remove_entry(struct vnode *vp) { struct synthfsnode *sp = VTOS(vp); struct synthfsnode *psp = sp->s_parent; struct timeval now; if (psp) { TAILQ_REMOVE(&psp->s_u.d.d_subnodes, sp, s_sibling); --psp->s_u.d.d_entrycount; psp->s_nodeflags |= IN_CHANGE | IN_MODIFIED; microtime(&now); synthfs_update(STOV(psp), &now, &now, 0); }; return 0; } int synthfs_move_rename_entry(struct vnode *source_vp, struct vnode *newparent_vp, char *new_name) { struct synthfsnode *source_sp = VTOS(source_vp); struct synthfsnode *parent_sp = VTOS(newparent_vp); char *new_name_ptr; int result = 0; /* Unlink the entry from its current place: */ result = synthfs_remove_entry(source_vp); if (result) goto err_exit; /* Change the name as necessary: */ if (new_name) { FREE(source_sp->s_name, M_TEMP); MALLOC(new_name_ptr, char *, strlen(new_name) + 1, M_TEMP, M_WAITOK); strcpy(new_name_ptr, new_name); source_sp->s_name = new_name_ptr; }; /* Insert the entry in its new home: */ result = synthfs_insertnode(source_sp, parent_sp); err_exit: return result; } int synthfs_new_directory(struct mount *mp, struct vnode *dp, const char *name, unsigned long nodeid, mode_t mode, struct proc *p, struct vnode **vpp) { int result; struct vnode *vp; struct synthfsnode *sp; result = synthfs_newnode(mp, dp, name, nodeid, mode, p, VDIR, &vp); if (result) { return result; }; sp = VTOS(vp); sp->s_linkcount = 2; if (dp) { ++VTOS(dp)->s_linkcount; /* Account for the [fictitious] ".." link */ }; /* Set up the directory-specific fields: */ sp->s_type = SYNTHFS_DIRECTORY; sp->s_u.d.d_entrycount = 0; /* No entries in this directory yet */ TAILQ_INIT(&sp->s_u.d.d_subnodes); /* No subnodes of this directory yet */ *vpp = vp; return 0; } int synthfs_remove_directory(struct vnode *vp) { struct synthfsnode *sp = VTOS(vp); struct synthfsnode *psp = sp->s_parent; if (psp && (sp->s_type == SYNTHFS_DIRECTORY) && (psp != sp)) { --psp->s_linkcount; /* account for the [fictitious] ".." link now removed */ }; vnode_rele(vp); /* Do the standard cleanup involved in pruning an entry from the filesystem: */ return synthfs_remove_entry(vp); /* Do whatever standard cleanup is required */ } int synthfs_new_symlink( struct mount *mp, struct vnode *dp, const char *name, unsigned long nodeid, char *targetstring, struct proc *p, struct vnode **vpp) { int result; struct vnode *vp; struct synthfsnode *sp; result = synthfs_newnode(mp, dp, name, nodeid, 0, p, VLNK, &vp); if (result) { return result; }; sp = VTOS(vp); sp->s_linkcount = 1; /* Set up the symlink-specific fields: */ sp->s_type = SYNTHFS_SYMLINK; sp->s_u.s.s_length = strlen(targetstring); MALLOC(sp->s_u.s.s_symlinktarget, char *, sp->s_u.s.s_length + 1, M_TEMP, M_WAITOK); strcpy(sp->s_u.s.s_symlinktarget, targetstring); *vpp = vp; return 0; } int synthfs_remove_symlink(struct vnode *vp) { struct synthfsnode *sp = VTOS(vp); FREE(sp->s_u.s.s_symlinktarget, M_TEMP); vnode_rele(vp); /* Do the standard cleanup involved in pruning an entry from the filesystem: */ return synthfs_remove_entry(vp); /* Do whatever standard cleanup is required */ } long synthfs_adddirentry(u_int32_t fileno, u_int8_t type, const char *name, struct uio *uio) { struct synthfs_direntry_head direntry; long namelength; int padding; long padtext = 0; unsigned short direntrylength; namelength = ((name == NULL) ? 0 : strlen(name) + 1); padding = (4 - (namelength & 3)) & 3; direntrylength = sizeof(struct synthfs_direntry_head) + namelength + padding; direntry.d_fileno = fileno; direntry.d_reclen = direntrylength; direntry.d_type = type; direntry.d_namlen = namelength; if (uio_resid(uio) < direntry.d_reclen) { direntrylength = 0; } else { uiomove((caddr_t)(&direntry), sizeof(direntry), uio); if (name != NULL) { uiomove((caddr_t)name, namelength, uio); }; if (padding > 0) { uiomove((caddr_t)&padtext, padding, uio); }; }; return direntrylength; } |