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