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 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
441 (uchar_t *)buf->lb_buf, buf->lb_len);
445 rc = __osd_sa_xattr_update(env, obj, oh);
450 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
451 const struct lu_buf *buf, const char *name, int fl,
452 struct osd_thandle *oh)
454 struct osd_device *osd = osd_obj2dev(obj);
455 udmu_objset_t *uos = &osd->od_objset;
456 dmu_buf_t *xa_zap_db = NULL;
457 dmu_buf_t *xa_data_db = NULL;
458 uint64_t xa_data_obj;
459 sa_handle_t *sa_hdl = NULL;
460 dmu_tx_t *tx = oh->ot_tx;
464 LASSERT(obj->oo_sa_hdl);
466 if (obj->oo_xattr == ZFS_NO_OBJECT) {
467 struct lu_attr *la = &osd_oti_get(env)->oti_la;
469 la->la_valid = LA_MODE;
470 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
471 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
472 obj->oo_db->db_object, FTAG, 0);
476 obj->oo_xattr = xa_zap_db->db_object;
477 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
478 &obj->oo_xattr, 8, oh);
483 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
486 if (fl & LU_XATTR_CREATE) {
491 * Entry already exists.
492 * We'll truncate the existing object.
494 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
499 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
500 SA_HDL_PRIVATE, &sa_hdl);
504 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
508 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
509 0, DMU_OBJECT_END, tx);
512 } else if (rc == -ENOENT) {
513 struct lu_attr *la = &osd_oti_get(env)->oti_la;
515 * Entry doesn't exist, we need to create a new one and a new
516 * object to store the value.
518 if (fl & LU_XATTR_REPLACE) {
519 /* should be ENOATTR according to the
520 * man, but that is undefined here */
525 la->la_valid = LA_MODE;
526 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
527 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
528 obj->oo_xattr, FTAG);
531 xa_data_obj = xa_data_db->db_object;
533 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
534 SA_HDL_PRIVATE, &sa_hdl);
538 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
539 1, &xa_data_obj, tx);
543 /* There was an error looking up the xattr name */
547 /* Finally write the xattr value */
548 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
551 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
554 sa_handle_destroy(sa_hdl);
556 if (xa_data_db != NULL)
557 dmu_buf_rele(xa_data_db, FTAG);
558 if (xa_zap_db != NULL)
559 dmu_buf_rele(xa_zap_db, FTAG);
564 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
565 const struct lu_buf *buf, const char *name, int fl,
566 struct thandle *handle, struct lustre_capa *capa)
568 struct osd_object *obj = osd_dt_obj(dt);
569 struct osd_thandle *oh;
573 LASSERT(handle != NULL);
574 LASSERT(osd_invariant(obj));
575 LASSERT(dt_object_exists(dt));
578 oh = container_of0(handle, struct osd_thandle, ot_super);
580 down(&obj->oo_guard);
581 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
582 name, (int)buf->lb_len);
583 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
590 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
591 const char *name, struct osd_thandle *oh)
593 struct osd_device *osd = osd_obj2dev(obj);
594 udmu_objset_t *uos = &osd->od_objset;
595 dmu_tx_t *tx = oh->ot_tx;
596 uint64_t xa_data_obj;
599 /* update SA_ZPL_DXATTR if xattr was in SA */
600 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
602 if (obj->oo_xattr == ZFS_NO_OBJECT)
605 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
609 * We'll delete the existing object and ZAP entry.
611 dmu_tx_hold_bonus(tx, xa_data_obj);
612 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
613 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
615 } else if (rc == -ENOENT) {
617 * Entry doesn't exist, nothing to be changed.
622 /* An error happened */
626 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
627 const char *name, struct thandle *handle)
629 struct osd_object *obj = osd_dt_obj(dt);
630 struct osd_thandle *oh;
633 LASSERT(handle != NULL);
634 LASSERT(dt_object_exists(dt));
635 LASSERT(osd_invariant(obj));
637 oh = container_of0(handle, struct osd_thandle, ot_super);
638 LASSERT(oh->ot_tx != NULL);
639 LASSERT(obj->oo_db != NULL);
641 down(&obj->oo_guard);
642 __osd_xattr_declare_del(env, obj, name, oh);
648 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
649 const char *name, struct osd_thandle *oh)
653 if (obj->oo_sa_xattr == NULL) {
654 rc = __osd_xattr_cache(env, obj);
659 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
661 rc = __osd_sa_xattr_update(env, obj, oh);
665 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
666 const char *name, struct osd_thandle *oh)
668 struct osd_device *osd = osd_obj2dev(obj);
669 udmu_objset_t *uos = &osd->od_objset;
670 uint64_t xa_data_obj;
673 /* try remove xattr from SA at first */
674 rc = __osd_sa_xattr_del(env, obj, name, oh);
678 if (obj->oo_xattr == ZFS_NO_OBJECT)
681 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
685 } else if (rc == 0) {
688 * We'll delete the existing object and ZAP entry.
690 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
694 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
700 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
701 const char *name, struct thandle *handle,
702 struct lustre_capa *capa)
704 struct osd_object *obj = osd_dt_obj(dt);
705 struct osd_thandle *oh;
709 LASSERT(handle != NULL);
710 LASSERT(obj->oo_db != NULL);
711 LASSERT(osd_invariant(obj));
712 LASSERT(dt_object_exists(dt));
713 oh = container_of0(handle, struct osd_thandle, ot_super);
714 LASSERT(oh->ot_tx != NULL);
716 down(&obj->oo_guard);
717 rc = __osd_xattr_del(env, obj, name, oh);
724 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
727 nvpair_t *nvp = NULL;
728 int len, counted = 0, remain = lb->lb_len;
731 if (obj->oo_sa_xattr == NULL) {
732 rc = __osd_xattr_cache(env, obj);
737 LASSERT(obj->oo_sa_xattr);
739 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
740 len = strlen(nvpair_name(nvp));
741 if (lb->lb_buf != NULL) {
742 if (len + 1 > remain)
745 memcpy(lb->lb_buf, nvpair_name(nvp), len);
747 *((char *)lb->lb_buf) = '\0';
756 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
757 struct lu_buf *lb, struct lustre_capa *capa)
759 struct osd_thread_info *oti = osd_oti_get(env);
760 struct osd_object *obj = osd_dt_obj(dt);
761 struct osd_device *osd = osd_obj2dev(obj);
762 udmu_objset_t *uos = &osd->od_objset;
764 int rc, counted = 0, remain = lb->lb_len;
767 LASSERT(obj->oo_db != NULL);
768 LASSERT(osd_invariant(obj));
769 LASSERT(dt_object_exists(dt));
771 down(&obj->oo_guard);
773 rc = osd_sa_xattr_list(env, obj, lb);
779 /* continue with dnode xattr if any */
780 if (obj->oo_xattr == ZFS_NO_OBJECT)
781 GOTO(out, rc = counted);
783 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
787 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
789 rc = strlen(oti->oti_key);
790 if (lb->lb_buf != NULL) {
794 memcpy(lb->lb_buf, oti->oti_key, rc);
796 *((char *)lb->lb_buf) = '\0';
802 zap_cursor_advance(zc);
804 if (rc == -ENOENT) /* no more kes in the index */
806 else if (unlikely(rc < 0))
811 udmu_zap_cursor_fini(zc);