Whamcloud - gitweb
LU-6070 libcfs: provide separate buffers for libcfs_*2str()
[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 /* Highest numerical lu_nodemap.nm_id defined */
48 static atomic_t nodemap_highest_id;
49
50 /* Simple flag to determine if nodemaps are active */
51 bool nodemap_active;
52
53 /**
54  * pointer to default nodemap kept to keep from
55  * lookup it up in the hash since it is needed
56  * more often
57  */
58 static struct lu_nodemap *default_nodemap;
59
60 /**
61  * Lock required to access the range tree.
62  */
63 rwlock_t nm_range_tree_lock;
64
65 /**
66  * Hash keyed on nodemap name containing all
67  * nodemaps
68  */
69 static cfs_hash_t *nodemap_hash;
70
71 /**
72  * Nodemap destructor
73  *
74  * \param       nodemap         nodemap to destroy
75  */
76 static void nodemap_destroy(struct lu_nodemap *nodemap)
77 {
78         struct lu_nid_range *range;
79         struct lu_nid_range *range_temp;
80
81         write_lock(&nm_range_tree_lock);
82         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
83                                  rn_list) {
84                 range_delete(range);
85         }
86         write_unlock(&nm_range_tree_lock);
87
88         write_lock(&nodemap->nm_idmap_lock);
89         idmap_delete_tree(nodemap);
90         write_unlock(&nodemap->nm_idmap_lock);
91         nm_member_reclassify_nodemap(nodemap);
92         if (!cfs_hash_is_empty(nodemap->nm_member_hash))
93                 CWARN("nodemap_destroy failed to reclassify all members\n");
94
95         nm_member_delete_hash(nodemap);
96
97         OBD_FREE_PTR(nodemap);
98 }
99
100 /**
101  * Functions used for the cfs_hash
102  */
103 static void nodemap_getref(struct lu_nodemap *nodemap)
104 {
105         atomic_inc(&nodemap->nm_refcount);
106 }
107
108 void nodemap_putref(struct lu_nodemap *nodemap)
109 {
110         LASSERT(nodemap != NULL);
111         LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
112
113         if (atomic_dec_and_test(&nodemap->nm_refcount))
114                 nodemap_destroy(nodemap);
115 }
116
117 static __u32 nodemap_hashfn(cfs_hash_t *hash_body,
118                             const void *key, unsigned mask)
119 {
120         return cfs_hash_djb2_hash(key, strlen(key), mask);
121 }
122
123 static void *nodemap_hs_key(struct hlist_node *hnode)
124 {
125         struct lu_nodemap *nodemap;
126
127         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
128
129         return nodemap->nm_name;
130 }
131
132 static int nodemap_hs_keycmp(const void *key,
133                              struct hlist_node *compared_hnode)
134 {
135         char *nodemap_name;
136
137         nodemap_name = nodemap_hs_key(compared_hnode);
138
139         return !strcmp(key, nodemap_name);
140 }
141
142 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
143 {
144         return hlist_entry(hnode, struct lu_nodemap, nm_hash);
145 }
146
147 static void nodemap_hs_get(cfs_hash_t *hs, struct hlist_node *hnode)
148 {
149         struct lu_nodemap *nodemap;
150
151         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
152         nodemap_getref(nodemap);
153 }
154
155 static void nodemap_hs_put_locked(cfs_hash_t *hs,
156                                   struct hlist_node *hnode)
157 {
158         struct lu_nodemap *nodemap;
159
160         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
161         nodemap_putref(nodemap);
162 }
163
164 static cfs_hash_ops_t nodemap_hash_operations = {
165         .hs_hash        = nodemap_hashfn,
166         .hs_key         = nodemap_hs_key,
167         .hs_keycmp      = nodemap_hs_keycmp,
168         .hs_object      = nodemap_hs_hashobject,
169         .hs_get         = nodemap_hs_get,
170         .hs_put_locked  = nodemap_hs_put_locked,
171 };
172
173 /* end of cfs_hash functions */
174
175 /**
176  * Helper iterator to clean up nodemap on module exit.
177  *
178  * \param       hs              hash structure
179  * \param       bd              bucket descriptor
180  * \param       hnode           hash node
181  * \param       data            not used here
182  */
183 static int nodemap_cleanup_iter_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
184                                    struct hlist_node *hnode, void *data)
185 {
186         struct lu_nodemap *nodemap;
187
188         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
189         nodemap_putref(nodemap);
190
191         return 0;
192 }
193
194 /**
195  * Walk the nodemap_hash and remove all nodemaps.
196  */
197 void nodemap_cleanup_all(void)
198 {
199         cfs_hash_for_each_safe(nodemap_hash, nodemap_cleanup_iter_cb, NULL);
200         cfs_hash_putref(nodemap_hash);
201 }
202
203 /**
204  * Initialize nodemap_hash
205  *
206  * \retval      0               success
207  * \retval      -ENOMEM         cannot create hash
208  */
209 static int nodemap_init_hash(void)
210 {
211         nodemap_hash = cfs_hash_create("NODEMAP", HASH_NODEMAP_CUR_BITS,
212                                        HASH_NODEMAP_MAX_BITS,
213                                        HASH_NODEMAP_BKT_BITS, 0,
214                                        CFS_HASH_MIN_THETA,
215                                        CFS_HASH_MAX_THETA,
216                                        &nodemap_hash_operations,
217                                        CFS_HASH_DEFAULT);
218
219         if (nodemap_hash == NULL) {
220                 CERROR("cannot create nodemap_hash table\n");
221                 return -ENOMEM;
222         }
223
224         return 0;
225 }
226
227 /**
228  * Check for valid nodemap name
229  *
230  * \param       name            nodemap name
231  * \retval      true            valid
232  * \retval      false           invalid
233  */
234 static bool nodemap_name_is_valid(const char *name)
235 {
236         if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
237             strlen(name) == 0)
238                 return false;
239
240         for (; *name != '\0'; name++) {
241                 if (!isalnum(*name) && *name != '_')
242                         return false;
243         }
244
245         return true;
246 }
247
248 /**
249  * Nodemap lookup
250  *
251  * Look nodemap up in the nodemap hash
252  *
253  * \param       name            name of nodemap
254  * \param       nodemap         found nodemap or NULL
255  * \retval      lu_nodemap      named nodemap
256  * \retval      NULL            nodemap doesn't exist
257  */
258 static int nodemap_lookup(const char *name, struct lu_nodemap **nodemap)
259 {
260         int rc = 0;
261
262         *nodemap = NULL;
263
264         if (!nodemap_name_is_valid(name))
265                 GOTO(out, rc = -EINVAL);
266
267         *nodemap = cfs_hash_lookup(nodemap_hash, name);
268         if (*nodemap == NULL)
269                 rc = -ENOENT;
270
271 out:
272         return rc;
273 }
274
275 /**
276  * classify the nid into the proper nodemap
277  *
278  * \param       nid                     nid to classify
279  * \retval      nodemap                 nodemap containing the nid
280  * \retval      default_nodemap         default nodemap
281  */
282 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
283 {
284         struct lu_nid_range     *range;
285
286         range = range_search(nid);
287         if (range != NULL)
288                 return range->rn_nodemap;
289
290         return default_nodemap;
291 }
292 EXPORT_SYMBOL(nodemap_classify_nid);
293
294 /**
295  * simple check for default nodemap
296  */
297 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
298 {
299         return nodemap->nm_id == 0;
300 }
301
302 /**
303  * parse a nodemap range string into two nids
304  *
305  * \param       range_str               string to parse
306  * \param       range[2]                array of two nids
307  * \reyval      0 on success
308  */
309 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
310 {
311         char    buf[LNET_NIDSTR_SIZE * 2 + 2];
312         char    *ptr = NULL;
313         char    *start_nidstr;
314         char    *end_nidstr;
315         int     rc = 0;
316
317         snprintf(buf, sizeof(buf), "%s", range_str);
318         ptr = buf;
319         start_nidstr = strsep(&ptr, ":");
320         end_nidstr = strsep(&ptr, ":");
321
322         if (start_nidstr == NULL || end_nidstr == NULL)
323                 GOTO(out, rc = -EINVAL);
324
325         range[0] = libcfs_str2nid(start_nidstr);
326         range[1] = libcfs_str2nid(end_nidstr);
327
328 out:
329         return rc;
330
331 }
332 EXPORT_SYMBOL(nodemap_parse_range);
333
334 /**
335  * parse a string containing an id map of form "client_id:filesystem_id"
336  * into an array of __u32 * for use in mapping functions
337  *
338  * \param       idmap_str               map string
339  * \param       idmap                   array[2] of __u32
340  *
341  * \retval      0 on success
342  * \retval      -EINVAL if idmap cannot be parsed
343  */
344 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
345 {
346         char                    *sep;
347         long unsigned int        idmap_buf;
348         int                      rc;
349
350         if (idmap_str == NULL)
351                 return -EINVAL;
352
353         sep = strchr(idmap_str, ':');
354         if (sep == NULL)
355                 return -EINVAL;
356         *sep = '\0';
357         sep++;
358
359         rc = kstrtoul(idmap_str, 10, &idmap_buf);
360         if (rc != 0)
361                 return -EINVAL;
362         idmap[0] = idmap_buf;
363
364         rc = kstrtoul(sep, 10, &idmap_buf);
365         if (rc != 0)
366                 return -EINVAL;
367         idmap[1] = idmap_buf;
368
369         return 0;
370 }
371 EXPORT_SYMBOL(nodemap_parse_idmap);
372
373 /**
374  * add a member to a nodemap
375  *
376  * \param       nid             nid to add to the members
377  * \param       exp             obd_export structure for the connection
378  *                              that is being added
379  * \retval      -EINVAL         export is NULL
380  * \retval      -EEXIST         export is already member of a nodemap
381  */
382 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
383 {
384         struct lu_nodemap       *nodemap;
385         int rc;
386
387         read_lock(&nm_range_tree_lock);
388         nodemap = nodemap_classify_nid(nid);
389         rc = nm_member_add(nodemap, exp);
390         read_unlock(&nm_range_tree_lock);
391         return rc;
392 }
393 EXPORT_SYMBOL(nodemap_add_member);
394
395 /**
396  * delete a member from a nodemap
397  *
398  * \param       exp             export to remove from a nodemap
399  */
400 void nodemap_del_member(struct obd_export *exp)
401 {
402         struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
403
404         if (nodemap != NULL)
405                 nm_member_del(nodemap, exp);
406 }
407 EXPORT_SYMBOL(nodemap_del_member);
408
409 /**
410  * add an idmap to the proper nodemap trees
411  *
412  * \param       name            name of nodemap
413  * \param       id_type         NODEMAP_UID or NODEMAP_GID
414  * \param       map             array[2] __u32 containing the map values
415  *                              map[0] is client id
416  *                              map[1] is the filesystem id
417  *
418  * \retval      0 on success
419  */
420 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
421                       const __u32 map[2])
422 {
423         struct lu_nodemap       *nodemap = NULL;
424         struct lu_idmap         *idmap;
425         int                     rc = 0;
426
427         rc = nodemap_lookup(name, &nodemap);
428         if (nodemap == NULL || is_default_nodemap(nodemap))
429                 GOTO(out, rc = -EINVAL);
430
431         idmap = idmap_create(map[0], map[1]);
432         if (idmap == NULL)
433                 GOTO(out_putref, rc = -ENOMEM);
434
435         write_lock(&nodemap->nm_idmap_lock);
436         idmap_insert(id_type, idmap, nodemap);
437         write_unlock(&nodemap->nm_idmap_lock);
438         nm_member_revoke_locks(nodemap);
439
440 out_putref:
441         nodemap_putref(nodemap);
442 out:
443         return rc;
444 }
445 EXPORT_SYMBOL(nodemap_add_idmap);
446
447 /**
448  * delete idmap from proper nodemap tree
449  *
450  * \param       name            name of nodemap
451  * \param       id_type         NODEMAP_UID or NODEMAP_GID
452  * \param       map             array[2] __u32 containing the mapA values
453  *                              map[0] is client id
454  *                              map[1] is the filesystem id
455  *
456  * \retval      0 on success
457  */
458 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
459                       const __u32 map[2])
460 {
461         struct lu_nodemap       *nodemap = NULL;
462         struct lu_idmap         *idmap = NULL;
463         int                     rc = 0;
464
465         rc = nodemap_lookup(name, &nodemap);
466         if (nodemap == NULL || is_default_nodemap(nodemap))
467                 GOTO(out, rc = -EINVAL);
468
469         write_lock(&nodemap->nm_idmap_lock);
470         idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
471                              map[0]);
472         if (idmap == NULL) {
473                 write_unlock(&nodemap->nm_idmap_lock);
474                 GOTO(out_putref, rc = -EINVAL);
475         }
476
477         idmap_delete(id_type, idmap, nodemap);
478         write_unlock(&nodemap->nm_idmap_lock);
479         nm_member_revoke_locks(nodemap);
480
481 out_putref:
482         nodemap_putref(nodemap);
483 out:
484         return rc;
485 }
486 EXPORT_SYMBOL(nodemap_del_idmap);
487
488 /**
489  * mapping function for nodemap idmaps
490  *
491  * \param       nodemap         lu_nodemap structure defining nodemap
492  * \param       node_type       NODEMAP_UID or NODEMAP_GID
493  * \param       tree_type       NODEMAP_CLIENT_TO_FS or
494  *                              NODEMAP_FS_TO_CLIENT
495  * \param       id              id to map
496  *
497  * \retval      mapped id according to the rules below.
498  *
499  * if the nodemap_active is false, just return the passed id without mapping
500  *
501  * if the id to be looked up is 0, check that root access is allowed and if it
502  * is, return 0. Otherwise, return the squash uid or gid.
503  *
504  * if the nodemap is configured to trusted the ids from the client system, just
505  * return the passwd id without mapping.
506  *
507  * if by this point, we haven't returned and the nodemap in question is the
508  * default nodemap, return the squash uid or gid.
509  *
510  * after these checks, search the proper tree for the mapping, and if found
511  * return the mapped value, otherwise return the squash uid or gid.
512  */
513 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
514                      enum nodemap_id_type id_type,
515                      enum nodemap_tree_type tree_type, __u32 id)
516 {
517         struct lu_idmap         *idmap = NULL;
518         __u32                    found_id;
519
520         if (!nodemap_active)
521                 goto out;
522
523         if (unlikely(nodemap == NULL))
524                 goto out;
525
526         if (id == 0) {
527                 if (nodemap->nmf_allow_root_access)
528                         goto out;
529                 else
530                         goto squash;
531         }
532
533         if (nodemap->nmf_trust_client_ids)
534                 goto out;
535
536         if (is_default_nodemap(nodemap))
537                 goto squash;
538
539         read_lock(&nodemap->nm_idmap_lock);
540         idmap = idmap_search(nodemap, tree_type, id_type, id);
541         if (idmap == NULL) {
542                 read_unlock(&nodemap->nm_idmap_lock);
543                 goto squash;
544         }
545
546         if (tree_type == NODEMAP_FS_TO_CLIENT)
547                 found_id = idmap->id_client;
548         else
549                 found_id = idmap->id_fs;
550         read_unlock(&nodemap->nm_idmap_lock);
551         return found_id;
552
553 squash:
554         if (id_type == NODEMAP_UID)
555                 return nodemap->nm_squash_uid;
556         else
557                 return nodemap->nm_squash_gid;
558 out:
559         return id;
560 }
561 EXPORT_SYMBOL(nodemap_map_id);
562
563 /**
564  * Map posix ACL entries according to the nodemap membership. Removes any
565  * squashed ACLs.
566  *
567  * \param       lu_nodemap      nodemap
568  * \param       buf             buffer containing xattr encoded ACLs
569  * \param       size            size of ACLs in bytes
570  * \param       tree_type       direction of mapping
571  * \retval      size            new size of ACLs in bytes
572  * \retval      -EINVAL         bad \a size param, see posix_acl_xattr_count()
573  */
574 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
575                         enum nodemap_tree_type tree_type)
576 {
577         posix_acl_xattr_header  *header = buf;
578         posix_acl_xattr_entry   *entry = &header->a_entries[0];
579         posix_acl_xattr_entry   *new_entry = entry;
580         posix_acl_xattr_entry   *end;
581         int                      count;
582
583         if (!nodemap_active)
584                 return size;
585
586         if (unlikely(nodemap == NULL))
587                 return size;
588
589         count = posix_acl_xattr_count(size);
590         if (count < 0)
591                 return -EINVAL;
592         if (count == 0)
593                 return 0;
594
595         for (end = entry + count; entry != end; entry++) {
596                 __u16 tag = le16_to_cpu(entry->e_tag);
597                 __u32 id = le32_to_cpu(entry->e_id);
598
599                 switch (tag) {
600                 case ACL_USER:
601                         id = nodemap_map_id(nodemap, NODEMAP_UID,
602                                             tree_type, id);
603                         if (id == nodemap->nm_squash_uid)
604                                 continue;
605                         entry->e_id = cpu_to_le32(id);
606                         break;
607                 case ACL_GROUP:
608                         id = nodemap_map_id(nodemap, NODEMAP_GID,
609                                             tree_type, id);
610                         if (id == nodemap->nm_squash_gid)
611                                 continue;
612                         entry->e_id = cpu_to_le32(id);
613                         break;
614                 }
615
616                 /* if we skip an ACL, copy the following ones over it */
617                 if (new_entry != entry)
618                         *new_entry = *entry;
619
620                 new_entry++;
621         }
622
623         return (void *)new_entry - (void *)header;
624 }
625 EXPORT_SYMBOL(nodemap_map_acl);
626
627 /*
628  * add nid range to nodemap
629  * \param       name            nodemap name
630  * \param       range_st        string containing nid range
631  * \retval      0 on success
632  *
633  * add an range to the global range tree and attached the
634  * range to the named nodemap.
635  */
636 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
637 {
638         struct lu_nodemap       *nodemap = NULL;
639         struct lu_nid_range     *range;
640         int rc;
641
642         rc = nodemap_lookup(name, &nodemap);
643         if (nodemap == NULL || is_default_nodemap(nodemap))
644                 GOTO(out, rc = -EINVAL);
645
646         range = range_create(nid[0], nid[1], nodemap);
647         if (range == NULL)
648                 GOTO(out_putref, rc = -ENOMEM);
649
650         write_lock(&nm_range_tree_lock);
651         rc = range_insert(range);
652         if (rc != 0) {
653                 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
654                       nodemap->nm_name, rc);
655                 write_unlock(&nm_range_tree_lock);
656                 list_del(&range->rn_list);
657                 range_destroy(range);
658                 GOTO(out_putref, rc = -ENOMEM);
659         }
660
661         list_add(&range->rn_list, &nodemap->nm_ranges);
662         write_unlock(&nm_range_tree_lock);
663
664         nm_member_reclassify_nodemap(default_nodemap);
665         nm_member_revoke_locks(default_nodemap);
666         nm_member_revoke_locks(nodemap);
667
668 out_putref:
669         nodemap_putref(nodemap);
670 out:
671         return rc;
672 }
673 EXPORT_SYMBOL(nodemap_add_range);
674
675 /**
676  * delete a range
677  * \param       name            nodemap name
678  * \param       range_str       string containing range
679  * \retval      0 on success
680  *
681  * Delete range from global range tree, and remove it
682  * from the list in the associated nodemap.
683  */
684 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
685 {
686         struct lu_nodemap       *nodemap;
687         struct lu_nid_range     *range;
688         int                     rc = 0;
689
690         rc = nodemap_lookup(name, &nodemap);
691         if (nodemap == NULL || is_default_nodemap(nodemap))
692                 GOTO(out, rc = -EINVAL);
693
694         write_lock(&nm_range_tree_lock);
695         range = range_find(nid[0], nid[1]);
696         if (range == NULL) {
697                 write_unlock(&nm_range_tree_lock);
698                 GOTO(out_putref, rc = -EINVAL);
699         }
700
701         range_delete(range);
702         write_unlock(&nm_range_tree_lock);
703         nm_member_reclassify_nodemap(nodemap);
704         nm_member_revoke_locks(default_nodemap);
705         nm_member_revoke_locks(nodemap);
706
707 out_putref:
708         nodemap_putref(nodemap);
709 out:
710         return rc;
711 }
712 EXPORT_SYMBOL(nodemap_del_range);
713
714 /**
715  * Nodemap constructor
716  *
717  * Creates an lu_nodemap structure and assigns sane default
718  * member values. If this is the default nodemap, the defaults
719  * are the most restictive in xterms of mapping behavior. Otherwise
720  * the default flags should be inherited from the default nodemap.
721  * The adds nodemap to nodemap_hash.
722  *
723  * \param       name            name of nodemap
724  * \param       is_default      true if default nodemap
725  * \retval      0               success
726  * \retval      -EINVAL         invalid nodemap name
727  * \retval      -EEXIST         nodemap already exists
728  * \retval      -ENOMEM         cannot allocate memory for nodemap
729  */
730 static int nodemap_create(const char *name, bool is_default)
731 {
732         struct lu_nodemap       *nodemap = NULL;
733         int                     rc = 0;
734
735         if (!nodemap_name_is_valid(name))
736                 GOTO(out, rc = -EINVAL);
737
738         OBD_ALLOC_PTR(nodemap);
739         if (nodemap == NULL) {
740                 CERROR("cannot allocate memory (%zu bytes)"
741                        "for nodemap '%s'\n", sizeof(*nodemap),
742                        name);
743                 GOTO(out, rc = -ENOMEM);
744         }
745
746         /*
747          * take an extra reference to prevent nodemap from being destroyed
748          * while its being created.
749          */
750         atomic_set(&nodemap->nm_refcount, 2);
751         snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
752         rc = cfs_hash_add_unique(nodemap_hash, name, &nodemap->nm_hash);
753         if (rc != 0) {
754                 OBD_FREE_PTR(nodemap);
755                 GOTO(out, rc = -EEXIST);
756         }
757
758
759         rc = nm_member_init_hash(nodemap);
760         if (rc != 0) {
761                 OBD_FREE_PTR(nodemap);
762                 goto out;
763         }
764
765         INIT_LIST_HEAD(&nodemap->nm_ranges);
766
767         rwlock_init(&nodemap->nm_idmap_lock);
768         nodemap->nm_fs_to_client_uidmap = RB_ROOT;
769         nodemap->nm_client_to_fs_uidmap = RB_ROOT;
770         nodemap->nm_fs_to_client_gidmap = RB_ROOT;
771         nodemap->nm_client_to_fs_gidmap = RB_ROOT;
772
773         if (is_default) {
774                 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
775                 nodemap->nmf_trust_client_ids = 0;
776                 nodemap->nmf_allow_root_access = 0;
777                 nodemap->nmf_block_lookups = 0;
778
779                 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
780                 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
781
782                 lprocfs_nodemap_register(name, is_default, nodemap);
783
784                 default_nodemap = nodemap;
785         } else {
786                 nodemap->nm_id = atomic_inc_return(&nodemap_highest_id);
787                 nodemap->nmf_trust_client_ids =
788                                 default_nodemap->nmf_trust_client_ids;
789                 nodemap->nmf_allow_root_access =
790                                 default_nodemap->nmf_allow_root_access;
791                 nodemap->nmf_block_lookups =
792                                 default_nodemap->nmf_block_lookups;
793
794                 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
795                 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
796
797                 lprocfs_nodemap_register(name, is_default, nodemap);
798         }
799
800         if (rc == 0) {
801                 nodemap_putref(nodemap);
802                 goto out;
803         }
804
805         CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
806         nodemap_destroy(nodemap);
807
808 out:
809         return rc;
810 }
811
812 /**
813  * update flag to turn on or off nodemap functions
814  * \param       name            nodemap name
815  * \param       admin_string    string containing updated value
816  * \retval      0 on success
817  *
818  * Update admin flag to turn on or off nodemap functions.
819  */
820 int nodemap_set_allow_root(const char *name, bool allow_root)
821 {
822         struct lu_nodemap       *nodemap = NULL;
823         int                     rc = 0;
824
825         rc = nodemap_lookup(name, &nodemap);
826         if (nodemap == NULL)
827                 GOTO(out, rc = -ENOENT);
828
829         nodemap->nmf_allow_root_access = allow_root;
830         nm_member_revoke_locks(nodemap);
831         nodemap_putref(nodemap);
832 out:
833         return rc;
834 }
835 EXPORT_SYMBOL(nodemap_set_allow_root);
836
837 /**
838  * updated trust_client_ids flag for nodemap
839  *
840  * \param       name            nodemap name
841  * \param       trust_string    new value for trust flag
842  * \retval      0 on success
843  *
844  * Update the trust_client_ids flag for a nodemap.
845  */
846 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
847 {
848         struct lu_nodemap       *nodemap = NULL;
849         int                     rc = 0;
850
851         rc = nodemap_lookup(name, &nodemap);
852         if (nodemap == NULL)
853                 GOTO(out, rc = -ENOENT);
854
855         nodemap->nmf_trust_client_ids = trust_client_ids;
856         nm_member_revoke_locks(nodemap);
857         nodemap_putref(nodemap);
858 out:
859         return rc;
860 }
861 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
862
863 /**
864  * update the squash_uid for a nodemap
865  *
866  * \param       name            nodemap name
867  * \param       uid_string      string containing new squash_uid value
868  * \retval      0 on success
869  *
870  * Update the squash_uid for a nodemap. The squash_uid is the uid
871  * that the all client uids are mapped to if nodemap is active,
872  * the trust_client_ids flag is not set, and the uid is not in
873  * the idmap tree.
874  */
875 int nodemap_set_squash_uid(const char *name, uid_t uid)
876 {
877         struct lu_nodemap       *nodemap = NULL;
878         int                     rc = 0;
879
880         rc = nodemap_lookup(name, &nodemap);
881         if (nodemap == NULL)
882                 GOTO(out, rc = -ENOENT);
883
884         nodemap->nm_squash_uid = uid;
885         nm_member_revoke_locks(nodemap);
886         nodemap_putref(nodemap);
887 out:
888         return rc;
889 }
890 EXPORT_SYMBOL(nodemap_set_squash_uid);
891
892 /**
893  * Update the squash_gid for a nodemap.
894  *
895  * \param       name            nodemap name
896  * \param       gid_string      string containing new squash_gid value
897  * \retval      0 on success
898  *
899  * Update the squash_gid for a nodemap. The squash_uid is the gid
900  * that the all client gids are mapped to if nodemap is active,
901  * the trust_client_ids flag is not set, and the gid is not in
902  * the idmap tree.
903  */
904 int nodemap_set_squash_gid(const char *name, gid_t gid)
905 {
906         struct lu_nodemap       *nodemap = NULL;
907         int                     rc = 0;
908
909         rc = nodemap_lookup(name, &nodemap);
910         if (nodemap == NULL)
911                 GOTO(out, rc = -ENOENT);
912
913         nodemap->nm_squash_gid = gid;
914         nm_member_revoke_locks(nodemap);
915         nodemap_putref(nodemap);
916 out:
917         return rc;
918 }
919 EXPORT_SYMBOL(nodemap_set_squash_gid);
920
921 /**
922  * Returns true if this nodemap has root user access. Always returns true if
923  * nodemaps are not active.
924  *
925  * \param       nodemap         nodemap to check access for
926  */
927 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
928 {
929         return !nodemap_active || nodemap->nmf_allow_root_access;
930 }
931 EXPORT_SYMBOL(nodemap_can_setquota);
932
933 /**
934  * Add a nodemap
935  *
936  * \param       name            name of nodemap
937  * \retval      0               success
938  * \retval      -EINVAL         invalid nodemap name
939  * \retval      -EEXIST         nodemap already exists
940  * \retval      -ENOMEM         cannot allocate memory for nodemap
941  */
942 int nodemap_add(const char *nodemap_name)
943 {
944         return nodemap_create(nodemap_name, 0);
945 }
946 EXPORT_SYMBOL(nodemap_add);
947
948 /**
949  * Delete a nodemap
950  *
951  * \param       name            name of nodemmap
952  * \retval      0               success
953  * \retval      -EINVAL         invalid input
954  * \retval      -ENOENT         no existing nodemap
955  */
956 int nodemap_del(const char *nodemap_name)
957 {
958         struct  lu_nodemap *nodemap;
959         int     rc = 0;
960
961         if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
962                 GOTO(out, rc = -EINVAL);
963
964         nodemap = cfs_hash_del_key(nodemap_hash, nodemap_name);
965         if (nodemap == NULL)
966                 GOTO(out, rc = -ENOENT);
967
968         /*
969          * remove procfs here in case nodemap_create called with same name
970          * before nodemap_destory is run.
971          */
972         lprocfs_remove(&nodemap->nm_proc_entry);
973         nodemap_putref(nodemap);
974 out:
975         return rc;
976 }
977 EXPORT_SYMBOL(nodemap_del);
978
979 /**
980  * activate nodemap functions
981  *
982  * \param       value           1 for on, 0 for off
983  */
984 void nodemap_activate(const bool value)
985 {
986         nodemap_active = value;
987         nm_member_revoke_all();
988 }
989 EXPORT_SYMBOL(nodemap_activate);
990
991 /**
992  * Cleanup nodemap module on exit
993  */
994 void nodemap_mod_exit(void)
995 {
996         nodemap_cleanup_all();
997         lprocfs_remove(&proc_lustre_nodemap_root);
998 }
999
1000 /**
1001  * Initialize the nodemap module
1002  */
1003 int nodemap_mod_init(void)
1004 {
1005         int rc = 0;
1006
1007         rc = nodemap_init_hash();
1008         if (rc != 0)
1009                 goto cleanup;
1010
1011         rwlock_init(&nm_range_tree_lock);
1012         nodemap_procfs_init();
1013         rc = nodemap_create(DEFAULT_NODEMAP, 1);
1014
1015 cleanup:
1016         if (rc != 0)
1017                 nodemap_mod_exit();
1018
1019         return rc;
1020 }
1021
1022 static int nm_member_revoke_all_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1023                                    struct hlist_node *hnode, void *data)
1024 {
1025         struct lu_nodemap *nodemap;
1026
1027         nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1028         nm_member_revoke_locks(nodemap);
1029         return 0;
1030 }
1031
1032 /**
1033  * Revoke locks for all nodemaps.
1034  */
1035 void nm_member_revoke_all()
1036 {
1037         cfs_hash_for_each_safe(nodemap_hash, nm_member_revoke_all_cb, NULL);
1038 }
1039