2 * Copyright (c) 1999-2002 Robert N. M. Watson
3 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
6 * This software was developed by Robert Watson for the TrustedBSD Project.
8 * This software was developed for the FreeBSD Project in part by Network
9 * Associates Laboratories, the Security Research Division of Network
10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
11 * as part of the DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Support for filesystem extended attribute: UFS-specific support functions.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/namei.h>
49 #include <sys/malloc.h>
50 #include <sys/fcntl.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
55 #include <sys/dirent.h>
56 #include <sys/extattr.h>
57 #include <sys/sysctl.h>
61 #include <ufs/ufs/dir.h>
62 #include <ufs/ufs/extattr.h>
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/ufsmount.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/ufs_extern.h>
70 static MALLOC_DEFINE(M_UFS_EXTATTR, "ufs_extattr", "ufs extended attribute");
72 static int ufs_extattr_sync = 0;
73 SYSCTL_INT(_debug, OID_AUTO, ufs_extattr_sync, CTLFLAG_RW, &ufs_extattr_sync,
76 static int ufs_extattr_valid_attrname(int attrnamespace,
77 const char *attrname);
78 static int ufs_extattr_enable_with_open(struct ufsmount *ump,
79 struct vnode *vp, int attrnamespace, const char *attrname,
81 static int ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
82 const char *attrname, struct vnode *backing_vnode,
84 static int ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
85 const char *attrname, struct thread *td);
86 static int ufs_extattr_get(struct vnode *vp, int attrnamespace,
87 const char *name, struct uio *uio, size_t *size,
88 struct ucred *cred, struct thread *td);
89 static int ufs_extattr_set(struct vnode *vp, int attrnamespace,
90 const char *name, struct uio *uio, struct ucred *cred,
92 static int ufs_extattr_rm(struct vnode *vp, int attrnamespace,
93 const char *name, struct ucred *cred, struct thread *td);
96 * Per-FS attribute lock protecting attribute operations.
97 * XXX Right now there is a lot of lock contention due to having a single
98 * lock per-FS; really, this should be far more fine-grained.
101 ufs_extattr_uepm_lock(struct ufsmount *ump, struct thread *td)
104 /* Ideally, LK_CANRECURSE would not be used, here. */
105 lockmgr(&ump->um_extattr.uepm_lock, LK_EXCLUSIVE | LK_RETRY |
106 LK_CANRECURSE, 0, td);
110 ufs_extattr_uepm_unlock(struct ufsmount *ump, struct thread *td)
113 lockmgr(&ump->um_extattr.uepm_lock, LK_RELEASE, 0, td);
117 * Determine whether the name passed is a valid name for an actual
120 * Invalid currently consists of:
121 * NULL pointer for attrname
122 * zero-length attrname (used to retrieve application attribute list)
125 ufs_extattr_valid_attrname(int attrnamespace, const char *attrname)
128 if (attrname == NULL)
130 if (strlen(attrname) == 0)
136 * Locate an attribute given a name and mountpoint.
137 * Must be holding uepm lock for the mount point.
139 static struct ufs_extattr_list_entry *
140 ufs_extattr_find_attr(struct ufsmount *ump, int attrnamespace,
141 const char *attrname)
143 struct ufs_extattr_list_entry *search_attribute;
145 for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
146 search_attribute != NULL;
147 search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
148 if (!(strncmp(attrname, search_attribute->uele_attrname,
149 UFS_EXTATTR_MAXEXTATTRNAME)) &&
150 (attrnamespace == search_attribute->uele_attrnamespace)) {
151 return (search_attribute);
159 * Initialize per-FS structures supporting extended attributes. Do not
160 * start extended attributes yet.
163 ufs_extattr_uepm_init(struct ufs_extattr_per_mount *uepm)
166 uepm->uepm_flags = 0;
168 LIST_INIT(&uepm->uepm_list);
169 /* XXX is PVFS right, here? */
170 lockinit(&uepm->uepm_lock, PVFS, "extattr", 0, 0);
171 uepm->uepm_flags |= UFS_EXTATTR_UEPM_INITIALIZED;
175 * Destroy per-FS structures supporting extended attributes. Assumes
176 * that EAs have already been stopped, and will panic if not.
179 ufs_extattr_uepm_destroy(struct ufs_extattr_per_mount *uepm)
182 if (!(uepm->uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
183 panic("ufs_extattr_uepm_destroy: not initialized");
185 if ((uepm->uepm_flags & UFS_EXTATTR_UEPM_STARTED))
186 panic("ufs_extattr_uepm_destroy: called while still started");
189 * It's not clear that either order for the next two lines is
190 * ideal, and it should never be a problem if this is only called
191 * during unmount, and with vfs_busy().
193 uepm->uepm_flags &= ~UFS_EXTATTR_UEPM_INITIALIZED;
194 lockdestroy(&uepm->uepm_lock);
198 * Start extended attribute support on an FS.
201 ufs_extattr_start(struct mount *mp, struct thread *td)
203 struct ufsmount *ump;
208 ufs_extattr_uepm_lock(ump, td);
210 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)) {
214 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED) {
219 ump->um_extattr.uepm_flags |= UFS_EXTATTR_UEPM_STARTED;
221 ump->um_extattr.uepm_ucred = crhold(td->td_ucred);
224 ufs_extattr_uepm_unlock(ump, td);
229 #ifdef UFS_EXTATTR_AUTOSTART
231 * Helper routine: given a locked parent directory and filename, return
232 * the locked vnode of the inode associated with the name. Will not
233 * follow symlinks, may return any type of vnode. Lock on parent will
234 * be released even in the event of a failure. In the event that the
235 * target is the parent (i.e., "."), there will be two references and
236 * one lock, requiring the caller to possibly special-case.
238 #define UE_GETDIR_LOCKPARENT 1
239 #define UE_GETDIR_LOCKPARENT_DONT 2
241 ufs_extattr_lookup(struct vnode *start_dvp, int lockparent, char *dirname,
242 struct vnode **vp, struct thread *td)
244 struct vop_cachedlookup_args vargs;
245 struct componentname cnp;
246 struct vnode *target_vp;
249 bzero(&cnp, sizeof(cnp));
250 cnp.cn_nameiop = LOOKUP;
251 cnp.cn_flags = ISLASTCN;
252 if (lockparent == UE_GETDIR_LOCKPARENT)
253 cnp.cn_flags |= LOCKPARENT;
254 cnp.cn_lkflags = LK_EXCLUSIVE;
256 cnp.cn_cred = td->td_ucred;
257 cnp.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
258 cnp.cn_nameptr = cnp.cn_pnbuf;
259 error = copystr(dirname, cnp.cn_pnbuf, MAXPATHLEN,
260 (size_t *) &cnp.cn_namelen);
262 if (lockparent == UE_GETDIR_LOCKPARENT_DONT) {
263 VOP_UNLOCK(start_dvp, 0, td);
265 uma_zfree(namei_zone, cnp.cn_pnbuf);
266 printf("ufs_extattr_lookup: copystr failed\n");
269 cnp.cn_namelen--; /* trim nul termination */
270 vargs.a_gen.a_desc = NULL;
271 vargs.a_dvp = start_dvp;
272 vargs.a_vpp = &target_vp;
274 error = ufs_lookup(&vargs);
275 uma_zfree(namei_zone, cnp.cn_pnbuf);
278 * Error condition, may have to release the lock on the parent
279 * if ufs_lookup() didn't.
281 if (lockparent == UE_GETDIR_LOCKPARENT_DONT)
282 VOP_UNLOCK(start_dvp, 0, td);
285 * Check that ufs_lookup() didn't release the lock when we
288 if (lockparent == UE_GETDIR_LOCKPARENT)
289 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
294 if (target_vp == start_dvp)
295 panic("ufs_extattr_lookup: target_vp == start_dvp");
298 if (target_vp != start_dvp && lockparent == UE_GETDIR_LOCKPARENT_DONT)
299 VOP_UNLOCK(start_dvp, 0, td);
301 if (lockparent == UE_GETDIR_LOCKPARENT)
302 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
304 /* printf("ufs_extattr_lookup: success\n"); */
308 #endif /* !UFS_EXTATTR_AUTOSTART */
311 * Enable an EA using the passed filesystem, backing vnode, attribute name,
312 * namespace, and proc. Will perform a VOP_OPEN() on the vp, so expects vp
313 * to be locked when passed in. The vnode will be returned unlocked,
314 * regardless of success/failure of the function. As a result, the caller
315 * will always need to vrele(), but not vput().
318 ufs_extattr_enable_with_open(struct ufsmount *ump, struct vnode *vp,
319 int attrnamespace, const char *attrname, struct thread *td)
323 error = VOP_OPEN(vp, FREAD|FWRITE, td->td_ucred, td, -1);
325 printf("ufs_extattr_enable_with_open.VOP_OPEN(): failed "
327 VOP_UNLOCK(vp, 0, td);
335 VOP_UNLOCK(vp, 0, td);
337 error = ufs_extattr_enable(ump, attrnamespace, attrname, vp, td);
339 vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
343 #ifdef UFS_EXTATTR_AUTOSTART
345 * Given a locked directory vnode, iterate over the names in the directory
346 * and use ufs_extattr_lookup() to retrieve locked vnodes of potential
347 * attribute files. Then invoke ufs_extattr_enable_with_open() on each
348 * to attempt to start the attribute. Leaves the directory locked on
352 ufs_extattr_iterate_directory(struct ufsmount *ump, struct vnode *dvp,
353 int attrnamespace, struct thread *td)
355 struct vop_readdir_args vargs;
356 struct dirent *dp, *edp;
357 struct vnode *attr_vp;
361 int error, eofflag = 0;
363 if (dvp->v_type != VDIR)
366 MALLOC(dirbuf, char *, DIRBLKSIZ, M_TEMP, M_WAITOK);
368 auio.uio_iov = &aiov;
370 auio.uio_rw = UIO_READ;
371 auio.uio_segflg = UIO_SYSSPACE;
375 vargs.a_gen.a_desc = NULL;
378 vargs.a_cred = td->td_ucred;
379 vargs.a_eofflag = &eofflag;
380 vargs.a_ncookies = NULL;
381 vargs.a_cookies = NULL;
384 auio.uio_resid = DIRBLKSIZ;
385 aiov.iov_base = dirbuf;
386 aiov.iov_len = DIRBLKSIZ;
387 error = ufs_readdir(&vargs);
389 printf("ufs_extattr_iterate_directory: ufs_readdir "
395 * XXXRW: While in UFS, we always get DIRBLKSIZ returns from
396 * the directory code on success, on other file systems this
397 * may not be the case. For portability, we should check the
398 * read length on return from ufs_readdir().
400 edp = (struct dirent *)&dirbuf[DIRBLKSIZ];
401 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
402 #if (BYTE_ORDER == LITTLE_ENDIAN)
403 dp->d_type = dp->d_namlen;
408 if (dp->d_reclen == 0)
410 error = ufs_extattr_lookup(dvp, UE_GETDIR_LOCKPARENT,
411 dp->d_name, &attr_vp, td);
413 printf("ufs_extattr_iterate_directory: lookup "
414 "%s %d\n", dp->d_name, error);
415 } else if (attr_vp == dvp) {
417 } else if (attr_vp->v_type != VREG) {
420 error = ufs_extattr_enable_with_open(ump,
421 attr_vp, attrnamespace, dp->d_name, td);
424 printf("ufs_extattr_iterate_directory: "
425 "enable %s %d\n", dp->d_name,
427 } else if (bootverbose) {
428 printf("UFS autostarted EA %s\n",
432 dp = (struct dirent *) ((char *)dp + dp->d_reclen);
437 FREE(dirbuf, M_TEMP);
443 * Auto-start of extended attributes, to be executed (optionally) at
447 ufs_extattr_autostart(struct mount *mp, struct thread *td)
449 struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
453 * Does UFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
454 * If so, automatically start EA's.
456 error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, td);
458 printf("ufs_extattr_autostart.VFS_ROOT() returned %d\n",
463 error = ufs_extattr_lookup(rvp, UE_GETDIR_LOCKPARENT_DONT,
464 UFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, td);
466 /* rvp ref'd but now unlocked */
470 if (rvp == attr_dvp) {
471 /* Should never happen. */
478 if (attr_dvp->v_type != VDIR) {
479 printf("ufs_extattr_autostart: %s != VDIR\n",
480 UFS_EXTATTR_FSROOTSUBDIR);
481 goto return_vput_attr_dvp;
484 error = ufs_extattr_start(mp, td);
486 printf("ufs_extattr_autostart: ufs_extattr_start failed (%d)\n",
488 goto return_vput_attr_dvp;
492 * Look for two subdirectories: UFS_EXTATTR_SUBDIR_SYSTEM,
493 * UFS_EXTATTR_SUBDIR_USER. For each, iterate over the sub-directory,
494 * and start with appropriate type. Failures in either don't
495 * result in an over-all failure. attr_dvp is left locked to
496 * be cleaned up on exit.
498 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
499 UFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, td);
501 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
502 attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, td);
504 printf("ufs_extattr_iterate_directory returned %d\n",
506 vput(attr_system_dvp);
509 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
510 UFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, td);
512 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
513 attr_user_dvp, EXTATTR_NAMESPACE_USER, td);
515 printf("ufs_extattr_iterate_directory returned %d\n",
520 /* Mask startup failures in sub-directories. */
523 return_vput_attr_dvp:
528 #endif /* !UFS_EXTATTR_AUTOSTART */
531 * Stop extended attribute support on an FS.
534 ufs_extattr_stop(struct mount *mp, struct thread *td)
536 struct ufs_extattr_list_entry *uele;
537 struct ufsmount *ump = VFSTOUFS(mp);
540 ufs_extattr_uepm_lock(ump, td);
542 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
547 while ((uele = LIST_FIRST(&ump->um_extattr.uepm_list)) != NULL) {
548 ufs_extattr_disable(ump, uele->uele_attrnamespace,
549 uele->uele_attrname, td);
552 ump->um_extattr.uepm_flags &= ~UFS_EXTATTR_UEPM_STARTED;
554 crfree(ump->um_extattr.uepm_ucred);
555 ump->um_extattr.uepm_ucred = NULL;
558 ufs_extattr_uepm_unlock(ump, td);
564 * Enable a named attribute on the specified filesystem; provide an
565 * unlocked backing vnode to hold the attribute data.
568 ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
569 const char *attrname, struct vnode *backing_vnode, struct thread *td)
571 struct ufs_extattr_list_entry *attribute;
576 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
578 if (backing_vnode->v_type != VREG)
581 MALLOC(attribute, struct ufs_extattr_list_entry *,
582 sizeof(struct ufs_extattr_list_entry), M_UFS_EXTATTR, M_WAITOK);
583 if (attribute == NULL)
586 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
591 if (ufs_extattr_find_attr(ump, attrnamespace, attrname)) {
596 strncpy(attribute->uele_attrname, attrname,
597 UFS_EXTATTR_MAXEXTATTRNAME);
598 attribute->uele_attrnamespace = attrnamespace;
599 bzero(&attribute->uele_fileheader,
600 sizeof(struct ufs_extattr_fileheader));
602 attribute->uele_backing_vnode = backing_vnode;
604 auio.uio_iov = &aiov;
606 aiov.iov_base = (caddr_t) &attribute->uele_fileheader;
607 aiov.iov_len = sizeof(struct ufs_extattr_fileheader);
608 auio.uio_resid = sizeof(struct ufs_extattr_fileheader);
609 auio.uio_offset = (off_t) 0;
610 auio.uio_segflg = UIO_SYSSPACE;
611 auio.uio_rw = UIO_READ;
614 vn_lock(backing_vnode, LK_SHARED | LK_RETRY, td);
615 error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
616 ump->um_extattr.uepm_ucred);
619 goto unlock_free_exit;
621 if (auio.uio_resid != 0) {
622 printf("ufs_extattr_enable: malformed attribute header\n");
624 goto unlock_free_exit;
627 if (attribute->uele_fileheader.uef_magic != UFS_EXTATTR_MAGIC) {
628 printf("ufs_extattr_enable: invalid attribute header magic\n");
630 goto unlock_free_exit;
633 if (attribute->uele_fileheader.uef_version != UFS_EXTATTR_VERSION) {
634 printf("ufs_extattr_enable: incorrect attribute header "
637 goto unlock_free_exit;
640 ASSERT_VOP_LOCKED(backing_vnode, "ufs_extattr_enable");
641 LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
644 VOP_UNLOCK(backing_vnode, 0, td);
648 VOP_UNLOCK(backing_vnode, 0, td);
651 FREE(attribute, M_UFS_EXTATTR);
656 * Disable extended attribute support on an FS.
659 ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
660 const char *attrname, struct thread *td)
662 struct ufs_extattr_list_entry *uele;
665 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
668 uele = ufs_extattr_find_attr(ump, attrnamespace, attrname);
672 LIST_REMOVE(uele, uele_entries);
674 vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY,
676 ASSERT_VOP_LOCKED(uele->uele_backing_vnode, "ufs_extattr_disable");
677 VOP_UNLOCK(uele->uele_backing_vnode, 0, td);
678 error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
681 FREE(uele, M_UFS_EXTATTR);
687 * VFS call to manage extended attributes in UFS. If filename_vp is
688 * non-NULL, it must be passed in locked, and regardless of errors in
689 * processing, will be unlocked.
692 ufs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
693 int attrnamespace, const char *attrname, struct thread *td)
695 struct ufsmount *ump = VFSTOUFS(mp);
699 * Processes with privilege, but in jail, are not allowed to
700 * configure extended attributes.
702 if ((error = suser(td))) {
703 if (filename_vp != NULL)
704 VOP_UNLOCK(filename_vp, 0, td);
709 case UFS_EXTATTR_CMD_START:
710 if (filename_vp != NULL) {
711 VOP_UNLOCK(filename_vp, 0, td);
714 if (attrname != NULL)
717 error = ufs_extattr_start(mp, td);
721 case UFS_EXTATTR_CMD_STOP:
722 if (filename_vp != NULL) {
723 VOP_UNLOCK(filename_vp, 0, td);
726 if (attrname != NULL)
729 error = ufs_extattr_stop(mp, td);
733 case UFS_EXTATTR_CMD_ENABLE:
735 if (filename_vp == NULL)
737 if (attrname == NULL) {
738 VOP_UNLOCK(filename_vp, 0, td);
743 * ufs_extattr_enable_with_open() will always unlock the
744 * vnode, regardless of failure.
746 ufs_extattr_uepm_lock(ump, td);
747 error = ufs_extattr_enable_with_open(ump, filename_vp,
748 attrnamespace, attrname, td);
749 ufs_extattr_uepm_unlock(ump, td);
753 case UFS_EXTATTR_CMD_DISABLE:
755 if (filename_vp != NULL) {
756 VOP_UNLOCK(filename_vp, 0, td);
759 if (attrname == NULL)
762 ufs_extattr_uepm_lock(ump, td);
763 error = ufs_extattr_disable(ump, attrnamespace, attrname,
765 ufs_extattr_uepm_unlock(ump, td);
775 * Vnode operating to retrieve a named extended attribute.
778 ufs_getextattr(struct vop_getextattr_args *ap)
781 IN struct vnode *a_vp;
782 IN int a_attrnamespace;
783 IN const char *a_name;
784 INOUT struct uio *a_uio;
786 IN struct ucred *a_cred;
787 IN struct thread *a_td;
791 struct mount *mp = ap->a_vp->v_mount;
792 struct ufsmount *ump = VFSTOUFS(mp);
795 ufs_extattr_uepm_lock(ump, ap->a_td);
797 error = ufs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
798 ap->a_uio, ap->a_size, ap->a_cred, ap->a_td);
800 ufs_extattr_uepm_unlock(ump, ap->a_td);
806 * Real work associated with retrieving a named attribute--assumes that
807 * the attribute lock has already been grabbed.
810 ufs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
811 struct uio *uio, size_t *size, struct ucred *cred, struct thread *td)
813 struct ufs_extattr_list_entry *attribute;
814 struct ufs_extattr_header ueh;
815 struct iovec local_aiov;
816 struct uio local_aio;
817 struct mount *mp = vp->v_mount;
818 struct ufsmount *ump = VFSTOUFS(mp);
819 struct inode *ip = VTOI(vp);
824 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
827 if (strlen(name) == 0)
830 error = extattr_check_cred(vp, attrnamespace, cred, td, IREAD);
834 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
839 * Allow only offsets of zero to encourage the read/replace
840 * extended attribute semantic. Otherwise we can't guarantee
841 * atomicity, as we don't provide locks for extended attributes.
843 if (uio != NULL && uio->uio_offset != 0)
847 * Find base offset of header in file based on file header size, and
848 * data header size + maximum data size, indexed by inode number.
850 base_offset = sizeof(struct ufs_extattr_fileheader) +
851 ip->i_number * (sizeof(struct ufs_extattr_header) +
852 attribute->uele_fileheader.uef_size);
855 * Read in the data header to see if the data is defined, and if so
858 bzero(&ueh, sizeof(struct ufs_extattr_header));
859 local_aiov.iov_base = (caddr_t) &ueh;
860 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
861 local_aio.uio_iov = &local_aiov;
862 local_aio.uio_iovcnt = 1;
863 local_aio.uio_rw = UIO_READ;
864 local_aio.uio_segflg = UIO_SYSSPACE;
865 local_aio.uio_td = td;
866 local_aio.uio_offset = base_offset;
867 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
872 * Don't need to get a lock on the backing file if the getattr is
873 * being applied to the backing file, as the lock is already held.
875 if (attribute->uele_backing_vnode != vp)
876 vn_lock(attribute->uele_backing_vnode, LK_SHARED |
879 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
880 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
885 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
890 /* Valid for the current inode generation? */
891 if (ueh.ueh_i_gen != ip->i_gen) {
893 * The inode itself has a different generation number
894 * than the attribute data. For now, the best solution
895 * is to coerce this to undefined, and let it get cleaned
896 * up by the next write or extattrctl clean.
898 printf("ufs_extattr_get (%s): inode number inconsistency (%d, %jd)\n",
899 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
904 /* Local size consistency check. */
905 if (ueh.ueh_len > attribute->uele_fileheader.uef_size) {
910 /* Return full data size if caller requested it. */
914 /* Return data if the caller requested it. */
916 /* Allow for offset into the attribute data. */
917 uio->uio_offset = base_offset + sizeof(struct
921 * Figure out maximum to transfer -- use buffer size and
924 len = MIN(uio->uio_resid, ueh.ueh_len);
925 old_len = uio->uio_resid;
926 uio->uio_resid = len;
928 error = VOP_READ(attribute->uele_backing_vnode, uio,
929 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
933 uio->uio_resid = old_len - (len - uio->uio_resid);
941 if (attribute->uele_backing_vnode != vp)
942 VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
948 * Vnode operation to remove a named attribute.
951 ufs_deleteextattr(struct vop_deleteextattr_args *ap)
954 IN struct vnode *a_vp;
955 IN int a_attrnamespace;
956 IN const char *a_name;
957 IN struct ucred *a_cred;
958 IN struct thread *a_td;
962 struct mount *mp = ap->a_vp->v_mount;
963 struct ufsmount *ump = VFSTOUFS(mp);
966 ufs_extattr_uepm_lock(ump, ap->a_td);
968 error = ufs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
969 ap->a_cred, ap->a_td);
972 ufs_extattr_uepm_unlock(ump, ap->a_td);
978 * Vnode operation to set a named attribute.
981 ufs_setextattr(struct vop_setextattr_args *ap)
984 IN struct vnode *a_vp;
985 IN int a_attrnamespace;
986 IN const char *a_name;
987 INOUT struct uio *a_uio;
988 IN struct ucred *a_cred;
989 IN struct thread *a_td;
993 struct mount *mp = ap->a_vp->v_mount;
994 struct ufsmount *ump = VFSTOUFS(mp);
997 ufs_extattr_uepm_lock(ump, ap->a_td);
1000 * XXX: No longer a supported way to delete extended attributes.
1002 if (ap->a_uio == NULL)
1005 error = ufs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
1006 ap->a_uio, ap->a_cred, ap->a_td);
1008 ufs_extattr_uepm_unlock(ump, ap->a_td);
1014 * Real work associated with setting a vnode's extended attributes;
1015 * assumes that the attribute lock has already been grabbed.
1018 ufs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
1019 struct uio *uio, struct ucred *cred, struct thread *td)
1021 struct ufs_extattr_list_entry *attribute;
1022 struct ufs_extattr_header ueh;
1023 struct iovec local_aiov;
1024 struct uio local_aio;
1025 struct mount *mp = vp->v_mount;
1026 struct ufsmount *ump = VFSTOUFS(mp);
1027 struct inode *ip = VTOI(vp);
1029 int error = 0, ioflag;
1031 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1033 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1034 return (EOPNOTSUPP);
1035 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1038 error = extattr_check_cred(vp, attrnamespace, cred, td, IWRITE);
1042 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1047 * Early rejection of invalid offsets/length.
1048 * Reject: any offset but 0 (replace)
1049 * Any size greater than attribute size limit
1051 if (uio->uio_offset != 0 ||
1052 uio->uio_resid > attribute->uele_fileheader.uef_size)
1056 * Find base offset of header in file based on file header size, and
1057 * data header size + maximum data size, indexed by inode number.
1059 base_offset = sizeof(struct ufs_extattr_fileheader) +
1060 ip->i_number * (sizeof(struct ufs_extattr_header) +
1061 attribute->uele_fileheader.uef_size);
1064 * Write out a data header for the data.
1066 ueh.ueh_len = uio->uio_resid;
1067 ueh.ueh_flags = UFS_EXTATTR_ATTR_FLAG_INUSE;
1068 ueh.ueh_i_gen = ip->i_gen;
1069 local_aiov.iov_base = (caddr_t) &ueh;
1070 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1071 local_aio.uio_iov = &local_aiov;
1072 local_aio.uio_iovcnt = 1;
1073 local_aio.uio_rw = UIO_WRITE;
1074 local_aio.uio_segflg = UIO_SYSSPACE;
1075 local_aio.uio_td = td;
1076 local_aio.uio_offset = base_offset;
1077 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1082 * Don't need to get a lock on the backing file if the setattr is
1083 * being applied to the backing file, as the lock is already held.
1085 if (attribute->uele_backing_vnode != vp)
1086 vn_lock(attribute->uele_backing_vnode,
1087 LK_EXCLUSIVE | LK_RETRY, td);
1089 ioflag = IO_NODELOCKED;
1090 if (ufs_extattr_sync)
1092 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1093 ump->um_extattr.uepm_ucred);
1095 goto vopunlock_exit;
1097 if (local_aio.uio_resid != 0) {
1099 goto vopunlock_exit;
1103 * Write out user data.
1105 uio->uio_offset = base_offset + sizeof(struct ufs_extattr_header);
1107 ioflag = IO_NODELOCKED;
1108 if (ufs_extattr_sync)
1110 error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
1111 ump->um_extattr.uepm_ucred);
1114 uio->uio_offset = 0;
1116 if (attribute->uele_backing_vnode != vp)
1117 VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
1123 * Real work associated with removing an extended attribute from a vnode.
1124 * Assumes the attribute lock has already been grabbed.
1127 ufs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
1128 struct ucred *cred, struct thread *td)
1130 struct ufs_extattr_list_entry *attribute;
1131 struct ufs_extattr_header ueh;
1132 struct iovec local_aiov;
1133 struct uio local_aio;
1134 struct mount *mp = vp->v_mount;
1135 struct ufsmount *ump = VFSTOUFS(mp);
1136 struct inode *ip = VTOI(vp);
1138 int error = 0, ioflag;
1140 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1142 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1143 return (EOPNOTSUPP);
1144 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1147 error = extattr_check_cred(vp, attrnamespace, cred, td, IWRITE);
1151 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1156 * Find base offset of header in file based on file header size, and
1157 * data header size + maximum data size, indexed by inode number.
1159 base_offset = sizeof(struct ufs_extattr_fileheader) +
1160 ip->i_number * (sizeof(struct ufs_extattr_header) +
1161 attribute->uele_fileheader.uef_size);
1164 * Check to see if currently defined.
1166 bzero(&ueh, sizeof(struct ufs_extattr_header));
1168 local_aiov.iov_base = (caddr_t) &ueh;
1169 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1170 local_aio.uio_iov = &local_aiov;
1171 local_aio.uio_iovcnt = 1;
1172 local_aio.uio_rw = UIO_READ;
1173 local_aio.uio_segflg = UIO_SYSSPACE;
1174 local_aio.uio_td = td;
1175 local_aio.uio_offset = base_offset;
1176 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1179 * Don't need to get the lock on the backing vnode if the vnode we're
1180 * modifying is it, as we already hold the lock.
1182 if (attribute->uele_backing_vnode != vp)
1183 vn_lock(attribute->uele_backing_vnode,
1184 LK_EXCLUSIVE | LK_RETRY, td);
1186 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
1187 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
1189 goto vopunlock_exit;
1192 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
1194 goto vopunlock_exit;
1197 /* Valid for the current inode generation? */
1198 if (ueh.ueh_i_gen != ip->i_gen) {
1200 * The inode itself has a different generation number than
1201 * the attribute data. For now, the best solution is to
1202 * coerce this to undefined, and let it get cleaned up by
1203 * the next write or extattrctl clean.
1205 printf("ufs_extattr_rm (%s): inode number inconsistency (%d, %jd)\n",
1206 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
1208 goto vopunlock_exit;
1211 /* Flag it as not in use. */
1215 local_aiov.iov_base = (caddr_t) &ueh;
1216 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1217 local_aio.uio_iov = &local_aiov;
1218 local_aio.uio_iovcnt = 1;
1219 local_aio.uio_rw = UIO_WRITE;
1220 local_aio.uio_segflg = UIO_SYSSPACE;
1221 local_aio.uio_td = td;
1222 local_aio.uio_offset = base_offset;
1223 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1225 ioflag = IO_NODELOCKED;
1226 if (ufs_extattr_sync)
1228 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1229 ump->um_extattr.uepm_ucred);
1231 goto vopunlock_exit;
1233 if (local_aio.uio_resid != 0)
1237 VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
1243 * Called by UFS when an inode is no longer active and should have its
1244 * attributes stripped.
1247 ufs_extattr_vnode_inactive(struct vnode *vp, struct thread *td)
1249 struct ufs_extattr_list_entry *uele;
1250 struct mount *mp = vp->v_mount;
1251 struct ufsmount *ump = VFSTOUFS(mp);
1254 * In that case, we cannot lock. We should not have any active vnodes
1255 * on the fs if this is not yet initialized but is going to be, so
1256 * this can go unlocked.
1258 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
1261 ufs_extattr_uepm_lock(ump, td);
1263 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
1264 ufs_extattr_uepm_unlock(ump, td);
1268 LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
1269 ufs_extattr_rm(vp, uele->uele_attrnamespace,
1270 uele->uele_attrname, NULL, td);
1272 ufs_extattr_uepm_unlock(ump, td);
1275 #endif /* !UFS_EXTATTR */