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