Whamcloud - gitweb
LU-7904 osd: honor LOC_F_NEW
[fs/lustre-release.git] / lustre / osd-zfs / osd_xattr.c
index 0d783aa..1be8363 100644 (file)
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
- */
-/*
- * Copyright (c) 2012, 2013, Intel Corporation.
- * Use is subject to license terms.
+ *
+ * Copyright (c) 2012, 2015, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #include <linux/posix_acl_xattr.h>
 
-/*
- * Copy an extended attribute into the buffer provided, or compute the
- * required buffer size.
- *
- * If buf is NULL, it computes the required buffer size.
- *
- * Returns 0 on success or a negative error number on failure.
- * On success, the number of bytes used / required is stored in 'size'.
- *
- * No locking is done here.
- */
+
 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
 {
        sa_handle_t *sa_hdl;
@@ -101,7 +89,7 @@ int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
                goto out_sa;
        }
 
-       buf = sa_spill_alloc(KM_SLEEP);
+       buf = osd_zio_buf_alloc(size);
        if (buf == NULL) {
                rc = -ENOMEM;
                goto out_sa;
@@ -109,7 +97,7 @@ int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
        rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
        if (rc == 0)
                rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
-       sa_spill_free(buf);
+       osd_zio_buf_free(buf, size);
 out_sa:
        sa_handle_destroy(sa_hdl);
 
@@ -126,8 +114,9 @@ static inline int __osd_xattr_cache(const struct lu_env *env,
                                &obj->oo_sa_xattr);
 }
 
-int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
-                      const struct lu_buf *buf, const char *name, int *sizep)
+static int
+__osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
+                  const struct lu_buf *buf, const char *name, int *sizep)
 {
        uchar_t *nv_value;
        int      rc;
@@ -218,25 +207,42 @@ out_rele:
        return rc;
 }
 
+/**
+ * Copy an extended attribute into the buffer provided, or compute
+ * the required buffer size if \a buf is NULL.
+ *
+ * On success, the number of bytes used or required is stored in \a sizep.
+ *
+ * Note that no locking is done here.
+ *
+ * \param[in] env      execution environment
+ * \param[in] obj      object for which to retrieve xattr
+ * \param[out] buf     buffer to store xattr value in
+ * \param[in] name     name of xattr to copy
+ * \param[out] sizep   bytes used or required to store xattr
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on failure
+ */
 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
                    struct lu_buf *buf, const char *name, int *sizep)
 {
        int rc;
 
+       if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
+               return -ENOENT;
+
        /* check SA_ZPL_DXATTR first then fallback to directory xattr */
        rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
        if (rc != -ENOENT)
                return rc;
 
-       rc = __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
-                                  buf, name, sizep);
-
-       return rc;
+       return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
+                                    buf, name, sizep);
 }
 
 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
