From 1bb10d0c175fc03b3f871e5f50a3fa10be20194c Mon Sep 17 00:00:00 2001
From: wangdi <wangdi>
Date: Fri, 9 Jul 2004 15:40:38 +0000
Subject: [PATCH] 1)some fix in cobd for cache miss tests. 2)some minor fix in
 smfs cache_hook.

---
 lustre/cobd/cache_obd.c     | 94 +++++++++++++++++++++++++++----------
 lustre/include/linux/obd.h  |  2 +
 lustre/smfs/dir.c           | 29 ++++++------
 lustre/smfs/journal.c       |  5 +-
 lustre/smfs/smfs_internal.h | 23 +++++----
 5 files changed, 105 insertions(+), 48 deletions(-)

diff --git a/lustre/cobd/cache_obd.c b/lustre/cobd/cache_obd.c
index 17181f34b1..fdebff1c88 100644
--- a/lustre/cobd/cache_obd.c
+++ b/lustre/cobd/cache_obd.c
@@ -45,69 +45,106 @@ static int cobd_detach(struct obd_device *obd)
         return lprocfs_obd_detach(obd);
 }
 
+static int connect_to_obd(char *name, struct lustre_handle *conn)
+{ 
+        struct obd_uuid   obd_uuid;
+        struct obd_device *obd;
+        int    rc = 0;
+        ENTRY;
+ 
+        obd = class_name2obd(name);
+        if (obd == NULL) {
+                CERROR("%s: unable to find a client for obd: %s\n",
+                       obd->obd_name, name);
+                RETURN(-EINVAL);
+        }
+        rc = obd_connect(conn, obd, &obd_uuid);
+        RETURN(rc);
+}
+
 static int cobd_setup(struct obd_device *obd, obd_count len, void *buf)
 {
         struct lustre_cfg *lcfg = (struct lustre_cfg *)buf;
         struct cache_obd  *cobd = &obd->u.cobd;
+        struct lustre_handle real_conn = {0,}, cache_conn = {0,};
         struct obd_device *real;
         struct obd_device *cache;
-        struct obd_uuid real_uuid;
-        struct obd_uuid cache_uuid;
-        struct lustre_handle real_conn = {0,}, cache_conn = {0,};
         int rc;
+        ENTRY;
 
         if (lcfg->lcfg_inllen1 == 0 || lcfg->lcfg_inlbuf1 == NULL) {
                 CERROR("%s: setup requires real device name\n", 
                        obd->obd_name);
-                return (-EINVAL);
+                RETURN(-EINVAL);
         }
 
         real = class_name2obd(lcfg->lcfg_inlbuf1);
         if (real == NULL) {
                 CERROR("%s: unable to find a client for real: %s\n",
                        obd->obd_name, lcfg->lcfg_inlbuf1);
-                return (-EINVAL);
+                RETURN(-EINVAL);
         }
 
         if (lcfg->lcfg_inllen2 == 0 || lcfg->lcfg_inlbuf2 == NULL) {
                 CERROR("%s: setup requires cache device name\n", obd->obd_name);
-                return (-EINVAL);
+                RETURN(-EINVAL);
         }
 
         cache  = class_name2obd(lcfg->lcfg_inlbuf2);
         if (cache == NULL) {
                 CERROR("%s: unable to find a client for cache: %s\n",
                        obd->obd_name, lcfg->lcfg_inlbuf2);
-                return (-EINVAL);
+                RETURN(-EINVAL);
         }
 
+        OBD_ALLOC(cobd->cobd_real_name, strlen(lcfg->lcfg_inlbuf1) + 1);
+        if (!cobd->cobd_real_name) 
+                GOTO(exit, rc = -ENOMEM);
+        memcpy(cobd->cobd_real_name, lcfg->lcfg_inlbuf1, 
+               strlen(lcfg->lcfg_inlbuf1));
+        
+        OBD_ALLOC(cobd->cobd_cache_name, strlen(lcfg->lcfg_inlbuf2) + 1);
+        if (!cobd->cobd_cache_name) 
+                GOTO(exit, rc = -ENOMEM);
+        memcpy(cobd->cobd_cache_name, lcfg->lcfg_inlbuf2, 
+               strlen(lcfg->lcfg_inlbuf2));
+
         /* don't bother checking attached/setup;
          * obd_connect() should, and it can change underneath us */
-        rc = obd_connect(&real_conn, real, &real_uuid);
+        rc = connect_to_obd(cobd->cobd_real_name, &real_conn);
         if (rc != 0)
-                return (rc);
+                GOTO(exit, rc);
         cobd->cobd_real_exp = class_conn2export(&real_conn);
-
-        rc = obd_connect(&cache_conn, cache, &cache_uuid);
+        
+        rc = connect_to_obd(cobd->cobd_cache_name, &cache_conn);
         if (rc != 0) {
-                obd_disconnect(cobd->cobd_real_exp, 0);
-                return rc;
+                obd_disconnect(cobd->cobd_cache_exp, 0);
+                GOTO(exit, rc);
         }
         cobd->cobd_cache_exp = class_conn2export(&cache_conn);
-        /* set mds_num for lustre */
-
+        
         if (!strcmp(real->obd_type->typ_name, LUSTRE_MDC_NAME)) {
+                /* set mds_num for lustre */
                 int mds_num;
                 mds_num = REAL_MDS_NUMBER;
                 obd_set_info(cobd->cobd_real_exp, strlen("mds_num"),
-                               "mds_num", sizeof(mds_num), &mds_num);
+                             "mds_num", sizeof(mds_num), &mds_num);
                 mds_num = CACHE_MDS_NUMBER;
                 obd_set_info(cobd->cobd_cache_exp, strlen("mds_num"),
-                               "mds_num", sizeof(mds_num), &mds_num);
+                             "mds_num", sizeof(mds_num), &mds_num);
         }
         /*default write to real obd*/
