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