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