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 <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_load(udmu_objset_t *uos, uint64_t dnode, nvlist_t **sa_xattr)
93 if (unlikely(dnode == ZFS_NO_OBJECT))
96 rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
100 rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
103 rc = -nvlist_alloc(sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
107 buf = sa_spill_alloc(KM_SLEEP);
112 rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
114 rc = -nvlist_unpack(buf, size, sa_xattr, KM_SLEEP);
117 sa_handle_destroy(sa_hdl);
122 static inline int __osd_xattr_cache(const struct lu_env *env,
123 struct osd_object *obj)
125 LASSERT(obj->oo_sa_xattr == NULL);
126 LASSERT(obj->oo_db != NULL);
128 return __osd_xattr_load(&osd_obj2dev(obj)->od_objset,
129 obj->oo_db->db_object, &obj->oo_sa_xattr);
132 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
133 const struct lu_buf *buf, const char *name, int *sizep)
138 LASSERT(obj->oo_sa_hdl);
140 if (obj->oo_sa_xattr == NULL) {
141 rc = __osd_xattr_cache(env, obj);
146 LASSERT(obj->oo_sa_xattr);
147 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
152 if (buf == NULL || buf->lb_buf == NULL) {
153 /* return the required size by *sizep */
157 if (*sizep > buf->lb_len)
158 return -ERANGE; /* match ldiskfs error */
160 memcpy(buf->lb_buf, nv_value, *sizep);
164 int __osd_xattr_get_large(const struct lu_env *env, udmu_objset_t *uos,
165 uint64_t xattr, struct lu_buf *buf,
166 const char *name, int *sizep)
168 dmu_buf_t *xa_data_db;
169 sa_handle_t *sa_hdl = NULL;
170 uint64_t xa_data_obj, size;
173 /* are there any extended attributes? */
174 if (xattr == ZFS_NO_OBJECT)
177 /* Lookup the object number containing the xattr data */
178 rc = -zap_lookup(uos->os, xattr, name, sizeof(uint64_t), 1,
183 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
187 rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
192 /* Get the xattr value length / object size */
193 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
197 if (size > INT_MAX) {
204 if (buf == NULL || buf->lb_buf == NULL) {
205 /* We only need to return the required size */
208 if (*sizep > buf->lb_len) {
209 rc = -ERANGE; /* match ldiskfs error */
213 rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
214 size, buf->lb_buf, DMU_READ_PREFETCH);
217 sa_handle_destroy(sa_hdl);
219 dmu_buf_rele(xa_data_db, FTAG);
224 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
225 struct lu_buf *buf, const char *name, int *sizep)
229 /* check SA_ZPL_DXATTR first then fallback to directory xattr */
230 rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
234 rc = __osd_xattr_get_large(env, &osd_obj2dev(obj)->od_objset,
235 obj->oo_xattr, buf, name, sizep);
240 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
241 struct lu_buf *buf, const char *name,
242 struct lustre_capa *capa)
244 struct osd_object *obj = osd_dt_obj(dt);
248 LASSERT(obj->oo_db != NULL);
249 LASSERT(osd_invariant(obj));
250 LASSERT(dt_object_exists(dt));
252 down(&obj->oo_guard);
253 rc = __osd_xattr_get(env, obj, buf, name, &size);
263 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
264 int vallen, const char *name, struct osd_thandle *oh)
266 struct osd_device *osd = osd_obj2dev(obj);
267 udmu_objset_t *uos = &osd->od_objset;
268 dmu_buf_t *db = obj->oo_db;
269 dmu_tx_t *tx = oh->ot_tx;
270 uint64_t xa_data_obj;
274 here = dt_object_exists(&obj->oo_dt);
276 /* object may be not yet created */
279 LASSERT(obj->oo_sa_hdl);
280 /* we might just update SA_ZPL_DXATTR */
281 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
283 if (obj->oo_xattr == ZFS_NO_OBJECT)
287 if (!here || rc == -ENOENT) {
288 /* we'll be updating SA_ZPL_XATTR */
290 LASSERT(obj->oo_sa_hdl);
291 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
293 /* xattr zap + entry */
294 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
295 /* xattr value obj */
296 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
297 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
301 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
305 * Entry already exists.
306 * We'll truncate the existing object.
308 dmu_tx_hold_bonus(tx, xa_data_obj);
309 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
310 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
312 } else if (rc == -ENOENT) {
314 * Entry doesn't exist, we need to create a new one and a new
315 * object to store the value.
317 dmu_tx_hold_bonus(tx, obj->oo_xattr);
318 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
319 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
320 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
324 /* An error happened */
328 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
329 const struct lu_buf *buf, const char *name,
330 int fl, struct thandle *handle)
332 struct osd_object *obj = osd_dt_obj(dt);
333 struct osd_thandle *oh;
336 LASSERT(handle != NULL);
337 oh = container_of0(handle, struct osd_thandle, ot_super);
339 down(&obj->oo_guard);
340 __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
347 * Set an extended attribute.
348 * This transaction must have called udmu_xattr_declare_set() first.
350 * Returns 0 on success or a negative error number on failure.
352 * No locking is done here.
355 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
356 struct osd_thandle *oh)
358 struct osd_device *osd = osd_obj2dev(obj);
359 udmu_objset_t *uos = &osd->od_objset;
365 LASSERT(obj->oo_sa_hdl);
366 LASSERT(obj->oo_sa_xattr);
368 /* Update the SA for additions, modifications, and removals. */
369 rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
373 dxattr = sa_spill_alloc(KM_SLEEP);
377 rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
378 NV_ENCODE_XDR, KM_SLEEP);
382 rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
384 sa_spill_free(dxattr);
388 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
389 const struct lu_buf *buf, const char *name, int fl,
390 struct osd_thandle *oh)
398 LASSERT(obj->oo_sa_hdl);
399 if (obj->oo_sa_xattr == NULL) {
400 rc = __osd_xattr_cache(env, obj);
405 LASSERT(obj->oo_sa_xattr);
406 /* Limited to 32k to keep nvpair memory allocations small */
407 if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
410 /* Prevent the DXATTR SA from consuming the entire SA
412 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
416 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
420 /* even in case of -EFBIG we must lookup xattr and check can we
421 * rewrite it then delete from SA */
422 rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
425 if (fl & LU_XATTR_CREATE) {
427 } else if (too_big) {
428 rc = -nvlist_remove(obj->oo_sa_xattr, name,
429 DATA_TYPE_BYTE_ARRAY);
432 rc = __osd_sa_xattr_update(env, obj, oh);
433 return rc == 0 ? -EFBIG : rc;
435 } else if (rc == -ENOENT) {
436 if (fl & LU_XATTR_REPLACE)
444 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
445 (uchar_t *)buf->lb_buf, buf->lb_len);
449 rc = __osd_sa_xattr_update(env, obj, oh);
454 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
455 const struct lu_buf *buf, const char *name, int fl,
456 struct osd_thandle *oh)
458 struct osd_device *osd = osd_obj2dev(obj);
459 udmu_objset_t *uos = &osd->od_objset;
460 dmu_buf_t *xa_zap_db = NULL;
461 dmu_buf_t *xa_data_db = NULL;
462 uint64_t xa_data_obj;
463 sa_handle_t *sa_hdl = NULL;
464 dmu_tx_t *tx = oh->ot_tx;
468 LASSERT(obj->oo_sa_hdl);
470 if (obj->oo_xattr == ZFS_NO_OBJECT) {
471 struct lu_attr *la = &osd_oti_get(env)->oti_la;
473 la->la_valid = LA_MODE;
474 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
475 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
476 obj->oo_db->db_object, FTAG, 0);
480 obj->oo_xattr = xa_zap_db->db_object;
481 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
482 &obj->oo_xattr, 8, oh);
487 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
490 if (fl & LU_XATTR_CREATE) {
495 * Entry already exists.
496 * We'll truncate the existing object.
498 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
503 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
504 SA_HDL_PRIVATE, &sa_hdl);
508 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
512 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
513 0, DMU_OBJECT_END, tx);
516 } else if (rc == -ENOENT) {
517 struct lu_attr *la = &osd_oti_get(env)->oti_la;
519 * Entry doesn't exist, we need to create a new one and a new
520 * object to store the value.
522 if (fl & LU_XATTR_REPLACE) {
523 /* should be ENOATTR according to the
524 * man, but that is undefined here */
529 la->la_valid = LA_MODE;
530 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
531 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
532 obj->oo_xattr, FTAG);
535 xa_data_obj = xa_data_db->db_object;
537 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
538 SA_HDL_PRIVATE, &sa_hdl);
542 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
543 1, &xa_data_obj, tx);
547 /* There was an error looking up the xattr name */
551 /* Finally write the xattr value */
552 dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
555 rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
558 sa_handle_destroy(sa_hdl);
560 if (xa_data_db != NULL)
561 dmu_buf_rele(xa_data_db, FTAG);
562 if (xa_zap_db != NULL)
563 dmu_buf_rele(xa_zap_db, FTAG);
568 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
569 const struct lu_buf *buf, const char *name, int fl,
570 struct thandle *handle, struct lustre_capa *capa)
572 struct osd_object *obj = osd_dt_obj(dt);
573 struct osd_thandle *oh;
577 LASSERT(handle != NULL);
578 LASSERT(osd_invariant(obj));
579 LASSERT(dt_object_exists(dt));
582 oh = container_of0(handle, struct osd_thandle, ot_super);
584 down(&obj->oo_guard);
585 CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
586 name, (int)buf->lb_len);
587 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
594 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
595 const char *name, struct osd_thandle *oh)
597 struct osd_device *osd = osd_obj2dev(obj);
598 udmu_objset_t *uos = &osd->od_objset;
599 dmu_tx_t *tx = oh->ot_tx;
600 uint64_t xa_data_obj;
603 /* update SA_ZPL_DXATTR if xattr was in SA */
604 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
606 if (obj->oo_xattr == ZFS_NO_OBJECT)
609 rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
613 * We'll delete the existing object and ZAP entry.
615 dmu_tx_hold_bonus(tx, xa_data_obj);
616 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
617 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
619 } else if (rc == -ENOENT) {
621 * Entry doesn't exist, nothing to be changed.
626 /* An error happened */
630 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
631 const char *name, struct thandle *handle)
633 struct osd_object *obj = osd_dt_obj(dt);
634 struct osd_thandle *oh;
637 LASSERT(handle != NULL);
638 LASSERT(dt_object_exists(dt));
639 LASSERT(osd_invariant(obj));
641 oh = container_of0(handle, struct osd_thandle, ot_super);
642 LASSERT(oh->ot_tx != NULL);
643 LASSERT(obj->oo_db != NULL);
645 down(&obj->oo_guard);
646 __osd_xattr_declare_del(env, obj, name, oh);
652 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
653 const char *name, struct osd_thandle *oh)
657 if (obj->oo_sa_xattr == NULL) {
658 rc = __osd_xattr_cache(env, obj);
663 rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
665 rc = __osd_sa_xattr_update(env, obj, oh);
669 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
670 const char *name, struct osd_thandle *oh)
672 struct osd_device *osd = osd_obj2dev(obj);
673 udmu_objset_t *uos = &osd->od_objset;
674 uint64_t xa_data_obj;
677 /* try remove xattr from SA at first */
678 rc = __osd_sa_xattr_del(env, obj, name, oh);
682 if (obj->oo_xattr == ZFS_NO_OBJECT)
685 rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
689 } else if (rc == 0) {
692 * We'll delete the existing object and ZAP entry.
694 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
698 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
704 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
705 const char *name, struct thandle *handle,
706 struct lustre_capa *capa)
708 struct osd_object *obj = osd_dt_obj(dt);
709 struct osd_thandle *oh;
713 LASSERT(handle != NULL);
714 LASSERT(obj->oo_db != NULL);
715 LASSERT(osd_invariant(obj));
716 LASSERT(dt_object_exists(dt));
717 oh = container_of0(handle, struct osd_thandle, ot_super);
718 LASSERT(oh->ot_tx != NULL);
720 down(&obj->oo_guard);
721 rc = __osd_xattr_del(env, obj, name, oh);
728 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
731 nvpair_t *nvp = NULL;
732 int len, counted = 0, remain = lb->lb_len;
735 if (obj->oo_sa_xattr == NULL) {
736 rc = __osd_xattr_cache(env, obj);
741 LASSERT(obj->oo_sa_xattr);
743 while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
744 len = strlen(nvpair_name(nvp));
745 if (lb->lb_buf != NULL) {
746 if (len + 1 > remain)
749 memcpy(lb->lb_buf, nvpair_name(nvp), len);
751 *((char *)lb->lb_buf) = '\0';
760 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
761 struct lu_buf *lb, struct lustre_capa *capa)
763 struct osd_thread_info *oti = osd_oti_get(env);
764 struct osd_object *obj = osd_dt_obj(dt);
765 struct osd_device *osd = osd_obj2dev(obj);
766 udmu_objset_t *uos = &osd->od_objset;
768 int rc, counted = 0, remain = lb->lb_len;
771 LASSERT(obj->oo_db != NULL);
772 LASSERT(osd_invariant(obj));
773 LASSERT(dt_object_exists(dt));
775 down(&obj->oo_guard);
777 rc = osd_sa_xattr_list(env, obj, lb);
783 /* continue with dnode xattr if any */
784 if (obj->oo_xattr == ZFS_NO_OBJECT)
785 GOTO(out, rc = counted);
787 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
791 while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
793 rc = strlen(oti->oti_key);
794 if (lb->lb_buf != NULL) {
798 memcpy(lb->lb_buf, oti->oti_key, rc);
800 *((char *)lb->lb_buf) = '\0';
806 zap_cursor_advance(zc);
808 if (rc == -ENOENT) /* no more kes in the index */
810 else if (unlikely(rc < 0))
815 udmu_zap_cursor_fini(zc);