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