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>
71 #include <linux/posix_acl_xattr.h>
74 * Copy an extended attribute into the buffer provided, or compute the
75 * required buffer size.
77 * If buf is NULL, it computes the required buffer size.
79 * Returns 0 on success or a negative error number on failure.
80 * On success, the number of bytes used / required is stored in 'size'.
82 * No locking is done here.
84 int __osd_xattr_load(udmu_objset_t *uos, uint64_t dnode, nvlist_t **sa_xattr)
90 if (unlikely(dnode == ZFS_NO_OBJECT))
93 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
97 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
100 rc = -nvlist_alloc(sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
104 buf = sa_spill_alloc(KM_SLEEP);
109 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
111 rc = -nvlist_unpack(buf, size, sa_xattr, KM_SLEEP);
114 sa_handle_destroy(sa_hdl);
119 static inline int __osd_xattr_cache(const struct lu_env *env,
120 struct osd_object *obj)
122 LASSERT(obj->oo_sa_xattr == NULL);
123 LASSERT(obj->oo_db != NULL);
125 return __osd_xattr_load(&osd_obj2dev(obj)->od_objset,
126 obj->oo_db->db_object, &obj->oo_sa_xattr);
129 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
130 const struct lu_buf *buf, const char *name, int *sizep)
135 LASSERT(obj->oo_sa_hdl);
137 if (obj->oo_sa_xattr == NULL) {
138 rc = __osd_xattr_cache(env, obj);
143 LASSERT(obj->oo_sa_xattr);
144 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
149 if (buf == NULL || buf->lb_buf == NULL) {
150 /* return the required size by *sizep */
154 if (*sizep > buf->lb_len)
155 return -ERANGE; /* match ldiskfs error */
157 memcpy(buf->lb_buf, nv_value, *sizep);
161 int __osd_xattr_get_large(const struct lu_env *env, udmu_objset_t *uos,
162 uint64_t xattr, struct lu_buf *buf,
163 const char *name, int *sizep)
165 dmu_buf_t *xa_data_db;
166 sa_handle_t *sa_hdl = NULL;
167 uint64_t xa_data_obj, size;
170 /* are there any extended attributes? */
171 if (xattr == ZFS_NO_OBJECT)
174 /* Lookup the object number containing the xattr data */
175 rc = -zap_lookup(uos->os, xattr, name, sizeof(uint64_t), 1,
180 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
184 rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
189 /* Get the xattr value length / object size */
190 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
194 if (size > INT_MAX) {
201 if (buf == NULL || buf->lb_buf == NULL) {
202 /* We only need to return the required size */
205 if (*sizep > buf->lb_len) {
206 rc = -ERANGE; /* match ldiskfs error */
210 rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
211 size, buf->lb_buf, DMU_READ_PREFETCH);
214 sa_handle_destroy(sa_hdl);
216 dmu_buf_rele(xa_data_db, FTAG);
221 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
222 struct lu_buf *buf, const char *name, int *sizep)
226 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
227 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
231 rc = __osd_xattr_get_large(env, &osd_obj2dev(obj)->od_objset,
232 obj->oo_xattr, buf, name, sizep);
237 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
238 struct lu_buf *buf, const char *name,
239 struct lustre_capa *capa)
241 struct osd_object *obj = osd_dt_obj(dt);
245 LASSERT(obj->oo_db != NULL);
246 LASSERT(osd_invariant(obj));
247 LASSERT(dt_object_exists(dt));
249 if (!osd_obj2dev(obj)->od_posix_acl &&
250 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
251 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
254 down(&obj->oo_guard);
255 rc = __osd_xattr_get(env, obj, buf, name, &size);
265 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
266 int vallen, const char *name, struct osd_thandle *oh)
268 struct osd_device *osd = osd_obj2dev(obj);
269 udmu_objset_t *uos = &osd->od_objset;
270 dmu_buf_t *db = obj->oo_db;
271 dmu_tx_t *tx = oh->ot_tx;
272 uint64_t xa_data_obj;
276 here = dt_object_exists(&obj->oo_dt);
278 /* object may be not yet created */
281 LASSERT(obj->oo_sa_hdl);
282 /* we might just update SA_ZPL_DXATTR */
283 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
285 if (obj->oo_xattr == ZFS_NO_OBJECT)
289 if (!here || rc == -ENOENT) {
290 /* we'll be updating SA_ZPL_XATTR */
292 LASSERT(obj->oo_sa_hdl);
293 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
295 /* xattr zap + entry */
296 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
297 /* xattr value obj */
298 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
299 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
303 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
307 * Entry already exists.
308 * We'll truncate the existing object.
310 dmu_tx_hold_bonus(tx, xa_data_obj);
311 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
312 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
314 } else if (rc == -ENOENT) {
316 * Entry doesn't exist, we need to create a new one and a new
317 * object to store the value.
319 dmu_tx_hold_bonus(tx, obj->oo_xattr);
320 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
321 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
322 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
326 /* An error happened */
330 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
331 const struct lu_buf *buf, const char *name,
332 int fl, struct thandle *handle)
334 struct osd_object *obj = osd_dt_obj(dt);
335 struct osd_thandle *oh;
338 LASSERT(handle != NULL);
339 oh = container_of0(handle, struct osd_thandle, ot_super);
341 down(&obj->oo_guard);
342 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
349 * Set an extended attribute.
350 * This transaction must have called udmu_xattr_declare_set() first.
352 * Returns 0 on success or a negative error number on failure.
354 * No locking is done here.
357 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
358 struct osd_thandle *oh)
360 struct osd_device *osd = osd_obj2dev(obj);
361 udmu_objset_t *uos = &osd->od_objset;
367 LASSERT(obj->oo_sa_hdl);
368 LASSERT(obj->oo_sa_xattr);
370 /* Update the SA for additions, modifications, and removals. */
371 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
375 dxattr = sa_spill_alloc(KM_SLEEP);
379 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
380 NV_ENCODE_XDR, KM_SLEEP);
384 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
386 sa_spill_free(dxattr);
390 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
391 const struct lu_buf *buf, const char *name, int fl,
392 struct osd_thandle *oh)
400 LASSERT(obj->oo_sa_hdl);
401 if (obj->oo_sa_xattr == NULL) {
402 rc = __osd_xattr_cache(env, obj);
407 LASSERT(obj->oo_sa_xattr);
408 /* Limited to 32k to keep nvpair memory allocations small */
409 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
412 /* Prevent the DXATTR SA from consuming the entire SA
414 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
418 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
422 /* even in case of -EFBIG we must lookup xattr and check can we
423 * rewrite it then delete from SA */
424 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
427 if (fl & LU_XATTR_CREATE) {
429 } else if (too_big) {
430 rc = -nvlist_remove(obj->oo_sa_xattr, name,
431 DATA_TYPE_BYTE_ARRAY);
434 rc = __osd_sa_xattr_update(env, obj, oh);
435 return rc == 0 ? -EFBIG : rc;
437 } else if (rc == -ENOENT) {
438 if (fl & LU_XATTR_REPLACE)
446 /* Ensure xattr doesn't exist in ZAP */
447 if (obj->oo_xattr != ZFS_NO_OBJECT) {
448 udmu_objset_t *uos = &osd_obj2dev(obj)->od_objset;
449 uint64_t xa_data_obj;
450 rc = -zap_lookup(uos->os, obj->oo_xattr,
451 name, 8, 1, &xa_data_obj);
453 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
455 zap_remove(uos->os, obj->oo_xattr,
460 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
461 (uchar_t *)buf->lb_buf, buf->lb_len);
465 rc = __osd_sa_xattr_update(env, obj, oh);
470 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
471 const struct lu_buf *buf, const char *name, int fl,
472 struct osd_thandle *oh)
474 struct osd_device *osd = osd_obj2dev(obj);
475 udmu_objset_t *uos = &osd->od_objset;
476 dmu_buf_t *xa_zap_db = NULL;
477 dmu_buf_t *xa_data_db = NULL;
478 uint64_t xa_data_obj;
479 sa_handle_t *sa_hdl = NULL;
480 dmu_tx_t *tx = oh->ot_tx;
484 LASSERT(obj->oo_sa_hdl);
486 if (obj->oo_xattr == ZFS_NO_OBJECT) {
487 struct lu_attr *la = &osd_oti_get(env)->oti_la;
489 la->la_valid = LA_MODE;
490 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
491 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
492 obj->oo_db->db_object, FTAG, 0);
496 obj->oo_xattr = xa_zap_db->db_object;
497 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
498 &obj->oo_xattr, 8, oh);
503 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
506 if (fl & LU_XATTR_CREATE) {
511 * Entry already exists.
512 * We'll truncate the existing object.
514 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
519 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
520 SA_HDL_PRIVATE, &sa_hdl);
524 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
528 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
529 0, DMU_OBJECT_END, tx);
532 } else if (rc == -ENOENT) {
533 struct lu_attr *la = &osd_oti_get(env)->oti_la;
535 * Entry doesn't exist, we need to create a new one and a new
536 * object to store the value.
538 if (fl & LU_XATTR_REPLACE) {
539 /* should be ENOATTR according to the
540 * man, but that is undefined here */
545 la->la_valid = LA_MODE;
546 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
547 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
548 obj->oo_xattr, FTAG);
551 xa_data_obj = xa_data_db->db_object;
553 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
554 SA_HDL_PRIVATE, &sa_hdl);
558 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
559 1, &xa_data_obj, tx);
563 /* There was an error looking up the xattr name */
567 /* Finally write the xattr value */
568 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
571 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
574 sa_handle_destroy(sa_hdl);
576 if (xa_data_db != NULL)
577 dmu_buf_rele(xa_data_db, FTAG);
578 if (xa_zap_db != NULL)
579 dmu_buf_rele(xa_zap_db, FTAG);
584 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
585 const struct lu_buf *buf, const char *name, int fl,
586 struct thandle *handle, struct lustre_capa *capa)
588 struct osd_object *obj = osd_dt_obj(dt);
589 struct osd_thandle *oh;
593 LASSERT(handle != NULL);
594 LASSERT(osd_invariant(obj));
595 LASSERT(dt_object_exists(dt));
598 if (!osd_obj2dev(obj)->od_posix_acl &&
599 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
600 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
603 oh = container_of0(handle, struct osd_thandle, ot_super);
605 down(&obj->oo_guard);
606 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
607 name, (int)buf->lb_len);
608 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
615 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
616 const char *name, struct osd_thandle *oh)
618 struct osd_device *osd = osd_obj2dev(obj);
619 udmu_objset_t *uos = &osd->od_objset;
620 dmu_tx_t *tx = oh->ot_tx;
621 uint64_t xa_data_obj;
624 /* update SA_ZPL_DXATTR if xattr was in SA */
625 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
627 if (obj->oo_xattr == ZFS_NO_OBJECT)
630 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
634 * We'll delete the existing object and ZAP entry.
636 dmu_tx_hold_bonus(tx, xa_data_obj);
637 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
638 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
640 } else if (rc == -ENOENT) {
642 * Entry doesn't exist, nothing to be changed.
647 /* An error happened */
651 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
652 const char *name, struct thandle *handle)
654 struct osd_object *obj = osd_dt_obj(dt);
655 struct osd_thandle *oh;
658 LASSERT(handle != NULL);
659 LASSERT(dt_object_exists(dt));
660 LASSERT(osd_invariant(obj));
662 oh = container_of0(handle, struct osd_thandle, ot_super);
663 LASSERT(oh->ot_tx != NULL);
664 LASSERT(obj->oo_db != NULL);
666 down(&obj->oo_guard);
667 __osd_xattr_declare_del(env, obj, name, oh);
673 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
674 const char *name, struct osd_thandle *oh)
678 if (obj->oo_sa_xattr == NULL) {
679 rc = __osd_xattr_cache(env, obj);
684 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
686 rc = __osd_sa_xattr_update(env, obj, oh);
690 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
691 const char *name, struct osd_thandle *oh)
693 struct osd_device *osd = osd_obj2dev(obj);
694 udmu_objset_t *uos = &osd->od_objset;
695 uint64_t xa_data_obj;
698 /* try remove xattr from SA at first */
699 rc = __osd_sa_xattr_del(env, obj, name, oh);
703 if (obj->oo_xattr == ZFS_NO_OBJECT)
706 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
710 } else if (rc == 0) {
713 * We'll delete the existing object and ZAP entry.
715 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
719 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
725 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
726 const char *name, struct thandle *handle,
727 struct lustre_capa *capa)
729 struct osd_object *obj = osd_dt_obj(dt);
730 struct osd_thandle *oh;
734 LASSERT(handle != NULL);
735 LASSERT(obj->oo_db != NULL);
736 LASSERT(osd_invariant(obj));
737 LASSERT(dt_object_exists(dt));
738 oh = container_of0(handle, struct osd_thandle, ot_super);
739 LASSERT(oh->ot_tx != NULL);
741 if (!osd_obj2dev(obj)->od_posix_acl &&
742 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
743 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
746 down(&obj->oo_guard);
747 rc = __osd_xattr_del(env, obj, name, oh);
754 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
757 nvpair_t *nvp = NULL;
758 int len, counted = 0, remain = lb->lb_len;
761 if (obj->oo_sa_xattr == NULL) {
762 rc = __osd_xattr_cache(env, obj);
767 LASSERT(obj->oo_sa_xattr);
769 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
770 const char *name = nvpair_name(nvp);
772 if (!osd_obj2dev(obj)->od_posix_acl &&
773 (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
774 strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
777 len = strlen(nvpair_name(nvp));
778 if (lb->lb_buf != NULL) {
779 if (len + 1 > remain)
782 memcpy(lb->lb_buf, name, len);
784 *((char *)lb->lb_buf) = '\0';
793 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
794 struct lu_buf *lb, struct lustre_capa *capa)
796 struct osd_thread_info *oti = osd_oti_get(env);
797 struct osd_object *obj = osd_dt_obj(dt);
798 struct osd_device *osd = osd_obj2dev(obj);
799 udmu_objset_t *uos = &osd->od_objset;
801 int rc, counted = 0, remain = lb->lb_len;
804 LASSERT(obj->oo_db != NULL);
805 LASSERT(osd_invariant(obj));
806 LASSERT(dt_object_exists(dt));
808 down(&obj->oo_guard);
810 rc = osd_sa_xattr_list(env, obj, lb);
816 /* continue with dnode xattr if any */
817 if (obj->oo_xattr == ZFS_NO_OBJECT)
818 GOTO(out, rc = counted);
820 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
824 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
826 if (!osd_obj2dev(obj)->od_posix_acl &&
827 (strcmp(oti->oti_key, POSIX_ACL_XATTR_ACCESS) == 0 ||
828 strcmp(oti->oti_key, POSIX_ACL_XATTR_DEFAULT) == 0)) {
829 zap_cursor_advance(zc);
833 rc = strlen(oti->oti_key);
834 if (lb->lb_buf != NULL) {
838 memcpy(lb->lb_buf, oti->oti_key, rc);
840 *((char *)lb->lb_buf) = '\0';
846 zap_cursor_advance(zc);
848 if (rc == -ENOENT) /* no more kes in the index */
850 else if (unlikely(rc < 0))
855 udmu_zap_cursor_fini(zc);