Merge branches 'acpi-ec' and 'acpi-button'
[cascardo/linux.git] / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LOV
34
35 #include "../../include/linux/libcfs/libcfs.h"
36
37 #include "../include/obd_class.h"
38 #include "lov_internal.h"
39
40 /** Merge the lock value block(&lvb) attributes and KMS from each of the
41  * stripes in a file into a single lvb. It is expected that the caller
42  * initializes the current atime, mtime, ctime to avoid regressing a more
43  * uptodate time on the local client.
44  */
45 int lov_merge_lvb_kms(struct lov_stripe_md *lsm,
46                       struct ost_lvb *lvb, __u64 *kms_place)
47 {
48         __u64 size = 0;
49         __u64 kms = 0;
50         __u64 blocks = 0;
51         s64 current_mtime = lvb->lvb_mtime;
52         s64 current_atime = lvb->lvb_atime;
53         s64 current_ctime = lvb->lvb_ctime;
54         int i;
55         int rc = 0;
56
57         assert_spin_locked(&lsm->lsm_lock);
58         LASSERT(lsm->lsm_lock_owner == current_pid());
59
60         CDEBUG(D_INODE, "MDT ID "DOSTID" initial value: s=%llu m=%llu a=%llu c=%llu b=%llu\n",
61                POSTID(&lsm->lsm_oi), lvb->lvb_size, lvb->lvb_mtime,
62                lvb->lvb_atime, lvb->lvb_ctime, lvb->lvb_blocks);
63         for (i = 0; i < lsm->lsm_stripe_count; i++) {
64                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
65                 u64 lov_size, tmpsize;
66
67                 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks)) {
68                         rc = OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
69                         continue;
70                 }
71
72                 tmpsize = loi->loi_kms;
73                 lov_size = lov_stripe_size(lsm, tmpsize, i);
74                 if (lov_size > kms)
75                         kms = lov_size;
76
77                 if (loi->loi_lvb.lvb_size > tmpsize)
78                         tmpsize = loi->loi_lvb.lvb_size;
79
80                 lov_size = lov_stripe_size(lsm, tmpsize, i);
81                 if (lov_size > size)
82                         size = lov_size;
83                 /* merge blocks, mtime, atime */
84                 blocks += loi->loi_lvb.lvb_blocks;
85                 if (loi->loi_lvb.lvb_mtime > current_mtime)
86                         current_mtime = loi->loi_lvb.lvb_mtime;
87                 if (loi->loi_lvb.lvb_atime > current_atime)
88                         current_atime = loi->loi_lvb.lvb_atime;
89                 if (loi->loi_lvb.lvb_ctime > current_ctime)
90                         current_ctime = loi->loi_lvb.lvb_ctime;
91
92                 CDEBUG(D_INODE, "MDT ID "DOSTID" on OST[%u]: s=%llu m=%llu a=%llu c=%llu b=%llu\n",
93                        POSTID(&lsm->lsm_oi), loi->loi_ost_idx,
94                        loi->loi_lvb.lvb_size, loi->loi_lvb.lvb_mtime,
95                        loi->loi_lvb.lvb_atime, loi->loi_lvb.lvb_ctime,
96                        loi->loi_lvb.lvb_blocks);
97         }
98
99         *kms_place = kms;
100         lvb->lvb_size = size;
101         lvb->lvb_blocks = blocks;
102         lvb->lvb_mtime = current_mtime;
103         lvb->lvb_atime = current_atime;
104         lvb->lvb_ctime = current_ctime;
105         return rc;
106 }
107
108 /* Must be called under the lov_stripe_lock() */
109 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
110                    u64 size, int shrink)
111 {
112         struct lov_oinfo *loi;
113         int stripe = 0;
114         __u64 kms;
115
116         assert_spin_locked(&lsm->lsm_lock);
117         LASSERT(lsm->lsm_lock_owner == current_pid());
118
119         if (shrink) {
120                 for (; stripe < lsm->lsm_stripe_count; stripe++) {
121                         struct lov_oinfo *loi = lsm->lsm_oinfo[stripe];
122
123                         kms = lov_size_to_stripe(lsm, size, stripe);
124                         CDEBUG(D_INODE,
125                                "stripe %d KMS %sing %llu->%llu\n",
126                                stripe, kms > loi->loi_kms ? "increase":"shrink",
127                                loi->loi_kms, kms);
128                         loi->loi_lvb.lvb_size = kms;
129                         loi_kms_set(loi, loi->loi_lvb.lvb_size);
130                 }
131                 return 0;
132         }
133
134         if (size > 0)
135                 stripe = lov_stripe_number(lsm, size - 1);
136         kms = lov_size_to_stripe(lsm, size, stripe);
137         loi = lsm->lsm_oinfo[stripe];
138
139         CDEBUG(D_INODE, "stripe %d KMS %sincreasing %llu->%llu\n",
140                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
141         if (kms > loi->loi_kms)
142                 loi_kms_set(loi, kms);
143
144         return 0;
145 }
146
147 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, u64 valid,
148                      struct lov_stripe_md *lsm, int stripeno, int *set)
149 {
150         valid &= src->o_valid;
151
152         if (*set) {
153                 tgt->o_valid &= valid;
154                 if (valid & OBD_MD_FLSIZE) {
155                         /* this handles sparse files properly */
156                         u64 lov_size;
157
158                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
159                         if (lov_size > tgt->o_size)
160                                 tgt->o_size = lov_size;
161                 }
162                 if (valid & OBD_MD_FLBLOCKS)
163                         tgt->o_blocks += src->o_blocks;
164                 if (valid & OBD_MD_FLBLKSZ)
165                         tgt->o_blksize += src->o_blksize;
166                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
167                         tgt->o_ctime = src->o_ctime;
168                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
169                         tgt->o_mtime = src->o_mtime;
170                 if (valid & OBD_MD_FLDATAVERSION)
171                         tgt->o_data_version += src->o_data_version;
172
173                 /* handle flags */
174                 if (valid & OBD_MD_FLFLAGS)
175                         tgt->o_flags &= src->o_flags;
176                 else
177                         tgt->o_flags = 0;
178         } else {
179                 memcpy(tgt, src, sizeof(*tgt));
180                 tgt->o_oi = lsm->lsm_oi;
181                 tgt->o_valid = valid;
182                 if (valid & OBD_MD_FLSIZE)
183                         tgt->o_size = lov_stripe_size(lsm, src->o_size,
184                                                       stripeno);
185                 tgt->o_flags = 0;
186                 if (valid & OBD_MD_FLFLAGS)
187                         tgt->o_flags = src->o_flags;
188         }
189
190         /* data_version needs to be valid on all stripes to be correct! */
191         if (!(valid & OBD_MD_FLDATAVERSION))
192                 tgt->o_valid &= ~OBD_MD_FLDATAVERSION;
193
194         *set += 1;
195 }