-                 struct lu_buf *buf, const char *name,
-                 struct lustre_capa *capa)
+                 struct lu_buf *buf, const char *name)
 {
        struct osd_object  *obj  = osd_dt_obj(dt);
        int                 rc, size = 0;
@@ -244,16 +250,15 @@ int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
 
        LASSERT(obj->oo_db != NULL);
        LASSERT(osd_invariant(obj));
-       LASSERT(dt_object_exists(dt));
 
        if (!osd_obj2dev(obj)->od_posix_acl &&
            (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
             strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
                RETURN(-EOPNOTSUPP);
 
-       down(&obj->oo_guard);
+       down_read(&obj->oo_guard);
        rc = __osd_xattr_get(env, obj, buf, name, &size);
-       up(&obj->oo_guard);
+       up_read(&obj->oo_guard);
 
        if (rc == -ENOENT)
                rc = -ENODATA;
@@ -273,6 +278,9 @@ void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
        int                rc = 0;
        int                here;
 
+       if (unlikely(obj->oo_destroyed))
+               return;
+
        here = dt_object_exists(&obj->oo_dt);
 
        /* object may be not yet created */
@@ -338,9 +346,9 @@ int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
        LASSERT(handle != NULL);
        oh = container_of0(handle, struct osd_thandle, ot_super);
 
-       down(&obj->oo_guard);
+       down_read(&obj->oo_guard);
        __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
-       up(&obj->oo_guard);
+       up_read(&obj->oo_guard);
 
        RETURN(0);
 }
@@ -371,7 +379,7 @@ __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
        if (rc)
                return rc;
 
-       dxattr = sa_spill_alloc(KM_SLEEP);
+       dxattr = osd_zio_buf_alloc(sa_size);
        if (dxattr == NULL)
                RETURN(-ENOMEM);
 
@@ -382,7 +390,7 @@ __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
 
        rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
 out_free:
-       sa_spill_free(dxattr);
+       osd_zio_buf_free(dxattr, sa_size);
        RETURN(rc);
 }
 
@@ -524,7 +532,7 @@ __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
                        goto out_sa;
 
                rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
-                                       0, DMU_OBJECT_END, tx);
+                                    0, DMU_OBJECT_END, tx);
                if (rc)
                        goto out_sa;
        } else if (rc == -ENOENT) {
@@ -542,7 +550,7 @@ __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
 
                la->la_valid = LA_MODE;
                la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
-               rc = __osd_object_create(env, osd, &xa_data_db, tx, la,
+               rc = __osd_object_create(env, obj, &xa_data_db, tx, la,
                                         obj->oo_xattr);
                if (rc)
                        goto out;
@@ -581,7 +589,7 @@ out:
 
 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
                  const struct lu_buf *buf, const char *name, int fl,
-                 struct thandle *handle, struct lustre_capa *capa)
+                 struct thandle *handle)
 {
        struct osd_object  *obj = osd_dt_obj(dt);
        struct osd_thandle *oh;
@@ -590,8 +598,6 @@ int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
 
        LASSERT(handle != NULL);
        LASSERT(osd_invariant(obj));
-       LASSERT(dt_object_exists(dt));
-       LASSERT(obj->oo_db);
 
        if (!osd_obj2dev(obj)->od_posix_acl &&
            (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0 ||
@@ -604,11 +610,11 @@ int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
 
        oh = container_of0(handle, struct osd_thandle, ot_super);
 
-       down(&obj->oo_guard);
+       down_write(&obj->oo_guard);
        CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
                name, (int)buf->lb_len);
-       rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
-       up(&obj->oo_guard);
+       rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
+       up_write(&obj->oo_guard);
 
        RETURN(rc);
 }
@@ -657,22 +663,22 @@ int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
        ENTRY;
 
        LASSERT(handle != NULL);
-       LASSERT(dt_object_exists(dt));
        LASSERT(osd_invariant(obj));
 
        oh = container_of0(handle, struct osd_thandle, ot_super);
        LASSERT(oh->ot_tx != NULL);
        LASSERT(obj->oo_db != NULL);
 
-       down(&obj->oo_guard);
-       __osd_xattr_declare_del(env, obj, name, oh);
-       up(&obj->oo_guard);
+       down_read(&obj->oo_guard);
+       if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
+               __osd_xattr_declare_del(env, obj, name, oh);
+       up_read(&obj->oo_guard);
 
        RETURN(0);
 }
 
-int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
-                      const char *name, struct osd_thandle *oh)
+static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
+                             const char *name, struct osd_thandle *oh)
 {
        int rc;
 
@@ -688,13 +694,16 @@ int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
        return rc;
 }
 
-int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
-                   const char *name, struct osd_thandle *oh)
+static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
+                          const char *name, struct osd_thandle *oh)
 {
        struct osd_device *osd = osd_obj2dev(obj);
        uint64_t           xa_data_obj;
        int                rc;
 
+       if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
+               return -ENOENT;
+
        /* try remove xattr from SA at first */
        rc = __osd_sa_xattr_del(env, obj, name, oh);
        if (rc != -ENOENT)
@@ -723,8 +732,7 @@ int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
 }
 
 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
-                 const char *name, struct thandle *handle,
-                 struct lustre_capa *capa)
+                 const char *name, struct thandle *handle)
 {
        struct osd_object  *obj = osd_dt_obj(dt);
        struct osd_thandle *oh;
@@ -743,19 +751,109 @@ int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
             strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
                RETURN(-EOPNOTSUPP);
 
-       down(&obj->oo_guard);
+       down_write(&obj->oo_guard);
        rc = __osd_xattr_del(env, obj, name, oh);
-       up(&obj->oo_guard);
+       up_write(&obj->oo_guard);
 
        RETURN(rc);
 }
 
