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/
31 * lustre/obdclass/obdo.c
33 * Object Devices Class Driver
34 * These are the only exported functions, they provide some generic
35 * infrastructure for managing object devices
38 #define DEBUG_SUBSYSTEM S_CLASS
40 #include <linux/user_namespace.h>
41 #include <linux/uidgid.h>
43 #include <obd_class.h>
44 #include <lustre_obdo.h>
46 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
48 dst->o_parent_oid = fid_oid(parent);
49 dst->o_parent_seq = fid_seq(parent);
50 dst->o_parent_ver = fid_ver(parent);
51 dst->o_valid |= OBD_MD_FLPARENT | OBD_MD_FLFID;
53 EXPORT_SYMBOL(obdo_set_parent_fid);
55 void obdo_set_o_projid(struct obdo *dst, u32 projid)
57 dst->o_projid = projid;
58 dst->o_valid |= OBD_MD_FLPROJID;
60 EXPORT_SYMBOL(obdo_set_o_projid);
63 * WARNING: the file systems must take care not to tinker with
64 * attributes they don't manage (such as blocks).
66 void obdo_from_inode(struct obdo *dst, struct inode *src, u64 valid)
70 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
71 CDEBUG(D_INODE, "valid %#llx, new time %lld/%lld\n",
72 valid, (s64) src->i_mtime.tv_sec,
73 (s64) src->i_ctime.tv_sec);
75 if (valid & OBD_MD_FLATIME) {
76 dst->o_atime = src->i_atime.tv_sec;
77 newvalid |= OBD_MD_FLATIME;
79 if (valid & OBD_MD_FLMTIME) {
80 dst->o_mtime = src->i_mtime.tv_sec;
81 newvalid |= OBD_MD_FLMTIME;
83 if (valid & OBD_MD_FLCTIME) {
84 dst->o_ctime = src->i_ctime.tv_sec;
85 newvalid |= OBD_MD_FLCTIME;
87 if (valid & OBD_MD_FLSIZE) {
88 dst->o_size = i_size_read(src);
89 newvalid |= OBD_MD_FLSIZE;
91 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
92 dst->o_blocks = src->i_blocks;
93 newvalid |= OBD_MD_FLBLOCKS;
95 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
96 dst->o_blksize = 1U << src->i_blkbits;
97 newvalid |= OBD_MD_FLBLKSZ;
99 if (valid & OBD_MD_FLTYPE) {
100 dst->o_mode = (dst->o_mode & S_IALLUGO) |
101 (src->i_mode & S_IFMT);
102 newvalid |= OBD_MD_FLTYPE;
104 if (valid & OBD_MD_FLMODE) {
105 dst->o_mode = (dst->o_mode & S_IFMT) |
106 (src->i_mode & S_IALLUGO);
107 newvalid |= OBD_MD_FLMODE;
109 if (valid & OBD_MD_FLUID) {
110 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
111 newvalid |= OBD_MD_FLUID;
113 if (valid & OBD_MD_FLGID) {
114 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
115 newvalid |= OBD_MD_FLGID;
117 if (valid & OBD_MD_FLFLAGS) {
118 dst->o_flags = src->i_flags;
119 newvalid |= OBD_MD_FLFLAGS;
121 dst->o_valid |= newvalid;
123 EXPORT_SYMBOL(obdo_from_inode);
125 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid)
127 CDEBUG(D_INODE, "src obdo "DOSTID" valid %#llx, dst obdo "DOSTID"\n",
128 POSTID(&src->o_oi), src->o_valid, POSTID(&dst->o_oi));
129 if (valid & OBD_MD_FLATIME)
130 dst->o_atime = src->o_atime;
131 if (valid & OBD_MD_FLMTIME)
132 dst->o_mtime = src->o_mtime;
133 if (valid & OBD_MD_FLCTIME)
134 dst->o_ctime = src->o_ctime;
135 if (valid & OBD_MD_FLSIZE)
136 dst->o_size = src->o_size;
137 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
138 dst->o_blocks = src->o_blocks;
139 if (valid & OBD_MD_FLBLKSZ)
140 dst->o_blksize = src->o_blksize;
141 if (valid & OBD_MD_FLTYPE)
142 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
143 if (valid & OBD_MD_FLMODE)
144 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
145 if (valid & OBD_MD_FLUID)
146 dst->o_uid = src->o_uid;
147 if (valid & OBD_MD_FLGID)
148 dst->o_gid = src->o_gid;
149 if (valid & OBD_MD_FLFLAGS)
150 dst->o_flags = src->o_flags;
151 if (valid & OBD_MD_FLFID) {
152 dst->o_parent_seq = src->o_parent_seq;
153 dst->o_parent_ver = src->o_parent_ver;
155 if (valid & OBD_MD_FLPARENT)
156 dst->o_parent_oid = src->o_parent_oid;
157 if (valid & OBD_MD_FLHANDLE)
158 dst->o_handle = src->o_handle;
160 dst->o_valid |= valid;
162 EXPORT_SYMBOL(obdo_cpy_md);
164 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj)
166 ioobj->ioo_oid = oa->o_oi;
167 if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
168 ostid_set_seq_mdt0(&ioobj->ioo_oid);
171 * Since 2.4 this does not contain o_mode in the low 16 bits.
172 * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs
174 ioobj->ioo_max_brw = 0;
176 EXPORT_SYMBOL(obdo_to_ioobj);
179 * Create an obdo to send over the wire
181 void lustre_set_wire_obdo(const struct obd_connect_data *ocd,
183 const struct obdo *lobdo)
189 if (!(wobdo->o_valid & OBD_MD_FLUID))
190 wobdo->o_uid = from_kuid(&init_user_ns, current_uid());
191 if (!(wobdo->o_valid & OBD_MD_FLGID))
192 wobdo->o_gid = from_kgid(&init_user_ns, current_gid());
194 if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
195 fid_seq_is_echo(ostid_seq(&lobdo->o_oi))) {
197 * Currently OBD_FL_OSTID will only be used when 2.4 echo
198 * client communicate with pre-2.4 server
200 wobdo->o_oi.oi.oi_id = fid_oid(&lobdo->o_oi.oi_fid);
201 wobdo->o_oi.oi.oi_seq = fid_seq(&lobdo->o_oi.oi_fid);
204 EXPORT_SYMBOL(lustre_set_wire_obdo);
207 * Create a local obdo from a wire based odbo
209 void lustre_get_wire_obdo(const struct obd_connect_data *ocd,
211 const struct obdo *wobdo)
217 if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
218 fid_seq_is_echo(wobdo->o_oi.oi.oi_seq)) {
220 lobdo->o_oi.oi_fid.f_seq = wobdo->o_oi.oi.oi_seq;
221 lobdo->o_oi.oi_fid.f_oid = wobdo->o_oi.oi.oi_id;
222 lobdo->o_oi.oi_fid.f_ver = 0;
225 EXPORT_SYMBOL(lustre_get_wire_obdo);