Merge branches 'acpi-ec' and 'acpi-button'
[cascardo/linux.git] / drivers / staging / lustre / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obdo.c
33  *
34  * Object Devices Class Driver
35  * These are the only exported functions, they provide some generic
36  * infrastructure for managing object devices
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include "../include/obd_class.h"
42 #include "../include/lustre/lustre_idl.h"
43
44 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
45 {
46         dst->o_parent_oid = fid_oid(parent);
47         dst->o_parent_seq = fid_seq(parent);
48         dst->o_parent_ver = fid_ver(parent);
49         dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
50 }
51 EXPORT_SYMBOL(obdo_set_parent_fid);
52
53 /* WARNING: the file systems must take care not to tinker with
54  * attributes they don't manage (such as blocks).
55  */
56 void obdo_from_inode(struct obdo *dst, struct inode *src, u32 valid)
57 {
58         u32 newvalid = 0;
59
60         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
61                 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
62                        valid, LTIME_S(src->i_mtime),
63                        LTIME_S(src->i_ctime));
64
65         if (valid & OBD_MD_FLATIME) {
66                 dst->o_atime = LTIME_S(src->i_atime);
67                 newvalid |= OBD_MD_FLATIME;
68         }
69         if (valid & OBD_MD_FLMTIME) {
70                 dst->o_mtime = LTIME_S(src->i_mtime);
71                 newvalid |= OBD_MD_FLMTIME;
72         }
73         if (valid & OBD_MD_FLCTIME) {
74                 dst->o_ctime = LTIME_S(src->i_ctime);
75                 newvalid |= OBD_MD_FLCTIME;
76         }
77         if (valid & OBD_MD_FLSIZE) {
78                 dst->o_size = i_size_read(src);
79                 newvalid |= OBD_MD_FLSIZE;
80         }
81         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
82                 dst->o_blocks = src->i_blocks;
83                 newvalid |= OBD_MD_FLBLOCKS;
84         }
85         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
86                 dst->o_blksize = 1 << src->i_blkbits;
87                 newvalid |= OBD_MD_FLBLKSZ;
88         }
89         if (valid & OBD_MD_FLTYPE) {
90                 dst->o_mode = (dst->o_mode & S_IALLUGO) |
91                               (src->i_mode & S_IFMT);
92                 newvalid |= OBD_MD_FLTYPE;
93         }
94         if (valid & OBD_MD_FLMODE) {
95                 dst->o_mode = (dst->o_mode & S_IFMT) |
96                               (src->i_mode & S_IALLUGO);
97                 newvalid |= OBD_MD_FLMODE;
98         }
99         if (valid & OBD_MD_FLUID) {
100                 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
101                 newvalid |= OBD_MD_FLUID;
102         }
103         if (valid & OBD_MD_FLGID) {
104                 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
105                 newvalid |= OBD_MD_FLGID;
106         }
107         if (valid & OBD_MD_FLFLAGS) {
108                 dst->o_flags = src->i_flags;
109                 newvalid |= OBD_MD_FLFLAGS;
110         }
111         dst->o_valid |= newvalid;
112 }
113 EXPORT_SYMBOL(obdo_from_inode);
114
115 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
116 {
117         ioobj->ioo_oid = oa->o_oi;
118         if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
119                 ostid_set_seq_mdt0(&ioobj->ioo_oid);
120
121         /* Since 2.4 this does not contain o_mode in the low 16 bits.
122          * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs
123          */
124         ioobj->ioo_max_brw = 0;
125 }
126 EXPORT_SYMBOL(obdo_to_ioobj);
127
128 static void iattr_from_obdo(struct iattr *attr, struct obdo *oa, u32 valid)
129 {
130         valid &= oa->o_valid;
131
132         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
133                 CDEBUG(D_INODE, "valid %#llx, new time %llu/%llu\n",
134                        oa->o_valid, oa->o_mtime, oa->o_ctime);
135
136         attr->ia_valid = 0;
137         if (valid & OBD_MD_FLATIME) {
138                 LTIME_S(attr->ia_atime) = oa->o_atime;
139                 attr->ia_valid |= ATTR_ATIME;
140         }
141         if (valid & OBD_MD_FLMTIME) {
142                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
143                 attr->ia_valid |= ATTR_MTIME;
144         }
145         if (valid & OBD_MD_FLCTIME) {
146                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
147                 attr->ia_valid |= ATTR_CTIME;
148         }
149         if (valid & OBD_MD_FLSIZE) {
150                 attr->ia_size = oa->o_size;
151                 attr->ia_valid |= ATTR_SIZE;
152         }
153 #if 0   /* you shouldn't be able to change a file's type with setattr */
154         if (valid & OBD_MD_FLTYPE) {
155                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
156                 attr->ia_valid |= ATTR_MODE;
157         }
158 #endif
159         if (valid & OBD_MD_FLMODE) {
160                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
161                 attr->ia_valid |= ATTR_MODE;
162                 if (!in_group_p(make_kgid(&init_user_ns, oa->o_gid)) &&
163                     !capable(CFS_CAP_FSETID))
164                         attr->ia_mode &= ~S_ISGID;
165         }
166         if (valid & OBD_MD_FLUID) {
167                 attr->ia_uid = make_kuid(&init_user_ns, oa->o_uid);
168                 attr->ia_valid |= ATTR_UID;
169         }
170         if (valid & OBD_MD_FLGID) {
171                 attr->ia_gid = make_kgid(&init_user_ns, oa->o_gid);
172                 attr->ia_valid |= ATTR_GID;
173         }
174 }
175
176 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, u32 valid)
177 {
178         iattr_from_obdo(&op_data->op_attr, oa, valid);
179         if (valid & OBD_MD_FLBLOCKS) {
180                 op_data->op_attr_blocks = oa->o_blocks;
181                 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
182         }
183         if (valid & OBD_MD_FLFLAGS) {
184                 op_data->op_attr_flags = oa->o_flags;
185                 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
186         }
187 }
188 EXPORT_SYMBOL(md_from_obdo);