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 <interval_tree.h>
33 #include "nodemap_internal.h"
35 static int nodemap_idmap_show(struct seq_file *m, void *data)
37 struct lu_nodemap *nodemap = m->private;
38 struct lu_idmap *idmap;
43 for (node = rb_first(&nodemap->nm_client_to_fs_uidmap); node;
44 node = rb_next(node)) {
48 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
50 seq_printf(m, " { idtype: uid, client_id: %u, "
51 "fs_id: %u }", idmap->id_client,
54 for (node = rb_first(&nodemap->nm_client_to_fs_gidmap);
55 node; node = rb_next(node)) {
58 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
60 seq_printf(m, " { idtype: gid, client_id: %u, "
61 "fs_id: %u }", idmap->id_client,
70 static int nodemap_idmap_open(struct inode *inode, struct file *file)
72 struct proc_dir_entry *dir;
73 struct lu_nodemap *nodemap;
78 return single_open(file, nodemap_idmap_show, nodemap);
81 static int nodemap_ranges_show(struct seq_file *m, void *data)
83 struct lu_nodemap *nodemap = m->private;
84 struct lu_nid_range *range;
85 struct interval_node_extent ext;
89 list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
93 ext = range->rn_node.in_extent;
94 seq_printf(m, " { id: %u, start_nid: %s, "
96 range->rn_id, libcfs_nid2str(ext.start),
97 libcfs_nid2str(ext.end));
100 seq_printf(m, "]\n");
105 static int nodemap_ranges_open(struct inode *inode, struct file *file)
107 struct proc_dir_entry *dir;
108 struct lu_nodemap *nodemap;
113 return single_open(file, nodemap_ranges_show, nodemap);
116 static int nodemap_active_seq_show(struct seq_file *m, void *data)
118 return seq_printf(m, "%u\n", (unsigned int)nodemap_active);
122 nodemap_active_seq_write(struct file *file, const char __user *buffer,
123 size_t count, loff_t *off)
125 char active_string[NODEMAP_LPROC_FLAG_LEN + 1];
132 if (count > NODEMAP_LPROC_FLAG_LEN)
135 if (copy_from_user(active_string, buffer, count))
138 active_string[count] = '\0';
139 active = simple_strtoul(active_string, NULL, 10);
140 nodemap_active = active;
144 LPROC_SEQ_FOPS(nodemap_active);
146 static int nodemap_id_seq_show(struct seq_file *m, void *data)
148 struct lu_nodemap *nodemap = m->private;
150 return seq_printf(m, "%u\n", nodemap->nm_id);
152 LPROC_SEQ_FOPS_RO(nodemap_id);
154 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
156 struct lu_nodemap *nodemap = m->private;
158 return seq_printf(m, "%u\n", nodemap->nm_squash_uid);
161 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
163 struct lu_nodemap *nodemap = m->private;
165 return seq_printf(m, "%u\n", nodemap->nm_squash_gid);
168 static int nodemap_trusted_seq_show(struct seq_file *m, void *data)
170 struct lu_nodemap *nodemap = m->private;
172 return seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
175 static int nodemap_admin_seq_show(struct seq_file *m, void *data)
177 struct lu_nodemap *nodemap = m->private;
179 return seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
182 #ifdef NODEMAP_PROC_DEBUG
183 static int nodemap_proc_read_flag(const char __user *buffer,
184 unsigned long count, unsigned int *flag_p)
186 char scratch[NODEMAP_LPROC_FLAG_LEN + 1];
191 if (count > NODEMAP_LPROC_FLAG_LEN)
194 if (copy_from_user(scratch, buffer, count))
197 scratch[count] = '\0';
198 *flag_p = simple_strtoul(scratch, NULL, 10);
204 nodemap_squash_uid_seq_write(struct file *file, const char __user *buffer,
205 size_t count, loff_t *off)
207 char squash[NODEMAP_LPROC_ID_LEN + 1];
208 struct seq_file *m = file->private_data;
209 struct lu_nodemap *nodemap = m->private;
216 if (count > NODEMAP_LPROC_FLAG_LEN)
219 if (copy_from_user(squash, buffer, count))
222 squash[count] = '\0';
223 squash_uid = simple_strtoul(squash, NULL, 10);
224 nodemap->nm_squash_uid = squash_uid;
230 nodemap_squash_gid_seq_write(struct file *file, const char __user *buffer,
231 size_t count, loff_t *off)
233 char squash[NODEMAP_LPROC_ID_LEN + 1];
234 struct seq_file *m = file->private_data;
235 struct lu_nodemap *nodemap = m->private;
242 if (count > NODEMAP_LPROC_FLAG_LEN)
245 if (copy_from_user(squash, buffer, count))
248 squash[count] = '\0';
249 squash_gid = simple_strtoul(squash, NULL, 10);
250 nodemap->nm_squash_gid = squash_gid;
256 nodemap_trusted_seq_write(struct file *file, const char __user *buffer,
257 size_t count, loff_t *off)
259 struct seq_file *m = file->private_data;
260 struct lu_nodemap *nodemap = m->private;
264 rc = nodemap_proc_read_flag(buffer, count, &flags);
266 nodemap->nmf_trust_client_ids = !!flags;
272 nodemap_admin_seq_write(struct file *file, const char __user *buffer,
273 size_t count, loff_t *off)
275 struct seq_file *m = file->private_data;
276 struct lu_nodemap *nodemap = m->private;
280 rc = nodemap_proc_read_flag(buffer, count, &flags);
282 nodemap->nmf_allow_root_access = !!flags;
288 lprocfs_add_nodemap_seq_write(struct file *file, const char __user *buffer,
289 size_t count, loff_t *off)
291 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
300 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
303 if (copy_from_user(buf, buffer, count))
307 pos = strchr(buf, '\n');
312 name = strsep(&cpybuf, " ");
316 rc = nodemap_add(name);
322 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap);
325 lprocfs_del_nodemap_seq_write(struct file *file, const char __user *buffer,
326 size_t count, loff_t *off)
328 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
337 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
340 if (copy_from_user(buf, buffer, count))
344 pos = strchr(buf, '\n');
349 name = strsep(&cpybuf, " ");
353 rc = nodemap_del(name);
360 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap);
362 #endif /* NODEMAP_PROC_DEBUG */
364 static struct lprocfs_seq_vars lprocfs_nodemap_module_vars[] = {
367 .fops = &nodemap_active_fops,
369 #ifdef NODEMAP_PROC_DEBUG
371 .name = "add_nodemap",
372 .fops = &nodemap_add_nodemap_fops,
375 .name = "remove_nodemap",
376 .fops = &nodemap_del_nodemap_fops,
378 #endif /* NODEMAP_PROC_DEBUG */
384 #ifdef NODEMAP_PROC_DEBUG
385 LPROC_SEQ_FOPS(nodemap_trusted);
386 LPROC_SEQ_FOPS(nodemap_admin);
387 LPROC_SEQ_FOPS(nodemap_squash_uid);
388 LPROC_SEQ_FOPS(nodemap_squash_gid);
390 LPROC_SEQ_FOPS_RO(nodemap_trusted);
391 LPROC_SEQ_FOPS_RO(nodemap_admin);
392 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
393 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
396 const struct file_operations nodemap_ranges_fops = {
397 .open = nodemap_ranges_open,
400 .release = single_release
403 const struct file_operations nodemap_idmap_fops = {
404 .open = nodemap_idmap_open,
407 .release = single_release
410 static struct lprocfs_seq_vars lprocfs_nodemap_vars[] = {
413 .fops = &nodemap_id_fops,
416 .name = "trusted_nodemap",
417 .fops = &nodemap_trusted_fops,
420 .name = "admin_nodemap",
421 .fops = &nodemap_admin_fops,
424 .name = "squash_uid",
425 .fops = &nodemap_squash_uid_fops,
428 .name = "squash_gid",
429 .fops = &nodemap_squash_gid_fops,
433 .fops = &nodemap_ranges_fops,
437 .fops = &nodemap_idmap_fops,
444 static struct lprocfs_seq_vars lprocfs_default_nodemap_vars[] = {
447 .fops = &nodemap_id_fops,
450 .name = "trusted_nodemap",
451 .fops = &nodemap_trusted_fops,
454 .name = "admin_nodemap",
455 .fops = &nodemap_admin_fops,
458 .name = "squash_uid",
459 .fops = &nodemap_squash_uid_fops,
462 .name = "squash_gid",
463 .fops = &nodemap_squash_gid_fops,
470 int nodemap_procfs_init(void)
474 proc_lustre_nodemap_root = lprocfs_seq_register(LUSTRE_NODEMAP_NAME,
476 lprocfs_nodemap_module_vars,
479 if (IS_ERR(proc_lustre_nodemap_root)) {
480 rc = PTR_ERR(proc_lustre_nodemap_root);
481 CERROR("cannot create 'nodemap' directory: rc = %d\n",
483 proc_lustre_nodemap_root = NULL;
490 * Register the proc directory for a nodemap
492 * \param name name of nodemap
493 * \param is_default: 1 if default nodemap
496 int lprocfs_nodemap_register(const char *name,
498 struct lu_nodemap *nodemap)
500 struct proc_dir_entry *nodemap_proc_entry;
505 lprocfs_seq_register(name,
506 proc_lustre_nodemap_root,
507 lprocfs_default_nodemap_vars,
510 nodemap_proc_entry = lprocfs_seq_register(name,
511 proc_lustre_nodemap_root,
512 lprocfs_nodemap_vars,
515 if (IS_ERR(nodemap_proc_entry)) {
516 rc = PTR_ERR(nodemap_proc_entry);
517 CERROR("cannot create 'nodemap/%s': rc = %d\n", name, rc);
518 nodemap_proc_entry = NULL;
521 nodemap->nm_proc_entry = nodemap_proc_entry;