Whamcloud - gitweb
LU-2675 lov: remove liblustre includes
[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 #include <libcfs/libcfs.h>
40
41 #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         obd_time current_mtime = lvb->lvb_mtime;
56         obd_time current_atime = lvb->lvb_atime;
57         obd_time 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="LPU64" m="LPU64
65                " a="LPU64" c="LPU64" b="LPU64"\n", POSTID(&lsm->lsm_oi),
66                lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime, lvb->lvb_ctime,
67                lvb->lvb_blocks);
68         for (i = 0; i < lsm->lsm_stripe_count; i++) {
69                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
70                 obd_size lov_size, tmpsize;
71
72                 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks)) {
73                         rc = OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
74                         continue;
75                 }
76
77                 tmpsize = loi->loi_kms;
78                 lov_size = lov_stripe_size(lsm, tmpsize, i);
79                 if (lov_size > kms)
80                         kms = lov_size;
81
82                 if (loi->loi_lvb.lvb_size > tmpsize)
83                         tmpsize = loi->loi_lvb.lvb_size;
84
85                 lov_size = lov_stripe_size(lsm, tmpsize, i);
86                 if (lov_size > size)
87                         size = lov_size;
88                 /* merge blocks, mtime, atime */
89                 blocks += loi->loi_lvb.lvb_blocks;
90                 if (loi->loi_lvb.lvb_mtime > current_mtime)
91                         current_mtime = loi->loi_lvb.lvb_mtime;
92                 if (loi->loi_lvb.lvb_atime > current_atime)
93                         current_atime = loi->loi_lvb.lvb_atime;
94                 if (loi->loi_lvb.lvb_ctime > current_ctime)
95                         current_ctime = loi->loi_lvb.lvb_ctime;
96
97                 CDEBUG(D_INODE, "MDT ID "DOSTID" on OST[%u]: s="LPU64" m="LPU64
98                        " a="LPU64" c="LPU64" b="LPU64"\n", POSTID(&lsm->lsm_oi),
99                        loi->loi_ost_idx, loi->loi_lvb.lvb_size,
100                        loi->loi_lvb.lvb_mtime, loi->loi_lvb.lvb_atime,
101                        loi->loi_lvb.lvb_ctime, loi->loi_lvb.lvb_blocks);
102         }
103
104         *kms_place = kms;
105         lvb->lvb_size = size;
106         lvb->lvb_blocks = blocks;
107         lvb->lvb_mtime = current_mtime;
108         lvb->lvb_atime = current_atime;
109         lvb->lvb_ctime = current_ctime;
110         RETURN(rc);
111 }
112
113 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_valid valid,
114                      struct lov_stripe_md *lsm, int stripeno, int *set)
115 {
116         valid &= src->o_valid;
117
118         if (*set != 0) {
119                 tgt->o_valid &= valid;
120                 if (valid & OBD_MD_FLSIZE) {
121                         /* this handles sparse files properly */
122                         obd_size lov_size;
123
124                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
125                         if (lov_size > tgt->o_size)
126                                 tgt->o_size = lov_size;
127                 }
128                 if (valid & OBD_MD_FLBLOCKS)
129                         tgt->o_blocks += src->o_blocks;
130                 if (valid & OBD_MD_FLBLKSZ)
131                         tgt->o_blksize += src->o_blksize;
132                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
133                         tgt->o_ctime = src->o_ctime;
134                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
135                         tgt->o_mtime = src->o_mtime;
136                 if (valid & OBD_MD_FLDATAVERSION)
137                         tgt->o_data_version += src->o_data_version;
138
139                 /* handle flags */
140                 if (valid & OBD_MD_FLFLAGS)
141                         tgt->o_flags &= src->o_flags;
142                 else
143                         tgt->o_flags = 0;
144         } else {
145                 memcpy(tgt, src, sizeof(*tgt));
146                 tgt->o_oi = lsm->lsm_oi;
147                 tgt->o_valid = valid;
148                 if (valid & OBD_MD_FLSIZE)
149                         tgt->o_size = lov_stripe_size(lsm, src->o_size,
150                                                       stripeno);
151                 tgt->o_flags = 0;
152                 if (valid & OBD_MD_FLFLAGS)
153                         tgt->o_flags = src->o_flags;
154         }
155
156         /* data_version needs to be valid on all stripes to be correct! */
157         if (!(valid & OBD_MD_FLDATAVERSION))
158                 tgt->o_valid &= ~OBD_MD_FLDATAVERSION;
159
160         *set += 1;
161 }