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