Whamcloud - gitweb
LU-1222 ldlm: Fix the race in AST sender vs multiple arriving RPCs
[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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/obdfilter/filter_lvb.c
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_FILTER
46
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
49 #endif
50 #include <linux/module.h>
51 #include <linux/version.h>
52
53 #include <libcfs/list.h>
54 #include <obd_class.h>
55 #include <lustre_dlm.h>
56
57 #include "filter_internal.h"
58
59 static int filter_lvbo_free(struct ldlm_resource *res) {
60         if (res->lr_lvb_inode) {
61                 iput(res->lr_lvb_inode);
62                 res->lr_lvb_inode = NULL;
63         }
64
65         if (res->lr_lvb_data)
66                 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
67
68         return 0;
69 }
70
71 /* Called with res->lr_lvb_sem held */
72 static int filter_lvbo_init(struct ldlm_resource *res)
73 {
74         struct ost_lvb *lvb = NULL;
75         struct obd_device *obd;
76         struct dentry *dentry;
77         int rc = 0;
78         ENTRY;
79
80         LASSERT(res);
81         LASSERT_SEM_LOCKED(&res->lr_lvb_sem);
82
83         if (res->lr_lvb_data)
84                 RETURN(0);
85
86         OBD_ALLOC(lvb, sizeof(*lvb));
87         if (lvb == NULL)
88                 RETURN(-ENOMEM);
89
90         res->lr_lvb_data = lvb;
91         res->lr_lvb_len = sizeof(*lvb);
92
93         obd = ldlm_res_to_ns(res)->ns_lvbp;
94         LASSERT(obd != NULL);
95
96         CDEBUG(D_INODE, "%s: filter_lvbo_init(o_seq="LPU64", o_id="
97                LPU64")\n", obd->obd_name, res->lr_name.name[1],
98                res->lr_name.name[0]);
99
100         dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
101                                               res->lr_name.name[0]);
102         if (IS_ERR(dentry)) {
103                 rc = PTR_ERR(dentry);
104                 CERROR("%s: bad object "LPU64"/"LPU64": rc %d\n", obd->obd_name,
105                        res->lr_name.name[0], res->lr_name.name[1], rc);
106                 RETURN(rc);
107         }
108
109         if (dentry->d_inode == NULL)
110                 /* This is always true for test_brw */
111                 GOTO(out_dentry, rc = -ENOENT);
112
113         inode_init_lvb(dentry->d_inode, lvb);
114
115         CDEBUG(D_DLMTRACE, "res: "LPX64" initial lvb size: "LPX64", "
116                "mtime: "LPX64", blocks: "LPX64"\n",
117                res->lr_name.name[0], lvb->lvb_size,
118                lvb->lvb_mtime, lvb->lvb_blocks);
119
120         res->lr_lvb_inode = igrab(dentry->d_inode);
121
122         EXIT;
123 out_dentry:
124         f_dput(dentry);
125
126         if (rc)
127                 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
128         /* Don't free lvb data on lookup error */
129         return rc;
130 }
131
132 /* This will be called in two ways:
133  *
134  *   r != NULL : called by the DLM itself after a glimpse callback
135  *   r == NULL : called by the filter after a disk write
136  *
137  *   If 'increase_only' is true, don't allow values to move backwards.
138  */
139 static int filter_lvbo_update(struct ldlm_resource *res,
140                               struct ptlrpc_request *r, int increase_only)
141 {
142         int rc = 0;
143         struct ost_lvb *lvb;
144         struct obd_device *obd;
145         struct inode *inode;
146         struct inode *tmpinode = NULL;
147         ENTRY;
148
149         LASSERT(res);
150
151         lock_res(res);
152         lvb = res->lr_lvb_data;
153         if (lvb == NULL) {
154                 CERROR("No lvb when running lvbo_update!\n");
155                 GOTO(out, rc = 0);
156         }
157
158         /* Update the LVB from the network message */
159         if (r != NULL) {
160                 struct ost_lvb *new;
161
162                 /* XXX update always from reply buffer */
163                 new = req_capsule_server_get(&r->rq_pill, &RMF_DLM_LVB);
164
165                 if (new == NULL) {
166                         CERROR("lustre_swab_buf failed\n");
167                         goto disk_update;
168                 }
169                 if (new->lvb_size > lvb->lvb_size || !increase_only) {
170                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
171                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
172                                lvb->lvb_size, new->lvb_size);
173                         lvb->lvb_size = new->lvb_size;
174                 }
175                 if (new->lvb_mtime > lvb->lvb_mtime || !increase_only) {
176                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
177                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
178                                lvb->lvb_mtime, new->lvb_mtime);
179                         lvb->lvb_mtime = new->lvb_mtime;
180                 }
181                 if (new->lvb_atime > lvb->lvb_atime || !increase_only) {
182                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
183                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
184                                lvb->lvb_atime, new->lvb_atime);
185                         lvb->lvb_atime = new->lvb_atime;
186                 }
187                 if (new->lvb_ctime > lvb->lvb_ctime || !increase_only) {
188                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
189                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
190                                lvb->lvb_ctime, new->lvb_ctime);
191                         lvb->lvb_ctime = new->lvb_ctime;
192                 }
193                 if (new->lvb_blocks > lvb->lvb_blocks || !increase_only) {
194                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks: "
195                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
196                                lvb->lvb_blocks, new->lvb_blocks);
197                         lvb->lvb_blocks = new->lvb_blocks;
198                 }
199         }
200
201  disk_update:
202         /* Update the LVB from the disk inode */
203         obd = ldlm_res_to_ns(res)->ns_lvbp;
204         LASSERT(obd);
205
206         inode = res->lr_lvb_inode;
207         /* filter_fid2dentry could fail, esp. in OBD_FAIL_OST_ENOENT test case */
208         if (unlikely(inode == NULL)) {
209                 struct dentry *dentry;
210
211                 unlock_res(res);
212
213                 dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
214                                            res->lr_name.name[0]);
215                 if (IS_ERR(dentry))
216                         RETURN(PTR_ERR(dentry));
217
218                 if (dentry->d_inode)
219                         tmpinode = igrab(dentry->d_inode);
220                 f_dput(dentry);
221                 /* tmpinode could be NULL, but it does not matter if other
222                  * have set res->lr_lvb_inode */
223                 lock_res(res);
224                 if (res->lr_lvb_inode == NULL) {
225                         res->lr_lvb_inode = tmpinode;
226                         tmpinode = NULL;
227                 }
228                 inode = res->lr_lvb_inode;
229         }
230
231         if (!inode || !inode->i_nlink)
232                 GOTO(out, rc = -ENOENT);
233
234         if (i_size_read(inode) > lvb->lvb_size || !increase_only) {
235                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
236                        LPU64" -> %llu\n", res->lr_name.name[0],
237                        lvb->lvb_size, i_size_read(inode));
238                 lvb->lvb_size = i_size_read(inode);
239         }
240
241         if (LTIME_S(inode->i_mtime) >lvb->lvb_mtime|| !increase_only){
242                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
243                        LPU64" -> %lu\n", res->lr_name.name[0],
244                        lvb->lvb_mtime, LTIME_S(inode->i_mtime));
245                 lvb->lvb_mtime = LTIME_S(inode->i_mtime);
246         }
247         if (LTIME_S(inode->i_atime) >lvb->lvb_atime|| !increase_only){
248                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
249                        LPU64" -> %lu\n", res->lr_name.name[0],
250                        lvb->lvb_atime, LTIME_S(inode->i_atime));
251                 lvb->lvb_atime = LTIME_S(inode->i_atime);
252         }
253         if (LTIME_S(inode->i_ctime) >lvb->lvb_ctime|| !increase_only){
254                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
255                        LPU64" -> %lu\n", res->lr_name.name[0],
256                        lvb->lvb_ctime, LTIME_S(inode->i_ctime));
257                 lvb->lvb_ctime = LTIME_S(inode->i_ctime);
258         }
259         if (inode->i_blocks > lvb->lvb_blocks || !increase_only) {
260                 CDEBUG(D_DLMTRACE,"res: "LPU64" updating lvb blocks from disk: "
261                        LPU64" -> %llu\n", res->lr_name.name[0],
262                        lvb->lvb_blocks, (unsigned long long)inode->i_blocks);
263                 lvb->lvb_blocks = inode->i_blocks;
264         }
265
266 out:
267         unlock_res(res);
268         if (tmpinode)
269                 iput(tmpinode);
270         return rc;
271 }
272
273 struct ldlm_valblock_ops filter_lvbo = {
274         lvbo_init: filter_lvbo_init,
275         lvbo_update: filter_lvbo_update,
276         lvbo_free: filter_lvbo_free
277 };