Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LOV
29
30 #ifdef __KERNEL__
31 #include <asm/div64.h>
32 #else
33 #include <liblustre.h>
34 #endif
35
36 #include <linux/obd_class.h>
37 #include <linux/obd_lov.h>
38
39 #include "lov_internal.h"
40
41 /* Merge rss if kms == 0
42  *
43  * Even when merging RSS, we will take the KMS value if it's larger.
44  * This prevents getattr from stomping on dirty cached pages which
45  * extend the file size. */
46 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms)
47 {
48         struct lov_oinfo *loi;
49         __u64 size = 0;
50         int i;
51
52         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
53 #ifdef __KERNEL__
54         LASSERT(lsm->lsm_lock_owner == current);
55 #endif
56
57         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
58              i++, loi++) {
59                 obd_size lov_size, tmpsize;
60
61                 tmpsize = loi->loi_kms;
62                 if (kms == 0 && loi->loi_rss > tmpsize)
63                         tmpsize = loi->loi_rss;
64
65                 lov_size = lov_stripe_size(lsm, tmpsize, i);
66                 if (lov_size > size)
67                         size = lov_size;
68         }
69
70         return size;
71 }
72 EXPORT_SYMBOL(lov_merge_size);
73
74 /* Merge blocks */
75 __u64 lov_merge_blocks(struct lov_stripe_md *lsm)
76 {
77         struct lov_oinfo *loi;
78         __u64 blocks = 0;
79         int i;
80
81         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++, loi++)
82                 blocks += loi->loi_blocks;
83         return blocks;
84 }
85 EXPORT_SYMBOL(lov_merge_blocks);
86
87 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time)
88 {
89         struct lov_oinfo *loi;
90         int i;
91
92         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++, loi++)
93                 if (loi->loi_mtime > current_time)
94                         current_time = loi->loi_mtime;
95         return current_time;
96 }
97 EXPORT_SYMBOL(lov_merge_mtime);
98
99 /* Must be called under the lov_stripe_lock() */
100 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
101                    obd_off size, int shrink)
102 {
103         struct lov_oinfo *loi;
104         int stripe = 0;
105         __u64 kms;
106         ENTRY;
107
108         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
109 #ifdef __KERNEL__
110         LASSERT(lsm->lsm_lock_owner == current);
111 #endif
112
113         if (shrink) {
114                 struct lov_oinfo *loi;
115                 for (loi = lsm->lsm_oinfo; stripe < lsm->lsm_stripe_count;
116                      stripe++, loi++) {
117                         kms = lov_size_to_stripe(lsm, size, stripe);
118                         loi->loi_kms = loi->loi_rss = kms;
119                         CDEBUG(D_INODE,
120                                "stripe %d KMS %sing "LPU64"->"LPU64"\n",
121                                stripe, kms > loi->loi_kms ? "increas":"shrink",
122                                loi->loi_kms, kms);
123                 }
124                 RETURN(0);
125         }
126
127         if (size > 0)
128                 stripe = lov_stripe_number(lsm, size - 1);
129         kms = lov_size_to_stripe(lsm, size, stripe);
130         loi = &(lsm->lsm_oinfo[stripe]);
131
132         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
133                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
134         if (kms > loi->loi_kms)
135                 loi->loi_kms = kms;
136
137         RETURN(0);
138 }
139
140 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
141                      struct lov_stripe_md *lsm, int stripeno, int *set)
142 {
143         valid &= src->o_valid;
144
145         if (*set) {
146                 if (valid & OBD_MD_FLSIZE) {
147                         /* this handles sparse files properly */
148                         obd_size lov_size;
149
150                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
151                         if (lov_size > tgt->o_size)
152                                 tgt->o_size = lov_size;
153                 }
154                 if (valid & OBD_MD_FLBLOCKS)
155                         tgt->o_blocks += src->o_blocks;
156                 if (valid & OBD_MD_FLBLKSZ)
157                         tgt->o_blksize += src->o_blksize;
158                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
159                         tgt->o_ctime = src->o_ctime;
160                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
161                         tgt->o_mtime = src->o_mtime;
162         } else {
163                 memcpy(tgt, src, sizeof(*tgt));
164                 tgt->o_id = lsm->lsm_object_id;
165                 if (valid & OBD_MD_FLSIZE)
166                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
167                 *set = 1;
168         }
169 }