Whamcloud - gitweb
LU-2675 obd: decruft md_enqueue() and md_intent_lock()
[fs/lustre-release.git] / lustre / liblustre / namei.c
index 0403ad5..c129f0b 100644 (file)
@@ -1,45 +1,63 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- * Lustre Light name resolution
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Copyright (c) 2011, 2012, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/liblustre/namei.c
+ *
+ * Lustre Light name resolution
  */
 
 #define DEBUG_SUBSYSTEM S_LLITE
 
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
-#include <time.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/fcntl.h>
-#include <sys/queue.h>
-
-#include <sysio.h>
-#include <fs.h>
-#include <mount.h>
-#include <inode.h>
-#include <file.h>
-
-#undef LIST_HEAD
-
+#include <sys/types.h>
+#include <libcfs/libcfs.h>
+#include <lustre/lustre_idl.h>
+#include <liblustre.h>
+#include <lclient.h>
+#include <lustre_dlm.h>
+#include <lustre_fid.h>
+#include <lustre_mdc.h>
+#include <lustre_net.h>
+#include <obd.h>
+#include <obd_class.h>
+#include <obd_support.h>
 #include "llite_lib.h"
 
 static void ll_intent_drop_lock(struct lookup_intent *it)
@@ -70,46 +88,28 @@ static void ll_intent_release(struct lookup_intent *it)
         EXIT;
 }
 
-/*
- * remove the stale inode from pnode
- */
-void unhook_stale_inode(struct pnode *pno)
-{
-        struct inode *inode = pno->p_base->pb_ino;
-        ENTRY;
-
-        LASSERT(inode);
-        LASSERT(llu_i2info(inode)->lli_stale_flag);
-
-        pno->p_base->pb_ino = NULL;
-        I_RELE(inode);
-
-        if (!llu_i2info(inode)->lli_open_count) {
-                CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
-                                inode, llu_i2info(inode)->lli_st_ino, pno);
-                if (!inode->i_ref)
-                        _sysio_i_gone(inode);
-        }
-
-        EXIT;
-        return;
-}
-
-void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
+static void
+llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
 {
+        struct inode *inode;
         LASSERT(it);
         LASSERT(pnode);
 
-        if (it && pnode->p_base->pb_ino != NULL) {
-                struct inode *inode = pnode->p_base->pb_ino;
-                CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
-                       inode, llu_i2info(inode)->lli_st_ino,
+        inode = pnode->p_base->pb_ino;
+        if (it->d.lustre.it_lock_mode && inode != NULL) {
+                struct llu_sb_info *sbi;
+
+                CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
+                       inode, (long long)llu_i2stat(inode)->st_ino,
                        llu_i2info(inode)->lli_st_generation);
-                mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
+
+                sbi = llu_i2sbi(inode);
+                md_set_lock_data(sbi->ll_md_exp,
+                                 &it->d.lustre.it_lock_handle, inode, NULL);
         }
 
         /* drop lookup/getattr locks */
-        if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
+        if (it->it_op & (IT_LOOKUP | IT_GETATTR))
                 ll_intent_release(it);
 
 }
@@ -119,47 +119,53 @@ static inline void llu_invalidate_inode_pages(struct inode * inode)
         /* do nothing */
 }
 
