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