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