4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (C) 2013, Trustees of Indiana University
24 * Author: Joshua Walgenbach <jjw@iu.edu>
27 #define NODEMAP_LPROC_ID_LEN 16
28 #define NODEMAP_LPROC_FLAG_LEN 2
30 #include <lprocfs_status.h>
31 #include <lustre_net.h>
32 #include <lustre_export.h>
33 #include <obd_class.h>
34 #include <interval_tree.h>
35 #include "nodemap_internal.h"
37 /* Turn on proc debug interface to allow OSS and
38 * MDS nodes to configure nodemap independently of
39 * MGS (since the nodemap distribution is not written
41 #define NODEMAP_PROC_DEBUG 1
44 * Reads and prints the idmap for the given nodemap.
46 * \param m seq file in proc fs
50 static int nodemap_idmap_show(struct seq_file *m, void *data)
52 struct lu_nodemap *nodemap = m->private;
53 struct lu_idmap *idmap;
58 read_lock(&nodemap->nm_idmap_lock);
59 for (node = rb_first(&nodemap->nm_client_to_fs_uidmap); node;
60 node = rb_next(node)) {
64 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
66 seq_printf(m, " { idtype: uid, client_id: %u, "
67 "fs_id: %u }", idmap->id_client,
70 for (node = rb_first(&nodemap->nm_client_to_fs_gidmap);
71 node; node = rb_next(node)) {
74 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
76 seq_printf(m, " { idtype: gid, client_id: %u, "
77 "fs_id: %u }", idmap->id_client,
80 read_unlock(&nodemap->nm_idmap_lock);
88 * Attaches nodemap_idmap_show to proc file.
90 * \param inode inode of seq file in proc fs
91 * \param file seq file
94 static int nodemap_idmap_open(struct inode *inode, struct file *file)
96 struct lu_nodemap *nodemap = PDE_DATA(inode);
98 return single_open(file, nodemap_idmap_show, nodemap);
102 * Reads and prints the NID ranges for the given nodemap.
104 * \param m seq file in proc fs
108 static int nodemap_ranges_show(struct seq_file *m, void *data)
110 struct lu_nodemap *nodemap = m->private;
111 struct lu_nid_range *range;
112 struct interval_node_extent ext;
115 seq_printf(m, "[\n");
116 read_lock(&nm_range_tree_lock);
117 list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
119 seq_printf(m, ",\n");
121 ext = range->rn_node.in_extent;
122 seq_printf(m, " { id: %u, start_nid: %s, "
124 range->rn_id, libcfs_nid2str(ext.start),
125 libcfs_nid2str(ext.end));
127 read_unlock(&nm_range_tree_lock);
129 seq_printf(m, "]\n");
135 * Connects nodemap_idmap_show to proc file.
137 * \param inode inode of seq file in proc fs
138 * \param file seq file
141 static int nodemap_ranges_open(struct inode *inode, struct file *file)
143 struct lu_nodemap *nodemap = PDE_DATA(inode);
145 return single_open(file, nodemap_ranges_show, nodemap);
149 * Hash callback, reads and prints the exports attached to this nodemap.
151 * \param hs nodemap member hash
153 * \param hnode current member in hash
154 * \param data seq_file to print to
157 static int nodemap_exports_show_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
158 struct hlist_node *hnode, void *data)
160 struct seq_file *m = data;
161 struct obd_export *exp;
164 exp = hlist_entry(hnode, struct obd_export,
165 exp_target_data.ted_nodemap_member);
166 key = cfs_hash_key(hs, hnode);
167 seq_printf(m, " { nid: %s, uuid: %s },",
168 obd_export_nid2str(exp), exp->exp_client_uuid.uuid);
174 * Reads and prints the exports attached to the given nodemap via hash
177 * \param m seq file in proc fs
181 static int nodemap_exports_show(struct seq_file *m, void *data)
183 struct lu_nodemap *nodemap = m->private;
185 seq_printf(m, "[\n");
187 cfs_hash_for_each(nodemap->nm_member_hash, nodemap_exports_show_cb, m);
190 seq_printf(m, "]\n");
196 * Attaches nodemap_idmap_show to proc file.
198 * \param inode inode of seq file in proc fs
199 * \param file seq file
202 static int nodemap_exports_open(struct inode *inode, struct file *file)
204 struct lu_nodemap *nodemap = PDE_DATA(inode);
206 return single_open(file, nodemap_exports_show, nodemap);
210 * Reads and prints the active flag for the given nodemap.
212 * \param m seq file in proc fs
216 static int nodemap_active_seq_show(struct seq_file *m, void *data)
218 return seq_printf(m, "%u\n", (unsigned int)nodemap_active);
222 * Activate/deactivate nodemap.
224 * \param[in] file proc file
225 * \param[in] buffer string, "1" or "0" to activate/deactivate nodemap
226 * \param[in] count \a buffer length
227 * \param[in] off unused
228 * \retval \a count on success
229 * \retval negative number on error
232 nodemap_active_seq_write(struct file *file, const char __user *buffer,
233 size_t count, loff_t *off)
235 char active_string[NODEMAP_LPROC_FLAG_LEN + 1];
236 long unsigned int active;
242 if (count >= sizeof(active_string))
245 if (copy_from_user(active_string, buffer, count))
248 active_string[count] = '\0';
249 rc = kstrtoul(active_string, 10, &active);
253 nodemap_active = active;
257 LPROC_SEQ_FOPS(nodemap_active);
260 * Reads and prints the nodemap ID for the given nodemap.
262 * \param m seq file in proc fs
266 static int nodemap_id_seq_show(struct seq_file *m, void *data)
268 struct lu_nodemap *nodemap = m->private;
270 return seq_printf(m, "%u\n", nodemap->nm_id);
272 LPROC_SEQ_FOPS_RO(nodemap_id);
275 * Reads and prints the root squash UID for the given nodemap.
277 * \param m seq file in proc fs
281 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
283 struct lu_nodemap *nodemap = m->private;
285 return seq_printf(m, "%u\n", nodemap->nm_squash_uid);
289 * Reads and prints the root squash GID for the given nodemap.
291 * \param m seq file in proc fs
295 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
297 struct lu_nodemap *nodemap = m->private;
299 return seq_printf(m, "%u\n", nodemap->nm_squash_gid);
303 * Reads and prints the trusted flag for the given nodemap.
305 * \param m seq file in proc fs
309 static int nodemap_trusted_seq_show(struct seq_file *m, void *data)
311 struct lu_nodemap *nodemap = m->private;
313 return seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
317 * Reads and prints the admin flag for the given nodemap.
319 * \param m seq file in proc fs
323 static int nodemap_admin_seq_show(struct seq_file *m, void *data)
325 struct lu_nodemap *nodemap = m->private;
327 return seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
330 #ifdef NODEMAP_PROC_DEBUG
332 * Helper functions to set nodemap flags.
334 * \param[in] buffer string, which is "1" or "0" to set/unset flag
335 * \param[in] count \a buffer length
336 * \param[out] flag_p where to store flag value
337 * \retval \a count on success
338 * \retval negative number on error
340 static int nodemap_proc_read_flag(const char __user *buffer,
341 unsigned long count, unsigned int *flag_p)
343 char scratch[NODEMAP_LPROC_FLAG_LEN + 1];
344 long unsigned int flag_buf;
350 if (count >= sizeof(scratch))
353 if (copy_from_user(scratch, buffer, count))
356 scratch[count] = '\0';
357 rc = kstrtoul(scratch, 10, &flag_buf);
367 * Set the squash UID.
369 * \param[in] file proc file
370 * \param[in] buffer string representing squash UID to set
371 * \param[in] count \a buffer length
372 * \param[in] off unused
373 * \retval \a count on success
374 * \retval negative number on error
377 nodemap_squash_uid_seq_write(struct file *file, const char __user *buffer,
378 size_t count, loff_t *off)
380 char squash[NODEMAP_LPROC_ID_LEN + 1];
381 struct seq_file *m = file->private_data;
382 struct lu_nodemap *nodemap = m->private;
383 long unsigned int squash_uid;
389 if (count >= sizeof(squash))
392 if (copy_from_user(squash, buffer, count))
395 squash[count] = '\0';
396 rc = kstrtoul(squash, 10, &squash_uid);
400 nodemap->nm_squash_uid = squash_uid;
406 * Set the squash GID.
408 * \param[in] file proc file
409 * \param[in] buffer string representing squash GID to set
410 * \param[in] count \a buffer length
411 * \param[in] off unused
412 * \retval \a count on success
413 * \retval negative number on error
416 nodemap_squash_gid_seq_write(struct file *file, const char __user *buffer,
417 size_t count, loff_t *off)
419 char squash[NODEMAP_LPROC_ID_LEN + 1];
420 struct seq_file *m = file->private_data;
421 struct lu_nodemap *nodemap = m->private;
422 long unsigned int squash_gid;
428 if (count >= sizeof(squash))
431 if (copy_from_user(squash, buffer, count))
434 squash[count] = '\0';
435 rc = kstrtoul(squash, 10, &squash_gid);
439 nodemap->nm_squash_gid = squash_gid;
445 * Set/unset the trusted flag.
447 * \param[in] file proc file
448 * \param[in] buffer string, "1" or "0"
449 * \param[in] count \a buffer length
450 * \param[in] off unused
451 * \retval \a count on success
452 * \retval negative number on error
455 nodemap_trusted_seq_write(struct file *file, const char __user *buffer,
456 size_t count, loff_t *off)
458 struct seq_file *m = file->private_data;
459 struct lu_nodemap *nodemap = m->private;
463 rc = nodemap_proc_read_flag(buffer, count, &flags);
465 nodemap->nmf_trust_client_ids = !!flags;
466 nm_member_revoke_locks(nodemap);
473 * Set/unset the admin flag.
475 * \param[in] file proc file
476 * \param[in] buffer string, "1" or "0"
477 * \param[in] count \a buffer length
478 * \param[in] off unused
479 * \retval \a count on success
480 * \retval negative number on error
483 nodemap_admin_seq_write(struct file *file, const char __user *buffer,
484 size_t count, loff_t *off)
486 struct seq_file *m = file->private_data;
487 struct lu_nodemap *nodemap = m->private;
491 rc = nodemap_proc_read_flag(buffer, count, &flags);
493 nodemap->nmf_allow_root_access = !!flags;
494 nm_member_revoke_locks(nodemap);
503 * \param[in] file proc file
504 * \param[in] buffer string, name of the nodemap to add
505 * \param[in] count \a buffer length
506 * \param[in] off unused
507 * \retval \a count on success
508 * \retval negative number on error
511 lprocfs_add_nodemap_seq_write(struct file *file, const char __user *buffer,
512 size_t count, loff_t *off)
514 char nodemap_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
522 if (count >= sizeof(nodemap_name))
525 if (copy_from_user(nodemap_name, buffer, count))
528 nodemap_name[count] = '\0';
530 cpybuf = nodemap_name;
531 pos = strsep(&cpybuf, " \n");
535 rc = nodemap_add(nodemap_name);
541 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap);
546 * \param[in] file proc file
547 * \param[in] buffer string, name of the nodemap to delete
548 * \param[in] count \a buffer length
549 * \param[in] off unused
550 * \retval \a count on success
551 * \retval negative number on error
554 lprocfs_del_nodemap_seq_write(struct file *file, const char __user *buffer,
555 size_t count, loff_t *off)
557 char nodemap_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
565 if (count >= sizeof(nodemap_name))
568 if (copy_from_user(nodemap_name, buffer, count))
571 nodemap_name[count] = '\0';
573 cpybuf = nodemap_name;
574 pos = strsep(&cpybuf, " \n");
578 rc = nodemap_del(nodemap_name);
585 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap);
588 * Helper function to parse a NID string.
590 * \param[in] rangestr string representation of NIDs, see libcfs_str2nid()
591 * \param[out] nids array of two nids
592 * \retval 0 on success
593 * \retval negative number on error
595 static int parse_nids(char *rangestr, lnet_nid_t nids[2])
597 struct list_head nidlist;
598 char nidstr[2][LNET_NIDSTR_SIZE];
599 char nidrange_str[2 * LNET_NIDSTR_SIZE + 2];
602 INIT_LIST_HEAD(&nidlist);
604 if (cfs_parse_nidlist(rangestr, strlen(rangestr),
608 if (!cfs_nidrange_is_contiguous(&nidlist))
611 cfs_nidrange_find_min_max(&nidlist, nidstr[0], nidstr[1],
613 snprintf(nidrange_str, sizeof(nidrange_str), "%s:%s",
614 nidstr[0], nidstr[1]);
616 rc = nodemap_parse_range(nidrange_str, nids);
620 cfs_free_nidlist(&nidlist);
626 * Add a NID range to nodemap.
628 * \param[in] file proc file
629 * \param[in] buffer string, "<nodemap name> <nid range>"
630 * \param[in] count \a buffer length
631 * \param[in] off unused
632 * \retval \a count on success
633 * \retval negative number on error
636 lprocfs_add_nodemap_range_seq_write(struct file *file,
637 const char __user *buffer,
638 size_t count, loff_t *off)
640 char name_range[LUSTRE_NODEMAP_NAME_LENGTH +
641 LNET_NIDSTR_SIZE * 2 + 2];
644 char *rangestr = NULL;
651 if (count >= sizeof(name_range))
652 GOTO(out, rc = -EINVAL);
654 if (copy_from_user(name_range, buffer, count))
655 GOTO(out, rc = -EFAULT);
657 name_range[count] = '\0';
660 name = strsep(&cpybuf, " ");
662 GOTO(out, rc = -EINVAL);
664 rangestr = strsep(&cpybuf, " \n");
665 if (rangestr == NULL)
666 GOTO(out, rc = -EINVAL);
668 rc = parse_nids(rangestr, nids);
672 rc = nodemap_add_range(name, nids);
674 GOTO(out, rc = -EINVAL);
682 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap_range);
685 * Delete a NID range from nodemap.
687 * \param[in] file proc file
688 * \param[in] buffer string, "<nodemap name> <nid range>"
689 * \param[in] count \a buffer length
690 * \param[in] off unused
691 * \retval \a count on success
692 * \retval negative number on error
695 lprocfs_del_nodemap_range_seq_write(struct file *file,
696 const char __user *buffer,
697 size_t count, loff_t *off)
699 char name_range[LUSTRE_NODEMAP_NAME_LENGTH +
700 LNET_NIDSTR_SIZE * 2 + 2];
703 char *rangestr = NULL;
710 if (count >= sizeof(name_range))
711 GOTO(out, rc = -EINVAL);
713 if (copy_from_user(name_range, buffer, count))
714 GOTO(out, rc = -EFAULT);
716 name_range[count] = '\0';
719 name = strsep(&cpybuf, " ");
721 GOTO(out, rc = -EINVAL);
723 rangestr = strsep(&cpybuf, " \n");
724 if (rangestr == NULL)
725 GOTO(out, rc = -EINVAL);
727 rc = parse_nids(rangestr, nids);
731 rc = nodemap_del_range(name, nids);
733 GOTO(out, rc = -EINVAL);
741 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap_range);
744 * Add an idmap to nodemap.
746 * \param[in] file proc file
747 * \param[in] buffer string, "<nodemap name> <uid|gid> <idmap>"
748 * \param[in] count \a buffer length
749 * \param[in] off unused
750 * \retval \a count on success
751 * \retval negative number on error
754 lprocfs_add_nodemap_idmap_seq_write(struct file *file,
755 const char __user *buffer,
756 size_t count, loff_t *off)
758 char name_idmapstr[LUSTRE_NODEMAP_NAME_LENGTH + 16];
761 char *idtypestr = NULL;
762 char *idmapstr = NULL;
769 if (count >= sizeof(name_idmapstr))
770 GOTO(out, rc = -EINVAL);
772 if (copy_from_user(name_idmapstr, buffer, count))
773 GOTO(out, rc = -EFAULT);
775 name_idmapstr[count] = '\0';
777 cpybuf = name_idmapstr;
778 name = strsep(&cpybuf, " ");
780 GOTO(out, rc = -EINVAL);
782 idtypestr = strsep(&cpybuf, " ");
783 if (idtypestr == NULL)
784 GOTO(out, rc = -EINVAL);
786 idmapstr = strsep(&cpybuf, " \n");
787 if (idmapstr == NULL)
788 GOTO(out, rc = -EINVAL);
790 rc = nodemap_parse_idmap(idmapstr, idmap);
792 GOTO(out, rc = -EINVAL);
794 if (strcmp(idtypestr, "uid") == 0)
795 rc = nodemap_add_idmap(name, NODEMAP_UID, idmap);
796 else if (strcmp(idtypestr, "gid") == 0)
797 rc = nodemap_add_idmap(name, NODEMAP_GID, idmap);
799 GOTO(out, rc = -EINVAL);
802 GOTO(out, rc = -EINVAL);
810 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap_idmap);
813 * Delete an idmap from nodemap.
815 * \param[in] file proc file
816 * \param[in] buffer string, "<nodemap name> <uid|gid> <idmap>"
817 * \param[in] count \a buffer length
818 * \param[in] off unused
819 * \retval \a count on success
820 * \retval negative number on error
823 lprocfs_del_nodemap_idmap_seq_write(struct file *file,
824 const char __user *buffer,
825 size_t count, loff_t *off)
827 char name_idmapstr[LUSTRE_NODEMAP_NAME_LENGTH + 16];
830 char *idtypestr = NULL;
831 char *idmapstr = NULL;
838 if (count >= sizeof(name_idmapstr))
839 GOTO(out, rc = -EINVAL);
841 if (copy_from_user(name_idmapstr, buffer, count))
842 GOTO(out, rc = -EFAULT);
844 name_idmapstr[count] = '\0';
846 cpybuf = name_idmapstr;
847 name = strsep(&cpybuf, " ");
849 GOTO(out, rc = -EINVAL);
851 idtypestr = strsep(&cpybuf, " ");
852 if (idtypestr == NULL)
853 GOTO(out, rc = -EINVAL);
855 idmapstr = strsep(&cpybuf, " \n");
856 if (idmapstr == NULL)
857 GOTO(out, rc = -EINVAL);
859 rc = nodemap_parse_idmap(idmapstr, idmap);
861 GOTO(out, rc = -EINVAL);
863 if (strcmp(idtypestr, "uid") == 0)
864 rc = nodemap_del_idmap(name, NODEMAP_UID, idmap);
865 else if (strcmp(idtypestr, "gid") == 0)
866 rc = nodemap_del_idmap(name, NODEMAP_GID, idmap);
868 GOTO(out, rc = -EINVAL);
871 GOTO(out, rc = -EINVAL);
879 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap_idmap);
880 #endif /* NODEMAP_PROC_DEBUG */
882 static struct lprocfs_vars lprocfs_nm_module_vars[] = {
885 .fops = &nodemap_active_fops,
887 #ifdef NODEMAP_PROC_DEBUG
889 .name = "add_nodemap",
890 .fops = &nodemap_add_nodemap_fops,
893 .name = "remove_nodemap",
894 .fops = &nodemap_del_nodemap_fops,
897 .name = "add_nodemap_range",
898 .fops = &nodemap_add_nodemap_range_fops,
901 .name = "del_nodemap_range",
902 .fops = &nodemap_del_nodemap_range_fops,
905 .name = "add_nodemap_idmap",
906 .fops = &nodemap_add_nodemap_idmap_fops,
909 .name = "del_nodemap_idmap",
910 .fops = &nodemap_del_nodemap_idmap_fops,
912 #endif /* NODEMAP_PROC_DEBUG */
918 #ifdef NODEMAP_PROC_DEBUG
919 LPROC_SEQ_FOPS(nodemap_trusted);
920 LPROC_SEQ_FOPS(nodemap_admin);
921 LPROC_SEQ_FOPS(nodemap_squash_uid);
922 LPROC_SEQ_FOPS(nodemap_squash_gid);
924 LPROC_SEQ_FOPS_RO(nodemap_trusted);
925 LPROC_SEQ_FOPS_RO(nodemap_admin);
926 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
927 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
930 const struct file_operations nodemap_ranges_fops = {
931 .open = nodemap_ranges_open,
934 .release = single_release
937 const struct file_operations nodemap_idmap_fops = {
938 .open = nodemap_idmap_open,
941 .release = single_release
944 const struct file_operations nodemap_exports_fops = {
945 .open = nodemap_exports_open,
948 .release = single_release
951 static struct lprocfs_vars lprocfs_nodemap_vars[] = {
954 .fops = &nodemap_id_fops,
957 .name = "trusted_nodemap",
958 .fops = &nodemap_trusted_fops,
961 .name = "admin_nodemap",
962 .fops = &nodemap_admin_fops,
965 .name = "squash_uid",
966 .fops = &nodemap_squash_uid_fops,
969 .name = "squash_gid",
970 .fops = &nodemap_squash_gid_fops,
974 .fops = &nodemap_ranges_fops,
978 .fops = &nodemap_exports_fops,
982 .fops = &nodemap_idmap_fops,
989 static struct lprocfs_vars lprocfs_default_nodemap_vars[] = {
992 .fops = &nodemap_id_fops,
995 .name = "trusted_nodemap",
996 .fops = &nodemap_trusted_fops,
999 .name = "admin_nodemap",
1000 .fops = &nodemap_admin_fops,
1003 .name = "squash_uid",
1004 .fops = &nodemap_squash_uid_fops,
1007 .name = "squash_gid",
1008 .fops = &nodemap_squash_gid_fops,
1012 .fops = &nodemap_exports_fops,
1020 * Initialize the nodemap procfs directory.
1024 int nodemap_procfs_init(void)
1028 proc_lustre_nodemap_root = lprocfs_register(LUSTRE_NODEMAP_NAME,
1030 lprocfs_nm_module_vars,
1033 if (IS_ERR(proc_lustre_nodemap_root)) {
1034 rc = PTR_ERR(proc_lustre_nodemap_root);
1035 CERROR("cannot create 'nodemap' directory: rc = %d\n",
1037 proc_lustre_nodemap_root = NULL;
1044 * Register the proc directory for a nodemap
1046 * \param name name of nodemap
1047 * \param is_default: 1 if default nodemap
1050 int lprocfs_nodemap_register(const char *name,
1052 struct lu_nodemap *nodemap)
1054 struct proc_dir_entry *nodemap_proc_entry;
1055 struct lprocfs_vars *vars;
1059 vars = lprocfs_default_nodemap_vars;
1061 vars = lprocfs_nodemap_vars;
1063 nodemap_proc_entry = lprocfs_register(name, proc_lustre_nodemap_root,
1065 if (IS_ERR(nodemap_proc_entry)) {
1066 rc = PTR_ERR(nodemap_proc_entry);
1067 CERROR("cannot create 'nodemap/%s': rc = %d\n", name, rc);
1068 nodemap_proc_entry = NULL;
1071 nodemap->nm_proc_entry = nodemap_proc_entry;