Whamcloud - gitweb
LU-1866 osd: ancillary work for initial OI scrub
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
index 8278c6b..b1edd8f 100644 (file)
 #include <obd_support.h>
 /* struct ptlrpc_thread */
 #include <lustre_net.h>
-
-/* fid_is_local() */
 #include <lustre_fid.h>
 
 #include "osd_internal.h"
-#include "osd_igif.h"
 
 /* llo_* api support */
 #include <md_object.h>
@@ -173,14 +170,20 @@ static struct lu_object *osd_object_alloc(const struct lu_env *env,
         }
 }
 
-static int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
-                      struct dentry *dentry, struct lustre_mdt_attrs *lma)
+static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
+                                 const char *name, void *buf, int len)
+{
+       dentry->d_inode = inode;
+       return inode->i_op->getxattr(dentry, name, buf, len);
+}
+
+int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
+               struct dentry *dentry, struct lustre_mdt_attrs *lma)
 {
        int rc;
 
-       dentry->d_inode = inode;
-       rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)lma,
-                                  sizeof(*lma));
+       rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
+                            sizeof(*lma));
        if (rc == -ERANGE) {
                /* try with old lma size */
                rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
@@ -255,8 +258,9 @@ struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
        return inode;
 }
 
-struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
-                          struct osd_inode_id *id, struct lu_fid *fid)
+static struct inode *
+osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
+            struct osd_inode_id *id, struct lu_fid *fid)
 {
        struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
        struct inode            *inode;
@@ -270,7 +274,10 @@ struct inode *osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
        if (rc == 0) {
                *fid = lma->lma_self_fid;
        } else if (rc == -ENODATA) {
-               LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
+               if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
+                       lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
+               else
+                       lu_igif_build(fid, inode->i_ino, inode->i_generation);
        } else {
                iput(inode);
                inode = ERR_PTR(rc);
@@ -367,7 +374,7 @@ static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
                GOTO(out, result = 0);
 
        /* Search order: 3. OI files. */
-       result = osd_oi_lookup(info, dev, fid, id);
+       result = osd_oi_lookup(info, dev, fid, id, true);
        if (result == -ENOENT) {
                if (!fid_is_norm(fid) ||
                    !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
@@ -457,17 +464,17 @@ static int osd_object_init(const struct lu_env *env, struct lu_object *l,
 
        LINVRNT(osd_invariant(obj));
 
+       if (fid_is_otable_it(&l->lo_header->loh_fid)) {
+               obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
+               l->lo_header->loh_attr |= LOHA_EXISTS;
+               return 0;
+       }
+
        result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
        obj->oo_dt.do_body_ops = &osd_body_ops_new;
-       if (result == 0) {
-               if (obj->oo_inode != NULL) {
-                       osd_object_init0(obj);
-               } else if (fid_is_otable_it(&l->lo_header->loh_fid)) {
-                       obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
-                       /* LFSCK iterator object is special without inode */
-                       l->lo_header->loh_attr |= LOHA_EXISTS;
-               }
-       }
+       if (result == 0 && obj->oo_inode != NULL)
+               osd_object_init0(obj);
+
        LINVRNT(osd_invariant(obj));
        return result;
 }
@@ -1035,19 +1042,6 @@ static void osd_conf_get(const struct lu_env *env,
 
 }
 
-/**
- * Helper function to get and fill the buffer with input values.
- */
-static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
-{
-        struct lu_buf *buf;
-
-        buf = &osd_oti_get(env)->oti_buf;
-        buf->lb_buf = area;
-        buf->lb_len = len;
-        return buf;
-}
-
 /*
  * Concurrency: shouldn't matter.
  */
@@ -1695,7 +1689,10 @@ static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
                  * NB: don't need any lock because no contention at this
                  * early stage */
                 inode->i_flags |= S_NOCMTIME;
-               inode->i_state |= I_LUSTRE_NOSCRUB;
+
+               /* For new created object, it must be consistent,
+                * and it is unnecessary to scrub against it. */
+               ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
                 obj->oo_inode = inode;
                 result = 0;
         } else {
@@ -2167,33 +2164,15 @@ static int osd_object_destroy(const struct lu_env *env,
         RETURN(0);
 }
 
-/**
- * Helper function for osd_xattr_set()
- */
-static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
-                           const struct lu_buf *buf, const char *name, int fl)
-{
-        struct osd_object      *obj      = osd_dt_obj(dt);
-        struct inode           *inode    = obj->oo_inode;
-        struct osd_thread_info *info     = osd_oti_get(env);
-        struct dentry          *dentry   = &info->oti_child_dentry;
-        int                     fs_flags = 0;
-        int                     rc;
-
-        LASSERT(dt_object_exists(dt));
-        LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
-
-        if (fl & LU_XATTR_REPLACE)
-                fs_flags |= XATTR_REPLACE;
-
-        if (fl & LU_XATTR_CREATE)
-                fs_flags |= XATTR_CREATE;
+static inline int __osd_xattr_set(struct osd_thread_info *info,
+                                 struct inode *inode, const char *name,
+                                 const void *buf, int buflen, int fl)
+{
+       struct dentry *dentry = &info->oti_child_dentry;
 
        ll_vfs_dq_init(inode);
-        dentry->d_inode = inode;
-        rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
-                                   buf->lb_len, fs_flags);
-        return rc;
+       dentry->d_inode = inode;
+       return inode->i_op->setxattr(dentry, name, buf, buflen, fl);
 }
 
 /**
@@ -2209,15 +2188,17 @@ static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
                           const struct lu_fid *fid)
 {
-        struct osd_thread_info  *info      = osd_oti_get(env);
-        struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
+       struct osd_thread_info  *info   = osd_oti_get(env);
+       struct inode            *inode  = osd_dt_obj(dt)->oo_inode;
+       struct lustre_mdt_attrs *lma    = &info->oti_mdt_attrs;
+       int                      rc;
 
-        lustre_lma_init(mdt_attrs, fid);
-        lustre_lma_swab(mdt_attrs);
-        return __osd_xattr_set(env, dt,
-                               osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
-                               XATTR_NAME_LMA, LU_XATTR_CREATE);
+       lustre_lma_init(lma, fid);
+       lustre_lma_swab(lma);
 
+       rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
+                            XATTR_CREATE);
+       return rc;
 }
 
 /**
@@ -2229,13 +2210,19 @@ static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
  * its inmemory API.
  */
 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
-                                  const struct dt_rec *fid)
+                                 const struct dt_rec *fid)
 {
-        param->edp_magic = LDISKFS_LUFID_MAGIC;
-        param->edp_len =  sizeof(struct lu_fid) + 1;
+       /* XXX: replace the check with "!fid_is_client_mdt_visible()"
+        *      when FID in OI file introduced for local object. */
+       if (!fid_is_norm((const struct lu_fid *)fid) &&
+           !fid_is_igif((const struct lu_fid *)fid)) {
+               param->edp_magic = 0;
+               return;
+       }
 
-        fid_cpu_to_be((struct lu_fid *)param->edp_data,
-                      (struct lu_fid *)fid);
+       param->edp_magic = LDISKFS_LUFID_MAGIC;
+       param->edp_len =  sizeof(struct lu_fid) + 1;
+       fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
 }
 
 /**
@@ -2465,8 +2452,7 @@ static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
                 return -EACCES;
 
-        dentry->d_inode = inode;
-        return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
+       return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
 }
 
 
@@ -2515,6 +2501,11 @@ static 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 osd_object      *obj      = osd_dt_obj(dt);
+       struct inode           *inode    = obj->oo_inode;
+       struct osd_thread_info *info     = osd_oti_get(env);
+       int                     fs_flags = 0;
+
         LASSERT(handle != NULL);
 
         /* version set is not real XATTR */
@@ -2530,7 +2521,14 @@ static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
                 return -EACCES;
 
        osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
-       return __osd_xattr_set(env, dt, buf, name, fl);
+       if (fl & LU_XATTR_REPLACE)
+               fs_flags |= XATTR_REPLACE;
+
+       if (fl & LU_XATTR_CREATE)
+               fs_flags |= XATTR_CREATE;
+
+       return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
+                              fs_flags);
 }
 
 /*
@@ -2588,7 +2586,6 @@ static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
 
         LASSERT(dt_object_exists(dt));
         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
-        LASSERT(osd_write_locked(env, obj));
         LASSERT(handle != NULL);
 
         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
@@ -2801,7 +2798,7 @@ static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
        } else if (unlikely(feat == &dt_otable_features)) {
                dt->do_index_ops = &osd_otable_ops;
                return 0;
-       } else if (feat == &dt_acct_features) {
+       } else if (unlikely(feat == &dt_acct_features)) {
                dt->do_index_ops = &osd_acct_index_ops;
                result = 0;
                skip_iam = 1;
@@ -3285,27 +3282,20 @@ static int __osd_ea_add_rec(struct osd_thread_info *info,
         oth = container_of(th, struct osd_thandle, ot_super);
         LASSERT(oth->ot_handle != NULL);
         LASSERT(oth->ot_handle->h_transaction != NULL);
-
-        child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
-
-        /* XXX: remove fid_is_igif() check here.
-         * IGIF check is just to handle insertion of .. when it is 'ROOT',
-         * it is IGIF now but needs FID in dir entry as well for readdir
-         * to work.
-         * LU-838 should fix that and remove fid_is_igif() check */
-        if (fid_is_igif((struct lu_fid *)fid) ||
-            fid_is_norm((struct lu_fid *)fid)) {
-                ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
-                osd_get_ldiskfs_dirent_param(ldp, fid);
-                child->d_fsdata = (void *)ldp;
-        } else {
-                child->d_fsdata = NULL;
-        }
        LASSERT(pobj->oo_inode);
