4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdfilter/filter_lvb.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_FILTER
45 #include <linux/module.h>
46 #include <linux/version.h>
48 #include <libcfs/list.h>
49 #include <obd_class.h>
50 #include <lustre_dlm.h>
52 #include "filter_internal.h"
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;
61 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
66 /* Called with res->lr_lvb_mutex held */
67 static int filter_lvbo_init(struct ldlm_resource *res)
69 struct ost_lvb *lvb = NULL;
70 struct obd_device *obd;
71 struct dentry *dentry;
76 LASSERT_MUTEX_LOCKED(&res->lr_lvb_mutex);
81 OBD_ALLOC(lvb, sizeof(*lvb));
85 res->lr_lvb_data = lvb;
86 res->lr_lvb_len = sizeof(*lvb);
88 obd = ldlm_res_to_ns(res)->ns_lvbp;
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]);
95 dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
96 res->lr_name.name[0]);
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);
104 if (dentry->d_inode == NULL)
105 /* This is always true for test_brw */
106 GOTO(out_dentry, rc = -ENOENT);
108 inode_init_lvb(dentry->d_inode, lvb);
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);
115 res->lr_lvb_inode = igrab(dentry->d_inode);
122 OST_LVB_SET_ERR(lvb->lvb_blocks, rc);
123 /* Don't free lvb data on lookup error */
127 /* This will be called in two ways:
129 * r != NULL : called by the DLM itself after a glimpse callback
130 * r == NULL : called by the filter after a disk write
132 * If 'increase_only' is true, don't allow values to move backwards.
134 static int filter_lvbo_update(struct ldlm_resource *res,
135 struct ptlrpc_request *r, int increase_only)
139 struct obd_device *obd;
141 struct inode *tmpinode = NULL;
147 lvb = res->lr_lvb_data;
149 CERROR("No lvb when running lvbo_update!\n");
153 /* Update the LVB from the network message */
157 /* XXX update always from reply buffer */
158 new = req_capsule_server_get(&r->rq_pill, &RMF_DLM_LVB);
161 CERROR("lustre_swab_buf failed\n");
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;
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;
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;
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;
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;
197 /* Update the LVB from the disk inode */
198 obd = ldlm_res_to_ns(res)->ns_lvbp;
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;
208 dentry = filter_fid2dentry(obd, NULL, res->lr_name.name[1],
209 res->lr_name.name[0]);
211 RETURN(PTR_ERR(dentry));
214 tmpinode = igrab(dentry->d_inode);
216 /* tmpinode could be NULL, but it does not matter if other
217 * have set res->lr_lvb_inode */
219 if (res->lr_lvb_inode == NULL) {
220 res->lr_lvb_inode = tmpinode;
223 inode = res->lr_lvb_inode;
226 if (!inode || !inode->i_nlink)
227 GOTO(out, rc = -ENOENT);
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);
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);
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);
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);
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;
268 static int filter_lvbo_size(struct ldlm_lock *unused)
270 return sizeof(struct ost_lvb);
273 static int filter_lvbo_fill(struct ldlm_lock *lock,
274 void *buf, int buflen)
276 struct ldlm_resource *res = lock->l_resource;
279 LASSERTF(buflen >= res->lr_lvb_len,
280 "actual %d, want %d\n", buflen, res->lr_lvb_len);
281 memcpy(buf, res->lr_lvb_data, res->lr_lvb_len);
284 return res->lr_lvb_len;
287 struct ldlm_valblock_ops filter_lvbo = {
288 lvbo_init: filter_lvbo_init,
289 lvbo_update: filter_lvbo_update,
290 lvbo_free: filter_lvbo_free,
291 lvbo_size: filter_lvbo_size,
292 lvbo_fill: filter_lvbo_fill