Whamcloud - gitweb
- unland b_fid to HEAD
[fs/lustre-release.git] / lustre / mds / mds_lmv.c
index a3a9144..df80c75 100644 (file)
 int mds_lmv_connect(struct obd_device *obd, char * lmv_name)
 {
         struct mds_obd *mds = &obd->u.mds;
-        struct lustre_handle conn = {0,};
-        int valsize, mdsize;
-        int rc;
+        struct lustre_handle conn = {0};
+        int rc, valsize, value;
         ENTRY;
 
         if (IS_ERR(mds->mds_lmv_obd))
                 RETURN(PTR_ERR(mds->mds_lmv_obd));
 
-        if (mds->mds_lmv_obd)
+        if (mds->mds_lmv_connected)
+                RETURN(0);
+
+        down(&mds->mds_lmv_sem);
+        if (mds->mds_lmv_connected) {
+                up(&mds->mds_lmv_sem);
                 RETURN(0);
+        }
 
         mds->mds_lmv_obd = class_name2obd(lmv_name);
         if (!mds->mds_lmv_obd) {
                 CERROR("MDS cannot locate LMV %s\n",
                        lmv_name);
                 mds->mds_lmv_obd = ERR_PTR(-ENOTCONN);
-                RETURN(-ENOTCONN);
+                GOTO(err_last, rc = -ENOTCONN);
         }
 
-        rc = obd_connect(&conn, mds->mds_lmv_obd, &obd->obd_uuid);
+        rc = obd_connect(&conn, mds->mds_lmv_obd, &obd->obd_uuid, OBD_OPT_MDS_CONNECTION);
         if (rc) {
                 CERROR("MDS cannot connect to LMV %s (%d)\n",
                        lmv_name, rc);
                 mds->mds_lmv_obd = ERR_PTR(rc);
-                RETURN(rc);
+                GOTO(err_last, rc);
         }
         mds->mds_lmv_exp = class_conn2export(&conn);
         if (mds->mds_lmv_exp == NULL)
@@ -79,51 +84,59 @@ int mds_lmv_connect(struct obd_device *obd, char * lmv_name)
 
         rc = obd_register_observer(mds->mds_lmv_obd, obd);
         if (rc) {
-                CERROR("MDS cannot register as observer of LMV %s (%d)\n",
-                       lmv_name, rc);
+                CERROR("MDS cannot register as observer of LMV %s, "
+                       "rc = %d\n", lmv_name, rc);
                 GOTO(err_discon, rc);
         }
 
         /* retrieve size of EA */
         rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsize"), "mdsize", 
-                          &valsize, &mdsize);
+                          &valsize, &value);
         if (rc) 
                 GOTO(err_reg, rc);
-        if (mdsize > mds->mds_max_mdsize)
-                mds->mds_max_mdsize = mdsize;
+
+        if (value > mds->mds_max_mdsize)
+                mds->mds_max_mdsize = value;
 
         /* find our number in LMV cluster */
         rc = obd_get_info(mds->mds_lmv_exp, strlen("mdsnum"), "mdsnum", 
-                          &valsize, &mdsize);
+                          &valsize, &value);
         if (rc) 
                 GOTO(err_reg, rc);
-        mds->mds_num = mdsize;
+        
+        mds->mds_num = value;
 
         rc = obd_set_info(mds->mds_lmv_exp, strlen("inter_mds"),
-                                "inter_mds", 0, NULL);
+                          "inter_mds", 0, NULL);
         if (rc)
                 GOTO(err_reg, rc);
+
+        mds->mds_lmv_connected = 1;
+        up(&mds->mds_lmv_sem);
        RETURN(0);
 
 err_reg:
-        RETURN(rc);
-
+        obd_register_observer(mds->mds_lmv_obd, NULL);
 err_discon:
-        /* FIXME: cleanups here! */
         obd_disconnect(mds->mds_lmv_exp, 0);
         mds->mds_lmv_exp = NULL;
         mds->mds_lmv_obd = ERR_PTR(rc);
+err_last:
+        up(&mds->mds_lmv_sem);
         RETURN(rc);
 }
 
 int mds_lmv_postsetup(struct obd_device *obd)
 {
         struct mds_obd *mds = &obd->u.mds;
+        int rc = 0;
         ENTRY;
+
         if (mds->mds_lmv_exp)
-                obd_init_ea_size(mds->mds_lmv_exp, mds->mds_max_mdsize,
-                                 mds->mds_max_cookiesize);
-        RETURN(0);
+                rc = obd_init_ea_size(mds->mds_lmv_exp, mds->mds_max_mdsize,
+                                      mds->mds_max_cookiesize);
+        
+        RETURN(rc);
 }
 
 int mds_lmv_disconnect(struct obd_device *obd, int flags)
