Whamcloud - gitweb
8c8f50bab1409ed5916a54dbf83b319b7b8975f1
[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 #define pct(a, b) (b ? a * 100 / b : 0)
80
81 static void display_rename_stats(struct seq_file *seq, char *name,
82                                  struct obd_histogram *hist)
83 {
84         unsigned long tot, t, cum = 0;
85         int i;
86
87         tot = lprocfs_oh_sum(hist);
88         if (tot > 0)
89                 seq_printf(seq, "- %-15s\n", name);
90         /* dir size start from 4K, start i from 10(2^10) here */
91         for (i = 0; i < OBD_HIST_MAX; i++) {
92                 t = hist->oh_buckets[i];
93                 cum += t;
94                 if (cum == 0)
95                         continue;
96
97                 if (i < 10)
98                         seq_printf(seq, "%6s%d%s", " ", 1<< i, "bytes:");
99                 else if (i < 20)
100                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-10), "KB:");
101                 else
102                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-20), "MB:");
103
104                 seq_printf(seq, " { sample: %3lu, pct: %3lu, cum_pct: %3lu }\n",
105                            t, pct(t, tot), pct(cum, tot));
106
107                 if (cum == tot)
108                         break;
109         }
110 }
111
112 static void rename_stats_show(struct seq_file *seq,
113                               struct rename_stats *rename_stats)
114 {
115         struct timespec64 now;
116
117         /* this sampling races with updates */
118         ktime_get_real_ts64(&now);
119         seq_printf(seq, "rename_stats:\n");
120         seq_printf(seq, "- %-15s %llu.%9lu\n", "snapshot_time:",
121                    (s64)now.tv_sec, now.tv_nsec);
122
123         display_rename_stats(seq, "same_dir",
124                              &rename_stats->hist[RENAME_SAMEDIR_SIZE]);
125         display_rename_stats(seq, "crossdir_src",
126                              &rename_stats->hist[RENAME_CROSSDIR_SRC_SIZE]);
127         display_rename_stats(seq, "crossdir_tgt",
128                              &rename_stats->hist[RENAME_CROSSDIR_TGT_SIZE]);
129 }
130
131 #undef pct
132
133 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
134 {
135         struct mdt_device *mdt = seq->private;
136
137         rename_stats_show(seq, &mdt->mdt_rename_stats);
138
139         return 0;
140 }
141
142 static ssize_t
143 mdt_rename_stats_seq_write(struct file *file, const char __user *buf,
144                            size_t len, loff_t *off)
145 {
146         struct seq_file *seq = file->private_data;
147         struct mdt_device *mdt = seq->private;
148         int i;
149
150         for (i = 0; i < RENAME_LAST; i++)
151                 lprocfs_oh_clear(&mdt->mdt_rename_stats.hist[i]);
152
153         return len;
154 }
155 LPROC_SEQ_FOPS(mdt_rename_stats);
156
157 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
158 {
159         int i;
160
161         for (i = 0; i < RENAME_LAST; i++)
162                 spin_lock_init(&mdt->mdt_rename_stats.hist[i].oh_lock);
163
164         return lprocfs_obd_seq_create(mdt2obd_dev(mdt), "rename_stats", 0644,
165                                       &mdt_rename_stats_fops, mdt);
166 }
167
168 void mdt_rename_counter_tally(struct mdt_thread_info *info,
169                               struct mdt_device *mdt,
170                               struct ptlrpc_request *req,
171                               struct mdt_object *src,
172                               struct mdt_object *tgt)
173 {
174         struct md_attr *ma = &info->mti_attr;
175         struct rename_stats *rstats = &mdt->mdt_rename_stats;
176         int rc;
177
178         ma->ma_need = MA_INODE;
179         ma->ma_valid = 0;
180         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
181         if (rc) {
182                 CERROR("%s: "DFID" attr_get, rc = %d\n",
183                        mdt_obd_name(mdt), PFID(mdt_object_fid(src)), rc);
184                 return;
185         }
186
187         if (src == tgt) {
188                 mdt_counter_incr(req, LPROC_MDT_SAMEDIR_RENAME);
189                 lprocfs_oh_tally_log2(&rstats->hist[RENAME_SAMEDIR_SIZE],
190                                       (unsigned int)ma->ma_attr.la_size);
191                 return;
192         }
193
194         mdt_counter_incr(req, LPROC_MDT_CROSSDIR_RENAME);
195         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_SRC_SIZE],
196                               (unsigned int)ma->ma_attr.la_size);
197
198         ma->ma_need = MA_INODE;
199         ma->ma_valid = 0;
200         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
201         if (rc) {
202                 CERROR("%s: "DFID" attr_get, rc = %d\n",
203                        mdt_obd_name(mdt), PFID(mdt_object_fid(tgt)), rc);
204                 return;
205         }
206
207         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_TGT_SIZE],
208                               (unsigned int)ma->ma_attr.la_size);
209 }
210
211 static int mdt_identity_expire_seq_show(struct seq_file *m, void *data)
212 {
213         struct obd_device *obd = m->private;
214         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
215
216         seq_printf(m, "%lld\n", mdt->mdt_identity_cache->uc_entry_expire);
217         return 0;
218 }
219
220 static ssize_t
221 mdt_identity_expire_seq_write(struct file *file, const char __user *buffer,
222                               size_t count, loff_t *off)
223 {
224         struct seq_file   *m = file->private_data;
225         struct obd_device *obd = m->private;
226         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
227         time64_t val;
228         int rc;
229
230         rc = lprocfs_str_to_s64(buffer, count, &val);
231         if (rc)
232                 return rc;
233
234         if (val < 0 || val > INT_MAX)
235                 return -ERANGE;
236
237         mdt->mdt_identity_cache->uc_entry_expire = val;
238
239         return count;
240 }
241 LPROC_SEQ_FOPS(mdt_identity_expire);
242
243 static int mdt_identity_acquire_expire_seq_show(struct seq_file *m, void *data)
244 {
245         struct obd_device *obd = m->private;
246         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
247
248         seq_printf(m, "%lld\n", mdt->mdt_identity_cache->uc_acquire_expire);
249         return 0;
250 }
251
252 static ssize_t
253 mdt_identity_acquire_expire_seq_write(struct file *file,
254                                       const char __user *buffer,
255                                       size_t count, loff_t *off)
256 {
257         struct seq_file   *m = file->private_data;
258         struct obd_device *obd = m->private;
259         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
260         time64_t val;
261         int rc;
262
263         rc = lprocfs_str_to_s64(buffer, count, &val);
264         if (rc)
265                 return rc;
266
267         if (val < 0 || val > INT_MAX)
268                 return -ERANGE;
269
270         mdt->mdt_identity_cache->uc_acquire_expire = val;
271
272         return count;
273 }
274 LPROC_SEQ_FOPS(mdt_identity_acquire_expire);
275
276 static int mdt_identity_upcall_seq_show(struct seq_file *m, void *data)
277 {
278         struct obd_device *obd = m->private;
279         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
280         struct upcall_cache *hash = mdt->mdt_identity_cache;
281
282         down_read(&hash->uc_upcall_rwsem);
283         seq_printf(m, "%s\n", hash->uc_upcall);
284         up_read(&hash->uc_upcall_rwsem);
285         return 0;
286 }
287
288 static ssize_t
289 mdt_identity_upcall_seq_write(struct file *file, const char __user *buffer,
290                               size_t count, loff_t *off)
291 {
292         struct seq_file         *m = file->private_data;
293         struct obd_device       *obd = m->private;
294         struct mdt_device       *mdt = mdt_dev(obd->obd_lu_dev);
295         struct upcall_cache     *hash = mdt->mdt_identity_cache;
296         int                      rc;
297         char                    *kernbuf;
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         OBD_ALLOC(kernbuf, count + 1);
304         if (kernbuf == NULL)
305                 GOTO(failed, rc = -ENOMEM);
306         if (copy_from_user(kernbuf, buffer, count))
307                 GOTO(failed, rc = -EFAULT);
308
309         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
310         down_write(&hash->uc_upcall_rwsem);
311         sscanf(kernbuf, "%s", hash->uc_upcall);
312         up_write(&hash->uc_upcall_rwsem);
313
314         if (strcmp(hash->uc_name, mdt_obd_name(mdt)) != 0)
315                 CWARN("%s: write to upcall name %s\n",
316                       mdt_obd_name(mdt), hash->uc_upcall);
317
318         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
319                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
320                       "cause unexpected \"EACCESS\"\n", mdt_obd_name(mdt));
321
322         CDEBUG(D_CONFIG, "%s: identity upcall set to %s\n", mdt_obd_name(mdt),
323                hash->uc_upcall);
324         OBD_FREE(kernbuf, count + 1);
325         RETURN(count);
326
327  failed:
328         if (kernbuf)
329                 OBD_FREE(kernbuf, count + 1);
330         RETURN(rc);
331 }
332 LPROC_SEQ_FOPS(mdt_identity_upcall);
333
334 static ssize_t
335 lprocfs_identity_flush_seq_write(struct file *file, const char __user *buffer,
336                                  size_t count, void *data)
337 {
338         struct seq_file   *m = file->private_data;
339         struct obd_device *obd = m->private;
340         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
341         int rc;
342         __s64 uid;
343
344         rc = lprocfs_str_to_s64(buffer, count, &uid);
345         if (rc)
346                 return rc;
347         if (uid < INT_MIN || uid > INT_MAX)
348                 return -ERANGE;
349
350         mdt_flush_identity(mdt->mdt_identity_cache, uid);
351         return count;
352 }
353 LPROC_SEQ_FOPS_WR_ONLY(mdt, identity_flush);
354
355 static ssize_t
356 lprocfs_identity_info_seq_write(struct file *file, const char __user *buffer,
357                                 size_t count, void *data)
358 {
359         struct seq_file   *m = file->private_data;
360         struct obd_device *obd = m->private;
361         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
362         struct identity_downcall_data *param;
363         int size = sizeof(*param), rc, checked = 0;
364
365 again:
366         if (count < size) {
367                 CERROR("%s: invalid data count = %lu, size = %d\n",
368                        mdt_obd_name(mdt), (unsigned long) count, size);
369                 return -EINVAL;
370         }
371
372         OBD_ALLOC(param, size);
373         if (param == NULL)
374                 return -ENOMEM;
375
376         if (copy_from_user(param, buffer, size)) {
377                 CERROR("%s: bad identity data\n", mdt_obd_name(mdt));
378                 GOTO(out, rc = -EFAULT);
379         }
380
381         if (checked == 0) {
382                 checked = 1;
383                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
384                         CERROR("%s: MDS identity downcall bad params\n",
385                                mdt_obd_name(mdt));
386                         GOTO(out, rc = -EINVAL);
387                 }
388
389                 if (param->idd_nperms > N_PERMS_MAX) {
390                         CERROR("%s: perm count %d more than maximum %d\n",
391                                mdt_obd_name(mdt), param->idd_nperms,
392                                N_PERMS_MAX);
393                         GOTO(out, rc = -EINVAL);
394                 }
395
396                 if (param->idd_ngroups > NGROUPS_MAX) {
397                         CERROR("%s: group count %d more than maximum %d\n",
398                                mdt_obd_name(mdt), param->idd_ngroups,
399                                NGROUPS_MAX);
400                         GOTO(out, rc = -EINVAL);
401                 }
402
403                 if (param->idd_ngroups) {
404                         rc = param->idd_ngroups; /* save idd_ngroups */
405                         OBD_FREE(param, size);
406                         size = offsetof(struct identity_downcall_data,
407                                         idd_groups[rc]);
408                         goto again;
409                 }
410         }
411
412         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
413                                    param->idd_uid, param);
414
415 out:
416         if (param != NULL)
417                 OBD_FREE(param, size);
418
419         return rc ? rc : count;
420 }
421 LPROC_SEQ_FOPS_WR_ONLY(mdt, identity_info);
422
423 static int mdt_site_stats_seq_show(struct seq_file *m, void *data)
424 {
425         struct obd_device *obd = m->private;
426         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
427
428         return lu_site_stats_seq_print(mdt_lu_site(mdt), m);
429 }
430 LPROC_SEQ_FOPS_RO(mdt_site_stats);
431
432 #define BUFLEN (UUID_MAX + 4)
433
434 static ssize_t
435 lprocfs_mds_evict_client_seq_write(struct file *file, const char __user *buf,
436                                    size_t count, loff_t *off)
437 {
438         struct seq_file   *m = file->private_data;
439         struct obd_device *obd = m->private;
440         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
441         char *kbuf;
442         char *tmpbuf;
443         int rc = 0;
444
445         OBD_ALLOC(kbuf, BUFLEN);
446         if (kbuf == NULL)
447                 return -ENOMEM;
448
449         /*
450          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
451          * bytes into kbuf, to ensure that the string is NUL-terminated.
452          * UUID_MAX should include a trailing NUL already.
453          */
454         if (copy_from_user(kbuf, buf, min_t(unsigned long, BUFLEN - 1, count)))
455                 GOTO(out, rc = -EFAULT);
456         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
457
458         if (strncmp(tmpbuf, "nid:", 4) != 0) {
459                 count = lprocfs_evict_client_seq_write(file, buf, count, off);
460                 goto out;
461         }
462
463         if (mdt->mdt_opts.mo_evict_tgt_nids) {
464                 rc = obd_set_info_async(NULL, mdt->mdt_child_exp,
465                                         sizeof(KEY_EVICT_BY_NID),
466                                         KEY_EVICT_BY_NID,
467                                         strlen(tmpbuf + 4) + 1,
468                                         tmpbuf + 4, NULL);
469                 if (rc)
470                         CERROR("Failed to evict nid %s from OSTs: rc %d\n",
471                                tmpbuf + 4, rc);
472         }
473
474         /* See the comments in function lprocfs_wr_evict_client()
475          * in ptlrpc/lproc_ptlrpc.c for details. - jay */
476         class_incref(obd, __func__, current);
477         obd_export_evict_by_nid(obd, tmpbuf + 4);
478         class_decref(obd, __func__, current);
479
480
481 out:
482         OBD_FREE(kbuf, BUFLEN);
483         return rc < 0 ? rc : count;
484 }
485
486 #undef BUFLEN
487
488 static int mdt_evict_tgt_nids_seq_show(struct seq_file *m, void *data)
489 {
490         struct obd_device *obd = m->private;
491         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
492
493         seq_printf(m, "%u\n", mdt->mdt_opts.mo_evict_tgt_nids);
494         return 0;
495 }
496
497 static ssize_t
498 mdt_evict_tgt_nids_seq_write(struct file *file, const char __user *buffer,
499                                size_t count, loff_t *off)
500 {
501         struct seq_file   *m = file->private_data;
502         struct obd_device *obd = m->private;
503         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
504         __s64 val;
505         int rc;
506
507         rc = lprocfs_str_to_s64(buffer, count, &val);
508         if (rc)
509                 return rc;
510         mdt->mdt_opts.mo_evict_tgt_nids = !!val;
511         return count;
512 }
513 LPROC_SEQ_FOPS(mdt_evict_tgt_nids);
514
515 static int mdt_cos_seq_show(struct seq_file *m, void *data)
516 {
517         struct obd_device *obd = m->private;
518         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
519
520         seq_printf(m, "%u\n", mdt_cos_is_enabled(mdt));
521         return 0;
522 }
523
524 static ssize_t
525 mdt_cos_seq_write(struct file *file, const char __user *buffer,
526                   size_t count, loff_t *off)
527 {
528         struct seq_file   *m = file->private_data;
529         struct obd_device *obd = m->private;
530         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
531         int rc;
532         __s64 val;
533
534         rc = lprocfs_str_to_s64(buffer, count, &val);
535         if (rc)
536                 return rc;
537         if (val < INT_MIN || val > INT_MAX)
538                 return -ERANGE;
539
540         mdt_enable_cos(mdt, val);
541         return count;
542 }
543 LPROC_SEQ_FOPS(mdt_cos);
544
545 static int mdt_root_squash_seq_show(struct seq_file *m, void *data)
546 {
547         struct obd_device *obd = m->private;
548         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
549         struct root_squash_info *squash = &mdt->mdt_squash;
550
551         seq_printf(m, "%u:%u\n", squash->rsi_uid,
552                    squash->rsi_gid);
553         return 0;
554 }
555
556 static ssize_t
557 mdt_root_squash_seq_write(struct file *file, const char __user *buffer,
558                           size_t count, loff_t *off)
559 {
560         struct seq_file   *m = file->private_data;
561         struct obd_device *obd = m->private;
562         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
563         struct root_squash_info *squash = &mdt->mdt_squash;
564
565         return lprocfs_wr_root_squash(buffer, count, squash,
566                                       mdt_obd_name(mdt));
567 }
568 LPROC_SEQ_FOPS(mdt_root_squash);
569
570 static int mdt_nosquash_nids_seq_show(struct seq_file *m, void *data)
571 {
572         struct obd_device *obd = m->private;
573         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
574         struct root_squash_info *squash = &mdt->mdt_squash;
575         int len = 0;
576
577         down_read(&squash->rsi_sem);
578         if (!list_empty(&squash->rsi_nosquash_nids)) {
579                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
580                                         &squash->rsi_nosquash_nids);
581                 m->count += len;
582                 seq_putc(m, '\n');
583         } else
584                 seq_puts(m, "NONE\n");
585         up_read(&squash->rsi_sem);
586
587         return 0;
588 }
589
590 static ssize_t
591 mdt_nosquash_nids_seq_write(struct file *file, const char __user *buffer,
592                             size_t count, loff_t *off)
593 {
594         struct seq_file   *m = file->private_data;
595         struct obd_device *obd = m->private;
596         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
597         struct root_squash_info *squash = &mdt->mdt_squash;
598
599         return lprocfs_wr_nosquash_nids(buffer, count, squash,
600                                         mdt_obd_name(mdt));
601 }
602 LPROC_SEQ_FOPS(mdt_nosquash_nids);
603
604 static int mdt_enable_remote_dir_seq_show(struct seq_file *m, void *data)
605 {
606         struct obd_device *obd = m->private;
607         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
608
609         seq_printf(m, "%u\n", mdt->mdt_enable_remote_dir);
610         return 0;
611 }
612
613 static ssize_t
614 mdt_enable_remote_dir_seq_write(struct file *file, const char __user *buffer,
615                                 size_t count, loff_t *off)
616 {
617         struct seq_file   *m = file->private_data;
618         struct obd_device *obd = m->private;
619         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
620         __s64 val;
621         int rc;
622
623         rc = lprocfs_str_to_s64(buffer, count, &val);
624         if (rc)
625                 return rc;
626
627         if (val > 1 || val < 0)
628                 return -ERANGE;
629
630         mdt->mdt_enable_remote_dir = val;
631         return count;
632 }
633 LPROC_SEQ_FOPS(mdt_enable_remote_dir);
634
635 static int mdt_enable_remote_dir_gid_seq_show(struct seq_file *m, void *data)
636 {
637         struct obd_device *obd = m->private;
638         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
639
640         seq_printf(m, "%d\n",
641                   (int)mdt->mdt_enable_remote_dir_gid);
642         return 0;
643 }
644
645 static ssize_t
646 mdt_enable_remote_dir_gid_seq_write(struct file *file,
647                                     const char __user *buffer,
648                                     size_t count, loff_t *off)
649 {
650         struct seq_file   *m = file->private_data;
651         struct obd_device *obd = m->private;
652         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
653         __s64 val;
654         int rc;
655
656         rc = lprocfs_str_to_s64(buffer, count, &val);
657         if (rc)
658                 return rc;
659
660         mdt->mdt_enable_remote_dir_gid = val;
661         return count;
662 }
663 LPROC_SEQ_FOPS(mdt_enable_remote_dir_gid);
664
665 /**
666  * Show MDT policy for handling dirty metadata under a lock being cancelled.
667  *
668  * \param[in] m         seq_file handle
669  * \param[in] data      unused for single entry
670  *
671  * \retval              0 on success
672  * \retval              negative value on error
673  */
674 static int mdt_slc_seq_show(struct seq_file *m, void *data)
675 {
676         struct obd_device *obd = m->private;
677         struct lu_target *tgt = obd->u.obt.obt_lut;
678         char *slc_states[] = {"never", "blocking", "always" };
679
680         seq_printf(m, "%s\n", slc_states[tgt->lut_sync_lock_cancel]);
681         return 0;
682 }
683 LPROC_SEQ_FOPS_RO(mdt_slc);
684
685 /**
686  * Show MDT async commit count.
687  *
688  * \param[in] m         seq_file handle
689  * \param[in] data      unused for single entry
690  *
691  * \retval              0 on success
692  * \retval              negative value on error
693  */
694 static int mdt_async_commit_count_seq_show(struct seq_file *m, void *data)
695 {
696         struct obd_device *obd = m->private;
697         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
698
699         seq_printf(m, "%d\n", atomic_read(&mdt->mdt_async_commit_count));
700         return 0;
701 }
702
703 static ssize_t
704 mdt_async_commit_count_seq_write(struct file *file, const char __user *buffer,
705                                  size_t count, loff_t *off)
706 {
707         struct seq_file *m = file->private_data;
708         struct obd_device *obd = m->private;
709         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
710         __s64 val;
711         int rc;
712
713         rc = lprocfs_str_to_s64(buffer, count, &val);
714         if (rc)
715                 return rc;
716
717         if (val < INT_MIN || val > INT_MAX)
718                 return -ERANGE;
719
720         atomic_set(&mdt->mdt_async_commit_count, val);
721
722         return count;
723 }
724 LPROC_SEQ_FOPS(mdt_async_commit_count);
725
726 /**
727  * Show MDT sync count.
728  *
729  * \param[in] m         seq_file handle
730  * \param[in] data      unused for single entry
731  *
732  * \retval              0 on success
733  * \retval              negative value on error
734  */
735 static int mdt_sync_count_seq_show(struct seq_file *m, void *data)
736 {
737         struct obd_device *obd = m->private;
738         struct lu_target *tgt = obd->u.obt.obt_lut;
739
740         seq_printf(m, "%d\n", atomic_read(&tgt->lut_sync_count));
741         return 0;
742 }
743
744 static ssize_t
745 mdt_sync_count_seq_write(struct file *file, const char __user *buffer,
746                          size_t count, loff_t *off)
747 {
748         struct seq_file *m = file->private_data;
749         struct obd_device *obd = m->private;
750         struct lu_target *tgt = obd->u.obt.obt_lut;
751         __s64 val;
752         int rc;
753
754         rc = lprocfs_str_to_s64(buffer, count, &val);
755         if (rc)
756                 return rc;
757
758         if (val < INT_MIN || val > INT_MAX)
759                 return -ERANGE;
760
761         atomic_set(&tgt->lut_sync_count, val);
762
763         return count;
764 }
765 LPROC_SEQ_FOPS(mdt_sync_count);
766
767 static int mdt_dom_lock_seq_show(struct seq_file *m, void *data)
768 {
769         struct obd_device *obd = m->private;
770         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
771
772         seq_printf(m, "%u\n",  (mdt->mdt_opts.mo_dom_lock != 0));
773         return 0;
774 }
775
776 static ssize_t
777 mdt_dom_lock_seq_write(struct file *file, const char __user *buffer,
778                        size_t count, loff_t *off)
779 {
780         struct seq_file *m = file->private_data;
781         struct obd_device *obd = m->private;
782         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
783         __s64 val;
784         int rc;
785
786         rc = lprocfs_str_to_s64(buffer, count, &val);
787         if (rc)
788                 return rc;
789
790         mdt->mdt_opts.mo_dom_lock = !!val;
791         return count;
792 }
793 LPROC_SEQ_FOPS(mdt_dom_lock);
794
795 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
796 LPROC_SEQ_FOPS_RO_TYPE(mdt, num_exports);
797 LPROC_SEQ_FOPS_RO_TYPE(mdt, target_instance);
798 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
799 LPROC_SEQ_FOPS_WR_ONLY(mdt, mds_evict_client);
800 LPROC_SEQ_FOPS_RW_TYPE(mdt, job_interval);
801 LPROC_SEQ_FOPS_RW_TYPE(mdt, ir_factor);
802 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
803 LPROC_SEQ_FOPS(mdt_hsm_cdt_control);
804
805 LPROC_SEQ_FOPS_RW_TYPE(mdt, recovery_time_hard);
806 LPROC_SEQ_FOPS_RW_TYPE(mdt, recovery_time_soft);
807
808 LPROC_SEQ_FOPS_RO(tgt_tot_dirty);
809 LPROC_SEQ_FOPS_RO(tgt_tot_granted);
810 LPROC_SEQ_FOPS_RO(tgt_tot_pending);
811 LPROC_SEQ_FOPS(tgt_grant_compat_disable);
812
813 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
814         { .name =       "tot_dirty",
815           .fops =       &tgt_tot_dirty_fops             },
816         { .name =       "tot_pending",
817           .fops =       &tgt_tot_pending_fops           },
818         { .name =       "tot_granted",
819           .fops =       &tgt_tot_granted_fops           },
820         { .name =       "grant_compat_disable",
821           .fops =       &tgt_grant_compat_disable_fops  },
822         { .name =       "recovery_status",
823           .fops =       &mdt_recovery_status_fops               },
824         { .name =       "num_exports",
825           .fops =       &mdt_num_exports_fops                   },
826         { .name =       "identity_expire",
827           .fops =       &mdt_identity_expire_fops               },
828         { .name =       "identity_acquire_expire",
829           .fops =       &mdt_identity_acquire_expire_fops       },
830         { .name =       "identity_upcall",
831           .fops =       &mdt_identity_upcall_fops               },
832         { .name =       "identity_flush",
833           .fops =       &mdt_identity_flush_fops                },
834         { .name =       "identity_info",
835           .fops =       &mdt_identity_info_fops                 },
836         { .name =       "site_stats",
837           .fops =       &mdt_site_stats_fops                    },
838         { .name =       "evict_client",
839           .fops =       &mdt_mds_evict_client_fops              },
840         { .name =       "evict_tgt_nids",
841           .fops =       &mdt_evict_tgt_nids_fops                },
842         { .name =       "hash_stats",
843           .fops =       &mdt_hash_fops                          },
844         { .name =       "commit_on_sharing",
845           .fops =       &mdt_cos_fops                           },
846         { .name =       "root_squash",
847           .fops =       &mdt_root_squash_fops                   },
848         { .name =       "nosquash_nids",
849           .fops =       &mdt_nosquash_nids_fops                 },
850         { .name =       "instance",
851           .fops =       &mdt_target_instance_fops               },
852         { .name =       "ir_factor",
853           .fops =       &mdt_ir_factor_fops                     },
854         { .name =       "job_cleanup_interval",
855           .fops =       &mdt_job_interval_fops                  },
856         { .name =       "enable_remote_dir",
857           .fops =       &mdt_enable_remote_dir_fops             },
858         { .name =       "enable_remote_dir_gid",
859           .fops =       &mdt_enable_remote_dir_gid_fops         },
860         { .name =       "hsm_control",
861           .fops =       &mdt_hsm_cdt_control_fops               },
862         { .name =       "recovery_time_hard",
863           .fops =       &mdt_recovery_time_hard_fops    },
864         { .name =       "recovery_time_soft",
865           .fops =       &mdt_recovery_time_soft_fops    },
866         { .name =       "sync_lock_cancel",
867           .fops =       &mdt_slc_fops                           },
868         { .name =       "async_commit_count",
869           .fops =       &mdt_async_commit_count_fops            },
870         { .name =       "sync_count",
871           .fops =       &mdt_sync_count_fops                    },
872         { .name =       "dom_lock",
873           .fops =       &mdt_dom_lock_fops                      },
874         { NULL }
875 };
876
877 static int
878 lprocfs_mdt_print_open_files(struct cfs_hash *hs, struct cfs_hash_bd *bd,
879                              struct hlist_node *hnode, void *v)
880 {
881         struct obd_export       *exp = cfs_hash_object(hs, hnode);
882         struct seq_file         *seq = v;
883
884         if (exp->exp_lock_hash != NULL) {
885                 struct mdt_export_data  *med = &exp->exp_mdt_data;
886                 struct mdt_file_data    *mfd;
887
888                 spin_lock(&med->med_open_lock);
889                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
890                         seq_printf(seq, DFID"\n",
891                                    PFID(mdt_object_fid(mfd->mfd_object)));
892                 }
893                 spin_unlock(&med->med_open_lock);
894         }
895
896         return 0;
897 }
898
899 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
900 {
901         struct nid_stat *stats = seq->private;
902         struct obd_device *obd = stats->nid_obd;
903
904         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
905                               lprocfs_mdt_print_open_files, seq);
906
907         return 0;
908 }
909
910 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
911 {
912         struct seq_file         *seq;
913         int                     rc;
914
915         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
916         if (rc != 0)
917                 return rc;
918
919         seq = file->private_data;
920         seq->private = PDE_DATA(inode);
921
922         return 0;
923 }
924
925 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
926 {
927         struct obd_export *exp = req->rq_export;
928
929         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
930                 lprocfs_counter_incr(exp->exp_obd->obd_md_stats, opcode);
931         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
932                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
933         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
934             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
935                 lprocfs_job_stats_log(exp->exp_obd,
936                                       lustre_msg_get_jobid(req->rq_reqmsg),
937                                       opcode, 1);
938 }
939
940 void mdt_stats_counter_init(struct lprocfs_stats *stats)
941 {
942         LASSERT(stats && stats->ls_num >= LPROC_MDT_LAST);
943
944         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
945         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
946         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
947         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
948         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
949         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
950         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
951         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
952         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
953         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
954         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
955         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
956         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
957         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
958         lprocfs_counter_init(stats, LPROC_MDT_SAMEDIR_RENAME, 0,
959                              "samedir_rename", "reqs");
960         lprocfs_counter_init(stats, LPROC_MDT_CROSSDIR_RENAME, 0,
961                              "crossdir_rename", "reqs");
962         lprocfs_counter_init(stats, LPROC_MDT_IO_READ,
963                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
964         lprocfs_counter_init(stats, LPROC_MDT_IO_WRITE,
965                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
966         lprocfs_counter_init(stats, LPROC_MDT_IO_PUNCH, 0, "punch", "reqs");
967 }
968
969 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
970 {
971         struct obd_device               *obd = mdt2obd_dev(mdt);
972         int                              rc;
973         ENTRY;
974
975         LASSERT(name != NULL);
976
977         obd->obd_vars = lprocfs_mdt_obd_vars;
978         rc = lprocfs_obd_setup(obd, true);
979         if (rc) {
980                 CERROR("%s: cannot create proc entries: rc = %d\n",
981                        mdt_obd_name(mdt), rc);
982                 return rc;
983         }
984
985         rc = hsm_cdt_procfs_init(mdt);
986         if (rc) {
987                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
988                        mdt_obd_name(mdt), rc);
989                 return rc;
990         }
991
992         obd->obd_proc_exports_entry = proc_mkdir("exports",
993                                                  obd->obd_proc_entry);
994         if (obd->obd_proc_exports_entry)
995                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
996                                    obd, &mdt_nid_stats_clear_fops);
997         rc = lprocfs_alloc_md_stats(obd, LPROC_MDT_LAST);
998         if (rc)
999                 return rc;
1000         mdt_stats_counter_init(obd->obd_md_stats);
1001
1002         rc = lprocfs_job_stats_init(obd, LPROC_MDT_LAST,
1003                                     mdt_stats_counter_init);
1004
1005         rc = lproc_mdt_attach_rename_seqstat(mdt);
1006         if (rc)
1007                 CERROR("%s: MDT can not create rename stats rc = %d\n",
1008                        mdt_obd_name(mdt), rc);
1009
1010         RETURN(rc);
1011 }
1012
1013 void mdt_procfs_fini(struct mdt_device *mdt)
1014 {
1015         struct obd_device *obd = mdt2obd_dev(mdt);
1016
1017         if (obd->obd_proc_exports_entry != NULL) {
1018                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
1019                 obd->obd_proc_exports_entry = NULL;
1020         }
1021
1022         lprocfs_free_per_client_stats(obd);
1023         hsm_cdt_procfs_fini(mdt);
1024         lprocfs_obd_cleanup(obd);
1025         lprocfs_free_md_stats(obd);
1026         lprocfs_free_obd_stats(obd);
1027         lprocfs_job_stats_fini(obd);
1028 }