Whamcloud - gitweb
LU-9929 nodemap: add default ACL unmapping handling
[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 <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         write_lock(&nodemap->nm_idmap_lock);
75         idmap_delete_tree(nodemap);
76         write_unlock(&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       name            name of nodemap
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  */
459 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
460                              enum nodemap_id_type id_type,
461                              const __u32 map[2])
462 {
463         struct lu_idmap         *idmap;
464         int                     rc = 0;
465
466         idmap = idmap_create(map[0], map[1]);
467         if (idmap == NULL)
468                 GOTO(out, rc = -ENOMEM);
469
470         write_lock(&nodemap->nm_idmap_lock);
471         idmap_insert(id_type, idmap, nodemap);
472         write_unlock(&nodemap->nm_idmap_lock);
473         nm_member_revoke_locks(nodemap);
474
475 out:
476         return rc;
477 }
478
479 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
480                       const __u32 map[2])
481 {
482         struct lu_nodemap       *nodemap = NULL;
483         int                      rc;
484
485         mutex_lock(&active_config_lock);
486         nodemap = nodemap_lookup(name);
487         if (IS_ERR(nodemap)) {
488                 mutex_unlock(&active_config_lock);
489                 GOTO(out, rc = PTR_ERR(nodemap));
490         }
491
492         if (is_default_nodemap(nodemap)) {
493                 rc = -EINVAL;
494         } else {
495                 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
496                 if (rc == 0)
497                         rc = nodemap_idx_idmap_add(nodemap, id_type, map);
498         }
499         mutex_unlock(&active_config_lock);
500         nodemap_putref(nodemap);
501
502 out:
503         return rc;
504 }
505 EXPORT_SYMBOL(nodemap_add_idmap);
506
507 /**
508  * delete idmap from proper nodemap tree
509  *
510  * \param       name            name of nodemap
511  * \param       id_type         NODEMAP_UID or NODEMAP_GID
512  * \param       map             array[2] __u32 containing the mapA values
513  *                              map[0] is client id
514  *                              map[1] is the filesystem id
515  *
516  * \retval      0 on success
517  */
518 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
519                       const __u32 map[2])
520 {
521         struct lu_nodemap       *nodemap = NULL;
522         struct lu_idmap         *idmap = NULL;
523         int                     rc = 0;
524
525         mutex_lock(&active_config_lock);
526         nodemap = nodemap_lookup(name);
527         if (IS_ERR(nodemap)) {
528                 mutex_unlock(&active_config_lock);
529                 GOTO(out, rc = PTR_ERR(nodemap));
530         }
531
532         if (is_default_nodemap(nodemap))
533                 GOTO(out_putref, rc = -EINVAL);
534
535         write_lock(&nodemap->nm_idmap_lock);
536         idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
537                              map[0]);
538         if (idmap == NULL) {
539                 rc = -EINVAL;
540         } else {
541                 idmap_delete(id_type, idmap, nodemap);
542                 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
543         }
544         write_unlock(&nodemap->nm_idmap_lock);
545
546 out_putref:
547         mutex_unlock(&active_config_lock);
548         if (rc == 0)
549                 nm_member_revoke_locks(nodemap);
550         nodemap_putref(nodemap);
551
552 out:
553         return rc;
554 }
555 EXPORT_SYMBOL(nodemap_del_idmap);
556
557 /**
558  * Get nodemap assigned to given export. Takes a reference on the nodemap.
559  *
560  * Note that this function may return either NULL, or an ERR_PTR()
561  * or a valid nodemap pointer.  All of the functions accessing the
562  * returned nodemap can check IS_ERR(nodemap) to see if an error is
563  * returned.  NULL is not considered an error, which is OK since this
564  * is a valid case if nodemap are not in use.  All nodemap handling
565  * functions must check for nodemap == NULL and do nothing, and the
566  * nodemap returned from this function should not be dereferenced.
567  *
568  * \param       export          export to get nodemap for
569  *
570  * \retval      pointer to nodemap on success
571  * \retval      NULL    nodemap subsystem disabled
572  * \retval      -EACCES export does not have nodemap assigned
573  */
574 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
575 {
576         struct lu_nodemap *nodemap;
577
578         ENTRY;
579
580         if (!nodemap_active)
581                 RETURN(NULL);
582
583         spin_lock(&exp->exp_target_data.ted_nodemap_lock);
584         nodemap = exp->exp_target_data.ted_nodemap;
585         if (nodemap)
586                 nodemap_getref(nodemap);
587         spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
588
589         if (!nodemap) {
590                 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
591                        exp->exp_obd->obd_name,
592                        obd_uuid2str(&exp->exp_client_uuid),
593                        obd_export_nid2str(exp));
594                 RETURN(ERR_PTR(-EACCES));
595         }
596
597         RETURN(nodemap);
598 }
599 EXPORT_SYMBOL(nodemap_get_from_exp);
600
601 /**
602  * mapping function for nodemap idmaps
603  *
604  * \param       nodemap         lu_nodemap structure defining nodemap
605  * \param       node_type       NODEMAP_UID or NODEMAP_GID
606  * \param       tree_type       NODEMAP_CLIENT_TO_FS or
607  *                              NODEMAP_FS_TO_CLIENT
608  * \param       id              id to map
609  *
610  * \retval      mapped id according to the rules below.
611  *
612  * if the nodemap_active is false, just return the passed id without mapping
613  *
614  * if the id to be looked up is 0, check that root access is allowed and if it
615  * is, return 0. Otherwise, return the squash uid or gid.
616  *
617  * if the nodemap is configured to trusted the ids from the client system, just
618  * return the passwd id without mapping.
619  *
620  * if by this point, we haven't returned and the nodemap in question is the
621  * default nodemap, return the squash uid or gid.
622  *
623  * after these checks, search the proper tree for the mapping, and if found
624  * return the mapped value, otherwise return the squash uid or gid.
625  */
626 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
627                      enum nodemap_id_type id_type,
628                      enum nodemap_tree_type tree_type, __u32 id)
629 {
630         struct lu_idmap         *idmap = NULL;
631         __u32                    found_id;
632
633         ENTRY;
634
635         if (!nodemap_active)
636                 goto out;
637
638         if (unlikely(nodemap == NULL))
639                 goto out;
640
641         if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID)
642                 goto out;
643
644         if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID)
645                 goto out;
646
647         if (id == 0) {
648                 if (nodemap->nmf_allow_root_access)
649                         goto out;
650                 else
651                         goto squash;
652         }
653
654         if (nodemap->nmf_trust_client_ids)
655                 goto out;
656
657         if (is_default_nodemap(nodemap))
658                 goto squash;
659
660         read_lock(&nodemap->nm_idmap_lock);
661         idmap = idmap_search(nodemap, tree_type, id_type, id);
662         if (idmap == NULL) {
663                 read_unlock(&nodemap->nm_idmap_lock);
664                 goto squash;
665         }
666
667         if (tree_type == NODEMAP_FS_TO_CLIENT)
668                 found_id = idmap->id_client;
669         else
670                 found_id = idmap->id_fs;
671         read_unlock(&nodemap->nm_idmap_lock);
672         RETURN(found_id);
673
674 squash:
675         if (id_type == NODEMAP_UID)
676                 RETURN(nodemap->nm_squash_uid);
677         else
678                 RETURN(nodemap->nm_squash_gid);
679 out:
680         RETURN(id);
681 }
682 EXPORT_SYMBOL(nodemap_map_id);
683
684 /**
685  * Map posix ACL entries according to the nodemap membership. Removes any
686  * squashed ACLs.
687  *
688  * \param       lu_nodemap      nodemap
689  * \param       buf             buffer containing xattr encoded ACLs
690  * \param       size            size of ACLs in bytes
691  * \param       tree_type       direction of mapping
692  * \retval      size            new size of ACLs in bytes
693  * \retval      -EINVAL         bad \a size param, see posix_acl_xattr_count()
694  */
695 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
696                         enum nodemap_tree_type tree_type)
697 {
698         posix_acl_xattr_header  *header = buf;
699         posix_acl_xattr_entry   *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
700         posix_acl_xattr_entry   *new_entry = entry;
701         posix_acl_xattr_entry   *end;
702         int                      count;
703
704         ENTRY;
705
706         if (!nodemap_active)
707                 RETURN(size);
708
709         if (unlikely(nodemap == NULL))
710                 RETURN(size);
711
712         count = posix_acl_xattr_count(size);
713         if (count < 0)
714                 RETURN(-EINVAL);
715         if (count == 0)
716                 RETURN(0);
717
718         for (end = entry + count; entry != end; entry++) {
719                 __u16 tag = le16_to_cpu(entry->e_tag);
720                 __u32 id = le32_to_cpu(entry->e_id);
721
722                 switch (tag) {
723                 case ACL_USER:
724                         id = nodemap_map_id(nodemap, NODEMAP_UID,
725                                             tree_type, id);
726                         if (id == nodemap->nm_squash_uid)
727                                 continue;
728                         entry->e_id = cpu_to_le32(id);
729                         break;
730                 case ACL_GROUP:
731                         id = nodemap_map_id(nodemap, NODEMAP_GID,
732                                             tree_type, id);
733                         if (id == nodemap->nm_squash_gid)
734                                 continue;
735                         entry->e_id = cpu_to_le32(id);
736                         break;
737                 }
738
739                 /* if we skip an ACL, copy the following ones over it */
740                 if (new_entry != entry)
741                         *new_entry = *entry;
742
743                 new_entry++;
744         }
745
746         RETURN((void *)new_entry - (void *)header);
747 }
748 EXPORT_SYMBOL(nodemap_map_acl);
749
750 /*
751  * Add nid range to given nodemap
752  *
753  * \param       config          nodemap config to work on
754  * \param       nodemap         nodemap to add range to
755  * \param       nid             nid range to add
756  * \param       range_id        should be 0 unless loading from disk
757  * \retval      0               success
758  * \retval      -ENOMEM
759  *
760  */
761 int nodemap_add_range_helper(struct nodemap_config *config,
762                              struct lu_nodemap *nodemap,
763                              const lnet_nid_t nid[2],
764                              unsigned int range_id)
765 {
766         struct lu_nid_range     *range;
767         int rc;
768
769         down_write(&config->nmc_range_tree_lock);
770         range = range_create(&config->nmc_range_tree, nid[0], nid[1],
771                              nodemap, range_id);
772         if (range == NULL) {
773                 up_write(&config->nmc_range_tree_lock);
774                 GOTO(out, rc = -ENOMEM);
775         }
776
777         rc = range_insert(&config->nmc_range_tree, range);
778         if (rc != 0) {
779                 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
780                       nodemap->nm_name, rc);
781                 up_write(&config->nmc_range_tree_lock);
782                 list_del(&range->rn_list);
783                 range_destroy(range);
784                 GOTO(out, rc = -ENOMEM);
785         }
786
787         list_add(&range->rn_list, &nodemap->nm_ranges);
788
789         /* nodemaps have no members if they aren't on the active config */
790         if (config == active_config)
791                 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
792
793         up_write(&config->nmc_range_tree_lock);
794
795         /* if range_id is non-zero, we are loading from disk */
796         if (range_id == 0)
797                 rc = nodemap_idx_range_add(range, nid);
798
799         if (config == active_config) {
800                 nm_member_revoke_locks(config->nmc_default_nodemap);
801                 nm_member_revoke_locks(nodemap);
802         }
803
804 out:
805         return rc;
806 }
807 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
808 {
809         struct lu_nodemap       *nodemap = NULL;
810         int                      rc;
811
812         mutex_lock(&active_config_lock);
813         nodemap = nodemap_lookup(name);
814         if (IS_ERR(nodemap)) {
815                 mutex_unlock(&active_config_lock);
816                 GOTO(out, rc = PTR_ERR(nodemap));
817         }
818
819         if (is_default_nodemap(nodemap))
820                 rc = -EINVAL;
821         else
822                 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
823         mutex_unlock(&active_config_lock);
824         nodemap_putref(nodemap);
825 out:
826         return rc;
827 }
828 EXPORT_SYMBOL(nodemap_add_range);
829
830 /**
831  * delete a range
832  * \param       name            nodemap name
833  * \param       nid             nid range
834  * \retval      0 on success
835  *
836  * Delete range from global range tree, and remove it
837  * from the list in the associated nodemap.
838  */
839 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
840 {
841         struct lu_nodemap       *nodemap;
842         struct lu_nid_range     *range;
843         int                     rc = 0;
844
845         mutex_lock(&active_config_lock);
846         nodemap = nodemap_lookup(name);
847         if (IS_ERR(nodemap)) {
848                 mutex_unlock(&active_config_lock);
849                 GOTO(out, rc = PTR_ERR(nodemap));
850         }
851
852         if (is_default_nodemap(nodemap))
853                 GOTO(out_putref, rc = -EINVAL);
854
855         down_write(&active_config->nmc_range_tree_lock);
856         range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
857         if (range == NULL) {
858                 up_write(&active_config->nmc_range_tree_lock);
859                 GOTO(out_putref, rc = -EINVAL);
860         }
861         if (range->rn_nodemap != nodemap) {
862                 up_write(&active_config->nmc_range_tree_lock);
863                 GOTO(out_putref, rc = -EINVAL);
864         }
865         rc = nodemap_idx_range_del(range);
866         range_delete(&active_config->nmc_range_tree, range);
867         nm_member_reclassify_nodemap(nodemap);
868         up_write(&active_config->nmc_range_tree_lock);
869
870         nm_member_revoke_locks(active_config->nmc_default_nodemap);
871         nm_member_revoke_locks(nodemap);
872
873 out_putref:
874         mutex_unlock(&active_config_lock);
875         nodemap_putref(nodemap);
876 out:
877         return rc;
878 }
879 EXPORT_SYMBOL(nodemap_del_range);
880
881 /**
882  * set fileset on nodemap
883  * \param       name            nodemap to set fileset on
884  * \param       fileset         string containing fileset
885  * \retval      0 on success
886  *
887  * set a fileset on the named nodemap
888  */
889 static int nodemap_set_fileset_helper(struct nodemap_config *config,
890                                       struct lu_nodemap *nodemap,
891                                       const char *fileset)
892 {
893         int rc = 0;
894
895         /* we allow fileset = "" which means clear fileset info */
896         if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/'))
897                 rc = -EINVAL;
898         else if (strlcpy(nodemap->nm_fileset, fileset,
899                          sizeof(nodemap->nm_fileset)) >=
900                  sizeof(nodemap->nm_fileset))
901                 rc = -ENAMETOOLONG;
902
903         return rc;
904 }
905
906 int nodemap_set_fileset(const char *name, const char *fileset)
907 {
908         struct lu_nodemap       *nodemap = NULL;
909         int                      rc = 0;
910
911         mutex_lock(&active_config_lock);
912         nodemap = nodemap_lookup(name);
913         if (IS_ERR(nodemap)) {
914                 mutex_unlock(&active_config_lock);
915                 GOTO(out, rc = PTR_ERR(nodemap));
916         }
917
918         if (is_default_nodemap(nodemap))
919                 rc = -EINVAL;
920         else
921                 rc = nodemap_set_fileset_helper(active_config, nodemap,
922                                                 fileset);
923         mutex_unlock(&active_config_lock);
924
925         nodemap_putref(nodemap);
926 out:
927         return rc;
928 }
929 EXPORT_SYMBOL(nodemap_set_fileset);
930
931 /**
932  * get fileset defined on nodemap
933  * \param       nodemap         nodemap to get fileset from
934  * \retval      fileset name, or NULL if not defined or not activated
935  *
936  * get the fileset defined on the nodemap
937  */
938 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
939 {
940         if (!nodemap_active || is_default_nodemap(nodemap))
941                 return NULL;
942         else
943                 return (char *)nodemap->nm_fileset;
944 }
945 EXPORT_SYMBOL(nodemap_get_fileset);
946
947 /**
948  * Nodemap constructor
949  *
950  * Creates an lu_nodemap structure and assigns sane default
951  * member values. If this is the default nodemap, the defaults
952  * are the most restictive in xterms of mapping behavior. Otherwise
953  * the default flags should be inherited from the default nodemap.
954  * The adds nodemap to nodemap_hash.
955  *
956  * Requires that the caller take the active_config_lock
957  *
958  * \param       name            name of nodemap
959  * \param       is_default      true if default nodemap
960  * \retval      nodemap         success
961  * \retval      -EINVAL         invalid nodemap name
962  * \retval      -EEXIST         nodemap already exists
963  * \retval      -ENOMEM         cannot allocate memory for nodemap
964  */
965 struct lu_nodemap *nodemap_create(const char *name,
966                                   struct nodemap_config *config,
967                                   bool is_default)
968 {
969         struct lu_nodemap       *nodemap = NULL;
970         struct lu_nodemap       *default_nodemap;
971         struct cfs_hash         *hash = config->nmc_nodemap_hash;
972         int                      rc = 0;
973
974         default_nodemap = config->nmc_default_nodemap;
975
976         if (!nodemap_name_is_valid(name))
977                 GOTO(out, rc = -EINVAL);
978
979         if (hash == NULL) {
980                 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
981                 GOTO(out, rc = -EINVAL);
982         }
983
984         OBD_ALLOC_PTR(nodemap);
985         if (nodemap == NULL) {
986                 CERROR("cannot allocate memory (%zu bytes)"
987                        "for nodemap '%s'\n", sizeof(*nodemap),
988                        name);
989                 GOTO(out, rc = -ENOMEM);
990         }
991
992         /*
993          * take an extra reference to prevent nodemap from being destroyed
994          * while it's being created.
995          */
996         atomic_set(&nodemap->nm_refcount, 2);
997         snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
998         rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
999         if (rc != 0) {
1000                 OBD_FREE_PTR(nodemap);
1001                 GOTO(out, rc = -EEXIST);
1002         }
1003
1004         INIT_LIST_HEAD(&nodemap->nm_ranges);
1005         INIT_LIST_HEAD(&nodemap->nm_list);
1006         INIT_LIST_HEAD(&nodemap->nm_member_list);
1007
1008         mutex_init(&nodemap->nm_member_list_lock);
1009         rwlock_init(&nodemap->nm_idmap_lock);
1010         nodemap->nm_fs_to_client_uidmap = RB_ROOT;
1011         nodemap->nm_client_to_fs_uidmap = RB_ROOT;
1012         nodemap->nm_fs_to_client_gidmap = RB_ROOT;
1013         nodemap->nm_client_to_fs_gidmap = RB_ROOT;
1014
1015         if (is_default) {
1016                 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
1017                 config->nmc_default_nodemap = nodemap;
1018         } else {
1019                 config->nmc_nodemap_highest_id++;
1020                 nodemap->nm_id = config->nmc_nodemap_highest_id;
1021         }
1022
1023         if (is_default || default_nodemap == NULL) {
1024                 nodemap->nmf_trust_client_ids = 0;
1025                 nodemap->nmf_allow_root_access = 0;
1026                 nodemap->nmf_deny_unknown = 0;
1027                 nodemap->nmf_map_uid_only = 0;
1028                 nodemap->nmf_map_gid_only = 0;
1029
1030                 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
1031                 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
1032                 if (!is_default)
1033                         CWARN("adding nodemap '%s' to config without"
1034                               " default nodemap\n", nodemap->nm_name);
1035         } else {
1036                 nodemap->nmf_trust_client_ids =
1037                                 default_nodemap->nmf_trust_client_ids;
1038                 nodemap->nmf_allow_root_access =
1039                                 default_nodemap->nmf_allow_root_access;
1040                 nodemap->nmf_deny_unknown =
1041                                 default_nodemap->nmf_deny_unknown;
1042                 nodemap->nmf_map_uid_only =
1043                                 default_nodemap->nmf_map_uid_only;
1044                 nodemap->nmf_map_gid_only =
1045                                 default_nodemap->nmf_map_gid_only;
1046
1047                 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
1048                 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
1049                 nodemap->nm_fileset[0] = 0;
1050         }
1051
1052         return nodemap;
1053
1054 out:
1055         CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1056         return ERR_PTR(rc);
1057 }
1058
1059 /**
1060  * Set the nmf_deny_unknown flag to true or false.
1061  * \param       name            nodemap name
1062  * \param       deny_unknown    if true, squashed users will get EACCES
1063  * \retval      0 on success
1064  *
1065  */
1066 int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
1067 {
1068         struct lu_nodemap       *nodemap = NULL;
1069         int                     rc = 0;
1070
1071         mutex_lock(&active_config_lock);
1072         nodemap = nodemap_lookup(name);
1073         mutex_unlock(&active_config_lock);
1074         if (IS_ERR(nodemap))
1075                 GOTO(out, rc = PTR_ERR(nodemap));
1076
1077         nodemap->nmf_deny_unknown = deny_unknown;
1078         rc = nodemap_idx_nodemap_update(nodemap);
1079
1080         nm_member_revoke_locks(nodemap);
1081         nodemap_putref(nodemap);
1082 out:
1083         return rc;
1084 }
1085 EXPORT_SYMBOL(nodemap_set_deny_unknown);
1086
1087 /**
1088  * Set the nmf_allow_root_access flag to true or false.
1089  * \param       name            nodemap name
1090  * \param       allow_root      if true, nodemap will not squash the root user
1091  * \retval      0 on success
1092  *
1093  */
1094 int nodemap_set_allow_root(const char *name, bool allow_root)
1095 {
1096         struct lu_nodemap       *nodemap = NULL;
1097         int                     rc = 0;
1098
1099         mutex_lock(&active_config_lock);
1100         nodemap = nodemap_lookup(name);
1101         mutex_unlock(&active_config_lock);
1102         if (IS_ERR(nodemap))
1103                 GOTO(out, rc = PTR_ERR(nodemap));
1104
1105         nodemap->nmf_allow_root_access = allow_root;
1106         rc = nodemap_idx_nodemap_update(nodemap);
1107
1108         nm_member_revoke_locks(nodemap);
1109         nodemap_putref(nodemap);
1110 out:
1111         return rc;
1112 }
1113 EXPORT_SYMBOL(nodemap_set_allow_root);
1114
1115 /**
1116  * Set the nmf_trust_client_ids flag to true or false.
1117  *
1118  * \param       name                    nodemap name
1119  * \param       trust_client_ids        if true, nodemap will not map its IDs
1120  * \retval      0 on success
1121  *
1122  */
1123 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1124 {
1125         struct lu_nodemap       *nodemap = NULL;
1126         int                     rc = 0;
1127
1128         mutex_lock(&active_config_lock);
1129         nodemap = nodemap_lookup(name);
1130         mutex_unlock(&active_config_lock);
1131         if (IS_ERR(nodemap))
1132                 GOTO(out, rc = PTR_ERR(nodemap));
1133
1134         nodemap->nmf_trust_client_ids = trust_client_ids;
1135         rc = nodemap_idx_nodemap_update(nodemap);
1136
1137         nm_member_revoke_locks(nodemap);
1138         nodemap_putref(nodemap);
1139 out:
1140         return rc;
1141 }
1142 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1143
1144 int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
1145 {
1146         struct lu_nodemap       *nodemap = NULL;
1147         int                     rc = 0;
1148
1149         mutex_lock(&active_config_lock);
1150         nodemap = nodemap_lookup(name);
1151         mutex_unlock(&active_config_lock);
1152         if (IS_ERR(nodemap))
1153                 GOTO(out, rc = PTR_ERR(nodemap));
1154
1155         switch (mode) {
1156         case NODEMAP_MAP_BOTH:
1157                 nodemap->nmf_map_uid_only = 0;
1158                 nodemap->nmf_map_gid_only = 0;
1159                 break;
1160         case NODEMAP_MAP_UID_ONLY:
1161                 nodemap->nmf_map_uid_only = 1;
1162                 nodemap->nmf_map_gid_only = 0;
1163                 break;
1164         case NODEMAP_MAP_GID_ONLY:
1165                 nodemap->nmf_map_uid_only = 0;
1166                 nodemap->nmf_map_gid_only = 1;
1167                 break;
1168         default:
1169                 CWARN("cannot set unknown mapping mode, mode = %d\n", mode);
1170         }
1171         rc = nodemap_idx_nodemap_update(nodemap);
1172
1173         nm_member_revoke_locks(nodemap);
1174         nodemap_putref(nodemap);
1175 out:
1176         return rc;
1177 }
1178 EXPORT_SYMBOL(nodemap_set_mapping_mode);
1179
1180 /**
1181  * Update the squash_uid for a nodemap.
1182  *
1183  * \param       name            nodemap name
1184  * \param       uid             the new uid to squash unknown users to
1185  * \retval      0 on success
1186  *
1187  * Update the squash_uid for a nodemap. The squash_uid is the uid
1188  * that the all client uids are mapped to if nodemap is active,
1189  * the trust_client_ids flag is not set, and the uid is not in
1190  * the idmap tree.
1191  */
1192 int nodemap_set_squash_uid(const char *name, uid_t uid)
1193 {
1194         struct lu_nodemap       *nodemap = NULL;
1195         int                     rc = 0;
1196
1197         mutex_lock(&active_config_lock);
1198         nodemap = nodemap_lookup(name);
1199         mutex_unlock(&active_config_lock);
1200         if (IS_ERR(nodemap))
1201                 GOTO(out, rc = PTR_ERR(nodemap));
1202
1203         nodemap->nm_squash_uid = uid;
1204         rc = nodemap_idx_nodemap_update(nodemap);
1205
1206         nm_member_revoke_locks(nodemap);
1207         nodemap_putref(nodemap);
1208 out:
1209         return rc;
1210 }
1211 EXPORT_SYMBOL(nodemap_set_squash_uid);
1212
1213 /**
1214  * Update the squash_gid for a nodemap.
1215  *
1216  * \param       name            nodemap name
1217  * \param       gid             the new gid to squash unknown gids to
1218  * \retval      0 on success
1219  *
1220  * Update the squash_gid for a nodemap. The squash_uid is the gid
1221  * that the all client gids are mapped to if nodemap is active,
1222  * the trust_client_ids flag is not set, and the gid is not in
1223  * the idmap tree.
1224  */
1225 int nodemap_set_squash_gid(const char *name, gid_t gid)
1226 {
1227         struct lu_nodemap       *nodemap = NULL;
1228         int                     rc = 0;
1229
1230         mutex_lock(&active_config_lock);
1231         nodemap = nodemap_lookup(name);
1232         mutex_unlock(&active_config_lock);
1233         if (IS_ERR(nodemap))
1234                 GOTO(out, rc = PTR_ERR(nodemap));
1235
1236         nodemap->nm_squash_gid = gid;
1237         rc = nodemap_idx_nodemap_update(nodemap);
1238
1239         nm_member_revoke_locks(nodemap);
1240         nodemap_putref(nodemap);
1241 out:
1242         return rc;
1243 }
1244 EXPORT_SYMBOL(nodemap_set_squash_gid);
1245
1246 /**
1247  * Returns true if this nodemap has root user access. Always returns true if
1248  * nodemaps are not active.
1249  *
1250  * \param       nodemap         nodemap to check access for
1251  */
1252 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1253 {
1254         return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1255 }
1256 EXPORT_SYMBOL(nodemap_can_setquota);
1257
1258 /**
1259  * Add a nodemap
1260  *
1261  * \param       name            name of nodemap
1262  * \retval      0               success
1263  * \retval      -EINVAL         invalid nodemap name
1264  * \retval      -EEXIST         nodemap already exists
1265  * \retval      -ENOMEM         cannot allocate memory for nodemap
1266  */
1267 int nodemap_add(const char *nodemap_name)
1268 {
1269         struct lu_nodemap *nodemap;
1270         int rc;
1271
1272         mutex_lock(&active_config_lock);
1273         nodemap = nodemap_create(nodemap_name, active_config, 0);
1274         if (IS_ERR(nodemap)) {
1275                 mutex_unlock(&active_config_lock);
1276                 return PTR_ERR(nodemap);
1277         }
1278
1279         rc = nodemap_idx_nodemap_add(nodemap);
1280         if (rc == 0)
1281                 rc = lprocfs_nodemap_register(nodemap, 0);
1282
1283         mutex_unlock(&active_config_lock);
1284         nodemap_putref(nodemap);
1285
1286         return rc;
1287 }
1288 EXPORT_SYMBOL(nodemap_add);
1289
1290 /**
1291  * Delete a nodemap
1292  *
1293  * \param       name            name of nodemmap
1294  * \retval      0               success
1295  * \retval      -EINVAL         invalid input
1296  * \retval      -ENOENT         no existing nodemap
1297  */
1298 int nodemap_del(const char *nodemap_name)
1299 {
1300         struct lu_nodemap       *nodemap;
1301         struct lu_nid_range     *range;
1302         struct lu_nid_range     *range_temp;
1303         int                      rc = 0;
1304         int                      rc2 = 0;
1305
1306         if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1307                 RETURN(-EINVAL);
1308
1309         mutex_lock(&active_config_lock);
1310         nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1311                                    nodemap_name);
1312         if (nodemap == NULL) {
1313                 mutex_unlock(&active_config_lock);
1314                 GOTO(out, rc = -ENOENT);
1315         }
1316
1317         /* erase nodemap from active ranges to prevent client assignment */
1318         down_write(&active_config->nmc_range_tree_lock);
1319         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1320                                  rn_list) {
1321                 rc2 = nodemap_idx_range_del(range);
1322                 if (rc2 < 0)
1323                         rc = rc2;
1324
1325                 range_delete(&active_config->nmc_range_tree, range);
1326         }
1327         up_write(&active_config->nmc_range_tree_lock);
1328
1329         rc2 = nodemap_idx_nodemap_del(nodemap);
1330         if (rc2 < 0)
1331                 rc = rc2;
1332
1333         /*
1334          * remove procfs here in case nodemap_create called with same name
1335          * before nodemap_destroy is run.
1336          */
1337         lprocfs_nodemap_remove(nodemap->nm_pde_data);
1338         nodemap->nm_pde_data = NULL;
1339
1340         /* reclassify all member exports from nodemap, so they put their refs */
1341         down_read(&active_config->nmc_range_tree_lock);
1342         nm_member_reclassify_nodemap(nodemap);
1343         up_read(&active_config->nmc_range_tree_lock);
1344
1345         if (!list_empty(&nodemap->nm_member_list))
1346                 CWARN("nodemap_del failed to reclassify all members\n");
1347
1348         mutex_unlock(&active_config_lock);
1349
1350         nodemap_putref(nodemap);
1351
1352 out:
1353         return rc;
1354 }
1355 EXPORT_SYMBOL(nodemap_del);
1356
1357 /**
1358  * activate nodemap functions
1359  *
1360  * \param       value           1 for on, 0 for off
1361  */
1362 void nodemap_activate(const bool value)
1363 {
1364         mutex_lock(&active_config_lock);
1365         active_config->nmc_nodemap_is_active = value;
1366
1367         /* copy active value to global to avoid locking in map functions */
1368         nodemap_active = value;
1369         nodemap_idx_nodemap_activate(value);
1370         mutex_unlock(&active_config_lock);
1371         nm_member_revoke_all();
1372 }
1373 EXPORT_SYMBOL(nodemap_activate);
1374
1375 /**
1376  * Helper iterator to convert nodemap hash to list.
1377  *
1378  * \param       hs                      hash structure
1379  * \param       bd                      bucket descriptor
1380  * \param       hnode                   hash node
1381  * \param       nodemap_list_head       list head for list of nodemaps in hash
1382  */
1383 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1384                                    struct hlist_node *hnode,
1385                                    void *nodemap_list_head)
1386 {
1387         struct lu_nodemap *nodemap;
1388
1389         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1390         list_add(&nodemap->nm_list, nodemap_list_head);
1391
1392         cfs_hash_bd_del_locked(hs, bd, hnode);
1393
1394         return 0;
1395 }
1396
1397 struct nodemap_config *nodemap_config_alloc(void)
1398 {
1399         struct nodemap_config *config;
1400         int rc = 0;
1401
1402         OBD_ALLOC_PTR(config);
1403         if (config == NULL)
1404                 return ERR_PTR(-ENOMEM);
1405
1406         rc = nodemap_init_hash(config);
1407         if (rc != 0) {
1408                 OBD_FREE_PTR(config);
1409                 return ERR_PTR(rc);
1410         }
1411
1412         init_rwsem(&config->nmc_range_tree_lock);
1413
1414         return config;
1415 }
1416 EXPORT_SYMBOL(nodemap_config_alloc);
1417
1418 /**
1419  * Walk the nodemap_hash and remove all nodemaps.
1420  */
1421 void nodemap_config_dealloc(struct nodemap_config *config)
1422 {
1423         struct lu_nodemap       *nodemap = NULL;
1424         struct lu_nodemap       *nodemap_temp;
1425         struct lu_nid_range     *range;
1426         struct lu_nid_range     *range_temp;
1427         LIST_HEAD(nodemap_list_head);
1428
1429         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1430                                nodemap_cleanup_iter_cb, &nodemap_list_head);
1431         cfs_hash_putref(config->nmc_nodemap_hash);
1432
1433         /* Because nodemap_destroy might sleep, we can't destroy them
1434          * in cfs_hash_for_each, so we build a list there and destroy here
1435          */
1436         list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1437                                  nm_list) {
1438                 mutex_lock(&active_config_lock);
1439                 down_write(&config->nmc_range_tree_lock);
1440
1441                 /* move members to new config, requires ac lock */
1442                 nm_member_reclassify_nodemap(nodemap);
1443                 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1444                                          rn_list)
1445                         range_delete(&config->nmc_range_tree, range);
1446                 up_write(&config->nmc_range_tree_lock);
1447                 mutex_unlock(&active_config_lock);
1448
1449                 /* putref must be outside of ac lock if nm could be destroyed */
1450                 nodemap_putref(nodemap);
1451         }
1452         OBD_FREE_PTR(config);
1453 }
1454 EXPORT_SYMBOL(nodemap_config_dealloc);
1455
1456 /*
1457  * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
1458  * nodemap list, generally for locking purposes as a hash cb can't sleep.
1459  */
1460 int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1461                     struct hlist_node *hnode,
1462                     void *nodemap_list_head)
1463 {
1464         struct lu_nodemap *nodemap;
1465
1466         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1467         list_add(&nodemap->nm_list, nodemap_list_head);
1468         return 0;
1469 }
1470
1471 void nodemap_config_set_active(struct nodemap_config *config)
1472 {
1473         struct nodemap_config   *old_config = active_config;
1474         struct lu_nodemap       *nodemap;
1475         struct lu_nodemap       *tmp;
1476         bool revoke_locks;
1477         LIST_HEAD(nodemap_list_head);
1478
1479         ENTRY;
1480
1481         LASSERT(active_config != config);
1482         LASSERT(config->nmc_default_nodemap);
1483
1484         mutex_lock(&active_config_lock);
1485
1486         /* move proc entries from already existing nms, create for new nms */
1487         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1488                                nm_hash_list_cb, &nodemap_list_head);
1489         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1490                 struct lu_nodemap *old_nm = NULL;
1491
1492                 if (active_config != NULL)
1493                         old_nm = cfs_hash_lookup(
1494                                         active_config->nmc_nodemap_hash,
1495                                         nodemap->nm_name);
1496                 if (old_nm != NULL) {
1497                         nodemap->nm_pde_data = old_nm->nm_pde_data;
1498                         old_nm->nm_pde_data = NULL;
1499                         nodemap_putref(old_nm);
1500                 } else {
1501                         bool is_def = (nodemap == config->nmc_default_nodemap);
1502
1503                         lprocfs_nodemap_register(nodemap, is_def);
1504                 }
1505         }
1506
1507         /*
1508          * We only need to revoke locks if old nodemap was active, and new
1509          * config is now nodemap inactive. nodemap_config_dealloc will
1510          * reclassify exports, triggering a lock revoke if and only if new
1511          * nodemap is active.
1512          */
1513         revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
1514
1515         /* if new config is inactive, deactivate live config before switching */
1516         if (!config->nmc_nodemap_is_active)
1517                 nodemap_active = false;
1518         active_config = config;
1519         if (config->nmc_nodemap_is_active)
1520                 nodemap_active = true;
1521
1522         mutex_unlock(&active_config_lock);
1523
1524         if (old_config != NULL)
1525                 nodemap_config_dealloc(old_config);
1526
1527         if (revoke_locks)
1528                 nm_member_revoke_all();
1529
1530         EXIT;
1531 }
1532
1533 /**
1534  * Cleanup nodemap module on exit
1535  */
1536 void nodemap_mod_exit(void)
1537 {
1538         nodemap_config_dealloc(active_config);
1539         nodemap_procfs_exit();
1540 }
1541
1542 /**
1543  * Initialize the nodemap module
1544  */
1545 int nodemap_mod_init(void)
1546 {
1547         struct nodemap_config   *new_config;
1548         struct lu_nodemap       *nodemap;
1549         int                      rc = 0;
1550
1551         rc = nodemap_procfs_init();
1552         if (rc != 0)
1553                 return rc;
1554
1555         new_config = nodemap_config_alloc();
1556         if (IS_ERR(new_config)) {
1557                 nodemap_procfs_exit();
1558                 GOTO(out, rc = PTR_ERR(new_config));
1559         }
1560
1561         nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1562         if (IS_ERR(nodemap)) {
1563                 nodemap_config_dealloc(new_config);
1564                 nodemap_procfs_exit();
1565                 GOTO(out, rc = PTR_ERR(nodemap));
1566         }
1567
1568         nodemap_config_set_active(new_config);
1569         nodemap_putref(nodemap);
1570
1571 out:
1572         return rc;
1573 }
1574
1575 /**
1576  * Revoke locks for all nodemaps.
1577  */
1578 void nm_member_revoke_all(void)
1579 {
1580         struct lu_nodemap *nodemap;
1581         struct lu_nodemap *tmp;
1582         LIST_HEAD(nodemap_list_head);
1583
1584         mutex_lock(&active_config_lock);
1585         cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1586                                nm_hash_list_cb, &nodemap_list_head);
1587
1588         /* revoke_locks sleeps, so can't call in cfs hash cb */
1589         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1590                 nm_member_revoke_locks_always(nodemap);
1591         mutex_unlock(&active_config_lock);
1592 }
1593
1594 /**
1595  * Returns the nodemap classification for a given nid into an ioctl buffer.
1596  * Useful for testing the nodemap configuration to make sure it is working as
1597  * expected.
1598  *
1599  * \param       nid             nid to classify
1600  * \param[out]  name_buf        buffer to write the nodemap name to
1601  * \param       name_len        length of buffer
1602  */
1603 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1604 {
1605         struct lu_nodemap       *nodemap;
1606
1607         mutex_lock(&active_config_lock);
1608         down_read(&active_config->nmc_range_tree_lock);
1609         nodemap = nodemap_classify_nid(nid);
1610         up_read(&active_config->nmc_range_tree_lock);
1611         mutex_unlock(&active_config_lock);
1612
1613         if (IS_ERR(nodemap))
1614                 return;
1615
1616         strncpy(name_buf, nodemap->nm_name, name_len);
1617         if (name_len > 0)
1618                 name_buf[name_len - 1] = '\0';
1619
1620         nodemap_putref(nodemap);
1621 }
1622 EXPORT_SYMBOL(nodemap_test_nid);
1623
1624 /**
1625  * Passes back the id mapping for a given nid/id pair. Useful for testing the
1626  * nodemap configuration to make sure it is working as expected.
1627  *
1628  * \param       nid             nid to classify
1629  * \param       idtype          uid or gid
1630  * \param       client_id       id to map to fs
1631  * \param       fs_id_buf       pointer to save mapped fs_id to
1632  *
1633  * \retval      0       success
1634  * \retval      -EINVAL invalid NID
1635  */
1636 int nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1637                     __u32 client_id, __u32 *fs_id)
1638 {
1639         struct lu_nodemap       *nodemap;
1640
1641         mutex_lock(&active_config_lock);
1642         down_read(&active_config->nmc_range_tree_lock);
1643         nodemap = nodemap_classify_nid(nid);
1644         up_read(&active_config->nmc_range_tree_lock);
1645         mutex_unlock(&active_config_lock);
1646
1647         if (IS_ERR(nodemap))
1648                 return PTR_ERR(nodemap);
1649
1650         *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1651                                client_id);
1652         nodemap_putref(nodemap);
1653
1654         return 0;
1655 }
1656 EXPORT_SYMBOL(nodemap_test_id);