Whamcloud - gitweb
LU-6401 headers: move lu_fid, ost_id funcs out of lustre_idl.h
[fs/lustre-release.git] / lustre / include / lustre / lustre_fid.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 2016 Cray Inc, all rights reserved.
29  * Author: Ben Evans.
30  *
31  * all fid manipulation functions go here
32  *
33  * FIDS are globally unique within a Lustre filessytem, and are made up
34  * of three parts: sequence, Object ID, and version.
35  *
36  */
37 #ifndef _LUSTRE_LUSTRE_FID_H_
38 #define _LUSTRE_LUSTRE_FID_H_
39
40 #include <lustre/lustre_idl.h>
41
42 /** returns fid object sequence */
43 static inline __u64 fid_seq(const struct lu_fid *fid)
44 {
45         return fid->f_seq;
46 }
47
48 /** returns fid object id */
49 static inline __u32 fid_oid(const struct lu_fid *fid)
50 {
51         return fid->f_oid;
52 }
53
54 /** returns fid object version */
55 static inline __u32 fid_ver(const struct lu_fid *fid)
56 {
57         return fid->f_ver;
58 }
59
60 static inline void fid_zero(struct lu_fid *fid)
61 {
62         memset(fid, 0, sizeof(*fid));
63 }
64
65 static inline __u64 fid_ver_oid(const struct lu_fid *fid)
66 {
67         return (__u64)fid_ver(fid) << 32 | fid_oid(fid);
68 }
69
70 static inline bool fid_seq_is_mdt0(__u64 seq)
71 {
72         return seq == FID_SEQ_OST_MDT0;
73 }
74
75 static inline bool fid_seq_is_mdt(__u64 seq)
76 {
77         return seq == FID_SEQ_OST_MDT0 || seq >= FID_SEQ_NORMAL;
78 };
79
80 static inline bool fid_seq_is_echo(__u64 seq)
81 {
82         return seq == FID_SEQ_ECHO;
83 }
84
85 static inline bool fid_is_echo(const struct lu_fid *fid)
86 {
87         return fid_seq_is_echo(fid_seq(fid));
88 }
89
90 static inline bool fid_seq_is_llog(__u64 seq)
91 {
92         return seq == FID_SEQ_LLOG;
93 }
94
95 static inline bool fid_is_llog(const struct lu_fid *fid)
96 {
97         /* file with OID == 0 is not llog but contains last oid */
98         return fid_seq_is_llog(fid_seq(fid)) && fid_oid(fid) > 0;
99 }
100
101 static inline bool fid_seq_is_rsvd(__u64 seq)
102 {
103         return seq > FID_SEQ_OST_MDT0 && seq <= FID_SEQ_RSVD;
104 };
105
106 static inline bool fid_seq_is_special(__u64 seq)
107 {
108         return seq == FID_SEQ_SPECIAL;
109 };
110
111 static inline bool fid_seq_is_local_file(__u64 seq)
112 {
113         return seq == FID_SEQ_LOCAL_FILE ||
114                seq == FID_SEQ_LOCAL_NAME;
115 };
116
117 static inline bool fid_seq_is_root(__u64 seq)
118 {
119         return seq == FID_SEQ_ROOT;
120 }
121
122 static inline bool fid_seq_is_dot(__u64 seq)
123 {
124         return seq == FID_SEQ_DOT_LUSTRE;
125 }
126
127 static inline bool fid_seq_is_default(__u64 seq)
128 {
129         return seq == FID_SEQ_LOV_DEFAULT;
130 }
131
132 static inline bool fid_is_mdt0(const struct lu_fid *fid)
133 {
134         return fid_seq_is_mdt0(fid_seq(fid));
135 }
136
137 static inline void lu_root_fid(struct lu_fid *fid)
138 {
139         fid->f_seq = FID_SEQ_ROOT;
140         fid->f_oid = FID_OID_ROOT;
141         fid->f_ver = 0;
142 }
143
144 static inline void lu_echo_root_fid(struct lu_fid *fid)
145 {
146         fid->f_seq = FID_SEQ_ROOT;
147         fid->f_oid = FID_OID_ECHO_ROOT;
148         fid->f_ver = 0;
149 }
150
151 static inline void lu_update_log_fid(struct lu_fid *fid, __u32 index)
152 {
153         fid->f_seq = FID_SEQ_UPDATE_LOG;
154         fid->f_oid = index;
155         fid->f_ver = 0;
156 }
157
158 static inline void lu_update_log_dir_fid(struct lu_fid *fid, __u32 index)
159 {
160         fid->f_seq = FID_SEQ_UPDATE_LOG_DIR;
161         fid->f_oid = index;
162         fid->f_ver = 0;
163 }
164
165 /**
166  * Check if a fid is igif or not.
167  * \param fid the fid to be tested.
168  * \return true if the fid is an igif; otherwise false.
169  */
170 static inline bool fid_seq_is_igif(__u64 seq)
171 {
172         return seq >= FID_SEQ_IGIF && seq <= FID_SEQ_IGIF_MAX;
173 }
174
175 static inline bool fid_is_igif(const struct lu_fid *fid)
176 {
177         return fid_seq_is_igif(fid_seq(fid));
178 }
179
180 /**
181  * Check if a fid is idif or not.
182  * \param fid the fid to be tested.
183  * \return true if the fid is an idif; otherwise false.
184  */
185 static inline bool fid_seq_is_idif(__u64 seq)
186 {
187         return seq >= FID_SEQ_IDIF && seq <= FID_SEQ_IDIF_MAX;
188 }
189
190 static inline bool fid_is_idif(const struct lu_fid *fid)
191 {
192         return fid_seq_is_idif(fid_seq(fid));
193 }
194
195 static inline bool fid_is_local_file(const struct lu_fid *fid)
196 {
197         return fid_seq_is_local_file(fid_seq(fid));
198 }
199
200 static inline bool fid_seq_is_norm(__u64 seq)
201 {
202         return (seq >= FID_SEQ_NORMAL);
203 }
204
205 static inline bool fid_is_norm(const struct lu_fid *fid)
206 {
207         return fid_seq_is_norm(fid_seq(fid));
208 }
209
210 static inline int fid_is_layout_rbtree(const struct lu_fid *fid)
211 {
212         return fid_seq(fid) == FID_SEQ_LAYOUT_RBTREE;
213 }
214
215 static inline bool fid_seq_is_update_log(__u64 seq)
216 {
217         return seq == FID_SEQ_UPDATE_LOG;
218 }
219
220 static inline bool fid_is_update_log(const struct lu_fid *fid)
221 {
222         return fid_seq_is_update_log(fid_seq(fid));
223 }
224
225 static inline bool fid_seq_is_update_log_dir(__u64 seq)
226 {
227         return seq == FID_SEQ_UPDATE_LOG_DIR;
228 }
229
230 static inline bool fid_is_update_log_dir(const struct lu_fid *fid)
231 {
232         return fid_seq_is_update_log_dir(fid_seq(fid));
233 }
234
235 /* convert an OST objid into an IDIF FID SEQ number */
236 static inline __u64 fid_idif_seq(__u64 id, __u32 ost_idx)
237 {
238         return FID_SEQ_IDIF | (ost_idx << 16) | ((id >> 32) & 0xffff);
239 }
240
241 /* convert a packed IDIF FID into an OST objid */
242 static inline __u64 fid_idif_id(__u64 seq, __u32 oid, __u32 ver)
243 {
244         return ((__u64)ver << 48) | ((seq & 0xffff) << 32) | oid;
245 }
246
247 static inline __u32 idif_ost_idx(__u64 seq)
248 {
249         return (seq >> 16) & 0xffff;
250 }
251
252 /* extract ost index from IDIF FID */
253 static inline __u32 fid_idif_ost_idx(const struct lu_fid *fid)
254 {
255         return idif_ost_idx(fid_seq(fid));
256 }
257
258 /* Check whether the fid is for LAST_ID */
259 static inline bool fid_is_last_id(const struct lu_fid *fid)
260 {
261         return !fid_oid(fid) && fid_seq(fid) != FID_SEQ_UPDATE_LOG &&
262                fid_seq(fid) != FID_SEQ_UPDATE_LOG_DIR;
263 }
264
265 /**
266  * Get inode number from an igif.
267  * \param fid an igif to get inode number from.
268  * \return inode number for the igif.
269  */
270 static inline ino_t lu_igif_ino(const struct lu_fid *fid)
271 {
272         return fid_seq(fid);
273 }
274
275 /**
276  * Get inode generation from an igif.
277  * \param fid an igif to get inode generation from.
278  * \return inode generation for the igif.
279  */
280 static inline __u32 lu_igif_gen(const struct lu_fid *fid)
281 {
282         return fid_oid(fid);
283 }
284
285 /**
286  * Build igif from the inode number/generation.
287  */
288 static inline void lu_igif_build(struct lu_fid *fid, __u32 ino, __u32 gen)
289 {
290         fid->f_seq = ino;
291         fid->f_oid = gen;
292         fid->f_ver = 0;
293 }
294
295 /*
296  * Fids are transmitted across network (in the sender byte-ordering),
297  * and stored on disk in big-endian order.
298  */
299 static inline void fid_cpu_to_le(struct lu_fid *dst, const struct lu_fid *src)
300 {
301         dst->f_seq = __cpu_to_le64(fid_seq(src));
302         dst->f_oid = __cpu_to_le32(fid_oid(src));
303         dst->f_ver = __cpu_to_le32(fid_ver(src));
304 }
305
306 static inline void fid_le_to_cpu(struct lu_fid *dst, const struct lu_fid *src)
307 {
308         dst->f_seq = __le64_to_cpu(fid_seq(src));
309         dst->f_oid = __le32_to_cpu(fid_oid(src));
310         dst->f_ver = __le32_to_cpu(fid_ver(src));
311 }
312
313 static inline void fid_cpu_to_be(struct lu_fid *dst, const struct lu_fid *src)
314 {
315         dst->f_seq = __cpu_to_be64(fid_seq(src));
316         dst->f_oid = __cpu_to_be32(fid_oid(src));
317         dst->f_ver = __cpu_to_be32(fid_ver(src));
318 }
319
320 static inline void fid_be_to_cpu(struct lu_fid *dst, const struct lu_fid *src)
321 {
322         dst->f_seq = __be64_to_cpu(fid_seq(src));
323         dst->f_oid = __be32_to_cpu(fid_oid(src));
324         dst->f_ver = __be32_to_cpu(fid_ver(src));
325 }
326
327 static inline bool fid_is_sane(const struct lu_fid *fid)
328 {
329         return fid && ((fid_seq(fid) >= FID_SEQ_START && !fid_ver(fid)) ||
330                         fid_is_igif(fid) || fid_is_idif(fid) ||
331                         fid_seq_is_rsvd(fid_seq(fid)));
332 }
333
334 static inline bool lu_fid_eq(const struct lu_fid *f0, const struct lu_fid *f1)
335 {
336         return !memcmp(f0, f1, sizeof(*f0));
337 }
338
339 static inline int lu_fid_cmp(const struct lu_fid *f0,
340                              const struct lu_fid *f1)
341 {
342         if (fid_seq(f0) != fid_seq(f1))
343                 return fid_seq(f0) > fid_seq(f1) ? 1 : -1;
344
345         if (fid_oid(f0) != fid_oid(f1))
346                 return fid_oid(f0) > fid_oid(f1) ? 1 : -1;
347
348         if (fid_ver(f0) != fid_ver(f1))
349                 return fid_ver(f0) > fid_ver(f1) ? 1 : -1;
350
351         return 0;
352 }
353 #endif