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