Whamcloud - gitweb
254a3fba14418a2c202ea301657e29eb8ef9a780
[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 obdo *oa = NULL;
45         struct dentry *dentry;
46         ENTRY;
47
48         LASSERT(res);
49
50         /* we only want lvb's for object resources */
51         /* check for internal locks: these have name[1] != 0 */
52         if (res->lr_name.name[1])
53                 RETURN(0);
54
55         down(&res->lr_lvb_sem);
56         if (res->lr_lvb_data)
57                 GOTO(out, rc = 0);
58
59         OBD_ALLOC(lvb, sizeof(*lvb));
60         if (lvb == NULL)
61                 GOTO(out, rc = -ENOMEM);
62
63         res->lr_lvb_data = lvb;
64         res->lr_lvb_len = sizeof(*lvb);
65
66         obd = res->lr_namespace->ns_lvbp;
67         LASSERT(obd != NULL);
68
69         oa = obdo_alloc();
70         if (oa == NULL)
71                 GOTO(out, rc = -ENOMEM);
72
73         oa->o_id = res->lr_name.name[0];
74         oa->o_gr = 0;
75         dentry = filter_oa2dentry(obd, oa);
76         if (IS_ERR(dentry))
77                 GOTO(out, rc = PTR_ERR(dentry));
78
79         /* Limit the valid bits in the return data to what we actually use */
80         oa->o_valid = OBD_MD_FLID;
81         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
82         f_dput(dentry);
83
84         lvb->lvb_size = dentry->d_inode->i_size;
85         lvb->lvb_time = LTIME_S(dentry->d_inode->i_mtime);
86
87  out:
88         if (oa)
89                 obdo_free(oa);
90         if (rc && lvb != NULL) {
91                 OBD_FREE(lvb, sizeof(*lvb));
92                 res->lr_lvb_data = NULL;
93                 res->lr_lvb_len = 0;
94         }
95         up(&res->lr_lvb_sem);
96         return rc;
97 }
98
99 /* This will be called in two ways:
100  *
101  *   m != NULL : called by the DLM itself after a glimpse callback
102  *   m == NULL : called by the filter after a disk write
103  */
104 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
105                               int buf_idx)
106 {
107         int rc = 0;
108         struct ost_lvb *lvb = res->lr_lvb_data;
109         struct obd_device *obd;
110         struct obdo *oa = NULL;
111         struct dentry *dentry;
112         ENTRY;
113
114         LASSERT(res);
115
116         /* we only want lvb's for object resources */
117         /* check for internal locks: these have name[1] != 0 */
118         if (res->lr_name.name[1])
119                 RETURN(0);
120
121         down(&res->lr_lvb_sem);
122         if (!res->lr_lvb_data) {
123                 CERROR("No lvb when running lvbo_update!\n");
124                 GOTO(out, rc = 0);
125         }
126
127         /* Update the LVB from the network message */
128         if (m != NULL) {
129                 struct ost_lvb *new;
130
131                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
132                                       lustre_swab_ost_lvb);
133                 if (new == NULL) {
134                         CERROR("lustre_swab_buf failed\n");
135                         //GOTO(out, rc = -EPROTO);
136                         GOTO(out, rc = 0);
137                 }
138                 if (new->lvb_size > lvb->lvb_size) {
139                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
140                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
141                                lvb->lvb_size, new->lvb_size);
142                         lvb->lvb_size = new->lvb_size;
143                 }
144                 if (new->lvb_time > lvb->lvb_time) {
145                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb time: "
146                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
147                                lvb->lvb_time, new->lvb_time);
148                         lvb->lvb_time = new->lvb_time;
149                 }
150                 GOTO(out, rc = 0);
151         }
152
153         /* Update the LVB from the disk inode */
154         obd = res->lr_namespace->ns_lvbp;
155         LASSERT(obd);
156
157         oa = obdo_alloc();
158         if (oa == NULL)
159                 GOTO(out, rc = -ENOMEM);
160
161         oa->o_id = res->lr_name.name[0];
162         oa->o_gr = 0;
163         dentry = filter_oa2dentry(obd, oa);
164         if (IS_ERR(dentry))
165                 GOTO(out, rc = PTR_ERR(dentry));
166
167         /* Limit the valid bits in the return data to what we actually use */
168         oa->o_valid = OBD_MD_FLID;
169         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
170
171         lvb->lvb_size = dentry->d_inode->i_size;
172         lvb->lvb_time = LTIME_S(dentry->d_inode->i_mtime);
173         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", time: "
174                LPU64"\n", res->lr_name.name[0], lvb->lvb_size, lvb->lvb_time);
175         f_dput(dentry);
176
177  out:
178         if (oa != NULL)
179                 obdo_free(oa);
180         up(&res->lr_lvb_sem);
181         return rc;
182 }
183
184
185
186 struct ldlm_valblock_ops filter_lvbo = {
187         lvbo_init: filter_lvbo_init,
188         lvbo_update: filter_lvbo_update
189 };