Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / liblustre / rw.c
index fbdca31..94783c4 100644 (file)
@@ -1,24 +1,41 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Lustre Light block IO
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (c) 2002-2004 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 [sun.com URL with a
+ * copy of GPLv2].
  *
- *   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.
  *
- *   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.
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/liblustre/rw.c
+ *
+ * Lustre Light block IO
  */
 
 #define DEBUG_SUBSYSTEM S_LLITE
 #include <fcntl.h>
 #include <sys/uio.h>
 
+#include <sysio.h>
 #ifdef HAVE_XTIO_H
 #include <xtio.h>
 #endif
-#include <sysio.h>
 #include <fs.h>
 #include <mount.h>
 #include <inode.h>
@@ -94,7 +111,7 @@ static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock
                 char name[16];
                 struct ldlm_lock *lock;
                 struct lov_stripe_md *lsm;
-        } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
+        } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock, .lsm = lsm };
         __u32 stripe, vallen = sizeof(stripe);
         int rc;
         ENTRY;
@@ -112,9 +129,9 @@ static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock
         RETURN(stripe);
 }
 
-static int llu_extent_lock_callback(struct ldlm_lock *lock,
-                                    struct ldlm_lock_desc *new, void *data,
-                                    int flag)
+int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
+                              struct ldlm_lock_desc *new, void *data,
+                              int flag)
 {
         struct lustre_handle lockh = { 0 };
         int rc;
@@ -156,12 +173,12 @@ static int llu_extent_lock_callback(struct ldlm_lock *lock,
                 stripe = llu_lock_to_stripe_offset(inode, lock);
                 lock_res_and_lock(lock);
                 kms = ldlm_extent_shift_kms(lock,
-                                            lsm->lsm_oinfo[stripe].loi_kms);
+                                            lsm->lsm_oinfo[stripe]->loi_kms);
                 unlock_res_and_lock(lock);
-                if (lsm->lsm_oinfo[stripe].loi_kms != kms)
+                if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
-                                   lsm->lsm_oinfo[stripe].loi_kms, kms);
-                lsm->lsm_oinfo[stripe].loi_kms = kms;
+                                   lsm->lsm_oinfo[stripe]->loi_kms, kms);
+                lsm->lsm_oinfo[stripe]->loi_kms = kms;
 iput:
                 I_RELE(inode);
                 break;
@@ -179,7 +196,6 @@ static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
         struct inode *inode = llu_inode_from_lock(lock);
         struct llu_inode_info *lli;
         struct ost_lvb *lvb;
-        int size[2] = { sizeof(struct ptlrpc_body), sizeof(*lvb) };
         int rc, stripe = 0;
         ENTRY;
 
@@ -195,17 +211,20 @@ static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
         if (lli->lli_smd->lsm_stripe_count > 1)
                 stripe = llu_lock_to_stripe_offset(inode, lock);
 
-        rc = lustre_pack_reply(req, 2, size, NULL);
+        req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
+        req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
+                             sizeof(*lvb));
+        rc = req_capsule_server_pack(&req->rq_pill);
         if (rc) {
-                CERROR("lustre_pack_reply: %d\n", rc);
+                CERROR("failed pack reply: %d\n", rc);
                 GOTO(iput, rc);
         }
 
-        lvb = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*lvb));
-        lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
+        lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
+        lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
 
