Whamcloud - gitweb
LU-812 kernel: AUTOCONF_INCLUDED removed
[fs/lustre-release.git] / lustre / obdfilter / filter_lvb.c
index acfba4c..a548983 100644 (file)
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *  linux/fs/obdfilter/filter_log.c
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
- *   Author: Peter Braam <braam@clusterfs.com>
- *   Author: Andreas Dilger <adilger@clusterfs.com>
- *   Author: Phil Schwan <phil@clusterfs.com>
+ * 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.
  *
- *   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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, Whamcloud, Inc.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/obdfilter/filter_lvb.c
+ *
+ * Author: Peter Braam <braam@clusterfs.com>
+ * Author: Andreas Dilger <adilger@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
  */
 
 #define DEBUG_SUBSYSTEM S_FILTER
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/version.h>
 
-#include <portals/list.h>
-#include <linux/obd_class.h>
-#include <linux/lustre_dlm.h>
+#include <libcfs/list.h>
+#include <obd_class.h>
+#include <lustre_dlm.h>
 
 #include "filter_internal.h"
 
+static int filter_lvbo_free(struct ldlm_resource *res) {
+        if (res->lr_lvb_inode) {
+                iput(res->lr_lvb_inode);
+                res->lr_lvb_inode = NULL;
+        }
+
+        if (res->lr_lvb_data)
+                OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
+
+        return 0;
+}
+
+/* Called with res->lr_lvb_mutex held */
 static int filter_lvbo_init(struct ldlm_resource *res)
 {
-        int rc = 0;
         struct ost_lvb *lvb = NULL;
         struct obd_device *obd;
-        struct obdo *oa = NULL;
         struct dentry *dentry;
+        int rc = 0;
         ENTRY;
 
         LASSERT(res);
+        LASSERT_MUTEX_LOCKED(&res->lr_lvb_mutex);
 
-        /* we only want lvb's for object resources */
-        /* check for internal locks: these have name[1] != 0 */
-        if (res->lr_name.name[1])
-                RETURN(0);
-
-        down(&res->lr_lvb_sem);
         if (res->lr_lvb_data)
-                GOTO(out, rc = 0);
+                RETURN(0);
 
         OBD_ALLOC(lvb, sizeof(*lvb));
         if (lvb == NULL)
-                GOTO(out, rc = -ENOMEM);
+                RETURN(-ENOMEM);
 
         res->lr_lvb_data = lvb;
         res->lr_lvb_len = sizeof(*lvb);
 
-        obd = res->lr_namespace->ns_lvbp;
+        obd = ldlm_res_to_ns(res)->ns_lvbp;
         LASSERT(obd != NULL);
 
-        oa = obdo_alloc();
-        if (oa == NULL)
-                GOTO(out, rc = -ENOMEM);
+        CDEBUG(D_INODE, "%s: filter_lvbo_init(o_seq="LPU64", o_id="
+               LPU64")\n", obd->obd_name, res->lr_name.name[1],
+               res->lr_name.name[0]);
 
-        oa->o_id = res->lr_name.name[0];
-        oa->o_gr = 0;
-        dentry = filter_oa2dentry(obd, oa);
-        if (IS_ERR(dentry))
-                GOTO(out, rc = PTR_ERR(dentry));
+        dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
+                                              res->lr_name.name[0]);
+        if (IS_ERR(dentry)) {
+                rc = PTR_ERR(dentry);
+                CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
+                       res->lr_name.name[0], res->lr_name.name[1], rc);
+                RETURN(rc);
+        }
 
-        /* Limit the valid bits in the return data to what we actually use */
-        oa->o_valid = OBD_MD_FLID;
-        obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
+        if (dentry->d_inode == NULL)
+                /* This is always true for test_brw */
+                GOTO(out_dentry, rc = -ENOENT);
+
+        inode_init_lvb(dentry->d_inode, lvb);
+
+        CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
+               "mtime: "LPX64", blocks: "LPX64"\n",
+               res->lr_name.name[0], lvb->lvb_size,
+               lvb->lvb_mtime, lvb->lvb_blocks);
+
+        res->lr_lvb_inode = igrab(dentry->d_inode);
+
+        EXIT;
+out_dentry:
         f_dput(dentry);
 
