Whamcloud - gitweb
- update from parent
[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 the lock value block(&lvb) attributes from each of the stripes in a
42  * file into a single lvb. It is expected that the caller initializes the
43  * current atime, mtime, ctime to avoid regressing a more uptodate time on 
44  * the local client.
45  *
46  * If @kms_only is set then we do not consider the recently seen size (rss)
47  * when updating the known minimum size (kms).  Even when merging RSS, we will
48  * take the KMS value if it's larger.  This prevents getattr from stomping on
49  * dirty cached pages which extend the file size. */
50 int lov_merge_lvb(struct obd_export *exp, struct lov_stripe_md *lsm,
51                   struct ost_lvb *lvb, int kms_only)
52 {
53         struct lov_oinfo *loi;
54         __u64 size = 0;
55         __u64 blocks = 0;
56         __u64 current_mtime = lvb->lvb_mtime;
57         __u64 current_atime = lvb->lvb_atime;
58         __u64 current_ctime = lvb->lvb_ctime;
59         int i;
60
61         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
62 #ifdef __KERNEL__
63         LASSERT(lsm->lsm_lock_owner == current);
64 #endif
65
66         for (i = 0, loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count;
67              i++, loi++) {
68                 obd_size lov_size, tmpsize;
69
70                 tmpsize = loi->loi_kms;
71                 if (kms_only == 0 && loi->loi_lvb.lvb_size > tmpsize)
72                         tmpsize = loi->loi_lvb.lvb_size;
73
74                 lov_size = lov_stripe_size(lsm, tmpsize, i);
75                 if (lov_size > size)
76                         size = lov_size;
77                 /* merge blocks, mtime, atime */ 
78                 blocks += loi->loi_lvb.lvb_blocks;
79                 if (loi->loi_lvb.lvb_mtime > current_mtime)
80                         current_mtime = loi->loi_lvb.lvb_mtime;
81                 if (loi->loi_lvb.lvb_atime > current_atime)
82                         current_atime = loi->loi_lvb.lvb_atime;
83                 if (loi->loi_lvb.lvb_ctime > current_ctime)
84                         current_ctime = loi->loi_lvb.lvb_ctime;
85         }
86
87         lvb->lvb_size = size;
88         lvb->lvb_blocks = blocks;
89         lvb->lvb_mtime = current_mtime; 
90         lvb->lvb_atime = current_atime; 
91         lvb->lvb_ctime = current_ctime; 
92         RETURN(0);
93 }
94
95 /* Must be called under the lov_stripe_lock() */
96 int lov_adjust_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
97                    obd_off size, int shrink)
98 {
99         struct lov_oinfo *loi;
100         int stripe = 0;
101         __u64 kms;
102         ENTRY;
103
104         LASSERT_SPIN_LOCKED(&lsm->lsm_lock);
105 #ifdef __KERNEL__
106         LASSERT(lsm->lsm_lock_owner == current);
107 #endif
108
109         if (shrink) {
110                 struct lov_oinfo *loi;
111                 for (loi = lsm->lsm_oinfo; stripe < lsm->lsm_stripe_count;
112                      stripe++, loi++) {
113                         kms = lov_size_to_stripe(lsm, size, stripe);
114                         CDEBUG(D_INODE,
115                                "stripe %d KMS %sing "LPU64"->"LPU64"\n",
116                                stripe, kms > loi->loi_kms ? "increas":"shrink",
117                                loi->loi_kms, kms);
118                         loi->loi_kms = loi->loi_lvb.lvb_size = kms;
119                 }
120                 RETURN(0);
121         }
122
123         if (size > 0)
124                 stripe = lov_stripe_number(lsm, size - 1);
125         kms = lov_size_to_stripe(lsm, size, stripe);
126         loi = &(lsm->lsm_oinfo[stripe]);
127
128         CDEBUG(D_INODE, "stripe %d KMS %sincreasing "LPU64"->"LPU64"\n",
129                stripe, kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms);
130         if (kms > loi->loi_kms)
131                 loi->loi_kms = kms;
132
133         RETURN(0);
134 }
135
136 void lov_merge_attrs(struct obdo *tgt, struct obdo *src, obd_flag valid,
137                      struct lov_stripe_md *lsm, int stripeno, int *set)
138 {
139         valid &= src->o_valid;
140
141         if (*set) {
142                 if (valid & OBD_MD_FLSIZE) {
143                         /* this handles sparse files properly */
144                         obd_size lov_size;
145
146                         lov_size = lov_stripe_size(lsm, src->o_size, stripeno);
147                         if (lov_size > tgt->o_size)
148                                 tgt->o_size = lov_size;
149                 }
150                 if (valid & OBD_MD_FLBLOCKS)
151                         tgt->o_blocks += src->o_blocks;
152                 if (valid & OBD_MD_FLBLKSZ)
153                         tgt->o_blksize += src->o_blksize;
154                 if (valid & OBD_MD_FLCTIME && tgt->o_ctime < src->o_ctime)
155                         tgt->o_ctime = src->o_ctime;
156                 if (valid & OBD_MD_FLMTIME && tgt->o_mtime < src->o_mtime)
157                         tgt->o_mtime = src->o_mtime;
158         } else {
159                 memcpy(tgt, src, sizeof(*tgt));
160                 tgt->o_id = lsm->lsm_object_id;
161                 if (valid & OBD_MD_FLSIZE)
162                         tgt->o_size = lov_stripe_size(lsm,src->o_size,stripeno);
163                 *set = 1;
164         }
165 }