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/
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>
48 #include <lustre_linkea.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>
66 #include <lustre_scrub.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_internal(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 static int osd_get_pfid_from_lma(const struct lu_env *env,
227 struct osd_object *obj,
228 struct lu_buf *buf, int *sizep)
230 struct osd_thread_info *info = osd_oti_get(env);
231 struct lustre_ost_attrs *loa =
232 (struct lustre_ost_attrs *)&info->oti_buf;
233 struct lustre_mdt_attrs *lma = &loa->loa_lma;
234 struct filter_fid *ff;
235 struct ost_layout *ol;
236 struct lu_buf tbuf = {
238 .lb_len = sizeof(info->oti_buf),
243 CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
244 rc = osd_xattr_get_internal(env, obj, &tbuf,
245 XATTR_NAME_LMA, sizep);
249 lustre_loa_swab(loa, true);
250 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
252 *sizep = sizeof(*ff);
253 if (buf->lb_len == 0 || !buf->lb_buf)
256 if (buf->lb_len < *sizep)
261 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
262 PFID_STRIPE_IDX_BITS);
263 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
264 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
265 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
266 if (lma->lma_compat & LMAC_COMP_INFO) {
267 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
268 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
269 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
271 ol->ol_comp_start = 0;
279 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
280 struct lu_buf *buf, const char *name)
282 struct osd_object *obj = osd_dt_obj(dt);
286 LASSERT(obj->oo_dn != NULL);
287 LASSERT(osd_invariant(obj));
289 if (!osd_obj2dev(obj)->od_posix_acl &&
290 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
291 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
294 down_read(&obj->oo_guard);
295 /* For the OST migrated from ldiskfs, the PFID EA may
296 * be stored in LMA because of ldiskfs inode size. */
297 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
298 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
300 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
301 up_read(&obj->oo_guard);
310 /* the function is used to declare EAs when SA is not supported */
311 void __osd_xattr_declare_legacy(const struct lu_env *env,
312 struct osd_object *obj,
313 int vallen, const char *name,
314 struct osd_thandle *oh)
316 struct osd_device *osd = osd_obj2dev(obj);
317 dmu_tx_t *tx = oh->ot_tx;
318 uint64_t xa_data_obj;
321 if (obj->oo_xattr == ZFS_NO_OBJECT) {
322 /* xattr zap + entry */
323 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
324 /* xattr value obj */
325 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
326 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
330 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
334 * Entry already exists.
335 * We'll truncate the existing object.
337 dmu_tx_hold_bonus(tx, xa_data_obj);
338 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
339 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
340 } else if (rc == -ENOENT) {
342 * Entry doesn't exist, we need to create a new one and a new
343 * object to store the value.
345 dmu_tx_hold_bonus(tx, obj->oo_xattr);
346 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
347 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
348 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
352 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
353 int vallen, const char *name,
354 struct osd_thandle *oh)
356 struct osd_device *osd = osd_obj2dev(obj);
357 dmu_tx_t *tx = oh->ot_tx;
360 if (unlikely(obj->oo_destroyed))
363 if (strcmp(name, XATTR_NAME_LINK) == 0 &&
364 osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
365 /* If some name entry resides on remote MDT, then will create
366 * agent entry under remote parent. On the other hand, if the
367 * remote entry will be removed, then related agent entry may
368 * need to be removed from the remote parent. So there may be
369 * kinds of cases, let's declare enough credits. The credits
370 * for create agent entry is enough for remove case. */
371 osd_tx_hold_zap(tx, osd->od_remote_parent_dir,
375 if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
376 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
380 /* declare EA in SA */
381 if (dt_object_exists(&obj->oo_dt)) {
382 LASSERT(obj->oo_sa_hdl);
383 /* XXX: it should be possible to skip spill
384 * declaration if specific EA is part of
385 * bonus and doesn't grow */
386 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
390 bonuslen = osd_obj_bonuslen(obj);
392 /* the object doesn't exist, but we've declared bonus
393 * in osd_declare_object_create() yet */
394 if (obj->oo_ea_in_bonus > bonuslen) {
395 /* spill has been declared already */
396 } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
397 /* we're about to exceed bonus, let's declare spill */
398 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
400 obj->oo_ea_in_bonus += vallen;
403 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
404 const struct lu_buf *buf, const char *name,
405 int fl, struct thandle *handle)
407 struct osd_object *obj = osd_dt_obj(dt);
408 struct osd_thandle *oh;
411 LASSERT(handle != NULL);
412 oh = container_of0(handle, struct osd_thandle, ot_super);
414 down_read(&obj->oo_guard);
415 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
416 up_read(&obj->oo_guard);
421 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
422 struct osd_thandle *oh)
424 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
425 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
426 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
427 struct osd_device *osd = osd_obj2dev(obj);
428 uint64_t crtime[2], gen;
433 obj->oo_late_xattr = 0;
434 obj->oo_late_attr_set = 0;
436 gen = dmu_tx_get_txg(oh->ot_tx);
438 ZFS_TIME_ENCODE(&now, crtime);
440 osa->atime[0] = obj->oo_attr.la_atime;
441 osa->ctime[0] = obj->oo_attr.la_ctime;
442 osa->mtime[0] = obj->oo_attr.la_mtime;
443 osa->mode = obj->oo_attr.la_mode;
444 osa->uid = obj->oo_attr.la_uid;
445 osa->gid = obj->oo_attr.la_gid;
446 osa->rdev = obj->oo_attr.la_rdev;
447 osa->nlink = obj->oo_attr.la_nlink;
448 osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
449 osa->size = obj->oo_attr.la_size;
450 #ifdef ZFS_PROJINHERIT
451 if (osd->od_projectused_dn) {
452 if (obj->oo_attr.la_valid & LA_PROJID)
453 osa->projid = obj->oo_attr.la_projid;
455 osa->projid = ZFS_DEFAULT_PROJID;
456 osa->flags |= ZFS_PROJID;
457 obj->oo_with_projid = 1;
462 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
463 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
464 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
465 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
466 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
467 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
469 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
470 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
471 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
472 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
473 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
474 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
475 #ifdef ZFS_PROJINHERIT
476 if (osd->od_projectused_dn)
477 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
480 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
481 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
483 /* Update the SA for additions, modifications, and removals. */
484 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
488 lu_buf_check_and_alloc(lb, size);
489 if (lb->lb_buf == NULL) {
490 CERROR("%s: can't allocate buffer for xattr update\n",
495 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
496 NV_ENCODE_XDR, KM_SLEEP);
500 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
502 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
507 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
508 struct osd_thandle *oh)
510 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
511 struct osd_device *osd = osd_obj2dev(obj);
516 obj->oo_late_xattr = 0;
518 /* Update the SA for additions, modifications, and removals. */
519 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
523 lu_buf_check_and_alloc(lb, size);
524 if (lb->lb_buf == NULL) {
525 CERROR("%s: can't allocate buffer for xattr update\n",
531 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
532 NV_ENCODE_XDR, KM_SLEEP);
535 LASSERT(dxattr == lb->lb_buf);
537 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
543 * Set an extended attribute.
544 * This transaction must have called udmu_xattr_declare_set() first.
546 * Returns 0 on success or a negative error number on failure.
548 * No locking is done here.
550 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
551 struct osd_object *obj,
552 struct osd_thandle *oh)
555 LASSERT(obj->oo_sa_hdl);
556 LASSERT(obj->oo_sa_xattr);
558 /* schedule batched SA update in osd_object_sa_dirty_rele() */
559 obj->oo_late_xattr = 1;
560 osd_object_sa_dirty_add(obj, oh);
566 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
567 const struct lu_buf *buf, const char *name, int fl,
568 struct osd_thandle *oh)
576 rc = __osd_xattr_cache(obj);
580 LASSERT(obj->oo_sa_xattr);
581 /* Limited to 32k to keep nvpair memory allocations small */
582 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
585 /* Prevent the DXATTR SA from consuming the entire SA
587 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
591 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
595 /* even in case of -EFBIG we must lookup xattr and check can we
596 * rewrite it then delete from SA */
597 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
600 if (fl & LU_XATTR_CREATE) {
602 } else if (too_big) {
603 rc = -nvlist_remove(obj->oo_sa_xattr, name,
604 DATA_TYPE_BYTE_ARRAY);
607 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
608 return rc == 0 ? -EFBIG : rc;
610 } else if (rc == -ENOENT) {
611 if (fl & LU_XATTR_REPLACE)
619 /* Ensure xattr doesn't exist in ZAP */
620 if (obj->oo_xattr != ZFS_NO_OBJECT) {
621 struct osd_device *osd = osd_obj2dev(obj);
623 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
626 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
628 zap_remove(osd->od_os, obj->oo_xattr,
633 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
634 (uchar_t *)buf->lb_buf, buf->lb_len);
638 /* batch updates only for just created dnodes where we
639 * used to set number of EAs in a single transaction */
640 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
641 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
643 rc = __osd_sa_xattr_update(env, obj, oh);
649 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
650 const struct lu_buf *buf, const char *name, int fl,
651 struct osd_thandle *oh)
653 struct osd_device *osd = osd_obj2dev(obj);
654 dnode_t *xa_zap_dn = NULL;
655 dnode_t *xa_data_dn = NULL;
656 uint64_t xa_data_obj;
657 sa_handle_t *sa_hdl = NULL;
658 dmu_tx_t *tx = oh->ot_tx;
662 LASSERT(obj->oo_sa_hdl);
664 if (obj->oo_xattr == ZFS_NO_OBJECT) {
665 struct lu_attr *la = &osd_oti_get(env)->oti_la;
667 la->la_valid = LA_MODE;
668 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
669 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
673 obj->oo_xattr = xa_zap_dn->dn_object;
674 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
675 &obj->oo_xattr, 8, oh);
680 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
683 if (fl & LU_XATTR_CREATE) {
688 * Entry already exists.
689 * We'll truncate the existing object.
691 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
695 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
696 SA_HDL_PRIVATE, &sa_hdl);
700 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
704 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
705 0, DMU_OBJECT_END, tx);
708 } else if (rc == -ENOENT) {
709 struct lu_attr *la = &osd_oti_get(env)->oti_la;
711 * Entry doesn't exist, we need to create a new one and a new
712 * object to store the value.
714 if (fl & LU_XATTR_REPLACE) {
715 /* should be ENOATTR according to the
716 * man, but that is undefined here */
721 la->la_valid = LA_MODE;
722 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
723 rc = __osd_object_create(env, osd, obj,
724 lu_object_fid(&obj->oo_dt.do_lu),
725 &xa_data_dn, tx, la);
728 xa_data_obj = xa_data_dn->dn_object;
730 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
731 SA_HDL_PRIVATE, &sa_hdl);
735 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
736 1, &xa_data_obj, tx);
740 /* There was an error looking up the xattr name */
744 /* Finally write the xattr value */
745 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
748 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
751 sa_handle_destroy(sa_hdl);
753 if (xa_data_dn != NULL)
754 osd_dnode_rele(xa_data_dn);
755 if (xa_zap_dn != NULL)
756 osd_dnode_rele(xa_zap_dn);
761 static int osd_xattr_split_pfid(const struct lu_env *env,
762 struct osd_object *obj, struct osd_thandle *oh)
764 struct osd_thread_info *info = osd_oti_get(env);
765 struct lustre_ost_attrs *loa =
766 (struct lustre_ost_attrs *)&info->oti_buf;
767 struct lustre_mdt_attrs *lma = &loa->loa_lma;
768 struct lu_buf buf = {
770 .lb_len = sizeof(info->oti_buf),
776 CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
777 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
781 lustre_loa_swab(loa, true);
782 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
784 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
785 lustre_lma_swab(lma);
787 buf.lb_len = sizeof(*lma);
788 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
789 LU_XATTR_REPLACE, oh);
791 obj->oo_pfid_in_lma = 0;
797 * In DNE environment, the object (in spite of regular file or directory)
798 * and its name entry may reside on different MDTs. Under such case, we will
799 * create an agent entry on the MDT where the object resides. The agent entry
800 * references the object locally, that makes the object to be visible to the
801 * userspace when mounted as 'zfs' directly. Then the userspace tools, such
802 * as 'tar' can handle the object properly.
804 * We handle the agent entry during set linkEA that is the common interface
805 * for both regular file and directroy, can handle kinds of cases, such as
806 * create/link/unlink/rename, and so on.
808 * NOTE: we need to do that for both directory and regular file, so we can NOT
809 * do that when ea_{insert,delete} that are directory based operations.
811 static int osd_xattr_handle_linkea(const struct lu_env *env,
812 struct osd_device *osd,
813 struct osd_object *obj,
814 const struct lu_buf *buf,
815 struct osd_thandle *oh)
817 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
818 struct lu_fid *tfid = &osd_oti_get(env)->oti_fid;
819 struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
820 struct lu_name tmpname;
825 rc = linkea_init_with_rec(&ldata);
827 linkea_first_entry(&ldata);
828 while (ldata.ld_lee != NULL && !remote) {
829 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
831 if (osd_remote_fid(env, osd, tfid) > 0)
834 linkea_next_entry(&ldata);
836 } else if (rc == -ENODATA) {
842 if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
843 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
845 CERROR("%s: failed to remove agent entry for "DFID
846 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
847 } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
848 rc = osd_add_to_remote_parent(env, osd, obj, oh);
850 CWARN("%s: failed to create agent entry for "DFID
851 ": rc = %d\n", osd_name(osd), PFID(fid), rc);
857 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
858 const struct lu_buf *buf, const char *name, int fl,
859 struct thandle *handle)
861 struct osd_object *obj = osd_dt_obj(dt);
862 struct osd_device *osd = osd_obj2dev(obj);
863 struct osd_thandle *oh;
867 LASSERT(handle != NULL);
868 LASSERT(osd_invariant(obj));
870 if (!osd_obj2dev(obj)->od_posix_acl &&
871 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
872 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
875 oh = container_of0(handle, struct osd_thandle, ot_super);
877 down_write(&obj->oo_guard);
878 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
879 name, (int)buf->lb_len);
880 /* For the OST migrated from ldiskfs, the PFID EA may
881 * be stored in LMA because of ldiskfs inode size. */
882 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
883 obj->oo_pfid_in_lma)) {
884 rc = osd_xattr_split_pfid(env, obj, oh);
886 fl = LU_XATTR_CREATE;
887 } else if (strcmp(name, XATTR_NAME_LINK) == 0 &&
888 osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
889 rc = osd_xattr_handle_linkea(env, osd, obj, buf, oh);
893 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
894 up_write(&obj->oo_guard);
900 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
901 const char *name, struct osd_thandle *oh)
903 struct osd_device *osd = osd_obj2dev(obj);
904 dmu_tx_t *tx = oh->ot_tx;
905 uint64_t xa_data_obj;
908 /* update SA_ZPL_DXATTR if xattr was in SA */
909 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
911 if (obj->oo_xattr == ZFS_NO_OBJECT)
914 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
918 * We'll delete the existing object and ZAP entry.
920 dmu_tx_hold_bonus(tx, xa_data_obj);
921 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
922 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
924 } else if (rc == -ENOENT) {
926 * Entry doesn't exist, nothing to be changed.
931 /* An error happened */
935 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
936 const char *name, struct thandle *handle)
938 struct osd_object *obj = osd_dt_obj(dt);
939 struct osd_thandle *oh;
942 LASSERT(handle != NULL);
943 LASSERT(osd_invariant(obj));
945 oh = container_of0(handle, struct osd_thandle, ot_super);
946 LASSERT(oh->ot_tx != NULL);
947 LASSERT(obj->oo_dn != NULL);
949 down_read(&obj->oo_guard);
950 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
951 __osd_xattr_declare_del(env, obj, name, oh);
952 up_read(&obj->oo_guard);
957 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
958 const char *name, struct osd_thandle *oh)
962 rc = __osd_xattr_cache(obj);
966 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
968 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
972 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
973 const char *name, struct osd_thandle *oh)
975 struct osd_device *osd = osd_obj2dev(obj);
976 uint64_t xa_data_obj;
979 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
982 /* try remove xattr from SA at first */
983 rc = __osd_sa_xattr_del(env, obj, name, oh);
987 if (obj->oo_xattr == ZFS_NO_OBJECT)
990 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
994 } else if (rc == 0) {
997 * We'll delete the existing object and ZAP entry.
999 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
1003 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
1009 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
1010 const char *name, struct thandle *handle)
1012 struct osd_object *obj = osd_dt_obj(dt);
1013 struct osd_thandle *oh;
1017 LASSERT(handle != NULL);
1018 LASSERT(obj->oo_dn != NULL);
1019 LASSERT(osd_invariant(obj));
1020 LASSERT(dt_object_exists(dt));
1021 oh = container_of0(handle, struct osd_thandle, ot_super);
1022 LASSERT(oh->ot_tx != NULL);
1024 if (!osd_obj2dev(obj)->od_posix_acl &&
1025 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1026 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1027 RETURN(-EOPNOTSUPP);
1029 down_write(&obj->oo_guard);
1030 /* For the OST migrated from ldiskfs, the PFID EA may
1031 * be stored in LMA because of ldiskfs inode size. */
1032 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
1033 rc = osd_xattr_split_pfid(env, obj, oh);
1035 rc = __osd_xattr_del(env, obj, name, oh);
1036 up_write(&obj->oo_guard);
1041 void osd_declare_xattrs_destroy(const struct lu_env *env,
1042 struct osd_object *obj, struct osd_thandle *oh)
1044 struct osd_device *osd = osd_obj2dev(obj);
1045 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1046 uint64_t oid = obj->oo_xattr, xid;
1047 dmu_tx_t *tx = oh->ot_tx;
1051 if (oid == ZFS_NO_OBJECT)
1052 return; /* Nothing to do for SA xattrs */
1054 /* Declare to free the ZAP holding xattrs */
1055 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
1057 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
1061 while (zap_cursor_retrieve(zc, za) == 0) {
1062 LASSERT(za->za_num_integers == 1);
1063 LASSERT(za->za_integer_length == sizeof(uint64_t));
1065 rc = -zap_lookup(osd->od_os, oid, za->za_name,
1066 sizeof(uint64_t), 1, &xid);
1068 CERROR("%s: xattr %s lookup failed: rc = %d\n",
1069 osd->od_svname, za->za_name, rc);
1072 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
1074 zap_cursor_advance(zc);
1077 osd_zap_cursor_fini(zc);
1079 if (rc && tx->tx_err == 0)
1083 int osd_xattrs_destroy(const struct lu_env *env,
1084 struct osd_object *obj, struct osd_thandle *oh)
1086 struct osd_device *osd = osd_obj2dev(obj);
1087 dmu_tx_t *tx = oh->ot_tx;
1088 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1093 /* The transaction must have been assigned to a transaction group. */
1094 LASSERT(tx->tx_txg != 0);
1096 if (obj->oo_xattr == ZFS_NO_OBJECT)
1097 return 0; /* Nothing to do for SA xattrs */
1099 /* Free the ZAP holding the xattrs */
1100 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1104 while (zap_cursor_retrieve(zc, za) == 0) {
1105 LASSERT(za->za_num_integers == 1);
1106 LASSERT(za->za_integer_length == sizeof(uint64_t));
1108 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1109 sizeof(uint64_t), 1, &xid);
1111 CERROR("%s: lookup xattr %s failed: rc = %d\n",
1112 osd->od_svname, za->za_name, rc);
1114 rc = -dmu_object_free(osd->od_os, xid, tx);
1116 CERROR("%s: free xattr %s failed: rc = %d\n",
1117 osd->od_svname, za->za_name, rc);
1119 zap_cursor_advance(zc);
1121 osd_zap_cursor_fini(zc);
1123 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1125 CERROR("%s: free xattr %llu failed: rc = %d\n",
1126 osd->od_svname, obj->oo_xattr, rc);
1132 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1133 const struct lu_buf *lb)
1135 nvpair_t *nvp = NULL;
1136 int len, counted = 0;
1139 rc = __osd_xattr_cache(obj);
1143 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1144 const char *name = nvpair_name(nvp);
1146 if (!osd_obj2dev(obj)->od_posix_acl &&
1147 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1148 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1152 if (lb->lb_buf != NULL) {
1153 if (counted + len + 1 > lb->lb_len)
1156 memcpy(lb->lb_buf + counted, name, len + 1);
1163 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1164 const struct lu_buf *lb)
1166 struct osd_object *obj = osd_dt_obj(dt);
1167 struct osd_device *osd = osd_obj2dev(obj);
1168 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1173 LASSERT(obj->oo_dn != NULL);
1174 LASSERT(osd_invariant(obj));
1175 LASSERT(dt_object_exists(dt));
1177 down_read(&obj->oo_guard);
1179 rc = osd_sa_xattr_list(env, obj, lb);
1185 /* continue with dnode xattr if any */
1186 if (obj->oo_xattr == ZFS_NO_OBJECT)
1187 GOTO(out, rc = counted);
1189 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1193 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1194 if (!osd_obj2dev(obj)->od_posix_acl &&
1195 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1196 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1197 zap_cursor_advance(zc);
1201 rc = strlen(za->za_name);
1202 if (lb->lb_buf != NULL) {
1203 if (counted + rc + 1 > lb->lb_len)
1204 GOTO(out_fini, rc = -ERANGE);
1206 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1210 zap_cursor_advance(zc);
1212 if (rc == -ENOENT) /* no more kes in the index */
1214 else if (unlikely(rc < 0))
1219 osd_zap_cursor_fini(zc);
1221 up_read(&obj->oo_guard);