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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_xattr.c
33 * functions to manipulate extended attributes and system attributes
35 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36 * Author: Mike Pershin <tappro@whamcloud.com>
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
49 #include "osd_internal.h"
51 #include <sys/dnode.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
64 #include <linux/posix_acl_xattr.h>
67 int __osd_xattr_load(struct osd_device *osd, sa_handle_t *hdl, nvlist_t **sa)
72 rc = -sa_size(hdl, SA_ZPL_DXATTR(osd), &size);
75 rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
79 buf = osd_zio_buf_alloc(size);
84 rc = -sa_lookup(hdl, SA_ZPL_DXATTR(osd), buf, size);
86 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
87 osd_zio_buf_free(buf, size);
93 static inline int __osd_xattr_cache(struct osd_object *obj)
95 LASSERT(obj->oo_sa_hdl);
96 if (obj->oo_sa_xattr != NULL)
98 return __osd_xattr_load(osd_obj2dev(obj),
99 obj->oo_sa_hdl, &obj->oo_sa_xattr);
103 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
104 const struct lu_buf *buf, const char *name, int *sizep)
109 rc = __osd_xattr_cache(obj);
113 LASSERT(obj->oo_sa_xattr);
114 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name,
119 if (buf == NULL || buf->lb_buf == NULL) {
120 /* return the required size by *sizep */
124 if (*sizep > buf->lb_len)
125 return -ERANGE; /* match ldiskfs error */
127 memcpy(buf->lb_buf, nv_value, *sizep);
131 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
132 uint64_t xattr, struct lu_buf *buf,
133 const char *name, int *sizep)
136 sa_handle_t *sa_hdl = NULL;
137 uint64_t xa_data_obj, size;
140 /* are there any extended attributes? */
141 if (xattr == ZFS_NO_OBJECT)
144 /* Lookup the object number containing the xattr data */
145 rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
150 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
154 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
159 /* Get the xattr value length / object size */
160 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
164 if (size > INT_MAX) {
171 if (buf == NULL || buf->lb_buf == NULL) {
172 /* We only need to return the required size */
175 if (*sizep > buf->lb_len) {
176 rc = -ERANGE; /* match ldiskfs error */
180 rc = -dmu_read(osd->od_os, xa_data_dn->dn_object, 0,
181 size, buf->lb_buf, DMU_READ_PREFETCH);
184 sa_handle_destroy(sa_hdl);
186 osd_dnode_rele(xa_data_dn);
192 * Copy an extended attribute into the buffer provided, or compute
193 * the required buffer size if \a buf is NULL.
195 * On success, the number of bytes used or required is stored in \a sizep.
197 * Note that no locking is done here.
199 * \param[in] env execution environment
200 * \param[in] obj object for which to retrieve xattr
201 * \param[out] buf buffer to store xattr value in
202 * \param[in] name name of xattr to copy
203 * \param[out] sizep bytes used or required to store xattr
205 * \retval 0 on success
206 * \retval negative negated errno on failure
208 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
209 struct lu_buf *buf, const char *name, int *sizep)
213 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
216 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
217 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
221 return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
225 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
226 struct lu_buf *buf, const char *name)
228 struct osd_object *obj = osd_dt_obj(dt);
232 LASSERT(obj->oo_dn != NULL);
233 LASSERT(osd_invariant(obj));
235 if (!osd_obj2dev(obj)->od_posix_acl &&
236 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
237 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
240 down_read(&obj->oo_guard);
241 rc = __osd_xattr_get(env, obj, buf, name, &size);
242 up_read(&obj->oo_guard);
251 /* the function is used to declare EAs when SA is not supported */
252 void __osd_xattr_declare_legacy(const struct lu_env *env,
253 struct osd_object *obj,
254 int vallen, const char *name,
255 struct osd_thandle *oh)
257 struct osd_device *osd = osd_obj2dev(obj);
258 dmu_tx_t *tx = oh->ot_tx;
259 uint64_t xa_data_obj;
262 if (obj->oo_xattr == ZFS_NO_OBJECT) {
263 /* xattr zap + entry */
264 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
265 /* xattr value obj */
266 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
267 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
271 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
275 * Entry already exists.
276 * We'll truncate the existing object.
278 dmu_tx_hold_bonus(tx, xa_data_obj);
279 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
280 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
281 } else if (rc == -ENOENT) {
283 * Entry doesn't exist, we need to create a new one and a new
284 * object to store the value.
286 dmu_tx_hold_bonus(tx, obj->oo_xattr);
287 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
288 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
289 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
293 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
294 int vallen, const char *name,
295 struct osd_thandle *oh)
297 dmu_tx_t *tx = oh->ot_tx;
300 if (unlikely(obj->oo_destroyed))
303 if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
304 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
308 /* declare EA in SA */
309 if (dt_object_exists(&obj->oo_dt)) {
310 LASSERT(obj->oo_sa_hdl);
311 /* XXX: it should be possible to skip spill
312 * declaration if specific EA is part of
313 * bonus and doesn't grow */
314 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
318 bonuslen = osd_obj_bonuslen(obj);
320 /* the object doesn't exist, but we've declared bonus
321 * in osd_declare_object_create() yet */
322 if (obj->oo_ea_in_bonus > bonuslen) {
323 /* spill has been declared already */
324 } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
325 /* we're about to exceed bonus, let's declare spill */
326 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
328 obj->oo_ea_in_bonus += vallen;
331 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
332 const struct lu_buf *buf, const char *name,
333 int fl, struct thandle *handle)
335 struct osd_object *obj = osd_dt_obj(dt);
336 struct osd_thandle *oh;
339 LASSERT(handle != NULL);
340 oh = container_of0(handle, struct osd_thandle, ot_super);
342 down_read(&obj->oo_guard);
343 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
344 up_read(&obj->oo_guard);
349 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
350 struct osd_thandle *oh)
352 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
353 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
354 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
355 struct osd_device *osd = osd_obj2dev(obj);
356 uint64_t crtime[2], gen;
361 obj->oo_late_xattr = 0;
362 obj->oo_late_attr_set = 0;
364 gen = dmu_tx_get_txg(oh->ot_tx);
366 ZFS_TIME_ENCODE(&now, crtime);
368 osa->atime[0] = obj->oo_attr.la_atime;
369 osa->ctime[0] = obj->oo_attr.la_ctime;
370 osa->mtime[0] = obj->oo_attr.la_mtime;
371 osa->mode = obj->oo_attr.la_mode;
372 osa->uid = obj->oo_attr.la_uid;
373 osa->gid = obj->oo_attr.la_gid;
374 osa->rdev = obj->oo_attr.la_rdev;
375 osa->nlink = obj->oo_attr.la_nlink;
376 osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
377 osa->size = obj->oo_attr.la_size;
380 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
381 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
382 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
383 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
384 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
385 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
386 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
387 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
388 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
389 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
390 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
391 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
392 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
394 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
396 /* Update the SA for additions, modifications, and removals. */
397 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
401 lu_buf_check_and_alloc(lb, size);
402 if (lb->lb_buf == NULL) {
403 CERROR("%s: can't allocate buffer for xattr update\n",
408 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
409 NV_ENCODE_XDR, KM_SLEEP);
413 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
415 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
420 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
421 struct osd_thandle *oh)
423 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
424 struct osd_device *osd = osd_obj2dev(obj);
429 obj->oo_late_xattr = 0;
431 /* Update the SA for additions, modifications, and removals. */
432 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
436 lu_buf_check_and_alloc(lb, size);
437 if (lb->lb_buf == NULL) {
438 CERROR("%s: can't allocate buffer for xattr update\n",
444 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
445 NV_ENCODE_XDR, KM_SLEEP);
448 LASSERT(dxattr == lb->lb_buf);
450 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
456 * Set an extended attribute.
457 * This transaction must have called udmu_xattr_declare_set() first.
459 * Returns 0 on success or a negative error number on failure.
461 * No locking is done here.
463 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
464 struct osd_object *obj,
465 struct osd_thandle *oh)
468 LASSERT(obj->oo_sa_hdl);
469 LASSERT(obj->oo_sa_xattr);
471 /* schedule batched SA update in osd_object_sa_dirty_rele() */
472 obj->oo_late_xattr = 1;
473 osd_object_sa_dirty_add(obj, oh);
479 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
480 const struct lu_buf *buf, const char *name, int fl,
481 struct osd_thandle *oh)
489 rc = __osd_xattr_cache(obj);
493 LASSERT(obj->oo_sa_xattr);
494 /* Limited to 32k to keep nvpair memory allocations small */
495 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
498 /* Prevent the DXATTR SA from consuming the entire SA
500 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
504 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
508 /* even in case of -EFBIG we must lookup xattr and check can we
509 * rewrite it then delete from SA */
510 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
513 if (fl & LU_XATTR_CREATE) {
515 } else if (too_big) {
516 rc = -nvlist_remove(obj->oo_sa_xattr, name,
517 DATA_TYPE_BYTE_ARRAY);
520 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
521 return rc == 0 ? -EFBIG : rc;
523 } else if (rc == -ENOENT) {
524 if (fl & LU_XATTR_REPLACE)
532 /* Ensure xattr doesn't exist in ZAP */
533 if (obj->oo_xattr != ZFS_NO_OBJECT) {
534 struct osd_device *osd = osd_obj2dev(obj);
536 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
539 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
541 zap_remove(osd->od_os, obj->oo_xattr,
546 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
547 (uchar_t *)buf->lb_buf, buf->lb_len);
551 /* batch updates only for just created dnodes where we
552 * used to set number of EAs in a single transaction */
553 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
554 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
556 rc = __osd_sa_xattr_update(env, obj, oh);
562 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
563 const struct lu_buf *buf, const char *name, int fl,
564 struct osd_thandle *oh)
566 struct osd_device *osd = osd_obj2dev(obj);
567 dnode_t *xa_zap_dn = NULL;
568 dnode_t *xa_data_dn = NULL;
569 uint64_t xa_data_obj;
570 sa_handle_t *sa_hdl = NULL;
571 dmu_tx_t *tx = oh->ot_tx;
575 LASSERT(obj->oo_sa_hdl);
577 if (obj->oo_xattr == ZFS_NO_OBJECT) {
578 struct lu_attr *la = &osd_oti_get(env)->oti_la;
580 la->la_valid = LA_MODE;
581 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
582 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0);
586 obj->oo_xattr = xa_zap_dn->dn_object;
587 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
588 &obj->oo_xattr, 8, oh);
593 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
596 if (fl & LU_XATTR_CREATE) {
601 * Entry already exists.
602 * We'll truncate the existing object.
604 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
608 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
609 SA_HDL_PRIVATE, &sa_hdl);
613 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
617 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
618 0, DMU_OBJECT_END, tx);
621 } else if (rc == -ENOENT) {
622 struct lu_attr *la = &osd_oti_get(env)->oti_la;
624 * Entry doesn't exist, we need to create a new one and a new
625 * object to store the value.
627 if (fl & LU_XATTR_REPLACE) {
628 /* should be ENOATTR according to the
629 * man, but that is undefined here */
634 la->la_valid = LA_MODE;
635 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
636 rc = __osd_object_create(env, obj, &xa_data_dn, tx, la);
639 xa_data_obj = xa_data_dn->dn_object;
641 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
642 SA_HDL_PRIVATE, &sa_hdl);
646 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
647 1, &xa_data_obj, tx);
651 /* There was an error looking up the xattr name */
655 /* Finally write the xattr value */
656 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
659 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
662 sa_handle_destroy(sa_hdl);
664 if (xa_data_dn != NULL)
665 osd_dnode_rele(xa_data_dn);
666 if (xa_zap_dn != NULL)
667 osd_dnode_rele(xa_zap_dn);
672 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
673 const struct lu_buf *buf, const char *name, int fl,
674 struct thandle *handle)
676 struct osd_object *obj = osd_dt_obj(dt);
677 struct osd_thandle *oh;
681 LASSERT(handle != NULL);
682 LASSERT(osd_invariant(obj));
684 if (!osd_obj2dev(obj)->od_posix_acl &&
685 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
686 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
689 oh = container_of0(handle, struct osd_thandle, ot_super);
691 down_write(&obj->oo_guard);
692 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
693 name, (int)buf->lb_len);
694 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
695 up_write(&obj->oo_guard);
701 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
702 const char *name, struct osd_thandle *oh)
704 struct osd_device *osd = osd_obj2dev(obj);
705 dmu_tx_t *tx = oh->ot_tx;
706 uint64_t xa_data_obj;
709 /* update SA_ZPL_DXATTR if xattr was in SA */
710 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
712 if (obj->oo_xattr == ZFS_NO_OBJECT)
715 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
719 * We'll delete the existing object and ZAP entry.
721 dmu_tx_hold_bonus(tx, xa_data_obj);
722 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
723 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
725 } else if (rc == -ENOENT) {
727 * Entry doesn't exist, nothing to be changed.
732 /* An error happened */
736 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
737 const char *name, struct thandle *handle)
739 struct osd_object *obj = osd_dt_obj(dt);
740 struct osd_thandle *oh;
743 LASSERT(handle != NULL);
744 LASSERT(osd_invariant(obj));
746 oh = container_of0(handle, struct osd_thandle, ot_super);
747 LASSERT(oh->ot_tx != NULL);
748 LASSERT(obj->oo_dn != NULL);
750 down_read(&obj->oo_guard);
751 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
752 __osd_xattr_declare_del(env, obj, name, oh);
753 up_read(&obj->oo_guard);
758 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
759 const char *name, struct osd_thandle *oh)
763 rc = __osd_xattr_cache(obj);
767 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
769 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
773 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
774 const char *name, struct osd_thandle *oh)
776 struct osd_device *osd = osd_obj2dev(obj);
777 uint64_t xa_data_obj;
780 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
783 /* try remove xattr from SA at first */
784 rc = __osd_sa_xattr_del(env, obj, name, oh);
788 if (obj->oo_xattr == ZFS_NO_OBJECT)
791 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
795 } else if (rc == 0) {
798 * We'll delete the existing object and ZAP entry.
800 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
804 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
810 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
811 const char *name, struct thandle *handle)
813 struct osd_object *obj = osd_dt_obj(dt);
814 struct osd_thandle *oh;
818 LASSERT(handle != NULL);
819 LASSERT(obj->oo_dn != NULL);
820 LASSERT(osd_invariant(obj));
821 LASSERT(dt_object_exists(dt));
822 oh = container_of0(handle, struct osd_thandle, ot_super);
823 LASSERT(oh->ot_tx != NULL);
825 if (!osd_obj2dev(obj)->od_posix_acl &&
826 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
827 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
830 down_write(&obj->oo_guard);
831 rc = __osd_xattr_del(env, obj, name, oh);
832 up_write(&obj->oo_guard);
837 void osd_declare_xattrs_destroy(const struct lu_env *env,
838 struct osd_object *obj, struct osd_thandle *oh)
840 struct osd_device *osd = osd_obj2dev(obj);
841 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
842 uint64_t oid = obj->oo_xattr, xid;
843 dmu_tx_t *tx = oh->ot_tx;
847 if (oid == ZFS_NO_OBJECT)
848 return; /* Nothing to do for SA xattrs */
850 /* Declare to free the ZAP holding xattrs */
851 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
853 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
857 while (zap_cursor_retrieve(zc, za) == 0) {
858 LASSERT(za->za_num_integers == 1);
859 LASSERT(za->za_integer_length == sizeof(uint64_t));
861 rc = -zap_lookup(osd->od_os, oid, za->za_name,
862 sizeof(uint64_t), 1, &xid);
864 CERROR("%s: xattr %s lookup failed: rc = %d\n",
865 osd->od_svname, za->za_name, rc);
868 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
870 zap_cursor_advance(zc);
873 osd_zap_cursor_fini(zc);
875 if (rc && tx->tx_err == 0)
879 int osd_xattrs_destroy(const struct lu_env *env,
880 struct osd_object *obj, struct osd_thandle *oh)
882 struct osd_device *osd = osd_obj2dev(obj);
883 dmu_tx_t *tx = oh->ot_tx;
884 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
889 /* The transaction must have been assigned to a transaction group. */
890 LASSERT(tx->tx_txg != 0);
892 if (obj->oo_xattr == ZFS_NO_OBJECT)
893 return 0; /* Nothing to do for SA xattrs */
895 /* Free the ZAP holding the xattrs */
896 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
900 while (zap_cursor_retrieve(zc, za) == 0) {
901 LASSERT(za->za_num_integers == 1);
902 LASSERT(za->za_integer_length == sizeof(uint64_t));
904 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
905 sizeof(uint64_t), 1, &xid);
907 CERROR("%s: lookup xattr %s failed: rc = %d\n",
908 osd->od_svname, za->za_name, rc);
910 rc = -dmu_object_free(osd->od_os, xid, tx);
912 CERROR("%s: free xattr %s failed: rc = %d\n",
913 osd->od_svname, za->za_name, rc);
915 zap_cursor_advance(zc);
917 osd_zap_cursor_fini(zc);
919 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
921 CERROR("%s: free xattr %llu failed: rc = %d\n",
922 osd->od_svname, obj->oo_xattr, rc);
928 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
929 const struct lu_buf *lb)
931 nvpair_t *nvp = NULL;
932 int len, counted = 0;
935 rc = __osd_xattr_cache(obj);
939 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
940 const char *name = nvpair_name(nvp);
942 if (!osd_obj2dev(obj)->od_posix_acl &&
943 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
944 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
948 if (lb->lb_buf != NULL) {
949 if (counted + len + 1 > lb->lb_len)
952 memcpy(lb->lb_buf + counted, name, len + 1);
959 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
960 const struct lu_buf *lb)
962 struct osd_object *obj = osd_dt_obj(dt);
963 struct osd_device *osd = osd_obj2dev(obj);
964 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
969 LASSERT(obj->oo_dn != NULL);
970 LASSERT(osd_invariant(obj));
971 LASSERT(dt_object_exists(dt));
973 down_read(&obj->oo_guard);
975 rc = osd_sa_xattr_list(env, obj, lb);
981 /* continue with dnode xattr if any */
982 if (obj->oo_xattr == ZFS_NO_OBJECT)
983 GOTO(out, rc = counted);
985 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
989 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
990 if (!osd_obj2dev(obj)->od_posix_acl &&
991 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
992 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
993 zap_cursor_advance(zc);
997 rc = strlen(za->za_name);
998 if (lb->lb_buf != NULL) {
999 if (counted + rc + 1 > lb->lb_len)
1000 GOTO(out_fini, rc = -ERANGE);
1002 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1006 zap_cursor_advance(zc);
1008 if (rc == -ENOENT) /* no more kes in the index */
1010 else if (unlikely(rc < 0))
1015 osd_zap_cursor_fini(zc);
1017 up_read(&obj->oo_guard);