1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/obdo.c
38 * Object Devices Class Driver
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
43 #define DEBUG_SUBSYSTEM S_CLASS
45 # define EXPORT_SYMTAB
49 #include "../liblustre/llite_lib.h"
51 #include <obd_class.h>
52 #include <lustre/lustre_idl.h>
55 static void obdo_set_parent_fid(struct obdo *dst, struct lu_fid *parent)
57 dst->o_parent_oid = fid_oid(parent);
58 dst->o_parent_seq = fid_seq(parent);
59 dst->o_parent_ver = fid_ver(parent);
60 dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
63 /* WARNING: the file systems must take care not to tinker with
64 attributes they don't manage (such as blocks). */
65 void obdo_from_inode(struct obdo *dst, struct inode *src, struct lu_fid *parent,
68 obd_flag newvalid = 0;
70 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
71 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
72 valid, LTIME_S(src->i_mtime),
73 LTIME_S(src->i_ctime));
75 if (valid & OBD_MD_FLATIME) {
76 dst->o_atime = LTIME_S(src->i_atime);
77 newvalid |= OBD_MD_FLATIME;
79 if (valid & OBD_MD_FLMTIME) {
80 dst->o_mtime = LTIME_S(src->i_mtime);
81 newvalid |= OBD_MD_FLMTIME;
83 if (valid & OBD_MD_FLCTIME) {
84 dst->o_ctime = LTIME_S(src->i_ctime);
85 newvalid |= OBD_MD_FLCTIME;
87 if (valid & OBD_MD_FLSIZE) {
88 dst->o_size = src->i_size;
89 newvalid |= OBD_MD_FLSIZE;
91 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
92 dst->o_blocks = src->i_blocks;
93 newvalid |= OBD_MD_FLBLOCKS;
95 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
96 dst->o_blksize = ll_inode_blksize(src);
97 newvalid |= OBD_MD_FLBLKSZ;
99 if (valid & OBD_MD_FLTYPE) {
100 dst->o_mode = (dst->o_mode & S_IALLUGO) |
101 (src->i_mode & S_IFMT);
102 newvalid |= OBD_MD_FLTYPE;
104 if (valid & OBD_MD_FLMODE) {
105 dst->o_mode = (dst->o_mode & S_IFMT) |
106 (src->i_mode & S_IALLUGO);
107 newvalid |= OBD_MD_FLMODE;
109 if (valid & OBD_MD_FLUID) {
110 dst->o_uid = src->i_uid;
111 newvalid |= OBD_MD_FLUID;
113 if (valid & OBD_MD_FLGID) {
114 dst->o_gid = src->i_gid;
115 newvalid |= OBD_MD_FLGID;
117 if (valid & OBD_MD_FLFLAGS) {
118 dst->o_flags = ll_inode_flags(src);
119 newvalid |= OBD_MD_FLFLAGS;
122 obdo_set_parent_fid(dst, parent);
123 dst->o_valid |= newvalid;
125 EXPORT_SYMBOL(obdo_from_inode);
127 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
130 CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
131 src->o_id, src->o_valid, dst->o_id);
133 if (valid & OBD_MD_FLATIME)
134 dst->o_atime = src->o_atime;
135 if (valid & OBD_MD_FLMTIME)
136 dst->o_mtime = src->o_mtime;
137 if (valid & OBD_MD_FLCTIME)
138 dst->o_ctime = src->o_ctime;
139 if (valid & OBD_MD_FLSIZE)
140 dst->o_size = src->o_size;
141 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
142 dst->o_blocks = src->o_blocks;
143 if (valid & OBD_MD_FLBLKSZ)
144 dst->o_blksize = src->o_blksize;
145 if (valid & OBD_MD_FLTYPE)
146 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
147 if (valid & OBD_MD_FLMODE)
148 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
149 if (valid & OBD_MD_FLUID)
150 dst->o_uid = src->o_uid;
151 if (valid & OBD_MD_FLGID)
152 dst->o_gid = src->o_gid;
153 if (valid & OBD_MD_FLFLAGS)
154 dst->o_flags = src->o_flags;
155 if (valid & OBD_MD_FLFID) {
156 dst->o_parent_seq = src->o_parent_seq;
157 dst->o_parent_ver = src->o_parent_ver;
159 if (valid & OBD_MD_FLGENER)
160 dst->o_parent_oid = src->o_parent_oid;
161 if (valid & OBD_MD_FLHANDLE)
162 dst->o_handle = src->o_handle;
163 if (valid & OBD_MD_FLCOOKIE)
164 dst->o_lcookie = src->o_lcookie;
166 dst->o_valid |= valid;
168 EXPORT_SYMBOL(obdo_cpy_md);
170 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
171 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
175 if ( compare & OBD_MD_FLATIME )
176 res = (res || (dst->o_atime != src->o_atime));
177 if ( compare & OBD_MD_FLMTIME )
178 res = (res || (dst->o_mtime != src->o_mtime));
179 if ( compare & OBD_MD_FLCTIME )
180 res = (res || (dst->o_ctime != src->o_ctime));
181 if ( compare & OBD_MD_FLSIZE )
182 res = (res || (dst->o_size != src->o_size));
183 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
184 res = (res || (dst->o_blocks != src->o_blocks));
185 if ( compare & OBD_MD_FLBLKSZ )
186 res = (res || (dst->o_blksize != src->o_blksize));
187 if ( compare & OBD_MD_FLTYPE )
188 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
189 if ( compare & OBD_MD_FLMODE )
190 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
191 if ( compare & OBD_MD_FLUID )
192 res = (res || (dst->o_uid != src->o_uid));
193 if ( compare & OBD_MD_FLGID )
194 res = (res || (dst->o_gid != src->o_gid));
195 if ( compare & OBD_MD_FLFLAGS )
196 res = (res || (dst->o_flags != src->o_flags));
197 if ( compare & OBD_MD_FLNLINK )
198 res = (res || (dst->o_nlink != src->o_nlink));
199 if ( compare & OBD_MD_FLFID ) {
200 res = (res || (dst->o_parent_seq != src->o_parent_seq));
201 res = (res || (dst->o_parent_ver != src->o_parent_ver));
203 if ( compare & OBD_MD_FLGENER )
204 res = (res || (dst->o_parent_oid != src->o_parent_oid));
205 /* XXX Don't know if thses should be included here - wasn't previously
206 if ( compare & OBD_MD_FLINLINE )
207 res = (res || memcmp(dst->o_inline, src->o_inline));
211 EXPORT_SYMBOL(obdo_cmp_md);
213 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
215 ioobj->ioo_id = oa->o_id;
216 if (oa->o_valid & OBD_MD_FLGROUP)
217 ioobj->ioo_seq = oa->o_seq;
220 ioobj->ioo_type = oa->o_mode;
222 EXPORT_SYMBOL(obdo_to_ioobj);
224 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
226 if (ia_valid & ATTR_ATIME) {
227 oa->o_atime = LTIME_S(attr->ia_atime);
228 oa->o_valid |= OBD_MD_FLATIME;
230 if (ia_valid & ATTR_MTIME) {
231 oa->o_mtime = LTIME_S(attr->ia_mtime);
232 oa->o_valid |= OBD_MD_FLMTIME;
234 if (ia_valid & ATTR_CTIME) {
235 oa->o_ctime = LTIME_S(attr->ia_ctime);
236 oa->o_valid |= OBD_MD_FLCTIME;
238 if (ia_valid & ATTR_SIZE) {
239 oa->o_size = attr->ia_size;
240 oa->o_valid |= OBD_MD_FLSIZE;
242 if (ia_valid & ATTR_MODE) {
243 oa->o_mode = attr->ia_mode;
244 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
245 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
246 !cfs_capable(CFS_CAP_FSETID))
247 oa->o_mode &= ~S_ISGID;
249 if (ia_valid & ATTR_UID) {
250 oa->o_uid = attr->ia_uid;
251 oa->o_valid |= OBD_MD_FLUID;
253 if (ia_valid & ATTR_GID) {
254 oa->o_gid = attr->ia_gid;
255 oa->o_valid |= OBD_MD_FLGID;
258 EXPORT_SYMBOL(obdo_from_iattr);
260 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
262 valid &= oa->o_valid;
264 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
265 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
266 oa->o_valid, oa->o_mtime, oa->o_ctime);
269 if (valid & OBD_MD_FLATIME) {
270 LTIME_S(attr->ia_atime) = oa->o_atime;
271 attr->ia_valid |= ATTR_ATIME;
273 if (valid & OBD_MD_FLMTIME) {
274 LTIME_S(attr->ia_mtime) = oa->o_mtime;
275 attr->ia_valid |= ATTR_MTIME;
277 if (valid & OBD_MD_FLCTIME) {
278 LTIME_S(attr->ia_ctime) = oa->o_ctime;
279 attr->ia_valid |= ATTR_CTIME;
281 if (valid & OBD_MD_FLSIZE) {
282 attr->ia_size = oa->o_size;
283 attr->ia_valid |= ATTR_SIZE;
285 #if 0 /* you shouldn't be able to change a file's type with setattr */
286 if (valid & OBD_MD_FLTYPE) {
287 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
288 attr->ia_valid |= ATTR_MODE;
291 if (valid & OBD_MD_FLMODE) {
292 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
293 attr->ia_valid |= ATTR_MODE;
294 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
295 !cfs_capable(CFS_CAP_FSETID))
296 attr->ia_mode &= ~S_ISGID;
298 if (valid & OBD_MD_FLUID) {
299 attr->ia_uid = oa->o_uid;
300 attr->ia_valid |= ATTR_UID;
302 if (valid & OBD_MD_FLGID) {
303 attr->ia_gid = oa->o_gid;
304 attr->ia_valid |= ATTR_GID;
307 EXPORT_SYMBOL(iattr_from_obdo);
309 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
311 iattr_from_obdo(&op_data->op_attr, oa, valid);
312 if (valid & OBD_MD_FLBLOCKS) {
313 op_data->op_attr_blocks = oa->o_blocks;
314 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
316 if (valid & OBD_MD_FLFLAGS) {
317 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
319 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
322 EXPORT_SYMBOL(md_from_obdo);
324 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
327 obdo_from_iattr(oa, &op_data->op_attr, valid);
328 if (valid & ATTR_BLOCKS) {
329 oa->o_blocks = op_data->op_attr_blocks;
330 oa->o_valid |= OBD_MD_FLBLOCKS;
332 if (valid & ATTR_ATTR_FLAG) {
334 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
335 oa->o_valid |= OBD_MD_FLFLAGS;
338 EXPORT_SYMBOL(obdo_from_md);