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