Whamcloud - gitweb
d683d0dc51650611a1aa9f5b652c280811f0b43b
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_internal.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) 2013, Trustees of Indiana University
24  *
25  * Copyright (c) 2013, 2014, Intel Corporation.
26  *
27  * Author: Joshua Walgenbach <jjw@iu.edu>
28  */
29
30 #ifndef _NODEMAP_INTERNAL_H
31 #define _NODEMAP_INTERNAL_H
32
33 #include <lustre_nodemap.h>
34 #include <interval_tree.h>
35
36 #define DEFAULT_NODEMAP "default"
37
38 /* Turn on proc debug interface to allow OSS and
39  * MDS nodes to configure nodemap independently of
40  * MGS (since the nodemap distribution is not written
41  * yet */
42 #define NODEMAP_PROC_DEBUG 1
43
44 /* Default nobody uid and gid values */
45
46 #define NODEMAP_NOBODY_UID 99
47 #define NODEMAP_NOBODY_GID 99
48
49 struct lprocfs_static_vars;
50
51 /* nodemap root proc directory under fs/lustre */
52 extern struct proc_dir_entry *proc_lustre_nodemap_root;
53 /* flag if nodemap is active */
54 extern bool nodemap_active;
55
56 extern struct mutex active_config_lock;
57 extern struct nodemap_config *active_config;
58
59 struct lu_nid_range {
60         /* unique id set by mgs */
61         unsigned int             rn_id;
62         /* lu_nodemap containing this range */
63         struct lu_nodemap       *rn_nodemap;
64         /* list for nodemap */
65         struct list_head         rn_list;
66         /* nid interval tree */
67         struct interval_node     rn_node;
68 };
69
70 struct lu_idmap {
71         /* uid/gid of client */
72         __u32           id_client;
73         /* uid/gid on filesystem */
74         __u32           id_fs;
75         /* tree mapping client ids to filesystem ids */
76         struct rb_node  id_client_to_fs;
77         /* tree mappung filesystem to client */
78         struct rb_node  id_fs_to_client;
79 };
80
81 struct nodemap_range_tree {
82         struct interval_node *nmrt_range_interval_root;
83         unsigned int nmrt_range_highest_id;
84 };
85
86 struct nodemap_config {
87         /* Highest numerical lu_nodemap.nm_id defined */
88         unsigned int nmc_nodemap_highest_id;
89
90         /* Simple flag to determine if nodemaps are active */
91         bool nmc_nodemap_is_active;
92
93         /* Pointer to default nodemap as it is needed more often */
94         struct lu_nodemap *nmc_default_nodemap;
95
96         /**
97          * Lock required to access the range tree.
98          */
99         struct rw_semaphore nmc_range_tree_lock;
100         struct nodemap_range_tree nmc_range_tree;
101
102         /**
103          * Hash keyed on nodemap name containing all
104          * nodemaps
105          */
106         struct cfs_hash *nmc_nodemap_hash;
107 };
108
109 /* first 4 bits of the nodemap_id is the index type */
110 struct nodemap_key {
111         __u32 nk_nodemap_id;
112         union {
113                 __u32 nk_range_id;
114                 __u32 nk_id_client;
115                 __u32 nk_unused;
116         };
117 };
118
119 enum nodemap_idx_type {
120         NODEMAP_EMPTY_IDX = 0,          /* index created with blank record */
121         NODEMAP_CLUSTER_IDX = 1,        /* a nodemap cluster of nodes */
122         NODEMAP_RANGE_IDX = 2,          /* nid range assigned to a nm cluster */
123         NODEMAP_UIDMAP_IDX = 3,         /* uid map assigned to a nm cluster */
124         NODEMAP_GIDMAP_IDX = 4,         /* gid map assigned to a nm cluster */
125         NODEMAP_GLOBAL_IDX = 15,        /* stores nodemap activation status */
126 };
127
128 #define NM_TYPE_MASK 0x0FFFFFFF
129 #define NM_TYPE_SHIFT 28
130
131 static inline enum nodemap_idx_type nm_idx_get_type(unsigned int id)
132 {
133         return id >> NM_TYPE_SHIFT;
134 }
135
136 static inline __u32 nm_idx_set_type(unsigned int id, enum nodemap_idx_type t)
137 {
138         return (id & NM_TYPE_MASK) | (t << NM_TYPE_SHIFT);
139 }
140
141 struct nodemap_config *nodemap_config_alloc(void);
142 void nodemap_config_dealloc(struct nodemap_config *config);
143 void nodemap_config_set_active(struct nodemap_config *config);
144 struct lu_nodemap *nodemap_create(const char *name,
145                                   struct nodemap_config *config,
146                                   bool is_default);
147 void nodemap_putref(struct lu_nodemap *nodemap);
148 struct lu_nodemap *nodemap_lookup(const char *name);
149
150 int nodemap_procfs_init(void);
151 void nodemap_procfs_exit(void);
152 int lprocfs_nodemap_register(struct lu_nodemap *nodemap,
153                              bool is_default_nodemap);
154 void lprocfs_nodemap_remove(struct nodemap_pde *nodemap_pde);
155 struct lu_nid_range *nodemap_range_find(lnet_nid_t start_nid,
156                                         lnet_nid_t end_nid);
157 struct lu_nid_range *range_create(struct nodemap_range_tree *nm_range_tree,
158                                   lnet_nid_t start_nid, lnet_nid_t end_nid,
159                                   struct lu_nodemap *nodemap,
160                                   unsigned int range_id);
161 void range_destroy(struct lu_nid_range *range);
162 int range_insert(struct nodemap_range_tree *nm_range_tree,
163                  struct lu_nid_range *data);
164 void range_delete(struct nodemap_range_tree *nm_range_tree,
165                   struct lu_nid_range *data);
166 struct lu_nid_range *range_search(struct nodemap_range_tree *nm_range_tree,
167                                   lnet_nid_t nid);
168 struct lu_nid_range *range_find(struct nodemap_range_tree *nm_range_tree,
169                                 lnet_nid_t start_nid, lnet_nid_t end_nid);
170 int range_parse_nidstring(char *range_string, lnet_nid_t *start_nid,
171                           lnet_nid_t *end_nid);
172 void range_init_tree(void);
173 struct lu_idmap *idmap_create(__u32 client_id, __u32 fs_id);
174 void idmap_insert(enum nodemap_id_type id_type, struct lu_idmap *idmap,
175                  struct lu_nodemap *nodemap);
176 void idmap_delete(enum nodemap_id_type id_type,  struct lu_idmap *idmap,
177                   struct lu_nodemap *nodemap);
178 void idmap_delete_tree(struct lu_nodemap *nodemap);
179 struct lu_idmap *idmap_search(struct lu_nodemap *nodemap,
180                               enum nodemap_tree_type,
181                               enum nodemap_id_type id_type,
182                               __u32 id);
183 int nm_member_add(struct lu_nodemap *nodemap, struct obd_export *exp);
184 void nm_member_del(struct lu_nodemap *nodemap, struct obd_export *exp);
185 void nm_member_delete_list(struct lu_nodemap *nodemap);
186 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid);
187 void nm_member_reclassify_nodemap(struct lu_nodemap *nodemap);
188 void nm_member_revoke_locks(struct lu_nodemap *nodemap);
189 void nm_member_revoke_all(void);
190
191 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
192                              enum nodemap_id_type id_type,
193                              const __u32 map[2]);
194 int nodemap_add_range_helper(struct nodemap_config *config,
195                              struct lu_nodemap *nodemap,
196                              const lnet_nid_t nid[2],
197                              unsigned int range_id);
198
199 struct rb_node *nm_rb_next_postorder(const struct rb_node *node);
200 struct rb_node *nm_rb_first_postorder(const struct rb_root *root);
201 void nodemap_getref(struct lu_nodemap *nodemap);
202 void nodemap_putref(struct lu_nodemap *nodemap);
203
204 #define nm_rbtree_postorder_for_each_entry_safe(pos, n,                 \
205                                                 root, field)            \
206         for (pos = nm_rb_first_postorder(root) ?                        \
207                 rb_entry(nm_rb_first_postorder(root), typeof(*pos),     \
208                 field) : NULL,                                          \
209                 n = (pos && nm_rb_next_postorder(&pos->field)) ?        \
210                 rb_entry(nm_rb_next_postorder(&pos->field),             \
211                 typeof(*pos), field) : NULL;                            \
212                 pos != NULL;                                            \
213                 pos = n,                                                \
214                 n = (pos && nm_rb_next_postorder(&pos->field)) ?        \
215                 rb_entry(nm_rb_next_postorder(&pos->field),             \
216                 typeof(*pos), field) : NULL)
217
218 int nodemap_idx_nodemap_add(const struct lu_nodemap *nodemap);
219 int nodemap_idx_nodemap_update(const struct lu_nodemap *nodemap);
220 int nodemap_idx_nodemap_del(const struct lu_nodemap *nodemap);
221 int nodemap_idx_idmap_add(const struct lu_nodemap *nodemap,
222                           enum nodemap_id_type id_type,
223                           const __u32 map[2]);
224 int nodemap_idx_idmap_del(const struct lu_nodemap *nodemap,
225                           enum nodemap_id_type id_type,
226                           const __u32 map[2]);
227 int nodemap_idx_range_add(const struct lu_nid_range *range,
228                           const lnet_nid_t nid[2]);
229 int nodemap_idx_range_del(const struct lu_nid_range *range);
230 int nodemap_idx_nodemap_activate(bool value);
231 #endif  /* _NODEMAP_INTERNAL_H */