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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd-zfs/osd_xattr.c
37 * functions to manipulate extended attributes and system attributes
39 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40 * Author: Mike Pershin <tappro@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_OSD
45 #include <lustre_ver.h>
46 #include <libcfs/libcfs.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
50 #include <obd_class.h>
51 #include <lustre_disk.h>
52 #include <lustre_fid.h>
54 #include "osd_internal.h"
56 #include <sys/dnode.h>
61 #include <sys/spa_impl.h>
62 #include <sys/zfs_znode.h>
63 #include <sys/dmu_tx.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dsl_prop.h>
66 #include <sys/sa_impl.h>
69 #include <linux/posix_acl_xattr.h>
72 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
78 if (unlikely(dnode == ZFS_NO_OBJECT))
81 rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
85 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(osd), &size);
88 rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
92 buf = sa_spill_alloc(KM_SLEEP);
97 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
99 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
102 sa_handle_destroy(sa_hdl);
107 static inline int __osd_xattr_cache(const struct lu_env *env,
108 struct osd_object *obj)
110 LASSERT(obj->oo_sa_xattr == NULL);
111 LASSERT(obj->oo_db != NULL);
113 return __osd_xattr_load(osd_obj2dev(obj), obj->oo_db->db_object,
118 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
119 const struct lu_buf *buf, const char *name, int *sizep)
124 LASSERT(obj->oo_sa_hdl);
126 if (obj->oo_sa_xattr == NULL) {
127 rc = __osd_xattr_cache(env, obj);
132 LASSERT(obj->oo_sa_xattr);
133 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
138 if (buf == NULL || buf->lb_buf == NULL) {
139 /* return the required size by *sizep */
143 if (*sizep > buf->lb_len)
144 return -ERANGE; /* match ldiskfs error */
146 memcpy(buf->lb_buf, nv_value, *sizep);
150 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
151 uint64_t xattr, struct lu_buf *buf,
152 const char *name, int *sizep)
154 dmu_buf_t *xa_data_db;
155 sa_handle_t *sa_hdl = NULL;
156 uint64_t xa_data_obj, size;
159 /* are there any extended attributes? */
160 if (xattr == ZFS_NO_OBJECT)
163 /* Lookup the object number containing the xattr data */
164 rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
169 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj, &xa_data_db);
173 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
178 /* Get the xattr value length / object size */
179 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
183 if (size > INT_MAX) {
190 if (buf == NULL || buf->lb_buf == NULL) {
191 /* We only need to return the required size */
194 if (*sizep > buf->lb_len) {
195 rc = -ERANGE; /* match ldiskfs error */
199 rc = -dmu_read(osd->od_os, xa_data_db->db_object, 0,
200 size, buf->lb_buf, DMU_READ_PREFETCH);
203 sa_handle_destroy(sa_hdl);
205 dmu_buf_rele(xa_data_db, FTAG);
211 * Copy an extended attribute into the buffer provided, or compute
212 * the required buffer size if \a buf is NULL.
214 * On success, the number of bytes used or required is stored in \a sizep.
216 * Note that no locking is done here.
218 * \param[in] env execution environment
219 * \param[in] obj object for which to retrieve xattr
220 * \param[out] buf buffer to store xattr value in
221 * \param[in] name name of xattr to copy
222 * \param[out] sizep bytes used or required to store xattr
224 * \retval 0 on success
225 * \retval negative negated errno on failure
227 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
228 struct lu_buf *buf, const char *name, int *sizep)
232 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
233 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
237 return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
241 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
242 struct lu_buf *buf, const char *name,
243 struct lustre_capa *capa)
245 struct osd_object *obj = osd_dt_obj(dt);
249 LASSERT(obj->oo_db != NULL);
250 LASSERT(osd_invariant(obj));
251 LASSERT(dt_object_exists(dt));
253 if (!osd_obj2dev(obj)->od_posix_acl &&
254 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
255 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
258 down(&obj->oo_guard);
259 rc = __osd_xattr_get(env, obj, buf, name, &size);
269 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
270 int vallen, const char *name,
271 struct osd_thandle *oh)
273 struct osd_device *osd = osd_obj2dev(obj);
274 dmu_buf_t *db = obj->oo_db;
275 dmu_tx_t *tx = oh->ot_tx;
276 uint64_t xa_data_obj;
280 here = dt_object_exists(&obj->oo_dt);
282 /* object may be not yet created */
285 LASSERT(obj->oo_sa_hdl);
286 /* we might just update SA_ZPL_DXATTR */
287 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
289 if (obj->oo_xattr == ZFS_NO_OBJECT)
293 if (!here || rc == -ENOENT) {
294 /* we'll be updating SA_ZPL_XATTR */
296 LASSERT(obj->oo_sa_hdl);
297 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
299 /* xattr zap + entry */
300 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
301 /* xattr value obj */
302 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
303 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
307 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
311 * Entry already exists.
312 * We'll truncate the existing object.
314 dmu_tx_hold_bonus(tx, xa_data_obj);
315 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
316 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
318 } else if (rc == -ENOENT) {
320 * Entry doesn't exist, we need to create a new one and a new
321 * object to store the value.
323 dmu_tx_hold_bonus(tx, obj->oo_xattr);
324 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
325 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
326 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
330 /* An error happened */
334 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
335 const struct lu_buf *buf, const char *name,
336 int fl, struct thandle *handle)
338 struct osd_object *obj = osd_dt_obj(dt);
339 struct osd_thandle *oh;
342 LASSERT(handle != NULL);
343 oh = container_of0(handle, struct osd_thandle, ot_super);
345 down(&obj->oo_guard);
346 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
353 * Set an extended attribute.
354 * This transaction must have called udmu_xattr_declare_set() first.
356 * Returns 0 on success or a negative error number on failure.
358 * No locking is done here.
361 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
362 struct osd_thandle *oh)
364 struct osd_device *osd = osd_obj2dev(obj);
370 LASSERT(obj->oo_sa_hdl);
371 LASSERT(obj->oo_sa_xattr);
373 /* Update the SA for additions, modifications, and removals. */
374 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
378 dxattr = sa_spill_alloc(KM_SLEEP);
382 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
383 NV_ENCODE_XDR, KM_SLEEP);
387 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
389 sa_spill_free(dxattr);
393 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
394 const struct lu_buf *buf, const char *name, int fl,
395 struct osd_thandle *oh)
403 LASSERT(obj->oo_sa_hdl);
404 if (obj->oo_sa_xattr == NULL) {
405 rc = __osd_xattr_cache(env, obj);
410 LASSERT(obj->oo_sa_xattr);
411 /* Limited to 32k to keep nvpair memory allocations small */
412 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
415 /* Prevent the DXATTR SA from consuming the entire SA
417 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
421 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
425 /* even in case of -EFBIG we must lookup xattr and check can we
426 * rewrite it then delete from SA */
427 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
430 if (fl & LU_XATTR_CREATE) {
432 } else if (too_big) {
433 rc = -nvlist_remove(obj->oo_sa_xattr, name,
434 DATA_TYPE_BYTE_ARRAY);
437 rc = __osd_sa_xattr_update(env, obj, oh);
438 return rc == 0 ? -EFBIG : rc;
440 } else if (rc == -ENOENT) {
441 if (fl & LU_XATTR_REPLACE)
449 /* Ensure xattr doesn't exist in ZAP */
450 if (obj->oo_xattr != ZFS_NO_OBJECT) {
451 struct osd_device *osd = osd_obj2dev(obj);
453 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
456 rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
458 zap_remove(osd->od_os, obj->oo_xattr,
463 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
464 (uchar_t *)buf->lb_buf, buf->lb_len);
468 rc = __osd_sa_xattr_update(env, obj, oh);
473 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
474 const struct lu_buf *buf, const char *name, int fl,
475 struct osd_thandle *oh)
477 struct osd_device *osd = osd_obj2dev(obj);
478 dmu_buf_t *xa_zap_db = NULL;
479 dmu_buf_t *xa_data_db = NULL;
480 uint64_t xa_data_obj;
481 sa_handle_t *sa_hdl = NULL;
482 dmu_tx_t *tx = oh->ot_tx;
486 LASSERT(obj->oo_sa_hdl);
488 if (obj->oo_xattr == ZFS_NO_OBJECT) {
489 struct lu_attr *la = &osd_oti_get(env)->oti_la;
491 la->la_valid = LA_MODE;
492 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
493 rc = __osd_zap_create(env, osd, &xa_zap_db, tx, la,
494 obj->oo_db->db_object, 0);
498 obj->oo_xattr = xa_zap_db->db_object;
499 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
500 &obj->oo_xattr, 8, oh);
505 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
508 if (fl & LU_XATTR_CREATE) {
513 * Entry already exists.
514 * We'll truncate the existing object.
516 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj,
521 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
522 SA_HDL_PRIVATE, &sa_hdl);
526 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
530 rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
531 0, DMU_OBJECT_END, tx);
534 } else if (rc == -ENOENT) {
535 struct lu_attr *la = &osd_oti_get(env)->oti_la;
537 * Entry doesn't exist, we need to create a new one and a new
538 * object to store the value.
540 if (fl & LU_XATTR_REPLACE) {
541 /* should be ENOATTR according to the
542 * man, but that is undefined here */
547 la->la_valid = LA_MODE;
548 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
549 rc = __osd_object_create(env, osd, &xa_data_db, tx, la,
553 xa_data_obj = xa_data_db->db_object;
555 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
556 SA_HDL_PRIVATE, &sa_hdl);
560 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
561 1, &xa_data_obj, tx);
565 /* There was an error looking up the xattr name */
569 /* Finally write the xattr value */
570 dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
573 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
576 sa_handle_destroy(sa_hdl);
578 if (xa_data_db != NULL)
579 dmu_buf_rele(xa_data_db, FTAG);
580 if (xa_zap_db != NULL)
581 dmu_buf_rele(xa_zap_db, FTAG);
586 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
587 const struct lu_buf *buf, const char *name, int fl,
588 struct thandle *handle, struct lustre_capa *capa)
590 struct osd_object *obj = osd_dt_obj(dt);
591 struct osd_thandle *oh;
595 LASSERT(handle != NULL);
596 LASSERT(osd_invariant(obj));
597 LASSERT(dt_object_exists(dt));
600 if (!osd_obj2dev(obj)->od_posix_acl &&
601 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
602 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
605 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
606 strcmp(name, XATTR_NAME_LINK) == 0)
609 oh = container_of0(handle, struct osd_thandle, ot_super);
611 down(&obj->oo_guard);
612 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
613 name, (int)buf->lb_len);
614 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
621 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
622 const char *name, struct osd_thandle *oh)
624 struct osd_device *osd = osd_obj2dev(obj);
625 dmu_tx_t *tx = oh->ot_tx;
626 uint64_t xa_data_obj;
629 /* update SA_ZPL_DXATTR if xattr was in SA */
630 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
632 if (obj->oo_xattr == ZFS_NO_OBJECT)
635 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
639 * We'll delete the existing object and ZAP entry.
641 dmu_tx_hold_bonus(tx, xa_data_obj);
642 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
643 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
645 } else if (rc == -ENOENT) {
647 * Entry doesn't exist, nothing to be changed.
652 /* An error happened */
656 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
657 const char *name, struct thandle *handle)
659 struct osd_object *obj = osd_dt_obj(dt);
660 struct osd_thandle *oh;
663 LASSERT(handle != NULL);
664 LASSERT(dt_object_exists(dt));
665 LASSERT(osd_invariant(obj));
667 oh = container_of0(handle, struct osd_thandle, ot_super);
668 LASSERT(oh->ot_tx != NULL);
669 LASSERT(obj->oo_db != NULL);
671 down(&obj->oo_guard);
672 __osd_xattr_declare_del(env, obj, name, oh);
678 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
679 const char *name, struct osd_thandle *oh)
683 if (obj->oo_sa_xattr == NULL) {
684 rc = __osd_xattr_cache(env, obj);
689 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
691 rc = __osd_sa_xattr_update(env, obj, oh);
695 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
696 const char *name, struct osd_thandle *oh)
698 struct osd_device *osd = osd_obj2dev(obj);
699 uint64_t xa_data_obj;
702 /* try remove xattr from SA at first */
703 rc = __osd_sa_xattr_del(env, obj, name, oh);
707 if (obj->oo_xattr == ZFS_NO_OBJECT)
710 rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
714 } else if (rc == 0) {
717 * We'll delete the existing object and ZAP entry.
719 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
723 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
729 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
730 const char *name, struct thandle *handle,
731 struct lustre_capa *capa)
733 struct osd_object *obj = osd_dt_obj(dt);
734 struct osd_thandle *oh;
738 LASSERT(handle != NULL);
739 LASSERT(obj->oo_db != NULL);
740 LASSERT(osd_invariant(obj));
741 LASSERT(dt_object_exists(dt));
742 oh = container_of0(handle, struct osd_thandle, ot_super);
743 LASSERT(oh->ot_tx != NULL);
745 if (!osd_obj2dev(obj)->od_posix_acl &&
746 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
747 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
750 down(&obj->oo_guard);
751 rc = __osd_xattr_del(env, obj, name, oh);
758 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
759 const struct lu_buf *lb)
761 nvpair_t *nvp = NULL;
762 int len, counted = 0;
765 if (obj->oo_sa_xattr == NULL) {
766 rc = __osd_xattr_cache(env, obj);
771 LASSERT(obj->oo_sa_xattr);
773 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
774 const char *name = nvpair_name(nvp);
776 if (!osd_obj2dev(obj)->od_posix_acl &&
777 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
778 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
782 if (lb->lb_buf != NULL) {
783 if (counted + len + 1 > lb->lb_len)
786 memcpy(lb->lb_buf + counted, name, len + 1);
793 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
794 const struct lu_buf *lb, struct lustre_capa *capa)
796 struct osd_object *obj = osd_dt_obj(dt);
797 struct osd_device *osd = osd_obj2dev(obj);
798 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
803 LASSERT(obj->oo_db != NULL);
804 LASSERT(osd_invariant(obj));
805 LASSERT(dt_object_exists(dt));
807 down(&obj->oo_guard);
809 rc = osd_sa_xattr_list(env, obj, lb);
815 /* continue with dnode xattr if any */
816 if (obj->oo_xattr == ZFS_NO_OBJECT)
817 GOTO(out, rc = counted);
819 rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
823 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
824 if (!osd_obj2dev(obj)->od_posix_acl &&
825 (strcmp(za->za_name, POSIX_ACL_XATTR_ACCESS) == 0 ||
826 strcmp(za->za_name, POSIX_ACL_XATTR_DEFAULT) == 0)) {
827 zap_cursor_advance(zc);
831 rc = strlen(za->za_name);
832 if (lb->lb_buf != NULL) {
833 if (counted + rc + 1 > lb->lb_len)
836 memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
840 zap_cursor_advance(zc);
842 if (rc == -ENOENT) /* no more kes in the index */
844 else if (unlikely(rc < 0))
849 osd_zap_cursor_fini(zc);