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 lu_nodemap *nodemap = PDE_DATA(inode);
74 return single_open(file, nodemap_idmap_show, nodemap);
77 static int nodemap_ranges_show(struct seq_file *m, void *data)
79 struct lu_nodemap *nodemap = m->private;
80 struct lu_nid_range *range;
81 struct interval_node_extent ext;
85 list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
89 ext = range->rn_node.in_extent;
90 seq_printf(m, " { id: %u, start_nid: %s, "
92 range->rn_id, libcfs_nid2str(ext.start),
93 libcfs_nid2str(ext.end));
101 static int nodemap_ranges_open(struct inode *inode, struct file *file)
103 struct lu_nodemap *nodemap = PDE_DATA(inode);
105 return single_open(file, nodemap_ranges_show, nodemap);
108 static int nodemap_active_seq_show(struct seq_file *m, void *data)
110 return seq_printf(m, "%u\n", (unsigned int)nodemap_active);
114 nodemap_active_seq_write(struct file *file, const char __user *buffer,
115 size_t count, loff_t *off)
117 char active_string[NODEMAP_LPROC_FLAG_LEN + 1];
124 if (count > NODEMAP_LPROC_FLAG_LEN)
127 if (copy_from_user(active_string, buffer, count))
130 active_string[count] = '\0';
131 active = simple_strtoul(active_string, NULL, 10);
132 nodemap_active = active;
136 LPROC_SEQ_FOPS(nodemap_active);
138 static int nodemap_id_seq_show(struct seq_file *m, void *data)
140 struct lu_nodemap *nodemap = m->private;
142 return seq_printf(m, "%u\n", nodemap->nm_id);
144 LPROC_SEQ_FOPS_RO(nodemap_id);
146 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
148 struct lu_nodemap *nodemap = m->private;
150 return seq_printf(m, "%u\n", nodemap->nm_squash_uid);
153 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
155 struct lu_nodemap *nodemap = m->private;
157 return seq_printf(m, "%u\n", nodemap->nm_squash_gid);
160 static int nodemap_trusted_seq_show(struct seq_file *m, void *data)
162 struct lu_nodemap *nodemap = m->private;
164 return seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
167 static int nodemap_admin_seq_show(struct seq_file *m, void *data)
169 struct lu_nodemap *nodemap = m->private;
171 return seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
174 #ifdef NODEMAP_PROC_DEBUG
175 static int nodemap_proc_read_flag(const char __user *buffer,
176 unsigned long count, unsigned int *flag_p)
178 char scratch[NODEMAP_LPROC_FLAG_LEN + 1];
183 if (count > NODEMAP_LPROC_FLAG_LEN)
186 if (copy_from_user(scratch, buffer, count))
189 scratch[count] = '\0';
190 *flag_p = simple_strtoul(scratch, NULL, 10);
196 nodemap_squash_uid_seq_write(struct file *file, const char __user *buffer,
197 size_t count, loff_t *off)
199 char squash[NODEMAP_LPROC_ID_LEN + 1];
200 struct seq_file *m = file->private_data;
201 struct lu_nodemap *nodemap = m->private;
208 if (count > NODEMAP_LPROC_FLAG_LEN)
211 if (copy_from_user(squash, buffer, count))
214 squash[count] = '\0';
215 squash_uid = simple_strtoul(squash, NULL, 10);
216 nodemap->nm_squash_uid = squash_uid;
222 nodemap_squash_gid_seq_write(struct file *file, const char __user *buffer,
223 size_t count, loff_t *off)
225 char squash[NODEMAP_LPROC_ID_LEN + 1];
226 struct seq_file *m = file->private_data;
227 struct lu_nodemap *nodemap = m->private;
234 if (count > NODEMAP_LPROC_FLAG_LEN)
237 if (copy_from_user(squash, buffer, count))
240 squash[count] = '\0';
241 squash_gid = simple_strtoul(squash, NULL, 10);
242 nodemap->nm_squash_gid = squash_gid;
248 nodemap_trusted_seq_write(struct file *file, const char __user *buffer,
249 size_t count, loff_t *off)
251 struct seq_file *m = file->private_data;
252 struct lu_nodemap *nodemap = m->private;
256 rc = nodemap_proc_read_flag(buffer, count, &flags);
258 nodemap->nmf_trust_client_ids = !!flags;
264 nodemap_admin_seq_write(struct file *file, const char __user *buffer,
265 size_t count, loff_t *off)
267 struct seq_file *m = file->private_data;
268 struct lu_nodemap *nodemap = m->private;
272 rc = nodemap_proc_read_flag(buffer, count, &flags);
274 nodemap->nmf_allow_root_access = !!flags;
280 lprocfs_add_nodemap_seq_write(struct file *file, const char __user *buffer,
281 size_t count, loff_t *off)
283 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
292 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
295 if (copy_from_user(buf, buffer, count))
299 pos = strchr(buf, '\n');
304 name = strsep(&cpybuf, " ");
308 rc = nodemap_add(name);
314 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap);
317 lprocfs_del_nodemap_seq_write(struct file *file, const char __user *buffer,
318 size_t count, loff_t *off)
320 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
329 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
332 if (copy_from_user(buf, buffer, count))
336 pos = strchr(buf, '\n');
341 name = strsep(&cpybuf, " ");
345 rc = nodemap_del(name);
352 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap);
354 #endif /* NODEMAP_PROC_DEBUG */
356 static struct lprocfs_seq_vars lprocfs_nodemap_module_vars[] = {
359 .fops = &nodemap_active_fops,
361 #ifdef NODEMAP_PROC_DEBUG
363 .name = "add_nodemap",
364 .fops = &nodemap_add_nodemap_fops,
367 .name = "remove_nodemap",
368 .fops = &nodemap_del_nodemap_fops,
370 #endif /* NODEMAP_PROC_DEBUG */
376 #ifdef NODEMAP_PROC_DEBUG
377 LPROC_SEQ_FOPS(nodemap_trusted);
378 LPROC_SEQ_FOPS(nodemap_admin);
379 LPROC_SEQ_FOPS(nodemap_squash_uid);
380 LPROC_SEQ_FOPS(nodemap_squash_gid);
382 LPROC_SEQ_FOPS_RO(nodemap_trusted);
383 LPROC_SEQ_FOPS_RO(nodemap_admin);
384 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
385 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
388 const struct file_operations nodemap_ranges_fops = {
389 .open = nodemap_ranges_open,
392 .release = single_release
395 const struct file_operations nodemap_idmap_fops = {
396 .open = nodemap_idmap_open,
399 .release = single_release
402 static struct lprocfs_seq_vars lprocfs_nodemap_vars[] = {
405 .fops = &nodemap_id_fops,
408 .name = "trusted_nodemap",
409 .fops = &nodemap_trusted_fops,
412 .name = "admin_nodemap",
413 .fops = &nodemap_admin_fops,
416 .name = "squash_uid",
417 .fops = &nodemap_squash_uid_fops,
420 .name = "squash_gid",
421 .fops = &nodemap_squash_gid_fops,
425 .fops = &nodemap_ranges_fops,
429 .fops = &nodemap_idmap_fops,
436 static struct lprocfs_seq_vars lprocfs_default_nodemap_vars[] = {
439 .fops = &nodemap_id_fops,
442 .name = "trusted_nodemap",
443 .fops = &nodemap_trusted_fops,
446 .name = "admin_nodemap",
447 .fops = &nodemap_admin_fops,
450 .name = "squash_uid",
451 .fops = &nodemap_squash_uid_fops,
454 .name = "squash_gid",
455 .fops = &nodemap_squash_gid_fops,
462 int nodemap_procfs_init(void)
466 proc_lustre_nodemap_root = lprocfs_seq_register(LUSTRE_NODEMAP_NAME,
468 lprocfs_nodemap_module_vars,
471 if (IS_ERR(proc_lustre_nodemap_root)) {
472 rc = PTR_ERR(proc_lustre_nodemap_root);
473 CERROR("cannot create 'nodemap' directory: rc = %d\n",
475 proc_lustre_nodemap_root = NULL;
482 * Register the proc directory for a nodemap
484 * \param name name of nodemap
485 * \param is_default: 1 if default nodemap
488 int lprocfs_nodemap_register(const char *name,
490 struct lu_nodemap *nodemap)
492 struct proc_dir_entry *nodemap_proc_entry;
497 lprocfs_seq_register(name,
498 proc_lustre_nodemap_root,
499 lprocfs_default_nodemap_vars,
502 nodemap_proc_entry = lprocfs_seq_register(name,
503 proc_lustre_nodemap_root,
504 lprocfs_nodemap_vars,
507 if (IS_ERR(nodemap_proc_entry)) {
508 rc = PTR_ERR(nodemap_proc_entry);
509 CERROR("cannot create 'nodemap/%s': rc = %d\n", name, rc);
510 nodemap_proc_entry = NULL;
513 nodemap->nm_proc_entry = nodemap_proc_entry;