Whamcloud - gitweb
Update copyrights on source files changed since 2010-02-15.
[fs/lustre-release.git] / lustre / lov / lov_merge.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) 2005, 2010, Oracle and/or its affiliates. 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
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LOV
41
42 #ifdef __KERNEL__
43 #include <libcfs/libcfs.h>
44 #else
45 #include <liblustre.h>
46 #endif
47
48 #include <obd_class.h>
49 #include <obd_lov.h>
50
51 #include "lov_internal.h"
52
53 /** Merge the lock value block(&lvb) attributes and KMS from each of the
54  * stripes in a file into a single lvb. It is expected that the caller
55  * initializes the current atime, mtime, ctime to avoid regressing a more
56  * uptodate time on the local client.
57  */
58 int lov_merge_lvb_kms(struct lov_stripe_md *lsm,
59                       struct ost_lvb *lvb, __u64 *kms_place)
60 {
61         __u64 size = 0;
62         __u64 kms = 0;
63         __u64 blocks = 0;
64         __u64 current_mtime = lvb->lvb_mtime;
65         __u64 current_atime = lvb->lvb_atime;
66         __u64 current_ctime = lvb->lvb_ctime;
67         int i;
68         int rc = 0;
69
70         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
71 #ifdef __KERNEL__
72         LASSERT(lsm->lsm_lock_owner == cfs_curproc_pid());
73 #endif
74
75         for (i = 0; i < lsm->lsm_stripe_count; i++) {
76                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
77                 obd_size lov_size, tmpsize;
78
79                 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks)) {
80                         rc = OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
81                         continue;
82                 }
83
84                 tmpsize = loi->loi_kms;
85                 lov_size = lov_stripe_size(lsm, tmpsize, i);
86                 if (lov_size > kms)
87                         kms = lov_size;
88
89                 if (loi->loi_lvb.lvb_size > tmpsize)
90                         tmpsize = loi->loi_lvb.lvb_size;
91
92                 lov_size = lov_stripe_size(lsm, tmpsize, i);
93                 if (lov_size > size)
94                         size = lov_size;
95                 /* merge blocks, mtime, atime */
96                 blocks += loi->loi_lvb.lvb_blocks;
97                 if (loi->loi_lvb.lvb_mtime > current_mtime)
98                         current_mtime = loi->loi_lvb.lvb_mtime;
99                 if (loi->loi_lvb.lvb_atime > current_atime)
100                         current_atime = loi->loi_lvb.lvb_atime;
101                 if (loi->loi_lvb.lvb_ctime > current_ctime)
102                         current_ctime = loi->loi_lvb.lvb_ctime;
103         }
104
105         *kms_place = kms;
106         lvb->lvb_size = size;
107         lvb->lvb_blocks = blocks;
108         lvb->lvb_mtime = current_mtime;
109         lvb->lvb_atime = current_atime;
110         lvb->lvb_ctime = current_ctime;
111         RETURN(rc);
112 }
113
114 /** Merge the lock value block(&lvb) attributes from each of the stripes in a
115  * file into a single lvb. It is expected that the caller initializes the
116  * current atime, mtime, ctime to avoid regressing a more uptodate time on
117  * the local client.
118  *
119  * If \a kms_only is set then we do not consider the recently seen size (rss)
120  * when updating the known minimum size (kms).  Even when merging RSS, we will
121  * take the KMS value if it's larger.  This prevents getattr from stomping on
122  * dirty cached pages which extend the file size. */
123 int lov_merge_lvb(struct obd_export *exp,
124                   struct lov_stripe_md *lsm, struct ost_lvb *lvb, int kms_only)
125 {
126         int   rc;
127         __u64 kms;
128
129         ENTRY;
130         rc = lov_merge_lvb_kms(lsm, lvb, &kms);
131         if (kms_only)
132                 lvb->lvb_size = kms;
133         CDEBUG(D_INODE, "merged: "LPU64" "LPU64" "LPU64" "LPU64" "LPU64"\n",
134                lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
135                lvb->lvb_ctime, lvb->lvb_blocks);
136         RETURN(rc);
137 }
138
139 /* Must be called under the lov_stripe_lock() */
140 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
141                    obd_off size, int shrink)
142 {
143         struct lov_oinfo *loi;
144         int stripe = 0;
145         __u64 kms;
146         ENTRY;
147
148         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
149 #ifdef __KERNEL__
150         LASSERT(lsm->lsm_lock_owner == cfs_curproc_pid());
151 #endif
152
153         if (shrink) {
154                 for (; stripe < lsm->lsm_stripe_count; stripe++) {
155                         struct lov_oinfo *loi = lsm->lsm_oinfo[stripe];
156                         kms = lov_size_to_stripe(lsm, size, stripe);
157                         CDEBUG(D_INODE,
158                                "stripe %d KMS %sing "LPU64"->"LPU64"\n",
159                                stripe, kms > loi->loi_kms ? "increas":"shrink",
160                                loi->loi_kms, kms);
161                         loi_kms_set(loi, loi->loi_lvb.lvb_size = kms);
162                 }
163                 RETURN(0);
164         }
165
166         if (size > 0)
167                 stripe = lov_stripe_number(lsm, size - 1);
168         kms = lov_size_to_stripe(lsm, size, stripe);
169         loi = lsm->lsm_oinfo[stripe];
170
171         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
172                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
173         if (kms > loi->loi_kms)
174                 loi_kms_set(loi, kms);
175
176         RETURN(0);
177 }
178
179 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
180                      struct lov_stripe_md *lsm, int stripeno, int *set)
181 {
182         valid &= src->o_valid;
183
184         if (*set) {
185                 if (valid & OBD_MD_FLSIZE) {
186                         /* this handles sparse files properly */
187                         obd_size lov_size;
188
189                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
190                         if (lov_size > tgt->o_size)
191                                 tgt->o_size = lov_size;
192                 }
193                 if (valid & OBD_MD_FLBLOCKS)
194                         tgt->o_blocks += src->o_blocks;
195                 if (valid & OBD_MD_FLBLKSZ)
196                         tgt->o_blksize += src->o_blksize;
197                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
198                         tgt->o_ctime = src->o_ctime;
199                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
200                         tgt->o_mtime = src->o_mtime;
201         } else {
202                 memcpy(tgt, src, sizeof(*tgt));
203                 tgt->o_id = lsm->lsm_object_id;
204                 if (valid & OBD_MD_FLSIZE)
205                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
206         }
207         *set += 1;
208 }