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 <lustre_ver.h>
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
46 #include <obd_class.h>
47 #include <lustre_disk.h>
48 #include <lustre_fid.h>
50 #include "osd_internal.h"
52 #include <sys/dnode.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
65 #include <linux/posix_acl_xattr.h>
68 int __osd_xattr_load(struct osd_device *osd, sa_handle_t *hdl, nvlist_t **sa)
73 rc = -sa_size(hdl, SA_ZPL_DXATTR(osd), &size);
76 rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
80 buf = osd_zio_buf_alloc(size);
85 rc = -sa_lookup(hdl, SA_ZPL_DXATTR(osd), buf, size);
87 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
88 osd_zio_buf_free(buf, size);
94 static inline int __osd_xattr_cache(struct osd_object *obj)
96 LASSERT(obj->oo_sa_hdl);
97 if (obj->oo_sa_xattr != NULL)
99 return __osd_xattr_load(osd_obj2dev(obj),
100 obj->oo_sa_hdl, &obj->oo_sa_xattr);
104 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
105 const struct lu_buf *buf, const char *name, int *sizep)
110 rc = __osd_xattr_cache(obj);
114 LASSERT(obj->oo_sa_xattr);
115 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name,
120 if (buf == NULL || buf->lb_buf == NULL) {
121 /* return the required size by *sizep */
125 if (*sizep > buf->lb_len)
126 return -ERANGE; /* match ldiskfs error */
128 memcpy(buf->lb_buf, nv_value, *sizep);
132 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
133 uint64_t xattr, struct lu_buf *buf,
134 const char *name, int *sizep)
137 sa_handle_t *sa_hdl = NULL;
138 uint64_t xa_data_obj, size;
141 /* are there any extended attributes? */
142 if (xattr == ZFS_NO_OBJECT)
145 /* Lookup the object number containing the xattr data */
146 rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
151 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
155 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
160 /* Get the xattr value length / object size */
161 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
165 if (size > INT_MAX) {
172 if (buf == NULL || buf->lb_buf == NULL) {
173 /* We only need to return the required size */
176 if (*sizep > buf->lb_len) {
177 rc = -ERANGE; /* match ldiskfs error */
181 rc = -dmu_read(osd->od_os, xa_data_dn->dn_object, 0,
182 size, buf->lb_buf, DMU_READ_PREFETCH);
185 sa_handle_destroy(sa_hdl);
187 osd_dnode_rele(xa_data_dn);
193 * Copy an extended attribute into the buffer provided, or compute
194 * the required buffer size if \a buf is NULL.
196 * On success, the number of bytes used or required is stored in \a sizep.
198 * Note that no locking is done here.
200 * \param[in] env execution environment
201 * \param[in] obj object for which to retrieve xattr
202 * \param[out] buf buffer to store xattr value in
203 * \param[in] name name of xattr to copy
204 * \param[out] sizep bytes used or required to store xattr
206 * \retval 0 on success
207 * \retval negative negated errno on failure
209 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
210 struct lu_buf *buf, const char *name, int *sizep)
214 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
217 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
218 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
222 return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
226 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
227 struct lu_buf *buf, const char *name)
229 struct osd_object *obj = osd_dt_obj(dt);
233 LASSERT(obj->oo_dn != NULL);
234 LASSERT(osd_invariant(obj));
236 if (!osd_obj2dev(obj)->od_posix_acl &&
237 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
238 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
241 down_read(&obj->oo_guard);
242 rc = __osd_xattr_get(env, obj, buf, name, &size);
243 up_read(&obj->oo_guard);
252 /* the function is used to declare EAs when SA is not supported */
253 void __osd_xattr_declare_legacy(const struct lu_env *env,
254 struct osd_object *obj,
255 int vallen, const char *name,
256 struct osd_thandle *oh)
258 struct osd_device *osd = osd_obj2dev(obj);
259 dmu_tx_t *tx = oh->ot_tx;
260 uint64_t xa_data_obj;
263 if (obj->oo_xattr == ZFS_NO_OBJECT) {
264 /* xattr zap + entry */
265 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
266 /* xattr value obj */
267 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
268 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
272 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
276 * Entry already exists.
277 * We'll truncate the existing object.
279 dmu_tx_hold_bonus(tx, xa_data_obj);
280 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
281 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
282 } else if (rc == -ENOENT) {
284 * Entry doesn't exist, we need to create a new one and a new
285 * object to store the value.
287 dmu_tx_hold_bonus(tx, obj->oo_xattr);
288 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
289 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
290 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
294 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
295 int vallen, const char *name,
296 struct osd_thandle *oh)
298 dmu_tx_t *tx = oh->ot_tx;
301 if (unlikely(obj->oo_destroyed))
304 if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
305 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
309 /* declare EA in SA */
310 if (dt_object_exists(&obj->oo_dt)) {
311 LASSERT(obj->oo_sa_hdl);
312 /* XXX: it should be possible to skip spill
313 * declaration if specific EA is part of
314 * bonus and doesn't grow */
315 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
319 bonuslen = osd_obj_bonuslen(obj);
321 /* the object doesn't exist, but we've declared bonus
322 * in osd_declare_object_create() yet */
323 if (obj->oo_ea_in_bonus > bonuslen) {
324 /* spill has been declared already */
325 } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
326 /* we're about to exceed bonus, let's declare spill */
327 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
329 obj->oo_ea_in_bonus += vallen;
332 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
333 const struct lu_buf *buf, const char *name,
334 int fl, struct thandle *handle)
336 struct osd_object *obj = osd_dt_obj(dt);
337 struct osd_thandle *oh;
340 LASSERT(handle != NULL);
341 oh = container_of0(handle, struct osd_thandle, ot_super);
343 down_read(&obj->oo_guard);
344 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
345 up_read(&obj->oo_guard);
350 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
351 struct osd_thandle *oh)
353 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
354 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
355 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
356 struct osd_device *osd = osd_obj2dev(obj);
357 uint64_t crtime[2], gen;
362 obj->oo_late_xattr = 0;
363 obj->oo_late_attr_set = 0;
365 gen = dmu_tx_get_txg(oh->ot_tx);
367 ZFS_TIME_ENCODE(&now, crtime);
369 osa->atime[0] = obj->oo_attr.la_atime;
370 osa->ctime[0] = obj->oo_attr.la_ctime;
371 osa->mtime[0] = obj->oo_attr.la_mtime;
372 osa->mode = obj->oo_attr.la_mode;
373 osa->uid = obj->oo_attr.la_uid;
374 osa->gid = obj->oo_attr.la_gid;
375 osa->rdev = obj->oo_attr.la_rdev;
376 osa->nlink = obj->oo_attr.la_nlink;
377 osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
378 osa->size = obj->oo_attr.la_size;
381 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
382 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
383 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
384 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
385 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
386 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
387 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
388 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
389 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
390 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
391 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
392 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
393 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
395 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
397 /* Update the SA for additions, modifications, and removals. */
398 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
402 lu_buf_check_and_alloc(lb, size);
403 if (lb->lb_buf == NULL) {
404 CERROR("%s: can't allocate buffer for xattr update\n",
409 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
410 NV_ENCODE_XDR, KM_SLEEP);
414 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
416 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
421 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
422 struct osd_thandle *oh)
424 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
425 struct osd_device *osd = osd_obj2dev(obj);
430 obj->oo_late_xattr = 0;
432 /* Update the SA for additions, modifications, and removals. */
433 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
437 lu_buf_check_and_alloc(lb, size);
438 if (lb->lb_buf == NULL) {
439 CERROR("%s: can't allocate buffer for xattr update\n",
445 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
446 NV_ENCODE_XDR, KM_SLEEP);
449 LASSERT(dxattr == lb->lb_buf);
451 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
457 * Set an extended attribute.
458 * This transaction must have called udmu_xattr_declare_set() first.
460 * Returns 0 on success or a negative error number on failure.
462 * No locking is done here.
464 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
465 struct osd_object *obj,
466 struct osd_thandle *oh)
469 LASSERT(obj->oo_sa_hdl);
470 LASSERT(obj->oo_sa_xattr);
472 /* schedule batched SA update in osd_object_sa_dirty_rele() */
473 obj->oo_late_xattr = 1;
474 osd_object_sa_dirty_add(obj, oh);
480 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
481 const struct lu_buf *buf, const char *name, int fl,
482 struct osd_thandle *oh)
490 rc = __osd_xattr_cache(obj);
494 LASSERT(obj->oo_sa_xattr);
495 /* Limited to 32k to keep nvpair memory allocations small */
496 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
499 /* Prevent the DXATTR SA from consuming the entire SA
501 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
505 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
509 /* even in case of -EFBIG we must lookup xattr and check can we
510 * rewrite it then delete from SA */
511 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
514 if (fl & LU_XATTR_CREATE) {
516 } else if (too_big) {
517 rc = -nvlist_remove(obj->oo_sa_xattr, name,
518 DATA_TYPE_BYTE_ARRAY);
521 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
522 return rc == 0 ? -EFBIG : rc;
524 } else if (rc == -ENOENT) {
525 if (fl & LU_XATTR_REPLACE)
533 /* Ensure xattr doesn't exist in ZAP */
534 if (obj->oo_xattr != ZFS_NO_OBJECT) {
535 struct osd_device *osd = osd_obj2dev(obj);
537 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
540 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
542 zap_remove(osd->od_os, obj->oo_xattr,
547 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
548 (uchar_t *)buf->lb_buf, buf->lb_len);
552 /* batch updates only for just created dnodes where we
553 * used to set number of EAs in a single transaction */
554 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
555 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
557 rc = __osd_sa_xattr_update(env, obj, oh);
563 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
564 const struct lu_buf *buf, const char *name, int fl,
565 struct osd_thandle *oh)
567 struct osd_device *osd = osd_obj2dev(obj);
568 dnode_t *xa_zap_dn = NULL;
569 dnode_t *xa_data_dn = NULL;
570 uint64_t xa_data_obj;
571 sa_handle_t *sa_hdl = NULL;
572 dmu_tx_t *tx = oh->ot_tx;
576 LASSERT(obj->oo_sa_hdl);
578 if (obj->oo_xattr == ZFS_NO_OBJECT) {
579 struct lu_attr *la = &osd_oti_get(env)->oti_la;
581 la->la_valid = LA_MODE;
582 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
583 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0);
587 obj->oo_xattr = xa_zap_dn->dn_object;
588 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
589 &obj->oo_xattr, 8, oh);
594 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
597 if (fl & LU_XATTR_CREATE) {
602 * Entry already exists.
603 * We'll truncate the existing object.
605 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
609 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
610 SA_HDL_PRIVATE, &sa_hdl);
614 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
618 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
619 0, DMU_OBJECT_END, tx);
622 } else if (rc == -ENOENT) {
623 struct lu_attr *la = &osd_oti_get(env)->oti_la;
625 * Entry doesn't exist, we need to create a new one and a new
626 * object to store the value.
628 if (fl & LU_XATTR_REPLACE) {
629 /* should be ENOATTR according to the
630 * man, but that is undefined here */
635 la->la_valid = LA_MODE;
636 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
637 rc = __osd_object_create(env, obj, &xa_data_dn, tx, la);
640 xa_data_obj = xa_data_dn->dn_object;
642 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
643 SA_HDL_PRIVATE, &sa_hdl);
647 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
648 1, &xa_data_obj, tx);
652 /* There was an error looking up the xattr name */
656 /* Finally write the xattr value */
657 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
660 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
663 sa_handle_destroy(sa_hdl);
665 if (xa_data_dn != NULL)
666 osd_dnode_rele(xa_data_dn);
667 if (xa_zap_dn != NULL)
668 osd_dnode_rele(xa_zap_dn);
673 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
674 const struct lu_buf *buf, const char *name, int fl,
675 struct thandle *handle)
677 struct osd_object *obj = osd_dt_obj(dt);
678 struct osd_thandle *oh;
682 LASSERT(handle != NULL);
683 LASSERT(osd_invariant(obj));
685 if (!osd_obj2dev(obj)->od_posix_acl &&
686 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
687 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
690 oh = container_of0(handle, struct osd_thandle, ot_super);
692 down_write(&obj->oo_guard);
693 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
694 name, (int)buf->lb_len);
695 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
696 up_write(&obj->oo_guard);
702 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
703 const char *name, struct osd_thandle *oh)
705 struct osd_device *osd = osd_obj2dev(obj);
706 dmu_tx_t *tx = oh->ot_tx;
707 uint64_t xa_data_obj;
710 /* update SA_ZPL_DXATTR if xattr was in SA */
711 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
713 if (obj->oo_xattr == ZFS_NO_OBJECT)
716 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
720 * We'll delete the existing object and ZAP entry.
722 dmu_tx_hold_bonus(tx, xa_data_obj);
723 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
724 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
726 } else if (rc == -ENOENT) {
728 * Entry doesn't exist, nothing to be changed.
733 /* An error happened */
737 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
738 const char *name, struct thandle *handle)
740 struct osd_object *obj = osd_dt_obj(dt);
741 struct osd_thandle *oh;
744 LASSERT(handle != NULL);
745 LASSERT(osd_invariant(obj));
747 oh = container_of0(handle, struct osd_thandle, ot_super);
748 LASSERT(oh->ot_tx != NULL);
749 LASSERT(obj->oo_dn != NULL);
751 down_read(&obj->oo_guard);
752 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
753 __osd_xattr_declare_del(env, obj, name, oh);
754 up_read(&obj->oo_guard);
759 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
760 const char *name, struct osd_thandle *oh)
764 rc = __osd_xattr_cache(obj);
768 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
770 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
774 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
775 const char *name, struct osd_thandle *oh)
777 struct osd_device *osd = osd_obj2dev(obj);
778 uint64_t xa_data_obj;
781 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
784 /* try remove xattr from SA at first */
785 rc = __osd_sa_xattr_del(env, obj, name, oh);
789 if (obj->oo_xattr == ZFS_NO_OBJECT)
792 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
796 } else if (rc == 0) {
799 * We'll delete the existing object and ZAP entry.
801 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
805 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
811 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
812 const char *name, struct thandle *handle)
814 struct osd_object *obj = osd_dt_obj(dt);
815 struct osd_thandle *oh;
819 LASSERT(handle != NULL);
820 LASSERT(obj->oo_dn != NULL);
821 LASSERT(osd_invariant(obj));
822 LASSERT(dt_object_exists(dt));
823 oh = container_of0(handle, struct osd_thandle, ot_super);
824 LASSERT(oh->ot_tx != NULL);
826 if (!osd_obj2dev(obj)->od_posix_acl &&
827 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
828 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
831 down_write(&obj->oo_guard);
832 rc = __osd_xattr_del(env, obj, name, oh);
833 up_write(&obj->oo_guard);
838 void osd_declare_xattrs_destroy(const struct lu_env *env,
839 struct osd_object *obj, struct osd_thandle *oh)
841 struct osd_device *osd = osd_obj2dev(obj);
842 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
843 uint64_t oid = obj->oo_xattr, xid;
844 dmu_tx_t *tx = oh->ot_tx;
848 if (oid == ZFS_NO_OBJECT)
849 return; /* Nothing to do for SA xattrs */
851 /* Declare to free the ZAP holding xattrs */
852 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
854 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
858 while (zap_cursor_retrieve(zc, za) == 0) {
859 LASSERT(za->za_num_integers == 1);
860 LASSERT(za->za_integer_length == sizeof(uint64_t));
862 rc = -zap_lookup(osd->od_os, oid, za->za_name,
863 sizeof(uint64_t), 1, &xid);
865 CERROR("%s: xattr %s lookup failed: rc = %d\n",
866 osd->od_svname, za->za_name, rc);
869 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
871 zap_cursor_advance(zc);
874 osd_zap_cursor_fini(zc);
876 if (rc && tx->tx_err == 0)
880 int osd_xattrs_destroy(const struct lu_env *env,
881 struct osd_object *obj, struct osd_thandle *oh)
883 struct osd_device *osd = osd_obj2dev(obj);
884 dmu_tx_t *tx = oh->ot_tx;
885 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
890 /* The transaction must have been assigned to a transaction group. */
891 LASSERT(tx->tx_txg != 0);
893 if (obj->oo_xattr == ZFS_NO_OBJECT)
894 return 0; /* Nothing to do for SA xattrs */
896 /* Free the ZAP holding the xattrs */
897 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
901 while (zap_cursor_retrieve(zc, za) == 0) {
902 LASSERT(za->za_num_integers == 1);
903 LASSERT(za->za_integer_length == sizeof(uint64_t));
905 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
906 sizeof(uint64_t), 1, &xid);
908 CERROR("%s: lookup xattr %s failed: rc = %d\n",
909 osd->od_svname, za->za_name, rc);
911 rc = -dmu_object_free(osd->od_os, xid, tx);
913 CERROR("%s: free xattr %s failed: rc = %d\n",
914 osd->od_svname, za->za_name, rc);
916 zap_cursor_advance(zc);
918 osd_zap_cursor_fini(zc);
920 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
922 CERROR("%s: free xattr %llu failed: rc = %d\n",
923 osd->od_svname, obj->oo_xattr, rc);
929 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
930 const struct lu_buf *lb)
932 nvpair_t *nvp = NULL;
933 int len, counted = 0;
936 rc = __osd_xattr_cache(obj);
940 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
941 const char *name = nvpair_name(nvp);
943 if (!osd_obj2dev(obj)->od_posix_acl &&
944 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
945 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
949 if (lb->lb_buf != NULL) {
950 if (counted + len + 1 > lb->lb_len)
953 memcpy(lb->lb_buf + counted, name, len + 1);
960 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
961 const struct lu_buf *lb)
963 struct osd_object *obj = osd_dt_obj(dt);
964 struct osd_device *osd = osd_obj2dev(obj);
965 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
970 LASSERT(obj->oo_dn != NULL);
971 LASSERT(osd_invariant(obj));
972 LASSERT(dt_object_exists(dt));
974 down_read(&obj->oo_guard);
976 rc = osd_sa_xattr_list(env, obj, lb);
982 /* continue with dnode xattr if any */
983 if (obj->oo_xattr == ZFS_NO_OBJECT)
984 GOTO(out, rc = counted);
986 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
990 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
991 if (!osd_obj2dev(obj)->od_posix_acl &&
992 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
993 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
994 zap_cursor_advance(zc);
998 rc = strlen(za->za_name);
999 if (lb->lb_buf != NULL) {
1000 if (counted + rc + 1 > lb->lb_len)
1001 GOTO(out_fini, rc = -ERANGE);
1003 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1007 zap_cursor_advance(zc);
1009 if (rc == -ENOENT) /* no more kes in the index */
1011 else if (unlikely(rc < 0))
1016 osd_zap_cursor_fini(zc);
1018 up_read(&obj->oo_guard);