Whamcloud - gitweb
1f627b257a6774661dfc4b76268742f20bde0ca9
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdfilter/filter_lvb.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/version.h>
50
51 #include <libcfs/list.h>
52 #include <obd_class.h>
53 #include <lustre_dlm.h>
54
55 #include "filter_internal.h"
56
57 /* Called with res->lr_lvb_sem held */
58 static int filter_lvbo_init(struct ldlm_resource *res)
59 {
60         struct ost_lvb *lvb = NULL;
61         struct obd_device *obd;
62         struct dentry *dentry;
63         int rc = 0;
64         ENTRY;
65
66         LASSERT(res);
67         LASSERT_SEM_LOCKED(&res->lr_lvb_sem);
68
69         if (res->lr_lvb_data)
70                 RETURN(0);
71
72         OBD_ALLOC(lvb, sizeof(*lvb));
73         if (lvb == NULL)
74                 RETURN(-ENOMEM);
75
76         res->lr_lvb_data = lvb;
77         res->lr_lvb_len = sizeof(*lvb);
78
79         obd = res->lr_namespace->ns_lvbp;
80         LASSERT(obd != NULL);
81
82         CDEBUG(D_INODE, "%s: filter_lvbo_init(o_gr="LPU64", o_id="
83                LPU64")\n", obd->obd_name, res->lr_name.name[1],
84                res->lr_name.name[0]);
85
86         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1], 
87                                               res->lr_name.name[0]);
88         if (IS_ERR(dentry)) {
89                 rc = PTR_ERR(dentry);
90                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
91                        res->lr_name.name[0], res->lr_name.name[1], rc);
92                 RETURN(rc);
93         }
94
95         if (dentry->d_inode == NULL)
96                 /* This is always true for test_brw */
97                 GOTO(out_dentry, rc = -ENOENT);
98
99         inode_init_lvb(dentry->d_inode, lvb);
100
101         CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
102                "mtime: "LPX64", blocks: "LPX64"\n",
103                res->lr_name.name[0], lvb->lvb_size,
104                lvb->lvb_mtime, lvb->lvb_blocks);
105
106         EXIT;
107 out_dentry:
108         f_dput(dentry);
109
110         if (rc)
111                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
112         /* Don't free lvb data on lookup error */
113         return rc;
114 }
115
116 /* This will be called in two ways:
117  *
118  *   m != NULL : called by the DLM itself after a glimpse callback
119  *   m == NULL : called by the filter after a disk write
120  *
121  *   If 'increase_only' is true, don't allow values to move backwards.
122  */
123 static int filter_lvbo_update(struct ldlm_resource *res,
124                               struct ptlrpc_request *r,
125                               int buf_idx, int increase_only)
126 {
127         int rc = 0;
128         struct ost_lvb *lvb;
129         struct obd_device *obd;
130         struct dentry *dentry;
131         ENTRY;
132
133         LASSERT(res);
134
135         down(&res->lr_lvb_sem);
136         lvb = res->lr_lvb_data;
137         if (lvb == NULL) {
138                 CERROR("No lvb when running lvbo_update!\n");
139                 GOTO(out, rc = 0);
140         }
141
142         /* Update the LVB from the network message */
143         if (r != NULL) {
144                 struct ost_lvb *new;
145
146                 /* XXX update always from reply buffer */
147                 new = lustre_swab_repbuf(r, buf_idx, sizeof(*new),
148                                          lustre_swab_ost_lvb);
149
150                 if (new == NULL) {
151                         CERROR("lustre_swab_buf failed\n");
152                         goto disk_update;
153                 }
154                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
155                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
156                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
157                                lvb->lvb_size, new->lvb_size);
158                         lvb->lvb_size = new->lvb_size;
159                 }
160                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
161                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
162                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
163                                lvb->lvb_mtime, new->lvb_mtime);
164                         lvb->lvb_mtime = new->lvb_mtime;
165                 }
166                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
167                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
168                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
169                                lvb->lvb_atime, new->lvb_atime);
170                         lvb->lvb_atime = new->lvb_atime;
171                 }
172                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
173                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
174                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
175                                lvb->lvb_ctime, new->lvb_ctime);
176                         lvb->lvb_ctime = new->lvb_ctime;
177                 }
178         }
179
180  disk_update:
181         /* Update the LVB from the disk inode */
182         obd = res->lr_namespace->ns_lvbp;
183         LASSERT(obd);
184         
185         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1], 
186                                               res->lr_name.name[0]);
187         if (IS_ERR(dentry))
188                 GOTO(out, rc = PTR_ERR(dentry));
189
190         if (dentry->d_inode == NULL)
191                 GOTO(out_dentry, rc = -ENOENT);
192
193         if (i_size_read(dentry->d_inode) > lvb->lvb_size || !increase_only) {
194                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
195                        LPU64" -> %llu\n", res->lr_name.name[0],
196                        lvb->lvb_size, i_size_read(dentry->d_inode));
197                 lvb->lvb_size = i_size_read(dentry->d_inode);
198         }
199
200         if (LTIME_S(dentry->d_inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
201                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
202                        LPU64" -> %lu\n", res->lr_name.name[0],
203                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
204                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
205         }
206         if (LTIME_S(dentry->d_inode->i_atime) >lvb->lvb_atime|| !increase_only){
207                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
208                        LPU64" -> %lu\n", res->lr_name.name[0],
209                        lvb->lvb_atime, LTIME_S(dentry->d_inode->i_atime));
210                 lvb->lvb_atime = LTIME_S(dentry->d_inode->i_atime);
211         }
212         if (LTIME_S(dentry->d_inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
213                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
214                        LPU64" -> %lu\n", res->lr_name.name[0],
215                        lvb->lvb_ctime, LTIME_S(dentry->d_inode->i_ctime));
216                 lvb->lvb_ctime = LTIME_S(dentry->d_inode->i_ctime);
217         }
218         if (lvb->lvb_blocks != dentry->d_inode->i_blocks) {
219                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
220                        LPU64" -> %llu\n", res->lr_name.name[0],
221                        lvb->lvb_blocks, (unsigned long long)dentry->d_inode->i_blocks);
222                 lvb->lvb_blocks = dentry->d_inode->i_blocks;
223         }
224
225 out_dentry:
226         f_dput(dentry);
227
228 out:
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 };