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