Whamcloud - gitweb
Reverted #974 for now as it causes problems for people.
[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_FLNLINK) {
187                 dst->o_nlink = src->i_nlink;
188                 newvalid |= OBD_MD_FLNLINK;
189         }
190         if (valid & OBD_MD_FLGENER) {
191                 dst->o_generation = src->i_generation;
192                 newvalid |= OBD_MD_FLGENER;
193         }
194         if (valid & OBD_MD_FLRDEV) {
195 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
196                 dst->o_rdev = (__u32)kdev_t_to_nr(src->i_rdev);
197 #else
198                 dst->o_rdev = (__u32)old_decode_dev(src->i_rdev);
199 #endif
200                 newvalid |= OBD_MD_FLRDEV;
201         }
202
203         dst->o_valid |= newvalid;
204 }
205 EXPORT_SYMBOL(obdo_from_inode);
206
207 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
208 {
209         valid &= src->o_valid;
210
211         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
212                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
213                        src->o_valid, LTIME_S(dst->i_mtime), 
214                        LTIME_S(dst->i_ctime),
215                        (long)src->o_mtime, (long)src->o_ctime);
216
217         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
218                 LTIME_S(dst->i_atime) = src->o_atime;
219         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
220                 LTIME_S(dst->i_mtime) = src->o_mtime;
221         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
222                 LTIME_S(dst->i_ctime) = src->o_ctime;
223         if (valid & OBD_MD_FLSIZE) 
224                 dst->i_size = src->o_size;
225         /* optimum IO size */
226         if (valid & OBD_MD_FLBLKSZ && src->o_blksize > dst->i_blksize)
227                 dst->i_blksize = src->o_blksize;
228         if (dst->i_blksize < PAGE_CACHE_SIZE)
229                 dst->i_blksize = PAGE_CACHE_SIZE;
230         /* allocation of space */
231         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
232                 dst->i_blocks = src->o_blocks;
233 }
234 EXPORT_SYMBOL(obdo_refresh_inode);
235
236 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
237 {
238         valid &= src->o_valid;
239
240         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
241                 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
242                        src->o_valid, 
243                        LTIME_S(dst->i_mtime), LTIME_S(dst->i_ctime),
244                        (long)src->o_mtime, (long)src->o_ctime);
245
246         if (valid & OBD_MD_FLATIME)
247                 LTIME_S(dst->i_atime) = src->o_atime;
248         if (valid & OBD_MD_FLMTIME)
249                 LTIME_S(dst->i_mtime) = src->o_mtime;
250         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
251                 LTIME_S(dst->i_ctime) = src->o_ctime;
252         if (valid & OBD_MD_FLSIZE)
253                 dst->i_size = src->o_size;
254         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
255                 dst->i_blocks = src->o_blocks;
256         if (valid & OBD_MD_FLBLKSZ)
257                 dst->i_blksize = src->o_blksize;
258         if (valid & OBD_MD_FLTYPE)
259                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
260         if (valid & OBD_MD_FLMODE)
261                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
262         if (valid & OBD_MD_FLUID)
263                 dst->i_uid = src->o_uid;
264         if (valid & OBD_MD_FLGID)
265                 dst->i_gid = src->o_gid;
266         if (valid & OBD_MD_FLFLAGS)
267                 dst->i_flags = src->o_flags;
268         if (valid & OBD_MD_FLNLINK)
269                 dst->i_nlink = src->o_nlink;
270         if (valid & OBD_MD_FLGENER)
271                 dst->i_generation = src->o_generation;
272         if (valid & OBD_MD_FLRDEV)
273 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
274                 dst->i_rdev = to_kdev_t(src->o_rdev);
275 #else
276                 dst->i_rdev = old_decode_dev(src->o_rdev);
277 #endif
278 }
279 EXPORT_SYMBOL(obdo_to_inode);
280 #endif
281
282 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
283 {
284 #ifdef __KERNEL__
285         CDEBUG(D_INODE, "src obdo "LPX64" valid 0x%x, dst obdo "LPX64"\n",
286                src->o_id, src->o_valid, dst->o_id);
287 #endif
288         if (valid & OBD_MD_FLATIME)
289                 dst->o_atime = src->o_atime;
290         if (valid & OBD_MD_FLMTIME)
291                 dst->o_mtime = src->o_mtime;
292         if (valid & OBD_MD_FLCTIME)
293                 dst->o_ctime = src->o_ctime;
294         if (valid & OBD_MD_FLSIZE)
295                 dst->o_size = src->o_size;
296         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
297                 dst->o_blocks = src->o_blocks;
298         if (valid & OBD_MD_FLBLKSZ)
299                 dst->o_blksize = src->o_blksize;
300         if (valid & OBD_MD_FLTYPE)
301                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
302         if (valid & OBD_MD_FLMODE)
303                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
304         if (valid & OBD_MD_FLUID)
305                 dst->o_uid = src->o_uid;
306         if (valid & OBD_MD_FLGID)
307                 dst->o_gid = src->o_gid;
308         if (valid & OBD_MD_FLFLAGS)
309                 dst->o_flags = src->o_flags;
310         /*
311         if (valid & OBD_MD_FLOBDFLG)
312                 dst->o_obdflags = src->o_obdflags;
313         */
314         if (valid & OBD_MD_FLNLINK)
315                 dst->o_nlink = src->o_nlink;
316         if (valid & OBD_MD_FLGENER)
317                 dst->o_generation = src->o_generation;
318         if (valid & OBD_MD_FLRDEV)
319                 dst->o_rdev = src->o_rdev;
320         if (valid & OBD_MD_FLINLINE &&
321              src->o_obdflags & OBD_FL_INLINEDATA) {
322                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
323                 dst->o_obdflags |= OBD_FL_INLINEDATA;
324         }
325
326         dst->o_valid |= valid;
327 }
328 EXPORT_SYMBOL(obdo_cpy_md);
329
330 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
331 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
332 {
333         int res = 0;
334
335         if ( compare & OBD_MD_FLATIME )
336                 res = (res || (dst->o_atime != src->o_atime));
337         if ( compare & OBD_MD_FLMTIME )
338                 res = (res || (dst->o_mtime != src->o_mtime));
339         if ( compare & OBD_MD_FLCTIME )
340                 res = (res || (dst->o_ctime != src->o_ctime));
341         if ( compare & OBD_MD_FLSIZE )
342                 res = (res || (dst->o_size != src->o_size));
343         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
344                 res = (res || (dst->o_blocks != src->o_blocks));
345         if ( compare & OBD_MD_FLBLKSZ )
346                 res = (res || (dst->o_blksize != src->o_blksize));
347         if ( compare & OBD_MD_FLTYPE )
348                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
349         if ( compare & OBD_MD_FLMODE )
350                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
351         if ( compare & OBD_MD_FLUID )
352                 res = (res || (dst->o_uid != src->o_uid));
353         if ( compare & OBD_MD_FLGID )
354                 res = (res || (dst->o_gid != src->o_gid));
355         if ( compare & OBD_MD_FLFLAGS )
356                 res = (res || (dst->o_flags != src->o_flags));
357         if ( compare & OBD_MD_FLNLINK )
358                 res = (res || (dst->o_nlink != src->o_nlink));
359         if ( compare & OBD_MD_FLGENER )
360                 res = (res || (dst->o_generation != src->o_generation));
361         /* XXX Don't know if thses should be included here - wasn't previously
362         if ( compare & OBD_MD_FLINLINE )
363                 res = (res || memcmp(dst->o_inline, src->o_inline));
364         */
365         return res;
366 }
367 EXPORT_SYMBOL(obdo_cmp_md);
368
369 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
370 {
371         ioobj->ioo_id = oa->o_id;
372         if (oa->o_valid & OBD_MD_FLGROUP)
373                 ioobj->ioo_gr = oa->o_gr;
374         else 
375                 ioobj->ioo_gr = 0;
376         ioobj->ioo_type = oa->o_mode;
377 }
378 EXPORT_SYMBOL(obdo_to_ioobj);