-        LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
-                   (long long)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
+        LDLM_DEBUG(lock, "i_size: "LPU64" -> stripe number %u -> kms "LPU64,
+                   (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
  iput:
         I_RELE(inode);
  out:
@@ -218,6 +237,54 @@ static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
         return rc;
 }
 
+static int llu_merge_lvb(struct inode *inode)
+{
+        struct llu_inode_info *lli = llu_i2info(inode);
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        struct intnl_stat *st = llu_i2stat(inode);
+        struct ost_lvb lvb;
+        int rc;
+        ENTRY;
+
+        inode_init_lvb(inode, &lvb);
+        rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
+        st->st_size = lvb.lvb_size;
+        st->st_blocks = lvb.lvb_blocks;
+        /* handle st_blocks overflow gracefully */
+        if (st->st_blocks < lvb.lvb_blocks)
+                st->st_blocks = ~0UL;
+        st->st_mtime = lvb.lvb_mtime;
+        st->st_atime = lvb.lvb_atime;
+        st->st_ctime = lvb.lvb_ctime;
+
+        RETURN(rc);
+}
+
+int llu_local_size(struct inode *inode)
+{
+        ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
+        struct llu_inode_info *lli = llu_i2info(inode);
+        struct llu_sb_info *sbi = llu_i2sbi(inode);
+        struct lustre_handle lockh = { 0 };
+        int flags = 0;
+        int rc;
+        ENTRY;
+
+        if (lli->lli_smd->lsm_stripe_count == 0)
+                RETURN(0);
+        
+        rc = obd_match(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT,
+                       &policy, LCK_PR, &flags, inode, &lockh);
+        if (rc < 0)
+                RETURN(rc);
+        else if (rc == 0)
+                RETURN(-ENODATA);
+        
+        rc = llu_merge_lvb(inode);
+        obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
+        RETURN(rc);
+}
+
 /* NB: lov_merge_size will prefer locally cached writes if they extend the
  * file (because it prefers KMS over RSS when larger) */
 int llu_glimpse_size(struct inode *inode)
@@ -226,24 +293,26 @@ int llu_glimpse_size(struct inode *inode)
         struct intnl_stat *st = llu_i2stat(inode);
         struct llu_sb_info *sbi = llu_i2sbi(inode);
         struct lustre_handle lockh = { 0 };
-        struct obd_enqueue_info einfo = { 0 };
+        struct ldlm_enqueue_info einfo = { 0 };
         struct obd_info oinfo = { { { 0 } } };
-        struct ost_lvb lvb;
         int rc;
         ENTRY;
 
-        CDEBUG(D_DLMTRACE, "Glimpsing inode %llu\n", (long long)st->st_ino);
+        /* If size is cached on the mds, skip glimpse. */
+        if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
+                RETURN(0);
+
+        CDEBUG(D_DLMTRACE, "Glimpsing inode "LPU64"\n", (__u64)st->st_ino);
 
         if (!lli->lli_smd) {
-                CDEBUG(D_DLMTRACE, "No objects for inode %llu\n", 
-                       (long long)st->st_ino);
+                CDEBUG(D_DLMTRACE, "No objects for inode "LPU64"\n", 
+                       (__u64)st->st_ino);
                 RETURN(0);
         }
 
         einfo.ei_type = LDLM_EXTENT;
         einfo.ei_mode = LCK_PR;
-        einfo.ei_flags = LDLM_FL_HAS_INTENT;
-        einfo.ei_cb_bl = llu_extent_lock_callback;
+        einfo.ei_cb_bl = osc_extent_blocking_cb;
         einfo.ei_cb_cp = ldlm_completion_ast;
         einfo.ei_cb_gl = llu_glimpse_callback;
         einfo.ei_cbdata = inode;
@@ -251,23 +320,17 @@ int llu_glimpse_size(struct inode *inode)
         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
         oinfo.oi_lockh = &lockh;
         oinfo.oi_md = lli->lli_smd;
+        oinfo.oi_flags = LDLM_FL_HAS_INTENT;
 
-        rc = obd_enqueue_rqset(sbi->ll_osc_exp, &oinfo, &einfo);
+        rc = obd_enqueue_rqset(sbi->ll_dt_exp, &oinfo, &einfo);
         if (rc) {
                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
                 RETURN(rc > 0 ? -EIO : rc);
         }
 
-        inode_init_lvb(inode, &lvb);
-        obd_merge_lvb(sbi->ll_osc_exp, lli->lli_smd, &lvb, 0);
-        st->st_size = lvb.lvb_size;
-        st->st_blocks = lvb.lvb_blocks;
-        st->st_mtime = lvb.lvb_mtime;
-        st->st_atime = lvb.lvb_atime;
-        st->st_ctime = lvb.lvb_ctime;
-
-        CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %llu\n",
-               (long long)st->st_size, (long long)st->st_blocks);
+        rc = llu_merge_lvb(inode);
+        CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
+               (__u64)st->st_size, (__u64)st->st_blocks);
 
         RETURN(rc);
 }