+
+       ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
+       if (unlikely(pobj->oo_inode ==
+                    osd_sb(osd_obj2dev(pobj))->s_root->d_inode))
+               ldp->edp_magic = 0;
+       else
+               osd_get_ldiskfs_dirent_param(ldp, fid);
+       child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
+       child->d_fsdata = (void *)ldp;
        ll_vfs_dq_init(pobj->oo_inode);
-        rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
+       rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 /**
@@ -3346,31 +3336,25 @@ static int osd_add_dot_dotdot(struct osd_thread_info *info,
                         result = 0;
                 }
         } else if(strcmp(name, dotdot) == 0) {
-                dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
-                dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
-
-                if (!dir->oo_compat_dot_created)
-                        return -EINVAL;
-                if (!fid_is_igif((struct lu_fid *)dot_fid)) {
-                        osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
-                        osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
-                } else {
-                        dot_ldp = NULL;
-                        dot_dot_ldp = NULL;
-                }
-                /* in case of rename, dotdot is already created */
-                if (dir->oo_compat_dotdot_created) {
-                        return __osd_ea_add_rec(info, dir, parent_dir, name,
-                                                dot_dot_fid, NULL, th);
-                }
-
-                result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
-                                                inode, dot_ldp, dot_dot_ldp);
-                if (result == 0)
-                       dir->oo_compat_dotdot_created = 1;
-        }
+               if (!dir->oo_compat_dot_created)
+                       return -EINVAL;
+
+               dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
+               osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
+               /* in case of rename, dotdot is already created */
+               if (dir->oo_compat_dotdot_created)
+                       return __osd_ea_add_rec(info, dir, parent_dir, name,
+                                               dot_dot_fid, NULL, th);
+
+               dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
+               dot_ldp->edp_magic = 0;
+               result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
+                                               inode, dot_ldp, dot_dot_ldp);
+               if (result == 0)
+                       dir->oo_compat_dotdot_created = 1;
+       }
 
