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