Whamcloud - gitweb
305f95bccbf2e0499de9ce62979a4942cbb4fbe8
[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, 2015, 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         read_lock(&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         read_unlock(&nodemap->nm_idmap_lock);
91         seq_printf(m, "\n");
92         seq_printf(m, "]\n");
93
94         nodemap_putref(nodemap);
95         return 0;
96 }
97
98 /**
99  * Attaches nodemap_idmap_show to proc file.
100  *
101  * \param       inode           inode of seq file in proc fs
102  * \param       file            seq file
103  * \retval      0               success
104  */
105 static int nodemap_idmap_open(struct inode *inode, struct file *file)
106 {
107         return single_open(file, nodemap_idmap_show, PDE_DATA(inode));
108 }
109
110 /**
111  * Reads and prints the NID ranges for the given nodemap.
112  *
113  * \param       m               seq file in proc fs
114  * \param       data            unused
115  * \retval      0               success
116  */
117 static int nodemap_ranges_show(struct seq_file *m, void *data)
118 {
119         struct lu_nodemap               *nodemap;
120         struct lu_nid_range             *range;
121         struct interval_node_extent     ext;
122         char                            start_nidstr[LNET_NIDSTR_SIZE];
123         char                            end_nidstr[LNET_NIDSTR_SIZE];
124         bool                            cont = false;
125         int rc;
126
127         mutex_lock(&active_config_lock);
128         nodemap = nodemap_lookup(m->private);
129         if (IS_ERR(nodemap)) {
130                 mutex_unlock(&active_config_lock);
131                 rc = PTR_ERR(nodemap);
132                 CERROR("cannot find nodemap '%s': rc = %d\n",
133                         (char *)m->private, rc);
134                 return rc;
135         }
136
137         seq_printf(m, "[\n");
138         down_read(&active_config->nmc_range_tree_lock);
139         list_for_each_entry(range, &nodemap->nm_ranges, rn_list) {
140                 if (cont)
141                         seq_printf(m, ",\n");
142                 cont = 1;
143                 ext = range->rn_node.in_extent;
144                 libcfs_nid2str_r(ext.start, start_nidstr, sizeof(start_nidstr));
145                 libcfs_nid2str_r(ext.end, end_nidstr, sizeof(end_nidstr));
146                 seq_printf(m, " { id: %u, start_nid: %s, end_nid: %s }",
147                            range->rn_id, start_nidstr, end_nidstr);
148         }
149         up_read(&active_config->nmc_range_tree_lock);
150         mutex_unlock(&active_config_lock);
151         seq_printf(m, "\n");
152         seq_printf(m, "]\n");
153
154         nodemap_putref(nodemap);
155         return 0;
156 }
157
158 /**
159  * Connects nodemap_idmap_show to proc file.
160  *
161  * \param       inode           inode of seq file in proc fs
162  * \param       file            seq file
163  * \retval      0               success
164  */
165 static int nodemap_ranges_open(struct inode *inode, struct file *file)
166 {
167         return single_open(file, nodemap_ranges_show, PDE_DATA(inode));
168 }
169
170 /**
171  * Reads and prints the fileset for the given nodemap.
172  *
173  * \param       m               seq file in proc fs
174  * \param       data            unused
175  * \retval      0               success
176  */
177 static int nodemap_fileset_seq_show(struct seq_file *m, void *data)
178 {
179         struct lu_nodemap *nodemap;
180         int rc = 0;
181
182         mutex_lock(&active_config_lock);
183         nodemap = nodemap_lookup(m->private);
184         mutex_unlock(&active_config_lock);
185         if (IS_ERR(nodemap)) {
186                 rc = PTR_ERR(nodemap);
187                 CERROR("cannot find nodemap '%s': rc = %d\n",
188                         (char *)m->private, rc);
189                 return rc;
190         }
191
192         seq_printf(m, "%s\n", nodemap->nm_fileset);
193         nodemap_putref(nodemap);
194         return rc;
195 }
196
197 /**
198  * Set a fileset on a nodemap.
199  *
200  * \param[in] file      proc file
201  * \param[in] buffer    string, "<fileset>"
202  * \param[in] count     \a buffer length
203  * \param[in] off       unused
204  * \retval              \a count on success
205  * \retval              negative number on error
206  */
207 static ssize_t
208 nodemap_fileset_seq_write(struct file *file,
209                                       const char __user *buffer,
210                                       size_t count, loff_t *off)
211 {
212         struct seq_file *m = file->private_data;
213         char *nm_fileset;
214         int rc = 0;
215         ENTRY;
216
217         if (count == 0)
218                 RETURN(0);
219
220         if (count > PATH_MAX)
221                 RETURN(-EINVAL);
222
223         OBD_ALLOC(nm_fileset, count);
224         if (nm_fileset == NULL)
225                 RETURN(-ENOMEM);
226
227         if (copy_from_user(nm_fileset, buffer, count))
228                 GOTO(out, rc = -EFAULT);
229
230         rc = nodemap_set_fileset(m->private, nm_fileset);
231         if (rc != 0)
232                 GOTO(out, rc = -EINVAL);
233
234         rc = count;
235 out:
236         OBD_FREE(nm_fileset, count);
237
238         return rc;
239 }
240 LPROC_SEQ_FOPS(nodemap_fileset);
241
242 /**
243  * Reads and prints the exports attached to the given nodemap.
244  *
245  * \param       m               seq file in proc fs, stores nodemap
246  * \param       data            unused
247  * \retval      0               success
248  */
249 static int nodemap_exports_show(struct seq_file *m, void *data)
250 {
251         struct lu_nodemap *nodemap;
252         struct obd_export *exp;
253         char nidstr[LNET_NIDSTR_SIZE] = "<unknown>";
254         int rc;
255
256         mutex_lock(&active_config_lock);
257         nodemap = nodemap_lookup(m->private);
258         mutex_unlock(&active_config_lock);
259         if (IS_ERR(nodemap)) {
260                 rc = PTR_ERR(nodemap);
261                 CERROR("cannot find nodemap '%s': rc = %d\n",
262                         (char *)m->private, rc);
263                 return rc;
264         }
265
266         seq_printf(m, "[\n");
267
268         mutex_lock(&nodemap->nm_member_list_lock);
269         list_for_each_entry(exp, &nodemap->nm_member_list,
270                             exp_target_data.ted_nodemap_member) {
271                 if (exp->exp_connection != NULL)
272                         libcfs_nid2str_r(exp->exp_connection->c_peer.nid,
273                                          nidstr, sizeof(nidstr));
274
275                 seq_printf(m, " { nid: %s, uuid: %s },",
276                            nidstr, exp->exp_client_uuid.uuid);
277         }
278         mutex_unlock(&nodemap->nm_member_list_lock);
279
280         seq_printf(m, "\n");
281         seq_printf(m, "]\n");
282
283         nodemap_putref(nodemap);
284         return 0;
285 }
286
287 /**
288  * Attaches nodemap_idmap_show to proc file.
289  *
290  * \param       inode           inode of seq file in proc fs
291  * \param       file            seq file
292  * \retval      0               success
293  */
294 static int nodemap_exports_open(struct inode *inode, struct file *file)
295 {
296         return single_open(file, nodemap_exports_show, PDE_DATA(inode));
297 }
298
299 /**
300  * Reads and prints the active flag for the given nodemap.
301  *
302  * \param       m               seq file in proc fs
303  * \param       data            unused
304  * \retval      0               success
305  */
306 static int nodemap_active_seq_show(struct seq_file *m, void *data)
307 {
308         seq_printf(m, "%u\n", (unsigned int)nodemap_active);
309         return 0;
310 }
311
312 /**
313  * Activate/deactivate nodemap.
314  *
315  * \param[in] file      proc file
316  * \param[in] buffer    string, "1" or "0" to activate/deactivate nodemap
317  * \param[in] count     \a buffer length
318  * \param[in] off       unused
319  * \retval              \a count on success
320  * \retval              negative number on error
321  */
322 static ssize_t
323 nodemap_active_seq_write(struct file *file, const char __user *buffer,
324                          size_t count, loff_t *off)
325 {
326         char                    active_string[NODEMAP_LPROC_FLAG_LEN + 1];
327         long unsigned int       active;
328         int                     rc;
329
330         if (count == 0)
331                 return 0;
332
333         if (count >= sizeof(active_string))
334                 return -EINVAL;
335
336         if (copy_from_user(active_string, buffer, count))
337                 return -EFAULT;
338
339         active_string[count] = '\0';
340         rc = kstrtoul(active_string, 10, &active);
341         if (rc != 0)
342                 return -EINVAL;
343
344         nodemap_activate(active);
345
346         return count;
347 }
348 LPROC_SEQ_FOPS(nodemap_active);
349
350 /**
351  * Reads and prints the nodemap ID for the given nodemap.
352  *
353  * \param       m               seq file in proc fs
354  * \param       data            unused
355  * \retval      0               success
356  */
357 static int nodemap_id_seq_show(struct seq_file *m, void *data)
358 {
359         struct lu_nodemap *nodemap;
360
361         mutex_lock(&active_config_lock);
362         nodemap = nodemap_lookup(m->private);
363         mutex_unlock(&active_config_lock);
364         if (IS_ERR(nodemap)) {
365                 int rc = PTR_ERR(nodemap);
366                 CERROR("cannot find nodemap '%s': rc = %d\n",
367                         (char *)m->private, rc);
368                 return rc;
369         }
370
371         seq_printf(m, "%u\n", nodemap->nm_id);
372         nodemap_putref(nodemap);
373         return 0;
374 }
375 LPROC_SEQ_FOPS_RO(nodemap_id);
376
377 /**
378  * Reads and prints the root squash UID for the given nodemap.
379  *
380  * \param       m               seq file in proc fs
381  * \param       data            unused
382  * \retval      0               success
383  */
384 static int nodemap_squash_uid_seq_show(struct seq_file *m, void *data)
385 {
386         struct lu_nodemap *nodemap;
387
388         mutex_lock(&active_config_lock);
389         nodemap = nodemap_lookup(m->private);
390         mutex_unlock(&active_config_lock);
391         if (IS_ERR(nodemap)) {
392                 int rc = PTR_ERR(nodemap);
393                 CERROR("cannot find nodemap '%s': rc = %d\n",
394                         (char *)m->private, rc);
395                 return rc;
396         }
397
398         seq_printf(m, "%u\n", nodemap->nm_squash_uid);
399         nodemap_putref(nodemap);
400         return 0;
401 }
402
403 /**
404  * Reads and prints the root squash GID for the given nodemap.
405  *
406  * \param       m               seq file in proc fs
407  * \param       data            unused
408  * \retval      0               success
409  */
410 static int nodemap_squash_gid_seq_show(struct seq_file *m, void *data)
411 {
412         struct lu_nodemap *nodemap;
413
414         mutex_lock(&active_config_lock);
415         nodemap = nodemap_lookup(m->private);
416         mutex_unlock(&active_config_lock);
417         if (IS_ERR(nodemap)) {
418                 int rc = PTR_ERR(nodemap);
419                 CERROR("cannot find nodemap '%s': rc = %d\n",
420                         (char *)m->private, rc);
421                 return rc;
422         }
423
424         seq_printf(m, "%u\n", nodemap->nm_squash_gid);
425         nodemap_putref(nodemap);
426         return 0;
427 }
428
429 /**
430  * Reads and prints the trusted flag 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_trusted_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
446                 CERROR("cannot find nodemap '%s': rc = %d\n",
447                         (char *)m->private, rc);
448                 return rc;
449         }
450
451         seq_printf(m, "%d\n", (int)nodemap->nmf_trust_client_ids);
452         nodemap_putref(nodemap);
453         return 0;
454 }
455
456 /**
457  * Reads and prints the admin flag 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_admin_seq_show(struct seq_file *m, void *data)
464 {
465         struct lu_nodemap *nodemap;
466         int rc;
467
468         mutex_lock(&active_config_lock);
469         nodemap = nodemap_lookup(m->private);
470         mutex_unlock(&active_config_lock);
471         if (IS_ERR(nodemap)) {
472                 rc = PTR_ERR(nodemap);
473                 CERROR("cannot find nodemap '%s': rc = %d\n",
474                         (char *)m->private, rc);
475                 return rc;
476         }
477
478         seq_printf(m, "%d\n", (int)nodemap->nmf_allow_root_access);
479         nodemap_putref(nodemap);
480         return 0;
481 }
482
483 /**
484  * Reads and prints the deny_unknown flag for the given nodemap.
485  *
486  * \param       m               seq file in proc fs
487  * \param       data            unused
488  * \retval      0               success
489  */
490 static int nodemap_deny_unknown_seq_show(struct seq_file *m, void *data)
491 {
492         struct lu_nodemap *nodemap;
493         int rc;
494
495         mutex_lock(&active_config_lock);
496         nodemap = nodemap_lookup(m->private);
497         mutex_unlock(&active_config_lock);
498         if (IS_ERR(nodemap)) {
499                 rc = PTR_ERR(nodemap);
500                 CERROR("cannot find nodemap '%s': rc = %d\n",
501                         (char *)m->private, rc);
502                 return rc;
503         }
504
505         seq_printf(m, "%d\n", (int)nodemap->nmf_deny_unknown);
506         nodemap_putref(nodemap);
507         return 0;
508 }
509
510 #ifdef NODEMAP_PROC_DEBUG
511 /**
512  * Helper functions to set nodemap flags.
513  *
514  * \param[in] buffer    string, which is "1" or "0" to set/unset flag
515  * \param[in] count     \a buffer length
516  * \param[out] flag_p   where to store flag value
517  * \retval              \a count on success
518  * \retval              negative number on error
519  */
520 static int nodemap_proc_read_flag(const char __user *buffer,
521                                   unsigned long count, unsigned int *flag_p)
522 {
523         char                    scratch[NODEMAP_LPROC_FLAG_LEN + 1];
524         long unsigned int       flag_buf;
525         int                     rc;
526
527         if (count == 0)
528                 return 0;
529
530         if (count >= sizeof(scratch))
531                 return -EINVAL;
532
533         if (copy_from_user(scratch, buffer, count))
534                 return -EFAULT;
535
536         scratch[count] = '\0';
537         rc = kstrtoul(scratch, 10, &flag_buf);
538         if (rc != 0)
539                 return -EINVAL;
540
541         *flag_p = flag_buf;
542
543         return count;
544 }
545
546 /**
547  * Set the squash UID.
548  *
549  * \param[in] file      proc file
550  * \param[in] buffer    string representing squash UID to set
551  * \param[in] count     \a buffer length
552  * \param[in] off       unused
553  * \retval              \a count on success
554  * \retval              negative number on error
555  */
556 static ssize_t
557 nodemap_squash_uid_seq_write(struct file *file, const char __user *buffer,
558                              size_t count, loff_t *off)
559 {
560         char                     squash[NODEMAP_LPROC_ID_LEN + 1];
561         struct seq_file         *m = file->private_data;
562         long unsigned int        squash_uid;
563         int                      rc;
564
565         if (count == 0)
566                 return 0;
567
568         if (count >= sizeof(squash))
569                 return -EINVAL;
570
571         if (copy_from_user(squash, buffer, count))
572                 return -EFAULT;
573
574         squash[count] = '\0';
575         rc = kstrtoul(squash, 10, &squash_uid);
576         if (rc != 0)
577                 return -EINVAL;
578
579         rc = nodemap_set_squash_uid(m->private, squash_uid);
580         if (rc != 0)
581                 return rc;
582
583         return count;
584 }
585
586 /**
587  * Set the squash GID.
588  *
589  * \param[in] file      proc file
590  * \param[in] buffer    string representing squash GID to set
591  * \param[in] count     \a buffer length
592  * \param[in] off       unused
593  * \retval              \a count on success
594  * \retval              negative number on error
595  */
596 static ssize_t
597 nodemap_squash_gid_seq_write(struct file *file, const char __user *buffer,
598                              size_t count, loff_t *off)
599 {
600         char                     squash[NODEMAP_LPROC_ID_LEN + 1];
601         struct seq_file         *m = file->private_data;
602         long unsigned int        squash_gid;
603         int                      rc;
604
605         if (count == 0)
606                 return 0;
607
608         if (count >= sizeof(squash))
609                 return -EINVAL;
610
611         if (copy_from_user(squash, buffer, count))
612                 return -EFAULT;
613
614         squash[count] = '\0';
615         rc = kstrtoul(squash, 10, &squash_gid);
616         if (rc != 0)
617                 return -EINVAL;
618
619         rc = nodemap_set_squash_gid(m->private, squash_gid);
620         if (rc != 0)
621                 return rc;
622
623         return count;
624 }
625
626 /**
627  * Set/unset the trusted flag.
628  *
629  * \param[in] file      proc file
630  * \param[in] buffer    string, "1" or "0"
631  * \param[in] count     \a buffer length
632  * \param[in] off       unused
633  * \retval              \a count on success
634  * \retval              negative number on error
635  */
636 static ssize_t
637 nodemap_trusted_seq_write(struct file *file, const char __user *buffer,
638                           size_t count, loff_t *off)
639 {
640         struct seq_file         *m = file->private_data;
641         int                     flags;
642         int                     rc;
643
644         rc = nodemap_proc_read_flag(buffer, count, &flags);
645         if (rc < 0)
646                 return rc;
647
648         rc = nodemap_set_trust_client_ids(m->private, flags);
649         if (rc != 0)
650                 return rc;
651
652         return count;
653 }
654
655 /**
656  * Set/unset the admin flag.
657  *
658  * \param[in] file      proc file
659  * \param[in] buffer    string, "1" or "0"
660  * \param[in] count     \a buffer length
661  * \param[in] off       unused
662  * \retval              \a count on success
663  * \retval              negative number on error
664  */
665 static ssize_t
666 nodemap_admin_seq_write(struct file *file, const char __user *buffer,
667                         size_t count, loff_t *off)
668 {
669         struct seq_file         *m = file->private_data;
670         int                     flags;
671         int                     rc;
672
673         rc = nodemap_proc_read_flag(buffer, count, &flags);
674         if (rc < 0)
675                 return rc;
676
677         rc = nodemap_set_allow_root(m->private, flags);
678         if (rc != 0)
679                 return rc;
680
681         return count;
682 }
683
684 /**
685  * Add a nodemap.
686  *
687  * \param[in] file      proc file
688  * \param[in] buffer    string, name of the nodemap to add
689  * \param[in] count     \a buffer length
690  * \param[in] off       unused
691  * \retval              \a count on success
692  * \retval              negative number on error
693  */
694 static ssize_t
695 lprocfs_add_nodemap_seq_write(struct file *file, const char __user *buffer,
696                               size_t count, loff_t *off)
697 {
698         char    nodemap_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
699         char    *cpybuf = NULL;
700         char    *pos;
701         int     rc;
702
703         if (count == 0)
704                 return 0;
705
706         if (count >= sizeof(nodemap_name))
707                 return -EINVAL;
708
709         if (copy_from_user(nodemap_name, buffer, count))
710                 return -EFAULT;
711
712         nodemap_name[count] = '\0';
713
714         cpybuf = nodemap_name;
715         pos = strsep(&cpybuf, " \n");
716         if (pos == NULL)
717                 return -EINVAL;
718
719         rc = nodemap_add(nodemap_name);
720         if (rc == 0)
721                 rc = count;
722
723         return rc;
724 }
725 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap);
726
727 /**
728  * Delete a nodemap.
729  *
730  * \param[in] file      proc file
731  * \param[in] buffer    string, name of the nodemap to delete
732  * \param[in] count     \a buffer length
733  * \param[in] off       unused
734  * \retval              \a count on success
735  * \retval              negative number on error
736  */
737 static ssize_t
738 lprocfs_del_nodemap_seq_write(struct file *file, const char __user *buffer,
739                               size_t count, loff_t *off)
740 {
741         char    nodemap_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
742         char    *cpybuf = NULL;
743         char    *pos;
744         int     rc = count;
745
746         if (count == 0)
747                 return 0;
748
749         if (count >= sizeof(nodemap_name))
750                 return -EINVAL;
751
752         if (copy_from_user(nodemap_name, buffer, count))
753                 return -EFAULT;
754
755         nodemap_name[count] = '\0';
756
757         cpybuf = nodemap_name;
758         pos = strsep(&cpybuf, " \n");
759         if (pos == NULL)
760                 return -EINVAL;
761
762         rc = nodemap_del(nodemap_name);
763         if (rc == 0)
764                 rc = count;
765
766         return rc;
767
768 }
769 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap);
770
771 /**
772  * Helper function to parse a NID string.
773  *
774  * \param[in] rangestr  string representation of NIDs, see libcfs_str2nid()
775  * \param[out] nids     array of two nids
776  * \retval              0 on success
777  * \retval              negative number on error
778  */
779 static int parse_nids(char *rangestr, lnet_nid_t nids[2])
780 {
781         struct list_head        nidlist;
782         char                    nidstr[2][LNET_NIDSTR_SIZE];
783         char                    nidrange_str[2 * LNET_NIDSTR_SIZE + 2];
784         int                     rc = 0;
785
786         INIT_LIST_HEAD(&nidlist);
787
788         if (cfs_parse_nidlist(rangestr, strlen(rangestr),
789             &nidlist) <= 0)
790                 return -EINVAL;
791
792         if (!cfs_nidrange_is_contiguous(&nidlist))
793                 return -EINVAL;
794
795         cfs_nidrange_find_min_max(&nidlist, nidstr[0], nidstr[1],
796                                   LNET_NIDSTR_SIZE);
797         snprintf(nidrange_str, sizeof(nidrange_str), "%s:%s",
798                 nidstr[0], nidstr[1]);
799
800         rc = nodemap_parse_range(nidrange_str, nids);
801         if (rc != 0)
802                 return -EINVAL;
803
804         cfs_free_nidlist(&nidlist);
805
806         return 0;
807 }
808
809 /**
810  * Add a NID range to nodemap.
811  *
812  * \param[in] file      proc file
813  * \param[in] buffer    string, "<nodemap name> <nid range>"
814  * \param[in] count     \a buffer length
815  * \param[in] off       unused
816  * \retval              \a count on success
817  * \retval              negative number on error
818  */
819 static ssize_t
820 lprocfs_add_nodemap_range_seq_write(struct file *file,
821                                     const char __user *buffer,
822                                     size_t count, loff_t *off)
823 {
824         char                    name_range[LUSTRE_NODEMAP_NAME_LENGTH +
825                                            LNET_NIDSTR_SIZE * 2 + 2];
826         char                    *cpybuf = NULL;
827         char                    *name;
828         char                    *rangestr = NULL;
829         lnet_nid_t              nids[2];
830         int                     rc;
831
832         if (count == 0)
833                 return 0;
834
835         if (count >= sizeof(name_range))
836                 GOTO(out, rc = -EINVAL);
837
838         if (copy_from_user(name_range, buffer, count))
839                 GOTO(out, rc = -EFAULT);
840
841         name_range[count] = '\0';
842
843         cpybuf = name_range;
844         name = strsep(&cpybuf, " ");
845         if (name == NULL)
846                 GOTO(out, rc = -EINVAL);
847
848         rangestr = strsep(&cpybuf, " \n");
849         if (rangestr == NULL)
850                 GOTO(out, rc = -EINVAL);
851
852         rc = parse_nids(rangestr, nids);
853         if (rc != 0)
854                 GOTO(out, rc = rc);
855
856         rc = nodemap_add_range(name, nids);
857         if (rc != 0)
858                 GOTO(out, rc = -EINVAL);
859
860         if (rc == 0)
861                 rc = count;
862
863 out:
864         return rc;
865 }
866 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap_range);
867
868 /**
869  * Delete a NID range from nodemap.
870  *
871  * \param[in] file      proc file
872  * \param[in] buffer    string, "<nodemap name> <nid range>"
873  * \param[in] count     \a buffer length
874  * \param[in] off       unused
875  * \retval              \a count on success
876  * \retval              negative number on error
877  */
878 static ssize_t
879 lprocfs_del_nodemap_range_seq_write(struct file *file,
880                                     const char __user *buffer,
881                                     size_t count, loff_t *off)
882 {
883         char                    name_range[LUSTRE_NODEMAP_NAME_LENGTH +
884                                            LNET_NIDSTR_SIZE * 2 + 2];
885         char                    *cpybuf = NULL;
886         char                    *name;
887         char                    *rangestr = NULL;
888         lnet_nid_t              nids[2];
889         int                     rc;
890
891         if (count == 0)
892                 return 0;
893
894         if (count >= sizeof(name_range))
895                 GOTO(out, rc = -EINVAL);
896
897         if (copy_from_user(name_range, buffer, count))
898                 GOTO(out, rc = -EFAULT);
899
900         name_range[count] = '\0';
901
902         cpybuf = name_range;
903         name = strsep(&cpybuf, " ");
904         if (name == NULL)
905                 GOTO(out, rc = -EINVAL);
906
907         rangestr = strsep(&cpybuf, " \n");
908         if (rangestr == NULL)
909                 GOTO(out, rc = -EINVAL);
910
911         rc = parse_nids(rangestr, nids);
912         if (rc != 0)
913                 GOTO(out, rc = rc);
914
915         rc = nodemap_del_range(name, nids);
916         if (rc != 0)
917                 GOTO(out, rc = -EINVAL);
918
919         if (rc == 0)
920                 rc = count;
921
922 out:
923         return rc;
924 }
925 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap_range);
926
927 /**
928  * Add an idmap to nodemap.
929  *
930  * \param[in] file      proc file
931  * \param[in] buffer    string, "<nodemap name> <uid|gid> <idmap>"
932  * \param[in] count     \a buffer length
933  * \param[in] off       unused
934  * \retval              \a count on success
935  * \retval              negative number on error
936  */
937 static ssize_t
938 lprocfs_add_nodemap_idmap_seq_write(struct file *file,
939                                     const char __user *buffer,
940                                     size_t count, loff_t *off)
941 {
942         char                    name_idmapstr[LUSTRE_NODEMAP_NAME_LENGTH + 16];
943         char                    *cpybuf = NULL;
944         char                    *name;
945         char                    *idtypestr = NULL;
946         char                    *idmapstr = NULL;
947         __u32                   idmap[2];
948         int                     rc = count;
949
950         if (count == 0)
951                 return 0;
952
953         if (count >= sizeof(name_idmapstr))
954                 GOTO(out, rc = -EINVAL);
955
956         if (copy_from_user(name_idmapstr, buffer, count))
957                 GOTO(out, rc = -EFAULT);
958
959         name_idmapstr[count] = '\0';
960
961         cpybuf = name_idmapstr;
962         name = strsep(&cpybuf, " ");
963         if (name == NULL)
964                 GOTO(out, rc = -EINVAL);
965
966         idtypestr = strsep(&cpybuf, " ");
967         if (idtypestr == NULL)
968                 GOTO(out, rc = -EINVAL);
969
970         idmapstr = strsep(&cpybuf, " \n");
971         if (idmapstr == NULL)
972                 GOTO(out, rc = -EINVAL);
973
974         rc = nodemap_parse_idmap(idmapstr, idmap);
975         if (rc != 0)
976                 GOTO(out, rc = -EINVAL);
977
978         if (strcmp(idtypestr, "uid") == 0)
979                 rc = nodemap_add_idmap(name, NODEMAP_UID, idmap);
980         else if (strcmp(idtypestr, "gid") == 0)
981                 rc = nodemap_add_idmap(name, NODEMAP_GID, idmap);
982         else
983                 GOTO(out, rc = -EINVAL);
984
985         if (rc != 0)
986                 GOTO(out, rc = -EINVAL);
987
988         if (rc == 0)
989                 rc = count;
990
991 out:
992         return rc;
993 }
994 LPROC_SEQ_FOPS_WO_TYPE(nodemap, add_nodemap_idmap);
995
996 /**
997  * Delete an idmap from nodemap.
998  *
999  * \param[in] file      proc file
1000  * \param[in] buffer    string, "<nodemap name> <uid|gid> <idmap>"
1001  * \param[in] count     \a buffer length
1002  * \param[in] off       unused
1003  * \retval              \a count on success
1004  * \retval              negative number on error
1005  */
1006 static ssize_t
1007 lprocfs_del_nodemap_idmap_seq_write(struct file *file,
1008                                     const char __user *buffer,
1009                                     size_t count, loff_t *off)
1010 {
1011         char                    name_idmapstr[LUSTRE_NODEMAP_NAME_LENGTH + 16];
1012         char                    *cpybuf = NULL;
1013         char                    *name;
1014         char                    *idtypestr = NULL;
1015         char                    *idmapstr = NULL;
1016         __u32                   idmap[2];
1017         int                     rc = count;
1018
1019         if (count == 0)
1020                 return 0;
1021
1022         if (count >= sizeof(name_idmapstr))
1023                 GOTO(out, rc = -EINVAL);
1024
1025         if (copy_from_user(name_idmapstr, buffer, count))
1026                 GOTO(out, rc = -EFAULT);
1027
1028         name_idmapstr[count] = '\0';
1029
1030         cpybuf = name_idmapstr;
1031         name = strsep(&cpybuf, " ");
1032         if (name == NULL)
1033                 GOTO(out, rc = -EINVAL);
1034
1035         idtypestr = strsep(&cpybuf, " ");
1036         if (idtypestr == NULL)
1037                 GOTO(out, rc = -EINVAL);
1038
1039         idmapstr = strsep(&cpybuf, " \n");
1040         if (idmapstr == NULL)
1041                 GOTO(out, rc = -EINVAL);
1042
1043         rc = nodemap_parse_idmap(idmapstr, idmap);
1044         if (rc != 0)
1045                 GOTO(out, rc = -EINVAL);
1046
1047         if (strcmp(idtypestr, "uid") == 0)
1048                 rc = nodemap_del_idmap(name, NODEMAP_UID, idmap);
1049         else if (strcmp(idtypestr, "gid") == 0)
1050                 rc = nodemap_del_idmap(name, NODEMAP_GID, idmap);
1051         else
1052                 GOTO(out, rc = -EINVAL);
1053
1054         if (rc != 0)
1055                 GOTO(out, rc = -EINVAL);
1056
1057         if (rc == 0)
1058                 rc = count;
1059
1060 out:
1061         return rc;
1062 }
1063 LPROC_SEQ_FOPS_WO_TYPE(nodemap, del_nodemap_idmap);
1064 #endif /* NODEMAP_PROC_DEBUG */
1065
1066 static struct lprocfs_vars lprocfs_nm_module_vars[] = {
1067         {
1068                 .name           = "active",
1069                 .fops           = &nodemap_active_fops,
1070         },
1071 #ifdef NODEMAP_PROC_DEBUG
1072         {
1073                 .name           = "add_nodemap",
1074                 .fops           = &nodemap_add_nodemap_fops,
1075         },
1076         {
1077                 .name           = "remove_nodemap",
1078                 .fops           = &nodemap_del_nodemap_fops,
1079         },
1080         {
1081                 .name           = "add_nodemap_range",
1082                 .fops           = &nodemap_add_nodemap_range_fops,
1083         },
1084         {
1085                 .name           = "del_nodemap_range",
1086                 .fops           = &nodemap_del_nodemap_range_fops,
1087         },
1088         {
1089                 .name           = "add_nodemap_idmap",
1090                 .fops           = &nodemap_add_nodemap_idmap_fops,
1091         },
1092         {
1093                 .name           = "del_nodemap_idmap",
1094                 .fops           = &nodemap_del_nodemap_idmap_fops,
1095         },
1096 #endif /* NODEMAP_PROC_DEBUG */
1097         {
1098                 NULL
1099         }
1100 };
1101
1102 #ifdef NODEMAP_PROC_DEBUG
1103 LPROC_SEQ_FOPS(nodemap_trusted);
1104 LPROC_SEQ_FOPS(nodemap_admin);
1105 LPROC_SEQ_FOPS(nodemap_squash_uid);
1106 LPROC_SEQ_FOPS(nodemap_squash_gid);
1107 #else
1108 LPROC_SEQ_FOPS_RO(nodemap_trusted);
1109 LPROC_SEQ_FOPS_RO(nodemap_admin);
1110 LPROC_SEQ_FOPS_RO(nodemap_squash_uid);
1111 LPROC_SEQ_FOPS_RO(nodemap_squash_gid);
1112 #endif
1113
1114 LPROC_SEQ_FOPS_RO(nodemap_deny_unknown);
1115
1116 const struct file_operations nodemap_ranges_fops = {
1117         .open                   = nodemap_ranges_open,
1118         .read                   = seq_read,
1119         .llseek                 = seq_lseek,
1120         .release                = single_release
1121 };
1122
1123 const struct file_operations nodemap_idmap_fops = {
1124         .open                   = nodemap_idmap_open,
1125         .read                   = seq_read,
1126         .llseek                 = seq_lseek,
1127         .release                = single_release
1128 };
1129
1130 const struct file_operations nodemap_exports_fops = {
1131         .open                   = nodemap_exports_open,
1132         .read                   = seq_read,
1133         .llseek                 = seq_lseek,
1134         .release                = single_release
1135 };
1136
1137 static struct lprocfs_vars lprocfs_nodemap_vars[] = {
1138         {
1139                 .name           = "id",
1140                 .fops           = &nodemap_id_fops,
1141         },
1142         {
1143                 .name           = "trusted_nodemap",
1144                 .fops           = &nodemap_trusted_fops,
1145         },
1146         {
1147                 .name           = "admin_nodemap",
1148                 .fops           = &nodemap_admin_fops,
1149         },
1150         {
1151                 .name           = "deny_unknown",
1152                 .fops           = &nodemap_deny_unknown_fops,
1153         },
1154         {
1155                 .name           = "squash_uid",
1156                 .fops           = &nodemap_squash_uid_fops,
1157         },
1158         {
1159                 .name           = "squash_gid",
1160                 .fops           = &nodemap_squash_gid_fops,
1161         },
1162         {
1163                 .name           = "ranges",
1164                 .fops           = &nodemap_ranges_fops,
1165         },
1166         {
1167                 .name           = "fileset",
1168                 .fops           = &nodemap_fileset_fops,
1169         },
1170         {
1171                 .name           = "exports",
1172                 .fops           = &nodemap_exports_fops,
1173         },
1174         {
1175                 .name           = "idmap",
1176                 .fops           = &nodemap_idmap_fops,
1177         },
1178         {
1179                 NULL
1180         }
1181 };
1182
1183 static struct lprocfs_vars lprocfs_default_nodemap_vars[] = {
1184         {
1185                 .name           = "id",
1186                 .fops           = &nodemap_id_fops,
1187         },
1188         {
1189                 .name           = "trusted_nodemap",
1190                 .fops           = &nodemap_trusted_fops,
1191         },
1192         {
1193                 .name           = "admin_nodemap",
1194                 .fops           = &nodemap_admin_fops,
1195         },
1196         {
1197                 .name           = "squash_uid",
1198                 .fops           = &nodemap_squash_uid_fops,
1199         },
1200         {
1201                 .name           = "squash_gid",
1202                 .fops           = &nodemap_squash_gid_fops,
1203         },
1204         {
1205                 .name           = "exports",
1206                 .fops           = &nodemap_exports_fops,
1207         },
1208         {
1209                 NULL
1210         }
1211 };
1212
1213 /**
1214  * Initialize the nodemap procfs directory.
1215  *
1216  * \retval      0               success
1217  */
1218 int nodemap_procfs_init(void)
1219 {
1220         int rc = 0;
1221
1222         proc_lustre_nodemap_root = lprocfs_register(LUSTRE_NODEMAP_NAME,
1223                                                     proc_lustre_root,
1224                                                     lprocfs_nm_module_vars,
1225                                                     NULL);
1226         if (IS_ERR(proc_lustre_nodemap_root)) {
1227                 rc = PTR_ERR(proc_lustre_nodemap_root);
1228                 CERROR("cannot create 'nodemap' directory: rc = %d\n",
1229                        rc);
1230                 proc_lustre_nodemap_root = NULL;
1231         }
1232         return rc;
1233 }
1234
1235 /**
1236  * Cleanup nodemap proc entry data structures.
1237  */
1238 void nodemap_procfs_exit(void)
1239 {
1240         struct nodemap_pde *nm_pde;
1241         struct nodemap_pde *tmp;
1242
1243         lprocfs_remove(&proc_lustre_nodemap_root);
1244         list_for_each_entry_safe(nm_pde, tmp, &nodemap_pde_list,
1245                                  npe_list_member) {
1246                 list_del(&nm_pde->npe_list_member);
1247                 OBD_FREE_PTR(nm_pde);
1248         }
1249 }
1250
1251 /**
1252  * Remove a nodemap's procfs entry and related data.
1253  */
1254 void lprocfs_nodemap_remove(struct nodemap_pde *nm_pde)
1255 {
1256         lprocfs_remove(&nm_pde->npe_proc_entry);
1257         list_del(&nm_pde->npe_list_member);
1258         OBD_FREE_PTR(nm_pde);
1259 }
1260
1261 /**
1262  * Register the proc directory for a nodemap
1263  *
1264  * \param       nodemap         nodemap to make the proc dir for
1265  * \param       is_default:     1 if default nodemap
1266  * \retval      0               success
1267  */
1268 int lprocfs_nodemap_register(struct lu_nodemap *nodemap, bool is_default)
1269 {
1270         struct nodemap_pde      *nm_entry;
1271         int                      rc = 0;
1272
1273         OBD_ALLOC_PTR(nm_entry);
1274         if (nm_entry == NULL)
1275                 GOTO(out, rc = -ENOMEM);
1276
1277         nm_entry->npe_proc_entry = proc_mkdir(nodemap->nm_name,
1278                                               proc_lustre_nodemap_root);
1279         if (IS_ERR(nm_entry->npe_proc_entry))
1280                 GOTO(out, rc = PTR_ERR(nm_entry->npe_proc_entry));
1281
1282         snprintf(nm_entry->npe_name, sizeof(nm_entry->npe_name), "%s",
1283                  nodemap->nm_name);
1284
1285         /* Use the nodemap name as stored on the PDE as the private data. This
1286          * is so a nodemap struct can be replaced without updating the proc
1287          * entries.
1288          */
1289         rc = lprocfs_add_vars(nm_entry->npe_proc_entry,
1290                               (is_default ? lprocfs_default_nodemap_vars :
1291                                             lprocfs_nodemap_vars),
1292                               nm_entry->npe_name);
1293         if (rc != 0)
1294                 lprocfs_remove(&nm_entry->npe_proc_entry);
1295         else
1296                 list_add(&nm_entry->npe_list_member, &nodemap_pde_list);
1297
1298 out:
1299         if (rc != 0) {
1300                 CERROR("cannot create 'nodemap/%s': rc = %d\n",
1301                        nodemap->nm_name, rc);
1302                 if (nm_entry != NULL) {
1303                         OBD_FREE_PTR(nm_entry);
1304                         nm_entry = NULL;
1305                 }
1306         }
1307
1308         nodemap->nm_pde_data = nm_entry;
1309
1310         return rc;
1311 }