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 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
80 if (unlikely(dnode == ZFS_NO_OBJECT))
83 rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
87 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(osd), &size);
90 rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
94 buf = sa_spill_alloc(KM_SLEEP);
99 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
101 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
104 sa_handle_destroy(sa_hdl);
109 static inline int __osd_xattr_cache(const struct lu_env *env,
110 struct osd_object *obj)
112 LASSERT(obj->oo_sa_xattr == NULL);
113 LASSERT(obj->oo_db != NULL);
115 return __osd_xattr_load(osd_obj2dev(obj), obj->oo_db->db_object,
120 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
121 const struct lu_buf *buf, const char *name, int *sizep)
126 LASSERT(obj->oo_sa_hdl);
128 if (obj->oo_sa_xattr == NULL) {
129 rc = __osd_xattr_cache(env, obj);
134 LASSERT(obj->oo_sa_xattr);
135 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
140 if (buf == NULL || buf->lb_buf == NULL) {
141 /* return the required size by *sizep */
145 if (*sizep > buf->lb_len)
146 return -ERANGE; /* match ldiskfs error */
148 memcpy(buf->lb_buf, nv_value, *sizep);
152 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
153 uint64_t xattr, struct lu_buf *buf,
154 const char *name, int *sizep)
156 dmu_buf_t *xa_data_db;
157 sa_handle_t *sa_hdl = NULL;
158 uint64_t xa_data_obj, size;
161 /* are there any extended attributes? */
162 if (xattr == ZFS_NO_OBJECT)
165 /* Lookup the object number containing the xattr data */
166 rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
171 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj, &xa_data_db);
175 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
180 /* Get the xattr value length / object size */
181 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
185 if (size > INT_MAX) {
192 if (buf == NULL || buf->lb_buf == NULL) {
193 /* We only need to return the required size */
196 if (*sizep > buf->lb_len) {
197 rc = -ERANGE; /* match ldiskfs error */
201 rc = -dmu_read(osd->od_os, xa_data_db->db_object, 0,
202 size, buf->lb_buf, DMU_READ_PREFETCH);
205 sa_handle_destroy(sa_hdl);
207 dmu_buf_rele(xa_data_db, FTAG);
213 * Copy an extended attribute into the buffer provided, or compute
214 * the required buffer size if \a buf is NULL.
216 * On success, the number of bytes used or required is stored in \a sizep.
218 * Note that no locking is done here.
220 * \param[in] env execution environment
221 * \param[in] obj object for which to retrieve xattr
222 * \param[out] buf buffer to store xattr value in
223 * \param[in] name name of xattr to copy
224 * \param[out] sizep bytes used or required to store xattr
226 * \retval 0 on success
227 * \retval negative negated errno on failure
229 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
230 struct lu_buf *buf, const char *name, int *sizep)
234 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
235 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
239 return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
243 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
244 struct lu_buf *buf, const char *name,
245 struct lustre_capa *capa)
247 struct osd_object *obj = osd_dt_obj(dt);
251 LASSERT(obj->oo_db != NULL);
252 LASSERT(osd_invariant(obj));
253 LASSERT(dt_object_exists(dt));
255 if (!osd_obj2dev(obj)->od_posix_acl &&
256 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
257 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
260 down(&obj->oo_guard);
261 rc = __osd_xattr_get(env, obj, buf, name, &size);
271 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
272 int vallen, const char *name,
273 struct osd_thandle *oh)
275 struct osd_device *osd = osd_obj2dev(obj);
276 dmu_buf_t *db = obj->oo_db;
277 dmu_tx_t *tx = oh->ot_tx;
278 uint64_t xa_data_obj;
282 here = dt_object_exists(&obj->oo_dt);
284 /* object may be not yet created */
287 LASSERT(obj->oo_sa_hdl);
288 /* we might just update SA_ZPL_DXATTR */
289 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
291 if (obj->oo_xattr == ZFS_NO_OBJECT)
295 if (!here || rc == -ENOENT) {
296 /* we'll be updating SA_ZPL_XATTR */
298 LASSERT(obj->oo_sa_hdl);
299 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
301 /* xattr zap + entry */
302 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
303 /* xattr value obj */
304 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
305 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
309 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
313 * Entry already exists.
314 * We'll truncate the existing object.
316 dmu_tx_hold_bonus(tx, xa_data_obj);
317 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
318 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
320 } else if (rc == -ENOENT) {
322 * Entry doesn't exist, we need to create a new one and a new
323 * object to store the value.
325 dmu_tx_hold_bonus(tx, obj->oo_xattr);
326 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
327 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
328 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
332 /* An error happened */
336 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
337 const struct lu_buf *buf, const char *name,
338 int fl, struct thandle *handle)
340 struct osd_object *obj = osd_dt_obj(dt);
341 struct osd_thandle *oh;
344 LASSERT(handle != NULL);
345 oh = container_of0(handle, struct osd_thandle, ot_super);
347 down(&obj->oo_guard);
348 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
355 * Set an extended attribute.
356 * This transaction must have called udmu_xattr_declare_set() first.
358 * Returns 0 on success or a negative error number on failure.
360 * No locking is done here.
363 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
364 struct osd_thandle *oh)
366 struct osd_device *osd = osd_obj2dev(obj);
372 LASSERT(obj->oo_sa_hdl);
373 LASSERT(obj->oo_sa_xattr);
375 /* Update the SA for additions, modifications, and removals. */
376 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
380 dxattr = sa_spill_alloc(KM_SLEEP);
384 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
385 NV_ENCODE_XDR, KM_SLEEP);
389 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
391 sa_spill_free(dxattr);
395 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
396 const struct lu_buf *buf, const char *name, int fl,
397 struct osd_thandle *oh)
405 LASSERT(obj->oo_sa_hdl);
406 if (obj->oo_sa_xattr == NULL) {
407 rc = __osd_xattr_cache(env, obj);
412 LASSERT(obj->oo_sa_xattr);
413 /* Limited to 32k to keep nvpair memory allocations small */
414 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
417 /* Prevent the DXATTR SA from consuming the entire SA
419 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
423 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
427 /* even in case of -EFBIG we must lookup xattr and check can we
428 * rewrite it then delete from SA */
429 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
432 if (fl & LU_XATTR_CREATE) {
434 } else if (too_big) {
435 rc = -nvlist_remove(obj->oo_sa_xattr, name,
436 DATA_TYPE_BYTE_ARRAY);
439 rc = __osd_sa_xattr_update(env, obj, oh);
440 return rc == 0 ? -EFBIG : rc;
442 } else if (rc == -ENOENT) {
443 if (fl & LU_XATTR_REPLACE)
451 /* Ensure xattr doesn't exist in ZAP */
452 if (obj->oo_xattr != ZFS_NO_OBJECT) {
453 struct osd_device *osd = osd_obj2dev(obj);
455 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
458 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
460 zap_remove(osd->od_os, obj->oo_xattr,
465 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
466 (uchar_t *)buf->lb_buf, buf->lb_len);
470 rc = __osd_sa_xattr_update(env, obj, oh);
475 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
476 const struct lu_buf *buf, const char *name, int fl,
477 struct osd_thandle *oh)
479 struct osd_device *osd = osd_obj2dev(obj);
480 dmu_buf_t *xa_zap_db = NULL;
481 dmu_buf_t *xa_data_db = NULL;
482 uint64_t xa_data_obj;
483 sa_handle_t *sa_hdl = NULL;
484 dmu_tx_t *tx = oh->ot_tx;
488 LASSERT(obj->oo_sa_hdl);
490 if (obj->oo_xattr == ZFS_NO_OBJECT) {
491 struct lu_attr *la = &osd_oti_get(env)->oti_la;
493 la->la_valid = LA_MODE;
494 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
495 rc = __osd_zap_create(env, osd, &xa_zap_db, tx, la,
496 obj->oo_db->db_object, 0);
500 obj->oo_xattr = xa_zap_db->db_object;
501 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
502 &obj->oo_xattr, 8, oh);
507 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
510 if (fl & LU_XATTR_CREATE) {
515 * Entry already exists.
516 * We'll truncate the existing object.
518 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj,
523 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
524 SA_HDL_PRIVATE, &sa_hdl);
528 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
532 rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
533 0, DMU_OBJECT_END, tx);
536 } else if (rc == -ENOENT) {
537 struct lu_attr *la = &osd_oti_get(env)->oti_la;
539 * Entry doesn't exist, we need to create a new one and a new
540 * object to store the value.
542 if (fl & LU_XATTR_REPLACE) {
543 /* should be ENOATTR according to the
544 * man, but that is undefined here */
549 la->la_valid = LA_MODE;
550 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
551 rc = __osd_object_create(env, osd, &xa_data_db, tx, la,
555 xa_data_obj = xa_data_db->db_object;
557 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
558 SA_HDL_PRIVATE, &sa_hdl);
562 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
563 1, &xa_data_obj, tx);
567 /* There was an error looking up the xattr name */
571 /* Finally write the xattr value */
572 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
575 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
578 sa_handle_destroy(sa_hdl);
580 if (xa_data_db != NULL)
581 dmu_buf_rele(xa_data_db, FTAG);
582 if (xa_zap_db != NULL)
583 dmu_buf_rele(xa_zap_db, FTAG);
588 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
589 const struct lu_buf *buf, const char *name, int fl,
590 struct thandle *handle, struct lustre_capa *capa)
592 struct osd_object *obj = osd_dt_obj(dt);
593 struct osd_thandle *oh;
597 LASSERT(handle != NULL);
598 LASSERT(osd_invariant(obj));
599 LASSERT(dt_object_exists(dt));
602 if (!osd_obj2dev(obj)->od_posix_acl &&
603 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
604 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
607 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
608 strcmp(name, XATTR_NAME_LINK) == 0)
611 oh = container_of0(handle, struct osd_thandle, ot_super);
613 down(&obj->oo_guard);
614 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
615 name, (int)buf->lb_len);
616 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
623 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
624 const char *name, struct osd_thandle *oh)
626 struct osd_device *osd = osd_obj2dev(obj);
627 dmu_tx_t *tx = oh->ot_tx;
628 uint64_t xa_data_obj;
631 /* update SA_ZPL_DXATTR if xattr was in SA */
632 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
634 if (obj->oo_xattr == ZFS_NO_OBJECT)
637 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
641 * We'll delete the existing object and ZAP entry.
643 dmu_tx_hold_bonus(tx, xa_data_obj);
644 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
645 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
647 } else if (rc == -ENOENT) {
649 * Entry doesn't exist, nothing to be changed.
654 /* An error happened */
658 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
659 const char *name, struct thandle *handle)
661 struct osd_object *obj = osd_dt_obj(dt);
662 struct osd_thandle *oh;
665 LASSERT(handle != NULL);
666 LASSERT(dt_object_exists(dt));
667 LASSERT(osd_invariant(obj));
669 oh = container_of0(handle, struct osd_thandle, ot_super);
670 LASSERT(oh->ot_tx != NULL);
671 LASSERT(obj->oo_db != NULL);
673 down(&obj->oo_guard);
674 __osd_xattr_declare_del(env, obj, name, oh);
680 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
681 const char *name, struct osd_thandle *oh)
685 if (obj->oo_sa_xattr == NULL) {
686 rc = __osd_xattr_cache(env, obj);
691 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
693 rc = __osd_sa_xattr_update(env, obj, oh);
697 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
698 const char *name, struct osd_thandle *oh)
700 struct osd_device *osd = osd_obj2dev(obj);
701 uint64_t xa_data_obj;
704 /* try remove xattr from SA at first */
705 rc = __osd_sa_xattr_del(env, obj, name, oh);
709 if (obj->oo_xattr == ZFS_NO_OBJECT)
712 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
716 } else if (rc == 0) {
719 * We'll delete the existing object and ZAP entry.
721 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
725 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
731 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
732 const char *name, struct thandle *handle,
733 struct lustre_capa *capa)
735 struct osd_object *obj = osd_dt_obj(dt);
736 struct osd_thandle *oh;
740 LASSERT(handle != NULL);
741 LASSERT(obj->oo_db != NULL);
742 LASSERT(osd_invariant(obj));
743 LASSERT(dt_object_exists(dt));
744 oh = container_of0(handle, struct osd_thandle, ot_super);
745 LASSERT(oh->ot_tx != NULL);
747 if (!osd_obj2dev(obj)->od_posix_acl &&
748 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
749 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
752 down(&obj->oo_guard);
753 rc = __osd_xattr_del(env, obj, name, oh);
760 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
763 nvpair_t *nvp = NULL;
764 int len, counted = 0, remain = lb->lb_len;
767 if (obj->oo_sa_xattr == NULL) {
768 rc = __osd_xattr_cache(env, obj);
773 LASSERT(obj->oo_sa_xattr);
775 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
776 const char *name = nvpair_name(nvp);
778 if (!osd_obj2dev(obj)->od_posix_acl &&
779 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
780 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
783 len = strlen(nvpair_name(nvp));
784 if (lb->lb_buf != NULL) {
785 if (len + 1 > remain)
788 memcpy(lb->lb_buf, name, len);
790 *((char *)lb->lb_buf) = '\0';
799 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
800 struct lu_buf *lb, struct lustre_capa *capa)
802 struct osd_object *obj = osd_dt_obj(dt);
803 struct osd_device *osd = osd_obj2dev(obj);
804 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
806 int rc, counted = 0, remain = lb->lb_len;
809 LASSERT(obj->oo_db != NULL);
810 LASSERT(osd_invariant(obj));
811 LASSERT(dt_object_exists(dt));
813 down(&obj->oo_guard);
815 rc = osd_sa_xattr_list(env, obj, lb);
821 /* continue with dnode xattr if any */
822 if (obj->oo_xattr == ZFS_NO_OBJECT)
823 GOTO(out, rc = counted);
825 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
829 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
830 if (!osd_obj2dev(obj)->od_posix_acl &&
831 (strcmp(za->za_name, POSIX_ACL_XATTR_ACCESS) == 0 ||
832 strcmp(za->za_name, POSIX_ACL_XATTR_DEFAULT) == 0)) {
833 zap_cursor_advance(zc);
837 rc = strlen(za->za_name);
838 if (lb->lb_buf != NULL) {
842 memcpy(lb->lb_buf, za->za_name, rc);
844 *((char *)lb->lb_buf) = '\0';
850 zap_cursor_advance(zc);
852 if (rc == -ENOENT) /* no more kes in the index */
854 else if (unlikely(rc < 0))
859 osd_zap_cursor_fini(zc);