Whamcloud - gitweb
LU-985 lprocfs: verify user buffer access
[fs/lustre-release.git] / lustre / mdt / mdt_lproc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/mdt/mdt_lproc.c
40  *
41  * Author: Lai Siyao <lsy@clusterfs.com>
42  * Author: Fan Yong <fanyong@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_MDS
49
50 #include <linux/version.h>
51 #include <asm/statfs.h>
52
53 #include <linux/module.h>
54
55 /* LUSTRE_VERSION_CODE */
56 #include <lustre_ver.h>
57 /*
58  * struct OBD_{ALLOC,FREE}*()
59  * MDT_FAIL_CHECK
60  */
61 #include <obd_support.h>
62 /* struct obd_export */
63 #include <lustre_export.h>
64 /* struct obd_device */
65 #include <obd.h>
66 #include <obd_class.h>
67 #include <lustre_mds.h>
68 #include <lustre_mdt.h>
69 #include <lprocfs_status.h>
70 #include <lu_time.h>
71 #include "mdt_internal.h"
72 #include <lnet/lib-lnet.h>
73
74 enum {
75         LPROC_MDT_NR
76 };
77 static const char *mdt_proc_names[LPROC_MDT_NR] = {
78 };
79
80 /**
81  * The rename stats output would be YAML formats, like
82  * rename_stats:
83  * - snapshot_time: 1234567890.123456
84  * - same_dir:
85  *     4kB: { samples: 1230, pct: 33, cum_pct: 45 }
86  *     8kB: { samples: 1242, pct: 33, cum_pct: 78 }
87  *     16kB: { samples: 132, pct: 3, cum_pct: 81 }
88  * - crossdir_src:
89  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
90  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
91  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
92  * - crossdir_tgt:
93  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
94  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
95  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
96  **/
97
98 #define pct(a, b) (b ? a * 100 / b : 0)
99
100 static void display_rename_stats(struct seq_file *seq, char *name,
101                                  struct obd_histogram *hist)
102 {
103         unsigned long tot, t, cum = 0;
104         int i;
105
106         tot = lprocfs_oh_sum(hist);
107         if (tot > 0)
108                 seq_printf(seq, "- %-15s\n", name);
109         /* dir size start from 4K, start i from 10(2^10) here */
110         for (i = 0; i < OBD_HIST_MAX; i++) {
111                 t = hist->oh_buckets[i];
112                 cum += t;
113                 if (cum == 0)
114                         continue;
115
116                 if (i < 10)
117                         seq_printf(seq, "%6s%d%s", " ", 1<< i, "bytes:");
118                 else if (i < 20)
119                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-10), "KB:");
120                 else
121                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-20), "MB:");
122
123                 seq_printf(seq, " { sample: %3lu, pct: %3lu, cum_pct: %3lu }\n",
124                            t, pct(t, tot), pct(cum, tot));
125
126                 if (cum == tot)
127                         break;
128         }
129 }
130
131 static void rename_stats_show(struct seq_file *seq,
132                               struct rename_stats *rename_stats)
133 {
134         struct timeval now;
135
136         /* this sampling races with updates */
137         do_gettimeofday(&now);
138         seq_printf(seq, "rename_stats:\n");
139         seq_printf(seq, "- %-15s %lu.%lu\n", "snapshot_time:",
140                    now.tv_sec, now.tv_usec);
141
142         display_rename_stats(seq, "same_dir",
143                              &rename_stats->hist[RENAME_SAMEDIR_SIZE]);
144         display_rename_stats(seq, "crossdir_src",
145                              &rename_stats->hist[RENAME_CROSSDIR_SRC_SIZE]);
146         display_rename_stats(seq, "crossdir_tgt",
147                              &rename_stats->hist[RENAME_CROSSDIR_TGT_SIZE]);
148 }
149
150 #undef pct
151
152 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
153 {
154         struct mdt_device *mdt = seq->private;
155
156         rename_stats_show(seq, &mdt->mdt_rename_stats);
157
158         return 0;
159 }
160
161 static ssize_t mdt_rename_stats_seq_write(struct file *file, const char *buf,
162                                           size_t len, loff_t *off)
163 {
164         struct seq_file *seq = file->private_data;
165         struct mdt_device *mdt = seq->private;
166         int i;
167
168         for (i = 0; i < RENAME_LAST; i++)
169                 lprocfs_oh_clear(&mdt->mdt_rename_stats.hist[i]);
170
171         return len;
172 }
173
174 LPROC_SEQ_FOPS(mdt_rename_stats);
175
176 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
177 {
178         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
179         struct obd_device *obd = ld->ld_obd;
180         int i;
181
182         for (i = 0; i < RENAME_LAST; i++)
183                 spin_lock_init(&mdt->mdt_rename_stats.hist[i].oh_lock);
184
185         return lprocfs_obd_seq_create(obd, "rename_stats", 0444,
186                                       &mdt_rename_stats_fops, mdt);
187 }
188
189 void mdt_rename_counter_tally(struct mdt_thread_info *info,
190                               struct mdt_device *mdt,
191                               struct obd_export *exp,
192                               struct mdt_object *src,
193                               struct mdt_object *tgt)
194 {
195         struct md_attr *ma = &info->mti_attr;
196         struct rename_stats *rstats = &mdt->mdt_rename_stats;
197         int rc;
198
199         ma->ma_need = MA_INODE;
200         ma->ma_valid = 0;
201         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
202         if (rc) {
203                 CERROR("%s: "DFID" attr_get, rc = %d\n",
204                        exp->exp_obd->obd_name, PFID(mdt_object_fid(src)), rc);
205                 return;
206         }
207
208         if (src == tgt) {
209                 mdt_counter_incr(exp, LPROC_MDT_SAMEDIR_RENAME);
210                 lprocfs_oh_tally_log2(&rstats->hist[RENAME_SAMEDIR_SIZE],
211                                       (unsigned int)ma->ma_attr.la_size);
212                 return;
213         }
214
215         mdt_counter_incr(exp, LPROC_MDT_CROSSDIR_RENAME);
216         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_SRC_SIZE],
217                               (unsigned int)ma->ma_attr.la_size);
218
219         ma->ma_need = MA_INODE;
220         ma->ma_valid = 0;
221         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
222         if (rc) {
223                 CERROR("%s: "DFID" attr_get, rc = %d\n",
224                        exp->exp_obd->obd_name, PFID(mdt_object_fid(tgt)), rc);
225                 return;
226         }
227
228         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_TGT_SIZE],
229                               (unsigned int)ma->ma_attr.la_size);
230 }
231
232 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
233 {
234         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
235         struct obd_device *obd = ld->ld_obd;
236         struct lprocfs_static_vars lvars;
237         int rc;
238         ENTRY;
239
240         LASSERT(name != NULL);
241
242         lprocfs_mdt_init_vars(&lvars);
243         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
244         if (rc) {
245                 CERROR("Can't init lprocfs, rc %d\n", rc);
246                 return rc;
247         }
248         ptlrpc_lprocfs_register_obd(obd);
249
250         mdt->mdt_proc_entry = obd->obd_proc_entry;
251         LASSERT(mdt->mdt_proc_entry != NULL);
252
253         rc = lu_time_init(&mdt->mdt_stats, mdt->mdt_proc_entry,
254                           mdt_proc_names, ARRAY_SIZE(mdt_proc_names));
255         if (rc == 0)
256                 rc = lu_time_named_init(&ld->ld_site->ls_time_stats,
257                                         "site_time", mdt->mdt_proc_entry,
258                                          lu_time_names,
259                                          ARRAY_SIZE(lu_time_names));
260         if (rc)
261                 return rc;
262
263         obd->obd_proc_exports_entry = proc_mkdir("exports",
264                                                  obd->obd_proc_entry);
265         if (obd->obd_proc_exports_entry)
266                 lprocfs_add_simple(obd->obd_proc_exports_entry,
267                                    "clear", lprocfs_nid_stats_clear_read,
268                                    lprocfs_nid_stats_clear_write, obd, NULL);
269         rc = lprocfs_alloc_md_stats(obd, LPROC_MDT_LAST);
270         if (rc == 0)
271                 mdt_stats_counter_init(obd->md_stats);
272
273         rc = lproc_mdt_attach_rename_seqstat(mdt);
274         if (rc)
275                 CERROR("%s: MDT can not create rename stats rc = %d\n",
276                        obd->obd_name, rc);
277
278         RETURN(rc);
279 }
280
281 int mdt_procfs_fini(struct mdt_device *mdt)
282 {
283         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
284         struct obd_device *obd = ld->ld_obd;
285
286         if (obd->obd_proc_exports_entry) {
287                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
288                 obd->obd_proc_exports_entry = NULL;
289         }
290         lprocfs_free_per_client_stats(obd);
291         lprocfs_obd_cleanup(obd);
292         ptlrpc_lprocfs_unregister_obd(obd);
293         if (mdt->mdt_proc_entry) {
294                 lu_time_fini(&ld->ld_site->ls_time_stats);
295                 lu_time_fini(&mdt->mdt_stats);
296                 mdt->mdt_proc_entry = NULL;
297         }
298         lprocfs_free_md_stats(obd);
299         lprocfs_free_obd_stats(obd);
300
301         RETURN(0);
302 }
303
304 void mdt_time_start(const struct mdt_thread_info *info)
305 {
306         lu_lprocfs_time_start(info->mti_env);
307 }
308
309 void mdt_time_end(const struct mdt_thread_info *info, int idx)
310 {
311         lu_lprocfs_time_end(info->mti_env, info->mti_mdt->mdt_stats, idx);
312 }
313
314 static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
315                                       int count, int *eof, void *data)
316 {
317         struct obd_device *obd = data;
318         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
319
320         *eof = 1;
321         return snprintf(page, count, "%u\n",
322                         mdt->mdt_identity_cache->uc_entry_expire);
323 }
324
325 static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
326                                       unsigned long count, void *data)
327 {
328         struct obd_device *obd = data;
329         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
330         int rc, val;
331
332         rc = lprocfs_write_helper(buffer, count, &val);
333         if (rc)
334                 return rc;
335
336         mdt->mdt_identity_cache->uc_entry_expire = val;
337         return count;
338 }
339
340 static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
341                                               off_t off, int count, int *eof,
342                                               void *data)
343 {
344         struct obd_device *obd = data;
345         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
346
347         *eof = 1;
348         return snprintf(page, count, "%u\n",
349                         mdt->mdt_identity_cache->uc_acquire_expire);
350 }
351
352 static int lprocfs_wr_identity_acquire_expire(struct file *file,
353                                               const char *buffer,
354                                               unsigned long count,
355                                               void *data)
356 {
357         struct obd_device *obd = data;
358         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
359         int rc, val;
360
361         rc = lprocfs_write_helper(buffer, count, &val);
362         if (rc)
363                 return rc;
364
365         mdt->mdt_identity_cache->uc_acquire_expire = val;
366         return count;
367 }
368
369 static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
370                                       int count, int *eof, void *data)
371 {
372         struct obd_device *obd = data;
373         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
374         struct upcall_cache *hash = mdt->mdt_identity_cache;
375         int len;
376
377         *eof = 1;
378         cfs_read_lock(&hash->uc_upcall_rwlock);
379         len = snprintf(page, count, "%s\n", hash->uc_upcall);
380         cfs_read_unlock(&hash->uc_upcall_rwlock);
381         return len;
382 }
383
384 static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
385                                       unsigned long count, void *data)
386 {
387         struct obd_device *obd = data;
388         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
389         struct upcall_cache *hash = mdt->mdt_identity_cache;
390         int rc;
391         char *kernbuf;
392
393         if (count >= UC_CACHE_UPCALL_MAXPATH) {
394                 CERROR("%s: identity upcall too long\n", obd->obd_name);
395                 return -EINVAL;
396         }
397         OBD_ALLOC(kernbuf, count + 1);
398         if (kernbuf == NULL)
399                 GOTO(failed, rc = -ENOMEM);
400         if (cfs_copy_from_user(kernbuf, buffer, count))
401                 GOTO(failed, rc = -EFAULT);
402
403         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
404         cfs_write_lock(&hash->uc_upcall_rwlock);
405         sscanf(kernbuf, "%s", hash->uc_upcall);
406         cfs_write_unlock(&hash->uc_upcall_rwlock);
407
408         if (strcmp(hash->uc_name, obd->obd_name) != 0)
409                 CWARN("%s: write to upcall name %s\n",
410                       obd->obd_name, hash->uc_upcall);
411
412         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
413                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
414                       "cause unexpected \"EACCESS\"\n", obd->obd_name);
415
416         CWARN("%s: identity upcall set to %s\n", obd->obd_name, hash->uc_upcall);
417         OBD_FREE(kernbuf, count + 1);
418         RETURN(count);
419
420  failed:
421         if (kernbuf)
422                 OBD_FREE(kernbuf, count + 1);
423         RETURN(rc);
424 }
425
426 static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
427                                      unsigned long count, void *data)
428 {
429         struct obd_device *obd = data;
430         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
431         int rc, uid;
432
433         rc = lprocfs_write_helper(buffer, count, &uid);
434         if (rc)
435                 return rc;
436
437         mdt_flush_identity(mdt->mdt_identity_cache, uid);
438         return count;
439 }
440
441 static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
442                                     unsigned long count, void *data)
443 {
444         struct obd_device *obd = data;
445         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
446         struct identity_downcall_data *param;
447         int size = sizeof(*param), rc, checked = 0;
448
449 again:
450         if (count < size) {
451                 CERROR("%s: invalid data count = %lu, size = %d\n",
452                        obd->obd_name, count, size);
453                 return -EINVAL;
454         }
455
456         OBD_ALLOC(param, size);
457         if (param == NULL)
458                 return -ENOMEM;
459
460         if (cfs_copy_from_user(param, buffer, size)) {
461                 CERROR("%s: bad identity data\n", obd->obd_name);
462                 GOTO(out, rc = -EFAULT);
463         }
464
465         if (checked == 0) {
466                 checked = 1;
467                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
468                         CERROR("%s: MDS identity downcall bad params\n",
469                                obd->obd_name);
470                         GOTO(out, rc = -EINVAL);
471                 }
472
473                 if (param->idd_nperms > N_PERMS_MAX) {
474                         CERROR("%s: perm count %d more than maximum %d\n",
475                                obd->obd_name, param->idd_nperms, N_PERMS_MAX);
476                         GOTO(out, rc = -EINVAL);
477                 }
478
479                 if (param->idd_ngroups > NGROUPS_MAX) {
480                         CERROR("%s: group count %d more than maximum %d\n",
481                                obd->obd_name, param->idd_ngroups, NGROUPS_MAX);
482                         GOTO(out, rc = -EINVAL);
483                 }
484
485                 if (param->idd_ngroups) {
486                         rc = param->idd_ngroups; /* save idd_ngroups */
487                         OBD_FREE(param, size);
488                         size = offsetof(struct identity_downcall_data,
489                                         idd_groups[rc]);
490                         goto again;
491                 }
492         }
493
494         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
495                                    param->idd_uid, param);
496
497 out:
498         if (param != NULL)
499                 OBD_FREE(param, size);
500
501         return rc ? rc : count;
502 }
503
504 /* for debug only */
505 static int lprocfs_rd_capa(char *page, char **start, off_t off,
506                            int count, int *eof, void *data)
507 {
508         struct obd_device *obd = data;
509         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
510
511         return snprintf(page, count, "capability on: %s %s\n",
512                         mdt->mdt_opts.mo_oss_capa ? "oss" : "",
513                         mdt->mdt_opts.mo_mds_capa ? "mds" : "");
514 }
515
516 static int lprocfs_wr_capa(struct file *file, const char *buffer,
517                            unsigned long count, void *data)
518 {
519         struct obd_device *obd = data;
520         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
521         int val, rc;
522
523         rc = lprocfs_write_helper(buffer, count, &val);
524         if (rc)
525                 return rc;
526
527         if (val < 0 || val > 3) {
528                 CERROR("invalid capability mode, only 0/2/3 is accepted.\n"
529                        " 0:  disable fid capability\n"
530                        " 2:  enable MDS fid capability\n"
531                        " 3:  enable both MDS and OSS fid capability\n");
532                 return -EINVAL;
533         }
534
535         /* OSS fid capability needs enable both MDS and OSS fid capability on
536          * MDS */
537         if (val == 1) {
538                 CERROR("can't enable OSS fid capability only, you should use "
539                        "'3' to enable both MDS and OSS fid capability.\n");
540                 return -EINVAL;
541         }
542
543         mdt->mdt_opts.mo_oss_capa = (val & 0x1);
544         mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
545         mdt->mdt_capa_conf = 1;
546         LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
547                       obd->obd_name,
548                       mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
549         LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
550                       obd->obd_name,
551                       mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
552         return count;
553 }
554
555 static int lprocfs_rd_capa_count(char *page, char **start, off_t off,
556                                  int count, int *eof, void *data)
557 {
558         return snprintf(page, count, "%d %d\n",
559                         capa_count[CAPA_SITE_CLIENT],
560                         capa_count[CAPA_SITE_SERVER]);
561 }
562
563 static int lprocfs_rd_site_stats(char *page, char **start, off_t off,
564                                  int count, int *eof, void *data)
565 {
566         struct obd_device *obd = data;
567         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
568
569         return lu_site_stats_print(mdt_lu_site(mdt), page, count);
570 }
571
572 static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
573                                    int count, int *eof, void *data)
574 {
575         struct obd_device *obd = data;
576         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
577
578         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
579 }
580
581 static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
582                                    unsigned long count, void *data)
583 {
584         struct obd_device *obd = data;
585         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
586         int val, rc;
587
588         rc = lprocfs_write_helper(buffer, count, &val);
589         if (rc)
590                 return rc;
591
592         mdt->mdt_capa_timeout = (unsigned long)val;
593         mdt->mdt_capa_conf = 1;
594         return count;
595 }
596
597 static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
598                                  int *eof, void *data)
599 {
600         struct obd_device *obd = data;
601         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
602
603         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
604 }
605
606 static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
607                                  unsigned long count, void *data)
608 {
609         struct obd_device *obd = data;
610         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
611         int val, rc;
612
613         rc = lprocfs_write_helper(buffer, count, &val);
614         if (rc)
615                 return rc;
616
617         mdt->mdt_ck_timeout = (unsigned long)val;
618         mdt->mdt_capa_conf = 1;
619         return count;
620 }
621
622 #define BUFLEN (UUID_MAX + 4)
623
624 static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
625                                        unsigned long count, void *data)
626 {
627         char *kbuf;
628         char *tmpbuf;
629
630         OBD_ALLOC(kbuf, BUFLEN);
631         if (kbuf == NULL)
632                 return -ENOMEM;
633
634         /*
635          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
636          * bytes into kbuf, to ensure that the string is NUL-terminated.
637          * UUID_MAX should include a trailing NUL already.
638          */
639         if (cfs_copy_from_user(kbuf, buffer,
640                                min_t(unsigned long, BUFLEN - 1, count))) {
641                 count = -EFAULT;
642                 goto out;
643         }
644         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
645
646         if (strncmp(tmpbuf, "nid:", 4) != 0) {
647                 count = lprocfs_wr_evict_client(file, buffer, count, data);
648                 goto out;
649         }
650
651         CERROR("NOT implement evict client by nid %s\n", tmpbuf);
652
653 out:
654         OBD_FREE(kbuf, BUFLEN);
655         return count;
656 }
657
658 #undef BUFLEN
659
660 static int lprocfs_rd_sec_level(char *page, char **start, off_t off,
661                                 int count, int *eof, void *data)
662 {
663         struct obd_device *obd = data;
664         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
665
666         return snprintf(page, count, "%d\n", mdt->mdt_sec_level);
667 }
668
669 static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
670                                 unsigned long count, void *data)
671 {
672         struct obd_device *obd = data;
673         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
674         int val, rc;
675
676         rc = lprocfs_write_helper(buffer, count, &val);
677         if (rc)
678                 return rc;
679
680         if (val > LUSTRE_SEC_ALL || val < LUSTRE_SEC_NONE)
681                 return -EINVAL;
682
683         if (val == LUSTRE_SEC_SPECIFY) {
684                 CWARN("security level %d will be supported in future.\n",
685                       LUSTRE_SEC_SPECIFY);
686                 return -EINVAL;
687         }
688
689         mdt->mdt_sec_level = val;
690         return count;
691 }
692
693 static int lprocfs_rd_cos(char *page, char **start, off_t off,
694                               int count, int *eof, void *data)
695 {
696         struct obd_device *obd = data;
697         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
698
699         return snprintf(page, count, "%u\n", mdt_cos_is_enabled(mdt));
700 }
701
702 static int lprocfs_wr_cos(struct file *file, const char *buffer,
703                                   unsigned long count, void *data)
704 {
705         struct obd_device *obd = data;
706         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
707         int val, rc;
708
709         rc = lprocfs_write_helper(buffer, count, &val);
710         if (rc)
711                 return rc;
712         mdt_enable_cos(mdt, val);
713         return count;
714 }
715
716 static int lprocfs_rd_root_squash(char *page, char **start, off_t off,
717                                   int count, int *eof, void *data)
718 {
719         struct obd_device *obd = data;
720         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
721
722         return snprintf(page, count, "%u:%u\n", mdt->mdt_squash_uid,
723                         mdt->mdt_squash_gid);
724 }
725
726 static int safe_strtoul(const char *str, char **endp, unsigned long *res)
727 {
728         char n[24];
729
730         *res = simple_strtoul(str, endp, 0);
731         if (str == *endp)
732                 return 1;
733
734         sprintf(n, "%lu", *res);
735         if (strncmp(n, str, *endp - str))
736                 /* overflow */
737                 return 1;
738         return 0;
739 }
740
741 static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
742                                   unsigned long count, void *data)
743 {
744         struct obd_device *obd = data;
745         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
746         int rc;
747         char kernbuf[50], *tmp, *end, *errmsg;
748         unsigned long uid, gid;
749         int nouid, nogid;
750         ENTRY;
751
752         if (count >= sizeof(kernbuf)) {
753                 errmsg = "string too long";
754                 GOTO(failed, rc = -EINVAL);
755         }
756         if (cfs_copy_from_user(kernbuf, buffer, count)) {
757                 errmsg = "bad address";
758                 GOTO(failed, rc = -EFAULT);
759         }
760         kernbuf[count] = '\0';
761
762         nouid = nogid = 0;
763         if (safe_strtoul(buffer, &tmp, &uid)) {
764                 uid = mdt->mdt_squash_uid;
765                 nouid = 1;
766         }
767
768         /* skip ':' */
769         if (*tmp == ':') {
770                 tmp++;
771                 if (safe_strtoul(tmp, &end, &gid)) {
772                         gid = mdt->mdt_squash_gid;
773                         nogid = 1;
774                 }
775         } else {
776                 gid = mdt->mdt_squash_gid;
777                 nogid = 1;
778         }
779
780         mdt->mdt_squash_uid = uid;
781         mdt->mdt_squash_gid = gid;
782
783         if (nouid && nogid) {
784                 errmsg = "needs uid:gid format";
785                 GOTO(failed, rc = -EINVAL);
786         }
787
788         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
789                       obd->obd_name,
790                       mdt->mdt_squash_uid,  mdt->mdt_squash_gid);
791         RETURN(count);
792
793  failed:
794         CWARN("%s: failed to set root_squash to \"%s\", %s: rc %d\n",
795               obd->obd_name, buffer, errmsg, rc);
796         RETURN(rc);
797 }
798
799 static int lprocfs_rd_nosquash_nids(char *page, char **start, off_t off,
800                                     int count, int *eof, void *data)
801 {
802         struct obd_device *obd = data;
803         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
804
805         if (mdt->mdt_nosquash_str)
806                 return snprintf(page, count, "%s\n", mdt->mdt_nosquash_str);
807         return snprintf(page, count, "NONE\n");
808 }
809
810 static int lprocfs_wr_nosquash_nids(struct file *file, const char *buffer,
811                                     unsigned long count, void *data)
812 {
813         struct obd_device *obd = data;
814         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
815         int rc;
816         char *kernbuf, *errmsg;
817         cfs_list_t tmp;
818         ENTRY;
819
820         OBD_ALLOC(kernbuf, count + 1);
821         if (kernbuf == NULL) {
822                 errmsg = "no memory";
823                 GOTO(failed, rc = -ENOMEM);
824         }
825         if (cfs_copy_from_user(kernbuf, buffer, count)) {
826                 errmsg = "bad address";
827                 GOTO(failed, rc = -EFAULT);
828         }
829         kernbuf[count] = '\0';
830
831         if (!strcmp(kernbuf, "NONE") || !strcmp(kernbuf, "clear")) {
832                 /* empty string is special case */
833                 cfs_down_write(&mdt->mdt_squash_sem);
834                 if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
835                         cfs_free_nidlist(&mdt->mdt_nosquash_nids);
836                         OBD_FREE(mdt->mdt_nosquash_str,
837                                  mdt->mdt_nosquash_strlen);
838                         mdt->mdt_nosquash_str = NULL;
839                         mdt->mdt_nosquash_strlen = 0;
840                 }
841                 cfs_up_write(&mdt->mdt_squash_sem);
842                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n",
843                               obd->obd_name);
844                 OBD_FREE(kernbuf, count + 1);
845                 RETURN(count);
846         }
847
848         CFS_INIT_LIST_HEAD(&tmp);
849         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
850                 errmsg = "can't parse";
851                 GOTO(failed, rc = -EINVAL);
852         }
853
854         cfs_down_write(&mdt->mdt_squash_sem);
855         if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
856                 cfs_free_nidlist(&mdt->mdt_nosquash_nids);
857                 OBD_FREE(mdt->mdt_nosquash_str, mdt->mdt_nosquash_strlen);
858         }
859         mdt->mdt_nosquash_str = kernbuf;
860         mdt->mdt_nosquash_strlen = count + 1;
861         cfs_list_splice(&tmp, &mdt->mdt_nosquash_nids);
862
863         LCONSOLE_INFO("%s: nosquash_nids is set to %s\n",
864                       obd->obd_name, kernbuf);
865         cfs_up_write(&mdt->mdt_squash_sem);
866         RETURN(count);
867
868  failed:
869         CWARN("%s: failed to set nosquash_nids to \"%s\", %s: rc %d\n",
870               obd->obd_name, kernbuf, errmsg, rc);
871         if (kernbuf)
872                 OBD_FREE(kernbuf, count + 1);
873         RETURN(rc);
874 }
875
876 static int lprocfs_rd_mdt_som(char *page, char **start, off_t off,
877                               int count, int *eof, void *data)
878 {
879         struct obd_device *obd = data;
880         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
881
882         return snprintf(page, count, "%sabled\n",
883                         mdt->mdt_som_conf ? "en" : "dis");
884 }
885
886 static int lprocfs_wr_mdt_som(struct file *file, const char *buffer,
887                               unsigned long count, void *data)
888 {
889         struct obd_export *exp;
890         struct obd_device *obd = data;
891         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
892         char kernbuf[16];
893         unsigned long val = 0;
894
895         if (count > (sizeof(kernbuf) - 1))
896                 return -EINVAL;
897
898         if (cfs_copy_from_user(kernbuf, buffer, count))
899                 return -EFAULT;
900
901         kernbuf[count] = '\0';
902
903         if (!strcmp(kernbuf, "enabled"))
904                 val = 1;
905         else if (strcmp(kernbuf, "disabled"))
906                 return -EINVAL;
907
908         if (mdt->mdt_som_conf == val)
909                 return count;
910
911         if (!obd->obd_process_conf) {
912                 CERROR("Temporary SOM change is not supported, use lctl "
913                        "conf_param for permanent setting\n");
914                 return count;
915         }
916
917         /* 1 stands for self export. */
918         cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
919                 if (exp == obd->obd_self_export)
920                         continue;
921                 if (exp->exp_connect_flags & OBD_CONNECT_MDS_MDS)
922                         continue;
923                 /* Some clients are already connected, skip the change */
924                 LCONSOLE_INFO("%s is already connected, SOM will be %s on "
925                               "the next mount\n", exp->exp_client_uuid.uuid,
926                               val ? "enabled" : "disabled");
927                 return count;
928         }
929
930         mdt->mdt_som_conf = val;
931         LCONSOLE_INFO("Enabling SOM\n");
932
933         return count;
934 }
935
936 /* Temporary; for testing purposes only */
937 static int lprocfs_mdt_wr_mdc(struct file *file, const char *buffer,
938                               unsigned long count, void *data)
939 {
940         struct obd_device *obd = data;
941         struct obd_export *exp = NULL;
942         struct obd_uuid   *uuid;
943         char              *kbuf;
944         char              *tmpbuf;
945
946         OBD_ALLOC(kbuf, UUID_MAX);
947         if (kbuf == NULL)
948                 return -ENOMEM;
949
950         /*
951          * OBD_ALLOC() will zero kbuf, but we only copy UUID_MAX - 1
952          * bytes into kbuf, to ensure that the string is NUL-terminated.
953          * UUID_MAX should include a trailing NUL already.
954          */
955         if (cfs_copy_from_user(kbuf, buffer,
956                                min_t(unsigned long, UUID_MAX - 1, count))) {
957                 count = -EFAULT;
958                 goto out;
959         }
960         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, UUID_MAX - 1, count));
961
962         OBD_ALLOC(uuid, UUID_MAX);
963         if (uuid == NULL) {
964                 count = -ENOMEM;
965                 goto out;
966         }
967
968         obd_str2uuid(uuid, tmpbuf);
969         exp = cfs_hash_lookup(obd->obd_uuid_hash, uuid);
970         if (exp == NULL) {
971                 CERROR("%s: no export %s found\n",
972                        obd->obd_name, obd_uuid2str(uuid));
973         } else {
974                 mdt_hsm_copytool_send(exp);
975                 class_export_put(exp);
976         }
977
978         OBD_FREE(uuid, UUID_MAX);
979 out:
980         OBD_FREE(kbuf, UUID_MAX);
981         return count;
982 }
983
984 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
985         { "uuid",                       lprocfs_rd_uuid,                 0, 0 },
986         { "recovery_status",            lprocfs_obd_rd_recovery_status,  0, 0 },
987         { "num_exports",                lprocfs_rd_num_exports,          0, 0 },
988         { "identity_expire",            lprocfs_rd_identity_expire,
989                                         lprocfs_wr_identity_expire,         0 },
990         { "identity_acquire_expire",    lprocfs_rd_identity_acquire_expire,
991                                         lprocfs_wr_identity_acquire_expire, 0 },
992         { "identity_upcall",            lprocfs_rd_identity_upcall,
993                                         lprocfs_wr_identity_upcall,         0 },
994         { "identity_flush",             0, lprocfs_wr_identity_flush,       0 },
995         { "identity_info",              0, lprocfs_wr_identity_info,        0 },
996         { "capa",                       lprocfs_rd_capa,
997                                         lprocfs_wr_capa,                    0 },
998         { "capa_timeout",               lprocfs_rd_capa_timeout,
999                                         lprocfs_wr_capa_timeout,            0 },
1000         { "capa_key_timeout",           lprocfs_rd_ck_timeout,
1001                                         lprocfs_wr_ck_timeout,              0 },
1002         { "capa_count",                 lprocfs_rd_capa_count,           0, 0 },
1003         { "site_stats",                 lprocfs_rd_site_stats,           0, 0 },
1004         { "evict_client",               0, lprocfs_mdt_wr_evict_client,     0 },
1005         { "hash_stats",                 lprocfs_obd_rd_hash,    0, 0 },
1006         { "sec_level",                  lprocfs_rd_sec_level,
1007                                         lprocfs_wr_sec_level,               0 },
1008         { "commit_on_sharing",          lprocfs_rd_cos, lprocfs_wr_cos, 0 },
1009         { "root_squash",                lprocfs_rd_root_squash,
1010                                         lprocfs_wr_root_squash,             0 },
1011         { "nosquash_nids",              lprocfs_rd_nosquash_nids,
1012                                         lprocfs_wr_nosquash_nids,           0 },
1013         { "som",                        lprocfs_rd_mdt_som,
1014                                         lprocfs_wr_mdt_som, 0 },
1015         { "mdccomm",                    0, lprocfs_mdt_wr_mdc,              0 },
1016         { "instance",                   lprocfs_target_rd_instance,         0 },
1017         { "ir_factor",                  lprocfs_obd_rd_ir_factor,
1018                                         lprocfs_obd_wr_ir_factor,           0 },
1019         { 0 }
1020 };
1021
1022 static struct lprocfs_vars lprocfs_mdt_module_vars[] = {
1023         { "num_refs",                   lprocfs_rd_numrefs,              0, 0 },
1024         { 0 }
1025 };
1026
1027 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
1028 {
1029     lvars->module_vars  = lprocfs_mdt_module_vars;
1030     lvars->obd_vars     = lprocfs_mdt_obd_vars;
1031 }
1032
1033 void mdt_counter_incr(struct obd_export *exp, int opcode)
1034 {
1035         if (exp->exp_obd && exp->exp_obd->md_stats)
1036                 lprocfs_counter_incr(exp->exp_obd->md_stats, opcode);
1037         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1038                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
1039
1040 }
1041
1042 void mdt_stats_counter_init(struct lprocfs_stats *stats)
1043 {
1044         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
1045         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
1046         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
1047         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
1048         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
1049         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
1050         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
1051         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
1052         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
1053         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
1054         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
1055         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
1056         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
1057         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
1058         lprocfs_counter_init(stats, LPROC_MDT_SAMEDIR_RENAME, 0,
1059                              "samedir_rename", "reqs");
1060         lprocfs_counter_init(stats, LPROC_MDT_CROSSDIR_RENAME, 0,
1061                              "crossdir_rename", "reqs");
1062 }