-int llu_mdc_blocking_ast(struct ldlm_lock *lock,
-                         struct ldlm_lock_desc *desc,
-                         void *data, int flag)
+int llu_md_blocking_ast(struct ldlm_lock *lock,
+                        struct ldlm_lock_desc *desc,
+                        void *data, int flag)
 {
-        int rc;
         struct lustre_handle lockh;
+        int rc;
         ENTRY;
 
 
         switch (flag) {
         case LDLM_CB_BLOCKING:
                 ldlm_lock2handle(lock, &lockh);
-                rc = ldlm_cli_cancel(&lockh);
+               rc = ldlm_cli_cancel(&lockh, 0);
                 if (rc < 0) {
                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
                         RETURN(rc);
                 }
                 break;
         case LDLM_CB_CANCELING: {
-                struct inode *inode = llu_inode_from_lock(lock);
+               struct inode *inode = llu_inode_from_resource_lock(lock);
                 struct llu_inode_info *lli;
+                struct intnl_stat *st;
                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
+                struct lu_fid *fid;
+
+               /* Inode is set to lock->l_resource->lr_lvb_inode
+               * for mdc - bug 24555 */
+               LASSERT(lock->l_ast_data == NULL);
 
                 /* Invalidate all dentries associated with this inode */
                 if (inode == NULL)
                         break;
 
                 lli =  llu_i2info(inode);
+                st = llu_i2stat(inode);
 
                 if (bits & MDS_INODELOCK_UPDATE)
-                        clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
+                        lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
 
-                if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
-                    lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
-                        LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
-                                   lli->lli_st_ino, lli->lli_st_generation);
-                }
-                if (S_ISDIR(lli->lli_st_mode) &&
+               fid = &lli->lli_fid;
+               if (!fid_res_name_eq(fid, &lock->l_resource->lr_name))
+                       LDLM_ERROR(lock, "data mismatch with object "
+                                  DFID" (%p)", PFID(fid), inode);
+                if (S_ISDIR(st->st_mode) &&
                     (bits & MDS_INODELOCK_UPDATE)) {
-                        CDEBUG(D_INODE, "invalidating inode %lu\n",
-                               lli->lli_st_ino);
+                        CDEBUG(D_INODE, "invalidating inode %llu\n",
+                               (long long)st->st_ino);
 
                         llu_invalidate_inode_pages(inode);
                 }
@@ -180,7 +186,6 @@ int llu_mdc_blocking_ast(struct ldlm_lock *lock,
 }
 
 static int pnode_revalidate_finish(struct ptlrpc_request *req,
-                                   int offset,
                                    struct lookup_intent *it,
                                    struct pnode *pnode)
 {
@@ -197,29 +202,30 @@ static int pnode_revalidate_finish(struct ptlrpc_request *req,
         if (it_disposition(it, DISP_LOOKUP_NEG))
                 RETURN(-ENOENT);
 
-        rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
+        rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
+                              llu_i2sbi(inode)->ll_dt_exp, 
+                              llu_i2sbi(inode)->ll_md_exp, &md);
         if (rc)
                 RETURN(rc);
 
-        llu_update_inode(inode, md.body, md.lsm);
+        llu_update_inode(inode, &md);
 
         RETURN(rc);
 }
 
-int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
+static int llu_pb_revalidate(struct pnode *pnode, int flags,
+                             struct lookup_intent *it)
 {
         struct pnode_base *pb = pnode->p_base;
-        struct ll_fid pfid, cfid;
-        struct it_cb_data icbd;
-        struct ll_uctxt ctxt;
+        struct md_op_data op_data = {{ 0 }};
         struct ptlrpc_request *req = NULL;
         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
         struct obd_export *exp;
         int rc;
         ENTRY;
 
-        CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
-               pb->pb_name.name, it ? it->it_op : 0);
+        CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
+               (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
 
         /* We don't want to cache negative dentries, so return 0 immediately.
          * We believe that this is safe, that negative dentries cannot be
@@ -229,26 +235,16 @@ int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
                 RETURN(0);
         }
 
-        /* check stale inode */
-        if (llu_i2info(pb->pb_ino)->lli_stale_flag)
-                unhook_stale_inode(pnode);
-
-        /* check again because unhook_stale_inode() might generate
-         * negative pnode */
-        if (pb->pb_ino == NULL) {
-                CDEBUG(D_INODE, "negative pb\n");
-                RETURN(0);
-        }
-
         /* This is due to bad interaction with libsysio. remove this when we
          * switched to libbsdio XXX
          */
         {
                 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
+                struct intnl_stat *st = llu_i2stat(pb->pb_ino);
                 if (lli->lli_it) {
-                        CDEBUG(D_INODE, "inode %lu still have intent "
+                        CDEBUG(D_INODE, "inode %llu still have intent "
                                         "%p(opc 0x%x), release it\n",
-                                        lli->lli_st_ino, lli->lli_it,
+                                        (long long) st->st_ino, lli->lli_it,
                                         lli->lli_it->it_op);
                         ll_intent_release(lli->lli_it);
                         OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
@@ -256,24 +252,20 @@ int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
                 }
         }
 
-        exp = llu_i2mdcexp(pb->pb_ino);
-        ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
-        ll_inode2fid(&cfid, pb->pb_ino);
-        icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
-        icbd.icbd_child = pnode;
+        exp = llu_i2mdexp(pb->pb_ino);
 
         if (!it) {
                 it = &lookup_it;
                 it->it_op_release = ll_intent_release;
         }
 
-        ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
+        llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
+                            pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
+                            0, LUSTRE_OPC_ANY);
 
-        rc = mdc_intent_lock(exp, &ctxt, &pfid,
-                             pb->pb_name.name, pb->pb_name.len,
-                             NULL, 0, &cfid, it, flags, &req,
-                             llu_mdc_blocking_ast);
-        /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
+       rc = md_intent_lock(exp, &op_data, it, &req, &llu_md_blocking_ast,
+                           LDLM_FL_CANCEL_ON_BLOCK);
+        /* If req is NULL, then md_intent_lock only tried to do a lock match;
          * if all was well, it will return 1 if it found locks, 0 otherwise. */
         if (req == NULL && rc >= 0)
                 GOTO(out, rc);
@@ -281,7 +273,7 @@ int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
         if (rc < 0)
                 GOTO(out, rc = 0);
 
-        rc = pnode_revalidate_finish(req, 1, it, pnode);
+        rc = pnode_revalidate_finish(req, it, pnode);
         if (rc != 0) {
                 ll_intent_release(it);
                 GOTO(out, rc = 0);
@@ -302,65 +294,84 @@ int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
                 pb->pb_ino = NULL;
         } else {
                 llu_lookup_finish_locks(it, pnode);
-                llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
         }
         RETURN(rc);
 }
 
 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
-                            struct lookup_intent *it, void *data)
+                           struct lookup_intent *it,
+                           struct inode *parent, struct pnode *child)
 {
-        struct it_cb_data *icbd = data;
-        struct pnode *child = icbd->icbd_child;
-        struct inode *parent = icbd->icbd_parent;
         struct llu_sb_info *sbi = llu_i2sbi(parent);
         struct inode *inode = NULL;
         int rc;
 
+        /* libsysio require us generate inode right away if success.
+         * so if mds created new inode for us we need make sure it
+         * succeeded. thus for any error we can't delay to the
+         * llu_file_open() time. */
+        if (it_disposition(it, DISP_OPEN_CREATE) &&
+            it_open_error(DISP_OPEN_CREATE, it)) {
+                CDEBUG(D_INODE, "detect mds create error\n");
+                return it_open_error(DISP_OPEN_CREATE, it);
+        }
+        if (it_disposition(it, DISP_OPEN_OPEN) &&
+            it_open_error(DISP_OPEN_OPEN, it)) {
+                CDEBUG(D_INODE, "detect mds open error\n");
+                /* undo which did by md_intent_lock */
+                if (it_disposition(it, DISP_OPEN_CREATE) &&
+                    !it_open_error(DISP_OPEN_CREATE, it)) {
+                       LASSERT(request);
+                       LASSERT(atomic_read(&request->rq_refcount) > 1);
+                       CDEBUG(D_INODE, "dec a ref of req %p\n", request);
+                       ptlrpc_req_finished(request);
+                }
+                return it_open_error(DISP_OPEN_OPEN, it);
+        }
+
         /* NB 1 request reference will be taken away by ll_intent_lock()
          * when I return
-         * Note: libsysio require the inode must be generated here
          */
-        if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
+        if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
                 struct lustre_md md;
                 struct llu_inode_info *lli;
+                struct intnl_stat *st;
                 ENTRY;
 
-                rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
+                if (it_disposition(it, DISP_OPEN_CREATE))
+                        ptlrpc_req_finished(request);
+
+                rc = md_get_lustre_md(sbi->ll_md_exp, request,
+                                      sbi->ll_dt_exp, sbi->ll_md_exp, &md);
                 if (rc)
                         RETURN(rc);
 
                 inode = llu_iget(parent->i_fs, &md);
-                if (!inode) {
+                if (!inode || IS_ERR(inode)) {
                         /* free the lsm if we allocated one above */
                         if (md.lsm != NULL)
-                                obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
-                        RETURN(-ENOMEM);
-                } else if (md.lsm != NULL &&
-                           llu_i2info(inode)->lli_smd != md.lsm) {
-                        obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
+                                obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
+                        RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
+               } else if (md.lsm != NULL) {
+                        obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
                 }
 
                 lli = llu_i2info(inode);
+                st = llu_i2stat(inode);
 
                 /* If this is a stat, get the authoritative file size */
-                if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
-                    lli->lli_smd != NULL) {
-                        struct lov_stripe_md *lsm = lli->lli_smd;
-                        struct ost_lvb lvb;
+                if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
+                   lli->lli_has_smd) {
                         ldlm_error_t rc;
 
-                        LASSERT(lsm->lsm_object_id != 0);
-
                         /* bug 2334: drop MDS lock before acquiring OST lock */
                         ll_intent_drop_lock(it);
 
-                        rc = llu_glimpse_size(inode, &lvb);
+                        rc = cl_glimpse_size(inode);
                         if (rc) {
                                 I_RELE(inode);
                                 RETURN(rc);
                         }
-                        lli->lli_st_size = lvb.lvb_size;
                 }
         } else {
                 ENTRY;
@@ -375,29 +386,28 @@ static int lookup_it_finish(struct ptlrpc_request *request, int offset,
         RETURN(0);
 }
 
-struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
+struct inode *llu_inode_from_resource_lock(struct ldlm_lock *lock)
 {
-        struct inode *inode;
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
+       struct inode *inode;
+       lock_res_and_lock(lock);
 
-        if (lock->l_ast_data) {
-                inode = (struct inode *)lock->l_ast_data;
-                I_REF(inode);
-        } else
-                inode = NULL;
+       if (lock->l_resource->lr_lvb_inode) {
+               inode = (struct inode *)lock->l_resource->lr_lvb_inode;
+               I_REF(inode);
+       } else
+               inode = NULL;
 
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
-        return inode;
+       unlock_res_and_lock(lock);
+       return inode;
 }
 
 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
                          struct lookup_intent *it, int flags)
 {
-        struct ll_fid pfid;
-        struct ll_uctxt ctxt;
-        struct it_cb_data icbd;
+        struct md_op_data op_data = {{ 0 }};
         struct ptlrpc_request *req = NULL;
         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
+        __u32 opc;
         int rc;
         ENTRY;
 
@@ -409,21 +419,21 @@ static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
                 it->it_op_release = ll_intent_release;
         }
 
