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_ranges_show(struct seq_file *m, void *data)
37 struct lu_nodemap *nodemap = m->private;
38 struct lu_nid_range *range;
39 struct interval_node_extent ext;
42 list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
43 ext = range->rn_node.in_extent;
44 seq_printf(m, "id: %u: { start_nid: %s, "
46 range->rn_id, libcfs_nid2str(ext.start),
47 libcfs_nid2str(ext.end));
53 static int nodemap_ranges_open(struct inode *inode, struct file *file)
55 struct proc_dir_entry *dir;
56 struct lu_nodemap *nodemap;
61 return single_open(file, nodemap_ranges_show, nodemap);
64 static int nodemap_active_seq_show(struct seq_file *m, void *data)
66 return seq_printf(m, "%u\n", (unsigned int)nodemap_idmap_active);
70 nodemap_active_seq_write(struct file *file, const char __user *buffer,
71 size_t count, loff_t *off)
73 char active_string[NODEMAP_LPROC_FLAG_LEN + 1];
80 if (count > NODEMAP_LPROC_FLAG_LEN)
83 if (copy_from_user(active_string, buffer, count))
86 active_string[count] = '\0';
87 active = simple_strtoul(active_string, NULL, 10);
88 nodemap_idmap_active = active;
92 LPROC_SEQ_FOPS(nodemap_active);
94 static int nodemap_id_seq_show(struct seq_file *m, void *data)
96 struct lu_nodemap *nodemap = m->private;
98 return seq_printf(m, "%u\n", nodemap->nm_id);
100 LPROC_SEQ_FOPS_RO(nodemap_id);
102 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
104 struct lu_nodemap *nodemap = m->private;
106 return seq_printf(m, "%u\n", nodemap->nm_squash_uid);
109 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
111 struct lu_nodemap *nodemap = m->private;
113 return seq_printf(m, "%u\n", nodemap->nm_squash_gid);
116 static int nodemap_trusted_seq_show(struct seq_file *m, void *data)
118 struct lu_nodemap *nodemap = m->private;
120 return seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
123 static int nodemap_admin_seq_show(struct seq_file *m, void *data)
125 struct lu_nodemap *nodemap = m->private;
127 return seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
130 #ifdef NODEMAP_PROC_DEBUG
131 static int nodemap_proc_read_flag(const char __user *buffer,
132 unsigned long count, unsigned int *flag_p)
134 char scratch[NODEMAP_LPROC_FLAG_LEN + 1];
139 if (count > NODEMAP_LPROC_FLAG_LEN)
142 if (copy_from_user(scratch, buffer, count))
145 scratch[count] = '\0';
146 *flag_p = simple_strtoul(scratch, NULL, 10);
152 nodemap_squash_uid_seq_write(struct file *file, const char __user *buffer,
153 size_t count, loff_t *off)
155 char squash[NODEMAP_LPROC_ID_LEN + 1];
156 struct seq_file *m = file->private_data;
157 struct lu_nodemap *nodemap = m->private;
164 if (count > NODEMAP_LPROC_FLAG_LEN)
167 if (copy_from_user(squash, buffer, count))
170 squash[count] = '\0';
171 squash_uid = simple_strtoul(squash, NULL, 10);
172 nodemap->nm_squash_uid = squash_uid;
178 nodemap_squash_gid_seq_write(struct file *file, const char __user *buffer,
179 size_t count, loff_t *off)
181 char squash[NODEMAP_LPROC_ID_LEN + 1];
182 struct seq_file *m = file->private_data;
183 struct lu_nodemap *nodemap = m->private;
190 if (count > NODEMAP_LPROC_FLAG_LEN)
193 if (copy_from_user(squash, buffer, count))
196 squash[count] = '\0';
197 squash_gid = simple_strtoul(squash, NULL, 10);
198 nodemap->nm_squash_gid = squash_gid;
204 nodemap_trusted_seq_write(struct file *file, const char __user *buffer,
205 size_t count, loff_t *off)
207 struct seq_file *m = file->private_data;
208 struct lu_nodemap *nodemap = m->private;
212 rc = nodemap_proc_read_flag(buffer, count, &flags);
214 nodemap->nmf_trust_client_ids = !!flags;
220 nodemap_admin_seq_write(struct file *file, const char __user *buffer,
221 size_t count, loff_t *off)
223 struct seq_file *m = file->private_data;
224 struct lu_nodemap *nodemap = m->private;
228 rc = nodemap_proc_read_flag(buffer, count, &flags);
230 nodemap->nmf_allow_root_access = !!flags;
236 lprocfs_add_nodemap_seq_write(struct file *file, const char __user *buffer,
237 size_t count, loff_t *off)
239 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
248 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
251 if (copy_from_user(buf, buffer, count))
255 pos = strchr(buf, '\n');
260 name = strsep(&cpybuf, " ");
264 rc = nodemap_add(name);
270 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap);
273 lprocfs_del_nodemap_seq_write(struct file *file, const char __user *buffer,
274 size_t count, loff_t *off)
276 char buf[LUSTRE_NODEMAP_NAME_LENGTH + 1];
285 if (count > LUSTRE_NODEMAP_NAME_LENGTH)
288 if (copy_from_user(buf, buffer, count))
292 pos = strchr(buf, '\n');
297 name = strsep(&cpybuf, " ");
301 rc = nodemap_del(name);
308 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap);
310 #endif /* NODEMAP_PROC_DEBUG */
312 static struct lprocfs_seq_vars lprocfs_nodemap_module_vars[] = {
315 .fops = &nodemap_active_fops,
317 #ifdef NODEMAP_PROC_DEBUG
319 .name = "add_nodemap",
320 .fops = &nodemap_add_nodemap_fops,
323 .name = "remove_nodemap",
324 .fops = &nodemap_del_nodemap_fops,
326 #endif /* NODEMAP_PROC_DEBUG */
332 #ifdef NODEMAP_PROC_DEBUG
333 LPROC_SEQ_FOPS(nodemap_trusted);
334 LPROC_SEQ_FOPS(nodemap_admin);
335 LPROC_SEQ_FOPS(nodemap_squash_uid);
336 LPROC_SEQ_FOPS(nodemap_squash_gid);
338 LPROC_SEQ_FOPS_RO(nodemap_trusted);
339 LPROC_SEQ_FOPS_RO(nodemap_admin);
340 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
341 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
344 const struct file_operations nodemap_ranges_fops = {
345 .open = nodemap_ranges_open,
348 .release = single_release
351 static struct lprocfs_seq_vars lprocfs_nodemap_vars[] = {
354 .fops = &nodemap_id_fops,
357 .name = "trusted_nodemap",
358 .fops = &nodemap_trusted_fops,
361 .name = "admin_nodemap",
362 .fops = &nodemap_admin_fops,
365 .name = "squash_uid",
366 .fops = &nodemap_squash_uid_fops,
369 .name = "squash_gid",
370 .fops = &nodemap_squash_gid_fops,
374 .fops = &nodemap_ranges_fops,
381 static struct lprocfs_seq_vars lprocfs_default_nodemap_vars[] = {
384 .fops = &nodemap_id_fops,
387 .name = "trusted_nodemap",
388 .fops = &nodemap_trusted_fops,
391 .name = "admin_nodemap",
392 .fops = &nodemap_admin_fops,
395 .name = "squash_uid",
396 .fops = &nodemap_squash_uid_fops,
399 .name = "squash_gid",
400 .fops = &nodemap_squash_gid_fops,
407 int nodemap_procfs_init(void)
411 proc_lustre_nodemap_root = lprocfs_seq_register(LUSTRE_NODEMAP_NAME,
413 lprocfs_nodemap_module_vars,
416 if (IS_ERR(proc_lustre_nodemap_root)) {
417 rc = PTR_ERR(proc_lustre_nodemap_root);
418 CERROR("cannot create 'nodemap' directory: rc = %d\n",
420 proc_lustre_nodemap_root = NULL;
427 * Register the proc directory for a nodemap
429 * \param name name of nodemap
430 * \param is_default: 1 if default nodemap
433 int lprocfs_nodemap_register(const char *name,
435 struct lu_nodemap *nodemap)
437 struct proc_dir_entry *nodemap_proc_entry;
442 lprocfs_seq_register(name,
443 proc_lustre_nodemap_root,
444 lprocfs_default_nodemap_vars,
447 nodemap_proc_entry = lprocfs_seq_register(name,
448 proc_lustre_nodemap_root,
449 lprocfs_nodemap_vars,
452 if (IS_ERR(nodemap_proc_entry)) {
453 rc = PTR_ERR(nodemap_proc_entry);
454 CERROR("cannot create 'nodemap/%s': rc = %d\n", name, rc);
455 nodemap_proc_entry = NULL;
458 nodemap->nm_proc_entry = nodemap_proc_entry;