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