@@ -132,26 +145,29 @@ int mds_lmv_disconnect(struct obd_device *obd, int flags)
         int rc = 0;
         ENTRY;
 
+        down(&mds->mds_lmv_sem);
         if (!IS_ERR(mds->mds_lmv_obd) && mds->mds_lmv_exp != NULL) {
-
+                LASSERT(mds->mds_lmv_connected != 0);
+                mds->mds_lmv_connected = 0;
                 obd_register_observer(mds->mds_lmv_obd, NULL);
 
+                /* if obd_disconnect fails (probably because the export was
+                 * disconnected by class_disconnect_exports) then we just need
+                 * to drop our ref. */
                 rc = obd_disconnect(mds->mds_lmv_exp, flags);
-                /* if obd_disconnect fails (probably because the
-                 * export was disconnected by class_disconnect_exports)
-                 * then we just need to drop our ref. */
-                if (rc != 0)
+                if (rc)
                         class_export_put(mds->mds_lmv_exp);
+                
                 mds->mds_lmv_exp = NULL;
                 mds->mds_lmv_obd = NULL;
         }
+        up(&mds->mds_lmv_sem);
 
         RETURN(rc);
 }
 
-
 int mds_get_lmv_attr(struct obd_device *obd, struct inode *inode,
-                       struct mea **mea, int *mea_size)
+                     struct mea **mea, int *mea_size)
 {
         struct mds_obd *mds = &obd->u.mds;
        int rc;
@@ -159,24 +175,23 @@ int mds_get_lmv_attr(struct obd_device *obd, struct inode *inode,
 
        if (!mds->mds_lmv_obd)
                RETURN(0);
+        if (!S_ISDIR(inode->i_mode))
+                RETURN(0);
 
        /* first calculate mea size */
         *mea_size = obd_alloc_diskmd(mds->mds_lmv_exp,
                                      (struct lov_mds_md **)mea);
-       /* FIXME: error handling here */
-       LASSERT(*mea != NULL);
+        if (*mea_size < 0 || *mea == NULL)
+                return *mea_size < 0 ? *mea_size : -EINVAL;
+
+        rc = mds_get_md(obd, inode, *mea, mea_size, 1);
 
-       down(&inode->i_sem);
-       rc = fsfilt_get_md(obd, inode, *mea, *mea_size);
-       up(&inode->i_sem);
-       /* FIXME: error handling here */
        if (rc <= 0) {
                OBD_FREE(*mea, *mea_size);
                *mea = NULL;
-       }
-        if (rc > 0)
+       } else
                 rc = 0;
-                        
+
        RETURN(rc);
 }
 
@@ -223,7 +238,7 @@ static int dc_new_page_to_cache(struct dir_cache * dirc)
         page = alloc_page(GFP_KERNEL);
         if (page == NULL)
                 return -ENOMEM;
-        list_add_tail(&page->list, &dirc->list);
+        list_add_tail(&page->lru, &dirc->list);
         dirc->cur = page_address(page);
         dirc->free = PAGE_SIZE;
         return 0;
@@ -286,13 +301,13 @@ static int flush_buffer_onto_mds(struct dirsplit_control *dc, int mdsnum)
         list_for_each_safe(cur, tmp, &ca->list) {
                 struct page *page;
 
-                page = list_entry(cur, struct page, list);
+                page = list_entry(cur, struct page, lru);
                 LASSERT(page != NULL);
 
                 retrieve_generation_numbers(dc, page_address(page));
 
                 ca->brwc.pg = page;
-                ca->brwc.off = 0;
+                ca->brwc.disk_offset = ca->brwc.page_offset = 0;
                 ca->brwc.count = PAGE_SIZE;
                 ca->brwc.flag = 0;
                 ca->oa.o_mds = mdsnum;
@@ -319,7 +334,7 @@ static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
 
         ca = dc->cache + mdsnum;
         list_for_each_safe(cur, tmp, &ca->list) {
-                page = list_entry(cur, struct page, list);
+                page = list_entry(cur, struct page, lru);
                 buf = page_address(page);
                 end = buf + PAGE_SIZE;
 
@@ -335,7 +350,6 @@ static int remove_entries_from_orig_dir(struct dirsplit_control *dc, int mdsnum)
                                                 de->name, (int) PTR_ERR(dentry));
                                 goto next;
                         }
-                        LASSERT(dentry->d_inode != NULL);
                         rc = fsfilt_del_dir_entry(dc->obd, dentry);
                         l_dput(dentry);
 next:
