]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ufs/ufs_extattr.c
This commit was generated by cvs2svn to compensate for changes in r171164,
[FreeBSD/FreeBSD.git] / sys / ufs / ufs / ufs_extattr.c
1 /*-
2  * Copyright (c) 1999-2002 Robert N. M. Watson
3  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed by Robert Watson for the TrustedBSD Project.
7  *
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.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  */
35
36 /*
37  * Support for filesystem extended attribute: UFS-specific support functions.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include "opt_ufs.h"
44
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>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/lock.h>
56 #include <sys/dirent.h>
57 #include <sys/extattr.h>
58 #include <sys/sysctl.h>
59
60 #include <vm/uma.h>
61
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/extattr.h>
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufs_extern.h>
68
69 #ifdef UFS_EXTATTR
70
71 static MALLOC_DEFINE(M_UFS_EXTATTR, "ufs_extattr", "ufs extended attribute");
72
73 static int ufs_extattr_sync = 0;
74 SYSCTL_INT(_debug, OID_AUTO, ufs_extattr_sync, CTLFLAG_RW, &ufs_extattr_sync,
75     0, "");
76
77 static int      ufs_extattr_valid_attrname(int attrnamespace,
78                     const char *attrname);
79 static int      ufs_extattr_enable_with_open(struct ufsmount *ump,
80                     struct vnode *vp, int attrnamespace, const char *attrname,
81                     struct thread *td);
82 static int      ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
83                     const char *attrname, struct vnode *backing_vnode,
84                     struct thread *td);
85 static int      ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
86                     const char *attrname, struct thread *td);
87 static int      ufs_extattr_get(struct vnode *vp, int attrnamespace,
88                     const char *name, struct uio *uio, size_t *size,
89                     struct ucred *cred, struct thread *td);
90 static int      ufs_extattr_set(struct vnode *vp, int attrnamespace,
91                     const char *name, struct uio *uio, struct ucred *cred,
92                     struct thread *td);
93 static int      ufs_extattr_rm(struct vnode *vp, int attrnamespace,
94                     const char *name, struct ucred *cred, struct thread *td);
95
96 /*
97  * Per-FS attribute lock protecting attribute operations.
98  * XXX Right now there is a lot of lock contention due to having a single
99  * lock per-FS; really, this should be far more fine-grained.
100  */
101 static void
102 ufs_extattr_uepm_lock(struct ufsmount *ump, struct thread *td)
103 {
104
105         /* Ideally, LK_CANRECURSE would not be used, here. */
106         lockmgr(&ump->um_extattr.uepm_lock, LK_EXCLUSIVE | LK_RETRY |
107             LK_CANRECURSE, 0, td);
108 }
109
110 static void
111 ufs_extattr_uepm_unlock(struct ufsmount *ump, struct thread *td)
112 {
113
114         lockmgr(&ump->um_extattr.uepm_lock, LK_RELEASE, 0, td);
115 }
116
117 /*-
118  * Determine whether the name passed is a valid name for an actual
119  * attribute.
120  *
121  * Invalid currently consists of:
122  *       NULL pointer for attrname
123  *       zero-length attrname (used to retrieve application attribute list)
124  */
125 static int
126 ufs_extattr_valid_attrname(int attrnamespace, const char *attrname)
127 {
128
129         if (attrname == NULL)
130                 return (0);
131         if (strlen(attrname) == 0)
132                 return (0);
133         return (1);
134 }
135
136 /*
137  * Locate an attribute given a name and mountpoint.
138  * Must be holding uepm lock for the mount point.
139  */
140 static struct ufs_extattr_list_entry *
141 ufs_extattr_find_attr(struct ufsmount *ump, int attrnamespace,
142     const char *attrname)
143 {
144         struct ufs_extattr_list_entry *search_attribute;
145
146         for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
147             search_attribute != NULL;
148             search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
149                 if (!(strncmp(attrname, search_attribute->uele_attrname,
150                     UFS_EXTATTR_MAXEXTATTRNAME)) &&
151                     (attrnamespace == search_attribute->uele_attrnamespace)) {
152                         return (search_attribute);
153                 }
154         }
155
156         return (0);
157 }
158
159 /*
160  * Initialize per-FS structures supporting extended attributes.  Do not
161  * start extended attributes yet.
162  */
163 void
164 ufs_extattr_uepm_init(struct ufs_extattr_per_mount *uepm)
165 {
166
167         uepm->uepm_flags = 0;
168
169         LIST_INIT(&uepm->uepm_list);
170         /* XXX is PVFS right, here? */
171         lockinit(&uepm->uepm_lock, PVFS, "extattr", 0, 0);
172         uepm->uepm_flags |= UFS_EXTATTR_UEPM_INITIALIZED;
173 }
174
175 /*
176  * Destroy per-FS structures supporting extended attributes.  Assumes
177  * that EAs have already been stopped, and will panic if not.
178  */
179 void
180 ufs_extattr_uepm_destroy(struct ufs_extattr_per_mount *uepm)
181 {
182
183         if (!(uepm->uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
184                 panic("ufs_extattr_uepm_destroy: not initialized");
185
186         if ((uepm->uepm_flags & UFS_EXTATTR_UEPM_STARTED))
187                 panic("ufs_extattr_uepm_destroy: called while still started");
188
189         /*
190          * It's not clear that either order for the next two lines is
191          * ideal, and it should never be a problem if this is only called
192          * during unmount, and with vfs_busy().
193          */
194         uepm->uepm_flags &= ~UFS_EXTATTR_UEPM_INITIALIZED;
195         lockdestroy(&uepm->uepm_lock);
196 }
197
198 /*
199  * Start extended attribute support on an FS.
200  */
201 int
202 ufs_extattr_start(struct mount *mp, struct thread *td)
203 {
204         struct ufsmount *ump;
205         int error = 0;
206
207         ump = VFSTOUFS(mp);
208
209         ufs_extattr_uepm_lock(ump, td);
210
211         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED)) {
212                 error = EOPNOTSUPP;
213                 goto unlock;
214         }
215         if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED) {
216                 error = EBUSY;
217                 goto unlock;
218         }
219
220         ump->um_extattr.uepm_flags |= UFS_EXTATTR_UEPM_STARTED;
221
222         ump->um_extattr.uepm_ucred = crhold(td->td_ucred);
223
224 unlock:
225         ufs_extattr_uepm_unlock(ump, td);
226
227         return (error);
228 }
229
230 #ifdef UFS_EXTATTR_AUTOSTART
231 /*
232  * Helper routine: given a locked parent directory and filename, return
233  * the locked vnode of the inode associated with the name.  Will not
234  * follow symlinks, may return any type of vnode.  Lock on parent will
235  * be released even in the event of a failure.  In the event that the
236  * target is the parent (i.e., "."), there will be two references and
237  * one lock, requiring the caller to possibly special-case.
238  */
239 #define UE_GETDIR_LOCKPARENT    1
240 #define UE_GETDIR_LOCKPARENT_DONT       2
241 static int
242 ufs_extattr_lookup(struct vnode *start_dvp, int lockparent, char *dirname,
243     struct vnode **vp, struct thread *td)
244 {
245         struct vop_cachedlookup_args vargs;
246         struct componentname cnp;
247         struct vnode *target_vp;
248         int error;
249
250         bzero(&cnp, sizeof(cnp));
251         cnp.cn_nameiop = LOOKUP;
252         cnp.cn_flags = ISLASTCN;
253         if (lockparent == UE_GETDIR_LOCKPARENT)
254                 cnp.cn_flags |= LOCKPARENT;
255         cnp.cn_lkflags = LK_EXCLUSIVE;
256         cnp.cn_thread = td;
257         cnp.cn_cred = td->td_ucred;
258         cnp.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
259         cnp.cn_nameptr = cnp.cn_pnbuf;
260         error = copystr(dirname, cnp.cn_pnbuf, MAXPATHLEN,
261             (size_t *) &cnp.cn_namelen);
262         if (error) {
263                 if (lockparent == UE_GETDIR_LOCKPARENT_DONT) {
264                         VOP_UNLOCK(start_dvp, 0, td);
265                 }
266                 uma_zfree(namei_zone, cnp.cn_pnbuf);
267                 printf("ufs_extattr_lookup: copystr failed\n");
268                 return (error);
269         }
270         cnp.cn_namelen--;       /* trim nul termination */
271         vargs.a_gen.a_desc = NULL;
272         vargs.a_dvp = start_dvp;
273         vargs.a_vpp = &target_vp;
274         vargs.a_cnp = &cnp;
275         error = ufs_lookup(&vargs);
276         uma_zfree(namei_zone, cnp.cn_pnbuf);
277         if (error) {
278                 /*
279                  * Error condition, may have to release the lock on the parent
280                  * if ufs_lookup() didn't.
281                  */
282                 if (lockparent == UE_GETDIR_LOCKPARENT_DONT)
283                         VOP_UNLOCK(start_dvp, 0, td);
284
285                 /*
286                  * Check that ufs_lookup() didn't release the lock when we
287                  * didn't want it to.
288                  */
289                 if (lockparent == UE_GETDIR_LOCKPARENT)
290                         ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
291
292                 return (error);
293         }
294 /*
295         if (target_vp == start_dvp)
296                 panic("ufs_extattr_lookup: target_vp == start_dvp");
297 */
298
299         if (target_vp != start_dvp && lockparent == UE_GETDIR_LOCKPARENT_DONT)
300                 VOP_UNLOCK(start_dvp, 0, td);
301
302         if (lockparent == UE_GETDIR_LOCKPARENT)
303                 ASSERT_VOP_LOCKED(start_dvp, "ufs_extattr_lookup");
304
305         /* printf("ufs_extattr_lookup: success\n"); */
306         *vp = target_vp;
307         return (0);
308 }
309 #endif /* !UFS_EXTATTR_AUTOSTART */
310
311 /*
312  * Enable an EA using the passed filesystem, backing vnode, attribute name,
313  * namespace, and proc.  Will perform a VOP_OPEN() on the vp, so expects vp
314  * to be locked when passed in.  The vnode will be returned unlocked,
315  * regardless of success/failure of the function.  As a result, the caller
316  * will always need to vrele(), but not vput().
317  */
318 static int
319 ufs_extattr_enable_with_open(struct ufsmount *ump, struct vnode *vp,
320     int attrnamespace, const char *attrname, struct thread *td)
321 {
322         int error;
323
324         error = VOP_OPEN(vp, FREAD|FWRITE, td->td_ucred, td, NULL);
325         if (error) {
326                 printf("ufs_extattr_enable_with_open.VOP_OPEN(): failed "
327                     "with %d\n", error);
328                 VOP_UNLOCK(vp, 0, td);
329                 return (error);
330         }
331
332         vp->v_writecount++;
333
334         vref(vp);
335
336         VOP_UNLOCK(vp, 0, td);
337
338         error = ufs_extattr_enable(ump, attrnamespace, attrname, vp, td);
339         if (error != 0)
340                 vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
341         return (error);
342 }
343
344 #ifdef UFS_EXTATTR_AUTOSTART
345 /*
346  * Given a locked directory vnode, iterate over the names in the directory
347  * and use ufs_extattr_lookup() to retrieve locked vnodes of potential
348  * attribute files.  Then invoke ufs_extattr_enable_with_open() on each
349  * to attempt to start the attribute.  Leaves the directory locked on
350  * exit.
351  */
352 static int
353 ufs_extattr_iterate_directory(struct ufsmount *ump, struct vnode *dvp,
354     int attrnamespace, struct thread *td)
355 {
356         struct vop_readdir_args vargs;
357         struct dirent *dp, *edp;
358         struct vnode *attr_vp;
359         struct uio auio;
360         struct iovec aiov;
361         char *dirbuf;
362         int error, eofflag = 0;
363
364         if (dvp->v_type != VDIR)
365                 return (ENOTDIR);
366
367         MALLOC(dirbuf, char *, DIRBLKSIZ, M_TEMP, M_WAITOK);
368
369         auio.uio_iov = &aiov;
370         auio.uio_iovcnt = 1;
371         auio.uio_rw = UIO_READ;
372         auio.uio_segflg = UIO_SYSSPACE;
373         auio.uio_td = td;
374         auio.uio_offset = 0;
375
376         vargs.a_gen.a_desc = NULL;
377         vargs.a_vp = dvp;
378         vargs.a_uio = &auio;
379         vargs.a_cred = td->td_ucred;
380         vargs.a_eofflag = &eofflag;
381         vargs.a_ncookies = NULL;
382         vargs.a_cookies = NULL;
383
384         while (!eofflag) {
385                 auio.uio_resid = DIRBLKSIZ;
386                 aiov.iov_base = dirbuf;
387                 aiov.iov_len = DIRBLKSIZ;
388                 error = ufs_readdir(&vargs);
389                 if (error) {
390                         printf("ufs_extattr_iterate_directory: ufs_readdir "
391                             "%d\n", error);
392                         return (error);
393                 }
394
395                 /*
396                  * XXXRW: While in UFS, we always get DIRBLKSIZ returns from
397                  * the directory code on success, on other file systems this
398                  * may not be the case.  For portability, we should check the
399                  * read length on return from ufs_readdir().
400                  */
401                 edp = (struct dirent *)&dirbuf[DIRBLKSIZ];
402                 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
403 #if (BYTE_ORDER == LITTLE_ENDIAN)
404                         dp->d_type = dp->d_namlen;
405                         dp->d_namlen = 0;
406 #else
407                         dp->d_type = 0;
408 #endif
409                         if (dp->d_reclen == 0)
410                                 break;
411                         error = ufs_extattr_lookup(dvp, UE_GETDIR_LOCKPARENT,
412                             dp->d_name, &attr_vp, td);
413                         if (error) {
414                                 printf("ufs_extattr_iterate_directory: lookup "
415                                     "%s %d\n", dp->d_name, error);
416                         } else if (attr_vp == dvp) {
417                                 vrele(attr_vp);
418                         } else if (attr_vp->v_type != VREG) {
419                                 vput(attr_vp);
420                         } else {
421                                 error = ufs_extattr_enable_with_open(ump,
422                                     attr_vp, attrnamespace, dp->d_name, td);
423                                 vrele(attr_vp);
424                                 if (error) {
425                                         printf("ufs_extattr_iterate_directory: "
426                                             "enable %s %d\n", dp->d_name,
427                                             error);
428                                 } else if (bootverbose) {
429                                         printf("UFS autostarted EA %s\n",
430                                             dp->d_name);
431                                 }
432                         }
433                         dp = (struct dirent *) ((char *)dp + dp->d_reclen);
434                         if (dp >= edp)
435                                 break;
436                 }
437         }
438         FREE(dirbuf, M_TEMP);
439         
440         return (0);
441 }
442
443 /*
444  * Auto-start of extended attributes, to be executed (optionally) at
445  * mount-time.
446  */
447 int
448 ufs_extattr_autostart(struct mount *mp, struct thread *td)
449 {
450         struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
451         int error;
452
453         /*
454          * Does UFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
455          * If so, automatically start EA's.
456          */
457         error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, td);
458         if (error) {
459                 printf("ufs_extattr_autostart.VFS_ROOT() returned %d\n",
460                     error);
461                 return (error);
462         }
463
464         error = ufs_extattr_lookup(rvp, UE_GETDIR_LOCKPARENT_DONT,
465             UFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, td);
466         if (error) {
467                 /* rvp ref'd but now unlocked */
468                 vrele(rvp);
469                 return (error);
470         }
471         if (rvp == attr_dvp) {
472                 /* Should never happen. */
473                 vput(rvp);
474                 vrele(attr_dvp);
475                 return (EINVAL);
476         }
477         vrele(rvp);
478
479         if (attr_dvp->v_type != VDIR) {
480                 printf("ufs_extattr_autostart: %s != VDIR\n",
481                     UFS_EXTATTR_FSROOTSUBDIR);
482                 goto return_vput_attr_dvp;
483         }
484
485         error = ufs_extattr_start(mp, td);
486         if (error) {
487                 printf("ufs_extattr_autostart: ufs_extattr_start failed (%d)\n",
488                     error);
489                 goto return_vput_attr_dvp;
490         }
491
492         /*
493          * Look for two subdirectories: UFS_EXTATTR_SUBDIR_SYSTEM,
494          * UFS_EXTATTR_SUBDIR_USER.  For each, iterate over the sub-directory,
495          * and start with appropriate type.  Failures in either don't
496          * result in an over-all failure.  attr_dvp is left locked to
497          * be cleaned up on exit.
498          */
499         error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
500             UFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, td);
501         if (!error) {
502                 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
503                     attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, td);
504                 if (error)
505                         printf("ufs_extattr_iterate_directory returned %d\n",
506                             error);
507                 vput(attr_system_dvp);
508         }
509
510         error = ufs_extattr_lookup(attr_dvp, UE_GETDIR_LOCKPARENT,
511             UFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, td);
512         if (!error) {
513                 error = ufs_extattr_iterate_directory(VFSTOUFS(mp),
514                     attr_user_dvp, EXTATTR_NAMESPACE_USER, td);
515                 if (error)
516                         printf("ufs_extattr_iterate_directory returned %d\n",
517                             error);
518                 vput(attr_user_dvp);
519         }
520
521         /* Mask startup failures in sub-directories. */
522         error = 0;
523
524 return_vput_attr_dvp:
525         vput(attr_dvp);
526
527         return (error);
528 }
529 #endif /* !UFS_EXTATTR_AUTOSTART */
530
531 /*
532  * Stop extended attribute support on an FS.
533  */
534 int
535 ufs_extattr_stop(struct mount *mp, struct thread *td)
536 {
537         struct ufs_extattr_list_entry *uele;
538         struct ufsmount *ump = VFSTOUFS(mp);
539         int error = 0;
540
541         ufs_extattr_uepm_lock(ump, td);
542
543         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
544                 error = EOPNOTSUPP;
545                 goto unlock;
546         }
547
548         while ((uele = LIST_FIRST(&ump->um_extattr.uepm_list)) != NULL) {
549                 ufs_extattr_disable(ump, uele->uele_attrnamespace,
550                     uele->uele_attrname, td);
551         }
552
553         ump->um_extattr.uepm_flags &= ~UFS_EXTATTR_UEPM_STARTED;
554
555         crfree(ump->um_extattr.uepm_ucred);
556         ump->um_extattr.uepm_ucred = NULL;
557
558 unlock:
559         ufs_extattr_uepm_unlock(ump, td);
560
561         return (error);
562 }
563
564 /*
565  * Enable a named attribute on the specified filesystem; provide an
566  * unlocked backing vnode to hold the attribute data.
567  */
568 static int
569 ufs_extattr_enable(struct ufsmount *ump, int attrnamespace,
570     const char *attrname, struct vnode *backing_vnode, struct thread *td)
571 {
572         struct ufs_extattr_list_entry *attribute;
573         struct iovec aiov;
574         struct uio auio;
575         int error = 0;
576
577         if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
578                 return (EINVAL);
579         if (backing_vnode->v_type != VREG)
580                 return (EINVAL);
581
582         MALLOC(attribute, struct ufs_extattr_list_entry *,
583             sizeof(struct ufs_extattr_list_entry), M_UFS_EXTATTR, M_WAITOK);
584         if (attribute == NULL)
585                 return (ENOMEM);
586
587         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
588                 error = EOPNOTSUPP;
589                 goto free_exit;
590         }
591
592         if (ufs_extattr_find_attr(ump, attrnamespace, attrname)) {
593                 error = EEXIST;
594                 goto free_exit;
595         }
596
597         strncpy(attribute->uele_attrname, attrname,
598             UFS_EXTATTR_MAXEXTATTRNAME);
599         attribute->uele_attrnamespace = attrnamespace;
600         bzero(&attribute->uele_fileheader,
601             sizeof(struct ufs_extattr_fileheader));
602         
603         attribute->uele_backing_vnode = backing_vnode;
604
605         auio.uio_iov = &aiov;
606         auio.uio_iovcnt = 1;
607         aiov.iov_base = (caddr_t) &attribute->uele_fileheader;
608         aiov.iov_len = sizeof(struct ufs_extattr_fileheader);
609         auio.uio_resid = sizeof(struct ufs_extattr_fileheader);
610         auio.uio_offset = (off_t) 0;
611         auio.uio_segflg = UIO_SYSSPACE;
612         auio.uio_rw = UIO_READ;
613         auio.uio_td = td;
614
615         vn_lock(backing_vnode, LK_SHARED | LK_RETRY, td);
616         error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
617             ump->um_extattr.uepm_ucred);
618
619         if (error)
620                 goto unlock_free_exit;
621
622         if (auio.uio_resid != 0) {
623                 printf("ufs_extattr_enable: malformed attribute header\n");
624                 error = EINVAL;
625                 goto unlock_free_exit;
626         }
627
628         if (attribute->uele_fileheader.uef_magic != UFS_EXTATTR_MAGIC) {
629                 printf("ufs_extattr_enable: invalid attribute header magic\n");
630                 error = EINVAL;
631                 goto unlock_free_exit;
632         }
633
634         if (attribute->uele_fileheader.uef_version != UFS_EXTATTR_VERSION) {
635                 printf("ufs_extattr_enable: incorrect attribute header "
636                     "version\n");
637                 error = EINVAL;
638                 goto unlock_free_exit;
639         }
640
641         ASSERT_VOP_LOCKED(backing_vnode, "ufs_extattr_enable");
642         LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
643             uele_entries);
644
645         VOP_UNLOCK(backing_vnode, 0, td);
646         return (0);
647
648 unlock_free_exit:
649         VOP_UNLOCK(backing_vnode, 0, td);
650
651 free_exit:
652         FREE(attribute, M_UFS_EXTATTR);
653         return (error);
654 }
655
656 /*
657  * Disable extended attribute support on an FS.
658  */
659 static int
660 ufs_extattr_disable(struct ufsmount *ump, int attrnamespace,
661     const char *attrname, struct thread *td)
662 {
663         struct ufs_extattr_list_entry *uele;
664         int error = 0;
665
666         if (!ufs_extattr_valid_attrname(attrnamespace, attrname))
667                 return (EINVAL);
668
669         uele = ufs_extattr_find_attr(ump, attrnamespace, attrname);
670         if (!uele)
671                 return (ENOATTR);
672
673         LIST_REMOVE(uele, uele_entries);
674
675         vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY,
676             td);
677         ASSERT_VOP_LOCKED(uele->uele_backing_vnode, "ufs_extattr_disable");
678         VOP_UNLOCK(uele->uele_backing_vnode, 0, td);
679         error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
680             td->td_ucred, td);
681
682         FREE(uele, M_UFS_EXTATTR);
683
684         return (error);
685 }
686
687 /*
688  * VFS call to manage extended attributes in UFS.  If filename_vp is
689  * non-NULL, it must be passed in locked, and regardless of errors in
690  * processing, will be unlocked.
691  */
692 int
693 ufs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
694     int attrnamespace, const char *attrname, struct thread *td)
695 {
696         struct ufsmount *ump = VFSTOUFS(mp);
697         int error;
698
699         /*
700          * Processes with privilege, but in jail, are not allowed to
701          * configure extended attributes.
702          */
703         error = priv_check(td, PRIV_UFS_EXTATTRCTL);
704         if (error) {
705                 if (filename_vp != NULL)
706                         VOP_UNLOCK(filename_vp, 0, td);
707                 return (error);
708         }
709
710         switch(cmd) {
711         case UFS_EXTATTR_CMD_START:
712                 if (filename_vp != NULL) {
713                         VOP_UNLOCK(filename_vp, 0, td);
714                         return (EINVAL);
715                 }
716                 if (attrname != NULL)
717                         return (EINVAL);
718
719                 error = ufs_extattr_start(mp, td);
720
721                 return (error);
722                 
723         case UFS_EXTATTR_CMD_STOP:
724                 if (filename_vp != NULL) {
725                         VOP_UNLOCK(filename_vp, 0, td);
726                         return (EINVAL);
727                 }
728                 if (attrname != NULL)
729                         return (EINVAL);
730
731                 error = ufs_extattr_stop(mp, td);
732
733                 return (error);
734
735         case UFS_EXTATTR_CMD_ENABLE:
736
737                 if (filename_vp == NULL)
738                         return (EINVAL);
739                 if (attrname == NULL) {
740                         VOP_UNLOCK(filename_vp, 0, td);
741                         return (EINVAL);
742                 }
743
744                 /*
745                  * ufs_extattr_enable_with_open() will always unlock the
746                  * vnode, regardless of failure.
747                  */
748                 ufs_extattr_uepm_lock(ump, td);
749                 error = ufs_extattr_enable_with_open(ump, filename_vp,
750                     attrnamespace, attrname, td);
751                 ufs_extattr_uepm_unlock(ump, td);
752
753                 return (error);
754
755         case UFS_EXTATTR_CMD_DISABLE:
756
757                 if (filename_vp != NULL) {
758                         VOP_UNLOCK(filename_vp, 0, td);
759                         return (EINVAL);
760                 }
761                 if (attrname == NULL)
762                         return (EINVAL);
763
764                 ufs_extattr_uepm_lock(ump, td);
765                 error = ufs_extattr_disable(ump, attrnamespace, attrname,
766                     td);
767                 ufs_extattr_uepm_unlock(ump, td);
768
769                 return (error);
770
771         default:
772                 return (EINVAL);
773         }
774 }
775
776 /*
777  * Vnode operating to retrieve a named extended attribute.
778  */
779 int
780 ufs_getextattr(struct vop_getextattr_args *ap)
781 /*
782 vop_getextattr {
783         IN struct vnode *a_vp;
784         IN int a_attrnamespace;
785         IN const char *a_name;
786         INOUT struct uio *a_uio;
787         OUT size_t *a_size;
788         IN struct ucred *a_cred;
789         IN struct thread *a_td;
790 };
791 */
792 {
793         struct mount *mp = ap->a_vp->v_mount;
794         struct ufsmount *ump = VFSTOUFS(mp);
795         int error;
796
797         ufs_extattr_uepm_lock(ump, ap->a_td);
798
799         error = ufs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
800             ap->a_uio, ap->a_size, ap->a_cred, ap->a_td);
801
802         ufs_extattr_uepm_unlock(ump, ap->a_td);
803
804         return (error);
805 }
806
807 /*
808  * Real work associated with retrieving a named attribute--assumes that
809  * the attribute lock has already been grabbed.
810  */
811 static int
812 ufs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
813     struct uio *uio, size_t *size, struct ucred *cred, struct thread *td)
814 {
815         struct ufs_extattr_list_entry *attribute;
816         struct ufs_extattr_header ueh;
817         struct iovec local_aiov;
818         struct uio local_aio;
819         struct mount *mp = vp->v_mount;
820         struct ufsmount *ump = VFSTOUFS(mp);
821         struct inode *ip = VTOI(vp);
822         off_t base_offset;
823         size_t len, old_len;
824         int error = 0;
825
826         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
827                 return (EOPNOTSUPP);
828
829         if (strlen(name) == 0)
830                 return (EINVAL);
831
832         error = extattr_check_cred(vp, attrnamespace, cred, td, IREAD);
833         if (error)
834                 return (error);
835
836         attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
837         if (!attribute)
838                 return (ENOATTR);
839
840         /*
841          * Allow only offsets of zero to encourage the read/replace
842          * extended attribute semantic.  Otherwise we can't guarantee
843          * atomicity, as we don't provide locks for extended attributes.
844          */
845         if (uio != NULL && uio->uio_offset != 0)
846                 return (ENXIO);
847
848         /*
849          * Find base offset of header in file based on file header size, and
850          * data header size + maximum data size, indexed by inode number.
851          */
852         base_offset = sizeof(struct ufs_extattr_fileheader) +
853             ip->i_number * (sizeof(struct ufs_extattr_header) +
854             attribute->uele_fileheader.uef_size);
855
856         /*
857          * Read in the data header to see if the data is defined, and if so
858          * how much.
859          */
860         bzero(&ueh, sizeof(struct ufs_extattr_header));
861         local_aiov.iov_base = (caddr_t) &ueh;
862         local_aiov.iov_len = sizeof(struct ufs_extattr_header);
863         local_aio.uio_iov = &local_aiov;
864         local_aio.uio_iovcnt = 1;
865         local_aio.uio_rw = UIO_READ;
866         local_aio.uio_segflg = UIO_SYSSPACE;
867         local_aio.uio_td = td;
868         local_aio.uio_offset = base_offset;
869         local_aio.uio_resid = sizeof(struct ufs_extattr_header);
870         
871         /*
872          * Acquire locks.
873          *
874          * Don't need to get a lock on the backing file if the getattr is
875          * being applied to the backing file, as the lock is already held.
876          */
877         if (attribute->uele_backing_vnode != vp)
878                 vn_lock(attribute->uele_backing_vnode, LK_SHARED |
879                     LK_RETRY, td);
880
881         error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
882             IO_NODELOCKED, ump->um_extattr.uepm_ucred);
883         if (error)
884                 goto vopunlock_exit;
885
886         /* Defined? */
887         if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
888                 error = ENOATTR;
889                 goto vopunlock_exit;
890         }
891
892         /* Valid for the current inode generation? */
893         if (ueh.ueh_i_gen != ip->i_gen) {
894                 /*
895                  * The inode itself has a different generation number
896                  * than the attribute data.  For now, the best solution
897                  * is to coerce this to undefined, and let it get cleaned
898                  * up by the next write or extattrctl clean.
899                  */
900                 printf("ufs_extattr_get (%s): inode number inconsistency (%d, %jd)\n",
901                     mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
902                 error = ENOATTR;
903                 goto vopunlock_exit;
904         }
905
906         /* Local size consistency check. */
907         if (ueh.ueh_len > attribute->uele_fileheader.uef_size) {
908                 error = ENXIO;
909                 goto vopunlock_exit;
910         }
911
912         /* Return full data size if caller requested it. */
913         if (size != NULL)
914                 *size = ueh.ueh_len;
915
916         /* Return data if the caller requested it. */
917         if (uio != NULL) {
918                 /* Allow for offset into the attribute data. */
919                 uio->uio_offset = base_offset + sizeof(struct
920                     ufs_extattr_header);
921
922                 /*
923                  * Figure out maximum to transfer -- use buffer size and
924                  * local data limit.
925                  */
926                 len = MIN(uio->uio_resid, ueh.ueh_len);
927                 old_len = uio->uio_resid;
928                 uio->uio_resid = len;
929
930                 error = VOP_READ(attribute->uele_backing_vnode, uio,
931                     IO_NODELOCKED, ump->um_extattr.uepm_ucred);
932                 if (error)
933                         goto vopunlock_exit;
934
935                 uio->uio_resid = old_len - (len - uio->uio_resid);
936         }
937
938 vopunlock_exit:
939
940         if (uio != NULL)
941                 uio->uio_offset = 0;
942
943         if (attribute->uele_backing_vnode != vp)
944                 VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
945
946         return (error);
947 }
948
949 /*
950  * Vnode operation to remove a named attribute.
951  */
952 int
953 ufs_deleteextattr(struct vop_deleteextattr_args *ap)
954 /*
955 vop_deleteextattr {
956         IN struct vnode *a_vp;
957         IN int a_attrnamespace;
958         IN const char *a_name;
959         IN struct ucred *a_cred;
960         IN struct thread *a_td;
961 };
962 */
963 {
964         struct mount *mp = ap->a_vp->v_mount;
965         struct ufsmount *ump = VFSTOUFS(mp); 
966         int error;
967
968         ufs_extattr_uepm_lock(ump, ap->a_td);
969
970         error = ufs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
971             ap->a_cred, ap->a_td);
972
973
974         ufs_extattr_uepm_unlock(ump, ap->a_td);
975
976         return (error);
977 }
978
979 /*
980  * Vnode operation to set a named attribute.
981  */
982 int
983 ufs_setextattr(struct vop_setextattr_args *ap)
984 /*
985 vop_setextattr {
986         IN struct vnode *a_vp;
987         IN int a_attrnamespace;
988         IN const char *a_name;
989         INOUT struct uio *a_uio;
990         IN struct ucred *a_cred;
991         IN struct thread *a_td;
992 };
993 */
994 {
995         struct mount *mp = ap->a_vp->v_mount;
996         struct ufsmount *ump = VFSTOUFS(mp); 
997         int error;
998
999         ufs_extattr_uepm_lock(ump, ap->a_td);
1000
1001         /*
1002          * XXX: No longer a supported way to delete extended attributes.
1003          */
1004         if (ap->a_uio == NULL)
1005                 return (EINVAL);
1006
1007         error = ufs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
1008             ap->a_uio, ap->a_cred, ap->a_td);
1009
1010         ufs_extattr_uepm_unlock(ump, ap->a_td);
1011
1012         return (error);
1013 }
1014
1015 /*
1016  * Real work associated with setting a vnode's extended attributes;
1017  * assumes that the attribute lock has already been grabbed.
1018  */
1019 static int
1020 ufs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
1021     struct uio *uio, struct ucred *cred, struct thread *td)
1022 {
1023         struct ufs_extattr_list_entry *attribute;
1024         struct ufs_extattr_header ueh;
1025         struct iovec local_aiov;
1026         struct uio local_aio;
1027         struct mount *mp = vp->v_mount;
1028         struct ufsmount *ump = VFSTOUFS(mp);
1029         struct inode *ip = VTOI(vp);
1030         off_t base_offset;
1031         int error = 0, ioflag;
1032
1033         if (vp->v_mount->mnt_flag & MNT_RDONLY)
1034                 return (EROFS);
1035         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1036                 return (EOPNOTSUPP);
1037         if (!ufs_extattr_valid_attrname(attrnamespace, name))
1038                 return (EINVAL);
1039
1040         error = extattr_check_cred(vp, attrnamespace, cred, td, IWRITE);
1041         if (error)
1042                 return (error);
1043
1044         attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1045         if (!attribute)
1046                 return (ENOATTR);
1047
1048         /*
1049          * Early rejection of invalid offsets/length.
1050          * Reject: any offset but 0 (replace)
1051          *       Any size greater than attribute size limit
1052          */
1053         if (uio->uio_offset != 0 ||
1054             uio->uio_resid > attribute->uele_fileheader.uef_size)
1055                 return (ENXIO);
1056
1057         /*
1058          * Find base offset of header in file based on file header size, and
1059          * data header size + maximum data size, indexed by inode number.
1060          */
1061         base_offset = sizeof(struct ufs_extattr_fileheader) +
1062             ip->i_number * (sizeof(struct ufs_extattr_header) +
1063             attribute->uele_fileheader.uef_size);
1064
1065         /*
1066          * Write out a data header for the data.
1067          */
1068         ueh.ueh_len = uio->uio_resid;
1069         ueh.ueh_flags = UFS_EXTATTR_ATTR_FLAG_INUSE;
1070         ueh.ueh_i_gen = ip->i_gen;
1071         local_aiov.iov_base = (caddr_t) &ueh;
1072         local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1073         local_aio.uio_iov = &local_aiov;
1074         local_aio.uio_iovcnt = 1;
1075         local_aio.uio_rw = UIO_WRITE;
1076         local_aio.uio_segflg = UIO_SYSSPACE;
1077         local_aio.uio_td = td;
1078         local_aio.uio_offset = base_offset;
1079         local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1080
1081         /*
1082          * Acquire locks.
1083          *
1084          * Don't need to get a lock on the backing file if the setattr is
1085          * being applied to the backing file, as the lock is already held.
1086          */
1087         if (attribute->uele_backing_vnode != vp)
1088                 vn_lock(attribute->uele_backing_vnode, 
1089                     LK_EXCLUSIVE | LK_RETRY, td);
1090
1091         ioflag = IO_NODELOCKED;
1092         if (ufs_extattr_sync)
1093                 ioflag |= IO_SYNC;
1094         error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1095             ump->um_extattr.uepm_ucred);
1096         if (error)
1097                 goto vopunlock_exit;
1098
1099         if (local_aio.uio_resid != 0) {
1100                 error = ENXIO;
1101                 goto vopunlock_exit;
1102         }
1103
1104         /*
1105          * Write out user data.
1106          */
1107         uio->uio_offset = base_offset + sizeof(struct ufs_extattr_header);
1108
1109         ioflag = IO_NODELOCKED;
1110         if (ufs_extattr_sync)
1111                 ioflag |= IO_SYNC;
1112         error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
1113             ump->um_extattr.uepm_ucred);
1114
1115 vopunlock_exit:
1116         uio->uio_offset = 0;
1117
1118         if (attribute->uele_backing_vnode != vp)
1119                 VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
1120
1121         return (error);
1122 }
1123
1124 /*
1125  * Real work associated with removing an extended attribute from a vnode.
1126  * Assumes the attribute lock has already been grabbed.
1127  */
1128 static int
1129 ufs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
1130     struct ucred *cred, struct thread *td)
1131 {
1132         struct ufs_extattr_list_entry *attribute;
1133         struct ufs_extattr_header ueh;
1134         struct iovec local_aiov;
1135         struct uio local_aio;
1136         struct mount *mp = vp->v_mount;
1137         struct ufsmount *ump = VFSTOUFS(mp);
1138         struct inode *ip = VTOI(vp);
1139         off_t base_offset;
1140         int error = 0, ioflag;
1141
1142         if (vp->v_mount->mnt_flag & MNT_RDONLY)  
1143                 return (EROFS);
1144         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED))
1145                 return (EOPNOTSUPP);
1146         if (!ufs_extattr_valid_attrname(attrnamespace, name))
1147                 return (EINVAL);
1148
1149         error = extattr_check_cred(vp, attrnamespace, cred, td, IWRITE);
1150         if (error)
1151                 return (error);
1152
1153         attribute = ufs_extattr_find_attr(ump, attrnamespace, name);
1154         if (!attribute)
1155                 return (ENOATTR);
1156
1157         /*
1158          * Find base offset of header in file based on file header size, and
1159          * data header size + maximum data size, indexed by inode number.
1160          */
1161         base_offset = sizeof(struct ufs_extattr_fileheader) +
1162             ip->i_number * (sizeof(struct ufs_extattr_header) +
1163             attribute->uele_fileheader.uef_size);
1164
1165         /*
1166          * Check to see if currently defined.
1167          */
1168         bzero(&ueh, sizeof(struct ufs_extattr_header));
1169
1170         local_aiov.iov_base = (caddr_t) &ueh;
1171         local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1172         local_aio.uio_iov = &local_aiov;
1173         local_aio.uio_iovcnt = 1;
1174         local_aio.uio_rw = UIO_READ;
1175         local_aio.uio_segflg = UIO_SYSSPACE;
1176         local_aio.uio_td = td;
1177         local_aio.uio_offset = base_offset;
1178         local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1179
1180         /*
1181          * Don't need to get the lock on the backing vnode if the vnode we're
1182          * modifying is it, as we already hold the lock.
1183          */
1184         if (attribute->uele_backing_vnode != vp)
1185                 vn_lock(attribute->uele_backing_vnode,
1186                     LK_EXCLUSIVE | LK_RETRY, td);
1187
1188         error = VOP_READ(attribute->uele_backing_vnode, &local_aio,
1189             IO_NODELOCKED, ump->um_extattr.uepm_ucred);
1190         if (error)
1191                 goto vopunlock_exit;
1192
1193         /* Defined? */
1194         if ((ueh.ueh_flags & UFS_EXTATTR_ATTR_FLAG_INUSE) == 0) {
1195                 error = ENOATTR;
1196                 goto vopunlock_exit;
1197         }
1198
1199         /* Valid for the current inode generation? */
1200         if (ueh.ueh_i_gen != ip->i_gen) {
1201                 /*
1202                  * The inode itself has a different generation number than
1203                  * the attribute data.  For now, the best solution is to
1204                  * coerce this to undefined, and let it get cleaned up by
1205                  * the next write or extattrctl clean.
1206                  */
1207                 printf("ufs_extattr_rm (%s): inode number inconsistency (%d, %jd)\n",
1208                     mp->mnt_stat.f_mntonname, ueh.ueh_i_gen, (intmax_t)ip->i_gen);
1209                 error = ENOATTR;
1210                 goto vopunlock_exit;
1211         }
1212
1213         /* Flag it as not in use. */
1214         ueh.ueh_flags = 0;
1215         ueh.ueh_len = 0;
1216
1217         local_aiov.iov_base = (caddr_t) &ueh;
1218         local_aiov.iov_len = sizeof(struct ufs_extattr_header);
1219         local_aio.uio_iov = &local_aiov;
1220         local_aio.uio_iovcnt = 1;
1221         local_aio.uio_rw = UIO_WRITE;
1222         local_aio.uio_segflg = UIO_SYSSPACE;
1223         local_aio.uio_td = td;
1224         local_aio.uio_offset = base_offset;
1225         local_aio.uio_resid = sizeof(struct ufs_extattr_header);
1226
1227         ioflag = IO_NODELOCKED;
1228         if (ufs_extattr_sync)
1229                 ioflag |= IO_SYNC;
1230         error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
1231             ump->um_extattr.uepm_ucred);
1232         if (error)
1233                 goto vopunlock_exit;
1234
1235         if (local_aio.uio_resid != 0)
1236                 error = ENXIO;
1237
1238 vopunlock_exit:
1239         VOP_UNLOCK(attribute->uele_backing_vnode, 0, td);
1240
1241         return (error);
1242 }
1243
1244 /*
1245  * Called by UFS when an inode is no longer active and should have its
1246  * attributes stripped.
1247  */
1248 void
1249 ufs_extattr_vnode_inactive(struct vnode *vp, struct thread *td)
1250 {
1251         struct ufs_extattr_list_entry *uele;
1252         struct mount *mp = vp->v_mount;
1253         struct ufsmount *ump = VFSTOUFS(mp);
1254
1255         /*
1256          * In that case, we cannot lock. We should not have any active vnodes
1257          * on the fs if this is not yet initialized but is going to be, so
1258          * this can go unlocked.
1259          */
1260         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED))
1261                 return;
1262
1263         ufs_extattr_uepm_lock(ump, td);
1264
1265         if (!(ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED)) {
1266                 ufs_extattr_uepm_unlock(ump, td);
1267                 return;
1268         }
1269
1270         LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
1271                 ufs_extattr_rm(vp, uele->uele_attrnamespace,
1272                     uele->uele_attrname, NULL, td);
1273
1274         ufs_extattr_uepm_unlock(ump, td);
1275 }
1276
1277 #endif /* !UFS_EXTATTR */