@@ -279,7 +342,7 @@ int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
 {
         struct llu_sb_info *sbi = llu_i2sbi(inode);
         struct intnl_stat *st = llu_i2stat(inode);
-        struct obd_enqueue_info einfo = { 0 };
+        struct ldlm_enqueue_info einfo = { 0 };
         struct obd_info oinfo = { { { 0 } } };
         struct ost_lvb lvb;
         int rc;
@@ -294,13 +357,12 @@ int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
                 RETURN(0);
 
         CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
-               (long long)st->st_ino, policy->l_extent.start,
+               (unsigned long long)st->st_ino, policy->l_extent.start,
                policy->l_extent.end);
 
         einfo.ei_type = LDLM_EXTENT;
         einfo.ei_mode = mode;
-        einfo.ei_flags = ast_flags;
-        einfo.ei_cb_bl = llu_extent_lock_callback;
+        einfo.ei_cb_bl = osc_extent_blocking_cb;
         einfo.ei_cb_cp = ldlm_completion_ast;
         einfo.ei_cb_gl = llu_glimpse_callback;
         einfo.ei_cbdata = inode;
@@ -308,14 +370,15 @@ int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
         oinfo.oi_policy = *policy;
         oinfo.oi_lockh = lockh;
         oinfo.oi_md = lsm;
+        oinfo.oi_flags = ast_flags;
 
-        rc = obd_enqueue(sbi->ll_osc_exp, &oinfo, &einfo);
+        rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
         *policy = oinfo.oi_policy;
         if (rc > 0)
                 rc = -EIO;
 
         inode_init_lvb(inode, &lvb);
-        obd_merge_lvb(sbi->ll_osc_exp, lsm, &lvb, 1);
+        obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
         if (policy->l_extent.start == 0 &&
             policy->l_extent.end == OBD_OBJECT_EOF)
                 st->st_size = lvb.lvb_size;
@@ -344,7 +407,7 @@ int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
                 RETURN(0);
 
-        rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
+        rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
 
         RETURN(rc);
 }
@@ -414,12 +477,18 @@ static int llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
         RETURN(0);
 }
 
+static struct obd_capa * llu_ap_lookup_capa(void *data, int cmd)
+{
+        return NULL;
+}
+
 static struct obd_async_page_ops llu_async_page_ops = {
         .ap_make_ready =        NULL,
         .ap_refresh_count =     NULL,
         .ap_fill_obdo =         llu_ap_fill_obdo,
         .ap_update_obdo =       llu_ap_update_obdo,
         .ap_completion =        llu_ap_completion,
+        .ap_lookup_capa =       llu_ap_lookup_capa,
 };
 
 static int llu_queue_pio(int cmd, struct llu_io_group *group,
@@ -485,12 +554,15 @@ static int llu_queue_pio(int cmd, struct llu_io_group *group,
                 rc = obd_prep_async_page(exp, lsm, NULL, page,
                                          (obd_off)page->index << CFS_PAGE_SHIFT,
                                          &llu_async_page_ops,
-                                         llap, &llap->llap_cookie);
+                                         llap, &llap->llap_cookie,
+                                         1 /* no cache in liblustre at all */,
+                                         NULL);
                 if (rc) {
                         LASSERT(rc < 0);
                         llap->llap_cookie = NULL;
                         RETURN(rc);
                 }
+
                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
                        llap, page, llap->llap_cookie,
                        (obd_off)pages->index << CFS_PAGE_SHIFT);
@@ -556,7 +628,8 @@ struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
         if (!llap_cookie_size)
                 llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
                                                        NULL, NULL, NULL, 0,
-                                                       NULL, NULL, NULL);
+                                                       NULL, NULL, NULL, 0,
+                                                       NULL);
 
         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
         if (!group)
@@ -581,7 +654,7 @@ struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
 
 static int max_io_pages(ssize_t len, int iovlen)
 {
-        return (((len + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT) + 2 + iovlen - 1);
+        return (((len + CFS_PAGE_SIZE -1) / CFS_PAGE_SIZE) + 2 + iovlen - 1);
 }
 
 static
@@ -735,7 +808,7 @@ ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
 
         err = oig_wait(iogroup->lig_oig);
         if (err) {
-                CERROR("sync error %d, data corruption possible\n", err);
+                CERROR("%s error: %s\n", is_read ? "read" : "write", strerror(-err));
                 GOTO(err_unlock, err);
         }