@@ -464,8 +478,8 @@ cleanup:
                         continue;
                 list_for_each_safe(cur, tmp, &dc.cache[i].list) {
                         struct page *page;
-                        page = list_entry(cur, struct page, list);
-                        list_del(&page->list);
+                        page = list_entry(cur, struct page, lru);
+                        list_del(&page->lru);
                         __free_page(page);
                 }
         }
@@ -477,6 +491,8 @@ cleanup:
 
 #define MAX_DIR_SIZE    (64 * 1024)
 
+#define I_NON_SPLITTABLE        256
+
 int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
 {
         struct mds_obd *mds = &obd->u.mds;
@@ -485,31 +501,37 @@ int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
 
        /* clustered MD ? */
        if (!mds->mds_lmv_obd)
-               RETURN(0);
+               RETURN(MDS_NO_SPLITTABLE);
 
         /* inode exist? */
         if (dentry->d_inode == NULL)
-                return 0;
+                return MDS_NO_SPLITTABLE;
 
         /* a dir can be splitted only */
         if (!S_ISDIR(dentry->d_inode->i_mode))
-                return 0;
+                return MDS_NO_SPLITTABLE;
 
-        /* large enough to be splitted? */
-        if (dentry->d_inode->i_size < MAX_DIR_SIZE)
-                return 0;
+        /* already splittied or slave directory (part of splitted dir) */
+        if (dentry->d_inode->i_flags & I_NON_SPLITTABLE)
+                return MDS_NO_SPLITTABLE;
 
         /* don't split root directory */
         if (dentry->d_inode->i_ino == mds->mds_rootfid.id)
-                return 0;
+                return MDS_NO_SPLITTABLE;
+
+        /* large enough to be splitted? */
+        if (dentry->d_inode->i_size < MAX_DIR_SIZE)
+                return MDS_NO_SPLIT_EXPECTED;
 
         mds_get_lmv_attr(obd, dentry->d_inode, &mea, &size);
         if (mea) {
                 /* already splitted or slave object: shouldn't be splitted */
-                rc = 0;
+                rc = MDS_NO_SPLITTABLE;
+                /* mark to skip subsequent checks */
+                dentry->d_inode->i_flags |= I_NON_SPLITTABLE;
         } else {
                 /* may be splitted */
-                rc = 1;
+                rc = MDS_EXPECT_SPLIT;
         }
 
         if (mea)
@@ -518,10 +540,10 @@ int mds_splitting_expected(struct obd_device *obd, struct dentry *dentry)
 }
 
 /*
- * must not be called on already splitted directories
+ * must not be called on already splitted directories.
  */
