Whamcloud - gitweb
Changed quotes for newer bash
[fs/lustre-release.git] / lustre / mds / mds_unlink_open.c
index 415b133..c480885 100644 (file)
@@ -32,7 +32,7 @@
 #include <linux/module.h>
 #include <linux/version.h>
 
-#include <portals/list.h>
+#include <libcfs/list.h>
 #include <linux/obd_class.h>
 #include <linux/lustre_fsfilt.h>
 #include <linux/lustre_commit_confd.h>
 
 #include "mds_internal.h"
 
-
-/* If we are unlinking an open file/dir (i.e. creating an orphan) then
- * we instead link the inode into the PENDING directory until it is
- * finally released.  We can't simply call mds_reint_rename() or some
- * part thereof, because we don't have the inode to check for link
- * count/open status until after it is locked.
- *
- * For lock ordering, we always get the PENDING, then pending_child lock
- * last to avoid deadlocks.
+/*
+ * used when destroying orphanes and from mds_reint_unlink() when MDS wants to
+ * destroy objects on OSS.
  */
-
-int mds_open_unlink_rename(struct mds_update_record *rec,
-                           struct obd_device *obd, struct dentry *dparent,
-                           struct dentry *dchild, void **handle)
-{
-        struct mds_obd *mds = &obd->u.mds;
-        struct inode *pending_dir = mds->mds_pending_dir->d_inode;
-        struct dentry *pending_child;
-        char fidname[LL_FID_NAMELEN];
-        int fidlen = 0, rc;
-        ENTRY;
-
-        LASSERT(!mds_inode_is_orphan(dchild->d_inode));
-
-        down(&pending_dir->i_sem);
-        fidlen = ll_fid2str(fidname, dchild->d_inode->i_ino,
-                            dchild->d_inode->i_generation);
-
-        CWARN("pending destroy of %dx open file %s = %s\n",
-              mds_open_orphan_count(dchild->d_inode),
-              rec->ur_name, fidname);
-
-        pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
-        if (IS_ERR(pending_child))
-                GOTO(out_lock, rc = PTR_ERR(pending_child));
-
-        if (pending_child->d_inode != NULL) {
-                CERROR("re-destroying orphan file %s?\n", rec->ur_name);
-                LASSERT(pending_child->d_inode == dchild->d_inode);
-                GOTO(out_dput, rc = 0);
-        }
-
-        *handle = fsfilt_start(obd, pending_dir, FSFILT_OP_RENAME, NULL);
-        if (IS_ERR(*handle))
-                GOTO(out_dput, rc = PTR_ERR(*handle));
-
-        lock_kernel();
-        rc = vfs_rename(dparent->d_inode, dchild, pending_dir, pending_child);
-        unlock_kernel();
-        if (rc)
-                CERROR("error renaming orphan %lu/%s to PENDING: rc = %d\n",
-                       dparent->d_inode->i_ino, rec->ur_name, rc);
-        else
-                mds_inode_set_orphan(dchild->d_inode);
-out_dput:
-        dput(pending_child);
-out_lock:
-        up(&pending_dir->i_sem);
-        RETURN(rc);
-}
-
-static int mds_osc_destroy_orphan(struct mds_obd *mds, 
-                                  struct ptlrpc_request *request)
+int
+mds_unlink_object(struct mds_obd *mds, struct inode *inode,
+                  struct lov_mds_md *lmm, int lmm_size,
+                  struct llog_cookie *logcookies,
+                  int log_unlink, int async)
 {
-        struct mds_body *body;
-        struct lov_mds_md *lmm = NULL;
         struct lov_stripe_md *lsm = NULL;
         struct obd_trans_info oti = { 0 };
         struct obdo *oa;
         int rc;
         ENTRY;
 
-        body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
-        if (!(body->valid & OBD_MD_FLEASIZE))
+        if (lmm_size == 0)
                 RETURN(0);
-        if (body->eadatasize == 0) {
-                CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
-                RETURN(rc = -EPROTO); 
-        }
-
-        lmm = lustre_msg_buf(request->rq_repmsg, 1, body->eadatasize);
-        LASSERT(lmm != NULL);
 
-        rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, body->eadatasize);
+        rc = obd_unpackmd(mds->mds_dt_exp, &lsm, lmm, lmm_size);
         if (rc < 0) {
                 CERROR("Error unpack md %p\n", lmm);
                 RETURN(rc);
@@ -136,27 +72,25 @@ static int mds_osc_destroy_orphan(struct mds_obd *mds,
         if (oa == NULL)
                 GOTO(out_free_memmd, rc = -ENOMEM);
         oa->o_id = lsm->lsm_object_id;
-        oa->o_mode = body->mode & S_IFMT;
-        oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
+        oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
+        oa->o_mode = inode->i_mode & S_IFMT;
+        oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
 
-        if (body->valid & OBD_MD_FLCOOKIE) {
+        if (log_unlink && logcookies) {
                 oa->o_valid |= OBD_MD_FLCOOKIE;
-                oti.oti_logcookies = 
-                        lustre_msg_buf(request->rq_repmsg, 2,
-                                       sizeof(struct llog_cookie) *
-                                       lsm->lsm_stripe_count);
-                if (oti.oti_logcookies == NULL)
-                        oa->o_valid &= ~OBD_MD_FLCOOKIE;
-                        body->valid &= ~OBD_MD_FLCOOKIE;
+                oti.oti_logcookies = logcookies;
         }
 
-        rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
+        CDEBUG(D_INODE, "destroy OSS object %d/%d\n",
+               (int)oa->o_id, (int)oa->o_gr);
+
+        if (async)
+                oti.oti_flags |= OBD_MODE_ASYNC;
+        
+        rc = obd_destroy(mds->mds_dt_exp, oa, lsm, &oti);
         obdo_free(oa);
-        if (rc) 
-                CERROR("destroy orphan objid 0x"LPX64" on ost error "
-                       "%d\n", lsm->lsm_object_id, rc);
 out_free_memmd:
-        obd_free_memmd(mds->mds_osc_exp, &lsm);
+        obd_free_memmd(mds->mds_dt_exp, &lsm);
         RETURN(rc);
 }
 
@@ -164,158 +98,177 @@ static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
                              struct inode *inode, struct inode *pending_dir)
 {
         struct mds_obd *mds = &obd->u.mds;
-        struct mds_body *body;
+        struct lov_mds_md *lmm = NULL;
+        struct llog_cookie *logcookies = NULL;
+        int lmm_size, log_unlink = 0;
         void *handle = NULL;
-        struct ptlrpc_request *req;
-        int lengths[3] = {sizeof(struct mds_body),
-                          mds->mds_max_mdsize,
-                          mds->mds_max_cookiesize};
-        int rc;
+        int rc, err;
         ENTRY;
 
-        LASSERT(mds->mds_osc_obd != NULL);
-        OBD_ALLOC(req, sizeof(*req));
-        if (!req) {
-                CERROR("request allocation out of memory\n");
-                GOTO(err_alloc_req, rc = -ENOMEM);
-        }
-        rc = lustre_pack_reply(req, 3, lengths, NULL);
-        if (rc) {
-                CERROR("cannot pack request %d\n", rc);
-                GOTO(out_free_req, rc);
+        LASSERT(mds->mds_dt_obd != NULL);
+        LASSERT(obd->obd_recovering == 0);
+
+        /* We don't need to do any of these other things for orhpan dirs,
+         * especially not mds_get_md (may get a default LOV EA, bug 4554) */
+        if (S_ISDIR(inode->i_mode)) {
+                rc = vfs_rmdir(pending_dir, dchild);
+                if (rc)
+                        CERROR("error %d unlinking dir %*s from PENDING\n",
+                               rc, dchild->d_name.len, dchild->d_name.name);
+                RETURN(rc);
         }
-        body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
-        LASSERT(body != NULL);
 
-        mds_pack_inode2body(body, inode);
-        mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
+        lmm_size = mds->mds_max_mdsize;
+        OBD_ALLOC(lmm, lmm_size);
+        if (lmm == NULL)
+                RETURN(-ENOMEM);
+
+        rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0);
+        if (rc < 0)
+                GOTO(out_free_lmm, rc);
 
-        handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG, NULL);
+        handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
+                                  le32_to_cpu(lmm->lmm_stripe_count));
         if (IS_ERR(handle)) {
                 rc = PTR_ERR(handle);
                 CERROR("error fsfilt_start: %d\n", rc);
                 handle = NULL;
-                GOTO(out_free_msg, rc);
+                GOTO(out_free_lmm, rc);
         }
 
