Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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 #define EXPORT_SYMTAB
29
30 #include <linux/module.h>
31 #include <linux/obd_class.h>
32 #include <linux/lustre_idl.h>
33
34 #ifdef __KERNEL__
35 #include <linux/fs.h>
36
37 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
38 {
39         if (ia_valid & ATTR_ATIME) {
40                 oa->o_atime = LTIME_S(attr->ia_atime);
41                 oa->o_valid |= OBD_MD_FLATIME;
42         }
43         if (ia_valid & ATTR_MTIME) {
44                 oa->o_mtime = LTIME_S(attr->ia_mtime);
45                 oa->o_valid |= OBD_MD_FLMTIME;
46         }
47         if (ia_valid & ATTR_CTIME) {
48                 oa->o_ctime = LTIME_S(attr->ia_ctime);
49                 oa->o_valid |= OBD_MD_FLCTIME;
50         }
51         if (ia_valid & ATTR_SIZE) {
52                 oa->o_size = attr->ia_size;
53                 oa->o_valid |= OBD_MD_FLSIZE;
54         }
55         if (ia_valid & ATTR_MODE) {
56                 oa->o_mode = attr->ia_mode;
57                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
58                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
59                         oa->o_mode &= ~S_ISGID;
60         }
61         if (ia_valid & ATTR_UID) {
62                 oa->o_uid = attr->ia_uid;
63                 oa->o_valid |= OBD_MD_FLUID;
64         }
65         if (ia_valid & ATTR_GID) {
66                 oa->o_gid = attr->ia_gid;
67                 oa->o_valid |= OBD_MD_FLGID;
68         }
69 }
70 EXPORT_SYMBOL(obdo_from_iattr);
71
72 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
73 {
74         valid &= oa->o_valid;
75
76         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
77                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
78                        oa->o_valid, (long)oa->o_mtime, (long)oa->o_ctime);
79
80         attr->ia_valid = 0;
81         if (valid & OBD_MD_FLATIME) {
82                 LTIME_S(attr->ia_atime) = oa->o_atime;
83                 attr->ia_valid |= ATTR_ATIME;
84         }
85         if (valid & OBD_MD_FLMTIME) {
86                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
87                 attr->ia_valid |= ATTR_MTIME;
88         }
89         if (valid & OBD_MD_FLCTIME) {
90                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
91                 attr->ia_valid |= ATTR_CTIME;
92         }
93         if (valid & OBD_MD_FLSIZE) {
94                 attr->ia_size = oa->o_size;
95                 attr->ia_valid |= ATTR_SIZE;
96         }
97 #if 0   /* you shouldn't be able to change a file's type with setattr */
98         if (valid & OBD_MD_FLTYPE) {
99                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
100                 attr->ia_valid |= ATTR_MODE;
101         }
102 #endif
103         if (valid & OBD_MD_FLMODE) {
104                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
105                 attr->ia_valid |= ATTR_MODE;
106                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
107                         attr->ia_mode &= ~S_ISGID;
108         }
109         if (valid & OBD_MD_FLUID) {
110                 attr->ia_uid = oa->o_uid;
111                 attr->ia_valid |= ATTR_UID;
112         }
113         if (valid & OBD_MD_FLGID) {
114                 attr->ia_gid = oa->o_gid;
115                 attr->ia_valid |= ATTR_GID;
116         }
117 }
118 EXPORT_SYMBOL(iattr_from_obdo);
119
120 /* WARNING: the file systems must take care not to tinker with
121    attributes they don't manage (such as blocks). */
122 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
123 {
124         obd_flag newvalid = 0;
125
126         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
127                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
128                        valid, src->i_mtime, src->i_ctime);
129
130         if (valid & OBD_MD_FLATIME) {
131                 dst->o_atime = LTIME_S(src->i_atime);
132                 newvalid |= OBD_MD_FLATIME;
133         }
134         if (valid & OBD_MD_FLMTIME) {
135                 dst->o_mtime = LTIME_S(src->i_mtime);
136                 newvalid |= OBD_MD_FLMTIME;
137         }
138         if (valid & OBD_MD_FLCTIME) {
139                 dst->o_ctime = LTIME_S(src->i_ctime);
140                 newvalid |= OBD_MD_FLCTIME;
141         }
142         if (valid & OBD_MD_FLSIZE) {
143                 dst->o_size = src->i_size;
144                 newvalid |= OBD_MD_FLSIZE;
145         }
146         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
147                 dst->o_blocks = src->i_blocks;
148                 newvalid |= OBD_MD_FLBLOCKS;
149         }
150         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
151                 dst->o_blksize = src->i_blksize;
152                 newvalid |= OBD_MD_FLBLKSZ;
153         }
154         if (valid & OBD_MD_FLTYPE) {
155                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
156                 newvalid |= OBD_MD_FLTYPE;
157         }
158         if (valid & OBD_MD_FLMODE) {
159                 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
160                 newvalid |= OBD_MD_FLMODE;
161         }
162         if (valid & OBD_MD_FLUID) {
163                 dst->o_uid = src->i_uid;
164                 newvalid |= OBD_MD_FLUID;
165         }
166         if (valid & OBD_MD_FLGID) {
167                 dst->o_gid = src->i_gid;
168                 newvalid |= OBD_MD_FLGID;
169         }
170         if (valid & OBD_MD_FLFLAGS) {
171                 dst->o_flags = src->i_flags;
172                 newvalid |= OBD_MD_FLFLAGS;
173         }
174         if (valid & OBD_MD_FLNLINK) {
175                 dst->o_nlink = src->i_nlink;
176                 newvalid |= OBD_MD_FLNLINK;
177         }
178         if (valid & OBD_MD_FLGENER) {
179                 dst->o_generation = src->i_generation;
180                 newvalid |= OBD_MD_FLGENER;
181         }
182         if (valid & OBD_MD_FLRDEV) {
183                 dst->o_rdev = (__u32)kdev_t_to_nr(src->i_rdev);
184                 newvalid |= OBD_MD_FLRDEV;
185         }
186
187         dst->o_valid |= newvalid;
188 }
189 EXPORT_SYMBOL(obdo_from_inode);
190
191 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
192 {
193         valid &= src->o_valid;
194
195         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
196                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
197                        src->o_valid, dst->i_mtime, dst->i_ctime,
198                        (long)src->o_mtime, (long)src->o_ctime);
199
200         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
201                 LTIME_S(dst->i_atime) = src->o_atime;
202         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
203                 LTIME_S(dst->i_mtime) = src->o_mtime;
204         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
205                 LTIME_S(dst->i_ctime) = src->o_ctime;
206         if (valid & OBD_MD_FLSIZE && src->o_size > dst->i_size)
207                 dst->i_size = src->o_size;
208         /* optimum IO size */
209         if (valid & OBD_MD_FLBLKSZ)
210                 dst->i_blksize = src->o_blksize;
211         /* allocation of space */
212         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
213                 dst->i_blocks = src->o_blocks;
214 }
215 EXPORT_SYMBOL(obdo_refresh_inode);
216
217 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
218 {
219         valid &= src->o_valid;
220
221         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
222                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
223                        src->o_valid, dst->i_mtime, dst->i_ctime,
224                        (long)src->o_mtime, (long)src->o_ctime);
225
226         if (valid & OBD_MD_FLATIME)
227                 LTIME_S(dst->i_atime) = src->o_atime;
228         if (valid & OBD_MD_FLMTIME)
229                 LTIME_S(dst->i_mtime) = src->o_mtime;
230         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
231                 LTIME_S(dst->i_ctime) = src->o_ctime;
232         if (valid & OBD_MD_FLSIZE)
233                 dst->i_size = src->o_size;
234         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
235                 dst->i_blocks = src->o_blocks;
236         if (valid & OBD_MD_FLBLKSZ)
237                 dst->i_blksize = src->o_blksize;
238         if (valid & OBD_MD_FLTYPE)
239                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
240         if (valid & OBD_MD_FLMODE)
241                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
242         if (valid & OBD_MD_FLUID)
243                 dst->i_uid = src->o_uid;
244         if (valid & OBD_MD_FLGID)
245                 dst->i_gid = src->o_gid;
246         if (valid & OBD_MD_FLFLAGS)
247                 dst->i_flags = src->o_flags;
248         if (valid & OBD_MD_FLNLINK)
249                 dst->i_nlink = src->o_nlink;
250         if (valid & OBD_MD_FLGENER)
251                 dst->i_generation = src->o_generation;
252         if (valid & OBD_MD_FLRDEV)
253                 dst->i_rdev = to_kdev_t(src->o_rdev);
254 }
255 EXPORT_SYMBOL(obdo_to_inode);
256 #endif
257
258 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
259 {
260 #ifdef __KERNEL__
261         CDEBUG(D_INODE, "src obdo "LPX64" valid 0x%x, dst obdo "LPX64"\n",
262                src->o_id, src->o_valid, dst->o_id);
263 #endif
264         if (valid & OBD_MD_FLATIME)
265                 dst->o_atime = src->o_atime;
266         if (valid & OBD_MD_FLMTIME)
267                 dst->o_mtime = src->o_mtime;
268         if (valid & OBD_MD_FLCTIME)
269                 dst->o_ctime = src->o_ctime;
270         if (valid & OBD_MD_FLSIZE)
271                 dst->o_size = src->o_size;
272         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
273                 dst->o_blocks = src->o_blocks;
274         if (valid & OBD_MD_FLBLKSZ)
275                 dst->o_blksize = src->o_blksize;
276         if (valid & OBD_MD_FLTYPE)
277                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
278         if (valid & OBD_MD_FLMODE)
279                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
280         if (valid & OBD_MD_FLUID)
281                 dst->o_uid = src->o_uid;
282         if (valid & OBD_MD_FLGID)
283                 dst->o_gid = src->o_gid;
284         if (valid & OBD_MD_FLFLAGS)
285                 dst->o_flags = src->o_flags;
286         /*
287         if (valid & OBD_MD_FLOBDFLG)
288                 dst->o_obdflags = src->o_obdflags;
289         */
290         if (valid & OBD_MD_FLNLINK)
291                 dst->o_nlink = src->o_nlink;
292         if (valid & OBD_MD_FLGENER)
293                 dst->o_generation = src->o_generation;
294         if (valid & OBD_MD_FLRDEV)
295                 dst->o_rdev = src->o_rdev;
296         if (valid & OBD_MD_FLINLINE &&
297              src->o_obdflags & OBD_FL_INLINEDATA) {
298                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
299                 dst->o_obdflags |= OBD_FL_INLINEDATA;
300         }
301
302         dst->o_valid |= valid;
303 }
304 EXPORT_SYMBOL(obdo_cpy_md);
305
306 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
307 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
308 {
309         int res = 0;
310
311         if ( compare & OBD_MD_FLATIME )
312                 res = (res || (dst->o_atime != src->o_atime));
313         if ( compare & OBD_MD_FLMTIME )
314                 res = (res || (dst->o_mtime != src->o_mtime));
315         if ( compare & OBD_MD_FLCTIME )
316                 res = (res || (dst->o_ctime != src->o_ctime));
317         if ( compare & OBD_MD_FLSIZE )
318                 res = (res || (dst->o_size != src->o_size));
319         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
320                 res = (res || (dst->o_blocks != src->o_blocks));
321         if ( compare & OBD_MD_FLBLKSZ )
322                 res = (res || (dst->o_blksize != src->o_blksize));
323         if ( compare & OBD_MD_FLTYPE )
324                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
325         if ( compare & OBD_MD_FLMODE )
326                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
327         if ( compare & OBD_MD_FLUID )
328                 res = (res || (dst->o_uid != src->o_uid));
329         if ( compare & OBD_MD_FLGID )
330                 res = (res || (dst->o_gid != src->o_gid));
331         if ( compare & OBD_MD_FLFLAGS )
332                 res = (res || (dst->o_flags != src->o_flags));
333         if ( compare & OBD_MD_FLNLINK )
334                 res = (res || (dst->o_nlink != src->o_nlink));
335         if ( compare & OBD_MD_FLGENER )
336                 res = (res || (dst->o_generation != src->o_generation));
337         /* XXX Don't know if thses should be included here - wasn't previously
338         if ( compare & OBD_MD_FLINLINE )
339                 res = (res || memcmp(dst->o_inline, src->o_inline));
340         */
341         return res;
342 }
343 EXPORT_SYMBOL(obdo_cmp_md);