Whamcloud - gitweb
LU-1302 llog: pass lu_env as parametr in llog functions
[fs/lustre-release.git] / lustre / mdd / mdd_lov.c
index c6bdb36..681543c 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -28,6 +26,8 @@
 /*
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -41,9 +41,6 @@
  * Author: wangdi <wangdi@clusterfs.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
 #include <linux/module.h>
@@ -149,6 +146,9 @@ int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
         obd->obd_recovering = 1;
         cfs_spin_unlock(&obd->obd_dev_lock);
         obd->u.mds.mds_id = mds_id;
+        obd->u.obt.obt_osd_properties.osd_max_ea_size =
+                                               mdd->mdd_dt_conf.ddp_max_ea_size;
+
         rc = class_setup(obd, lcfg);
         if (rc)
                 GOTO(class_detach, rc);
@@ -160,6 +160,7 @@ int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
         obd->obd_upcall.onu_upcall = mdd_notify;
         obd->obd_upcall.onu_owner = mdd;
         mdd->mdd_obd_dev = obd;
+
         EXIT;
 class_detach:
         if (rc)
@@ -216,7 +217,8 @@ int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
                 *md_size = 0;
                 rc = 0;
         } else if (rc < 0) {
-                CERROR("Error %d reading eadata - %d\n", rc, *md_size);
+                CDEBUG(D_OTHER, "Error %d reading eadata - %d\n",
+                       rc, *md_size);
         } else {
                 /* XXX: Convert lov EA but fixed after verification test. */
                 *md_size = rc;
@@ -274,8 +276,9 @@ static int mdd_lov_set_dir_md(const struct lu_env *env,
         LASSERT(S_ISDIR(mdd_object_type(obj)));
         lum = (struct lov_user_md*)buf->lb_buf;
 
-        /* if { size, offset, count } = { 0, -1, 0 } and no pool (i.e. all default
-         * values specified) then delete default striping from dir. */
+        /* if { size, offset, count } = { 0, -1, 0 } and no pool
+         * (i.e. all default values specified) then delete default
+         * striping from dir. */
         if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
                                 lum->lmm_stripe_offset) &&
             lum->lmm_magic != LOV_USER_MAGIC_V3) {
@@ -341,7 +344,8 @@ int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
                         if (rc > 0) {
                                 buf = mdd_buf_get(env, lmm, size);
                                 rc = mdd_xattr_set_txn(env, child, buf,
-                                               XATTR_NAME_LOV, 0, handle);
+                                                       XATTR_NAME_LOV, 0,
+                                                       handle);
                                 if (rc)
                                         CERROR("error on copy stripe info: rc "
                                                 "= %d\n", rc);
@@ -362,6 +366,24 @@ int mdd_lov_objid_prepare(struct mdd_device *mdd, struct lov_mds_md *lmm)
         return mds_lov_prepare_objids(mdd->mdd_obd_dev, lmm);
 }
 
+int mdd_declare_lov_objid_update(const struct lu_env *env,
+                                 struct mdd_device *mdd,
+                                 struct thandle *handle)
+{
+        struct obd_device *obd = mdd2obd_dev(mdd);
+        int size;
+
+        /* in prepare we create local files */
+        if (unlikely(mdd->mdd_capa == NULL))
+                return 0;
+
+        /* XXX: this is a temporary solution to declare llog changes
+         *      will be fixed in 2.3 with new llog implementation */
+
+        size = obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id);
+        return dt_declare_record_write(env, mdd->mdd_capa, size, 0, handle);
+}
+
 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm)
 {
         /* copy mds_lov code is using wrong layer */
@@ -373,13 +395,13 @@ void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
                            const struct md_op_spec *spec)
 {
         if (lmm && !spec->no_create)
-                OBD_FREE(lmm, lmm_size);
+                OBD_FREE_LARGE(lmm, lmm_size);
 }
 
 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
                    struct mdd_object *parent, struct mdd_object *child,
                    struct lov_mds_md **lmm, int *lmm_size,
-                   const struct md_op_spec *spec, struct lu_attr *la)
+                   const struct md_op_spec *spec, struct md_attr *ma)
 {
         struct obd_device     *obd = mdd2obd_dev(mdd);
         struct obd_export     *lov_exp = obd->u.mds.mds_lov_exp;
@@ -387,8 +409,9 @@ int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
         struct obdo           *oa;
         struct lov_stripe_md  *lsm = NULL;
         const void            *eadata = spec->u.sp_ea.eadata;
-        __u32                  create_flags = spec->sp_cr_flags;
+        __u64                  create_flags = spec->sp_cr_flags;
         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
+        struct lu_attr        *la = &ma->ma_attr;
         int                    rc = 0;
         ENTRY;
 
@@ -433,31 +456,32 @@ int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
                                            0, &lsm, (void*)eadata);
                         if (rc)
                                 GOTO(out_oti, rc);
