2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1998, Apple Computer, Inc. All rights reserved. */
26 * 17-Aug-1999 Pat Dirks New today.
30 #include <mach/mach_types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
40 #include <sys/mount.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/dirent.h>
44 #include <sys/namei.h>
49 #include <sys/errno.h>
50 #include <vfs/vfs_support.h>
54 struct synthfs_direntry_head
{
55 u_int32_t d_fileno
; /* file number of entry */
56 u_int16_t d_reclen
; /* length of this record */
57 u_int8_t d_type
; /* file type, see below */
58 u_int8_t d_namlen
; /* length of string in d_name */
62 #define PATHSEPARATOR '/'
65 void synthfs_setupuio(struct iovec
*iov
,
70 enum uio_rw direction
,
72 iov
->iov_base
= (char *)buffer
;
73 iov
->iov_len
= bufsize
;
78 uio
->uio_resid
= bufsize
;
79 uio
->uio_segflg
= space
;
80 uio
->uio_rw
= direction
;
85 static int synthfs_insertnode(struct synthfsnode
*newnode_sp
, struct synthfsnode
*parent_sp
) {
88 DBG_ASSERT(parent_sp
->s_type
== SYNTHFS_DIRECTORY
);
90 TAILQ_INSERT_TAIL(&parent_sp
->s_u
.d
.d_subnodes
, newnode_sp
, s_sibling
);
91 ++parent_sp
->s_u
.d
.d_entrycount
;
92 newnode_sp
->s_parent
= parent_sp
;
94 parent_sp
->s_nodeflags
|= IN_CHANGE
| IN_MODIFIED
;
96 VOP_UPDATE(STOV(parent_sp
), &now
, &now
, 0);
103 static int synthfs_newnode(struct mount
*mp
, struct vnode
*dp
, const char *name
, unsigned long nodeid
, mode_t mode
, struct proc
*p
, struct vnode
**vpp
) {
105 struct synthfsnode
*sp
;
110 /* Allocate the synthfsnode now to avoid blocking between the call
111 to getnewvnode(), below, and the initialization of v_data: */
112 MALLOC(sp
, struct synthfsnode
*, sizeof(struct synthfsnode
), M_SYNTHFS
, M_WAITOK
);
115 MALLOC(nodename
, char *, 1, M_TEMP
, M_WAITOK
);
118 MALLOC(nodename
, char *, strlen(name
) + 1, M_TEMP
, M_WAITOK
);
119 strcpy(nodename
, name
);
123 Note that getnewvnode() returns the vnode with a refcount of +1;
124 this routine returns the newly created vnode with this positive refcount.
126 result
= getnewvnode(VT_SYNTHFS
, mp
, synthfs_vnodeop_p
, &vp
);
128 DBG_VOP(("getnewvnode failed with error code %d\n", result
));
129 FREE(nodename
, M_TEMP
);
134 DBG_VOP(("getnewvnod returned NULL without an error!\n"));
135 FREE(nodename
, M_TEMP
);
140 /* Initialize the relevant synthfsnode fields: */
141 bzero(sp
, sizeof(*sp
));
142 lockinit(&sp
->s_lock
, PINOD
, "synthfsnode", 0, 0);
143 sp
->s_nodeid
= nodeid
;
145 /* Initialize all times from a consistent snapshot of the clock: */
147 sp
->s_createtime
= now
;
148 sp
->s_accesstime
= now
;
149 sp
->s_modificationtime
= now
;
150 sp
->s_changetime
= now
;
151 sp
->s_name
= nodename
;
157 vget(vp
, LK_EXCLUSIVE
, p
);
159 /* If there's a parent directory, update its subnode structures to insert this new node: */
161 result
= synthfs_insertnode(sp
, VTOS(dp
));
171 int synthfs_remove_entry(struct vnode
*vp
) {
172 struct synthfsnode
*sp
= VTOS(vp
);
173 struct synthfsnode
*psp
= sp
->s_parent
;
177 TAILQ_REMOVE(&psp
->s_u
.d
.d_subnodes
, sp
, s_sibling
);
178 --psp
->s_u
.d
.d_entrycount
;
180 psp
->s_nodeflags
|= IN_CHANGE
| IN_MODIFIED
;
182 VOP_UPDATE(STOV(psp
), &now
, &now
, 0);
190 int synthfs_move_rename_entry(struct vnode
*source_vp
, struct vnode
*newparent_vp
, char *new_name
) {
191 struct synthfsnode
*source_sp
= VTOS(source_vp
);
192 struct synthfsnode
*parent_sp
= VTOS(newparent_vp
);
196 if (parent_sp
== source_sp
->s_parent
) return 0;
198 /* Unlink the entry from its current place: */
199 result
= synthfs_remove_entry(source_vp
);
200 if (result
) return result
;
202 /* Change the name as necessary: */
203 FREE(source_sp
->s_name
, M_TEMP
);
204 if (new_name
== NULL
) {
205 MALLOC(new_name_ptr
, char *, 1, M_TEMP
, M_WAITOK
);
208 MALLOC(new_name_ptr
, char *, strlen(new_name
) + 1, M_TEMP
, M_WAITOK
);
209 strcpy(new_name_ptr
, new_name
);
211 source_sp
->s_name
= new_name_ptr
;
213 /* Insert the entry in its new home: */
214 return synthfs_insertnode(source_sp
, parent_sp
);
219 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
) {
222 struct synthfsnode
*sp
;
224 result
= synthfs_newnode(mp
, dp
, name
, nodeid
, mode
, p
, &vp
);
231 /* Initialize the relevant vnode fields: */
234 ++VTOS(dp
)->s_linkcount
; /* Account for the [fictitious] ".." link */
237 /* Set up the directory-specific fields: */
238 sp
->s_type
= SYNTHFS_DIRECTORY
;
239 sp
->s_u
.d
.d_entrycount
= 0; /* No entries in this directory yet */
240 TAILQ_INIT(&sp
->s_u
.d
.d_subnodes
); /* No subnodes of this directory yet */
249 int synthfs_remove_directory(struct vnode
*vp
) {
250 struct synthfsnode
*sp
= VTOS(vp
);
251 struct synthfsnode
*psp
= sp
->s_parent
;
253 if (psp
&& (sp
->s_type
== SYNTHFS_DIRECTORY
) && (psp
!= sp
)) {
254 --psp
->s_linkcount
; /* account for the [fictitious] ".." link now removed */
257 /* Do the standard cleanup involved in pruning an entry from the filesystem: */
258 return synthfs_remove_entry(vp
); /* Do whatever standard cleanup is required */
263 int synthfs_new_symlink(
267 unsigned long nodeid
,
270 struct vnode
**vpp
) {
274 struct synthfsnode
*sp
;
276 result
= synthfs_newnode(mp
, dp
, name
, nodeid
, 0, p
, &vp
);
283 /* Initialize the relevant vnode fields: */
286 /* Set up the symlink-specific fields: */
287 sp
->s_type
= SYNTHFS_SYMLINK
;
288 sp
->s_u
.s
.s_length
= strlen(targetstring
);
289 MALLOC(sp
->s_u
.s
.s_symlinktarget
, char *, sp
->s_u
.s
.s_length
+ 1, M_TEMP
, M_WAITOK
);
290 strcpy(sp
->s_u
.s
.s_symlinktarget
, targetstring
);
299 int synthfs_remove_symlink(struct vnode
*vp
) {
300 struct synthfsnode
*sp
= VTOS(vp
);
302 FREE(sp
->s_u
.s
.s_symlinktarget
, M_TEMP
);
304 /* Do the standard cleanup involved in pruning an entry from the filesystem: */
305 return synthfs_remove_entry(vp
); /* Do whatever standard cleanup is required */
313 long synthfs_adddirentry(u_int32_t fileno
, u_int8_t type
, const char *name
, struct uio
*uio
) {
314 struct synthfs_direntry_head direntry
;
318 unsigned short direntrylength
;
320 namelength
= ((name
== NULL
) ? 0 : strlen(name
));
321 padding
= (4 - (namelength
& 3)) & 3;
322 direntrylength
= sizeof(struct synthfs_direntry_head
) + namelength
+ padding
;
324 direntry
.d_fileno
= fileno
;
325 direntry
.d_reclen
= direntrylength
;
326 direntry
.d_type
= type
;
327 direntry
.d_namlen
= namelength
;
329 if (uio
->uio_resid
< direntry
.d_reclen
) {
332 uiomove((caddr_t
)(&direntry
), sizeof(direntry
), uio
);
334 uiomove((caddr_t
)name
, namelength
, uio
);
337 uiomove((caddr_t
)&padtext
, padding
, uio
);
341 return direntrylength
;