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