Whamcloud - gitweb
Branch 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 #ifndef AUTOCONF_INCLUDED
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         if (res->lr_lvb_data)
57                 RETURN(0);
58
59         OBD_ALLOC(lvb, sizeof(*lvb));
60         if (lvb == NULL)
61                 RETURN(-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         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1], 
70                                               res->lr_name.name[0]);
71         if (IS_ERR(dentry)) {
72                 rc = PTR_ERR(dentry);
73                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
74                        res->lr_name.name[0], res->lr_name.name[1], rc);
75                 RETURN(rc);
76         }
77
78         if (dentry->d_inode == NULL)
79                 /* This is always true for test_brw */
80                 GOTO(out_dentry, rc = -ENOENT);
81
82         inode_init_lvb(dentry->d_inode, lvb);
83
84         CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
85                "mtime: "LPX64", blocks: "LPX64"\n",
86                res->lr_name.name[0], lvb->lvb_size,
87                lvb->lvb_mtime, lvb->lvb_blocks);
88
89         EXIT;
90 out_dentry:
91         f_dput(dentry);
92
93         if (rc)
94                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
95         /* Don't free lvb data on lookup error */
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  *   If 'increase_only' is true, don't allow values to move backwards.
105  */
106 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
107                               int buf_idx, int increase_only)
108 {
109         int rc = 0;
110         struct ost_lvb *lvb;
111         struct obd_device *obd;
112         struct dentry *dentry;
113         ENTRY;
114
115         LASSERT(res);
116
117         down(&res->lr_lvb_sem);
118         lvb = res->lr_lvb_data;
119         if (lvb == NULL) {
120                 CERROR("No lvb when running lvbo_update!\n");
121                 GOTO(out, rc = 0);
122         }
123
124         /* Update the LVB from the network message */
125         if (m != NULL) {
126                 struct ost_lvb *new;
127
128                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
129                                       lustre_swab_ost_lvb);
130                 if (new == NULL) {
131                         CERROR("lustre_swab_buf failed\n");
132                         goto disk_update;
133                 }
134                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
135                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
136                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
137                                lvb->lvb_size, new->lvb_size);
138                         lvb->lvb_size = new->lvb_size;
139                 }
140                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
141                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
142                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
143                                lvb->lvb_mtime, new->lvb_mtime);
144                         lvb->lvb_mtime = new->lvb_mtime;
145                 }
146                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
147                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
148                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
149                                lvb->lvb_atime, new->lvb_atime);
150                         lvb->lvb_atime = new->lvb_atime;
151                 }
152                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
153                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
154                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
155                                lvb->lvb_ctime, new->lvb_ctime);
156                         lvb->lvb_ctime = new->lvb_ctime;
157                 }
158         }
159
160  disk_update:
161         /* Update the LVB from the disk inode */
162         obd = res->lr_namespace->ns_lvbp;
163         LASSERT(obd);
164         
165         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1], 
166                                               res->lr_name.name[0]);
167         if (IS_ERR(dentry))
168                 GOTO(out, rc = PTR_ERR(dentry));
169
170         if (dentry->d_inode == NULL)
171                 GOTO(out_dentry, rc = -ENOENT);
172
173         if (i_size_read(dentry->d_inode) > lvb->lvb_size || !increase_only) {
174                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
175                        LPU64" -> %llu\n", res->lr_name.name[0],
176                        lvb->lvb_size, i_size_read(dentry->d_inode));
177                 lvb->lvb_size = i_size_read(dentry->d_inode);
178         }
179
180         if (LTIME_S(dentry->d_inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
181                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
182                        LPU64" -> %lu\n", res->lr_name.name[0],
183                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
184                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
185         }
186         if (LTIME_S(dentry->d_inode->i_atime) >lvb->lvb_atime|| !increase_only){
187                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
188                        LPU64" -> %lu\n", res->lr_name.name[0],
189                        lvb->lvb_atime, LTIME_S(dentry->d_inode->i_atime));
190                 lvb->lvb_atime = LTIME_S(dentry->d_inode->i_atime);
191         }
192         if (LTIME_S(dentry->d_inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
193                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
194                        LPU64" -> %lu\n", res->lr_name.name[0],
195                        lvb->lvb_ctime, LTIME_S(dentry->d_inode->i_ctime));
196                 lvb->lvb_ctime = LTIME_S(dentry->d_inode->i_ctime);
197         }
198         if (lvb->lvb_blocks != dentry->d_inode->i_blocks) {
199                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
200                        LPU64" -> %llu\n", res->lr_name.name[0],
201                        lvb->lvb_blocks, (unsigned long long)dentry->d_inode->i_blocks);
202                 lvb->lvb_blocks = dentry->d_inode->i_blocks;
203         }
204
205 out_dentry:
206         f_dput(dentry);
207
208 out:
209         up(&res->lr_lvb_sem);
210         return rc;
211 }
212
213 struct ldlm_valblock_ops filter_lvbo = {
214         lvbo_init: filter_lvbo_init,
215         lvbo_update: filter_lvbo_update
216 };