Whamcloud - gitweb
LU-812 kernel: AUTOCONF_INCLUDED removed
[fs/lustre-release.git] / lustre / obdfilter / filter_lvb.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #include <linux/module.h>
46 #include <linux/version.h>
47
48 #include <libcfs/list.h>
49 #include <obd_class.h>
50 #include <lustre_dlm.h>
51
52 #include "filter_internal.h"
53
54 static int filter_lvbo_free(struct ldlm_resource *res) {
55         if (res->lr_lvb_inode) {
56                 iput(res->lr_lvb_inode);
57                 res->lr_lvb_inode = NULL;
58         }
59
60         if (res->lr_lvb_data)
61                 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
62
63         return 0;
64 }
65
66 /* Called with res->lr_lvb_mutex held */
67 static int filter_lvbo_init(struct ldlm_resource *res)
68 {
69         struct ost_lvb *lvb = NULL;
70         struct obd_device *obd;
71         struct dentry *dentry;
72         int rc = 0;
73         ENTRY;
74
75         LASSERT(res);
76         LASSERT_MUTEX_LOCKED(&res->lr_lvb_mutex);
77
78         if (res->lr_lvb_data)
79                 RETURN(0);
80
81         OBD_ALLOC(lvb, sizeof(*lvb));
82         if (lvb == NULL)
83                 RETURN(-ENOMEM);
84
85         res->lr_lvb_data = lvb;
86         res->lr_lvb_len = sizeof(*lvb);
87
88         obd = ldlm_res_to_ns(res)->ns_lvbp;
89         LASSERT(obd != NULL);
90
91         CDEBUG(D_INODE, "%s: filter_lvbo_init(o_seq="LPU64", o_id="
92                LPU64")\n", obd->obd_name, res->lr_name.name[1],
93                res->lr_name.name[0]);
94
95         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
96                                               res->lr_name.name[0]);
97         if (IS_ERR(dentry)) {
98                 rc = PTR_ERR(dentry);
99                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
100                        res->lr_name.name[0], res->lr_name.name[1], rc);
101                 RETURN(rc);
102         }
103
104         if (dentry->d_inode == NULL)
105                 /* This is always true for test_brw */
106                 GOTO(out_dentry, rc = -ENOENT);
107
108         inode_init_lvb(dentry->d_inode, lvb);
109
110         CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
111                "mtime: "LPX64", blocks: "LPX64"\n",
112                res->lr_name.name[0], lvb->lvb_size,
113                lvb->lvb_mtime, lvb->lvb_blocks);
114
115         res->lr_lvb_inode = igrab(dentry->d_inode);
116
117         EXIT;
118 out_dentry:
119         f_dput(dentry);
120
121         if (rc)
122                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
123         /* Don't free lvb data on lookup error */
124         return rc;
125 }
126
127 /* This will be called in two ways:
128  *
129  *   r != NULL : called by the DLM itself after a glimpse callback
130  *   r == NULL : called by the filter after a disk write
131  *
132  *   If 'increase_only' is true, don't allow values to move backwards.
133  */
134 static int filter_lvbo_update(struct ldlm_resource *res,
135                               struct ptlrpc_request *r, int increase_only)
136 {
137         int rc = 0;
138         struct ost_lvb *lvb;
139         struct obd_device *obd;
140         struct inode *inode;
141         struct inode *tmpinode = NULL;
142         ENTRY;
143
144         LASSERT(res);
145
146         lock_res(res);
147         lvb = res->lr_lvb_data;
148         if (lvb == NULL) {
149                 CERROR("No lvb when running lvbo_update!\n");
150                 GOTO(out, rc = 0);
151         }
152
153         /* Update the LVB from the network message */
154         if (r != NULL) {
155                 struct ost_lvb *new;
156
157                 /* XXX update always from reply buffer */
158                 new = req_capsule_server_get(&r->rq_pill, &RMF_DLM_LVB);
159
160                 if (new == NULL) {
161                         CERROR("lustre_swab_buf failed\n");
162                         goto disk_update;
163                 }
164                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
165                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
166                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
167                                lvb->lvb_size, new->lvb_size);
168                         lvb->lvb_size = new->lvb_size;
169                 }
170                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
171                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
172                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
173                                lvb->lvb_mtime, new->lvb_mtime);
174                         lvb->lvb_mtime = new->lvb_mtime;
175                 }
176                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
177                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
178                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
179                                lvb->lvb_atime, new->lvb_atime);
180                         lvb->lvb_atime = new->lvb_atime;
181                 }
182                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
183                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
184                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
185                                lvb->lvb_ctime, new->lvb_ctime);
186                         lvb->lvb_ctime = new->lvb_ctime;
187                 }
188                 if (new->lvb_blocks > lvb->lvb_blocks || !increase_only) {
189                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks: "
190                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
191                                lvb->lvb_blocks, new->lvb_blocks);
192                         lvb->lvb_blocks = new->lvb_blocks;
193                 }
194         }
195
196  disk_update:
197         /* Update the LVB from the disk inode */
198         obd = ldlm_res_to_ns(res)->ns_lvbp;
199         LASSERT(obd);
200
201         inode = res->lr_lvb_inode;
202         /* filter_fid2dentry could fail, esp. in OBD_FAIL_OST_ENOENT test case */
203         if (unlikely(inode == NULL)) {
204                 struct dentry *dentry;
205
206                 unlock_res(res);
207
208                 dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
209                                            res->lr_name.name[0]);
210                 if (IS_ERR(dentry))
211                         RETURN(PTR_ERR(dentry));
212
213                 if (dentry->d_inode)
214                         tmpinode = igrab(dentry->d_inode);
215                 f_dput(dentry);
216                 /* tmpinode could be NULL, but it does not matter if other
217                  * have set res->lr_lvb_inode */
218                 lock_res(res);
219                 if (res->lr_lvb_inode == NULL) {
220                         res->lr_lvb_inode = tmpinode;
221                         tmpinode = NULL;
222                 }
223                 inode = res->lr_lvb_inode;
224         }
225
226         if (!inode || !inode->i_nlink)
227                 GOTO(out, rc = -ENOENT);
228
229         if (i_size_read(inode) > lvb->lvb_size || !increase_only) {
230                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
231                        LPU64" -> %llu\n", res->lr_name.name[0],
232                        lvb->lvb_size, i_size_read(inode));
233                 lvb->lvb_size = i_size_read(inode);
234         }
235
236         if (LTIME_S(inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
237                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
238                        LPU64" -> %lu\n", res->lr_name.name[0],
239                        lvb->lvb_mtime, LTIME_S(inode->i_mtime));
240                 lvb->lvb_mtime = LTIME_S(inode->i_mtime);
241         }
242         if (LTIME_S(inode->i_atime) >lvb->lvb_atime|| !increase_only){
243                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
244                        LPU64" -> %lu\n", res->lr_name.name[0],
245                        lvb->lvb_atime, LTIME_S(inode->i_atime));
246                 lvb->lvb_atime = LTIME_S(inode->i_atime);
247         }
248         if (LTIME_S(inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
249                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
250                        LPU64" -> %lu\n", res->lr_name.name[0],
251                        lvb->lvb_ctime, LTIME_S(inode->i_ctime));
252                 lvb->lvb_ctime = LTIME_S(inode->i_ctime);
253         }
254         if (inode->i_blocks > lvb->lvb_blocks || !increase_only) {
255                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
256                        LPU64" -> %llu\n", res->lr_name.name[0],
257                        lvb->lvb_blocks, (unsigned long long)inode->i_blocks);
258                 lvb->lvb_blocks = inode->i_blocks;
259         }
260
261 out:
262         unlock_res(res);
263         if (tmpinode)
264                 iput(tmpinode);
265         return rc;
266 }
267
268 struct ldlm_valblock_ops filter_lvbo = {
269         lvbo_init: filter_lvbo_init,
270         lvbo_update: filter_lvbo_update,
271         lvbo_free: filter_lvbo_free
272 };