Whamcloud - gitweb
LU-1526 tests: Handle OFD procfs changes
[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 static int filter_lvbo_free(struct ldlm_resource *res) {
58         if (res->lr_lvb_inode) {
59                 iput(res->lr_lvb_inode);
60                 res->lr_lvb_inode = NULL;
61         }
62
63         if (res->lr_lvb_data)
64                 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
65
66         return 0;
67 }
68
69 /* Called with res->lr_lvb_sem held */
70 static int filter_lvbo_init(struct ldlm_resource *res)
71 {
72         struct ost_lvb *lvb = NULL;
73         struct obd_device *obd;
74         struct dentry *dentry;
75         int rc = 0;
76         ENTRY;
77
78         LASSERT(res);
79         LASSERT_SEM_LOCKED(&res->lr_lvb_sem);
80
81         if (res->lr_lvb_data)
82                 RETURN(0);
83
84         OBD_ALLOC(lvb, sizeof(*lvb));
85         if (lvb == NULL)
86                 RETURN(-ENOMEM);
87
88         res->lr_lvb_data = lvb;
89         res->lr_lvb_len = sizeof(*lvb);
90
91         obd = res->lr_namespace->ns_lvbp;
92         LASSERT(obd != NULL);
93
94         CDEBUG(D_INODE, "%s: filter_lvbo_init(o_gr="LPU64", o_id="
95                LPU64")\n", obd->obd_name, res->lr_name.name[1],
96                res->lr_name.name[0]);
97
98         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
99         if (IS_ERR(dentry)) {
100                 rc = PTR_ERR(dentry);
101                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
102                        res->lr_name.name[0], res->lr_name.name[1], rc);
103                 RETURN(rc);
104         }
105
106         if (dentry->d_inode == NULL)
107                 /* This is always true for test_brw */
108                 GOTO(out_dentry, rc = -ENOENT);
109
110         inode_init_lvb(dentry->d_inode, lvb);
111
112         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", "
113                "mtime: "LPU64", blocks: "LPU64"\n",
114                res->lr_name.name[0], lvb->lvb_size,
115                lvb->lvb_mtime, lvb->lvb_blocks);
116
117         res->lr_lvb_inode = igrab(dentry->d_inode);
118
119         EXIT;
120 out_dentry:
121         f_dput(dentry);
122
123         if (rc)
124                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
125         /* Don't free lvb data on lookup error */
126         return rc;
127 }
128
129 /* This will be called in two ways:
130  *
131  *   m != NULL : called by the DLM itself after a glimpse callback
132  *   m == NULL : called by the filter after a disk write
133  *
134  *   If 'increase_only' is true, don't allow values to move backwards.
135  */
136 static int filter_lvbo_update(struct ldlm_resource *res, struct ptlrpc_request *r,
137                               int buf_idx, int increase_only)
138 {
139         int rc = 0;
140         struct ost_lvb *lvb;
141         struct obd_device *obd;
142         struct inode *inode;
143         ENTRY;
144
145         LASSERT(res);
146
147         down(&res->lr_lvb_sem);
148         lvb = res->lr_lvb_data;
149         if (lvb == NULL) {
150                 CERROR("No lvb when running lvbo_update!\n");
151                 GOTO(out, rc = 0);
152         }
153
154         /* Update the LVB from the network message */
155         if (r != NULL) {
156                 struct ost_lvb *new;
157
158                 /* XXX update always from reply buffer */
159                 new = lustre_swab_repbuf(r, buf_idx, sizeof(*new),
160                                          lustre_swab_ost_lvb);
161                 if (new == NULL) {
162                         CERROR("lustre_swab_buf failed\n");
163                         goto disk_update;
164                 }
165                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
166                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
167                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
168                                lvb->lvb_size, new->lvb_size);
169                         lvb->lvb_size = new->lvb_size;
170                 }
171                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
172                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
173                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
174                                lvb->lvb_mtime, new->lvb_mtime);
175                         lvb->lvb_mtime = new->lvb_mtime;
176                 }
177                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
178                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
179                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
180                                lvb->lvb_atime, new->lvb_atime);
181                         lvb->lvb_atime = new->lvb_atime;
182                 }
183                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
184                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
185                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
186                                lvb->lvb_ctime, new->lvb_ctime);
187                         lvb->lvb_ctime = new->lvb_ctime;
188                 }
189         }
190
191  disk_update:
192         /* Update the LVB from the disk inode */
193         obd = res->lr_namespace->ns_lvbp;
194         LASSERT(obd);
195
196         inode = res->lr_lvb_inode;
197         /* filter_fid2dentry could fail */
198         if (unlikely(!inode)) {
199                 struct dentry *dentry;
200
201                 dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
202                 if (IS_ERR(dentry))
203                         GOTO(out, rc = PTR_ERR(dentry));
204                 if (dentry->d_inode)
205                         inode = res->lr_lvb_inode = igrab(dentry->d_inode);
206                 f_dput(dentry);
207         }
208
209         if (!inode || !inode->i_nlink)
210                 GOTO(out, rc = -ENOENT);
211
212         if (i_size_read(inode) > lvb->lvb_size || !increase_only) {
213                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
214                        LPU64" -> %llu\n", res->lr_name.name[0],
215                        lvb->lvb_size, i_size_read(inode));
216                 lvb->lvb_size = i_size_read(inode);
217         }
218
219         if (LTIME_S(inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
220                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
221                        LPU64" -> %lu\n", res->lr_name.name[0],
222                        lvb->lvb_mtime, LTIME_S(inode->i_mtime));
223                 lvb->lvb_mtime = LTIME_S(inode->i_mtime);
224         }
225         if (LTIME_S(inode->i_atime) >lvb->lvb_atime|| !increase_only){
226                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
227                        LPU64" -> %lu\n", res->lr_name.name[0],
228                        lvb->lvb_atime, LTIME_S(inode->i_atime));
229                 lvb->lvb_atime = LTIME_S(inode->i_atime);
230         }
231         if (LTIME_S(inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
232                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
233                        LPU64" -> %lu\n", res->lr_name.name[0],
234                        lvb->lvb_ctime, LTIME_S(inode->i_ctime));
235                 lvb->lvb_ctime = LTIME_S(inode->i_ctime);
236         }
237         if (lvb->lvb_blocks != inode->i_blocks) {
238                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
239                        LPU64" -> "LPU64"\n", res->lr_name.name[0],
240                        lvb->lvb_blocks, (__u64)inode->i_blocks);
241                 lvb->lvb_blocks = inode->i_blocks;
242         }
243
244 out:
245         up(&res->lr_lvb_sem);
246         return rc;
247 }
248
249 struct ldlm_valblock_ops filter_lvbo = {
250         lvbo_init: filter_lvbo_init,
251         lvbo_update: filter_lvbo_update,
252         lvbo_free: filter_lvbo_free
253 };