Whamcloud - gitweb
add enough arguments for the printf format string.
[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 Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_FILTER
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/version.h>
32
33 #include <libcfs/list.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_dlm.h>
36
37 #include "filter_internal.h"
38
39 /* Called with res->lr_lvb_sem held */
40 static int filter_lvbo_init(struct ldlm_resource *res)
41 {
42         int rc = 0;
43         struct ost_lvb *lvb = NULL;
44         struct obd_device *obd;
45         struct dentry *dentry;
46         ENTRY;
47
48         LASSERT(res);
49         LASSERT(down_trylock(&res->lr_lvb_sem) != 0);
50
51         /* we only want lvb's for object resources */
52         /* check for internal locks: these have name[1] != 0 */
53         if (res->lr_name.name[1])
54                 RETURN(0);
55
56         if (res->lr_lvb_data)
57                 GOTO(out, rc = 0);
58
59         OBD_ALLOC(lvb, sizeof(*lvb));
60         if (lvb == NULL)
61                 GOTO(out, rc = -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, 0, res->lr_name.name[0]);
70         if (IS_ERR(dentry))
71                 GOTO(out, rc = PTR_ERR(dentry));
72
73         if (dentry->d_inode == NULL)
74                 GOTO(out_dentry, rc = -ENOENT);
75
76         lvb->lvb_size = dentry->d_inode->i_size;
77         lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
78         lvb->lvb_blocks = dentry->d_inode->i_blocks;
79
80         CDEBUG(D_DLMTRACE, "res: "LPU64" initial lvb size: "LPU64", "
81                "mtime: "LPU64", blocks: "LPU64"\n",
82                res->lr_name.name[0], lvb->lvb_size,
83                lvb->lvb_mtime, lvb->lvb_blocks);
84
85  out_dentry:
86         f_dput(dentry);
87  out:
88         /* Don't free lvb data on lookup error */
89         return rc;
90 }
91
92 /* This will be called in two ways:
93  *
94  *   m != NULL : called by the DLM itself after a glimpse callback
95  *   m == NULL : called by the filter after a disk write
96  *
97  *   If 'increase' is true, don't allow values to move backwards.
98  */
99 static int filter_lvbo_update(struct ldlm_resource *res, struct lustre_msg *m,
100                               int buf_idx, int increase)
101 {
102         int rc = 0;
103         struct ost_lvb *lvb = res->lr_lvb_data;
104         struct obd_device *obd;
105         struct dentry *dentry;
106         ENTRY;
107
108         LASSERT(res);
109
110         /* we only want lvb's for object resources */
111         /* check for internal locks: these have name[1] != 0 */
112         if (res->lr_name.name[1])
113                 RETURN(0);
114
115         down(&res->lr_lvb_sem);
116         if (lvb == NULL) {
117                 CERROR("No lvb when running lvbo_update!\n");
118                 GOTO(out, rc = 0);
119         }
120
121         /* Update the LVB from the network message */
122         if (m != NULL) {
123                 struct ost_lvb *new;
124
125                 new = lustre_swab_buf(m, buf_idx, sizeof(*new),
126                                       lustre_swab_ost_lvb);
127                 if (new == NULL) {
128                         CERROR("lustre_swab_buf failed\n");
129                         //GOTO(out, rc = -EPROTO);
130                         GOTO(out, rc = 0);
131                 }
132                 if (new->lvb_size > lvb->lvb_size || !increase) {
133                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size: "
134                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
135                                lvb->lvb_size, new->lvb_size);
136                         lvb->lvb_size = new->lvb_size;
137                 }
138                 if (new->lvb_mtime > lvb->lvb_mtime || !increase) {
139                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime: "
140                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
141                                lvb->lvb_mtime, new->lvb_mtime);
142                         lvb->lvb_mtime = new->lvb_mtime;
143                 }
144                 if (new->lvb_atime > lvb->lvb_atime || !increase) {
145                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime: "
146                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
147                                lvb->lvb_atime, new->lvb_atime);
148                         lvb->lvb_atime = new->lvb_atime;
149                 }
150                 if (new->lvb_ctime > lvb->lvb_ctime || !increase) {
151                         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime: "
152                                LPU64" -> "LPU64"\n", res->lr_name.name[0],
153                                lvb->lvb_ctime, new->lvb_ctime);
154                         lvb->lvb_ctime = new->lvb_ctime;
155                 }
156         }
157
158         /* Update the LVB from the disk inode */
159         obd = res->lr_namespace->ns_lvbp;
160         LASSERT(obd);
161
162         dentry = filter_fid2dentry(obd, NULL, 0, res->lr_name.name[0]);
163         if (IS_ERR(dentry))
164                 GOTO(out, rc = PTR_ERR(dentry));
165
166         if (dentry->d_inode == NULL)
167                 GOTO(out_dentry, rc = -ENOENT);
168
169         if (dentry->d_inode->i_size > lvb->lvb_size || !increase) {
170                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb size from disk: "
171                        LPU64" -> %llu\n", res->lr_name.name[0],
172                        lvb->lvb_size, dentry->d_inode->i_size);
173                 lvb->lvb_size = dentry->d_inode->i_size;
174         }
175
176         if (LTIME_S(dentry->d_inode->i_mtime) > lvb->lvb_mtime || !increase) {
177                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb mtime from disk: "
178                        LPU64" -> %lu\n", res->lr_name.name[0],
179                        lvb->lvb_mtime, LTIME_S(dentry->d_inode->i_mtime));
180                 lvb->lvb_mtime = LTIME_S(dentry->d_inode->i_mtime);
181         }
182         if (LTIME_S(dentry->d_inode->i_atime) > lvb->lvb_atime || !increase) {
183                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb atime from disk: "
184                        LPU64" -> %lu\n", res->lr_name.name[0],
185                        lvb->lvb_atime, LTIME_S(dentry->d_inode->i_atime));
186                 lvb->lvb_atime = LTIME_S(dentry->d_inode->i_atime);
187         }
188         if (LTIME_S(dentry->d_inode->i_ctime) > lvb->lvb_ctime || !increase) {
189                 CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb ctime from disk: "
190                        LPU64" -> %lu\n", res->lr_name.name[0],
191                        lvb->lvb_ctime, LTIME_S(dentry->d_inode->i_ctime));
192                 lvb->lvb_ctime = LTIME_S(dentry->d_inode->i_ctime);
193         }
194         CDEBUG(D_DLMTRACE, "res: "LPU64" updating lvb blocks from disk: "
195                LPU64" -> %lu\n", res->lr_name.name[0],
196                lvb->lvb_blocks, dentry->d_inode->i_blocks);
197         lvb->lvb_blocks = dentry->d_inode->i_blocks;
198
199 out_dentry:
200         f_dput(dentry);
201
202 out:
203         up(&res->lr_lvb_sem);
204         return rc;
205 }
206
207 struct ldlm_valblock_ops filter_lvbo = {
208         lvbo_init: filter_lvbo_init,
209         lvbo_update: filter_lvbo_update
210 };