-        return result;
+       return result;
 }
 
 
@@ -3433,7 +3417,7 @@ osd_consistency_check(struct osd_thread_info *oti, struct osd_device *dev,
                RETURN_EXIT;
 
 again:
-       rc = osd_oi_lookup(oti, dev, fid, id);
+       rc = osd_oi_lookup(oti, dev, fid, id, true);
        if (rc != 0 && rc != -ENOENT)
                RETURN_EXIT;
 
@@ -3486,6 +3470,7 @@ static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
         struct htree_lock          *hlock = NULL;
         int                         ino;
         int                         rc;
+       ENTRY;
 
         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
 
@@ -4083,6 +4068,7 @@ static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
                                unsigned d_type)
 {
         struct osd_it_ea        *it   = (struct osd_it_ea *)buf;
+       struct osd_object       *obj  = it->oie_obj;
         struct osd_it_ea_dirent *ent  = it->oie_dirent;
         struct lu_fid           *fid  = &ent->oied_fid;
         struct osd_fid_pack     *rec;
@@ -4098,16 +4084,23 @@ static int osd_ldiskfs_filldir(char *buf, const char *name, int namelen,
             OSD_IT_EA_BUFSIZE)
                 RETURN(1);
 
-        if (d_type & LDISKFS_DIRENT_LUFID) {
-                rec = (struct osd_fid_pack*) (name + namelen + 1);
-
-                if (osd_fid_unpack(fid, rec) != 0)
-                        fid_zero(fid);
+       /* "." is just the object itself. */
+       if (namelen == 1 && name[0] == '.') {
+               *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
+       } else if (d_type & LDISKFS_DIRENT_LUFID) {
+               rec = (struct osd_fid_pack*) (name + namelen + 1);
+               if (osd_fid_unpack(fid, rec) != 0)
+                       fid_zero(fid);
+       } else {
+               fid_zero(fid);
+       }
+       d_type &= ~LDISKFS_DIRENT_LUFID;
 
-                d_type &= ~LDISKFS_DIRENT_LUFID;
-        } else {
-                fid_zero(fid);
-        }
+       /* NOT export local root. */
+       if (unlikely(osd_sb(osd_obj2dev(obj))->s_root->d_inode->i_ino == ino)) {
+               ino = obj->oo_inode->i_ino;
+               *fid = obj->oo_dt.do_lu.lo_header->loh_fid;
+       }
 
         ent->oied_ino     = ino;
         ent->oied_off     = offset;
@@ -4550,6 +4543,8 @@ static int osd_mount(const struct lu_env *env,
                GOTO(out, rc = -EINVAL);
        }
 
+       ldiskfs_set_inode_state(osd_sb(o)->s_root->d_inode,
+                               LDISKFS_STATE_LUSTRE_NO_OI);
        if (lmd_flags & LMD_FLG_NOSCRUB)
                o->od_noscrub = 1;