4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/obdo.c
34 * Object Devices Class Driver
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
39 #define DEBUG_SUBSYSTEM S_CLASS
41 #include <linux/user_namespace.h>
42 #ifdef HAVE_UIDGID_HEADER
43 # include <linux/uidgid.h>
45 #include <obd_class.h>
46 #include <lustre_obdo.h>
48 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
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_FLPARENT | OBD_MD_FLFID;
55 EXPORT_SYMBOL(obdo_set_parent_fid);
57 void obdo_set_o_projid(struct obdo *dst, u32 projid)
59 dst->o_projid = projid;
60 dst->o_valid |= OBD_MD_FLPROJID;
62 EXPORT_SYMBOL(obdo_set_o_projid);
65 * WARNING: the file systems must take care not to tinker with
66 * attributes they don't manage (such as blocks).
68 void obdo_from_inode(struct obdo *dst, struct inode *src, u64 valid)
72 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
73 CDEBUG(D_INODE, "valid %#llx, new time %lld/%lld\n",
74 valid, (s64) src->i_mtime.tv_sec,
75 (s64) src->i_ctime.tv_sec);
77 if (valid & OBD_MD_FLATIME) {
78 dst->o_atime = src->i_atime.tv_sec;
79 newvalid |= OBD_MD_FLATIME;
81 if (valid & OBD_MD_FLMTIME) {
82 dst->o_mtime = src->i_mtime.tv_sec;
83 newvalid |= OBD_MD_FLMTIME;
85 if (valid & OBD_MD_FLCTIME) {
86 dst->o_ctime = src->i_ctime.tv_sec;
87 newvalid |= OBD_MD_FLCTIME;
89 if (valid & OBD_MD_FLSIZE) {
90 dst->o_size = i_size_read(src);
91 newvalid |= OBD_MD_FLSIZE;
93 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
94 dst->o_blocks = src->i_blocks;
95 newvalid |= OBD_MD_FLBLOCKS;
97 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
98 dst->o_blksize = 1U << src->i_blkbits;
99 newvalid |= OBD_MD_FLBLKSZ;
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;
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;
111 if (valid & OBD_MD_FLUID) {
112 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
113 newvalid |= OBD_MD_FLUID;
115 if (valid & OBD_MD_FLGID) {
116 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
117 newvalid |= OBD_MD_FLGID;
119 if (valid & OBD_MD_FLFLAGS) {
120 dst->o_flags = src->i_flags;
121 newvalid |= OBD_MD_FLFLAGS;
123 dst->o_valid |= newvalid;
125 EXPORT_SYMBOL(obdo_from_inode);
127 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid)
129 CDEBUG(D_INODE, "src obdo "DOSTID" valid %#llx, dst obdo "DOSTID"\n",
130 POSTID(&src->o_oi), src->o_valid, POSTID(&dst->o_oi));
131 if (valid & OBD_MD_FLATIME)
132 dst->o_atime = src->o_atime;
133 if (valid & OBD_MD_FLMTIME)
134 dst->o_mtime = src->o_mtime;
135 if (valid & OBD_MD_FLCTIME)
136 dst->o_ctime = src->o_ctime;
137 if (valid & OBD_MD_FLSIZE)
138 dst->o_size = src->o_size;
139 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
140 dst->o_blocks = src->o_blocks;
141 if (valid & OBD_MD_FLBLKSZ)
142 dst->o_blksize = src->o_blksize;
143 if (valid & OBD_MD_FLTYPE)
144 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
145 if (valid & OBD_MD_FLMODE)
146 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
147 if (valid & OBD_MD_FLUID)
148 dst->o_uid = src->o_uid;
149 if (valid & OBD_MD_FLGID)
150 dst->o_gid = src->o_gid;
151 if (valid & OBD_MD_FLFLAGS)
152 dst->o_flags = src->o_flags;
153 if (valid & OBD_MD_FLFID) {
154 dst->o_parent_seq = src->o_parent_seq;
155 dst->o_parent_ver = src->o_parent_ver;
157 if (valid & OBD_MD_FLPARENT)
158 dst->o_parent_oid = src->o_parent_oid;
159 if (valid & OBD_MD_FLHANDLE)
160 dst->o_handle = src->o_handle;
162 dst->o_valid |= valid;
164 EXPORT_SYMBOL(obdo_cpy_md);
166 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj)
168 ioobj->ioo_oid = oa->o_oi;
169 if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
170 ostid_set_seq_mdt0(&ioobj->ioo_oid);
173 * Since 2.4 this does not contain o_mode in the low 16 bits.
174 * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs
176 ioobj->ioo_max_brw = 0;
178 EXPORT_SYMBOL(obdo_to_ioobj);
181 * Create an obdo to send over the wire
183 void lustre_set_wire_obdo(const struct obd_connect_data *ocd,
185 const struct obdo *lobdo)
191 if (!(wobdo->o_valid & OBD_MD_FLUID))
192 wobdo->o_uid = from_kuid(&init_user_ns, current_uid());
193 if (!(wobdo->o_valid & OBD_MD_FLGID))
194 wobdo->o_gid = from_kgid(&init_user_ns, current_gid());
196 if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
197 fid_seq_is_echo(ostid_seq(&lobdo->o_oi))) {
199 * Currently OBD_FL_OSTID will only be used when 2.4 echo
200 * client communicate with pre-2.4 server
202 wobdo->o_oi.oi.oi_id = fid_oid(&lobdo->o_oi.oi_fid);
203 wobdo->o_oi.oi.oi_seq = fid_seq(&lobdo->o_oi.oi_fid);
206 EXPORT_SYMBOL(lustre_set_wire_obdo);
209 * Create a local obdo from a wire based odbo
211 void lustre_get_wire_obdo(const struct obd_connect_data *ocd,
213 const struct obdo *wobdo)
219 if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
220 fid_seq_is_echo(wobdo->o_oi.oi.oi_seq)) {
222 lobdo->o_oi.oi_fid.f_seq = wobdo->o_oi.oi.oi_seq;
223 lobdo->o_oi.oi_fid.f_oid = wobdo->o_oi.oi.oi_id;
224 lobdo->o_oi.oi_fid.f_ver = 0;
227 EXPORT_SYMBOL(lustre_get_wire_obdo);