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,
461 obj->oo_db->db_object, FTAG, 0);
465 obj->oo_xattr = xa_zap_db->db_object;
466 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
467 &obj->oo_xattr, 8, oh);
472 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
475 if (fl & LU_XATTR_CREATE) {
480 * Entry already exists.
481 * We'll truncate the existing object.
483 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
488 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
489 SA_HDL_PRIVATE, &sa_hdl);
493 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
497 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
498 0, DMU_OBJECT_END, tx);
501 } else if (rc == -ENOENT) {
502 struct lu_attr *la = &osd_oti_get(env)->oti_la;
504 * Entry doesn't exist, we need to create a new one and a new
505 * object to store the value.
507 if (fl & LU_XATTR_REPLACE) {
508 /* should be ENOATTR according to the
509 * man, but that is undefined here */
514 la->la_valid = LA_MODE;
515 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
516 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
517 obj->oo_xattr, FTAG);
520 xa_data_obj = xa_data_db->db_object;
522 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
523 SA_HDL_PRIVATE, &sa_hdl);
527 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
528 1, &xa_data_obj, tx);
532 /* There was an error looking up the xattr name */
536 /* Finally write the xattr value */
537 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
540 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
543 sa_handle_destroy(sa_hdl);
545 if (xa_data_db != NULL)
546 dmu_buf_rele(xa_data_db, FTAG);
547 if (xa_zap_db != NULL)
548 dmu_buf_rele(xa_zap_db, FTAG);
553 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
554 const struct lu_buf *buf, const char *name, int fl,
555 struct thandle *handle, struct lustre_capa *capa)
557 struct osd_object *obj = osd_dt_obj(dt);
558 struct osd_thandle *oh;
562 LASSERT(handle != NULL);
563 LASSERT(osd_invariant(obj));
564 LASSERT(dt_object_exists(dt));
567 oh = container_of0(handle, struct osd_thandle, ot_super);
569 down(&obj->oo_guard);
570 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
571 name, (int)buf->lb_len);
572 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
579 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
580 const char *name, struct osd_thandle *oh)
582 struct osd_device *osd = osd_obj2dev(obj);
583 udmu_objset_t *uos = &osd->od_objset;
584 dmu_tx_t *tx = oh->ot_tx;
585 uint64_t xa_data_obj;
588 /* update SA_ZPL_DXATTR if xattr was in SA */
589 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
591 if (obj->oo_xattr == ZFS_NO_OBJECT)
594 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
598 * We'll delete the existing object and ZAP entry.
600 dmu_tx_hold_bonus(tx, xa_data_obj);
601 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
602 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
604 } else if (rc == -ENOENT) {
606 * Entry doesn't exist, nothing to be changed.
611 /* An error happened */
615 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
616 const char *name, struct thandle *handle)
618 struct osd_object *obj = osd_dt_obj(dt);
619 struct osd_thandle *oh;
622 LASSERT(handle != NULL);
623 LASSERT(dt_object_exists(dt));
624 LASSERT(osd_invariant(obj));
626 oh = container_of0(handle, struct osd_thandle, ot_super);
627 LASSERT(oh->ot_tx != NULL);
628 LASSERT(obj->oo_db != NULL);
630 down(&obj->oo_guard);
631 __osd_xattr_declare_del(env, obj, name, oh);
637 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
638 const char *name, struct osd_thandle *oh)
642 if (obj->oo_sa_xattr == NULL) {
643 rc = __osd_xattr_cache(env, obj);
648 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
650 rc = __osd_sa_xattr_update(env, obj, oh);
654 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
655 const char *name, struct osd_thandle *oh)
657 struct osd_device *osd = osd_obj2dev(obj);
658 udmu_objset_t *uos = &osd->od_objset;
659 uint64_t xa_data_obj;
662 /* try remove xattr from SA at first */
663 rc = __osd_sa_xattr_del(env, obj, name, oh);
667 if (obj->oo_xattr == ZFS_NO_OBJECT)
670 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
674 } else if (rc == 0) {
677 * We'll delete the existing object and ZAP entry.
679 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
683 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
689 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
690 const char *name, struct thandle *handle,
691 struct lustre_capa *capa)
693 struct osd_object *obj = osd_dt_obj(dt);
694 struct osd_thandle *oh;
698 LASSERT(handle != NULL);
699 LASSERT(obj->oo_db != NULL);
700 LASSERT(osd_invariant(obj));
701 LASSERT(dt_object_exists(dt));
702 oh = container_of0(handle, struct osd_thandle, ot_super);
703 LASSERT(oh->ot_tx != NULL);
705 down(&obj->oo_guard);
706 rc = __osd_xattr_del(env, obj, name, oh);
713 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
716 nvpair_t *nvp = NULL;
717 int len, counted = 0, remain = lb->lb_len;
720 if (obj->oo_sa_xattr == NULL) {
721 rc = __osd_xattr_cache(env, obj);
726 LASSERT(obj->oo_sa_xattr);
728 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
729 len = strlen(nvpair_name(nvp));
730 if (lb->lb_buf != NULL) {
731 if (len + 1 > remain)
734 memcpy(lb->lb_buf, nvpair_name(nvp), len);
736 *((char *)lb->lb_buf) = '\0';
745 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
746 struct lu_buf *lb, struct lustre_capa *capa)
748 struct osd_thread_info *oti = osd_oti_get(env);
749 struct osd_object *obj = osd_dt_obj(dt);
750 struct osd_device *osd = osd_obj2dev(obj);
751 udmu_objset_t *uos = &osd->od_objset;
753 int rc, counted = 0, remain = lb->lb_len;
756 LASSERT(obj->oo_db != NULL);
757 LASSERT(osd_invariant(obj));
758 LASSERT(dt_object_exists(dt));
760 down(&obj->oo_guard);
762 rc = osd_sa_xattr_list(env, obj, lb);
768 /* continue with dnode xattr if any */
769 if (obj->oo_xattr == ZFS_NO_OBJECT)
770 GOTO(out, rc = counted);
772 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
776 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
778 rc = strlen(oti->oti_key);
779 if (lb->lb_buf != NULL) {
783 memcpy(lb->lb_buf, oti->oti_key, rc);
785 *((char *)lb->lb_buf) = '\0';
791 zap_cursor_advance(zc);
798 udmu_zap_cursor_fini(zc);