Use directory xattrs for symlinks
[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 error;
359         const int xattr_mode = S_IFREG | 0644;
360
361         /* Lookup the xattr directory and create it if required. */
362         error = -zfs_lookup(ip, NULL, &dxip, LOOKUP_XATTR | CREATE_XATTR_DIR,
363             cr, NULL, NULL);
364         if (error)
365                 goto out;
366
367         /* Lookup a specific xattr name in the directory */
368         error = -zfs_lookup(dxip, (char *)name, &xip, 0, cr, NULL, NULL);
369         if (error && (error != -ENOENT))
370                 goto out;
371
372         error = 0;
373
374         /* Remove a specific name xattr when value is set to NULL. */
375         if (value == NULL) {
376                 if (xip)
377                         error = -zfs_remove(dxip, (char *)name, cr);
378
379                 goto out;
380         }
381
382         /* Lookup failed create a new xattr. */
383         if (xip == NULL) {
384                 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
385                 vap->va_mode = xattr_mode;
386                 vap->va_mask = ATTR_MODE;
387                 vap->va_uid = crgetfsuid(cr);
388                 vap->va_gid = crgetfsgid(cr);
389
390                 error = -zfs_create(dxip, (char *)name, vap, 0, 0644, &xip,
391                     cr, 0, NULL);
392                 if (error)
393                         goto out;
394         }
395
396         ASSERT(xip != NULL);
397
398         error = -zfs_freesp(ITOZ(xip), 0, 0, xattr_mode, TRUE);
399         if (error)
400                 goto out;
401
402         wrote = zpl_write_common(xip, value, size, 0, UIO_SYSSPACE, 0, cr);
403         if (wrote < 0)
404                 error = wrote;
405
406 out:
407         if (vap)
408                 kmem_free(vap, sizeof(vattr_t));
409
410         if (xip)
411                 iput(xip);
412
413         if (dxip)
414                 iput(dxip);
415
416         if (error == -ENOENT)
417                 error = -ENODATA;
418
419         ASSERT3S(error, <=, 0);
420
421         return (error);
422 }
423
424 static int
425 zpl_xattr_set_sa(struct inode *ip, const char *name, const void *value,
426     size_t size, int flags, cred_t *cr)
427 {
428         znode_t *zp = ITOZ(ip);
429         nvlist_t *nvl;
430         size_t sa_size;
431         int error;
432
433         ASSERT(zp->z_xattr_cached);
434         nvl = zp->z_xattr_cached;
435
436         if (value == NULL) {
437                 error = -nvlist_remove(nvl, name, DATA_TYPE_BYTE_ARRAY);
438                 if (error == -ENOENT)
439                         error = zpl_xattr_set_dir(ip, name, NULL, 0, flags, cr);
440         } else {
441                 /* Do not allow SA xattrs in symlinks (issue #1648) */
442                 if (S_ISLNK(ip->i_mode))
443                         return (-EMLINK);
444
445                 /* Limited to 32k to keep nvpair memory allocations small */
446                 if (size > DXATTR_MAX_ENTRY_SIZE)
447                         return (-EFBIG);
448
449                 /* Prevent the DXATTR SA from consuming the entire SA region */
450                 error = -nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
451                 if (error)
452                         return (error);
453
454                 if (sa_size > DXATTR_MAX_SA_SIZE)
455                         return (-EFBIG);
456
457                 error = -nvlist_add_byte_array(nvl, name,
458                     (uchar_t *)value, size);
459                 if (error)
460                         return (error);
461         }
462
463         /* Update the SA for additions, modifications, and removals. */
464         if (!error)
465                 error = -zfs_sa_set_xattr(zp);
466
467         ASSERT3S(error, <=, 0);
468
469         return (error);
470 }
471
472 static int
473 zpl_xattr_set(struct inode *ip, const char *name, const void *value,
474     size_t size, int flags)
475 {
476         znode_t *zp = ITOZ(ip);
477         zfs_sb_t *zsb = ZTOZSB(zp);
478         cred_t *cr = CRED();
479         int error;
480
481         crhold(cr);
482         rw_enter(&ITOZ(ip)->z_xattr_lock, RW_WRITER);
483
484         /*
485          * Before setting the xattr check to see if it already exists.
486          * This is done to ensure the following optional flags are honored.
487          *
488          *   XATTR_CREATE: fail if xattr already exists
489          *   XATTR_REPLACE: fail if xattr does not exist
490          */
491         error = __zpl_xattr_get(ip, name, NULL, 0, cr);
492         if (error < 0) {
493                 if (error != -ENODATA)
494                         goto out;
495
496                 if ((error == -ENODATA) && (flags & XATTR_REPLACE))
497                         goto out;
498         } else {
499                 error = -EEXIST;
500                 if (flags & XATTR_CREATE)
501                         goto out;
502         }
503
504         /* Preferentially store the xattr as a SA for better performance */
505         if (zsb->z_use_sa && zsb->z_xattr_sa && zp->z_is_sa) {
506                 error = zpl_xattr_set_sa(ip, name, value, size, flags, cr);
507                 if (error == 0)
508                         goto out;
509         }
510
511         error = zpl_xattr_set_dir(ip, name, value, size, flags, cr);
512 out:
513         rw_exit(&ITOZ(ip)->z_xattr_lock);
514         crfree(cr);
515         ASSERT3S(error, <=, 0);
516
517         return (error);
518 }
519
520 static int
521 __zpl_xattr_user_get(struct inode *ip, const char *name,
522     void *value, size_t size)
523 {
524         char *xattr_name;
525         int error;
526
527         if (strcmp(name, "") == 0)
528                 return -EINVAL;
529
530         if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
531                 return -EOPNOTSUPP;
532
533         xattr_name = kmem_asprintf("%s%s", XATTR_USER_PREFIX, name);
534         error = zpl_xattr_get(ip, xattr_name, value, size);
535         strfree(xattr_name);
536
537         return (error);
538 }
539 ZPL_XATTR_GET_WRAPPER(zpl_xattr_user_get);
540
541 static int
542 __zpl_xattr_user_set(struct inode *ip, const char *name,
543     const void *value, size_t size, int flags)
544 {
545         char *xattr_name;
546         int error;
547
548         if (strcmp(name, "") == 0)
549                 return -EINVAL;
550
551         if (!(ITOZSB(ip)->z_flags & ZSB_XATTR))
552                 return -EOPNOTSUPP;
553
554         xattr_name = kmem_asprintf("%s%s", XATTR_USER_PREFIX, name);
555         error = zpl_xattr_set(ip, xattr_name, value, size, flags);
556         strfree(xattr_name);
557
558         return (error);
559 }
560 ZPL_XATTR_SET_WRAPPER(zpl_xattr_user_set);
561
562 xattr_handler_t zpl_xattr_user_handler = {
563         .prefix = XATTR_USER_PREFIX,
564         .get    = zpl_xattr_user_get,
565         .set    = zpl_xattr_user_set,
566 };
567
568 static int
569 __zpl_xattr_trusted_get(struct inode *ip, const char *name,
570     void *value, size_t size)
571 {
572         char *xattr_name;
573         int error;
574
575         if (!capable(CAP_SYS_ADMIN))
576                 return -EACCES;
577
578         if (strcmp(name, "") == 0)
579                 return -EINVAL;
580
581         xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
582         error = zpl_xattr_get(ip, xattr_name, value, size);
583         strfree(xattr_name);
584
585         return (error);
586 }
587 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get);
588
589 static int
590 __zpl_xattr_trusted_set(struct inode *ip, const char *name,
591     const void *value, size_t size, int flags)
592 {
593         char *xattr_name;
594         int error;
595
596         if (!capable(CAP_SYS_ADMIN))
597                 return -EACCES;
598
599         if (strcmp(name, "") == 0)
600                 return -EINVAL;
601
602         xattr_name = kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX, name);
603         error = zpl_xattr_set(ip, xattr_name, value, size, flags);
604         strfree(xattr_name);
605
606         return (error);
607 }
608 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set);
609
610 xattr_handler_t zpl_xattr_trusted_handler = {
611         .prefix = XATTR_TRUSTED_PREFIX,
612         .get    = zpl_xattr_trusted_get,
613         .set    = zpl_xattr_trusted_set,
614 };
615
616 static int
617 __zpl_xattr_security_get(struct inode *ip, const char *name,
618     void *value, size_t size)
619 {
620         char *xattr_name;
621         int error;
622
623         if (strcmp(name, "") == 0)
624                 return -EINVAL;
625
626         xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
627         error = zpl_xattr_get(ip, xattr_name, value, size);
628         strfree(xattr_name);
629
630         return (error);
631 }
632 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get);
633
634 static int
635 __zpl_xattr_security_set(struct inode *ip, const char *name,
636     const void *value, size_t size, int flags)
637 {
638         char *xattr_name;
639         int error;
640
641         if (strcmp(name, "") == 0)
642                 return -EINVAL;
643
644         xattr_name = kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX, name);
645         error = zpl_xattr_set(ip, xattr_name, value, size, flags);
646         strfree(xattr_name);
647
648         return (error);
649 }
650 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set);
651
652 #ifdef HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY
653 static int
654 __zpl_xattr_security_init(struct inode *ip, const struct xattr *xattrs,
655     void *fs_info)
656 {
657         const struct xattr *xattr;
658         int error = 0;
659
660         for (xattr = xattrs; xattr->name != NULL; xattr++) {
661                 error = __zpl_xattr_security_set(ip,
662                     xattr->name, xattr->value, xattr->value_len, 0);
663
664                 if (error < 0)
665                         break;
666         }
667
668         return (error);
669 }
670
671 int
672 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
673     const struct qstr *qstr)
674 {
675         return security_inode_init_security(ip, dip, qstr,
676             &__zpl_xattr_security_init, NULL);
677 }
678
679 #else
680 int
681 zpl_xattr_security_init(struct inode *ip, struct inode *dip,
682     const struct qstr *qstr)
683 {
684         int error;
685         size_t len;
686         void *value;
687         char *name;
688
689         error = zpl_security_inode_init_security(ip, dip, qstr,
690           &name, &value, &len);
691         if (error) {
692                 if (error == -EOPNOTSUPP)
693                         return 0;
694                 return (error);
695         }
696
697         error = __zpl_xattr_security_set(ip, name, value, len, 0);
698
699         kfree(name);
700         kfree(value);
701
702         return (error);
703 }
704 #endif /* HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY */
705
706 xattr_handler_t zpl_xattr_security_handler = {
707         .prefix = XATTR_SECURITY_PREFIX,
708         .get    = zpl_xattr_security_get,
709         .set    = zpl_xattr_security_set,
710 };
711
712 xattr_handler_t *zpl_xattr_handlers[] = {
713         &zpl_xattr_security_handler,
714         &zpl_xattr_trusted_handler,
715         &zpl_xattr_user_handler,
716 #ifdef HAVE_POSIX_ACLS
717         &zpl_xattr_acl_access_handler,
718         &zpl_xattr_acl_default_handler,
719 #endif /* HAVE_POSIX_ACLS */
720         NULL
721 };