Whamcloud - gitweb
b=16098
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LOV
41
42 #ifdef __KERNEL__
43 #include <libcfs/libcfs.h>
44 #else
45 #include <liblustre.h>
46 #endif
47
48 #include <obd_class.h>
49 #include <obd_lov.h>
50
51 #include "lov_internal.h"
52
53 /* Merge the lock value block(&lvb) attributes from each of the stripes in a
54  * file into a single lvb. It is expected that the caller initializes the
55  * current atime, mtime, ctime to avoid regressing a more uptodate time on
56  * the local client.
57  *
58  * If @kms_only is set then we do not consider the recently seen size (rss)
59  * when updating the known minimum size (kms).  Even when merging RSS, we will
60  * take the KMS value if it's larger.  This prevents getattr from stomping on
61  * dirty cached pages which extend the file size. */
62 int lov_merge_lvb(struct obd_export *exp, struct lov_stripe_md *lsm,
63                   struct ost_lvb *lvb, int kms_only)
64 {
65         __u64 size = 0;
66         __u64 blocks = 0;
67         __u64 current_mtime = lvb->lvb_mtime;
68         __u64 current_atime = lvb->lvb_atime;
69         __u64 current_ctime = lvb->lvb_ctime;
70         int i;
71         int rc = 0;
72
73         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
74 #ifdef __KERNEL__
75         LASSERT(lsm->lsm_lock_owner == cfs_curproc_pid());
76 #endif
77
78         for (i = 0; i < lsm->lsm_stripe_count; i++) {
79                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
80                 obd_size lov_size, tmpsize;
81
82                 if (OST_LVB_IS_ERR(loi->loi_lvb.lvb_blocks)) {
83                         rc = OST_LVB_GET_ERR(loi->loi_lvb.lvb_blocks);
84                         continue;
85                 }
86
87                 tmpsize = loi->loi_kms;
88                 if (kms_only == 0 && loi->loi_lvb.lvb_size > tmpsize)
89                         tmpsize = loi->loi_lvb.lvb_size;
90
91                 lov_size = lov_stripe_size(lsm, tmpsize, i);
92                 if (lov_size > size)
93                         size = lov_size;
94                 /* merge blocks, mtime, atime */
95                 blocks += loi->loi_lvb.lvb_blocks;
96                 if (loi->loi_lvb.lvb_atime > current_atime)
97                         current_atime = loi->loi_lvb.lvb_atime;
98
99                 /* mtime is always updated with ctime, but can be set in past.
100                    As write and utime(2) may happen within 1 second, and utime's
101                    mtime has a priority over write's one, leave mtime from mds 
102                    for the same ctimes. */
103                 if (loi->loi_lvb.lvb_ctime > current_ctime) {
104                         current_ctime = loi->loi_lvb.lvb_ctime;
105                         current_mtime = loi->loi_lvb.lvb_mtime;
106                 }
107         }
108
109         lvb->lvb_size = size;
110         lvb->lvb_blocks = blocks;
111         lvb->lvb_mtime = current_mtime;
112         lvb->lvb_atime = current_atime;
113         lvb->lvb_ctime = current_ctime;
114         RETURN(rc);
115 }
116
117 /* Must be called under the lov_stripe_lock() */
118 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
119                    obd_off size, int shrink)
120 {
121         struct lov_oinfo *loi;
122         int stripe = 0;
123         __u64 kms;
124         ENTRY;
125
126         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
127 #ifdef __KERNEL__
128         LASSERT(lsm->lsm_lock_owner == cfs_curproc_pid());
129 #endif
130
131         if (shrink) {
132                 for (; stripe < lsm->lsm_stripe_count; stripe++) {
133                         struct lov_oinfo *loi = lsm->lsm_oinfo[stripe];
134                         kms = lov_size_to_stripe(lsm, size, stripe);
135                         CDEBUG(D_INODE,
136                                "stripe %d KMS %sing "LPU64"->"LPU64"\n",
137                                stripe, kms > loi->loi_kms ? "increas":"shrink",
138                                loi->loi_kms, kms);
139                         loi->loi_kms = loi->loi_lvb.lvb_size = kms;
140                 }
141                 RETURN(0);
142         }
143
144         if (size > 0)
145                 stripe = lov_stripe_number(lsm, size - 1);
146         kms = lov_size_to_stripe(lsm, size, stripe);
147         loi = lsm->lsm_oinfo[stripe];
148
149         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
150                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
151         if (kms > loi->loi_kms)
152                 loi->loi_kms = kms;
153
154         RETURN(0);
155 }
156
157 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
158                      struct lov_stripe_md *lsm, int stripeno, int *set)
159 {
160         valid &= src->o_valid;
161
162         if (*set) {
163                 if (valid & OBD_MD_FLSIZE) {
164                         /* this handles sparse files properly */
165                         obd_size lov_size;
166
167                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
168                         if (lov_size > tgt->o_size)
169                                 tgt->o_size = lov_size;
170                 }
171                 if (valid & OBD_MD_FLBLOCKS)
172                         tgt->o_blocks += src->o_blocks;
173                 if (valid & OBD_MD_FLBLKSZ)
174                         tgt->o_blksize += src->o_blksize;
175                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
176                         tgt->o_ctime = src->o_ctime;
177                 /* Only mtime from OSTs are merged here, as they cannot be set
178                    in past (only MDS's mtime can) do not look at ctime. */
179                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
180                         tgt->o_mtime = src->o_mtime;
181         } else {
182                 memcpy(tgt, src, sizeof(*tgt));
183                 tgt->o_id = lsm->lsm_object_id;
184                 if (valid & OBD_MD_FLSIZE)
185                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
186                 *set = 1;
187         }
188 }