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;
378 #ifdef ZFS_PROJINHERIT
379 if (osd->od_projectused_dn) {
380 if (obj->oo_attr.la_valid & LA_PROJID)
381 osa->projid = obj->oo_attr.la_projid;
383 osa->projid = ZFS_DEFAULT_PROJID;
384 osa->flags |= ZFS_PROJID;
385 obj->oo_with_projid = 1;
390 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
391 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
392 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
393 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
394 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
395 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
397 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
398 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
399 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
400 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
401 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
402 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
403 #ifdef ZFS_PROJINHERIT
404 if (osd->od_projectused_dn)
405 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
408 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
409 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
411 /* Update the SA for additions, modifications, and removals. */
412 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
416 lu_buf_check_and_alloc(lb, size);
417 if (lb->lb_buf == NULL) {
418 CERROR("%s: can't allocate buffer for xattr update\n",
423 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
424 NV_ENCODE_XDR, KM_SLEEP);
428 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
430 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
435 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
436 struct osd_thandle *oh)
438 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
439 struct osd_device *osd = osd_obj2dev(obj);
444 obj->oo_late_xattr = 0;
446 /* Update the SA for additions, modifications, and removals. */
447 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
451 lu_buf_check_and_alloc(lb, size);
452 if (lb->lb_buf == NULL) {
453 CERROR("%s: can't allocate buffer for xattr update\n",
459 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
460 NV_ENCODE_XDR, KM_SLEEP);
463 LASSERT(dxattr == lb->lb_buf);
465 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
471 * Set an extended attribute.
472 * This transaction must have called udmu_xattr_declare_set() first.
474 * Returns 0 on success or a negative error number on failure.
476 * No locking is done here.
478 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
479 struct osd_object *obj,
480 struct osd_thandle *oh)
483 LASSERT(obj->oo_sa_hdl);
484 LASSERT(obj->oo_sa_xattr);
486 /* schedule batched SA update in osd_object_sa_dirty_rele() */
487 obj->oo_late_xattr = 1;
488 osd_object_sa_dirty_add(obj, oh);
494 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
495 const struct lu_buf *buf, const char *name, int fl,
496 struct osd_thandle *oh)
504 rc = __osd_xattr_cache(obj);
508 LASSERT(obj->oo_sa_xattr);
509 /* Limited to 32k to keep nvpair memory allocations small */
510 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
513 /* Prevent the DXATTR SA from consuming the entire SA
515 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
519 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
523 /* even in case of -EFBIG we must lookup xattr and check can we
524 * rewrite it then delete from SA */
525 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
528 if (fl & LU_XATTR_CREATE) {
530 } else if (too_big) {
531 rc = -nvlist_remove(obj->oo_sa_xattr, name,
532 DATA_TYPE_BYTE_ARRAY);
535 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
536 return rc == 0 ? -EFBIG : rc;
538 } else if (rc == -ENOENT) {
539 if (fl & LU_XATTR_REPLACE)
547 /* Ensure xattr doesn't exist in ZAP */
548 if (obj->oo_xattr != ZFS_NO_OBJECT) {
549 struct osd_device *osd = osd_obj2dev(obj);
551 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
554 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
556 zap_remove(osd->od_os, obj->oo_xattr,
561 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
562 (uchar_t *)buf->lb_buf, buf->lb_len);
566 /* batch updates only for just created dnodes where we
567 * used to set number of EAs in a single transaction */
568 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
569 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
571 rc = __osd_sa_xattr_update(env, obj, oh);
577 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
578 const struct lu_buf *buf, const char *name, int fl,
579 struct osd_thandle *oh)
581 struct osd_device *osd = osd_obj2dev(obj);
582 dnode_t *xa_zap_dn = NULL;
583 dnode_t *xa_data_dn = NULL;
584 uint64_t xa_data_obj;
585 sa_handle_t *sa_hdl = NULL;
586 dmu_tx_t *tx = oh->ot_tx;
590 LASSERT(obj->oo_sa_hdl);
592 if (obj->oo_xattr == ZFS_NO_OBJECT) {
593 struct lu_attr *la = &osd_oti_get(env)->oti_la;
595 la->la_valid = LA_MODE;
596 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
597 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
601 obj->oo_xattr = xa_zap_dn->dn_object;
602 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
603 &obj->oo_xattr, 8, oh);
608 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
611 if (fl & LU_XATTR_CREATE) {
616 * Entry already exists.
617 * We'll truncate the existing object.
619 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
623 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
624 SA_HDL_PRIVATE, &sa_hdl);
628 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
632 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
633 0, DMU_OBJECT_END, tx);
636 } else if (rc == -ENOENT) {
637 struct lu_attr *la = &osd_oti_get(env)->oti_la;
639 * Entry doesn't exist, we need to create a new one and a new
640 * object to store the value.
642 if (fl & LU_XATTR_REPLACE) {
643 /* should be ENOATTR according to the
644 * man, but that is undefined here */
649 la->la_valid = LA_MODE;
650 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
651 rc = __osd_object_create(env, obj, &xa_data_dn, tx, la);
654 xa_data_obj = xa_data_dn->dn_object;
656 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
657 SA_HDL_PRIVATE, &sa_hdl);
661 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
662 1, &xa_data_obj, tx);
666 /* There was an error looking up the xattr name */
670 /* Finally write the xattr value */
671 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
674 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
677 sa_handle_destroy(sa_hdl);
679 if (xa_data_dn != NULL)
680 osd_dnode_rele(xa_data_dn);
681 if (xa_zap_dn != NULL)
682 osd_dnode_rele(xa_zap_dn);
687 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
688 const struct lu_buf *buf, const char *name, int fl,
689 struct thandle *handle)
691 struct osd_object *obj = osd_dt_obj(dt);
692 struct osd_thandle *oh;
696 LASSERT(handle != NULL);
697 LASSERT(osd_invariant(obj));
699 if (!osd_obj2dev(obj)->od_posix_acl &&
700 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
701 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
704 oh = container_of0(handle, struct osd_thandle, ot_super);
706 down_write(&obj->oo_guard);
707 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
708 name, (int)buf->lb_len);
709 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
710 up_write(&obj->oo_guard);
716 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
717 const char *name, struct osd_thandle *oh)
719 struct osd_device *osd = osd_obj2dev(obj);
720 dmu_tx_t *tx = oh->ot_tx;
721 uint64_t xa_data_obj;
724 /* update SA_ZPL_DXATTR if xattr was in SA */
725 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
727 if (obj->oo_xattr == ZFS_NO_OBJECT)
730 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
734 * We'll delete the existing object and ZAP entry.
736 dmu_tx_hold_bonus(tx, xa_data_obj);
737 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
738 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
740 } else if (rc == -ENOENT) {
742 * Entry doesn't exist, nothing to be changed.
747 /* An error happened */
751 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
752 const char *name, struct thandle *handle)
754 struct osd_object *obj = osd_dt_obj(dt);
755 struct osd_thandle *oh;
758 LASSERT(handle != NULL);
759 LASSERT(osd_invariant(obj));
761 oh = container_of0(handle, struct osd_thandle, ot_super);
762 LASSERT(oh->ot_tx != NULL);
763 LASSERT(obj->oo_dn != NULL);
765 down_read(&obj->oo_guard);
766 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
767 __osd_xattr_declare_del(env, obj, name, oh);
768 up_read(&obj->oo_guard);
773 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
774 const char *name, struct osd_thandle *oh)
778 rc = __osd_xattr_cache(obj);
782 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
784 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
788 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
789 const char *name, struct osd_thandle *oh)
791 struct osd_device *osd = osd_obj2dev(obj);
792 uint64_t xa_data_obj;
795 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
798 /* try remove xattr from SA at first */
799 rc = __osd_sa_xattr_del(env, obj, name, oh);
803 if (obj->oo_xattr == ZFS_NO_OBJECT)
806 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
810 } else if (rc == 0) {
813 * We'll delete the existing object and ZAP entry.
815 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
819 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
825 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
826 const char *name, struct thandle *handle)
828 struct osd_object *obj = osd_dt_obj(dt);
829 struct osd_thandle *oh;
833 LASSERT(handle != NULL);
834 LASSERT(obj->oo_dn != NULL);
835 LASSERT(osd_invariant(obj));
836 LASSERT(dt_object_exists(dt));
837 oh = container_of0(handle, struct osd_thandle, ot_super);
838 LASSERT(oh->ot_tx != NULL);
840 if (!osd_obj2dev(obj)->od_posix_acl &&
841 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
842 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
845 down_write(&obj->oo_guard);
846 rc = __osd_xattr_del(env, obj, name, oh);
847 up_write(&obj->oo_guard);
852 void osd_declare_xattrs_destroy(const struct lu_env *env,
853 struct osd_object *obj, struct osd_thandle *oh)
855 struct osd_device *osd = osd_obj2dev(obj);
856 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
857 uint64_t oid = obj->oo_xattr, xid;
858 dmu_tx_t *tx = oh->ot_tx;
862 if (oid == ZFS_NO_OBJECT)
863 return; /* Nothing to do for SA xattrs */
865 /* Declare to free the ZAP holding xattrs */
866 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
868 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
872 while (zap_cursor_retrieve(zc, za) == 0) {
873 LASSERT(za->za_num_integers == 1);
874 LASSERT(za->za_integer_length == sizeof(uint64_t));
876 rc = -zap_lookup(osd->od_os, oid, za->za_name,
877 sizeof(uint64_t), 1, &xid);
879 CERROR("%s: xattr %s lookup failed: rc = %d\n",
880 osd->od_svname, za->za_name, rc);
883 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
885 zap_cursor_advance(zc);
888 osd_zap_cursor_fini(zc);
890 if (rc && tx->tx_err == 0)
894 int osd_xattrs_destroy(const struct lu_env *env,
895 struct osd_object *obj, struct osd_thandle *oh)
897 struct osd_device *osd = osd_obj2dev(obj);
898 dmu_tx_t *tx = oh->ot_tx;
899 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
904 /* The transaction must have been assigned to a transaction group. */
905 LASSERT(tx->tx_txg != 0);
907 if (obj->oo_xattr == ZFS_NO_OBJECT)
908 return 0; /* Nothing to do for SA xattrs */
910 /* Free the ZAP holding the xattrs */
911 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
915 while (zap_cursor_retrieve(zc, za) == 0) {
916 LASSERT(za->za_num_integers == 1);
917 LASSERT(za->za_integer_length == sizeof(uint64_t));
919 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
920 sizeof(uint64_t), 1, &xid);
922 CERROR("%s: lookup xattr %s failed: rc = %d\n",
923 osd->od_svname, za->za_name, rc);
925 rc = -dmu_object_free(osd->od_os, xid, tx);
927 CERROR("%s: free xattr %s failed: rc = %d\n",
928 osd->od_svname, za->za_name, rc);
930 zap_cursor_advance(zc);
932 osd_zap_cursor_fini(zc);
934 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
936 CERROR("%s: free xattr %llu failed: rc = %d\n",
937 osd->od_svname, obj->oo_xattr, rc);
943 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
944 const struct lu_buf *lb)
946 nvpair_t *nvp = NULL;
947 int len, counted = 0;
950 rc = __osd_xattr_cache(obj);
954 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
955 const char *name = nvpair_name(nvp);
957 if (!osd_obj2dev(obj)->od_posix_acl &&
958 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
959 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
963 if (lb->lb_buf != NULL) {
964 if (counted + len + 1 > lb->lb_len)
967 memcpy(lb->lb_buf + counted, name, len + 1);
974 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
975 const struct lu_buf *lb)
977 struct osd_object *obj = osd_dt_obj(dt);
978 struct osd_device *osd = osd_obj2dev(obj);
979 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
984 LASSERT(obj->oo_dn != NULL);
985 LASSERT(osd_invariant(obj));
986 LASSERT(dt_object_exists(dt));
988 down_read(&obj->oo_guard);
990 rc = osd_sa_xattr_list(env, obj, lb);
996 /* continue with dnode xattr if any */
997 if (obj->oo_xattr == ZFS_NO_OBJECT)
998 GOTO(out, rc = counted);
1000 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1004 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1005 if (!osd_obj2dev(obj)->od_posix_acl &&
1006 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1007 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1008 zap_cursor_advance(zc);
1012 rc = strlen(za->za_name);
1013 if (lb->lb_buf != NULL) {
1014 if (counted + rc + 1 > lb->lb_len)
1015 GOTO(out_fini, rc = -ERANGE);
1017 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1021 zap_cursor_advance(zc);
1023 if (rc == -ENOENT) /* no more kes in the index */
1025 else if (unlikely(rc < 0))
1030 osd_zap_cursor_fini(zc);
1032 up_read(&obj->oo_guard);