umount(8): Add missing braces.
[dragonfly.git] / lib / libpuffs / subr.c
blob4e1f4c39a60a74d61832b3cf8e977e16c7bdf3b2
1 /* $NetBSD: subr.c,v 1.27 2011/02/17 17:55:36 pooka Exp $ */
3 /*
4 * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/vnode.h>
31 #include <sys/dirent.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "puffs.h"
41 #include "puffs_priv.h"
43 #ifndef MAXNAMLEN
44 #define MAXNAMLEN 255
45 #endif
47 int
48 puffs_gendotdent(struct dirent **dent, ino_t id, int dotdot, size_t *reslen)
50 const char *name;
52 assert(dotdot == 0 || dotdot == 1);
53 name = dotdot == 0 ? "." : "..";
55 return puffs_nextdent(dent, name, id, DT_DIR, reslen);
58 int
59 puffs_nextdent(struct dirent **dent, const char *name, ino_t id, uint8_t dtype,
60 size_t *reslen)
62 struct dirent *d = *dent;
63 size_t reclen;
65 /* check if we have enough room for our dent-aligned dirent */
66 reclen = _DIRENT_RECLEN(strlen(name));
67 if (reclen > *reslen)
68 return 0;
70 d->d_fileno = id;
71 d->d_type = dtype;
72 d->d_namlen = (uint16_t)strlen(name);
73 (void)memcpy(&d->d_name, name, (size_t)d->d_namlen);
74 d->d_name[d->d_namlen] = '\0';
76 *reslen -= reclen;
77 *dent = _DIRENT_NEXT(d);
79 return 1;
82 /*ARGSUSED*/
83 int
84 puffs_fsnop_unmount(struct puffs_usermount *dontuse1, int dontuse2)
87 /* would you like to see puffs rule again, my friend? */
88 return 0;
91 /*ARGSUSED*/
92 int
93 puffs_fsnop_sync(struct puffs_usermount *dontuse1, int dontuse2)
96 return 0;
99 /*ARGSUSED*/
101 puffs_fsnop_statvfs(struct puffs_usermount *dontuse1, struct statvfs *sbp)
104 memset(sbp, 0, sizeof(*sbp));
106 /* XXXDF
107 * sbp->f_iosize
109 sbp->f_bsize = sbp->f_frsize = DEV_BSIZE;
111 /* XXXDF
112 * sbp->f_bresvd
113 * sbp->f_fresvd
115 sbp->f_bfree=sbp->f_bavail=sbp->f_blocks = (fsblkcnt_t)0;
116 sbp->f_ffree=sbp->f_favail=sbp->f_files = (fsfilcnt_t)0;
118 sbp->f_namemax = MAXNAMLEN;
120 return 0;
123 /*ARGSUSED3*/
125 puffs_genfs_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
126 struct vattr *va, const struct puffs_cred *pcr)
128 struct puffs_node *pn = PU_CMAP(pu, opc);
130 memcpy(va, &pn->pn_va, sizeof(struct vattr));
131 return 0;
135 * Just put the node, don't do anything else. Don't use this if
136 * your fs needs more cleanup.
138 /*ARGSUSED2*/
140 puffs_genfs_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
143 puffs_pn_put(PU_CMAP(pu, opc));
145 return 0;
149 * Just a wrapper to make calling the above nicer without having to pass
150 * NULLs from application code
152 void
153 puffs_zerostatvfs(struct statvfs *sbp)
156 puffs_fsnop_statvfs(NULL, sbp);
160 * Set vattr values for those applicable (i.e. not PUFFS_VNOVAL).
162 void
163 puffs_setvattr(struct vattr *vap, const struct vattr *sva)
166 #define SETIFVAL(a, t) if (sva->a != (t)PUFFS_VNOVAL) vap->a = sva->a
167 if (sva->va_type != VNON)
168 vap->va_type = sva->va_type;
169 SETIFVAL(va_mode, mode_t);
170 SETIFVAL(va_nlink, nlink_t);
171 SETIFVAL(va_uid, uid_t);
172 SETIFVAL(va_gid, gid_t);
173 SETIFVAL(va_fsid, dev_t);
174 SETIFVAL(va_size, u_quad_t);
175 SETIFVAL(va_fileid, ino_t);
176 SETIFVAL(va_blocksize, long);
177 SETIFVAL(va_atime.tv_sec, time_t);
178 SETIFVAL(va_ctime.tv_sec, time_t);
179 SETIFVAL(va_mtime.tv_sec, time_t);
180 SETIFVAL(va_atime.tv_nsec, long);
181 SETIFVAL(va_ctime.tv_nsec, long);
182 SETIFVAL(va_mtime.tv_nsec, long);
183 SETIFVAL(va_gen, u_long);
184 SETIFVAL(va_flags, u_long);
185 SETIFVAL(va_rmajor, int);
186 SETIFVAL(va_rminor, int);
187 SETIFVAL(va_bytes, u_quad_t);
188 #undef SETIFVAL
189 /* ignore va->va_vaflags */
192 void
193 puffs_vattr_null(struct vattr *vap)
196 vap->va_type = VNON;
199 * Assign individually so that it is safe even if size and
200 * sign of each member are varied.
202 vap->va_mode = (mode_t)PUFFS_VNOVAL;
203 vap->va_nlink = (nlink_t)PUFFS_VNOVAL;
204 vap->va_uid = (uid_t)PUFFS_VNOVAL;
205 vap->va_gid = (gid_t)PUFFS_VNOVAL;
206 vap->va_fsid = (dev_t)PUFFS_VNOVAL;
207 vap->va_fileid = (ino_t)PUFFS_VNOVAL;
208 vap->va_size = (u_quad_t)PUFFS_VNOVAL;
209 vap->va_blocksize = sysconf(_SC_PAGESIZE);
210 vap->va_atime.tv_sec =
211 vap->va_mtime.tv_sec =
212 vap->va_ctime.tv_sec = PUFFS_VNOVAL;
213 vap->va_atime.tv_nsec =
214 vap->va_mtime.tv_nsec =
215 vap->va_ctime.tv_nsec = PUFFS_VNOVAL;
216 vap->va_rmajor = PUFFS_VNOVAL;
217 vap->va_rminor = PUFFS_VNOVAL;
218 vap->va_bytes = (u_quad_t)PUFFS_VNOVAL;
220 vap->va_flags = 0;
221 vap->va_gen = 0;
222 vap->va_vaflags = 0;
225 static int vdmap[] = {
226 DT_UNKNOWN, /* VNON */
227 DT_REG, /* VREG */
228 DT_DIR, /* VDIR */
229 DT_BLK, /* VBLK */
230 DT_CHR, /* VCHR */
231 DT_LNK, /* VLNK */
232 DT_SOCK, /* VSUCK*/
233 DT_FIFO, /* VFIFO*/
234 DT_UNKNOWN /* VBAD */
236 /* XXX: DT_WHT ? */
238 puffs_vtype2dt(enum vtype vt)
241 if ((int)vt >= VNON && vt < (sizeof(vdmap)/sizeof(vdmap[0])))
242 return vdmap[vt];
244 return DT_UNKNOWN;
247 enum vtype
248 puffs_mode2vt(mode_t mode)
251 switch (mode & S_IFMT) {
252 case S_IFIFO:
253 return VFIFO;
254 case S_IFCHR:
255 return VCHR;
256 case S_IFDIR:
257 return VDIR;
258 case S_IFBLK:
259 return VBLK;
260 case S_IFREG:
261 return VREG;
262 case S_IFLNK:
263 return VLNK;
264 case S_IFSOCK:
265 return VSOCK;
266 default:
267 return VBAD; /* XXX: not really true, but ... */
271 void
272 puffs_stat2vattr(struct vattr *va, const struct stat *sb)
275 memset(va, 0, sizeof(struct vattr));
276 va->va_type = puffs_mode2vt(sb->st_mode);
277 va->va_mode = sb->st_mode;
278 va->va_nlink = sb->st_nlink;
279 va->va_uid = sb->st_uid;
280 va->va_gid = sb->st_gid;
281 va->va_fsid = sb->st_dev;
282 va->va_fileid = sb->st_ino;
283 va->va_size = sb->st_size;
284 va->va_blocksize = sb->st_blksize;
285 va->va_atime = sb->st_atimespec;
286 va->va_ctime = sb->st_ctimespec;
287 va->va_mtime = sb->st_mtimespec;
288 va->va_gen = sb->st_gen;
289 va->va_flags = sb->st_flags;
290 va->va_bytes = sb->st_blocks << DEV_BSHIFT;
291 va->va_filerev = 0;
292 va->va_vaflags = 0;
295 mode_t
296 puffs_addvtype2mode(mode_t mode, enum vtype type)
299 switch (type) {
300 case VCHR:
301 mode |= S_IFCHR;
302 break;
303 case VBLK:
304 mode |= S_IFBLK;
305 break;
306 case VSOCK:
307 mode |= S_IFSOCK;
308 break;
309 case VFIFO:
310 mode |= S_IFIFO;
311 break;
312 case VREG:
313 mode |= S_IFREG;
314 break;
315 case VLNK:
316 mode |= S_IFLNK;
317 break;
318 case VDIR:
319 mode |= S_IFDIR;
320 break;
321 default:
322 break;
325 return mode;