Whamcloud - gitweb
several fixes: expiry timer adjusted.
[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 <libcfs/list.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_dlm.h>
36
37 #include "filter_internal.h"
38
39 /* Called with res->lr_lvb_sem held */
40 static int filter_lvbo_init(struct ldlm_resource *res)
41 {
42         struct ost_lvb *lvb = NULL;
43         struct filter_obd *filter;
44         struct obd_device *obd;
45         struct dentry *dentry;
46         __u64 ogr, oid;
47         int rc = 0;
48         ENTRY;
49
50         LASSERT(res);
51         LASSERT(down_trylock(&res->lr_lvb_sem) != 0);
52
53         /* we only want lvb's for object resources */
54         /* check for internal locks: these have name[1] != 0 */
55         if (res->lr_name.name[1])
56                 RETURN(0);
57
58         if (res->lr_lvb_data)
59                 GOTO(out, rc = 0);
60
61         OBD_ALLOC(lvb, sizeof(*lvb));
62         if (lvb == NULL)
63                 GOTO(out, rc = -ENOMEM);
64
65         res->lr_lvb_data = lvb;
66         res->lr_lvb_len = sizeof(*lvb);
67
68         obd = res->lr_namespace->ns_lvbp;
69         filter = &obd->u.filter;
70         LASSERT(obd != NULL);
71
72         oid = res->lr_name.name[0];
73         ogr = res->lr_name.name[2];
74
75         dentry = filter_id2dentry(obd, NULL, ogr, oid);
76         if (IS_ERR(dentry))
77                 GOTO(out, rc = PTR_ERR(dentry));
78
79         if (dentry->d_inode == NULL) {
80                 lvb->lvb_size = 0;
81                 lvb->lvb_blocks = 0;
82
83                 /* making client use MDS mtime as this one is zero, bigger one
84                  * will be taken and this does not break POSIX. Thanks to
85                  * Andreas. --umka */
86                 lvb->lvb_mtime = 0;
87         } else {
88                 lvb->lvb_size = dentry->d_inode->i_size;
89                 lvb->lvb_blocks = dentry->d_inode->i_blocks;
90                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
91         }
92
93         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", "
94                "mtime: "LPU64", blocks: "LPU64"\n", res->lr_name.name[0],
95                lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_blocks);
96
97         f_dput(dentry);
98         EXIT;
99 out:
100         /* don't free lvb data on lookup error */
101         return rc;
102 }
103
104 /* This will be called in two ways:
105  *
106  *   m != NULL : called by the DLM itself after a glimpse callback
107  *   m == NULL : called by the filter after a disk write
108  *
109  *   If 'increase' is true, don't allow values to move backwards.
110  */
111 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
112                               int buf_idx, int increase)
113 {
114         struct ost_lvb *lvb = res->lr_lvb_data;
115         struct obd_device *obd;
116         struct obdo *oa = NULL;
117         struct dentry *dentry;
118         int rc = 0;
119         ENTRY;
120
121         LASSERT(res);
122
123         /* we only want lvb's for object resources */
124         /* check for internal locks: these have name[1] != 0 */
125         if (res->lr_name.name[1])
126                 RETURN(0);
127
128         down(&res->lr_lvb_sem);
129         if (!res->lr_lvb_data) {
130                 CERROR("No lvb when running lvbo_update!\n");
131                 GOTO(out, rc = 0);
132         }
133
134         /* Update the LVB from the network message */
135         if (m != NULL) {
136                 struct ost_lvb *new;
137
138                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
139                                       lustre_swab_ost_lvb);
140                 if (new == NULL) {
141                         CERROR("lustre_swab_buf failed\n");
142                         //GOTO(out, rc = -EPROTO);
143                         GOTO(out, rc = 0);
144                 }
145
146                 lock_res(res);
147                 if (new->lvb_size > lvb->lvb_size || !increase) {
148                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
149                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
150                                lvb->lvb_size, new->lvb_size);
151                         lvb->lvb_size = new->lvb_size;
152                 }
153                 if (new->lvb_mtime > lvb->lvb_mtime || !increase) {
154                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
155                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
156                                lvb->lvb_mtime, new->lvb_mtime);
157                         lvb->lvb_mtime = new->lvb_mtime;
158                 }
159                 if (new->lvb_atime > lvb->lvb_atime || !increase) {
160                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
161                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
162                                lvb->lvb_atime, new->lvb_atime);
163                         lvb->lvb_atime = new->lvb_atime;
164                 }
165                 if (new->lvb_ctime > lvb->lvb_ctime || !increase) {
166                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
167                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
168                                lvb->lvb_ctime, new->lvb_ctime);
169                         lvb->lvb_ctime = new->lvb_ctime;
170                 }
171                 unlock_res(res);
172         }
173
174         /* Update the LVB from the disk inode */
175         obd = res->lr_namespace->ns_lvbp;
176         LASSERT(obd);
177
178         oa = obdo_alloc();
179         if (oa == NULL)
180                 GOTO(out, rc = -ENOMEM);
181
182         oa->o_id = res->lr_name.name[0];
183         oa->o_gr = res->lr_name.name[2];
184         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
185         dentry = filter_oa2dentry(obd, oa);
186         if (IS_ERR(dentry))
187                 GOTO(out, rc = PTR_ERR(dentry));
188
189         /* Limit the valid bits in the return data to what we actually use */
190         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
191         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
192
193         lock_res(res);
194         if (dentry->d_inode->i_size > lvb->lvb_size || !increase) {
195                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
196                        LPU64" -> %llu\n", res->lr_name.name[0],
197                        lvb->lvb_size, dentry->d_inode->i_size);
198                 lvb->lvb_size = dentry->d_inode->i_size;
199         }
200
201         if (LTIME_S(dentry->d_inode->i_mtime) > lvb->lvb_mtime || !increase) {
202                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
203                        LPU64" -> %lu\n", res->lr_name.name[0],
204                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
205                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
206         }
207         if (LTIME_S(dentry->d_inode->i_atime) > lvb->lvb_atime || !increase) {
208                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
209                        LPU64" -> %lu\n", res->lr_name.name[0],
210                        lvb->lvb_atime, LTIME_S(dentry->d_inode->i_atime));
211                 lvb->lvb_atime = LTIME_S(dentry->d_inode->i_atime);
212         }
213         if (LTIME_S(dentry->d_inode->i_ctime) > lvb->lvb_ctime || !increase) {
214                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
215                        LPU64" -> %lu\n", res->lr_name.name[0],
216                        lvb->lvb_ctime, LTIME_S(dentry->d_inode->i_ctime));
217                 lvb->lvb_ctime = LTIME_S(dentry->d_inode->i_ctime);
218         }
219         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks from disk: "
220                LPU64" -> %lu\n", res->lr_name.name[0],
221                lvb->lvb_blocks, dentry->d_inode->i_blocks);
222         lvb->lvb_blocks = dentry->d_inode->i_blocks;
223         unlock_res(res);
224
225         f_dput(dentry);
226 out:
227         if (oa)
228                 obdo_free(oa);
229         up(&res->lr_lvb_sem);
230         return rc;
231 }
232
233 struct ldlm_valblock_ops filter_lvbo = {
234         lvbo_init: filter_lvbo_init,
235         lvbo_update: filter_lvbo_update
236 };