Whamcloud - gitweb
LU-6215 lprocfs: handle seq_printf api change
[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, val;
232
233         rc = lprocfs_write_helper(buffer, count, &val);
234         if (rc)
235                 return rc;
236
237         mdt->mdt_identity_cache->uc_entry_expire = val;
238         return count;
239 }
240 LPROC_SEQ_FOPS(mdt_identity_expire);
241
242 static int mdt_identity_acquire_expire_seq_show(struct seq_file *m, void *data)
243 {
244         struct obd_device *obd = m->private;
245         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
246
247         seq_printf(m, "%u\n", mdt->mdt_identity_cache->uc_acquire_expire);
248         return 0;
249 }
250
251 static ssize_t
252 mdt_identity_acquire_expire_seq_write(struct file *file,
253                                       const char __user *buffer,
254                                       size_t count, loff_t *off)
255 {
256         struct seq_file   *m = file->private_data;
257         struct obd_device *obd = m->private;
258         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
259         int rc, val;
260
261         rc = lprocfs_write_helper(buffer, count, &val);
262         if (rc)
263                 return rc;
264
265         mdt->mdt_identity_cache->uc_acquire_expire = val;
266         return count;
267 }
268 LPROC_SEQ_FOPS(mdt_identity_acquire_expire);
269
270 static int mdt_identity_upcall_seq_show(struct seq_file *m, void *data)
271 {
272         struct obd_device *obd = m->private;
273         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
274         struct upcall_cache *hash = mdt->mdt_identity_cache;
275
276         read_lock(&hash->uc_upcall_rwlock);
277         seq_printf(m, "%s\n", hash->uc_upcall);
278         read_unlock(&hash->uc_upcall_rwlock);
279         return 0;
280 }
281
282 static ssize_t
283 mdt_identity_upcall_seq_write(struct file *file, const char __user *buffer,
284                               size_t count, loff_t *off)
285 {
286         struct seq_file         *m = file->private_data;
287         struct obd_device       *obd = m->private;
288         struct mdt_device       *mdt = mdt_dev(obd->obd_lu_dev);
289         struct upcall_cache     *hash = mdt->mdt_identity_cache;
290         int                      rc;
291         char                    *kernbuf;
292
293         if (count >= UC_CACHE_UPCALL_MAXPATH) {
294                 CERROR("%s: identity upcall too long\n", mdt_obd_name(mdt));
295                 return -EINVAL;
296         }
297         OBD_ALLOC(kernbuf, count + 1);
298         if (kernbuf == NULL)
299                 GOTO(failed, rc = -ENOMEM);
300         if (copy_from_user(kernbuf, buffer, count))
301                 GOTO(failed, rc = -EFAULT);
302
303         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
304         write_lock(&hash->uc_upcall_rwlock);
305         sscanf(kernbuf, "%s", hash->uc_upcall);
306         write_unlock(&hash->uc_upcall_rwlock);
307
308         if (strcmp(hash->uc_name, mdt_obd_name(mdt)) != 0)
309                 CWARN("%s: write to upcall name %s\n",
310                       mdt_obd_name(mdt), hash->uc_upcall);
311
312         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
313                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
314                       "cause unexpected \"EACCESS\"\n", mdt_obd_name(mdt));
315
316         CDEBUG(D_CONFIG, "%s: identity upcall set to %s\n", mdt_obd_name(mdt),
317                hash->uc_upcall);
318         OBD_FREE(kernbuf, count + 1);
319         RETURN(count);
320
321  failed:
322         if (kernbuf)
323                 OBD_FREE(kernbuf, count + 1);
324         RETURN(rc);
325 }
326 LPROC_SEQ_FOPS(mdt_identity_upcall);
327
328 static ssize_t
329 lprocfs_identity_flush_seq_write(struct file *file, const char __user *buffer,
330                                  size_t count, void *data)
331 {
332         struct seq_file   *m = file->private_data;
333         struct obd_device *obd = m->private;
334         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
335         int rc, uid;
336
337         rc = lprocfs_write_helper(buffer, count, &uid);
338         if (rc)
339                 return rc;
340
341         mdt_flush_identity(mdt->mdt_identity_cache, uid);
342         return count;
343 }
344 LPROC_SEQ_FOPS_WO_TYPE(mdt, identity_flush);
345
346 static ssize_t
347 lprocfs_identity_info_seq_write(struct file *file, const char __user *buffer,
348                                 size_t count, void *data)
349 {
350         struct seq_file   *m = file->private_data;
351         struct obd_device *obd = m->private;
352         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
353         struct identity_downcall_data *param;
354         int size = sizeof(*param), rc, checked = 0;
355
356 again:
357         if (count < size) {
358                 CERROR("%s: invalid data count = %lu, size = %d\n",
359                        mdt_obd_name(mdt), (unsigned long) count, size);
360                 return -EINVAL;
361         }
362
363         OBD_ALLOC(param, size);
364         if (param == NULL)
365                 return -ENOMEM;
366
367         if (copy_from_user(param, buffer, size)) {
368                 CERROR("%s: bad identity data\n", mdt_obd_name(mdt));
369                 GOTO(out, rc = -EFAULT);
370         }
371
372         if (checked == 0) {
373                 checked = 1;
374                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
375                         CERROR("%s: MDS identity downcall bad params\n",
376                                mdt_obd_name(mdt));
377                         GOTO(out, rc = -EINVAL);
378                 }
379
380                 if (param->idd_nperms > N_PERMS_MAX) {
381                         CERROR("%s: perm count %d more than maximum %d\n",
382                                mdt_obd_name(mdt), param->idd_nperms,
383                                N_PERMS_MAX);
384                         GOTO(out, rc = -EINVAL);
385                 }
386
387                 if (param->idd_ngroups > NGROUPS_MAX) {
388                         CERROR("%s: group count %d more than maximum %d\n",
389                                mdt_obd_name(mdt), param->idd_ngroups,
390                                NGROUPS_MAX);
391                         GOTO(out, rc = -EINVAL);
392                 }
393
394                 if (param->idd_ngroups) {
395                         rc = param->idd_ngroups; /* save idd_ngroups */
396                         OBD_FREE(param, size);
397                         size = offsetof(struct identity_downcall_data,
398                                         idd_groups[rc]);
399                         goto again;
400                 }
401         }
402
403         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
404                                    param->idd_uid, param);
405
406 out:
407         if (param != NULL)
408                 OBD_FREE(param, size);
409
410         return rc ? rc : count;
411 }
412 LPROC_SEQ_FOPS_WO_TYPE(mdt, identity_info);
413
414 static int mdt_site_stats_seq_show(struct seq_file *m, void *data)
415 {
416         struct obd_device *obd = m->private;
417         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
418
419         return lu_site_stats_seq_print(mdt_lu_site(mdt), m);
420 }
421 LPROC_SEQ_FOPS_RO(mdt_site_stats);
422
423 #define BUFLEN (UUID_MAX + 4)
424
425 static ssize_t
426 lprocfs_mds_evict_client_seq_write(struct file *file, const char __user *buf,
427                                    size_t count, loff_t *off)
428 {
429         struct seq_file   *m = file->private_data;
430         struct obd_device *obd = m->private;
431         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
432         char *kbuf;
433         char *tmpbuf;
434         int rc = 0;
435
436         OBD_ALLOC(kbuf, BUFLEN);
437         if (kbuf == NULL)
438                 return -ENOMEM;
439
440         /*
441          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
442          * bytes into kbuf, to ensure that the string is NUL-terminated.
443          * UUID_MAX should include a trailing NUL already.
444          */
445         if (copy_from_user(kbuf, buf, min_t(unsigned long, BUFLEN - 1, count)))
446                 GOTO(out, rc = -EFAULT);
447         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
448
449         if (strncmp(tmpbuf, "nid:", 4) != 0) {
450                 count = lprocfs_evict_client_seq_write(file, buf, count, off);
451                 goto out;
452         }
453
454         if (mdt->mdt_opts.mo_evict_tgt_nids) {
455                 rc = obd_set_info_async(NULL, mdt->mdt_child_exp,
456                                         sizeof(KEY_EVICT_BY_NID),
457                                         KEY_EVICT_BY_NID,
458                                         strlen(tmpbuf + 4) + 1,
459                                         tmpbuf + 4, NULL);
460                 if (rc)
461                         CERROR("Failed to evict nid %s from OSTs: rc %d\n",
462                                tmpbuf + 4, rc);
463         }
464
465         /* See the comments in function lprocfs_wr_evict_client()
466          * in ptlrpc/lproc_ptlrpc.c for details. - jay */
467         class_incref(obd, __func__, current);
468         obd_export_evict_by_nid(obd, tmpbuf + 4);
469         class_decref(obd, __func__, current);
470
471
472 out:
473         OBD_FREE(kbuf, BUFLEN);
474         return rc < 0 ? rc : count;
475 }
476
477 #undef BUFLEN
478
479 static int mdt_evict_tgt_nids_seq_show(struct seq_file *m, void *data)
480 {
481         struct obd_device *obd = m->private;
482         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
483
484         seq_printf(m, "%u\n", mdt->mdt_opts.mo_evict_tgt_nids);
485         return 0;
486 }
487
488 static ssize_t
489 mdt_evict_tgt_nids_seq_write(struct file *file, const char __user *buffer,
490                                size_t count, loff_t *off)
491 {
492         struct seq_file   *m = file->private_data;
493         struct obd_device *obd = m->private;
494         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
495         int val, rc;
496
497         rc = lprocfs_write_helper(buffer, count, &val);
498         if (rc)
499                 return rc;
500         mdt->mdt_opts.mo_evict_tgt_nids = !!val;
501         return count;
502 }
503 LPROC_SEQ_FOPS(mdt_evict_tgt_nids);
504
505
506 static int mdt_sec_level_seq_show(struct seq_file *m, void *data)
507 {
508         struct obd_device *obd = m->private;
509         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
510
511         seq_printf(m, "%d\n", mdt->mdt_lut.lut_sec_level);
512         return 0;
513 }
514
515 static ssize_t
516 mdt_sec_level_seq_write(struct file *file, const char __user *buffer,
517                         size_t count, loff_t *off)
518 {
519         struct seq_file   *m = file->private_data;
520         struct obd_device *obd = m->private;
521         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
522         int val, rc;
523
524         rc = lprocfs_write_helper(buffer, count, &val);
525         if (rc)
526                 return rc;
527
528         if (val > LUSTRE_SEC_ALL || val < LUSTRE_SEC_NONE)
529                 return -EINVAL;
530
531         if (val == LUSTRE_SEC_SPECIFY) {
532                 CWARN("security level %d will be supported in future.\n",
533                       LUSTRE_SEC_SPECIFY);
534                 return -EINVAL;
535         }
536
537         mdt->mdt_lut.lut_sec_level = val;
538         return count;
539 }
540 LPROC_SEQ_FOPS(mdt_sec_level);
541
542 static int mdt_cos_seq_show(struct seq_file *m, void *data)
543 {
544         struct obd_device *obd = m->private;
545         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
546
547         seq_printf(m, "%u\n", mdt_cos_is_enabled(mdt));
548         return 0;
549 }
550
551 static ssize_t
552 mdt_cos_seq_write(struct file *file, const char __user *buffer,
553                   size_t count, loff_t *off)
554 {
555         struct seq_file   *m = file->private_data;
556         struct obd_device *obd = m->private;
557         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
558         int val, rc;
559
560         rc = lprocfs_write_helper(buffer, count, &val);
561         if (rc)
562                 return rc;
563         mdt_enable_cos(mdt, val);
564         return count;
565 }
566 LPROC_SEQ_FOPS(mdt_cos);
567
568 static int mdt_root_squash_seq_show(struct seq_file *m, void *data)
569 {
570         struct obd_device *obd = m->private;
571         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
572         struct root_squash_info *squash = &mdt->mdt_squash;
573
574         seq_printf(m, "%u:%u\n", squash->rsi_uid,
575                    squash->rsi_gid);
576         return 0;
577 }
578
579 static ssize_t
580 mdt_root_squash_seq_write(struct file *file, const char __user *buffer,
581                           size_t count, loff_t *off)
582 {
583         struct seq_file   *m = file->private_data;
584         struct obd_device *obd = m->private;
585         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
586         struct root_squash_info *squash = &mdt->mdt_squash;
587
588         return lprocfs_wr_root_squash(buffer, count, squash,
589                                       mdt_obd_name(mdt));
590 }
591 LPROC_SEQ_FOPS(mdt_root_squash);
592
593 static int mdt_nosquash_nids_seq_show(struct seq_file *m, void *data)
594 {
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         int len = 0;
599
600         down_read(&squash->rsi_sem);
601         if (!list_empty(&squash->rsi_nosquash_nids)) {
602                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
603                                         &squash->rsi_nosquash_nids);
604                 m->count += len;
605                 seq_putc(m, '\n');
606         } else
607                 seq_puts(m, "NONE\n");
608         up_read(&squash->rsi_sem);
609
610         return 0;
611 }
612
613 static ssize_t
614 mdt_nosquash_nids_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         struct root_squash_info *squash = &mdt->mdt_squash;
621
622         return lprocfs_wr_nosquash_nids(buffer, count, squash,
623                                         mdt_obd_name(mdt));
624 }
625 LPROC_SEQ_FOPS(mdt_nosquash_nids);
626
627 static int mdt_enable_remote_dir_seq_show(struct seq_file *m, void *data)
628 {
629         struct obd_device *obd = m->private;
630         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
631
632         seq_printf(m, "%u\n", mdt->mdt_enable_remote_dir);
633         return 0;
634 }
635
636 static ssize_t
637 mdt_enable_remote_dir_seq_write(struct file *file, const char __user *buffer,
638                                 size_t count, loff_t *off)
639 {
640         struct seq_file   *m = file->private_data;
641         struct obd_device *obd = m->private;
642         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
643         __u32 val;
644         int rc;
645
646         rc = lprocfs_write_helper(buffer, count, &val);
647         if (rc)
648                 return rc;
649
650         if (val > 1)
651                 return -ERANGE;
652
653         mdt->mdt_enable_remote_dir = val;
654         return count;
655 }
656 LPROC_SEQ_FOPS(mdt_enable_remote_dir);
657
658 static int mdt_enable_remote_dir_gid_seq_show(struct seq_file *m, void *data)
659 {
660         struct obd_device *obd = m->private;
661         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
662
663         seq_printf(m, "%d\n",
664                   (int)mdt->mdt_enable_remote_dir_gid);
665         return 0;
666 }
667
668 static ssize_t
669 mdt_enable_remote_dir_gid_seq_write(struct file *file,
670                                     const char __user *buffer,
671                                     size_t count, loff_t *off)
672 {
673         struct seq_file   *m = file->private_data;
674         struct obd_device *obd = m->private;
675         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
676         __u32 val;
677         int rc;
678
679         rc = lprocfs_write_helper(buffer, count, &val);
680         if (rc)
681                 return rc;
682
683         mdt->mdt_enable_remote_dir_gid = val;
684         return count;
685 }
686 LPROC_SEQ_FOPS(mdt_enable_remote_dir_gid);
687
688 /**
689  * Show MDT policy for handling dirty metadata under a lock being cancelled.
690  *
691  * \param[in] m         seq_file handle
692  * \param[in] data      unused for single entry
693  *
694  * \retval              0 on success
695  * \retval              negative value on error
696  */
697 static int mdt_slc_seq_show(struct seq_file *m, void *data)
698 {
699         struct obd_device *obd = m->private;
700         struct lu_target *tgt = obd->u.obt.obt_lut;
701         char *slc_states[] = {"never", "blocking", "always" };
702
703         seq_printf(m, "%s\n", slc_states[tgt->lut_sync_lock_cancel]);
704         return 0;
705 }
706 LPROC_SEQ_FOPS_RO(mdt_slc);
707
708 /**
709  * Show MDT async commit count.
710  *
711  * \param[in] m         seq_file handle
712  * \param[in] data      unused for single entry
713  *
714  * \retval              0 on success
715  * \retval              negative value on error
716  */
717 static int mdt_async_commit_count_seq_show(struct seq_file *m, void *data)
718 {
719         struct obd_device *obd = m->private;
720         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
721
722         seq_printf(m, "%d\n", atomic_read(&mdt->mdt_async_commit_count));
723         return 0;
724 }
725
726 static ssize_t
727 mdt_async_commit_count_seq_write(struct file *file, const char __user *buffer,
728                                  size_t count, loff_t *off)
729 {
730         struct seq_file *m = file->private_data;
731         struct obd_device *obd = m->private;
732         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
733         int val;
734         int rc;
735
736         rc = lprocfs_write_helper(buffer, count, &val);
737         if (rc)
738                 return rc;
739
740         atomic_set(&mdt->mdt_async_commit_count, val);
741
742         return count;
743 }
744 LPROC_SEQ_FOPS(mdt_async_commit_count);
745
746 /**
747  * Show MDT sync count.
748  *
749  * \param[in] m         seq_file handle
750  * \param[in] data      unused for single entry
751  *
752  * \retval              0 on success
753  * \retval              negative value on error
754  */
755 static int mdt_sync_count_seq_show(struct seq_file *m, void *data)
756 {
757         struct obd_device *obd = m->private;
758         struct lu_target *tgt = obd->u.obt.obt_lut;
759
760         seq_printf(m, "%d\n", atomic_read(&tgt->lut_sync_count));
761         return 0;
762 }
763
764 static ssize_t
765 mdt_sync_count_seq_write(struct file *file, const char __user *buffer,
766                          size_t count, loff_t *off)
767 {
768         struct seq_file *m = file->private_data;
769         struct obd_device *obd = m->private;
770         struct lu_target *tgt = obd->u.obt.obt_lut;
771         int val;
772         int rc;
773
774         rc = lprocfs_write_helper(buffer, count, &val);
775         if (rc)
776                 return rc;
777
778         atomic_set(&tgt->lut_sync_count, val);
779
780         return count;
781 }
782 LPROC_SEQ_FOPS(mdt_sync_count);
783
784
785 LPROC_SEQ_FOPS_RO_TYPE(mdt, uuid);
786 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
787 LPROC_SEQ_FOPS_RO_TYPE(mdt, num_exports);
788 LPROC_SEQ_FOPS_RO_TYPE(mdt, target_instance);
789 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
790 LPROC_SEQ_FOPS_WO_TYPE(mdt, mds_evict_client);
791 LPROC_SEQ_FOPS_RW_TYPE(mdt, job_interval);
792 LPROC_SEQ_FOPS_RW_TYPE(mdt, ir_factor);
793 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
794 LPROC_SEQ_FOPS(mdt_hsm_cdt_control);
795
796 LPROC_SEQ_FOPS_RW_TYPE(mdt, recovery_time_hard);
797 LPROC_SEQ_FOPS_RW_TYPE(mdt, recovery_time_soft);
798
799 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
800         { .name =       "uuid",
801           .fops =       &mdt_uuid_fops                          },
802         { .name =       "recovery_status",
803           .fops =       &mdt_recovery_status_fops               },
804         { .name =       "num_exports",
805           .fops =       &mdt_num_exports_fops                   },
806         { .name =       "identity_expire",
807           .fops =       &mdt_identity_expire_fops               },
808         { .name =       "identity_acquire_expire",
809           .fops =       &mdt_identity_acquire_expire_fops       },
810         { .name =       "identity_upcall",
811           .fops =       &mdt_identity_upcall_fops               },
812         { .name =       "identity_flush",
813           .fops =       &mdt_identity_flush_fops                },
814         { .name =       "identity_info",
815           .fops =       &mdt_identity_info_fops                 },
816         { .name =       "site_stats",
817           .fops =       &mdt_site_stats_fops                    },
818         { .name =       "evict_client",
819           .fops =       &mdt_mds_evict_client_fops              },
820         { .name =       "evict_tgt_nids",
821           .fops =       &mdt_evict_tgt_nids_fops                },
822         { .name =       "hash_stats",
823           .fops =       &mdt_hash_fops                          },
824         { .name =       "sec_level",
825           .fops =       &mdt_sec_level_fops                     },
826         { .name =       "commit_on_sharing",
827           .fops =       &mdt_cos_fops                           },
828         { .name =       "root_squash",
829           .fops =       &mdt_root_squash_fops                   },
830         { .name =       "nosquash_nids",
831           .fops =       &mdt_nosquash_nids_fops                 },
832         { .name =       "instance",
833           .fops =       &mdt_target_instance_fops               },
834         { .name =       "ir_factor",
835           .fops =       &mdt_ir_factor_fops                     },
836         { .name =       "job_cleanup_interval",
837           .fops =       &mdt_job_interval_fops                  },
838         { .name =       "enable_remote_dir",
839           .fops =       &mdt_enable_remote_dir_fops             },
840         { .name =       "enable_remote_dir_gid",
841           .fops =       &mdt_enable_remote_dir_gid_fops         },
842         { .name =       "hsm_control",
843           .fops =       &mdt_hsm_cdt_control_fops               },
844         { .name =       "recovery_time_hard",
845           .fops =       &mdt_recovery_time_hard_fops    },
846         { .name =       "recovery_time_soft",
847           .fops =       &mdt_recovery_time_soft_fops    },
848         { .name =       "sync_lock_cancel",
849           .fops =       &mdt_slc_fops                           },
850         { .name =       "async_commit_count",
851           .fops =       &mdt_async_commit_count_fops            },
852         { .name =       "sync_count",
853           .fops =       &mdt_sync_count_fops                    },
854         { NULL }
855 };
856
857 static int
858 lprocfs_mdt_print_open_files(struct cfs_hash *hs, struct cfs_hash_bd *bd,
859                              struct hlist_node *hnode, void *v)
860 {
861         struct obd_export       *exp = cfs_hash_object(hs, hnode);
862         struct seq_file         *seq = v;
863
864         if (exp->exp_lock_hash != NULL) {
865                 struct mdt_export_data  *med = &exp->exp_mdt_data;
866                 struct mdt_file_data    *mfd;
867
868                 spin_lock(&med->med_open_lock);
869                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
870                         seq_printf(seq, DFID"\n",
871                                    PFID(mdt_object_fid(mfd->mfd_object)));
872                 }
873                 spin_unlock(&med->med_open_lock);
874         }
875
876         return 0;
877 }
878
879 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
880 {
881         struct nid_stat *stats = seq->private;
882         struct obd_device *obd = stats->nid_obd;
883
884         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
885                               lprocfs_mdt_print_open_files, seq);
886
887         return 0;
888 }
889
890 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
891 {
892         struct seq_file         *seq;
893         int                     rc;
894
895         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
896         if (rc != 0)
897                 return rc;
898
899         seq = file->private_data;
900         seq->private = PDE_DATA(inode);
901
902         return 0;
903 }
904
905 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
906 {
907         struct obd_export *exp = req->rq_export;
908
909         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
910                 lprocfs_counter_incr(exp->exp_obd->obd_md_stats, opcode);
911         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
912                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
913         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
914             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
915                 lprocfs_job_stats_log(exp->exp_obd,
916                                       lustre_msg_get_jobid(req->rq_reqmsg),
917                                       opcode, 1);
918 }
919
920 void mdt_stats_counter_init(struct lprocfs_stats *stats)
921 {
922         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
923         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
924         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
925         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
926         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
927         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
928         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
929         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
930         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
931         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
932         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
933         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
934         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
935         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
936         lprocfs_counter_init(stats, LPROC_MDT_SAMEDIR_RENAME, 0,
937                              "samedir_rename", "reqs");
938         lprocfs_counter_init(stats, LPROC_MDT_CROSSDIR_RENAME, 0,
939                              "crossdir_rename", "reqs");
940 }
941
942 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
943 {
944         struct obd_device               *obd = mdt2obd_dev(mdt);
945         int                              rc;
946         ENTRY;
947
948         LASSERT(name != NULL);
949
950         obd->obd_vars = lprocfs_mdt_obd_vars;
951         rc = lprocfs_obd_setup(obd);
952         if (rc) {
953                 CERROR("%s: cannot create proc entries: rc = %d\n",
954                        mdt_obd_name(mdt), rc);
955                 return rc;
956         }
957
958         rc = hsm_cdt_procfs_init(mdt);
959         if (rc) {
960                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
961                        mdt_obd_name(mdt), rc);
962                 return rc;
963         }
964
965         obd->obd_proc_exports_entry = proc_mkdir("exports",
966                                                  obd->obd_proc_entry);
967         if (obd->obd_proc_exports_entry)
968                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
969                                    obd, &mdt_nid_stats_clear_fops);
970         rc = lprocfs_alloc_md_stats(obd, LPROC_MDT_LAST);
971         if (rc)
972                 return rc;
973         mdt_stats_counter_init(obd->obd_md_stats);
974
975         rc = lprocfs_job_stats_init(obd, LPROC_MDT_LAST,
976                                     mdt_stats_counter_init);
977
978         rc = lproc_mdt_attach_rename_seqstat(mdt);
979         if (rc)
980                 CERROR("%s: MDT can not create rename stats rc = %d\n",
981                        mdt_obd_name(mdt), rc);
982
983         RETURN(rc);
984 }
985
986 void mdt_procfs_fini(struct mdt_device *mdt)
987 {
988         struct obd_device *obd = mdt2obd_dev(mdt);
989
990         if (obd->obd_proc_exports_entry != NULL) {
991                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
992                 obd->obd_proc_exports_entry = NULL;
993         }
994
995         lprocfs_free_per_client_stats(obd);
996         hsm_cdt_procfs_fini(mdt);
997         lprocfs_obd_cleanup(obd);
998         lprocfs_free_md_stats(obd);
999         lprocfs_free_obd_stats(obd);
1000         lprocfs_job_stats_fini(obd);
1001 }