-        if (S_ISDIR(inode->i_mode)) {
-                rc = vfs_rmdir(pending_dir, dchild);
-        } else {
-                rc = vfs_unlink(pending_dir, dchild);
-        }
-        if (rc) 
-                CERROR("error %d unlinking orphan %*s from PENDING directory\n",
+        rc = vfs_unlink(pending_dir, dchild);
+        if (rc) {
+                CERROR("error %d unlinking orphan %.*s from PENDING\n",
                        rc, dchild->d_name.len, dchild->d_name.name);
-
-        if ((body->valid & OBD_MD_FLEASIZE)) {
-                if (mds_log_op_unlink(obd, inode, req->rq_repmsg, 1) > 0)
-                        body->valid |= OBD_MD_FLCOOKIE;
+        } else if (lmm_size) {
+                OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
+                if (logcookies == NULL)
+                        rc = -ENOMEM;
+                else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
+                                           mds->mds_max_cookiesize, NULL) > 0)
+                        log_unlink = 1;
         }
-
-        if (handle) {
-                int err = fsfilt_commit(obd, pending_dir, handle, 0);
-                if (err) {
-                        CERROR("error committing orphan unlink: %d\n", err);
+        err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 0);
+        if (err) {
+                CERROR("error committing orphan unlink: %d\n", err);
+                if (!rc)
                         rc = err;
-                        GOTO(out_free_msg, rc);
-                }
+        } else if (!rc) {
+                rc = mds_unlink_object(mds, inode, lmm, lmm_size,
+                                       logcookies, log_unlink, 0);
         }
