]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_xattr.c
Only check directory xattr on ENOENT
[mirror_zfs.git] / module / zfs / zpl_xattr.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
23 *
24 * Extended attributes (xattr) on Solaris are implemented as files
25 * which exist in a hidden xattr directory. These extended attributes
26 * can be accessed using the attropen() system call which opens
27 * the extended attribute. It can then be manipulated just like
28 * a standard file descriptor. This has a couple advantages such
29 * as practically no size limit on the file, and the extended
30 * attributes permissions may differ from those of the parent file.
31 * This interface is really quite clever, but it's also completely
32 * different than what is supported on Linux. It also comes with a
33 * steep performance penalty when accessing small xattrs because they
34 * are not stored with the parent file.
35 *
36 * Under Linux extended attributes are manipulated by the system
37 * calls getxattr(2), setxattr(2), and listxattr(2). They consider
38 * extended attributes to be name/value pairs where the name is a
39 * NULL terminated string. The name must also include one of the
40 * following namespace prefixes:
41 *
42 * user - No restrictions and is available to user applications.
43 * trusted - Restricted to kernel and root (CAP_SYS_ADMIN) use.
44 * system - Used for access control lists (system.nfs4_acl, etc).
45 * security - Used by SELinux to store a files security context.
46 *
47 * The value under Linux to limited to 65536 bytes of binary data.
48 * In practice, individual xattrs tend to be much smaller than this
49 * and are typically less than 100 bytes. A good example of this
50 * are the security.selinux xattrs which are less than 100 bytes and
51 * exist for every file when xattr labeling is enabled.
52 *
53 * The Linux xattr implemenation has been written to take advantage of
54 * this typical usage. When the dataset property 'xattr=sa' is set,
55 * then xattrs will be preferentially stored as System Attributes (SA).
56 * This allows tiny xattrs (~100 bytes) to be stored with the dnode and
57 * up to 64k of xattrs to be stored in the spill block. If additional
58 * xattr space is required, which is unlikely under Linux, they will
59 * be stored using the traditional directory approach.
60 *
61 * This optimization results in roughly a 3x performance improvement
62 * when accessing xattrs because it avoids the need to perform a seek
63 * for every xattr value. When multiple xattrs are stored per-file
64 * the performance improvements are even greater because all of the
65 * xattrs stored in the spill block will be cached.
66 *
67 * However, by default SA based xattrs are disabled in the Linux port
68 * to maximize compatibility with other implementations. If you do
69 * enable SA based xattrs then they will not be visible on platforms
70 * which do not support this feature.
71 *
72 * NOTE: One additional consequence of the xattr directory implementation
73 * is that when an extended attribute is manipulated an inode is created.
74 * This inode will exist in the Linux inode cache but there will be no
75 * associated entry in the dentry cache which references it. This is
76 * safe but it may result in some confusion. Enabling SA based xattrs
77 * largely avoids the issue except in the overflow case.
78 */
79
80 #include <sys/zfs_vfsops.h>
81 #include <sys/zfs_vnops.h>
82 #include <sys/zfs_znode.h>
83 #include <sys/vfs.h>
84 #include <sys/zpl.h>
85
86 typedef struct xattr_filldir {
87 size_t size;
88 size_t offset;
89 char *buf;
90 struct inode *inode;
91 } xattr_filldir_t;
92
93 static int
94 zpl_xattr_filldir(void *arg, const char *name, int name_len,
95 loff_t offset, uint64_t objnum, unsigned int d_type)
96 {
97 xattr_filldir_t *xf = arg;
98
99 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
100 if (!(ITOZSB(xf->inode)->z_flags & ZSB_XATTR))
101 return (0);
102
103 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
104 if (!capable(CAP_SYS_ADMIN))
105 return (0);
106
107 /* When xf->buf is NULL only calculate the required size. */
108 if (xf->buf) {
109 if (xf->offset + name_len + 1 > xf->size)
110 return (-ERANGE);
111
112 memcpy(xf->buf + xf->offset, name, name_len);
113 xf->buf[xf->offset + name_len] = '\0';
114 }
115
116 xf->offset += (name_len + 1);
117
118 return (0);
119 }
120
121 static ssize_t
122 zpl_xattr_list_dir(xattr_filldir_t *xf, cred_t *cr)
123 {
124 struct inode *ip = xf->inode;
125 struct inode *dxip = NULL;
126 loff_t pos = 3; /* skip '.', '..', and '.zfs' entries. */
127 int error;
128
129 /* Lookup the xattr directory */
130 error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR, cr, NULL, NULL);
131 if (error) {
132 if (error == -ENOENT)
133 error = 0;
134
135 return (error);
136 }
137
138 /* Fill provided buffer via zpl_zattr_filldir helper */
139 error = -zfs_readdir(dxip, (void *)xf, zpl_xattr_filldir, &pos, cr);
140 iput(dxip);
141
142 return (error);
143 }
144
145 static ssize_t
146 zpl_xattr_list_sa(xattr_filldir_t *xf)
147 {
148 znode_t *zp = ITOZ(xf->inode);
149 nvpair_t *nvp = NULL;
150 int error = 0;
151
152 mutex_enter(&zp->z_lock);
153 if (zp->z_xattr_cached == NULL)
154 error = -zfs_sa_get_xattr(zp);
155 mutex_exit(&zp->z_lock);
156
157 if (error)
158 return (error);
159
160 ASSERT(zp->z_xattr_cached);
161
162 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
163 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
164
165 error = zpl_xattr_filldir((void *)xf, nvpair_name(nvp),
166 strlen(nvpair_name(nvp)), 0, 0, 0);
167 if (error)
168 return (error);
169 }
170
171 return (0);
172 }
173
174 ssize_t
175 zpl_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
176 {
177 znode_t *zp = ITOZ(dentry->d_inode);
178 zfs_sb_t *zsb = ZTOZSB(zp);
179 xattr_filldir_t xf = { buffer_size, 0, buffer, dentry->d_inode };
180 cred_t *cr = CRED();
181 int error = 0;
182
183 crhold(cr);
184 rw_enter(&zp->z_xattr_lock, RW_READER);
185
186 if (zsb->z_use_sa && zp->z_is_sa) {
187 error = zpl_xattr_list_sa(&xf);
188 if (error)
189 goto out;
190 }
191
192 error = zpl_xattr_list_dir(&xf, cr);
193 if (error)
194 goto out;
195
196 error = xf.offset;
197 out:
198
199 rw_exit(&zp->z_xattr_lock);
200 crfree(cr);
201
202 return (error);
203 }
204
205 static int
206 zpl_xattr_get_dir(struct inode *ip, const char *name, void *value,
207 size_t size, cred_t *cr)
208 {
209 struct inode *dxip = NULL;
210 struct inode *xip = NULL;
211 int error;
212
213 /* Lookup the xattr directory */
214 error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR, cr, NULL, NULL);
215 if (error)
216 goto out;
217
218 /* Lookup a specific xattr name in the directory */
219 error = -zfs_lookup(dxip, (char *)name, &xip, 0, cr, NULL, NULL);
220 if (error)
221 goto out;
222
223 if (!size) {
224 error = i_size_read(xip);
225 goto out;
226 }
227
228 if (size < i_size_read(xip)) {
229 error = -ERANGE;
230 goto out;
231 }
232
233 error = zpl_read_common(xip, value, size, 0, UIO_SYSSPACE, 0, cr);
234 out:
235 if (xip)
236 iput(xip);
237
238 if (dxip)
239 iput(dxip);
240
241 return (error);
242 }
243
244 static int
245 zpl_xattr_get_sa(struct inode *ip, const char *name, void *value, size_t size)
246 {
247 znode_t *zp = ITOZ(ip);
248 uchar_t *nv_value;
249 uint_t nv_size;
250 int error = 0;
251
252 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
253
254 mutex_enter(&zp->z_lock);
255 if (zp->z_xattr_cached == NULL)
256 error = -zfs_sa_get_xattr(zp);
257 mutex_exit(&zp->z_lock);
258
259 if (error)
260 return (error);
261
262 ASSERT(zp->z_xattr_cached);
263 error = -nvlist_lookup_byte_array(zp->z_xattr_cached, name,
264 &nv_value, &nv_size);
265 if (error)
266 return (error);
267
268 if (!size)
269 return (nv_size);
270
271 if (size < nv_size)
272 return (-ERANGE);
273
274 memcpy(value, nv_value, nv_size);
275
276 return (nv_size);
277 }
278
279 static int
280 __zpl_xattr_get(struct inode *ip, const char *name, void *value, size_t size,
281 cred_t *cr)
282 {
283 znode_t *zp = ITOZ(ip);
284 zfs_sb_t *zsb = ZTOZSB(zp);
285 int error;
286
287 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
288
289 if (zsb->z_use_sa && zp->z_is_sa) {
290 error = zpl_xattr_get_sa(ip, name, value, size);
291 if (error != -ENOENT)
292 goto out;
293 }
294
295 error = zpl_xattr_get_dir(ip, name, value, size, cr);
296 out:
297 if (error == -ENOENT)
298 error = -ENODATA;
299
300 return (error);
301 }
302
303 static int
304 zpl_xattr_get(struct inode *ip, const char *name, void *value, size_t size)
305 {
306 znode_t *zp = ITOZ(ip);
307 cred_t *cr = CRED();
308 int error;
309
310 crhold(cr);
311 rw_enter(&zp->z_xattr_lock, RW_READER);
312 error = __zpl_xattr_get(ip, name, value, size, cr);
313 rw_exit(&zp->z_xattr_lock);
314 crfree(cr);
315
316 return (error);
317 }
318
319 static int
320 zpl_xattr_set_dir(struct inode *ip, const char *name, const void *value,
321 size_t size, int flags, cred_t *cr)
322 {
323 struct inode *dxip = NULL;
324 struct inode *xip = NULL;
325 vattr_t *vap = NULL;
326 ssize_t wrote;
327 int error;
328 const int xattr_mode = S_IFREG | 0644;
329
330 /* Lookup the xattr directory and create it if required. */
331 error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR | CREATE_XATTR_DIR,
332 cr, NULL, NULL);
333 if (error)
334 goto out;
335
336 /* Lookup a specific xattr name in the directory */
337 error = -zfs_lookup(dxip, (char *)name, &xip, 0, cr, NULL, NULL);
338 if (error && (error != -ENOENT))
339 goto out;
340
341 error = 0;
342
343 /* Remove a specific name xattr when value is set to NULL. */
344 if (value == NULL) {
345 if (xip)
346 error = -zfs_remove(dxip, (char *)name, cr);
347
348 goto out;
349 }
350
351 /* Lookup failed create a new xattr. */
352 if (xip == NULL) {
353 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
354 vap->va_mode = xattr_mode;
355 vap->va_mask = ATTR_MODE;
356 vap->va_uid = crgetfsuid(cr);
357 vap->va_gid = crgetfsgid(cr);
358
359 error = -zfs_create(dxip, (char *)name, vap, 0, 0644, &xip,
360 cr, 0, NULL);
361 if (error)
362 goto out;
363 }
364
365 ASSERT(xip != NULL);
366
367 error = -zfs_freesp(ITOZ(xip), 0, 0, xattr_mode, TRUE);
368 if (error)
369 goto out;
370
371 wrote = zpl_write_common(xip, value, size, 0, UIO_SYSSPACE, 0, cr);
372 if (wrote < 0)
373 error = wrote;
374
375 out:
376 if (vap)
377 kmem_free(vap, sizeof(vattr_t));
378
379 if (xip)
380 iput(xip);
381
382 if (dxip)
383 iput(dxip);
384
385 if (error == -ENOENT)
386 error = -ENODATA;
387
388 ASSERT3S(error, <=, 0);
389
390 return (error);
391 }
392
393 static int
394 zpl_xattr_set_sa(struct inode *ip, const char *name, const void *value,
395 size_t size, int flags, cred_t *cr)
396 {
397 znode_t *zp = ITOZ(ip);
398 nvlist_t *nvl;
399 size_t sa_size;
400 int error;
401
402 ASSERT(zp->z_xattr_cached);
403 nvl = zp->z_xattr_cached;
404
405 if (value == NULL) {
406 error = -nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
407 if (error == -ENOENT)
408 error = zpl_xattr_set_dir(ip, name, NULL, 0, flags, cr);
409 } else {
410 /* Limited to 32k to keep nvpair memory allocations small */
411 if (size > DXATTR_MAX_ENTRY_SIZE)
412 return (-EFBIG);
413
414 /* Prevent the DXATTR SA from consuming the entire SA region */
415 error = -nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
416 if (error)
417 return (error);
418
419 if (sa_size > DXATTR_MAX_SA_SIZE)
420 return (-EFBIG);
421
422 error = -nvlist_add_byte_array(nvl, name,
423 (uchar_t *)value, size);
424 if (error)
425 return (error);
426 }
427
428 /* Update the SA for additions, modifications, and removals. */
429 if (!error)
430 error = -zfs_sa_set_xattr(zp);
431
432 ASSERT3S(error, <=, 0);
433
434 return (error);
435 }
436
437 static int
438 zpl_xattr_set(struct inode *ip, const char *name, const void *value,
439 size_t size, int flags)
440 {
441 znode_t *zp = ITOZ(ip);
442 zfs_sb_t *zsb = ZTOZSB(zp);
443 cred_t *cr = CRED();
444 int error;
445
446 crhold(cr);
447 rw_enter(&ITOZ(ip)->z_xattr_lock, RW_WRITER);
448
449 /*
450 * Before setting the xattr check to see if it already exists.
451 * This is done to ensure the following optional flags are honored.
452 *
453 * XATTR_CREATE: fail if xattr already exists
454 * XATTR_REPLACE: fail if xattr does not exist
455 */
456 error = __zpl_xattr_get(ip, name, NULL, 0, cr);
457 if (error < 0) {
458 if (error != -ENODATA)
459 goto out;
460
461 if ((error == -ENODATA) && (flags & XATTR_REPLACE))
462 goto out;
463 } else {
464 error = -EEXIST;
465 if (flags & XATTR_CREATE)
466 goto out;
467 }
468
469 /* Preferentially store the xattr as a SA for better performance */
470 if (zsb->z_use_sa && zsb->z_xattr_sa && zp->z_is_sa) {
471 error = zpl_xattr_set_sa(ip, name, value, size, flags, cr);
472 if (error == 0)
473 goto out;
474 }
475
476 error = zpl_xattr_set_dir(ip, name, value, size, flags, cr);
477 out:
478 rw_exit(&ITOZ(ip)->z_xattr_lock);
479 crfree(cr);
480 ASSERT3S(error, <=, 0);
481
482 return (error);
483 }
484
485 static int
486 __zpl_xattr_user_get(struct inode *ip, const char *name,
487 void *value, size_t size)
488 {
489 char *xattr_name;
490 int error;
491
492 if (strcmp(name, "") == 0)
493 return -EINVAL;
494
495 if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
496 return -EOPNOTSUPP;
497
498 xattr_name = kmem_asprintf("%s%s", XATTR_USER_PREFIX, name);
499 error = zpl_xattr_get(ip, xattr_name, value, size);
500 strfree(xattr_name);
501
502 return (error);
503 }
504 ZPL_XATTR_GET_WRAPPER(zpl_xattr_user_get);
505
506 static int
507 __zpl_xattr_user_set(struct inode *ip, const char *name,
508 const void *value, size_t size, int flags)
509 {
510 char *xattr_name;
511 int error;
512
513 if (strcmp(name, "") == 0)
514 return -EINVAL;
515
516 if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
517 return -EOPNOTSUPP;
518
519 xattr_name = kmem_asprintf("%s%s", XATTR_USER_PREFIX, name);
520 error = zpl_xattr_set(ip, xattr_name, value, size, flags);
521 strfree(xattr_name);
522
523 return (error);
524 }
525 ZPL_XATTR_SET_WRAPPER(zpl_xattr_user_set);
526
527 xattr_handler_t zpl_xattr_user_handler = {
528 .prefix = XATTR_USER_PREFIX,
529 .get = zpl_xattr_user_get,
530 .set = zpl_xattr_user_set,
531 };
532
533 static int
534 __zpl_xattr_trusted_get(struct inode *ip, const char *name,
535 void *value, size_t size)
536 {
537 char *xattr_name;
538 int error;
539
540 if (!capable(CAP_SYS_ADMIN))
541 return -EACCES;
542
543 if (strcmp(name, "") == 0)
544 return -EINVAL;
545
546 xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
547 error = zpl_xattr_get(ip, xattr_name, value, size);
548 strfree(xattr_name);
549
550 return (error);
551 }
552 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get);
553
554 static int
555 __zpl_xattr_trusted_set(struct inode *ip, const char *name,
556 const void *value, size_t size, int flags)
557 {
558 char *xattr_name;
559 int error;
560
561 if (!capable(CAP_SYS_ADMIN))
562 return -EACCES;
563
564 if (strcmp(name, "") == 0)
565 return -EINVAL;
566
567 xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
568 error = zpl_xattr_set(ip, xattr_name, value, size, flags);
569 strfree(xattr_name);
570
571 return (error);
572 }
573 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set);
574
575 xattr_handler_t zpl_xattr_trusted_handler = {
576 .prefix = XATTR_TRUSTED_PREFIX,
577 .get = zpl_xattr_trusted_get,
578 .set = zpl_xattr_trusted_set,
579 };
580
581 static int
582 __zpl_xattr_security_get(struct inode *ip, const char *name,
583 void *value, size_t size)
584 {
585 char *xattr_name;
586 int error;
587
588 if (strcmp(name, "") == 0)
589 return -EINVAL;
590
591 xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
592 error = zpl_xattr_get(ip, xattr_name, value, size);
593 strfree(xattr_name);
594
595 return (error);
596 }
597 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get);
598
599 static int
600 __zpl_xattr_security_set(struct inode *ip, const char *name,
601 const void *value, size_t size, int flags)
602 {
603 char *xattr_name;
604 int error;
605
606 if (strcmp(name, "") == 0)
607 return -EINVAL;
608
609 xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
610 error = zpl_xattr_set(ip, xattr_name, value, size, flags);
611 strfree(xattr_name);
612
613 return (error);
614 }
615 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set);
616
617 #ifdef HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY
618 static int
619 __zpl_xattr_security_init(struct inode *ip, const struct xattr *xattrs,
620 void *fs_info)
621 {
622 const struct xattr *xattr;
623 int error = 0;
624
625 for (xattr = xattrs; xattr->name != NULL; xattr++) {
626 error = __zpl_xattr_security_set(ip,
627 xattr->name, xattr->value, xattr->value_len, 0);
628
629 if (error < 0)
630 break;
631 }
632
633 return (error);
634 }
635
636 int
637 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
638 const struct qstr *qstr)
639 {
640 return security_inode_init_security(ip, dip, qstr,
641 &__zpl_xattr_security_init, NULL);
642 }
643
644 #else
645 int
646 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
647 const struct qstr *qstr)
648 {
649 int error;
650 size_t len;
651 void *value;
652 char *name;
653
654 error = zpl_security_inode_init_security(ip, dip, qstr,
655 &name, &value, &len);
656 if (error) {
657 if (error == -EOPNOTSUPP)
658 return 0;
659 return (error);
660 }
661
662 error = __zpl_xattr_security_set(ip, name, value, len, 0);
663
664 kfree(name);
665 kfree(value);
666
667 return (error);
668 }
669 #endif /* HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY */
670
671 xattr_handler_t zpl_xattr_security_handler = {
672 .prefix = XATTR_SECURITY_PREFIX,
673 .get = zpl_xattr_security_get,
674 .set = zpl_xattr_security_set,
675 };
676
677 xattr_handler_t *zpl_xattr_handlers[] = {
678 &zpl_xattr_security_handler,
679 &zpl_xattr_trusted_handler,
680 &zpl_xattr_user_handler,
681 #ifdef HAVE_POSIX_ACLS
682 &zpl_xattr_acl_access_handler,
683 &zpl_xattr_acl_default_handler,
684 #endif /* HAVE_POSIX_ACLS */
685 NULL
686 };