Whamcloud - gitweb
Landing b_bug974 onto HEAD (20040213_1538).
[fs/lustre-release.git] / lustre / obdclass / obdo.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Object Devices Class Driver
5  *
6  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * These are the only exported functions, they provide some generic
24  * infrastructure for managing object devices
25  */
26
27 #define DEBUG_SUBSYSTEM S_CLASS
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31
32 #ifndef __KERNEL__
33 #include <liblustre.h>
34 #else
35 #include <linux/module.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_idl.h>
38 #endif
39
40 #ifdef __KERNEL__
41 #include <linux/fs.h>
42 #include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
43
44 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
45 {
46         if (ia_valid & ATTR_ATIME) {
47                 oa->o_atime = LTIME_S(attr->ia_atime);
48                 oa->o_valid |= OBD_MD_FLATIME;
49         }
50         if (ia_valid & ATTR_MTIME) {
51                 oa->o_mtime = LTIME_S(attr->ia_mtime);
52                 oa->o_valid |= OBD_MD_FLMTIME;
53         }
54         if (ia_valid & ATTR_CTIME) {
55                 oa->o_ctime = LTIME_S(attr->ia_ctime);
56                 oa->o_valid |= OBD_MD_FLCTIME;
57         }
58         if (ia_valid & ATTR_SIZE) {
59                 oa->o_size = attr->ia_size;
60                 oa->o_valid |= OBD_MD_FLSIZE;
61         }
62         if (ia_valid & ATTR_MODE) {
63                 oa->o_mode = attr->ia_mode;
64                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
65                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
66                         oa->o_mode &= ~S_ISGID;
67         }
68         if (ia_valid & ATTR_UID) {
69                 oa->o_uid = attr->ia_uid;
70                 oa->o_valid |= OBD_MD_FLUID;
71         }
72         if (ia_valid & ATTR_GID) {
73                 oa->o_gid = attr->ia_gid;
74                 oa->o_valid |= OBD_MD_FLGID;
75         }
76 }
77 EXPORT_SYMBOL(obdo_from_iattr);
78
79 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
80 {
81         valid &= oa->o_valid;
82
83         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
84                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
85                        oa->o_valid, (long)oa->o_mtime, (long)oa->o_ctime);
86
87         attr->ia_valid = 0;
88         if (valid & OBD_MD_FLATIME) {
89                 LTIME_S(attr->ia_atime) = oa->o_atime;
90                 attr->ia_valid |= ATTR_ATIME;
91         }
92         if (valid & OBD_MD_FLMTIME) {
93                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
94                 attr->ia_valid |= ATTR_MTIME;
95         }
96         if (valid & OBD_MD_FLCTIME) {
97                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
98                 attr->ia_valid |= ATTR_CTIME;
99         }
100         if (valid & OBD_MD_FLSIZE) {
101                 attr->ia_size = oa->o_size;
102                 attr->ia_valid |= ATTR_SIZE;
103         }
104 #if 0   /* you shouldn't be able to change a file's type with setattr */
105         if (valid & OBD_MD_FLTYPE) {
106                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
107                 attr->ia_valid |= ATTR_MODE;
108         }
109 #endif
110         if (valid & OBD_MD_FLMODE) {
111                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
112                 attr->ia_valid |= ATTR_MODE;
113                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
114                         attr->ia_mode &= ~S_ISGID;
115         }
116         if (valid & OBD_MD_FLUID) {
117                 attr->ia_uid = oa->o_uid;
118                 attr->ia_valid |= ATTR_UID;
119         }
120         if (valid & OBD_MD_FLGID) {
121                 attr->ia_gid = oa->o_gid;
122                 attr->ia_valid |= ATTR_GID;
123         }
124         if (valid & OBD_MD_FLFLAGS) {
125                 attr->ia_attr_flags = oa->o_flags;
126                 attr->ia_valid |= ATTR_ATTR_FLAG;
127         }
128 }
129 EXPORT_SYMBOL(iattr_from_obdo);
130
131 /* WARNING: the file systems must take care not to tinker with
132    attributes they don't manage (such as blocks). */
133 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
134 {
135         obd_flag newvalid = 0;
136
137         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
138                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
139                        valid, LTIME_S(src->i_mtime), 
140                        LTIME_S(src->i_ctime));
141
142         if (valid & OBD_MD_FLATIME) {
143                 dst->o_atime = LTIME_S(src->i_atime);
144                 newvalid |= OBD_MD_FLATIME;
145         }
146         if (valid & OBD_MD_FLMTIME) {
147                 dst->o_mtime = LTIME_S(src->i_mtime);
148                 newvalid |= OBD_MD_FLMTIME;
149         }
150         if (valid & OBD_MD_FLCTIME) {
151                 dst->o_ctime = LTIME_S(src->i_ctime);
152                 newvalid |= OBD_MD_FLCTIME;
153         }
154         if (valid & OBD_MD_FLSIZE) {
155                 dst->o_size = src->i_size;
156                 newvalid |= OBD_MD_FLSIZE;
157         }
158         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
159                 dst->o_blocks = src->i_blocks;
160                 newvalid |= OBD_MD_FLBLOCKS;
161         }
162         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
163                 dst->o_blksize = src->i_blksize;
164                 newvalid |= OBD_MD_FLBLKSZ;
165         }
166         if (valid & OBD_MD_FLTYPE) {
167                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
168                 newvalid |= OBD_MD_FLTYPE;
169         }
170         if (valid & OBD_MD_FLMODE) {
171                 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
172                 newvalid |= OBD_MD_FLMODE;
173         }
174         if (valid & OBD_MD_FLUID) {
175                 dst->o_uid = src->i_uid;
176                 newvalid |= OBD_MD_FLUID;
177         }
178         if (valid & OBD_MD_FLGID) {
179                 dst->o_gid = src->i_gid;
180                 newvalid |= OBD_MD_FLGID;
181         }
182         if (valid & OBD_MD_FLFLAGS) {
183                 dst->o_flags = src->i_flags;
184                 newvalid |= OBD_MD_FLFLAGS;
185         }
186         if (valid & OBD_MD_FLGENER) {
187                 dst->o_generation = src->i_generation;
188                 newvalid |= OBD_MD_FLGENER;
189         }
190
191         dst->o_valid |= newvalid;
192 }
193 EXPORT_SYMBOL(obdo_from_inode);
194
195 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
196 {
197         valid &= src->o_valid;
198
199         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
200                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
201                        src->o_valid, LTIME_S(dst->i_mtime), 
202                        LTIME_S(dst->i_ctime),
203                        (long)src->o_mtime, (long)src->o_ctime);
204
205         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
206                 LTIME_S(dst->i_atime) = src->o_atime;
207         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
208                 LTIME_S(dst->i_mtime) = src->o_mtime;
209         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
210                 LTIME_S(dst->i_ctime) = src->o_ctime;
211         if (valid & OBD_MD_FLSIZE) 
212                 dst->i_size = src->o_size;
213         /* optimum IO size */
214         if (valid & OBD_MD_FLBLKSZ && src->o_blksize > dst->i_blksize)
215                 dst->i_blksize = src->o_blksize;
216         if (dst->i_blksize < PAGE_CACHE_SIZE)
217                 dst->i_blksize = PAGE_CACHE_SIZE;
218         /* allocation of space */
219         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
220                 dst->i_blocks = src->o_blocks;
221 }
222 EXPORT_SYMBOL(obdo_refresh_inode);
223
224 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
225 {
226         valid &= src->o_valid;
227
228         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
229                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
230                        src->o_valid, 
231                        LTIME_S(dst->i_mtime), LTIME_S(dst->i_ctime),
232                        (long)src->o_mtime, (long)src->o_ctime);
233
234         if (valid & OBD_MD_FLATIME)
235                 LTIME_S(dst->i_atime) = src->o_atime;
236         if (valid & OBD_MD_FLMTIME)
237                 LTIME_S(dst->i_mtime) = src->o_mtime;
238         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
239                 LTIME_S(dst->i_ctime) = src->o_ctime;
240         if (valid & OBD_MD_FLSIZE)
241                 dst->i_size = src->o_size;
242         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
243                 dst->i_blocks = src->o_blocks;
244         if (valid & OBD_MD_FLBLKSZ)
245                 dst->i_blksize = src->o_blksize;
246         if (valid & OBD_MD_FLTYPE)
247                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
248         if (valid & OBD_MD_FLMODE)
249                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
250         if (valid & OBD_MD_FLUID)
251                 dst->i_uid = src->o_uid;
252         if (valid & OBD_MD_FLGID)
253                 dst->i_gid = src->o_gid;
254         if (valid & OBD_MD_FLFLAGS)
255                 dst->i_flags = src->o_flags;
256         if (valid & OBD_MD_FLGENER)
257                 dst->i_generation = src->o_generation;
258 }
259 EXPORT_SYMBOL(obdo_to_inode);
260 #endif
261
262 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
263 {
264 #ifdef __KERNEL__
265         CDEBUG(D_INODE, "src obdo "LPX64" valid 0x%x, dst obdo "LPX64"\n",
266                src->o_id, src->o_valid, dst->o_id);
267 #endif
268         if (valid & OBD_MD_FLATIME)
269                 dst->o_atime = src->o_atime;
270         if (valid & OBD_MD_FLMTIME)
271                 dst->o_mtime = src->o_mtime;
272         if (valid & OBD_MD_FLCTIME)
273                 dst->o_ctime = src->o_ctime;
274         if (valid & OBD_MD_FLSIZE)
275                 dst->o_size = src->o_size;
276         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
277                 dst->o_blocks = src->o_blocks;
278         if (valid & OBD_MD_FLBLKSZ)
279                 dst->o_blksize = src->o_blksize;
280         if (valid & OBD_MD_FLTYPE)
281                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
282         if (valid & OBD_MD_FLMODE)
283                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
284         if (valid & OBD_MD_FLUID)
285                 dst->o_uid = src->o_uid;
286         if (valid & OBD_MD_FLGID)
287                 dst->o_gid = src->o_gid;
288         if (valid & OBD_MD_FLFLAGS)
289                 dst->o_flags = src->o_flags;
290         if (valid & OBD_MD_FLGENER)
291                 dst->o_generation = src->o_generation;
292         if (valid & OBD_MD_FLINLINE)
293                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
294
295         dst->o_valid |= valid;
296 }
297 EXPORT_SYMBOL(obdo_cpy_md);
298
299 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
300 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
301 {
302         int res = 0;
303
304         if ( compare & OBD_MD_FLATIME )
305                 res = (res || (dst->o_atime != src->o_atime));
306         if ( compare & OBD_MD_FLMTIME )
307                 res = (res || (dst->o_mtime != src->o_mtime));
308         if ( compare & OBD_MD_FLCTIME )
309                 res = (res || (dst->o_ctime != src->o_ctime));
310         if ( compare & OBD_MD_FLSIZE )
311                 res = (res || (dst->o_size != src->o_size));
312         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
313                 res = (res || (dst->o_blocks != src->o_blocks));
314         if ( compare & OBD_MD_FLBLKSZ )
315                 res = (res || (dst->o_blksize != src->o_blksize));
316         if ( compare & OBD_MD_FLTYPE )
317                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
318         if ( compare & OBD_MD_FLMODE )
319                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
320         if ( compare & OBD_MD_FLUID )
321                 res = (res || (dst->o_uid != src->o_uid));
322         if ( compare & OBD_MD_FLGID )
323                 res = (res || (dst->o_gid != src->o_gid));
324         if ( compare & OBD_MD_FLFLAGS )
325                 res = (res || (dst->o_flags != src->o_flags));
326         if ( compare & OBD_MD_FLNLINK )
327                 res = (res || (dst->o_nlink != src->o_nlink));
328         if ( compare & OBD_MD_FLGENER )
329                 res = (res || (dst->o_generation != src->o_generation));
330         /* XXX Don't know if thses should be included here - wasn't previously
331         if ( compare & OBD_MD_FLINLINE )
332                 res = (res || memcmp(dst->o_inline, src->o_inline));
333         */
334         return res;
335 }
336 EXPORT_SYMBOL(obdo_cmp_md);
337
338 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
339 {
340         ioobj->ioo_id = oa->o_id;
341         if (oa->o_valid & OBD_MD_FLGROUP)
342                 ioobj->ioo_gr = oa->o_gr;
343         else 
344                 ioobj->ioo_gr = 0;
345         ioobj->ioo_type = oa->o_mode;
346 }
347 EXPORT_SYMBOL(obdo_to_ioobj);