Whamcloud - gitweb
LU-15496 tests: fix sanity/398c to use proper OSC name
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_lproc.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) 2014, 2017, Intel Corporation.
26  *
27  * Author: Joshua Walgenbach <jjw@iu.edu>
28  */
29
30 #define NODEMAP_LPROC_ID_LEN 16
31 #define NODEMAP_LPROC_FLAG_LEN 2
32
33 #include <lprocfs_status.h>
34 #include <lustre_net.h>
35 #include <lustre_export.h>
36 #include <obd_class.h>
37 #include <interval_tree.h>
38 #include "nodemap_internal.h"
39
40 static LIST_HEAD(nodemap_pde_list);
41
42 /**
43  * Reads and prints the idmap for the given nodemap.
44  *
45  * \param       m               seq file in proc fs
46  * \param       data            unused
47  * \retval      0               success
48  */
49 static int nodemap_idmap_show(struct seq_file *m, void *data)
50 {
51         struct lu_nodemap       *nodemap;
52         struct lu_idmap         *idmap;
53         struct rb_node          *node;
54         bool                    cont = 0;
55         int rc;
56
57         mutex_lock(&active_config_lock);
58         nodemap = nodemap_lookup(m->private);
59         mutex_unlock(&active_config_lock);
60         if (IS_ERR(nodemap)) {
61                 rc = PTR_ERR(nodemap);
62                 CERROR("cannot find nodemap '%s': rc = %d\n",
63                         (char *)m->private, rc);
64                 return rc;
65         }
66
67         seq_printf(m, "[\n");
68         down_read(&nodemap->nm_idmap_lock);
69         for (node = rb_first(&nodemap->nm_client_to_fs_uidmap); node;
70                                 node = rb_next(node)) {
71                 if (cont)
72                         seq_printf(m, ",\n");
73                 cont = 1;
74                 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
75                 if (idmap != NULL)
76                         seq_printf(m, " { idtype: uid, client_id: %u, "
77                                    "fs_id: %u }", idmap->id_client,
78                                    idmap->id_fs);
79         }
80         for (node = rb_first(&nodemap->nm_client_to_fs_gidmap);
81                                 node; node = rb_next(node)) {
82                 if (cont)
83                         seq_printf(m, ",\n");
84                 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
85                 if (idmap != NULL)
86                         seq_printf(m, " { idtype: gid, client_id: %u, "
87                                    "fs_id: %u }", idmap->id_client,
88                                    idmap->id_fs);
89         }
90         for (node = rb_first(&nodemap->nm_client_to_fs_projidmap);
91              node; node = rb_next(node)) {
92                 if (cont)
93                         seq_printf(m, ",\n");
94                 idmap = rb_entry(node, struct lu_idmap, id_client_to_fs);
95                 if (idmap != NULL)
96                         seq_printf(m,
97                                    " { idtype: projid, client_id: %u, fs_id: %u }",
98                                    idmap->id_client,
99                                    idmap->id_fs);
100         }
101         up_read(&nodemap->nm_idmap_lock);
102         seq_printf(m, "\n");
103         seq_printf(m, "]\n");
104
105         nodemap_putref(nodemap);
106         return 0;
107 }
108
109 /**
110  * Attaches nodemap_idmap_show to proc file.
111  *
112  * \param       inode           inode of seq file in proc fs
113  * \param       file            seq file
114  * \retval      0               success
115  */
116 static int nodemap_idmap_open(struct inode *inode, struct file *file)
117 {
118         return single_open(file, nodemap_idmap_show, pde_data(inode));
119 }
120
121 /**
122  * Reads and prints the NID ranges for the given nodemap.
123  *
124  * \param       m               seq file in proc fs
125  * \param       data            unused
126  * \retval      0               success
127  */
128 static int nodemap_ranges_show(struct seq_file *m, void *data)
129 {
130         struct lu_nodemap               *nodemap;
131         struct lu_nid_range             *range;
132         char                            start_nidstr[LNET_NIDSTR_SIZE];
133         char                            end_nidstr[LNET_NIDSTR_SIZE];
134         bool                            cont = false;
135         int rc;
136
137         mutex_lock(&active_config_lock);
138         nodemap = nodemap_lookup(m->private);
139         if (IS_ERR(nodemap)) {
140                 mutex_unlock(&active_config_lock);
141                 rc = PTR_ERR(nodemap);
142                 CERROR("cannot find nodemap '%s': rc = %d\n",
143                         (char *)m->private, rc);
144                 return rc;
145         }
146
147         seq_printf(m, "[\n");
148         down_read(&active_config->nmc_range_tree_lock);
149         list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
150                 if (cont)
151                         seq_printf(m, ",\n");
152                 cont = 1;
153                 libcfs_nidstr_r(&range->rn_start, start_nidstr, sizeof(start_nidstr));
154                 libcfs_nidstr_r(&range->rn_end, end_nidstr, sizeof(end_nidstr));
155                 seq_printf(m, " { id: %u, start_nid: %s, end_nid: %s }",
156                            range->rn_id, start_nidstr, end_nidstr);
157         }
158         up_read(&active_config->nmc_range_tree_lock);
159         mutex_unlock(&active_config_lock);
160         seq_printf(m, "\n");
161         seq_printf(m, "]\n");
162
163         nodemap_putref(nodemap);
164         return 0;
165 }
166
167 /**
168  * Connects nodemap_idmap_show to proc file.
169  *
170  * \param       inode           inode of seq file in proc fs
171  * \param       file            seq file
172  * \retval      0               success
173  */
174 static int nodemap_ranges_open(struct inode *inode, struct file *file)
175 {
176         return single_open(file, nodemap_ranges_show, pde_data(inode));
177 }
178
179 /**
180  * Reads and prints the fileset for the given nodemap.
181  *
182  * \param       m               seq file in proc fs
183  * \param       data            unused
184  * \retval      0               success
185  */
186 static int nodemap_fileset_seq_show(struct seq_file *m, void *data)
187 {
188         struct lu_nodemap *nodemap;
189         int rc = 0;
190
191         mutex_lock(&active_config_lock);
192         nodemap = nodemap_lookup(m->private);
193         mutex_unlock(&active_config_lock);
194         if (IS_ERR(nodemap)) {
195                 rc = PTR_ERR(nodemap);
196                 CERROR("cannot find nodemap '%s': rc = %d\n",
197                         (char *)m->private, rc);
198                 return rc;
199         }
200
201         seq_printf(m, "%s\n", nodemap->nm_fileset);
202         nodemap_putref(nodemap);
203         return rc;
204 }
205
206 /**
207  * Set a fileset on a nodemap.
208  *
209  * \param[in] file      proc file
210  * \param[in] buffer    string, "<fileset>"
211  * \param[in] count     \a buffer length
212  * \param[in] off       unused
213  * \retval              \a count on success
214  * \retval              negative number on error
215  */
216 static ssize_t
217 nodemap_fileset_seq_write(struct file *file,
218                                       const char __user *buffer,
219                                       size_t count, loff_t *off)
220 {
221         struct seq_file *m = file->private_data;
222         char *nm_fileset;
223         int rc = 0;
224         ENTRY;
225
226         if (count == 0)
227                 RETURN(0);
228
229         if (count > PATH_MAX)
230                 RETURN(-EINVAL);
231
232         OBD_ALLOC(nm_fileset, count + 1);
233         /* OBD_ALLOC zero-fills the buffer */
234         if (nm_fileset == NULL)
235                 RETURN(-ENOMEM);
236
237         if (copy_from_user(nm_fileset, buffer, count))
238                 GOTO(out, rc = -EFAULT);
239
240         rc = nodemap_set_fileset(m->private, nm_fileset);
241         if (rc != 0)
242                 GOTO(out, rc = -EINVAL);
243
244         rc = count;
245 out:
246         OBD_FREE(nm_fileset, count + 1);
247
248         return rc;
249 }
250 LPROC_SEQ_FOPS(nodemap_fileset);
251
252 /**
253  * Reads and prints the SELinux policy info for the given nodemap.
254  *
255  * \param       m               seq file in proc fs
256  * \param       data            unused
257  * \retval      0               success
258  */
259 static int nodemap_sepol_seq_show(struct seq_file *m, void *data)
260 {
261         struct lu_nodemap *nodemap;
262         int rc = 0;
263
264         mutex_lock(&active_config_lock);
265         nodemap = nodemap_lookup(m->private);
266         mutex_unlock(&active_config_lock);
267         if (IS_ERR(nodemap)) {
268                 rc = PTR_ERR(nodemap);
269                 CERROR("cannot find nodemap '%s': rc = %d\n",
270                         (char *)m->private, rc);
271                 return rc;
272         }
273
274         seq_printf(m, "%s\n", nodemap_get_sepol(nodemap));
275         nodemap_putref(nodemap);
276         return rc;
277 }
278
279 /**
280  * Set SELinux policy info on a nodemap.
281  *
282  * \param[in] file      proc file
283  * \param[in] buffer    string, "<sepol>"
284  * \param[in] count     \a buffer length
285  * \param[in] off       unused
286  * \retval              \a count on success
287  * \retval              negative number on error
288  */
289 static ssize_t
290 nodemap_sepol_seq_write(struct file *file,
291                         const char __user *buffer,
292                         size_t count, loff_t *off)
293 {
294         struct seq_file *m = file->private_data;
295         char sepol[LUSTRE_NODEMAP_SEPOL_LENGTH + 1];
296         int rc = 0;
297
298         BUILD_BUG_ON(sizeof(sepol) !=
299                      sizeof(((struct lu_nodemap *)0)->nm_sepol));
300
301         if (count > 0) {
302                 if (count >= sizeof(sepol))
303                         GOTO(out, rc = -ENAMETOOLONG);
304
305                 if (copy_from_user(sepol, buffer, count))
306                         GOTO(out, rc = -EFAULT);
307
308                 sepol[count] = '\0';
309
310                 rc = nodemap_set_sepol(m->private, sepol);
311         }
312
313 out:
314         if (rc != 0)
315                 return rc;
316
317         return count;
318 }
319 LPROC_SEQ_FOPS(nodemap_sepol);
320
321 /**
322  * Reads and prints the exports attached to the given nodemap.
323  *
324  * \param       m               seq file in proc fs, stores nodemap
325  * \param       data            unused
326  * \retval      0               success
327  */
328 static int nodemap_exports_show(struct seq_file *m, void *data)
329 {
330         struct lu_nodemap *nodemap;
331         struct obd_export *exp;
332         char nidstr[LNET_NIDSTR_SIZE] = "<unknown>";
333         int rc;
334
335         mutex_lock(&active_config_lock);
336         nodemap = nodemap_lookup(m->private);
337         mutex_unlock(&active_config_lock);
338         if (IS_ERR(nodemap)) {
339                 rc = PTR_ERR(nodemap);
340                 CERROR("cannot find nodemap '%s': rc = %d\n",
341                         (char *)m->private, rc);
342                 return rc;
343         }
344
345         seq_printf(m, "[\n");
346
347         mutex_lock(&nodemap->nm_member_list_lock);
348         list_for_each_entry(exp, &nodemap->nm_member_list,
349                             exp_target_data.ted_nodemap_member) {
350                 if (exp->exp_connection != NULL)
351                         libcfs_nidstr_r(&exp->exp_connection->c_peer.nid,
352                                           nidstr, sizeof(nidstr));
353
354                 seq_printf(m, " { nid: %s, uuid: %s },",
355                            nidstr, exp->exp_client_uuid.uuid);
356         }
357         mutex_unlock(&nodemap->nm_member_list_lock);
358
359         seq_printf(m, "\n");
360         seq_printf(m, "]\n");
361
362         nodemap_putref(nodemap);
363         return 0;
364 }
365
366 /**
367  * Attaches nodemap_idmap_show to proc file.
368  *
369  * \param       inode           inode of seq file in proc fs
370  * \param       file            seq file
371  * \retval      0               success
372  */
373 static int nodemap_exports_open(struct inode *inode, struct file *file)
374 {
375         return single_open(file, nodemap_exports_show, pde_data(inode));
376 }
377
378 /**
379  * Reads and prints the active flag for the given nodemap.
380  *
381  * \param       m               seq file in proc fs
382  * \param       data            unused
383  * \retval      0               success
384  */
385 static int nodemap_active_seq_show(struct seq_file *m, void *data)
386 {
387         seq_printf(m, "%u\n", (unsigned int)nodemap_active);
388         return 0;
389 }
390
391 /**
392  * Activate/deactivate nodemap.
393  *
394  * \param[in] file      proc file
395  * \param[in] buffer    string, "1" or "0" to activate/deactivate nodemap
396  * \param[in] count     \a buffer length
397  * \param[in] off       unused
398  * \retval              \a count on success
399  * \retval              negative number on error
400  */
401 static ssize_t
402 nodemap_active_seq_write(struct file *file, const char __user *buffer,
403                          size_t count, loff_t *off)
404 {
405         char                    active_string[NODEMAP_LPROC_FLAG_LEN + 1];
406         long unsigned int       active;
407         int                     rc;
408
409         if (count == 0)
410                 return 0;
411
412         if (count >= sizeof(active_string))
413                 return -EINVAL;
414
415         if (copy_from_user(active_string, buffer, count))
416                 return -EFAULT;
417
418         active_string[count] = '\0';
419         rc = kstrtoul(active_string, 10, &active);
420         if (rc != 0)
421                 return -EINVAL;
422
423         nodemap_activate(active);
424
425         return count;
426 }
427 LPROC_SEQ_FOPS(nodemap_active);
428
429 /**
430  * Reads and prints the nodemap ID for the given nodemap.
431  *
432  * \param       m               seq file in proc fs
433  * \param       data            unused
434  * \retval      0               success
435  */
436 static int nodemap_id_seq_show(struct seq_file *m, void *data)
437 {
438         struct lu_nodemap *nodemap;
439
440         mutex_lock(&active_config_lock);
441         nodemap = nodemap_lookup(m->private);
442         mutex_unlock(&active_config_lock);
443         if (IS_ERR(nodemap)) {
444                 int rc = PTR_ERR(nodemap);
445                 CERROR("cannot find nodemap '%s': rc = %d\n",
446                         (char *)m->private, rc);
447                 return rc;
448         }
449
450         seq_printf(m, "%u\n", nodemap->nm_id);
451         nodemap_putref(nodemap);
452         return 0;
453 }
454 LPROC_SEQ_FOPS_RO(nodemap_id);
455
456 /**
457  * Reads and prints the root squash UID for the given nodemap.
458  *
459  * \param       m               seq file in proc fs
460  * \param       data            unused
461  * \retval      0               success
462  */
463 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
464 {
465         struct lu_nodemap *nodemap;
466
467         mutex_lock(&active_config_lock);
468         nodemap = nodemap_lookup(m->private);
469         mutex_unlock(&active_config_lock);
470         if (IS_ERR(nodemap)) {
471                 int rc = PTR_ERR(nodemap);
472                 CERROR("cannot find nodemap '%s': rc = %d\n",
473                         (char *)m->private, rc);
474                 return rc;
475         }
476
477         seq_printf(m, "%u\n", nodemap->nm_squash_uid);
478         nodemap_putref(nodemap);
479         return 0;
480 }
481
482 /**
483  * Reads and prints the root squash GID for the given nodemap.
484  *
485  * \param       m               seq file in proc fs
486  * \param       data            unused
487  * \retval      0               success
488  */
489 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
490 {
491         struct lu_nodemap *nodemap;
492
493         mutex_lock(&active_config_lock);
494         nodemap = nodemap_lookup(m->private);
495         mutex_unlock(&active_config_lock);
496         if (IS_ERR(nodemap)) {
497                 int rc = PTR_ERR(nodemap);
498                 CERROR("cannot find nodemap '%s': rc = %d\n",
499                         (char *)m->private, rc);
500                 return rc;
501         }
502
503         seq_printf(m, "%u\n", nodemap->nm_squash_gid);
504         nodemap_putref(nodemap);
505         return 0;
506 }
507
508 /**
509  * Reads and prints the squash PROJID for the given nodemap.
510  *
511  * \param       m               seq file in proc fs
512  * \param       data            unused
513  * \retval      0               success
514  */
515 static int nodemap_squash_projid_seq_show(struct seq_file *m, void *data)
516 {
517         struct lu_nodemap *nodemap;
518
519         mutex_lock(&active_config_lock);
520         nodemap = nodemap_lookup(m->private);
521         mutex_unlock(&active_config_lock);
522         if (IS_ERR(nodemap)) {
523                 int rc = PTR_ERR(nodemap);
524
525                 CERROR("cannot find nodemap '%s': rc = %d\n",
526                        (char *)m->private, rc);
527                 return rc;
528         }
529
530         seq_printf(m, "%u\n", nodemap->nm_squash_projid);
531         nodemap_putref(nodemap);
532         return 0;
533 }
534
535 /**
536  * Reads and prints the trusted flag for the given nodemap.
537  *
538  * \param       m               seq file in proc fs
539  * \param       data            unused
540  * \retval      0               success
541  */
542 static int nodemap_trusted_seq_show(struct seq_file *m, void *data)
543 {
544         struct lu_nodemap *nodemap;
545
546         mutex_lock(&active_config_lock);
547         nodemap = nodemap_lookup(m->private);
548         mutex_unlock(&active_config_lock);
549         if (IS_ERR(nodemap)) {
550                 int rc = PTR_ERR(nodemap);
551
552                 CERROR("cannot find nodemap '%s': rc = %d\n",
553                         (char *)m->private, rc);
554                 return rc;
555         }
556
557         seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
558         nodemap_putref(nodemap);
559         return 0;
560 }
561
562 /**
563  * Reads and prints the admin flag for the given nodemap.
564  *
565  * \param       m               seq file in proc fs
566  * \param       data            unused
567  * \retval      0               success
568  */
569 static int nodemap_admin_seq_show(struct seq_file *m, void *data)
570 {
571         struct lu_nodemap *nodemap;
572         int rc;
573
574         mutex_lock(&active_config_lock);
575         nodemap = nodemap_lookup(m->private);
576         mutex_unlock(&active_config_lock);
577         if (IS_ERR(nodemap)) {
578                 rc = PTR_ERR(nodemap);
579                 CERROR("cannot find nodemap '%s': rc = %d\n",
580                         (char *)m->private, rc);
581                 return rc;
582         }
583
584         seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
585         nodemap_putref(nodemap);
586         return 0;
587 }
588
589 /**
590  * Reads and prints the mapping mode for the given nodemap.
591  *
592  * \param       m               seq file in proc fs
593  * \param       data            unused
594  * \retval      0               success
595  */
596 static int nodemap_map_mode_seq_show(struct seq_file *m, void *data)
597 {
598         struct lu_nodemap *nodemap;
599         bool need_sep = false;
600         int rc;
601
602         mutex_lock(&active_config_lock);
603         nodemap = nodemap_lookup(m->private);
604         mutex_unlock(&active_config_lock);
605         if (IS_ERR(nodemap)) {
606                 rc = PTR_ERR(nodemap);
607                 CERROR("cannot find nodemap '%s': rc = %d\n",
608                         (char *)m->private, rc);
609                 return rc;
610         }
611
612         if (nodemap->nmf_map_mode == NODEMAP_MAP_ALL) {
613                 seq_puts(m, "all\n");
614         } else {
615                 if (nodemap->nmf_map_mode & NODEMAP_MAP_UID) {
616                         seq_puts(m, "uid");
617                         need_sep = true;
618                 }
619                 if (nodemap->nmf_map_mode & NODEMAP_MAP_GID) {
620                         seq_puts(m, need_sep ? ",gid" : "gid");
621                         need_sep = true;
622                 }
623                 if (nodemap->nmf_map_mode & NODEMAP_MAP_PROJID)
624                         seq_puts(m, need_sep ? ",projid" : "projid");
625                 seq_puts(m, "\n");
626         }
627
628         nodemap_putref(nodemap);
629         return 0;
630 }
631
632 /**
633  * Reads and prints the rbac for the given nodemap.
634  *
635  * \param       m               seq file in proc fs
636  * \param       data            unused
637  * \retval      0               success
638  */
639 static int nodemap_rbac_seq_show(struct seq_file *m, void *data)
640 {
641         struct lu_nodemap *nodemap;
642         char *sep = "";
643         int i, rc;
644
645         mutex_lock(&active_config_lock);
646         nodemap = nodemap_lookup(m->private);
647         mutex_unlock(&active_config_lock);
648         if (IS_ERR(nodemap)) {
649                 rc = PTR_ERR(nodemap);
650                 CERROR("cannot find nodemap '%s': rc = %d\n",
651                        (char *)m->private, rc);
652                 return rc;
653         }
654
655         if (nodemap->nmf_rbac == NODEMAP_RBAC_ALL) {
656                 for (i = 0; i < ARRAY_SIZE(nodemap_rbac_names); i++)
657                         seq_printf(m, "%s%s", i == 0 ? "" : ",",
658                                    nodemap_rbac_names[i].nrn_name);
659                 seq_puts(m, "\n");
660         } else if (nodemap->nmf_rbac == NODEMAP_RBAC_NONE) {
661                 seq_puts(m, "none\n");
662         } else {
663                 for (i = 0; i < ARRAY_SIZE(nodemap_rbac_names); i++) {
664                         if (nodemap->nmf_rbac &
665                             nodemap_rbac_names[i].nrn_mode) {
666                                 seq_printf(m, "%s%s", sep,
667                                            nodemap_rbac_names[i].nrn_name);
668                                 sep = ",";
669                         }
670                 }
671                 seq_puts(m, "\n");
672         }
673
674         nodemap_putref(nodemap);
675         return 0;
676 }
677
678 /**
679  * Reads and prints the deny_unknown flag for the given nodemap.
680  *
681  * \param       m               seq file in proc fs
682  * \param       data            unused
683  * \retval      0               success
684  */
685 static int nodemap_deny_unknown_seq_show(struct seq_file *m, void *data)
686 {
687         struct lu_nodemap *nodemap;
688         int rc;
689
690         mutex_lock(&active_config_lock);
691         nodemap = nodemap_lookup(m->private);
692         mutex_unlock(&active_config_lock);
693         if (IS_ERR(nodemap)) {
694                 rc = PTR_ERR(nodemap);
695                 CERROR("cannot find nodemap '%s': rc = %d\n",
696                         (char *)m->private, rc);
697                 return rc;
698         }
699
700         seq_printf(m, "%d\n", (int)nodemap->nmf_deny_unknown);
701         nodemap_putref(nodemap);
702         return 0;
703 }
704
705 /**
706  * Reads and prints the audit_mode flag for the given nodemap.
707  *
708  * \param       m               seq file in proc fs
709  * \param       data            unused
710  * \retval      0               success
711  */
712 static int nodemap_audit_mode_seq_show(struct seq_file *m, void *data)
713 {
714         struct lu_nodemap *nodemap;
715         int rc;
716
717         mutex_lock(&active_config_lock);
718         nodemap = nodemap_lookup(m->private);
719         mutex_unlock(&active_config_lock);
720         if (IS_ERR(nodemap)) {
721                 rc = PTR_ERR(nodemap);
722                 CERROR("cannot find nodemap '%s': rc = %d\n",
723                        (char *)m->private, rc);
724                 return rc;
725         }
726
727         seq_printf(m, "%d\n", (int)nodemap->nmf_enable_audit);
728         nodemap_putref(nodemap);
729         return 0;
730 }
731
732 /**
733  * Reads and prints the forbid_encryption flag for the given nodemap.
734  *
735  * \param       m               seq file in proc fs
736  * \param       data            unused
737  * \retval      0               success
738  */
739 static int nodemap_forbid_encryption_seq_show(struct seq_file *m, void *data)
740 {
741         struct lu_nodemap *nodemap;
742         int rc;
743
744         mutex_lock(&active_config_lock);
745         nodemap = nodemap_lookup(m->private);
746         mutex_unlock(&active_config_lock);
747         if (IS_ERR(nodemap)) {
748                 rc = PTR_ERR(nodemap);
749                 CERROR("cannot find nodemap '%s': rc = %d\n",
750                        (char *)m->private, rc);
751                 return rc;
752         }
753
754         seq_printf(m, "%d\n", (int)nodemap->nmf_forbid_encryption);
755         nodemap_putref(nodemap);
756         return 0;
757 }
758
759 /**
760  * Reads and prints the readonly_mount flag for the given nodemap.
761  *
762  * \param       m               seq file in proc fs
763  * \param       data            unused
764  * \retval      0               success
765  */
766 static int nodemap_readonly_mount_seq_show(struct seq_file *m, void *data)
767 {
768         struct lu_nodemap *nodemap;
769         int rc;
770
771         mutex_lock(&active_config_lock);
772         nodemap = nodemap_lookup(m->private);
773         mutex_unlock(&active_config_lock);
774         if (IS_ERR(nodemap)) {
775                 rc = PTR_ERR(nodemap);
776                 CERROR("cannot find nodemap '%s': rc = %d\n",
777                        (char *)m->private, rc);
778                 return rc;
779         }
780
781         seq_printf(m, "%d\n", (int)nodemap->nmf_readonly_mount);
782         nodemap_putref(nodemap);
783         return 0;
784 }
785
786 static struct lprocfs_vars lprocfs_nm_module_vars[] = {
787         {
788                 .name           = "active",
789                 .fops           = &nodemap_active_fops,
790         },
791         {
792                 NULL
793         }
794 };
795
796 LPROC_SEQ_FOPS_RO(nodemap_trusted);
797 LPROC_SEQ_FOPS_RO(nodemap_admin);
798 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
799 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
800 LPROC_SEQ_FOPS_RO(nodemap_squash_projid);
801
802 LPROC_SEQ_FOPS_RO(nodemap_deny_unknown);
803 LPROC_SEQ_FOPS_RO(nodemap_map_mode);
804 LPROC_SEQ_FOPS_RO(nodemap_rbac);
805 LPROC_SEQ_FOPS_RO(nodemap_audit_mode);
806 LPROC_SEQ_FOPS_RO(nodemap_forbid_encryption);
807 LPROC_SEQ_FOPS_RO(nodemap_readonly_mount);
808
809 static const struct proc_ops nodemap_ranges_fops = {
810         .proc_open              = nodemap_ranges_open,
811         .proc_read              = seq_read,
812         .proc_lseek             = seq_lseek,
813         .proc_release           = single_release
814 };
815
816 static const struct proc_ops nodemap_idmap_fops = {
817         .proc_open              = nodemap_idmap_open,
818         .proc_read              = seq_read,
819         .proc_lseek             = seq_lseek,
820         .proc_release           = single_release
821 };
822
823 static const struct proc_ops nodemap_exports_fops = {
824         .proc_open              = nodemap_exports_open,
825         .proc_read              = seq_read,
826         .proc_lseek             = seq_lseek,
827         .proc_release           = single_release
828 };
829
830 static struct lprocfs_vars lprocfs_nodemap_vars[] = {
831         /* in alphabetical order */
832         {
833                 .name           = "admin_nodemap",
834                 .fops           = &nodemap_admin_fops,
835         },
836         {
837                 .name           = "audit_mode",
838                 .fops           = &nodemap_audit_mode_fops,
839         },
840         {
841                 .name           = "deny_unknown",
842                 .fops           = &nodemap_deny_unknown_fops,
843         },
844         {
845                 .name           = "exports",
846                 .fops           = &nodemap_exports_fops,
847         },
848         {
849                 .name           = "fileset",
850                 .fops           = &nodemap_fileset_fops,
851         },
852         {
853                 .name           = "forbid_encryption",
854                 .fops           = &nodemap_forbid_encryption_fops,
855         },
856         {
857                 .name           = "id",
858                 .fops           = &nodemap_id_fops,
859         },
860         {
861                 .name           = "idmap",
862                 .fops           = &nodemap_idmap_fops,
863         },
864         {
865                 .name           = "map_mode",
866                 .fops           = &nodemap_map_mode_fops,
867         },
868         {
869                 .name           = "ranges",
870                 .fops           = &nodemap_ranges_fops,
871         },
872         {
873                 .name           = "rbac",
874                 .fops           = &nodemap_rbac_fops,
875         },
876         {
877                 .name           = "readonly_mount",
878                 .fops           = &nodemap_readonly_mount_fops,
879         },
880         {
881                 .name           = "sepol",
882                 .fops           = &nodemap_sepol_fops,
883         },
884         {
885                 .name           = "squash_gid",
886                 .fops           = &nodemap_squash_gid_fops,
887         },
888         {
889                 .name           = "squash_projid",
890                 .fops           = &nodemap_squash_projid_fops,
891         },
892         {
893                 .name           = "squash_uid",
894                 .fops           = &nodemap_squash_uid_fops,
895         },
896         {
897                 .name           = "trusted_nodemap",
898                 .fops           = &nodemap_trusted_fops,
899         },
900         {
901                 NULL
902         }
903 };
904
905 static struct lprocfs_vars lprocfs_default_nodemap_vars[] = {
906         /* in alphabetical order */
907         {
908                 .name           = "admin_nodemap",
909                 .fops           = &nodemap_admin_fops,
910         },
911         {
912                 .name           = "audit_mode",
913                 .fops           = &nodemap_audit_mode_fops,
914         },
915         {
916                 .name           = "deny_unknown",
917                 .fops           = &nodemap_deny_unknown_fops,
918         },
919         {
920                 .name           = "exports",
921                 .fops           = &nodemap_exports_fops,
922         },
923         {
924                 .name           = "fileset",
925                 .fops           = &nodemap_fileset_fops,
926         },
927         {
928                 .name           = "forbid_encryption",
929                 .fops           = &nodemap_forbid_encryption_fops,
930         },
931         {
932                 .name           = "id",
933                 .fops           = &nodemap_id_fops,
934         },
935         {
936                 .name           = "map_mode",
937                 .fops           = &nodemap_map_mode_fops,
938         },
939         {
940                 .name           = "readonly_mount",
941                 .fops           = &nodemap_readonly_mount_fops,
942         },
943         {
944                 .name           = "squash_gid",
945                 .fops           = &nodemap_squash_gid_fops,
946         },
947         {
948                 .name           = "squash_projid",
949                 .fops           = &nodemap_squash_projid_fops,
950         },
951         {
952                 .name           = "squash_uid",
953                 .fops           = &nodemap_squash_uid_fops,
954         },
955         {
956                 .name           = "trusted_nodemap",
957                 .fops           = &nodemap_trusted_fops,
958         },
959         {
960                 NULL
961         }
962 };
963
964 /**
965  * Initialize the nodemap procfs directory.
966  *
967  * \retval      0               success
968  */
969 int nodemap_procfs_init(void)
970 {
971         int rc = 0;
972
973         proc_lustre_nodemap_root = lprocfs_register(LUSTRE_NODEMAP_NAME,
974                                                     proc_lustre_root,
975                                                     lprocfs_nm_module_vars,
976                                                     NULL);
977         if (IS_ERR(proc_lustre_nodemap_root)) {
978                 rc = PTR_ERR(proc_lustre_nodemap_root);
979                 CERROR("cannot create 'nodemap' directory: rc = %d\n",
980                        rc);
981                 proc_lustre_nodemap_root = NULL;
982         }
983         return rc;
984 }
985
986 /**
987  * Cleanup nodemap proc entry data structures.
988  */
989 void nodemap_procfs_exit(void)
990 {
991         struct nodemap_pde *nm_pde;
992         struct nodemap_pde *tmp;
993
994         lprocfs_remove(&proc_lustre_nodemap_root);
995         list_for_each_entry_safe(nm_pde, tmp, &nodemap_pde_list,
996                                  npe_list_member) {
997                 list_del(&nm_pde->npe_list_member);
998                 OBD_FREE_PTR(nm_pde);
999         }
1000 }
1001
1002 /**
1003  * Remove a nodemap's procfs entry and related data.
1004  */
1005 void lprocfs_nodemap_remove(struct nodemap_pde *nm_pde)
1006 {
1007         lprocfs_remove(&nm_pde->npe_proc_entry);
1008         list_del(&nm_pde->npe_list_member);
1009         OBD_FREE_PTR(nm_pde);
1010 }
1011
1012 /**
1013  * Register the proc directory for a nodemap
1014  *
1015  * \param       nodemap         nodemap to make the proc dir for
1016  * \param       is_default:     1 if default nodemap
1017  * \retval      0               success
1018  */
1019 int lprocfs_nodemap_register(struct lu_nodemap *nodemap, bool is_default)
1020 {
1021         struct nodemap_pde      *nm_entry;
1022         int                      rc = 0;
1023
1024         OBD_ALLOC_PTR(nm_entry);
1025         if (nm_entry == NULL)
1026                 GOTO(out, rc = -ENOMEM);
1027
1028         nm_entry->npe_proc_entry = proc_mkdir(nodemap->nm_name,
1029                                               proc_lustre_nodemap_root);
1030         if (IS_ERR(nm_entry->npe_proc_entry))
1031                 GOTO(out, rc = PTR_ERR(nm_entry->npe_proc_entry));
1032
1033         snprintf(nm_entry->npe_name, sizeof(nm_entry->npe_name), "%s",
1034                  nodemap->nm_name);
1035
1036         /* Use the nodemap name as stored on the PDE as the private data. This
1037          * is so a nodemap struct can be replaced without updating the proc
1038          * entries.
1039          */
1040         rc = lprocfs_add_vars(nm_entry->npe_proc_entry,
1041                               (is_default ? lprocfs_default_nodemap_vars :
1042                                             lprocfs_nodemap_vars),
1043                               nm_entry->npe_name);
1044         if (rc != 0)
1045                 lprocfs_remove(&nm_entry->npe_proc_entry);
1046         else
1047                 list_add(&nm_entry->npe_list_member, &nodemap_pde_list);
1048
1049 out:
1050         if (rc != 0) {
1051                 CERROR("cannot create 'nodemap/%s': rc = %d\n",
1052                        nodemap->nm_name, rc);
1053                 if (nm_entry != NULL) {
1054                         OBD_FREE_PTR(nm_entry);
1055                         nm_entry = NULL;
1056                 }
1057         }
1058
1059         nodemap->nm_pde_data = nm_entry;
1060
1061         return rc;
1062 }