1 1.18 andvar /* $NetBSD: ulfs_extattr.c,v 1.18 2024/02/10 18:43:53 andvar Exp $ */ 2 1.14 dholland /* from NetBSD: ulfs_extattr.c,v 1.48 2016/11/09 05:08:35 dholland Exp */ 3 1.1 dholland 4 1.1 dholland /*- 5 1.1 dholland * Copyright (c) 1999-2002 Robert N. M. Watson 6 1.1 dholland * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 7 1.1 dholland * All rights reserved. 8 1.1 dholland * 9 1.1 dholland * This software was developed by Robert Watson for the TrustedBSD Project. 10 1.1 dholland * 11 1.1 dholland * This software was developed for the FreeBSD Project in part by Network 12 1.1 dholland * Associates Laboratories, the Security Research Division of Network 13 1.1 dholland * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), 14 1.1 dholland * as part of the DARPA CHATS research program. 15 1.1 dholland * 16 1.1 dholland * Redistribution and use in source and binary forms, with or without 17 1.1 dholland * modification, are permitted provided that the following conditions 18 1.1 dholland * are met: 19 1.1 dholland * 1. Redistributions of source code must retain the above copyright 20 1.1 dholland * notice, this list of conditions and the following disclaimer. 21 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright 22 1.1 dholland * notice, this list of conditions and the following disclaimer in the 23 1.1 dholland * documentation and/or other materials provided with the distribution. 24 1.1 dholland * 25 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 dholland * SUCH DAMAGE. 36 1.1 dholland * 37 1.1 dholland */ 38 1.1 dholland 39 1.1 dholland /* 40 1.4 dholland * Support for file system extended attributes on the ULFS1 file system. 41 1.1 dholland * 42 1.1 dholland * Extended attributes are defined in the form name=value, where name is 43 1.1 dholland * a nul-terminated string in the style of a file name, and value is a 44 1.4 dholland * binary blob of zero or more bytes. The ULFS1 extended attribute service 45 1.1 dholland * layers support for extended attributes onto a backing file, in the style 46 1.1 dholland * of the quota implementation, meaning that it requires no underlying format 47 1.1 dholland * changes to the file system. This design choice exchanges simplicity, 48 1.1 dholland * usability, and easy deployment for performance. 49 1.1 dholland */ 50 1.1 dholland 51 1.1 dholland #include <sys/cdefs.h> 52 1.18 andvar __KERNEL_RCSID(0, "$NetBSD: ulfs_extattr.c,v 1.18 2024/02/10 18:43:53 andvar Exp $"); 53 1.1 dholland 54 1.1 dholland #ifdef _KERNEL_OPT 55 1.3 dholland #include "opt_lfs.h" 56 1.1 dholland #endif 57 1.1 dholland 58 1.1 dholland #include <sys/param.h> 59 1.1 dholland #include <sys/systm.h> 60 1.1 dholland #include <sys/reboot.h> 61 1.1 dholland #include <sys/kauth.h> 62 1.1 dholland #include <sys/kernel.h> 63 1.1 dholland #include <sys/namei.h> 64 1.1 dholland #include <sys/kmem.h> 65 1.1 dholland #include <sys/fcntl.h> 66 1.1 dholland #include <sys/lwp.h> 67 1.1 dholland #include <sys/vnode.h> 68 1.1 dholland #include <sys/mount.h> 69 1.1 dholland #include <sys/lock.h> 70 1.1 dholland #include <sys/dirent.h> 71 1.1 dholland #include <sys/extattr.h> 72 1.1 dholland #include <sys/sysctl.h> 73 1.1 dholland 74 1.2 dholland #include <ufs/lfs/ulfs_extattr.h> 75 1.2 dholland #include <ufs/lfs/ulfsmount.h> 76 1.2 dholland #include <ufs/lfs/ulfs_inode.h> 77 1.2 dholland #include <ufs/lfs/ulfs_bswap.h> 78 1.2 dholland #include <ufs/lfs/ulfs_extern.h> 79 1.1 dholland 80 1.4 dholland int ulfs_extattr_sync = 1; 81 1.4 dholland int ulfs_extattr_autocreate = 1024; 82 1.1 dholland 83 1.4 dholland static int ulfs_extattr_valid_attrname(int attrnamespace, 84 1.1 dholland const char *attrname); 85 1.4 dholland static int ulfs_extattr_enable_with_open(struct ulfsmount *ump, 86 1.1 dholland struct vnode *vp, int attrnamespace, const char *attrname, 87 1.1 dholland struct lwp *l); 88 1.4 dholland static int ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace, 89 1.1 dholland const char *attrname, struct vnode *backing_vnode, 90 1.1 dholland struct lwp *l); 91 1.4 dholland static int ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace, 92 1.1 dholland const char *attrname, struct lwp *l); 93 1.4 dholland static int ulfs_extattr_get(struct vnode *vp, int attrnamespace, 94 1.1 dholland const char *name, struct uio *uio, size_t *size, 95 1.1 dholland kauth_cred_t cred, struct lwp *l); 96 1.4 dholland static int ulfs_extattr_list(struct vnode *vp, int attrnamespace, 97 1.1 dholland struct uio *uio, size_t *size, int flag, 98 1.1 dholland kauth_cred_t cred, struct lwp *l); 99 1.4 dholland static int ulfs_extattr_set(struct vnode *vp, int attrnamespace, 100 1.1 dholland const char *name, struct uio *uio, kauth_cred_t cred, 101 1.1 dholland struct lwp *l); 102 1.4 dholland static int ulfs_extattr_rm(struct vnode *vp, int attrnamespace, 103 1.1 dholland const char *name, kauth_cred_t cred, struct lwp *l); 104 1.4 dholland static struct ulfs_extattr_list_entry *ulfs_extattr_find_attr(struct ulfsmount *, 105 1.1 dholland int, const char *); 106 1.4 dholland static int ulfs_extattr_get_header(struct vnode *, 107 1.4 dholland struct ulfs_extattr_list_entry *, 108 1.4 dholland struct ulfs_extattr_header *, off_t *); 109 1.1 dholland 110 1.1 dholland /* 111 1.1 dholland * Per-FS attribute lock protecting attribute operations. 112 1.1 dholland * XXX Right now there is a lot of lock contention due to having a single 113 1.1 dholland * lock per-FS; really, this should be far more fine-grained. 114 1.1 dholland */ 115 1.1 dholland static void 116 1.4 dholland ulfs_extattr_uepm_lock(struct ulfsmount *ump) 117 1.1 dholland { 118 1.1 dholland 119 1.14 dholland /* 120 1.14 dholland * XXX This needs to be recursive for the following reasons: 121 1.14 dholland * - it is taken in ulfs_extattr_vnode_inactive 122 1.14 dholland * - which is called from VOP_INACTIVE 123 1.14 dholland * - which can be triggered by any vrele, vput, or vn_close 124 1.14 dholland * - several of these can happen while it's held 125 1.14 dholland */ 126 1.1 dholland if (mutex_owned(&ump->um_extattr.uepm_lock)) { 127 1.1 dholland ump->um_extattr.uepm_lockcnt++; 128 1.1 dholland return; 129 1.1 dholland } 130 1.1 dholland mutex_enter(&ump->um_extattr.uepm_lock); 131 1.1 dholland } 132 1.1 dholland 133 1.1 dholland static void 134 1.4 dholland ulfs_extattr_uepm_unlock(struct ulfsmount *ump) 135 1.1 dholland { 136 1.1 dholland 137 1.1 dholland if (ump->um_extattr.uepm_lockcnt != 0) { 138 1.1 dholland KASSERT(mutex_owned(&ump->um_extattr.uepm_lock)); 139 1.1 dholland ump->um_extattr.uepm_lockcnt--; 140 1.1 dholland return; 141 1.1 dholland } 142 1.1 dholland mutex_exit(&ump->um_extattr.uepm_lock); 143 1.1 dholland } 144 1.1 dholland 145 1.1 dholland /*- 146 1.1 dholland * Determine whether the name passed is a valid name for an actual 147 1.1 dholland * attribute. 148 1.1 dholland * 149 1.1 dholland * Invalid currently consists of: 150 1.1 dholland * NULL pointer for attrname 151 1.1 dholland * zero-length attrname (used to retrieve application attribute list) 152 1.1 dholland */ 153 1.1 dholland static int 154 1.4 dholland ulfs_extattr_valid_attrname(int attrnamespace, const char *attrname) 155 1.1 dholland { 156 1.1 dholland 157 1.1 dholland if (attrname == NULL) 158 1.1 dholland return (0); 159 1.1 dholland if (strlen(attrname) == 0) 160 1.1 dholland return (0); 161 1.1 dholland return (1); 162 1.1 dholland } 163 1.1 dholland 164 1.1 dholland /* 165 1.1 dholland * Autocreate an attribute storage 166 1.1 dholland */ 167 1.11 dholland static int 168 1.4 dholland ulfs_extattr_autocreate_attr(struct vnode *vp, int attrnamespace, 169 1.11 dholland const char *attrname, struct lwp *l, struct ulfs_extattr_list_entry **uelep) 170 1.1 dholland { 171 1.1 dholland struct mount *mp = vp->v_mount; 172 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 173 1.1 dholland struct vnode *backing_vp; 174 1.1 dholland struct pathbuf *pb; 175 1.1 dholland char *path; 176 1.4 dholland struct ulfs_extattr_fileheader uef; 177 1.4 dholland struct ulfs_extattr_list_entry *uele; 178 1.1 dholland int error; 179 1.1 dholland 180 1.1 dholland path = PNBUF_GET(); 181 1.1 dholland 182 1.1 dholland /* 183 1.1 dholland * We only support system and user namespace autocreation 184 1.1 dholland */ 185 1.1 dholland switch (attrnamespace) { 186 1.1 dholland case EXTATTR_NAMESPACE_SYSTEM: 187 1.1 dholland (void)snprintf(path, PATH_MAX, "%s/%s/%s/%s", 188 1.1 dholland mp->mnt_stat.f_mntonname, 189 1.4 dholland ULFS_EXTATTR_FSROOTSUBDIR, 190 1.4 dholland ULFS_EXTATTR_SUBDIR_SYSTEM, 191 1.1 dholland attrname); 192 1.1 dholland break; 193 1.1 dholland case EXTATTR_NAMESPACE_USER: 194 1.1 dholland (void)snprintf(path, PATH_MAX, "%s/%s/%s/%s", 195 1.1 dholland mp->mnt_stat.f_mntonname, 196 1.4 dholland ULFS_EXTATTR_FSROOTSUBDIR, 197 1.4 dholland ULFS_EXTATTR_SUBDIR_USER, 198 1.1 dholland attrname); 199 1.1 dholland break; 200 1.1 dholland default: 201 1.1 dholland PNBUF_PUT(path); 202 1.11 dholland *uelep = NULL; 203 1.11 dholland return EINVAL; 204 1.1 dholland break; 205 1.1 dholland } 206 1.1 dholland 207 1.1 dholland /* 208 1.11 dholland * Release extended attribute mount lock, otherwise 209 1.11 dholland * we can deadlock with another thread that would lock 210 1.11 dholland * vp after we unlock it below, and call 211 1.11 dholland * ulfs_extattr_uepm_lock(ump), for instance 212 1.11 dholland * in ulfs_getextattr(). 213 1.11 dholland */ 214 1.11 dholland ulfs_extattr_uepm_unlock(ump); 215 1.11 dholland 216 1.11 dholland /* 217 1.1 dholland * XXX unlock/lock should only be done when setting extattr 218 1.1 dholland * on backing store or one of its parent directory 219 1.1 dholland * including root, but we always do it for now. 220 1.1 dholland */ 221 1.1 dholland KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE); 222 1.1 dholland VOP_UNLOCK(vp); 223 1.1 dholland 224 1.1 dholland pb = pathbuf_create(path); 225 1.1 dholland 226 1.11 dholland /* 227 1.11 dholland * Since we do not hold ulfs_extattr_uepm_lock anymore, 228 1.11 dholland * another thread may race with us for backend creation, 229 1.17 dholland * but only one can succeed here thanks to O_EXCL. 230 1.17 dholland * 231 1.17 dholland * backing_vp is the backing store. 232 1.11 dholland */ 233 1.17 dholland error = vn_open(NULL, pb, 0, O_CREAT|O_EXCL|O_RDWR, 0600, 234 1.17 dholland &backing_vp, NULL, NULL); 235 1.1 dholland 236 1.1 dholland /* 237 1.1 dholland * Reacquire the lock on the vnode 238 1.1 dholland */ 239 1.1 dholland KASSERT(VOP_ISLOCKED(vp) == 0); 240 1.1 dholland vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 241 1.1 dholland 242 1.11 dholland ulfs_extattr_uepm_lock(ump); 243 1.11 dholland 244 1.1 dholland if (error != 0) { 245 1.1 dholland pathbuf_destroy(pb); 246 1.1 dholland PNBUF_PUT(path); 247 1.11 dholland *uelep = NULL; 248 1.11 dholland return error; 249 1.1 dholland } 250 1.1 dholland 251 1.17 dholland KASSERT(backing_vp != NULL); 252 1.17 dholland KASSERT(VOP_ISLOCKED(backing_vp) == LK_EXCLUSIVE); 253 1.1 dholland 254 1.1 dholland pathbuf_destroy(pb); 255 1.1 dholland PNBUF_PUT(path); 256 1.1 dholland 257 1.4 dholland uef.uef_magic = ULFS_EXTATTR_MAGIC; 258 1.4 dholland uef.uef_version = ULFS_EXTATTR_VERSION; 259 1.4 dholland uef.uef_size = ulfs_extattr_autocreate; 260 1.1 dholland 261 1.1 dholland error = vn_rdwr(UIO_WRITE, backing_vp, &uef, sizeof(uef), 0, 262 1.1 dholland UIO_SYSSPACE, IO_NODELOCKED|IO_APPEND, 263 1.1 dholland l->l_cred, NULL, l); 264 1.1 dholland 265 1.1 dholland VOP_UNLOCK(backing_vp); 266 1.1 dholland 267 1.1 dholland if (error != 0) { 268 1.1 dholland printf("%s: write uef header failed for %s, error = %d\n", 269 1.1 dholland __func__, attrname, error); 270 1.1 dholland vn_close(backing_vp, FREAD|FWRITE, l->l_cred); 271 1.11 dholland *uelep = NULL; 272 1.11 dholland return error; 273 1.1 dholland } 274 1.1 dholland 275 1.1 dholland /* 276 1.1 dholland * Now enable attribute. 277 1.1 dholland */ 278 1.4 dholland error = ulfs_extattr_enable(ump,attrnamespace, attrname, backing_vp, l); 279 1.1 dholland KASSERT(VOP_ISLOCKED(backing_vp) == 0); 280 1.1 dholland 281 1.1 dholland if (error != 0) { 282 1.1 dholland printf("%s: enable %s failed, error %d\n", 283 1.1 dholland __func__, attrname, error); 284 1.1 dholland vn_close(backing_vp, FREAD|FWRITE, l->l_cred); 285 1.11 dholland *uelep = NULL; 286 1.11 dholland return error; 287 1.1 dholland } 288 1.1 dholland 289 1.4 dholland uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname); 290 1.1 dholland if (uele == NULL) { 291 1.18 andvar printf("%s: attribute %s created but not found!\n", 292 1.1 dholland __func__, attrname); 293 1.1 dholland vn_close(backing_vp, FREAD|FWRITE, l->l_cred); 294 1.11 dholland *uelep = NULL; 295 1.11 dholland return ESRCH; /* really internal error */ 296 1.1 dholland } 297 1.1 dholland 298 1.1 dholland printf("%s: EA backing store autocreated for %s\n", 299 1.1 dholland mp->mnt_stat.f_mntonname, attrname); 300 1.1 dholland 301 1.11 dholland *uelep = uele; 302 1.11 dholland return 0; 303 1.1 dholland } 304 1.1 dholland 305 1.1 dholland /* 306 1.1 dholland * Locate an attribute given a name and mountpoint. 307 1.1 dholland * Must be holding uepm lock for the mount point. 308 1.1 dholland */ 309 1.4 dholland static struct ulfs_extattr_list_entry * 310 1.4 dholland ulfs_extattr_find_attr(struct ulfsmount *ump, int attrnamespace, 311 1.1 dholland const char *attrname) 312 1.1 dholland { 313 1.4 dholland struct ulfs_extattr_list_entry *search_attribute; 314 1.1 dholland 315 1.1 dholland for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list); 316 1.1 dholland search_attribute != NULL; 317 1.1 dholland search_attribute = LIST_NEXT(search_attribute, uele_entries)) { 318 1.1 dholland if (!(strncmp(attrname, search_attribute->uele_attrname, 319 1.4 dholland ULFS_EXTATTR_MAXEXTATTRNAME)) && 320 1.1 dholland (attrnamespace == search_attribute->uele_attrnamespace)) { 321 1.1 dholland return (search_attribute); 322 1.1 dholland } 323 1.1 dholland } 324 1.1 dholland 325 1.1 dholland return (0); 326 1.1 dholland } 327 1.1 dholland 328 1.1 dholland /* 329 1.1 dholland * Initialize per-FS structures supporting extended attributes. Do not 330 1.1 dholland * start extended attributes yet. 331 1.1 dholland */ 332 1.1 dholland void 333 1.4 dholland ulfs_extattr_uepm_init(struct ulfs_extattr_per_mount *uepm) 334 1.1 dholland { 335 1.1 dholland 336 1.1 dholland uepm->uepm_flags = 0; 337 1.1 dholland uepm->uepm_lockcnt = 0; 338 1.1 dholland 339 1.1 dholland LIST_INIT(&uepm->uepm_list); 340 1.1 dholland mutex_init(&uepm->uepm_lock, MUTEX_DEFAULT, IPL_NONE); 341 1.4 dholland uepm->uepm_flags |= ULFS_EXTATTR_UEPM_INITIALIZED; 342 1.1 dholland } 343 1.1 dholland 344 1.1 dholland /* 345 1.1 dholland * Destroy per-FS structures supporting extended attributes. Assumes 346 1.1 dholland * that EAs have already been stopped, and will panic if not. 347 1.1 dholland */ 348 1.1 dholland void 349 1.4 dholland ulfs_extattr_uepm_destroy(struct ulfs_extattr_per_mount *uepm) 350 1.1 dholland { 351 1.1 dholland 352 1.4 dholland if (!(uepm->uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) 353 1.4 dholland panic("ulfs_extattr_uepm_destroy: not initialized"); 354 1.1 dholland 355 1.4 dholland if ((uepm->uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 356 1.4 dholland panic("ulfs_extattr_uepm_destroy: called while still started"); 357 1.1 dholland 358 1.1 dholland /* 359 1.10 dholland * It's not clear that either order for the next three lines is 360 1.1 dholland * ideal, and it should never be a problem if this is only called 361 1.1 dholland * during unmount, and with vfs_busy(). 362 1.1 dholland */ 363 1.10 dholland uepm->uepm_flags &= ~ULFS_EXTATTR_UEPM_STARTED; 364 1.4 dholland uepm->uepm_flags &= ~ULFS_EXTATTR_UEPM_INITIALIZED; 365 1.1 dholland mutex_destroy(&uepm->uepm_lock); 366 1.1 dholland } 367 1.1 dholland 368 1.1 dholland /* 369 1.1 dholland * Start extended attribute support on an FS. 370 1.1 dholland */ 371 1.1 dholland int 372 1.4 dholland ulfs_extattr_start(struct mount *mp, struct lwp *l) 373 1.1 dholland { 374 1.4 dholland struct ulfsmount *ump; 375 1.1 dholland int error = 0; 376 1.1 dholland 377 1.4 dholland ump = VFSTOULFS(mp); 378 1.1 dholland 379 1.10 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) 380 1.13 msaitoh ulfs_extattr_uepm_init(&ump->um_extattr); 381 1.10 dholland 382 1.4 dholland ulfs_extattr_uepm_lock(ump); 383 1.1 dholland 384 1.4 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) { 385 1.1 dholland error = EOPNOTSUPP; 386 1.1 dholland goto unlock; 387 1.1 dholland } 388 1.4 dholland if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) { 389 1.1 dholland error = EBUSY; 390 1.1 dholland goto unlock; 391 1.1 dholland } 392 1.1 dholland 393 1.4 dholland ump->um_extattr.uepm_flags |= ULFS_EXTATTR_UEPM_STARTED; 394 1.1 dholland 395 1.1 dholland ump->um_extattr.uepm_ucred = l->l_cred; 396 1.1 dholland kauth_cred_hold(ump->um_extattr.uepm_ucred); 397 1.1 dholland 398 1.1 dholland unlock: 399 1.4 dholland ulfs_extattr_uepm_unlock(ump); 400 1.1 dholland 401 1.1 dholland return (error); 402 1.1 dholland } 403 1.1 dholland 404 1.1 dholland /* 405 1.1 dholland * Helper routine: given a locked parent directory and filename, return 406 1.1 dholland * the locked vnode of the inode associated with the name. Will not 407 1.1 dholland * follow symlinks, may return any type of vnode. Lock on parent will 408 1.1 dholland * be released even in the event of a failure. In the event that the 409 1.1 dholland * target is the parent (i.e., "."), there will be two references and 410 1.1 dholland * one lock, requiring the caller to possibly special-case. 411 1.1 dholland */ 412 1.1 dholland static int 413 1.4 dholland ulfs_extattr_lookup(struct vnode *start_dvp, int lockparent, const char *dirname, 414 1.1 dholland struct vnode **vp, struct lwp *l) 415 1.1 dholland { 416 1.7 hannken struct vop_lookup_v2_args vargs; 417 1.1 dholland struct componentname cnp; 418 1.1 dholland struct vnode *target_vp; 419 1.1 dholland char *pnbuf; 420 1.1 dholland int error; 421 1.1 dholland 422 1.1 dholland KASSERT(VOP_ISLOCKED(start_dvp) == LK_EXCLUSIVE); 423 1.1 dholland 424 1.1 dholland pnbuf = PNBUF_GET(); 425 1.1 dholland 426 1.1 dholland memset(&cnp, 0, sizeof(cnp)); 427 1.1 dholland cnp.cn_nameiop = LOOKUP; 428 1.1 dholland cnp.cn_flags = ISLASTCN | lockparent; 429 1.1 dholland cnp.cn_cred = l->l_cred; 430 1.1 dholland cnp.cn_nameptr = pnbuf; 431 1.1 dholland error = copystr(dirname, pnbuf, MAXPATHLEN, &cnp.cn_namelen); 432 1.1 dholland if (error) { 433 1.1 dholland if (lockparent == 0) { 434 1.1 dholland VOP_UNLOCK(start_dvp); 435 1.1 dholland } 436 1.1 dholland PNBUF_PUT(pnbuf); 437 1.4 dholland printf("ulfs_extattr_lookup: copystr failed\n"); 438 1.1 dholland return (error); 439 1.1 dholland } 440 1.1 dholland cnp.cn_namelen--; /* trim nul termination */ 441 1.1 dholland vargs.a_desc = NULL; 442 1.1 dholland vargs.a_dvp = start_dvp; 443 1.1 dholland vargs.a_vpp = &target_vp; 444 1.1 dholland vargs.a_cnp = &cnp; 445 1.4 dholland error = ulfs_lookup(&vargs); 446 1.1 dholland PNBUF_PUT(pnbuf); 447 1.1 dholland if (error) { 448 1.1 dholland if (lockparent == 0) { 449 1.1 dholland VOP_UNLOCK(start_dvp); 450 1.1 dholland } 451 1.1 dholland return (error); 452 1.1 dholland } 453 1.1 dholland #if 0 454 1.1 dholland if (target_vp == start_dvp) 455 1.4 dholland panic("ulfs_extattr_lookup: target_vp == start_dvp"); 456 1.1 dholland #endif 457 1.1 dholland 458 1.7 hannken if (target_vp != start_dvp) { 459 1.7 hannken error = vn_lock(target_vp, LK_EXCLUSIVE); 460 1.7 hannken if (lockparent == 0) 461 1.7 hannken VOP_UNLOCK(start_dvp); 462 1.7 hannken if (error) { 463 1.7 hannken vrele(target_vp); 464 1.7 hannken return error; 465 1.7 hannken } 466 1.7 hannken } 467 1.1 dholland 468 1.1 dholland KASSERT(VOP_ISLOCKED(target_vp) == LK_EXCLUSIVE); 469 1.1 dholland *vp = target_vp; 470 1.1 dholland return (0); 471 1.1 dholland } 472 1.1 dholland 473 1.1 dholland /* 474 1.1 dholland * Enable an EA using the passed filesystem, backing vnode, attribute name, 475 1.1 dholland * namespace, and proc. Will perform a VOP_OPEN() on the vp, so expects vp 476 1.1 dholland * to be locked when passed in. The vnode will be returned unlocked, 477 1.1 dholland * regardless of success/failure of the function. As a result, the caller 478 1.1 dholland * will always need to vrele(), but not vput(). 479 1.1 dholland */ 480 1.1 dholland static int 481 1.4 dholland ulfs_extattr_enable_with_open(struct ulfsmount *ump, struct vnode *vp, 482 1.1 dholland int attrnamespace, const char *attrname, struct lwp *l) 483 1.1 dholland { 484 1.1 dholland int error; 485 1.1 dholland 486 1.1 dholland error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred); 487 1.1 dholland if (error) { 488 1.4 dholland printf("ulfs_extattr_enable_with_open.VOP_OPEN(): failed " 489 1.1 dholland "with %d\n", error); 490 1.1 dholland VOP_UNLOCK(vp); 491 1.1 dholland return (error); 492 1.1 dholland } 493 1.1 dholland 494 1.1 dholland mutex_enter(vp->v_interlock); 495 1.1 dholland vp->v_writecount++; 496 1.1 dholland mutex_exit(vp->v_interlock); 497 1.1 dholland 498 1.1 dholland vref(vp); 499 1.1 dholland 500 1.1 dholland VOP_UNLOCK(vp); 501 1.1 dholland 502 1.4 dholland error = ulfs_extattr_enable(ump, attrnamespace, attrname, vp, l); 503 1.1 dholland if (error != 0) 504 1.1 dholland vn_close(vp, FREAD|FWRITE, l->l_cred); 505 1.1 dholland return (error); 506 1.1 dholland } 507 1.1 dholland 508 1.1 dholland /* 509 1.1 dholland * Given a locked directory vnode, iterate over the names in the directory 510 1.4 dholland * and use ulfs_extattr_lookup() to retrieve locked vnodes of potential 511 1.4 dholland * attribute files. Then invoke ulfs_extattr_enable_with_open() on each 512 1.1 dholland * to attempt to start the attribute. Leaves the directory locked on 513 1.1 dholland * exit. 514 1.1 dholland */ 515 1.1 dholland static int 516 1.4 dholland ulfs_extattr_iterate_directory(struct ulfsmount *ump, struct vnode *dvp, 517 1.1 dholland int attrnamespace, struct lwp *l) 518 1.1 dholland { 519 1.1 dholland struct vop_readdir_args vargs; 520 1.1 dholland struct statvfs *sbp = &ump->um_mountp->mnt_stat; 521 1.1 dholland struct dirent *dp, *edp; 522 1.1 dholland struct vnode *attr_vp; 523 1.1 dholland struct uio auio; 524 1.1 dholland struct iovec aiov; 525 1.1 dholland char *dirbuf; 526 1.1 dholland int error, eofflag = 0; 527 1.1 dholland 528 1.1 dholland if (dvp->v_type != VDIR) 529 1.1 dholland return (ENOTDIR); 530 1.1 dholland 531 1.5 dholland dirbuf = kmem_alloc(LFS_DIRBLKSIZ, KM_SLEEP); 532 1.1 dholland 533 1.1 dholland auio.uio_iov = &aiov; 534 1.1 dholland auio.uio_iovcnt = 1; 535 1.1 dholland auio.uio_rw = UIO_READ; 536 1.1 dholland auio.uio_offset = 0; 537 1.1 dholland UIO_SETUP_SYSSPACE(&auio); 538 1.1 dholland 539 1.1 dholland vargs.a_desc = NULL; 540 1.1 dholland vargs.a_vp = dvp; 541 1.1 dholland vargs.a_uio = &auio; 542 1.1 dholland vargs.a_cred = l->l_cred; 543 1.1 dholland vargs.a_eofflag = &eofflag; 544 1.1 dholland vargs.a_ncookies = NULL; 545 1.1 dholland vargs.a_cookies = NULL; 546 1.1 dholland 547 1.1 dholland while (!eofflag) { 548 1.5 dholland auio.uio_resid = LFS_DIRBLKSIZ; 549 1.1 dholland aiov.iov_base = dirbuf; 550 1.5 dholland aiov.iov_len = LFS_DIRBLKSIZ; 551 1.4 dholland error = ulfs_readdir(&vargs); 552 1.1 dholland if (error) { 553 1.4 dholland printf("ulfs_extattr_iterate_directory: ulfs_readdir " 554 1.1 dholland "%d\n", error); 555 1.1 dholland return (error); 556 1.1 dholland } 557 1.1 dholland 558 1.1 dholland /* 559 1.5 dholland * XXXRW: While in LFS, we always get LFS_DIRBLKSIZ returns from 560 1.1 dholland * the directory code on success, on other file systems this 561 1.1 dholland * may not be the case. For portability, we should check the 562 1.4 dholland * read length on return from ulfs_readdir(). 563 1.1 dholland */ 564 1.5 dholland edp = (struct dirent *)&dirbuf[LFS_DIRBLKSIZ]; 565 1.1 dholland for (dp = (struct dirent *)dirbuf; dp < edp; ) { 566 1.1 dholland if (dp->d_reclen == 0) 567 1.1 dholland break; 568 1.1 dholland /* Skip "." and ".." */ 569 1.1 dholland if (dp->d_name[0] == '.' && 570 1.1 dholland (dp->d_name[1] == '\0' || 571 1.1 dholland (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) 572 1.1 dholland goto next; 573 1.4 dholland error = ulfs_extattr_lookup(dvp, LOCKPARENT, 574 1.1 dholland dp->d_name, &attr_vp, l); 575 1.1 dholland if (error == ENOENT) { 576 1.1 dholland goto next; /* keep silent */ 577 1.1 dholland } else if (error) { 578 1.4 dholland printf("ulfs_extattr_iterate_directory: lookup " 579 1.1 dholland "%s %d\n", dp->d_name, error); 580 1.1 dholland } else if (attr_vp == dvp) { 581 1.1 dholland vrele(attr_vp); 582 1.1 dholland } else if (attr_vp->v_type != VREG) { 583 1.1 dholland vput(attr_vp); 584 1.1 dholland } else { 585 1.4 dholland error = ulfs_extattr_enable_with_open(ump, 586 1.1 dholland attr_vp, attrnamespace, dp->d_name, l); 587 1.1 dholland vrele(attr_vp); 588 1.1 dholland if (error) { 589 1.4 dholland printf("ulfs_extattr_iterate_directory: " 590 1.1 dholland "enable %s %d\n", dp->d_name, 591 1.1 dholland error); 592 1.1 dholland } else if (bootverbose) { 593 1.1 dholland printf("%s: EA %s loaded\n", 594 1.1 dholland sbp->f_mntonname, dp->d_name); 595 1.1 dholland } 596 1.1 dholland } 597 1.1 dholland next: 598 1.1 dholland dp = (struct dirent *) ((char *)dp + dp->d_reclen); 599 1.1 dholland if (dp >= edp) 600 1.1 dholland break; 601 1.1 dholland } 602 1.1 dholland } 603 1.5 dholland kmem_free(dirbuf, LFS_DIRBLKSIZ); 604 1.1 dholland 605 1.1 dholland return (0); 606 1.1 dholland } 607 1.1 dholland 608 1.1 dholland /* 609 1.1 dholland * Auto-start of extended attributes, to be executed (optionally) at 610 1.1 dholland * mount-time. 611 1.1 dholland */ 612 1.1 dholland int 613 1.4 dholland ulfs_extattr_autostart(struct mount *mp, struct lwp *l) 614 1.1 dholland { 615 1.1 dholland struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp; 616 1.1 dholland int error; 617 1.1 dholland 618 1.1 dholland /* 619 1.4 dholland * Does ULFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root? 620 1.1 dholland * If so, automatically start EA's. 621 1.1 dholland */ 622 1.15 ad error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp); 623 1.1 dholland if (error) { 624 1.4 dholland printf("ulfs_extattr_autostart.VFS_ROOT() returned %d\n", 625 1.1 dholland error); 626 1.1 dholland return (error); 627 1.1 dholland } 628 1.1 dholland 629 1.1 dholland KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE); 630 1.1 dholland 631 1.4 dholland error = ulfs_extattr_lookup(rvp, 0, 632 1.4 dholland ULFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, l); 633 1.1 dholland if (error) { 634 1.1 dholland /* rvp ref'd but now unlocked */ 635 1.1 dholland KASSERT(VOP_ISLOCKED(rvp) == 0); 636 1.1 dholland vrele(rvp); 637 1.1 dholland return (error); 638 1.1 dholland } 639 1.1 dholland if (rvp == attr_dvp) { 640 1.1 dholland /* Should never happen. */ 641 1.1 dholland KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE); 642 1.1 dholland vrele(attr_dvp); 643 1.1 dholland vput(rvp); 644 1.1 dholland return (EINVAL); 645 1.1 dholland } 646 1.1 dholland KASSERT(VOP_ISLOCKED(rvp) == 0); 647 1.1 dholland vrele(rvp); 648 1.1 dholland 649 1.1 dholland KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE); 650 1.1 dholland 651 1.1 dholland if (attr_dvp->v_type != VDIR) { 652 1.4 dholland printf("ulfs_extattr_autostart: %s != VDIR\n", 653 1.4 dholland ULFS_EXTATTR_FSROOTSUBDIR); 654 1.1 dholland goto return_vput_attr_dvp; 655 1.1 dholland } 656 1.1 dholland 657 1.4 dholland error = ulfs_extattr_start(mp, l); 658 1.1 dholland if (error) { 659 1.4 dholland printf("ulfs_extattr_autostart: ulfs_extattr_start failed (%d)\n", 660 1.1 dholland error); 661 1.1 dholland goto return_vput_attr_dvp; 662 1.1 dholland } 663 1.1 dholland 664 1.1 dholland /* 665 1.4 dholland * Look for two subdirectories: ULFS_EXTATTR_SUBDIR_SYSTEM, 666 1.4 dholland * ULFS_EXTATTR_SUBDIR_USER. For each, iterate over the sub-directory, 667 1.1 dholland * and start with appropriate type. Failures in either don't 668 1.1 dholland * result in an over-all failure. attr_dvp is left locked to 669 1.1 dholland * be cleaned up on exit. 670 1.1 dholland */ 671 1.4 dholland error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT, 672 1.4 dholland ULFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, l); 673 1.1 dholland KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE); 674 1.1 dholland if (error == 0) { 675 1.1 dholland KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE); 676 1.4 dholland error = ulfs_extattr_iterate_directory(VFSTOULFS(mp), 677 1.1 dholland attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, l); 678 1.1 dholland if (error) 679 1.4 dholland printf("ulfs_extattr_iterate_directory returned %d\n", 680 1.1 dholland error); 681 1.1 dholland KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE); 682 1.1 dholland vput(attr_system_dvp); 683 1.1 dholland } 684 1.1 dholland 685 1.4 dholland error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT, 686 1.4 dholland ULFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, l); 687 1.1 dholland KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE); 688 1.1 dholland if (error == 0) { 689 1.1 dholland KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE); 690 1.4 dholland error = ulfs_extattr_iterate_directory(VFSTOULFS(mp), 691 1.1 dholland attr_user_dvp, EXTATTR_NAMESPACE_USER, l); 692 1.1 dholland if (error) 693 1.4 dholland printf("ulfs_extattr_iterate_directory returned %d\n", 694 1.1 dholland error); 695 1.1 dholland KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE); 696 1.1 dholland vput(attr_user_dvp); 697 1.1 dholland } 698 1.1 dholland 699 1.1 dholland /* Mask startup failures in sub-directories. */ 700 1.1 dholland error = 0; 701 1.1 dholland 702 1.1 dholland return_vput_attr_dvp: 703 1.1 dholland KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE); 704 1.1 dholland vput(attr_dvp); 705 1.1 dholland 706 1.1 dholland return (error); 707 1.1 dholland } 708 1.1 dholland 709 1.1 dholland /* 710 1.1 dholland * Stop extended attribute support on an FS. 711 1.1 dholland */ 712 1.1 dholland void 713 1.4 dholland ulfs_extattr_stop(struct mount *mp, struct lwp *l) 714 1.1 dholland { 715 1.4 dholland struct ulfs_extattr_list_entry *uele; 716 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 717 1.1 dholland 718 1.4 dholland ulfs_extattr_uepm_lock(ump); 719 1.1 dholland 720 1.1 dholland /* 721 1.1 dholland * If we haven't been started, no big deal. Just short-circuit 722 1.1 dholland * the processing work. 723 1.1 dholland */ 724 1.4 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) { 725 1.1 dholland goto unlock; 726 1.1 dholland } 727 1.1 dholland 728 1.1 dholland while (LIST_FIRST(&ump->um_extattr.uepm_list) != NULL) { 729 1.1 dholland uele = LIST_FIRST(&ump->um_extattr.uepm_list); 730 1.4 dholland ulfs_extattr_disable(ump, uele->uele_attrnamespace, 731 1.1 dholland uele->uele_attrname, l); 732 1.1 dholland } 733 1.1 dholland 734 1.4 dholland ump->um_extattr.uepm_flags &= ~ULFS_EXTATTR_UEPM_STARTED; 735 1.1 dholland 736 1.1 dholland kauth_cred_free(ump->um_extattr.uepm_ucred); 737 1.1 dholland ump->um_extattr.uepm_ucred = NULL; 738 1.1 dholland 739 1.1 dholland unlock: 740 1.4 dholland ulfs_extattr_uepm_unlock(ump); 741 1.1 dholland } 742 1.1 dholland 743 1.1 dholland /* 744 1.1 dholland * Enable a named attribute on the specified filesystem; provide an 745 1.1 dholland * unlocked backing vnode to hold the attribute data. 746 1.1 dholland */ 747 1.1 dholland static int 748 1.4 dholland ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace, 749 1.1 dholland const char *attrname, struct vnode *backing_vnode, struct lwp *l) 750 1.1 dholland { 751 1.4 dholland struct ulfs_extattr_list_entry *attribute; 752 1.1 dholland struct iovec aiov; 753 1.1 dholland struct uio auio; 754 1.1 dholland int error = 0; 755 1.1 dholland 756 1.4 dholland if (!ulfs_extattr_valid_attrname(attrnamespace, attrname)) 757 1.1 dholland return (EINVAL); 758 1.1 dholland if (backing_vnode->v_type != VREG) 759 1.1 dholland return (EINVAL); 760 1.1 dholland 761 1.1 dholland attribute = kmem_zalloc(sizeof(*attribute), KM_SLEEP); 762 1.1 dholland 763 1.4 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) { 764 1.1 dholland error = EOPNOTSUPP; 765 1.1 dholland goto free_exit; 766 1.1 dholland } 767 1.1 dholland 768 1.4 dholland if (ulfs_extattr_find_attr(ump, attrnamespace, attrname)) { 769 1.1 dholland error = EEXIST; 770 1.1 dholland goto free_exit; 771 1.1 dholland } 772 1.1 dholland 773 1.1 dholland strncpy(attribute->uele_attrname, attrname, 774 1.4 dholland ULFS_EXTATTR_MAXEXTATTRNAME); 775 1.1 dholland attribute->uele_attrnamespace = attrnamespace; 776 1.1 dholland memset(&attribute->uele_fileheader, 0, 777 1.4 dholland sizeof(struct ulfs_extattr_fileheader)); 778 1.1 dholland 779 1.1 dholland attribute->uele_backing_vnode = backing_vnode; 780 1.1 dholland 781 1.1 dholland auio.uio_iov = &aiov; 782 1.1 dholland auio.uio_iovcnt = 1; 783 1.1 dholland aiov.iov_base = (void *) &attribute->uele_fileheader; 784 1.4 dholland aiov.iov_len = sizeof(struct ulfs_extattr_fileheader); 785 1.4 dholland auio.uio_resid = sizeof(struct ulfs_extattr_fileheader); 786 1.1 dholland auio.uio_offset = (off_t) 0; 787 1.1 dholland auio.uio_rw = UIO_READ; 788 1.1 dholland UIO_SETUP_SYSSPACE(&auio); 789 1.1 dholland 790 1.1 dholland vn_lock(backing_vnode, LK_SHARED | LK_RETRY); 791 1.1 dholland error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED, 792 1.1 dholland ump->um_extattr.uepm_ucred); 793 1.1 dholland 794 1.1 dholland if (error) 795 1.1 dholland goto unlock_free_exit; 796 1.1 dholland 797 1.1 dholland if (auio.uio_resid != 0) { 798 1.4 dholland printf("ulfs_extattr_enable: malformed attribute header\n"); 799 1.1 dholland error = EINVAL; 800 1.1 dholland goto unlock_free_exit; 801 1.1 dholland } 802 1.1 dholland 803 1.1 dholland /* 804 1.1 dholland * Try to determine the byte order of the attribute file. 805 1.1 dholland */ 806 1.4 dholland if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) { 807 1.1 dholland attribute->uele_flags |= UELE_F_NEEDSWAP; 808 1.1 dholland attribute->uele_fileheader.uef_magic = 809 1.4 dholland ulfs_rw32(attribute->uele_fileheader.uef_magic, 810 1.1 dholland UELE_NEEDSWAP(attribute)); 811 1.4 dholland if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) { 812 1.4 dholland printf("ulfs_extattr_enable: invalid attribute header " 813 1.1 dholland "magic\n"); 814 1.1 dholland error = EINVAL; 815 1.1 dholland goto unlock_free_exit; 816 1.1 dholland } 817 1.1 dholland } 818 1.1 dholland attribute->uele_fileheader.uef_version = 819 1.4 dholland ulfs_rw32(attribute->uele_fileheader.uef_version, 820 1.1 dholland UELE_NEEDSWAP(attribute)); 821 1.1 dholland attribute->uele_fileheader.uef_size = 822 1.4 dholland ulfs_rw32(attribute->uele_fileheader.uef_size, 823 1.1 dholland UELE_NEEDSWAP(attribute)); 824 1.1 dholland 825 1.4 dholland if (attribute->uele_fileheader.uef_version != ULFS_EXTATTR_VERSION) { 826 1.4 dholland printf("ulfs_extattr_enable: incorrect attribute header " 827 1.1 dholland "version\n"); 828 1.1 dholland error = EINVAL; 829 1.1 dholland goto unlock_free_exit; 830 1.1 dholland } 831 1.1 dholland 832 1.1 dholland LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute, 833 1.1 dholland uele_entries); 834 1.1 dholland 835 1.1 dholland VOP_UNLOCK(backing_vnode); 836 1.1 dholland return (0); 837 1.1 dholland 838 1.1 dholland unlock_free_exit: 839 1.1 dholland VOP_UNLOCK(backing_vnode); 840 1.1 dholland 841 1.1 dholland free_exit: 842 1.1 dholland kmem_free(attribute, sizeof(*attribute)); 843 1.1 dholland return (error); 844 1.1 dholland } 845 1.1 dholland 846 1.1 dholland /* 847 1.1 dholland * Disable extended attribute support on an FS. 848 1.1 dholland */ 849 1.1 dholland static int 850 1.4 dholland ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace, 851 1.1 dholland const char *attrname, struct lwp *l) 852 1.1 dholland { 853 1.4 dholland struct ulfs_extattr_list_entry *uele; 854 1.1 dholland int error = 0; 855 1.1 dholland 856 1.4 dholland if (!ulfs_extattr_valid_attrname(attrnamespace, attrname)) 857 1.1 dholland return (EINVAL); 858 1.1 dholland 859 1.4 dholland uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname); 860 1.1 dholland if (!uele) 861 1.1 dholland return (ENODATA); 862 1.1 dholland 863 1.1 dholland LIST_REMOVE(uele, uele_entries); 864 1.1 dholland 865 1.1 dholland error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE, 866 1.1 dholland l->l_cred); 867 1.1 dholland 868 1.1 dholland kmem_free(uele, sizeof(*uele)); 869 1.1 dholland 870 1.1 dholland return (error); 871 1.1 dholland } 872 1.1 dholland 873 1.1 dholland /* 874 1.4 dholland * VFS call to manage extended attributes in ULFS. If filename_vp is 875 1.1 dholland * non-NULL, it must be passed in locked, and regardless of errors in 876 1.1 dholland * processing, will be unlocked. 877 1.1 dholland */ 878 1.1 dholland int 879 1.4 dholland ulfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 880 1.1 dholland int attrnamespace, const char *attrname) 881 1.1 dholland { 882 1.1 dholland struct lwp *l = curlwp; 883 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 884 1.1 dholland int error; 885 1.1 dholland 886 1.1 dholland /* 887 1.1 dholland * Only privileged processes can configure extended attributes. 888 1.1 dholland */ 889 1.1 dholland error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_EXTATTR, 890 1.1 dholland 0, mp, NULL, NULL); 891 1.1 dholland if (error) { 892 1.1 dholland if (filename_vp != NULL) 893 1.1 dholland VOP_UNLOCK(filename_vp); 894 1.1 dholland return (error); 895 1.1 dholland } 896 1.1 dholland 897 1.1 dholland switch(cmd) { 898 1.4 dholland case ULFS_EXTATTR_CMD_START: 899 1.1 dholland if (filename_vp != NULL) { 900 1.1 dholland VOP_UNLOCK(filename_vp); 901 1.1 dholland return (EINVAL); 902 1.1 dholland } 903 1.1 dholland if (attrname != NULL) 904 1.1 dholland return (EINVAL); 905 1.1 dholland 906 1.4 dholland error = ulfs_extattr_autostart(mp, l); 907 1.1 dholland return (error); 908 1.1 dholland 909 1.4 dholland case ULFS_EXTATTR_CMD_STOP: 910 1.1 dholland if (filename_vp != NULL) { 911 1.1 dholland VOP_UNLOCK(filename_vp); 912 1.1 dholland return (EINVAL); 913 1.1 dholland } 914 1.1 dholland if (attrname != NULL) 915 1.1 dholland return (EINVAL); 916 1.1 dholland 917 1.4 dholland ulfs_extattr_stop(mp, l); 918 1.1 dholland return (0); 919 1.1 dholland 920 1.4 dholland case ULFS_EXTATTR_CMD_ENABLE: 921 1.1 dholland if (filename_vp == NULL) 922 1.1 dholland return (EINVAL); 923 1.1 dholland if (attrname == NULL) { 924 1.1 dholland VOP_UNLOCK(filename_vp); 925 1.1 dholland return (EINVAL); 926 1.1 dholland } 927 1.1 dholland 928 1.1 dholland /* 929 1.4 dholland * ulfs_extattr_enable_with_open() will always unlock the 930 1.1 dholland * vnode, regardless of failure. 931 1.1 dholland */ 932 1.4 dholland ulfs_extattr_uepm_lock(ump); 933 1.4 dholland error = ulfs_extattr_enable_with_open(ump, filename_vp, 934 1.1 dholland attrnamespace, attrname, l); 935 1.4 dholland ulfs_extattr_uepm_unlock(ump); 936 1.1 dholland return (error); 937 1.1 dholland 938 1.4 dholland case ULFS_EXTATTR_CMD_DISABLE: 939 1.1 dholland if (filename_vp != NULL) { 940 1.1 dholland VOP_UNLOCK(filename_vp); 941 1.1 dholland return (EINVAL); 942 1.1 dholland } 943 1.1 dholland if (attrname == NULL) 944 1.1 dholland return (EINVAL); 945 1.1 dholland 946 1.4 dholland ulfs_extattr_uepm_lock(ump); 947 1.4 dholland error = ulfs_extattr_disable(ump, attrnamespace, attrname, l); 948 1.4 dholland ulfs_extattr_uepm_unlock(ump); 949 1.1 dholland return (error); 950 1.1 dholland 951 1.1 dholland default: 952 1.1 dholland return (EINVAL); 953 1.1 dholland } 954 1.1 dholland } 955 1.1 dholland 956 1.1 dholland /* 957 1.1 dholland * Read extended attribute header for a given vnode and attribute. 958 1.1 dholland * Backing vnode should be locked and unlocked by caller. 959 1.1 dholland */ 960 1.1 dholland static int 961 1.4 dholland ulfs_extattr_get_header(struct vnode *vp, struct ulfs_extattr_list_entry *uele, 962 1.4 dholland struct ulfs_extattr_header *ueh, off_t *bap) 963 1.1 dholland { 964 1.1 dholland struct mount *mp = vp->v_mount; 965 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 966 1.1 dholland struct inode *ip = VTOI(vp); 967 1.1 dholland off_t base_offset; 968 1.1 dholland struct iovec aiov; 969 1.1 dholland struct uio aio; 970 1.1 dholland int error; 971 1.1 dholland 972 1.1 dholland /* 973 1.1 dholland * Find base offset of header in file based on file header size, and 974 1.1 dholland * data header size + maximum data size, indexed by inode number. 975 1.1 dholland */ 976 1.4 dholland base_offset = sizeof(struct ulfs_extattr_fileheader) + 977 1.4 dholland ip->i_number * (sizeof(struct ulfs_extattr_header) + 978 1.1 dholland uele->uele_fileheader.uef_size); 979 1.1 dholland 980 1.1 dholland /* 981 1.1 dholland * Read in the data header to see if the data is defined, and if so 982 1.1 dholland * how much. 983 1.1 dholland */ 984 1.4 dholland memset(ueh, 0, sizeof(struct ulfs_extattr_header)); 985 1.1 dholland aiov.iov_base = ueh; 986 1.4 dholland aiov.iov_len = sizeof(struct ulfs_extattr_header); 987 1.1 dholland aio.uio_iov = &aiov; 988 1.1 dholland aio.uio_iovcnt = 1; 989 1.1 dholland aio.uio_rw = UIO_READ; 990 1.1 dholland aio.uio_offset = base_offset; 991 1.4 dholland aio.uio_resid = sizeof(struct ulfs_extattr_header); 992 1.1 dholland UIO_SETUP_SYSSPACE(&aio); 993 1.1 dholland 994 1.1 dholland error = VOP_READ(uele->uele_backing_vnode, &aio, 995 1.1 dholland IO_NODELOCKED, ump->um_extattr.uepm_ucred); 996 1.1 dholland if (error) 997 1.1 dholland return error; 998 1.1 dholland 999 1.1 dholland /* 1000 1.1 dholland * Attribute headers are kept in file system byte order. 1001 1.1 dholland * XXX What about the blob of data? 1002 1.1 dholland */ 1003 1.4 dholland ueh->ueh_flags = ulfs_rw32(ueh->ueh_flags, UELE_NEEDSWAP(uele)); 1004 1.4 dholland ueh->ueh_len = ulfs_rw32(ueh->ueh_len, UELE_NEEDSWAP(uele)); 1005 1.4 dholland ueh->ueh_i_gen = ulfs_rw32(ueh->ueh_i_gen, UELE_NEEDSWAP(uele)); 1006 1.1 dholland 1007 1.1 dholland /* Defined? */ 1008 1.4 dholland if ((ueh->ueh_flags & ULFS_EXTATTR_ATTR_FLAG_INUSE) == 0) 1009 1.1 dholland return ENODATA; 1010 1.1 dholland 1011 1.1 dholland /* Valid for the current inode generation? */ 1012 1.1 dholland if (ueh->ueh_i_gen != ip->i_gen) { 1013 1.1 dholland /* 1014 1.1 dholland * The inode itself has a different generation number 1015 1.1 dholland * than the uele data. For now, the best solution 1016 1.1 dholland * is to coerce this to undefined, and let it get cleaned 1017 1.1 dholland * up by the next write or extattrctl clean. 1018 1.1 dholland */ 1019 1.1 dholland printf("%s (%s): inode gen inconsistency (%u, %jd)\n", 1020 1.1 dholland __func__, mp->mnt_stat.f_mntonname, ueh->ueh_i_gen, 1021 1.1 dholland (intmax_t)ip->i_gen); 1022 1.1 dholland return ENODATA; 1023 1.1 dholland } 1024 1.1 dholland 1025 1.1 dholland /* Local size consistency check. */ 1026 1.1 dholland if (ueh->ueh_len > uele->uele_fileheader.uef_size) 1027 1.1 dholland return ENXIO; 1028 1.1 dholland 1029 1.1 dholland /* Return base offset */ 1030 1.1 dholland if (bap != NULL) 1031 1.1 dholland *bap = base_offset; 1032 1.1 dholland 1033 1.1 dholland return 0; 1034 1.1 dholland } 1035 1.1 dholland 1036 1.1 dholland /* 1037 1.1 dholland * Vnode operation to retrieve a named extended attribute. 1038 1.1 dholland */ 1039 1.1 dholland int 1040 1.4 dholland ulfs_getextattr(struct vop_getextattr_args *ap) 1041 1.1 dholland /* 1042 1.1 dholland vop_getextattr { 1043 1.1 dholland IN struct vnode *a_vp; 1044 1.1 dholland IN int a_attrnamespace; 1045 1.1 dholland IN const char *a_name; 1046 1.1 dholland INOUT struct uio *a_uio; 1047 1.1 dholland OUT size_t *a_size; 1048 1.1 dholland IN kauth_cred_t a_cred; 1049 1.1 dholland }; 1050 1.1 dholland */ 1051 1.1 dholland { 1052 1.1 dholland struct mount *mp = ap->a_vp->v_mount; 1053 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1054 1.1 dholland int error; 1055 1.1 dholland 1056 1.12 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 1057 1.12 dholland return (EOPNOTSUPP); 1058 1.12 dholland 1059 1.4 dholland ulfs_extattr_uepm_lock(ump); 1060 1.1 dholland 1061 1.4 dholland error = ulfs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name, 1062 1.1 dholland ap->a_uio, ap->a_size, ap->a_cred, curlwp); 1063 1.1 dholland 1064 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1065 1.1 dholland 1066 1.1 dholland return (error); 1067 1.1 dholland } 1068 1.1 dholland 1069 1.1 dholland /* 1070 1.1 dholland * Real work associated with retrieving a named attribute--assumes that 1071 1.1 dholland * the attribute lock has already been grabbed. 1072 1.1 dholland */ 1073 1.1 dholland static int 1074 1.4 dholland ulfs_extattr_get(struct vnode *vp, int attrnamespace, const char *name, 1075 1.1 dholland struct uio *uio, size_t *size, kauth_cred_t cred, struct lwp *l) 1076 1.1 dholland { 1077 1.4 dholland struct ulfs_extattr_list_entry *attribute; 1078 1.4 dholland struct ulfs_extattr_header ueh; 1079 1.1 dholland struct mount *mp = vp->v_mount; 1080 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1081 1.1 dholland off_t base_offset; 1082 1.1 dholland size_t len, old_len; 1083 1.1 dholland int error = 0; 1084 1.1 dholland 1085 1.1 dholland if (strlen(name) == 0) 1086 1.1 dholland return (EINVAL); 1087 1.1 dholland 1088 1.16 christos error = extattr_check_cred(vp, attrnamespace, cred, VREAD); 1089 1.1 dholland if (error) 1090 1.1 dholland return (error); 1091 1.1 dholland 1092 1.4 dholland attribute = ulfs_extattr_find_attr(ump, attrnamespace, name); 1093 1.1 dholland if (!attribute) 1094 1.1 dholland return (ENODATA); 1095 1.1 dholland 1096 1.1 dholland /* 1097 1.1 dholland * Allow only offsets of zero to encourage the read/replace 1098 1.1 dholland * extended attribute semantic. Otherwise we can't guarantee 1099 1.1 dholland * atomicity, as we don't provide locks for extended attributes. 1100 1.1 dholland */ 1101 1.1 dholland if (uio != NULL && uio->uio_offset != 0) 1102 1.1 dholland return (ENXIO); 1103 1.1 dholland 1104 1.1 dholland /* 1105 1.1 dholland * Don't need to get a lock on the backing file if the getattr is 1106 1.1 dholland * being applied to the backing file, as the lock is already held. 1107 1.1 dholland */ 1108 1.1 dholland if (attribute->uele_backing_vnode != vp) 1109 1.1 dholland vn_lock(attribute->uele_backing_vnode, LK_SHARED | LK_RETRY); 1110 1.1 dholland 1111 1.4 dholland error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset); 1112 1.1 dholland if (error) 1113 1.1 dholland goto vopunlock_exit; 1114 1.1 dholland 1115 1.1 dholland /* Return full data size if caller requested it. */ 1116 1.1 dholland if (size != NULL) 1117 1.1 dholland *size = ueh.ueh_len; 1118 1.1 dholland 1119 1.1 dholland /* Return data if the caller requested it. */ 1120 1.1 dholland if (uio != NULL) { 1121 1.1 dholland /* Allow for offset into the attribute data. */ 1122 1.1 dholland uio->uio_offset = base_offset + sizeof(struct 1123 1.4 dholland ulfs_extattr_header); 1124 1.1 dholland 1125 1.1 dholland /* 1126 1.1 dholland * Figure out maximum to transfer -- use buffer size and 1127 1.1 dholland * local data limit. 1128 1.1 dholland */ 1129 1.1 dholland len = MIN(uio->uio_resid, ueh.ueh_len); 1130 1.1 dholland old_len = uio->uio_resid; 1131 1.1 dholland uio->uio_resid = len; 1132 1.1 dholland 1133 1.1 dholland error = VOP_READ(attribute->uele_backing_vnode, uio, 1134 1.1 dholland IO_NODELOCKED, ump->um_extattr.uepm_ucred); 1135 1.1 dholland if (error) 1136 1.1 dholland goto vopunlock_exit; 1137 1.1 dholland 1138 1.1 dholland uio->uio_resid = old_len - (len - uio->uio_resid); 1139 1.1 dholland } 1140 1.1 dholland 1141 1.1 dholland vopunlock_exit: 1142 1.1 dholland 1143 1.1 dholland if (uio != NULL) 1144 1.1 dholland uio->uio_offset = 0; 1145 1.1 dholland 1146 1.1 dholland if (attribute->uele_backing_vnode != vp) 1147 1.1 dholland VOP_UNLOCK(attribute->uele_backing_vnode); 1148 1.1 dholland 1149 1.1 dholland return (error); 1150 1.1 dholland } 1151 1.1 dholland 1152 1.1 dholland /* 1153 1.1 dholland * Vnode operation to list extended attribute for a vnode 1154 1.1 dholland */ 1155 1.1 dholland int 1156 1.4 dholland ulfs_listextattr(struct vop_listextattr_args *ap) 1157 1.1 dholland /* 1158 1.1 dholland vop_listextattr { 1159 1.1 dholland IN struct vnode *a_vp; 1160 1.1 dholland IN int a_attrnamespace; 1161 1.1 dholland INOUT struct uio *a_uio; 1162 1.1 dholland OUT size_t *a_size; 1163 1.1 dholland IN int flag; 1164 1.1 dholland IN kauth_cred_t a_cred; 1165 1.1 dholland struct proc *a_p; 1166 1.1 dholland }; 1167 1.1 dholland */ 1168 1.1 dholland { 1169 1.1 dholland struct mount *mp = ap->a_vp->v_mount; 1170 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1171 1.1 dholland int error; 1172 1.1 dholland 1173 1.12 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 1174 1.12 dholland return (EOPNOTSUPP); 1175 1.12 dholland 1176 1.4 dholland ulfs_extattr_uepm_lock(ump); 1177 1.1 dholland 1178 1.4 dholland error = ulfs_extattr_list(ap->a_vp, ap->a_attrnamespace, 1179 1.1 dholland ap->a_uio, ap->a_size, ap->a_flag, ap->a_cred, curlwp); 1180 1.1 dholland 1181 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1182 1.1 dholland 1183 1.1 dholland return (error); 1184 1.1 dholland } 1185 1.1 dholland 1186 1.1 dholland /* 1187 1.1 dholland * Real work associated with retrieving list of attributes--assumes that 1188 1.1 dholland * the attribute lock has already been grabbed. 1189 1.1 dholland */ 1190 1.1 dholland static int 1191 1.4 dholland ulfs_extattr_list(struct vnode *vp, int attrnamespace, 1192 1.1 dholland struct uio *uio, size_t *size, int flag, 1193 1.1 dholland kauth_cred_t cred, struct lwp *l) 1194 1.1 dholland { 1195 1.4 dholland struct ulfs_extattr_list_entry *uele; 1196 1.4 dholland struct ulfs_extattr_header ueh; 1197 1.1 dholland struct mount *mp = vp->v_mount; 1198 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1199 1.1 dholland size_t listsize = 0; 1200 1.1 dholland int error = 0; 1201 1.1 dholland 1202 1.1 dholland /* 1203 1.1 dholland * XXX: We can move this inside the loop and iterate on individual 1204 1.1 dholland * attributes. 1205 1.1 dholland */ 1206 1.16 christos error = extattr_check_cred(vp, attrnamespace, cred, VREAD); 1207 1.1 dholland if (error) 1208 1.1 dholland return (error); 1209 1.1 dholland 1210 1.1 dholland LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries) { 1211 1.1 dholland unsigned char attrnamelen; 1212 1.1 dholland 1213 1.1 dholland if (uele->uele_attrnamespace != attrnamespace) 1214 1.1 dholland continue; 1215 1.1 dholland 1216 1.4 dholland error = ulfs_extattr_get_header(vp, uele, &ueh, NULL); 1217 1.1 dholland if (error == ENODATA) 1218 1.13 msaitoh continue; 1219 1.1 dholland if (error != 0) 1220 1.1 dholland return error; 1221 1.1 dholland 1222 1.1 dholland /* 1223 1.1 dholland * Don't need to get a lock on the backing file if 1224 1.1 dholland * the listattr is being applied to the backing file, 1225 1.1 dholland * as the lock is already held. 1226 1.1 dholland */ 1227 1.1 dholland if (uele->uele_backing_vnode != vp) 1228 1.1 dholland vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY); 1229 1.1 dholland 1230 1.1 dholland /* 1231 1.1 dholland * +1 for trailing NUL (listxattr flavor) 1232 1.1 dholland * or leading name length (extattr_list_file flavor) 1233 1.1 dholland */ 1234 1.1 dholland attrnamelen = strlen(uele->uele_attrname); 1235 1.1 dholland listsize += attrnamelen + 1; 1236 1.1 dholland 1237 1.1 dholland /* Return data if the caller requested it. */ 1238 1.1 dholland if (uio != NULL) { 1239 1.1 dholland /* 1240 1.1 dholland * We support two flavors. Either NUL-terminated 1241 1.1 dholland * strings (a la listxattr), or non NUL-terminated, 1242 1.1 dholland * one byte length prefixed strings (for 1243 1.1 dholland * extattr_list_file). EXTATTR_LIST_LENPREFIX switches 1244 1.1 dholland * that second behavior. 1245 1.1 dholland */ 1246 1.1 dholland if (flag & EXTATTR_LIST_LENPREFIX) { 1247 1.1 dholland uint8_t len = (uint8_t)attrnamelen; 1248 1.1 dholland 1249 1.1 dholland /* Copy leading name length */ 1250 1.1 dholland error = uiomove(&len, sizeof(len), uio); 1251 1.1 dholland if (error != 0) 1252 1.13 msaitoh break; 1253 1.1 dholland } else { 1254 1.1 dholland /* Include trailing NULL */ 1255 1.13 msaitoh attrnamelen++; 1256 1.1 dholland } 1257 1.1 dholland 1258 1.1 dholland error = uiomove(uele->uele_attrname, 1259 1.1 dholland (size_t)attrnamelen, uio); 1260 1.1 dholland if (error != 0) 1261 1.13 msaitoh break; 1262 1.1 dholland } 1263 1.1 dholland 1264 1.1 dholland if (uele->uele_backing_vnode != vp) 1265 1.1 dholland VOP_UNLOCK(uele->uele_backing_vnode); 1266 1.1 dholland 1267 1.1 dholland if (error != 0) 1268 1.1 dholland return error; 1269 1.1 dholland } 1270 1.1 dholland 1271 1.1 dholland if (uio != NULL) 1272 1.1 dholland uio->uio_offset = 0; 1273 1.1 dholland 1274 1.1 dholland /* Return full data size if caller requested it. */ 1275 1.1 dholland if (size != NULL) 1276 1.1 dholland *size = listsize; 1277 1.1 dholland 1278 1.1 dholland return 0; 1279 1.1 dholland } 1280 1.1 dholland 1281 1.1 dholland /* 1282 1.1 dholland * Vnode operation to remove a named attribute. 1283 1.1 dholland */ 1284 1.1 dholland int 1285 1.4 dholland ulfs_deleteextattr(struct vop_deleteextattr_args *ap) 1286 1.1 dholland /* 1287 1.1 dholland vop_deleteextattr { 1288 1.1 dholland IN struct vnode *a_vp; 1289 1.1 dholland IN int a_attrnamespace; 1290 1.1 dholland IN const char *a_name; 1291 1.1 dholland IN kauth_cred_t a_cred; 1292 1.1 dholland }; 1293 1.1 dholland */ 1294 1.1 dholland { 1295 1.1 dholland struct mount *mp = ap->a_vp->v_mount; 1296 1.13 msaitoh struct ulfsmount *ump = VFSTOULFS(mp); 1297 1.1 dholland int error; 1298 1.1 dholland 1299 1.12 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 1300 1.12 dholland return (EOPNOTSUPP); 1301 1.12 dholland 1302 1.4 dholland ulfs_extattr_uepm_lock(ump); 1303 1.1 dholland 1304 1.4 dholland error = ulfs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name, 1305 1.1 dholland ap->a_cred, curlwp); 1306 1.1 dholland 1307 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1308 1.1 dholland 1309 1.1 dholland return (error); 1310 1.1 dholland } 1311 1.1 dholland 1312 1.1 dholland /* 1313 1.1 dholland * Vnode operation to set a named attribute. 1314 1.1 dholland */ 1315 1.1 dholland int 1316 1.4 dholland ulfs_setextattr(struct vop_setextattr_args *ap) 1317 1.1 dholland /* 1318 1.1 dholland vop_setextattr { 1319 1.1 dholland IN struct vnode *a_vp; 1320 1.1 dholland IN int a_attrnamespace; 1321 1.1 dholland IN const char *a_name; 1322 1.1 dholland INOUT struct uio *a_uio; 1323 1.1 dholland IN kauth_cred_t a_cred; 1324 1.1 dholland }; 1325 1.1 dholland */ 1326 1.1 dholland { 1327 1.1 dholland struct mount *mp = ap->a_vp->v_mount; 1328 1.13 msaitoh struct ulfsmount *ump = VFSTOULFS(mp); 1329 1.1 dholland int error; 1330 1.1 dholland 1331 1.12 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 1332 1.12 dholland return (EOPNOTSUPP); 1333 1.12 dholland 1334 1.4 dholland ulfs_extattr_uepm_lock(ump); 1335 1.1 dholland 1336 1.1 dholland /* 1337 1.1 dholland * XXX: No longer a supported way to delete extended attributes. 1338 1.1 dholland */ 1339 1.1 dholland if (ap->a_uio == NULL) { 1340 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1341 1.1 dholland return (EINVAL); 1342 1.1 dholland } 1343 1.1 dholland 1344 1.4 dholland error = ulfs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name, 1345 1.1 dholland ap->a_uio, ap->a_cred, curlwp); 1346 1.1 dholland 1347 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1348 1.1 dholland 1349 1.1 dholland return (error); 1350 1.1 dholland } 1351 1.1 dholland 1352 1.1 dholland /* 1353 1.1 dholland * Real work associated with setting a vnode's extended attributes; 1354 1.1 dholland * assumes that the attribute lock has already been grabbed. 1355 1.1 dholland */ 1356 1.1 dholland static int 1357 1.4 dholland ulfs_extattr_set(struct vnode *vp, int attrnamespace, const char *name, 1358 1.1 dholland struct uio *uio, kauth_cred_t cred, struct lwp *l) 1359 1.1 dholland { 1360 1.4 dholland struct ulfs_extattr_list_entry *attribute; 1361 1.4 dholland struct ulfs_extattr_header ueh; 1362 1.1 dholland struct iovec local_aiov; 1363 1.1 dholland struct uio local_aio; 1364 1.1 dholland struct mount *mp = vp->v_mount; 1365 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1366 1.1 dholland struct inode *ip = VTOI(vp); 1367 1.1 dholland off_t base_offset; 1368 1.1 dholland int error = 0, ioflag; 1369 1.1 dholland 1370 1.1 dholland if (vp->v_mount->mnt_flag & MNT_RDONLY) 1371 1.1 dholland return (EROFS); 1372 1.12 dholland 1373 1.4 dholland if (!ulfs_extattr_valid_attrname(attrnamespace, name)) 1374 1.1 dholland return (EINVAL); 1375 1.1 dholland 1376 1.16 christos error = extattr_check_cred(vp, attrnamespace, cred, VWRITE); 1377 1.1 dholland if (error) 1378 1.1 dholland return (error); 1379 1.1 dholland 1380 1.4 dholland attribute = ulfs_extattr_find_attr(ump, attrnamespace, name); 1381 1.1 dholland if (!attribute) { 1382 1.11 dholland error = ulfs_extattr_autocreate_attr(vp, attrnamespace, 1383 1.11 dholland name, l, &attribute); 1384 1.11 dholland if (error == EEXIST) { 1385 1.11 dholland /* Another thread raced us for backend creation */ 1386 1.11 dholland error = 0; 1387 1.11 dholland attribute = 1388 1.11 dholland ulfs_extattr_find_attr(ump, attrnamespace, name); 1389 1.11 dholland } 1390 1.11 dholland 1391 1.11 dholland if (error || !attribute) 1392 1.11 dholland return ENODATA; 1393 1.1 dholland } 1394 1.1 dholland 1395 1.1 dholland /* 1396 1.1 dholland * Early rejection of invalid offsets/length. 1397 1.1 dholland * Reject: any offset but 0 (replace) 1398 1.1 dholland * Any size greater than attribute size limit 1399 1.1 dholland */ 1400 1.1 dholland if (uio->uio_offset != 0 || 1401 1.1 dholland uio->uio_resid > attribute->uele_fileheader.uef_size) 1402 1.1 dholland return (ENXIO); 1403 1.1 dholland 1404 1.1 dholland /* 1405 1.1 dholland * Find base offset of header in file based on file header size, and 1406 1.1 dholland * data header size + maximum data size, indexed by inode number. 1407 1.1 dholland */ 1408 1.4 dholland base_offset = sizeof(struct ulfs_extattr_fileheader) + 1409 1.4 dholland ip->i_number * (sizeof(struct ulfs_extattr_header) + 1410 1.1 dholland attribute->uele_fileheader.uef_size); 1411 1.1 dholland 1412 1.1 dholland /* 1413 1.1 dholland * Write out a data header for the data. 1414 1.1 dholland */ 1415 1.4 dholland ueh.ueh_len = ulfs_rw32((uint32_t) uio->uio_resid, 1416 1.1 dholland UELE_NEEDSWAP(attribute)); 1417 1.4 dholland ueh.ueh_flags = ulfs_rw32(ULFS_EXTATTR_ATTR_FLAG_INUSE, 1418 1.1 dholland UELE_NEEDSWAP(attribute)); 1419 1.4 dholland ueh.ueh_i_gen = ulfs_rw32(ip->i_gen, UELE_NEEDSWAP(attribute)); 1420 1.1 dholland local_aiov.iov_base = &ueh; 1421 1.4 dholland local_aiov.iov_len = sizeof(struct ulfs_extattr_header); 1422 1.1 dholland local_aio.uio_iov = &local_aiov; 1423 1.1 dholland local_aio.uio_iovcnt = 1; 1424 1.1 dholland local_aio.uio_rw = UIO_WRITE; 1425 1.1 dholland local_aio.uio_offset = base_offset; 1426 1.4 dholland local_aio.uio_resid = sizeof(struct ulfs_extattr_header); 1427 1.1 dholland UIO_SETUP_SYSSPACE(&local_aio); 1428 1.1 dholland 1429 1.1 dholland /* 1430 1.1 dholland * Don't need to get a lock on the backing file if the setattr is 1431 1.1 dholland * being applied to the backing file, as the lock is already held. 1432 1.1 dholland */ 1433 1.1 dholland if (attribute->uele_backing_vnode != vp) 1434 1.1 dholland vn_lock(attribute->uele_backing_vnode, 1435 1.1 dholland LK_EXCLUSIVE | LK_RETRY); 1436 1.1 dholland 1437 1.1 dholland ioflag = IO_NODELOCKED; 1438 1.4 dholland if (ulfs_extattr_sync) 1439 1.1 dholland ioflag |= IO_SYNC; 1440 1.1 dholland error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag, 1441 1.1 dholland ump->um_extattr.uepm_ucred); 1442 1.1 dholland if (error) 1443 1.1 dholland goto vopunlock_exit; 1444 1.1 dholland 1445 1.1 dholland if (local_aio.uio_resid != 0) { 1446 1.1 dholland error = ENXIO; 1447 1.1 dholland goto vopunlock_exit; 1448 1.1 dholland } 1449 1.1 dholland 1450 1.1 dholland /* 1451 1.1 dholland * Write out user data. 1452 1.1 dholland * XXX NOT ATOMIC WITH RESPECT TO THE HEADER. 1453 1.1 dholland */ 1454 1.4 dholland uio->uio_offset = base_offset + sizeof(struct ulfs_extattr_header); 1455 1.1 dholland 1456 1.1 dholland ioflag = IO_NODELOCKED; 1457 1.4 dholland if (ulfs_extattr_sync) 1458 1.1 dholland ioflag |= IO_SYNC; 1459 1.1 dholland error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag, 1460 1.1 dholland ump->um_extattr.uepm_ucred); 1461 1.1 dholland 1462 1.1 dholland vopunlock_exit: 1463 1.1 dholland uio->uio_offset = 0; 1464 1.1 dholland 1465 1.1 dholland if (attribute->uele_backing_vnode != vp) 1466 1.1 dholland VOP_UNLOCK(attribute->uele_backing_vnode); 1467 1.1 dholland 1468 1.1 dholland return (error); 1469 1.1 dholland } 1470 1.1 dholland 1471 1.1 dholland /* 1472 1.1 dholland * Real work associated with removing an extended attribute from a vnode. 1473 1.1 dholland * Assumes the attribute lock has already been grabbed. 1474 1.1 dholland */ 1475 1.1 dholland static int 1476 1.4 dholland ulfs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name, 1477 1.1 dholland kauth_cred_t cred, struct lwp *l) 1478 1.1 dholland { 1479 1.4 dholland struct ulfs_extattr_list_entry *attribute; 1480 1.4 dholland struct ulfs_extattr_header ueh; 1481 1.1 dholland struct mount *mp = vp->v_mount; 1482 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1483 1.1 dholland struct iovec local_aiov; 1484 1.1 dholland struct uio local_aio; 1485 1.1 dholland off_t base_offset; 1486 1.1 dholland int error = 0, ioflag; 1487 1.1 dholland 1488 1.1 dholland if (vp->v_mount->mnt_flag & MNT_RDONLY) 1489 1.1 dholland return (EROFS); 1490 1.12 dholland 1491 1.4 dholland if (!ulfs_extattr_valid_attrname(attrnamespace, name)) 1492 1.1 dholland return (EINVAL); 1493 1.1 dholland 1494 1.16 christos error = extattr_check_cred(vp, attrnamespace, cred, VWRITE); 1495 1.1 dholland if (error) 1496 1.1 dholland return (error); 1497 1.1 dholland 1498 1.4 dholland attribute = ulfs_extattr_find_attr(ump, attrnamespace, name); 1499 1.1 dholland if (!attribute) 1500 1.1 dholland return (ENODATA); 1501 1.1 dholland 1502 1.1 dholland /* 1503 1.1 dholland * Don't need to get a lock on the backing file if the getattr is 1504 1.1 dholland * being applied to the backing file, as the lock is already held. 1505 1.1 dholland */ 1506 1.1 dholland if (attribute->uele_backing_vnode != vp) 1507 1.1 dholland vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY); 1508 1.1 dholland 1509 1.4 dholland error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset); 1510 1.1 dholland if (error) 1511 1.1 dholland goto vopunlock_exit; 1512 1.1 dholland 1513 1.1 dholland /* Flag it as not in use. */ 1514 1.1 dholland ueh.ueh_flags = 0; /* No need to byte swap 0 */ 1515 1.1 dholland ueh.ueh_len = 0; /* ...ditto... */ 1516 1.1 dholland 1517 1.1 dholland local_aiov.iov_base = &ueh; 1518 1.4 dholland local_aiov.iov_len = sizeof(struct ulfs_extattr_header); 1519 1.1 dholland local_aio.uio_iov = &local_aiov; 1520 1.1 dholland local_aio.uio_iovcnt = 1; 1521 1.1 dholland local_aio.uio_rw = UIO_WRITE; 1522 1.1 dholland local_aio.uio_offset = base_offset; 1523 1.4 dholland local_aio.uio_resid = sizeof(struct ulfs_extattr_header); 1524 1.1 dholland UIO_SETUP_SYSSPACE(&local_aio); 1525 1.1 dholland 1526 1.1 dholland ioflag = IO_NODELOCKED; 1527 1.4 dholland if (ulfs_extattr_sync) 1528 1.1 dholland ioflag |= IO_SYNC; 1529 1.1 dholland error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag, 1530 1.1 dholland ump->um_extattr.uepm_ucred); 1531 1.1 dholland if (error) 1532 1.1 dholland goto vopunlock_exit; 1533 1.1 dholland 1534 1.1 dholland if (local_aio.uio_resid != 0) 1535 1.1 dholland error = ENXIO; 1536 1.1 dholland 1537 1.1 dholland vopunlock_exit: 1538 1.1 dholland VOP_UNLOCK(attribute->uele_backing_vnode); 1539 1.1 dholland 1540 1.1 dholland return (error); 1541 1.1 dholland } 1542 1.1 dholland 1543 1.1 dholland /* 1544 1.4 dholland * Called by ULFS when an inode is no longer active and should have its 1545 1.1 dholland * attributes stripped. 1546 1.1 dholland */ 1547 1.1 dholland void 1548 1.4 dholland ulfs_extattr_vnode_inactive(struct vnode *vp, struct lwp *l) 1549 1.1 dholland { 1550 1.4 dholland struct ulfs_extattr_list_entry *uele; 1551 1.1 dholland struct mount *mp = vp->v_mount; 1552 1.4 dholland struct ulfsmount *ump = VFSTOULFS(mp); 1553 1.1 dholland 1554 1.1 dholland /* 1555 1.1 dholland * In that case, we cannot lock. We should not have any active vnodes 1556 1.1 dholland * on the fs if this is not yet initialized but is going to be, so 1557 1.1 dholland * this can go unlocked. 1558 1.1 dholland */ 1559 1.4 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) 1560 1.1 dholland return; 1561 1.1 dholland 1562 1.12 dholland if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) 1563 1.12 dholland return; 1564 1.12 dholland 1565 1.4 dholland ulfs_extattr_uepm_lock(ump); 1566 1.1 dholland 1567 1.1 dholland LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries) 1568 1.4 dholland ulfs_extattr_rm(vp, uele->uele_attrnamespace, 1569 1.1 dholland uele->uele_attrname, lwp0.l_cred, l); 1570 1.1 dholland 1571 1.4 dholland ulfs_extattr_uepm_unlock(ump); 1572 1.1 dholland } 1573 1.1 dholland 1574 1.1 dholland void 1575 1.4 dholland ulfs_extattr_init(void) 1576 1.1 dholland { 1577 1.1 dholland 1578 1.1 dholland } 1579 1.1 dholland 1580 1.1 dholland void 1581 1.4 dholland ulfs_extattr_done(void) 1582 1.1 dholland { 1583 1.1 dholland 1584 1.1 dholland } 1585