Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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 the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  * These are the only exported functions, they provide some generic
27  * infrastructure for managing object devices
28  */
29
30 #define DEBUG_SUBSYSTEM S_CLASS
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34
35 #ifndef __KERNEL__
36 #include <liblustre.h>
37 #include <obd.h>
38 #else
39 #include <obd_class.h>
40 #include <lustre/lustre_idl.h>
41 #endif
42
43 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
44 {
45 #ifdef __KERNEL__
46         CLASSERT(sizeof(struct lustre_handle) +
47                  sizeof(struct llog_cookie) <= sizeof(src->o_inline));
48         
49         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
50                src->o_id, src->o_valid, dst->o_id);
51 #endif
52         if (valid & OBD_MD_FLATIME)
53                 dst->o_atime = src->o_atime;
54         if (valid & OBD_MD_FLMTIME)
55                 dst->o_mtime = src->o_mtime;
56         if (valid & OBD_MD_FLCTIME)
57                 dst->o_ctime = src->o_ctime;
58         if (valid & OBD_MD_FLSIZE)
59                 dst->o_size = src->o_size;
60         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
61                 dst->o_blocks = src->o_blocks;
62         if (valid & OBD_MD_FLBLKSZ)
63                 dst->o_blksize = src->o_blksize;
64         if (valid & OBD_MD_FLTYPE)
65                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
66         if (valid & OBD_MD_FLMODE)
67                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
68         if (valid & OBD_MD_FLUID)
69                 dst->o_uid = src->o_uid;
70         if (valid & OBD_MD_FLGID)
71                 dst->o_gid = src->o_gid;
72         if (valid & OBD_MD_FLFLAGS)
73                 dst->o_flags = src->o_flags;
74         if (valid & OBD_MD_FLGENER)
75                 dst->o_generation = src->o_generation;
76         if (valid & OBD_MD_FLINLINE)
77                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
78
79         dst->o_valid |= valid;
80 }
81 EXPORT_SYMBOL(obdo_cpy_md);
82
83 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
84 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
85 {
86         int res = 0;
87
88         if ( compare & OBD_MD_FLATIME )
89                 res = (res || (dst->o_atime != src->o_atime));
90         if ( compare & OBD_MD_FLMTIME )
91                 res = (res || (dst->o_mtime != src->o_mtime));
92         if ( compare & OBD_MD_FLCTIME )
93                 res = (res || (dst->o_ctime != src->o_ctime));
94         if ( compare & OBD_MD_FLSIZE )
95                 res = (res || (dst->o_size != src->o_size));
96         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
97                 res = (res || (dst->o_blocks != src->o_blocks));
98         if ( compare & OBD_MD_FLBLKSZ )
99                 res = (res || (dst->o_blksize != src->o_blksize));
100         if ( compare & OBD_MD_FLTYPE )
101                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
102         if ( compare & OBD_MD_FLMODE )
103                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
104         if ( compare & OBD_MD_FLUID )
105                 res = (res || (dst->o_uid != src->o_uid));
106         if ( compare & OBD_MD_FLGID )
107                 res = (res || (dst->o_gid != src->o_gid));
108         if ( compare & OBD_MD_FLFLAGS )
109                 res = (res || (dst->o_flags != src->o_flags));
110         if ( compare & OBD_MD_FLNLINK )
111                 res = (res || (dst->o_nlink != src->o_nlink));
112         if ( compare & OBD_MD_FLGENER )
113                 res = (res || (dst->o_generation != src->o_generation));
114         /* XXX Don't know if thses should be included here - wasn't previously
115         if ( compare & OBD_MD_FLINLINE )
116                 res = (res || memcmp(dst->o_inline, src->o_inline));
117         */
118         return res;
119 }
120 EXPORT_SYMBOL(obdo_cmp_md);
121
122 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
123 {
124         ioobj->ioo_id = oa->o_id;
125         if (oa->o_valid & OBD_MD_FLGROUP)
126                 ioobj->ioo_gr = oa->o_gr;
127         else 
128                 ioobj->ioo_gr = 0;
129         ioobj->ioo_type = oa->o_mode;
130 }
131 EXPORT_SYMBOL(obdo_to_ioobj);
132
133 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
134 {
135         if (ia_valid & ATTR_ATIME) {
136                 oa->o_atime = LTIME_S(attr->ia_atime);
137                 oa->o_valid |= OBD_MD_FLATIME;
138         }
139         if (ia_valid & ATTR_MTIME) {
140                 oa->o_mtime = LTIME_S(attr->ia_mtime);
141                 oa->o_valid |= OBD_MD_FLMTIME;
142         }
143         if (ia_valid & ATTR_CTIME) {
144                 oa->o_ctime = LTIME_S(attr->ia_ctime);
145                 oa->o_valid |= OBD_MD_FLCTIME;
146         }
147         if (ia_valid & ATTR_SIZE) {
148                 oa->o_size = attr->ia_size;
149                 oa->o_valid |= OBD_MD_FLSIZE;
150         }
151         if (ia_valid & ATTR_MODE) {
152                 oa->o_mode = attr->ia_mode;
153                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
154                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
155                         oa->o_mode &= ~S_ISGID;
156         }
157         if (ia_valid & ATTR_UID) {
158                 oa->o_uid = attr->ia_uid;
159                 oa->o_valid |= OBD_MD_FLUID;
160         }
161         if (ia_valid & ATTR_GID) {
162                 oa->o_gid = attr->ia_gid;
163                 oa->o_valid |= OBD_MD_FLGID;
164         }
165 }
166 EXPORT_SYMBOL(obdo_from_iattr);
167
168 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
169 {
170         valid &= oa->o_valid;
171
172         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
173                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
174                        oa->o_valid, oa->o_mtime, oa->o_ctime);
175
176         attr->ia_valid = 0;
177         if (valid & OBD_MD_FLATIME) {
178                 LTIME_S(attr->ia_atime) = oa->o_atime;
179                 attr->ia_valid |= ATTR_ATIME;
180         }
181         if (valid & OBD_MD_FLMTIME) {
182                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
183                 attr->ia_valid |= ATTR_MTIME;
184         }
185         if (valid & OBD_MD_FLCTIME) {
186                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
187                 attr->ia_valid |= ATTR_CTIME;
188         }
189         if (valid & OBD_MD_FLSIZE) {
190                 attr->ia_size = oa->o_size;
191                 attr->ia_valid |= ATTR_SIZE;
192         }
193 #if 0   /* you shouldn't be able to change a file's type with setattr */
194         if (valid & OBD_MD_FLTYPE) {
195                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
196                 attr->ia_valid |= ATTR_MODE;
197         }
198 #endif
199         if (valid & OBD_MD_FLMODE) {
200                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
201                 attr->ia_valid |= ATTR_MODE;
202                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
203                         attr->ia_mode &= ~S_ISGID;
204         }
205         if (valid & OBD_MD_FLUID) {
206                 attr->ia_uid = oa->o_uid;
207                 attr->ia_valid |= ATTR_UID;
208         }
209         if (valid & OBD_MD_FLGID) {
210                 attr->ia_gid = oa->o_gid;
211                 attr->ia_valid |= ATTR_GID;
212         }
213 }
214 EXPORT_SYMBOL(iattr_from_obdo);
215
216 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
217 {
218         iattr_from_obdo(&op_data->op_attr, oa, valid);
219         if (valid & OBD_MD_FLBLOCKS) {
220                 op_data->op_attr_blocks = oa->o_blocks;
221                 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
222         }
223         if (valid & OBD_MD_FLFLAGS) {
224                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
225                         oa->o_flags;
226                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
227         }
228 }
229 EXPORT_SYMBOL(md_from_obdo);
230
231 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
232                   unsigned int valid)
233 {
234         obdo_from_iattr(oa, &op_data->op_attr, valid);
235         if (valid & ATTR_BLOCKS) {
236                 oa->o_blocks = op_data->op_attr_blocks;
237                 oa->o_valid |= OBD_MD_FLBLOCKS;
238         }
239         if (valid & ATTR_ATTR_FLAG) {
240                 oa->o_flags = 
241                         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
242                 oa->o_valid |= OBD_MD_FLFLAGS;
243         }
244 }
245 EXPORT_SYMBOL(obdo_from_md);