+exit:
+        if (rc) {
+                if (cobd->cobd_cache_name)
+                        OBD_FREE(cobd->cobd_cache_name, 
+                                 strlen(cobd->cobd_cache_name) + 1);
+                if (cobd->cobd_real_name)
+                        OBD_FREE(cobd->cobd_real_name, 
+                                 strlen(cobd->cobd_real_name) + 1);
+        }
         cobd->cache_on = 1;
-        return 0;
+        RETURN(rc);
 }
 
 static int cobd_cleanup(struct obd_device *obd, int flags)
@@ -117,6 +154,10 @@ static int cobd_cleanup(struct obd_device *obd, int flags)
 
         if (!list_empty(&obd->obd_exports))
                 return (-EBUSY);
+        
+        OBD_FREE(cobd->cobd_cache_name, strlen(cobd->cobd_cache_name) + 1);
+        OBD_FREE(cobd->cobd_real_name, strlen(cobd->cobd_real_name) + 1);
+        
         if (cobd->cache_on) { 
                 rc = obd_disconnect(cobd->cobd_cache_exp, flags);
                 if (rc != 0)
@@ -640,15 +681,20 @@ static int cobd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
         struct obd_device *obd = class_exp2obd(exp);
         struct cache_obd  *cobd = &obd->u.cobd;
         struct obd_device *real_dev = class_exp2obd(cobd->cobd_real_exp);
-        struct obd_device *cache_dev = class_exp2obd(cobd->cobd_cache_exp);
+        struct obd_device *cache_dev = class_exp2obd(cobd->cobd_cache_exp); 
         struct obd_export *cobd_exp;
         int rc = 0;
  
         switch (cmd) {
         case OBD_IOC_COBD_CON:
                 if (!cobd->cache_on) {
+                        struct lustre_handle cache_conn = {0,};
+                        
+                        rc = connect_to_obd(cobd->cobd_cache_name, &cache_conn);
+                        if (rc != 0)
+                                RETURN(rc); 
+                        cobd->cobd_cache_exp = class_conn2export(&cache_conn);
                         cobd->cache_on = 1;
-                        /*FIXME should connect the cache obd again*/ 
                 }
                 break;
         case OBD_IOC_COBD_COFF: 
@@ -663,18 +709,18 @@ static int cobd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
 
                         cobd->cache_on = 0;
                         /*FIXME, should read from real_dev*/
-                        real_dev->u.cli.cl_max_mds_easize = 
+                       real_dev->u.cli.cl_max_mds_easize =
                                               cache_dev->u.cli.cl_max_mds_easize;
-                        real_dev->u.cli.cl_max_mds_cookiesize = 
+                       real_dev->u.cli.cl_max_mds_cookiesize =
                                               cache_dev->u.cli.cl_max_mds_cookiesize;
-                        break;
                 }
+                break;
         case OBD_IOC_COBD_CFLUSH:
                 if (cobd->cache_on) {
                         cobd->cache_on = 0;
                         cobd_flush(obd);
-                        break;
                 }
+                break;
         default:
                 cobd_exp = cobd_get_exp(obd);
                 rc = obd_iocontrol(cmd, cobd_exp, len, karg, uarg);
diff --git a/lustre/include/linux/obd.h b/lustre/include/linux/obd.h
index c8727d720a..75bd3d44a3 100644
--- a/lustre/include/linux/obd.h
+++ b/lustre/include/linux/obd.h
@@ -401,6 +401,8 @@ struct echo_client_obd {
 struct cache_obd {
         struct obd_export *cobd_real_exp;/* local connection to target obd */
         struct obd_export *cobd_cache_exp; /* local connection to cache obd */
+        char   *cobd_real_name;
+        char   *cobd_cache_name;
         int    refcount;
         int    cache_on;
 };
diff --git a/lustre/smfs/dir.c b/lustre/smfs/dir.c
index 4a3c943f85..02e23ba5e1 100644
--- a/lustre/smfs/dir.c
+++ b/lustre/smfs/dir.c
@@ -65,7 +65,7 @@ static int smfs_create(struct inode *dir, struct dentry *dentry,
         if (IS_ERR(handle))
                        RETURN(-ENOSPC);
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_CREATE, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_CREATE, handle, dir, rc);
 
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
@@ -134,11 +134,10 @@ static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
         if (!(cache_dir = I2CI(dir)))
                 RETURN(ERR_PTR(-ENOENT));
 
-        handle = smfs_trans_start(dir, KML_CACHE_NOOP, NULL);
-        if (IS_ERR(handle))
-                RETURN(ERR_PTR(-ENOSPC));
-
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir, rc2);
+        
+        if (rc2)
+                RETURN(ERR_PTR(rc2));
 
         /* preparing artificial backing fs dentries. */
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
@@ -204,10 +203,10 @@ static int smfs_link(struct dentry * old_dentry,
 
         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
         if (IS_ERR(handle))
-                       RETURN(-ENOSPC);
-
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir);
+                 RETURN(-ENOSPC);
 
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir, rc);
+              
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
 