-                } else if (parent != NULL) {
+                } else {
                         /* get lov ea from parent and set to lov */
                         struct lov_mds_md *_lmm;
-                        int _lmm_size;
+                        int _lmm_size = mdd_lov_mdsize(env, mdd);
 
-                        _lmm_size = mdd_lov_mdsize(env, mdd);
-                        _lmm = mdd_max_lmm_get(env, mdd);
+                        LASSERT(parent != NULL);
 
+                        _lmm = mdd_max_lmm_get(env, mdd);
                         if (_lmm == NULL)
                                 GOTO(out_oti, rc = -ENOMEM);
 
                         rc = mdd_get_md_locked(env, parent, _lmm,
                                                &_lmm_size,
                                                XATTR_NAME_LOV);
-                        if (rc > 0)
+                        if (rc > 0) {
+                                _lmm_size = mdd_lov_mdsize(env, mdd);
                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
-                                                   lov_exp, *lmm_size,
+                                                   lov_exp, _lmm_size,
                                                    &lsm, _lmm);
-
+                        }
                         if (rc)
                                 GOTO(out_oti, rc);
                 }
 
                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_OPEN_WAIT_CREATE, 10);
-                rc = obd_create(lov_exp, oa, &lsm, oti);
+                rc = obd_create(env, lov_exp, oa, &lsm, oti);
                 if (rc) {
                         if (rc > 0) {
                                 CERROR("Create error for "DFID": %d\n",
@@ -466,6 +490,19 @@ int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
                         }
                         GOTO(out_oti, rc);
                 }
+
+                if (ma->ma_valid & MA_LAY_GEN)
+                        /* If we already have a lsm, the file is not new and we
+                         * are about to change the layout, so we have to bump
+                         * the generation. It is worth noting that old versions
+                         * will be confused by a non-zero gen, that's why
+                         * OBD_INCOMPAT_LMM_VER has been introduced */
+                        lsm->lsm_layout_gen = ma->ma_layout_gen + 1;
+                else
+                        /* Start with a null generation for backward
+                         * compatiblity with old versions */
+                        lsm->lsm_layout_gen = 0;
+
                 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
         } else {
                 LASSERT(eadata != NULL);
@@ -474,6 +511,10 @@ int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
                 if (rc)
                         GOTO(out_oti, rc);
 
+                if (ma->ma_valid & MA_LAY_GEN)
+                        lsm->lsm_layout_gen = ma->ma_layout_gen;
+                else
+                        lsm->lsm_layout_gen = 0;
         }
 
         lsm->lsm_object_id = fid_ver_oid(mdd_object_fid(child));
@@ -490,16 +531,14 @@ int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
 
                 /* When setting attr to ost, FLBKSZ is not needed. */
                 oa->o_valid &= ~OBD_MD_FLBLKSZ;
-                obdo_from_la(oa, la, OBD_MD_FLTYPE | OBD_MD_FLATIME |
-                             OBD_MD_FLMTIME | OBD_MD_FLCTIME | OBD_MD_FLSIZE);
-
+                obdo_from_la(oa, la, LA_TYPE | LA_ATIME | LA_MTIME |
+                                     LA_CTIME | LA_SIZE);
                 /*
                  * XXX: Pack lustre id to OST, in OST, it will be packed by
                  * filter_fid, but can not see what is the usages. So just pack
                  * o_seq o_ver here, maybe fix it after this cycle.
                  */
-                obdo_from_inode(oa, NULL,
-                                (struct lu_fid *)mdd_object_fid(child), 0);
+                obdo_set_parent_fid(oa, mdd_object_fid(child));
                 oinfo->oi_oa = oa;
                 oinfo->oi_md = lsm;
                 oinfo->oi_capa = NULL;
@@ -549,12 +588,9 @@ out_ids:
  * used when destroying orphans and from mds_reint_unlink() when MDS wants to
  * destroy objects on OSS.
  */
-static
 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
-                      struct mdd_object *obj, struct lu_attr *la,
-                      struct lov_mds_md *lmm, int lmm_size,
-                      struct llog_cookie *logcookies,
-                      int log_unlink)
+                     struct mdd_object *obj, struct lu_attr *la,
+                     struct md_attr *ma, int log_unlink)
 {
         struct obd_device     *obd = mdd2obd_dev(mdd);
         struct obd_export     *lov_exp = obd->u.mds.mds_lov_exp;
@@ -562,7 +598,10 @@ int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
         struct obdo           *oa = &mdd_env_info(env)->mti_oa;
         struct lu_site        *site = mdd2lu_dev(mdd)->ld_site;
-        int rc;
+       struct lov_mds_md     *lmm = ma->ma_lmm;
+       int                    lmm_size = ma->ma_lmm_size;
+       struct llog_cookie    *logcookies = ma->ma_cookie;
+       int                    rc;
         ENTRY;
 
         if (lmm_size == 0)
@@ -588,17 +627,20 @@ int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
                 oti->oti_logcookies = logcookies;
         }
 
+       if (!(ma->ma_attr_flags & MDS_UNLINK_DESTROY))
+               oa->o_flags = OBD_FL_DELORPHAN;
+
         CDEBUG(D_INFO, "destroying OSS object "LPU64":"LPU64"\n", oa->o_seq,
                oa->o_id);
 
