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