Whamcloud - gitweb
LU-1842 iam: store key & rec in LE order for quota IAM files
[fs/lustre-release.git] / lustre / include / 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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/lustre_fid.h
37  *
38  * Author: Yury Umanets <umka@clusterfs.com>
39  */
40
41 #ifndef __LINUX_FID_H
42 #define __LINUX_FID_H
43
44 /** \defgroup fid fid
45  *
46  * @{
47  */
48
49 /*
50  * struct lu_fid
51  */
52 #include <libcfs/libcfs.h>
53 #include <lustre/lustre_idl.h>
54 #include <lustre_req_layout.h>
55 #include <lustre_mdt.h>
56
57
58 struct lu_site;
59 struct lu_context;
60
61 /* Whole sequences space range and zero range definitions */
62 extern const struct lu_seq_range LUSTRE_SEQ_SPACE_RANGE;
63 extern const struct lu_seq_range LUSTRE_SEQ_ZERO_RANGE;
64 extern const struct lu_fid LUSTRE_BFL_FID;
65 extern const struct lu_fid LU_OBF_FID;
66 extern const struct lu_fid LU_DOT_LUSTRE_FID;
67
68 enum {
69         /*
70          * This is how may FIDs may be allocated in one sequence(128k)
71          */
72         LUSTRE_SEQ_MAX_WIDTH = 0x0000000000020000ULL,
73
74         /*
75          * How many sequences to allocate to a client at once.
76          */
77         LUSTRE_SEQ_META_WIDTH = 0x0000000000000001ULL,
78
79          /*
80           * seq allocation pool size.
81           */
82         LUSTRE_SEQ_BATCH_WIDTH = LUSTRE_SEQ_META_WIDTH * 1000,
83
84         /*
85          * This is how many sequences may be in one super-sequence allocated to
86          * MDTs.
87          */
88         LUSTRE_SEQ_SUPER_WIDTH = ((1ULL << 30ULL) * LUSTRE_SEQ_META_WIDTH)
89 };
90
91 enum {
92         /** 2^6 FIDs for OI containers */
93         OSD_OI_FID_OID_BITS     = 6,
94         /** reserve enough FIDs in case we want more in the future */
95         OSD_OI_FID_OID_BITS_MAX = 10,
96 };
97
98 /** special OID for local objects */
99 enum local_oid {
100         /** \see fld_mod_init */
101         FLD_INDEX_OID           = 3UL,
102         /** \see fid_mod_init */
103         FID_SEQ_CTL_OID         = 4UL,
104         FID_SEQ_SRV_OID         = 5UL,
105         /** \see mdd_mod_init */
106         MDD_ROOT_INDEX_OID      = 6UL,
107         MDD_ORPHAN_OID          = 7UL,
108         MDD_LOV_OBJ_OID         = 8UL,
109         MDD_CAPA_KEYS_OID       = 9UL,
110         /** \see mdt_mod_init */
111         MDT_LAST_RECV_OID       = 11UL,
112         OSD_FS_ROOT_OID         = 13UL,
113         ACCT_USER_OID           = 15UL,
114         ACCT_GROUP_OID          = 16UL,
115         LFSCK_BOOKMARK_OID      = 17UL,
116         OTABLE_IT_OID           = 18UL,
117         OFD_LAST_RECV_OID       = 19UL,
118         OFD_GROUP0_LAST_OID     = 20UL,
119         OFD_GROUP4K_LAST_OID    = 20UL+4096,
120         OFD_LAST_GROUP_OID      = 4117UL,
121         LLOG_CATALOGS_OID       = 4118UL,
122         MGS_CONFIGS_OID         = 4119UL,
123         OFD_HEALTH_CHECK_OID    = 4120UL,
124 };
125
126 static inline void lu_local_obj_fid(struct lu_fid *fid, __u32 oid)
127 {
128         fid->f_seq = FID_SEQ_LOCAL_FILE;
129         fid->f_oid = oid;
130         fid->f_ver = 0;
131 }
132
133 static inline int fid_is_otable_it(const struct lu_fid *fid)
134 {
135         return unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
136                         fid_oid(fid) == OTABLE_IT_OID);
137 }
138
139 static inline int fid_is_acct(const struct lu_fid *fid)
140 {
141         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
142                (fid_oid(fid) == ACCT_USER_OID ||
143                 fid_oid(fid) == ACCT_GROUP_OID);
144 }
145
146 static inline int fid_is_quota(const struct lu_fid *fid)
147 {
148         return fid_seq(fid) == FID_SEQ_QUOTA ||
149                fid_seq(fid) == FID_SEQ_QUOTA_GLB;
150 }
151
152 enum lu_mgr_type {
153         LUSTRE_SEQ_SERVER,
154         LUSTRE_SEQ_CONTROLLER
155 };
156
157 enum lu_cli_type {
158         LUSTRE_SEQ_METADATA,
159         LUSTRE_SEQ_DATA
160 };
161
162 struct lu_server_seq;
163
164 /* Client sequence manager interface. */
165 struct lu_client_seq {
166         /* Sequence-controller export. */
167         struct obd_export      *lcs_exp;
168         cfs_mutex_t             lcs_mutex;
169
170         /*
171          * Range of allowed for allocation sequeces. When using lu_client_seq on
172          * clients, this contains meta-sequence range. And for servers this
173          * contains super-sequence range.
174          */
175         struct lu_seq_range         lcs_space;
176
177         /* Seq related proc */
178         cfs_proc_dir_entry_t   *lcs_proc_dir;
179
180         /* This holds last allocated fid in last obtained seq */
181         struct lu_fid           lcs_fid;
182
183         /* LUSTRE_SEQ_METADATA or LUSTRE_SEQ_DATA */
184         enum lu_cli_type        lcs_type;
185
186         /*
187          * Service uuid, passed from MDT + seq name to form unique seq name to
188          * use it with procfs.
189          */
190         char                    lcs_name[80];
191
192         /*
193          * Sequence width, that is how many objects may be allocated in one
194          * sequence. Default value for it is LUSTRE_SEQ_MAX_WIDTH.
195          */
196         __u64                   lcs_width;
197
198         /* Seq-server for direct talking */
199         struct lu_server_seq   *lcs_srv;
200
201         /* wait queue for fid allocation and update indicator */
202         cfs_waitq_t             lcs_waitq;
203         int                     lcs_update;
204 };
205
206 /* server sequence manager interface */
207 struct lu_server_seq {
208         /* Available sequences space */
209         struct lu_seq_range         lss_space;
210
211         /* keeps highwater in lsr_end for seq allocation algorithm */
212         struct lu_seq_range         lss_lowater_set;
213         struct lu_seq_range         lss_hiwater_set;
214
215         /*
216          * Device for server side seq manager needs (saving sequences to backing
217          * store).
218          */
219         struct dt_device       *lss_dev;
220
221         /* /seq file object device */
222         struct dt_object       *lss_obj;
223
224         /* Seq related proc */
225         cfs_proc_dir_entry_t   *lss_proc_dir;
226
227         /* LUSTRE_SEQ_SERVER or LUSTRE_SEQ_CONTROLLER */
228         enum lu_mgr_type       lss_type;
229
230         /* Client interafce to request controller */
231         struct lu_client_seq   *lss_cli;
232
233         /* Mutex for protecting allocation */
234         cfs_mutex_t             lss_mutex;
235
236         /*
237          * Service uuid, passed from MDT + seq name to form unique seq name to
238          * use it with procfs.
239          */
240         char                    lss_name[80];
241
242         /*
243          * Allocation chunks for super and meta sequences. Default values are
244          * LUSTRE_SEQ_SUPER_WIDTH and LUSTRE_SEQ_META_WIDTH.
245          */
246         __u64                   lss_width;
247
248         /*
249          * minimum lss_alloc_set size that should be allocated from
250          * lss_space
251          */
252         __u64                   lss_set_width;
253
254         /* sync is needed for update operation */
255         __u32                   lss_need_sync;
256         /**
257          * Pointer to site object, required to access site fld.
258          */
259         struct md_site         *lss_site;
260 };
261
262 int seq_query(struct com_thread_info *info);
263
264 /* Server methods */
265 int seq_server_init(struct lu_server_seq *seq,
266                     struct dt_device *dev,
267                     const char *prefix,
268                     enum lu_mgr_type type,
269                     struct md_site *ls,
270                     const struct lu_env *env);
271
272 void seq_server_fini(struct lu_server_seq *seq,
273                      const struct lu_env *env);
274
275 int seq_server_alloc_super(struct lu_server_seq *seq,
276                            struct lu_seq_range *out,
277                            const struct lu_env *env);
278
279 int seq_server_alloc_meta(struct lu_server_seq *seq,
280                           struct lu_seq_range *out,
281                           const struct lu_env *env);
282
283 int seq_server_set_cli(struct lu_server_seq *seq,
284                        struct lu_client_seq *cli,
285                        const struct lu_env *env);
286
287 /* Client methods */
288 int seq_client_init(struct lu_client_seq *seq,
289                     struct obd_export *exp,
290                     enum lu_cli_type type,
291                     const char *prefix,
292                     struct lu_server_seq *srv);
293
294 void seq_client_fini(struct lu_client_seq *seq);
295
296 void seq_client_flush(struct lu_client_seq *seq);
297
298 int seq_client_alloc_fid(const struct lu_env *env, struct lu_client_seq *seq,
299                          struct lu_fid *fid);
300 int seq_client_get_seq(const struct lu_env *env, struct lu_client_seq *seq,
301                        seqno_t *seqnr);
302
303 /* Fids common stuff */
304 int fid_is_local(const struct lu_env *env,
305                  struct lu_site *site, const struct lu_fid *fid);
306
307 /* fid locking */
308
309 struct ldlm_namespace;
310
311 /*
312  * Build (DLM) resource name from FID.
313  *
314  * NOTE: until Lustre 1.8.7/2.1.1 the fid_ver() was packed into name[2],
315  * but was moved into name[1] along with the OID to avoid consuming the
316  * renaming name[2,3] fields that need to be used for the quota identifier.
317  */
318 static inline struct ldlm_res_id *
319 fid_build_reg_res_name(const struct lu_fid *f,
320                        struct ldlm_res_id *name)
321 {
322         memset(name, 0, sizeof *name);
323         name->name[LUSTRE_RES_ID_SEQ_OFF] = fid_seq(f);
324         name->name[LUSTRE_RES_ID_VER_OID_OFF] = fid_ver_oid(f);
325         return name;
326 }
327
328 /*
329  * Return true if resource is for object identified by fid.
330  */
331 static inline int fid_res_name_eq(const struct lu_fid *f,
332                                   const struct ldlm_res_id *name)
333 {
334         return name->name[LUSTRE_RES_ID_SEQ_OFF] == fid_seq(f) &&
335                name->name[LUSTRE_RES_ID_VER_OID_OFF] == fid_ver_oid(f);
336 }
337
338
339 static inline struct ldlm_res_id *
340 fid_build_pdo_res_name(const struct lu_fid *f,
341                        unsigned int hash,
342                        struct ldlm_res_id *name)
343 {
344         fid_build_reg_res_name(f, name);
345         name->name[LUSTRE_RES_ID_HSH_OFF] = hash;
346         return name;
347 }
348
349
350 /**
351  * Flatten 128-bit FID values into a 64-bit value for use as an inode number.
352  * For non-IGIF FIDs this starts just over 2^32, and continues without
353  * conflict until 2^64, at which point we wrap the high 24 bits of the SEQ
354  * into the range where there may not be many OID values in use, to minimize
355  * the risk of conflict.
356  *
357  * Suppose LUSTRE_SEQ_MAX_WIDTH less than (1 << 24) which is currently true,
358  * the time between re-used inode numbers is very long - 2^40 SEQ numbers,
359  * or about 2^40 client mounts, if clients create less than 2^24 files/mount.
360  */
361 static inline __u64 fid_flatten(const struct lu_fid *fid)
362 {
363         __u64 ino;
364         __u64 seq;
365
366         if (fid_is_igif(fid)) {
367                 ino = lu_igif_ino(fid);
368                 RETURN(ino);
369         }
370
371         seq = fid_seq(fid);
372
373         ino = (seq << 24) + ((seq >> 24) & 0xffffff0000ULL) + fid_oid(fid);
374
375         RETURN(ino ? ino : fid_oid(fid));
376 }
377
378 static inline __u32 fid_hash(const struct lu_fid *f, int bits)
379 {
380         /* all objects with same id and different versions will belong to same
381          * collisions list. */
382         return cfs_hash_long(fid_flatten(f), bits);
383 }
384
385 /**
386  * map fid to 32 bit value for ino on 32bit systems. */
387 static inline __u32 fid_flatten32(const struct lu_fid *fid)
388 {
389         __u32 ino;
390         __u64 seq;
391
392         if (fid_is_igif(fid)) {
393                 ino = lu_igif_ino(fid);
394                 RETURN(ino);
395         }
396
397         seq = fid_seq(fid) - FID_SEQ_START;
398
399         /* Map the high bits of the OID into higher bits of the inode number so
400          * that inodes generated at about the same time have a reduced chance
401          * of collisions. This will give a period of 2^12 = 1024 unique clients
402          * (from SEQ) and up to min(LUSTRE_SEQ_MAX_WIDTH, 2^20) = 128k objects
403          * (from OID), or up to 128M inodes without collisions for new files. */
404         ino = ((seq & 0x000fffffULL) << 12) + ((seq >> 8) & 0xfffff000) +
405                (seq >> (64 - (40-8)) & 0xffffff00) +
406                (fid_oid(fid) & 0xff000fff) + ((fid_oid(fid) & 0x00fff000) << 8);
407
408         RETURN(ino ? ino : fid_oid(fid));
409 }
410
411 #define LUSTRE_SEQ_SRV_NAME "seq_srv"
412 #define LUSTRE_SEQ_CTL_NAME "seq_ctl"
413
414 /* Range common stuff */
415 static inline void range_cpu_to_le(struct lu_seq_range *dst, const struct lu_seq_range *src)
416 {
417         dst->lsr_start = cpu_to_le64(src->lsr_start);
418         dst->lsr_end = cpu_to_le64(src->lsr_end);
419         dst->lsr_index = cpu_to_le32(src->lsr_index);
420         dst->lsr_flags = cpu_to_le32(src->lsr_flags);
421 }
422
423 static inline void range_le_to_cpu(struct lu_seq_range *dst, const struct lu_seq_range *src)
424 {
425         dst->lsr_start = le64_to_cpu(src->lsr_start);
426         dst->lsr_end = le64_to_cpu(src->lsr_end);
427         dst->lsr_index = le32_to_cpu(src->lsr_index);
428         dst->lsr_flags = le32_to_cpu(src->lsr_flags);
429 }
430
431 static inline void range_cpu_to_be(struct lu_seq_range *dst, const struct lu_seq_range *src)
432 {
433         dst->lsr_start = cpu_to_be64(src->lsr_start);
434         dst->lsr_end = cpu_to_be64(src->lsr_end);
435         dst->lsr_index = cpu_to_be32(src->lsr_index);
436         dst->lsr_flags = cpu_to_be32(src->lsr_flags);
437 }
438
439 static inline void range_be_to_cpu(struct lu_seq_range *dst, const struct lu_seq_range *src)
440 {
441         dst->lsr_start = be64_to_cpu(src->lsr_start);
442         dst->lsr_end = be64_to_cpu(src->lsr_end);
443         dst->lsr_index = be32_to_cpu(src->lsr_index);
444         dst->lsr_flags = be32_to_cpu(src->lsr_flags);
445 }
446
447 /** @} fid */
448
449 #endif /* __LINUX_FID_H */