@@ -266,7 +265,7 @@ static int smfs_unlink(struct inode * dir,
         if (IS_ERR(handle))
                 RETURN(-ENOSPC);
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir, rc);
 
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
@@ -317,7 +316,7 @@ static int smfs_symlink(struct inode *dir, struct dentry *dentry,
         if (IS_ERR(handle))
                 RETURN(-ENOSPC);
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir, rc);
 
         pre_smfs_inode(dir, cache_dir);
         lock_kernel();
@@ -363,7 +362,7 @@ static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
         if (IS_ERR(handle))
                 RETURN(-ENOSPC);
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir, rc);
 
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
@@ -419,7 +418,7 @@ static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
                 RETURN(-ENOSPC);
         }
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir, rc);
 
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
@@ -472,7 +471,7 @@ static int smfs_mknod(struct inode *dir, struct dentry *dentry,
                 CERROR("smfs_do_mkdir: no space for transaction\n");
                 RETURN(-ENOSPC);
         }
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir, rc);
 
         cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
@@ -537,7 +536,7 @@ static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
         }
         lock_kernel();
 
-        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir);
+        SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir, rc);
 
         cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
diff --git a/lustre/smfs/journal.c b/lustre/smfs/journal.c
index 838440a625..d23fcd5d21 100644
--- a/lustre/smfs/journal.c
+++ b/lustre/smfs/journal.c
@@ -60,7 +60,7 @@ void *smfs_trans_start(struct inode *inode, int op, void *desc_private)
         CDEBUG(D_INFO, "trans start %p\n", fsfilt->fs_start);
 
         SMFS_TRANS_OP(inode, op);
-
+        
         /* There are some problem here. fs_start in fsfilt is used by lustre
          * the journal blocks of write rec are not counted in FIXME later */
         if (fsfilt->fs_start)
@@ -72,6 +72,9 @@ void smfs_trans_commit(struct inode *inode, void *handle, int force_sync)
 {
         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
 
+        if (!handle)
+                return;
+
         CDEBUG(D_INFO, "trans commit %p\n", fsfilt->fs_commit);
 
         if (fsfilt->fs_commit)
diff --git a/lustre/smfs/smfs_internal.h b/lustre/smfs/smfs_internal.h
index 46a750fabf..53ae60510a 100644
--- a/lustre/smfs/smfs_internal.h
+++ b/lustre/smfs/smfs_internal.h
@@ -284,14 +284,21 @@ static inline int get_active_entry(struct inode *dir, __u64 *active_entry)
 
 #define CACHE_HOOK_MAX          9
 
-#define SMFS_CACHE_HOOK_PRE(op, handle, dir)                            \
-{                                                                       \
-        if (smfs_cache_hook(dir)) {                                     \
-                LASSERT(handle != NULL);                                \
-                CDEBUG(D_INODE, "cache hook pre: op %d, dir %lu\n",     \
-                       op, dir->i_ino);                                 \
-                cache_space_pre(dir, op);                               \
-        }                                                               \
+#define SMFS_CACHE_HOOK_PRE(op, handle, dir, rc)                                \
+{                                                                               \
+        while (smfs_cache_hook(dir)) {                                          \
+                if (!handle) {                                                  \
+                        handle = smfs_trans_start(dir, KML_CACHE_NOOP, NULL);   \
+                        if (IS_ERR(handle)) {                                   \
+                               rc = -ENOSPC;                                    \
+                               break;                                           \
+                        }                                                       \
+                }                                                               \
+                CDEBUG(D_INODE, "cache hook pre: op %d, dir %lu\n",             \
+                       op, dir->i_ino);                                         \
+                cache_space_pre(dir, op);                                       \
+                break;                                                          \
+        }                                                                       \
 }
 
 #define SMFS_CACHE_HOOK_POST(op, handle, old_dir, old_dentry,           \
-- 
GitLab