Whamcloud - gitweb
LU-5092 nodemap: save id maps to targets in new index file
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (C) 2013, Trustees of Indiana University
24  *
25  * Copyright (c) 2014, Intel Corporation.
26  *
27  * Author: Joshua Walgenbach <jjw@iu.edu>
28  */
29 #include <linux/module.h>
30 #include <linux/sort.h>
31 #include <lnet/nidstr.h>
32 #include <lustre_net.h>
33 #include <lustre_acl.h>
34 #include <lustre_eacl.h>
35 #include <obd_class.h>
36 #include "nodemap_internal.h"
37
38 #define HASH_NODEMAP_BKT_BITS 3
39 #define HASH_NODEMAP_CUR_BITS 3
40 #define HASH_NODEMAP_MAX_BITS 7
41
42 #define DEFAULT_NODEMAP "default"
43
44 /* nodemap proc root proc directory under fs/lustre */
45 struct proc_dir_entry *proc_lustre_nodemap_root;
46
47 /* Copy of config active flag to avoid locking in mapping functions */
48 bool nodemap_active;
49
50 /* Lock protecting the active config, useful primarily when proc and
51  * nodemap_hash might be replaced when loading a new config
52  * Any time the active config is referenced, the lock should be held.
53  */
54 DEFINE_MUTEX(active_config_lock);
55 struct nodemap_config *active_config;
56
57 /**
58  * Nodemap destructor
59  *
60  * \param       nodemap         nodemap to destroy
61  */
62 static void nodemap_destroy(struct lu_nodemap *nodemap)
63 {
64         ENTRY;
65
66         if (nodemap->nm_pde_data != NULL)
67                 lprocfs_nodemap_remove(nodemap->nm_pde_data);
68
69         mutex_lock(&active_config_lock);
70         down_read(&active_config->nmc_range_tree_lock);
71         nm_member_reclassify_nodemap(nodemap);
72         up_read(&active_config->nmc_range_tree_lock);
73         mutex_unlock(&active_config_lock);
74
75         if (!list_empty(&nodemap->nm_member_list))
76                 CWARN("nodemap_destroy failed to reclassify all members\n");
77
78         write_lock(&nodemap->nm_idmap_lock);
79         idmap_delete_tree(nodemap);
80         write_unlock(&nodemap->nm_idmap_lock);
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 static void nodemap_getref(struct lu_nodemap *nodemap)
93 {
94         atomic_inc(&nodemap->nm_refcount);
95 }
96
97 /**
98  * Destroy nodemap if last reference is put. Should be called outside
99  * active_config_lock
100  */
101 void nodemap_putref(struct lu_nodemap *nodemap)
102 {
103         LASSERT(nodemap != NULL);
104         LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
105
106         if (atomic_dec_and_test(&nodemap->nm_refcount))
107                 nodemap_destroy(nodemap);
108 }
109
110 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
111                             const void *key, unsigned mask)
112 {
113         return cfs_hash_djb2_hash(key, strlen(key), mask);
114 }
115
116 static void *nodemap_hs_key(struct hlist_node *hnode)
117 {
118         struct lu_nodemap *nodemap;
119
120         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
121
122         return nodemap->nm_name;
123 }
124
125 static int nodemap_hs_keycmp(const void *key,
126                              struct hlist_node *compared_hnode)
127 {
128         char *nodemap_name;
129
130         nodemap_name = nodemap_hs_key(compared_hnode);
131
132         return !strcmp(key, nodemap_name);
133 }
134
135 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
136 {
137         return hlist_entry(hnode, struct lu_nodemap, nm_hash);
138 }
139
140 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
141 {
142         struct lu_nodemap *nodemap;
143
144         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
145         nodemap_getref(nodemap);
146 }
147
148 static void nodemap_hs_put_locked(struct cfs_hash *hs,
149                                   struct hlist_node *hnode)
150 {
151         struct lu_nodemap *nodemap;
152
153         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
154         nodemap_putref(nodemap);
155 }
156
157 static struct cfs_hash_ops nodemap_hash_operations = {
158         .hs_hash        = nodemap_hashfn,
159         .hs_key         = nodemap_hs_key,
160         .hs_keycmp      = nodemap_hs_keycmp,
161         .hs_object      = nodemap_hs_hashobject,
162         .hs_get         = nodemap_hs_get,
163         .hs_put_locked  = nodemap_hs_put_locked,
164 };
165
166 /* end of cfs_hash functions */
167
168 /**
169  * Initialize nodemap_hash
170  *
171  * \retval      0               success
172  * \retval      -ENOMEM         cannot create hash
173  */
174 static int nodemap_init_hash(struct nodemap_config *nmc)
175 {
176         nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
177                                                 HASH_NODEMAP_CUR_BITS,
178                                                 HASH_NODEMAP_MAX_BITS,
179                                                 HASH_NODEMAP_BKT_BITS, 0,
180                                                 CFS_HASH_MIN_THETA,
181                                                 CFS_HASH_MAX_THETA,
182                                                 &nodemap_hash_operations,
183                                                 CFS_HASH_DEFAULT);
184
185         if (nmc->nmc_nodemap_hash == NULL) {
186                 CERROR("cannot create nodemap_hash table\n");
187                 return -ENOMEM;
188         }
189
190         return 0;
191 }
192
193 /**
194  * Check for valid nodemap name
195  *
196  * \param       name            nodemap name
197  * \retval      true            valid
198  * \retval      false           invalid
199  */
200 static bool nodemap_name_is_valid(const char *name)
201 {
202         if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
203             strlen(name) == 0)
204                 return false;
205
206         for (; *name != '\0'; name++) {
207                 if (!isalnum(*name) && *name != '_')
208                         return false;
209         }
210
211         return true;
212 }
213
214 /**
215  * Nodemap lookup
216  *
217  * Look nodemap up in the active_config nodemap hash. Caller should hold the
218  * active_config_lock.
219  *
220  * \param       name            name of nodemap
221  * \retval      nodemap         pointer set to found nodemap
222  * \retval      -EINVAL         name is not valid
223  * \retval      -ENOENT         nodemap not found
224  */
225 struct lu_nodemap *nodemap_lookup(const char *name)
226 {
227         struct lu_nodemap *nodemap = NULL;
228
229         if (!nodemap_name_is_valid(name))
230                 return ERR_PTR(-EINVAL);
231
232         nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
233         if (nodemap == NULL)
234                 return ERR_PTR(-ENOENT);
235
236         return nodemap;
237 }
238
239 /**
240  * Classify the nid into the proper nodemap. Caller must hold active config and
241  * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
242  *
243  * \param       nid                     nid to classify
244  * \retval      nodemap                 nodemap containing the nid
245  * \retval      default_nodemap         default nodemap
246  */
247 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
248 {
249         struct lu_nid_range     *range;
250         struct lu_nodemap       *nodemap;
251
252         range = range_search(&active_config->nmc_range_tree, nid);
253         if (range != NULL)
254                 nodemap = range->rn_nodemap;
255         else
256                 nodemap = active_config->nmc_default_nodemap;
257
258         nodemap_getref(nodemap);
259
260         return nodemap;
261 }
262
263 /**
264  * simple check for default nodemap
265  */
266 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
267 {
268         return nodemap->nm_id == 0;
269 }
270
271 /**
272  * parse a nodemap range string into two nids
273  *
274  * \param       range_str               string to parse
275  * \param       range[2]                array of two nids
276  * \reyval      0 on success
277  */
278 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
279 {
280         char    buf[LNET_NIDSTR_SIZE * 2 + 2];
281         char    *ptr = NULL;
282         char    *start_nidstr;
283         char    *end_nidstr;
284         int     rc = 0;
285
286         snprintf(buf, sizeof(buf), "%s", range_str);
287         ptr = buf;
288         start_nidstr = strsep(&ptr, ":");
289         end_nidstr = strsep(&ptr, ":");
290
291         if (start_nidstr == NULL || end_nidstr == NULL)
292                 GOTO(out, rc = -EINVAL);
293
294         range[0] = libcfs_str2nid(start_nidstr);
295         range[1] = libcfs_str2nid(end_nidstr);
296
297 out:
298         return rc;
299
300 }
301 EXPORT_SYMBOL(nodemap_parse_range);
302
303 /**
304  * parse a string containing an id map of form "client_id:filesystem_id"
305  * into an array of __u32 * for use in mapping functions
306  *
307  * \param       idmap_str               map string
308  * \param       idmap                   array[2] of __u32
309  *
310  * \retval      0 on success
311  * \retval      -EINVAL if idmap cannot be parsed
312  */
313 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
314 {
315         char                    *sep;
316         long unsigned int        idmap_buf;
317         int                      rc;
318
319         if (idmap_str == NULL)
320                 return -EINVAL;
321
322         sep = strchr(idmap_str, ':');
323         if (sep == NULL)
324                 return -EINVAL;
325         *sep = '\0';
326         sep++;
327
328         rc = kstrtoul(idmap_str, 10, &idmap_buf);
329         if (rc != 0)
330                 return -EINVAL;
331         idmap[0] = idmap_buf;
332
333         rc = kstrtoul(sep, 10, &idmap_buf);
334         if (rc != 0)
335                 return -EINVAL;
336         idmap[1] = idmap_buf;
337
338         return 0;
339 }
340 EXPORT_SYMBOL(nodemap_parse_idmap);
341
342 /**
343  * add a member to a nodemap
344  *
345  * \param       nid             nid to add to the members
346  * \param       exp             obd_export structure for the connection
347  *                              that is being added
348  * \retval      -EINVAL         export is NULL
349  * \retval      -EEXIST         export is already member of a nodemap
350  */
351 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
352 {
353         struct lu_nodemap       *nodemap;
354         int rc;
355
356         mutex_lock(&active_config_lock);
357         down_read(&active_config->nmc_range_tree_lock);
358
359         nodemap = nodemap_classify_nid(nid);
360         rc = nm_member_add(nodemap, exp);
361
362         up_read(&active_config->nmc_range_tree_lock);
363         mutex_unlock(&active_config_lock);
364
365         nodemap_putref(nodemap);
366
367         return rc;
368 }
369 EXPORT_SYMBOL(nodemap_add_member);
370
371 /**
372  * delete a member from a nodemap
373  *
374  * \param       exp             export to remove from a nodemap
375  */
376 void nodemap_del_member(struct obd_export *exp)
377 {
378         struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
379
380         if (nodemap != NULL)
381                 nm_member_del(nodemap, exp);
382 }
383 EXPORT_SYMBOL(nodemap_del_member);
384
385 /**
386  * add an idmap to the proper nodemap trees
387  *
388  * \param       name            name of nodemap
389  * \param       id_type         NODEMAP_UID or NODEMAP_GID
390  * \param       map             array[2] __u32 containing the map values
391  *                              map[0] is client id
392  *                              map[1] is the filesystem id
393  *
394  * \retval      0 on success
395  */
396 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
397                              enum nodemap_id_type id_type,
398                              const __u32 map[2])
399 {
400         struct lu_idmap         *idmap;
401         int                     rc = 0;
402
403         idmap = idmap_create(map[0], map[1]);
404         if (idmap == NULL)
405                 GOTO(out, rc = -ENOMEM);
406
407         write_lock(&nodemap->nm_idmap_lock);
408         idmap_insert(id_type, idmap, nodemap);
409         write_unlock(&nodemap->nm_idmap_lock);
410         nm_member_revoke_locks(nodemap);
411
412 out:
413         return rc;
414 }
415
416 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
417                       const __u32 map[2])
418 {
419         struct lu_nodemap       *nodemap = NULL;
420         int                      rc;
421
422         mutex_lock(&active_config_lock);
423         nodemap = nodemap_lookup(name);
424         if (IS_ERR(nodemap)) {
425                 mutex_unlock(&active_config_lock);
426                 GOTO(out, rc = PTR_ERR(nodemap));
427         }
428
429         if (is_default_nodemap(nodemap)) {
430                 rc = -EINVAL;
431         } else {
432                 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
433                 if (rc == 0)
434                         rc = nodemap_idx_idmap_add(nodemap, id_type, map);
435         }
436         mutex_unlock(&active_config_lock);
437         nodemap_putref(nodemap);
438
439 out:
440         return rc;
441 }
442 EXPORT_SYMBOL(nodemap_add_idmap);
443
444 /**
445  * delete idmap from proper nodemap tree
446  *
447  * \param       name            name of nodemap
448  * \param       id_type         NODEMAP_UID or NODEMAP_GID
449  * \param       map             array[2] __u32 containing the mapA values
450  *                              map[0] is client id
451  *                              map[1] is the filesystem id
452  *
453  * \retval      0 on success
454  */
455 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
456                       const __u32 map[2])
457 {
458         struct lu_nodemap       *nodemap = NULL;
459         struct lu_idmap         *idmap = NULL;
460         int                     rc = 0;
461
462         mutex_lock(&active_config_lock);
463         nodemap = nodemap_lookup(name);
464         if (IS_ERR(nodemap)) {
465                 mutex_unlock(&active_config_lock);
466                 GOTO(out, rc = PTR_ERR(nodemap));
467         }
468
469         if (is_default_nodemap(nodemap))
470                 GOTO(out_putref, rc = -EINVAL);
471
472         write_lock(&nodemap->nm_idmap_lock);
473         idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
474                              map[0]);
475         if (idmap == NULL) {
476                 rc = -EINVAL;
477         } else {
478                 idmap_delete(id_type, idmap, nodemap);
479                 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
480         }
481         write_unlock(&nodemap->nm_idmap_lock);
482
483 out_putref:
484         mutex_unlock(&active_config_lock);
485         if (rc == 0)
486                 nm_member_revoke_locks(nodemap);
487         nodemap_putref(nodemap);
488
489 out:
490         return rc;
491 }
492 EXPORT_SYMBOL(nodemap_del_idmap);
493
494 /**
495  * mapping function for nodemap idmaps
496  *
497  * \param       nodemap         lu_nodemap structure defining nodemap
498  * \param       node_type       NODEMAP_UID or NODEMAP_GID
499  * \param       tree_type       NODEMAP_CLIENT_TO_FS or
500  *                              NODEMAP_FS_TO_CLIENT
501  * \param       id              id to map
502  *
503  * \retval      mapped id according to the rules below.
504  *
505  * if the nodemap_active is false, just return the passed id without mapping
506  *
507  * if the id to be looked up is 0, check that root access is allowed and if it
508  * is, return 0. Otherwise, return the squash uid or gid.
509  *
510  * if the nodemap is configured to trusted the ids from the client system, just
511  * return the passwd id without mapping.
512  *
513  * if by this point, we haven't returned and the nodemap in question is the
514  * default nodemap, return the squash uid or gid.
515  *
516  * after these checks, search the proper tree for the mapping, and if found
517  * return the mapped value, otherwise return the squash uid or gid.
518  */
519 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
520                      enum nodemap_id_type id_type,
521                      enum nodemap_tree_type tree_type, __u32 id)
522 {
523         struct lu_idmap         *idmap = NULL;
524         __u32                    found_id;
525
526         if (!nodemap_active)
527                 goto out;
528
529         if (unlikely(nodemap == NULL))
530                 goto out;
531
532         if (id == 0) {
533                 if (nodemap->nmf_allow_root_access)
534                         goto out;
535                 else
536                         goto squash;
537         }
538
539         if (nodemap->nmf_trust_client_ids)
540                 goto out;
541
542         if (is_default_nodemap(nodemap))
543                 goto squash;
544
545         read_lock(&nodemap->nm_idmap_lock);
546         idmap = idmap_search(nodemap, tree_type, id_type, id);
547         if (idmap == NULL) {
548                 read_unlock(&nodemap->nm_idmap_lock);
549                 goto squash;
550         }
551
552         if (tree_type == NODEMAP_FS_TO_CLIENT)
553                 found_id = idmap->id_client;
554         else
555                 found_id = idmap->id_fs;
556         read_unlock(&nodemap->nm_idmap_lock);
557         return found_id;
558
559 squash:
560         if (id_type == NODEMAP_UID)
561                 return nodemap->nm_squash_uid;
562         else
563                 return nodemap->nm_squash_gid;
564 out:
565         return id;
566 }
567 EXPORT_SYMBOL(nodemap_map_id);
568
569 /**
570  * Map posix ACL entries according to the nodemap membership. Removes any
571  * squashed ACLs.
572  *
573  * \param       lu_nodemap      nodemap
574  * \param       buf             buffer containing xattr encoded ACLs
575  * \param       size            size of ACLs in bytes
576  * \param       tree_type       direction of mapping
577  * \retval      size            new size of ACLs in bytes
578  * \retval      -EINVAL         bad \a size param, see posix_acl_xattr_count()
579  */
580 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
581                         enum nodemap_tree_type tree_type)
582 {
583         posix_acl_xattr_header  *header = buf;
584         posix_acl_xattr_entry   *entry = &header->a_entries[0];
585         posix_acl_xattr_entry   *new_entry = entry;
586         posix_acl_xattr_entry   *end;
587         int                      count;
588
589         if (!nodemap_active)
590                 return size;
591
592         if (unlikely(nodemap == NULL))
593                 return size;
594
595         count = posix_acl_xattr_count(size);
596         if (count < 0)
597                 return -EINVAL;
598         if (count == 0)
599                 return 0;
600
601         for (end = entry + count; entry != end; entry++) {
602                 __u16 tag = le16_to_cpu(entry->e_tag);
603                 __u32 id = le32_to_cpu(entry->e_id);
604
605                 switch (tag) {
606                 case ACL_USER:
607                         id = nodemap_map_id(nodemap, NODEMAP_UID,
608                                             tree_type, id);
609                         if (id == nodemap->nm_squash_uid)
610                                 continue;
611                         entry->e_id = cpu_to_le32(id);
612                         break;
613                 case ACL_GROUP:
614                         id = nodemap_map_id(nodemap, NODEMAP_GID,
615                                             tree_type, id);
616                         if (id == nodemap->nm_squash_gid)
617                                 continue;
618                         entry->e_id = cpu_to_le32(id);
619                         break;
620                 }
621
622                 /* if we skip an ACL, copy the following ones over it */
623                 if (new_entry != entry)
624                         *new_entry = *entry;
625
626                 new_entry++;
627         }
628
629         return (void *)new_entry - (void *)header;
630 }
631 EXPORT_SYMBOL(nodemap_map_acl);
632
633 /*
634  * Add nid range to given nodemap
635  *
636  * \param       config          nodemap config to work on
637  * \param       nodemap         nodemap to add range to
638  * \param       nid             nid range to add
639  * \param       range_id        should be 0 unless loading from disk
640  * \retval      0               success
641  * \retval      -ENOMEM
642  *
643  */
644 int nodemap_add_range_helper(struct nodemap_config *config,
645                              struct lu_nodemap *nodemap,
646                              const lnet_nid_t nid[2],
647                              unsigned int range_id)
648 {
649         struct lu_nid_range     *range;
650         int rc;
651
652         down_write(&config->nmc_range_tree_lock);
653         range = range_create(&config->nmc_range_tree, nid[0], nid[1],
654                              nodemap, range_id);
655         if (range == NULL) {
656                 up_write(&config->nmc_range_tree_lock);
657                 GOTO(out, rc = -ENOMEM);
658         }
659
660         rc = range_insert(&config->nmc_range_tree, range);
661         if (rc != 0) {
662                 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
663                       nodemap->nm_name, rc);
664                 up_write(&config->nmc_range_tree_lock);
665                 list_del(&range->rn_list);
666                 range_destroy(range);
667                 GOTO(out, rc = -ENOMEM);
668         }
669
670         list_add(&range->rn_list, &nodemap->nm_ranges);
671         nm_member_reclassify_nodemap(config->nmc_default_nodemap);
672         up_write(&config->nmc_range_tree_lock);
673
674         /* if range_id is non-zero, we are loading from disk */
675         if (range_id == 0)
676                 rc = nodemap_idx_range_add(range, nid);
677
678         nm_member_revoke_locks(config->nmc_default_nodemap);
679         nm_member_revoke_locks(nodemap);
680
681 out:
682         return rc;
683 }
684 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
685 {
686         struct lu_nodemap       *nodemap = NULL;
687         int                      rc;
688
689         mutex_lock(&active_config_lock);
690         nodemap = nodemap_lookup(name);
691         if (IS_ERR(nodemap)) {
692                 mutex_unlock(&active_config_lock);
693                 GOTO(out, rc = PTR_ERR(nodemap));
694         }
695
696         if (is_default_nodemap(nodemap))
697                 rc = -EINVAL;
698         else
699                 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
700         mutex_unlock(&active_config_lock);
701         nodemap_putref(nodemap);
702 out:
703         return rc;
704 }
705 EXPORT_SYMBOL(nodemap_add_range);
706
707 /**
708  * delete a range
709  * \param       name            nodemap name
710  * \param       range_str       string containing range
711  * \retval      0 on success
712  *
713  * Delete range from global range tree, and remove it
714  * from the list in the associated nodemap.
715  */
716 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
717 {
718         struct lu_nodemap       *nodemap;
719         struct lu_nid_range     *range;
720         int                     rc = 0;
721
722         mutex_lock(&active_config_lock);
723         nodemap = nodemap_lookup(name);
724         if (IS_ERR(nodemap)) {
725                 mutex_unlock(&active_config_lock);
726                 GOTO(out, rc = PTR_ERR(nodemap));
727         }
728
729         if (is_default_nodemap(nodemap))
730                 GOTO(out_putref, rc = -EINVAL);
731
732         down_write(&active_config->nmc_range_tree_lock);
733         range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
734         if (range == NULL) {
735                 up_write(&active_config->nmc_range_tree_lock);
736                 GOTO(out_putref, rc = -EINVAL);
737         }
738         rc = nodemap_idx_range_del(range);
739         range_delete(&active_config->nmc_range_tree, range);
740         nm_member_reclassify_nodemap(nodemap);
741         up_write(&active_config->nmc_range_tree_lock);
742
743         nm_member_revoke_locks(active_config->nmc_default_nodemap);
744         nm_member_revoke_locks(nodemap);
745
746 out_putref:
747         mutex_unlock(&active_config_lock);
748         nodemap_putref(nodemap);
749 out:
750         return rc;
751 }
752 EXPORT_SYMBOL(nodemap_del_range);
753
754 /**
755  * Nodemap constructor
756  *
757  * Creates an lu_nodemap structure and assigns sane default
758  * member values. If this is the default nodemap, the defaults
759  * are the most restictive in xterms of mapping behavior. Otherwise
760  * the default flags should be inherited from the default nodemap.
761  * The adds nodemap to nodemap_hash.
762  *
763  * Requires that the caller take the active_config_lock
764  *
765  * \param       name            name of nodemap
766  * \param       is_default      true if default nodemap
767  * \retval      nodemap         success
768  * \retval      -EINVAL         invalid nodemap name
769  * \retval      -EEXIST         nodemap already exists
770  * \retval      -ENOMEM         cannot allocate memory for nodemap
771  */
772 struct lu_nodemap *nodemap_create(const char *name,
773                                   struct nodemap_config *config,
774                                   bool is_default)
775 {
776         struct lu_nodemap       *nodemap = NULL;
777         struct lu_nodemap       *default_nodemap;
778         struct cfs_hash         *hash = config->nmc_nodemap_hash;
779         int                      rc = 0;
780
781         default_nodemap = config->nmc_default_nodemap;
782
783         if (!nodemap_name_is_valid(name))
784                 GOTO(out, rc = -EINVAL);
785
786         if (hash == NULL) {
787                 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
788                 GOTO(out, rc = -EINVAL);
789         }
790
791         OBD_ALLOC_PTR(nodemap);
792         if (nodemap == NULL) {
793                 CERROR("cannot allocate memory (%zu bytes)"
794                        "for nodemap '%s'\n", sizeof(*nodemap),
795                        name);
796                 GOTO(out, rc = -ENOMEM);
797         }
798
799         /*
800          * take an extra reference to prevent nodemap from being destroyed
801          * while it's being created.
802          */
803         atomic_set(&nodemap->nm_refcount, 2);
804         snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
805         rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
806         if (rc != 0) {
807                 OBD_FREE_PTR(nodemap);
808                 GOTO(out, rc = -EEXIST);
809         }
810
811         INIT_LIST_HEAD(&nodemap->nm_ranges);
812         INIT_LIST_HEAD(&nodemap->nm_list);
813         INIT_LIST_HEAD(&nodemap->nm_member_list);
814
815         mutex_init(&nodemap->nm_member_list_lock);
816         rwlock_init(&nodemap->nm_idmap_lock);
817         nodemap->nm_fs_to_client_uidmap = RB_ROOT;
818         nodemap->nm_client_to_fs_uidmap = RB_ROOT;
819         nodemap->nm_fs_to_client_gidmap = RB_ROOT;
820         nodemap->nm_client_to_fs_gidmap = RB_ROOT;
821
822         if (is_default) {
823                 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
824                 config->nmc_default_nodemap = nodemap;
825         } else {
826                 config->nmc_nodemap_highest_id++;
827                 nodemap->nm_id = config->nmc_nodemap_highest_id;
828         }
829
830         if (is_default || default_nodemap == NULL) {
831                 nodemap->nmf_trust_client_ids = 0;
832                 nodemap->nmf_allow_root_access = 0;
833
834                 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
835                 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
836                 if (!is_default)
837                         CWARN("adding nodemap '%s' to config without"
838                               " default nodemap\n", nodemap->nm_name);
839         } else {
840                 nodemap->nmf_trust_client_ids =
841                                 default_nodemap->nmf_trust_client_ids;
842                 nodemap->nmf_allow_root_access =
843                                 default_nodemap->nmf_allow_root_access;
844
845                 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
846                 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
847         }
848
849         return nodemap;
850
851 out:
852         CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
853         return ERR_PTR(rc);
854 }
855
856 /**
857  * update flag to turn on or off nodemap functions
858  * \param       name            nodemap name
859  * \param       admin_string    string containing updated value
860  * \retval      0 on success
861  *
862  * Update admin flag to turn on or off nodemap functions.
863  */
864 int nodemap_set_allow_root(const char *name, bool allow_root)
865 {
866         struct lu_nodemap       *nodemap = NULL;
867         int                     rc = 0;
868
869         mutex_lock(&active_config_lock);
870         nodemap = nodemap_lookup(name);
871         mutex_unlock(&active_config_lock);
872         if (IS_ERR(nodemap))
873                 GOTO(out, rc = PTR_ERR(nodemap));
874
875         nodemap->nmf_allow_root_access = allow_root;
876         rc = nodemap_idx_nodemap_update(nodemap);
877
878         nm_member_revoke_locks(nodemap);
879         nodemap_putref(nodemap);
880 out:
881         return rc;
882 }
883 EXPORT_SYMBOL(nodemap_set_allow_root);
884
885 /**
886  * updated trust_client_ids flag for nodemap
887  *
888  * \param       name            nodemap name
889  * \param       trust_string    new value for trust flag
890  * \retval      0 on success
891  *
892  * Update the trust_client_ids flag for a nodemap.
893  */
894 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
895 {
896         struct lu_nodemap       *nodemap = NULL;
897         int                     rc = 0;
898
899         mutex_lock(&active_config_lock);
900         nodemap = nodemap_lookup(name);
901         mutex_unlock(&active_config_lock);
902         if (IS_ERR(nodemap))
903                 GOTO(out, rc = PTR_ERR(nodemap));
904
905         nodemap->nmf_trust_client_ids = trust_client_ids;
906         rc = nodemap_idx_nodemap_update(nodemap);
907
908         nm_member_revoke_locks(nodemap);
909         nodemap_putref(nodemap);
910 out:
911         return rc;
912 }
913 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
914
915 /**
916  * update the squash_uid for a nodemap
917  *
918  * \param       name            nodemap name
919  * \param       uid_string      string containing new squash_uid value
920  * \retval      0 on success
921  *
922  * Update the squash_uid for a nodemap. The squash_uid is the uid
923  * that the all client uids are mapped to if nodemap is active,
924  * the trust_client_ids flag is not set, and the uid is not in
925  * the idmap tree.
926  */
927 int nodemap_set_squash_uid(const char *name, uid_t uid)
928 {
929         struct lu_nodemap       *nodemap = NULL;
930         int                     rc = 0;
931
932         mutex_lock(&active_config_lock);
933         nodemap = nodemap_lookup(name);
934         mutex_unlock(&active_config_lock);
935         if (IS_ERR(nodemap))
936                 GOTO(out, rc = PTR_ERR(nodemap));
937
938         nodemap->nm_squash_uid = uid;
939         rc = nodemap_idx_nodemap_update(nodemap);
940
941         nm_member_revoke_locks(nodemap);
942         nodemap_putref(nodemap);
943 out:
944         return rc;
945 }
946 EXPORT_SYMBOL(nodemap_set_squash_uid);
947
948 /**
949  * Update the squash_gid for a nodemap.
950  *
951  * \param       name            nodemap name
952  * \param       gid_string      string containing new squash_gid value
953  * \retval      0 on success
954  *
955  * Update the squash_gid for a nodemap. The squash_uid is the gid
956  * that the all client gids are mapped to if nodemap is active,
957  * the trust_client_ids flag is not set, and the gid is not in
958  * the idmap tree.
959  */
960 int nodemap_set_squash_gid(const char *name, gid_t gid)
961 {
962         struct lu_nodemap       *nodemap = NULL;
963         int                     rc = 0;
964
965         mutex_lock(&active_config_lock);
966         nodemap = nodemap_lookup(name);
967         mutex_unlock(&active_config_lock);
968         if (IS_ERR(nodemap))
969                 GOTO(out, rc = PTR_ERR(nodemap));
970
971         nodemap->nm_squash_gid = gid;
972         rc = nodemap_idx_nodemap_update(nodemap);
973
974         nm_member_revoke_locks(nodemap);
975         nodemap_putref(nodemap);
976 out:
977         return rc;
978 }
979 EXPORT_SYMBOL(nodemap_set_squash_gid);
980
981 /**
982  * Returns true if this nodemap has root user access. Always returns true if
983  * nodemaps are not active.
984  *
985  * \param       nodemap         nodemap to check access for
986  */
987 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
988 {
989         return !nodemap_active || nodemap->nmf_allow_root_access;
990 }
991 EXPORT_SYMBOL(nodemap_can_setquota);
992
993 /**
994  * Add a nodemap
995  *
996  * \param       name            name of nodemap
997  * \retval      0               success
998  * \retval      -EINVAL         invalid nodemap name
999  * \retval      -EEXIST         nodemap already exists
1000  * \retval      -ENOMEM         cannot allocate memory for nodemap
1001  */
1002 int nodemap_add(const char *nodemap_name)
1003 {
1004         struct lu_nodemap *nodemap;
1005         int rc;
1006
1007         mutex_lock(&active_config_lock);
1008         nodemap = nodemap_create(nodemap_name, active_config, 0);
1009         if (IS_ERR(nodemap)) {
1010                 mutex_unlock(&active_config_lock);
1011                 return PTR_ERR(nodemap);
1012         }
1013
1014         rc = nodemap_idx_nodemap_add(nodemap);
1015         if (rc == 0)
1016                 rc = lprocfs_nodemap_register(nodemap, 0);
1017
1018         mutex_unlock(&active_config_lock);
1019         nodemap_putref(nodemap);
1020
1021         return rc;
1022 }
1023 EXPORT_SYMBOL(nodemap_add);
1024
1025 /**
1026  * Delete a nodemap
1027  *
1028  * \param       name            name of nodemmap
1029  * \retval      0               success
1030  * \retval      -EINVAL         invalid input
1031  * \retval      -ENOENT         no existing nodemap
1032  */
1033 int nodemap_del(const char *nodemap_name)
1034 {
1035         struct lu_nodemap       *nodemap;
1036         struct lu_nid_range     *range;
1037         struct lu_nid_range     *range_temp;
1038         int                      rc = 0;
1039         int                      rc2 = 0;
1040
1041         if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1042                 RETURN(-EINVAL);
1043
1044         mutex_lock(&active_config_lock);
1045         nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1046                                    nodemap_name);
1047         if (nodemap == NULL) {
1048                 mutex_unlock(&active_config_lock);
1049                 GOTO(out, rc = -ENOENT);
1050         }
1051
1052         /* erase nodemap from active ranges to prevent client assignment */
1053         down_write(&active_config->nmc_range_tree_lock);
1054         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1055                                  rn_list) {
1056                 rc2 = nodemap_idx_range_del(range);
1057                 if (rc2 < 0)
1058                         rc = rc2;
1059
1060                 range_delete(&active_config->nmc_range_tree, range);
1061         }
1062         up_write(&active_config->nmc_range_tree_lock);
1063
1064         rc2 = nodemap_idx_nodemap_del(nodemap);
1065         if (rc2 < 0)
1066                 rc = rc2;
1067
1068         /*
1069          * remove procfs here in case nodemap_create called with same name
1070          * before nodemap_destroy is run.
1071          */
1072         lprocfs_nodemap_remove(nodemap->nm_pde_data);
1073         nodemap->nm_pde_data = NULL;
1074         mutex_unlock(&active_config_lock);
1075
1076         nodemap_putref(nodemap);
1077
1078 out:
1079         return rc;
1080 }
1081 EXPORT_SYMBOL(nodemap_del);
1082
1083 /**
1084  * activate nodemap functions
1085  *
1086  * \param       value           1 for on, 0 for off
1087  */
1088 void nodemap_activate(const bool value)
1089 {
1090         mutex_lock(&active_config_lock);
1091         active_config->nmc_nodemap_is_active = value;
1092
1093         /* copy active value to global to avoid locking in map functions */
1094         nodemap_active = value;
1095         nodemap_idx_nodemap_activate(value);
1096         mutex_unlock(&active_config_lock);
1097         nm_member_revoke_all();
1098 }
1099 EXPORT_SYMBOL(nodemap_activate);
1100
1101 /**
1102  * Helper iterator to convert nodemap hash to list.
1103  *
1104  * \param       hs                      hash structure
1105  * \param       bd                      bucket descriptor
1106  * \param       hnode                   hash node
1107  * \param       nodemap_list_head       list head for list of nodemaps in hash
1108  */
1109 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1110                                    struct hlist_node *hnode,
1111                                    void *nodemap_list_head)
1112 {
1113         struct lu_nodemap *nodemap;
1114
1115         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1116         list_add(&nodemap->nm_list, nodemap_list_head);
1117
1118         cfs_hash_bd_del_locked(hs, bd, hnode);
1119
1120         return 0;
1121 }
1122
1123 /**
1124  * Walk the nodemap_hash and remove all nodemaps.
1125  */
1126 void nodemap_config_cleanup(struct nodemap_config *config)
1127 {
1128         struct lu_nodemap       *nodemap = NULL;
1129         struct lu_nodemap       *nodemap_temp;
1130         struct lu_nid_range     *range;
1131         struct lu_nid_range     *range_temp;
1132         LIST_HEAD(nodemap_list_head);
1133
1134         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1135                                nodemap_cleanup_iter_cb, &nodemap_list_head);
1136         cfs_hash_putref(config->nmc_nodemap_hash);
1137
1138         /* Because nodemap_destroy might sleep, we can't destroy them
1139          * in cfs_hash_for_each, so we build a list there and destroy here
1140          */
1141         list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1142                                  nm_list) {
1143                 down_write(&config->nmc_range_tree_lock);
1144                 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1145                                          rn_list)
1146                         range_delete(&config->nmc_range_tree, range);
1147                 up_write(&config->nmc_range_tree_lock);
1148
1149                 nodemap_putref(nodemap);
1150         }
1151 }
1152
1153 struct nodemap_config *nodemap_config_alloc(void)
1154 {
1155         struct nodemap_config *config;
1156         int rc = 0;
1157
1158         OBD_ALLOC_PTR(config);
1159         if (config == NULL)
1160                 return ERR_PTR(-ENOMEM);
1161
1162         rc = nodemap_init_hash(config);
1163         if (rc != 0) {
1164                 OBD_FREE_PTR(config);
1165                 return ERR_PTR(rc);
1166         }
1167
1168         init_rwsem(&config->nmc_range_tree_lock);
1169
1170         return config;
1171 }
1172
1173 void nodemap_config_dealloc(struct nodemap_config *config)
1174 {
1175         nodemap_config_cleanup(config);
1176         OBD_FREE_PTR(config);
1177 }
1178
1179 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1180                            struct hlist_node *hnode,
1181                            void *nodemap_list_head)
1182 {
1183         struct lu_nodemap *nodemap;
1184
1185         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1186         list_add(&nodemap->nm_list, nodemap_list_head);
1187         return 0;
1188 }
1189
1190 void nodemap_config_set_active(struct nodemap_config *config)
1191 {
1192         struct nodemap_config   *old_config = active_config;
1193         struct lu_nodemap       *nodemap;
1194         struct lu_nodemap       *tmp;
1195         LIST_HEAD(nodemap_list_head);
1196
1197         ENTRY;
1198
1199         LASSERT(active_config != config);
1200
1201         mutex_lock(&active_config_lock);
1202
1203         /* move proc entries from already existing nms, create for new nms */
1204         cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1205                                nm_hash_list_cb, &nodemap_list_head);
1206         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1207                 struct lu_nodemap *old_nm = NULL;
1208
1209                 if (active_config != NULL)
1210                         old_nm = cfs_hash_lookup(
1211                                         active_config->nmc_nodemap_hash,
1212                                         nodemap->nm_name);
1213                 if (old_nm != NULL) {
1214                         nodemap->nm_pde_data = old_nm->nm_pde_data;
1215                         old_nm->nm_pde_data = NULL;
1216                         nodemap_putref(old_nm);
1217                 } else {
1218                         bool is_def = (nodemap == config->nmc_default_nodemap);
1219
1220                         lprocfs_nodemap_register(nodemap, is_def);
1221                 }
1222         }
1223
1224         /* if new config is inactive, deactivate live config before switching */
1225         if (!config->nmc_nodemap_is_active)
1226                 nodemap_active = false;
1227         active_config = config;
1228         if (config->nmc_nodemap_is_active)
1229                 nodemap_active = true;
1230
1231         mutex_unlock(&active_config_lock);
1232
1233         if (old_config != NULL)
1234                 nodemap_config_dealloc(old_config);
1235
1236         nm_member_revoke_all();
1237
1238         EXIT;
1239 }
1240
1241 /**
1242  * Cleanup nodemap module on exit
1243  */
1244 void nodemap_mod_exit(void)
1245 {
1246         nodemap_config_dealloc(active_config);
1247         nodemap_procfs_exit();
1248 }
1249
1250 /**
1251  * Initialize the nodemap module
1252  */
1253 int nodemap_mod_init(void)
1254 {
1255         struct nodemap_config   *new_config;
1256         struct lu_nodemap       *nodemap;
1257         int                      rc = 0;
1258
1259         rc = nodemap_procfs_init();
1260         if (rc != 0)
1261                 return rc;
1262
1263         new_config = nodemap_config_alloc();
1264         if (IS_ERR(new_config)) {
1265                 nodemap_procfs_exit();
1266                 GOTO(out, rc = PTR_ERR(new_config));
1267         }
1268
1269         nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1270         if (IS_ERR(nodemap)) {
1271                 nodemap_config_dealloc(new_config);
1272                 nodemap_procfs_exit();
1273                 GOTO(out, rc = PTR_ERR(nodemap));
1274         }
1275
1276         nodemap_config_set_active(new_config);
1277         nodemap_putref(nodemap);
1278
1279 out:
1280         return rc;
1281 }
1282
1283 /**
1284  * Revoke locks for all nodemaps.
1285  */
1286 void nm_member_revoke_all(void)
1287 {
1288         struct lu_nodemap *nodemap;
1289         struct lu_nodemap *tmp;
1290         LIST_HEAD(nodemap_list_head);
1291
1292         mutex_lock(&active_config_lock);
1293         cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1294                                nm_hash_list_cb, &nodemap_list_head);
1295
1296         /* revoke_locks sleeps, so can't call in cfs hash cb */
1297         list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1298                 nm_member_revoke_locks(nodemap);
1299         mutex_unlock(&active_config_lock);
1300 }
1301
1302 /**
1303  * Returns the nodemap classification for a given nid into an ioctl buffer.
1304  * Useful for testing the nodemap configuration to make sure it is working as
1305  * expected.
1306  *
1307  * \param       nid             nid to classify
1308  * \param[out]  name_buf        buffer to write the nodemap name to
1309  * \param       name_len        length of buffer
1310  */
1311 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1312 {
1313         struct lu_nodemap       *nodemap;
1314
1315         mutex_lock(&active_config_lock);
1316         down_read(&active_config->nmc_range_tree_lock);
1317         nodemap = nodemap_classify_nid(nid);
1318         up_read(&active_config->nmc_range_tree_lock);
1319         mutex_unlock(&active_config_lock);
1320
1321         strncpy(name_buf, nodemap->nm_name, name_len);
1322         if (name_len > 0)
1323                 name_buf[name_len - 1] = '\0';
1324
1325         nodemap_putref(nodemap);
1326 }
1327 EXPORT_SYMBOL(nodemap_test_nid);
1328
1329 /**
1330  * Returns the id mapping for a given nid/id pair. Useful for testing the
1331  * nodemap configuration to make sure it is working as expected.
1332  *
1333  * \param       nid             nid to classify
1334  * \param       idtype          uid or gid
1335  * \param       client_id       id to map to fs
1336  *
1337  * \retval      the mapped fs_id of the given client_id
1338  */
1339 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1340                       __u32 client_id)
1341 {
1342         struct lu_nodemap       *nodemap;
1343         __u32                    fs_id;
1344
1345         mutex_lock(&active_config_lock);
1346         down_read(&active_config->nmc_range_tree_lock);
1347         nodemap = nodemap_classify_nid(nid);
1348         up_read(&active_config->nmc_range_tree_lock);
1349         mutex_unlock(&active_config_lock);
1350
1351         fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1352                                client_id);
1353         nodemap_putref(nodemap);
1354
1355         return fs_id;
1356 }
1357 EXPORT_SYMBOL(nodemap_test_id);