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>
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 = 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_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,
225 static int osd_get_pfid_from_lma(const struct lu_env *env,
226 struct osd_object *obj,
227 struct lu_buf *buf, int *sizep)
229 struct osd_thread_info *info = osd_oti_get(env);
230 struct lustre_ost_attrs *loa =
231 (struct lustre_ost_attrs *)&info->oti_buf;
232 struct lustre_mdt_attrs *lma = &loa->loa_lma;
233 struct filter_fid *ff;
234 struct ost_layout *ol;
235 struct lu_buf tbuf = {
237 .lb_len = sizeof(info->oti_buf),
242 CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
243 rc = osd_xattr_get_internal(env, obj, &tbuf,
244 XATTR_NAME_LMA, sizep);
248 lustre_loa_swab(loa, true);
249 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
251 *sizep = sizeof(*ff);
252 if (buf->lb_len == 0 || !buf->lb_buf)
255 if (buf->lb_len < *sizep)
260 ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
261 PFID_STRIPE_IDX_BITS);
262 ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
263 loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
264 fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
265 if (lma->lma_compat & LMAC_COMP_INFO) {
266 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
267 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
268 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
270 ol->ol_comp_start = 0;
278 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
279 struct lu_buf *buf, const char *name)
281 struct osd_object *obj = osd_dt_obj(dt);
285 LASSERT(obj->oo_dn != NULL);
286 LASSERT(osd_invariant(obj));
288 if (!osd_obj2dev(obj)->od_posix_acl &&
289 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
290 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
293 down_read(&obj->oo_guard);
294 /* For the OST migrated from ldiskfs, the PFID EA may
295 * be stored in LMA because of ldiskfs inode size. */
296 if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
297 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
299 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
300 up_read(&obj->oo_guard);
309 /* the function is used to declare EAs when SA is not supported */
310 void __osd_xattr_declare_legacy(const struct lu_env *env,
311 struct osd_object *obj,
312 int vallen, const char *name,
313 struct osd_thandle *oh)
315 struct osd_device *osd = osd_obj2dev(obj);
316 dmu_tx_t *tx = oh->ot_tx;
317 uint64_t xa_data_obj;
320 if (obj->oo_xattr == ZFS_NO_OBJECT) {
321 /* xattr zap + entry */
322 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
323 /* xattr value obj */
324 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
325 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
329 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
333 * Entry already exists.
334 * We'll truncate the existing object.
336 dmu_tx_hold_bonus(tx, xa_data_obj);
337 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
338 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
339 } else if (rc == -ENOENT) {
341 * Entry doesn't exist, we need to create a new one and a new
342 * object to store the value.
344 dmu_tx_hold_bonus(tx, obj->oo_xattr);
345 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
346 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
347 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
351 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
352 int vallen, const char *name,
353 struct osd_thandle *oh)
355 dmu_tx_t *tx = oh->ot_tx;
358 if (unlikely(obj->oo_destroyed))
361 if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
362 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
366 /* declare EA in SA */
367 if (dt_object_exists(&obj->oo_dt)) {
368 LASSERT(obj->oo_sa_hdl);
369 /* XXX: it should be possible to skip spill
370 * declaration if specific EA is part of
371 * bonus and doesn't grow */
372 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
376 bonuslen = osd_obj_bonuslen(obj);
378 /* the object doesn't exist, but we've declared bonus
379 * in osd_declare_object_create() yet */
380 if (obj->oo_ea_in_bonus > bonuslen) {
381 /* spill has been declared already */
382 } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
383 /* we're about to exceed bonus, let's declare spill */
384 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
386 obj->oo_ea_in_bonus += vallen;
389 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
390 const struct lu_buf *buf, const char *name,
391 int fl, struct thandle *handle)
393 struct osd_object *obj = osd_dt_obj(dt);
394 struct osd_thandle *oh;
397 LASSERT(handle != NULL);
398 oh = container_of0(handle, struct osd_thandle, ot_super);
400 down_read(&obj->oo_guard);
401 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
402 up_read(&obj->oo_guard);
407 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
408 struct osd_thandle *oh)
410 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
411 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
412 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
413 struct osd_device *osd = osd_obj2dev(obj);
414 uint64_t crtime[2], gen;
419 obj->oo_late_xattr = 0;
420 obj->oo_late_attr_set = 0;
422 gen = dmu_tx_get_txg(oh->ot_tx);
424 ZFS_TIME_ENCODE(&now, crtime);
426 osa->atime[0] = obj->oo_attr.la_atime;
427 osa->ctime[0] = obj->oo_attr.la_ctime;
428 osa->mtime[0] = obj->oo_attr.la_mtime;
429 osa->mode = obj->oo_attr.la_mode;
430 osa->uid = obj->oo_attr.la_uid;
431 osa->gid = obj->oo_attr.la_gid;
432 osa->rdev = obj->oo_attr.la_rdev;
433 osa->nlink = obj->oo_attr.la_nlink;
434 osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
435 osa->size = obj->oo_attr.la_size;
436 #ifdef ZFS_PROJINHERIT
437 if (osd->od_projectused_dn) {
438 if (obj->oo_attr.la_valid & LA_PROJID)
439 osa->projid = obj->oo_attr.la_projid;
441 osa->projid = ZFS_DEFAULT_PROJID;
442 osa->flags |= ZFS_PROJID;
443 obj->oo_with_projid = 1;
448 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
449 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
450 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
451 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
452 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
453 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
455 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
456 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
457 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
458 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
459 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
460 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
461 #ifdef ZFS_PROJINHERIT
462 if (osd->od_projectused_dn)
463 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
466 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
467 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
469 /* Update the SA for additions, modifications, and removals. */
470 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
474 lu_buf_check_and_alloc(lb, size);
475 if (lb->lb_buf == NULL) {
476 CERROR("%s: can't allocate buffer for xattr update\n",
481 rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
482 NV_ENCODE_XDR, KM_SLEEP);
486 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
488 rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
493 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
494 struct osd_thandle *oh)
496 struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
497 struct osd_device *osd = osd_obj2dev(obj);
502 obj->oo_late_xattr = 0;
504 /* Update the SA for additions, modifications, and removals. */
505 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
509 lu_buf_check_and_alloc(lb, size);
510 if (lb->lb_buf == NULL) {
511 CERROR("%s: can't allocate buffer for xattr update\n",
517 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
518 NV_ENCODE_XDR, KM_SLEEP);
521 LASSERT(dxattr == lb->lb_buf);
523 sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
529 * Set an extended attribute.
530 * This transaction must have called udmu_xattr_declare_set() first.
532 * Returns 0 on success or a negative error number on failure.
534 * No locking is done here.
536 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
537 struct osd_object *obj,
538 struct osd_thandle *oh)
541 LASSERT(obj->oo_sa_hdl);
542 LASSERT(obj->oo_sa_xattr);
544 /* schedule batched SA update in osd_object_sa_dirty_rele() */
545 obj->oo_late_xattr = 1;
546 osd_object_sa_dirty_add(obj, oh);
552 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
553 const struct lu_buf *buf, const char *name, int fl,
554 struct osd_thandle *oh)
562 rc = __osd_xattr_cache(obj);
566 LASSERT(obj->oo_sa_xattr);
567 /* Limited to 32k to keep nvpair memory allocations small */
568 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
571 /* Prevent the DXATTR SA from consuming the entire SA
573 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
577 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
581 /* even in case of -EFBIG we must lookup xattr and check can we
582 * rewrite it then delete from SA */
583 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
586 if (fl & LU_XATTR_CREATE) {
588 } else if (too_big) {
589 rc = -nvlist_remove(obj->oo_sa_xattr, name,
590 DATA_TYPE_BYTE_ARRAY);
593 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
594 return rc == 0 ? -EFBIG : rc;
596 } else if (rc == -ENOENT) {
597 if (fl & LU_XATTR_REPLACE)
605 /* Ensure xattr doesn't exist in ZAP */
606 if (obj->oo_xattr != ZFS_NO_OBJECT) {
607 struct osd_device *osd = osd_obj2dev(obj);
609 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
612 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
614 zap_remove(osd->od_os, obj->oo_xattr,
619 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
620 (uchar_t *)buf->lb_buf, buf->lb_len);
624 /* batch updates only for just created dnodes where we
625 * used to set number of EAs in a single transaction */
626 if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
627 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
629 rc = __osd_sa_xattr_update(env, obj, oh);
635 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
636 const struct lu_buf *buf, const char *name, int fl,
637 struct osd_thandle *oh)
639 struct osd_device *osd = osd_obj2dev(obj);
640 dnode_t *xa_zap_dn = NULL;
641 dnode_t *xa_data_dn = NULL;
642 uint64_t xa_data_obj;
643 sa_handle_t *sa_hdl = NULL;
644 dmu_tx_t *tx = oh->ot_tx;
648 LASSERT(obj->oo_sa_hdl);
650 if (obj->oo_xattr == ZFS_NO_OBJECT) {
651 struct lu_attr *la = &osd_oti_get(env)->oti_la;
653 la->la_valid = LA_MODE;
654 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
655 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
659 obj->oo_xattr = xa_zap_dn->dn_object;
660 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
661 &obj->oo_xattr, 8, oh);
666 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
669 if (fl & LU_XATTR_CREATE) {
674 * Entry already exists.
675 * We'll truncate the existing object.
677 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
681 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
682 SA_HDL_PRIVATE, &sa_hdl);
686 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
690 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
691 0, DMU_OBJECT_END, tx);
694 } else if (rc == -ENOENT) {
695 struct lu_attr *la = &osd_oti_get(env)->oti_la;
697 * Entry doesn't exist, we need to create a new one and a new
698 * object to store the value.
700 if (fl & LU_XATTR_REPLACE) {
701 /* should be ENOATTR according to the
702 * man, but that is undefined here */
707 la->la_valid = LA_MODE;
708 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
709 rc = __osd_object_create(env, osd, obj,
710 lu_object_fid(&obj->oo_dt.do_lu),
711 &xa_data_dn, tx, la);
714 xa_data_obj = xa_data_dn->dn_object;
716 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
717 SA_HDL_PRIVATE, &sa_hdl);
721 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
722 1, &xa_data_obj, tx);
726 /* There was an error looking up the xattr name */
730 /* Finally write the xattr value */
731 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
734 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
737 sa_handle_destroy(sa_hdl);
739 if (xa_data_dn != NULL)
740 osd_dnode_rele(xa_data_dn);
741 if (xa_zap_dn != NULL)
742 osd_dnode_rele(xa_zap_dn);
747 static int osd_xattr_split_pfid(const struct lu_env *env,
748 struct osd_object *obj, struct osd_thandle *oh)
750 struct osd_thread_info *info = osd_oti_get(env);
751 struct lustre_ost_attrs *loa =
752 (struct lustre_ost_attrs *)&info->oti_buf;
753 struct lustre_mdt_attrs *lma = &loa->loa_lma;
754 struct lu_buf buf = {
756 .lb_len = sizeof(info->oti_buf),
762 CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
763 rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
767 lustre_loa_swab(loa, true);
768 LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
770 lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
771 lustre_lma_swab(lma);
773 buf.lb_len = sizeof(*lma);
774 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
775 LU_XATTR_REPLACE, oh);
777 obj->oo_pfid_in_lma = 0;
782 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
783 const struct lu_buf *buf, const char *name, int fl,
784 struct thandle *handle)
786 struct osd_object *obj = osd_dt_obj(dt);
787 struct osd_thandle *oh;
791 LASSERT(handle != NULL);
792 LASSERT(osd_invariant(obj));
794 if (!osd_obj2dev(obj)->od_posix_acl &&
795 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
796 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
799 oh = container_of0(handle, struct osd_thandle, ot_super);
801 down_write(&obj->oo_guard);
802 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
803 name, (int)buf->lb_len);
804 /* For the OST migrated from ldiskfs, the PFID EA may
805 * be stored in LMA because of ldiskfs inode size. */
806 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
807 obj->oo_pfid_in_lma)) {
808 rc = osd_xattr_split_pfid(env, obj, oh);
810 fl = LU_XATTR_CREATE;
814 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
815 up_write(&obj->oo_guard);
821 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
822 const char *name, struct osd_thandle *oh)
824 struct osd_device *osd = osd_obj2dev(obj);
825 dmu_tx_t *tx = oh->ot_tx;
826 uint64_t xa_data_obj;
829 /* update SA_ZPL_DXATTR if xattr was in SA */
830 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
832 if (obj->oo_xattr == ZFS_NO_OBJECT)
835 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
839 * We'll delete the existing object and ZAP entry.
841 dmu_tx_hold_bonus(tx, xa_data_obj);
842 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
843 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
845 } else if (rc == -ENOENT) {
847 * Entry doesn't exist, nothing to be changed.
852 /* An error happened */
856 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
857 const char *name, struct thandle *handle)
859 struct osd_object *obj = osd_dt_obj(dt);
860 struct osd_thandle *oh;
863 LASSERT(handle != NULL);
864 LASSERT(osd_invariant(obj));
866 oh = container_of0(handle, struct osd_thandle, ot_super);
867 LASSERT(oh->ot_tx != NULL);
868 LASSERT(obj->oo_dn != NULL);
870 down_read(&obj->oo_guard);
871 if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
872 __osd_xattr_declare_del(env, obj, name, oh);
873 up_read(&obj->oo_guard);
878 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
879 const char *name, struct osd_thandle *oh)
883 rc = __osd_xattr_cache(obj);
887 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
889 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
893 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
894 const char *name, struct osd_thandle *oh)
896 struct osd_device *osd = osd_obj2dev(obj);
897 uint64_t xa_data_obj;
900 if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
903 /* try remove xattr from SA at first */
904 rc = __osd_sa_xattr_del(env, obj, name, oh);
908 if (obj->oo_xattr == ZFS_NO_OBJECT)
911 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
915 } else if (rc == 0) {
918 * We'll delete the existing object and ZAP entry.
920 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
924 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
930 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
931 const char *name, struct thandle *handle)
933 struct osd_object *obj = osd_dt_obj(dt);
934 struct osd_thandle *oh;
938 LASSERT(handle != NULL);
939 LASSERT(obj->oo_dn != NULL);
940 LASSERT(osd_invariant(obj));
941 LASSERT(dt_object_exists(dt));
942 oh = container_of0(handle, struct osd_thandle, ot_super);
943 LASSERT(oh->ot_tx != NULL);
945 if (!osd_obj2dev(obj)->od_posix_acl &&
946 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
947 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
950 down_write(&obj->oo_guard);
951 /* For the OST migrated from ldiskfs, the PFID EA may
952 * be stored in LMA because of ldiskfs inode size. */
953 if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
954 rc = osd_xattr_split_pfid(env, obj, oh);
956 rc = __osd_xattr_del(env, obj, name, oh);
957 up_write(&obj->oo_guard);
962 void osd_declare_xattrs_destroy(const struct lu_env *env,
963 struct osd_object *obj, struct osd_thandle *oh)
965 struct osd_device *osd = osd_obj2dev(obj);
966 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
967 uint64_t oid = obj->oo_xattr, xid;
968 dmu_tx_t *tx = oh->ot_tx;
972 if (oid == ZFS_NO_OBJECT)
973 return; /* Nothing to do for SA xattrs */
975 /* Declare to free the ZAP holding xattrs */
976 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
978 rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
982 while (zap_cursor_retrieve(zc, za) == 0) {
983 LASSERT(za->za_num_integers == 1);
984 LASSERT(za->za_integer_length == sizeof(uint64_t));
986 rc = -zap_lookup(osd->od_os, oid, za->za_name,
987 sizeof(uint64_t), 1, &xid);
989 CERROR("%s: xattr %s lookup failed: rc = %d\n",
990 osd->od_svname, za->za_name, rc);
993 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
995 zap_cursor_advance(zc);
998 osd_zap_cursor_fini(zc);
1000 if (rc && tx->tx_err == 0)
1004 int osd_xattrs_destroy(const struct lu_env *env,
1005 struct osd_object *obj, struct osd_thandle *oh)
1007 struct osd_device *osd = osd_obj2dev(obj);
1008 dmu_tx_t *tx = oh->ot_tx;
1009 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1014 /* The transaction must have been assigned to a transaction group. */
1015 LASSERT(tx->tx_txg != 0);
1017 if (obj->oo_xattr == ZFS_NO_OBJECT)
1018 return 0; /* Nothing to do for SA xattrs */
1020 /* Free the ZAP holding the xattrs */
1021 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1025 while (zap_cursor_retrieve(zc, za) == 0) {
1026 LASSERT(za->za_num_integers == 1);
1027 LASSERT(za->za_integer_length == sizeof(uint64_t));
1029 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1030 sizeof(uint64_t), 1, &xid);
1032 CERROR("%s: lookup xattr %s failed: rc = %d\n",
1033 osd->od_svname, za->za_name, rc);
1035 rc = -dmu_object_free(osd->od_os, xid, tx);
1037 CERROR("%s: free xattr %s failed: rc = %d\n",
1038 osd->od_svname, za->za_name, rc);
1040 zap_cursor_advance(zc);
1042 osd_zap_cursor_fini(zc);
1044 rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1046 CERROR("%s: free xattr %llu failed: rc = %d\n",
1047 osd->od_svname, obj->oo_xattr, rc);
1053 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1054 const struct lu_buf *lb)
1056 nvpair_t *nvp = NULL;
1057 int len, counted = 0;
1060 rc = __osd_xattr_cache(obj);
1064 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1065 const char *name = nvpair_name(nvp);
1067 if (!osd_obj2dev(obj)->od_posix_acl &&
1068 (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1069 strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1073 if (lb->lb_buf != NULL) {
1074 if (counted + len + 1 > lb->lb_len)
1077 memcpy(lb->lb_buf + counted, name, len + 1);
1084 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1085 const struct lu_buf *lb)
1087 struct osd_object *obj = osd_dt_obj(dt);
1088 struct osd_device *osd = osd_obj2dev(obj);
1089 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
1094 LASSERT(obj->oo_dn != NULL);
1095 LASSERT(osd_invariant(obj));
1096 LASSERT(dt_object_exists(dt));
1098 down_read(&obj->oo_guard);
1100 rc = osd_sa_xattr_list(env, obj, lb);
1106 /* continue with dnode xattr if any */
1107 if (obj->oo_xattr == ZFS_NO_OBJECT)
1108 GOTO(out, rc = counted);
1110 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1114 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1115 if (!osd_obj2dev(obj)->od_posix_acl &&
1116 (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1117 strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1118 zap_cursor_advance(zc);
1122 rc = strlen(za->za_name);
1123 if (lb->lb_buf != NULL) {
1124 if (counted + rc + 1 > lb->lb_len)
1125 GOTO(out_fini, rc = -ERANGE);
1127 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1131 zap_cursor_advance(zc);
1133 if (rc == -ENOENT) /* no more kes in the index */
1135 else if (unlikely(rc < 0))
1140 osd_zap_cursor_fini(zc);
1142 up_read(&obj->oo_guard);