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