Whamcloud - gitweb
* Compiles after merging b1_4
[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 /* Must be called with the inode's lli_size_sem held. */
92 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
93                    obd_off size, int shrink)
94 {
95         struct lov_oinfo *loi;
96         int stripe = 0;
97         __u64 kms;
98         ENTRY;
99
100         if (shrink) {
101                 struct lov_oinfo *loi;
102                 for (loi = lsm->lsm_oinfo; stripe < lsm->lsm_stripe_count;
103                      stripe++, loi++) {
104                         kms = lov_size_to_stripe(lsm, size, stripe);
105                         loi->loi_kms = loi->loi_rss = kms;
106                         CDEBUG(D_INODE,
107                                "stripe %d KMS %sing "LPU64"->"LPU64"\n",
108                                stripe, kms > loi->loi_kms ? "increas":"shrink",
109                                loi->loi_kms, kms);
110                 }
111                 RETURN(0);
112         }
113
114         if (size > 0)
115                 stripe = lov_stripe_number(lsm, size - 1);
116         kms = lov_size_to_stripe(lsm, size, stripe);
117         loi = &(lsm->lsm_oinfo[stripe]);
118
119         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
120                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
121         if (kms > loi->loi_kms)
122                 loi->loi_kms = kms;
123
124         RETURN(0);
125 }
126
127 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
128                      struct lov_stripe_md *lsm, int stripeno, int *set)
129 {
130         valid &= src->o_valid;
131
132         if (*set) {
133                 if (valid & OBD_MD_FLSIZE) {
134                         /* this handles sparse files properly */
135                         obd_size lov_size;
136
137                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
138                         if (lov_size > tgt->o_size)
139                                 tgt->o_size = lov_size;
140                 }
141                 if (valid & OBD_MD_FLBLOCKS)
142                         tgt->o_blocks += src->o_blocks;
143                 if (valid & OBD_MD_FLBLKSZ)
144                         tgt->o_blksize += src->o_blksize;
145                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
146                         tgt->o_ctime = src->o_ctime;
147                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
148                         tgt->o_mtime = src->o_mtime;
149         } else {
150                 memcpy(tgt, src, sizeof(*tgt));
151                 tgt->o_id = lsm->lsm_object_id;
152                 if (valid & OBD_MD_FLSIZE)
153                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
154                 *set = 1;
155         }
156 }