Whamcloud - gitweb
land b1_5 onto 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 the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #define DEBUG_SUBSYSTEM S_FILTER
31
32 #ifdef HAVE_KERNEL_CONFIG_H
33 #include <linux/config.h>
34 #endif
35 #include <linux/module.h>
36 #include <linux/version.h>
37
38 #include <libcfs/list.h>
39 #include <obd_class.h>
40 #include <lustre_dlm.h>
41
42 #include "filter_internal.h"
43
44 /* Called with res->lr_lvb_sem held */
45 static int filter_lvbo_init(struct ldlm_resource *res)
46 {
47         struct ost_lvb *lvb = NULL;
48         struct obd_device *obd;
49         struct dentry *dentry;
50         int rc = 0;
51         ENTRY;
52
53         LASSERT(res);
54         LASSERT_SEM_LOCKED(&res->lr_lvb_sem);
55
56         /* we only want lvb's for object resources */
57         /* check for internal locks: these have name[1] != 0 */
58         if (res->lr_name.name[1])
59                 RETURN(0);
60
61         if (res->lr_lvb_data)
62                 RETURN(0);
63
64         OBD_ALLOC(lvb, sizeof(*lvb));
65         if (lvb == NULL)
66                 RETURN(-ENOMEM);
67
68         res->lr_lvb_data = lvb;
69         res->lr_lvb_len = sizeof(*lvb);
70
71         obd = res->lr_namespace->ns_lvbp;
72         LASSERT(obd != NULL);
73
74         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
75         if (IS_ERR(dentry)) {
76                 rc = PTR_ERR(dentry);
77                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
78                        res->lr_name.name[0], res->lr_name.name[1], rc);
79                 RETURN(rc);
80         }
81
82         if (dentry->d_inode == NULL)
83                 /* This is always true for test_brw */
84                 GOTO(out_dentry, rc = -ENOENT);
85
86         inode_init_lvb(dentry->d_inode, lvb);
87
88         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", "
89                "mtime: "LPU64", blocks: "LPU64"\n",
90                res->lr_name.name[0], lvb->lvb_size,
91                lvb->lvb_mtime, lvb->lvb_blocks);
92
93         EXIT;
94 out_dentry:
95         f_dput(dentry);
96
97         /* Don't free lvb data on lookup error */
98         return rc;
99 }
100
101 /* This will be called in two ways:
102  *
103  *   m != NULL : called by the DLM itself after a glimpse callback
104  *   m == NULL : called by the filter after a disk write
105  *
106  *   If 'increase_only' is true, don't allow values to move backwards.
107  */
108 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
109                               int buf_idx, int increase_only)
110 {
111         int rc = 0;
112         struct ost_lvb *lvb;
113         struct obd_device *obd;
114         struct dentry *dentry;
115         ENTRY;
116
117         LASSERT(res);
118
119         /* we only want lvb's for object resources */
120         /* check for internal locks: these have name[1] != 0 */
121         if (res->lr_name.name[1])
122                 RETURN(0);
123
124         down(&res->lr_lvb_sem);
125         lvb = res->lr_lvb_data;
126         if (lvb == NULL) {
127                 CERROR("No lvb when running lvbo_update!\n");
128                 GOTO(out, rc = 0);
129         }
130
131         /* Update the LVB from the network message */
132         if (m != NULL) {
133                 struct ost_lvb *new;
134
135                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
136                                       lustre_swab_ost_lvb);
137                 if (new == NULL) {
138                         CERROR("lustre_swab_buf failed\n");
139                         goto disk_update;
140                 }
141                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
142                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
143                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
144                                lvb->lvb_size, new->lvb_size);
145                         lvb->lvb_size = new->lvb_size;
146                 }
147                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
148                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
149                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
150                                lvb->lvb_mtime, new->lvb_mtime);
151                         lvb->lvb_mtime = new->lvb_mtime;
152                 }
153                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
154                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
155                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
156                                lvb->lvb_atime, new->lvb_atime);
157                         lvb->lvb_atime = new->lvb_atime;
158                 }
159                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
160                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
161                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
162                                lvb->lvb_ctime, new->lvb_ctime);
163                         lvb->lvb_ctime = new->lvb_ctime;
164                 }
165         }
166
167  disk_update:
168         /* Update the LVB from the disk inode */
169         obd = res->lr_namespace->ns_lvbp;
170         LASSERT(obd);
171
172         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
173         if (IS_ERR(dentry))
174                 GOTO(out, rc = PTR_ERR(dentry));
175
176         if (dentry->d_inode == NULL)
177                 GOTO(out_dentry, rc = -ENOENT);
178
179         if (dentry->d_inode->i_size > lvb->lvb_size || !increase_only) {
180                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
181                        LPU64" -> %llu\n", res->lr_name.name[0],
182                        lvb->lvb_size, dentry->d_inode->i_size);
183                 lvb->lvb_size = dentry->d_inode->i_size;
184         }
185
186         if (LTIME_S(dentry->d_inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
187                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
188                        LPU64" -> %lu\n", res->lr_name.name[0],
189                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
190                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
191         }
192         if (LTIME_S(dentry->d_inode->i_atime) >lvb->lvb_atime|| !increase_only){
193                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
194                        LPU64" -> %lu\n", res->lr_name.name[0],
195                        lvb->lvb_atime, LTIME_S(dentry->d_inode->i_atime));
196                 lvb->lvb_atime = LTIME_S(dentry->d_inode->i_atime);
197         }
198         if (LTIME_S(dentry->d_inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
199                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
200                        LPU64" -> %lu\n", res->lr_name.name[0],
201                        lvb->lvb_ctime, LTIME_S(dentry->d_inode->i_ctime));
202                 lvb->lvb_ctime = LTIME_S(dentry->d_inode->i_ctime);
203         }
204         if (lvb->lvb_blocks != dentry->d_inode->i_blocks) {
205                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
206                        LPU64" -> %lu\n", res->lr_name.name[0],
207                        lvb->lvb_blocks, dentry->d_inode->i_blocks);
208                 lvb->lvb_blocks = dentry->d_inode->i_blocks;
209         }
210
211 out_dentry:
212         f_dput(dentry);
213
214 out:
215         up(&res->lr_lvb_sem);
216         return rc;
217 }
218
219 struct ldlm_valblock_ops filter_lvbo = {
220         lvbo_init: filter_lvbo_init,
221         lvbo_update: filter_lvbo_update
222 };