Whamcloud - gitweb
land b_smallfix 20040407_1414:
[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         if (dentry->d_inode == NULL)
73                 GOTO(out, rc = -ENOENT);
74
75         lvb->lvb_size = dentry->d_inode->i_size;
76         lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
77         lvb->lvb_blocks = dentry->d_inode->i_blocks;
78         f_dput(dentry);
79
80         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", "
81                "mtime: "LPU64", blocks: "LPU64"\n",
82                res->lr_name.name[0], lvb->lvb_size,
83                lvb->lvb_mtime, lvb->lvb_blocks);
84
85  out:
86         /* Don't free lvb data on lookup error */
87         up(&res->lr_lvb_sem);
88         return rc;
89 }
90
91 /* This will be called in two ways:
92  *
93  *   m != NULL : called by the DLM itself after a glimpse callback
94  *   m == NULL : called by the filter after a disk write
95  *
96  *   If 'increase' is true, don't allow values to move backwards.
97  */
98 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
99                               int buf_idx, int increase)
100 {
101         int rc = 0;
102         struct ost_lvb *lvb = res->lr_lvb_data;
103         struct obd_device *obd;
104         struct dentry *dentry;
105         ENTRY;
106
107         LASSERT(res);
108
109         /* we only want lvb's for object resources */
110         /* check for internal locks: these have name[1] != 0 */
111         if (res->lr_name.name[1])
112                 RETURN(0);
113
114         down(&res->lr_lvb_sem);
115         if (!res->lr_lvb_data) {
116                 CERROR("No lvb when running lvbo_update!\n");
117                 GOTO(out, rc = 0);
118         }
119
120         /* Update the LVB from the network message */
121         if (m != NULL) {
122                 struct ost_lvb *new;
123
124                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
125                                       lustre_swab_ost_lvb);
126                 if (new == NULL) {
127                         CERROR("lustre_swab_buf failed\n");
128                         //GOTO(out, rc = -EPROTO);
129                         GOTO(out, rc = 0);
130                 }
131                 if (new->lvb_size > lvb->lvb_size || !increase) {
132                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
133                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
134                                lvb->lvb_size, new->lvb_size);
135                         lvb->lvb_size = new->lvb_size;
136                 }
137                 if (new->lvb_mtime > lvb->lvb_mtime || !increase) {
138                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
139                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
140                                lvb->lvb_mtime, new->lvb_mtime);
141                         lvb->lvb_mtime = new->lvb_mtime;
142                 }
143                 if (new->lvb_blocks > lvb->lvb_blocks || !increase) {
144                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks: "
145                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
146                                lvb->lvb_blocks, new->lvb_blocks);
147                         lvb->lvb_blocks = new->lvb_blocks;
148                 }
149                 GOTO(out, rc = 0);
150         }
151
152         /* Update the LVB from the disk inode */
153         obd = res->lr_namespace->ns_lvbp;
154         LASSERT(obd);
155
156         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
157         if (IS_ERR(dentry))
158                 GOTO(out, rc = PTR_ERR(dentry));
159
160         if (dentry->d_inode->i_size > lvb->lvb_size || !increase) {
161                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
162                        LPU64" -> %llu\n", res->lr_name.name[0],
163                        lvb->lvb_size, dentry->d_inode->i_size);
164                 lvb->lvb_size = dentry->d_inode->i_size;
165         }
166
167         if (LTIME_S(dentry->d_inode->i_mtime) > lvb->lvb_mtime || !increase) {
168                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
169                        LPU64" -> %lu\n", res->lr_name.name[0],
170                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
171                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
172         }
173         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks from disk: "
174                LPU64" -> %lu\n", res->lr_name.name[0],
175                lvb->lvb_blocks, dentry->d_inode->i_blocks);
176         lvb->lvb_blocks = dentry->d_inode->i_blocks;
177
178         f_dput(dentry);
179
180  out:
181         up(&res->lr_lvb_sem);
182         return rc;
183 }
184
185
186
187 struct ldlm_valblock_ops filter_lvbo = {
188         lvbo_init: filter_lvbo_init,
189         lvbo_update: filter_lvbo_update
190 };