-        rc = obd_destroy(lov_exp, oa, lsm, oti, NULL, NULL);
+        rc = obd_destroy(env, lov_exp, oa, lsm, oti, NULL, NULL);
 
         obd_free_memmd(lov_exp, &lsm);
         RETURN(rc);
 }
 
 /*
- * called with obj locked. 
+ * called with obj locked.
  */
 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
                     struct mdd_object *obj, struct lu_attr *la)
@@ -647,11 +689,52 @@ int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
                 RETURN(rc);
         }
 
-        if (ma->ma_valid & MA_COOKIE)
-                rc = mdd_lovobj_unlink(env, mdd, obj, la,
-                                       ma->ma_lmm, ma->ma_lmm_size,
-                                       ma->ma_cookie, 1);
-        RETURN(rc);
+       if (ma->ma_valid & MA_COOKIE)
+               rc = mdd_lovobj_unlink(env, mdd, obj, la, ma, 1);
+
+       RETURN(rc);
+}
+
+int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
+                           struct md_attr *ma, struct thandle *handle)
+{
+        struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
+        int rc, i;
+        __u16 stripe;
+
+        LASSERT(obj);
+        LASSERT(ma);
+
+        if (!S_ISREG(lu_object_attr(&obj->mod_obj.mo_lu)))
+                return 0;
+
+        rc = mdd_lmm_get_locked(env, obj, ma);
+        if (rc || !(ma->ma_valid & MA_LOV))
+                return rc;
+
+        LASSERT(ma->ma_lmm);
+        if (le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V1 &&
+                        le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V3) {
+                CERROR("%s: invalid LOV_MAGIC %08x on object "DFID"\n",
+                                mdd->mdd_obd_dev->obd_name,
+                                le32_to_cpu(ma->ma_lmm->lmm_magic),
+                                PFID(lu_object_fid(&obj->mod_obj.mo_lu)));
+                return -EINVAL;
+        }
+
+        stripe = le16_to_cpu(ma->ma_lmm->lmm_stripe_count);
+        if (stripe == LOV_ALL_STRIPES);
+                stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
+
+        for (i = 0; i < stripe; i++) {
+                rc = mdd_declare_llog_record(env, mdd,
+                                             sizeof(struct llog_unlink_rec),
+                                             handle);
+                if (rc)
+                        return rc;
+        }
+
+        return rc;
 }
 
 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
@@ -699,8 +782,8 @@ int mdd_log_op_setattr(struct obd_device *obd, __u32 uid, __u32 gid,
 
         /* write setattr log */
         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
-        rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
-                      cookies_size / sizeof(struct llog_cookie));
+       rc = llog_add(NULL, ctxt, &lsr->lsr_hdr, lsm, logcookies,
+                     cookies_size / sizeof(struct llog_cookie));
 
         llog_ctxt_put(ctxt);
 
@@ -770,7 +853,7 @@ static int mdd_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
                 oti.oti_logcookies = logcookies;
         }
 
-        obdo_from_inode(oinfo.oi_oa, NULL, (struct lu_fid *)parent, 0);
+        obdo_set_parent_fid(oinfo.oi_oa, parent);
         oinfo.oi_capa = oc;
 
         /* do async setattr from mds to ost not waiting for responses. */
@@ -970,3 +1053,37 @@ int mdd_file_unlock(const struct lu_env *env, struct md_object *obj,
 
         RETURN(rc);
 }
+
+/* file lov is in ma->ma_lmm */
+/* requested lov is in info->mti_spec.u.sp_ea.eadata */
+int mdd_lum_lmm_cmp(const struct lu_env *env, struct md_object *cobj,
+                    const struct md_op_spec *spec, struct md_attr *ma)
+{
+        struct obd_export *lov_exp =
+                mdd2obd_dev(mdo2mdd(cobj))->u.mds.mds_lov_exp;
+        struct lov_mds_md *lmm = ma->ma_lmm;
+        struct lov_user_md_v3 *lum =
+                (struct lov_user_md_v3 *)(spec->u.sp_ea.eadata);
+        struct lov_stripe_md *lsm = NULL;
+        int lmm_magic, rc;
+        ENTRY;
+
+        rc = obd_unpackmd(lov_exp, &lsm, lmm,
+                          lov_mds_md_size(lmm->lmm_stripe_count,
+                                          lmm->lmm_magic));
+        ma->ma_layout_gen = lsm->lsm_layout_gen;
+        ma->ma_valid |= MA_LAY_GEN;
+
+        rc = lov_lum_swab_if_needed(lum, &lmm_magic, NULL);
+        if (rc)
+                GOTO(out, rc);
+
+        rc = lov_lum_lsm_cmp((struct lov_user_md *)lum, lsm);
+        if (rc)
+                GOTO(out, rc);  /* keep GOTO to for traces */
+
+out:
+        /* free lsm */
+        obd_unpackmd(lov_exp, &lsm, NULL, 0);
+        return rc;
+}