Whamcloud - gitweb
- changes about @flags in m_disconnect(). It should be cohenernt with m_connect(),
[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_valid valid)
80 {
81         valid &= oa->o_valid;
82
83         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
84                 CDEBUG(D_INODE, "valid "LPX64", 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_valid valid)
134 {
135         obd_valid newvalid = 0;
136
137         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
138                 CDEBUG(D_INODE, "valid "LPX64", new time %lu/%lu\n",
139                        valid, LTIME_S(src->i_mtime), LTIME_S(src->i_ctime));
140
141         if (valid & OBD_MD_FLATIME) {
142                 dst->o_atime = LTIME_S(src->i_atime);
143                 newvalid |= OBD_MD_FLATIME;
144         }
145         if (valid & OBD_MD_FLMTIME) {
146                 dst->o_mtime = LTIME_S(src->i_mtime);
147                 newvalid |= OBD_MD_FLMTIME;
148         }
149         if (valid & OBD_MD_FLCTIME) {
150                 dst->o_ctime = LTIME_S(src->i_ctime);
151                 newvalid |= OBD_MD_FLCTIME;
152         }
153         if (valid & OBD_MD_FLSIZE) {
154                 dst->o_size = src->i_size;
155                 newvalid |= OBD_MD_FLSIZE;
156         }
157         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
158                 dst->o_blocks = src->i_blocks;
159                 newvalid |= OBD_MD_FLBLOCKS;
160         }
161         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
162                 dst->o_blksize = src->i_blksize;
163                 newvalid |= OBD_MD_FLBLKSZ;
164         }
165         if (valid & OBD_MD_FLTYPE) {
166                 dst->o_mode = (dst->o_mode & S_IALLUGO)|(src->i_mode & S_IFMT);
167                 newvalid |= OBD_MD_FLTYPE;
168         }
169         if (valid & OBD_MD_FLMODE) {
170                 dst->o_mode = (dst->o_mode & S_IFMT)|(src->i_mode & S_IALLUGO);
171                 newvalid |= OBD_MD_FLMODE;
172         }
173         if (valid & OBD_MD_FLUID) {
174                 dst->o_uid = src->i_uid;
175                 newvalid |= OBD_MD_FLUID;
176         }
177         if (valid & OBD_MD_FLGID) {
178                 dst->o_gid = src->i_gid;
179                 newvalid |= OBD_MD_FLGID;
180         }
181         if (valid & OBD_MD_FLFLAGS) {
182                 dst->o_flags = src->i_flags;
183                 newvalid |= OBD_MD_FLFLAGS;
184         }
185         if (valid & OBD_MD_FLGENER) {
186                 dst->o_generation = src->i_generation;
187                 newvalid |= OBD_MD_FLGENER;
188         }
189
190         dst->o_valid |= newvalid;
191 }
192 EXPORT_SYMBOL(obdo_from_inode);
193
194 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_valid valid)
195 {
196         valid &= src->o_valid;
197
198         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
199                 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
200                        src->o_valid, LTIME_S(dst->i_mtime), 
201                        LTIME_S(dst->i_ctime),
202                        (long)src->o_mtime, (long)src->o_ctime);
203
204         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
205                 LTIME_S(dst->i_atime) = src->o_atime;
206         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
207                 LTIME_S(dst->i_mtime) = src->o_mtime;
208         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
209                 LTIME_S(dst->i_ctime) = src->o_ctime;
210         if (valid & OBD_MD_FLSIZE) 
211                 dst->i_size = src->o_size;
212         /* optimum IO size */
213         if (valid & OBD_MD_FLBLKSZ && src->o_blksize > dst->i_blksize)
214                 dst->i_blksize = src->o_blksize;
215         if (dst->i_blksize < PAGE_CACHE_SIZE)
216                 dst->i_blksize = PAGE_CACHE_SIZE;
217         /* allocation of space */
218         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
219                 dst->i_blocks = src->o_blocks;
220 }
221 EXPORT_SYMBOL(obdo_refresh_inode);
222
223 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_valid valid)
224 {
225         valid &= src->o_valid;
226
227         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
228                 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
229                        src->o_valid, 
230                        LTIME_S(dst->i_mtime), LTIME_S(dst->i_ctime),
231                        (long)src->o_mtime, (long)src->o_ctime);
232
233         if (valid & OBD_MD_FLATIME)
234                 LTIME_S(dst->i_atime) = src->o_atime;
235         if (valid & OBD_MD_FLMTIME)
236                 LTIME_S(dst->i_mtime) = src->o_mtime;
237         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
238                 LTIME_S(dst->i_ctime) = src->o_ctime;
239         if (valid & OBD_MD_FLSIZE)
240                 dst->i_size = src->o_size;
241         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
242                 dst->i_blocks = src->o_blocks;
243         if (valid & OBD_MD_FLBLKSZ)
244                 dst->i_blksize = src->o_blksize;
245         if (valid & OBD_MD_FLTYPE)
246                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
247         if (valid & OBD_MD_FLMODE)
248                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
249         if (valid & OBD_MD_FLUID)
250                 dst->i_uid = src->o_uid;
251         if (valid & OBD_MD_FLGID)
252                 dst->i_gid = src->o_gid;
253         if (valid & OBD_MD_FLFLAGS)
254                 dst->i_flags = src->o_flags;
255         if (valid & OBD_MD_FLGENER)
256                 dst->i_generation = src->o_generation;
257 }
258 EXPORT_SYMBOL(obdo_to_inode);
259 #endif
260
261 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_valid valid)
262 {
263 #ifdef __KERNEL__
264         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
265                src->o_id, src->o_valid, dst->o_id);
266 #endif
267         if (valid & OBD_MD_FLATIME)
268                 dst->o_atime = src->o_atime;
269         if (valid & OBD_MD_FLMTIME)
270                 dst->o_mtime = src->o_mtime;
271         if (valid & OBD_MD_FLCTIME)
272                 dst->o_ctime = src->o_ctime;
273         if (valid & OBD_MD_FLSIZE)
274                 dst->o_size = src->o_size;
275         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
276                 dst->o_blocks = src->o_blocks;
277         if (valid & OBD_MD_FLBLKSZ)
278                 dst->o_blksize = src->o_blksize;
279         if (valid & OBD_MD_FLTYPE)
280                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
281         if (valid & OBD_MD_FLMODE)
282                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
283         if (valid & OBD_MD_FLUID)
284                 dst->o_uid = src->o_uid;
285         if (valid & OBD_MD_FLGID)
286                 dst->o_gid = src->o_gid;
287         if (valid & OBD_MD_FLFLAGS)
288                 dst->o_flags = src->o_flags;
289         if (valid & OBD_MD_FLGENER)
290                 dst->o_generation = src->o_generation;
291         if (valid & OBD_MD_FLINLINE)
292                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
293
294         dst->o_valid |= valid;
295 }
296 EXPORT_SYMBOL(obdo_cpy_md);
297
298 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
299 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_valid compare)
300 {
301         int res = 0;
302
303         if ( compare & OBD_MD_FLATIME )
304                 res = (res || (dst->o_atime != src->o_atime));
305         if ( compare & OBD_MD_FLMTIME )
306                 res = (res || (dst->o_mtime != src->o_mtime));
307         if ( compare & OBD_MD_FLCTIME )
308                 res = (res || (dst->o_ctime != src->o_ctime));
309         if ( compare & OBD_MD_FLSIZE )
310                 res = (res || (dst->o_size != src->o_size));
311         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
312                 res = (res || (dst->o_blocks != src->o_blocks));
313         if ( compare & OBD_MD_FLBLKSZ )
314                 res = (res || (dst->o_blksize != src->o_blksize));
315         if ( compare & OBD_MD_FLTYPE )
316                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
317         if ( compare & OBD_MD_FLMODE )
318                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
319         if ( compare & OBD_MD_FLUID )
320                 res = (res || (dst->o_uid != src->o_uid));
321         if ( compare & OBD_MD_FLGID )
322                 res = (res || (dst->o_gid != src->o_gid));
323         if ( compare & OBD_MD_FLFLAGS )
324                 res = (res || (dst->o_flags != src->o_flags));
325         if ( compare & OBD_MD_FLNLINK )
326                 res = (res || (dst->o_nlink != src->o_nlink));
327         if ( compare & OBD_MD_FLGENER )
328                 res = (res || (dst->o_generation != src->o_generation));
329         /* XXX Don't know if thses should be included here - wasn't previously
330         if ( compare & OBD_MD_FLINLINE )
331                 res = (res || memcmp(dst->o_inline, src->o_inline));
332         */
333         return res;
334 }
335 EXPORT_SYMBOL(obdo_cmp_md);
336
337 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
338 {
339         ioobj->ioo_id = oa->o_id;
340         if (oa->o_valid & OBD_MD_FLGROUP)
341                 ioobj->ioo_gr = oa->o_gr;
342         else 
343                 ioobj->ioo_gr = 0;
344         ioobj->ioo_type = oa->o_mode;
345 }
346 EXPORT_SYMBOL(obdo_to_ioobj);