Whamcloud - gitweb
Don't free resource lvb if there is a lookup error (causes later assertion).
[fs/lustre-release.git] / lustre / obdfilter / filter_lvb.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/obdfilter/filter_log.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_FILTER
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/version.h>
32
33 #include <portals/list.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_dlm.h>
36
37 #include "filter_internal.h"
38
39 static int filter_lvbo_init(struct ldlm_resource *res)
40 {
41         int rc = 0;
42         struct ost_lvb *lvb = NULL;
43         struct obd_device *obd;
44         struct dentry *dentry;
45         ENTRY;
46
47         LASSERT(res);
48
49         /* we only want lvb's for object resources */
50         /* check for internal locks: these have name[1] != 0 */
51         if (res->lr_name.name[1])
52                 RETURN(0);
53
54         down(&res->lr_lvb_sem);
55         if (res->lr_lvb_data)
56                 GOTO(out, rc = 0);
57
58         OBD_ALLOC(lvb, sizeof(*lvb));
59         if (lvb == NULL)
60                 GOTO(out, rc = -ENOMEM);
61
62         res->lr_lvb_data = lvb;
63         res->lr_lvb_len = sizeof(*lvb);
64
65         obd = res->lr_namespace->ns_lvbp;
66         LASSERT(obd != NULL);
67
68         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
69         if (IS_ERR(dentry))
70                 GOTO(out, rc = PTR_ERR(dentry));
71
72         lvb->lvb_size = dentry->d_inode->i_size;
73         lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
74         f_dput(dentry);
75
76         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", mtime: "
77                LPU64"\n", res->lr_name.name[0], lvb->lvb_size, lvb->lvb_mtime);
78
79  out:
80         /* Don't free lvb data on lookup error */
81         up(&res->lr_lvb_sem);
82         return rc;
83 }
84
85 /* This will be called in two ways:
86  *
87  *   m != NULL : called by the DLM itself after a glimpse callback
88  *   m == NULL : called by the filter after a disk write
89  *
90  *   If 'increase' is true, don't allow values to move backwards.
91  */
92 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
93                               int buf_idx, int increase)
94 {
95         int rc = 0;
96         struct ost_lvb *lvb = res->lr_lvb_data;
97         struct obd_device *obd;
98         struct dentry *dentry;
99         ENTRY;
100
101         LASSERT(res);
102
103         /* we only want lvb's for object resources */
104         /* check for internal locks: these have name[1] != 0 */
105         if (res->lr_name.name[1])
106                 RETURN(0);
107
108         down(&res->lr_lvb_sem);
109         if (!res->lr_lvb_data) {
110                 CERROR("No lvb when running lvbo_update!\n");
111                 GOTO(out, rc = 0);
112         }
113
114         /* Update the LVB from the network message */
115         if (m != NULL) {
116                 struct ost_lvb *new;
117
118                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
119                                       lustre_swab_ost_lvb);
120                 if (new == NULL) {
121                         CERROR("lustre_swab_buf failed\n");
122                         //GOTO(out, rc = -EPROTO);
123                         GOTO(out, rc = 0);
124                 }
125                 if (new->lvb_size > lvb->lvb_size || !increase) {
126                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
127                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
128                                lvb->lvb_size, new->lvb_size);
129                         lvb->lvb_size = new->lvb_size;
130                 }
131                 if (new->lvb_mtime > lvb->lvb_mtime || !increase) {
132                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
133                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
134                                lvb->lvb_mtime, new->lvb_mtime);
135                         lvb->lvb_mtime = new->lvb_mtime;
136                 }
137                 GOTO(out, rc = 0);
138         }
139
140         /* Update the LVB from the disk inode */
141         obd = res->lr_namespace->ns_lvbp;
142         LASSERT(obd);
143
144         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
145         if (IS_ERR(dentry))
146                 GOTO(out, rc = PTR_ERR(dentry));
147
148         if (LTIME_S(dentry->d_inode->i_size) > lvb->lvb_size || !increase) {
149                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
150                        LPU64" -> %llu\n", res->lr_name.name[0],
151                        lvb->lvb_size, dentry->d_inode->i_size);
152                 lvb->lvb_size = dentry->d_inode->i_size;
153         }
154
155         if (LTIME_S(dentry->d_inode->i_mtime) > lvb->lvb_mtime || !increase) {
156                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
157                        LPU64" -> %lu\n", res->lr_name.name[0],
158                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
159                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
160         }
161         f_dput(dentry);
162
163  out:
164         up(&res->lr_lvb_sem);
165         return rc;
166 }
167
168
169
170 struct ldlm_valblock_ops filter_lvbo = {
171         lvbo_init: filter_lvbo_init,
172         lvbo_update: filter_lvbo_update
173 };