Whamcloud - gitweb
6e63b64cdddb00da757920d048ab4236e5c97839
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdt/mdt_lproc.c
33  *
34  * Author: Lai Siyao <lsy@clusterfs.com>
35  * Author: Fan Yong <fanyong@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <linux/version.h>
41 #include <asm/statfs.h>
42
43 #include <linux/module.h>
44 #include <uapi/linux/lnet/nidstr.h>
45 /* LUSTRE_VERSION_CODE */
46 #include <uapi/linux/lustre/lustre_ver.h>
47 /*
48  * struct OBD_{ALLOC,FREE}*()
49  * MDT_FAIL_CHECK
50  */
51 #include <obd_support.h>
52 /* struct obd_export */
53 #include <lustre_export.h>
54 /* struct obd_device */
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <lustre_mds.h>
58 #include <lprocfs_status.h>
59 #include "mdt_internal.h"
60
61 /**
62  * The rename stats output would be YAML formats, like
63  * rename_stats:
64  * - snapshot_time: 1234567890.123456
65  * - same_dir:
66  *     4kB: { samples: 1230, pct: 33, cum_pct: 45 }
67  *     8kB: { samples: 1242, pct: 33, cum_pct: 78 }
68  *     16kB: { samples: 132, pct: 3, cum_pct: 81 }
69  * - crossdir_src:
70  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
71  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
72  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
73  * - crossdir_tgt:
74  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
75  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
76  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
77  **/
78
79 static void display_rename_stats(struct seq_file *seq, char *name,
80                                  struct obd_histogram *hist)
81 {
82         unsigned long tot, t, cum = 0;
83         int i;
84
85         tot = lprocfs_oh_sum(hist);
86         if (tot > 0)
87                 seq_printf(seq, "- %-15s\n", name);
88         /* dir size start from 4K, start i from 10(2^10) here */
89         for (i = 0; i < OBD_HIST_MAX; i++) {
90                 t = hist->oh_buckets[i];
91                 cum += t;
92                 if (cum == 0)
93                         continue;
94
95                 if (i < 10)
96                         seq_printf(seq, "%6s%d%s", " ", 1<< i, "bytes:");
97                 else if (i < 20)
98                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-10), "KB:");
99                 else
100                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-20), "MB:");
101
102                 seq_printf(seq, " { sample: %3lu, pct: %3u, cum_pct: %3u }\n",
103                            t, pct(t, tot), pct(cum, tot));
104
105                 if (cum == tot)
106                         break;
107         }
108 }
109
110 static void rename_stats_show(struct seq_file *seq,
111                               struct rename_stats *rename_stats)
112 {
113         struct timespec64 now;
114
115         /* this sampling races with updates */
116         ktime_get_real_ts64(&now);
117         seq_printf(seq, "rename_stats:\n");
118         seq_printf(seq, "- %-15s %llu.%9lu\n", "snapshot_time:",
119                    (s64)now.tv_sec, now.tv_nsec);
120
121         display_rename_stats(seq, "same_dir",
122                              &rename_stats->hist[RENAME_SAMEDIR_SIZE]);
123         display_rename_stats(seq, "crossdir_src",
124                              &rename_stats->hist[RENAME_CROSSDIR_SRC_SIZE]);
125         display_rename_stats(seq, "crossdir_tgt",
126                              &rename_stats->hist[RENAME_CROSSDIR_TGT_SIZE]);
127 }
128
129 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
130 {
131         struct mdt_device *mdt = seq->private;
132
133         rename_stats_show(seq, &mdt->mdt_rename_stats);
134
135         return 0;
136 }
137
138 static ssize_t
139 mdt_rename_stats_seq_write(struct file *file, const char __user *buf,
140                            size_t len, loff_t *off)
141 {
142         struct seq_file *seq = file->private_data;
143         struct mdt_device *mdt = seq->private;
144         int i;
145
146         for (i = 0; i < RENAME_LAST; i++)
147                 lprocfs_oh_clear(&mdt->mdt_rename_stats.hist[i]);
148
149         return len;
150 }
151 LPROC_SEQ_FOPS(mdt_rename_stats);
152
153 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
154 {
155         int i;
156
157         for (i = 0; i < RENAME_LAST; i++)
158                 spin_lock_init(&mdt->mdt_rename_stats.hist[i].oh_lock);
159
160         return lprocfs_obd_seq_create(mdt2obd_dev(mdt), "rename_stats", 0644,
161                                       &mdt_rename_stats_fops, mdt);
162 }
163
164 void mdt_rename_counter_tally(struct mdt_thread_info *info,
165                               struct mdt_device *mdt,
166                               struct ptlrpc_request *req,
167                               struct mdt_object *src,
168                               struct mdt_object *tgt)
169 {
170         struct md_attr *ma = &info->mti_attr;
171         struct rename_stats *rstats = &mdt->mdt_rename_stats;
172         int rc;
173
174         ma->ma_need = MA_INODE;
175         ma->ma_valid = 0;
176         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
177         if (rc) {
178                 CERROR("%s: "DFID" attr_get, rc = %d\n",
179                        mdt_obd_name(mdt), PFID(mdt_object_fid(src)), rc);
180                 return;
181         }
182
183         if (src == tgt) {
184                 mdt_counter_incr(req, LPROC_MDT_SAMEDIR_RENAME);
185                 lprocfs_oh_tally_log2(&rstats->hist[RENAME_SAMEDIR_SIZE],
186                                       (unsigned int)ma->ma_attr.la_size);
187                 return;
188         }
189
190         mdt_counter_incr(req, LPROC_MDT_CROSSDIR_RENAME);
191         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_SRC_SIZE],
192                               (unsigned int)ma->ma_attr.la_size);
193
194         ma->ma_need = MA_INODE;
195         ma->ma_valid = 0;
196         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
197         if (rc) {
198                 CERROR("%s: "DFID" attr_get, rc = %d\n",
199                        mdt_obd_name(mdt), PFID(mdt_object_fid(tgt)), rc);
200                 return;
201         }
202
203         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_TGT_SIZE],
204                               (unsigned int)ma->ma_attr.la_size);
205 }
206
207 static ssize_t identity_expire_show(struct kobject *kobj,
208                                     struct attribute *attr, char *buf)
209 {
210         struct obd_device *obd = container_of(kobj, struct obd_device,
211                                               obd_kset.kobj);
212         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
213
214         return scnprintf(buf, PAGE_SIZE, "%lld\n",
215                          mdt->mdt_identity_cache->uc_entry_expire);
216 }
217
218 static ssize_t identity_expire_store(struct kobject *kobj,
219                                      struct attribute *attr,
220                                      const char *buffer, size_t count)
221 {
222         struct obd_device *obd = container_of(kobj, struct obd_device,
223                                               obd_kset.kobj);
224         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
225         time64_t val;
226         int rc;
227
228         rc = kstrtoll(buffer, 10, &val);
229         if (rc)
230                 return rc;
231
232         if (val < 0)
233                 return -ERANGE;
234
235         mdt->mdt_identity_cache->uc_entry_expire = val;
236
237         return count;
238 }
239 LUSTRE_RW_ATTR(identity_expire);
240
241 static ssize_t identity_acquire_expire_show(struct kobject *kobj,
242                                             struct attribute *attr, char *buf)
243 {
244         struct obd_device *obd = container_of(kobj, struct obd_device,
245                                               obd_kset.kobj);
246         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
247
248         return scnprintf(buf, PAGE_SIZE, "%lld\n",
249                          mdt->mdt_identity_cache->uc_acquire_expire);
250 }
251
252 static ssize_t identity_acquire_expire_store(struct kobject *kobj,
253                                              struct attribute *attr,
254                                              const char *buffer, size_t count)
255 {
256         struct obd_device *obd = container_of(kobj, struct obd_device,
257                                               obd_kset.kobj);
258         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
259         time64_t val;
260         int rc;
261
262         rc = kstrtoll(buffer, 0, &val);
263         if (rc)
264                 return rc;
265
266         if (val < 0 || val > INT_MAX)
267                 return -ERANGE;
268
269         mdt->mdt_identity_cache->uc_acquire_expire = val;
270
271         return count;
272 }
273 LUSTRE_RW_ATTR(identity_acquire_expire);
274
275 static ssize_t identity_upcall_show(struct kobject *kobj,
276                                     struct attribute *attr, char *buf)
277 {
278         struct obd_device *obd = container_of(kobj, struct obd_device,
279                                               obd_kset.kobj);
280         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
281         struct upcall_cache *hash = mdt->mdt_identity_cache;
282         int rc;
283
284         down_read(&hash->uc_upcall_rwsem);
285         rc = scnprintf(buf, PAGE_SIZE, "%s\n", hash->uc_upcall);
286         up_read(&hash->uc_upcall_rwsem);
287         return rc;
288 }
289
290 static ssize_t identity_upcall_store(struct kobject *kobj,
291                                      struct attribute *attr,
292                                      const char *buffer, size_t count)
293 {
294         struct obd_device *obd = container_of(kobj, struct obd_device,
295                                               obd_kset.kobj);
296         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
297         struct upcall_cache *hash = mdt->mdt_identity_cache;
298
299         if (count >= UC_CACHE_UPCALL_MAXPATH) {
300                 CERROR("%s: identity upcall too long\n", mdt_obd_name(mdt));
301                 return -EINVAL;
302         }
303
304         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
305         down_write(&hash->uc_upcall_rwsem);
306         sscanf(buffer, "%s", hash->uc_upcall);
307         up_write(&hash->uc_upcall_rwsem);
308
309         if (strcmp(hash->uc_name, mdt_obd_name(mdt)) != 0)
310                 CWARN("%s: write to upcall name %s\n",
311                       mdt_obd_name(mdt), hash->uc_upcall);
312
313         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
314                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
315                       "cause unexpected \"EACCESS\"\n", mdt_obd_name(mdt));
316
317         CDEBUG(D_CONFIG, "%s: identity upcall set to %s\n", mdt_obd_name(mdt),
318                hash->uc_upcall);
319         RETURN(count);
320 }
321 LUSTRE_RW_ATTR(identity_upcall);
322
323 static ssize_t identity_flush_store(struct kobject *kobj,
324                                     struct attribute *attr,
325                                     const char *buffer, size_t count)
326 {
327         struct obd_device *obd = container_of(kobj, struct obd_device,
328                                               obd_kset.kobj);
329         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
330         int uid;
331         int rc;
332
333         rc = kstrtoint(buffer, 0, &uid);
334         if (rc)
335                 return rc;
336
337         mdt_flush_identity(mdt->mdt_identity_cache, uid);
338         return count;
339 }
340 LUSTRE_WO_ATTR(identity_flush);
341
342 static ssize_t
343 lprocfs_identity_info_seq_write(struct file *file, const char __user *buffer,
344                                 size_t count, void *data)
345 {
346         struct seq_file   *m = file->private_data;
347         struct obd_device *obd = m->private;
348         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
349         struct identity_downcall_data *param;
350         int size = sizeof(*param), rc, checked = 0;
351
352 again:
353         if (count < size) {
354                 CERROR("%s: invalid data count = %lu, size = %d\n",
355                        mdt_obd_name(mdt), (unsigned long) count, size);
356                 return -EINVAL;
357         }
358
359         OBD_ALLOC(param, size);
360         if (param == NULL)
361                 return -ENOMEM;
362
363         if (copy_from_user(param, buffer, size)) {
364                 CERROR("%s: bad identity data\n", mdt_obd_name(mdt));
365                 GOTO(out, rc = -EFAULT);
366         }
367
368         if (checked == 0) {
369                 checked = 1;
370                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
371                         CERROR("%s: MDS identity downcall bad params\n",
372                                mdt_obd_name(mdt));
373                         GOTO(out, rc = -EINVAL);
374                 }
375
376                 if (param->idd_nperms > N_PERMS_MAX) {
377                         CERROR("%s: perm count %d more than maximum %d\n",
378                                mdt_obd_name(mdt), param->idd_nperms,
379                                N_PERMS_MAX);
380                         GOTO(out, rc = -EINVAL);
381                 }
382
383                 if (param->idd_ngroups > NGROUPS_MAX) {
384                         CERROR("%s: group count %d more than maximum %d\n",
385                                mdt_obd_name(mdt), param->idd_ngroups,
386                                NGROUPS_MAX);
387                         GOTO(out, rc = -EINVAL);
388                 }
389
390                 if (param->idd_ngroups) {
391                         rc = param->idd_ngroups; /* save idd_ngroups */
392                         OBD_FREE(param, size);
393                         size = offsetof(struct identity_downcall_data,
394                                         idd_groups[rc]);
395                         goto again;
396                 }
397         }
398
399         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
400                                    param->idd_uid, param);
401
402 out:
403         if (param != NULL)
404                 OBD_FREE(param, size);
405
406         return rc ? rc : count;
407 }
408 LPROC_SEQ_FOPS_WR_ONLY(mdt, identity_info);
409
410 static int mdt_site_stats_seq_show(struct seq_file *m, void *data)
411 {
412         struct obd_device *obd = m->private;
413         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
414
415         return lu_site_stats_seq_print(mdt_lu_site(mdt), m);
416 }
417 LPROC_SEQ_FOPS_RO(mdt_site_stats);
418
419 #define BUFLEN (UUID_MAX + 4)
420
421 static ssize_t
422 lprocfs_mds_evict_client_seq_write(struct file *file, const char __user *buf,
423                                    size_t count, loff_t *off)
424 {
425         struct seq_file   *m = file->private_data;
426         struct obd_device *obd = m->private;
427         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
428         char *kbuf;
429         char *tmpbuf;
430         int rc = 0;
431
432         OBD_ALLOC(kbuf, BUFLEN);
433         if (kbuf == NULL)
434                 return -ENOMEM;
435
436         /*
437          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
438          * bytes into kbuf, to ensure that the string is NUL-terminated.
439          * UUID_MAX should include a trailing NUL already.
440          */
441         if (copy_from_user(kbuf, buf, min_t(unsigned long, BUFLEN - 1, count)))
442                 GOTO(out, rc = -EFAULT);
443         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
444
445         if (strncmp(tmpbuf, "nid:", 4) != 0) {
446                 count = lprocfs_evict_client_seq_write(file, buf, count, off);
447                 goto out;
448         }
449
450         if (mdt->mdt_opts.mo_evict_tgt_nids) {
451                 rc = obd_set_info_async(NULL, mdt->mdt_child_exp,
452                                         sizeof(KEY_EVICT_BY_NID),
453                                         KEY_EVICT_BY_NID,
454                                         strlen(tmpbuf + 4) + 1,
455                                         tmpbuf + 4, NULL);
456                 if (rc)
457                         CERROR("Failed to evict nid %s from OSTs: rc %d\n",
458                                tmpbuf + 4, rc);
459         }
460
461         /* See the comments in function lprocfs_wr_evict_client()
462          * in ptlrpc/lproc_ptlrpc.c for details. - jay */
463         class_incref(obd, __func__, current);
464         obd_export_evict_by_nid(obd, tmpbuf + 4);
465         class_decref(obd, __func__, current);
466
467
468 out:
469         OBD_FREE(kbuf, BUFLEN);
470         return rc < 0 ? rc : count;
471 }
472
473 #undef BUFLEN
474
475 static ssize_t evict_tgt_nids_show(struct kobject *kobj,
476                                    struct attribute *attr, char *buf)
477 {
478         struct obd_device *obd = container_of(kobj, struct obd_device,
479                                               obd_kset.kobj);
480         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
481
482         return scnprintf(buf, PAGE_SIZE, "%u\n",
483                          mdt->mdt_opts.mo_evict_tgt_nids);
484 }
485
486 static ssize_t evict_tgt_nids_store(struct kobject *kobj,
487                                     struct attribute *attr, const char *buffer,
488                                     size_t count)
489 {
490         struct obd_device *obd = container_of(kobj, struct obd_device,
491                                               obd_kset.kobj);
492         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
493         bool val;
494         int rc;
495
496         rc = kstrtobool(buffer, &val);
497         if (rc)
498                 return rc;
499
500         mdt->mdt_opts.mo_evict_tgt_nids = val;
501         return count;
502 }
503 LUSTRE_RW_ATTR(evict_tgt_nids);
504
505 static ssize_t commit_on_sharing_show(struct kobject *kobj,
506                                       struct attribute *attr, char *buf)
507 {
508         struct obd_device *obd = container_of(kobj, struct obd_device,
509                                               obd_kset.kobj);
510         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
511
512         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt_cos_is_enabled(mdt));
513 }
514
515 static ssize_t commit_on_sharing_store(struct kobject *kobj,
516                                        struct attribute *attr,
517                                        const char *buffer, size_t count)
518 {
519         struct obd_device *obd = container_of(kobj, struct obd_device,
520                                               obd_kset.kobj);
521         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
522         bool val;
523         int rc;
524
525         rc = kstrtobool(buffer, &val);
526         if (rc)
527                 return rc;
528
529         mdt_enable_cos(mdt, val);
530         return count;
531 }
532 LUSTRE_RW_ATTR(commit_on_sharing);
533
534 static ssize_t local_recovery_show(struct kobject *kobj,
535                                       struct attribute *attr, char *buf)
536 {
537         struct obd_device *obd = container_of(kobj, struct obd_device,
538                                               obd_kset.kobj);
539
540         return scnprintf(buf, PAGE_SIZE, "%u\n",
541                          obd->u.obt.obt_lut->lut_local_recovery);
542 }
543
544 static ssize_t local_recovery_store(struct kobject *kobj,
545                                        struct attribute *attr,
546                                        const char *buffer, size_t count)
547 {
548         struct obd_device *obd = container_of(kobj, struct obd_device,
549                                               obd_kset.kobj);
550         bool val;
551         int rc;
552
553         rc = kstrtobool(buffer, &val);
554         if (rc)
555                 return rc;
556
557         obd->u.obt.obt_lut->lut_local_recovery = !!val;
558         return count;
559 }
560 LUSTRE_RW_ATTR(local_recovery);
561
562 static int mdt_root_squash_seq_show(struct seq_file *m, void *data)
563 {
564         struct obd_device *obd = m->private;
565         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
566         struct root_squash_info *squash = &mdt->mdt_squash;
567
568         seq_printf(m, "%u:%u\n", squash->rsi_uid,
569                    squash->rsi_gid);
570         return 0;
571 }
572
573 static ssize_t
574 mdt_root_squash_seq_write(struct file *file, const char __user *buffer,
575                           size_t count, loff_t *off)
576 {
577         struct seq_file   *m = file->private_data;
578         struct obd_device *obd = m->private;
579         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
580         struct root_squash_info *squash = &mdt->mdt_squash;
581
582         return lprocfs_wr_root_squash(buffer, count, squash,
583                                       mdt_obd_name(mdt));
584 }
585 LPROC_SEQ_FOPS(mdt_root_squash);
586
587 static int mdt_nosquash_nids_seq_show(struct seq_file *m, void *data)
588 {
589         struct obd_device *obd = m->private;
590         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
591         struct root_squash_info *squash = &mdt->mdt_squash;
592         int len = 0;
593
594         spin_lock(&squash->rsi_lock);
595         if (!list_empty(&squash->rsi_nosquash_nids)) {
596                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
597                                         &squash->rsi_nosquash_nids);
598                 m->count += len;
599                 seq_putc(m, '\n');
600         } else
601                 seq_puts(m, "NONE\n");
602         spin_unlock(&squash->rsi_lock);
603
604         return 0;
605 }
606
607 static ssize_t
608 mdt_nosquash_nids_seq_write(struct file *file, const char __user *buffer,
609                             size_t count, loff_t *off)
610 {
611         struct seq_file   *m = file->private_data;
612         struct obd_device *obd = m->private;
613         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
614         struct root_squash_info *squash = &mdt->mdt_squash;
615
616         return lprocfs_wr_nosquash_nids(buffer, count, squash,
617                                         mdt_obd_name(mdt));
618 }
619 LPROC_SEQ_FOPS(mdt_nosquash_nids);
620
621 static ssize_t enable_remote_dir_show(struct kobject *kobj,
622                                       struct attribute *attr, char *buf)
623 {
624         struct obd_device *obd = container_of(kobj, struct obd_device,
625                                               obd_kset.kobj);
626         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
627
628         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_remote_dir);
629 }
630
631 static ssize_t enable_remote_dir_store(struct kobject *kobj,
632                                        struct attribute *attr,
633                                        const char *buffer, size_t count)
634 {
635         struct obd_device *obd = container_of(kobj, struct obd_device,
636                                               obd_kset.kobj);
637         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
638         bool val;
639         int rc;
640
641         rc = kstrtobool(buffer, &val);
642         if (rc)
643                 return rc;
644
645         mdt->mdt_enable_remote_dir = val;
646         return count;
647 }
648 LUSTRE_RW_ATTR(enable_remote_dir);
649
650 static ssize_t enable_remote_dir_gid_show(struct kobject *kobj,
651                                           struct attribute *attr, char *buf)
652 {
653         struct obd_device *obd = container_of(kobj, struct obd_device,
654                                               obd_kset.kobj);
655         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
656
657         return scnprintf(buf, PAGE_SIZE, "%d\n",
658                          (int)mdt->mdt_enable_remote_dir_gid);
659 }
660
661 static ssize_t enable_remote_dir_gid_store(struct kobject *kobj,
662                                            struct attribute *attr,
663                                            const char *buffer, size_t count)
664 {
665         struct obd_device *obd = container_of(kobj, struct obd_device,
666                                               obd_kset.kobj);
667         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
668         int val;
669         int rc;
670
671         rc = kstrtoint(buffer, 0, &val);
672         if (rc)
673                 return rc;
674
675         mdt->mdt_enable_remote_dir_gid = val;
676         return count;
677 }
678 LUSTRE_RW_ATTR(enable_remote_dir_gid);
679
680 static ssize_t enable_chprojid_gid_show(struct kobject *kobj,
681                                         struct attribute *attr, char *buf)
682 {
683         struct obd_device *obd = container_of(kobj, struct obd_device,
684                                               obd_kset.kobj);
685         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
686
687         return scnprintf(buf, PAGE_SIZE, "%d\n",
688                          (int)mdt->mdt_enable_chprojid_gid);
689 }
690
691 static ssize_t enable_chprojid_gid_store(struct kobject *kobj,
692                                          struct attribute *attr,
693                                          const char *buffer, size_t count)
694 {
695         struct obd_device *obd = container_of(kobj, struct obd_device,
696                                               obd_kset.kobj);
697         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
698         int val;
699         int rc;
700
701         rc = kstrtoint(buffer, 0, &val);
702         if (rc)
703                 return rc;
704
705         mdt->mdt_enable_chprojid_gid = val;
706         return count;
707 }
708 LUSTRE_RW_ATTR(enable_chprojid_gid);
709
710 static ssize_t enable_striped_dir_show(struct kobject *kobj,
711                                        struct attribute *attr, char *buf)
712 {
713         struct obd_device *obd = container_of(kobj, struct obd_device,
714                                               obd_kset.kobj);
715         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
716
717         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_striped_dir);
718 }
719
720 static ssize_t enable_striped_dir_store(struct kobject *kobj,
721                                         struct attribute *attr,
722                                         const char *buffer, size_t count)
723 {
724         struct obd_device *obd = container_of(kobj, struct obd_device,
725                                               obd_kset.kobj);
726         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
727         bool val;
728         int rc;
729
730         rc = kstrtobool(buffer, &val);
731         if (rc)
732                 return rc;
733
734         mdt->mdt_enable_striped_dir = val;
735         return count;
736 }
737 LUSTRE_RW_ATTR(enable_striped_dir);
738
739 static ssize_t enable_dir_migration_show(struct kobject *kobj,
740                                          struct attribute *attr, char *buf)
741 {
742         struct obd_device *obd = container_of(kobj, struct obd_device,
743                                               obd_kset.kobj);
744         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
745
746         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_dir_migration);
747 }
748
749 static ssize_t enable_dir_migration_store(struct kobject *kobj,
750                                           struct attribute *attr,
751                                           const char *buffer, size_t count)
752 {
753         struct obd_device *obd = container_of(kobj, struct obd_device,
754                                               obd_kset.kobj);
755         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
756         bool val;
757         int rc;
758
759         rc = kstrtobool(buffer, &val);
760         if (rc)
761                 return rc;
762
763         mdt->mdt_enable_dir_migration = val;
764         return count;
765 }
766 LUSTRE_RW_ATTR(enable_dir_migration);
767
768 static ssize_t enable_dir_restripe_show(struct kobject *kobj,
769                                         struct attribute *attr, char *buf)
770 {
771         struct obd_device *obd = container_of(kobj, struct obd_device,
772                                               obd_kset.kobj);
773         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
774
775         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_dir_restripe);
776 }
777
778 static ssize_t enable_dir_restripe_store(struct kobject *kobj,
779                                          struct attribute *attr,
780                                          const char *buffer, size_t count)
781 {
782         struct obd_device *obd = container_of(kobj, struct obd_device,
783                                               obd_kset.kobj);
784         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
785         bool val;
786         int rc;
787
788         rc = kstrtobool(buffer, &val);
789         if (rc)
790                 return rc;
791
792         mdt->mdt_enable_dir_restripe = val;
793         return count;
794 }
795 LUSTRE_RW_ATTR(enable_dir_restripe);
796
797 /**
798  * Show MDT async commit count.
799  *
800  * @m           seq_file handle
801  * @data        unused for single entry
802  *
803  * Return:      0 on success
804  *              negative value on error
805  */
806 static ssize_t async_commit_count_show(struct kobject *kobj,
807                                        struct attribute *attr, char *buf)
808 {
809         struct obd_device *obd = container_of(kobj, struct obd_device,
810                                               obd_kset.kobj);
811         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
812
813         return scnprintf(buf, PAGE_SIZE, "%d\n",
814                          atomic_read(&mdt->mdt_async_commit_count));
815 }
816
817 static ssize_t async_commit_count_store(struct kobject *kobj,
818                                         struct attribute *attr,
819                                         const char *buffer, size_t count)
820 {
821         struct obd_device *obd = container_of(kobj, struct obd_device,
822                                               obd_kset.kobj);
823         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
824         int val;
825         int rc;
826
827         rc = kstrtoint(buffer, 10, &val);
828         if (rc)
829                 return rc;
830
831         atomic_set(&mdt->mdt_async_commit_count, val);
832
833         return count;
834 }
835 LUSTRE_RW_ATTR(async_commit_count);
836
837 /**
838  * Show MDT sync count.
839  *
840  * \param[in] m         seq_file handle
841  * \param[in] data      unused for single entry
842  *
843  * \retval              0 on success
844  * \retval              negative value on error
845  */
846 static ssize_t sync_count_show(struct kobject *kobj, struct attribute *attr,
847                                char *buf)
848 {
849         struct obd_device *obd = container_of(kobj, struct obd_device,
850                                               obd_kset.kobj);
851         struct lu_target *tgt = obd->u.obt.obt_lut;
852
853         return scnprintf(buf, PAGE_SIZE, "%d\n",
854                          atomic_read(&tgt->lut_sync_count));
855 }
856
857 static ssize_t sync_count_store(struct kobject *kobj, struct attribute *attr,
858                                 const char *buffer, size_t count)
859 {
860         struct obd_device *obd = container_of(kobj, struct obd_device,
861                                               obd_kset.kobj);
862         struct lu_target *tgt = obd->u.obt.obt_lut;
863         int val;
864         int rc;
865
866         rc = kstrtoint(buffer, 0, &val);
867         if (rc)
868                 return rc;
869
870         atomic_set(&tgt->lut_sync_count, val);
871
872         return count;
873 }
874 LUSTRE_RW_ATTR(sync_count);
875
876 static const char *dom_open_lock_modes[NUM_DOM_LOCK_ON_OPEN_MODES] = {
877         [NO_DOM_LOCK_ON_OPEN] = "never",
878         [TRYLOCK_DOM_ON_OPEN] = "trylock",
879         [ALWAYS_DOM_LOCK_ON_OPEN] = "always",
880 };
881
882 /* This must be longer than the longest string above */
883 #define DOM_LOCK_MODES_MAXLEN 16
884
885 /**
886  * Show MDT policy for data prefetch on open for DoM files..
887  *
888  * \param[in] m         seq_file handle
889  * \param[in] data      unused
890  *
891  * \retval              0 on success
892  * \retval              negative value on error
893  */
894 static ssize_t dom_lock_show(struct kobject *kobj, struct attribute *attr,
895                              char *buf)
896 {
897         struct obd_device *obd = container_of(kobj, struct obd_device,
898                                               obd_kset.kobj);
899         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
900
901         return scnprintf(buf, PAGE_SIZE, "%s\n",
902                          dom_open_lock_modes[mdt->mdt_opts.mo_dom_lock]);
903 }
904
905 /**
906  * Change MDT policy for data prefetch on open for DoM files.
907  *
908  * This variable defines how DOM lock is taken at open enqueue.
909  * There are three possible modes:
910  * 1) never - never take DoM lock on open. DoM lock will be taken as separate
911  *    IO lock with own enqueue.
912  * 2) trylock - DoM lock will be taken only if non-blocked.
913  * 3) always - DoM lock will be taken always even if it is blocking lock.
914  *
915  * If dom_read_open is enabled too then DoM lock is taken in PR mode and
916  * is paired with LAYOUT lock when possible.
917  *
918  * \param[in] file      proc file
919  * \param[in] buffer    string which represents policy
920  * \param[in] count     \a buffer length
921  * \param[in] off       unused for single entry
922  *
923  * \retval              \a count on success
924  * \retval              negative number on error
925  */
926 static ssize_t dom_lock_store(struct kobject *kobj, struct attribute *attr,
927                               const char *buffer, size_t count)
928 {
929         struct obd_device *obd = container_of(kobj, struct obd_device,
930                                               obd_kset.kobj);
931         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
932         int val = -1;
933         int i, rc;
934
935         if (count == 0 || count >= DOM_LOCK_MODES_MAXLEN)
936                 return -EINVAL;
937
938         for (i = 0 ; i < NUM_DOM_LOCK_ON_OPEN_MODES; i++) {
939                 /* buffer might have '\n' but using strlen() avoids it */
940                 if (strncmp(buffer, dom_open_lock_modes[i],
941                             strlen(dom_open_lock_modes[i])) == 0) {
942                         val = i;
943                         break;
944                 }
945         }
946
947         /* Legacy numeric codes */
948         if (val == -1) {
949                 rc = kstrtoint(buffer, 0, &val);
950                 if (rc)
951                         return rc;
952         }
953
954         if (val < 0 || val >= NUM_DOM_LOCK_ON_OPEN_MODES)
955                 return -EINVAL;
956
957         mdt->mdt_opts.mo_dom_lock = val;
958         return count;
959 }
960 LUSTRE_RW_ATTR(dom_lock);
961
962 /**
963  * Show MDT policy for data prefetch on open for DoM files..
964  *
965  * \param[in] m         seq_file handle
966  * \param[in] data      unused
967  *
968  * \retval              0 on success
969  * \retval              negative value on error
970  */
971 static ssize_t dom_read_open_show(struct kobject *kobj,
972                                   struct attribute *attr, char *buf)
973 {
974         struct obd_device *obd = container_of(kobj, struct obd_device,
975                                               obd_kset.kobj);
976         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
977
978         return scnprintf(buf, PAGE_SIZE, "%u\n",
979                          !!mdt->mdt_opts.mo_dom_read_open);
980 }
981
982 /**
983  * Modify MDT policy for data prefetch on open for DoM files.
984  *
985  * If enabled then Data-on-MDT file data may be read during open and
986  * returned back in reply. It works only with mo_dom_lock enabled.
987  *
988  * \param[in] file      proc file
989  * \param[in] buffer    string which represents policy
990  * \param[in] count     \a buffer length
991  * \param[in] off       unused for single entry
992  *
993  * \retval              \a count on success
994  * \retval              negative number on error
995  */
996 static ssize_t dom_read_open_store(struct kobject *kobj,
997                                    struct attribute *attr, const char *buffer,
998                                    size_t count)
999 {
1000         struct obd_device *obd = container_of(kobj, struct obd_device,
1001                                               obd_kset.kobj);
1002         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1003         bool val;
1004         int rc;
1005
1006         rc = kstrtobool(buffer, &val);
1007         if (rc)
1008                 return rc;
1009
1010         mdt->mdt_opts.mo_dom_read_open = !!val;
1011         return count;
1012 }
1013 LUSTRE_RW_ATTR(dom_read_open);
1014
1015 static ssize_t migrate_hsm_allowed_show(struct kobject *kobj,
1016                                         struct attribute *attr, char *buf)
1017 {
1018         struct obd_device *obd = container_of(kobj, struct obd_device,
1019                                               obd_kset.kobj);
1020         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1021
1022         return scnprintf(buf, PAGE_SIZE, "%u\n",
1023                          mdt->mdt_opts.mo_migrate_hsm_allowed);
1024 }
1025
1026 static ssize_t migrate_hsm_allowed_store(struct kobject *kobj,
1027                                          struct attribute *attr,
1028                                          const char *buffer, size_t count)
1029 {
1030         struct obd_device *obd = container_of(kobj, struct obd_device,
1031                                               obd_kset.kobj);
1032         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1033         bool val;
1034         int rc;
1035
1036         rc = kstrtobool(buffer, &val);
1037         if (rc)
1038                 return rc;
1039
1040         mdt->mdt_opts.mo_migrate_hsm_allowed = val;
1041         return count;
1042 }
1043 LUSTRE_RW_ATTR(migrate_hsm_allowed);
1044
1045 static ssize_t readonly_show(struct kobject *kobj, struct attribute *attr,
1046                              char *buf)
1047 {
1048         struct obd_device *obd = container_of(kobj, struct obd_device,
1049                                               obd_kset.kobj);
1050         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1051
1052         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_readonly);
1053 }
1054
1055 static ssize_t readonly_store(struct kobject *kobj, struct attribute *attr,
1056                               const char *buffer, size_t count)
1057 {
1058         struct obd_device *obd = container_of(kobj, struct obd_device,
1059                                               obd_kset.kobj);
1060         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1061         bool val;
1062         int rc;
1063
1064         rc = kstrtobool(buffer, &val);
1065         if (rc)
1066                 return rc;
1067
1068         mdt->mdt_readonly = val;
1069         return count;
1070 }
1071 LUSTRE_RW_ATTR(readonly);
1072
1073 static ssize_t enable_remote_rename_show(struct kobject *kobj,
1074                                          struct attribute *attr,
1075                                          char *buf)
1076 {
1077         struct obd_device *obd = container_of(kobj, struct obd_device,
1078                                               obd_kset.kobj);
1079         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1080
1081         return scnprintf(buf, PAGE_SIZE, "%u\n",
1082                          mdt->mdt_enable_remote_rename);
1083 }
1084
1085 static ssize_t enable_remote_rename_store(struct kobject *kobj,
1086                                           struct attribute *attr,
1087                                           const char *buffer, size_t count)
1088 {
1089         struct obd_device *obd = container_of(kobj, struct obd_device,
1090                                               obd_kset.kobj);
1091         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1092         bool val;
1093         int rc;
1094
1095         rc = kstrtobool(buffer, &val);
1096         if (rc)
1097                 return rc;
1098
1099         mdt->mdt_enable_remote_rename = val;
1100         return count;
1101 }
1102 LUSTRE_RW_ATTR(enable_remote_rename);
1103
1104 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
1105 LPROC_SEQ_FOPS_WR_ONLY(mdt, mds_evict_client);
1106 LUSTRE_RW_ATTR(job_cleanup_interval);
1107 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
1108 LUSTRE_RW_ATTR(hsm_control);
1109
1110 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
1111 LUSTRE_RW_ATTR(recovery_time_hard);
1112 LUSTRE_RW_ATTR(recovery_time_soft);
1113 LUSTRE_RW_ATTR(ir_factor);
1114
1115 LUSTRE_RO_ATTR(tot_dirty);
1116 LUSTRE_RO_ATTR(tot_granted);
1117 LUSTRE_RO_ATTR(tot_pending);
1118 LUSTRE_RW_ATTR(grant_compat_disable);
1119 LUSTRE_RO_ATTR(instance);
1120
1121 LUSTRE_RO_ATTR(num_exports);
1122
1123 static struct attribute *mdt_attrs[] = {
1124         &lustre_attr_tot_dirty.attr,
1125         &lustre_attr_tot_granted.attr,
1126         &lustre_attr_tot_pending.attr,
1127         &lustre_attr_grant_compat_disable.attr,
1128         &lustre_attr_instance.attr,
1129         &lustre_attr_recovery_time_hard.attr,
1130         &lustre_attr_recovery_time_soft.attr,
1131         &lustre_attr_ir_factor.attr,
1132         &lustre_attr_num_exports.attr,
1133         &lustre_attr_identity_expire.attr,
1134         &lustre_attr_identity_acquire_expire.attr,
1135         &lustre_attr_identity_upcall.attr,
1136         &lustre_attr_identity_flush.attr,
1137         &lustre_attr_evict_tgt_nids.attr,
1138         &lustre_attr_enable_remote_dir.attr,
1139         &lustre_attr_enable_remote_dir_gid.attr,
1140         &lustre_attr_enable_chprojid_gid.attr,
1141         &lustre_attr_enable_striped_dir.attr,
1142         &lustre_attr_enable_dir_migration.attr,
1143         &lustre_attr_enable_dir_restripe.attr,
1144         &lustre_attr_enable_remote_rename.attr,
1145         &lustre_attr_commit_on_sharing.attr,
1146         &lustre_attr_local_recovery.attr,
1147         &lustre_attr_async_commit_count.attr,
1148         &lustre_attr_sync_count.attr,
1149         &lustre_attr_dom_lock.attr,
1150         &lustre_attr_dom_read_open.attr,
1151         &lustre_attr_migrate_hsm_allowed.attr,
1152         &lustre_attr_hsm_control.attr,
1153         &lustre_attr_job_cleanup_interval.attr,
1154         &lustre_attr_readonly.attr,
1155         NULL,
1156 };
1157
1158 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
1159         { .name =       "recovery_status",
1160           .fops =       &mdt_recovery_status_fops               },
1161         { .name =       "identity_info",
1162           .fops =       &mdt_identity_info_fops                 },
1163         { .name =       "site_stats",
1164           .fops =       &mdt_site_stats_fops                    },
1165         { .name =       "evict_client",
1166           .fops =       &mdt_mds_evict_client_fops              },
1167         { .name =       "hash_stats",
1168           .fops =       &mdt_hash_fops                          },
1169         { .name =       "root_squash",
1170           .fops =       &mdt_root_squash_fops                   },
1171         { .name =       "nosquash_nids",
1172           .fops =       &mdt_nosquash_nids_fops                 },
1173         { NULL }
1174 };
1175
1176 static int
1177 lprocfs_mdt_print_open_files(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1178                              struct hlist_node *hnode, void *v)
1179 {
1180         struct obd_export       *exp = cfs_hash_object(hs, hnode);
1181         struct seq_file         *seq = v;
1182
1183         if (exp->exp_lock_hash != NULL) {
1184                 struct mdt_export_data  *med = &exp->exp_mdt_data;
1185                 struct mdt_file_data    *mfd;
1186
1187                 spin_lock(&med->med_open_lock);
1188                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1189                         seq_printf(seq, DFID"\n",
1190                                    PFID(mdt_object_fid(mfd->mfd_object)));
1191                 }
1192                 spin_unlock(&med->med_open_lock);
1193         }
1194
1195         return 0;
1196 }
1197
1198 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
1199 {
1200         struct nid_stat *stats = seq->private;
1201         struct obd_device *obd = stats->nid_obd;
1202
1203         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
1204                               lprocfs_mdt_print_open_files, seq);
1205
1206         return 0;
1207 }
1208
1209 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
1210 {
1211         struct seq_file         *seq;
1212         int                     rc;
1213
1214         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
1215         if (rc != 0)
1216                 return rc;
1217
1218         seq = file->private_data;
1219         seq->private = PDE_DATA(inode);
1220
1221         return 0;
1222 }
1223
1224 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
1225 {
1226         struct obd_export *exp = req->rq_export;
1227
1228         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
1229                 lprocfs_counter_incr(exp->exp_obd->obd_md_stats,
1230                                      opcode + LPROC_MD_LAST_OPC);
1231         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1232                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
1233         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
1234             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1235                 lprocfs_job_stats_log(exp->exp_obd,
1236                                       lustre_msg_get_jobid(req->rq_reqmsg),
1237                                       opcode, 1);
1238 }
1239
1240 static const char * const mdt_stats[] = {
1241         [LPROC_MDT_OPEN]                = "open",
1242         [LPROC_MDT_CLOSE]               = "close",
1243         [LPROC_MDT_MKNOD]               = "mknod",
1244         [LPROC_MDT_LINK]                = "link",
1245         [LPROC_MDT_UNLINK]              = "unlink",
1246         [LPROC_MDT_MKDIR]               = "mkdir",
1247         [LPROC_MDT_RMDIR]               = "rmdir",
1248         [LPROC_MDT_RENAME]              = "rename",
1249         [LPROC_MDT_GETATTR]             = "getattr",
1250         [LPROC_MDT_SETATTR]             = "setattr",
1251         [LPROC_MDT_GETXATTR]            = "getxattr",
1252         [LPROC_MDT_SETXATTR]            = "setxattr",
1253         [LPROC_MDT_STATFS]              = "statfs",
1254         [LPROC_MDT_SYNC]                = "sync",
1255         [LPROC_MDT_SAMEDIR_RENAME]      = "samedir_rename",
1256         [LPROC_MDT_CROSSDIR_RENAME]     = "crossdir_rename",
1257         [LPROC_MDT_IO_READ]             = "read_bytes",
1258         [LPROC_MDT_IO_WRITE]            = "write_bytes",
1259         [LPROC_MDT_IO_PUNCH]            = "punch",
1260         [LPROC_MDT_MIGRATE]             = "migrate",
1261 };
1262
1263 void mdt_stats_counter_init(struct lprocfs_stats *stats)
1264 {
1265         int idx;
1266
1267         LASSERT(stats && stats->ls_num >= ARRAY_SIZE(mdt_stats));
1268
1269         for (idx = 0; idx < ARRAY_SIZE(mdt_stats); idx++) {
1270                 int flags = 0;
1271
1272                 if (idx == LPROC_MDT_IO_WRITE || idx == LPROC_MDT_IO_READ)
1273                         flags = LPROCFS_CNTR_AVGMINMAX;
1274
1275                 lprocfs_counter_init(stats, idx, flags, mdt_stats[idx], "reqs");
1276         }
1277 }
1278
1279 int mdt_tunables_init(struct mdt_device *mdt, const char *name)
1280 {
1281         struct obd_device *obd = mdt2obd_dev(mdt);
1282         int rc;
1283         int i;
1284
1285         ENTRY;
1286         LASSERT(name != NULL);
1287
1288         obd->obd_ktype.default_attrs = mdt_attrs;
1289         obd->obd_vars = lprocfs_mdt_obd_vars;
1290         rc = lprocfs_obd_setup(obd, true);
1291         if (rc) {
1292                 CERROR("%s: cannot create proc entries: rc = %d\n",
1293                        mdt_obd_name(mdt), rc);
1294                 return rc;
1295         }
1296
1297         rc = tgt_tunables_init(&mdt->mdt_lut);
1298         if (rc) {
1299                 CERROR("%s: failed to init target tunables: rc = %d\n",
1300                        mdt_obd_name(mdt), rc);
1301                 return rc;
1302         }
1303
1304         rc = hsm_cdt_tunables_init(mdt);
1305         if (rc) {
1306                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
1307                        mdt_obd_name(mdt), rc);
1308                 return rc;
1309         }
1310
1311         obd->obd_proc_exports_entry = proc_mkdir("exports",
1312                                                  obd->obd_proc_entry);
1313         if (obd->obd_proc_exports_entry)
1314                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
1315                                    obd, &mdt_nid_stats_clear_fops);
1316
1317         rc = lprocfs_alloc_md_stats(obd, ARRAY_SIZE(mdt_stats));
1318         if (rc)
1319                 return rc;
1320
1321         /* add additional MDT md_stats after the default ones */
1322         for (i = 0; i < ARRAY_SIZE(mdt_stats); i++) {
1323                 int idx = i + LPROC_MD_LAST_OPC;
1324                 int flags = 0;
1325
1326                 if (idx == LPROC_MDT_IO_WRITE || idx == LPROC_MDT_IO_READ)
1327                         flags = LPROCFS_CNTR_AVGMINMAX;
1328
1329                 lprocfs_counter_init(obd->obd_md_stats, idx, flags,
1330                                      mdt_stats[i], "reqs");
1331         }
1332
1333         rc = lprocfs_job_stats_init(obd, ARRAY_SIZE(mdt_stats),
1334                                     mdt_stats_counter_init);
1335
1336         rc = lproc_mdt_attach_rename_seqstat(mdt);
1337         if (rc)
1338                 CERROR("%s: MDT can not create rename stats rc = %d\n",
1339                        mdt_obd_name(mdt), rc);
1340
1341         RETURN(rc);
1342 }
1343
1344 void mdt_tunables_fini(struct mdt_device *mdt)
1345 {
1346         struct obd_device *obd = mdt2obd_dev(mdt);
1347
1348         if (obd->obd_proc_exports_entry != NULL) {
1349                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
1350                 obd->obd_proc_exports_entry = NULL;
1351         }
1352
1353         lprocfs_free_per_client_stats(obd);
1354         /* hsm_cdt_tunables is disabled earlier than this to avoid
1355          * coordinator restart.
1356          */
1357         hsm_cdt_tunables_fini(mdt);
1358         tgt_tunables_fini(&mdt->mdt_lut);
1359         lprocfs_obd_cleanup(obd);
1360         lprocfs_free_md_stats(obd);
1361         lprocfs_free_obd_stats(obd);
1362         lprocfs_job_stats_fini(obd);
1363 }