-        lvb->lvb_size = dentry->d_inode->i_size;
-        lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
-        CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", mtime: "
-               LPU64"\n", res->lr_name.name[0], lvb->lvb_size, lvb->lvb_mtime);
-
- out:
-        if (oa)
-                obdo_free(oa);
-        if (rc && lvb != NULL) {
-                OBD_FREE(lvb, sizeof(*lvb));
-                res->lr_lvb_data = NULL;
-                res->lr_lvb_len = 0;
-        }
-        up(&res->lr_lvb_sem);
+        if (rc)
+                OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
+        /* Don't free lvb data on lookup error */
         return rc;
 }
 
 /* This will be called in two ways:
  *
- *   m != NULL : called by the DLM itself after a glimpse callback
- *   m == NULL : called by the filter after a disk write
+ *   r != NULL : called by the DLM itself after a glimpse callback
+ *   r == NULL : called by the filter after a disk write
  *
- *   If 'increase' is true, don't allow values to move backwards.
+ *   If 'increase_only' is true, don't allow values to move backwards.
  */
-static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
-                              int buf_idx, int increase)
+static int filter_lvbo_update(struct ldlm_resource *res,
+                              struct ptlrpc_request *r, int increase_only)
 {
         int rc = 0;
-        struct ost_lvb *lvb = res->lr_lvb_data;
+        struct ost_lvb *lvb;
         struct obd_device *obd;
-        struct obdo *oa = NULL;
-        struct dentry *dentry;
+        struct inode *inode;
+        struct inode *tmpinode = NULL;
         ENTRY;
 
         LASSERT(res);
 
-        /* we only want lvb's for object resources */
-        /* check for internal locks: these have name[1] != 0 */
-        if (res->lr_name.name[1])
-                RETURN(0);
-
-        down(&res->lr_lvb_sem);
-        if (!res->lr_lvb_data) {
+        lock_res(res);
+        lvb = res->lr_lvb_data;
+        if (lvb == NULL) {
                 CERROR("No lvb when running lvbo_update!\n");
                 GOTO(out, rc = 0);
         }
 
         /* Update the LVB from the network message */
-        if (m != NULL) {
+        if (r != NULL) {
                 struct ost_lvb *new;
 
-                new = lustre_swab_buf(m, buf_idx, sizeof(*new),
-                                      lustre_swab_ost_lvb);
+                /* XXX update always from reply buffer */
+                new = req_capsule_server_get(&r->rq_pill, &RMF_DLM_LVB);
+
                 if (new == NULL) {
                         CERROR("lustre_swab_buf failed\n");
-                        //GOTO(out, rc = -EPROTO);
-                        GOTO(out, rc = 0);
+                        goto disk_update;
                 }
-                if (new->lvb_size > lvb->lvb_size || !increase) {
+                if (new->lvb_size > lvb->lvb_size || !increase_only) {
                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
                                lvb->lvb_size, new->lvb_size);
                         lvb->lvb_size = new->lvb_size;
                 }
-                if (new->lvb_mtime > lvb->lvb_mtime || !increase) {
+                if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
                                lvb->lvb_mtime, new->lvb_mtime);
                         lvb->lvb_mtime = new->lvb_mtime;
                 }
-                GOTO(out, rc = 0);
+                if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
+                        CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
+                               LPU64" -> "LPU64"\n", res->lr_name.name[0],
+                               lvb->lvb_atime, new->lvb_atime);
+                        lvb->lvb_atime = new->lvb_atime;
+                }
+                if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
+                        CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
+                               LPU64" -> "LPU64"\n", res->lr_name.name[0],
+                               lvb->lvb_ctime, new->lvb_ctime);
+                        lvb->lvb_ctime = new->lvb_ctime;
+                }
+                if (new->lvb_blocks > lvb->lvb_blocks || !increase_only) {
+                        CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks: "
+                               LPU64" -> "LPU64"\n", res->lr_name.name[0],
+                               lvb->lvb_blocks, new->lvb_blocks);
+                        lvb->lvb_blocks = new->lvb_blocks;
+                }
         }
 
+ disk_update:
         /* Update the LVB from the disk inode */
