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