Whamcloud - gitweb
LU-911 obdclass: la_from_obdo helper
[fs/lustre-release.git] / lustre / obdclass / obdo.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/obdclass/obdo.c
39  *
40  * Object Devices Class Driver
41  * These are the only exported functions, they provide some generic
42  * infrastructure for managing object devices
43  */
44
45 #define DEBUG_SUBSYSTEM S_CLASS
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49
50 #ifndef __KERNEL__
51 #include "../liblustre/llite_lib.h"
52 #else
53 #include <obd_class.h>
54 #include <lustre/lustre_idl.h>
55 #endif
56
57 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
58 {
59         dst->o_parent_oid = fid_oid(parent);
60         dst->o_parent_seq = fid_seq(parent);
61         dst->o_parent_ver = fid_ver(parent);
62         dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
63 }
64 EXPORT_SYMBOL(obdo_set_parent_fid);
65
66 /* WARNING: the file systems must take care not to tinker with
67    attributes they don't manage (such as blocks). */
68 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
69 {
70         obd_flag newvalid = 0;
71
72         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
73                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
74                        valid, LTIME_S(src->i_mtime),
75                        LTIME_S(src->i_ctime));
76
77         if (valid & OBD_MD_FLATIME) {
78                 dst->o_atime = LTIME_S(src->i_atime);
79                 newvalid |= OBD_MD_FLATIME;
80         }
81         if (valid & OBD_MD_FLMTIME) {
82                 dst->o_mtime = LTIME_S(src->i_mtime);
83                 newvalid |= OBD_MD_FLMTIME;
84         }
85         if (valid & OBD_MD_FLCTIME) {
86                 dst->o_ctime = LTIME_S(src->i_ctime);
87                 newvalid |= OBD_MD_FLCTIME;
88         }
89         if (valid & OBD_MD_FLSIZE) {
90                 dst->o_size = i_size_read(src);
91                 newvalid |= OBD_MD_FLSIZE;
92         }
93         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
94                 dst->o_blocks = src->i_blocks;
95                 newvalid |= OBD_MD_FLBLOCKS;
96         }
97         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
98                 dst->o_blksize = ll_inode_blksize(src);
99                 newvalid |= OBD_MD_FLBLKSZ;
100         }
101         if (valid & OBD_MD_FLTYPE) {
102                 dst->o_mode = (dst->o_mode & S_IALLUGO) |
103                               (src->i_mode & S_IFMT);
104                 newvalid |= OBD_MD_FLTYPE;
105         }
106         if (valid & OBD_MD_FLMODE) {
107                 dst->o_mode = (dst->o_mode & S_IFMT) |
108                               (src->i_mode & S_IALLUGO);
109                 newvalid |= OBD_MD_FLMODE;
110         }
111         if (valid & OBD_MD_FLUID) {
112                 dst->o_uid = src->i_uid;
113                 newvalid |= OBD_MD_FLUID;
114         }
115         if (valid & OBD_MD_FLGID) {
116                 dst->o_gid = src->i_gid;
117                 newvalid |= OBD_MD_FLGID;
118         }
119         if (valid & OBD_MD_FLFLAGS) {
120                 dst->o_flags = ll_inode_flags(src);
121                 newvalid |= OBD_MD_FLFLAGS;
122         }
123         dst->o_valid |= newvalid;
124 }
125 EXPORT_SYMBOL(obdo_from_inode);
126
127 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
128 {
129 #ifdef __KERNEL__
130         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
131                src->o_id, src->o_valid, dst->o_id);
132 #endif
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;
158         }
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;
165
166         dst->o_valid |= valid;
167 }
168 EXPORT_SYMBOL(obdo_cpy_md);
169
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)
172 {
173         int res = 0;
174
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));
202         }
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));
208         */
209         return res;
210 }
211 EXPORT_SYMBOL(obdo_cmp_md);
212
213 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
214 {
215         ioobj->ioo_id = oa->o_id;
216         if (oa->o_valid & OBD_MD_FLGROUP)
217                 ioobj->ioo_seq = oa->o_seq;
218         else
219                 ioobj->ioo_seq = 0;
220         ioobj->ioo_type = oa->o_mode;
221 }
222 EXPORT_SYMBOL(obdo_to_ioobj);
223
224 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
225 {
226         if (ia_valid & ATTR_ATIME) {
227                 oa->o_atime = LTIME_S(attr->ia_atime);
228                 oa->o_valid |= OBD_MD_FLATIME;
229         }
230         if (ia_valid & ATTR_MTIME) {
231                 oa->o_mtime = LTIME_S(attr->ia_mtime);
232                 oa->o_valid |= OBD_MD_FLMTIME;
233         }
234         if (ia_valid & ATTR_CTIME) {
235                 oa->o_ctime = LTIME_S(attr->ia_ctime);
236                 oa->o_valid |= OBD_MD_FLCTIME;
237         }
238         if (ia_valid & ATTR_SIZE) {
239                 oa->o_size = attr->ia_size;
240                 oa->o_valid |= OBD_MD_FLSIZE;
241         }
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;
248         }
249         if (ia_valid & ATTR_UID) {
250                 oa->o_uid = attr->ia_uid;
251                 oa->o_valid |= OBD_MD_FLUID;
252         }
253         if (ia_valid & ATTR_GID) {
254                 oa->o_gid = attr->ia_gid;
255                 oa->o_valid |= OBD_MD_FLGID;
256         }
257 }
258 EXPORT_SYMBOL(obdo_from_iattr);
259
260 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
261 {
262         valid &= oa->o_valid;
263
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);
267
268         attr->ia_valid = 0;
269         if (valid & OBD_MD_FLATIME) {
270                 LTIME_S(attr->ia_atime) = oa->o_atime;
271                 attr->ia_valid |= ATTR_ATIME;
272         }
273         if (valid & OBD_MD_FLMTIME) {
274                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
275                 attr->ia_valid |= ATTR_MTIME;
276         }
277         if (valid & OBD_MD_FLCTIME) {
278                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
279                 attr->ia_valid |= ATTR_CTIME;
280         }
281         if (valid & OBD_MD_FLSIZE) {
282                 attr->ia_size = oa->o_size;
283                 attr->ia_valid |= ATTR_SIZE;
284         }
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;
289         }
290 #endif
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;
297         }
298         if (valid & OBD_MD_FLUID) {
299                 attr->ia_uid = oa->o_uid;
300                 attr->ia_valid |= ATTR_UID;
301         }
302         if (valid & OBD_MD_FLGID) {
303                 attr->ia_gid = oa->o_gid;
304                 attr->ia_valid |= ATTR_GID;
305         }
306 }
307 EXPORT_SYMBOL(iattr_from_obdo);
308
309 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
310 {
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;
315         }
316         if (valid & OBD_MD_FLFLAGS) {
317                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
318                         oa->o_flags;
319                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
320         }
321 }
322 EXPORT_SYMBOL(md_from_obdo);
323
324 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
325                   unsigned int valid)
326 {
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;
331         }
332         if (valid & ATTR_ATTR_FLAG) {
333                 oa->o_flags = 
334                         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
335                 oa->o_valid |= OBD_MD_FLFLAGS;
336         }
337 }
338 EXPORT_SYMBOL(obdo_from_md);