Whamcloud - gitweb
Branch: 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 int lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
92                       obd_off size)
93 {
94         struct lov_oinfo *loi;
95         int stripe = 0;
96         __u64 kms;
97         ENTRY;
98
99         if (size > 0)
100                 stripe = lov_stripe_number(lsm, size - 1);
101         kms = lov_size_to_stripe(lsm, size, stripe);
102         loi = &(lsm->lsm_oinfo[stripe]);
103
104         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
105                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
106         if (kms > loi->loi_kms)
107                 loi->loi_kms = kms;
108
109         RETURN(0);
110 }
111 EXPORT_SYMBOL(lov_increase_kms);
112
113 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
114                      struct lov_stripe_md *lsm, int stripeno, int *set)
115 {
116         valid &= src->o_valid;
117
118         if (*set) {
119                 if (valid & OBD_MD_FLSIZE) {
120                         /* this handles sparse files properly */
121                         obd_size lov_size;
122
123                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
124                         if (lov_size > tgt->o_size)
125                                 tgt->o_size = lov_size;
126                 }
127                 if (valid & OBD_MD_FLBLOCKS)
128                         tgt->o_blocks += src->o_blocks;
129                 if (valid & OBD_MD_FLBLKSZ)
130                         tgt->o_blksize += src->o_blksize;
131                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
132                         tgt->o_ctime = src->o_ctime;
133                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
134                         tgt->o_mtime = src->o_mtime;
135         } else {
136                 memcpy(tgt, src, sizeof(*tgt));
137                 tgt->o_id = lsm->lsm_object_id;
138                 if (valid & OBD_MD_FLSIZE)
139                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
140                 *set = 1;
141         }
142 }