Whamcloud - gitweb
LU-11914 build: add a configure check for l_getsepol
[fs/lustre-release.git] / lustre / osd-zfs / osd_xattr.c
index aaa94ab..67be1b4 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2016, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -45,6 +45,7 @@
 #include <obd_class.h>
 #include <lustre_disk.h>
 #include <lustre_fid.h>
+#include <lustre_linkea.h>
 
 #include "osd_internal.h"
 
@@ -62,7 +63,7 @@
 #include <sys/txg.h>
 
 #include <linux/posix_acl_xattr.h>
-
+#include <lustre_scrub.h>
 
 int __osd_xattr_load(struct osd_device *osd, sa_handle_t *hdl, nvlist_t **sa)
 {
@@ -205,8 +206,8 @@ out_rele:
  * \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 osd_xattr_get_internal(const struct lu_env *env, struct osd_object *obj,
+                          struct lu_buf *buf, const char *name, int *sizep)
 {
        int rc;
 
@@ -222,6 +223,59 @@ int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
                                     buf, name, sizep);
 }
 
+static int osd_get_pfid_from_lma(const struct lu_env *env,
+                                struct osd_object *obj,
+                                struct lu_buf *buf, int *sizep)
+{
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lustre_ost_attrs *loa =
+               (struct lustre_ost_attrs *)&info->oti_buf;
+       struct lustre_mdt_attrs *lma = &loa->loa_lma;
+       struct filter_fid *ff;
+       struct ost_layout *ol;
+       struct lu_buf tbuf = {
+               .lb_buf = loa,
+               .lb_len = sizeof(info->oti_buf),
+       };
+       int rc;
+       ENTRY;
+
+       CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
+       rc = osd_xattr_get_internal(env, obj, &tbuf,
+                                   XATTR_NAME_LMA, sizep);
+       if (rc)
+               RETURN(rc);
+
+       lustre_loa_swab(loa, true);
+       LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
+
+       *sizep = sizeof(*ff);
+       if (buf->lb_len == 0 || !buf->lb_buf)
+               RETURN(0);
+
+       if (buf->lb_len < *sizep)
+               RETURN(-ERANGE);
+
+       ff = buf->lb_buf;
+       ol = &ff->ff_layout;
+       ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
+                                         PFID_STRIPE_IDX_BITS);
+       ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
+       loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
+       fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
+       if (lma->lma_compat & LMAC_COMP_INFO) {
+               ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
+               ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
+               ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
+       } else {
+               ol->ol_comp_start = 0;
+               ol->ol_comp_end = 0;
+               ol->ol_comp_id = 0;
+       }
+
+       RETURN(0);
+}
+
 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
                  struct lu_buf *buf, const char *name)
 {
@@ -238,7 +292,17 @@ int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
                RETURN(-EOPNOTSUPP);
 
        down_read(&obj->oo_guard);
-       rc = __osd_xattr_get(env, obj, buf, name, &size);
+       if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed)) {
+               up_read(&obj->oo_guard);
+               RETURN(-ENOENT);
+       }
+
+       /* For the OST migrated from ldiskfs, the PFID EA may
+        * be stored in LMA because of ldiskfs inode size. */
+       if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
+               rc = osd_get_pfid_from_lma(env, obj, buf, &size);
+       else
+               rc = osd_xattr_get_internal(env, obj, buf, name, &size);
        up_read(&obj->oo_guard);
 
        if (rc == -ENOENT)
@@ -294,12 +358,25 @@ void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
                             int vallen, const char *name,
                             struct osd_thandle *oh)
 {
+       struct osd_device *osd = osd_obj2dev(obj);
        dmu_tx_t *tx = oh->ot_tx;
        int bonuslen;
 
        if (unlikely(obj->oo_destroyed))
                return;
 
+       if (strcmp(name, XATTR_NAME_LINK) == 0 &&
+           osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
+               /* If some name entry resides on remote MDT, then will create
+                * agent entry under remote parent. On the other hand, if the
+                * remote entry will be removed, then related agent entry may
+                * need to be removed from the remote parent. So there may be
+                * kinds of cases, let's declare enough credits. The credits
+                * for create agent entry is enough for remove case. */
+               osd_tx_hold_zap(tx, osd->od_remote_parent_dir,
+                               NULL, TRUE, NULL);
+       }
+
        if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
                __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
                return;
@@ -349,12 +426,12 @@ int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
                       struct osd_thandle *oh)
 {
-       sa_bulk_attr_t  *bulk = osd_oti_get(env)->oti_attr_bulk;
-       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
+       sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
+       struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
        struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
        struct osd_device *osd = osd_obj2dev(obj);
        uint64_t crtime[2], gen;
-       timestruc_t now;
+       inode_timespec_t now;
        size_t size;
        int rc, cnt;
 
@@ -648,7 +725,9 @@ __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, obj, &xa_data_dn, tx, la);
+               rc = __osd_object_create(env, osd, obj,
+                                        lu_object_fid(&obj->oo_dt.do_lu),
+                                        &xa_data_dn, tx, la);
                if (rc)
                        goto out;
                xa_data_obj = xa_data_dn->dn_object;
@@ -684,11 +763,108 @@ out:
        return rc;
 }
 
