]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_xattr.c
Posix ACL Support
[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/zap.h>
84 #include <sys/vfs.h>
85 #include <sys/zpl.h>
86
87 typedef struct xattr_filldir {
88 size_t size;
89 size_t offset;
90 char *buf;
91 struct inode *inode;
92 } xattr_filldir_t;
93
94 static int
95 zpl_xattr_filldir(xattr_filldir_t *xf, const char *name, int name_len)
96 {
97 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
98 if (!(ITOZSB(xf->inode)->z_flags & ZSB_XATTR))
99 return (0);
100
101 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
102 if (!capable(CAP_SYS_ADMIN))
103 return (0);
104
105 /* When xf->buf is NULL only calculate the required size. */
106 if (xf->buf) {
107 if (xf->offset + name_len + 1 > xf->size)
108 return (-ERANGE);
109
110 memcpy(xf->buf + xf->offset, name, name_len);
111 xf->buf[xf->offset + name_len] = '\0';
112 }
113
114 xf->offset += (name_len + 1);
115
116 return (0);
117 }
118
119 /*
120 * Read as many directory entry names as will fit in to the provided buffer,
121 * or when no buffer is provided calculate the required buffer size.
122 */
123 int
124 zpl_xattr_readdir(struct inode *dxip, xattr_filldir_t *xf)
125 {
126 zap_cursor_t zc;
127 zap_attribute_t zap;
128 int error;
129
130 zap_cursor_init(&zc, ITOZSB(dxip)->z_os, ITOZ(dxip)->z_id);
131
132 while ((error = -zap_cursor_retrieve(&zc, &zap)) == 0) {
133
134 if (zap.za_integer_length != 8 || zap.za_num_integers != 1) {
135 error = -ENXIO;
136 break;
137 }
138
139 error = zpl_xattr_filldir(xf, zap.za_name, strlen(zap.za_name));
140 if (error)
141 break;
142
143 zap_cursor_advance(&zc);
144 }
145
146 zap_cursor_fini(&zc);
147
148 if (error == -ENOENT)
149 error = 0;
150
151 return (error);
152 }
153
154 static ssize_t
155 zpl_xattr_list_dir(xattr_filldir_t *xf, cred_t *cr)
156 {
157 struct inode *ip = xf->inode;
158 struct inode *dxip = NULL;
159 int error;
160
161 /* Lookup the xattr directory */
162 error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR, cr, NULL, NULL);
163 if (error) {
164 if (error == -ENOENT)
165 error = 0;
166
167 return (error);
168 }
169
170 error = zpl_xattr_readdir(dxip, xf);
171 iput(dxip);
172
173 return (error);
174 }
175
176 static ssize_t
177 zpl_xattr_list_sa(xattr_filldir_t *xf)
178 {
179 znode_t *zp = ITOZ(xf->inode);
180 nvpair_t *nvp = NULL;
181 int error = 0;
182
183 mutex_enter(&zp->z_lock);
184 if (zp->z_xattr_cached == NULL)
185 error = -zfs_sa_get_xattr(zp);
186 mutex_exit(&zp->z_lock);
187
188 if (error)
189 return (error);
190
191 ASSERT(zp->z_xattr_cached);
192
193 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
194 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
195
196 error = zpl_xattr_filldir(xf, nvpair_name(nvp),
197 strlen(nvpair_name(nvp)));
198 if (error)
199 return (error);
200 }
201
202 return (0);
203 }
204
205 ssize_t
206 zpl_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
207 {
208 znode_t *zp = ITOZ(dentry->d_inode);
209 zfs_sb_t *zsb = ZTOZSB(zp);
210 xattr_filldir_t xf = { buffer_size, 0, buffer, dentry->d_inode };
211 cred_t *cr = CRED();
212 int error = 0;
213
214 crhold(cr);
215 rw_enter(&zp->z_xattr_lock, RW_READER);
216
217 if (zsb->z_use_sa && zp->z_is_sa) {
218 error = zpl_xattr_list_sa(&xf);
219 if (error)
220 goto out;
221 }
222
223 error = zpl_xattr_list_dir(&xf, cr);
224 if (error)
225 goto out;
226
227 error = xf.offset;
228 out:
229
230 rw_exit(&zp->z_xattr_lock);
231 crfree(cr);
232
233 return (error);
234 }
235
236 static int
237 zpl_xattr_get_dir(struct inode *ip, const char *name, void *value,
238 size_t size, cred_t *cr)
239 {
240 struct inode *dxip = NULL;
241 struct inode *xip = NULL;
242 int error;
243
244 /* Lookup the xattr directory */
245 error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR, cr, NULL, NULL);
246 if (error)
247 goto out;
248
249 /* Lookup a specific xattr name in the directory */
250 error = -zfs_lookup(dxip, (char *)name, &xip, 0, cr, NULL, NULL);
251 if (error)
252 goto out;
253
254 if (!size) {
255 error = i_size_read(xip);
256 goto out;
257 }
258
259 if (size < i_size_read(xip)) {
260 error = -ERANGE;
261 goto out;
262 }
263
264 error = zpl_read_common(xip, value, size, 0, UIO_SYSSPACE, 0, cr);
265 out:
266 if (xip)
267 iput(xip);
268
269 if (dxip)
270 iput(dxip);
271
272 return (error);
273 }
274
275 static int
276 zpl_xattr_get_sa(struct inode *ip, const char *name, void *value, size_t size)
277 {
278 znode_t *zp = ITOZ(ip);
279 uchar_t *nv_value;
280 uint_t nv_size;
281 int error = 0;
282
283 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
284
285 mutex_enter(&zp->z_lock);
286 if (zp->z_xattr_cached == NULL)
287 error = -zfs_sa_get_xattr(zp);
288 mutex_exit(&zp->z_lock);
289
290 if (error)
291 return (error);
292
293 ASSERT(zp->z_xattr_cached);
294 error = -nvlist_lookup_byte_array(zp->z_xattr_cached, name,
295 &nv_value, &nv_size);
296 if (error)
297 return (error);
298
299 if (!size)
300 return (nv_size);
301
302 if (size < nv_size)
303 return (-ERANGE);
304
305 memcpy(value, nv_value, nv_size);
306
307 return (nv_size);
308 }
309
310 static int
311 __zpl_xattr_get(struct inode *ip, const char *name, void *value, size_t size,
312 cred_t *cr)
313 {
314 znode_t *zp = ITOZ(ip);
315 zfs_sb_t *zsb = ZTOZSB(zp);
316 int error;
317
318 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
319
320 if (zsb->z_use_sa && zp->z_is_sa) {
321 error = zpl_xattr_get_sa(ip, name, value, size);
322 if (error != -ENOENT)
323 goto out;
324 }
325
326 error = zpl_xattr_get_dir(ip, name, value, size, cr);
327 out:
328 if (error == -ENOENT)
329 error = -ENODATA;
330
331 return (error);
332 }
333
334 static int
335 zpl_xattr_get(struct inode *ip, const char *name, void *value, size_t size)
336 {
337 znode_t *zp = ITOZ(ip);
338 cred_t *cr = CRED();
339 int error;
340
341 crhold(cr);
342 rw_enter(&zp->z_xattr_lock, RW_READER);
343 error = __zpl_xattr_get(ip, name, value, size, cr);
344 rw_exit(&zp->z_xattr_lock);
345 crfree(cr);
346
347 return (error);
348 }
349
350 static int
351 zpl_xattr_set_dir(struct inode *ip, const char *name, const void *value,
352 size_t size, int flags, cred_t *cr)
353 {
354 struct inode *dxip = NULL;
355 struct inode *xip = NULL;
356 vattr_t *vap = NULL;
357 ssize_t wrote;
358 int lookup_flags, error;
359 const int xattr_mode = S_IFREG | 0644;
360
361 /*
362 * Lookup the xattr directory. When we're adding an entry pass
363 * CREATE_XATTR_DIR to ensure the xattr directory is created.
364 * When removing an entry this flag is not passed to avoid
365 * unnecessarily creating a new xattr directory.
366 */
367 lookup_flags = LOOKUP_XATTR;
368 if (value != NULL)
369 lookup_flags |= CREATE_XATTR_DIR;
370
371 error = -zfs_lookup(ip, NULL, &dxip, lookup_flags, cr, NULL, NULL);
372 if (error)
373 goto out;
374
375 /* Lookup a specific xattr name in the directory */
376 error = -zfs_lookup(dxip, (char *)name, &xip, 0, cr, NULL, NULL);
377 if (error && (error != -ENOENT))
378 goto out;
379
380 error = 0;
381
382 /* Remove a specific name xattr when value is set to NULL. */
383 if (value == NULL) {
384 if (xip)
385 error = -zfs_remove(dxip, (char *)name, cr);
386
387 goto out;
388 }
389
390 /* Lookup failed create a new xattr. */
391 if (xip == NULL) {
392 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
393 vap->va_mode = xattr_mode;
394 vap->va_mask = ATTR_MODE;
395 vap->va_uid = crgetfsuid(cr);
396 vap->va_gid = crgetfsgid(cr);
397
398 error = -zfs_create(dxip, (char *)name, vap, 0, 0644, &xip,
399 cr, 0, NULL);
400 if (error)
401 goto out;
402 }
403
404 ASSERT(xip != NULL);
405
406 error = -zfs_freesp(ITOZ(xip), 0, 0, xattr_mode, TRUE);
407 if (error)
408 goto out;
409
410 wrote = zpl_write_common(xip, value, size, 0, UIO_SYSSPACE, 0, cr);
411 if (wrote < 0)
412 error = wrote;
413
414 out:
415 if (vap)
416 kmem_free(vap, sizeof(vattr_t));
417
418 if (xip)
419 iput(xip);
420
421 if (dxip)
422 iput(dxip);
423
424 if (error == -ENOENT)
425 error = -ENODATA;
426
427 ASSERT3S(error, <=, 0);
428
429 return (error);
430 }
431
432 static int
433 zpl_xattr_set_sa(struct inode *ip, const char *name, const void *value,
434 size_t size, int flags, cred_t *cr)
435 {
436 znode_t *zp = ITOZ(ip);
437 nvlist_t *nvl;
438 size_t sa_size;
439 int error;
440
441 ASSERT(zp->z_xattr_cached);
442 nvl = zp->z_xattr_cached;
443
444 if (value == NULL) {
445 error = -nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
446 if (error == -ENOENT)
447 error = zpl_xattr_set_dir(ip, name, NULL, 0, flags, cr);
448 } else {
449 /* Do not allow SA xattrs in symlinks (issue #1648) */
450 if (S_ISLNK(ip->i_mode))
451 return (-EMLINK);
452
453 /* Limited to 32k to keep nvpair memory allocations small */
454 if (size > DXATTR_MAX_ENTRY_SIZE)
455 return (-EFBIG);
456
457 /* Prevent the DXATTR SA from consuming the entire SA region */
458 error = -nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
459 if (error)
460 return (error);
461
462 if (sa_size > DXATTR_MAX_SA_SIZE)
463 return (-EFBIG);
464
465 error = -nvlist_add_byte_array(nvl, name,
466 (uchar_t *)value, size);
467 if (error)
468 return (error);
469 }
470
471 /* Update the SA for additions, modifications, and removals. */
472 if (!error)
473 error = -zfs_sa_set_xattr(zp);
474
475 ASSERT3S(error, <=, 0);
476
477 return (error);
478 }
479
480 static int
481 zpl_xattr_set(struct inode *ip, const char *name, const void *value,
482 size_t size, int flags)
483 {
484 znode_t *zp = ITOZ(ip);
485 zfs_sb_t *zsb = ZTOZSB(zp);
486 cred_t *cr = CRED();
487 int error;
488
489 crhold(cr);
490 rw_enter(&ITOZ(ip)->z_xattr_lock, RW_WRITER);
491
492 /*
493 * Before setting the xattr check to see if it already exists.
494 * This is done to ensure the following optional flags are honored.
495 *
496 * XATTR_CREATE: fail if xattr already exists
497 * XATTR_REPLACE: fail if xattr does not exist
498 */
499 error = __zpl_xattr_get(ip, name, NULL, 0, cr);
500 if (error < 0) {
501 if (error != -ENODATA)
502 goto out;
503
504 if (flags & XATTR_REPLACE)
505 goto out;
506
507 /* The xattr to be removed already doesn't exist */
508 error = 0;
509 if (value == NULL)
510 goto out;
511 } else {
512 error = -EEXIST;
513 if (flags & XATTR_CREATE)
514 goto out;
515 }
516
517 /* Preferentially store the xattr as a SA for better performance */
518 if (zsb->z_use_sa && zsb->z_xattr_sa && zp->z_is_sa) {
519 error = zpl_xattr_set_sa(ip, name, value, size, flags, cr);
520 if (error == 0)
521 goto out;
522 }
523
524 error = zpl_xattr_set_dir(ip, name, value, size, flags, cr);
525 out:
526 rw_exit(&ITOZ(ip)->z_xattr_lock);
527 crfree(cr);
528 ASSERT3S(error, <=, 0);
529
530 return (error);
531 }
532
533 static int
534 __zpl_xattr_user_get(struct inode *ip, const char *name,
535 void *value, size_t size)
536 {
537 char *xattr_name;
538 int error;
539
540 if (strcmp(name, "") == 0)
541 return -EINVAL;
542
543 if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
544 return -EOPNOTSUPP;
545
546 xattr_name = kmem_asprintf("%s%s", XATTR_USER_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_user_get);
553
554 static int
555 __zpl_xattr_user_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 (strcmp(name, "") == 0)
562 return -EINVAL;
563
564 if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
565 return -EOPNOTSUPP;
566
567 xattr_name = kmem_asprintf("%s%s", XATTR_USER_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_user_set);
574
575 xattr_handler_t zpl_xattr_user_handler = {
576 .prefix = XATTR_USER_PREFIX,
577 .get = zpl_xattr_user_get,
578 .set = zpl_xattr_user_set,
579 };
580
581 static int
582 __zpl_xattr_trusted_get(struct inode *ip, const char *name,
583 void *value, size_t size)
584 {
585 char *xattr_name;
586 int error;
587
588 if (!capable(CAP_SYS_ADMIN))
589 return -EACCES;
590
591 if (strcmp(name, "") == 0)
592 return -EINVAL;
593
594 xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
595 error = zpl_xattr_get(ip, xattr_name, value, size);
596 strfree(xattr_name);
597
598 return (error);
599 }
600 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get);
601
602 static int
603 __zpl_xattr_trusted_set(struct inode *ip, const char *name,
604 const void *value, size_t size, int flags)
605 {
606 char *xattr_name;
607 int error;
608
609 if (!capable(CAP_SYS_ADMIN))
610 return -EACCES;
611
612 if (strcmp(name, "") == 0)
613 return -EINVAL;
614
615 xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
616 error = zpl_xattr_set(ip, xattr_name, value, size, flags);
617 strfree(xattr_name);
618
619 return (error);
620 }
621 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set);
622
623 xattr_handler_t zpl_xattr_trusted_handler = {
624 .prefix = XATTR_TRUSTED_PREFIX,
625 .get = zpl_xattr_trusted_get,
626 .set = zpl_xattr_trusted_set,
627 };
628
629 static int
630 __zpl_xattr_security_get(struct inode *ip, const char *name,
631 void *value, size_t size)
632 {
633 char *xattr_name;
634 int error;
635
636 if (strcmp(name, "") == 0)
637 return -EINVAL;
638
639 xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
640 error = zpl_xattr_get(ip, xattr_name, value, size);
641 strfree(xattr_name);
642
643 return (error);
644 }
645 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get);
646
647 static int
648 __zpl_xattr_security_set(struct inode *ip, const char *name,
649 const void *value, size_t size, int flags)
650 {
651 char *xattr_name;
652 int error;
653
654 if (strcmp(name, "") == 0)
655 return -EINVAL;
656
657 xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
658 error = zpl_xattr_set(ip, xattr_name, value, size, flags);
659 strfree(xattr_name);
660
661 return (error);
662 }
663 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set);
664
665 #ifdef HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY
666 static int
667 __zpl_xattr_security_init(struct inode *ip, const struct xattr *xattrs,
668 void *fs_info)
669 {
670 const struct xattr *xattr;
671 int error = 0;
672
673 for (xattr = xattrs; xattr->name != NULL; xattr++) {
674 error = __zpl_xattr_security_set(ip,
675 xattr->name, xattr->value, xattr->value_len, 0);
676
677 if (error < 0)
678 break;
679 }
680
681 return (error);
682 }
683
684 int
685 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
686 const struct qstr *qstr)
687 {
688 return security_inode_init_security(ip, dip, qstr,
689 &__zpl_xattr_security_init, NULL);
690 }
691
692 #else
693 int
694 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
695 const struct qstr *qstr)
696 {
697 int error;
698 size_t len;
699 void *value;
700 char *name;
701
702 error = zpl_security_inode_init_security(ip, dip, qstr,
703 &name, &value, &len);
704 if (error) {
705 if (error == -EOPNOTSUPP)
706 return 0;
707 return (error);
708 }
709
710 error = __zpl_xattr_security_set(ip, name, value, len, 0);
711
712 kfree(name);
713 kfree(value);
714
715 return (error);
716 }
717 #endif /* HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY */
718
719 xattr_handler_t zpl_xattr_security_handler = {
720 .prefix = XATTR_SECURITY_PREFIX,
721 .get = zpl_xattr_security_get,
722 .set = zpl_xattr_security_set,
723 };
724
725 int
726 zpl_set_acl(struct inode *ip, int type, struct posix_acl *acl)
727 {
728 struct super_block *sb = ITOZSB(ip)->z_sb;
729 char *name, *value = NULL;
730 int error = 0;
731 size_t size = 0;
732
733 if (S_ISLNK(ip->i_mode))
734 return (-EOPNOTSUPP);
735
736 switch(type) {
737 case ACL_TYPE_ACCESS:
738 name = POSIX_ACL_XATTR_ACCESS;
739 if (acl) {
740 zpl_equivmode_t mode = ip->i_mode;
741 error = posix_acl_equiv_mode(acl, &mode);
742 if (error < 0) {
743 return (error);
744 } else {
745 /*
746 * The mode bits will have been set by
747 * ->zfs_setattr()->zfs_acl_chmod_setattr()
748 * using the ZFS ACL conversion. If they
749 * differ from the Posix ACL conversion dirty
750 * the inode to write the Posix mode bits.
751 */
752 if (ip->i_mode != mode) {
753 ip->i_mode = mode;
754 ip->i_ctime = current_fs_time(sb);
755 mark_inode_dirty(ip);
756 }
757
758 if (error == 0)
759 acl = NULL;
760 }
761 }
762 break;
763
764 case ACL_TYPE_DEFAULT:
765 name = POSIX_ACL_XATTR_DEFAULT;
766 if (!S_ISDIR(ip->i_mode))
767 return (acl ? -EACCES : 0);
768 break;
769
770 default:
771 return (-EINVAL);
772 }
773
774 if (acl) {
775 size = posix_acl_xattr_size(acl->a_count);
776 value = kmem_alloc(size, KM_SLEEP);
777
778 error = zpl_acl_to_xattr(acl, value, size);
779 if (error < 0) {
780 kmem_free(value, size);
781 return (error);
782 }
783 }
784
785 error = zpl_xattr_set(ip, name, value, size, 0);
786 if (value)
787 kmem_free(value, size);
788
789 if (!error) {
790 if (acl)
791 zpl_set_cached_acl(ip, type, acl);
792 else
793 zpl_forget_cached_acl(ip, type);
794 }
795
796 return (error);
797 }
798
799 struct posix_acl *
800 zpl_get_acl(struct inode *ip, int type)
801 {
802 struct posix_acl *acl;
803 void *value = NULL;
804 char *name;
805 int size;
806
807 #ifdef HAVE_POSIX_ACL_CACHING
808 acl = get_cached_acl(ip, type);
809 if (acl != ACL_NOT_CACHED)
810 return (acl);
811 #endif /* HAVE_POSIX_ACL_CACHING */
812
813 switch (type) {
814 case ACL_TYPE_ACCESS:
815 name = POSIX_ACL_XATTR_ACCESS;
816 break;
817 case ACL_TYPE_DEFAULT:
818 name = POSIX_ACL_XATTR_DEFAULT;
819 break;
820 default:
821 return ERR_PTR(-EINVAL);
822 }
823
824 size = zpl_xattr_get(ip, name, NULL, 0);
825 if (size > 0) {
826 value = kmem_alloc(size, KM_PUSHPAGE);
827 size = zpl_xattr_get(ip, name, value, size);
828 }
829
830 if (size > 0) {
831 acl = zpl_acl_from_xattr(value, size);
832 } else if (size == -ENODATA || size == -ENOSYS) {
833 acl = NULL;
834 } else {
835 acl = ERR_PTR(-EIO);
836 }
837
838 if (size > 0)
839 kmem_free(value, size);
840
841 if (!IS_ERR(acl))
842 zpl_set_cached_acl(ip, type, acl);
843
844 return (acl);
845 }
846
847 #if !defined(HAVE_GET_ACL)
848 static int
849 __zpl_check_acl(struct inode *ip, int mask)
850 {
851 struct posix_acl *acl;
852 int error;
853
854 acl = zpl_get_acl(ip, ACL_TYPE_ACCESS);
855 if (IS_ERR(acl))
856 return (PTR_ERR(acl));
857
858 if (acl) {
859 error = posix_acl_permission(ip, acl, mask);
860 zpl_posix_acl_release(acl);
861 return (error);
862 }
863
864 return (-EAGAIN);
865 }
866
867 #if defined(HAVE_CHECK_ACL_WITH_FLAGS)
868 int
869 zpl_check_acl(struct inode *ip, int mask, unsigned int flags)
870 {
871 return __zpl_check_acl(ip, mask);
872 }
873 #elif defined(HAVE_CHECK_ACL)
874 int
875 zpl_check_acl(struct inode *ip, int mask)
876 {
877 return __zpl_check_acl(ip , mask);
878 }
879 #elif defined(HAVE_PERMISSION_WITH_NAMEIDATA)
880 int
881 zpl_permission(struct inode *ip, int mask, struct nameidata *nd)
882 {
883 return generic_permission(ip, mask, __zpl_check_acl);
884 }
885 #elif defined(HAVE_PERMISSION)
886 int
887 zpl_permission(struct inode *ip, int mask)
888 {
889 return generic_permission(ip, mask, __zpl_check_acl);
890 }
891 #endif /* HAVE_CHECK_ACL | HAVE_PERMISSION */
892 #endif /* !HAVE_GET_ACL */
893
894 int
895 zpl_init_acl(struct inode *ip, struct inode *dir)
896 {
897 struct posix_acl *acl = NULL;
898 int error = 0;
899
900 if (ITOZSB(ip)->z_acl_type != ZFS_ACLTYPE_POSIXACL)
901 return (0);
902
903 if (!S_ISLNK(ip->i_mode)) {
904 if (ITOZSB(ip)->z_acl_type == ZFS_ACLTYPE_POSIXACL) {
905 acl = zpl_get_acl(dir, ACL_TYPE_DEFAULT);
906 if (IS_ERR(acl))
907 return (PTR_ERR(acl));
908 }
909
910 if (!acl) {
911 ip->i_mode &= ~current_umask();
912 ip->i_ctime = current_fs_time(ITOZSB(ip)->z_sb);
913 mark_inode_dirty(ip);
914 return (0);
915 }
916 }
917
918 if ((ITOZSB(ip)->z_acl_type == ZFS_ACLTYPE_POSIXACL) && acl) {
919 umode_t mode;
920
921 if (S_ISDIR(ip->i_mode)) {
922 error = zpl_set_acl(ip, ACL_TYPE_DEFAULT, acl);
923 if (error)
924 goto out;
925 }
926
927 mode = ip->i_mode;
928 error = posix_acl_create(&acl,GFP_KERNEL, &mode);
929 if (error >= 0) {
930 ip->i_mode = mode;
931 mark_inode_dirty(ip);
932 if (error > 0)
933 error = zpl_set_acl(ip, ACL_TYPE_ACCESS, acl);
934 }
935 }
936 out:
937 zpl_posix_acl_release(acl);
938
939 return (error);
940 }
941
942 int
943 zpl_chmod_acl(struct inode *ip)
944 {
945 struct posix_acl *acl;
946 int error;
947
948 if (ITOZSB(ip)->z_acl_type != ZFS_ACLTYPE_POSIXACL)
949 return (0);
950
951 if (S_ISLNK(ip->i_mode))
952 return (-EOPNOTSUPP);
953
954 acl = zpl_get_acl(ip, ACL_TYPE_ACCESS);
955 if (IS_ERR(acl) || !acl)
956 return (PTR_ERR(acl));
957
958 error = posix_acl_chmod(&acl,GFP_KERNEL, ip->i_mode);
959 if (!error)
960 error = zpl_set_acl(ip,ACL_TYPE_ACCESS, acl);
961
962 zpl_posix_acl_release(acl);
963
964 return (error);
965 }
966
967 static size_t
968 zpl_xattr_acl_list(struct inode *ip, char *list, size_t list_size,
969 const char *name, size_t name_len, int type)
970 {
971 char *xattr_name;
972 size_t xattr_size;
973
974 if (ITOZSB(ip)->z_acl_type != ZFS_ACLTYPE_POSIXACL)
975 return (0);
976
977 switch (type) {
978 case ACL_TYPE_ACCESS:
979 xattr_name = POSIX_ACL_XATTR_ACCESS;
980 xattr_size = sizeof(xattr_name);
981 break;
982 case ACL_TYPE_DEFAULT:
983 xattr_name = POSIX_ACL_XATTR_DEFAULT;
984 xattr_size = sizeof(xattr_name);
985 break;
986 default:
987 return (0);
988 }
989
990 if (list && xattr_size <= list_size)
991 memcpy(list, xattr_name, xattr_size);
992
993 return (xattr_size);
994 }
995
996 #ifdef HAVE_DENTRY_XATTR_LIST
997 static size_t
998 zpl_xattr_acl_list_access(struct dentry *dentry, char *list,
999 size_t list_size, const char *name, size_t name_len, int type)
1000 {
1001 ASSERT3S(type, ==, ACL_TYPE_ACCESS);
1002 return zpl_xattr_acl_list(dentry->d_inode,
1003 list, list_size, name, name_len, type);
1004 }
1005
1006 static size_t
1007 zpl_xattr_acl_list_default(struct dentry *dentry, char *list,
1008 size_t list_size, const char *name, size_t name_len, int type)
1009 {
1010 ASSERT3S(type, ==, ACL_TYPE_DEFAULT);
1011 return zpl_xattr_acl_list(dentry->d_inode,
1012 list, list_size, name, name_len, type);
1013 }
1014
1015 #else
1016
1017 static size_t
1018 zpl_xattr_acl_list_access(struct inode *ip, char *list, size_t list_size,
1019 const char *name, size_t name_len)
1020 {
1021 return zpl_xattr_acl_list(ip,
1022 list, list_size, name, name_len, ACL_TYPE_ACCESS);
1023 }
1024
1025 static size_t
1026 zpl_xattr_acl_list_default(struct inode *ip, char *list, size_t list_size,
1027 const char *name, size_t name_len)
1028 {
1029 return zpl_xattr_acl_list(ip,
1030 list, list_size, name, name_len, ACL_TYPE_DEFAULT);
1031 }
1032 #endif /* HAVE_DENTRY_XATTR_LIST */
1033
1034 static int
1035 zpl_xattr_acl_get(struct inode *ip, const char *name,
1036 void *buffer, size_t size, int type)
1037 {
1038 struct posix_acl *acl;
1039 int error;
1040
1041 if (strcmp(name, "") != 0)
1042 return (-EINVAL);
1043
1044 if (ITOZSB(ip)->z_acl_type != ZFS_ACLTYPE_POSIXACL)
1045 return (-EOPNOTSUPP);
1046
1047 acl = zpl_get_acl(ip, type);
1048 if (IS_ERR(acl))
1049 return (PTR_ERR(acl));
1050 if (acl == NULL)
1051 return (-ENODATA);
1052
1053 error = zpl_acl_to_xattr(acl, buffer, size);
1054 zpl_posix_acl_release(acl);
1055
1056 return (error);
1057 }
1058
1059 #ifdef HAVE_DENTRY_XATTR_GET
1060 static int
1061 zpl_xattr_acl_get_access(struct dentry *dentry, const char *name,
1062 void *buffer, size_t size, int type)
1063 {
1064 ASSERT3S(type, ==, ACL_TYPE_ACCESS);
1065 return zpl_xattr_acl_get(dentry->d_inode, name, buffer, size, type);
1066 }
1067
1068 static int
1069 zpl_xattr_acl_get_default(struct dentry *dentry, const char *name,
1070 void *buffer, size_t size, int type)
1071 {
1072 ASSERT3S(type, ==, ACL_TYPE_DEFAULT);
1073 return zpl_xattr_acl_get(dentry->d_inode, name, buffer, size, type);
1074 }
1075
1076 #else
1077
1078 static int
1079 zpl_xattr_acl_get_access(struct inode *ip, const char *name,
1080 void *buffer, size_t size)
1081 {
1082 return zpl_xattr_acl_get(ip, name, buffer, size, ACL_TYPE_ACCESS);
1083 }
1084
1085 static int
1086 zpl_xattr_acl_get_default(struct inode *ip, const char *name,
1087 void *buffer, size_t size)
1088 {
1089 return zpl_xattr_acl_get(ip, name, buffer, size, ACL_TYPE_DEFAULT);
1090 }
1091 #endif /* HAVE_DENTRY_XATTR_GET */
1092
1093 static int
1094 zpl_xattr_acl_set(struct inode *ip, const char *name,
1095 const void *value, size_t size, int flags, int type)
1096 {
1097 struct posix_acl *acl;
1098 int error = 0;
1099
1100 if (strcmp(name, "") != 0)
1101 return (-EINVAL);
1102
1103 if (ITOZSB(ip)->z_acl_type != ZFS_ACLTYPE_POSIXACL)
1104 return (-EOPNOTSUPP);
1105
1106 if (!zpl_inode_owner_or_capable(ip))
1107 return (-EPERM);
1108
1109 if (value) {
1110 acl = zpl_acl_from_xattr(value, size);
1111 if (IS_ERR(acl))
1112 return (PTR_ERR(acl));
1113 else if (acl) {
1114 error = posix_acl_valid(acl);
1115 if (error) {
1116 zpl_posix_acl_release(acl);
1117 return (error);
1118 }
1119 }
1120 } else {
1121 acl = NULL;
1122 }
1123
1124 error = zpl_set_acl(ip, type, acl);
1125 zpl_posix_acl_release(acl);
1126
1127 return (error);
1128 }
1129
1130 #ifdef HAVE_DENTRY_XATTR_SET
1131 static int
1132 zpl_xattr_acl_set_access(struct dentry *dentry, const char *name,
1133 const void *value, size_t size, int flags, int type)
1134 {
1135 ASSERT3S(type, ==, ACL_TYPE_ACCESS);
1136 return zpl_xattr_acl_set(dentry->d_inode,
1137 name, value, size, flags, type);
1138 }
1139
1140 static int
1141 zpl_xattr_acl_set_default(struct dentry *dentry, const char *name,
1142 const void *value, size_t size,int flags, int type)
1143 {
1144 ASSERT3S(type, ==, ACL_TYPE_DEFAULT);
1145 return zpl_xattr_acl_set(dentry->d_inode,
1146 name, value, size, flags, type);
1147 }
1148
1149 #else
1150
1151 static int
1152 zpl_xattr_acl_set_access(struct inode *ip, const char *name,
1153 const void *value, size_t size, int flags)
1154 {
1155 return zpl_xattr_acl_set(ip,
1156 name, value, size, flags, ACL_TYPE_ACCESS);
1157 }
1158
1159 static int
1160 zpl_xattr_acl_set_default(struct inode *ip, const char *name,
1161 const void *value, size_t size, int flags)
1162 {
1163 return zpl_xattr_acl_set(ip,
1164 name, value, size, flags, ACL_TYPE_DEFAULT);
1165 }
1166 #endif /* HAVE_DENTRY_XATTR_SET */
1167
1168 struct xattr_handler zpl_xattr_acl_access_handler =
1169 {
1170 .prefix = POSIX_ACL_XATTR_ACCESS,
1171 .list = zpl_xattr_acl_list_access,
1172 .get = zpl_xattr_acl_get_access,
1173 .set = zpl_xattr_acl_set_access,
1174 #ifdef HAVE_DENTRY_XATTR_LIST
1175 .flags = ACL_TYPE_ACCESS,
1176 #endif /* HAVE_DENTRY_XATTR_LIST */
1177 };
1178
1179 struct xattr_handler zpl_xattr_acl_default_handler =
1180 {
1181 .prefix = POSIX_ACL_XATTR_DEFAULT,
1182 .list = zpl_xattr_acl_list_default,
1183 .get = zpl_xattr_acl_get_default,
1184 .set = zpl_xattr_acl_set_default,
1185 #ifdef HAVE_DENTRY_XATTR_LIST
1186 .flags = ACL_TYPE_DEFAULT,
1187 #endif /* HAVE_DENTRY_XATTR_LIST */
1188 };
1189
1190 xattr_handler_t *zpl_xattr_handlers[] = {
1191 &zpl_xattr_security_handler,
1192 &zpl_xattr_trusted_handler,
1193 &zpl_xattr_user_handler,
1194 &zpl_xattr_acl_access_handler,
1195 &zpl_xattr_acl_default_handler,
1196 NULL
1197 };