4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
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 void obdo_set_parent_fid(struct obdo *dst, const 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;
62 EXPORT_SYMBOL(obdo_set_parent_fid);
64 /* WARNING: the file systems must take care not to tinker with
65 attributes they don't manage (such as blocks). */
66 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
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 = i_size_read(src);
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;
121 dst->o_valid |= newvalid;
123 EXPORT_SYMBOL(obdo_from_inode);
125 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
128 CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
129 src->o_id, src->o_valid, dst->o_id);
131 if (valid & OBD_MD_FLATIME)
132 dst->o_atime = src->o_atime;
133 if (valid & OBD_MD_FLMTIME)
134 dst->o_mtime = src->o_mtime;
135 if (valid & OBD_MD_FLCTIME)
136 dst->o_ctime = src->o_ctime;
137 if (valid & OBD_MD_FLSIZE)
138 dst->o_size = src->o_size;
139 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
140 dst->o_blocks = src->o_blocks;
141 if (valid & OBD_MD_FLBLKSZ)
142 dst->o_blksize = src->o_blksize;
143 if (valid & OBD_MD_FLTYPE)
144 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
145 if (valid & OBD_MD_FLMODE)
146 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
147 if (valid & OBD_MD_FLUID)
148 dst->o_uid = src->o_uid;
149 if (valid & OBD_MD_FLGID)
150 dst->o_gid = src->o_gid;
151 if (valid & OBD_MD_FLFLAGS)
152 dst->o_flags = src->o_flags;
153 if (valid & OBD_MD_FLFID) {
154 dst->o_parent_seq = src->o_parent_seq;
155 dst->o_parent_ver = src->o_parent_ver;
157 if (valid & OBD_MD_FLGENER)
158 dst->o_parent_oid = src->o_parent_oid;
159 if (valid & OBD_MD_FLHANDLE)
160 dst->o_handle = src->o_handle;
161 if (valid & OBD_MD_FLCOOKIE)
162 dst->o_lcookie = src->o_lcookie;
164 dst->o_valid |= valid;
166 EXPORT_SYMBOL(obdo_cpy_md);
168 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
169 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
173 if ( compare & OBD_MD_FLATIME )
174 res = (res || (dst->o_atime != src->o_atime));
175 if ( compare & OBD_MD_FLMTIME )
176 res = (res || (dst->o_mtime != src->o_mtime));
177 if ( compare & OBD_MD_FLCTIME )
178 res = (res || (dst->o_ctime != src->o_ctime));
179 if ( compare & OBD_MD_FLSIZE )
180 res = (res || (dst->o_size != src->o_size));
181 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
182 res = (res || (dst->o_blocks != src->o_blocks));
183 if ( compare & OBD_MD_FLBLKSZ )
184 res = (res || (dst->o_blksize != src->o_blksize));
185 if ( compare & OBD_MD_FLTYPE )
186 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
187 if ( compare & OBD_MD_FLMODE )
188 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
189 if ( compare & OBD_MD_FLUID )
190 res = (res || (dst->o_uid != src->o_uid));
191 if ( compare & OBD_MD_FLGID )
192 res = (res || (dst->o_gid != src->o_gid));
193 if ( compare & OBD_MD_FLFLAGS )
194 res = (res || (dst->o_flags != src->o_flags));
195 if ( compare & OBD_MD_FLNLINK )
196 res = (res || (dst->o_nlink != src->o_nlink));
197 if ( compare & OBD_MD_FLFID ) {
198 res = (res || (dst->o_parent_seq != src->o_parent_seq));
199 res = (res || (dst->o_parent_ver != src->o_parent_ver));
201 if ( compare & OBD_MD_FLGENER )
202 res = (res || (dst->o_parent_oid != src->o_parent_oid));
203 /* XXX Don't know if thses should be included here - wasn't previously
204 if ( compare & OBD_MD_FLINLINE )
205 res = (res || memcmp(dst->o_inline, src->o_inline));
209 EXPORT_SYMBOL(obdo_cmp_md);
211 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
213 ioobj->ioo_id = oa->o_id;
214 if (oa->o_valid & OBD_MD_FLGROUP)
215 ioobj->ioo_seq = oa->o_seq;
218 ioobj->ioo_type = oa->o_mode;
220 EXPORT_SYMBOL(obdo_to_ioobj);
222 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
224 if (ia_valid & ATTR_ATIME) {
225 oa->o_atime = LTIME_S(attr->ia_atime);
226 oa->o_valid |= OBD_MD_FLATIME;
228 if (ia_valid & ATTR_MTIME) {
229 oa->o_mtime = LTIME_S(attr->ia_mtime);
230 oa->o_valid |= OBD_MD_FLMTIME;
232 if (ia_valid & ATTR_CTIME) {
233 oa->o_ctime = LTIME_S(attr->ia_ctime);
234 oa->o_valid |= OBD_MD_FLCTIME;
236 if (ia_valid & ATTR_SIZE) {
237 oa->o_size = attr->ia_size;
238 oa->o_valid |= OBD_MD_FLSIZE;
240 if (ia_valid & ATTR_MODE) {
241 oa->o_mode = attr->ia_mode;
242 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
243 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
244 !cfs_capable(CFS_CAP_FSETID))
245 oa->o_mode &= ~S_ISGID;
247 if (ia_valid & ATTR_UID) {
248 oa->o_uid = attr->ia_uid;
249 oa->o_valid |= OBD_MD_FLUID;
251 if (ia_valid & ATTR_GID) {
252 oa->o_gid = attr->ia_gid;
253 oa->o_valid |= OBD_MD_FLGID;
256 EXPORT_SYMBOL(obdo_from_iattr);
258 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
260 valid &= oa->o_valid;
262 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
263 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
264 oa->o_valid, oa->o_mtime, oa->o_ctime);
267 if (valid & OBD_MD_FLATIME) {
268 LTIME_S(attr->ia_atime) = oa->o_atime;
269 attr->ia_valid |= ATTR_ATIME;
271 if (valid & OBD_MD_FLMTIME) {
272 LTIME_S(attr->ia_mtime) = oa->o_mtime;
273 attr->ia_valid |= ATTR_MTIME;
275 if (valid & OBD_MD_FLCTIME) {
276 LTIME_S(attr->ia_ctime) = oa->o_ctime;
277 attr->ia_valid |= ATTR_CTIME;
279 if (valid & OBD_MD_FLSIZE) {
280 attr->ia_size = oa->o_size;
281 attr->ia_valid |= ATTR_SIZE;
283 #if 0 /* you shouldn't be able to change a file's type with setattr */
284 if (valid & OBD_MD_FLTYPE) {
285 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
286 attr->ia_valid |= ATTR_MODE;
289 if (valid & OBD_MD_FLMODE) {
290 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
291 attr->ia_valid |= ATTR_MODE;
292 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
293 !cfs_capable(CFS_CAP_FSETID))
294 attr->ia_mode &= ~S_ISGID;
296 if (valid & OBD_MD_FLUID) {
297 attr->ia_uid = oa->o_uid;
298 attr->ia_valid |= ATTR_UID;
300 if (valid & OBD_MD_FLGID) {
301 attr->ia_gid = oa->o_gid;
302 attr->ia_valid |= ATTR_GID;
305 EXPORT_SYMBOL(iattr_from_obdo);
307 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
309 iattr_from_obdo(&op_data->op_attr, oa, valid);
310 if (valid & OBD_MD_FLBLOCKS) {
311 op_data->op_attr_blocks = oa->o_blocks;
312 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
314 if (valid & OBD_MD_FLFLAGS) {
315 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
317 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
320 EXPORT_SYMBOL(md_from_obdo);
322 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
325 obdo_from_iattr(oa, &op_data->op_attr, valid);
326 if (valid & ATTR_BLOCKS) {
327 oa->o_blocks = op_data->op_attr_blocks;
328 oa->o_valid |= OBD_MD_FLBLOCKS;
330 if (valid & ATTR_ATTR_FLAG) {
332 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
333 oa->o_valid |= OBD_MD_FLFLAGS;
336 EXPORT_SYMBOL(obdo_from_md);