Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[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.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) 2012, 2014, 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 <linux/user_namespace.h>
42 #ifdef HAVE_UIDGID_HEADER
43 # include <linux/uidgid.h>
44 #endif
45 #include <obd_class.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_obdo.h>
48
49 void obdo_set_parent_fid(struct obdo *dst, const struct lu_fid *parent)
50 {
51         dst->o_parent_oid = fid_oid(parent);
52         dst->o_parent_seq = fid_seq(parent);
53         dst->o_parent_ver = fid_ver(parent);
54         dst->o_valid |= OBD_MD_FLGENER | OBD_MD_FLFID;
55 }
56 EXPORT_SYMBOL(obdo_set_parent_fid);
57
58 /* WARNING: the file systems must take care not to tinker with
59    attributes they don't manage (such as blocks). */
60 void obdo_from_inode(struct obdo *dst, struct inode *src, u64 valid)
61 {
62         u64 newvalid = 0;
63
64         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
65                 CDEBUG(D_INODE, "valid %#llx, new time %lu/%lu\n",
66                         valid, LTIME_S(src->i_mtime),
67                         LTIME_S(src->i_ctime));
68
69         if (valid & OBD_MD_FLATIME) {
70                 dst->o_atime = LTIME_S(src->i_atime);
71                 newvalid |= OBD_MD_FLATIME;
72         }
73         if (valid & OBD_MD_FLMTIME) {
74                 dst->o_mtime = LTIME_S(src->i_mtime);
75                 newvalid |= OBD_MD_FLMTIME;
76         }
77         if (valid & OBD_MD_FLCTIME) {
78                 dst->o_ctime = LTIME_S(src->i_ctime);
79                 newvalid |= OBD_MD_FLCTIME;
80         }
81         if (valid & OBD_MD_FLSIZE) {
82                 dst->o_size = i_size_read(src);
83                 newvalid |= OBD_MD_FLSIZE;
84         }
85         if (valid & OBD_MD_FLBLOCKS) {  /* allocation of space (x512 bytes) */
86                 dst->o_blocks = src->i_blocks;
87                 newvalid |= OBD_MD_FLBLOCKS;
88         }
89         if (valid & OBD_MD_FLBLKSZ) {   /* optimal block size */
90                 dst->o_blksize = 1U << src->i_blkbits;
91                 newvalid |= OBD_MD_FLBLKSZ;
92         }
93         if (valid & OBD_MD_FLTYPE) {
94                 dst->o_mode = (dst->o_mode & S_IALLUGO) |
95                               (src->i_mode & S_IFMT);
96                 newvalid |= OBD_MD_FLTYPE;
97         }
98         if (valid & OBD_MD_FLMODE) {
99                 dst->o_mode = (dst->o_mode & S_IFMT) |
100                               (src->i_mode & S_IALLUGO);
101                 newvalid |= OBD_MD_FLMODE;
102         }
103         if (valid & OBD_MD_FLUID) {
104                 dst->o_uid = from_kuid(&init_user_ns, src->i_uid);
105                 newvalid |= OBD_MD_FLUID;
106         }
107         if (valid & OBD_MD_FLGID) {
108                 dst->o_gid = from_kgid(&init_user_ns, src->i_gid);
109                 newvalid |= OBD_MD_FLGID;
110         }
111         if (valid & OBD_MD_FLFLAGS) {
112                 dst->o_flags = src->i_flags;
113                 newvalid |= OBD_MD_FLFLAGS;
114         }
115         dst->o_valid |= newvalid;
116 }
117 EXPORT_SYMBOL(obdo_from_inode);
118
119 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid)
120 {
121         CDEBUG(D_INODE, "src obdo "DOSTID" valid %#llx, dst obdo "DOSTID"\n",
122                POSTID(&src->o_oi), src->o_valid, POSTID(&dst->o_oi));
123         if (valid & OBD_MD_FLATIME)
124                 dst->o_atime = src->o_atime;
125         if (valid & OBD_MD_FLMTIME)
126                 dst->o_mtime = src->o_mtime;
127         if (valid & OBD_MD_FLCTIME)
128                 dst->o_ctime = src->o_ctime;
129         if (valid & OBD_MD_FLSIZE)
130                 dst->o_size = src->o_size;
131         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
132                 dst->o_blocks = src->o_blocks;
133         if (valid & OBD_MD_FLBLKSZ)
134                 dst->o_blksize = src->o_blksize;
135         if (valid & OBD_MD_FLTYPE)
136                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
137         if (valid & OBD_MD_FLMODE)
138                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
139         if (valid & OBD_MD_FLUID)
140                 dst->o_uid = src->o_uid;
141         if (valid & OBD_MD_FLGID)
142                 dst->o_gid = src->o_gid;
143         if (valid & OBD_MD_FLFLAGS)
144                 dst->o_flags = src->o_flags;
145         if (valid & OBD_MD_FLFID) {
146                 dst->o_parent_seq = src->o_parent_seq;
147                 dst->o_parent_ver = src->o_parent_ver;
148         }
149         if (valid & OBD_MD_FLGENER)
150                 dst->o_parent_oid = src->o_parent_oid;
151         if (valid & OBD_MD_FLHANDLE)
152                 dst->o_handle = src->o_handle;
153
154         dst->o_valid |= valid;
155 }
156 EXPORT_SYMBOL(obdo_cpy_md);
157
158 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj)
159 {
160         ioobj->ioo_oid = oa->o_oi;
161         if (unlikely(!(oa->o_valid & OBD_MD_FLGROUP)))
162                 ostid_set_seq_mdt0(&ioobj->ioo_oid);
163
164         /* Since 2.4 this does not contain o_mode in the low 16 bits.
165          * Instead, it holds (bd_md_max_brw - 1) for multi-bulk BRW RPCs */
166         ioobj->ioo_max_brw = 0;
167 }
168 EXPORT_SYMBOL(obdo_to_ioobj);
169
170 /**
171  * Create an obdo to send over the wire
172  */
173 void lustre_set_wire_obdo(const struct obd_connect_data *ocd,
174                                  struct obdo *wobdo,
175                                  const struct obdo *lobdo)
176 {
177         *wobdo = *lobdo;
178         wobdo->o_flags &= ~OBD_FL_LOCAL_MASK;
179         if (ocd == NULL)
180                 return;
181
182         if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
183             fid_seq_is_echo(ostid_seq(&lobdo->o_oi))) {
184                 /* Currently OBD_FL_OSTID will only be used when 2.4 echo
185                  * client communicate with pre-2.4 server */
186                 wobdo->o_oi.oi.oi_id = fid_oid(&lobdo->o_oi.oi_fid);
187                 wobdo->o_oi.oi.oi_seq = fid_seq(&lobdo->o_oi.oi_fid);
188         }
189 }
190 EXPORT_SYMBOL(lustre_set_wire_obdo);
191
192 /**
193  * Create a local obdo from a wire based odbo
194  */
195 void lustre_get_wire_obdo(const struct obd_connect_data *ocd,
196                                  struct obdo *lobdo,
197                                  const struct obdo *wobdo)
198 {
199         __u32 local_flags = 0;
200
201         if (lobdo->o_valid & OBD_MD_FLFLAGS)
202                 local_flags = lobdo->o_flags & OBD_FL_LOCAL_MASK;
203
204         *lobdo = *wobdo;
205         if (local_flags != 0) {
206                 lobdo->o_valid |= OBD_MD_FLFLAGS;
207                 lobdo->o_flags &= ~OBD_FL_LOCAL_MASK;
208                 lobdo->o_flags |= local_flags;
209         }
210         if (ocd == NULL)
211                 return;
212
213         if (unlikely(!(ocd->ocd_connect_flags & OBD_CONNECT_FID)) &&
214             fid_seq_is_echo(wobdo->o_oi.oi.oi_seq)) {
215                 /* see above */
216                 lobdo->o_oi.oi_fid.f_seq = wobdo->o_oi.oi.oi_seq;
217                 lobdo->o_oi.oi_fid.f_oid = wobdo->o_oi.oi.oi_id;
218                 lobdo->o_oi.oi_fid.f_ver = 0;
219         }
220 }
221 EXPORT_SYMBOL(lustre_get_wire_obdo);