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_FLNLINK) {
187 dst->o_nlink = src->i_nlink;
188 newvalid |= OBD_MD_FLNLINK;
190 if (valid & OBD_MD_FLGENER) {
191 dst->o_generation = src->i_generation;
192 newvalid |= OBD_MD_FLGENER;
194 if (valid & OBD_MD_FLRDEV) {
195 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
196 dst->o_rdev = (__u32)kdev_t_to_nr(src->i_rdev);
198 dst->o_rdev = (__u32)old_decode_dev(src->i_rdev);
200 newvalid |= OBD_MD_FLRDEV;
203 dst->o_valid |= newvalid;
205 EXPORT_SYMBOL(obdo_from_inode);
207 void obdo_refresh_inode(struct inode *dst, struct obdo *src, obd_flag valid)
209 valid &= src->o_valid;
211 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
212 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
213 src->o_valid, LTIME_S(dst->i_mtime),
214 LTIME_S(dst->i_ctime),
215 (long)src->o_mtime, (long)src->o_ctime);
217 if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
218 LTIME_S(dst->i_atime) = src->o_atime;
219 if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
220 LTIME_S(dst->i_mtime) = src->o_mtime;
221 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
222 LTIME_S(dst->i_ctime) = src->o_ctime;
223 if (valid & OBD_MD_FLSIZE)
224 dst->i_size = src->o_size;
225 /* optimum IO size */
226 if (valid & OBD_MD_FLBLKSZ && src->o_blksize > dst->i_blksize)
227 dst->i_blksize = src->o_blksize;
228 if (dst->i_blksize < PAGE_CACHE_SIZE)
229 dst->i_blksize = PAGE_CACHE_SIZE;
230 /* allocation of space */
231 if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
232 dst->i_blocks = src->o_blocks;
234 EXPORT_SYMBOL(obdo_refresh_inode);
236 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
238 valid &= src->o_valid;
240 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
241 CDEBUG(D_INODE, "valid %x, cur time %lu/%lu, new %lu/%lu\n",
243 LTIME_S(dst->i_mtime), LTIME_S(dst->i_ctime),
244 (long)src->o_mtime, (long)src->o_ctime);
246 if (valid & OBD_MD_FLATIME)
247 LTIME_S(dst->i_atime) = src->o_atime;
248 if (valid & OBD_MD_FLMTIME)
249 LTIME_S(dst->i_mtime) = src->o_mtime;
250 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
251 LTIME_S(dst->i_ctime) = src->o_ctime;
252 if (valid & OBD_MD_FLSIZE)
253 dst->i_size = src->o_size;
254 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
255 dst->i_blocks = src->o_blocks;
256 if (valid & OBD_MD_FLBLKSZ)
257 dst->i_blksize = src->o_blksize;
258 if (valid & OBD_MD_FLTYPE)
259 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
260 if (valid & OBD_MD_FLMODE)
261 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
262 if (valid & OBD_MD_FLUID)
263 dst->i_uid = src->o_uid;
264 if (valid & OBD_MD_FLGID)
265 dst->i_gid = src->o_gid;
266 if (valid & OBD_MD_FLFLAGS)
267 dst->i_flags = src->o_flags;
268 if (valid & OBD_MD_FLNLINK)
269 dst->i_nlink = src->o_nlink;
270 if (valid & OBD_MD_FLGENER)
271 dst->i_generation = src->o_generation;
272 if (valid & OBD_MD_FLRDEV)
273 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
274 dst->i_rdev = to_kdev_t(src->o_rdev);
276 dst->i_rdev = old_decode_dev(src->o_rdev);
279 EXPORT_SYMBOL(obdo_to_inode);
282 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
285 CDEBUG(D_INODE, "src obdo "LPX64" valid 0x%x, dst obdo "LPX64"\n",
286 src->o_id, src->o_valid, dst->o_id);
288 if (valid & OBD_MD_FLATIME)
289 dst->o_atime = src->o_atime;
290 if (valid & OBD_MD_FLMTIME)
291 dst->o_mtime = src->o_mtime;
292 if (valid & OBD_MD_FLCTIME)
293 dst->o_ctime = src->o_ctime;
294 if (valid & OBD_MD_FLSIZE)
295 dst->o_size = src->o_size;
296 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
297 dst->o_blocks = src->o_blocks;
298 if (valid & OBD_MD_FLBLKSZ)
299 dst->o_blksize = src->o_blksize;
300 if (valid & OBD_MD_FLTYPE)
301 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
302 if (valid & OBD_MD_FLMODE)
303 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
304 if (valid & OBD_MD_FLUID)
305 dst->o_uid = src->o_uid;
306 if (valid & OBD_MD_FLGID)
307 dst->o_gid = src->o_gid;
308 if (valid & OBD_MD_FLFLAGS)
309 dst->o_flags = src->o_flags;
311 if (valid & OBD_MD_FLOBDFLG)
312 dst->o_obdflags = src->o_obdflags;
314 if (valid & OBD_MD_FLNLINK)
315 dst->o_nlink = src->o_nlink;
316 if (valid & OBD_MD_FLGENER)
317 dst->o_generation = src->o_generation;
318 if (valid & OBD_MD_FLRDEV)
319 dst->o_rdev = src->o_rdev;
320 if (valid & OBD_MD_FLINLINE &&
321 src->o_obdflags & OBD_FL_INLINEDATA) {
322 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
323 dst->o_obdflags |= OBD_FL_INLINEDATA;
326 dst->o_valid |= valid;
328 EXPORT_SYMBOL(obdo_cpy_md);
330 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
331 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
335 if ( compare & OBD_MD_FLATIME )
336 res = (res || (dst->o_atime != src->o_atime));
337 if ( compare & OBD_MD_FLMTIME )
338 res = (res || (dst->o_mtime != src->o_mtime));
339 if ( compare & OBD_MD_FLCTIME )
340 res = (res || (dst->o_ctime != src->o_ctime));
341 if ( compare & OBD_MD_FLSIZE )
342 res = (res || (dst->o_size != src->o_size));
343 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
344 res = (res || (dst->o_blocks != src->o_blocks));
345 if ( compare & OBD_MD_FLBLKSZ )
346 res = (res || (dst->o_blksize != src->o_blksize));
347 if ( compare & OBD_MD_FLTYPE )
348 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
349 if ( compare & OBD_MD_FLMODE )
350 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
351 if ( compare & OBD_MD_FLUID )
352 res = (res || (dst->o_uid != src->o_uid));
353 if ( compare & OBD_MD_FLGID )
354 res = (res || (dst->o_gid != src->o_gid));
355 if ( compare & OBD_MD_FLFLAGS )
356 res = (res || (dst->o_flags != src->o_flags));
357 if ( compare & OBD_MD_FLNLINK )
358 res = (res || (dst->o_nlink != src->o_nlink));
359 if ( compare & OBD_MD_FLGENER )
360 res = (res || (dst->o_generation != src->o_generation));
361 /* XXX Don't know if thses should be included here - wasn't previously
362 if ( compare & OBD_MD_FLINLINE )
363 res = (res || memcmp(dst->o_inline, src->o_inline));
367 EXPORT_SYMBOL(obdo_cmp_md);
369 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
371 ioobj->ioo_id = oa->o_id;
372 if (oa->o_valid & OBD_MD_FLGROUP)
373 ioobj->ioo_gr = oa->o_gr;
376 ioobj->ioo_type = oa->o_mode;
378 EXPORT_SYMBOL(obdo_to_ioobj);