drop net-snmp dep
[unleashed.git] / kernel / fs / xattr.c
blob35567e636131b4d260f49bb24c85b2a15d97ae07
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
27 * Big Theory Statement for Extended Attribute (XATTR) directories
29 * The Solaris VFS layer presents extended file attributes using a special
30 * "XATTR" directory under files or directories that have extended file
31 * attributes. See fsattr(5) for background.
33 * This design avoids the need for a separate set of VFS or vnode functions
34 * for operating on XATTR objects. File system implementations that support
35 * XATTR instantiate a special XATTR directory using this module.
36 * Applications get to the XATTR directory by passing the LOOKUP_XATTR flag
37 * to fop_lookup. Once the XATTR directory is obtained, all other file
38 * system operations on extended attributes happen via the normal vnode
39 * functions, applied to the XATTR directory or its contents.
41 * The XATTR directories returned by fop_lookup (with LOOKUP_XATTR) are
42 * implemented differntly, depending on whether the file system supports
43 * "extended attributes" (XATTR), "system attributes" (SYSATTR), or both.
45 * When SYSATTR=true, XATTR=true:
46 * The XATTR directory is a "generic file system" (GFS) object
47 * that adds the special system attribute names (SUNWattr*) to
48 * the list of XATTR files presented by the underling FS.
49 * In this case, many operations are "passed through" to the
50 * lower-level FS.
52 * When SYSATTR=true, XATTR=false:
53 * The XATTR directory is a "generic file system" (GFS) object,
54 * presenting only the system attribute names (SUNWattr*)
55 * In this case there's no lower-level FS, only the GFS object.
57 * When SYSATTR=false, XATTR=true:
58 * The XATTR directory is implemented by the file system code,
59 * and this module is not involved after xattr_dir_lookup()
60 * returns the XATTR dir from the underlying file system.
62 * When SYSATTR=false, XATTR=false:
63 * xattr_dir_lookup just returns EINVAL
65 * In the first two cases (where we have system attributes) this module
66 * implements what can be thought of as a "translucent" directory containing
67 * both the system attribute names (SUNWattr*) and whatever XATTR names may
68 * exist in the XATTR directory of the underlying file system, if any.
70 * This affects operations on the (GFS) XATTR directory as follows:
72 * readdir: Merges the SUNWattr* names with any contents from the
73 * underlying XATTR directory.
75 * rename: If "to" or "from" is a SUNWattr name, special handling,
76 * else pass through to the lower FS.
78 * link: If "from" is a SUNWattr name, disallow.
80 * create: If a SUNWattr name, disallow, else pass to lower FS.
81 * remove: (same)
83 * open,close: Just pass through to the XATTR dir in the lower FS.
85 * lookup: Lookup an XATTR file in either the (GFS) XATTR directory
86 * or the "real" XATTR directory of the underlying FS.
87 * Note for file systems the support SYSATTR but not XATTR,
88 * only the GFS XATTR directory will exist. When both exist,
89 * gfs_vop_lookup uses the xattr_lookup_cb callback function
90 * which passes the lookup call through to the "real" FS.
92 * Operations on the XATTR _files_ are simpler:
94 * If the file vnode came from lookup at the GFS level, the file is one of
95 * the special SUNWattr* vnodes, and it's vnode operations (xattr_file_tops)
96 * allow only what's appropriate on these "files".
98 * If the file vnode came from the underlying FS, all operations on that
99 * object are handled through the vnode operations set by that FS.
102 #include <sys/param.h>
103 #include <sys/isa_defs.h>
104 #include <sys/types.h>
105 #include <sys/sysmacros.h>
106 #include <sys/cred.h>
107 #include <sys/systm.h>
108 #include <sys/errno.h>
109 #include <sys/fcntl.h>
110 #include <sys/pathname.h>
111 #include <sys/stat.h>
112 #include <sys/vfs.h>
113 #include <sys/acl.h>
114 #include <sys/file.h>
115 #include <sys/sunddi.h>
116 #include <sys/debug.h>
117 #include <sys/cmn_err.h>
118 #include <sys/vnode.h>
119 #include <sys/mode.h>
120 #include <sys/nvpair.h>
121 #include <sys/attr.h>
122 #include <sys/gfs.h>
123 #include <sys/mutex.h>
124 #include <sys/fs_subr.h>
125 #include <sys/kidmap.h>
127 typedef struct {
128 gfs_file_t xattr_gfs_private;
129 xattr_view_t xattr_view;
130 } xattr_file_t;
132 typedef struct {
133 gfs_dir_t xattr_gfs_private;
134 vnode_t *xattr_realvp;
135 } xattr_dir_t;
137 /* ARGSUSED */
138 static int
139 xattr_file_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
141 xattr_file_t *np = (*vpp)->v_data;
143 if ((np->xattr_view == XATTR_VIEW_READONLY) && (flags & FWRITE))
144 return (EACCES);
146 return (0);
149 /* ARGSUSED */
150 static int
151 xattr_file_access(vnode_t *vp, int mode, int flags, cred_t *cr,
152 caller_context_t *ct)
154 xattr_file_t *np = vp->v_data;
156 if ((np->xattr_view == XATTR_VIEW_READONLY) && (mode & VWRITE))
157 return (EACCES);
159 return (0);
162 /* ARGSUSED */
163 static int
164 xattr_file_close(vnode_t *vp, int flags, int count, offset_t off,
165 cred_t *cr, caller_context_t *ct)
167 cleanlocks(vp, ddi_get_pid(), 0);
168 cleanshares(vp, ddi_get_pid());
169 return (0);
172 static int
173 xattr_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
175 xattr_fid_t *xfidp;
176 vnode_t *pvp, *savevp;
177 int error;
178 uint16_t orig_len;
180 if (fidp->fid_len < XATTR_FIDSZ) {
181 fidp->fid_len = XATTR_FIDSZ;
182 return (ENOSPC);
185 savevp = pvp = gfs_file_parent(vp);
186 mutex_enter(&savevp->v_lock);
187 if (pvp->v_flag & V_XATTRDIR) {
188 pvp = gfs_file_parent(pvp);
190 mutex_exit(&savevp->v_lock);
192 xfidp = (xattr_fid_t *)fidp;
193 orig_len = fidp->fid_len;
194 fidp->fid_len = sizeof (xfidp->parent_fid);
196 error = fop_fid(pvp, fidp, ct);
197 if (error) {
198 fidp->fid_len = orig_len;
199 return (error);
202 xfidp->parent_len = fidp->fid_len;
203 fidp->fid_len = XATTR_FIDSZ;
204 xfidp->dir_offset = gfs_file_inode(vp);
206 return (0);
209 /* ARGSUSED */
210 static int
211 xattr_fill_nvlist(vnode_t *vp, xattr_view_t xattr_view, nvlist_t *nvlp,
212 cred_t *cr, caller_context_t *ct)
214 int error;
215 f_attr_t attr;
216 uint64_t fsid;
217 xvattr_t xvattr;
218 xoptattr_t *xoap; /* Pointer to optional attributes */
219 vnode_t *ppvp;
220 const char *domain;
221 uint32_t rid;
223 xva_init(&xvattr);
225 if ((xoap = xva_getxoptattr(&xvattr)) == NULL)
226 return (EINVAL);
229 * For detecting ephemeral uid/gid
231 xvattr.xva_vattr.va_mask |= (VATTR_UID|VATTR_GID);
234 * We need to access the real fs object.
235 * vp points to a GFS file; ppvp points to the real object.
237 ppvp = gfs_file_parent(gfs_file_parent(vp));
240 * Iterate through the attrs associated with this view
243 for (attr = 0; attr < F_ATTR_ALL; attr++) {
244 if (xattr_view != attr_to_xattr_view(attr)) {
245 continue;
248 switch (attr) {
249 case F_SYSTEM:
250 XVA_SET_REQ(&xvattr, XAT_SYSTEM);
251 break;
252 case F_READONLY:
253 XVA_SET_REQ(&xvattr, XAT_READONLY);
254 break;
255 case F_HIDDEN:
256 XVA_SET_REQ(&xvattr, XAT_HIDDEN);
257 break;
258 case F_ARCHIVE:
259 XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
260 break;
261 case F_IMMUTABLE:
262 XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
263 break;
264 case F_APPENDONLY:
265 XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
266 break;
267 case F_NOUNLINK:
268 XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
269 break;
270 case F_OPAQUE:
271 XVA_SET_REQ(&xvattr, XAT_OPAQUE);
272 break;
273 case F_NODUMP:
274 XVA_SET_REQ(&xvattr, XAT_NODUMP);
275 break;
276 case F_AV_QUARANTINED:
277 XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
278 break;
279 case F_AV_MODIFIED:
280 XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
281 break;
282 case F_AV_SCANSTAMP:
283 if (ppvp->v_type == VREG)
284 XVA_SET_REQ(&xvattr, XAT_AV_SCANSTAMP);
285 break;
286 case F_CRTIME:
287 XVA_SET_REQ(&xvattr, XAT_CREATETIME);
288 break;
289 case F_FSID:
290 fsid = (((uint64_t)vp->v_vfsp->vfs_fsid.val[0] << 32) |
291 (uint64_t)(vp->v_vfsp->vfs_fsid.val[1] &
292 0xffffffff));
293 VERIFY(nvlist_add_uint64(nvlp, attr_to_name(attr),
294 fsid) == 0);
295 break;
296 case F_REPARSE:
297 XVA_SET_REQ(&xvattr, XAT_REPARSE);
298 break;
299 case F_GEN:
300 XVA_SET_REQ(&xvattr, XAT_GEN);
301 break;
302 case F_OFFLINE:
303 XVA_SET_REQ(&xvattr, XAT_OFFLINE);
304 break;
305 case F_SPARSE:
306 XVA_SET_REQ(&xvattr, XAT_SPARSE);
307 break;
308 default:
309 break;
313 error = fop_getattr(ppvp, &xvattr.xva_vattr, 0, cr, ct);
314 if (error)
315 return (error);
318 * Process all the optional attributes together here. Notice that
319 * xoap was set when the optional attribute bits were set above.
321 if ((xvattr.xva_vattr.va_mask & VATTR_XVATTR) && xoap) {
322 if (XVA_ISSET_RTN(&xvattr, XAT_READONLY)) {
323 VERIFY(nvlist_add_boolean_value(nvlp,
324 attr_to_name(F_READONLY),
325 xoap->xoa_readonly) == 0);
327 if (XVA_ISSET_RTN(&xvattr, XAT_HIDDEN)) {
328 VERIFY(nvlist_add_boolean_value(nvlp,
329 attr_to_name(F_HIDDEN),
330 xoap->xoa_hidden) == 0);
332 if (XVA_ISSET_RTN(&xvattr, XAT_SYSTEM)) {
333 VERIFY(nvlist_add_boolean_value(nvlp,
334 attr_to_name(F_SYSTEM),
335 xoap->xoa_system) == 0);
337 if (XVA_ISSET_RTN(&xvattr, XAT_ARCHIVE)) {
338 VERIFY(nvlist_add_boolean_value(nvlp,
339 attr_to_name(F_ARCHIVE),
340 xoap->xoa_archive) == 0);
342 if (XVA_ISSET_RTN(&xvattr, XAT_IMMUTABLE)) {
343 VERIFY(nvlist_add_boolean_value(nvlp,
344 attr_to_name(F_IMMUTABLE),
345 xoap->xoa_immutable) == 0);
347 if (XVA_ISSET_RTN(&xvattr, XAT_NOUNLINK)) {
348 VERIFY(nvlist_add_boolean_value(nvlp,
349 attr_to_name(F_NOUNLINK),
350 xoap->xoa_nounlink) == 0);
352 if (XVA_ISSET_RTN(&xvattr, XAT_APPENDONLY)) {
353 VERIFY(nvlist_add_boolean_value(nvlp,
354 attr_to_name(F_APPENDONLY),
355 xoap->xoa_appendonly) == 0);
357 if (XVA_ISSET_RTN(&xvattr, XAT_NODUMP)) {
358 VERIFY(nvlist_add_boolean_value(nvlp,
359 attr_to_name(F_NODUMP),
360 xoap->xoa_nodump) == 0);
362 if (XVA_ISSET_RTN(&xvattr, XAT_OPAQUE)) {
363 VERIFY(nvlist_add_boolean_value(nvlp,
364 attr_to_name(F_OPAQUE),
365 xoap->xoa_opaque) == 0);
367 if (XVA_ISSET_RTN(&xvattr, XAT_AV_QUARANTINED)) {
368 VERIFY(nvlist_add_boolean_value(nvlp,
369 attr_to_name(F_AV_QUARANTINED),
370 xoap->xoa_av_quarantined) == 0);
372 if (XVA_ISSET_RTN(&xvattr, XAT_AV_MODIFIED)) {
373 VERIFY(nvlist_add_boolean_value(nvlp,
374 attr_to_name(F_AV_MODIFIED),
375 xoap->xoa_av_modified) == 0);
377 if (XVA_ISSET_RTN(&xvattr, XAT_AV_SCANSTAMP)) {
378 VERIFY(nvlist_add_uint8_array(nvlp,
379 attr_to_name(F_AV_SCANSTAMP),
380 xoap->xoa_av_scanstamp,
381 sizeof (xoap->xoa_av_scanstamp)) == 0);
383 if (XVA_ISSET_RTN(&xvattr, XAT_CREATETIME)) {
384 VERIFY(nvlist_add_uint64_array(nvlp,
385 attr_to_name(F_CRTIME),
386 (uint64_t *)&(xoap->xoa_createtime),
387 sizeof (xoap->xoa_createtime) /
388 sizeof (uint64_t)) == 0);
390 if (XVA_ISSET_RTN(&xvattr, XAT_REPARSE)) {
391 VERIFY(nvlist_add_boolean_value(nvlp,
392 attr_to_name(F_REPARSE),
393 xoap->xoa_reparse) == 0);
395 if (XVA_ISSET_RTN(&xvattr, XAT_GEN)) {
396 VERIFY(nvlist_add_uint64(nvlp,
397 attr_to_name(F_GEN),
398 xoap->xoa_generation) == 0);
400 if (XVA_ISSET_RTN(&xvattr, XAT_OFFLINE)) {
401 VERIFY(nvlist_add_boolean_value(nvlp,
402 attr_to_name(F_OFFLINE),
403 xoap->xoa_offline) == 0);
405 if (XVA_ISSET_RTN(&xvattr, XAT_SPARSE)) {
406 VERIFY(nvlist_add_boolean_value(nvlp,
407 attr_to_name(F_SPARSE),
408 xoap->xoa_sparse) == 0);
412 * Check for optional ownersid/groupsid
415 if (xvattr.xva_vattr.va_uid > MAXUID) {
416 nvlist_t *nvl_sid;
418 if (nvlist_alloc(&nvl_sid, NV_UNIQUE_NAME, KM_SLEEP))
419 return (ENOMEM);
421 if (kidmap_getsidbyuid(crgetzone(cr), xvattr.xva_vattr.va_uid,
422 &domain, &rid) == 0) {
423 VERIFY(nvlist_add_string(nvl_sid,
424 SID_DOMAIN, domain) == 0);
425 VERIFY(nvlist_add_uint32(nvl_sid, SID_RID, rid) == 0);
426 VERIFY(nvlist_add_nvlist(nvlp, attr_to_name(F_OWNERSID),
427 nvl_sid) == 0);
429 nvlist_free(nvl_sid);
431 if (xvattr.xva_vattr.va_gid > MAXUID) {
432 nvlist_t *nvl_sid;
434 if (nvlist_alloc(&nvl_sid, NV_UNIQUE_NAME, KM_SLEEP))
435 return (ENOMEM);
437 if (kidmap_getsidbygid(crgetzone(cr), xvattr.xva_vattr.va_gid,
438 &domain, &rid) == 0) {
439 VERIFY(nvlist_add_string(nvl_sid,
440 SID_DOMAIN, domain) == 0);
441 VERIFY(nvlist_add_uint32(nvl_sid, SID_RID, rid) == 0);
442 VERIFY(nvlist_add_nvlist(nvlp, attr_to_name(F_GROUPSID),
443 nvl_sid) == 0);
445 nvlist_free(nvl_sid);
448 return (0);
452 * The size of a sysattr file is the size of the nvlist that will be
453 * returned by xattr_file_read(). A call to xattr_file_write() could
454 * change the size of that nvlist. That size is not stored persistently
455 * so xattr_fill_nvlist() calls fop_getattr so that it can be calculated.
457 static int
458 xattr_file_size(vnode_t *vp, xattr_view_t xattr_view, size_t *size,
459 cred_t *cr, caller_context_t *ct)
461 nvlist_t *nvl;
463 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP)) {
464 return (ENOMEM);
467 if (xattr_fill_nvlist(vp, xattr_view, nvl, cr, ct)) {
468 nvlist_free(nvl);
469 return (EFAULT);
472 VERIFY(nvlist_size(nvl, size, NV_ENCODE_XDR) == 0);
473 nvlist_free(nvl);
474 return (0);
477 /* ARGSUSED */
478 static int
479 xattr_file_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
480 caller_context_t *ct)
482 xattr_file_t *np = vp->v_data;
483 timestruc_t now;
484 size_t size;
485 int error;
486 vnode_t *pvp;
487 vattr_t pvattr;
489 vap->va_type = VREG;
490 vap->va_mode = MAKEIMODE(vap->va_type,
491 (np->xattr_view == XATTR_VIEW_READONLY ? 0444 : 0644));
492 vap->va_nodeid = gfs_file_inode(vp);
493 vap->va_nlink = 1;
494 pvp = gfs_file_parent(vp);
495 (void) memset(&pvattr, 0, sizeof (pvattr));
496 pvattr.va_mask = VATTR_CTIME|VATTR_MTIME;
497 error = fop_getattr(pvp, &pvattr, flags, cr, ct);
498 if (error) {
499 return (error);
501 vap->va_ctime = pvattr.va_ctime;
502 vap->va_mtime = pvattr.va_mtime;
503 gethrestime(&now);
504 vap->va_atime = now;
505 vap->va_uid = 0;
506 vap->va_gid = 0;
507 vap->va_rdev = 0;
508 vap->va_blksize = DEV_BSIZE;
509 vap->va_seq = 0;
510 vap->va_fsid = vp->v_vfsp->vfs_dev;
511 error = xattr_file_size(vp, np->xattr_view, &size, cr, ct);
512 vap->va_size = size;
513 vap->va_nblocks = howmany(vap->va_size, vap->va_blksize);
514 return (error);
517 /* ARGSUSED */
518 static int
519 xattr_file_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
520 caller_context_t *ct)
522 xattr_file_t *np = vp->v_data;
523 xattr_view_t xattr_view = np->xattr_view;
524 char *buf;
525 size_t filesize;
526 nvlist_t *nvl;
527 int error;
530 * Validate file offset and fasttrack empty reads
532 if (uiop->uio_loffset < 0)
533 return (EINVAL);
535 if (uiop->uio_resid == 0)
536 return (0);
538 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP))
539 return (ENOMEM);
541 if (xattr_fill_nvlist(vp, xattr_view, nvl, cr, ct)) {
542 nvlist_free(nvl);
543 return (EFAULT);
546 VERIFY(nvlist_size(nvl, &filesize, NV_ENCODE_XDR) == 0);
548 if (uiop->uio_loffset >= filesize) {
549 nvlist_free(nvl);
550 return (0);
553 buf = kmem_alloc(filesize, KM_SLEEP);
554 VERIFY(nvlist_pack(nvl, &buf, &filesize, NV_ENCODE_XDR,
555 KM_SLEEP) == 0);
557 error = uiomove((caddr_t)buf, filesize, UIO_READ, uiop);
558 kmem_free(buf, filesize);
559 nvlist_free(nvl);
560 return (error);
563 /* ARGSUSED */
564 static int
565 xattr_file_write(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
566 caller_context_t *ct)
568 int error = 0;
569 char *buf;
570 char *domain;
571 uint32_t rid;
572 ssize_t size = uiop->uio_resid;
573 nvlist_t *nvp;
574 nvpair_t *pair = NULL;
575 vnode_t *ppvp;
576 xvattr_t xvattr;
577 xoptattr_t *xoap = NULL; /* Pointer to optional attributes */
579 if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0)
580 return (EINVAL);
583 * Validate file offset and size.
585 if (uiop->uio_loffset < 0)
586 return (EINVAL);
588 if (size == 0)
589 return (EINVAL);
591 xva_init(&xvattr);
593 if ((xoap = xva_getxoptattr(&xvattr)) == NULL) {
594 return (EINVAL);
598 * Copy and unpack the nvlist
600 buf = kmem_alloc(size, KM_SLEEP);
601 if (uiomove((caddr_t)buf, size, UIO_WRITE, uiop)) {
602 return (EFAULT);
605 if (nvlist_unpack(buf, size, &nvp, KM_SLEEP) != 0) {
606 kmem_free(buf, size);
607 uiop->uio_resid = size;
608 return (EINVAL);
610 kmem_free(buf, size);
613 * Fasttrack empty writes (nvlist with no nvpairs)
615 if (nvlist_next_nvpair(nvp, NULL) == 0)
616 return (0);
618 ppvp = gfs_file_parent(gfs_file_parent(vp));
620 while (pair = nvlist_next_nvpair(nvp, pair)) {
621 data_type_t type;
622 f_attr_t attr;
623 boolean_t value;
624 uint64_t *time, *times;
625 uint_t elem, nelems;
626 nvlist_t *nvp_sid;
627 uint8_t *scanstamp;
630 * Validate the name and type of each attribute.
631 * Log any unknown names and continue. This will
632 * help if additional attributes are added later.
634 type = nvpair_type(pair);
635 if ((attr = name_to_attr(nvpair_name(pair))) == F_ATTR_INVAL) {
636 cmn_err(CE_WARN, "Unknown attribute %s",
637 nvpair_name(pair));
638 continue;
642 * Verify nvlist type matches required type and view is OK
645 if (type != attr_to_data_type(attr) ||
646 (attr_to_xattr_view(attr) == XATTR_VIEW_READONLY)) {
647 nvlist_free(nvp);
648 return (EINVAL);
652 * For OWNERSID/GROUPSID make sure the target
653 * file system support ephemeral ID's
655 if ((attr == F_OWNERSID || attr == F_GROUPSID) &&
656 (!(vp->v_vfsp->vfs_flag & VFS_XID))) {
657 nvlist_free(nvp);
658 return (EINVAL);
662 * Retrieve data from nvpair
664 switch (type) {
665 case DATA_TYPE_BOOLEAN_VALUE:
666 if (nvpair_value_boolean_value(pair, &value)) {
667 nvlist_free(nvp);
668 return (EINVAL);
670 break;
671 case DATA_TYPE_UINT64_ARRAY:
672 if (nvpair_value_uint64_array(pair, &times, &nelems)) {
673 nvlist_free(nvp);
674 return (EINVAL);
676 break;
677 case DATA_TYPE_NVLIST:
678 if (nvpair_value_nvlist(pair, &nvp_sid)) {
679 nvlist_free(nvp);
680 return (EINVAL);
682 break;
683 case DATA_TYPE_UINT8_ARRAY:
684 if (nvpair_value_uint8_array(pair,
685 &scanstamp, &nelems)) {
686 nvlist_free(nvp);
687 return (EINVAL);
689 break;
690 default:
691 nvlist_free(nvp);
692 return (EINVAL);
695 switch (attr) {
697 * If we have several similar optional attributes to
698 * process then we should do it all together here so that
699 * xoap and the requested bitmap can be set in one place.
701 case F_READONLY:
702 XVA_SET_REQ(&xvattr, XAT_READONLY);
703 xoap->xoa_readonly = value;
704 break;
705 case F_HIDDEN:
706 XVA_SET_REQ(&xvattr, XAT_HIDDEN);
707 xoap->xoa_hidden = value;
708 break;
709 case F_SYSTEM:
710 XVA_SET_REQ(&xvattr, XAT_SYSTEM);
711 xoap->xoa_system = value;
712 break;
713 case F_ARCHIVE:
714 XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
715 xoap->xoa_archive = value;
716 break;
717 case F_IMMUTABLE:
718 XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
719 xoap->xoa_immutable = value;
720 break;
721 case F_NOUNLINK:
722 XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
723 xoap->xoa_nounlink = value;
724 break;
725 case F_APPENDONLY:
726 XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
727 xoap->xoa_appendonly = value;
728 break;
729 case F_NODUMP:
730 XVA_SET_REQ(&xvattr, XAT_NODUMP);
731 xoap->xoa_nodump = value;
732 break;
733 case F_AV_QUARANTINED:
734 XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
735 xoap->xoa_av_quarantined = value;
736 break;
737 case F_AV_MODIFIED:
738 XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
739 xoap->xoa_av_modified = value;
740 break;
741 case F_CRTIME:
742 XVA_SET_REQ(&xvattr, XAT_CREATETIME);
743 time = (uint64_t *)&(xoap->xoa_createtime);
744 for (elem = 0; elem < nelems; elem++)
745 *time++ = times[elem];
746 break;
747 case F_OWNERSID:
748 case F_GROUPSID:
749 if (nvlist_lookup_string(nvp_sid, SID_DOMAIN,
750 &domain) || nvlist_lookup_uint32(nvp_sid, SID_RID,
751 &rid)) {
752 nvlist_free(nvp);
753 return (EINVAL);
757 * Now map domain+rid to ephemeral id's
759 * If mapping fails, then the uid/gid will
760 * be set to UID_NOBODY by Winchester.
763 if (attr == F_OWNERSID) {
764 (void) kidmap_getuidbysid(crgetzone(cr), domain,
765 rid, &xvattr.xva_vattr.va_uid);
766 xvattr.xva_vattr.va_mask |= VATTR_UID;
767 } else {
768 (void) kidmap_getgidbysid(crgetzone(cr), domain,
769 rid, &xvattr.xva_vattr.va_gid);
770 xvattr.xva_vattr.va_mask |= VATTR_GID;
772 break;
773 case F_AV_SCANSTAMP:
774 if (ppvp->v_type == VREG) {
775 XVA_SET_REQ(&xvattr, XAT_AV_SCANSTAMP);
776 (void) memcpy(xoap->xoa_av_scanstamp,
777 scanstamp, nelems);
778 } else {
779 nvlist_free(nvp);
780 return (EINVAL);
782 break;
783 case F_REPARSE:
784 XVA_SET_REQ(&xvattr, XAT_REPARSE);
785 xoap->xoa_reparse = value;
786 break;
787 case F_OFFLINE:
788 XVA_SET_REQ(&xvattr, XAT_OFFLINE);
789 xoap->xoa_offline = value;
790 break;
791 case F_SPARSE:
792 XVA_SET_REQ(&xvattr, XAT_SPARSE);
793 xoap->xoa_sparse = value;
794 break;
795 default:
796 break;
800 ppvp = gfs_file_parent(gfs_file_parent(vp));
801 error = fop_setattr(ppvp, &xvattr.xva_vattr, 0, cr, ct);
802 if (error)
803 uiop->uio_resid = size;
805 nvlist_free(nvp);
806 return (error);
809 static int
810 xattr_file_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
811 caller_context_t *ct)
813 switch (cmd) {
814 case _PC_XATTR_EXISTS:
815 case _PC_SATTR_ENABLED:
816 case _PC_SATTR_EXISTS:
817 *valp = 0;
818 return (0);
819 default:
820 return (fs_pathconf(vp, cmd, valp, cr, ct));
824 static const struct vnodeops xattr_file_ops = {
825 .vnop_name = "system attributes",
826 .vop_open = xattr_file_open,
827 .vop_close = xattr_file_close,
828 .vop_read = xattr_file_read,
829 .vop_write = xattr_file_write,
830 .vop_ioctl = fs_ioctl,
831 .vop_getattr = xattr_file_getattr,
832 .vop_access = xattr_file_access,
833 .vop_readdir = fs_notdir,
834 .vop_seek = fs_seek,
835 .vop_inactive = gfs_vop_inactive,
836 .vop_fid = xattr_common_fid,
837 .vop_pathconf = xattr_file_pathconf,
838 .vop_putpage = fs_putpage,
839 .vop_fsync = fs_fsync,
842 vnode_t *
843 xattr_mkfile(vnode_t *pvp, xattr_view_t xattr_view)
845 vnode_t *vp;
846 xattr_file_t *np;
848 vp = gfs_file_create(sizeof (xattr_file_t), pvp, &xattr_file_ops);
849 np = vp->v_data;
850 np->xattr_view = xattr_view;
851 vp->v_flag |= V_SYSATTR;
852 return (vp);
855 vnode_t *
856 xattr_mkfile_ro(vnode_t *pvp)
858 return (xattr_mkfile(pvp, XATTR_VIEW_READONLY));
861 vnode_t *
862 xattr_mkfile_rw(vnode_t *pvp)
864 return (xattr_mkfile(pvp, XATTR_VIEW_READWRITE));
867 static gfs_dirent_t xattr_dirents[] = {
868 { VIEW_READONLY, xattr_mkfile_ro, GFS_CACHE_VNODE, },
869 { VIEW_READWRITE, xattr_mkfile_rw, GFS_CACHE_VNODE, },
870 { NULL },
873 #define XATTRDIR_NENTS ((sizeof (xattr_dirents) / sizeof (gfs_dirent_t)) - 1)
875 static int
876 is_sattr_name(char *s)
878 int i;
880 for (i = 0; i < XATTRDIR_NENTS; ++i) {
881 if (strcmp(s, xattr_dirents[i].gfse_name) == 0) {
882 return (1);
885 return (0);
889 * Given the name of an extended attribute file, determine if there is a
890 * normalization conflict with a sysattr view name.
893 xattr_sysattr_casechk(char *s)
895 int i;
897 for (i = 0; i < XATTRDIR_NENTS; ++i) {
898 if (strcasecmp(s, xattr_dirents[i].gfse_name) == 0)
899 return (1);
901 return (0);
904 static int
905 xattr_copy(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
906 cred_t *cr, caller_context_t *ct)
908 xvattr_t xvattr;
909 vnode_t *pdvp;
910 int error;
913 * Only copy system attrs if the views are the same
915 if (strcmp(snm, tnm) != 0)
916 return (EINVAL);
918 xva_init(&xvattr);
920 XVA_SET_REQ(&xvattr, XAT_SYSTEM);
921 XVA_SET_REQ(&xvattr, XAT_READONLY);
922 XVA_SET_REQ(&xvattr, XAT_HIDDEN);
923 XVA_SET_REQ(&xvattr, XAT_ARCHIVE);
924 XVA_SET_REQ(&xvattr, XAT_APPENDONLY);
925 XVA_SET_REQ(&xvattr, XAT_NOUNLINK);
926 XVA_SET_REQ(&xvattr, XAT_IMMUTABLE);
927 XVA_SET_REQ(&xvattr, XAT_NODUMP);
928 XVA_SET_REQ(&xvattr, XAT_AV_MODIFIED);
929 XVA_SET_REQ(&xvattr, XAT_AV_QUARANTINED);
930 XVA_SET_REQ(&xvattr, XAT_CREATETIME);
931 XVA_SET_REQ(&xvattr, XAT_REPARSE);
932 XVA_SET_REQ(&xvattr, XAT_OFFLINE);
933 XVA_SET_REQ(&xvattr, XAT_SPARSE);
935 pdvp = gfs_file_parent(sdvp);
936 error = fop_getattr(pdvp, &xvattr.xva_vattr, 0, cr, ct);
937 if (error)
938 return (error);
940 pdvp = gfs_file_parent(tdvp);
941 error = fop_setattr(pdvp, &xvattr.xva_vattr, 0, cr, ct);
942 return (error);
946 * Get the "real" XATTR directory associtated with the GFS XATTR directory.
947 * Note: This does NOT take any additional hold on the returned real_vp,
948 * because when this lookup succeeds we save the result in xattr_realvp
949 * and keep that hold until the GFS XATTR directory goes inactive.
951 static int
952 xattr_dir_realdir(vnode_t *gfs_dvp, vnode_t **ret_vpp, int flags,
953 cred_t *cr, caller_context_t *ct)
955 struct pathname pn;
956 char *nm = "";
957 xattr_dir_t *xattr_dir;
958 vnode_t *realvp;
959 int error;
961 *ret_vpp = NULL;
964 * Usually, we've already found the underlying XATTR directory
965 * during some previous lookup and stored it in xattr_realvp.
967 mutex_enter(&gfs_dvp->v_lock);
968 xattr_dir = gfs_dvp->v_data;
969 realvp = xattr_dir->xattr_realvp;
970 mutex_exit(&gfs_dvp->v_lock);
971 if (realvp != NULL) {
972 *ret_vpp = realvp;
973 return (0);
977 * Lookup the XATTR dir in the underlying FS, relative to our
978 * "parent", which is the real object for which this GFS XATTR
979 * directory was created. Set the LOOKUP_HAVE_SYSATTR_DIR flag
980 * so that we don't get into an infinite loop with fop_lookup
981 * calling back to xattr_dir_lookup.
983 error = pn_get(nm, UIO_SYSSPACE, &pn);
984 if (error != 0)
985 return (error);
986 error = fop_lookup(gfs_file_parent(gfs_dvp), nm, &realvp, &pn,
987 flags | LOOKUP_HAVE_SYSATTR_DIR, rootvp, cr, ct, NULL, NULL);
988 pn_free(&pn);
989 if (error != 0)
990 return (error);
993 * Have the real XATTR directory. Save it -- but first
994 * check whether we lost a race doing the lookup.
996 mutex_enter(&gfs_dvp->v_lock);
997 xattr_dir = gfs_dvp->v_data;
998 if (xattr_dir->xattr_realvp == NULL) {
1000 * Note that the hold taken by the fop_lookup above is
1001 * retained from here until xattr_dir_inactive.
1003 xattr_dir->xattr_realvp = realvp;
1004 } else {
1005 /* We lost the race. */
1006 VN_RELE(realvp);
1007 realvp = xattr_dir->xattr_realvp;
1009 mutex_exit(&gfs_dvp->v_lock);
1011 *ret_vpp = realvp;
1012 return (0);
1015 /* ARGSUSED */
1016 static int
1017 xattr_dir_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
1019 vnode_t *realvp;
1020 int error;
1022 if (flags & FWRITE) {
1023 return (EACCES);
1027 * If there is a real extended attribute directory,
1028 * let the underlying FS see the fop_open call;
1029 * otherwise just return zero.
1031 error = xattr_dir_realdir(*vpp, &realvp, LOOKUP_XATTR, cr, ct);
1032 if (error == 0) {
1033 error = fop_open(&realvp, flags, cr, ct);
1034 } else {
1035 error = 0;
1038 return (error);
1041 /* ARGSUSED */
1042 static int
1043 xattr_dir_close(vnode_t *vp, int flags, int count, offset_t off, cred_t *cr,
1044 caller_context_t *ct)
1046 vnode_t *realvp;
1047 int error;
1050 * If there is a real extended attribute directory,
1051 * let the underlying FS see the fop_close call;
1052 * otherwise just return zero.
1054 error = xattr_dir_realdir(vp, &realvp, LOOKUP_XATTR, cr, ct);
1055 if (error == 0) {
1056 error = fop_close(realvp, flags, count, off, cr, ct);
1057 } else {
1058 error = 0;
1061 return (error);
1065 * Retrieve the attributes on an xattr directory. If there is a "real"
1066 * xattr directory, use that. Otherwise, get the attributes (represented
1067 * by PARENT_ATTRMASK) from the "parent" node and fill in the rest. Note
1068 * that fop_getattr() could turn off bits in the va_mask.
1071 #define PARENT_ATTRMASK (VATTR_UID|VATTR_GID|VATTR_RDEV|VATTR_CTIME|VATTR_MTIME)
1073 /* ARGSUSED */
1074 static int
1075 xattr_dir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1076 caller_context_t *ct)
1078 timestruc_t now;
1079 vnode_t *pvp;
1080 int error;
1082 error = xattr_dir_realdir(vp, &pvp, LOOKUP_XATTR, cr, ct);
1083 if (error == 0) {
1084 error = fop_getattr(pvp, vap, 0, cr, ct);
1085 if (error) {
1086 return (error);
1088 vap->va_nlink += XATTRDIR_NENTS;
1089 vap->va_size += XATTRDIR_NENTS;
1090 return (0);
1094 * There is no real xattr directory. Cobble together
1095 * an entry using info from the parent object (if needed)
1096 * plus information common to all xattrs.
1098 if (vap->va_mask & PARENT_ATTRMASK) {
1099 vattr_t pvattr;
1100 uint_t off_bits;
1102 pvp = gfs_file_parent(vp);
1103 (void) memset(&pvattr, 0, sizeof (pvattr));
1104 pvattr.va_mask = PARENT_ATTRMASK;
1105 error = fop_getattr(pvp, &pvattr, 0, cr, ct);
1106 if (error) {
1107 return (error);
1111 * fop_getattr() might have turned off some bits in
1112 * pvattr.va_mask. This means that the underlying
1113 * file system couldn't process those attributes.
1114 * We need to make sure those bits get turned off
1115 * in the vattr_t structure that gets passed back
1116 * to the caller. Figure out which bits were turned
1117 * off (if any) then set pvattr.va_mask before it
1118 * gets copied to the vattr_t that the caller sees.
1120 off_bits = (pvattr.va_mask ^ PARENT_ATTRMASK) & PARENT_ATTRMASK;
1121 pvattr.va_mask = vap->va_mask & ~off_bits;
1122 *vap = pvattr;
1125 vap->va_type = VDIR;
1126 vap->va_mode = MAKEIMODE(vap->va_type, S_ISVTX | 0777);
1127 vap->va_fsid = vp->v_vfsp->vfs_dev;
1128 vap->va_nodeid = gfs_file_inode(vp);
1129 vap->va_nlink = XATTRDIR_NENTS+2;
1130 vap->va_size = vap->va_nlink;
1131 gethrestime(&now);
1132 vap->va_atime = now;
1133 vap->va_blksize = 0;
1134 vap->va_nblocks = 0;
1135 vap->va_seq = 0;
1136 return (0);
1139 static int
1140 xattr_dir_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1141 caller_context_t *ct)
1143 vnode_t *realvp;
1144 int error;
1147 * If there is a real xattr directory, do the setattr there.
1148 * Otherwise, just return success. The GFS directory is transient,
1149 * and any setattr changes can disappear anyway.
1151 error = xattr_dir_realdir(vp, &realvp, LOOKUP_XATTR, cr, ct);
1152 if (error == 0) {
1153 error = fop_setattr(realvp, vap, flags, cr, ct);
1155 if (error == ENOENT) {
1156 error = 0;
1158 return (error);
1161 /* ARGSUSED */
1162 static int
1163 xattr_dir_access(vnode_t *vp, int mode, int flags, cred_t *cr,
1164 caller_context_t *ct)
1166 int error;
1167 vnode_t *realvp = NULL;
1169 if (mode & VWRITE) {
1170 return (EACCES);
1173 error = xattr_dir_realdir(vp, &realvp, LOOKUP_XATTR, cr, ct);
1174 if ((error == ENOENT) || (error == EINVAL))
1176 * These errors mean there's no "real" xattr dir.
1177 * The GFS xattr dir always allows access.
1179 return (0);
1181 if (error != 0)
1183 * The "real" xattr dir was not accessible.
1185 return (error);
1188 * We got the "real" xattr dir.
1189 * Pass through the access call.
1191 error = fop_access(realvp, mode, flags, cr, ct);
1193 return (error);
1196 static int
1197 xattr_dir_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1198 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1199 vsecattr_t *vsecp)
1201 vnode_t *pvp;
1202 int error;
1204 *vpp = NULL;
1207 * Don't allow creation of extended attributes with sysattr names.
1209 if (is_sattr_name(name)) {
1210 return (gfs_dir_lookup(dvp, name, vpp, cr, 0, NULL, NULL));
1213 error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR|CREATE_XATTR_DIR,
1214 cr, ct);
1215 if (error == 0) {
1216 error = fop_create(pvp, name, vap, excl, mode, vpp, cr, flag,
1217 ct, vsecp);
1219 return (error);
1222 static int
1223 xattr_dir_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1224 int flags)
1226 vnode_t *pvp;
1227 int error;
1229 if (is_sattr_name(name)) {
1230 return (EACCES);
1233 error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR, cr, ct);
1234 if (error == 0) {
1235 error = fop_remove(pvp, name, cr, ct, flags);
1237 return (error);
1240 static int
1241 xattr_dir_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
1242 caller_context_t *ct, int flags)
1244 vnode_t *pvp;
1245 int error;
1247 if (svp->v_flag & V_SYSATTR) {
1248 return (EINVAL);
1251 error = xattr_dir_realdir(tdvp, &pvp, LOOKUP_XATTR, cr, ct);
1252 if (error == 0) {
1253 error = fop_link(pvp, svp, name, cr, ct, flags);
1255 return (error);
1258 static int
1259 xattr_dir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
1260 cred_t *cr, caller_context_t *ct, int flags)
1262 vnode_t *spvp, *tpvp;
1263 int error;
1265 if (is_sattr_name(snm) || is_sattr_name(tnm))
1266 return (xattr_copy(sdvp, snm, tdvp, tnm, cr, ct));
1268 * We know that sdvp is a GFS dir, or we wouldn't be here.
1269 * Get the real unnamed directory.
1271 error = xattr_dir_realdir(sdvp, &spvp, LOOKUP_XATTR, cr, ct);
1272 if (error) {
1273 return (error);
1276 if (sdvp == tdvp) {
1278 * If the source and target are the same GFS directory, the
1279 * underlying unnamed source and target dir will be the same.
1281 tpvp = spvp;
1282 } else if (tdvp->v_flag & V_SYSATTR) {
1284 * If the target dir is a different GFS directory,
1285 * find its underlying unnamed dir.
1287 error = xattr_dir_realdir(tdvp, &tpvp, LOOKUP_XATTR, cr, ct);
1288 if (error) {
1289 return (error);
1291 } else {
1293 * Target dir is outside of GFS, pass it on through.
1295 tpvp = tdvp;
1298 error = fop_rename(spvp, snm, tpvp, tnm, cr, ct, flags);
1300 return (error);
1304 * readdir_xattr_casecmp: given a system attribute name, see if there
1305 * is a real xattr with the same normalized name.
1307 static int
1308 readdir_xattr_casecmp(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct,
1309 int *eflags)
1311 int error;
1312 vnode_t *vp;
1313 struct pathname pn;
1315 *eflags = 0;
1317 error = pn_get(nm, UIO_SYSSPACE, &pn);
1318 if (error == 0) {
1319 error = fop_lookup(dvp, nm, &vp, &pn,
1320 FIGNORECASE, rootvp, cr, ct, NULL, NULL);
1321 if (error == 0) {
1322 *eflags = ED_CASE_CONFLICT;
1323 VN_RELE(vp);
1324 } else if (error == ENOENT) {
1325 error = 0;
1327 pn_free(&pn);
1330 return (error);
1333 static int
1334 xattr_dir_readdir(vnode_t *dvp, uio_t *uiop, cred_t *cr, int *eofp,
1335 caller_context_t *ct, int flags)
1337 vnode_t *pvp;
1338 int error;
1339 int local_eof;
1340 int reset_off = 0;
1341 int has_xattrs = 0;
1343 if (eofp == NULL) {
1344 eofp = &local_eof;
1346 *eofp = 0;
1349 * See if there is a real extended attribute directory.
1351 error = xattr_dir_realdir(dvp, &pvp, LOOKUP_XATTR, cr, ct);
1352 if (error == 0) {
1353 has_xattrs = 1;
1357 * Start by reading up the static entries.
1359 if (uiop->uio_loffset == 0) {
1360 ino64_t pino, ino;
1361 offset_t off;
1362 gfs_dir_t *dp = dvp->v_data;
1363 gfs_readdir_state_t gstate;
1365 if (has_xattrs) {
1367 * If there is a real xattr dir, skip . and ..
1368 * in the GFS dir. We'll pick them up below
1369 * when we call into the underlying fs.
1371 uiop->uio_loffset = GFS_STATIC_ENTRY_OFFSET;
1373 error = gfs_get_parent_ino(dvp, cr, ct, &pino, &ino);
1374 if (error == 0) {
1375 error = gfs_readdir_init(&gstate, dp->gfsd_maxlen, 1,
1376 uiop, pino, ino, flags);
1378 if (error) {
1379 return (error);
1382 while ((error = gfs_readdir_pred(&gstate, uiop, &off)) == 0 &&
1383 !*eofp) {
1384 if (off >= 0 && off < dp->gfsd_nstatic) {
1385 int eflags;
1388 * Check to see if this sysattr set name has a
1389 * case-insensitive conflict with a real xattr
1390 * name.
1392 eflags = 0;
1393 if ((flags & V_RDDIR_ENTFLAGS) && has_xattrs) {
1394 error = readdir_xattr_casecmp(pvp,
1395 dp->gfsd_static[off].gfse_name,
1396 cr, ct, &eflags);
1397 if (error)
1398 break;
1400 ino = dp->gfsd_inode(dvp, off);
1402 error = gfs_readdir_emit(&gstate, uiop, off,
1403 ino, dp->gfsd_static[off].gfse_name,
1404 eflags);
1405 if (error)
1406 break;
1407 } else {
1408 *eofp = 1;
1412 error = gfs_readdir_fini(&gstate, error, eofp, *eofp);
1413 if (error) {
1414 return (error);
1418 * We must read all of the static entries in the first
1419 * call. Otherwise we won't know if uio_loffset in a
1420 * subsequent call refers to the static entries or to those
1421 * in an underlying fs.
1423 if (*eofp == 0)
1424 return (EINVAL);
1425 reset_off = 1;
1428 if (!has_xattrs) {
1429 *eofp = 1;
1430 return (0);
1433 *eofp = 0;
1434 if (reset_off) {
1435 uiop->uio_loffset = 0;
1437 (void) fop_rwlock(pvp, V_WRITELOCK_FALSE, NULL);
1438 error = fop_readdir(pvp, uiop, cr, eofp, ct, flags);
1439 fop_rwunlock(pvp, V_WRITELOCK_FALSE, NULL);
1441 return (error);
1445 * Last reference on a (GFS) XATTR directory.
1447 * If there's a real XATTR directory in the underlying FS, we will have
1448 * taken a hold on that directory in xattr_dir_realdir. Now that the
1449 * last hold on the GFS directory is gone, it's time to release that
1450 * hold on the underlying XATTR directory.
1452 /* ARGSUSED */
1453 static void
1454 xattr_dir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1456 xattr_dir_t *dp;
1458 dp = gfs_dir_inactive(vp); /* will track v_count */
1459 if (dp != NULL) {
1460 /* vp was freed */
1461 if (dp->xattr_realvp != NULL)
1462 VN_RELE(dp->xattr_realvp);
1464 kmem_free(dp, ((gfs_file_t *)dp)->gfs_size);
1468 static int
1469 xattr_dir_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
1470 caller_context_t *ct)
1472 switch (cmd) {
1473 case _PC_XATTR_EXISTS:
1474 case _PC_SATTR_ENABLED:
1475 case _PC_SATTR_EXISTS:
1476 *valp = 0;
1477 return (0);
1478 default:
1479 return (fs_pathconf(vp, cmd, valp, cr, ct));
1483 /* ARGSUSED */
1484 static int
1485 xattr_dir_realvp(vnode_t *vp, vnode_t **realvp, caller_context_t *ct)
1487 int error;
1489 error = xattr_dir_realdir(vp, realvp, LOOKUP_XATTR, kcred, NULL);
1490 return (error);
1494 static const struct vnodeops xattr_dir_ops = {
1495 .vnop_name = "xattr dir",
1496 .vop_open = xattr_dir_open,
1497 .vop_close = xattr_dir_close,
1498 .vop_ioctl = fs_inval,
1499 .vop_getattr = xattr_dir_getattr,
1500 .vop_setattr = xattr_dir_setattr,
1501 .vop_access = xattr_dir_access,
1502 .vop_readdir = xattr_dir_readdir,
1503 .vop_lookup = gfs_vop_lookup,
1504 .vop_create = xattr_dir_create,
1505 .vop_remove = xattr_dir_remove,
1506 .vop_link = xattr_dir_link,
1507 .vop_rename = xattr_dir_rename,
1508 .vop_mkdir = fs_inval,
1509 .vop_seek = fs_seek,
1510 .vop_inactive = xattr_dir_inactive,
1511 .vop_fid = xattr_common_fid,
1512 .vop_pathconf = xattr_dir_pathconf,
1513 .vop_realvp = xattr_dir_realvp,
1517 * Callback supporting lookup in a GFS XATTR directory.
1519 static int
1520 xattr_lookup_cb(vnode_t *vp, const char *nm, vnode_t **vpp, ino64_t *inop,
1521 cred_t *cr, int flags, int *deflags, pathname_t *rpnp)
1523 vnode_t *pvp;
1524 struct pathname pn;
1525 int error;
1527 *vpp = NULL;
1528 *inop = 0;
1530 error = xattr_dir_realdir(vp, &pvp, LOOKUP_XATTR, cr, NULL);
1533 * Return ENOENT for EACCES requests during lookup. Once an
1534 * attribute create is attempted EACCES will be returned.
1536 if (error) {
1537 if (error == EACCES)
1538 return (ENOENT);
1539 return (error);
1542 error = pn_get((char *)nm, UIO_SYSSPACE, &pn);
1543 if (error == 0) {
1544 error = fop_lookup(pvp, (char *)nm, vpp, &pn, flags, rootvp,
1545 cr, NULL, deflags, rpnp);
1546 pn_free(&pn);
1549 return (error);
1552 /* ARGSUSED */
1553 static ino64_t
1554 xattrdir_do_ino(vnode_t *vp, int index)
1557 * We use index 0 for the directory fid. Start
1558 * the file numbering at 1.
1560 return ((ino64_t)index+1);
1563 void
1564 xattr_init(void)
1569 * Get the XATTR dir for some file or directory.
1570 * See vnode.c: fop_lookup()
1572 * Note this only gets the GFS XATTR directory. We'll get the
1573 * real XATTR directory later, in xattr_dir_realdir.
1576 xattr_dir_lookup(vnode_t *dvp, vnode_t **vpp, int flags, cred_t *cr)
1578 int error = 0;
1580 *vpp = NULL;
1582 if (dvp->v_type != VDIR && dvp->v_type != VREG)
1583 return (EINVAL);
1585 mutex_enter(&dvp->v_lock);
1588 * If we're already in sysattr space, don't allow creation
1589 * of another level of sysattrs.
1591 if (dvp->v_flag & V_SYSATTR) {
1592 mutex_exit(&dvp->v_lock);
1593 return (EINVAL);
1596 if (dvp->v_xattrdir != NULL) {
1597 *vpp = dvp->v_xattrdir;
1598 VN_HOLD(*vpp);
1599 } else {
1600 ulong_t val;
1601 int xattrs_allowed = dvp->v_vfsp->vfs_flag & VFS_XATTR;
1602 int sysattrs_allowed = 1;
1605 * We have to drop the lock on dvp. gfs_dir_create will
1606 * grab it for a VN_HOLD.
1608 mutex_exit(&dvp->v_lock);
1611 * If dvp allows xattr creation, but not sysattr
1612 * creation, return the real xattr dir vp. We can't
1613 * use the vfs feature mask here because _PC_SATTR_ENABLED
1614 * has vnode-level granularity (e.g. .zfs).
1616 error = fop_pathconf(dvp, _PC_SATTR_ENABLED, &val, cr, NULL);
1617 if (error != 0 || val == 0)
1618 sysattrs_allowed = 0;
1620 if (!xattrs_allowed && !sysattrs_allowed)
1621 return (EINVAL);
1623 if (!sysattrs_allowed) {
1624 struct pathname pn;
1625 char *nm = "";
1627 error = pn_get(nm, UIO_SYSSPACE, &pn);
1628 if (error)
1629 return (error);
1630 error = fop_lookup(dvp, nm, vpp, &pn,
1631 flags|LOOKUP_HAVE_SYSATTR_DIR, rootvp, cr, NULL,
1632 NULL, NULL);
1633 pn_free(&pn);
1634 return (error);
1638 * Note that we act as if we were given CREATE_XATTR_DIR,
1639 * but only for creation of the GFS directory.
1641 *vpp = gfs_dir_create(
1642 sizeof (xattr_dir_t), dvp, &xattr_dir_ops, xattr_dirents,
1643 xattrdir_do_ino, MAXNAMELEN, NULL, xattr_lookup_cb);
1644 mutex_enter(&dvp->v_lock);
1645 if (dvp->v_xattrdir != NULL) {
1647 * We lost the race to create the xattr dir.
1648 * Destroy this one, use the winner. We can't
1649 * just call VN_RELE(*vpp), because the vnode
1650 * is only partially initialized.
1652 gfs_dir_t *dp = (*vpp)->v_data;
1654 ASSERT((*vpp)->v_count == 1);
1655 vn_free(*vpp);
1656 VN_RELE_LOCKED(dvp);
1658 mutex_destroy(&dp->gfsd_lock);
1659 kmem_free(dp->gfsd_static,
1660 dp->gfsd_nstatic * sizeof (gfs_dirent_t));
1661 kmem_free(dp, dp->gfsd_file.gfs_size);
1663 /* dvp was held by winner in gfs_dir_create */
1664 *vpp = dvp->v_xattrdir;
1665 VN_HOLD(*vpp);
1666 } else {
1667 /* winner */
1668 (*vpp)->v_flag |= (V_XATTRDIR|V_SYSATTR);
1669 dvp->v_xattrdir = *vpp;
1672 mutex_exit(&dvp->v_lock);
1674 return (error);
1678 xattr_dir_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
1680 int error;
1681 vnode_t *pvp, *dvp;
1682 xattr_fid_t *xfidp;
1683 struct pathname pn;
1684 char *nm;
1685 uint16_t orig_len;
1687 *vpp = NULL;
1689 if (fidp->fid_len < XATTR_FIDSZ)
1690 return (EINVAL);
1692 xfidp = (xattr_fid_t *)fidp;
1693 orig_len = fidp->fid_len;
1694 fidp->fid_len = xfidp->parent_len;
1696 error = VFS_VGET(vfsp, &pvp, fidp);
1697 fidp->fid_len = orig_len;
1698 if (error)
1699 return (error);
1702 * Start by getting the GFS sysattr directory. We might need
1703 * to recreate it during the fop_lookup.
1705 nm = "";
1706 error = pn_get(nm, UIO_SYSSPACE, &pn);
1707 if (error) {
1708 VN_RELE(pvp);
1709 return (EINVAL);
1712 error = fop_lookup(pvp, nm, &dvp, &pn, LOOKUP_XATTR|CREATE_XATTR_DIR,
1713 rootvp, CRED(), NULL, NULL, NULL);
1714 pn_free(&pn);
1715 VN_RELE(pvp);
1716 if (error)
1717 return (error);
1719 if (xfidp->dir_offset == 0) {
1721 * If we were looking for the directory, we're done.
1723 *vpp = dvp;
1724 return (0);
1727 if (xfidp->dir_offset > XATTRDIR_NENTS) {
1728 VN_RELE(dvp);
1729 return (EINVAL);
1732 nm = xattr_dirents[xfidp->dir_offset - 1].gfse_name;
1734 error = pn_get(nm, UIO_SYSSPACE, &pn);
1735 if (error) {
1736 VN_RELE(dvp);
1737 return (EINVAL);
1740 error = fop_lookup(dvp, nm, vpp, &pn, 0, rootvp, CRED(), NULL,
1741 NULL, NULL);
1743 pn_free(&pn);
1744 VN_RELE(dvp);
1746 return (error);