Whamcloud - gitweb
1c48f89346b286adb35b469071de777ebaab46ce
[fs/lustre-release.git] / lustre / include / lustre_fid.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 /** special OID for local objects */
92 enum local_oid {
93         /** \see osd_oi_index_create */
94         OSD_OI_FID_16_OID       = 2UL,
95         /** \see fld_mod_init */
96         FLD_INDEX_OID           = 3UL,
97         /** \see fid_mod_init */
98         FID_SEQ_CTL_OID         = 4UL,
99         FID_SEQ_SRV_OID         = 5UL,
100         /** \see mdd_mod_init */
101         MDD_ROOT_INDEX_OID      = 6UL,
102         MDD_ORPHAN_OID          = 7UL,
103         MDD_LOV_OBJ_OID         = 8UL,
104         MDD_CAPA_KEYS_OID       = 9UL,
105         MDD_OBJECTS_OID         = 10UL,
106         /** \see mdt_mod_init */
107         MDT_LAST_RECV_OID       = 11UL,
108         /** \see osd_mod_init */
109         OSD_REM_OBJ_DIR_OID     = 12UL,
110 };
111
112 static inline void lu_local_obj_fid(struct lu_fid *fid, __u32 oid)
113 {
114         fid->f_seq = FID_SEQ_LOCAL_FILE;
115         fid->f_oid = oid;
116         fid->f_ver = 0;
117 }
118
119 enum lu_mgr_type {
120         LUSTRE_SEQ_SERVER,
121         LUSTRE_SEQ_CONTROLLER
122 };
123
124 enum lu_cli_type {
125         LUSTRE_SEQ_METADATA,
126         LUSTRE_SEQ_DATA
127 };
128
129 struct lu_server_seq;
130
131 /* Client sequence manager interface. */
132 struct lu_client_seq {
133         /* Sequence-controller export. */
134         struct obd_export      *lcs_exp;
135         cfs_semaphore_t         lcs_sem;
136
137         /*
138          * Range of allowed for allocation sequeces. When using lu_client_seq on
139          * clients, this contains meta-sequence range. And for servers this
140          * contains super-sequence range.
141          */
142         struct lu_seq_range         lcs_space;
143
144         /* Seq related proc */
145         cfs_proc_dir_entry_t   *lcs_proc_dir;
146
147         /* This holds last allocated fid in last obtained seq */
148         struct lu_fid           lcs_fid;
149
150         /* LUSTRE_SEQ_METADATA or LUSTRE_SEQ_DATA */
151         enum lu_cli_type        lcs_type;
152
153         /*
154          * Service uuid, passed from MDT + seq name to form unique seq name to
155          * use it with procfs.
156          */
157         char                    lcs_name[80];
158
159         /*
160          * Sequence width, that is how many objects may be allocated in one
161          * sequence. Default value for it is LUSTRE_SEQ_MAX_WIDTH.
162          */
163         __u64                   lcs_width;
164
165         /* Seq-server for direct talking */
166         struct lu_server_seq   *lcs_srv;
167 };
168
169 /* server sequence manager interface */
170 struct lu_server_seq {
171         /* Available sequences space */
172         struct lu_seq_range         lss_space;
173
174         /* keeps highwater in lsr_end for seq allocation algorithm */
175         struct lu_seq_range         lss_lowater_set;
176         struct lu_seq_range         lss_hiwater_set;
177
178         /*
179          * Device for server side seq manager needs (saving sequences to backing
180          * store).
181          */
182         struct dt_device       *lss_dev;
183
184         /* /seq file object device */
185         struct dt_object       *lss_obj;
186
187         /* Seq related proc */
188         cfs_proc_dir_entry_t   *lss_proc_dir;
189
190         /* LUSTRE_SEQ_SERVER or LUSTRE_SEQ_CONTROLLER */
191         enum lu_mgr_type       lss_type;
192
193         /* Client interafce to request controller */
194         struct lu_client_seq   *lss_cli;
195
196         /* Semaphore for protecting allocation */
197         cfs_semaphore_t         lss_sem;
198
199         /*
200          * Service uuid, passed from MDT + seq name to form unique seq name to
201          * use it with procfs.
202          */
203         char                    lss_name[80];
204
205         /*
206          * Allocation chunks for super and meta sequences. Default values are
207          * LUSTRE_SEQ_SUPER_WIDTH and LUSTRE_SEQ_META_WIDTH.
208          */
209         __u64                   lss_width;
210
211         /*
212          * minimum lss_alloc_set size that should be allocated from
213          * lss_space
214          */
215         __u64                   lss_set_width;
216
217         /* transaction no of seq update write operation */
218         __u64                   lss_set_transno;
219         /**
220          * Pointer to site object, required to access site fld.
221          */
222         struct md_site         *lss_site;
223 };
224
225 int seq_query(struct com_thread_info *info);
226
227 /* Server methods */
228 int seq_server_init(struct lu_server_seq *seq,
229                     struct dt_device *dev,
230                     const char *prefix,
231                     enum lu_mgr_type type,
232                     struct md_site *ls,
233                     const struct lu_env *env);
234
235 void seq_server_fini(struct lu_server_seq *seq,
236                      const struct lu_env *env);
237
238 int seq_server_alloc_super(struct lu_server_seq *seq,
239                            struct lu_seq_range *out,
240                            const struct lu_env *env);
241
242 int seq_server_alloc_meta(struct lu_server_seq *seq,
243                           struct lu_seq_range *out,
244                           const struct lu_env *env);
245
246 int seq_server_set_cli(struct lu_server_seq *seq,
247                        struct lu_client_seq *cli,
248                        const struct lu_env *env);
249
250 /* Client methods */
251 int seq_client_init(struct lu_client_seq *seq,
252                     struct obd_export *exp,
253                     enum lu_cli_type type,
254                     const char *prefix,
255                     struct lu_server_seq *srv);
256
257 void seq_client_fini(struct lu_client_seq *seq);
258
259 void seq_client_flush(struct lu_client_seq *seq);
260
261 int seq_client_alloc_fid(struct lu_client_seq *seq,
262                          struct lu_fid *fid);
263
264 /* Fids common stuff */
265 int fid_is_local(const struct lu_env *env,
266                  struct lu_site *site, const struct lu_fid *fid);
267
268 /* fid locking */
269
270 struct ldlm_namespace;
271
272 enum {
273         LUSTRE_RES_ID_SEQ_OFF = 0,
274         LUSTRE_RES_ID_OID_OFF = 1,
275         LUSTRE_RES_ID_VER_OFF = 2,
276         LUSTRE_RES_ID_HSH_OFF = 3
277 };
278
279 /*
280  * Build (DLM) resource name from fid.
281  */
282 static inline struct ldlm_res_id *
283 fid_build_reg_res_name(const struct lu_fid *f,
284                        struct ldlm_res_id *name)
285 {
286         memset(name, 0, sizeof *name);
287         name->name[LUSTRE_RES_ID_SEQ_OFF] = fid_seq(f);
288         name->name[LUSTRE_RES_ID_OID_OFF] = fid_oid(f);
289         name->name[LUSTRE_RES_ID_VER_OFF] = fid_ver(f);
290         return name;
291 }
292
293 /*
294  * Return true if resource is for object identified by fid.
295  */
296 static inline int fid_res_name_eq(const struct lu_fid *f,
297                                   const struct ldlm_res_id *name)
298 {
299         return
300                 name->name[LUSTRE_RES_ID_SEQ_OFF] == fid_seq(f) &&
301                 name->name[LUSTRE_RES_ID_OID_OFF] == fid_oid(f) &&
302                 name->name[LUSTRE_RES_ID_VER_OFF] == fid_ver(f);
303 }
304
305
306 static inline struct ldlm_res_id *
307 fid_build_pdo_res_name(const struct lu_fid *f,
308                        unsigned int hash,
309                        struct ldlm_res_id *name)
310 {
311         fid_build_reg_res_name(f, name);
312         name->name[LUSTRE_RES_ID_HSH_OFF] = hash;
313         return name;
314 }
315
316
317 /**
318  * Flatten 128-bit FID values into a 64-bit value for use as an inode number.
319  * For non-IGIF FIDs this starts just over 2^32, and continues without
320  * conflict until 2^64, at which point we wrap the high 24 bits of the SEQ
321  * into the range where there may not be many OID values in use, to minimize
322  * the risk of conflict.
323  *
324  * Suppose LUSTRE_SEQ_MAX_WIDTH less than (1 << 24) which is currently true,
325  * the time between re-used inode numbers is very long - 2^40 SEQ numbers,
326  * or about 2^40 client mounts, if clients create less than 2^24 files/mount.
327  */
328 static inline __u64 fid_flatten(const struct lu_fid *fid)
329 {
330         __u64 ino;
331         __u64 seq;
332
333         if (fid_is_igif(fid)) {
334                 ino = lu_igif_ino(fid);
335                 RETURN(ino);
336         }
337
338         seq = fid_seq(fid);
339
340         ino = (seq << 24) + ((seq >> 24) & 0xffffff0000ULL) + fid_oid(fid);
341
342         RETURN(ino ? ino : fid_oid(fid));
343 }
344
345 static inline __u32 fid_hash(const struct lu_fid *f, int bits)
346 {
347         /* all objects with same id and different versions will belong to same
348          * collisions list. */
349         return cfs_hash_long(fid_flatten(f), bits);
350 }
351
352 /**
353  * map fid to 32 bit value for ino on 32bit systems. */
354 static inline __u32 fid_flatten32(const struct lu_fid *fid)
355 {
356         __u32 ino;
357         __u64 seq;
358
359         if (fid_is_igif(fid)) {
360                 ino = lu_igif_ino(fid);
361                 RETURN(ino);
362         }
363
364         seq = fid_seq(fid) - FID_SEQ_START;
365
366         /*
367           map the high bits of the OID into higher bits of the inode number so that
368           inodes generated at about the same time have a reduced chance of collisions.
369           This will give a period of 1024 clients and 128 k = 128M inodes without collisions.
370         */
371
372         ino = ((seq & 0x000fffffULL) << 12) + ((seq >> 8) & 0xfffff000) +
373                (seq >> (64 - (40-8)) & 0xffffff00) +
374                (fid_oid(fid) & 0xff000fff) + ((fid_oid(fid) & 0x00fff000) << 16);
375
376         RETURN(ino ? ino : fid_oid(fid));
377 }
378
379 #define LUSTRE_SEQ_SRV_NAME "seq_srv"
380 #define LUSTRE_SEQ_CTL_NAME "seq_ctl"
381
382 /* Range common stuff */
383 static inline void range_cpu_to_le(struct lu_seq_range *dst, const struct lu_seq_range *src)
384 {
385         dst->lsr_start = cpu_to_le64(src->lsr_start);
386         dst->lsr_end = cpu_to_le64(src->lsr_end);
387         dst->lsr_index = cpu_to_le32(src->lsr_index);
388         dst->lsr_flags = cpu_to_le32(src->lsr_flags);
389 }
390
391 static inline void range_le_to_cpu(struct lu_seq_range *dst, const struct lu_seq_range *src)
392 {
393         dst->lsr_start = le64_to_cpu(src->lsr_start);
394         dst->lsr_end = le64_to_cpu(src->lsr_end);
395         dst->lsr_index = le32_to_cpu(src->lsr_index);
396         dst->lsr_flags = le32_to_cpu(src->lsr_flags);
397 }
398
399 static inline void range_cpu_to_be(struct lu_seq_range *dst, const struct lu_seq_range *src)
400 {
401         dst->lsr_start = cpu_to_be64(src->lsr_start);
402         dst->lsr_end = cpu_to_be64(src->lsr_end);
403         dst->lsr_index = cpu_to_be32(src->lsr_index);
404         dst->lsr_flags = cpu_to_be32(src->lsr_flags);
405 }
406
407 static inline void range_be_to_cpu(struct lu_seq_range *dst, const struct lu_seq_range *src)
408 {
409         dst->lsr_start = be64_to_cpu(src->lsr_start);
410         dst->lsr_end = be64_to_cpu(src->lsr_end);
411         dst->lsr_index = be32_to_cpu(src->lsr_index);
412         dst->lsr_flags = be32_to_cpu(src->lsr_flags);
413 }
414
415 /** @} fid */
416
417 #endif /* __LINUX_FID_H */