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