Whamcloud - gitweb
LU-1206: mdt: Fix error handling in mdt_mfd_open
[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 static void obdo_set_parent_fid(struct obdo *dst, 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
65 /* WARNING: the file systems must take care not to tinker with
66    attributes they don't manage (such as blocks). */
67 void obdo_from_inode(struct obdo *dst, struct inode *src, struct lu_fid *parent,
68                      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         if (parent)
124                 obdo_set_parent_fid(dst, parent);
125         dst->o_valid |= newvalid;
126 }
127 EXPORT_SYMBOL(obdo_from_inode);
128
129 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
130 {
131 #ifdef __KERNEL__
132         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
133                src->o_id, src->o_valid, dst->o_id);
134 #endif
135         if (valid & OBD_MD_FLATIME)
136                 dst->o_atime = src->o_atime;
137         if (valid & OBD_MD_FLMTIME)
138                 dst->o_mtime = src->o_mtime;
139         if (valid & OBD_MD_FLCTIME)
140                 dst->o_ctime = src->o_ctime;
141         if (valid & OBD_MD_FLSIZE)
142                 dst->o_size = src->o_size;
143         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
144                 dst->o_blocks = src->o_blocks;
145         if (valid & OBD_MD_FLBLKSZ)
146                 dst->o_blksize = src->o_blksize;
147         if (valid & OBD_MD_FLTYPE)
148                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
149         if (valid & OBD_MD_FLMODE)
150                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
151         if (valid & OBD_MD_FLUID)
152                 dst->o_uid = src->o_uid;
153         if (valid & OBD_MD_FLGID)
154                 dst->o_gid = src->o_gid;
155         if (valid & OBD_MD_FLFLAGS)
156                 dst->o_flags = src->o_flags;
157         if (valid & OBD_MD_FLFID) {
158                 dst->o_parent_seq = src->o_parent_seq;
159                 dst->o_parent_ver = src->o_parent_ver;
160         }
161         if (valid & OBD_MD_FLGENER)
162                 dst->o_parent_oid = src->o_parent_oid;
163         if (valid & OBD_MD_FLHANDLE)
164                 dst->o_handle = src->o_handle;
165         if (valid & OBD_MD_FLCOOKIE)
166                 dst->o_lcookie = src->o_lcookie;
167
168         dst->o_valid |= valid;
169 }
170 EXPORT_SYMBOL(obdo_cpy_md);
171
172 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
173 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
174 {
175         int res = 0;
176
177         if ( compare & OBD_MD_FLATIME )
178                 res = (res || (dst->o_atime != src->o_atime));
179         if ( compare & OBD_MD_FLMTIME )
180                 res = (res || (dst->o_mtime != src->o_mtime));
181         if ( compare & OBD_MD_FLCTIME )
182                 res = (res || (dst->o_ctime != src->o_ctime));
183         if ( compare & OBD_MD_FLSIZE )
184                 res = (res || (dst->o_size != src->o_size));
185         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
186                 res = (res || (dst->o_blocks != src->o_blocks));
187         if ( compare & OBD_MD_FLBLKSZ )
188                 res = (res || (dst->o_blksize != src->o_blksize));
189         if ( compare & OBD_MD_FLTYPE )
190                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
191         if ( compare & OBD_MD_FLMODE )
192                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
193         if ( compare & OBD_MD_FLUID )
194                 res = (res || (dst->o_uid != src->o_uid));
195         if ( compare & OBD_MD_FLGID )
196                 res = (res || (dst->o_gid != src->o_gid));
197         if ( compare & OBD_MD_FLFLAGS )
198                 res = (res || (dst->o_flags != src->o_flags));
199         if ( compare & OBD_MD_FLNLINK )
200                 res = (res || (dst->o_nlink != src->o_nlink));
201         if ( compare & OBD_MD_FLFID ) {
202                 res = (res || (dst->o_parent_seq != src->o_parent_seq));
203                 res = (res || (dst->o_parent_ver != src->o_parent_ver));
204         }
205         if ( compare & OBD_MD_FLGENER )
206                 res = (res || (dst->o_parent_oid != src->o_parent_oid));
207         /* XXX Don't know if thses should be included here - wasn't previously
208         if ( compare & OBD_MD_FLINLINE )
209                 res = (res || memcmp(dst->o_inline, src->o_inline));
210         */
211         return res;
212 }
213 EXPORT_SYMBOL(obdo_cmp_md);
214
215 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
216 {
217         ioobj->ioo_id = oa->o_id;
218         if (oa->o_valid & OBD_MD_FLGROUP)
219                 ioobj->ioo_seq = oa->o_seq;
220         else
221                 ioobj->ioo_seq = 0;
222         ioobj->ioo_type = oa->o_mode;
223 }
224 EXPORT_SYMBOL(obdo_to_ioobj);
225
226 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
227 {
228         if (ia_valid & ATTR_ATIME) {
229                 oa->o_atime = LTIME_S(attr->ia_atime);
230                 oa->o_valid |= OBD_MD_FLATIME;
231         }
232         if (ia_valid & ATTR_MTIME) {
233                 oa->o_mtime = LTIME_S(attr->ia_mtime);
234                 oa->o_valid |= OBD_MD_FLMTIME;
235         }
236         if (ia_valid & ATTR_CTIME) {
237                 oa->o_ctime = LTIME_S(attr->ia_ctime);
238                 oa->o_valid |= OBD_MD_FLCTIME;
239         }
240         if (ia_valid & ATTR_SIZE) {
241                 oa->o_size = attr->ia_size;
242                 oa->o_valid |= OBD_MD_FLSIZE;
243         }
244         if (ia_valid & ATTR_MODE) {
245                 oa->o_mode = attr->ia_mode;
246                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
247                 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
248                     !cfs_capable(CFS_CAP_FSETID))
249                         oa->o_mode &= ~S_ISGID;
250         }
251         if (ia_valid & ATTR_UID) {
252                 oa->o_uid = attr->ia_uid;
253                 oa->o_valid |= OBD_MD_FLUID;
254         }
255         if (ia_valid & ATTR_GID) {
256                 oa->o_gid = attr->ia_gid;
257                 oa->o_valid |= OBD_MD_FLGID;
258         }
259 }
260 EXPORT_SYMBOL(obdo_from_iattr);
261
262 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
263 {
264         valid &= oa->o_valid;
265
266         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
267                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
268                        oa->o_valid, oa->o_mtime, oa->o_ctime);
269
270         attr->ia_valid = 0;
271         if (valid & OBD_MD_FLATIME) {
272                 LTIME_S(attr->ia_atime) = oa->o_atime;
273                 attr->ia_valid |= ATTR_ATIME;
274         }
275         if (valid & OBD_MD_FLMTIME) {
276                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
277                 attr->ia_valid |= ATTR_MTIME;
278         }
279         if (valid & OBD_MD_FLCTIME) {
280                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
281                 attr->ia_valid |= ATTR_CTIME;
282         }
283         if (valid & OBD_MD_FLSIZE) {
284                 attr->ia_size = oa->o_size;
285                 attr->ia_valid |= ATTR_SIZE;
286         }
287 #if 0   /* you shouldn't be able to change a file's type with setattr */
288         if (valid & OBD_MD_FLTYPE) {
289                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
290                 attr->ia_valid |= ATTR_MODE;
291         }
292 #endif
293         if (valid & OBD_MD_FLMODE) {
294                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
295                 attr->ia_valid |= ATTR_MODE;
296                 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
297                     !cfs_capable(CFS_CAP_FSETID))
298                         attr->ia_mode &= ~S_ISGID;
299         }
300         if (valid & OBD_MD_FLUID) {
301                 attr->ia_uid = oa->o_uid;
302                 attr->ia_valid |= ATTR_UID;
303         }
304         if (valid & OBD_MD_FLGID) {
305                 attr->ia_gid = oa->o_gid;
306                 attr->ia_valid |= ATTR_GID;
307         }
308 }
309 EXPORT_SYMBOL(iattr_from_obdo);
310
311 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
312 {
313         iattr_from_obdo(&op_data->op_attr, oa, valid);
314         if (valid & OBD_MD_FLBLOCKS) {
315                 op_data->op_attr_blocks = oa->o_blocks;
316                 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
317         }
318         if (valid & OBD_MD_FLFLAGS) {
319                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
320                         oa->o_flags;
321                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
322         }
323 }
324 EXPORT_SYMBOL(md_from_obdo);
325
326 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
327                   unsigned int valid)
328 {
329         obdo_from_iattr(oa, &op_data->op_attr, valid);
330         if (valid & ATTR_BLOCKS) {
331                 oa->o_blocks = op_data->op_attr_blocks;
332                 oa->o_valid |= OBD_MD_FLBLOCKS;
333         }
334         if (valid & ATTR_ATTR_FLAG) {
335                 oa->o_flags = 
336                         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
337                 oa->o_valid |= OBD_MD_FLFLAGS;
338         }
339 }
340 EXPORT_SYMBOL(obdo_from_md);