-int mds_try_to_split_dir(struct obd_device *obd,
-                         struct dentry *dentry, struct mea **mea, int nstripes)
+int mds_try_to_split_dir(struct obd_device *obd, struct dentry *dentry,
+                         struct mea **mea, int nstripes, int update_mode)
 {
         struct inode *dir = dentry->d_inode;
         struct mds_obd *mds = &obd->u.mds;
@@ -531,9 +553,17 @@ int mds_try_to_split_dir(struct obd_device *obd,
        void *handle;
        ENTRY;
 
+        if (update_mode != LCK_EX)
+                return 0;
         /* TODO: optimization possible - we already may have mea here */
-        if (!mds_splitting_expected(obd, dentry))
-                RETURN(0);
+        rc = mds_splitting_expected(obd, dentry);
+        if (rc == MDS_NO_SPLITTABLE)
+                return 0;
+        if (rc == MDS_NO_SPLIT_EXPECTED && nstripes == 0)
+                return 0;
+        if (nstripes && nstripes == 1)
+                return 0;
+        
         LASSERT(mea == NULL || *mea == NULL);
 
         CDEBUG(D_OTHER, "%s: split directory %u/%lu/%lu\n",
@@ -548,52 +578,84 @@ int mds_try_to_split_dir(struct obd_device *obd,
          * necessary amount of stripes, but on the other hand with this
          * approach of allocating maximal possible amount of MDS slots,
          * it would be easier to split the dir over more MDSes */
-        rc = obd_alloc_diskmd(mds->mds_lmv_exp, (void *) mea);
-        if (!(*mea))
-                RETURN(-ENOMEM);
+        rc = obd_alloc_diskmd(mds->mds_lmv_exp, (void *)mea);
+        if (rc < 0) {
+                CERROR("obd_alloc_diskmd() failed, error %d.\n", rc);
+                RETURN(rc);
+        }
+        if (*mea == NULL)
+                RETURN(-EINVAL);
+
         (*mea)->mea_count = nstripes;
        
        /* 1) create directory objects on slave MDS'es */
        /* FIXME: should this be OBD method? */
         oa = obdo_alloc();
-        /* FIXME: error handling here */
-        LASSERT(oa != NULL);
+        if (!oa)
+                RETURN(-ENOMEM);
+
        oa->o_id = dir->i_ino;
        oa->o_generation = dir->i_generation;
-       obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
+
+        obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
                        OBD_MD_FLMTIME | OBD_MD_FLCTIME |
                         OBD_MD_FLUID | OBD_MD_FLGID);
+
         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
         oa->o_mode = dir->i_mode;
+
         CDEBUG(D_OTHER, "%s: create subdirs with mode %o, uid %u, gid %u\n",
                         obd->obd_name, dir->i_mode, dir->i_uid, dir->i_gid);
                         
         rc = obd_create(mds->mds_lmv_exp, oa,
-                        (struct lov_stripe_md **) mea, NULL);
-        /* FIXME: error handling here */
-       LASSERT(rc == 0);
-        CDEBUG(D_OTHER, "%d dirobjects created\n",
-               (int) (*mea)->mea_count);
+                        (struct lov_stripe_md **)mea, NULL);
+        if (rc)
+                GOTO(err_oa, rc);
+
+        CDEBUG(D_OTHER, "%d dirobjects created\n", (int)(*mea)->mea_count);
 
        /* 2) update dir attribute */
         down(&dir->i_sem);
+        
         handle = fsfilt_start(obd, dir, FSFILT_OP_SETATTR, NULL);
-        LASSERT(!IS_ERR(handle));
+        if (IS_ERR(handle)) {
+                up(&dir->i_sem);
+                CERROR("fsfilt_start() failed: %d\n", (int) PTR_ERR(handle));
+                GOTO(err_oa, rc = PTR_ERR(handle));
+        }
+        
        rc = fsfilt_set_md(obd, dir, handle, *mea, mea_size);
-        LASSERT(rc == 0);
-        fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
-        LASSERT(rc == 0);
+        if (rc) {
+                up(&dir->i_sem);
+                CERROR("fsfilt_set_md() failed, error %d.\n", rc);
+                GOTO(err_oa, rc);
+        }
+        
+        rc = fsfilt_commit(obd, mds->mds_sb, dir, handle, 0);
+        if (rc) {
+                up(&dir->i_sem);
+                CERROR("fsfilt_commit() failed, error %d.\n", rc);
+                GOTO(err_oa, rc);
+        }
+        
        up(&dir->i_sem);
        obdo_free(oa);
 
        /* 3) read through the dir and distribute it over objects */
-        scan_and_distribute(obd, dentry, *mea);
-
+        rc = scan_and_distribute(obd, dentry, *mea);
        if (mea == &tmea)
-                obd_free_diskmd(mds->mds_lmv_exp,
-                                (struct lov_mds_md **) mea);
+                obd_free_diskmd(mds->mds_lmv_exp, (struct lov_mds_md **)mea);
+        if (rc) {
+                CERROR("scan_and_distribute() failed, error %d.\n", rc);
+                RETURN(rc);
+        }
+
        RETURN(1);
+
+err_oa:
+       obdo_free(oa);
+        RETURN(rc);
 }
 
 static int filter_start_page_write(struct inode *inode,
@@ -637,7 +699,11 @@ int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
         fid.id = obj->ioo_id;
         fid.generation = obj->ioo_gr;
         dentry = mds_fid2dentry(mds, &fid, NULL);
-        LASSERT(!IS_ERR(dentry));
+        if (IS_ERR(dentry)) {
+                CERROR("can't get dentry for "LPU64"/%u: %d\n",
+                       fid.id, fid.generation, (int) PTR_ERR(dentry));
+                GOTO(cleanup, rc = (int) PTR_ERR(dentry));
+        }
 
         if (dentry->d_inode == NULL) {
                 CERROR("trying to BRW to non-existent file "LPU64"\n",
@@ -712,12 +778,24 @@ int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
                         err = fsfilt_add_dir_entry(obd, res->dentry, de->name,
                                                    de->namelen, de->ino,
                                                    de->generation, de->mds);
+                        if (err) {
+                                CERROR("can't add dir entry %*s->%u/%u/%u"
+                                       " to %lu/%u: %d\n",
+                                       de->namelen, de->name,
+                                       de->mds, (unsigned) de->ino,
+                                       (unsigned) de->generation,
+                                       res->dentry->d_inode->i_ino,
+                                       res->dentry->d_inode->i_generation,
+                                       err);
+                                rc = err;
+                                break;
+                        }
                         LASSERT(err == 0);
                         de = (struct dir_entry *)
                                 ((char *) de + DIR_REC_LEN(de->namelen));
                         entries++;
                 }
-                kunmap(buf);
+                kunmap(lnb->page);
         }
 
         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++)
@@ -737,7 +815,7 @@ int mds_choose_mdsnum(struct obd_device *obd, const char *name, int len, int fla
                 i = mds->mds_num;
         } else if (mds->mds_lmv_exp) {
                 lmv = &mds->mds_lmv_exp->exp_obd->u.lmv;
-                i = raw_name2idx(lmv->desc.ld_tgt_count, name, len);
+                i = raw_name2idx(MEA_MAGIC_LAST_CHAR, lmv->desc.ld_tgt_count, name, len);
         }
         RETURN(i);
 }
