Whamcloud - gitweb
10bbc49eac20ab2e4f4eea5fd8d34380a1375173
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
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) 2014, Intel Corporation.
26  *
27  * Author: Joshua Walgenbach <jjw@iu.edu>
28  */
29 #include <linux/module.h>
30 #include <linux/sort.h>
31 #include <lnet/nidstr.h>
32 #include <lustre_net.h>
33 #include <lustre_acl.h>
34 #include <lustre_eacl.h>
35 #include <obd_class.h>
36 #include "nodemap_internal.h"
37
38 #define HASH_NODEMAP_BKT_BITS 3
39 #define HASH_NODEMAP_CUR_BITS 3
40 #define HASH_NODEMAP_MAX_BITS 7
41
42 #define DEFAULT_NODEMAP "default"
43
44 /* nodemap proc root proc directory under fs/lustre */
45 struct proc_dir_entry *proc_lustre_nodemap_root;
46
47 /* Copy of config active flag to avoid locking in mapping functions */
48 bool nodemap_active;
49
50 /* Lock protecting the active config, useful primarily when proc and
51  * nodemap_hash might be replaced when loading a new config
52  * Any time the active config is referenced, the lock should be held.
53  */
54 DEFINE_MUTEX(active_config_lock);
55 struct nodemap_config *active_config;
56
57 /**
58  * Nodemap destructor
59  *
60  * \param       nodemap         nodemap to destroy
61  */
62 static void nodemap_destroy(struct lu_nodemap *nodemap)
63 {
64         ENTRY;
65
66         if (nodemap->nm_pde_data != NULL)
67                 lprocfs_nodemap_remove(nodemap->nm_pde_data);
68
69         mutex_lock(&active_config_lock);
70         down_read(&active_config->nmc_range_tree_lock);
71         nm_member_reclassify_nodemap(nodemap);
72         up_read(&active_config->nmc_range_tree_lock);
73         mutex_unlock(&active_config_lock);
74
75         if (!list_empty(&nodemap->nm_member_list))
76                 CWARN("nodemap_destroy failed to reclassify all members\n");
77
78         write_lock(&nodemap->nm_idmap_lock);
79         idmap_delete_tree(nodemap);
80         write_unlock(&nodemap->nm_idmap_lock);
81
82         nm_member_delete_list(nodemap);
83
84         OBD_FREE_PTR(nodemap);
85
86         EXIT;
87 }
88
89 /**
90  * Functions used for the cfs_hash
91  */
92 void nodemap_getref(struct lu_nodemap *nodemap)
93 {
94         atomic_inc(&nodemap->nm_refcount);
95         CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n",
96                nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount));
97 }
98
99 /**
100  * Destroy nodemap if last reference is put. Should be called outside
101  * active_config_lock
102  */
103 void nodemap_putref(struct lu_nodemap *nodemap)
104 {
105         if (!nodemap)
106                 return;
107
108         LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
109
110         CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n",
111                nodemap->nm_name, nodemap,
112                atomic_read(&nodemap->nm_refcount) - 1);
113
114         if (atomic_dec_and_test(&nodemap->nm_refcount))
115                 nodemap_destroy(nodemap);
116 }
117 EXPORT_SYMBOL(nodemap_putref);
118
119 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
120                             const void *key, unsigned mask)
121 {
122         return cfs_hash_djb2_hash(key, strlen(key), mask);
123 }
124
125 static void *nodemap_hs_key(struct hlist_node *hnode)
126 {
127         struct lu_nodemap *nodemap;
128
129         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
130
131         return nodemap->nm_name;
132 }
133
134 static int nodemap_hs_keycmp(const void *key,
135                              struct hlist_node *compared_hnode)
136 {
137         char *nodemap_name;
138
139         nodemap_name = nodemap_hs_key(compared_hnode);
140
141         return !strcmp(key, nodemap_name);
142 }
143
144 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
145 {
146         return hlist_entry(hnode, struct lu_nodemap, nm_hash);
147 }
148
149 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
150 {
151         struct lu_nodemap *nodemap;
152
153         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
154         nodemap_getref(nodemap);
155 }
156
157 static void nodemap_hs_put_locked(struct cfs_hash *hs,
158                                   struct hlist_node *hnode)
159 {
160         struct lu_nodemap *nodemap;
161
162         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
163         nodemap_putref(nodemap);
164 }
165
166 static struct cfs_hash_ops nodemap_hash_operations = {
167         .hs_hash        = nodemap_hashfn,
168         .hs_key         = nodemap_hs_key,
169         .hs_keycmp      = nodemap_hs_keycmp,
170         .hs_object      = nodemap_hs_hashobject,
171         .hs_get         = nodemap_hs_get,
172         .hs_put_locked  = nodemap_hs_put_locked,
173 };
174
175 /* end of cfs_hash functions */
176
177 /**
178  * Initialize nodemap_hash
179  *
180  * \retval      0               success
181  * \retval      -ENOMEM         cannot create hash
182  */
183 static int nodemap_init_hash(struct nodemap_config *nmc)
184 {
185         nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
186                                                 HASH_NODEMAP_CUR_BITS,
187                                                 HASH_NODEMAP_MAX_BITS,
188                                                 HASH_NODEMAP_BKT_BITS, 0,
189                                                 CFS_HASH_MIN_THETA,
190                                                 CFS_HASH_MAX_THETA,
191                                                 &nodemap_hash_operations,
192                                                 CFS_HASH_DEFAULT);
193
194         if (nmc->nmc_nodemap_hash == NULL) {
195                 CERROR("cannot create nodemap_hash table\n");
196                 return -ENOMEM;
197         }
198
199         return 0;
200 }
201
202 /**
203  * Check for valid nodemap name
204  *
205  * \param       name            nodemap name
206  * \retval      true            valid
207  * \retval      false           invalid
208  */
209 static bool nodemap_name_is_valid(const char *name)
210 {
211         if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
212             strlen(name) == 0)
213                 return false;
214
215         for (; *name != '\0'; name++) {
216                 if (!isalnum(*name) && *name != '_')
217                         return false;
218         }
219
220         return true;
221 }
222
223 /**
224  * Nodemap lookup
225  *
226  * Look nodemap up in the active_config nodemap hash. Caller should hold the
227  * active_config_lock.
228  *
229  * \param       name            name of nodemap
230  * \retval      nodemap         pointer set to found nodemap
231  * \retval      -EINVAL         name is not valid
232  * \retval      -ENOENT         nodemap not found
233  */
234 struct lu_nodemap *nodemap_lookup(const char *name)
235 {
236         struct lu_nodemap *nodemap = NULL;
237
238         if (!nodemap_name_is_valid(name))
239                 return ERR_PTR(-EINVAL);
240
241         nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
242         if (nodemap == NULL)
243                 return ERR_PTR(-ENOENT);
244
245         return nodemap;
246 }
247
248 /**
249  * Classify the nid into the proper nodemap. Caller must hold active config and
250  * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
251  *
252  * \param       nid                     nid to classify
253  * \retval      nodemap                 nodemap containing the nid
254  * \retval      default_nodemap         default nodemap
255  */
256 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
257 {
258         struct lu_nid_range     *range;
259         struct lu_nodemap       *nodemap;
260
261         range = range_search(&active_config->nmc_range_tree, nid);
262         if (range != NULL)
263                 nodemap = range->rn_nodemap;
264         else
265                 nodemap = active_config->nmc_default_nodemap;
266
267         nodemap_getref(nodemap);
268
269         return nodemap;
270 }
271
272 /**
273  * simple check for default nodemap
274  */
275 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
276 {
277         return nodemap->nm_id == 0;
278 }
279
280 /**
281  * parse a nodemap range string into two nids
282  *
283  * \param       range_str               string to parse
284  * \param       range[2]                array of two nids
285  * \reyval      0 on success
286  */
287 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
288 {
289         char    buf[LNET_NIDSTR_SIZE * 2 + 2];
290         char    *ptr = NULL;
291         char    *start_nidstr;
292         char    *end_nidstr;
293         int     rc = 0;
294
295         snprintf(buf, sizeof(buf), "%s", range_str);
296         ptr = buf;
297         start_nidstr = strsep(&ptr, ":");
298         end_nidstr = strsep(&ptr, ":");
299
300         if (start_nidstr == NULL || end_nidstr == NULL)
301                 GOTO(out, rc = -EINVAL);
302
303         range[0] = libcfs_str2nid(start_nidstr);
304         range[1] = libcfs_str2nid(end_nidstr);
305
306 out:
307         return rc;
308
309 }
310 EXPORT_SYMBOL(nodemap_parse_range);
311
312 /**
313  * parse a string containing an id map of form "client_id:filesystem_id"
314  * into an array of __u32 * for use in mapping functions
315  *
316  * \param       idmap_str               map string
317  * \param       idmap                   array[2] of __u32
318  *
319  * \retval      0 on success
320  * \retval      -EINVAL if idmap cannot be parsed
321  */
322 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
323 {
324         char                    *sep;
325         long unsigned int        idmap_buf;
326         int                      rc;
327
328         if (idmap_str == NULL)
329                 return -EINVAL;
330
331         sep = strchr(idmap_str, ':');
332         if (sep == NULL)
333                 return -EINVAL;
334         *sep = '\0';
335         sep++;
336
337         rc = kstrtoul(idmap_str, 10, &idmap_buf);
338         if (rc != 0)
339                 return -EINVAL;
340         idmap[0] = idmap_buf;
341
342         rc = kstrtoul(sep, 10, &idmap_buf);
343         if (rc != 0)
344                 return -EINVAL;
345         idmap[1] = idmap_buf;
346
347         return 0;
348 }
349 EXPORT_SYMBOL(nodemap_parse_idmap);
350
351 /**
352  * add a member to a nodemap
353  *
354  * \param       nid             nid to add to the members
355  * \param       exp             obd_export structure for the connection
356  *                              that is being added
357  * \retval      -EINVAL         export is NULL
358  * \retval      -EEXIST         export is already member of a nodemap
359  */
360 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
361 {
362         struct lu_nodemap *nodemap;
363         int rc;
364
365         mutex_lock(&active_config_lock);
366         down_read(&active_config->nmc_range_tree_lock);
367
368         nodemap = nodemap_classify_nid(nid);
369         rc = nm_member_add(nodemap, exp);
370
371         up_read(&active_config->nmc_range_tree_lock);
372         mutex_unlock(&active_config_lock);
373
374         nodemap_putref(nodemap);
375
376         return rc;
377 }
378 EXPORT_SYMBOL(nodemap_add_member);
379
380 /**
381  * delete a member from a nodemap
382  *
383  * \param       exp             export to remove from a nodemap
384  */
385 void nodemap_del_member(struct obd_export *exp)
386 {
387         struct lu_nodemap *nodemap;
388
389         ENTRY;
390
391         /* using ac lock to prevent nodemap reclassification while deleting */
392         mutex_lock(&active_config_lock);
393
394         /* use of ted_nodemap is protected by active_config_lock. we take an
395          * extra reference to make sure nodemap isn't destroyed under
396          * active_config_lock
397          */
398         nodemap = exp->exp_target_data.ted_nodemap;
399         if (nodemap == NULL)
400                 goto out;
401         else
402                 nodemap_getref(nodemap);
403
404         mutex_lock(&nodemap->nm_member_list_lock);
405         nm_member_del(nodemap, exp);
406         mutex_unlock(&nodemap->nm_member_list_lock);
407
408 out:
409         mutex_unlock(&active_config_lock);
410
411         if (nodemap)
412                 nodemap_putref(nodemap);
413
414         EXIT;
415 }
416 EXPORT_SYMBOL(nodemap_del_member);
417
418 /**
419  * add an idmap to the proper nodemap trees
420  *
421  * \param       name            name of nodemap
422  * \param       id_type         NODEMAP_UID or NODEMAP_GID
423  * \param       map             array[2] __u32 containing the map values
424  *                              map[0] is client id
425  *                              map[1] is the filesystem id
426  *
427  * \retval      0 on success
428  */
429 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
430                              enum nodemap_id_type id_type,
431                              const __u32 map[2])
432 {
433         struct lu_idmap         *idmap;
434         int                     rc = 0;
435
436         idmap = idmap_create(map[0], map[1]);
437         if (idmap == NULL)
438                 GOTO(out, rc = -ENOMEM);
439
440         write_lock(&nodemap->nm_idmap_lock);
441         idmap_insert(id_type, idmap, nodemap);
442         write_unlock(&nodemap->nm_idmap_lock);
443         nm_member_revoke_locks(nodemap);
444
445 out:
446         return rc;
447 }
448
449 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
450                       const __u32 map[2])
451 {
452         struct lu_nodemap       *nodemap = NULL;
453         int                      rc;
454
455         mutex_lock(&active_config_lock);
456         nodemap = nodemap_lookup(name);
457         if (IS_ERR(nodemap)) {
458                 mutex_unlock(&active_config_lock);
459                 GOTO(out, rc = PTR_ERR(nodemap));
460         }
461
462         if (is_default_nodemap(nodemap)) {
463                 rc = -EINVAL;
464         } else {
465                 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
466                 if (rc == 0)
467                         rc = nodemap_idx_idmap_add(nodemap, id_type, map);
468         }
469         mutex_unlock(&active_config_lock);
470         nodemap_putref(nodemap);
471
472 out:
473         return rc;
474 }
475 EXPORT_SYMBOL(nodemap_add_idmap);
476
477 /**
478  * delete idmap from proper nodemap tree
479  *
480  * \param       name            name of nodemap
481  * \param       id_type         NODEMAP_UID or NODEMAP_GID
482  * \param       map             array[2] __u32 containing the mapA values
483  *                              map[0] is client id
484  *                              map[1] is the filesystem id
485  *
486  * \retval      0 on success
487  */
488 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
489                       const __u32 map[2])
490 {
491         struct lu_nodemap       *nodemap = NULL;
492         struct lu_idmap         *idmap = NULL;
493         int                     rc = 0;
494
495         mutex_lock(&active_config_lock);
496         nodemap = nodemap_lookup(name);
497         if (IS_ERR(nodemap)) {
498                 mutex_unlock(&active_config_lock);
499                 GOTO(out, rc = PTR_ERR(nodemap));
500         }
501
502         if (is_default_nodemap(nodemap))
503                 GOTO(out_putref, rc = -EINVAL);
504
505         write_lock(&nodemap->nm_idmap_lock);
506         idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
507                              map[0]);
508         if (idmap == NULL) {
509                 rc = -EINVAL;
510         } else {
511                 idmap_delete(id_type, idmap, nodemap);
512                 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
513         }
514         write_unlock(&nodemap->nm_idmap_lock);
515
516 out_putref:
517         mutex_unlock(&active_config_lock);
518         if (rc == 0)
519                 nm_member_revoke_locks(nodemap);
520         nodemap_putref(nodemap);
521
522 out:
523         return rc;
524 }
525 EXPORT_SYMBOL(nodemap_del_idmap);
526
527 /**
528  * Get nodemap assigned to given export. Takes a reference on the nodemap.
529  *
530  * Note that this function may return either NULL, or an ERR_PTR()
531  * or a valid nodemap pointer.  All of the functions accessing the
532  * returned nodemap can check IS_ERR(nodemap) to see if an error is
533  * returned.  NULL is not considered an error, which is OK since this
534  * is a valid case if nodemap are not in use.  All nodemap handling
535  * functions must check for nodemap == NULL and do nothing, and the
536  * nodemap returned from this function should not be dereferenced.
537  *
538  * \param       export          export to get nodemap for
539  *
540  * \retval      pointer to nodemap on success
541  * \retval      NULL    nodemap subsystem disabled
542  * \retval      -EACCES export does not have nodemap assigned
543  */
544 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
545 {
546         struct lu_nodemap *nodemap;
547
548         ENTRY;
549
550         if (!nodemap_active)
551                 RETURN(NULL);
552
553         spin_lock(&exp->exp_target_data.ted_nodemap_lock);
554         nodemap = exp->exp_target_data.ted_nodemap;
555         if (nodemap)
556                 nodemap_getref(nodemap);
557         spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
558
559         if (!nodemap) {
560                 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
561                        exp->exp_obd->obd_name,
562                        obd_uuid2str(&exp->exp_client_uuid),
563                        obd_export_nid2str(exp));
564                 RETURN(ERR_PTR(-EACCES));
565         }
566
567         RETURN(nodemap);
568 }
569 EXPORT_SYMBOL(nodemap_get_from_exp);
570
571 /**
572  * mapping function for nodemap idmaps
573  *
574  * \param       nodemap         lu_nodemap structure defining nodemap
575  * \param       node_type       NODEMAP_UID or NODEMAP_GID
576  * \param       tree_type       NODEMAP_CLIENT_TO_FS or
577  *                              NODEMAP_FS_TO_CLIENT
578  * \param       id              id to map
579  *
580  * \retval      mapped id according to the rules below.
581  *
582  * if the nodemap_active is false, just return the passed id without mapping
583  *
584  * if the id to be looked up is 0, check that root access is allowed and if it
585  * is, return 0. Otherwise, return the squash uid or gid.
586  *
587  * if the nodemap is configured to trusted the ids from the client system, just
588  * return the passwd id without mapping.
589  *
590  * if by this point, we haven't returned and the nodemap in question is the
591  * default nodemap, return the squash uid or gid.
592  *
593  * after these checks, search the proper tree for the mapping, and if found
594  * return the mapped value, otherwise return the squash uid or gid.
595  */
596 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
597                      enum nodemap_id_type id_type,
598                      enum nodemap_tree_type tree_type, __u32 id)
599 {
600         struct lu_idmap         *idmap = NULL;
601         __u32                    found_id;
602
603         ENTRY;
604
605         if (!nodemap_active)
606                 goto out;
607
608         if (unlikely(nodemap == NULL))
609                 goto out;
610
611         if (id == 0) {
612                 if (nodemap->nmf_allow_root_access)
613                         goto out;
614                 else
615                         goto squash;
616         }
617
618         if (nodemap->nmf_trust_client_ids)
619                 goto out;
620
621         if (is_default_nodemap(nodemap))
622                 goto squash;
623
624         read_lock(&nodemap->nm_idmap_lock);
625         idmap = idmap_search(nodemap, tree_type, id_type, id);
626         if (idmap == NULL) {
627                 read_unlock(&nodemap->nm_idmap_lock);
628                 goto squash;
629         }
630
631         if (tree_type == NODEMAP_FS_TO_CLIENT)
632                 found_id = idmap->id_client;
633         else
634                 found_id = idmap->id_fs;
635         read_unlock(&nodemap->nm_idmap_lock);
636         RETURN(found_id);
637
638 squash:
639         if (id_type == NODEMAP_UID)
640                 RETURN(nodemap->nm_squash_uid);
641         else
642                 RETURN(nodemap->nm_squash_gid);
643 out:
644         RETURN(id);
645 }
646 EXPORT_SYMBOL(nodemap_map_id);
647
648 /**
649  * Map posix ACL entries according to the nodemap membership. Removes any
650  * squashed ACLs.
651  *
652  * \param       lu_nodemap      nodemap
653  * \param       buf             buffer containing xattr encoded ACLs
654  * \param       size            size of ACLs in bytes
655  * \param       tree_type       direction of mapping
656  * \retval      size            new size of ACLs in bytes
657  * \retval      -EINVAL         bad \a size param, see posix_acl_xattr_count()
658  */
659 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
660                         enum nodemap_tree_type tree_type)
661 {
662         posix_acl_xattr_header  *header = buf;
663         posix_acl_xattr_entry   *entry = &header->a_entries[0];
664         posix_acl_xattr_entry   *new_entry = entry;
665         posix_acl_xattr_entry   *end;
666         int                      count;
667
668         if (!nodemap_active)
669                 return size;
670
671         if (unlikely(nodemap == NULL))
672                 return size;
673
674         count = posix_acl_xattr_count(size);
675         if (count < 0)
676                 return -EINVAL;
677         if (count == 0)
678                 return 0;
679
680         for (end = entry + count; entry != end; entry++) {
681                 __u16 tag = le16_to_cpu(entry->e_tag);
682                 __u32 id = le32_to_cpu(entry->e_id);
683
684                 switch (tag) {
685                 case ACL_USER:
686                         id = nodemap_map_id(nodemap, NODEMAP_UID,
687                                             tree_type, id);
688                         if (id == nodemap->nm_squash_uid)
689                                 continue;
690                         entry->e_id = cpu_to_le32(id);
691                         break;
692                 case ACL_GROUP:
693                         id = nodemap_map_id(nodemap, NODEMAP_GID,
694                                             tree_type, id);
695                         if (id == nodemap->nm_squash_gid)
696                                 continue;
697                         entry->e_id = cpu_to_le32(id);
698                         break;
699                 }
700
701                 /* if we skip an ACL, copy the following ones over it */
702                 if (new_entry != entry)
703                         *new_entry = *entry;
704
705                 new_entry++;
706         }
707
708         return (void *)new_entry - (void *)header;
709 }
710 EXPORT_SYMBOL(nodemap_map_acl);
711
712 /*
713  * Add nid range to given nodemap
714  *
715  * \param       config          nodemap config to work on
716  * \param       nodemap         nodemap to add range to
717  * \param       nid             nid range to add
718  * \param       range_id        should be 0 unless loading from disk
719  * \retval      0               success
720  * \retval      -ENOMEM
721  *
722  */
723 int nodemap_add_range_helper(struct nodemap_config *config,
724                              struct lu_nodemap *nodemap,
725                              const lnet_nid_t nid[2],
726                              unsigned int range_id)
727 {
728         struct lu_nid_range     *range;
729         int rc;
730
731         down_write(&config->nmc_range_tree_lock);
732         range = range_create(&config->nmc_range_tree, nid[0], nid[1],
733                              nodemap, range_id);
734         if (range == NULL) {
735                 up_write(&config->nmc_range_tree_lock);
736                 GOTO(out, rc = -ENOMEM);
737         }
738
739         rc = range_insert(&config->nmc_range_tree, range);
740         if (rc != 0) {
741                 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
742                       nodemap->nm_name, rc);
743                 up_write(&config->nmc_range_tree_lock);
744                 list_del(&range->rn_list);
745                 range_destroy(range);
746                 GOTO(out, rc = -ENOMEM);
747         }
748
749         list_add(&range->rn_list, &nodemap->nm_ranges);
750         nm_member_reclassify_nodemap(config->nmc_default_nodemap);
751         up_write(&config->nmc_range_tree_lock);
752
753         /* if range_id is non-zero, we are loading from disk */
754         if (range_id == 0)
755                 rc = nodemap_idx_range_add(range, nid);
756
757         nm_member_revoke_locks(config->nmc_default_nodemap);
758         nm_member_revoke_locks(nodemap);
759
760 out:
761         return rc;
762 }
763 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
764 {
765         struct lu_nodemap       *nodemap = NULL;
766         int                      rc;
767
768         mutex_lock(&active_config_lock);
769         nodemap = nodemap_lookup(name);
770         if (IS_ERR(nodemap)) {
771                 mutex_unlock(&active_config_lock);
772                 GOTO(out, rc = PTR_ERR(nodemap));
773         }
774
775         if (is_default_nodemap(nodemap))
776                 rc = -EINVAL;
777         else
778                 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
779         mutex_unlock(&active_config_lock);
780         nodemap_putref(nodemap);
781 out:
782         return rc;
783 }
784 EXPORT_SYMBOL(nodemap_add_range);
785
786 /**
787  * delete a range
788  * \param       name            nodemap name
789  * \param       range_str       string containing range
790  * \retval      0 on success
791  *
792  * Delete range from global range tree, and remove it
793  * from the list in the associated nodemap.
794  */
795 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
796 {
797         struct lu_nodemap       *nodemap;
798         struct lu_nid_range     *range;
799         int                     rc = 0;
800
801         mutex_lock(&active_config_lock);
802         nodemap = nodemap_lookup(name);
803         if (IS_ERR(nodemap)) {
804                 mutex_unlock(&active_config_lock);
805                 GOTO(out, rc = PTR_ERR(nodemap));
806         }
807
808         if (is_default_nodemap(nodemap))
809                 GOTO(out_putref, rc = -EINVAL);
810
811         down_write(&active_config->nmc_range_tree_lock);
812         range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
813         if (range == NULL) {
814                 up_write(&active_config->nmc_range_tree_lock);
815                 GOTO(out_putref, rc = -EINVAL);
816         }
817         rc = nodemap_idx_range_del(range);
818         range_delete(&active_config->nmc_range_tree, range);
819         nm_member_reclassify_nodemap(nodemap);
820         up_write(&active_config->nmc_range_tree_lock);
821
822         nm_member_revoke_locks(active_config->nmc_default_nodemap);
823         nm_member_revoke_locks(nodemap);
824
825 out_putref:
826         mutex_unlock(&active_config_lock);
827         nodemap_putref(nodemap);
828 out:
829         return rc;
830 }
831 EXPORT_SYMBOL(nodemap_del_range);
832
833 /**
834  * set fileset on nodemap
835  * \param       name            nodemap to set fileset on
836  * \param       fileset         string containing fileset
837  * \retval      0 on success
838  *
839  * set a fileset on the named nodemap
840  */
841 static int nodemap_set_fileset_helper(struct nodemap_config *config,
842                                       struct lu_nodemap *nodemap,
843                                       const char *fileset)
844 {
845         int rc = 0;
846
847         /* we allow fileset = "" which means clear fileset info */
848         if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/'))
849                 rc = -EINVAL;
850         else if (strlcpy(nodemap->nm_fileset, fileset,
851                          sizeof(nodemap->nm_fileset)) >=
852                  sizeof(nodemap->nm_fileset))
853                 rc = -ENAMETOOLONG;
854
855         return rc;
856 }
857
858 int nodemap_set_fileset(const char *name, const char *fileset)
859 {
860         struct lu_nodemap       *nodemap = NULL;
861         int                      rc = 0;
862
863         mutex_lock(&active_config_lock);
864         nodemap = nodemap_lookup(name);
865         if (IS_ERR(nodemap)) {
866                 mutex_unlock(&active_config_lock);
867                 GOTO(out, rc = PTR_ERR(nodemap));
868         }
869
870         if (is_default_nodemap(nodemap))
871                 rc = -EINVAL;
872         else
873                 rc = nodemap_set_fileset_helper(active_config, nodemap,
874                                                 fileset);
875         mutex_unlock(&active_config_lock);
876
877         nodemap_putref(nodemap);
878 out:
879         return rc;
880 }
881 EXPORT_SYMBOL(nodemap_set_fileset);
882
883 /**
884  * get fileset defined on nodemap
885  * \param       nodemap         nodemap to get fileset from
886  * \retval      fileset name, or NULL if not defined or not activated
887  *
888  * get the fileset defined on the nodemap
889  */
890 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
891 {
892         if (!nodemap_active || is_default_nodemap(nodemap))
893                 return NULL;
894         else
895                 return (char *)nodemap->nm_fileset;
896 }
897 EXPORT_SYMBOL(nodemap_get_fileset);
898
899 /**
900  * Nodemap constructor
901  *
902  * Creates an lu_nodemap structure and assigns sane default
903  * member values. If this is the default nodemap, the defaults
904  * are the most restictive in xterms of mapping behavior. Otherwise
905  * the default flags should be inherited from the default nodemap.
906  * The adds nodemap to nodemap_hash.
907  *
908  * Requires that the caller take the active_config_lock
909  *
910  * \param       name            name of nodemap
911  * \param       is_default      true if default nodemap
912  * \retval      nodemap         success
913  * \retval      -EINVAL         invalid nodemap name
914  * \retval      -EEXIST         nodemap already exists
915  * \retval      -ENOMEM         cannot allocate memory for nodemap
916  */
917 struct lu_nodemap *nodemap_create(const char *name,
918                                   struct nodemap_config *config,
919                                   bool is_default)
920 {
921         struct lu_nodemap       *nodemap = NULL;
922         struct lu_nodemap       *default_nodemap;
923         struct cfs_hash         *hash = config->nmc_nodemap_hash;
924         int                      rc = 0;
925
926         default_nodemap = config->nmc_default_nodemap;
927
928         if (!nodemap_name_is_valid(name))
929                 GOTO(out, rc = -EINVAL);
930
931         if (hash == NULL) {
932                 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
933                 GOTO(out, rc = -EINVAL);
934         }
935
936         OBD_ALLOC_PTR(nodemap);
937         if (nodemap == NULL) {
938                 CERROR("cannot allocate memory (%zu bytes)"
939                        "for nodemap '%s'\n", sizeof(*nodemap),
940                        name);
941                 GOTO(out, rc = -ENOMEM);
942         }
943
944         /*
945          * take an extra reference to prevent nodemap from being destroyed
946          * while it's being created.
947          */
948         atomic_set(&nodemap->nm_refcount, 2);
949         snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
950         rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
951         if (rc != 0) {
952                 OBD_FREE_PTR(nodemap);
953                 GOTO(out, rc = -EEXIST);
954         }
955
956         INIT_LIST_HEAD(&nodemap->nm_ranges);
957         INIT_LIST_HEAD(&nodemap->nm_list);
958         INIT_LIST_HEAD(&nodemap->nm_member_list);
959
960         mutex_init(&nodemap->nm_member_list_lock);
961         rwlock_init(&nodemap->nm_idmap_lock);
962         nodemap->nm_fs_to_client_uidmap = RB_ROOT;
963         nodemap->nm_client_to_fs_uidmap = RB_ROOT;
964         nodemap->nm_fs_to_client_gidmap = RB_ROOT;
965         nodemap->nm_client_to_fs_gidmap = RB_ROOT;
966
967         if (is_default) {
968                 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
969                 config->nmc_default_nodemap = nodemap;
970         } else {
971                 config->nmc_nodemap_highest_id++;
972                 nodemap->nm_id = config->nmc_nodemap_highest_id;
973         }
974
975         if (is_default || default_nodemap == NULL) {
976                 nodemap->nmf_trust_client_ids = 0;
977                 nodemap->nmf_allow_root_access = 0;
978
979                 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
980                 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
981                 if (!is_default)
982                         CWARN("adding nodemap '%s' to config without"
983                               " default nodemap\n", nodemap->nm_name);
984         } else {
985                 nodemap->nmf_trust_client_ids =
986                                 default_nodemap->nmf_trust_client_ids;
987                 nodemap->nmf_allow_root_access =
988                                 default_nodemap->nmf_allow_root_access;
989
990                 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
991                 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
992                 nodemap->nm_fileset[0] = 0;
993         }
994
995         return nodemap;
996
997 out:
998         CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
999         return ERR_PTR(rc);
1000 }
1001
1002 /**
1003  * update flag to turn on or off nodemap functions
1004  * \param       name            nodemap name
1005  * \param       admin_string    string containing updated value
1006  * \retval      0 on success
1007  *
1008  * Update admin flag to turn on or off nodemap functions.
1009  */
1010 int nodemap_set_allow_root(const char *name, bool allow_root)
1011 {
1012         struct lu_nodemap       *nodemap = NULL;
1013         int                     rc = 0;
1014
1015         mutex_lock(&active_config_lock);
1016         nodemap = nodemap_lookup(name);
1017         mutex_unlock(&active_config_lock);
1018         if (IS_ERR(nodemap))
1019                 GOTO(out, rc = PTR_ERR(nodemap));
1020
1021         nodemap->nmf_allow_root_access = allow_root;
1022         rc = nodemap_idx_nodemap_update(nodemap);
1023
1024         nm_member_revoke_locks(nodemap);
1025         nodemap_putref(nodemap);
1026 out:
1027         return rc;
1028 }
1029 EXPORT_SYMBOL(nodemap_set_allow_root);
1030
1031 /**
1032  * updated trust_client_ids flag for nodemap
1033  *
1034  * \param       name            nodemap name
1035  * \param       trust_string    new value for trust flag
1036  * \retval      0 on success
1037  *
1038  * Update the trust_client_ids flag for a nodemap.
1039  */
1040 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1041 {
1042         struct lu_nodemap       *nodemap = NULL;
1043         int                     rc = 0;
1044
1045         mutex_lock(&active_config_lock);
1046         nodemap = nodemap_lookup(name);
1047         mutex_unlock(&active_config_lock);
1048         if (IS_ERR(nodemap))
1049                 GOTO(out, rc = PTR_ERR(nodemap));
1050
1051         nodemap->nmf_trust_client_ids = trust_client_ids;
1052         rc = nodemap_idx_nodemap_update(nodemap);
1053
1054         nm_member_revoke_locks(nodemap);
1055         nodemap_putref(nodemap);
1056 out:
1057         return rc;
1058 }
1059 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1060
1061 /**
1062  * update the squash_uid for a nodemap
1063  *
1064  * \param       name            nodemap name
1065  * \param       uid_string      string containing new squash_uid value
1066  * \retval      0 on success
1067  *
1068  * Update the squash_uid for a nodemap. The squash_uid is the uid
1069  * that the all client uids are mapped to if nodemap is active,
1070  * the trust_client_ids flag is not set, and the uid is not in
1071  * the idmap tree.
1072  */
1073 int nodemap_set_squash_uid(const char *name, uid_t uid)
1074 {
1075         struct lu_nodemap       *nodemap = NULL;
1076         int                     rc = 0;
1077
1078         mutex_lock(&active_config_lock);
1079         nodemap = nodemap_lookup(name);
1080         mutex_unlock(&active_config_lock);
1081         if (IS_ERR(nodemap))
1082                 GOTO(out, rc = PTR_ERR(nodemap));
1083
1084         nodemap->nm_squash_uid = uid;
1085         rc = nodemap_idx_nodemap_update(nodemap);
1086
1087         nm_member_revoke_locks(nodemap);
1088         nodemap_putref(nodemap);
1089 out:
1090         return rc;
1091 }
1092 EXPORT_SYMBOL(nodemap_set_squash_uid);
1093
1094 /**
1095  * Update the squash_gid for a nodemap.
1096  *
1097  * \param       name            nodemap name
1098  * \param       gid_string      string containing new squash_gid value
1099  * \retval      0 on success
1100  *
1101  * Update the squash_gid for a nodemap. The squash_uid is the gid
1102  * that the all client gids are mapped to if nodemap is active,
1103  * the trust_client_ids flag is not set, and the gid is not in
1104  * the idmap tree.
1105  */
1106 int nodemap_set_squash_gid(const char *name, gid_t gid)
1107 {
1108         struct lu_nodemap       *nodemap = NULL;
1109         int                     rc = 0;
1110
1111         mutex_lock(&active_config_lock);
1112         nodemap = nodemap_lookup(name);
1113         mutex_unlock(&active_config_lock);
1114         if (IS_ERR(nodemap))
1115                 GOTO(out, rc = PTR_ERR(nodemap));
1116
1117         nodemap->nm_squash_gid = gid;
1118         rc = nodemap_idx_nodemap_update(nodemap);
1119
1120         nm_member_revoke_locks(nodemap);
1121         nodemap_putref(nodemap);
1122 out:
1123         return rc;
1124 }
1125 EXPORT_SYMBOL(nodemap_set_squash_gid);
1126
1127 /**
1128  * Returns true if this nodemap has root user access. Always returns true if
1129  * nodemaps are not active.
1130  *
1131  * \param       nodemap         nodemap to check access for
1132  */
1133 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1134 {
1135         return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1136 }
1137 EXPORT_SYMBOL(nodemap_can_setquota);
1138
1139 /**
1140  * Add a nodemap
1141  *
1142  * \param       name            name of nodemap
1143  * \retval      0               success
1144  * \retval      -EINVAL         invalid nodemap name
1145  * \retval      -EEXIST         nodemap already exists
1146  * \retval      -ENOMEM         cannot allocate memory for nodemap
1147  */
1148 int nodemap_add(const char *nodemap_name)
1149 {
1150         struct lu_nodemap *nodemap;
1151         int rc;
1152
1153         mutex_lock(&active_config_lock);
1154         nodemap = nodemap_create(nodemap_name, active_config, 0);
1155         if (IS_ERR(nodemap)) {
1156                 mutex_unlock(&active_config_lock);
1157                 return PTR_ERR(nodemap);
1158         }
1159
1160         rc = nodemap_idx_nodemap_add(nodemap);
1161         if (rc == 0)
1162                 rc = lprocfs_nodemap_register(nodemap, 0);
1163
1164         mutex_unlock(&active_config_lock);
1165         nodemap_putref(nodemap);
1166
1167         return rc;
1168 }
1169 EXPORT_SYMBOL(nodemap_add);
1170
1171 /**
1172  * Delete a nodemap
1173  *
1174  * \param       name            name of nodemmap
1175  * \retval      0               success
1176  * \retval      -EINVAL         invalid input
1177  * \retval      -ENOENT         no existing nodemap
1178  */
1179 int nodemap_del(const char *nodemap_name)
1180 {
1181         struct lu_nodemap       *nodemap;
1182         struct lu_nid_range     *range;
1183         struct lu_nid_range     *range_temp;
1184         int                      rc = 0;
1185         int                      rc2 = 0;
1186
1187         if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1188                 RETURN(-EINVAL);
1189
1190         mutex_lock(&active_config_lock);
1191         nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1192                                    nodemap_name);
1193         if (nodemap == NULL) {
1194                 mutex_unlock(&active_config_lock);
1195                 GOTO(out, rc = -ENOENT);
1196         }
1197
1198         /* erase nodemap from active ranges to prevent client assignment */
1199         down_write(&active_config->nmc_range_tree_lock);
1200         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1201                                  rn_list) {
1202                 rc2 = nodemap_idx_range_del(range);
1203                 if (rc2 < 0)
1204                         rc = rc2;
1205
1206                 range_delete(&active_config->nmc_range_tree, range);
1207         }
1208         up_write(&active_config->nmc_range_tree_lock);
1209
1210         rc2 = nodemap_idx_nodemap_del(nodemap);
1211         if (rc2 < 0)
1212                 rc = rc2;
1213
1214         /*
1215          * remove procfs here in case nodemap_create called with same name
1216          * before nodemap_destroy is run.
1217          */
1218         lprocfs_nodemap_remove(nodemap->nm_pde_data);
1219         nodemap->nm_pde_data = NULL;
1220
1221         /* reclassify all member exports from nodemap, so they put their refs */
1222         down_read(&active_config->nmc_range_tree_lock);
1223         nm_member_reclassify_nodemap(nodemap);
1224         up_read(&active_config->nmc_range_tree_lock);
1225
1226         if (!list_empty(&nodemap->nm_member_list))
1227                 CWARN("nodemap_del failed to reclassify all members\n");
1228
1229         mutex_unlock(&active_config_lock);
1230
1231         nodemap_putref(nodemap);
1232
1233 out:
1234         return rc;
1235 }
1236 EXPORT_SYMBOL(nodemap_del);
1237
1238 /**
1239  * activate nodemap functions
1240  *
1241  * \param       value           1 for on, 0 for off
1242  */
1243 void nodemap_activate(const bool value)
1244 {
1245         mutex_lock(&active_config_lock);
1246         active_config->nmc_nodemap_is_active = value;
1247
1248         /* copy active value to global to avoid locking in map functions */
1249         nodemap_active = value;
1250         nodemap_idx_nodemap_activate(value);
1251         mutex_unlock(&active_config_lock);
1252         nm_member_revoke_all();
1253 }
1254 EXPORT_SYMBOL(nodemap_activate);
1255
1256 /**
1257  * Helper iterator to convert nodemap hash to list.
1258  *
1259  * \param       hs                      hash structure
1260  * \param       bd                      bucket descriptor
1261  * \param       hnode                   hash node
1262  * \param       nodemap_list_head       list head for list of nodemaps in hash
1263  */
1264 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1265                                    struct hlist_node *hnode,
1266                                    void *nodemap_list_head)
1267 {
1268         struct lu_nodemap *nodemap;
1269
1270         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1271         list_add(&nodemap->nm_list, nodemap_list_head);
1272
1273         cfs_hash_bd_del_locked(hs, bd, hnode);
1274
1275         return 0;
1276 }
1277
1278 struct nodemap_config *nodemap_config_alloc(void)
1279 {
1280         struct nodemap_config *config;
1281         int rc = 0;
1282
1283         OBD_ALLOC_PTR(config);
1284         if (config == NULL)
1285                 return ERR_PTR(-ENOMEM);
1286
1287         rc = nodemap_init_hash(config);
1288         if (rc != 0) {
1289                 OBD_FREE_PTR(config);
1290                 return ERR_PTR(rc);
1291         }
1292
1293         init_rwsem(&config->nmc_range_tree_lock);
1294
1295         return config;
1296 }
1297 EXPORT_SYMBOL(nodemap_config_alloc);
1298
1299 /**
1300  * Walk the nodemap_hash and remove all nodemaps.
1301  */
1302 void nodemap_config_dealloc(struct nodemap_config *config)
1303 {
1304         struct lu_nodemap       *nodemap = NULL;
1305         struct lu_nodemap       *nodemap_temp;
1306         struct lu_nid_range     *range;
1307         struct lu_nid_range     *range_temp;
1308         LIST_HEAD(nodemap_list_head);
1309
1310         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1311                                nodemap_cleanup_iter_cb, &nodemap_list_head);
1312         cfs_hash_putref(config->nmc_nodemap_hash);
1313
1314         /* Because nodemap_destroy might sleep, we can't destroy them
1315          * in cfs_hash_for_each, so we build a list there and destroy here
1316          */
1317         list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1318                                  nm_list) {
1319                 down_write(&config->nmc_range_tree_lock);
1320
1321                 /* move members to new config */
1322                 nm_member_reclassify_nodemap(nodemap);
1323                 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1324                                          rn_list)
1325                         range_delete(&config->nmc_range_tree, range);
1326                 up_write(&config->nmc_range_tree_lock);
1327
1328                 nodemap_putref(nodemap);
1329         }
1330         OBD_FREE_PTR(config);
1331 }
1332 EXPORT_SYMBOL(nodemap_config_dealloc);
1333
1334 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1335                            struct hlist_node *hnode,
1336                            void *nodemap_list_head)
1337 {
1338         struct lu_nodemap *nodemap;
1339
1340         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1341         list_add(&nodemap->nm_list, nodemap_list_head);
1342         return 0;
1343 }
1344
1345 void nodemap_config_set_active(struct nodemap_config *config)
1346 {
1347         struct nodemap_config   *old_config = active_config;
1348         struct lu_nodemap       *nodemap;
1349         struct lu_nodemap       *tmp;
1350         LIST_HEAD(nodemap_list_head);
1351
1352         ENTRY;
1353
1354         LASSERT(active_config != config);
1355
1356         mutex_lock(&active_config_lock);
1357
1358         /* move proc entries from already existing nms, create for new nms */
1359         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1360                                nm_hash_list_cb, &nodemap_list_head);
1361         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1362                 struct lu_nodemap *old_nm = NULL;
1363
1364                 if (active_config != NULL)
1365                         old_nm = cfs_hash_lookup(
1366                                         active_config->nmc_nodemap_hash,
1367                                         nodemap->nm_name);
1368                 if (old_nm != NULL) {
1369                         nodemap->nm_pde_data = old_nm->nm_pde_data;
1370                         old_nm->nm_pde_data = NULL;
1371                         nodemap_putref(old_nm);
1372                 } else {
1373                         bool is_def = (nodemap == config->nmc_default_nodemap);
1374
1375                         lprocfs_nodemap_register(nodemap, is_def);
1376                 }
1377         }
1378
1379         /* if new config is inactive, deactivate live config before switching */
1380         if (!config->nmc_nodemap_is_active)
1381                 nodemap_active = false;
1382         active_config = config;
1383         if (config->nmc_nodemap_is_active)
1384                 nodemap_active = true;
1385
1386         mutex_unlock(&active_config_lock);
1387
1388         if (old_config != NULL)
1389                 nodemap_config_dealloc(old_config);
1390
1391         nm_member_revoke_all();
1392
1393         EXIT;
1394 }
1395 EXPORT_SYMBOL(nodemap_config_set_active);
1396
1397 /**
1398  * Cleanup nodemap module on exit
1399  */
1400 void nodemap_mod_exit(void)
1401 {
1402         nodemap_config_dealloc(active_config);
1403         nodemap_procfs_exit();
1404 }
1405
1406 /**
1407  * Initialize the nodemap module
1408  */
1409 int nodemap_mod_init(void)
1410 {
1411         struct nodemap_config   *new_config;
1412         struct lu_nodemap       *nodemap;
1413         int                      rc = 0;
1414
1415         rc = nodemap_procfs_init();
1416         if (rc != 0)
1417                 return rc;
1418
1419         new_config = nodemap_config_alloc();
1420         if (IS_ERR(new_config)) {
1421                 nodemap_procfs_exit();
1422                 GOTO(out, rc = PTR_ERR(new_config));
1423         }
1424
1425         nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1426         if (IS_ERR(nodemap)) {
1427                 nodemap_config_dealloc(new_config);
1428                 nodemap_procfs_exit();
1429                 GOTO(out, rc = PTR_ERR(nodemap));
1430         }
1431
1432         nodemap_config_set_active(new_config);
1433         nodemap_putref(nodemap);
1434
1435 out:
1436         return rc;
1437 }
1438
1439 /**
1440  * Revoke locks for all nodemaps.
1441  */
1442 void nm_member_revoke_all(void)
1443 {
1444         struct lu_nodemap *nodemap;
1445         struct lu_nodemap *tmp;
1446         LIST_HEAD(nodemap_list_head);
1447
1448         mutex_lock(&active_config_lock);
1449         cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1450                                nm_hash_list_cb, &nodemap_list_head);
1451
1452         /* revoke_locks sleeps, so can't call in cfs hash cb */
1453         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1454                 nm_member_revoke_locks(nodemap);
1455         mutex_unlock(&active_config_lock);
1456 }
1457
1458 /**
1459  * Returns the nodemap classification for a given nid into an ioctl buffer.
1460  * Useful for testing the nodemap configuration to make sure it is working as
1461  * expected.
1462  *
1463  * \param       nid             nid to classify
1464  * \param[out]  name_buf        buffer to write the nodemap name to
1465  * \param       name_len        length of buffer
1466  */
1467 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1468 {
1469         struct lu_nodemap       *nodemap;
1470
1471         mutex_lock(&active_config_lock);
1472         down_read(&active_config->nmc_range_tree_lock);
1473         nodemap = nodemap_classify_nid(nid);
1474         up_read(&active_config->nmc_range_tree_lock);
1475         mutex_unlock(&active_config_lock);
1476
1477         strncpy(name_buf, nodemap->nm_name, name_len);
1478         if (name_len > 0)
1479                 name_buf[name_len - 1] = '\0';
1480
1481         nodemap_putref(nodemap);
1482 }
1483 EXPORT_SYMBOL(nodemap_test_nid);
1484
1485 /**
1486  * Returns the id mapping for a given nid/id pair. Useful for testing the
1487  * nodemap configuration to make sure it is working as expected.
1488  *
1489  * \param       nid             nid to classify
1490  * \param       idtype          uid or gid
1491  * \param       client_id       id to map to fs
1492  *
1493  * \retval      the mapped fs_id of the given client_id
1494  */
1495 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1496                       __u32 client_id)
1497 {
1498         struct lu_nodemap       *nodemap;
1499         __u32                    fs_id;
1500
1501         mutex_lock(&active_config_lock);
1502         down_read(&active_config->nmc_range_tree_lock);
1503         nodemap = nodemap_classify_nid(nid);
1504         up_read(&active_config->nmc_range_tree_lock);
1505         mutex_unlock(&active_config_lock);
1506
1507         fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1508                                client_id);
1509         nodemap_putref(nodemap);
1510
1511         return fs_id;
1512 }
1513 EXPORT_SYMBOL(nodemap_test_id);