1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Object Devices Class Driver
6 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 * These are the only exported functions, they provide some generic
24 * infrastructure for managing object devices
27 #define DEBUG_SUBSYSTEM S_CLASS
29 # define EXPORT_SYMTAB
33 #include <liblustre.h>
35 #include <linux/module.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_idl.h>
42 #include <linux/pagemap.h> /* for PAGE_CACHE_SIZE */
44 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
46 if (ia_valid & ATTR_ATIME) {
47 oa->o_atime = LTIME_S(attr->ia_atime);
48 oa->o_valid |= OBD_MD_FLATIME;
50 if (ia_valid & ATTR_MTIME) {
51 oa->o_mtime = LTIME_S(attr->ia_mtime);
52 oa->o_valid |= OBD_MD_FLMTIME;
54 if (ia_valid & ATTR_CTIME) {
55 oa->o_ctime = LTIME_S(attr->ia_ctime);
56 oa->o_valid |= OBD_MD_FLCTIME;
58 if (ia_valid & ATTR_SIZE) {
59 oa->o_size = attr->ia_size;
60 oa->o_valid |= OBD_MD_FLSIZE;
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;
68 if (ia_valid & ATTR_UID) {
69 oa->o_uid = attr->ia_uid;
70 oa->o_valid |= OBD_MD_FLUID;
72 if (ia_valid & ATTR_GID) {
73 oa->o_gid = attr->ia_gid;
74 oa->o_valid |= OBD_MD_FLGID;
77 EXPORT_SYMBOL(obdo_from_iattr);
79 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
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);
88 if (valid & OBD_MD_FLATIME) {
89 LTIME_S(attr->ia_atime) = oa->o_atime;
90 attr->ia_valid |= ATTR_ATIME;
92 if (valid & OBD_MD_FLMTIME) {
93 LTIME_S(attr->ia_mtime) = oa->o_mtime;
94 attr->ia_valid |= ATTR_MTIME;
96 if (valid & OBD_MD_FLCTIME) {
97 LTIME_S(attr->ia_ctime) = oa->o_ctime;
98 attr->ia_valid |= ATTR_CTIME;
100 if (valid & OBD_MD_FLSIZE) {
101 attr->ia_size = oa->o_size;
102 attr->ia_valid |= ATTR_SIZE;
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;
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;
116 if (valid & OBD_MD_FLUID) {
117 attr->ia_uid = oa->o_uid;
118 attr->ia_valid |= ATTR_UID;
120 if (valid & OBD_MD_FLGID) {
121 attr->ia_gid = oa->o_gid;
122 attr->ia_valid |= ATTR_GID;
124 if (valid & OBD_MD_FLFLAGS) {
125 attr->ia_attr_flags = oa->o_flags;
126 attr->ia_valid |= ATTR_ATTR_FLAG;
129 EXPORT_SYMBOL(iattr_from_obdo);
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)
135 obd_flag newvalid = 0;
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));
142 if (valid & OBD_MD_FLATIME) {
143 dst->o_atime = LTIME_S(src->i_atime);
144 newvalid |= OBD_MD_FLATIME;
146 if (valid & OBD_MD_FLMTIME) {
147 dst->o_mtime = LTIME_S(src->i_mtime);
148 newvalid |= OBD_MD_FLMTIME;
150 if (valid & OBD_MD_FLCTIME) {
151 dst->o_ctime = LTIME_S(src->i_ctime);
152 newvalid |= OBD_MD_FLCTIME;
154 if (valid & OBD_MD_FLSIZE) {
155 dst->o_size = src->i_size;
156 newvalid |= OBD_MD_FLSIZE;
158 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
159 dst->o_blocks = src->i_blocks;
160 newvalid |= OBD_MD_FLBLOCKS;
162 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
163 dst->o_blksize = src->i_blksize;
164 newvalid |= OBD_MD_FLBLKSZ;
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;
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;
174 if (valid & OBD_MD_FLUID) {
175 dst->o_uid = src->i_uid;
176 newvalid |= OBD_MD_FLUID;
178 if (valid & OBD_MD_FLGID) {
179 dst->o_gid = src->i_gid;
180 newvalid |= OBD_MD_FLGID;
182 if (valid & OBD_MD_FLFLAGS) {
183 dst->o_flags = src->i_flags;
184 newvalid |= OBD_MD_FLFLAGS;
186 if (valid & OBD_MD_FLGENER) {
187 dst->o_generation = src->i_generation;
188 newvalid |= OBD_MD_FLGENER;
191 dst->o_valid |= newvalid;
193 EXPORT_SYMBOL(obdo_from_inode);
195 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
197 valid &= src->o_valid;
199 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
200 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
201 src->o_valid, LTIME_S(dst->i_mtime),
202 LTIME_S(dst->i_ctime),
203 (long)src->o_mtime, (long)src->o_ctime);
205 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
206 LTIME_S(dst->i_atime) = src->o_atime;
207 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
208 LTIME_S(dst->i_mtime) = src->o_mtime;
209 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
210 LTIME_S(dst->i_ctime) = src->o_ctime;
211 if (valid & OBD_MD_FLSIZE)
212 dst->i_size = src->o_size;
213 /* optimum IO size */
214 if (valid & OBD_MD_FLBLKSZ && src->o_blksize > dst->i_blksize)
215 dst->i_blksize = src->o_blksize;
216 if (dst->i_blksize < PAGE_CACHE_SIZE)
217 dst->i_blksize = PAGE_CACHE_SIZE;
218 /* allocation of space */
219 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
220 dst->i_blocks = src->o_blocks;
222 EXPORT_SYMBOL(obdo_refresh_inode);
224 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
226 valid &= src->o_valid;
228 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
229 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
231 LTIME_S(dst->i_mtime), LTIME_S(dst->i_ctime),
232 (long)src->o_mtime, (long)src->o_ctime);
234 if (valid & OBD_MD_FLATIME)
235 LTIME_S(dst->i_atime) = src->o_atime;
236 if (valid & OBD_MD_FLMTIME)
237 LTIME_S(dst->i_mtime) = src->o_mtime;
238 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
239 LTIME_S(dst->i_ctime) = src->o_ctime;
240 if (valid & OBD_MD_FLSIZE)
241 dst->i_size = src->o_size;
242 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
243 dst->i_blocks = src->o_blocks;
244 if (valid & OBD_MD_FLBLKSZ)
245 dst->i_blksize = src->o_blksize;
246 if (valid & OBD_MD_FLTYPE)
247 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
248 if (valid & OBD_MD_FLMODE)
249 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
250 if (valid & OBD_MD_FLUID)
251 dst->i_uid = src->o_uid;
252 if (valid & OBD_MD_FLGID)
253 dst->i_gid = src->o_gid;
254 if (valid & OBD_MD_FLFLAGS)
255 dst->i_flags = src->o_flags;
256 if (valid & OBD_MD_FLGENER)
257 dst->i_generation = src->o_generation;
259 EXPORT_SYMBOL(obdo_to_inode);
262 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
265 CDEBUG(D_INODE, "src obdo "LPX64" valid 0x%x, dst obdo "LPX64"\n",
266 src->o_id, src->o_valid, dst->o_id);
268 if (valid & OBD_MD_FLATIME)
269 dst->o_atime = src->o_atime;
270 if (valid & OBD_MD_FLMTIME)
271 dst->o_mtime = src->o_mtime;
272 if (valid & OBD_MD_FLCTIME)
273 dst->o_ctime = src->o_ctime;
274 if (valid & OBD_MD_FLSIZE)
275 dst->o_size = src->o_size;
276 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
277 dst->o_blocks = src->o_blocks;
278 if (valid & OBD_MD_FLBLKSZ)
279 dst->o_blksize = src->o_blksize;
280 if (valid & OBD_MD_FLTYPE)
281 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
282 if (valid & OBD_MD_FLMODE)
283 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
284 if (valid & OBD_MD_FLUID)
285 dst->o_uid = src->o_uid;
286 if (valid & OBD_MD_FLGID)
287 dst->o_gid = src->o_gid;
288 if (valid & OBD_MD_FLFLAGS)
289 dst->o_flags = src->o_flags;
290 if (valid & OBD_MD_FLGENER)
291 dst->o_generation = src->o_generation;
292 if (valid & OBD_MD_FLINLINE &&
293 src->o_obdflags & OBD_FL_INLINEDATA) {
294 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
295 dst->o_obdflags |= OBD_FL_INLINEDATA;
298 dst->o_valid |= valid;
300 EXPORT_SYMBOL(obdo_cpy_md);
302 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
303 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
307 if ( compare & OBD_MD_FLATIME )
308 res = (res || (dst->o_atime != src->o_atime));
309 if ( compare & OBD_MD_FLMTIME )
310 res = (res || (dst->o_mtime != src->o_mtime));
311 if ( compare & OBD_MD_FLCTIME )
312 res = (res || (dst->o_ctime != src->o_ctime));
313 if ( compare & OBD_MD_FLSIZE )
314 res = (res || (dst->o_size != src->o_size));
315 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
316 res = (res || (dst->o_blocks != src->o_blocks));
317 if ( compare & OBD_MD_FLBLKSZ )
318 res = (res || (dst->o_blksize != src->o_blksize));
319 if ( compare & OBD_MD_FLTYPE )
320 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
321 if ( compare & OBD_MD_FLMODE )
322 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
323 if ( compare & OBD_MD_FLUID )
324 res = (res || (dst->o_uid != src->o_uid));
325 if ( compare & OBD_MD_FLGID )
326 res = (res || (dst->o_gid != src->o_gid));
327 if ( compare & OBD_MD_FLFLAGS )
328 res = (res || (dst->o_flags != src->o_flags));
329 if ( compare & OBD_MD_FLNLINK )
330 res = (res || (dst->o_nlink != src->o_nlink));
331 if ( compare & OBD_MD_FLGENER )
332 res = (res || (dst->o_generation != src->o_generation));
333 /* XXX Don't know if thses should be included here - wasn't previously
334 if ( compare & OBD_MD_FLINLINE )
335 res = (res || memcmp(dst->o_inline, src->o_inline));
339 EXPORT_SYMBOL(obdo_cmp_md);
341 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
343 ioobj->ioo_id = oa->o_id;
344 if (oa->o_valid & OBD_MD_FLGROUP)
345 ioobj->ioo_gr = oa->o_gr;
348 ioobj->ioo_type = oa->o_mode;
350 EXPORT_SYMBOL(obdo_to_ioobj);