Whamcloud - gitweb
a6da068c01259e69aa3319d92be05bdeea2f64ce
[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 mapped uid or gid if any.
666  * Otherwise, return the squash uid or gid.
667  *
668  * if the nodemap is configured to trusted the ids from the client system, just
669  * return the passed id without mapping.
670  *
671  * if by this point, we haven't returned and the nodemap in question is the
672  * default nodemap, return the squash uid or gid.
673  *
674  * after these checks, search the proper tree for the mapping, and if found
675  * return the mapped value, otherwise return the squash uid or gid.
676  */
677 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
678                      enum nodemap_id_type id_type,
679                      enum nodemap_tree_type tree_type, __u32 id)
680 {
681         struct lu_idmap         *idmap = NULL;
682         __u32                    found_id;
683
684         ENTRY;
685
686         if (!nodemap_active)
687                 goto out;
688
689         if (unlikely(nodemap == NULL))
690                 goto out;
691
692         if (id == 0) {
693                 if (nodemap->nmf_allow_root_access)
694                         goto out;
695                 goto map;
696         }
697
698         if (id_type == NODEMAP_UID &&
699             !(nodemap->nmf_map_mode & NODEMAP_MAP_UID))
700                 goto out;
701
702         if (id_type == NODEMAP_GID &&
703             !(nodemap->nmf_map_mode & NODEMAP_MAP_GID))
704                 goto out;
705
706         if (id_type == NODEMAP_PROJID &&
707             !(nodemap->nmf_map_mode & NODEMAP_MAP_PROJID))
708                 goto out;
709
710         if (nodemap->nmf_trust_client_ids)
711                 goto out;
712
713 map:
714         if (is_default_nodemap(nodemap))
715                 goto squash;
716
717         down_read(&nodemap->nm_idmap_lock);
718         idmap = idmap_search(nodemap, tree_type, id_type, id);
719         if (idmap == NULL) {
720                 up_read(&nodemap->nm_idmap_lock);
721                 goto squash;
722         }
723
724         if (tree_type == NODEMAP_FS_TO_CLIENT)
725                 found_id = idmap->id_client;
726         else
727                 found_id = idmap->id_fs;
728         up_read(&nodemap->nm_idmap_lock);
729         RETURN(found_id);
730
731 squash:
732         if (id_type == NODEMAP_UID)
733                 RETURN(nodemap->nm_squash_uid);
734         if (id_type == NODEMAP_GID)
735                 RETURN(nodemap->nm_squash_gid);
736         if (id_type == NODEMAP_PROJID)
737                 RETURN(nodemap->nm_squash_projid);
738 out:
739         RETURN(id);
740 }
741 EXPORT_SYMBOL(nodemap_map_id);
742
743 /**
744  * Map posix ACL entries according to the nodemap membership. Removes any
745  * squashed ACLs.
746  *
747  * \param       lu_nodemap      nodemap
748  * \param       buf             buffer containing xattr encoded ACLs
749  * \param       size            size of ACLs in bytes
750  * \param       tree_type       direction of mapping
751  * \retval      size            new size of ACLs in bytes
752  * \retval      -EINVAL         bad \a size param, see posix_acl_xattr_count()
753  */
754 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
755                         enum nodemap_tree_type tree_type)
756 {
757         posix_acl_xattr_header  *header = buf;
758         posix_acl_xattr_entry   *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
759         posix_acl_xattr_entry   *new_entry = entry;
760         posix_acl_xattr_entry   *end;
761         int                      count;
762
763         ENTRY;
764
765         if (!nodemap_active)
766                 RETURN(size);
767
768         if (unlikely(nodemap == NULL))
769                 RETURN(size);
770
771         count = posix_acl_xattr_count(size);
772         if (count < 0)
773                 RETURN(-EINVAL);
774         if (count == 0)
775                 /* if not proper ACL, do nothing and return initial size */
776                 RETURN(size);
777
778         for (end = entry + count; entry != end; entry++) {
779                 __u16 tag = le16_to_cpu(entry->e_tag);
780                 __u32 id = le32_to_cpu(entry->e_id);
781
782                 switch (tag) {
783                 case ACL_USER:
784                         id = nodemap_map_id(nodemap, NODEMAP_UID,
785                                             tree_type, id);
786                         if (id == nodemap->nm_squash_uid)
787                                 continue;
788                         entry->e_id = cpu_to_le32(id);
789                         break;
790                 case ACL_GROUP:
791                         id = nodemap_map_id(nodemap, NODEMAP_GID,
792                                             tree_type, id);
793                         if (id == nodemap->nm_squash_gid)
794                                 continue;
795                         entry->e_id = cpu_to_le32(id);
796                         break;
797                 }
798
799                 /* if we skip an ACL, copy the following ones over it */
800                 if (new_entry != entry)
801                         *new_entry = *entry;
802
803                 new_entry++;
804         }
805
806         RETURN((void *)new_entry - (void *)header);
807 }
808 EXPORT_SYMBOL(nodemap_map_acl);
809
810 /*
811  * Add nid range to given nodemap
812  *
813  * \param       config          nodemap config to work on
814  * \param       nodemap         nodemap to add range to
815  * \param       nid             nid range to add
816  * \param       range_id        should be 0 unless loading from disk
817  * \retval      0               success
818  * \retval      -ENOMEM
819  *
820  */
821 int nodemap_add_range_helper(struct nodemap_config *config,
822                              struct lu_nodemap *nodemap,
823                              const struct lnet_nid nid[2],
824                              u8 netmask, unsigned int range_id)
825 {
826         struct lu_nid_range *range;
827         int rc;
828
829         down_write(&config->nmc_range_tree_lock);
830         range = range_create(config, &nid[0], &nid[1], netmask, nodemap,
831                              range_id);
832         if (range == NULL) {
833                 up_write(&config->nmc_range_tree_lock);
834                 GOTO(out, rc = -ENOMEM);
835         }
836
837         rc = range_insert(config, range);
838         if (rc) {
839                 CDEBUG_LIMIT(rc == -EEXIST ? D_INFO : D_ERROR,
840                              "cannot insert nodemap range into '%s': rc = %d\n",
841                              nodemap->nm_name, rc);
842                 up_write(&config->nmc_range_tree_lock);
843                 list_del(&range->rn_list);
844                 range_destroy(range);
845                 GOTO(out, rc);
846         }
847
848         list_add(&range->rn_list, &nodemap->nm_ranges);
849
850         /* nodemaps have no members if they aren't on the active config */
851         if (config == active_config)
852                 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
853
854         up_write(&config->nmc_range_tree_lock);
855
856         /* if range_id is non-zero, we are loading from disk */
857         if (range_id == 0)
858                 rc = nodemap_idx_range_add(range);
859
860         if (config == active_config) {
861                 nm_member_revoke_locks(config->nmc_default_nodemap);
862                 nm_member_revoke_locks(nodemap);
863         }
864
865 out:
866         return rc;
867 }
868
869 int nodemap_add_range(const char *name, const struct lnet_nid nid[2],
870                       u8 netmask)
871 {
872         struct lu_nodemap       *nodemap = NULL;
873         int                      rc;
874
875         mutex_lock(&active_config_lock);
876         nodemap = nodemap_lookup(name);
877         if (IS_ERR(nodemap)) {
878                 mutex_unlock(&active_config_lock);
879                 GOTO(out, rc = PTR_ERR(nodemap));
880         }
881
882         if (is_default_nodemap(nodemap))
883                 rc = -EINVAL;
884         else
885                 rc = nodemap_add_range_helper(active_config, nodemap, nid,
886                                               netmask, 0);
887         mutex_unlock(&active_config_lock);
888         nodemap_putref(nodemap);
889 out:
890         return rc;
891 }
892 EXPORT_SYMBOL(nodemap_add_range);
893
894 /**
895  * delete a range
896  * \param       name            nodemap name
897  * \param       nid             nid range
898  * \retval      0 on success
899  *
900  * Delete range from global range tree, and remove it
901  * from the list in the associated nodemap.
902  */
903 int nodemap_del_range(const char *name, const struct lnet_nid nid[2],
904                       u8 netmask)
905 {
906         struct lu_nodemap       *nodemap;
907         struct lu_nid_range     *range;
908         int                     rc = 0;
909
910         mutex_lock(&active_config_lock);
911         nodemap = nodemap_lookup(name);
912         if (IS_ERR(nodemap)) {
913                 mutex_unlock(&active_config_lock);
914                 GOTO(out, rc = PTR_ERR(nodemap));
915         }
916
917         if (is_default_nodemap(nodemap))
918                 GOTO(out_putref, rc = -EINVAL);
919
920         down_write(&active_config->nmc_range_tree_lock);
921         range = range_find(active_config, &nid[0], &nid[1], netmask);
922         if (range == NULL) {
923                 up_write(&active_config->nmc_range_tree_lock);
924                 GOTO(out_putref, rc = -EINVAL);
925         }
926         if (range->rn_nodemap != nodemap) {
927                 up_write(&active_config->nmc_range_tree_lock);
928                 GOTO(out_putref, rc = -EINVAL);
929         }
930         rc = nodemap_idx_range_del(range);
931         range_delete(active_config, range);
932         nm_member_reclassify_nodemap(nodemap);
933         up_write(&active_config->nmc_range_tree_lock);
934
935         nm_member_revoke_locks(active_config->nmc_default_nodemap);
936         nm_member_revoke_locks(nodemap);
937
938 out_putref:
939         mutex_unlock(&active_config_lock);
940         nodemap_putref(nodemap);
941 out:
942         return rc;
943 }
944 EXPORT_SYMBOL(nodemap_del_range);
945
946 /**
947  * set fileset on nodemap
948  * \param       name            nodemap to set fileset on
949  * \param       fileset         string containing fileset
950  * \retval      0 on success
951  *
952  * set a fileset on the named nodemap
953  */
954 static int nodemap_set_fileset_helper(struct nodemap_config *config,
955                                       struct lu_nodemap *nodemap,
956                                       const char *fileset)
957 {
958         int rc = 0;
959
960         /* Allow 'fileset=clear' in addition to 'fileset=""' to clear fileset
961          * because either command 'lctl set_param -P *.*.fileset=""' or
962          * 'lctl nodemap_set_fileset --fileset ""' can only work correctly
963          * on MGS, while on other servers, both commands will invoke upcall
964          * "/usr/sbin/lctl set_param nodemap.default.fileset=" by function
965          * process_param2_config(), which will cause "no value" error and
966          * won't clear fileset.
967          * 'fileset=""' is still kept for compatibility reason.
968          */
969         if (fileset == NULL)
970                 rc = -EINVAL;
971         else if (fileset[0] == '\0' || strcmp(fileset, "clear") == 0)
972                 nodemap->nm_fileset[0] = '\0';
973         else if (fileset[0] != '/')
974                 rc = -EINVAL;
975         else if (strscpy(nodemap->nm_fileset, fileset,
976                          sizeof(nodemap->nm_fileset)) < 0)
977                 rc = -ENAMETOOLONG;
978
979         return rc;
980 }
981
982 int nodemap_set_fileset(const char *name, const char *fileset)
983 {
984         struct lu_nodemap       *nodemap = NULL;
985         int                      rc = 0;
986
987         mutex_lock(&active_config_lock);
988         nodemap = nodemap_lookup(name);
989         if (IS_ERR(nodemap)) {
990                 mutex_unlock(&active_config_lock);
991                 GOTO(out, rc = PTR_ERR(nodemap));
992         }
993
994         rc = nodemap_set_fileset_helper(active_config, nodemap, fileset);
995         mutex_unlock(&active_config_lock);
996
997         nodemap_putref(nodemap);
998 out:
999         return rc;
1000 }
1001 EXPORT_SYMBOL(nodemap_set_fileset);
1002
1003 /**
1004  * get fileset defined on nodemap
1005  * \param       nodemap         nodemap to get fileset from
1006  * \retval      fileset name, or NULL if not defined or not activated
1007  *
1008  * get the fileset defined on the nodemap
1009  */
1010 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
1011 {
1012         if (!nodemap_active)
1013                 return NULL;
1014
1015         return (char *)nodemap->nm_fileset;
1016 }
1017 EXPORT_SYMBOL(nodemap_get_fileset);
1018
1019 static int nodemap_validate_sepol(const char *sepol)
1020 {
1021         char buf[LUSTRE_NODEMAP_SEPOL_LENGTH + 1];
1022         char *p = (char *)sepol;
1023         char *q = buf;
1024         char polname[NAME_MAX + 1] = "";
1025         char hash[SELINUX_POLICY_HASH_LEN + 1] = "";
1026         unsigned char mode;
1027         unsigned short ver;
1028
1029         BUILD_BUG_ON(sizeof(buf) != sizeof(((struct lu_nodemap *)0)->nm_sepol));
1030
1031         if (sepol == NULL)
1032                 return -EINVAL;
1033
1034         /* we allow sepol = "" which means clear SELinux policy info */
1035         if (sepol[0] == '\0')
1036                 return 0;
1037
1038         /* make a copy of sepol, by replacing ':' with space
1039          * so that we can use sscanf over the string
1040          */
1041         while (p-sepol < sizeof(buf)) {
1042                 if (*p == ':')
1043                         *q = ' ';
1044                 else
1045                         *q = *p;
1046                 if (*p == '\0')
1047                         break;
1048                 p++;
1049                 q++;
1050         }
1051         if (p-sepol == sizeof(buf))
1052                 return -ENAMETOOLONG;
1053
1054         if (sscanf(buf, "%1hhu %s %hu %s", &mode, polname, &ver, hash) != 4)
1055                 return -EINVAL;
1056
1057         if (mode != 0 && mode != 1)
1058                 return -EINVAL;
1059
1060         return 0;
1061 }
1062
1063 /**
1064  * set SELinux policy on nodemap
1065  * \param       name            nodemap to set SELinux policy info on
1066  * \param       sepol           string containing SELinux policy info
1067  * \retval      0 on success
1068  *
1069  * set SELinux policy info on the named nodemap
1070  */
1071 int nodemap_set_sepol(const char *name, const char *sepol)
1072 {
1073         struct lu_nodemap       *nodemap = NULL;
1074         int                      rc;
1075
1076         rc = nodemap_validate_sepol(sepol);
1077         if (rc < 0)
1078                 GOTO(out, rc);
1079
1080         mutex_lock(&active_config_lock);
1081         nodemap = nodemap_lookup(name);
1082         if (IS_ERR(nodemap)) {
1083                 mutex_unlock(&active_config_lock);
1084                 GOTO(out, rc = PTR_ERR(nodemap));
1085         }
1086
1087         if (is_default_nodemap(nodemap)) {
1088                 /* We do not want nodes in the default nodemap to have
1089                  * SELinux restrictions. Sec admin should create dedicated
1090                  * nodemap entries for this.
1091                  */
1092                 GOTO(out_putref, rc = -EINVAL);
1093         }
1094
1095         /* truncation cannot happen, as string length was checked in
1096          * nodemap_validate_sepol()
1097          */
1098         strscpy(nodemap->nm_sepol, sepol, sizeof(nodemap->nm_sepol));
1099
1100 out_putref:
1101         mutex_unlock(&active_config_lock);
1102         nodemap_putref(nodemap);
1103 out:
1104         return rc;
1105 }
1106 EXPORT_SYMBOL(nodemap_set_sepol);
1107
1108 /**
1109  * get SELinux policy info defined on nodemap
1110  * \param       nodemap         nodemap to get SELinux policy info from
1111  * \retval      SELinux policy info, or NULL if not defined or not activated
1112  *
1113  * get the SELinux policy info defined on the nodemap
1114  */
1115 const char *nodemap_get_sepol(const struct lu_nodemap *nodemap)
1116 {
1117         if (is_default_nodemap(nodemap))
1118                 return NULL;
1119         else
1120                 return (char *)nodemap->nm_sepol;
1121 }
1122 EXPORT_SYMBOL(nodemap_get_sepol);
1123
1124 /**
1125  * Nodemap constructor
1126  *
1127  * Creates an lu_nodemap structure and assigns sane default
1128  * member values. If this is the default nodemap, the defaults
1129  * are the most restrictive in terms of mapping behavior. Otherwise
1130  * the default flags should be inherited from the default nodemap.
1131  * The adds nodemap to nodemap_hash.
1132  *
1133  * Requires that the caller take the active_config_lock
1134  *
1135  * \param       name            name of nodemap
1136  * \param       is_default      true if default nodemap
1137  * \retval      nodemap         success
1138  * \retval      -EINVAL         invalid nodemap name
1139  * \retval      -EEXIST         nodemap already exists
1140  * \retval      -ENOMEM         cannot allocate memory for nodemap
1141  */
1142 struct lu_nodemap *nodemap_create(const char *name,
1143                                   struct nodemap_config *config,
1144                                   bool is_default)
1145 {
1146         struct lu_nodemap       *nodemap = NULL;
1147         struct lu_nodemap       *default_nodemap;
1148         struct cfs_hash         *hash = config->nmc_nodemap_hash;
1149         int                      rc = 0;
1150         ENTRY;
1151
1152         default_nodemap = config->nmc_default_nodemap;
1153
1154         if (!nodemap_name_is_valid(name))
1155                 GOTO(out, rc = -EINVAL);
1156
1157         if (hash == NULL) {
1158                 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
1159                 GOTO(out, rc = -EINVAL);
1160         }
1161
1162         OBD_ALLOC_PTR(nodemap);
1163         if (nodemap == NULL) {
1164                 CERROR("cannot allocate memory (%zu bytes) for nodemap '%s'\n",
1165                        sizeof(*nodemap), name);
1166                 GOTO(out, rc = -ENOMEM);
1167         }
1168
1169         /*
1170          * take an extra reference to prevent nodemap from being destroyed
1171          * while it's being created.
1172          */
1173         atomic_set(&nodemap->nm_refcount, 2);
1174         snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
1175         rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
1176         if (rc != 0) {
1177                 OBD_FREE_PTR(nodemap);
1178                 GOTO(out, rc = -EEXIST);
1179         }
1180
1181         INIT_LIST_HEAD(&nodemap->nm_ranges);
1182         INIT_LIST_HEAD(&nodemap->nm_list);
1183         INIT_LIST_HEAD(&nodemap->nm_member_list);
1184
1185         mutex_init(&nodemap->nm_member_list_lock);
1186         init_rwsem(&nodemap->nm_idmap_lock);
1187         nodemap->nm_fs_to_client_uidmap = RB_ROOT;
1188         nodemap->nm_client_to_fs_uidmap = RB_ROOT;
1189         nodemap->nm_fs_to_client_gidmap = RB_ROOT;
1190         nodemap->nm_client_to_fs_gidmap = RB_ROOT;
1191         nodemap->nm_fs_to_client_projidmap = RB_ROOT;
1192         nodemap->nm_client_to_fs_projidmap = RB_ROOT;
1193
1194         if (is_default) {
1195                 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
1196                 config->nmc_default_nodemap = nodemap;
1197         } else {
1198                 config->nmc_nodemap_highest_id++;
1199                 nodemap->nm_id = config->nmc_nodemap_highest_id;
1200         }
1201
1202         if (is_default || default_nodemap == NULL) {
1203                 nodemap->nmf_trust_client_ids = 0;
1204                 nodemap->nmf_allow_root_access = 0;
1205                 nodemap->nmf_deny_unknown = 0;
1206                 nodemap->nmf_map_mode = NODEMAP_MAP_ALL;
1207                 nodemap->nmf_enable_audit = 1;
1208                 nodemap->nmf_forbid_encryption = 0;
1209                 nodemap->nmf_readonly_mount = 0;
1210                 nodemap->nmf_rbac = NODEMAP_RBAC_ALL;
1211
1212                 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
1213                 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
1214                 nodemap->nm_squash_projid = NODEMAP_NOBODY_PROJID;
1215                 nodemap->nm_fileset[0] = '\0';
1216                 nodemap->nm_sepol[0] = '\0';
1217                 if (!is_default)
1218                         CWARN("adding nodemap '%s' to config without"
1219                               " default nodemap\n", nodemap->nm_name);
1220         } else {
1221                 nodemap->nmf_trust_client_ids =
1222                                 default_nodemap->nmf_trust_client_ids;
1223                 nodemap->nmf_allow_root_access =
1224                                 default_nodemap->nmf_allow_root_access;
1225                 nodemap->nmf_deny_unknown = default_nodemap->nmf_deny_unknown;
1226                 nodemap->nmf_map_mode = default_nodemap->nmf_map_mode;
1227                 nodemap->nmf_enable_audit = default_nodemap->nmf_enable_audit;
1228                 nodemap->nmf_forbid_encryption =
1229                         default_nodemap->nmf_forbid_encryption;
1230                 nodemap->nmf_readonly_mount =
1231                         default_nodemap->nmf_readonly_mount;
1232                 nodemap->nmf_rbac = default_nodemap->nmf_rbac;
1233
1234                 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
1235                 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
1236                 nodemap->nm_squash_projid = default_nodemap->nm_squash_projid;
1237                 nodemap->nm_fileset[0] = '\0';
1238                 nodemap->nm_sepol[0] = '\0';
1239         }
1240
1241         RETURN(nodemap);
1242
1243 out:
1244         CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1245         RETURN(ERR_PTR(rc));
1246 }
1247
1248 /**
1249  * Set the nmf_deny_unknown flag to true or false.
1250  * \param       name            nodemap name
1251  * \param       deny_unknown    if true, squashed users will get EACCES
1252  * \retval      0 on success
1253  *
1254  */
1255 int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
1256 {
1257         struct lu_nodemap       *nodemap = NULL;
1258         int                     rc = 0;
1259
1260         mutex_lock(&active_config_lock);
1261         nodemap = nodemap_lookup(name);
1262         mutex_unlock(&active_config_lock);
1263         if (IS_ERR(nodemap))
1264                 GOTO(out, rc = PTR_ERR(nodemap));
1265
1266         nodemap->nmf_deny_unknown = deny_unknown;
1267         rc = nodemap_idx_nodemap_update(nodemap);
1268
1269         nm_member_revoke_locks(nodemap);
1270         nodemap_putref(nodemap);
1271 out:
1272         return rc;
1273 }
1274 EXPORT_SYMBOL(nodemap_set_deny_unknown);
1275
1276 /**
1277  * Set the nmf_allow_root_access flag to true or false.
1278  * \param       name            nodemap name
1279  * \param       allow_root      if true, nodemap will not squash the root user
1280  * \retval      0 on success
1281  *
1282  */
1283 int nodemap_set_allow_root(const char *name, bool allow_root)
1284 {
1285         struct lu_nodemap       *nodemap = NULL;
1286         int                     rc = 0;
1287
1288         mutex_lock(&active_config_lock);
1289         nodemap = nodemap_lookup(name);
1290         mutex_unlock(&active_config_lock);
1291         if (IS_ERR(nodemap))
1292                 GOTO(out, rc = PTR_ERR(nodemap));
1293
1294         nodemap->nmf_allow_root_access = allow_root;
1295         rc = nodemap_idx_nodemap_update(nodemap);
1296
1297         nm_member_revoke_locks(nodemap);
1298         nodemap_putref(nodemap);
1299 out:
1300         return rc;
1301 }
1302 EXPORT_SYMBOL(nodemap_set_allow_root);
1303
1304 /**
1305  * Set the nmf_trust_client_ids flag to true or false.
1306  *
1307  * \param       name                    nodemap name
1308  * \param       trust_client_ids        if true, nodemap will not map its IDs
1309  * \retval      0 on success
1310  *
1311  */
1312 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1313 {
1314         struct lu_nodemap       *nodemap = NULL;
1315         int                     rc = 0;
1316
1317         mutex_lock(&active_config_lock);
1318         nodemap = nodemap_lookup(name);
1319         mutex_unlock(&active_config_lock);
1320         if (IS_ERR(nodemap))
1321                 GOTO(out, rc = PTR_ERR(nodemap));
1322
1323         nodemap->nmf_trust_client_ids = trust_client_ids;
1324         rc = nodemap_idx_nodemap_update(nodemap);
1325
1326         nm_member_revoke_locks(nodemap);
1327         nodemap_putref(nodemap);
1328 out:
1329         return rc;
1330 }
1331 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1332
1333 int nodemap_set_mapping_mode(const char *name,
1334                              enum nodemap_mapping_modes map_mode)
1335 {
1336         struct lu_nodemap       *nodemap = NULL;
1337         int                     rc = 0;
1338
1339         mutex_lock(&active_config_lock);
1340         nodemap = nodemap_lookup(name);
1341         mutex_unlock(&active_config_lock);
1342         if (IS_ERR(nodemap))
1343                 GOTO(out, rc = PTR_ERR(nodemap));
1344
1345         nodemap->nmf_map_mode = map_mode;
1346         rc = nodemap_idx_nodemap_update(nodemap);
1347
1348         nm_member_revoke_locks(nodemap);
1349         nodemap_putref(nodemap);
1350 out:
1351         return rc;
1352 }
1353 EXPORT_SYMBOL(nodemap_set_mapping_mode);
1354
1355 int nodemap_set_rbac(const char *name, enum nodemap_rbac_roles rbac)
1356 {
1357         struct lu_nodemap *nodemap = NULL;
1358         enum nodemap_rbac_roles old_rbac;
1359         int rc = 0;
1360
1361         mutex_lock(&active_config_lock);
1362         nodemap = nodemap_lookup(name);
1363         mutex_unlock(&active_config_lock);
1364         if (IS_ERR(nodemap))
1365                 GOTO(out, rc = PTR_ERR(nodemap));
1366
1367         if (is_default_nodemap(nodemap))
1368                 GOTO(put, rc = -EINVAL);
1369
1370         old_rbac = nodemap->nmf_rbac;
1371         /* if value does not change, do nothing */
1372         if (rbac == old_rbac)
1373                 GOTO(put, rc = 0);
1374
1375         nodemap->nmf_rbac = rbac;
1376         if (rbac == NODEMAP_RBAC_ALL)
1377                 /* if new value is ALL (default), just delete
1378                  * NODEMAP_CLUSTER_ROLES idx
1379                  */
1380                 rc = nodemap_idx_cluster_roles_del(nodemap);
1381         else if (old_rbac == NODEMAP_RBAC_ALL)
1382                 /* if old value is ALL (default), need to insert
1383                  * NODEMAP_CLUSTER_ROLES idx
1384                  */
1385                 rc = nodemap_idx_cluster_roles_add(nodemap);
1386         else
1387                 /* otherwise just update existing NODEMAP_CLUSTER_ROLES idx */
1388                 rc = nodemap_idx_cluster_roles_update(nodemap);
1389
1390         nm_member_revoke_locks(nodemap);
1391 put:
1392         nodemap_putref(nodemap);
1393 out:
1394         return rc;
1395 }
1396 EXPORT_SYMBOL(nodemap_set_rbac);
1397
1398 /**
1399  * Update the squash_uid for a nodemap.
1400  *
1401  * \param       name            nodemap name
1402  * \param       uid             the new uid to squash unknown users to
1403  * \retval      0 on success
1404  *
1405  * Update the squash_uid for a nodemap. The squash_uid is the uid
1406  * that the all client uids are mapped to if nodemap is active,
1407  * the trust_client_ids flag is not set, and the uid is not in
1408  * the idmap tree.
1409  */
1410 int nodemap_set_squash_uid(const char *name, uid_t uid)
1411 {
1412         struct lu_nodemap       *nodemap = NULL;
1413         int                     rc = 0;
1414
1415         mutex_lock(&active_config_lock);
1416         nodemap = nodemap_lookup(name);
1417         mutex_unlock(&active_config_lock);
1418         if (IS_ERR(nodemap))
1419                 GOTO(out, rc = PTR_ERR(nodemap));
1420
1421         nodemap->nm_squash_uid = uid;
1422         rc = nodemap_idx_nodemap_update(nodemap);
1423
1424         nm_member_revoke_locks(nodemap);
1425         nodemap_putref(nodemap);
1426 out:
1427         return rc;
1428 }
1429 EXPORT_SYMBOL(nodemap_set_squash_uid);
1430
1431 /**
1432  * Update the squash_gid for a nodemap.
1433  *
1434  * \param       name            nodemap name
1435  * \param       gid             the new gid to squash unknown gids to
1436  * \retval      0 on success
1437  *
1438  * Update the squash_gid for a nodemap. The squash_gid is the gid
1439  * that the all client gids are mapped to if nodemap is active,
1440  * the trust_client_ids flag is not set, and the gid is not in
1441  * the idmap tree.
1442  */
1443 int nodemap_set_squash_gid(const char *name, gid_t gid)
1444 {
1445         struct lu_nodemap       *nodemap = NULL;
1446         int                     rc = 0;
1447
1448         mutex_lock(&active_config_lock);
1449         nodemap = nodemap_lookup(name);
1450         mutex_unlock(&active_config_lock);
1451         if (IS_ERR(nodemap))
1452                 GOTO(out, rc = PTR_ERR(nodemap));
1453
1454         nodemap->nm_squash_gid = gid;
1455         rc = nodemap_idx_nodemap_update(nodemap);
1456
1457         nm_member_revoke_locks(nodemap);
1458         nodemap_putref(nodemap);
1459 out:
1460         return rc;
1461 }
1462 EXPORT_SYMBOL(nodemap_set_squash_gid);
1463
1464 /**
1465  * Update the squash_projid for a nodemap.
1466  *
1467  * \param       name            nodemap name
1468  * \param       gid             the new projid to squash unknown projids to
1469  * \retval      0 on success
1470  *
1471  * Update the squash_projid for a nodemap. The squash_projid is the projid
1472  * that the all client projids are mapped to if nodemap is active,
1473  * the trust_client_ids flag is not set, and the projid is not in
1474  * the idmap tree.
1475  */
1476 int nodemap_set_squash_projid(const char *name, projid_t projid)
1477 {
1478         struct lu_nodemap       *nodemap = NULL;
1479         int                     rc = 0;
1480
1481         mutex_lock(&active_config_lock);
1482         nodemap = nodemap_lookup(name);
1483         mutex_unlock(&active_config_lock);
1484         if (IS_ERR(nodemap))
1485                 GOTO(out, rc = PTR_ERR(nodemap));
1486
1487         nodemap->nm_squash_projid = projid;
1488         rc = nodemap_idx_nodemap_update(nodemap);
1489
1490         nm_member_revoke_locks(nodemap);
1491         nodemap_putref(nodemap);
1492 out:
1493         return rc;
1494 }
1495 EXPORT_SYMBOL(nodemap_set_squash_projid);
1496
1497 /**
1498  * Check if nodemap allows setting quota.
1499  *
1500  * If nodemap is not active, always allow.
1501  * For user and group quota, allow if the nodemap allows root access.
1502  * For project quota, allow if project id is not squashed or deny_unknown
1503  * is not set.
1504  *
1505  * \param       nodemap         nodemap to check access for
1506  * \param       qc_type         quota type
1507  * \param       id              client id to map
1508  * \retval      true is setquota is allowed, false otherwise
1509  */
1510 bool nodemap_can_setquota(struct lu_nodemap *nodemap, __u32 qc_type, __u32 id)
1511 {
1512         if (!nodemap_active)
1513                 return true;
1514
1515         if (!nodemap || !nodemap->nmf_allow_root_access ||
1516             !(nodemap->nmf_rbac & NODEMAP_RBAC_QUOTA_OPS))
1517                 return false;
1518
1519         if (qc_type == PRJQUOTA) {
1520                 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
1521                                     NODEMAP_CLIENT_TO_FS, id);
1522
1523                 if (id == nodemap->nm_squash_projid &&
1524                     nodemap->nmf_deny_unknown)
1525                         return false;
1526         }
1527
1528         return true;
1529 }
1530 EXPORT_SYMBOL(nodemap_can_setquota);
1531
1532 /**
1533  * Set the nmf_enable_audit flag to true or false.
1534  * \param       name            nodemap name
1535  * \param       audit_mode      if true, allow audit
1536  * \retval      0 on success
1537  *
1538  */
1539 int nodemap_set_audit_mode(const char *name, bool enable_audit)
1540 {
1541         struct lu_nodemap       *nodemap = NULL;
1542         int                     rc = 0;
1543
1544         mutex_lock(&active_config_lock);
1545         nodemap = nodemap_lookup(name);
1546         mutex_unlock(&active_config_lock);
1547         if (IS_ERR(nodemap))
1548                 GOTO(out, rc = PTR_ERR(nodemap));
1549
1550         nodemap->nmf_enable_audit = enable_audit;
1551         rc = nodemap_idx_nodemap_update(nodemap);
1552
1553         nm_member_revoke_locks(nodemap);
1554         nodemap_putref(nodemap);
1555 out:
1556         return rc;
1557 }
1558 EXPORT_SYMBOL(nodemap_set_audit_mode);
1559
1560 /**
1561  * Set the nmf_forbid_encryption flag to true or false.
1562  * \param       name                    nodemap name
1563  * \param       forbid_encryption       if true, forbid encryption
1564  * \retval      0 on success
1565  *
1566  */
1567 int nodemap_set_forbid_encryption(const char *name, bool forbid_encryption)
1568 {
1569         struct lu_nodemap       *nodemap = NULL;
1570         int                     rc = 0;
1571
1572         mutex_lock(&active_config_lock);
1573         nodemap = nodemap_lookup(name);
1574         mutex_unlock(&active_config_lock);
1575         if (IS_ERR(nodemap))
1576                 GOTO(out, rc = PTR_ERR(nodemap));
1577
1578         nodemap->nmf_forbid_encryption = forbid_encryption;
1579         rc = nodemap_idx_nodemap_update(nodemap);
1580
1581         nm_member_revoke_locks(nodemap);
1582         nodemap_putref(nodemap);
1583 out:
1584         return rc;
1585 }
1586 EXPORT_SYMBOL(nodemap_set_forbid_encryption);
1587
1588 /**
1589  * Set the nmf_readonly_mount flag to true or false.
1590  * \param       name                    nodemap name
1591  * \param       readonly_mount          if true, forbid rw mount
1592  * \retval      0 on success
1593  *
1594  */
1595 int nodemap_set_readonly_mount(const char *name, bool readonly_mount)
1596 {
1597         struct lu_nodemap       *nodemap = NULL;
1598         int                     rc = 0;
1599
1600         mutex_lock(&active_config_lock);
1601         nodemap = nodemap_lookup(name);
1602         mutex_unlock(&active_config_lock);
1603         if (IS_ERR(nodemap))
1604                 GOTO(out, rc = PTR_ERR(nodemap));
1605
1606         nodemap->nmf_readonly_mount = readonly_mount;
1607         rc = nodemap_idx_nodemap_update(nodemap);
1608
1609         nm_member_revoke_locks(nodemap);
1610         nodemap_putref(nodemap);
1611 out:
1612         return rc;
1613 }
1614 EXPORT_SYMBOL(nodemap_set_readonly_mount);
1615
1616 /**
1617  * Add a nodemap
1618  *
1619  * \param       name            name of nodemap
1620  * \retval      0               success
1621  * \retval      -EINVAL         invalid nodemap name
1622  * \retval      -EEXIST         nodemap already exists
1623  * \retval      -ENOMEM         cannot allocate memory for nodemap
1624  */
1625 int nodemap_add(const char *nodemap_name)
1626 {
1627         struct lu_nodemap *nodemap;
1628         int rc;
1629
1630         mutex_lock(&active_config_lock);
1631         nodemap = nodemap_create(nodemap_name, active_config, 0);
1632         if (IS_ERR(nodemap)) {
1633                 mutex_unlock(&active_config_lock);
1634                 return PTR_ERR(nodemap);
1635         }
1636
1637         rc = nodemap_idx_nodemap_add(nodemap);
1638         if (rc == 0)
1639                 rc = lprocfs_nodemap_register(nodemap, 0);
1640
1641         mutex_unlock(&active_config_lock);
1642         nodemap_putref(nodemap);
1643
1644         return rc;
1645 }
1646 EXPORT_SYMBOL(nodemap_add);
1647
1648 /**
1649  * Delete a nodemap
1650  *
1651  * \param       name            name of nodemmap
1652  * \retval      0               success
1653  * \retval      -EINVAL         invalid input
1654  * \retval      -ENOENT         no existing nodemap
1655  */
1656 int nodemap_del(const char *nodemap_name)
1657 {
1658         struct lu_nodemap       *nodemap;
1659         struct lu_nid_range     *range;
1660         struct lu_nid_range     *range_temp;
1661         int                      rc = 0;
1662         int                      rc2 = 0;
1663
1664         if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1665                 RETURN(-EINVAL);
1666
1667         mutex_lock(&active_config_lock);
1668         nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1669                                    nodemap_name);
1670         if (nodemap == NULL) {
1671                 mutex_unlock(&active_config_lock);
1672                 GOTO(out, rc = -ENOENT);
1673         }
1674
1675         /* erase nodemap from active ranges to prevent client assignment */
1676         down_write(&active_config->nmc_range_tree_lock);
1677         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1678                                  rn_list) {
1679                 rc2 = nodemap_idx_range_del(range);
1680                 if (rc2 < 0)
1681                         rc = rc2;
1682
1683                 range_delete(active_config, range);
1684         }
1685         up_write(&active_config->nmc_range_tree_lock);
1686
1687         rc2 = nodemap_idx_nodemap_del(nodemap);
1688         if (rc2 < 0)
1689                 rc = rc2;
1690
1691         /*
1692          * remove procfs here in case nodemap_create called with same name
1693          * before nodemap_destroy is run.
1694          */
1695         lprocfs_nodemap_remove(nodemap->nm_pde_data);
1696         nodemap->nm_pde_data = NULL;
1697
1698         /* reclassify all member exports from nodemap, so they put their refs */
1699         down_read(&active_config->nmc_range_tree_lock);
1700         nm_member_reclassify_nodemap(nodemap);
1701         up_read(&active_config->nmc_range_tree_lock);
1702
1703         if (!list_empty(&nodemap->nm_member_list))
1704                 CWARN("nodemap_del failed to reclassify all members\n");
1705
1706         mutex_unlock(&active_config_lock);
1707
1708         nodemap_putref(nodemap);
1709
1710 out:
1711         return rc;
1712 }
1713 EXPORT_SYMBOL(nodemap_del);
1714
1715 /**
1716  * activate nodemap functions
1717  *
1718  * \param       value           1 for on, 0 for off
1719  */
1720 void nodemap_activate(const bool value)
1721 {
1722         mutex_lock(&active_config_lock);
1723         active_config->nmc_nodemap_is_active = value;
1724
1725         /* copy active value to global to avoid locking in map functions */
1726         nodemap_active = value;
1727         nodemap_idx_nodemap_activate(value);
1728         mutex_unlock(&active_config_lock);
1729         nm_member_revoke_all();
1730 }
1731 EXPORT_SYMBOL(nodemap_activate);
1732
1733 /**
1734  * Helper iterator to convert nodemap hash to list.
1735  *
1736  * \param       hs                      hash structure
1737  * \param       bd                      bucket descriptor
1738  * \param       hnode                   hash node
1739  * \param       nodemap_list_head       list head for list of nodemaps in hash
1740  */
1741 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1742                                    struct hlist_node *hnode,
1743                                    void *nodemap_list_head)
1744 {
1745         struct lu_nodemap *nodemap;
1746
1747         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1748         list_add(&nodemap->nm_list, nodemap_list_head);
1749
1750         cfs_hash_bd_del_locked(hs, bd, hnode);
1751
1752         return 0;
1753 }
1754
1755 struct nodemap_config *nodemap_config_alloc(void)
1756 {
1757         struct nodemap_config *config;
1758         int rc = 0;
1759
1760         OBD_ALLOC_PTR(config);
1761         if (config == NULL)
1762                 return ERR_PTR(-ENOMEM);
1763
1764         rc = nodemap_init_hash(config);
1765         if (rc != 0) {
1766                 OBD_FREE_PTR(config);
1767                 return ERR_PTR(rc);
1768         }
1769
1770         init_rwsem(&config->nmc_range_tree_lock);
1771
1772         INIT_LIST_HEAD(&config->nmc_netmask_setup);
1773         config->nmc_range_tree.nmrt_range_interval_root = INTERVAL_TREE_ROOT;
1774
1775         return config;
1776 }
1777 EXPORT_SYMBOL(nodemap_config_alloc);
1778
1779 /**
1780  * Walk the nodemap_hash and remove all nodemaps.
1781  */
1782 void nodemap_config_dealloc(struct nodemap_config *config)
1783 {
1784         struct lu_nodemap       *nodemap = NULL;
1785         struct lu_nodemap       *nodemap_temp;
1786         struct lu_nid_range     *range;
1787         struct lu_nid_range     *range_temp;
1788         LIST_HEAD(nodemap_list_head);
1789
1790         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1791                                nodemap_cleanup_iter_cb, &nodemap_list_head);
1792         cfs_hash_putref(config->nmc_nodemap_hash);
1793
1794         /* Because nodemap_destroy might sleep, we can't destroy them
1795          * in cfs_hash_for_each, so we build a list there and destroy here
1796          */
1797         list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1798                                  nm_list) {
1799                 mutex_lock(&active_config_lock);
1800                 down_write(&config->nmc_range_tree_lock);
1801
1802                 /* move members to new config, requires ac lock */
1803                 nm_member_reclassify_nodemap(nodemap);
1804                 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1805                                          rn_list)
1806                         range_delete(config, range);
1807                 up_write(&config->nmc_range_tree_lock);
1808                 mutex_unlock(&active_config_lock);
1809
1810                 /* putref must be outside of ac lock if nm could be destroyed */
1811                 nodemap_putref(nodemap);
1812         }
1813         OBD_FREE_PTR(config);
1814 }
1815 EXPORT_SYMBOL(nodemap_config_dealloc);
1816
1817 /*
1818  * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
1819  * nodemap list, generally for locking purposes as a hash cb can't sleep.
1820  */
1821 int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1822                     struct hlist_node *hnode,
1823                     void *nodemap_list_head)
1824 {
1825         struct lu_nodemap *nodemap;
1826
1827         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1828         list_add(&nodemap->nm_list, nodemap_list_head);
1829         return 0;
1830 }
1831
1832 void nodemap_config_set_active(struct nodemap_config *config)
1833 {
1834         struct nodemap_config   *old_config = active_config;
1835         struct lu_nodemap       *nodemap;
1836         struct lu_nodemap       *tmp;
1837         bool revoke_locks;
1838         LIST_HEAD(nodemap_list_head);
1839
1840         ENTRY;
1841
1842         LASSERT(active_config != config);
1843         LASSERT(config->nmc_default_nodemap);
1844
1845         mutex_lock(&active_config_lock);
1846
1847         /* move proc entries from already existing nms, create for new nms */
1848         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1849                                nm_hash_list_cb, &nodemap_list_head);
1850         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1851                 struct lu_nodemap *old_nm = NULL;
1852
1853                 if (active_config != NULL)
1854                         old_nm = cfs_hash_lookup(
1855                                         active_config->nmc_nodemap_hash,
1856                                         nodemap->nm_name);
1857                 if (old_nm != NULL) {
1858                         nodemap->nm_pde_data = old_nm->nm_pde_data;
1859                         old_nm->nm_pde_data = NULL;
1860                         nodemap_putref(old_nm);
1861                 } else {
1862                         bool is_def = (nodemap == config->nmc_default_nodemap);
1863
1864                         lprocfs_nodemap_register(nodemap, is_def);
1865                 }
1866         }
1867
1868         /*
1869          * We only need to revoke locks if old nodemap was active, and new
1870          * config is now nodemap inactive. nodemap_config_dealloc will
1871          * reclassify exports, triggering a lock revoke if and only if new
1872          * nodemap is active.
1873          */
1874         revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
1875
1876         /* if new config is inactive, deactivate live config before switching */
1877         if (!config->nmc_nodemap_is_active)
1878                 nodemap_active = false;
1879         active_config = config;
1880         if (config->nmc_nodemap_is_active)
1881                 nodemap_active = true;
1882
1883         mutex_unlock(&active_config_lock);
1884
1885         if (old_config != NULL)
1886                 nodemap_config_dealloc(old_config);
1887
1888         if (revoke_locks)
1889                 nm_member_revoke_all();
1890
1891         EXIT;
1892 }
1893
1894 /**
1895  * Cleanup nodemap module on exit
1896  */
1897 void nodemap_mod_exit(void)
1898 {
1899         nodemap_config_dealloc(active_config);
1900         nodemap_procfs_exit();
1901 }
1902
1903 /**
1904  * Initialize the nodemap module
1905  */
1906 int nodemap_mod_init(void)
1907 {
1908         struct nodemap_config *new_config;
1909         struct lu_nodemap *nodemap;
1910         int rc = 0;
1911
1912         rc = nodemap_procfs_init();
1913         if (rc != 0)
1914                 return rc;
1915
1916         new_config = nodemap_config_alloc();
1917         if (IS_ERR(new_config)) {
1918                 nodemap_procfs_exit();
1919                 GOTO(out, rc = PTR_ERR(new_config));
1920         }
1921
1922         nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1923         if (IS_ERR(nodemap)) {
1924                 nodemap_config_dealloc(new_config);
1925                 nodemap_procfs_exit();
1926                 GOTO(out, rc = PTR_ERR(nodemap));
1927         }
1928
1929         nodemap_config_set_active(new_config);
1930         nodemap_putref(nodemap);
1931
1932 out:
1933         return rc;
1934 }
1935
1936 /**
1937  * Revoke locks for all nodemaps.
1938  */
1939 void nm_member_revoke_all(void)
1940 {
1941         struct lu_nodemap *nodemap;
1942         struct lu_nodemap *tmp;
1943         LIST_HEAD(nodemap_list_head);
1944
1945         mutex_lock(&active_config_lock);
1946         cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1947                                nm_hash_list_cb, &nodemap_list_head);
1948
1949         /* revoke_locks sleeps, so can't call in cfs hash cb */
1950         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1951                 nm_member_revoke_locks_always(nodemap);
1952         mutex_unlock(&active_config_lock);
1953 }
1954
1955 /**
1956  * Returns the nodemap classification for a given nid into an ioctl buffer.
1957  * Useful for testing the nodemap configuration to make sure it is working as
1958  * expected.
1959  *
1960  * \param       nid             nid to classify
1961  * \param[out]  name_buf        buffer to write the nodemap name to
1962  * \param       name_len        length of buffer
1963  */
1964 void nodemap_test_nid(struct lnet_nid *nid, char *name_buf, size_t name_len)
1965 {
1966         struct lu_nodemap *nodemap;
1967
1968         mutex_lock(&active_config_lock);
1969         down_read(&active_config->nmc_range_tree_lock);
1970         nodemap = nodemap_classify_nid(nid);
1971         up_read(&active_config->nmc_range_tree_lock);
1972         mutex_unlock(&active_config_lock);
1973
1974         if (IS_ERR(nodemap))
1975                 return;
1976
1977         strncpy(name_buf, nodemap->nm_name, name_len);
1978         if (name_len > 0)
1979                 name_buf[name_len - 1] = '\0';
1980
1981         nodemap_putref(nodemap);
1982 }
1983 EXPORT_SYMBOL(nodemap_test_nid);
1984
1985 /**
1986  * Passes back the id mapping for a given nid/id pair. Useful for testing the
1987  * nodemap configuration to make sure it is working as expected.
1988  *
1989  * \param       nid             nid to classify
1990  * \param       idtype          uid or gid
1991  * \param       client_id       id to map to fs
1992  * \param       fs_id_buf       pointer to save mapped fs_id to
1993  *
1994  * \retval      0       success
1995  * \retval      -EINVAL invalid NID
1996  */
1997 int nodemap_test_id(struct lnet_nid *nid, enum nodemap_id_type idtype,
1998                     u32 client_id, u32 *fs_id)
1999 {
2000         struct lu_nodemap *nodemap;
2001
2002         mutex_lock(&active_config_lock);
2003         down_read(&active_config->nmc_range_tree_lock);
2004         nodemap = nodemap_classify_nid(nid);
2005         up_read(&active_config->nmc_range_tree_lock);
2006         mutex_unlock(&active_config_lock);
2007
2008         if (IS_ERR(nodemap))
2009                 return PTR_ERR(nodemap);
2010
2011         *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
2012                                client_id);
2013         nodemap_putref(nodemap);
2014
2015         return 0;
2016 }
2017 EXPORT_SYMBOL(nodemap_test_id);