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.
31 * Copyright (c) 2012, 2013, Intel Corporation.
32 * Use is subject to license terms.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd-zfs/osd_xattr.c
39 * functions to manipulate extended attributes and system attributes
41 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42 * Author: Mike Pershin <tappro@whamcloud.com>
45 #define DEBUG_SUBSYSTEM S_OSD
47 #include <lustre_ver.h>
48 #include <libcfs/libcfs.h>
49 #include <obd_support.h>
50 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_disk.h>
54 #include <lustre_fid.h>
56 #include "osd_internal.h"
58 #include <sys/dnode.h>
63 #include <sys/spa_impl.h>
64 #include <sys/zfs_znode.h>
65 #include <sys/dmu_tx.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/dsl_prop.h>
68 #include <sys/sa_impl.h>
73 * Copy an extended attribute into the buffer provided, or compute the
74 * required buffer size.
76 * If buf is NULL, it computes the required buffer size.
78 * Returns 0 on success or a negative error number on failure.
79 * On success, the number of bytes used / required is stored in 'size'.
81 * No locking is done here.
83 int __osd_xattr_load(udmu_objset_t *uos, uint64_t dnode, nvlist_t **sa_xattr)
89 if (unlikely(dnode == ZFS_NO_OBJECT))
92 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
96 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
99 rc = -nvlist_alloc(sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
103 buf = sa_spill_alloc(KM_SLEEP);
108 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
110 rc = -nvlist_unpack(buf, size, sa_xattr, KM_SLEEP);
113 sa_handle_destroy(sa_hdl);
118 static inline int __osd_xattr_cache(const struct lu_env *env,
119 struct osd_object *obj)
121 LASSERT(obj->oo_sa_xattr == NULL);
122 LASSERT(obj->oo_db != NULL);
124 return __osd_xattr_load(&osd_obj2dev(obj)->od_objset,
125 obj->oo_db->db_object, &obj->oo_sa_xattr);
128 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
129 const struct lu_buf *buf, const char *name, int *sizep)
134 LASSERT(obj->oo_sa_hdl);
136 if (obj->oo_sa_xattr == NULL) {
137 rc = __osd_xattr_cache(env, obj);
142 LASSERT(obj->oo_sa_xattr);
143 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
148 if (buf == NULL || buf->lb_buf == NULL) {
149 /* return the required size by *sizep */
153 if (*sizep > buf->lb_len)
154 return -ERANGE; /* match ldiskfs error */
156 memcpy(buf->lb_buf, nv_value, *sizep);
160 int __osd_xattr_get_large(const struct lu_env *env, udmu_objset_t *uos,
161 uint64_t xattr, struct lu_buf *buf,
162 const char *name, int *sizep)
164 dmu_buf_t *xa_data_db;
165 sa_handle_t *sa_hdl = NULL;
166 uint64_t xa_data_obj, size;
169 /* are there any extended attributes? */
170 if (xattr == ZFS_NO_OBJECT)
173 /* Lookup the object number containing the xattr data */
174 rc = -zap_lookup(uos->os, xattr, name, sizeof(uint64_t), 1,
179 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
183 rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
188 /* Get the xattr value length / object size */
189 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
193 if (size > INT_MAX) {
200 if (buf == NULL || buf->lb_buf == NULL) {
201 /* We only need to return the required size */
204 if (*sizep > buf->lb_len) {
205 rc = -ERANGE; /* match ldiskfs error */
209 rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
210 size, buf->lb_buf, DMU_READ_PREFETCH);
213 sa_handle_destroy(sa_hdl);
215 dmu_buf_rele(xa_data_db, FTAG);
220 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
221 struct lu_buf *buf, const char *name, int *sizep)
225 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
226 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
230 rc = __osd_xattr_get_large(env, &osd_obj2dev(obj)->od_objset,
231 obj->oo_xattr, buf, name, sizep);
236 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
237 struct lu_buf *buf, const char *name,
238 struct lustre_capa *capa)
240 struct osd_object *obj = osd_dt_obj(dt);
244 LASSERT(obj->oo_db != NULL);
245 LASSERT(osd_invariant(obj));
246 LASSERT(dt_object_exists(dt));
248 down(&obj->oo_guard);
249 rc = __osd_xattr_get(env, obj, buf, name, &size);
259 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
260 int vallen, const char *name, struct osd_thandle *oh)
262 struct osd_device *osd = osd_obj2dev(obj);
263 udmu_objset_t *uos = &osd->od_objset;
264 dmu_buf_t *db = obj->oo_db;
265 dmu_tx_t *tx = oh->ot_tx;
266 uint64_t xa_data_obj;
270 here = dt_object_exists(&obj->oo_dt);
272 /* object may be not yet created */
275 LASSERT(obj->oo_sa_hdl);
276 /* we might just update SA_ZPL_DXATTR */
277 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
279 if (obj->oo_xattr == ZFS_NO_OBJECT)
283 if (!here || rc == -ENOENT) {
284 /* we'll be updating SA_ZPL_XATTR */
286 LASSERT(obj->oo_sa_hdl);
287 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
289 /* xattr zap + entry */
290 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
291 /* xattr value obj */
292 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
293 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
297 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
301 * Entry already exists.
302 * We'll truncate the existing object.
304 dmu_tx_hold_bonus(tx, xa_data_obj);
305 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
306 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
308 } else if (rc == -ENOENT) {
310 * Entry doesn't exist, we need to create a new one and a new
311 * object to store the value.
313 dmu_tx_hold_bonus(tx, obj->oo_xattr);
314 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
315 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
316 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
320 /* An error happened */
324 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
325 const struct lu_buf *buf, const char *name,
326 int fl, struct thandle *handle)
328 struct osd_object *obj = osd_dt_obj(dt);
329 struct osd_thandle *oh;
332 LASSERT(handle != NULL);
333 oh = container_of0(handle, struct osd_thandle, ot_super);
335 down(&obj->oo_guard);
336 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
343 * Set an extended attribute.
344 * This transaction must have called udmu_xattr_declare_set() first.
346 * Returns 0 on success or a negative error number on failure.
348 * No locking is done here.
351 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
352 struct osd_thandle *oh)
354 struct osd_device *osd = osd_obj2dev(obj);
355 udmu_objset_t *uos = &osd->od_objset;
361 LASSERT(obj->oo_sa_hdl);
362 LASSERT(obj->oo_sa_xattr);
364 /* Update the SA for additions, modifications, and removals. */
365 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
369 dxattr = sa_spill_alloc(KM_SLEEP);
373 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
374 NV_ENCODE_XDR, KM_SLEEP);
378 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
380 sa_spill_free(dxattr);
384 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
385 const struct lu_buf *buf, const char *name, int fl,
386 struct osd_thandle *oh)
394 LASSERT(obj->oo_sa_hdl);
395 if (obj->oo_sa_xattr == NULL) {
396 rc = __osd_xattr_cache(env, obj);
401 LASSERT(obj->oo_sa_xattr);
402 /* Limited to 32k to keep nvpair memory allocations small */
403 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
406 /* Prevent the DXATTR SA from consuming the entire SA
408 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
412 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
416 /* even in case of -EFBIG we must lookup xattr and check can we
417 * rewrite it then delete from SA */
418 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
421 if (fl & LU_XATTR_CREATE) {
423 } else if (too_big) {
424 rc = -nvlist_remove(obj->oo_sa_xattr, name,
425 DATA_TYPE_BYTE_ARRAY);
428 rc = __osd_sa_xattr_update(env, obj, oh);
429 return rc == 0 ? -EFBIG : rc;
431 } else if (rc == -ENOENT) {
432 if (fl & LU_XATTR_REPLACE)
440 /* Ensure xattr doesn't exist in ZAP */
441 if (obj->oo_xattr != ZFS_NO_OBJECT) {
442 udmu_objset_t *uos = &osd_obj2dev(obj)->od_objset;
443 uint64_t xa_data_obj;
444 rc = -zap_lookup(uos->os, obj->oo_xattr,
445 name, 8, 1, &xa_data_obj);
447 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
449 zap_remove(uos->os, obj->oo_xattr,
454 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
455 (uchar_t *)buf->lb_buf, buf->lb_len);
459 rc = __osd_sa_xattr_update(env, obj, oh);
464 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
465 const struct lu_buf *buf, const char *name, int fl,
466 struct osd_thandle *oh)
468 struct osd_device *osd = osd_obj2dev(obj);
469 udmu_objset_t *uos = &osd->od_objset;
470 dmu_buf_t *xa_zap_db = NULL;
471 dmu_buf_t *xa_data_db = NULL;
472 uint64_t xa_data_obj;
473 sa_handle_t *sa_hdl = NULL;
474 dmu_tx_t *tx = oh->ot_tx;
478 LASSERT(obj->oo_sa_hdl);
480 if (obj->oo_xattr == ZFS_NO_OBJECT) {
481 struct lu_attr *la = &osd_oti_get(env)->oti_la;
483 la->la_valid = LA_MODE;
484 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
485 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
486 obj->oo_db->db_object, FTAG, 0);
490 obj->oo_xattr = xa_zap_db->db_object;
491 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
492 &obj->oo_xattr, 8, oh);
497 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
500 if (fl & LU_XATTR_CREATE) {
505 * Entry already exists.
506 * We'll truncate the existing object.
508 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
513 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
514 SA_HDL_PRIVATE, &sa_hdl);
518 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
522 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
523 0, DMU_OBJECT_END, tx);
526 } else if (rc == -ENOENT) {
527 struct lu_attr *la = &osd_oti_get(env)->oti_la;
529 * Entry doesn't exist, we need to create a new one and a new
530 * object to store the value.
532 if (fl & LU_XATTR_REPLACE) {
533 /* should be ENOATTR according to the
534 * man, but that is undefined here */
539 la->la_valid = LA_MODE;
540 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
541 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
542 obj->oo_xattr, FTAG);
545 xa_data_obj = xa_data_db->db_object;
547 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
548 SA_HDL_PRIVATE, &sa_hdl);
552 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
553 1, &xa_data_obj, tx);
557 /* There was an error looking up the xattr name */
561 /* Finally write the xattr value */
562 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
565 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
568 sa_handle_destroy(sa_hdl);
570 if (xa_data_db != NULL)
571 dmu_buf_rele(xa_data_db, FTAG);
572 if (xa_zap_db != NULL)
573 dmu_buf_rele(xa_zap_db, FTAG);
578 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
579 const struct lu_buf *buf, const char *name, int fl,
580 struct thandle *handle, struct lustre_capa *capa)
582 struct osd_object *obj = osd_dt_obj(dt);
583 struct osd_thandle *oh;
587 LASSERT(handle != NULL);
588 LASSERT(osd_invariant(obj));
589 LASSERT(dt_object_exists(dt));
592 oh = container_of0(handle, struct osd_thandle, ot_super);
594 down(&obj->oo_guard);
595 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
596 name, (int)buf->lb_len);
597 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
604 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
605 const char *name, struct osd_thandle *oh)
607 struct osd_device *osd = osd_obj2dev(obj);
608 udmu_objset_t *uos = &osd->od_objset;
609 dmu_tx_t *tx = oh->ot_tx;
610 uint64_t xa_data_obj;
613 /* update SA_ZPL_DXATTR if xattr was in SA */
614 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
616 if (obj->oo_xattr == ZFS_NO_OBJECT)
619 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
623 * We'll delete the existing object and ZAP entry.
625 dmu_tx_hold_bonus(tx, xa_data_obj);
626 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
627 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
629 } else if (rc == -ENOENT) {
631 * Entry doesn't exist, nothing to be changed.
636 /* An error happened */
640 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
641 const char *name, struct thandle *handle)
643 struct osd_object *obj = osd_dt_obj(dt);
644 struct osd_thandle *oh;
647 LASSERT(handle != NULL);
648 LASSERT(dt_object_exists(dt));
649 LASSERT(osd_invariant(obj));
651 oh = container_of0(handle, struct osd_thandle, ot_super);
652 LASSERT(oh->ot_tx != NULL);
653 LASSERT(obj->oo_db != NULL);
655 down(&obj->oo_guard);
656 __osd_xattr_declare_del(env, obj, name, oh);
662 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
663 const char *name, struct osd_thandle *oh)
667 if (obj->oo_sa_xattr == NULL) {
668 rc = __osd_xattr_cache(env, obj);
673 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
675 rc = __osd_sa_xattr_update(env, obj, oh);
679 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
680 const char *name, struct osd_thandle *oh)
682 struct osd_device *osd = osd_obj2dev(obj);
683 udmu_objset_t *uos = &osd->od_objset;
684 uint64_t xa_data_obj;
687 /* try remove xattr from SA at first */
688 rc = __osd_sa_xattr_del(env, obj, name, oh);
692 if (obj->oo_xattr == ZFS_NO_OBJECT)
695 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
699 } else if (rc == 0) {
702 * We'll delete the existing object and ZAP entry.
704 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
708 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
714 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
715 const char *name, struct thandle *handle,
716 struct lustre_capa *capa)
718 struct osd_object *obj = osd_dt_obj(dt);
719 struct osd_thandle *oh;
723 LASSERT(handle != NULL);
724 LASSERT(obj->oo_db != NULL);
725 LASSERT(osd_invariant(obj));
726 LASSERT(dt_object_exists(dt));
727 oh = container_of0(handle, struct osd_thandle, ot_super);
728 LASSERT(oh->ot_tx != NULL);
730 down(&obj->oo_guard);
731 rc = __osd_xattr_del(env, obj, name, oh);
738 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
741 nvpair_t *nvp = NULL;
742 int len, counted = 0, remain = lb->lb_len;
745 if (obj->oo_sa_xattr == NULL) {
746 rc = __osd_xattr_cache(env, obj);
751 LASSERT(obj->oo_sa_xattr);
753 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
754 len = strlen(nvpair_name(nvp));
755 if (lb->lb_buf != NULL) {
756 if (len + 1 > remain)
759 memcpy(lb->lb_buf, nvpair_name(nvp), len);
761 *((char *)lb->lb_buf) = '\0';
770 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
771 struct lu_buf *lb, struct lustre_capa *capa)
773 struct osd_thread_info *oti = osd_oti_get(env);
774 struct osd_object *obj = osd_dt_obj(dt);
775 struct osd_device *osd = osd_obj2dev(obj);
776 udmu_objset_t *uos = &osd->od_objset;
778 int rc, counted = 0, remain = lb->lb_len;
781 LASSERT(obj->oo_db != NULL);
782 LASSERT(osd_invariant(obj));
783 LASSERT(dt_object_exists(dt));
785 down(&obj->oo_guard);
787 rc = osd_sa_xattr_list(env, obj, lb);
793 /* continue with dnode xattr if any */
794 if (obj->oo_xattr == ZFS_NO_OBJECT)
795 GOTO(out, rc = counted);
797 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
801 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
803 rc = strlen(oti->oti_key);
804 if (lb->lb_buf != NULL) {
808 memcpy(lb->lb_buf, oti->oti_key, rc);
810 *((char *)lb->lb_buf) = '\0';
816 zap_cursor_advance(zc);
818 if (rc == -ENOENT) /* no more kes in the index */
820 else if (unlikely(rc < 0))
825 udmu_zap_cursor_fini(zc);