@@ -938,11 +1016,19 @@ int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
         struct dentry *dentry = NULL;
         struct lvfs_run_ctxt saved;
         int cleanup_phase = 0;
+        struct mds_req_sec_desc *rsd;
         struct mds_body *body;
         struct lvfs_ucred uc;
         int rc, update_mode;
         ENTRY;
 
+        rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
+        if (!rsd) {
+                CERROR("Can't unpack security desc\n");
+                GOTO(cleanup, rc = -EFAULT);
+        }
+        mds_squash_root(&obd->u.mds, rsd, &req->rq_peer.peer_id.nid); 
+
         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
                                   lustre_swab_mds_body);
         if (body == NULL) {
@@ -963,11 +1049,12 @@ int mds_lock_and_check_slave(int offset, struct ptlrpc_request *req,
 
         LASSERT(S_ISDIR(dentry->d_inode->i_mode));
 
-        uc.luc_fsuid = body->fsuid;
-        uc.luc_fsgid = body->fsgid;
-        uc.luc_cap = body->capability;
-        uc.luc_suppgid1 = body->suppgid;
-        uc.luc_suppgid2 = -1;
+        rc = mds_init_ucred(&uc, rsd);
+        if (rc) {
+                CERROR("can't init ucred\n");
+                GOTO(cleanup, rc);
+        }
+
         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
 
         rc = 0;
@@ -981,9 +1068,69 @@ cleanup:
                         ldlm_lock_decref(lockh, LCK_EX);
                 l_dput(dentry);
                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
+                mds_exit_ucred(&uc);
         default:
                 break;
         }
         RETURN(rc);
 }
 
+int mds_convert_mea_ea(struct obd_device *obd, struct inode *inode,
+                       struct lov_mds_md *lmm, int lmmsize)
+{
+        int i, rc, err, size;
+        struct mea_old *old;
+        struct mea *mea;
+        struct mea *new;
+        void *handle;
+        ENTRY;
+
+        mea = (struct mea *) lmm;
+        if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
+                mea->mea_magic == MEA_MAGIC_ALL_CHARS)
+                RETURN(0);
+
+        old = (struct mea_old *) lmm;
+        rc = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea_old);
+        if (old->mea_count > 256 || old->mea_master > 256 || lmmsize < rc
+                        || old->mea_master > old->mea_count) {
+                CWARN("unknown MEA format, dont convert it\n");
+                CWARN("  count %u, master %u, size %u\n",
+                      old->mea_count, old->mea_master, rc);
+                RETURN(0);
+        }
+                
+        CWARN("converting MEA EA on %lu/%u from V0 to V1 (%u/%u)\n",
+              inode->i_ino, inode->i_generation, old->mea_count, old->mea_master);
+
+        size = sizeof(struct ll_fid) * old->mea_count + sizeof(struct mea);
+        OBD_ALLOC(new, size);
+        if (new == NULL)
+                RETURN(-ENOMEM);
+
+        new->mea_magic = MEA_MAGIC_LAST_CHAR;
+        new->mea_count = old->mea_count;
+        new->mea_master = old->mea_master;
+        for (i = 0; i < new->mea_count; i++)
+                new->mea_fids[i] = old->mea_fids[i];
+
+        handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
+        if (IS_ERR(handle)) {
+                rc = PTR_ERR(handle);
+                GOTO(conv_free, rc);
+        }
+
+        rc = fsfilt_set_md(obd, inode, handle, (struct lov_mds_md *) new, size);
+        if (rc > lmmsize)
+                size = lmmsize;
+        memcpy(lmm, new, size);
+
+        err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
+        if (!rc)
+                rc = err ? err : size;
+        GOTO(conv_free, rc);
+conv_free:
+        OBD_FREE(new, size);
+        return rc;
+}
+