-        rc = mds_osc_destroy_orphan(mds, req);
-out_free_msg:
-        OBD_FREE(req->rq_repmsg, req->rq_replen);
-        req->rq_repmsg = NULL;
-out_free_req:
-        OBD_FREE(req, sizeof(*req));
-err_alloc_req:
+
+        if (logcookies != NULL)
+                OBD_FREE(logcookies, mds->mds_max_cookiesize);
+out_free_lmm:
+        OBD_FREE(lmm, mds->mds_max_mdsize);
         RETURN(rc);
 }
 
 int mds_cleanup_orphans(struct obd_device *obd)
 {
         struct mds_obd *mds = &obd->u.mds;
-        struct obd_run_ctxt saved;
+        struct lvfs_run_ctxt saved;
         struct file *file;
-        struct dentry *dchild;
+        struct dentry *dchild, *dentry;
+        struct vfsmount *mnt;
         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
-        struct l_linux_dirent *dirent, *ptr;
-        unsigned int count = pending_dir->i_size;
-        int rc = 0, rc2 = 0, item = 0;
+        struct l_linux_dirent *dirent, *n;
+        struct list_head dentry_list;
+        char d_name[LL_ID_NAMELEN];
+       unsigned long inum;
+        __u64 i = 0;
+        int rc = 0, item = 0, namlen;
         ENTRY;
 
-        push_ctxt(&saved, &obd->obd_ctxt, NULL);
-        dget(mds->mds_pending_dir);
-        mntget(mds->mds_vfsmnt);
+        LASSERT(obd->obd_recovering == 0);
+        push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        dentry = dget(mds->mds_pending_dir);
+        if (IS_ERR(dentry))
+                GOTO(err_pop, rc = PTR_ERR(dentry));
+        mnt = mntget(mds->mds_vfsmnt);
+        if (IS_ERR(mnt))
+                GOTO(err_mntget, rc = PTR_ERR(mnt));
+
         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
                            O_RDONLY | O_LARGEFILE);
         if (IS_ERR(file))
-                GOTO(err_open, rc2 = PTR_ERR(file));
-
-        OBD_ALLOC(dirent, count);
-        if (dirent == NULL)
-                GOTO(err_alloc_dirent, rc2 = -ENOMEM);
+                GOTO(err_pop, rc = PTR_ERR(file));
 
-        rc = l_readdir(file, dirent, count);
+        INIT_LIST_HEAD(&dentry_list);
+        rc = l_readdir(file, &dentry_list);
         filp_close(file, 0);
         if (rc < 0)
