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>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_OSD
50 #include <lustre_ver.h>
51 #include <libcfs/libcfs.h>
52 #include <obd_support.h>
53 #include <lustre_net.h>
55 #include <obd_class.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
59 #include "osd_internal.h"
61 #include <sys/dnode.h>
66 #include <sys/spa_impl.h>
67 #include <sys/zfs_znode.h>
68 #include <sys/dmu_tx.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/dsl_prop.h>
71 #include <sys/sa_impl.h>
76 * Copy an extended attribute into the buffer provided, or compute the
77 * required buffer size.
79 * If buf is NULL, it computes the required buffer size.
81 * Returns 0 on success or a negative error number on failure.
82 * On success, the number of bytes used / required is stored in 'size'.
84 * No locking is done here.
86 int __osd_xattr_load(udmu_objset_t *uos, uint64_t dnode, nvlist_t **sa_xattr)
92 if (unlikely(dnode == ZFS_NO_OBJECT))
95 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
99 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
102 rc = -nvlist_alloc(sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
106 buf = sa_spill_alloc(KM_SLEEP);
111 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
113 rc = -nvlist_unpack(buf, size, sa_xattr, KM_SLEEP);
116 sa_handle_destroy(sa_hdl);
121 static inline int __osd_xattr_cache(const struct lu_env *env,
122 struct osd_object *obj)
124 LASSERT(obj->oo_sa_xattr == NULL);
125 LASSERT(obj->oo_db != NULL);
127 return __osd_xattr_load(&osd_obj2dev(obj)->od_objset,
128 obj->oo_db->db_object, &obj->oo_sa_xattr);
131 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
132 const struct lu_buf *buf, const char *name, int *sizep)
137 LASSERT(obj->oo_sa_hdl);
139 if (obj->oo_sa_xattr == NULL) {
140 rc = __osd_xattr_cache(env, obj);
145 LASSERT(obj->oo_sa_xattr);
146 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
151 if (buf == NULL || buf->lb_buf == NULL) {
152 /* return the required size by *sizep */
156 if (*sizep > buf->lb_len)
157 return -ERANGE; /* match ldiskfs error */
159 memcpy(buf->lb_buf, nv_value, *sizep);
163 int __osd_xattr_get_large(const struct lu_env *env, udmu_objset_t *uos,
164 uint64_t xattr, struct lu_buf *buf,
165 const char *name, int *sizep)
167 dmu_buf_t *xa_data_db;
168 sa_handle_t *sa_hdl = NULL;
169 uint64_t xa_data_obj, size;
172 /* are there any extended attributes? */
173 if (xattr == ZFS_NO_OBJECT)
176 /* Lookup the object number containing the xattr data */
177 rc = -zap_lookup(uos->os, xattr, name, sizeof(uint64_t), 1,
182 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
186 rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
191 /* Get the xattr value length / object size */
192 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
196 if (size > INT_MAX) {
203 if (buf == NULL || buf->lb_buf == NULL) {
204 /* We only need to return the required size */
207 if (*sizep > buf->lb_len) {
208 rc = -ERANGE; /* match ldiskfs error */
212 rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
213 size, buf->lb_buf, DMU_READ_PREFETCH);
216 sa_handle_destroy(sa_hdl);
218 dmu_buf_rele(xa_data_db, FTAG);
223 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
224 struct lu_buf *buf, const char *name, int *sizep)
228 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
229 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
233 rc = __osd_xattr_get_large(env, &osd_obj2dev(obj)->od_objset,
234 obj->oo_xattr, buf, name, sizep);
239 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
240 struct lu_buf *buf, const char *name,
241 struct lustre_capa *capa)
243 struct osd_object *obj = osd_dt_obj(dt);
247 LASSERT(obj->oo_db != NULL);
248 LASSERT(osd_invariant(obj));
249 LASSERT(dt_object_exists(dt));
251 down(&obj->oo_guard);
252 rc = __osd_xattr_get(env, obj, buf, name, &size);
262 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
263 int vallen, const char *name, struct osd_thandle *oh)
265 struct osd_device *osd = osd_obj2dev(obj);
266 udmu_objset_t *uos = &osd->od_objset;
267 dmu_buf_t *db = obj->oo_db;
268 dmu_tx_t *tx = oh->ot_tx;
269 uint64_t xa_data_obj;
273 here = dt_object_exists(&obj->oo_dt);
275 /* object may be not yet created */
278 LASSERT(obj->oo_sa_hdl);
279 /* we might just update SA_ZPL_DXATTR */
280 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
282 if (obj->oo_xattr == ZFS_NO_OBJECT)
286 if (!here || rc == -ENOENT) {
287 /* we'll be updating SA_ZPL_XATTR */
289 LASSERT(obj->oo_sa_hdl);
290 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
292 /* xattr zap + entry */
293 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
294 /* xattr value obj */
295 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
296 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
300 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
304 * Entry already exists.
305 * We'll truncate the existing object.
307 dmu_tx_hold_bonus(tx, xa_data_obj);
308 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
309 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
311 } else if (rc == -ENOENT) {
313 * Entry doesn't exist, we need to create a new one and a new
314 * object to store the value.
316 dmu_tx_hold_bonus(tx, obj->oo_xattr);
317 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
318 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
319 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
323 /* An error happened */
327 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
328 const struct lu_buf *buf, const char *name,
329 int fl, struct thandle *handle)
331 struct osd_object *obj = osd_dt_obj(dt);
332 struct osd_thandle *oh;
335 LASSERT(handle != NULL);
336 oh = container_of0(handle, struct osd_thandle, ot_super);
338 down(&obj->oo_guard);
339 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
346 * Set an extended attribute.
347 * This transaction must have called udmu_xattr_declare_set() first.
349 * Returns 0 on success or a negative error number on failure.
351 * No locking is done here.
354 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
355 struct osd_thandle *oh)
357 struct osd_device *osd = osd_obj2dev(obj);
358 udmu_objset_t *uos = &osd->od_objset;
364 LASSERT(obj->oo_sa_hdl);
365 LASSERT(obj->oo_sa_xattr);
367 /* Update the SA for additions, modifications, and removals. */
368 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
372 dxattr = sa_spill_alloc(KM_SLEEP);
376 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
377 NV_ENCODE_XDR, KM_SLEEP);
381 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
383 sa_spill_free(dxattr);
387 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
388 const struct lu_buf *buf, const char *name, int fl,
389 struct osd_thandle *oh)
397 LASSERT(obj->oo_sa_hdl);
398 if (obj->oo_sa_xattr == NULL) {
399 rc = __osd_xattr_cache(env, obj);
404 LASSERT(obj->oo_sa_xattr);
405 /* Limited to 32k to keep nvpair memory allocations small */
406 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
409 /* Prevent the DXATTR SA from consuming the entire SA
411 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
415 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
419 /* even in case of -EFBIG we must lookup xattr and check can we
420 * rewrite it then delete from SA */
421 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
424 if (fl & LU_XATTR_CREATE) {
426 } else if (too_big) {
427 rc = -nvlist_remove(obj->oo_sa_xattr, name,
428 DATA_TYPE_BYTE_ARRAY);
431 rc = __osd_sa_xattr_update(env, obj, oh);
432 return rc == 0 ? -EFBIG : rc;
434 } else if (rc == -ENOENT) {
435 if (fl & LU_XATTR_REPLACE)
443 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
444 (uchar_t *)buf->lb_buf, buf->lb_len);
448 rc = __osd_sa_xattr_update(env, obj, oh);
453 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
454 const struct lu_buf *buf, const char *name, int fl,
455 struct osd_thandle *oh)
457 struct osd_device *osd = osd_obj2dev(obj);
458 udmu_objset_t *uos = &osd->od_objset;
459 dmu_buf_t *xa_zap_db = NULL;
460 dmu_buf_t *xa_data_db = NULL;
461 uint64_t xa_data_obj;
462 sa_handle_t *sa_hdl = NULL;
463 dmu_tx_t *tx = oh->ot_tx;
467 LASSERT(obj->oo_sa_hdl);
469 if (obj->oo_xattr == ZFS_NO_OBJECT) {
470 struct lu_attr *la = &osd_oti_get(env)->oti_la;
472 la->la_valid = LA_MODE;
473 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
474 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
475 obj->oo_db->db_object, FTAG, 0);
479 obj->oo_xattr = xa_zap_db->db_object;
480 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
481 &obj->oo_xattr, 8, oh);
486 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
489 if (fl & LU_XATTR_CREATE) {
494 * Entry already exists.
495 * We'll truncate the existing object.
497 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
502 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
503 SA_HDL_PRIVATE, &sa_hdl);
507 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
511 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
512 0, DMU_OBJECT_END, tx);
515 } else if (rc == -ENOENT) {
516 struct lu_attr *la = &osd_oti_get(env)->oti_la;
518 * Entry doesn't exist, we need to create a new one and a new
519 * object to store the value.
521 if (fl & LU_XATTR_REPLACE) {
522 /* should be ENOATTR according to the
523 * man, but that is undefined here */
528 la->la_valid = LA_MODE;
529 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
530 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
531 obj->oo_xattr, FTAG);
534 xa_data_obj = xa_data_db->db_object;
536 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
537 SA_HDL_PRIVATE, &sa_hdl);
541 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
542 1, &xa_data_obj, tx);
546 /* There was an error looking up the xattr name */
550 /* Finally write the xattr value */
551 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
554 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
557 sa_handle_destroy(sa_hdl);
559 if (xa_data_db != NULL)
560 dmu_buf_rele(xa_data_db, FTAG);
561 if (xa_zap_db != NULL)
562 dmu_buf_rele(xa_zap_db, FTAG);
567 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
568 const struct lu_buf *buf, const char *name, int fl,
569 struct thandle *handle, struct lustre_capa *capa)
571 struct osd_object *obj = osd_dt_obj(dt);
572 struct osd_thandle *oh;
576 LASSERT(handle != NULL);
577 LASSERT(osd_invariant(obj));
578 LASSERT(dt_object_exists(dt));
581 oh = container_of0(handle, struct osd_thandle, ot_super);
583 down(&obj->oo_guard);
584 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
585 name, (int)buf->lb_len);
586 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
593 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
594 const char *name, struct osd_thandle *oh)
596 struct osd_device *osd = osd_obj2dev(obj);
597 udmu_objset_t *uos = &osd->od_objset;
598 dmu_tx_t *tx = oh->ot_tx;
599 uint64_t xa_data_obj;
602 /* update SA_ZPL_DXATTR if xattr was in SA */
603 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
605 if (obj->oo_xattr == ZFS_NO_OBJECT)
608 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
612 * We'll delete the existing object and ZAP entry.
614 dmu_tx_hold_bonus(tx, xa_data_obj);
615 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
616 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
618 } else if (rc == -ENOENT) {
620 * Entry doesn't exist, nothing to be changed.
625 /* An error happened */
629 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
630 const char *name, struct thandle *handle)
632 struct osd_object *obj = osd_dt_obj(dt);
633 struct osd_thandle *oh;
636 LASSERT(handle != NULL);
637 LASSERT(dt_object_exists(dt));
638 LASSERT(osd_invariant(obj));
640 oh = container_of0(handle, struct osd_thandle, ot_super);
641 LASSERT(oh->ot_tx != NULL);
642 LASSERT(obj->oo_db != NULL);
644 down(&obj->oo_guard);
645 __osd_xattr_declare_del(env, obj, name, oh);
651 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
652 const char *name, struct osd_thandle *oh)
656 if (obj->oo_sa_xattr == NULL) {
657 rc = __osd_xattr_cache(env, obj);
662 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
664 rc = __osd_sa_xattr_update(env, obj, oh);
668 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
669 const char *name, struct osd_thandle *oh)
671 struct osd_device *osd = osd_obj2dev(obj);
672 udmu_objset_t *uos = &osd->od_objset;
673 uint64_t xa_data_obj;
676 /* try remove xattr from SA at first */
677 rc = __osd_sa_xattr_del(env, obj, name, oh);
681 if (obj->oo_xattr == ZFS_NO_OBJECT)
684 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
688 } else if (rc == 0) {
691 * We'll delete the existing object and ZAP entry.
693 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
697 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
703 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
704 const char *name, struct thandle *handle,
705 struct lustre_capa *capa)
707 struct osd_object *obj = osd_dt_obj(dt);
708 struct osd_thandle *oh;
712 LASSERT(handle != NULL);
713 LASSERT(obj->oo_db != NULL);
714 LASSERT(osd_invariant(obj));
715 LASSERT(dt_object_exists(dt));
716 oh = container_of0(handle, struct osd_thandle, ot_super);
717 LASSERT(oh->ot_tx != NULL);
719 down(&obj->oo_guard);
720 rc = __osd_xattr_del(env, obj, name, oh);
727 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
730 nvpair_t *nvp = NULL;
731 int len, counted = 0, remain = lb->lb_len;
734 if (obj->oo_sa_xattr == NULL) {
735 rc = __osd_xattr_cache(env, obj);
740 LASSERT(obj->oo_sa_xattr);
742 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
743 len = strlen(nvpair_name(nvp));
744 if (lb->lb_buf != NULL) {
745 if (len + 1 > remain)
748 memcpy(lb->lb_buf, nvpair_name(nvp), len);
750 *((char *)lb->lb_buf) = '\0';
759 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
760 struct lu_buf *lb, struct lustre_capa *capa)
762 struct osd_thread_info *oti = osd_oti_get(env);
763 struct osd_object *obj = osd_dt_obj(dt);
764 struct osd_device *osd = osd_obj2dev(obj);
765 udmu_objset_t *uos = &osd->od_objset;
767 int rc, counted = 0, remain = lb->lb_len;
770 LASSERT(obj->oo_db != NULL);
771 LASSERT(osd_invariant(obj));
772 LASSERT(dt_object_exists(dt));
774 down(&obj->oo_guard);
776 rc = osd_sa_xattr_list(env, obj, lb);
782 /* continue with dnode xattr if any */
783 if (obj->oo_xattr == ZFS_NO_OBJECT)
784 GOTO(out, rc = counted);
786 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
790 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
792 rc = strlen(oti->oti_key);
793 if (lb->lb_buf != NULL) {
797 memcpy(lb->lb_buf, oti->oti_key, rc);
799 *((char *)lb->lb_buf) = '\0';
805 zap_cursor_advance(zc);
807 if (rc == -ENOENT) /* no more kes in the index */
809 else if (unlikely(rc < 0))
814 udmu_zap_cursor_fini(zc);