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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/osd-zfs/osd_xattr.c
32 * functions to manipulate extended attributes and system attributes
34 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35 * Author: Mike Pershin <tappro@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSD
40 #include <libcfs/libcfs.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
44 #include <obd_class.h>
45 #include <lustre_disk.h>
46 #include <lustre_fid.h>
47 #include <lustre_linkea.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>
65 #include <lustre_scrub.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 = 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 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_internal(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,
226 * Copy LMA extended attribute into provided buffer
228 * Note that no locking is done here.
230 * \param[in] env execution environment
231 * \param[in] obj object for which to retrieve xattr
232 * \param[out] buf buffer to store xattr value in
234 * \retval 0 on success
235 * \retval negative negated errno on failure
237 int osd_xattr_get_lma(const struct lu_env *env, struct osd_object *obj,
246 if (unlikely(obj->oo_destroyed))
249 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
250 rc = __osd_sa_xattr_get(env, obj, buf, XATTR_NAME_LMA, &size);
251 if (!rc && unlikely(size < sizeof(struct lustre_mdt_attrs)))
256 rc = __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
257 buf, XATTR_NAME_LMA, &size);
258 if (!rc && unlikely(size < sizeof(struct lustre_mdt_attrs)))
265 static int osd_get_pfid_from_lma(const struct lu_env *env,
266 struct osd_object *obj,
267 struct lu_buf *buf, int *sizep)
269 struct osd_thread_info *info = osd_oti_get(env);
270 struct lustre_ost_attrs *loa =
271 (struct lustre_ost_attrs *)&info->oti_buf;
272 struct lustre_mdt_attrs *lma = &loa->loa_lma;
273 struct filter_fid *ff;
274 struct ost_layout *ol;
275 struct lu_buf tbuf = {
277 .lb_len = sizeof(info->oti_buf),
282 BUILD_BUG_ON(sizeof(info->oti_buf) < sizeof(*loa));
283 rc = osd_xattr_get_internal(env, obj, &tbuf,
284 XATTR_NAME_LMA, sizep);
288 lustre_loa_swab(loa, true);
289 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
291 *sizep = sizeof(*ff);
292 if (buf->lb_len == 0 || !buf->lb_buf)
295 if (buf->lb_len < *sizep)
300 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
301 PFID_STRIPE_IDX_BITS);
302 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
303 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
304 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
305 if (lma->lma_compat & LMAC_COMP_INFO) {
306 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
307 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
308 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
310 ol->ol_comp_start = 0;
318 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
319 struct lu_buf *buf, const char *name)
321 struct osd_object *obj = osd_dt_obj(dt);
325 if (!osd_obj2dev(obj)->od_posix_acl &&
326 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
327 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
330 down_read(&obj->oo_guard);
331 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
332 GOTO(out, rc = -ENOENT);
333 LASSERT(obj->oo_dn != NULL);
335 /* For the OST migrated from ldiskfs, the PFID EA may
336 * be stored in LMA because of ldiskfs inode size.
338 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
339 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
341 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
348 up_read(&obj->oo_guard);
352 /* the function is used to declare EAs when SA is not supported */
353 static void __osd_xattr_declare_legacy(const struct lu_env *env,
354 struct osd_object *obj,
355 int vallen, const char *name,
356 struct osd_thandle *oh)
358 struct osd_device *osd = osd_obj2dev(obj);
359 dmu_tx_t *tx = oh->ot_tx;
360 uint64_t xa_data_obj;
363 if (obj->oo_xattr == ZFS_NO_OBJECT) {
364 /* xattr zap + entry */
365 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
366 /* xattr value obj */
367 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
368 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
372 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
376 * Entry already exists.
377 * We'll truncate the existing object.
379 dmu_tx_hold_bonus(tx, xa_data_obj);
380 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
381 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
382 } else if (rc == -ENOENT) {
384 * Entry doesn't exist, we need to create a new one and a new
385 * object to store the value.
387 dmu_tx_hold_bonus(tx, obj->oo_xattr);
388 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
389 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
390 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
394 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
395 int vallen, const char *name,
396 struct osd_thandle *oh)
398 struct osd_device *osd = osd_obj2dev(obj);
399 dmu_tx_t *tx = oh->ot_tx;
402 if (unlikely(obj->oo_destroyed))
405 if (strcmp(name, XATTR_NAME_LINK) == 0 &&
406 osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
407 /* If some name entry resides on remote MDT, then will create
408 * agent entry under remote parent. On the other hand, if the
409 * remote entry will be removed, then related agent entry may
410 * need to be removed from the remote parent. So there may be
411 * kinds of cases, let's declare enough credits. The credits
412 * for create agent entry is enough for remove case.
414 osd_tx_hold_zap(tx, osd->od_remote_parent_dir,
418 if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
419 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
423 /* declare EA in SA */
424 if (dt_object_exists(&obj->oo_dt)) {
425 LASSERT(obj->oo_sa_hdl);
426 /* XXX: it should be possible to skip spill declaration if
427 * specific EA is part of bonus and doesn't grow
429 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
433 bonuslen = osd_obj_bonuslen(obj);
435 /* the object doesn't exist, but we've declared bonus
436 * in osd_declare_object_create() yet
438 if (obj->oo_ea_in_bonus > bonuslen) {
439 /* spill has been declared already */
440 } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
441 /* we're about to exceed bonus, let's declare spill */
442 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
444 obj->oo_ea_in_bonus += vallen;
447 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
448 const struct lu_buf *buf, const char *name,
449 int fl, struct thandle *handle)
451 struct osd_object *obj = osd_dt_obj(dt);
452 struct osd_thandle *oh;
455 LASSERT(handle != NULL);
456 oh = container_of(handle, struct osd_thandle, ot_super);
458 down_read(&obj->oo_guard);
459 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
460 up_read(&obj->oo_guard);
465 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
466 struct osd_thandle *oh)
468 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
469 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
470 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
471 struct osd_device *osd = osd_obj2dev(obj);
473 inode_timespec_t now;
477 obj->oo_late_xattr = 0;
478 obj->oo_late_attr_set = 0;
480 gen = dmu_tx_get_txg(oh->ot_tx);
482 ZFS_TIME_ENCODE(&now, osa->btime);
484 obj->oo_attr.la_valid |= LA_BTIME;
485 obj->oo_attr.la_btime = osa->btime[0];
486 osa->atime[0] = obj->oo_attr.la_atime;
487 osa->ctime[0] = obj->oo_attr.la_ctime;
488 osa->mtime[0] = obj->oo_attr.la_mtime;
489 osa->mode = obj->oo_attr.la_mode;
490 osa->uid = obj->oo_attr.la_uid;
491 osa->gid = obj->oo_attr.la_gid;
492 osa->rdev = obj->oo_attr.la_rdev;
493 osa->nlink = obj->oo_attr.la_nlink;
494 osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
495 osa->size = obj->oo_attr.la_size;
496 #ifdef ZFS_PROJINHERIT
497 if (osd->od_projectused_dn) {
498 if (obj->oo_attr.la_valid & LA_PROJID)
499 osa->projid = obj->oo_attr.la_projid;
501 osa->projid = ZFS_DEFAULT_PROJID;
502 osa->flags |= ZFS_PROJID;
503 obj->oo_with_projid = 1;
508 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
509 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
510 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
511 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
512 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
513 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
515 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
516 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
517 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
518 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
519 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, osa->btime, 16);
520 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
521 #ifdef ZFS_PROJINHERIT
522 if (osd->od_projectused_dn)
523 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
526 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
527 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
529 /* Update the SA for additions, modifications, and removals. */
530 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
534 lu_buf_check_and_alloc(lb, size);
535 if (lb->lb_buf == NULL) {
537 CERROR("%s: can't allocate buffer for xattr update: rc = %d\n",
542 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
543 NV_ENCODE_XDR, KM_SLEEP);
547 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
549 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
554 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
555 struct osd_thandle *oh)
557 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
558 struct osd_device *osd = osd_obj2dev(obj);
563 obj->oo_late_xattr = 0;
565 /* Update the SA for additions, modifications, and removals. */
566 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
570 lu_buf_check_and_alloc(lb, size);
571 if (lb->lb_buf == NULL) {
573 CERROR("%s: can't allocate buffer for xattr update: rc = %d\n",
579 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
580 NV_ENCODE_XDR, KM_SLEEP);
583 LASSERT(dxattr == lb->lb_buf);
585 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
591 * Set an extended attribute.
592 * This transaction must have called udmu_xattr_declare_set() first.
594 * Returns 0 on success or a negative error number on failure.
596 * No locking is done here.
598 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
599 struct osd_object *obj,
600 struct osd_thandle *oh)
603 LASSERT(obj->oo_sa_hdl);
604 LASSERT(obj->oo_sa_xattr);
606 /* schedule batched SA update in osd_object_sa_dirty_rele() */
607 obj->oo_late_xattr = 1;
608 osd_object_sa_dirty_add(obj, oh);
614 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
615 const struct lu_buf *buf, const char *name, int fl,
616 struct osd_thandle *oh)
624 rc = __osd_xattr_cache(obj);
628 LASSERT(obj->oo_sa_xattr);
629 if (buf->lb_len > OBD_MAX_EA_SIZE) {
632 /* Prevent the DXATTR SA from consuming the entire SA region */
633 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
637 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
641 /* even in case of -EFBIG we must lookup xattr and check can we
642 * rewrite it then delete from SA
644 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
647 if (fl & LU_XATTR_CREATE) {
649 } else if (too_big) {
650 rc = -nvlist_remove(obj->oo_sa_xattr, name,
651 DATA_TYPE_BYTE_ARRAY);
654 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
655 return rc == 0 ? -EFBIG : rc;
657 } else if (rc == -ENOENT) {
658 if (fl & LU_XATTR_REPLACE)
666 /* Ensure xattr doesn't exist in ZAP */
667 if (obj->oo_xattr != ZFS_NO_OBJECT) {
668 struct osd_device *osd = osd_obj2dev(obj);
671 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
674 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
676 zap_remove(osd->od_os, obj->oo_xattr,
681 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
682 (uchar_t *)buf->lb_buf, buf->lb_len);
686 /* batch updates only for just created dnodes where we
687 * used to set number of EAs in a single transaction
689 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
690 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
692 rc = __osd_sa_xattr_update(env, obj, oh);
698 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
699 const struct lu_buf *buf, const char *name, int fl,
700 struct osd_thandle *oh)
702 struct osd_device *osd = osd_obj2dev(obj);
703 dnode_t *xa_zap_dn = NULL;
704 dnode_t *xa_data_dn = NULL;
705 uint64_t xa_data_obj;
706 sa_handle_t *sa_hdl = NULL;
707 dmu_tx_t *tx = oh->ot_tx;
711 LASSERT(obj->oo_sa_hdl);
713 if (obj->oo_xattr == ZFS_NO_OBJECT) {
714 struct lu_attr *la = &osd_oti_get(env)->oti_la;
716 la->la_valid = LA_MODE;
717 la->la_mode = S_IFDIR | 0755;
718 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
722 obj->oo_xattr = xa_zap_dn->dn_object;
723 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
724 &obj->oo_xattr, 8, oh);
729 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
732 if (fl & LU_XATTR_CREATE) {
736 /* Entry already exists. We'll truncate the existing object. */
737 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
741 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
742 SA_HDL_PRIVATE, &sa_hdl);
746 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
750 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
751 0, DMU_OBJECT_END, tx);
754 } else if (rc == -ENOENT) {
755 struct lu_attr *la = &osd_oti_get(env)->oti_la;
757 * Entry doesn't exist, we need to create a new one and a new
758 * object to store the value.
760 if (fl & LU_XATTR_REPLACE) {
761 /* should be ENOATTR according to the
762 * man, but that is undefined here
768 la->la_valid = LA_MODE;
769 la->la_mode = S_IFREG | 0644;
770 rc = __osd_object_create(env, osd, obj,
771 lu_object_fid(&obj->oo_dt.do_lu),
772 &xa_data_dn, tx, la);
775 xa_data_obj = xa_data_dn->dn_object;
777 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
778 SA_HDL_PRIVATE, &sa_hdl);
782 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
783 1, &xa_data_obj, tx);
787 /* There was an error looking up the xattr name */
791 /* Finally write the xattr value */
792 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
795 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
798 sa_handle_destroy(sa_hdl);
800 if (xa_data_dn != NULL)
801 osd_dnode_rele(xa_data_dn);
802 if (xa_zap_dn != NULL)
803 osd_dnode_rele(xa_zap_dn);
808 static int osd_xattr_split_pfid(const struct lu_env *env,
809 struct osd_object *obj, struct osd_thandle *oh)
811 struct osd_thread_info *info = osd_oti_get(env);
812 struct lustre_ost_attrs *loa =
813 (struct lustre_ost_attrs *)&info->oti_buf;
814 struct lustre_mdt_attrs *lma = &loa->loa_lma;
815 struct lu_buf buf = {
817 .lb_len = sizeof(info->oti_buf),
823 BUILD_BUG_ON(sizeof(info->oti_buf) < sizeof(*loa));
824 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
828 lustre_loa_swab(loa, true);
829 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
831 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
832 lustre_lma_swab(lma);
834 buf.lb_len = sizeof(*lma);
835 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
836 LU_XATTR_REPLACE, oh);
838 obj->oo_pfid_in_lma = 0;
844 * In DNE environment, the object (in spite of regular file or directory)
845 * and its name entry may reside on different MDTs. Under such case, we will
846 * create an agent entry on the MDT where the object resides. The agent entry
847 * references the object locally, that makes the object to be visible to the
848 * userspace when mounted as 'zfs' directly. Then the userspace tools, such
849 * as 'tar' can handle the object properly.
851 * We handle the agent entry during set linkEA that is the common interface
852 * for both regular file and directroy, can handle kinds of cases, such as
853 * create/link/unlink/rename, and so on.
855 * NOTE: we need to do that for both directory and regular file, so we can NOT
856 * do that when ea_{insert,delete} that are directory based operations.
858 static int osd_xattr_handle_linkea(const struct lu_env *env,
859 struct osd_device *osd,
860 struct osd_object *obj,
861 const struct lu_buf *buf,
862 struct osd_thandle *oh)
864 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
865 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid;
866 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
867 struct lu_name tmpname;
872 rc = linkea_init_with_rec(&ldata);
874 linkea_first_entry(&ldata);
875 while (ldata.ld_lee != NULL && !remote) {
876 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
878 if (osd_remote_fid(env, osd, tfid) > 0)
881 linkea_next_entry(&ldata);
883 } else if (rc == -ENODATA) {
889 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
890 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
892 CERROR("%s: failed to remove agent entry for "DFID": rc = %d\n",
893 osd_name(osd), PFID(fid), rc);
894 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
895 rc = osd_add_to_remote_parent(env, osd, obj, oh);
897 CWARN("%s: failed to create agent entry for "DFID": rc = %d\n",
898 osd_name(osd), PFID(fid), rc);
904 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
905 const struct lu_buf *buf, const char *name, int fl,
906 struct thandle *handle)
908 struct osd_object *obj = osd_dt_obj(dt);
909 struct osd_device *osd = osd_obj2dev(obj);
910 struct osd_thandle *oh;
914 LASSERT(handle != NULL);
915 LASSERT(osd_invariant(obj));
917 if (!osd_obj2dev(obj)->od_posix_acl &&
918 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
919 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
922 oh = container_of(handle, struct osd_thandle, ot_super);
924 down_write(&obj->oo_guard);
925 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
926 name, (int)buf->lb_len);
927 /* For the OST migrated from ldiskfs, the PFID EA may
928 * be stored in LMA because of ldiskfs inode size.
930 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
931 obj->oo_pfid_in_lma)) {
932 rc = osd_xattr_split_pfid(env, obj, oh);
934 fl = LU_XATTR_CREATE;
935 } else if (strcmp(name, XATTR_NAME_LINK) == 0 &&
936 osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
937 rc = osd_xattr_handle_linkea(env, osd, obj, buf, oh);
941 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
942 up_write(&obj->oo_guard);
948 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
949 const char *name, struct osd_thandle *oh)
951 struct osd_device *osd = osd_obj2dev(obj);
952 dmu_tx_t *tx = oh->ot_tx;
953 uint64_t xa_data_obj;
956 /* update SA_ZPL_DXATTR if xattr was in SA */
957 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
959 if (obj->oo_xattr == ZFS_NO_OBJECT)
962 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
964 /* Entry exists. Will delete the existing obj and ZAP entry */
965 dmu_tx_hold_bonus(tx, xa_data_obj);
966 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
967 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
969 } else if (rc == -ENOENT) {
971 * Entry doesn't exist, nothing to be changed.
976 /* An error happened */
980 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
981 const char *name, struct thandle *handle)
983 struct osd_object *obj = osd_dt_obj(dt);
984 struct osd_thandle *oh;
987 LASSERT(handle != NULL);
988 LASSERT(osd_invariant(obj));
990 oh = container_of(handle, struct osd_thandle, ot_super);
991 LASSERT(oh->ot_tx != NULL);
993 down_read(&obj->oo_guard);
994 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed)) {
995 LASSERT(obj->oo_dn != NULL);
996 __osd_xattr_declare_del(env, obj, name, oh);
998 up_read(&obj->oo_guard);
1003 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
1004 const char *name, struct osd_thandle *oh)
1008 rc = __osd_xattr_cache(obj);
1012 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
1017 * only migrate delete LMV, and it needs to be done immediately, because
1018 * it's used in deleting sub stripes, and if this is delayed, later when
1019 * destroying the master object, it will delete sub stripes again.
1021 if (!strcmp(name, XATTR_NAME_LMV))
1022 rc = __osd_sa_xattr_update(env, obj, oh);
1024 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
1028 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
1029 const char *name, struct osd_thandle *oh)
1031 struct osd_device *osd = osd_obj2dev(obj);
1032 uint64_t xa_data_obj;
1035 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
1038 /* try remove xattr from SA at first */
1039 rc = __osd_sa_xattr_del(env, obj, name, oh);
1043 if (obj->oo_xattr == ZFS_NO_OBJECT)
1046 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
1048 if (rc == -ENOENT) {
1050 } else if (rc == 0) {
1051 /* Entry exists. We'll delete the existing obj and ZAP entry */
1052 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
1056 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
1062 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
1063 const char *name, struct thandle *handle)
1065 struct osd_object *obj = osd_dt_obj(dt);
1066 struct osd_thandle *oh;
1070 LASSERT(handle != NULL);
1071 oh = container_of(handle, struct osd_thandle, ot_super);
1072 LASSERT(oh->ot_tx != NULL);
1074 if (!osd_obj2dev(obj)->od_posix_acl &&
1075 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1076 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1077 RETURN(-EOPNOTSUPP);
1079 down_write(&obj->oo_guard);
1080 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
1081 GOTO(out, rc = -ENOENT);
1082 LASSERT(obj->oo_dn != NULL);
1083 /* For the OST migrated from ldiskfs, the PFID EA may
1084 * be stored in LMA because of ldiskfs inode size.
1086 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
1087 rc = osd_xattr_split_pfid(env, obj, oh);
1089 rc = __osd_xattr_del(env, obj, name, oh);
1092 up_write(&obj->oo_guard);
1096 void osd_declare_xattrs_destroy(const struct lu_env *env,
1097 struct osd_object *obj, struct osd_thandle *oh)
1099 struct osd_device *osd = osd_obj2dev(obj);
1100 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1101 uint64_t oid = obj->oo_xattr, xid;
1102 dmu_tx_t *tx = oh->ot_tx;
1106 if (oid == ZFS_NO_OBJECT)
1107 return; /* Nothing to do for SA xattrs */
1109 /* Declare to free the ZAP holding xattrs */
1110 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
1112 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
1116 while (zap_cursor_retrieve(zc, za) == 0) {
1117 LASSERT(za->za_num_integers == 1);
1118 LASSERT(za->za_integer_length == sizeof(uint64_t));
1120 rc = -zap_lookup(osd->od_os, oid, za->za_name,
1121 sizeof(uint64_t), 1, &xid);
1123 CERROR("%s: xattr %s lookup failed: rc = %d\n",
1124 osd->od_svname, za->za_name, rc);
1127 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
1129 zap_cursor_advance(zc);
1132 osd_zap_cursor_fini(zc);
1134 if (rc && tx->tx_err == 0)
1138 int osd_xattrs_destroy(const struct lu_env *env,
1139 struct osd_object *obj, struct osd_thandle *oh)
1141 struct osd_device *osd = osd_obj2dev(obj);
1142 dmu_tx_t *tx = oh->ot_tx;
1143 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1148 /* The transaction must have been assigned to a transaction group. */
1149 LASSERT(tx->tx_txg != 0);
1151 if (obj->oo_xattr == ZFS_NO_OBJECT)
1152 return 0; /* Nothing to do for SA xattrs */
1154 /* Free the ZAP holding the xattrs */
1155 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1159 while (zap_cursor_retrieve(zc, za) == 0) {
1160 LASSERT(za->za_num_integers == 1);
1161 LASSERT(za->za_integer_length == sizeof(uint64_t));
1163 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1164 sizeof(uint64_t), 1, &xid);
1166 CERROR("%s: lookup xattr %s failed: rc = %d\n",
1167 osd->od_svname, za->za_name, rc);
1169 rc = -dmu_object_free(osd->od_os, xid, tx);
1171 CERROR("%s: free xattr %s failed: rc = %d\n",
1172 osd->od_svname, za->za_name, rc);
1174 zap_cursor_advance(zc);
1176 osd_zap_cursor_fini(zc);
1178 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1180 CERROR("%s: free xattr %llu failed: rc = %d\n",
1181 osd->od_svname, obj->oo_xattr, rc);
1187 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1188 const struct lu_buf *lb)
1190 nvpair_t *nvp = NULL;
1191 int len, counted = 0;
1194 rc = __osd_xattr_cache(obj);
1198 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1199 const char *name = nvpair_name(nvp);
1201 if (!osd_obj2dev(obj)->od_posix_acl &&
1202 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1203 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1207 if (lb->lb_buf != NULL) {
1208 if (counted + len + 1 > lb->lb_len)
1211 memcpy(lb->lb_buf + counted, name, len + 1);
1218 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1219 const struct lu_buf *lb)
1221 struct osd_object *obj = osd_dt_obj(dt);
1222 struct osd_device *osd = osd_obj2dev(obj);
1223 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1228 down_read(&obj->oo_guard);
1229 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
1230 GOTO(out, rc = -ENOENT);
1231 LASSERT(obj->oo_dn != NULL);
1233 rc = osd_sa_xattr_list(env, obj, lb);
1239 /* continue with dnode xattr if any */
1240 if (obj->oo_xattr == ZFS_NO_OBJECT)
1241 GOTO(out, rc = counted);
1243 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1247 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1248 if (!osd_obj2dev(obj)->od_posix_acl &&
1249 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1250 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1251 zap_cursor_advance(zc);
1255 rc = strlen(za->za_name);
1256 if (lb->lb_buf != NULL) {
1257 if (counted + rc + 1 > lb->lb_len)
1258 GOTO(out_fini, rc = -ERANGE);
1260 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1264 zap_cursor_advance(zc);
1266 if (rc == -ENOENT) /* no more kes in the index */
1268 else if (unlikely(rc < 0))
1273 osd_zap_cursor_fini(zc);
1275 up_read(&obj->oo_guard);