Whamcloud - gitweb
lu_context_key: make ->lct_used atomic to avoid races
[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 #else
38 #include <obd_class.h>
39 #include <lustre/lustre_idl.h>
40 #endif
41
42 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
43 {
44 #ifdef __KERNEL__
45         CLASSERT(sizeof(struct lustre_handle) +
46                  sizeof(struct llog_cookie) <= sizeof(src->o_inline));
47         
48         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
49                src->o_id, src->o_valid, dst->o_id);
50 #endif
51         if (valid & OBD_MD_FLATIME)
52                 dst->o_atime = src->o_atime;
53         if (valid & OBD_MD_FLMTIME)
54                 dst->o_mtime = src->o_mtime;
55         if (valid & OBD_MD_FLCTIME)
56                 dst->o_ctime = src->o_ctime;
57         if (valid & OBD_MD_FLSIZE)
58                 dst->o_size = src->o_size;
59         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
60                 dst->o_blocks = src->o_blocks;
61         if (valid & OBD_MD_FLBLKSZ)
62                 dst->o_blksize = src->o_blksize;
63         if (valid & OBD_MD_FLTYPE)
64                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
65         if (valid & OBD_MD_FLMODE)
66                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
67         if (valid & OBD_MD_FLUID)
68                 dst->o_uid = src->o_uid;
69         if (valid & OBD_MD_FLGID)
70                 dst->o_gid = src->o_gid;
71         if (valid & OBD_MD_FLFLAGS)
72                 dst->o_flags = src->o_flags;
73         if (valid & OBD_MD_FLGENER)
74                 dst->o_generation = src->o_generation;
75         if (valid & OBD_MD_FLINLINE)
76                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
77
78         dst->o_valid |= valid;
79 }
80 EXPORT_SYMBOL(obdo_cpy_md);
81
82 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
83 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
84 {
85         int res = 0;
86
87         if ( compare & OBD_MD_FLATIME )
88                 res = (res || (dst->o_atime != src->o_atime));
89         if ( compare & OBD_MD_FLMTIME )
90                 res = (res || (dst->o_mtime != src->o_mtime));
91         if ( compare & OBD_MD_FLCTIME )
92                 res = (res || (dst->o_ctime != src->o_ctime));
93         if ( compare & OBD_MD_FLSIZE )
94                 res = (res || (dst->o_size != src->o_size));
95         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
96                 res = (res || (dst->o_blocks != src->o_blocks));
97         if ( compare & OBD_MD_FLBLKSZ )
98                 res = (res || (dst->o_blksize != src->o_blksize));
99         if ( compare & OBD_MD_FLTYPE )
100                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
101         if ( compare & OBD_MD_FLMODE )
102                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
103         if ( compare & OBD_MD_FLUID )
104                 res = (res || (dst->o_uid != src->o_uid));
105         if ( compare & OBD_MD_FLGID )
106                 res = (res || (dst->o_gid != src->o_gid));
107         if ( compare & OBD_MD_FLFLAGS )
108                 res = (res || (dst->o_flags != src->o_flags));
109         if ( compare & OBD_MD_FLNLINK )
110                 res = (res || (dst->o_nlink != src->o_nlink));
111         if ( compare & OBD_MD_FLGENER )
112                 res = (res || (dst->o_generation != src->o_generation));
113         /* XXX Don't know if thses should be included here - wasn't previously
114         if ( compare & OBD_MD_FLINLINE )
115                 res = (res || memcmp(dst->o_inline, src->o_inline));
116         */
117         return res;
118 }
119 EXPORT_SYMBOL(obdo_cmp_md);
120
121 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
122 {
123         ioobj->ioo_id = oa->o_id;
124         if (oa->o_valid & OBD_MD_FLGROUP)
125                 ioobj->ioo_gr = oa->o_gr;
126         else 
127                 ioobj->ioo_gr = 0;
128         ioobj->ioo_type = oa->o_mode;
129 }
130 EXPORT_SYMBOL(obdo_to_ioobj);
131
132 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
133 {
134         if (ia_valid & ATTR_ATIME) {
135                 oa->o_atime = LTIME_S(attr->ia_atime);
136                 oa->o_valid |= OBD_MD_FLATIME;
137         }
138         if (ia_valid & ATTR_MTIME) {
139                 oa->o_mtime = LTIME_S(attr->ia_mtime);
140                 oa->o_valid |= OBD_MD_FLMTIME;
141         }
142         if (ia_valid & ATTR_CTIME) {
143                 oa->o_ctime = LTIME_S(attr->ia_ctime);
144                 oa->o_valid |= OBD_MD_FLCTIME;
145         }
146         if (ia_valid & ATTR_SIZE) {
147                 oa->o_size = attr->ia_size;
148                 oa->o_valid |= OBD_MD_FLSIZE;
149         }
150         if (ia_valid & ATTR_MODE) {
151                 oa->o_mode = attr->ia_mode;
152                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
153                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
154                         oa->o_mode &= ~S_ISGID;
155         }
156         if (ia_valid & ATTR_UID) {
157                 oa->o_uid = attr->ia_uid;
158                 oa->o_valid |= OBD_MD_FLUID;
159         }
160         if (ia_valid & ATTR_GID) {
161                 oa->o_gid = attr->ia_gid;
162                 oa->o_valid |= OBD_MD_FLGID;
163         }
164 }
165 EXPORT_SYMBOL(obdo_from_iattr);
166
167 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
168 {
169         valid &= oa->o_valid;
170
171         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
172                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
173                        oa->o_valid, oa->o_mtime, oa->o_ctime);
174
175         attr->ia_valid = 0;
176         if (valid & OBD_MD_FLATIME) {
177                 LTIME_S(attr->ia_atime) = oa->o_atime;
178                 attr->ia_valid |= ATTR_ATIME;
179         }
180         if (valid & OBD_MD_FLMTIME) {
181                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
182                 attr->ia_valid |= ATTR_MTIME;
183         }
184         if (valid & OBD_MD_FLCTIME) {
185                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
186                 attr->ia_valid |= ATTR_CTIME;
187         }
188         if (valid & OBD_MD_FLSIZE) {
189                 attr->ia_size = oa->o_size;
190                 attr->ia_valid |= ATTR_SIZE;
191         }
192 #if 0   /* you shouldn't be able to change a file's type with setattr */
193         if (valid & OBD_MD_FLTYPE) {
194                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
195                 attr->ia_valid |= ATTR_MODE;
196         }
197 #endif
198         if (valid & OBD_MD_FLMODE) {
199                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
200                 attr->ia_valid |= ATTR_MODE;
201                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
202                         attr->ia_mode &= ~S_ISGID;
203         }
204         if (valid & OBD_MD_FLUID) {
205                 attr->ia_uid = oa->o_uid;
206                 attr->ia_valid |= ATTR_UID;
207         }
208         if (valid & OBD_MD_FLGID) {
209                 attr->ia_gid = oa->o_gid;
210                 attr->ia_valid |= ATTR_GID;
211         }
212 }
213 EXPORT_SYMBOL(iattr_from_obdo);
214
215 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
216 {
217         iattr_from_obdo(&op_data->attr, oa, valid);
218         if (valid & OBD_MD_FLBLOCKS) {
219                 op_data->attr_blocks = oa->o_blocks;
220                 op_data->attr.ia_valid |= ATTR_BLOCKS;
221         }
222         if (valid & OBD_MD_FLFLAGS) {
223                 ((struct ll_iattr *)&op_data->attr)->ia_attr_flags =
224                         oa->o_flags;
225                 op_data->attr.ia_valid |= ATTR_ATTR_FLAG;
226         }
227 }
228 EXPORT_SYMBOL(md_from_obdo);
229
230 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
231                   unsigned int valid)
232 {
233         obdo_from_iattr(oa, &op_data->attr, valid);
234         if (valid & ATTR_BLOCKS) {
235                 oa->o_blocks = op_data->attr_blocks;
236                 oa->o_valid |= OBD_MD_FLBLOCKS;
237         }
238         if (valid & ATTR_ATTR_FLAG) {
239                 oa->o_flags = 
240                         ((struct ll_iattr *)&op_data->attr)->ia_attr_flags;
241                 oa->o_valid |= OBD_MD_FLFLAGS;
242         }
243 }
244 EXPORT_SYMBOL(obdo_from_md);