1/* $NetBSD$ */ 2 3/* 4 * Copyright (c) 2006 Antti Kantee. 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 * 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. 26 */ 27 28#include <sys/cdefs.h> 29#if !defined(lint) 30__RCSID("$NetBSD$"); 31#endif /* !lint */ 32 33#include <sys/types.h> 34 35#include <assert.h> 36#include <puffs.h> 37#include <stdlib.h> 38#include <stdio.h> 39#include <string.h> 40 41#include "puffs_priv.h" 42 43/* 44 * Well, you're probably wondering why this isn't optimized. 45 * The reason is simple: my available time is not optimized for 46 * size ... so please be patient ;) 47 */ 48struct puffs_node * 49puffs_pn_new(struct puffs_usermount *pu, void *privdata) 50{ 51 struct puffs_node *pn; 52 53 pn = calloc(1, sizeof(struct puffs_node)); 54 if (pn == NULL) 55 return NULL; 56 57 pn->pn_data = privdata; 58 pn->pn_mnt = pu; 59 puffs_vattr_null(&pn->pn_va); 60 61 LIST_INSERT_HEAD(&pu->pu_pnodelst, pn, pn_entries); 62 63 pu->pu_flags |= PUFFS_FLAG_PNCOOKIE; 64 65 return pn; 66} 67 68void 69puffs_pn_remove(struct puffs_node *pn) 70{ 71 72 LIST_REMOVE(pn, pn_entries); 73 pn->pn_flags |= PUFFS_NODE_REMOVED; 74} 75 76void 77puffs_pn_put(struct puffs_node *pn) 78{ 79 struct puffs_usermount *pu = pn->pn_mnt; 80 81 pu->pu_pathfree(pu, &pn->pn_po); 82 if ((pn->pn_flags & PUFFS_NODE_REMOVED) == 0) 83 LIST_REMOVE(pn, pn_entries); 84 free(pn); 85} 86 87/* walk list, rv can be used either to halt or to return a value */ 88void * 89puffs_pn_nodewalk(struct puffs_usermount *pu, puffs_nodewalk_fn fn, void *arg) 90{ 91 struct puffs_node *pn_cur, *pn_next; 92 void *rv; 93 94 pn_cur = LIST_FIRST(&pu->pu_pnodelst); 95 while (pn_cur) { 96 pn_next = LIST_NEXT(pn_cur, pn_entries); 97 rv = fn(pu, pn_cur, arg); 98 if (rv) 99 return rv; 100 pn_cur = pn_next; 101 } 102 103 return NULL; 104} 105 106struct vattr * 107puffs_pn_getvap(struct puffs_node *pn) 108{ 109 110 return &pn->pn_va; 111} 112 113void * 114puffs_pn_getpriv(struct puffs_node *pn) 115{ 116 117 return pn->pn_data; 118} 119 120void 121puffs_pn_setpriv(struct puffs_node *pn, void *priv) 122{ 123 124 pn->pn_data = priv; 125} 126 127struct puffs_pathobj * 128puffs_pn_getpo(struct puffs_node *pn) 129{ 130 131 return &pn->pn_po; 132} 133 134struct puffs_usermount * 135puffs_pn_getmnt(struct puffs_node *pn) 136{ 137 138 return pn->pn_mnt; 139} 140 141/* convenience / shortcut */ 142void * 143puffs_pn_getmntspecific(struct puffs_node *pn) 144{ 145 146 return pn->pn_mnt->pu_privdata; 147} 148 149/* 150 * newnode parameters 151 */ 152void 153puffs_newinfo_setcookie(struct puffs_newinfo *pni, puffs_cookie_t cookie) 154{ 155 156 *pni->pni_cookie = cookie; 157} 158 159void 160puffs_newinfo_setvtype(struct puffs_newinfo *pni, enum vtype vt) 161{ 162 163 *pni->pni_vtype = vt; 164} 165 166void 167puffs_newinfo_setsize(struct puffs_newinfo *pni, voff_t size) 168{ 169 170 *pni->pni_size = size; 171} 172 173void 174puffs_newinfo_setrdev(struct puffs_newinfo *pni, dev_t rdev) 175{ 176 177 *pni->pni_rdev = rdev; 178} 179 180void 181puffs_newinfo_setva(struct puffs_newinfo *pni, struct vattr *va) 182{ 183 184 (void)memcpy(pni->pni_va, va, sizeof(struct vattr)); 185} 186 187void 188puffs_newinfo_setvattl(struct puffs_newinfo *pni, struct timespec *va_ttl) 189{ 190 191 pni->pni_va_ttl->tv_sec = va_ttl->tv_sec; 192 pni->pni_va_ttl->tv_nsec = va_ttl->tv_nsec; 193} 194 195void 196puffs_newinfo_setcnttl(struct puffs_newinfo *pni, struct timespec *cn_ttl) 197{ 198 199 pni->pni_cn_ttl->tv_sec = cn_ttl->tv_sec; 200 pni->pni_cn_ttl->tv_nsec = cn_ttl->tv_nsec; 201} 202 203