Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / lov / lov_merge.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LOV
26
27 #ifdef __KERNEL__
28 #include <asm/div64.h>
29 #else
30 #include <liblustre.h>
31 #endif
32
33 #include <linux/obd_class.h>
34 #include <linux/obd_lov.h>
35
36 #include "lov_internal.h"
37
38 /* Merge rss if kms == 0
39  *
40  * Even when merging RSS, we will take the KMS value if it's larger.
41  * This prevents getattr from stomping on dirty cached pages which
42  * extend the file size. */
43 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
44 {
45         struct lov_oinfo *loi;
46         __u64 size = 0;
47         int i;
48
49         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
50              i++, loi++) {
51                 obd_size lov_size, tmpsize;
52
53                 tmpsize = loi->loi_kms;
54                 if (kms == 0 && loi->loi_rss > tmpsize)
55                         tmpsize = loi->loi_rss;
56
57                 lov_size = lov_stripe_size(lsm, tmpsize, i);
58                 if (lov_size > size)
59                         size = lov_size;
60         }
61
62         return size;
63 }
64 EXPORT_SYMBOL(lov_merge_size);
65
66 /* Merge blocks */
67 __u64 lov_merge_blocks(struct lov_stripe_md *lsm)
68 {
69         struct lov_oinfo *loi;
70         __u64 blocks = 0;
71         int i;
72
73         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++, loi++)
74                 blocks += loi->loi_blocks;
75         return blocks;
76 }
77 EXPORT_SYMBOL(lov_merge_blocks);
78
79 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
80 {
81         struct lov_oinfo *loi;
82         int i;
83
84         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++, loi++)
85                 if (loi->loi_mtime > current_time)
86                         current_time = loi->loi_mtime;
87         return current_time;
88 }
89 EXPORT_SYMBOL(lov_merge_mtime);
90
91 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
92                    obd_off size, int shrink)
93 {
94         struct lov_oinfo *loi;
95         int stripe = 0;
96         __u64 kms;
97         ENTRY;
98
99         if (shrink) {
100                 struct lov_oinfo *loi;
101                 int i = 0;
102                 for (loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
103                      i++, loi++) {
104                         kms = lov_size_to_stripe(lsm, size, i);
105                         loi->loi_kms = loi->loi_rss = kms;
106                 }
107                 RETURN(0);
108         }
109
110         if (size > 0)
111                 stripe = lov_stripe_number(lsm, size - 1);
112         kms = lov_size_to_stripe(lsm, size, stripe);
113         loi = &(lsm->lsm_oinfo[stripe]);
114
115         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
116                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
117         if (kms > loi->loi_kms)
118                 loi->loi_kms = kms;
119
120         RETURN(0);
121 }
122 EXPORT_SYMBOL(lov_adjust_kms);
123
124 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flags valid,
125                      struct lov_stripe_md *lsm, int stripeno, int *set)
126 {
127         valid &= src->o_valid;
128
129         if (*set) {
130                 if (valid & OBD_MD_FLSIZE) {
131                         /* this handles sparse files properly */
132                         obd_size lov_size;
133
134                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
135                         if (lov_size > tgt->o_size)
136                                 tgt->o_size = lov_size;
137                 }
138                 if (valid & OBD_MD_FLBLOCKS)
139                         tgt->o_blocks += src->o_blocks;
140                 if (valid & OBD_MD_FLBLKSZ)
141                         tgt->o_blksize += src->o_blksize;
142                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
143                         tgt->o_ctime = src->o_ctime;
144                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
145                         tgt->o_mtime = src->o_mtime;
146         } else {
147                 memcpy(tgt, src, sizeof(*tgt));
148                 tgt->o_id = lsm->lsm_object_id;
149                 if (valid & OBD_MD_FLSIZE)
150                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
151                 *set = 1;
152         }
153 }