Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / lustre / 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, 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 #include "../../include/linux/libcfs/libcfs.h"
40
41 #include "../include/obd_class.h"
42 #include "lov_internal.h"
43
44 /** Merge the lock value block(&lvb) attributes and KMS from each of the
45  * stripes in a file into a single lvb. It is expected that the caller
46  * initializes the current atime, mtime, ctime to avoid regressing a more
47  * uptodate time on the local client.
48  */
49 int lov_merge_lvb_kms(struct lov_stripe_md *lsm,
50                       struct ost_lvb *lvb, __u64 *kms_place)
51 {
52         __u64 size = 0;
53         __u64 kms = 0;
54         __u64 blocks = 0;
55         s64 current_mtime = lvb->lvb_mtime;
56         s64 current_atime = lvb->lvb_atime;
57         s64 current_ctime = lvb->lvb_ctime;
58         int i;
59         int rc = 0;
60
61         assert_spin_locked(&lsm->lsm_lock);
62         LASSERT(lsm->lsm_lock_owner == current_pid());
63
64         CDEBUG(D_INODE, "MDT ID "DOSTID" initial value: s=%llu m=%llu a=%llu c=%llu b=%llu\n",
65                POSTID(&lsm->lsm_oi), lvb->lvb_size, lvb->lvb_mtime,
66                lvb->lvb_atime, lvb->lvb_ctime, lvb->lvb_blocks);
67         for (i = 0; i < lsm->lsm_stripe_count; i++) {
68                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
69                 u64 lov_size, tmpsize;
70
71                 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks)) {
72                         rc = OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
73                         continue;
74                 }
75
76                 tmpsize = loi->loi_kms;
77                 lov_size = lov_stripe_size(lsm, tmpsize, i);
78                 if (lov_size > kms)
79                         kms = lov_size;
80
81                 if (loi->loi_lvb.lvb_size > tmpsize)
82                         tmpsize = loi->loi_lvb.lvb_size;
83
84                 lov_size = lov_stripe_size(lsm, tmpsize, i);
85                 if (lov_size > size)
86                         size = lov_size;
87                 /* merge blocks, mtime, atime */
88                 blocks += loi->loi_lvb.lvb_blocks;
89                 if (loi->loi_lvb.lvb_mtime > current_mtime)
90                         current_mtime = loi->loi_lvb.lvb_mtime;
91                 if (loi->loi_lvb.lvb_atime > current_atime)
92                         current_atime = loi->loi_lvb.lvb_atime;
93                 if (loi->loi_lvb.lvb_ctime > current_ctime)
94                         current_ctime = loi->loi_lvb.lvb_ctime;
95
96                 CDEBUG(D_INODE, "MDT ID "DOSTID" on OST[%u]: s=%llu m=%llu a=%llu c=%llu b=%llu\n",
97                        POSTID(&lsm->lsm_oi), loi->loi_ost_idx,
98                        loi->loi_lvb.lvb_size, loi->loi_lvb.lvb_mtime,
99                        loi->loi_lvb.lvb_atime, loi->loi_lvb.lvb_ctime,
100                        loi->loi_lvb.lvb_blocks);
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 /* Must be called under the lov_stripe_lock() */
113 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
114                    u64 size, int shrink)
115 {
116         struct lov_oinfo *loi;
117         int stripe = 0;
118         __u64 kms;
119
120         assert_spin_locked(&lsm->lsm_lock);
121         LASSERT(lsm->lsm_lock_owner == current_pid());
122
123         if (shrink) {
124                 for (; stripe < lsm->lsm_stripe_count; stripe++) {
125                         struct lov_oinfo *loi = lsm->lsm_oinfo[stripe];
126                         kms = lov_size_to_stripe(lsm, size, stripe);
127                         CDEBUG(D_INODE,
128                                "stripe %d KMS %sing %llu->%llu\n",
129                                stripe, kms > loi->loi_kms ? "increase":"shrink",
130                                loi->loi_kms, kms);
131                         loi_kms_set(loi, loi->loi_lvb.lvb_size = kms);
132                 }
133                 return 0;
134         }
135
136         if (size > 0)
137                 stripe = lov_stripe_number(lsm, size - 1);
138         kms = lov_size_to_stripe(lsm, size, stripe);
139         loi = lsm->lsm_oinfo[stripe];
140
141         CDEBUG(D_INODE, "stripe %d KMS %sincreasing %llu->%llu\n",
142                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
143         if (kms > loi->loi_kms)
144                 loi_kms_set(loi, kms);
145
146         return 0;
147 }
148
149 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, u64 valid,
150                      struct lov_stripe_md *lsm, int stripeno, int *set)
151 {
152         valid &= src->o_valid;
153
154         if (*set) {
155                 if (valid & OBD_MD_FLSIZE) {
156                         /* this handles sparse files properly */
157                         u64 lov_size;
158
159                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
160                         if (lov_size > tgt->o_size)
161                                 tgt->o_size = lov_size;
162                 }
163                 if (valid & OBD_MD_FLBLOCKS)
164                         tgt->o_blocks += src->o_blocks;
165                 if (valid & OBD_MD_FLBLKSZ)
166                         tgt->o_blksize += src->o_blksize;
167                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
168                         tgt->o_ctime = src->o_ctime;
169                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
170                         tgt->o_mtime = src->o_mtime;
171                 if (valid & OBD_MD_FLDATAVERSION)
172                         tgt->o_data_version += src->o_data_version;
173         } else {
174                 memcpy(tgt, src, sizeof(*tgt));
175                 tgt->o_oi = lsm->lsm_oi;
176                 if (valid & OBD_MD_FLSIZE)
177                         tgt->o_size = lov_stripe_size(lsm, src->o_size,
178                                                       stripeno);
179         }
180
181         /* data_version needs to be valid on all stripes to be correct! */
182         if (!(valid & OBD_MD_FLDATAVERSION))
183                 tgt->o_valid &= ~OBD_MD_FLDATAVERSION;
184
185         *set += 1;
186 }