-        obd = res->lr_namespace->ns_lvbp;
+        obd = ldlm_res_to_ns(res)->ns_lvbp;
         LASSERT(obd);
 
-        oa = obdo_alloc();
-        if (oa == NULL)
-                GOTO(out, rc = -ENOMEM);
+        inode = res->lr_lvb_inode;
+        /* filter_fid2dentry could fail, esp. in OBD_FAIL_OST_ENOENT test case */
+        if (unlikely(inode == NULL)) {
+                struct dentry *dentry;
 
-        oa->o_id = res->lr_name.name[0];
-        oa->o_gr = 0;
-        dentry = filter_oa2dentry(obd, oa);
-        if (IS_ERR(dentry))
-                GOTO(out, rc = PTR_ERR(dentry));
+                unlock_res(res);
 
-        /* Limit the valid bits in the return data to what we actually use */
-        oa->o_valid = OBD_MD_FLID;
-        obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
+                dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
+                                           res->lr_name.name[0]);
+                if (IS_ERR(dentry))
+                        RETURN(PTR_ERR(dentry));
 
-        if (dentry->d_inode->i_size > lvb->lvb_size || !increase) {
+                if (dentry->d_inode)
+                        tmpinode = igrab(dentry->d_inode);
+                f_dput(dentry);
+                /* tmpinode could be NULL, but it does not matter if other
+                 * have set res->lr_lvb_inode */
+                lock_res(res);
+                if (res->lr_lvb_inode == NULL) {
+                        res->lr_lvb_inode = tmpinode;
+                        tmpinode = NULL;
+                }
+                inode = res->lr_lvb_inode;
+        }
+
+        if (!inode || !inode->i_nlink)
+                GOTO(out, rc = -ENOENT);
+
+        if (i_size_read(inode) > lvb->lvb_size || !increase_only) {
                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
-                       LPU64" -> "LPU64"\n", res->lr_name.name[0],
-                       lvb->lvb_size, dentry->d_inode->i_size);
-                lvb->lvb_size = dentry->d_inode->i_size;
+                       LPU64" -> %llu\n", res->lr_name.name[0],
+                       lvb->lvb_size, i_size_read(inode));
+                lvb->lvb_size = i_size_read(inode);
         }
-        if (dentry->d_inode->i_mtime > lvb->lvb_mtime || !increase) {
+
+        if (LTIME_S(inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
-                       LPU64" -> "LPU64"\n", res->lr_name.name[0],
-                       lvb->lvb_mtime,(__u64)LTIME_S(dentry->d_inode->i_mtime));
-                lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
+                       LPU64" -> %lu\n", res->lr_name.name[0],
+                       lvb->lvb_mtime, LTIME_S(inode->i_mtime));
+                lvb->lvb_mtime = LTIME_S(inode->i_mtime);
+        }
+        if (LTIME_S(inode->i_atime) >lvb->lvb_atime|| !increase_only){
+                CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
+                       LPU64" -> %lu\n", res->lr_name.name[0],
+                       lvb->lvb_atime, LTIME_S(inode->i_atime));
+                lvb->lvb_atime = LTIME_S(inode->i_atime);
+        }
+        if (LTIME_S(inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
+                CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
+                       LPU64" -> %lu\n", res->lr_name.name[0],
+                       lvb->lvb_ctime, LTIME_S(inode->i_ctime));
+                lvb->lvb_ctime = LTIME_S(inode->i_ctime);
+        }
+        if (inode->i_blocks > lvb->lvb_blocks || !increase_only) {
+                CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
+                       LPU64" -> %llu\n", res->lr_name.name[0],
+                       lvb->lvb_blocks, (unsigned long long)inode->i_blocks);
+                lvb->lvb_blocks = inode->i_blocks;
         }
-        f_dput(dentry);
 
- out:
-        if (oa != NULL)
-                obdo_free(oa);
-        up(&res->lr_lvb_sem);
+out:
+        unlock_res(res);
+        if (tmpinode)
+                iput(tmpinode);
         return rc;
 }
 
-
-
 struct ldlm_valblock_ops filter_lvbo = {
         lvbo_init: filter_lvbo_init,
-        lvbo_update: filter_lvbo_update
+        lvbo_update: filter_lvbo_update,
+        lvbo_free: filter_lvbo_free
 };