Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / obdclass / obdo.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/obdo.c
37  *
38  * Object Devices Class Driver
39  * These are the only exported functions, they provide some generic
40  * infrastructure for managing object devices
41  */
42
43 #define DEBUG_SUBSYSTEM S_CLASS
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 #ifndef __KERNEL__
49 #include "../liblustre/llite_lib.h"
50 #else
51 #include <obd_class.h>
52 #include <lustre/lustre_idl.h>
53 #endif
54
55 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
56 {
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;
61 }
62 EXPORT_SYMBOL(obdo_set_parent_fid);
63
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)
67 {
68         obd_flag newvalid = 0;
69
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));
74
75         if (valid & OBD_MD_FLATIME) {
76                 dst->o_atime = LTIME_S(src->i_atime);
77                 newvalid |= OBD_MD_FLATIME;
78         }
79         if (valid & OBD_MD_FLMTIME) {
80                 dst->o_mtime = LTIME_S(src->i_mtime);
81                 newvalid |= OBD_MD_FLMTIME;
82         }
83         if (valid & OBD_MD_FLCTIME) {
84                 dst->o_ctime = LTIME_S(src->i_ctime);
85                 newvalid |= OBD_MD_FLCTIME;
86         }
87         if (valid & OBD_MD_FLSIZE) {
88                 dst->o_size = i_size_read(src);
89                 newvalid |= OBD_MD_FLSIZE;
90         }
91         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
92                 dst->o_blocks = src->i_blocks;
93                 newvalid |= OBD_MD_FLBLOCKS;
94         }
95         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
96                 dst->o_blksize = ll_inode_blksize(src);
97                 newvalid |= OBD_MD_FLBLKSZ;
98         }
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;
103         }
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;
108         }
109         if (valid & OBD_MD_FLUID) {
110                 dst->o_uid = src->i_uid;
111                 newvalid |= OBD_MD_FLUID;
112         }
113         if (valid & OBD_MD_FLGID) {
114                 dst->o_gid = src->i_gid;
115                 newvalid |= OBD_MD_FLGID;
116         }
117         if (valid & OBD_MD_FLFLAGS) {
118                 dst->o_flags = ll_inode_flags(src);
119                 newvalid |= OBD_MD_FLFLAGS;
120         }
121         dst->o_valid |= newvalid;
122 }
123 EXPORT_SYMBOL(obdo_from_inode);
124
125 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
126 {
127 #ifdef __KERNEL__
128         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
129                src->o_id, src->o_valid, dst->o_id);
130 #endif
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;
156         }
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;
163
164         dst->o_valid |= valid;
165 }
166 EXPORT_SYMBOL(obdo_cpy_md);
167
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)
170 {
171         int res = 0;
172
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));
200         }
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));
206         */
207         return res;
208 }
209 EXPORT_SYMBOL(obdo_cmp_md);
210
211 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
212 {
213         ioobj->ioo_id = oa->o_id;
214         if (oa->o_valid & OBD_MD_FLGROUP)
215                 ioobj->ioo_seq = oa->o_seq;
216         else
217                 ioobj->ioo_seq = 0;
218         ioobj->ioo_type = oa->o_mode;
219 }
220 EXPORT_SYMBOL(obdo_to_ioobj);
221
222 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
223 {
224         if (ia_valid & ATTR_ATIME) {
225                 oa->o_atime = LTIME_S(attr->ia_atime);
226                 oa->o_valid |= OBD_MD_FLATIME;
227         }
228         if (ia_valid & ATTR_MTIME) {
229                 oa->o_mtime = LTIME_S(attr->ia_mtime);
230                 oa->o_valid |= OBD_MD_FLMTIME;
231         }
232         if (ia_valid & ATTR_CTIME) {
233                 oa->o_ctime = LTIME_S(attr->ia_ctime);
234                 oa->o_valid |= OBD_MD_FLCTIME;
235         }
236         if (ia_valid & ATTR_SIZE) {
237                 oa->o_size = attr->ia_size;
238                 oa->o_valid |= OBD_MD_FLSIZE;
239         }
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;
246         }
247         if (ia_valid & ATTR_UID) {
248                 oa->o_uid = attr->ia_uid;
249                 oa->o_valid |= OBD_MD_FLUID;
250         }
251         if (ia_valid & ATTR_GID) {
252                 oa->o_gid = attr->ia_gid;
253                 oa->o_valid |= OBD_MD_FLGID;
254         }
255 }
256 EXPORT_SYMBOL(obdo_from_iattr);
257
258 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
259 {
260         valid &= oa->o_valid;
261
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);
265
266         attr->ia_valid = 0;
267         if (valid & OBD_MD_FLATIME) {
268                 LTIME_S(attr->ia_atime) = oa->o_atime;
269                 attr->ia_valid |= ATTR_ATIME;
270         }
271         if (valid & OBD_MD_FLMTIME) {
272                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
273                 attr->ia_valid |= ATTR_MTIME;
274         }
275         if (valid & OBD_MD_FLCTIME) {
276                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
277                 attr->ia_valid |= ATTR_CTIME;
278         }
279         if (valid & OBD_MD_FLSIZE) {
280                 attr->ia_size = oa->o_size;
281                 attr->ia_valid |= ATTR_SIZE;
282         }
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;
287         }
288 #endif
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;
295         }
296         if (valid & OBD_MD_FLUID) {
297                 attr->ia_uid = oa->o_uid;
298                 attr->ia_valid |= ATTR_UID;
299         }
300         if (valid & OBD_MD_FLGID) {
301                 attr->ia_gid = oa->o_gid;
302                 attr->ia_valid |= ATTR_GID;
303         }
304 }
305 EXPORT_SYMBOL(iattr_from_obdo);
306
307 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
308 {
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;
313         }
314         if (valid & OBD_MD_FLFLAGS) {
315                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
316                         oa->o_flags;
317                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
318         }
319 }
320 EXPORT_SYMBOL(md_from_obdo);
321
322 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
323                   unsigned int valid)
324 {
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;
329         }
330         if (valid & ATTR_ATTR_FLAG) {
331                 oa->o_flags = 
332                         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
333                 oa->o_valid |= OBD_MD_FLFLAGS;
334         }
335 }
336 EXPORT_SYMBOL(obdo_from_md);