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>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
56 #include <sys/dirent.h>
57 #include <sys/extattr.h>
59 #include <sys/sysctl.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/inode.h>
68 #include <ufs/ufs/ufs_extern.h>
72 FEATURE(ufs_extattr, "ufs extended attribute support");
74 static MALLOC_DEFINE(M_UFS_EXTATTR, "ufs_extattr", "ufs extended attribute");
76 static int ufs_extattr_sync = 0;
77 SYSCTL_INT(_debug, OID_AUTO, ufs_extattr_sync, CTLFLAG_RW, &ufs_extattr_sync,
80 static int ufs_extattr_valid_attrname(int attrnamespace,
81 const char *attrname);
82 static int ufs_extattr_enable_with_open(struct ufsmount *ump,
83 struct vnode *vp, int attrnamespace, const char *attrname,
85 static int ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
86 const char *attrname, struct vnode *backing_vnode,
88 static int ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
89 const char *attrname, struct thread *td);
90 static int ufs_extattr_get(struct vnode *vp, int attrnamespace,
91 const char *name, struct uio *uio, size_t *size,
92 struct ucred *cred, struct thread *td);
93 static int ufs_extattr_set(struct vnode *vp, int attrnamespace,
94 const char *name, struct uio *uio, struct ucred *cred,
96 static int ufs_extattr_rm(struct vnode *vp, int attrnamespace,
97 const char *name, struct ucred *cred, struct thread *td);
98 #ifdef UFS_EXTATTR_AUTOSTART
99 static int ufs_extattr_autostart_locked(struct mount *mp,
102 static int ufs_extattr_start_locked(struct ufsmount *ump,
106 * Per-FS attribute lock protecting attribute operations.
108 * XXXRW: Perhaps something more fine-grained would be appropriate, but at
109 * the end of the day we're going to contend on the vnode lock for the
110 * backing file anyway.
113 ufs_extattr_uepm_lock(struct ufsmount *ump)
116 sx_xlock(&ump->um_extattr.uepm_lock);
120 ufs_extattr_uepm_unlock(struct ufsmount *ump)
123 sx_xunlock(&ump->um_extattr.uepm_lock);
127 * Determine whether the name passed is a valid name for an actual
130 * Invalid currently consists of:
131 * NULL pointer for attrname
132 * zero-length attrname (used to retrieve application attribute list)
135 ufs_extattr_valid_attrname(int attrnamespace, const char *attrname)
138 if (attrname == NULL)
140 if (strlen(attrname) == 0)
146 * Locate an attribute given a name and mountpoint.
147 * Must be holding uepm lock for the mount point.
149 static struct ufs_extattr_list_entry *
150 ufs_extattr_find_attr(struct ufsmount *ump, int attrnamespace,
151 const char *attrname)
153 struct ufs_extattr_list_entry *search_attribute;
155 sx_assert(&ump->um_extattr.uepm_lock, SA_XLOCKED);
157 for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
158 search_attribute != NULL;
159 search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
160 if (!(strncmp(attrname, search_attribute->uele_attrname,
161 UFS_EXTATTR_MAXEXTATTRNAME)) &&
162 (attrnamespace == search_attribute->uele_attrnamespace)) {
163 return (search_attribute);
171 * Initialize per-FS structures supporting extended attributes. Do not
172 * start extended attributes yet.
175 ufs_extattr_uepm_init(struct ufs_extattr_per_mount *uepm)
178 uepm->uepm_flags = 0;
179 LIST_INIT(&uepm->uepm_list);
180 sx_init(&uepm->uepm_lock, "ufs_extattr_sx");
181 uepm->uepm_flags |= UFS_EXTATTR_UEPM_INITIALIZED;
185 * Destroy per-FS structures supporting extended attributes. Assumes
186 * that EAs have already been stopped, and will panic if not.
189 ufs_extattr_uepm_destroy(struct ufs_extattr_per_mount *uepm)
192 if (!(uepm->uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
193 panic("ufs_extattr_uepm_destroy: not initialized");
195 if ((uepm->uepm_flags & UFS_EXTATTR_UEPM_STARTED))
196 panic("ufs_extattr_uepm_destroy: called while still started");
199 * It's not clear that either order for the next two lines is
200 * ideal, and it should never be a problem if this is only called
201 * during unmount, and with vfs_busy().
203 uepm->uepm_flags &= ~UFS_EXTATTR_UEPM_INITIALIZED;
204 sx_destroy(&uepm->uepm_lock);
208 * Start extended attribute support on an FS.
211 ufs_extattr_start(struct mount *mp, struct thread *td)
213 struct ufsmount *ump;
218 ufs_extattr_uepm_lock(ump);
219 error = ufs_extattr_start_locked(ump, td);
220 ufs_extattr_uepm_unlock(ump);
225 ufs_extattr_start_locked(struct ufsmount *ump, struct thread *td)
227 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
229 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)
232 ump->um_extattr.uepm_flags |= UFS_EXTATTR_UEPM_STARTED;
233 ump->um_extattr.uepm_ucred = crhold(td->td_ucred);
237 #ifdef UFS_EXTATTR_AUTOSTART
239 * Helper routine: given a locked parent directory and filename, return
240 * the locked vnode of the inode associated with the name. Will not
241 * follow symlinks, may return any type of vnode. Lock on parent will
242 * be released even in the event of a failure. In the event that the
243 * target is the parent (i.e., "."), there will be two references and
244 * one lock, requiring the caller to possibly special-case.
246 #define UE_GETDIR_LOCKPARENT 1
247 #define UE_GETDIR_LOCKPARENT_DONT 2
249 ufs_extattr_lookup(struct vnode *start_dvp, int lockparent, char *dirname,
250 struct vnode **vp, struct thread *td)
252 struct vop_cachedlookup_args vargs;
253 struct componentname cnp;
254 struct vnode *target_vp;
257 bzero(&cnp, sizeof(cnp));
258 cnp.cn_nameiop = LOOKUP;
259 cnp.cn_flags = ISLASTCN;
260 if (lockparent == UE_GETDIR_LOCKPARENT)
261 cnp.cn_flags |= LOCKPARENT;
262 cnp.cn_lkflags = LK_EXCLUSIVE;
264 cnp.cn_cred = td->td_ucred;
265 cnp.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
266 cnp.cn_nameptr = cnp.cn_pnbuf;
267 error = copystr(dirname, cnp.cn_pnbuf, MAXPATHLEN,
268 (size_t *) &cnp.cn_namelen);
270 if (lockparent == UE_GETDIR_LOCKPARENT_DONT) {
271 VOP_UNLOCK(start_dvp, 0);
273 uma_zfree(namei_zone, cnp.cn_pnbuf);
274 printf("ufs_extattr_lookup: copystr failed\n");
277 cnp.cn_namelen--; /* trim nul termination */
278 vargs.a_gen.a_desc = NULL;
279 vargs.a_dvp = start_dvp;
280 vargs.a_vpp = &target_vp;
282 error = ufs_lookup(&vargs);
283 uma_zfree(namei_zone, cnp.cn_pnbuf);
286 * Error condition, may have to release the lock on the parent
287 * if ufs_lookup() didn't.
289 if (lockparent == UE_GETDIR_LOCKPARENT_DONT)
290 VOP_UNLOCK(start_dvp, 0);
293 * Check that ufs_lookup() didn't release the lock when we
296 if (lockparent == UE_GETDIR_LOCKPARENT)
297 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
302 if (target_vp == start_dvp)
303 panic("ufs_extattr_lookup: target_vp == start_dvp");
306 if (target_vp != start_dvp && lockparent == UE_GETDIR_LOCKPARENT_DONT)
307 VOP_UNLOCK(start_dvp, 0);
309 if (lockparent == UE_GETDIR_LOCKPARENT)
310 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
312 /* printf("ufs_extattr_lookup: success\n"); */
316 #endif /* !UFS_EXTATTR_AUTOSTART */
319 * Enable an EA using the passed filesystem, backing vnode, attribute name,
320 * namespace, and proc. Will perform a VOP_OPEN() on the vp, so expects vp
321 * to be locked when passed in. The vnode will be returned unlocked,
322 * regardless of success/failure of the function. As a result, the caller
323 * will always need to vrele(), but not vput().
326 ufs_extattr_enable_with_open(struct ufsmount *ump, struct vnode *vp,
327 int attrnamespace, const char *attrname, struct thread *td)
331 error = VOP_OPEN(vp, FREAD|FWRITE, td->td_ucred, td, NULL);
333 printf("ufs_extattr_enable_with_open.VOP_OPEN(): failed "
339 VOP_ADD_WRITECOUNT(vp, 1);
340 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", __func__, vp,
347 error = ufs_extattr_enable(ump, attrnamespace, attrname, vp, td);
349 vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
353 #ifdef UFS_EXTATTR_AUTOSTART
355 * Given a locked directory vnode, iterate over the names in the directory
356 * and use ufs_extattr_lookup() to retrieve locked vnodes of potential
357 * attribute files. Then invoke ufs_extattr_enable_with_open() on each
358 * to attempt to start the attribute. Leaves the directory locked on
362 ufs_extattr_iterate_directory(struct ufsmount *ump, struct vnode *dvp,
363 int attrnamespace, struct thread *td)
365 struct vop_readdir_args vargs;
366 struct dirent *dp, *edp;
367 struct vnode *attr_vp;
371 int error, eofflag = 0;
373 if (dvp->v_type != VDIR)
376 dirbuf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK);
378 auio.uio_iov = &aiov;
380 auio.uio_rw = UIO_READ;
381 auio.uio_segflg = UIO_SYSSPACE;
385 vargs.a_gen.a_desc = NULL;
388 vargs.a_cred = td->td_ucred;
389 vargs.a_eofflag = &eofflag;
390 vargs.a_ncookies = NULL;
391 vargs.a_cookies = NULL;
394 auio.uio_resid = DIRBLKSIZ;
395 aiov.iov_base = dirbuf;
396 aiov.iov_len = DIRBLKSIZ;
397 error = ufs_readdir(&vargs);
399 printf("ufs_extattr_iterate_directory: ufs_readdir "
404 edp = (struct dirent *)&dirbuf[DIRBLKSIZ - auio.uio_resid];
405 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
406 if (dp->d_reclen == 0)
408 error = ufs_extattr_lookup(dvp, UE_GETDIR_LOCKPARENT,
409 dp->d_name, &attr_vp, td);
411 printf("ufs_extattr_iterate_directory: lookup "
412 "%s %d\n", dp->d_name, error);
413 } else if (attr_vp == dvp) {
415 } else if (attr_vp->v_type != VREG) {
418 error = ufs_extattr_enable_with_open(ump,
419 attr_vp, attrnamespace, dp->d_name, td);
422 printf("ufs_extattr_iterate_directory: "
423 "enable %s %d\n", dp->d_name,
425 } else if (bootverbose) {
426 printf("UFS autostarted EA %s\n",
430 dp = (struct dirent *) ((char *)dp + dp->d_reclen);
435 free(dirbuf, M_TEMP);
441 * Auto-start of extended attributes, to be executed (optionally) at
445 ufs_extattr_autostart(struct mount *mp, struct thread *td)
447 struct ufsmount *ump;
451 ufs_extattr_uepm_lock(ump);
452 error = ufs_extattr_autostart_locked(mp, td);
453 ufs_extattr_uepm_unlock(ump);
458 ufs_extattr_autostart_locked(struct mount *mp, struct thread *td)
460 struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
461 struct ufsmount *ump = VFSTOUFS(mp);
465 * UFS_EXTATTR applies only to UFS1, as UFS2 uses native extended
466 * attributes, so don't autostart.
468 if (ump->um_fstype != UFS1)
472 * Does UFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
473 * If so, automatically start EA's.
475 error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp);
477 printf("ufs_extattr_autostart.VFS_ROOT() returned %d\n",
482 error = ufs_extattr_lookup(rvp, UE_GETDIR_LOCKPARENT_DONT,
483 UFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, td);
485 /* rvp ref'd but now unlocked */
489 if (rvp == attr_dvp) {
490 /* Should never happen. */
497 if (attr_dvp->v_type != VDIR) {
498 printf("ufs_extattr_autostart: %s != VDIR\n",
499 UFS_EXTATTR_FSROOTSUBDIR);
500 goto return_vput_attr_dvp;
503 error = ufs_extattr_start_locked(ump, td);
505 printf("ufs_extattr_autostart: ufs_extattr_start failed (%d)\n",
507 goto return_vput_attr_dvp;
511 * Look for two subdirectories: UFS_EXTATTR_SUBDIR_SYSTEM,
512 * UFS_EXTATTR_SUBDIR_USER. For each, iterate over the sub-directory,
513 * and start with appropriate type. Failures in either don't
514 * result in an over-all failure. attr_dvp is left locked to
515 * be cleaned up on exit.
517 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
518 UFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, td);
520 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
521 attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, td);
523 printf("ufs_extattr_iterate_directory returned %d\n",
525 vput(attr_system_dvp);
528 error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
529 UFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, td);
531 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
532 attr_user_dvp, EXTATTR_NAMESPACE_USER, td);
534 printf("ufs_extattr_iterate_directory returned %d\n",
539 /* Mask startup failures in sub-directories. */
542 return_vput_attr_dvp:
547 #endif /* !UFS_EXTATTR_AUTOSTART */
550 * Stop extended attribute support on an FS.
553 ufs_extattr_stop(struct mount *mp, struct thread *td)
555 struct ufs_extattr_list_entry *uele;
556 struct ufsmount *ump = VFSTOUFS(mp);
559 ufs_extattr_uepm_lock(ump);
561 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
566 while ((uele = LIST_FIRST(&ump->um_extattr.uepm_list)) != NULL) {
567 ufs_extattr_disable(ump, uele->uele_attrnamespace,
568 uele->uele_attrname, td);
571 ump->um_extattr.uepm_flags &= ~UFS_EXTATTR_UEPM_STARTED;
573 crfree(ump->um_extattr.uepm_ucred);
574 ump->um_extattr.uepm_ucred = NULL;
577 ufs_extattr_uepm_unlock(ump);
583 * Enable a named attribute on the specified filesystem; provide an
584 * unlocked backing vnode to hold the attribute data.
587 ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
588 const char *attrname, struct vnode *backing_vnode, struct thread *td)
590 struct ufs_extattr_list_entry *attribute;
595 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
597 if (backing_vnode->v_type != VREG)
600 attribute = malloc(sizeof(struct ufs_extattr_list_entry),
601 M_UFS_EXTATTR, M_WAITOK);
602 if (attribute == NULL)
605 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
610 if (ufs_extattr_find_attr(ump, attrnamespace, attrname)) {
615 strncpy(attribute->uele_attrname, attrname,
616 UFS_EXTATTR_MAXEXTATTRNAME);
617 attribute->uele_attrnamespace = attrnamespace;
618 bzero(&attribute->uele_fileheader,
619 sizeof(struct ufs_extattr_fileheader));
621 attribute->uele_backing_vnode = backing_vnode;
623 auio.uio_iov = &aiov;
625 aiov.iov_base = (caddr_t) &attribute->uele_fileheader;
626 aiov.iov_len = sizeof(struct ufs_extattr_fileheader);
627 auio.uio_resid = sizeof(struct ufs_extattr_fileheader);
628 auio.uio_offset = (off_t) 0;
629 auio.uio_segflg = UIO_SYSSPACE;
630 auio.uio_rw = UIO_READ;
633 vn_lock(backing_vnode, LK_SHARED | LK_RETRY);
634 error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
635 ump->um_extattr.uepm_ucred);
638 goto unlock_free_exit;
640 if (auio.uio_resid != 0) {
641 printf("ufs_extattr_enable: malformed attribute header\n");
643 goto unlock_free_exit;
646 if (attribute->uele_fileheader.uef_magic != UFS_EXTATTR_MAGIC) {
647 printf("ufs_extattr_enable: invalid attribute header magic\n");
649 goto unlock_free_exit;
652 if (attribute->uele_fileheader.uef_version != UFS_EXTATTR_VERSION) {
653 printf("ufs_extattr_enable: incorrect attribute header "
656 goto unlock_free_exit;
659 ASSERT_VOP_LOCKED(backing_vnode, "ufs_extattr_enable");
660 LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
663 VOP_UNLOCK(backing_vnode, 0);
667 VOP_UNLOCK(backing_vnode, 0);
670 free(attribute, M_UFS_EXTATTR);
675 * Disable extended attribute support on an FS.
678 ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
679 const char *attrname, struct thread *td)
681 struct ufs_extattr_list_entry *uele;
684 if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
687 uele = ufs_extattr_find_attr(ump, attrnamespace, attrname);
691 LIST_REMOVE(uele, uele_entries);
693 vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY);
694 ASSERT_VOP_LOCKED(uele->uele_backing_vnode, "ufs_extattr_disable");
695 VOP_UNLOCK(uele->uele_backing_vnode, 0);
696 error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
699 free(uele, M_UFS_EXTATTR);
705 * VFS call to manage extended attributes in UFS. If filename_vp is
706 * non-NULL, it must be passed in locked, and regardless of errors in
707 * processing, will be unlocked.
710 ufs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
711 int attrnamespace, const char *attrname)
713 struct ufsmount *ump = VFSTOUFS(mp);
714 struct thread *td = curthread;
718 * Processes with privilege, but in jail, are not allowed to
719 * configure extended attributes.
721 error = priv_check(td, PRIV_UFS_EXTATTRCTL);
723 if (filename_vp != NULL)
724 VOP_UNLOCK(filename_vp, 0);
729 * We only allow extattrctl(2) on UFS1 file systems, as UFS2 uses
730 * native extended attributes.
732 if (ump->um_fstype != UFS1) {
733 if (filename_vp != NULL)
734 VOP_UNLOCK(filename_vp, 0);
739 case UFS_EXTATTR_CMD_START:
740 if (filename_vp != NULL) {
741 VOP_UNLOCK(filename_vp, 0);
744 if (attrname != NULL)
747 error = ufs_extattr_start(mp, td);
751 case UFS_EXTATTR_CMD_STOP:
752 if (filename_vp != NULL) {
753 VOP_UNLOCK(filename_vp, 0);
756 if (attrname != NULL)
759 error = ufs_extattr_stop(mp, td);
763 case UFS_EXTATTR_CMD_ENABLE:
765 if (filename_vp == NULL)
767 if (attrname == NULL) {
768 VOP_UNLOCK(filename_vp, 0);
773 * ufs_extattr_enable_with_open() will always unlock the
774 * vnode, regardless of failure.
776 ufs_extattr_uepm_lock(ump);
777 error = ufs_extattr_enable_with_open(ump, filename_vp,
778 attrnamespace, attrname, td);
779 ufs_extattr_uepm_unlock(ump);
783 case UFS_EXTATTR_CMD_DISABLE:
785 if (filename_vp != NULL) {
786 VOP_UNLOCK(filename_vp, 0);
789 if (attrname == NULL)
792 ufs_extattr_uepm_lock(ump);
793 error = ufs_extattr_disable(ump, attrnamespace, attrname,
795 ufs_extattr_uepm_unlock(ump);
805 * Vnode operating to retrieve a named extended attribute.
808 ufs_getextattr(struct vop_getextattr_args *ap)
811 IN struct vnode *a_vp;
812 IN int a_attrnamespace;
813 IN const char *a_name;
814 INOUT struct uio *a_uio;
816 IN struct ucred *a_cred;
817 IN struct thread *a_td;
821 struct mount *mp = ap->a_vp->v_mount;
822 struct ufsmount *ump = VFSTOUFS(mp);
825 ufs_extattr_uepm_lock(ump);
827 error = ufs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
828 ap->a_uio, ap->a_size, ap->a_cred, ap->a_td);
830 ufs_extattr_uepm_unlock(ump);
836 * Real work associated with retrieving a named attribute--assumes that
837 * the attribute lock has already been grabbed.
840 ufs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
841 struct uio *uio, size_t *size, struct ucred *cred, struct thread *td)
843 struct ufs_extattr_list_entry *attribute;
844 struct ufs_extattr_header ueh;
845 struct iovec local_aiov;
846 struct uio local_aio;
847 struct mount *mp = vp->v_mount;
848 struct ufsmount *ump = VFSTOUFS(mp);
849 struct inode *ip = VTOI(vp);
854 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
857 if (strlen(name) == 0)
860 error = extattr_check_cred(vp, attrnamespace, cred, td, VREAD);
864 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
869 * Allow only offsets of zero to encourage the read/replace
870 * extended attribute semantic. Otherwise we can't guarantee
871 * atomicity, as we don't provide locks for extended attributes.
873 if (uio != NULL && uio->uio_offset != 0)
877 * Find base offset of header in file based on file header size, and
878 * data header size + maximum data size, indexed by inode number.
880 base_offset = sizeof(struct ufs_extattr_fileheader) +
881 ip->i_number * (sizeof(struct ufs_extattr_header) +
882 attribute->uele_fileheader.uef_size);
885 * Read in the data header to see if the data is defined, and if so
888 bzero(&ueh, sizeof(struct ufs_extattr_header));
889 local_aiov.iov_base = (caddr_t) &ueh;
890 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
891 local_aio.uio_iov = &local_aiov;
892 local_aio.uio_iovcnt = 1;
893 local_aio.uio_rw = UIO_READ;
894 local_aio.uio_segflg = UIO_SYSSPACE;
895 local_aio.uio_td = td;
896 local_aio.uio_offset = base_offset;
897 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
902 * Don't need to get a lock on the backing file if the getattr is
903 * being applied to the backing file, as the lock is already held.
905 if (attribute->uele_backing_vnode != vp)
906 vn_lock(attribute->uele_backing_vnode, LK_SHARED | LK_RETRY);
908 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
909 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
914 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
919 /* Valid for the current inode generation? */
920 if (ueh.ueh_i_gen != ip->i_gen) {
922 * The inode itself has a different generation number
923 * than the attribute data. For now, the best solution
924 * is to coerce this to undefined, and let it get cleaned
925 * up by the next write or extattrctl clean.
927 printf("ufs_extattr_get (%s): inode number inconsistency (%d, %ju)\n",
928 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (uintmax_t)ip->i_gen);
933 /* Local size consistency check. */
934 if (ueh.ueh_len > attribute->uele_fileheader.uef_size) {
939 /* Return full data size if caller requested it. */
943 /* Return data if the caller requested it. */
945 /* Allow for offset into the attribute data. */
946 uio->uio_offset = base_offset + sizeof(struct
950 * Figure out maximum to transfer -- use buffer size and
953 len = MIN(uio->uio_resid, ueh.ueh_len);
954 old_len = uio->uio_resid;
955 uio->uio_resid = len;
957 error = VOP_READ(attribute->uele_backing_vnode, uio,
958 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
962 uio->uio_resid = old_len - (len - uio->uio_resid);
970 if (attribute->uele_backing_vnode != vp)
971 VOP_UNLOCK(attribute->uele_backing_vnode, 0);
977 * Vnode operation to remove a named attribute.
980 ufs_deleteextattr(struct vop_deleteextattr_args *ap)
983 IN struct vnode *a_vp;
984 IN int a_attrnamespace;
985 IN const char *a_name;
986 IN struct ucred *a_cred;
987 IN struct thread *a_td;
991 struct mount *mp = ap->a_vp->v_mount;
992 struct ufsmount *ump = VFSTOUFS(mp);
995 ufs_extattr_uepm_lock(ump);
997 error = ufs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
998 ap->a_cred, ap->a_td);
1001 ufs_extattr_uepm_unlock(ump);
1007 * Vnode operation to set a named attribute.
1010 ufs_setextattr(struct vop_setextattr_args *ap)
1013 IN struct vnode *a_vp;
1014 IN int a_attrnamespace;
1015 IN const char *a_name;
1016 INOUT struct uio *a_uio;
1017 IN struct ucred *a_cred;
1018 IN struct thread *a_td;
1022 struct mount *mp = ap->a_vp->v_mount;
1023 struct ufsmount *ump = VFSTOUFS(mp);
1027 * XXX: No longer a supported way to delete extended attributes.
1029 if (ap->a_uio == NULL)
1032 ufs_extattr_uepm_lock(ump);
1034 error = ufs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
1035 ap->a_uio, ap->a_cred, ap->a_td);
1037 ufs_extattr_uepm_unlock(ump);
1043 * Real work associated with setting a vnode's extended attributes;
1044 * assumes that the attribute lock has already been grabbed.
1047 ufs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
1048 struct uio *uio, struct ucred *cred, struct thread *td)
1050 struct ufs_extattr_list_entry *attribute;
1051 struct ufs_extattr_header ueh;
1052 struct iovec local_aiov;
1053 struct uio local_aio;
1054 struct mount *mp = vp->v_mount;
1055 struct ufsmount *ump = VFSTOUFS(mp);
1056 struct inode *ip = VTOI(vp);
1058 int error = 0, ioflag;
1060 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1062 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1063 return (EOPNOTSUPP);
1064 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1067 error = extattr_check_cred(vp, attrnamespace, cred, td, VWRITE);
1071 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1076 * Early rejection of invalid offsets/length.
1077 * Reject: any offset but 0 (replace)
1078 * Any size greater than attribute size limit
1080 if (uio->uio_offset != 0 ||
1081 uio->uio_resid > attribute->uele_fileheader.uef_size)
1085 * Find base offset of header in file based on file header size, and
1086 * data header size + maximum data size, indexed by inode number.
1088 base_offset = sizeof(struct ufs_extattr_fileheader) +
1089 ip->i_number * (sizeof(struct ufs_extattr_header) +
1090 attribute->uele_fileheader.uef_size);
1093 * Write out a data header for the data.
1095 ueh.ueh_len = uio->uio_resid;
1096 ueh.ueh_flags = UFS_EXTATTR_ATTR_FLAG_INUSE;
1097 ueh.ueh_i_gen = ip->i_gen;
1098 local_aiov.iov_base = (caddr_t) &ueh;
1099 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1100 local_aio.uio_iov = &local_aiov;
1101 local_aio.uio_iovcnt = 1;
1102 local_aio.uio_rw = UIO_WRITE;
1103 local_aio.uio_segflg = UIO_SYSSPACE;
1104 local_aio.uio_td = td;
1105 local_aio.uio_offset = base_offset;
1106 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1111 * Don't need to get a lock on the backing file if the setattr is
1112 * being applied to the backing file, as the lock is already held.
1114 if (attribute->uele_backing_vnode != vp)
1115 vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
1117 ioflag = IO_NODELOCKED;
1118 if (ufs_extattr_sync)
1120 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1121 ump->um_extattr.uepm_ucred);
1123 goto vopunlock_exit;
1125 if (local_aio.uio_resid != 0) {
1127 goto vopunlock_exit;
1131 * Write out user data.
1133 uio->uio_offset = base_offset + sizeof(struct ufs_extattr_header);
1135 ioflag = IO_NODELOCKED;
1136 if (ufs_extattr_sync)
1138 error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
1139 ump->um_extattr.uepm_ucred);
1142 uio->uio_offset = 0;
1144 if (attribute->uele_backing_vnode != vp)
1145 VOP_UNLOCK(attribute->uele_backing_vnode, 0);
1151 * Real work associated with removing an extended attribute from a vnode.
1152 * Assumes the attribute lock has already been grabbed.
1155 ufs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
1156 struct ucred *cred, struct thread *td)
1158 struct ufs_extattr_list_entry *attribute;
1159 struct ufs_extattr_header ueh;
1160 struct iovec local_aiov;
1161 struct uio local_aio;
1162 struct mount *mp = vp->v_mount;
1163 struct ufsmount *ump = VFSTOUFS(mp);
1164 struct inode *ip = VTOI(vp);
1166 int error = 0, ioflag;
1168 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1170 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1171 return (EOPNOTSUPP);
1172 if (!ufs_extattr_valid_attrname(attrnamespace, name))
1175 error = extattr_check_cred(vp, attrnamespace, cred, td, VWRITE);
1179 attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1184 * Find base offset of header in file based on file header size, and
1185 * data header size + maximum data size, indexed by inode number.
1187 base_offset = sizeof(struct ufs_extattr_fileheader) +
1188 ip->i_number * (sizeof(struct ufs_extattr_header) +
1189 attribute->uele_fileheader.uef_size);
1192 * Check to see if currently defined.
1194 bzero(&ueh, sizeof(struct ufs_extattr_header));
1196 local_aiov.iov_base = (caddr_t) &ueh;
1197 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1198 local_aio.uio_iov = &local_aiov;
1199 local_aio.uio_iovcnt = 1;
1200 local_aio.uio_rw = UIO_READ;
1201 local_aio.uio_segflg = UIO_SYSSPACE;
1202 local_aio.uio_td = td;
1203 local_aio.uio_offset = base_offset;
1204 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1207 * Don't need to get the lock on the backing vnode if the vnode we're
1208 * modifying is it, as we already hold the lock.
1210 if (attribute->uele_backing_vnode != vp)
1211 vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
1213 error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
1214 IO_NODELOCKED, ump->um_extattr.uepm_ucred);
1216 goto vopunlock_exit;
1219 if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
1221 goto vopunlock_exit;
1224 /* Valid for the current inode generation? */
1225 if (ueh.ueh_i_gen != ip->i_gen) {
1227 * The inode itself has a different generation number than
1228 * the attribute data. For now, the best solution is to
1229 * coerce this to undefined, and let it get cleaned up by
1230 * the next write or extattrctl clean.
1232 printf("ufs_extattr_rm (%s): inode number inconsistency (%d, %jd)\n",
1233 mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
1235 goto vopunlock_exit;
1238 /* Flag it as not in use. */
1242 local_aiov.iov_base = (caddr_t) &ueh;
1243 local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1244 local_aio.uio_iov = &local_aiov;
1245 local_aio.uio_iovcnt = 1;
1246 local_aio.uio_rw = UIO_WRITE;
1247 local_aio.uio_segflg = UIO_SYSSPACE;
1248 local_aio.uio_td = td;
1249 local_aio.uio_offset = base_offset;
1250 local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1252 ioflag = IO_NODELOCKED;
1253 if (ufs_extattr_sync)
1255 error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1256 ump->um_extattr.uepm_ucred);
1258 goto vopunlock_exit;
1260 if (local_aio.uio_resid != 0)
1264 VOP_UNLOCK(attribute->uele_backing_vnode, 0);
1270 * Called by UFS when an inode is no longer active and should have its
1271 * attributes stripped.
1274 ufs_extattr_vnode_inactive(struct vnode *vp, struct thread *td)
1276 struct ufs_extattr_list_entry *uele;
1277 struct mount *mp = vp->v_mount;
1278 struct ufsmount *ump = VFSTOUFS(mp);
1281 * In that case, we cannot lock. We should not have any active vnodes
1282 * on the fs if this is not yet initialized but is going to be, so
1283 * this can go unlocked.
1285 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
1288 ufs_extattr_uepm_lock(ump);
1290 if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
1291 ufs_extattr_uepm_unlock(ump);
1295 LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
1296 ufs_extattr_rm(vp, uele->uele_attrnamespace,
1297 uele->uele_attrname, NULL, td);
1299 ufs_extattr_uepm_unlock(ump);
1302 #endif /* !UFS_EXTATTR */