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