Whamcloud - gitweb
LU-8585 llite: add special fid handling for fhandle API
[fs/lustre-release.git] / lustre / include / uapi / linux / 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, 2017, 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 _UAPI_LUSTRE_FID_H_
38 #define _UAPI_LUSTRE_FID_H_
39
40 #include <linux/types.h>
41 #include <linux/lustre/lustre_idl.h>
42
43 /** Special fid for root directory */
44 static const struct lu_fid LU_ROOT_FID = {
45         .f_seq = FID_SEQ_ROOT,
46         .f_oid = FID_OID_ROOT,
47         .f_ver = 0x0000000000000000
48 };
49
50 /** Special fid for ".lustre" directory */
51 static const struct lu_fid LU_DOT_LUSTRE_FID = {
52         .f_seq = FID_SEQ_DOT_LUSTRE,
53         .f_oid = FID_OID_DOT_LUSTRE,
54         .f_ver = 0x0000000000000000
55 };
56
57 /** Special fid for "fid" special object in .lustre */
58 static const struct lu_fid LU_OBF_FID = {
59         .f_seq = FID_SEQ_DOT_LUSTRE,
60         .f_oid = FID_OID_DOT_LUSTRE_OBF,
61         .f_ver = 0x0000000000000000
62 };
63
64 /** returns fid object sequence */
65 static inline __u64 fid_seq(const struct lu_fid *fid)
66 {
67         return fid->f_seq;
68 }
69
70 /** returns fid object id */
71 static inline __u32 fid_oid(const struct lu_fid *fid)
72 {
73         return fid->f_oid;
74 }
75
76 /** returns fid object version */
77 static inline __u32 fid_ver(const struct lu_fid *fid)
78 {
79         return fid->f_ver;
80 }
81
82 static inline void fid_zero(struct lu_fid *fid)
83 {
84         memset(fid, 0, sizeof(*fid));
85 }
86
87 static inline __u64 fid_ver_oid(const struct lu_fid *fid)
88 {
89         return (__u64)fid_ver(fid) << 32 | fid_oid(fid);
90 }
91
92 static inline bool fid_seq_is_mdt0(__u64 seq)
93 {
94         return seq == FID_SEQ_OST_MDT0;
95 }
96
97 static inline bool fid_seq_is_mdt(__u64 seq)
98 {
99         return seq == FID_SEQ_OST_MDT0 || seq >= FID_SEQ_NORMAL;
100 };
101
102 static inline bool fid_seq_is_echo(__u64 seq)
103 {
104         return seq == FID_SEQ_ECHO;
105 }
106
107 static inline bool fid_is_echo(const struct lu_fid *fid)
108 {
109         return fid_seq_is_echo(fid_seq(fid));
110 }
111
112 static inline bool fid_seq_is_llog(__u64 seq)
113 {
114         return seq == FID_SEQ_LLOG;
115 }
116
117 static inline bool fid_is_llog(const struct lu_fid *fid)
118 {
119         /* file with OID == 0 is not llog but contains last oid */
120         return fid_seq_is_llog(fid_seq(fid)) && fid_oid(fid) > 0;
121 }
122
123 static inline bool fid_seq_is_rsvd(__u64 seq)
124 {
125         return seq > FID_SEQ_OST_MDT0 && seq <= FID_SEQ_RSVD;
126 };
127
128 static inline bool fid_seq_is_special(__u64 seq)
129 {
130         return seq == FID_SEQ_SPECIAL;
131 };
132
133 static inline bool fid_seq_is_local_file(__u64 seq)
134 {
135         return seq == FID_SEQ_LOCAL_FILE ||
136                seq == FID_SEQ_LOCAL_NAME;
137 };
138
139 static inline bool fid_seq_is_root(__u64 seq)
140 {
141         return seq == FID_SEQ_ROOT;
142 }
143
144 static inline bool fid_seq_is_dot(__u64 seq)
145 {
146         return seq == FID_SEQ_DOT_LUSTRE;
147 }
148
149 static inline bool fid_seq_is_default(__u64 seq)
150 {
151         return seq == FID_SEQ_LOV_DEFAULT;
152 }
153
154 static inline bool fid_is_mdt0(const struct lu_fid *fid)
155 {
156         return fid_seq_is_mdt0(fid_seq(fid));
157 }
158
159 static inline void lu_root_fid(struct lu_fid *fid)
160 {
161         fid->f_seq = FID_SEQ_ROOT;
162         fid->f_oid = FID_OID_ROOT;
163         fid->f_ver = 0;
164 }
165
166 static inline void lu_echo_root_fid(struct lu_fid *fid)
167 {
168         fid->f_seq = FID_SEQ_ROOT;
169         fid->f_oid = FID_OID_ECHO_ROOT;
170         fid->f_ver = 0;
171 }
172
173 static inline void lu_update_log_fid(struct lu_fid *fid, __u32 index)
174 {
175         fid->f_seq = FID_SEQ_UPDATE_LOG;
176         fid->f_oid = index;
177         fid->f_ver = 0;
178 }
179
180 static inline void lu_update_log_dir_fid(struct lu_fid *fid, __u32 index)
181 {
182         fid->f_seq = FID_SEQ_UPDATE_LOG_DIR;
183         fid->f_oid = index;
184         fid->f_ver = 0;
185 }
186
187 /**
188  * Check if a fid is igif or not.
189  * \param fid the fid to be tested.
190  * \return true if the fid is an igif; otherwise false.
191  */
192 static inline bool fid_seq_is_igif(__u64 seq)
193 {
194         return seq >= FID_SEQ_IGIF && seq <= FID_SEQ_IGIF_MAX;
195 }
196
197 static inline bool fid_is_igif(const struct lu_fid *fid)
198 {
199         return fid_seq_is_igif(fid_seq(fid));
200 }
201
202 /**
203  * Check if a fid is idif or not.
204  * \param fid the fid to be tested.
205  * \return true if the fid is an idif; otherwise false.
206  */
207 static inline bool fid_seq_is_idif(__u64 seq)
208 {
209         return seq >= FID_SEQ_IDIF && seq <= FID_SEQ_IDIF_MAX;
210 }
211
212 static inline bool fid_is_idif(const struct lu_fid *fid)
213 {
214         return fid_seq_is_idif(fid_seq(fid));
215 }
216
217 static inline bool fid_is_local_file(const struct lu_fid *fid)
218 {
219         return fid_seq_is_local_file(fid_seq(fid));
220 }
221
222 static inline bool fid_seq_is_norm(__u64 seq)
223 {
224         return (seq >= FID_SEQ_NORMAL);
225 }
226
227 static inline bool fid_is_norm(const struct lu_fid *fid)
228 {
229         return fid_seq_is_norm(fid_seq(fid));
230 }
231
232 static inline int fid_is_layout_rbtree(const struct lu_fid *fid)
233 {
234         return fid_seq(fid) == FID_SEQ_LAYOUT_RBTREE;
235 }
236
237 static inline bool fid_seq_is_update_log(__u64 seq)
238 {
239         return seq == FID_SEQ_UPDATE_LOG;
240 }
241
242 static inline bool fid_is_update_log(const struct lu_fid *fid)
243 {
244         return fid_seq_is_update_log(fid_seq(fid));
245 }
246
247 static inline bool fid_seq_is_update_log_dir(__u64 seq)
248 {
249         return seq == FID_SEQ_UPDATE_LOG_DIR;
250 }
251
252 static inline bool fid_is_update_log_dir(const struct lu_fid *fid)
253 {
254         return fid_seq_is_update_log_dir(fid_seq(fid));
255 }
256
257 /* convert an OST objid into an IDIF FID SEQ number */
258 static inline __u64 fid_idif_seq(__u64 id, __u32 ost_idx)
259 {
260         return FID_SEQ_IDIF | (ost_idx << 16) | ((id >> 32) & 0xffff);
261 }
262
263 /* convert a packed IDIF FID into an OST objid */
264 static inline __u64 fid_idif_id(__u64 seq, __u32 oid, __u32 ver)
265 {
266         return ((__u64)ver << 48) | ((seq & 0xffff) << 32) | oid;
267 }
268
269 static inline __u32 idif_ost_idx(__u64 seq)
270 {
271         return (seq >> 16) & 0xffff;
272 }
273
274 /* extract ost index from IDIF FID */
275 static inline __u32 fid_idif_ost_idx(const struct lu_fid *fid)
276 {
277         return idif_ost_idx(fid_seq(fid));
278 }
279
280 /* Check whether the fid is for LAST_ID */
281 static inline bool fid_is_last_id(const struct lu_fid *fid)
282 {
283         if (fid_oid(fid) != 0)
284                 return false;
285
286         if (fid_is_idif(fid) && ((fid_seq(fid) & 0xFFFF) != 0))
287                 return false;
288
289         if (fid_seq(fid) == FID_SEQ_UPDATE_LOG ||
290             fid_seq(fid) == FID_SEQ_UPDATE_LOG_DIR ||
291             fid_seq_is_igif(fid_seq(fid)))
292                 return false;
293
294         return true;
295 }
296
297 /**
298  * Get inode number from an igif.
299  * \param fid an igif to get inode number from.
300  * \return inode number for the igif.
301  */
302 static inline __kernel_ino_t lu_igif_ino(const struct lu_fid *fid)
303 {
304         return fid_seq(fid);
305 }
306
307 /**
308  * Get inode generation from an igif.
309  * \param fid an igif to get inode generation from.
310  * \return inode generation for the igif.
311  */
312 static inline __u32 lu_igif_gen(const struct lu_fid *fid)
313 {
314         return fid_oid(fid);
315 }
316
317 /**
318  * Build igif from the inode number/generation.
319  */
320 static inline void lu_igif_build(struct lu_fid *fid, __u32 ino, __u32 gen)
321 {
322         fid->f_seq = ino;
323         fid->f_oid = gen;
324         fid->f_ver = 0;
325 }
326
327 /*
328  * Fids are transmitted across network (in the sender byte-ordering),
329  * and stored on disk in big-endian order.
330  */
331 static inline void fid_cpu_to_le(struct lu_fid *dst, const struct lu_fid *src)
332 {
333         dst->f_seq = __cpu_to_le64(fid_seq(src));
334         dst->f_oid = __cpu_to_le32(fid_oid(src));
335         dst->f_ver = __cpu_to_le32(fid_ver(src));
336 }
337
338 static inline void fid_le_to_cpu(struct lu_fid *dst, const struct lu_fid *src)
339 {
340         dst->f_seq = __le64_to_cpu(fid_seq(src));
341         dst->f_oid = __le32_to_cpu(fid_oid(src));
342         dst->f_ver = __le32_to_cpu(fid_ver(src));
343 }
344
345 static inline void fid_cpu_to_be(struct lu_fid *dst, const struct lu_fid *src)
346 {
347         dst->f_seq = __cpu_to_be64(fid_seq(src));
348         dst->f_oid = __cpu_to_be32(fid_oid(src));
349         dst->f_ver = __cpu_to_be32(fid_ver(src));
350 }
351
352 static inline void fid_be_to_cpu(struct lu_fid *dst, const struct lu_fid *src)
353 {
354         dst->f_seq = __be64_to_cpu(fid_seq(src));
355         dst->f_oid = __be32_to_cpu(fid_oid(src));
356         dst->f_ver = __be32_to_cpu(fid_ver(src));
357 }
358
359 static inline bool fid_is_sane(const struct lu_fid *fid)
360 {
361         return fid && ((fid_seq(fid) >= FID_SEQ_START && !fid_ver(fid)) ||
362                         fid_is_igif(fid) || fid_is_idif(fid) ||
363                         fid_seq_is_rsvd(fid_seq(fid)));
364 }
365
366 static inline bool lu_fid_eq(const struct lu_fid *f0, const struct lu_fid *f1)
367 {
368         return !memcmp(f0, f1, sizeof(*f0));
369 }
370
371 static inline int lu_fid_cmp(const struct lu_fid *f0,
372                              const struct lu_fid *f1)
373 {
374         if (fid_seq(f0) != fid_seq(f1))
375                 return fid_seq(f0) > fid_seq(f1) ? 1 : -1;
376
377         if (fid_oid(f0) != fid_oid(f1))
378                 return fid_oid(f0) > fid_oid(f1) ? 1 : -1;
379
380         if (fid_ver(f0) != fid_ver(f1))
381                 return fid_ver(f0) > fid_ver(f1) ? 1 : -1;
382
383         return 0;
384 }
385
386 /**
387  * Flatten 128-bit FID values into a 64-bit value for use as an inode number.
388  * For non-IGIF FIDs this starts just over 2^32, and continues without
389  * conflict until 2^64, at which point we wrap the high 24 bits of the SEQ
390  * into the range where there may not be many OID values in use, to minimize
391  * the risk of conflict.
392  *
393  * Suppose LUSTRE_SEQ_MAX_WIDTH less than (1 << 24) which is currently true,
394  * the time between re-used inode numbers is very long - 2^40 SEQ numbers,
395  * or about 2^40 client mounts, if clients create less than 2^24 files/mount.
396  */
397 static inline __u64 fid_flatten64(const struct lu_fid *fid)
398 {
399         __u64 ino;
400         __u64 seq;
401
402         if (fid_is_igif(fid)) {
403                 ino = lu_igif_ino(fid);
404                 return ino;
405         }
406
407         seq = fid_seq(fid);
408
409         ino = (seq << 24) + ((seq >> 24) & 0xffffff0000ULL) + fid_oid(fid);
410
411         return ino ?: fid_oid(fid);
412 }
413
414 /**
415  * map fid to 32 bit value for ino on 32bit systems.
416  */
417 static inline __u32 fid_flatten32(const struct lu_fid *fid)
418 {
419         __u32 ino;
420         __u64 seq;
421
422         if (fid_is_igif(fid)) {
423                 ino = lu_igif_ino(fid);
424                 return ino;
425         }
426
427         seq = fid_seq(fid) - FID_SEQ_START;
428
429         /* Map the high bits of the OID into higher bits of the inode number so
430          * that inodes generated at about the same time have a reduced chance
431          * of collisions. This will give a period of 2^12 = 1024 unique clients
432          * (from SEQ) and up to min(LUSTRE_SEQ_MAX_WIDTH, 2^20) = 128k objects
433          * (from OID), or up to 128M inodes without collisions for new files.
434          */
435         ino = ((seq & 0x000fffffULL) << 12) + ((seq >> 8) & 0xfffff000) +
436               (seq >> (64 - (40-8)) & 0xffffff00) +
437               (fid_oid(fid) & 0xff000fff) + ((fid_oid(fid) & 0x00fff000) << 8);
438
439         return ino ?: fid_oid(fid);
440 }
441
442 #if __BITS_PER_LONG == 32
443 #define fid_flatten_long fid_flatten32
444 #elif __BITS_PER_LONG == 64
445 #define fid_flatten_long fid_flatten64
446 #else
447 #error "Wordsize not 32 or 64"
448 #endif
449
450 #endif