+static int osd_xattr_split_pfid(const struct lu_env *env,
+                               struct osd_object *obj, struct osd_thandle *oh)
+{
+       struct osd_thread_info *info = osd_oti_get(env);
+       struct lustre_ost_attrs *loa =
+               (struct lustre_ost_attrs *)&info->oti_buf;
+       struct lustre_mdt_attrs *lma = &loa->loa_lma;
+       struct lu_buf buf = {
+               .lb_buf = loa,
+               .lb_len = sizeof(info->oti_buf),
+       };
+       int size;
+       int rc;
+       ENTRY;
+
+       CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
+       rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
+       if (rc)
+               RETURN(rc);
+
+       lustre_loa_swab(loa, true);
+       LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
+
+       lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
+       lustre_lma_swab(lma);
+       buf.lb_buf = lma;
+       buf.lb_len = sizeof(*lma);
+       rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
+                                   LU_XATTR_REPLACE, oh);
+       if (!rc)
+               obj->oo_pfid_in_lma = 0;
+
+       RETURN(rc);
+}
+
+/*
+ * In DNE environment, the object (in spite of regular file or directory)
+ * and its name entry may reside on different MDTs. Under such case, we will
+ * create an agent entry on the MDT where the object resides. The agent entry
+ * references the object locally, that makes the object to be visible to the
+ * userspace when mounted as 'zfs' directly. Then the userspace tools, such
+ * as 'tar' can handle the object properly.
+ *
+ * We handle the agent entry during set linkEA that is the common interface
+ * for both regular file and directroy, can handle kinds of cases, such as
+ * create/link/unlink/rename, and so on.
+ *
+ * NOTE: we need to do that for both directory and regular file, so we can NOT
+ *      do that when ea_{insert,delete} that are directory based operations.
+ */
+static int osd_xattr_handle_linkea(const struct lu_env *env,
+                                  struct osd_device *osd,
+                                  struct osd_object *obj,
+                                  const struct lu_buf *buf,
+                                  struct osd_thandle *oh)
+{
+       const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
+       struct lu_fid *tfid = &osd_oti_get(env)->oti_fid;
+       struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
+       struct lu_name tmpname;
+       int rc;
+       bool remote = false;
+       ENTRY;
+
+       rc = linkea_init_with_rec(&ldata);
+       if (!rc) {
+               linkea_first_entry(&ldata);
+               while (ldata.ld_lee != NULL && !remote) {
+                       linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
+                                           &tmpname, tfid);
+                       if (osd_remote_fid(env, osd, tfid) > 0)
+                               remote = true;
+                       else
+                               linkea_next_entry(&ldata);
+               }
+       } else if (rc == -ENODATA) {
+               rc = 0;
+       } else {
+               RETURN(rc);
+       }
+
+       if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
+               rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
+               if (rc)
+                       CERROR("%s: failed to remove agent entry for "DFID
+                              ": rc = %d\n", osd_name(osd), PFID(fid), rc);
+       } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
+               rc = osd_add_to_remote_parent(env, osd, obj, oh);
+               if (rc)
+                       CWARN("%s: failed to create agent entry for "DFID
+                             ": rc = %d\n", osd_name(osd), PFID(fid), rc);
+       }
+
+       RETURN(rc);
+}
+
 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 osd_object  *obj = osd_dt_obj(dt);
+       struct osd_object *obj = osd_dt_obj(dt);
+       struct osd_device *osd = osd_obj2dev(obj);
        struct osd_thandle *oh;
        int rc = 0;
        ENTRY;
@@ -706,7 +882,20 @@ int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
        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);
+       /* For the OST migrated from ldiskfs, the PFID EA may
+        * be stored in LMA because of ldiskfs inode size. */
+       if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
+                    obj->oo_pfid_in_lma)) {
+               rc = osd_xattr_split_pfid(env, obj, oh);
+               if (!rc)
+                       fl = LU_XATTR_CREATE;
+       } else if (strcmp(name, XATTR_NAME_LINK) == 0 &&
+                  osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
+               rc = osd_xattr_handle_linkea(env, osd, obj, buf, oh);
+       }
+
+       if (!rc)
+               rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
        up_write(&obj->oo_guard);
 
        RETURN(rc);
@@ -780,7 +969,17 @@ static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
                return rc;
 
        rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
-       if (rc == 0)
+       if (rc)
+               return rc;
+
+       /*
+        * only migrate delete LMV, and it needs to be done immediately, because
+        * it's used in deleting sub stripes, and if this is delayed, later when
+        * destroying the master object, it will delete sub stripes again.
+        */
+       if (!strcmp(name, XATTR_NAME_LMV))
+               rc = __osd_sa_xattr_update(env, obj, oh);
+       else
                rc = __osd_sa_xattr_schedule_update(env, obj, oh);
        return rc;
 }
@@ -843,7 +1042,12 @@ int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
                RETURN(-EOPNOTSUPP);
 
        down_write(&obj->oo_guard);
-       rc = __osd_xattr_del(env, obj, name, oh);
+       /* For the OST migrated from ldiskfs, the PFID EA may
+        * be stored in LMA because of ldiskfs inode size. */
+       if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
+               rc = osd_xattr_split_pfid(env, obj, oh);
+       else
+               rc = __osd_xattr_del(env, obj, name, oh);
        up_write(&obj->oo_guard);
 
        RETURN(rc);