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, 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 <lustre_fsfilt.h>
53 #include <obd_support.h>
54 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
60 #include "osd_internal.h"
62 #include <sys/dnode.h>
67 #include <sys/spa_impl.h>
68 #include <sys/zfs_znode.h>
69 #include <sys/dmu_tx.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/dsl_prop.h>
72 #include <sys/sa_impl.h>
77 * Copy an extended attribute into the buffer provided, or compute the
78 * required buffer size.
80 * If buf is NULL, it computes the required buffer size.
82 * Returns 0 on success or a negative error number on failure.
83 * On success, the number of bytes used / required is stored in 'size'.
85 * No locking is done here.
87 int __osd_xattr_cache(const struct lu_env *env, struct osd_object *obj)
89 struct osd_device *osd = osd_obj2dev(obj);
90 udmu_objset_t *uos = &osd->od_objset;
96 LASSERT(obj->oo_sa_xattr == NULL);
97 LASSERT(obj->oo_db != NULL);
99 rc = -sa_handle_get(uos->os, obj->oo_db->db_object, NULL,
100 SA_HDL_PRIVATE, &sa_hdl);
104 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
107 rc = -nvlist_alloc(&obj->oo_sa_xattr,
108 NV_UNIQUE_NAME, KM_SLEEP);
112 buf = sa_spill_alloc(KM_SLEEP);
117 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
119 rc = -nvlist_unpack(buf, size, &obj->oo_sa_xattr, KM_SLEEP);
122 sa_handle_destroy(sa_hdl);
127 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
128 const struct lu_buf *buf, const char *name, int *sizep)
133 LASSERT(obj->oo_sa_hdl);
135 if (obj->oo_sa_xattr == NULL) {
136 rc = __osd_xattr_cache(env, obj);
141 LASSERT(obj->oo_sa_xattr);
142 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
147 if (buf == NULL || buf->lb_buf == NULL) {
148 /* return the required size by *sizep */
152 if (*sizep > buf->lb_len)
153 return -ERANGE; /* match ldiskfs error */
155 memcpy(buf->lb_buf, nv_value, *sizep);
159 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
160 struct lu_buf *buf, const char *name, int *sizep)
162 struct osd_device *osd = osd_obj2dev(obj);
163 udmu_objset_t *uos = &osd->od_objset;
164 uint64_t xa_data_obj;
165 dmu_buf_t *xa_data_db;
166 sa_handle_t *sa_hdl = NULL;
170 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
171 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
175 /* are there any extended attributes? */
176 if (obj->oo_xattr == ZFS_NO_OBJECT)
179 /* Lookup the object number containing the xattr data */
180 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
185 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
189 rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
194 /* Get the xattr value length / object size */
195 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
199 if (size > INT_MAX) {
206 if (buf == NULL || buf->lb_buf == NULL) {
207 /* We only need to return the required size */
210 if (*sizep > buf->lb_len) {
211 rc = -ERANGE; /* match ldiskfs error */
215 rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
216 size, buf->lb_buf, DMU_READ_PREFETCH);
219 sa_handle_destroy(sa_hdl);
221 dmu_buf_rele(xa_data_db, FTAG);
225 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
226 struct lu_buf *buf, const char *name,
227 struct lustre_capa *capa)
229 struct osd_object *obj = osd_dt_obj(dt);
233 LASSERT(obj->oo_db != NULL);
234 LASSERT(osd_invariant(obj));
235 LASSERT(dt_object_exists(dt));
237 down(&obj->oo_guard);
238 rc = __osd_xattr_get(env, obj, buf, name, &size);
248 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
249 int vallen, const char *name, struct osd_thandle *oh)
251 struct osd_device *osd = osd_obj2dev(obj);
252 udmu_objset_t *uos = &osd->od_objset;
253 dmu_buf_t *db = obj->oo_db;
254 dmu_tx_t *tx = oh->ot_tx;
255 uint64_t xa_data_obj;
259 here = dt_object_exists(&obj->oo_dt);
261 /* object may be not yet created */
264 LASSERT(obj->oo_sa_hdl);
265 /* we might just update SA_ZPL_DXATTR */
266 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
268 if (obj->oo_xattr == ZFS_NO_OBJECT)
272 if (!here || rc == -ENOENT) {
273 /* we'll be updating SA_ZPL_XATTR */
275 LASSERT(obj->oo_sa_hdl);
276 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
278 /* xattr zap + entry */
279 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
280 /* xattr value obj */
281 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
282 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
286 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
290 * Entry already exists.
291 * We'll truncate the existing object.
293 dmu_tx_hold_bonus(tx, xa_data_obj);
294 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
295 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
297 } else if (rc == -ENOENT) {
299 * Entry doesn't exist, we need to create a new one and a new
300 * object to store the value.
302 dmu_tx_hold_bonus(tx, obj->oo_xattr);
303 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
304 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
305 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
309 /* An error happened */
313 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
314 const struct lu_buf *buf, const char *name,
315 int fl, struct thandle *handle)
317 struct osd_object *obj = osd_dt_obj(dt);
318 struct osd_thandle *oh;
321 LASSERT(handle != NULL);
322 oh = container_of0(handle, struct osd_thandle, ot_super);
324 down(&obj->oo_guard);
325 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
332 * Set an extended attribute.
333 * This transaction must have called udmu_xattr_declare_set() first.
335 * Returns 0 on success or a negative error number on failure.
337 * No locking is done here.
340 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
341 struct osd_thandle *oh)
343 struct osd_device *osd = osd_obj2dev(obj);
344 udmu_objset_t *uos = &osd->od_objset;
350 LASSERT(obj->oo_sa_hdl);
351 LASSERT(obj->oo_sa_xattr);
353 /* Update the SA for additions, modifications, and removals. */
354 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
358 dxattr = sa_spill_alloc(KM_SLEEP);
362 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
363 NV_ENCODE_XDR, KM_SLEEP);
367 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
369 sa_spill_free(dxattr);
373 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
374 const struct lu_buf *buf, const char *name, int fl,
375 struct osd_thandle *oh)
383 LASSERT(obj->oo_sa_hdl);
384 if (obj->oo_sa_xattr == NULL) {
385 rc = __osd_xattr_cache(env, obj);
390 LASSERT(obj->oo_sa_xattr);
391 /* Limited to 32k to keep nvpair memory allocations small */
392 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
395 /* Prevent the DXATTR SA from consuming the entire SA
397 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
401 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
405 /* even in case of -EFBIG we must lookup xattr and check can we
406 * rewrite it then delete from SA */
407 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
410 if (fl & LU_XATTR_CREATE) {
412 } else if (too_big) {
413 rc = -nvlist_remove(obj->oo_sa_xattr, name,
414 DATA_TYPE_BYTE_ARRAY);
417 rc = __osd_sa_xattr_update(env, obj, oh);
418 return rc == 0 ? -EFBIG : rc;
420 } else if (rc == -ENOENT) {
421 if (fl & LU_XATTR_REPLACE)
429 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
430 (uchar_t *)buf->lb_buf, buf->lb_len);
434 rc = __osd_sa_xattr_update(env, obj, oh);
439 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
440 const struct lu_buf *buf, const char *name, int fl,
441 struct osd_thandle *oh)
443 struct osd_device *osd = osd_obj2dev(obj);
444 udmu_objset_t *uos = &osd->od_objset;
445 dmu_buf_t *xa_zap_db = NULL;
446 dmu_buf_t *xa_data_db = NULL;
447 uint64_t xa_data_obj;
448 sa_handle_t *sa_hdl = NULL;
449 dmu_tx_t *tx = oh->ot_tx;
453 LASSERT(obj->oo_sa_hdl);
455 if (obj->oo_xattr == ZFS_NO_OBJECT) {
456 struct lu_attr *la = &osd_oti_get(env)->oti_la;
458 la->la_valid = LA_MODE;
459 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
460 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la, FTAG, 0);
464 obj->oo_xattr = xa_zap_db->db_object;
465 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
466 &obj->oo_xattr, 8, oh);
471 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
474 if (fl & LU_XATTR_CREATE) {
479 * Entry already exists.
480 * We'll truncate the existing object.
482 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
487 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
488 SA_HDL_PRIVATE, &sa_hdl);
492 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
496 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
497 0, DMU_OBJECT_END, tx);
500 } else if (rc == -ENOENT) {
501 struct lu_attr *la = &osd_oti_get(env)->oti_la;
503 * Entry doesn't exist, we need to create a new one and a new
504 * object to store the value.
506 if (fl & LU_XATTR_REPLACE) {
507 /* should be ENOATTR according to the
508 * man, but that is undefined here */
513 la->la_valid = LA_MODE;
514 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
515 rc = __osd_object_create(env, uos, &xa_data_db, tx, la, FTAG);
518 xa_data_obj = xa_data_db->db_object;
520 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
521 SA_HDL_PRIVATE, &sa_hdl);
525 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
526 1, &xa_data_obj, tx);
530 /* There was an error looking up the xattr name */
534 /* Finally write the xattr value */
535 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
538 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
541 sa_handle_destroy(sa_hdl);
543 if (xa_data_db != NULL)
544 dmu_buf_rele(xa_data_db, FTAG);
545 if (xa_zap_db != NULL)
546 dmu_buf_rele(xa_zap_db, FTAG);
551 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
552 const struct lu_buf *buf, const char *name, int fl,
553 struct thandle *handle, struct lustre_capa *capa)
555 struct osd_object *obj = osd_dt_obj(dt);
556 struct osd_thandle *oh;
560 LASSERT(handle != NULL);
561 LASSERT(osd_invariant(obj));
562 LASSERT(dt_object_exists(dt));
565 oh = container_of0(handle, struct osd_thandle, ot_super);
567 down(&obj->oo_guard);
568 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
569 name, (int)buf->lb_len);
570 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
577 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
578 const char *name, struct osd_thandle *oh)
580 struct osd_device *osd = osd_obj2dev(obj);
581 udmu_objset_t *uos = &osd->od_objset;
582 dmu_tx_t *tx = oh->ot_tx;
583 uint64_t xa_data_obj;
586 /* update SA_ZPL_DXATTR if xattr was in SA */
587 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
589 if (obj->oo_xattr == ZFS_NO_OBJECT)
592 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
596 * We'll delete the existing object and ZAP entry.
598 dmu_tx_hold_bonus(tx, xa_data_obj);
599 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
600 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
602 } else if (rc == -ENOENT) {
604 * Entry doesn't exist, nothing to be changed.
609 /* An error happened */
613 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
614 const char *name, struct thandle *handle)
616 struct osd_object *obj = osd_dt_obj(dt);
617 struct osd_thandle *oh;
620 LASSERT(handle != NULL);
621 LASSERT(dt_object_exists(dt));
622 LASSERT(osd_invariant(obj));
624 oh = container_of0(handle, struct osd_thandle, ot_super);
625 LASSERT(oh->ot_tx != NULL);
626 LASSERT(obj->oo_db != NULL);
628 down(&obj->oo_guard);
629 __osd_xattr_declare_del(env, obj, name, oh);
635 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
636 const char *name, struct osd_thandle *oh)
640 if (obj->oo_sa_xattr == NULL) {
641 rc = __osd_xattr_cache(env, obj);
646 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
648 rc = __osd_sa_xattr_update(env, obj, oh);
652 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
653 const char *name, struct osd_thandle *oh)
655 struct osd_device *osd = osd_obj2dev(obj);
656 udmu_objset_t *uos = &osd->od_objset;
657 uint64_t xa_data_obj;
660 /* try remove xattr from SA at first */
661 rc = __osd_sa_xattr_del(env, obj, name, oh);
665 if (obj->oo_xattr == ZFS_NO_OBJECT)
668 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
672 } else if (rc == 0) {
675 * We'll delete the existing object and ZAP entry.
677 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
681 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
687 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
688 const char *name, struct thandle *handle,
689 struct lustre_capa *capa)
691 struct osd_object *obj = osd_dt_obj(dt);
692 struct osd_thandle *oh;
696 LASSERT(handle != NULL);
697 LASSERT(obj->oo_db != NULL);
698 LASSERT(osd_invariant(obj));
699 LASSERT(dt_object_exists(dt));
700 oh = container_of0(handle, struct osd_thandle, ot_super);
701 LASSERT(oh->ot_tx != NULL);
703 down(&obj->oo_guard);
704 rc = __osd_xattr_del(env, obj, name, oh);
711 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
714 nvpair_t *nvp = NULL;
715 int len, counted = 0, remain = lb->lb_len;
718 if (obj->oo_sa_xattr == NULL) {
719 rc = __osd_xattr_cache(env, obj);
724 LASSERT(obj->oo_sa_xattr);
726 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
727 len = strlen(nvpair_name(nvp));
728 if (lb->lb_buf != NULL) {
729 if (len + 1 > remain)
732 memcpy(lb->lb_buf, nvpair_name(nvp), len);
734 *((char *)lb->lb_buf) = '\0';
743 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
744 struct lu_buf *lb, struct lustre_capa *capa)
746 struct osd_thread_info *oti = osd_oti_get(env);
747 struct osd_object *obj = osd_dt_obj(dt);
748 struct osd_device *osd = osd_obj2dev(obj);
749 udmu_objset_t *uos = &osd->od_objset;
751 int rc, counted = 0, remain = lb->lb_len;
754 LASSERT(obj->oo_db != NULL);
755 LASSERT(osd_invariant(obj));
756 LASSERT(dt_object_exists(dt));
758 down(&obj->oo_guard);
760 rc = osd_sa_xattr_list(env, obj, lb);
766 /* continue with dnode xattr if any */
767 if (obj->oo_xattr == ZFS_NO_OBJECT)
768 GOTO(out, rc = counted);
770 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
774 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
776 rc = strlen(oti->oti_key);
777 if (lb->lb_buf != NULL) {
781 memcpy(lb->lb_buf, oti->oti_key, rc);
783 *((char *)lb->lb_buf) = '\0';
789 zap_cursor_advance(zc);
796 udmu_zap_cursor_fini(zc);