+void osd_declare_xattrs_destroy(const struct lu_env *env,
+                               struct osd_object *obj, struct osd_thandle *oh)
+{
+       struct osd_device *osd = osd_obj2dev(obj);
+       zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
+       uint64_t           oid = obj->oo_xattr, xid;
+       dmu_tx_t          *tx = oh->ot_tx;
+       zap_cursor_t      *zc;
+       int                rc;
+
+       if (oid == ZFS_NO_OBJECT)
+               return; /* Nothing to do for SA xattrs */
+
+       /* Declare to free the ZAP holding xattrs */
+       dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
+
+       rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
+       if (rc)
+               goto out;
+
+       while (zap_cursor_retrieve(zc, za) == 0) {
+               LASSERT(za->za_num_integers == 1);
+               LASSERT(za->za_integer_length == sizeof(uint64_t));
+
+               rc = -zap_lookup(osd->od_os, oid, za->za_name,
+                                sizeof(uint64_t), 1, &xid);
+               if (rc) {
+                       CERROR("%s: xattr %s lookup failed: rc = %d\n",
+                              osd->od_svname, za->za_name, rc);
+                       break;
+               }
+               dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
+
+               zap_cursor_advance(zc);
+       }
+
+       osd_zap_cursor_fini(zc);
+out:
+       if (rc && tx->tx_err == 0)
+               tx->tx_err = -rc;
+}
+
+int osd_xattrs_destroy(const struct lu_env *env,
+                      struct osd_object *obj, struct osd_thandle *oh)
+{
+       struct osd_device *osd = osd_obj2dev(obj);
+       dmu_tx_t          *tx = oh->ot_tx;
+       zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
+       zap_cursor_t      *zc;
+       uint64_t           xid;
+       int                rc;
+
+       /* The transaction must have been assigned to a transaction group. */
+       LASSERT(tx->tx_txg != 0);
+
+       if (obj->oo_xattr == ZFS_NO_OBJECT)
+               return 0; /* Nothing to do for SA xattrs */
+
+       /* Free the ZAP holding the xattrs */
+       rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
+       if (rc)
+               return rc;
+
+       while (zap_cursor_retrieve(zc, za) == 0) {
+               LASSERT(za->za_num_integers == 1);
+               LASSERT(za->za_integer_length == sizeof(uint64_t));
+
+               rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
+                                sizeof(uint64_t), 1, &xid);
+               if (rc) {
+                       CERROR("%s: lookup xattr %s failed: rc = %d\n",
+                              osd->od_svname, za->za_name, rc);
+               } else {
+                       rc = -dmu_object_free(osd->od_os, xid, tx);
+                       if (rc)
+                               CERROR("%s: free xattr %s failed: rc = %d\n",
+                                      osd->od_svname, za->za_name, rc);
+               }
+               zap_cursor_advance(zc);
+       }
+       osd_zap_cursor_fini(zc);
+
+       rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
+       if (rc)
+               CERROR("%s: free xattr "LPU64" failed: rc = %d\n",
+                      osd->od_svname, obj->oo_xattr, rc);
+
+       return rc;
+}
+
 static int
 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
-                 struct lu_buf *lb)
+                 const struct lu_buf *lb)
 {
        nvpair_t *nvp = NULL;
-       int       len, counted = 0, remain = lb->lb_len;
+       int       len, counted = 0;
        int       rc = 0;
 
        if (obj->oo_sa_xattr == NULL) {
@@ -774,16 +872,12 @@ osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
                     strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0))
                        continue;
 
-               len = strlen(nvpair_name(nvp));
+               len = strlen(name);
                if (lb->lb_buf != NULL) {
-                       if (len + 1 > remain)
+                       if (counted + len + 1 > lb->lb_len)
                                return -ERANGE;
 
-                       memcpy(lb->lb_buf, name, len);
-                       lb->lb_buf += len;
-                       *((char *)lb->lb_buf) = '\0';
-                       lb->lb_buf++;
-                       remain -= len + 1;
+                       memcpy(lb->lb_buf + counted, name, len + 1);
                }
                counted += len + 1;
        }
@@ -791,26 +885,26 @@ osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
 }
 
 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
-                  struct lu_buf *lb, struct lustre_capa *capa)
+                  const struct lu_buf *lb)
 {
        struct osd_object      *obj = osd_dt_obj(dt);
        struct osd_device      *osd = osd_obj2dev(obj);
        zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
        zap_cursor_t           *zc;
-       int                    rc, counted = 0, remain = lb->lb_len;
+       int                    rc, counted;
        ENTRY;
 
        LASSERT(obj->oo_db != NULL);
        LASSERT(osd_invariant(obj));
        LASSERT(dt_object_exists(dt));
 
-       down(&obj->oo_guard);
+       down_read(&obj->oo_guard);
 
        rc = osd_sa_xattr_list(env, obj, lb);
        if (rc < 0)
                GOTO(out, rc);
+
        counted = rc;
-       remain -= counted;
 
        /* continue with dnode xattr if any */
        if (obj->oo_xattr == ZFS_NO_OBJECT)
@@ -830,14 +924,10 @@ int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
 
                rc = strlen(za->za_name);
                if (lb->lb_buf != NULL) {
-                       if (rc + 1 > remain)
-                               RETURN(-ERANGE);
-
-                       memcpy(lb->lb_buf, za->za_name, rc);
-                       lb->lb_buf += rc;
-                       *((char *)lb->lb_buf) = '\0';
-                       lb->lb_buf++;
-                       remain -= rc + 1;
+                       if (counted + rc + 1 > lb->lb_len)
+                               GOTO(out_fini, rc = -ERANGE);
+
+                       memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
                }
                counted += rc + 1;
 
@@ -852,9 +942,7 @@ int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
 out_fini:
        osd_zap_cursor_fini(zc);
 out:
-       up(&obj->oo_guard);
+       up_read(&obj->oo_guard);
        RETURN(rc);
 
 }
-
-