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 #include "../include/obd_class.h"
46 #include "../include/lustre/lustre_idl.h"
47
obdo_set_parent_fid(struct obdo * dst,const struct lu_fid * parent)48 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
49 {
50 dst->o_parent_oid = fid_oid(parent);
51 dst->o_parent_seq = fid_seq(parent);
52 dst->o_parent_ver = fid_ver(parent);
53 dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
54 }
55 EXPORT_SYMBOL(obdo_set_parent_fid);
56
57 /* WARNING: the file systems must take care not to tinker with
58 attributes they don't manage (such as blocks). */
obdo_from_inode(struct obdo * dst,struct inode * src,u32 valid)59 void obdo_from_inode(struct obdo *dst, struct inode *src, u32 valid)
60 {
61 u32 newvalid = 0;
62
63 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
64 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
65 valid, LTIME_S(src->i_mtime),
66 LTIME_S(src->i_ctime));
67
68 if (valid & OBD_MD_FLATIME) {
69 dst->o_atime = LTIME_S(src->i_atime);
70 newvalid |= OBD_MD_FLATIME;
71 }
72 if (valid & OBD_MD_FLMTIME) {
73 dst->o_mtime = LTIME_S(src->i_mtime);
74 newvalid |= OBD_MD_FLMTIME;
75 }
76 if (valid & OBD_MD_FLCTIME) {
77 dst->o_ctime = LTIME_S(src->i_ctime);
78 newvalid |= OBD_MD_FLCTIME;
79 }
80 if (valid & OBD_MD_FLSIZE) {
81 dst->o_size = i_size_read(src);
82 newvalid |= OBD_MD_FLSIZE;
83 }
84 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
85 dst->o_blocks = src->i_blocks;
86 newvalid |= OBD_MD_FLBLOCKS;
87 }
88 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
89 dst->o_blksize = 1 << src->i_blkbits;
90 newvalid |= OBD_MD_FLBLKSZ;
91 }
92 if (valid & OBD_MD_FLTYPE) {
93 dst->o_mode = (dst->o_mode & S_IALLUGO) |
94 (src->i_mode & S_IFMT);
95 newvalid |= OBD_MD_FLTYPE;
96 }
97 if (valid & OBD_MD_FLMODE) {
98 dst->o_mode = (dst->o_mode & S_IFMT) |
99 (src->i_mode & S_IALLUGO);
100 newvalid |= OBD_MD_FLMODE;
101 }
102 if (valid & OBD_MD_FLUID) {
103 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
104 newvalid |= OBD_MD_FLUID;
105 }
106 if (valid & OBD_MD_FLGID) {
107 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
108 newvalid |= OBD_MD_FLGID;
109 }
110 if (valid & OBD_MD_FLFLAGS) {
111 dst->o_flags = src->i_flags;
112 newvalid |= OBD_MD_FLFLAGS;
113 }
114 dst->o_valid |= newvalid;
115 }
116 EXPORT_SYMBOL(obdo_from_inode);
117
obdo_to_ioobj(struct obdo * oa,struct obd_ioobj * ioobj)118 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj)
119 {
120 ioobj->ioo_oid = oa->o_oi;
121 if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
122 ostid_set_seq_mdt0(&ioobj->ioo_oid);
123
124 /* Since 2.4 this does not contain o_mode in the low 16 bits.
125 * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs */
126 ioobj->ioo_max_brw = 0;
127 }
128 EXPORT_SYMBOL(obdo_to_ioobj);
129
iattr_from_obdo(struct iattr * attr,struct obdo * oa,u32 valid)130 static void iattr_from_obdo(struct iattr *attr, struct obdo *oa, u32 valid)
131 {
132 valid &= oa->o_valid;
133
134 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
135 CDEBUG(D_INODE, "valid %#llx, new time %llu/%llu\n",
136 oa->o_valid, oa->o_mtime, oa->o_ctime);
137
138 attr->ia_valid = 0;
139 if (valid & OBD_MD_FLATIME) {
140 LTIME_S(attr->ia_atime) = oa->o_atime;
141 attr->ia_valid |= ATTR_ATIME;
142 }
143 if (valid & OBD_MD_FLMTIME) {
144 LTIME_S(attr->ia_mtime) = oa->o_mtime;
145 attr->ia_valid |= ATTR_MTIME;
146 }
147 if (valid & OBD_MD_FLCTIME) {
148 LTIME_S(attr->ia_ctime) = oa->o_ctime;
149 attr->ia_valid |= ATTR_CTIME;
150 }
151 if (valid & OBD_MD_FLSIZE) {
152 attr->ia_size = oa->o_size;
153 attr->ia_valid |= ATTR_SIZE;
154 }
155 #if 0 /* you shouldn't be able to change a file's type with setattr */
156 if (valid & OBD_MD_FLTYPE) {
157 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
158 attr->ia_valid |= ATTR_MODE;
159 }
160 #endif
161 if (valid & OBD_MD_FLMODE) {
162 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
163 attr->ia_valid |= ATTR_MODE;
164 if (!in_group_p(make_kgid(&init_user_ns, oa->o_gid)) &&
165 !capable(CFS_CAP_FSETID))
166 attr->ia_mode &= ~S_ISGID;
167 }
168 if (valid & OBD_MD_FLUID) {
169 attr->ia_uid = make_kuid(&init_user_ns, oa->o_uid);
170 attr->ia_valid |= ATTR_UID;
171 }
172 if (valid & OBD_MD_FLGID) {
173 attr->ia_gid = make_kgid(&init_user_ns, oa->o_gid);
174 attr->ia_valid |= ATTR_GID;
175 }
176 }
177
md_from_obdo(struct md_op_data * op_data,struct obdo * oa,u32 valid)178 void md_from_obdo(struct md_op_data *op_data, struct obdo *oa, u32 valid)
179 {
180 iattr_from_obdo(&op_data->op_attr, oa, valid);
181 if (valid & OBD_MD_FLBLOCKS) {
182 op_data->op_attr_blocks = oa->o_blocks;
183 op_data->op_attr.ia_valid |= ATTR_BLOCKS;
184 }
185 if (valid & OBD_MD_FLFLAGS) {
186 ((struct ll_iattr *)&op_data->op_attr)->ia_attr_flags =
187 oa->o_flags;
188 op_data->op_attr.ia_valid |= ATTR_ATTR_FLAG;
189 }
190 }
191 EXPORT_SYMBOL(md_from_obdo);
192