4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * Copyright (c) 2012, 2013, Intel Corporation.
32 * Use is subject to license terms.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd-zfs/osd_xattr.c
39 * functions to manipulate extended attributes and system attributes
41 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42 * Author: Mike Pershin <tappro@whamcloud.com>
45 #define DEBUG_SUBSYSTEM S_OSD
47 #include <lustre_ver.h>
48 #include <libcfs/libcfs.h>
49 #include <obd_support.h>
50 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_disk.h>
54 #include <lustre_fid.h>
56 #include "osd_internal.h"
58 #include <sys/dnode.h>
63 #include <sys/spa_impl.h>
64 #include <sys/zfs_znode.h>
65 #include <sys/dmu_tx.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/dsl_prop.h>
68 #include <sys/sa_impl.h>
71 #include <linux/posix_acl_xattr.h>
74 * Copy an extended attribute into the buffer provided, or compute the
75 * required buffer size.
77 * If buf is NULL, it computes the required buffer size.
79 * Returns 0 on success or a negative error number on failure.
80 * On success, the number of bytes used / required is stored in 'size'.
82 * No locking is done here.
84 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
90 if (unlikely(dnode == ZFS_NO_OBJECT))
93 rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
97 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(osd), &size);
100 rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
104 buf = sa_spill_alloc(KM_SLEEP);
109 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
111 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
114 sa_handle_destroy(sa_hdl);
119 static inline int __osd_xattr_cache(const struct lu_env *env,
120 struct osd_object *obj)
122 LASSERT(obj->oo_sa_xattr == NULL);
123 LASSERT(obj->oo_db != NULL);
125 return __osd_xattr_load(osd_obj2dev(obj), obj->oo_db->db_object,
129 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
130 const struct lu_buf *buf, const char *name, int *sizep)
135 LASSERT(obj->oo_sa_hdl);
137 if (obj->oo_sa_xattr == NULL) {
138 rc = __osd_xattr_cache(env, obj);
143 LASSERT(obj->oo_sa_xattr);
144 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
149 if (buf == NULL || buf->lb_buf == NULL) {
150 /* return the required size by *sizep */
154 if (*sizep > buf->lb_len)
155 return -ERANGE; /* match ldiskfs error */
157 memcpy(buf->lb_buf, nv_value, *sizep);
161 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
162 uint64_t xattr, struct lu_buf *buf,
163 const char *name, int *sizep)
165 dmu_buf_t *xa_data_db;
166 sa_handle_t *sa_hdl = NULL;
167 uint64_t xa_data_obj, size;
170 /* are there any extended attributes? */
171 if (xattr == ZFS_NO_OBJECT)
174 /* Lookup the object number containing the xattr data */
175 rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
180 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj, &xa_data_db);
184 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
189 /* Get the xattr value length / object size */
190 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
194 if (size > INT_MAX) {
201 if (buf == NULL || buf->lb_buf == NULL) {
202 /* We only need to return the required size */
205 if (*sizep > buf->lb_len) {
206 rc = -ERANGE; /* match ldiskfs error */
210 rc = -dmu_read(osd->od_os, xa_data_db->db_object, 0,
211 size, buf->lb_buf, DMU_READ_PREFETCH);
214 sa_handle_destroy(sa_hdl);
216 dmu_buf_rele(xa_data_db, FTAG);
221 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
222 struct lu_buf *buf, const char *name, int *sizep)
226 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
227 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
231 rc = __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
237 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
238 struct lu_buf *buf, const char *name,
239 struct lustre_capa *capa)
241 struct osd_object *obj = osd_dt_obj(dt);
245 LASSERT(obj->oo_db != NULL);
246 LASSERT(osd_invariant(obj));
247 LASSERT(dt_object_exists(dt));
249 if (!osd_obj2dev(obj)->od_posix_acl &&
250 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
251 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
254 down(&obj->oo_guard);
255 rc = __osd_xattr_get(env, obj, buf, name, &size);
265 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
266 int vallen, const char *name,
267 struct osd_thandle *oh)
269 struct osd_device *osd = osd_obj2dev(obj);
270 dmu_buf_t *db = obj->oo_db;
271 dmu_tx_t *tx = oh->ot_tx;
272 uint64_t xa_data_obj;
276 here = dt_object_exists(&obj->oo_dt);
278 /* object may be not yet created */
281 LASSERT(obj->oo_sa_hdl);
282 /* we might just update SA_ZPL_DXATTR */
283 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
285 if (obj->oo_xattr == ZFS_NO_OBJECT)
289 if (!here || rc == -ENOENT) {
290 /* we'll be updating SA_ZPL_XATTR */
292 LASSERT(obj->oo_sa_hdl);
293 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
295 /* xattr zap + entry */
296 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
297 /* xattr value obj */
298 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
299 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
303 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
307 * Entry already exists.
308 * We'll truncate the existing object.
310 dmu_tx_hold_bonus(tx, xa_data_obj);
311 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
312 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
314 } else if (rc == -ENOENT) {
316 * Entry doesn't exist, we need to create a new one and a new
317 * object to store the value.
319 dmu_tx_hold_bonus(tx, obj->oo_xattr);
320 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
321 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
322 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
326 /* An error happened */
330 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
331 const struct lu_buf *buf, const char *name,
332 int fl, struct thandle *handle)
334 struct osd_object *obj = osd_dt_obj(dt);
335 struct osd_thandle *oh;
338 LASSERT(handle != NULL);
339 oh = container_of0(handle, struct osd_thandle, ot_super);
341 down(&obj->oo_guard);
342 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
349 * Set an extended attribute.
350 * This transaction must have called udmu_xattr_declare_set() first.
352 * Returns 0 on success or a negative error number on failure.
354 * No locking is done here.
357 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
358 struct osd_thandle *oh)
360 struct osd_device *osd = osd_obj2dev(obj);
366 LASSERT(obj->oo_sa_hdl);
367 LASSERT(obj->oo_sa_xattr);
369 /* Update the SA for additions, modifications, and removals. */
370 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
374 dxattr = sa_spill_alloc(KM_SLEEP);
378 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
379 NV_ENCODE_XDR, KM_SLEEP);
383 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
385 sa_spill_free(dxattr);
389 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
390 const struct lu_buf *buf, const char *name, int fl,
391 struct osd_thandle *oh)
399 LASSERT(obj->oo_sa_hdl);
400 if (obj->oo_sa_xattr == NULL) {
401 rc = __osd_xattr_cache(env, obj);
406 LASSERT(obj->oo_sa_xattr);
407 /* Limited to 32k to keep nvpair memory allocations small */
408 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
411 /* Prevent the DXATTR SA from consuming the entire SA
413 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
417 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
421 /* even in case of -EFBIG we must lookup xattr and check can we
422 * rewrite it then delete from SA */
423 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
426 if (fl & LU_XATTR_CREATE) {
428 } else if (too_big) {
429 rc = -nvlist_remove(obj->oo_sa_xattr, name,
430 DATA_TYPE_BYTE_ARRAY);
433 rc = __osd_sa_xattr_update(env, obj, oh);
434 return rc == 0 ? -EFBIG : rc;
436 } else if (rc == -ENOENT) {
437 if (fl & LU_XATTR_REPLACE)
445 /* Ensure xattr doesn't exist in ZAP */
446 if (obj->oo_xattr != ZFS_NO_OBJECT) {
447 struct osd_device *osd = osd_obj2dev(obj);
449 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
452 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
454 zap_remove(osd->od_os, obj->oo_xattr,
459 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
460 (uchar_t *)buf->lb_buf, buf->lb_len);
464 rc = __osd_sa_xattr_update(env, obj, oh);
469 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
470 const struct lu_buf *buf, const char *name, int fl,
471 struct osd_thandle *oh)
473 struct osd_device *osd = osd_obj2dev(obj);
474 dmu_buf_t *xa_zap_db = NULL;
475 dmu_buf_t *xa_data_db = NULL;
476 uint64_t xa_data_obj;
477 sa_handle_t *sa_hdl = NULL;
478 dmu_tx_t *tx = oh->ot_tx;
482 LASSERT(obj->oo_sa_hdl);
484 if (obj->oo_xattr == ZFS_NO_OBJECT) {
485 struct lu_attr *la = &osd_oti_get(env)->oti_la;
487 la->la_valid = LA_MODE;
488 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
489 rc = __osd_zap_create(env, osd, &xa_zap_db, tx, la,
490 obj->oo_db->db_object, 0);
494 obj->oo_xattr = xa_zap_db->db_object;
495 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
496 &obj->oo_xattr, 8, oh);
501 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
504 if (fl & LU_XATTR_CREATE) {
509 * Entry already exists.
510 * We'll truncate the existing object.
512 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj,
517 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
518 SA_HDL_PRIVATE, &sa_hdl);
522 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
526 rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
527 0, DMU_OBJECT_END, tx);
530 } else if (rc == -ENOENT) {
531 struct lu_attr *la = &osd_oti_get(env)->oti_la;
533 * Entry doesn't exist, we need to create a new one and a new
534 * object to store the value.
536 if (fl & LU_XATTR_REPLACE) {
537 /* should be ENOATTR according to the
538 * man, but that is undefined here */
543 la->la_valid = LA_MODE;
544 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
545 rc = __osd_object_create(env, osd, &xa_data_db, tx, la,
549 xa_data_obj = xa_data_db->db_object;
551 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
552 SA_HDL_PRIVATE, &sa_hdl);
556 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
557 1, &xa_data_obj, tx);
561 /* There was an error looking up the xattr name */
565 /* Finally write the xattr value */
566 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
569 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
572 sa_handle_destroy(sa_hdl);
574 if (xa_data_db != NULL)
575 dmu_buf_rele(xa_data_db, FTAG);
576 if (xa_zap_db != NULL)
577 dmu_buf_rele(xa_zap_db, FTAG);
582 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
583 const struct lu_buf *buf, const char *name, int fl,
584 struct thandle *handle, struct lustre_capa *capa)
586 struct osd_object *obj = osd_dt_obj(dt);
587 struct osd_thandle *oh;
591 LASSERT(handle != NULL);
592 LASSERT(osd_invariant(obj));
593 LASSERT(dt_object_exists(dt));
596 if (!osd_obj2dev(obj)->od_posix_acl &&
597 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
598 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
601 oh = container_of0(handle, struct osd_thandle, ot_super);
603 down(&obj->oo_guard);
604 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
605 name, (int)buf->lb_len);
606 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
613 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
614 const char *name, struct osd_thandle *oh)
616 struct osd_device *osd = osd_obj2dev(obj);
617 dmu_tx_t *tx = oh->ot_tx;
618 uint64_t xa_data_obj;
621 /* update SA_ZPL_DXATTR if xattr was in SA */
622 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
624 if (obj->oo_xattr == ZFS_NO_OBJECT)
627 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
631 * We'll delete the existing object and ZAP entry.
633 dmu_tx_hold_bonus(tx, xa_data_obj);
634 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
635 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
637 } else if (rc == -ENOENT) {
639 * Entry doesn't exist, nothing to be changed.
644 /* An error happened */
648 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
649 const char *name, struct thandle *handle)
651 struct osd_object *obj = osd_dt_obj(dt);
652 struct osd_thandle *oh;
655 LASSERT(handle != NULL);
656 LASSERT(dt_object_exists(dt));
657 LASSERT(osd_invariant(obj));
659 oh = container_of0(handle, struct osd_thandle, ot_super);
660 LASSERT(oh->ot_tx != NULL);
661 LASSERT(obj->oo_db != NULL);
663 down(&obj->oo_guard);
664 __osd_xattr_declare_del(env, obj, name, oh);
670 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
671 const char *name, struct osd_thandle *oh)
675 if (obj->oo_sa_xattr == NULL) {
676 rc = __osd_xattr_cache(env, obj);
681 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
683 rc = __osd_sa_xattr_update(env, obj, oh);
687 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
688 const char *name, struct osd_thandle *oh)
690 struct osd_device *osd = osd_obj2dev(obj);
691 uint64_t xa_data_obj;
694 /* try remove xattr from SA at first */
695 rc = __osd_sa_xattr_del(env, obj, name, oh);
699 if (obj->oo_xattr == ZFS_NO_OBJECT)
702 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
706 } else if (rc == 0) {
709 * We'll delete the existing object and ZAP entry.
711 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
715 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
721 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
722 const char *name, struct thandle *handle,
723 struct lustre_capa *capa)
725 struct osd_object *obj = osd_dt_obj(dt);
726 struct osd_thandle *oh;
730 LASSERT(handle != NULL);
731 LASSERT(obj->oo_db != NULL);
732 LASSERT(osd_invariant(obj));
733 LASSERT(dt_object_exists(dt));
734 oh = container_of0(handle, struct osd_thandle, ot_super);
735 LASSERT(oh->ot_tx != NULL);
737 if (!osd_obj2dev(obj)->od_posix_acl &&
738 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
739 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
742 down(&obj->oo_guard);
743 rc = __osd_xattr_del(env, obj, name, oh);
750 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
753 nvpair_t *nvp = NULL;
754 int len, counted = 0, remain = lb->lb_len;
757 if (obj->oo_sa_xattr == NULL) {
758 rc = __osd_xattr_cache(env, obj);
763 LASSERT(obj->oo_sa_xattr);
765 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
766 const char *name = nvpair_name(nvp);
768 if (!osd_obj2dev(obj)->od_posix_acl &&
769 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
770 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
773 len = strlen(nvpair_name(nvp));
774 if (lb->lb_buf != NULL) {
775 if (len + 1 > remain)
778 memcpy(lb->lb_buf, name, len);
780 *((char *)lb->lb_buf) = '\0';
789 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
790 struct lu_buf *lb, struct lustre_capa *capa)
792 struct osd_object *obj = osd_dt_obj(dt);
793 struct osd_device *osd = osd_obj2dev(obj);
794 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
796 int rc, counted = 0, remain = lb->lb_len;
799 LASSERT(obj->oo_db != NULL);
800 LASSERT(osd_invariant(obj));
801 LASSERT(dt_object_exists(dt));
803 down(&obj->oo_guard);
805 rc = osd_sa_xattr_list(env, obj, lb);
811 /* continue with dnode xattr if any */
812 if (obj->oo_xattr == ZFS_NO_OBJECT)
813 GOTO(out, rc = counted);
815 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
819 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
820 if (!osd_obj2dev(obj)->od_posix_acl &&
821 (strcmp(za->za_name, POSIX_ACL_XATTR_ACCESS) == 0 ||
822 strcmp(za->za_name, POSIX_ACL_XATTR_DEFAULT) == 0)) {
823 zap_cursor_advance(zc);
827 rc = strlen(za->za_name);
828 if (lb->lb_buf != NULL) {
832 memcpy(lb->lb_buf, za->za_name, rc);
834 *((char *)lb->lb_buf) = '\0';
840 zap_cursor_advance(zc);
842 if (rc == -ENOENT) /* no more kes in the index */
844 else if (unlikely(rc < 0))
849 osd_zap_cursor_fini(zc);