Whamcloud - gitweb
LU-1940 test: use default resend count for 118c
[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) 2011, 2012, Intel Corporation.
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
45 #ifndef __KERNEL__
46 #include "../liblustre/llite_lib.h"
47 #else
48 #include <obd_class.h>
49 #include <lustre/lustre_idl.h>
50 #endif
51
52 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
53 {
54         dst->o_parent_oid = fid_oid(parent);
55         dst->o_parent_seq = fid_seq(parent);
56         dst->o_parent_ver = fid_ver(parent);
57         dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
58 }
59 EXPORT_SYMBOL(obdo_set_parent_fid);
60
61 /* WARNING: the file systems must take care not to tinker with
62    attributes they don't manage (such as blocks). */
63 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
64 {
65         obd_flag newvalid = 0;
66
67         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
68                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
69                        valid, LTIME_S(src->i_mtime),
70                        LTIME_S(src->i_ctime));
71
72         if (valid & OBD_MD_FLATIME) {
73                 dst->o_atime = LTIME_S(src->i_atime);
74                 newvalid |= OBD_MD_FLATIME;
75         }
76         if (valid & OBD_MD_FLMTIME) {
77                 dst->o_mtime = LTIME_S(src->i_mtime);
78                 newvalid |= OBD_MD_FLMTIME;
79         }
80         if (valid & OBD_MD_FLCTIME) {
81                 dst->o_ctime = LTIME_S(src->i_ctime);
82                 newvalid |= OBD_MD_FLCTIME;
83         }
84         if (valid & OBD_MD_FLSIZE) {
85                 dst->o_size = i_size_read(src);
86                 newvalid |= OBD_MD_FLSIZE;
87         }
88         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
89                 dst->o_blocks = src->i_blocks;
90                 newvalid |= OBD_MD_FLBLOCKS;
91         }
92         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
93                 dst->o_blksize = ll_inode_blksize(src);
94                 newvalid |= OBD_MD_FLBLKSZ;
95         }
96         if (valid & OBD_MD_FLTYPE) {
97                 dst->o_mode = (dst->o_mode & S_IALLUGO) |
98                               (src->i_mode & S_IFMT);
99                 newvalid |= OBD_MD_FLTYPE;
100         }
101         if (valid & OBD_MD_FLMODE) {
102                 dst->o_mode = (dst->o_mode & S_IFMT) |
103                               (src->i_mode & S_IALLUGO);
104                 newvalid |= OBD_MD_FLMODE;
105         }
106         if (valid & OBD_MD_FLUID) {
107                 dst->o_uid = src->i_uid;
108                 newvalid |= OBD_MD_FLUID;
109         }
110         if (valid & OBD_MD_FLGID) {
111                 dst->o_gid = src->i_gid;
112                 newvalid |= OBD_MD_FLGID;
113         }
114         if (valid & OBD_MD_FLFLAGS) {
115                 dst->o_flags = ll_inode_flags(src);
116                 newvalid |= OBD_MD_FLFLAGS;
117         }
118         dst->o_valid |= newvalid;
119 }
120 EXPORT_SYMBOL(obdo_from_inode);
121
122 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid)
123 {
124 #ifdef __KERNEL__
125         CDEBUG(D_INODE, "src obdo "LPX64" valid "LPX64", dst obdo "LPX64"\n",
126                src->o_id, src->o_valid, dst->o_id);
127 #endif
128         if (valid & OBD_MD_FLATIME)
129                 dst->o_atime = src->o_atime;
130         if (valid & OBD_MD_FLMTIME)
131                 dst->o_mtime = src->o_mtime;
132         if (valid & OBD_MD_FLCTIME)
133                 dst->o_ctime = src->o_ctime;
134         if (valid & OBD_MD_FLSIZE)
135                 dst->o_size = src->o_size;
136         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
137                 dst->o_blocks = src->o_blocks;
138         if (valid & OBD_MD_FLBLKSZ)
139                 dst->o_blksize = src->o_blksize;
140         if (valid & OBD_MD_FLTYPE)
141                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
142         if (valid & OBD_MD_FLMODE)
143                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
144         if (valid & OBD_MD_FLUID)
145                 dst->o_uid = src->o_uid;
146         if (valid & OBD_MD_FLGID)
147                 dst->o_gid = src->o_gid;
148         if (valid & OBD_MD_FLFLAGS)
149                 dst->o_flags = src->o_flags;
150         if (valid & OBD_MD_FLFID) {
151                 dst->o_parent_seq = src->o_parent_seq;
152                 dst->o_parent_ver = src->o_parent_ver;
153         }
154         if (valid & OBD_MD_FLGENER)
155                 dst->o_parent_oid = src->o_parent_oid;
156         if (valid & OBD_MD_FLHANDLE)
157                 dst->o_handle = src->o_handle;
158         if (valid & OBD_MD_FLCOOKIE)
159                 dst->o_lcookie = src->o_lcookie;
160
161         dst->o_valid |= valid;
162 }
163 EXPORT_SYMBOL(obdo_cpy_md);
164
165 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
166 int obdo_cmp_md(struct obdo *dst, struct obdo *src, obd_flag compare)
167 {
168         int res = 0;
169
170         if ( compare & OBD_MD_FLATIME )
171                 res = (res || (dst->o_atime != src->o_atime));
172         if ( compare & OBD_MD_FLMTIME )
173                 res = (res || (dst->o_mtime != src->o_mtime));
174         if ( compare & OBD_MD_FLCTIME )
175                 res = (res || (dst->o_ctime != src->o_ctime));
176         if ( compare & OBD_MD_FLSIZE )
177                 res = (res || (dst->o_size != src->o_size));
178         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
179                 res = (res || (dst->o_blocks != src->o_blocks));
180         if ( compare & OBD_MD_FLBLKSZ )
181                 res = (res || (dst->o_blksize != src->o_blksize));
182         if ( compare & OBD_MD_FLTYPE )
183                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
184         if ( compare & OBD_MD_FLMODE )
185                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
186         if ( compare & OBD_MD_FLUID )
187                 res = (res || (dst->o_uid != src->o_uid));
188         if ( compare & OBD_MD_FLGID )
189                 res = (res || (dst->o_gid != src->o_gid));
190         if ( compare & OBD_MD_FLFLAGS )
191                 res = (res || (dst->o_flags != src->o_flags));
192         if ( compare & OBD_MD_FLNLINK )
193                 res = (res || (dst->o_nlink != src->o_nlink));
194         if ( compare & OBD_MD_FLFID ) {
195                 res = (res || (dst->o_parent_seq != src->o_parent_seq));
196                 res = (res || (dst->o_parent_ver != src->o_parent_ver));
197         }
198         if ( compare & OBD_MD_FLGENER )
199                 res = (res || (dst->o_parent_oid != src->o_parent_oid));
200         /* XXX Don't know if thses should be included here - wasn't previously
201         if ( compare & OBD_MD_FLINLINE )
202                 res = (res || memcmp(dst->o_inline, src->o_inline));
203         */
204         return res;
205 }
206 EXPORT_SYMBOL(obdo_cmp_md);
207
208 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
209 {
210         ioobj->ioo_id = oa->o_id;
211         if (oa->o_valid & OBD_MD_FLGROUP)
212                 ioobj->ioo_seq = oa->o_seq;
213         else
214                 ioobj->ioo_seq = 0;
215         /* Since 2.4 this does not contain o_mode in the low 16 bits.
216          * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs */
217         ioobj->ioo_max_brw = 0;
218 }
219 EXPORT_SYMBOL(obdo_to_ioobj);
220
221 void obdo_from_iattr(struct obdo *oa, struct iattr *attr, unsigned int ia_valid)
222 {
223         if (ia_valid & ATTR_ATIME) {
224                 oa->o_atime = LTIME_S(attr->ia_atime);
225                 oa->o_valid |= OBD_MD_FLATIME;
226         }
227         if (ia_valid & ATTR_MTIME) {
228                 oa->o_mtime = LTIME_S(attr->ia_mtime);
229                 oa->o_valid |= OBD_MD_FLMTIME;
230         }
231         if (ia_valid & ATTR_CTIME) {
232                 oa->o_ctime = LTIME_S(attr->ia_ctime);
233                 oa->o_valid |= OBD_MD_FLCTIME;
234         }
235         if (ia_valid & ATTR_SIZE) {
236                 oa->o_size = attr->ia_size;
237                 oa->o_valid |= OBD_MD_FLSIZE;
238         }
239         if (ia_valid & ATTR_MODE) {
240                 oa->o_mode = attr->ia_mode;
241                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
242                 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
243                     !cfs_capable(CFS_CAP_FSETID))
244                         oa->o_mode &= ~S_ISGID;
245         }
246         if (ia_valid & ATTR_UID) {
247                 oa->o_uid = attr->ia_uid;
248                 oa->o_valid |= OBD_MD_FLUID;
249         }
250         if (ia_valid & ATTR_GID) {
251                 oa->o_gid = attr->ia_gid;
252                 oa->o_valid |= OBD_MD_FLGID;
253         }
254 }
255 EXPORT_SYMBOL(obdo_from_iattr);
256
257 void iattr_from_obdo(struct iattr *attr, struct obdo *oa, obd_flag valid)
258 {
259         valid &= oa->o_valid;
260
261         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
262                 CDEBUG(D_INODE, "valid "LPX64", new time "LPU64"/"LPU64"\n",
263                        oa->o_valid, oa->o_mtime, oa->o_ctime);
264
265         attr->ia_valid = 0;
266         if (valid & OBD_MD_FLATIME) {
267                 LTIME_S(attr->ia_atime) = oa->o_atime;
268                 attr->ia_valid |= ATTR_ATIME;
269         }
270         if (valid & OBD_MD_FLMTIME) {
271                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
272                 attr->ia_valid |= ATTR_MTIME;
273         }
274         if (valid & OBD_MD_FLCTIME) {
275                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
276                 attr->ia_valid |= ATTR_CTIME;
277         }
278         if (valid & OBD_MD_FLSIZE) {
279                 attr->ia_size = oa->o_size;
280                 attr->ia_valid |= ATTR_SIZE;
281         }
282 #if 0   /* you shouldn't be able to change a file's type with setattr */
283         if (valid & OBD_MD_FLTYPE) {
284                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
285                 attr->ia_valid |= ATTR_MODE;
286         }
287 #endif
288         if (valid & OBD_MD_FLMODE) {
289                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
290                 attr->ia_valid |= ATTR_MODE;
291                 if (!cfs_curproc_is_in_groups(oa->o_gid) &&
292                     !cfs_capable(CFS_CAP_FSETID))
293                         attr->ia_mode &= ~S_ISGID;
294         }
295         if (valid & OBD_MD_FLUID) {
296                 attr->ia_uid = oa->o_uid;
297                 attr->ia_valid |= ATTR_UID;
298         }
299         if (valid & OBD_MD_FLGID) {
300                 attr->ia_gid = oa->o_gid;
301                 attr->ia_valid |= ATTR_GID;
302         }
303 }
304 EXPORT_SYMBOL(iattr_from_obdo);
305
306 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, obd_flag valid)
307 {
308         iattr_from_obdo(&op_data->op_attr, oa, valid);
309         if (valid & OBD_MD_FLBLOCKS) {
310                 op_data->op_attr_blocks = oa->o_blocks;
311                 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
312         }
313         if (valid & OBD_MD_FLFLAGS) {
314                 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
315                         oa->o_flags;
316                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
317         }
318 }
319 EXPORT_SYMBOL(md_from_obdo);
320
321 void obdo_from_md(struct obdo *oa, struct md_op_data *op_data,
322                   unsigned int valid)
323 {
324         obdo_from_iattr(oa, &op_data->op_attr, valid);
325         if (valid & ATTR_BLOCKS) {
326                 oa->o_blocks = op_data->op_attr_blocks;
327                 oa->o_valid |= OBD_MD_FLBLOCKS;
328         }
329         if (valid & ATTR_ATTR_FLAG) {
330                 oa->o_flags = 
331                         ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags;
332                 oa->o_valid |= OBD_MD_FLFLAGS;
333         }
334 }
335 EXPORT_SYMBOL(obdo_from_md);
336
337 void obdo_cpu_to_le(struct obdo *dobdo, struct obdo *sobdo)
338 {
339         dobdo->o_size = cpu_to_le64(sobdo->o_size);
340         dobdo->o_mtime = cpu_to_le64(sobdo->o_mtime);
341         dobdo->o_atime = cpu_to_le64(sobdo->o_atime);
342         dobdo->o_ctime = cpu_to_le64(sobdo->o_ctime);
343         dobdo->o_blocks = cpu_to_le64(sobdo->o_blocks);
344         dobdo->o_mode = cpu_to_le32(sobdo->o_mode);
345         dobdo->o_uid = cpu_to_le32(sobdo->o_uid);
346         dobdo->o_gid = cpu_to_le32(sobdo->o_gid);
347         dobdo->o_flags = cpu_to_le32(sobdo->o_flags);
348         dobdo->o_nlink = cpu_to_le32(sobdo->o_nlink);
349         dobdo->o_blksize = cpu_to_le32(sobdo->o_blksize);
350         dobdo->o_valid = cpu_to_le64(sobdo->o_valid);
351 }
352 EXPORT_SYMBOL(obdo_cpu_to_le);
353
354 void obdo_le_to_cpu(struct obdo *dobdo, struct obdo *sobdo)
355 {
356         dobdo->o_size = le64_to_cpu(sobdo->o_size);
357         dobdo->o_mtime = le64_to_cpu(sobdo->o_mtime);
358         dobdo->o_atime = le64_to_cpu(sobdo->o_atime);
359         dobdo->o_ctime = le64_to_cpu(sobdo->o_ctime);
360         dobdo->o_blocks = le64_to_cpu(sobdo->o_blocks);
361         dobdo->o_mode = le32_to_cpu(sobdo->o_mode);
362         dobdo->o_uid = le32_to_cpu(sobdo->o_uid);
363         dobdo->o_gid = le32_to_cpu(sobdo->o_gid);
364         dobdo->o_flags = le32_to_cpu(sobdo->o_flags);
365         dobdo->o_nlink = le32_to_cpu(sobdo->o_nlink);
366         dobdo->o_blksize = le32_to_cpu(sobdo->o_blksize);
367         dobdo->o_valid = le64_to_cpu(sobdo->o_valid);
368 }
369 EXPORT_SYMBOL(obdo_le_to_cpu);
370