2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999-2002 Robert N. M. Watson
5 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
8 * This software was developed by Robert Watson for the TrustedBSD Project.
10 * This software was developed for the FreeBSD Project in part by Network
11 * Associates Laboratories, the Security Research Division of Network
12 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
13 * as part of the DARPA CHATS research program.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Support for filesystem extended attribute: UFS-specific support functions.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/fcntl.h>
56 #include <sys/vnode.h>
57 #include <sys/mount.h>
59 #include <sys/dirent.h>
60 #include <sys/extattr.h>
62 #include <sys/sysctl.h>
66 #include <ufs/ufs/dir.h>
67 #include <ufs/ufs/extattr.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufs_extern.h>
75 FEATURE(ufs_extattr, "ufs extended attribute support");
77 static MALLOC_DEFINE(M_UFS_EXTATTR, "ufs_extattr", "ufs extended attribute");
79 static int ufs_extattr_sync = 0;
80 SYSCTL_INT(_debug, OID_AUTO, ufs_extattr_sync, CTLFLAG_RW, &ufs_extattr_sync,
83 static int ufs_extattr_valid_attrname(int attrnamespace,
84 const char *attrname);
85 static int ufs_extattr_enable_with_open(struct ufsmount *ump,
86 struct vnode *vp, int attrnamespace, const char *attrname,
88 static int ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
89 const char *attrname, struct vnode *backing_vnode,
91 static int ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
92 const char *attrname, struct thread *td);
93 static int ufs_extattr_get(struct vnode *vp, int attrnamespace,
94 const char *name, struct uio *uio, size_t *size,
95 struct ucred *cred, struct thread *td);
96 static int ufs_extattr_set(struct vnode *vp, int attrnamespace,
97 const char *name, struct uio *uio, struct ucred *cred,
99 static int ufs_extattr_rm(struct vnode *vp, int attrnamespace,
100 const char *name, struct ucred *cred, struct thread *td);
101 #ifdef UFS_EXTATTR_AUTOSTART
102 static int ufs_extattr_autostart_locked(struct mount *mp,
105 static int ufs_extattr_start_locked(struct ufsmount *ump,
109 * Per-FS attribute lock protecting attribute operations.
111 * XXXRW: Perhaps something more fine-grained would be appropriate, but at
112 * the end of the day we're going to contend on the vnode lock for the
113 * backing file anyway.
116 ufs_extattr_uepm_lock(struct ufsmount *ump)
119 sx_xlock(&ump->um_extattr.uepm_lock);
123 ufs_extattr_uepm_unlock(struct ufsmount *ump)
126 sx_xunlock(&ump->um_extattr.uepm_lock);
130 * Determine whether the name passed is a valid name for an actual
133 * Invalid currently consists of:
134 * NULL pointer for attrname
135 * zero-length attrname (used to retrieve application attribute list)
138 ufs_extattr_valid_attrname(int attrnamespace, const char *attrname)
141 if (attrname == NULL)
143 if (strlen(attrname) == 0)
149 * Locate an attribute given a name and mountpoint.
150 * Must be holding uepm lock for the mount point.
152 static struct ufs_extattr_list_entry *
153 ufs_extattr_find_attr(struct ufsmount *ump, int attrnamespace,
154 const char *attrname)
156 struct ufs_extattr_list_entry *search_attribute;
158 sx_assert(&ump->um_extattr.uepm_lock, SA_XLOCKED);
160 for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
161 search_attribute != NULL;
162 search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
163 if (!(strncmp(attrname, search_attribute->uele_attrname,
164 UFS_EXTATTR_MAXEXTATTRNAME)) &&
165 (attrnamespace == search_attribute->uele_attrnamespace)) {
166 return (search_attribute);
174 * Initialize per-FS structures supporting extended attributes. Do not
175 * start extended attributes yet.
178 ufs_extattr_uepm_init(struct ufs_extattr_per_mount *uepm)
181 uepm->uepm_flags = 0;
182 LIST_INIT(&uepm->uepm_list);
183 sx_init(&uepm->uepm_lock, "ufs_extattr_sx");
184 uepm->uepm_flags |= UFS_EXTATTR_UEPM_INITIALIZED;
188 * Destroy per-FS structures supporting extended attributes. Assumes
189 * that EAs have already been stopped, and will panic if not.
192 ufs_extattr_uepm_destroy(struct ufs_extattr_per_mount *uepm)
195 if (!(uepm->uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
196 panic("ufs_extattr_uepm_destroy: not initialized");
198 if ((uepm->uepm_flags & UFS_EXTATTR_UEPM_STARTED))
199 panic("ufs_extattr_uepm_destroy: called while still started");
202 * It's not clear that either order for the next two lines is
203 * ideal, and it should never be a problem if this is only called
204 * during unmount, and with vfs_busy().
206 uepm->uepm_flags &= ~UFS_EXTATTR_UEPM_INITIALIZED;
207 sx_destroy(&uepm->uepm_lock);
211 * Start extended attribute support on an FS.
214 ufs_extattr_start(struct mount *mp, struct thread *td)
216 struct ufsmount *ump;
221 ufs_extattr_uepm_lock(ump);
222 error = ufs_extattr_start_locked(ump, td);
223 ufs_extattr_uepm_unlock(ump);
228 ufs_extattr_start_locked(struct ufsmount *ump, struct thread *td)
230 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
232 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
235 ump->um_extattr.uepm_flags |= UFS_EXTATTR_UEPM_STARTED;
236 ump->um_extattr.uepm_ucred = crhold(td->td_ucred);
240 #ifdef UFS_EXTATTR_AUTOSTART
242 * Helper routine: given a locked parent directory and filename, return
243 * the locked vnode of the inode associated with the name. Will not
244 * follow symlinks, may return any type of vnode. Lock on parent will
245 * be released even in the event of a failure. In the event that the
246 * target is the parent (i.e., "."), there will be two references and
247 * one lock, requiring the caller to possibly special-case.
249 #define UE_GETDIR_LOCKPARENT 1
250 #define UE_GETDIR_LOCKPARENT_DONT 2
252 ufs_extattr_lookup(struct vnode *start_dvp, int lockparent, char *dirname,
253 struct vnode **vp, struct thread *td)
255 struct vop_cachedlookup_args vargs;
256 struct componentname cnp;
257 struct vnode *target_vp;
260 bzero(&cnp, sizeof(cnp));
261 cnp.cn_nameiop = LOOKUP;
262 cnp.cn_flags = ISLASTCN;
263 if (lockparent == UE_GETDIR_LOCKPARENT)
264 cnp.cn_flags |= LOCKPARENT;
265 cnp.cn_lkflags = LK_EXCLUSIVE;
267 cnp.cn_cred = td->td_ucred;
268 cnp.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
269 cnp.cn_nameptr = cnp.cn_pnbuf;
270 error = copystr(dirname, cnp.cn_pnbuf, MAXPATHLEN,
271 (size_t *) &cnp.cn_namelen);
273 if (lockparent == UE_GETDIR_LOCKPARENT_DONT) {
274 VOP_UNLOCK(start_dvp);
276 uma_zfree(namei_zone, cnp.cn_pnbuf);
277 printf("ufs_extattr_lookup: copystr failed\n");
280 cnp.cn_namelen--; /* trim nul termination */
281 vargs.a_gen.a_desc = NULL;
282 vargs.a_dvp = start_dvp;
283 vargs.a_vpp = &target_vp;
285 error = ufs_lookup(&vargs);
286 uma_zfree(namei_zone, cnp.cn_pnbuf);
289 * Error condition, may have to release the lock on the parent
290 * if ufs_lookup() didn't.
292 if (lockparent == UE_GETDIR_LOCKPARENT_DONT)
293 VOP_UNLOCK(start_dvp);
296 * Check that ufs_lookup() didn't release the lock when we
299 if (lockparent == UE_GETDIR_LOCKPARENT)
300 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
305 if (target_vp == start_dvp)
306 panic("ufs_extattr_lookup: target_vp == start_dvp");
309 if (target_vp != start_dvp && lockparent == UE_GETDIR_LOCKPARENT_DONT)
310 VOP_UNLOCK(start_dvp);
312 if (lockparent == UE_GETDIR_LOCKPARENT)
313 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
315 /* printf("ufs_extattr_lookup: success\n"); */
319 #endif /* !UFS_EXTATTR_AUTOSTART */
322 * Enable an EA using the passed filesystem, backing vnode, attribute name,
323 * namespace, and proc. Will perform a VOP_OPEN() on the vp, so expects vp
324 * to be locked when passed in. The vnode will be returned unlocked,
325 * regardless of success/failure of the function. As a result, the caller
326 * will always need to vrele(), but not vput().
329 ufs_extattr_enable_with_open(struct ufsmount *ump, struct vnode *vp,
330 int attrnamespace, const char *attrname, struct thread *td)
334 error = VOP_OPEN(vp, FREAD|FWRITE, td->td_ucred, td, NULL);
336 printf("ufs_extattr_enable_with_open.VOP_OPEN(): failed "
342 error = VOP_ADD_WRITECOUNT(vp, 1);
344 VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td);
348 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", __func__, vp,
355 error = ufs_extattr_enable(ump, attrnamespace, attrname, vp, td);
357 vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
361 #ifdef UFS_EXTATTR_AUTOSTART
363 * Given a locked directory vnode, iterate over the names in the directory
364 * and use ufs_extattr_lookup() to retrieve locked vnodes of potential
365 * attribute files. Then invoke ufs_extattr_enable_with_open() on each
366 * to attempt to start the attribute. Leaves the directory locked on
370 ufs_extattr_iterate_directory(struct ufsmount *ump, struct vnode *dvp,
371 int attrnamespace, struct thread *td)
373 struct vop_readdir_args vargs;
374 struct dirent *dp, *edp;
375 struct vnode *attr_vp;
379 int error, eofflag = 0;
381 if (dvp->v_type != VDIR)
384 dirbuf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK);
386 auio.uio_iov = &aiov;
388 auio.uio_rw = UIO_READ;
389 auio.uio_segflg = UIO_SYSSPACE;
393 vargs.a_gen.a_desc = NULL;
396 vargs.a_cred = td->td_ucred;
397 vargs.a_eofflag = &eofflag;
398 vargs.a_ncookies = NULL;
399 vargs.a_cookies = NULL;
402 auio.uio_resid = DIRBLKSIZ;
403 aiov.iov_base = dirbuf;
404 aiov.iov_len = DIRBLKSIZ;
405 error = ufs_readdir(&vargs);
407 printf("ufs_extattr_iterate_directory: ufs_readdir "
412 edp = (struct dirent *)&dirbuf[DIRBLKSIZ - auio.uio_resid];
413 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
414 if (dp->d_reclen == 0)
416 error = ufs_extattr_lookup(dvp, UE_GETDIR_LOCKPARENT,
417 dp->d_name, &attr_vp, td);
419 printf("ufs_extattr_iterate_directory: lookup "
420 "%s %d\n", dp->d_name, error);
421 } else if (attr_vp == dvp) {
423 } else if (attr_vp->v_type != VREG) {
426 error = ufs_extattr_enable_with_open(ump,
427 attr_vp, attrnamespace, dp->d_name, td);
430 printf("ufs_extattr_iterate_directory: "
431 "enable %s %d\n", dp->d_name,
433 } else if (bootverbose) {
434 printf("UFS autostarted EA %s\n",
438 dp = (struct dirent *) ((char *)dp + dp->d_reclen);
443 free(dirbuf, M_TEMP);
449 * Auto-start of extended attributes, to be executed (optionally) at
453 ufs_extattr_autostart(struct mount *mp, struct thread *td)
455 struct ufsmount *ump;
459 ufs_extattr_uepm_lock(ump);
460 error = ufs_extattr_autostart_locked(mp, td);
461 ufs_extattr_uepm_unlock(ump);
466 ufs_extattr_autostart_locked(struct mount *mp, struct thread *td)
468 struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
469 struct ufsmount *ump = VFSTOUFS(mp);
473 * UFS_EXTATTR applies only to UFS1, as UFS2 uses native extended
474 * attributes, so don't autostart.
476 if (ump->um_fstype != UFS1)
480 * Does UFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
481 * If so, automatically start EA's.
483 error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp);
485 printf("ufs_extattr_autostart.VFS_ROOT() returned %d\n",
490 error = ufs_extattr_lookup(rvp, UE_GETDIR_LOCKPARENT_DONT,
491 UFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, td);
493 /* rvp ref'd but now unlocked */
497 if (rvp == attr_dvp) {
498 /* Should never happen. */
505 if (attr_dvp->v_type != VDIR) {
506 printf("ufs_extattr_autostart: %s != VDIR\n",
507 UFS_EXTATTR_FSROOTSUBDIR);
508 goto return_vput_attr_dvp;
511 error = ufs_extattr_start_locked(ump, td);
513 printf("ufs_extattr_autostart: ufs_extattr_start failed (%d)\n",
515 goto return_vput_attr_dvp;
519 * Look for two subdirectories: UFS_EXTATTR_SUBDIR_SYSTEM,
520 * UFS_EXTATTR_SUBDIR_USER. For each, iterate over the sub-directory,
521 * and start with appropriate type. Failures in either don't
522 * result in an over-all failure. attr_dvp is left locked to
523 * be cleaned up on exit.
525 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
526 UFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, td);
528 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
529 attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, td);
531 printf("ufs_extattr_iterate_directory returned %d\n",
533 vput(attr_system_dvp);
536 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
537 UFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, td);
539 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
540 attr_user_dvp, EXTATTR_NAMESPACE_USER, td);
542 printf("ufs_extattr_iterate_directory returned %d\n",
547 /* Mask startup failures in sub-directories. */
550 return_vput_attr_dvp:
555 #endif /* !UFS_EXTATTR_AUTOSTART */
558 * Stop extended attribute support on an FS.
561 ufs_extattr_stop(struct mount *mp, struct thread *td)
563 struct ufs_extattr_list_entry *uele;
564 struct ufsmount *ump = VFSTOUFS(mp);
567 ufs_extattr_uepm_lock(ump);
569 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
574 while ((uele = LIST_FIRST(&ump->um_extattr.uepm_list)) != NULL) {
575 ufs_extattr_disable(ump, uele->uele_attrnamespace,
576 uele->uele_attrname, td);
579 ump->um_extattr.uepm_flags &= ~UFS_EXTATTR_UEPM_STARTED;
581 crfree(ump->um_extattr.uepm_ucred);
582 ump->um_extattr.uepm_ucred = NULL;
585 ufs_extattr_uepm_unlock(ump);
591 * Enable a named attribute on the specified filesystem; provide an
592 * unlocked backing vnode to hold the attribute data.
595 ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
596 const char *attrname, struct vnode *backing_vnode, struct thread *td)
598 struct ufs_extattr_list_entry *attribute;
603 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
605 if (backing_vnode->v_type != VREG)
608 attribute = malloc(sizeof(struct ufs_extattr_list_entry),
609 M_UFS_EXTATTR, M_WAITOK);
611 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
616 if (ufs_extattr_find_attr(ump, attrnamespace, attrname)) {
621 strncpy(attribute->uele_attrname, attrname,
622 UFS_EXTATTR_MAXEXTATTRNAME);
623 attribute->uele_attrnamespace = attrnamespace;
624 bzero(&attribute->uele_fileheader,
625 sizeof(struct ufs_extattr_fileheader));
627 attribute->uele_backing_vnode = backing_vnode;
629 auio.uio_iov = &aiov;
631 aiov.iov_base = (caddr_t) &attribute->uele_fileheader;
632 aiov.iov_len = sizeof(struct ufs_extattr_fileheader);
633 auio.uio_resid = sizeof(struct ufs_extattr_fileheader);
634 auio.uio_offset = (off_t) 0;
635 auio.uio_segflg = UIO_SYSSPACE;
636 auio.uio_rw = UIO_READ;
639 vn_lock(backing_vnode, LK_SHARED | LK_RETRY);
640 error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
641 ump->um_extattr.uepm_ucred);
644 goto unlock_free_exit;
646 if (auio.uio_resid != 0) {
647 printf("ufs_extattr_enable: malformed attribute header\n");
649 goto unlock_free_exit;
652 if (attribute->uele_fileheader.uef_magic != UFS_EXTATTR_MAGIC) {
653 printf("ufs_extattr_enable: invalid attribute header magic\n");
655 goto unlock_free_exit;
658 if (attribute->uele_fileheader.uef_version != UFS_EXTATTR_VERSION) {
659 printf("ufs_extattr_enable: incorrect attribute header "
662 goto unlock_free_exit;
665 ASSERT_VOP_LOCKED(backing_vnode, "ufs_extattr_enable");
666 LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
669 VOP_UNLOCK(backing_vnode);
673 VOP_UNLOCK(backing_vnode);
676 free(attribute, M_UFS_EXTATTR);
681 * Disable extended attribute support on an FS.
684 ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
685 const char *attrname, struct thread *td)
687 struct ufs_extattr_list_entry *uele;
690 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
693 uele = ufs_extattr_find_attr(ump, attrnamespace, attrname);
697 LIST_REMOVE(uele, uele_entries);
699 vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY);
700 ASSERT_VOP_LOCKED(uele->uele_backing_vnode, "ufs_extattr_disable");
701 VOP_UNLOCK(uele->uele_backing_vnode);
702 error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
705 free(uele, M_UFS_EXTATTR);
711 * VFS call to manage extended attributes in UFS. If filename_vp is
712 * non-NULL, it must be passed in locked, and regardless of errors in
713 * processing, will be unlocked.
716 ufs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
717 int attrnamespace, const char *attrname)
719 struct ufsmount *ump = VFSTOUFS(mp);
720 struct thread *td = curthread;
724 * Processes with privilege, but in jail, are not allowed to
725 * configure extended attributes.
727 error = priv_check(td, PRIV_UFS_EXTATTRCTL);
729 if (filename_vp != NULL)
730 VOP_UNLOCK(filename_vp);
735 * We only allow extattrctl(2) on UFS1 file systems, as UFS2 uses
736 * native extended attributes.
738 if (ump->um_fstype != UFS1) {
739 if (filename_vp != NULL)
740 VOP_UNLOCK(filename_vp);
745 case UFS_EXTATTR_CMD_START:
746 if (filename_vp != NULL) {
747 VOP_UNLOCK(filename_vp);
750 if (attrname != NULL)
753 error = ufs_extattr_start(mp, td);
757 case UFS_EXTATTR_CMD_STOP:
758 if (filename_vp != NULL) {
759 VOP_UNLOCK(filename_vp);
762 if (attrname != NULL)
765 error = ufs_extattr_stop(mp, td);
769 case UFS_EXTATTR_CMD_ENABLE:
771 if (filename_vp == NULL)
773 if (attrname == NULL) {
774 VOP_UNLOCK(filename_vp);
779 * ufs_extattr_enable_with_open() will always unlock the
780 * vnode, regardless of failure.
782 ufs_extattr_uepm_lock(ump);
783 error = ufs_extattr_enable_with_open(ump, filename_vp,
784 attrnamespace, attrname, td);
785 ufs_extattr_uepm_unlock(ump);
789 case UFS_EXTATTR_CMD_DISABLE:
791 if (filename_vp != NULL) {
792 VOP_UNLOCK(filename_vp);
795 if (attrname == NULL)
798 ufs_extattr_uepm_lock(ump);
799 error = ufs_extattr_disable(ump, attrnamespace, attrname,
801 ufs_extattr_uepm_unlock(ump);
811 * Vnode operating to retrieve a named extended attribute.
814 ufs_getextattr(struct vop_getextattr_args *ap)
817 IN struct vnode *a_vp;
818 IN int a_attrnamespace;
819 IN const char *a_name;
820 INOUT struct uio *a_uio;
822 IN struct ucred *a_cred;
823 IN struct thread *a_td;
827 struct mount *mp = ap->a_vp->v_mount;
828 struct ufsmount *ump = VFSTOUFS(mp);
831 ufs_extattr_uepm_lock(ump);
833 error = ufs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
834 ap->a_uio, ap->a_size, ap->a_cred, ap->a_td);
836 ufs_extattr_uepm_unlock(ump);
842 * Real work associated with retrieving a named attribute--assumes that
843 * the attribute lock has already been grabbed.
846 ufs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
847 struct uio *uio, size_t *size, struct ucred *cred, struct thread *td)
849 struct ufs_extattr_list_entry *attribute;
850 struct ufs_extattr_header ueh;
851 struct iovec local_aiov;
852 struct uio local_aio;
853 struct mount *mp = vp->v_mount;
854 struct ufsmount *ump = VFSTOUFS(mp);
855 struct inode *ip = VTOI(vp);
860 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
863 if (strlen(name) == 0)
866 error = extattr_check_cred(vp, attrnamespace, cred, td, VREAD);
870 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
875 * Allow only offsets of zero to encourage the read/replace
876 * extended attribute semantic. Otherwise we can't guarantee
877 * atomicity, as we don't provide locks for extended attributes.
879 if (uio != NULL && uio->uio_offset != 0)
883 * Find base offset of header in file based on file header size, and
884 * data header size + maximum data size, indexed by inode number.
886 base_offset = sizeof(struct ufs_extattr_fileheader) +
887 ip->i_number * (sizeof(struct ufs_extattr_header) +
888 attribute->uele_fileheader.uef_size);
891 * Read in the data header to see if the data is defined, and if so
894 bzero(&ueh, sizeof(struct ufs_extattr_header));
895 local_aiov.iov_base = (caddr_t) &ueh;
896 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
897 local_aio.uio_iov = &local_aiov;
898 local_aio.uio_iovcnt = 1;
899 local_aio.uio_rw = UIO_READ;
900 local_aio.uio_segflg = UIO_SYSSPACE;
901 local_aio.uio_td = td;
902 local_aio.uio_offset = base_offset;
903 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
908 * Don't need to get a lock on the backing file if the getattr is
909 * being applied to the backing file, as the lock is already held.
911 if (attribute->uele_backing_vnode != vp)
912 vn_lock(attribute->uele_backing_vnode, LK_SHARED | LK_RETRY);
914 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
915 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
920 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
925 /* Valid for the current inode generation? */
926 if (ueh.ueh_i_gen != ip->i_gen) {
928 * The inode itself has a different generation number
929 * than the attribute data. For now, the best solution
930 * is to coerce this to undefined, and let it get cleaned
931 * up by the next write or extattrctl clean.
933 printf("ufs_extattr_get (%s): inode number inconsistency (%d, %ju)\n",
934 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (uintmax_t)ip->i_gen);
939 /* Local size consistency check. */
940 if (ueh.ueh_len > attribute->uele_fileheader.uef_size) {
945 /* Return full data size if caller requested it. */
949 /* Return data if the caller requested it. */
951 /* Allow for offset into the attribute data. */
952 uio->uio_offset = base_offset + sizeof(struct
956 * Figure out maximum to transfer -- use buffer size and
959 len = MIN(uio->uio_resid, ueh.ueh_len);
960 old_len = uio->uio_resid;
961 uio->uio_resid = len;
963 error = VOP_READ(attribute->uele_backing_vnode, uio,
964 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
968 uio->uio_resid = old_len - (len - uio->uio_resid);
976 if (attribute->uele_backing_vnode != vp)
977 VOP_UNLOCK(attribute->uele_backing_vnode);
983 * Vnode operation to remove a named attribute.
986 ufs_deleteextattr(struct vop_deleteextattr_args *ap)
989 IN struct vnode *a_vp;
990 IN int a_attrnamespace;
991 IN const char *a_name;
992 IN struct ucred *a_cred;
993 IN struct thread *a_td;
997 struct mount *mp = ap->a_vp->v_mount;
998 struct ufsmount *ump = VFSTOUFS(mp);
1001 ufs_extattr_uepm_lock(ump);
1003 error = ufs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
1004 ap->a_cred, ap->a_td);
1007 ufs_extattr_uepm_unlock(ump);
1013 * Vnode operation to set a named attribute.
1016 ufs_setextattr(struct vop_setextattr_args *ap)
1019 IN struct vnode *a_vp;
1020 IN int a_attrnamespace;
1021 IN const char *a_name;
1022 INOUT struct uio *a_uio;
1023 IN struct ucred *a_cred;
1024 IN struct thread *a_td;
1028 struct mount *mp = ap->a_vp->v_mount;
1029 struct ufsmount *ump = VFSTOUFS(mp);
1033 * XXX: No longer a supported way to delete extended attributes.
1035 if (ap->a_uio == NULL)
1038 ufs_extattr_uepm_lock(ump);
1040 error = ufs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
1041 ap->a_uio, ap->a_cred, ap->a_td);
1043 ufs_extattr_uepm_unlock(ump);
1049 * Real work associated with setting a vnode's extended attributes;
1050 * assumes that the attribute lock has already been grabbed.
1053 ufs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
1054 struct uio *uio, struct ucred *cred, struct thread *td)
1056 struct ufs_extattr_list_entry *attribute;
1057 struct ufs_extattr_header ueh;
1058 struct iovec local_aiov;
1059 struct uio local_aio;
1060 struct mount *mp = vp->v_mount;
1061 struct ufsmount *ump = VFSTOUFS(mp);
1062 struct inode *ip = VTOI(vp);
1064 int error = 0, ioflag;
1066 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1068 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1069 return (EOPNOTSUPP);
1070 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1073 error = extattr_check_cred(vp, attrnamespace, cred, td, VWRITE);
1077 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1082 * Early rejection of invalid offsets/length.
1083 * Reject: any offset but 0 (replace)
1084 * Any size greater than attribute size limit
1086 if (uio->uio_offset != 0 ||
1087 uio->uio_resid > attribute->uele_fileheader.uef_size)
1091 * Find base offset of header in file based on file header size, and
1092 * data header size + maximum data size, indexed by inode number.
1094 base_offset = sizeof(struct ufs_extattr_fileheader) +
1095 ip->i_number * (sizeof(struct ufs_extattr_header) +
1096 attribute->uele_fileheader.uef_size);
1099 * Write out a data header for the data.
1101 ueh.ueh_len = uio->uio_resid;
1102 ueh.ueh_flags = UFS_EXTATTR_ATTR_FLAG_INUSE;
1103 ueh.ueh_i_gen = ip->i_gen;
1104 local_aiov.iov_base = (caddr_t) &ueh;
1105 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1106 local_aio.uio_iov = &local_aiov;
1107 local_aio.uio_iovcnt = 1;
1108 local_aio.uio_rw = UIO_WRITE;
1109 local_aio.uio_segflg = UIO_SYSSPACE;
1110 local_aio.uio_td = td;
1111 local_aio.uio_offset = base_offset;
1112 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1117 * Don't need to get a lock on the backing file if the setattr is
1118 * being applied to the backing file, as the lock is already held.
1120 if (attribute->uele_backing_vnode != vp)
1121 vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
1123 ioflag = IO_NODELOCKED;
1124 if (ufs_extattr_sync)
1126 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1127 ump->um_extattr.uepm_ucred);
1129 goto vopunlock_exit;
1131 if (local_aio.uio_resid != 0) {
1133 goto vopunlock_exit;
1137 * Write out user data.
1139 uio->uio_offset = base_offset + sizeof(struct ufs_extattr_header);
1141 ioflag = IO_NODELOCKED;
1142 if (ufs_extattr_sync)
1144 error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
1145 ump->um_extattr.uepm_ucred);
1148 uio->uio_offset = 0;
1150 if (attribute->uele_backing_vnode != vp)
1151 VOP_UNLOCK(attribute->uele_backing_vnode);
1157 * Real work associated with removing an extended attribute from a vnode.
1158 * Assumes the attribute lock has already been grabbed.
1161 ufs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
1162 struct ucred *cred, struct thread *td)
1164 struct ufs_extattr_list_entry *attribute;
1165 struct ufs_extattr_header ueh;
1166 struct iovec local_aiov;
1167 struct uio local_aio;
1168 struct mount *mp = vp->v_mount;
1169 struct ufsmount *ump = VFSTOUFS(mp);
1170 struct inode *ip = VTOI(vp);
1172 int error = 0, ioflag;
1174 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1176 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1177 return (EOPNOTSUPP);
1178 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1181 error = extattr_check_cred(vp, attrnamespace, cred, td, VWRITE);
1185 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1190 * Find base offset of header in file based on file header size, and
1191 * data header size + maximum data size, indexed by inode number.
1193 base_offset = sizeof(struct ufs_extattr_fileheader) +
1194 ip->i_number * (sizeof(struct ufs_extattr_header) +
1195 attribute->uele_fileheader.uef_size);
1198 * Check to see if currently defined.
1200 bzero(&ueh, sizeof(struct ufs_extattr_header));
1202 local_aiov.iov_base = (caddr_t) &ueh;
1203 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1204 local_aio.uio_iov = &local_aiov;
1205 local_aio.uio_iovcnt = 1;
1206 local_aio.uio_rw = UIO_READ;
1207 local_aio.uio_segflg = UIO_SYSSPACE;
1208 local_aio.uio_td = td;
1209 local_aio.uio_offset = base_offset;
1210 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1213 * Don't need to get the lock on the backing vnode if the vnode we're
1214 * modifying is it, as we already hold the lock.
1216 if (attribute->uele_backing_vnode != vp)
1217 vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
1219 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
1220 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
1222 goto vopunlock_exit;
1225 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
1227 goto vopunlock_exit;
1230 /* Valid for the current inode generation? */
1231 if (ueh.ueh_i_gen != ip->i_gen) {
1233 * The inode itself has a different generation number than
1234 * the attribute data. For now, the best solution is to
1235 * coerce this to undefined, and let it get cleaned up by
1236 * the next write or extattrctl clean.
1238 printf("ufs_extattr_rm (%s): inode number inconsistency (%d, %jd)\n",
1239 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
1241 goto vopunlock_exit;
1244 /* Flag it as not in use. */
1248 local_aiov.iov_base = (caddr_t) &ueh;
1249 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1250 local_aio.uio_iov = &local_aiov;
1251 local_aio.uio_iovcnt = 1;
1252 local_aio.uio_rw = UIO_WRITE;
1253 local_aio.uio_segflg = UIO_SYSSPACE;
1254 local_aio.uio_td = td;
1255 local_aio.uio_offset = base_offset;
1256 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1258 ioflag = IO_NODELOCKED;
1259 if (ufs_extattr_sync)
1261 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1262 ump->um_extattr.uepm_ucred);
1264 goto vopunlock_exit;
1266 if (local_aio.uio_resid != 0)
1270 VOP_UNLOCK(attribute->uele_backing_vnode);
1276 * Called by UFS when an inode is no longer active and should have its
1277 * attributes stripped.
1280 ufs_extattr_vnode_inactive(struct vnode *vp, struct thread *td)
1282 struct ufs_extattr_list_entry *uele;
1283 struct mount *mp = vp->v_mount;
1284 struct ufsmount *ump = VFSTOUFS(mp);
1287 * In that case, we cannot lock. We should not have any active vnodes
1288 * on the fs if this is not yet initialized but is going to be, so
1289 * this can go unlocked.
1291 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
1294 ufs_extattr_uepm_lock(ump);
1296 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
1297 ufs_extattr_uepm_unlock(ump);
1301 LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
1302 ufs_extattr_rm(vp, uele->uele_attrnamespace,
1303 uele->uele_attrname, NULL, td);
1305 ufs_extattr_uepm_unlock(ump);
1308 #endif /* !UFS_EXTATTR */