Whamcloud - gitweb
LU-9235 libcfs: don't dump stack if just touched
[fs/lustre-release.git] / lustre / include / uapi / linux / lustre_ostid.h
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  *
28  * Copyright 2015 Cray Inc, all rights reserved.
29  * Author: Ben Evans.
30  *
31  * Define ost_id  associated functions
32  */
33
34 #ifndef _UAPI_LUSTRE_OSTID_H_
35 #define _UAPI_LUSTRE_OSTID_H_
36
37 /*
38  * This is due to us being out of kernel and the way the OpenSFS branch
39  * handles CFLAGS. Upstream will just have linux/lustre_fid.h
40  */
41 #ifdef __KERNEL__
42 #include <uapi/linux/lustre_fid.h>
43 #else
44 #include <linux/lustre_fid.h>
45 #endif
46
47 static inline __u64 lmm_oi_id(const struct ost_id *oi)
48 {
49         return oi->oi.oi_id;
50 }
51
52 static inline __u64 lmm_oi_seq(const struct ost_id *oi)
53 {
54         return oi->oi.oi_seq;
55 }
56
57 static inline void lmm_oi_set_seq(struct ost_id *oi, __u64 seq)
58 {
59         oi->oi.oi_seq = seq;
60 }
61
62 static inline void lmm_oi_set_id(struct ost_id *oi, __u64 oid)
63 {
64         oi->oi.oi_id = oid;
65 }
66
67 static inline void lmm_oi_le_to_cpu(struct ost_id *dst_oi,
68                                     const struct ost_id *src_oi)
69 {
70         dst_oi->oi.oi_id = __le64_to_cpu(src_oi->oi.oi_id);
71         dst_oi->oi.oi_seq = __le64_to_cpu(src_oi->oi.oi_seq);
72 }
73
74 static inline void lmm_oi_cpu_to_le(struct ost_id *dst_oi,
75                                     const struct ost_id *src_oi)
76 {
77         dst_oi->oi.oi_id = __cpu_to_le64(src_oi->oi.oi_id);
78         dst_oi->oi.oi_seq = __cpu_to_le64(src_oi->oi.oi_seq);
79 }
80
81 /* extract OST sequence (group) from a wire ost_id (id/seq) pair */
82 static inline __u64 ostid_seq(const struct ost_id *ostid)
83 {
84         if (fid_seq_is_mdt0(ostid->oi.oi_seq))
85                 return FID_SEQ_OST_MDT0;
86
87         if (fid_seq_is_default(ostid->oi.oi_seq))
88                 return FID_SEQ_LOV_DEFAULT;
89
90         if (fid_is_idif(&ostid->oi_fid))
91                 return FID_SEQ_OST_MDT0;
92
93         return fid_seq(&ostid->oi_fid);
94 }
95
96 /* extract OST objid from a wire ost_id (id/seq) pair */
97 static inline __u64 ostid_id(const struct ost_id *ostid)
98 {
99         if (fid_seq_is_mdt0(ostid->oi.oi_seq))
100                 return ostid->oi.oi_id & IDIF_OID_MASK;
101
102         if (fid_seq_is_default(ostid->oi.oi_seq))
103                 return ostid->oi.oi_id;
104
105         if (fid_is_idif(&ostid->oi_fid))
106                 return fid_idif_id(fid_seq(&ostid->oi_fid),
107                                    fid_oid(&ostid->oi_fid), 0);
108
109         return fid_oid(&ostid->oi_fid);
110 }
111
112 static inline void ostid_set_seq(struct ost_id *oi, __u64 seq)
113 {
114         if (fid_seq_is_mdt0(seq) || fid_seq_is_default(seq)) {
115                 oi->oi.oi_seq = seq;
116         } else {
117                 oi->oi_fid.f_seq = seq;
118                 /*
119                  * Note: if f_oid + f_ver is zero, we need init it
120                  * to be 1, otherwise, ostid_seq will treat this
121                  * as old ostid (oi_seq == 0)
122                  */
123                 if (!oi->oi_fid.f_oid && !oi->oi_fid.f_ver)
124                         oi->oi_fid.f_oid = LUSTRE_FID_INIT_OID;
125         }
126 }
127
128 static inline void ostid_set_seq_mdt0(struct ost_id *oi)
129 {
130         ostid_set_seq(oi, FID_SEQ_OST_MDT0);
131 }
132
133 static inline void ostid_set_seq_echo(struct ost_id *oi)
134 {
135         ostid_set_seq(oi, FID_SEQ_ECHO);
136 }
137
138 static inline void ostid_set_seq_llog(struct ost_id *oi)
139 {
140         ostid_set_seq(oi, FID_SEQ_LLOG);
141 }
142
143 static inline void ostid_cpu_to_le(const struct ost_id *src_oi,
144                                    struct ost_id *dst_oi)
145 {
146         if (fid_seq_is_mdt0(src_oi->oi.oi_seq)) {
147                 dst_oi->oi.oi_id = __cpu_to_le64(src_oi->oi.oi_id);
148                 dst_oi->oi.oi_seq = __cpu_to_le64(src_oi->oi.oi_seq);
149         } else {
150                 fid_cpu_to_le(&dst_oi->oi_fid, &src_oi->oi_fid);
151         }
152 }
153
154 static inline void ostid_le_to_cpu(const struct ost_id *src_oi,
155                                    struct ost_id *dst_oi)
156 {
157         if (fid_seq_is_mdt0(src_oi->oi.oi_seq)) {
158                 dst_oi->oi.oi_id = __le64_to_cpu(src_oi->oi.oi_id);
159                 dst_oi->oi.oi_seq = __le64_to_cpu(src_oi->oi.oi_seq);
160         } else {
161                 fid_le_to_cpu(&dst_oi->oi_fid, &src_oi->oi_fid);
162         }
163 }
164
165 /**
166  * Sigh, because pre-2.4 uses
167  * struct lov_mds_md_v1 {
168  *      ........
169  *      __u64 lmm_object_id;
170  *      __u64 lmm_object_seq;
171  *      ......
172  *      }
173  * to identify the LOV(MDT) object, and lmm_object_seq will
174  * be normal_fid, which make it hard to combine these conversion
175  * to ostid_to FID. so we will do lmm_oi/fid conversion separately
176  *
177  * We can tell the lmm_oi by this way,
178  * 1.8: lmm_object_id = {inode}, lmm_object_gr = 0
179  * 2.1: lmm_object_id = {oid < 128k}, lmm_object_seq = FID_SEQ_NORMAL
180  * 2.4: lmm_oi.f_seq = FID_SEQ_NORMAL, lmm_oi.f_oid = {oid < 128k},
181  *      lmm_oi.f_ver = 0
182  *
183  * But currently lmm_oi/lsm_oi does not have any "real" usages,
184  * except for printing some information, and the user can always
185  * get the real FID from LMA, besides this multiple case check might
186  * make swab more complicate. So we will keep using id/seq for lmm_oi.
187  */
188
189 static inline void fid_to_lmm_oi(const struct lu_fid *fid,
190                                  struct ost_id *oi)
191 {
192         oi->oi.oi_id = fid_oid(fid);
193         oi->oi.oi_seq = fid_seq(fid);
194 }
195
196 /**
197  * Unpack an OST object id/seq (group) into a FID.  This is needed for
198  * converting all obdo, lmm, lsm, etc. 64-bit id/seq pairs into proper
199  * FIDs.  Note that if an id/seq is already in FID/IDIF format it will
200  * be passed through unchanged.  Only legacy OST objects in "group 0"
201  * will be mapped into the IDIF namespace so that they can fit into the
202  * struct lu_fid fields without loss.
203  */
204 static inline int ostid_to_fid(struct lu_fid *fid, const struct ost_id *ostid,
205                                __u32 ost_idx)
206 {
207         __u64 seq = ostid_seq(ostid);
208
209         if (ost_idx > 0xffff)
210                 return -EBADF;
211
212         if (fid_seq_is_mdt0(seq)) {
213                 __u64 oid = ostid_id(ostid);
214
215                 /* This is a "legacy" (old 1.x/2.early) OST object in "group 0"
216                  * that we map into the IDIF namespace.  It allows up to 2^48
217                  * objects per OST, as this is the object namespace that has
218                  * been in production for years.  This can handle create rates
219                  * of 1M objects/s/OST for 9 years, or combinations thereof.
220                  */
221                 if (oid >= IDIF_MAX_OID)
222                         return -EBADF;
223
224                 fid->f_seq = fid_idif_seq(oid, ost_idx);
225                 /* truncate to 32 bits by assignment */
226                 fid->f_oid = oid;
227                 /* in theory, not currently used */
228                 fid->f_ver = oid >> 48;
229         } else if (!fid_seq_is_default(seq)) {
230                 /* This is either an IDIF object, which identifies objects
231                  * across all OSTs, or a regular FID.  The IDIF namespace
232                  * maps legacy OST objects into the FID namespace.  In both
233                  * cases, we just pass the FID through, no conversion needed.
234                  */
235                 if (ostid->oi_fid.f_ver)
236                         return -EBADF;
237
238                 *fid = ostid->oi_fid;
239         }
240
241         return 0;
242 }
243 #endif /* _UAPI_LUSTRE_OSTID_H_ */