-                GOTO(err_out, rc2 = rc);
+                GOTO(err_out, rc);
+
+        list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
+                i++;
+                list_del(&dirent->lld_list);
+
+                namlen = strlen(dirent->lld_name);
+                LASSERT(sizeof(d_name) >= namlen + 1);
+                strcpy(d_name, dirent->lld_name);
+                inum = dirent->lld_ino;
+                OBD_FREE(dirent, sizeof(*dirent));
 
-        for (ptr = dirent; (char *)ptr < (char *)dirent + rc;
-                        (char *)ptr += ptr->d_reclen) {
-                int namlen = strlen(ptr->d_name);
+                CDEBUG(D_INODE, "entry "LPU64" of PENDING DIR: %s\n",
+                       i, d_name);
 
-                if (((namlen == 1) && !strcmp(ptr->d_name, ".")) ||
-                    ((namlen == 2) && !strcmp(ptr->d_name, "..")))
+                if (((namlen == 1) && !strcmp(d_name, ".")) ||
+                    ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
                         continue;
 
                 down(&pending_dir->i_sem);
-                dchild = lookup_one_len(ptr->d_name, mds->mds_pending_dir,
-                                        namlen);
+                dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
                 if (IS_ERR(dchild)) {
                         up(&pending_dir->i_sem);
-                        GOTO(err_out, rc2 = PTR_ERR(dchild));
+                        GOTO(err_out, rc = PTR_ERR(dchild));
                 }
                 if (!dchild->d_inode) {
-                        CDEBUG(D_ERROR, "orphan %s has been removed\n",
-                               ptr->d_name);
-                        GOTO(next, rc2 = 0);
+                        CERROR("orphan %s has been removed\n", d_name);
+                        GOTO(next, rc = 0);
                 }
 
-                child_inode = dchild->d_inode;
-                if (mds_inode_is_orphan(child_inode) &&
-                    mds_open_orphan_count(child_inode)) {
-                        CWARN("orphan %s was re-opened during recovery\n", 
-                              ptr->d_name);
-                        GOTO(next, rc2 = 0);
+                if (is_bad_inode(dchild->d_inode)) {
+                        CERROR("bad orphan inode found %lu/%u\n",
+                               dchild->d_inode->i_ino,
+                               dchild->d_inode->i_generation);
+                        GOTO(next, rc = -ENOENT);
                 }
 
-                rc2 = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
-                if (rc2 == 0) {
+                child_inode = dchild->d_inode;
+                DOWN_READ_I_ALLOC_SEM(child_inode);
+                if (mds_orphan_open_count(child_inode)) {
+                        UP_READ_I_ALLOC_SEM(child_inode);
+                        CWARN("orphan %s re-opened during recovery\n", d_name);
+                        GOTO(next, rc = 0);
+                }
+                UP_READ_I_ALLOC_SEM(child_inode);
+                rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
+                if (rc == 0) {
                         item ++;
-                        CWARN("removed orphan %s from MDS and OST\n",
-                               ptr->d_name);
+                        CWARN("removed orphan %s from MDS and OST\n", d_name);
                 } else {
-                        l_dput(dchild); 
-                        up(&pending_dir->i_sem);
-                        GOTO(err_out, rc2);
+                        CDEBUG(D_INODE, "removed orphan %s from MDS/OST failed,"
+                               " rc = %d\n", d_name, rc);
+                        rc = 0;
                 }
 next:
                 l_dput(dchild);
                 up(&pending_dir->i_sem);
         }
 err_out:
-        OBD_FREE(dirent, count);
+        list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
+                list_del(&dirent->lld_list);
+                OBD_FREE(dirent, sizeof(*dirent));
+        }
 err_pop:
-        pop_ctxt(&saved, &obd->obd_ctxt, NULL);
-        if (rc2 == 0)
-                rc2 = item;
-
-        RETURN(rc2);
+        pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
+        if (rc == 0)
+                rc = item;
+        RETURN(rc);
 
-err_open:
-        mntput(mds->mds_vfsmnt);
+err_mntget:
         l_dput(mds->mds_pending_dir);
         goto err_pop;
-err_alloc_dirent:
-        filp_close(file, 0);
-        goto err_pop;
 }