-        icbd.icbd_child = pnode;
-        icbd.icbd_parent = parent;
-        icbd.icbd_child = pnode;
-        ll_inode2fid(&pfid, parent);
-        ll_i2uctxt(&ctxt, parent, NULL);
-
-        rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
-                             pnode->p_base->pb_name.name,
-                             pnode->p_base->pb_name.len,
-                             NULL, 0, NULL, it, flags, &req,
-                             llu_mdc_blocking_ast);
+       if (it->it_op & IT_CREAT)
+               opc = LUSTRE_OPC_CREATE;
+       else
+               opc = LUSTRE_OPC_ANY;
+
+        llu_prep_md_op_data(&op_data, parent, NULL,
+                            pnode->p_base->pb_name.name,
+                            pnode->p_base->pb_name.len, flags, opc);
+
+       rc = md_intent_lock(llu_i2mdexp(parent), &op_data, it, &req,
+                           &llu_md_blocking_ast, LDLM_FL_CANCEL_ON_BLOCK);
         if (rc < 0)
                 GOTO(out, rc);
-        
-        rc = lookup_it_finish(req, 1, it, &icbd);
+
+       rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, parent, pnode);
         if (rc != 0) {
                 ll_intent_release(it);
                 GOTO(out, rc);
@@ -490,6 +500,12 @@ translate_lookup_intent(struct intent *intent, const char *path)
         if (it->it_flags & O_CREAT) {
                 it->it_op |= IT_CREAT;
                 it->it_create_mode = *((int*)intent->int_arg1);
+                /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
+                   this lookup to return negative result, but then there is no
+                   way to find out original intent in ll_iop_open(). So we just
+                   clear O_EXCL from libsysio flags here to avoid checking
+                   for negative result. O_EXCL will be enforced by MDS. */
+                *((int*)intent->int_arg2) &= ~O_EXCL;
         }
 
         if (intent->int_opmask & INT_GETATTR)
@@ -504,7 +520,7 @@ translate_lookup_intent(struct intent *intent, const char *path)
 
         /* conform to kernel code, if only IT_LOOKUP was set, don't
          * pass down it */
-        if (!it->it_op || it->it_op == IT_LOOKUP) {
+        if (!it->it_op || it->it_op & IT_LOOKUP) {
                 OBD_FREE(it, sizeof(*it));
                 it = NULL;
         }
@@ -524,6 +540,8 @@ int llu_iop_lookup(struct pnode *pnode,
         int rc;
         ENTRY;
 
+        liblustre_wait_event(0);
+
         *inop = NULL;
 
         /* the mount root inode have no name, so don't call
@@ -532,7 +550,7 @@ int llu_iop_lookup(struct pnode *pnode,
         if (pnode->p_mount->mnt_root == pnode) {
                 struct inode *i = pnode->p_base->pb_ino;
                 *inop = i;
-                return 0;
+                RETURN(0);
         }
 
         if (!pnode->p_base->pb_name.len)
@@ -544,7 +562,7 @@ int llu_iop_lookup(struct pnode *pnode,
         if (llu_pb_revalidate(pnode, 0, it)) {
                 LASSERT(pnode->p_base->pb_ino);
                 *inop = pnode->p_base->pb_ino;
-                RETURN(0);
+                GOTO(out, rc = 0);
         }
 
         rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
@@ -555,6 +573,9 @@ int llu_iop_lookup(struct pnode *pnode,
                         *inop = pnode->p_base->pb_ino;
         }
 
+out:
+        if (it)
+                OBD_FREE(it, sizeof(*it));
+        liblustre_wait_event(0);
         RETURN(rc);
 }
-