Whamcloud - gitweb
LU-14382 mdt: implement fallocate in MDC/MDT
[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  *
31  * lustre/mdt/mdt_lproc.c
32  *
33  * Author: Lai Siyao <lsy@clusterfs.com>
34  * Author: Fan Yong <fanyong@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include <linux/version.h>
40 #include <asm/statfs.h>
41
42 #include <linux/module.h>
43 #include <uapi/linux/lnet/nidstr.h>
44 /* LUSTRE_VERSION_CODE */
45 #include <uapi/linux/lustre/lustre_ver.h>
46 /*
47  * struct OBD_{ALLOC,FREE}*()
48  * MDT_FAIL_CHECK
49  */
50 #include <obd_support.h>
51 /* struct obd_export */
52 #include <lustre_export.h>
53 /* struct obd_device */
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_mds.h>
57 #include <lprocfs_status.h>
58 #include "mdt_internal.h"
59
60 /**
61  * The rename stats output would be YAML formats, like
62  * rename_stats:
63  * - snapshot_time: 1234567890.123456
64  * - same_dir:
65  *     4kB: { samples: 1230, pct: 33, cum_pct: 45 }
66  *     8kB: { samples: 1242, pct: 33, cum_pct: 78 }
67  *     16kB: { samples: 132, pct: 3, cum_pct: 81 }
68  * - crossdir_src:
69  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
70  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
71  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
72  * - crossdir_tgt:
73  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
74  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
75  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
76  **/
77
78 static void display_rename_stats(struct seq_file *seq, char *name,
79                                  struct obd_histogram *hist)
80 {
81         unsigned long tot, t, cum = 0;
82         int i;
83
84         tot = lprocfs_oh_sum(hist);
85         if (tot > 0)
86                 seq_printf(seq, "- %-15s\n", name);
87         /* dir size start from 4K, start i from 10(2^10) here */
88         for (i = 0; i < OBD_HIST_MAX; i++) {
89                 t = hist->oh_buckets[i];
90                 cum += t;
91                 if (cum == 0)
92                         continue;
93
94                 if (i < 10)
95                         seq_printf(seq, "%6s%d%s", " ", 1<< i, "bytes:");
96                 else if (i < 20)
97                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-10), "KB:");
98                 else
99                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-20), "MB:");
100
101                 seq_printf(seq, " { sample: %3lu, pct: %3u, cum_pct: %3u }\n",
102                            t, pct(t, tot), pct(cum, tot));
103
104                 if (cum == tot)
105                         break;
106         }
107 }
108
109 static void rename_stats_show(struct seq_file *seq,
110                               struct rename_stats *rename_stats)
111 {
112         struct timespec64 now;
113
114         /* this sampling races with updates */
115         ktime_get_real_ts64(&now);
116         seq_printf(seq, "rename_stats:\n");
117         seq_printf(seq, "- %-15s %llu.%9lu\n", "snapshot_time:",
118                    (s64)now.tv_sec, now.tv_nsec);
119
120         display_rename_stats(seq, "same_dir",
121                              &rename_stats->hist[RENAME_SAMEDIR_SIZE]);
122         display_rename_stats(seq, "crossdir_src",
123                              &rename_stats->hist[RENAME_CROSSDIR_SRC_SIZE]);
124         display_rename_stats(seq, "crossdir_tgt",
125                              &rename_stats->hist[RENAME_CROSSDIR_TGT_SIZE]);
126 }
127
128 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
129 {
130         struct mdt_device *mdt = seq->private;
131
132         rename_stats_show(seq, &mdt->mdt_rename_stats);
133
134         return 0;
135 }
136
137 static ssize_t
138 mdt_rename_stats_seq_write(struct file *file, const char __user *buf,
139                            size_t len, loff_t *off)
140 {
141         struct seq_file *seq = file->private_data;
142         struct mdt_device *mdt = seq->private;
143         int i;
144
145         for (i = 0; i < RENAME_LAST; i++)
146                 lprocfs_oh_clear(&mdt->mdt_rename_stats.hist[i]);
147
148         return len;
149 }
150 LPROC_SEQ_FOPS(mdt_rename_stats);
151
152 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
153 {
154         int i;
155
156         for (i = 0; i < RENAME_LAST; i++)
157                 spin_lock_init(&mdt->mdt_rename_stats.hist[i].oh_lock);
158
159         return lprocfs_obd_seq_create(mdt2obd_dev(mdt), "rename_stats", 0644,
160                                       &mdt_rename_stats_fops, mdt);
161 }
162
163 void mdt_rename_counter_tally(struct mdt_thread_info *info,
164                               struct mdt_device *mdt,
165                               struct ptlrpc_request *req,
166                               struct mdt_object *src,
167                               struct mdt_object *tgt, long count)
168 {
169         struct md_attr *ma = &info->mti_attr;
170         struct rename_stats *rstats = &mdt->mdt_rename_stats;
171         int rc;
172
173         ma->ma_need = MA_INODE;
174         ma->ma_valid = 0;
175         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
176         if (rc) {
177                 CERROR("%s: "DFID" attr_get, rc = %d\n",
178                        mdt_obd_name(mdt), PFID(mdt_object_fid(src)), rc);
179                 return;
180         }
181
182         if (src == tgt) {
183                 mdt_counter_incr(req, LPROC_MDT_SAMEDIR_RENAME, count);
184                 lprocfs_oh_tally_log2(&rstats->hist[RENAME_SAMEDIR_SIZE],
185                                       (unsigned int)ma->ma_attr.la_size);
186                 return;
187         }
188
189         mdt_counter_incr(req, LPROC_MDT_CROSSDIR_RENAME, count);
190         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_SRC_SIZE],
191                               (unsigned int)ma->ma_attr.la_size);
192
193         ma->ma_need = MA_INODE;
194         ma->ma_valid = 0;
195         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
196         if (rc) {
197                 CERROR("%s: "DFID" attr_get, rc = %d\n",
198                        mdt_obd_name(mdt), PFID(mdt_object_fid(tgt)), rc);
199                 return;
200         }
201
202         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_TGT_SIZE],
203                               (unsigned int)ma->ma_attr.la_size);
204 }
205
206 static ssize_t identity_expire_show(struct kobject *kobj,
207                                     struct attribute *attr, char *buf)
208 {
209         struct obd_device *obd = container_of(kobj, struct obd_device,
210                                               obd_kset.kobj);
211         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
212
213         return scnprintf(buf, PAGE_SIZE, "%lld\n",
214                          mdt->mdt_identity_cache->uc_entry_expire);
215 }
216
217 static ssize_t identity_expire_store(struct kobject *kobj,
218                                      struct attribute *attr,
219                                      const char *buffer, size_t count)
220 {
221         struct obd_device *obd = container_of(kobj, struct obd_device,
222                                               obd_kset.kobj);
223         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
224         time64_t val;
225         int rc;
226
227         rc = kstrtoll(buffer, 10, &val);
228         if (rc)
229                 return rc;
230
231         if (val < 0)
232                 return -ERANGE;
233
234         mdt->mdt_identity_cache->uc_entry_expire = val;
235
236         return count;
237 }
238 LUSTRE_RW_ATTR(identity_expire);
239
240 static ssize_t identity_acquire_expire_show(struct kobject *kobj,
241                                             struct attribute *attr, char *buf)
242 {
243         struct obd_device *obd = container_of(kobj, struct obd_device,
244                                               obd_kset.kobj);
245         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
246
247         return scnprintf(buf, PAGE_SIZE, "%lld\n",
248                          mdt->mdt_identity_cache->uc_acquire_expire);
249 }
250
251 static ssize_t identity_acquire_expire_store(struct kobject *kobj,
252                                              struct attribute *attr,
253                                              const char *buffer, size_t count)
254 {
255         struct obd_device *obd = container_of(kobj, struct obd_device,
256                                               obd_kset.kobj);
257         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
258         time64_t val;
259         int rc;
260
261         rc = kstrtoll(buffer, 0, &val);
262         if (rc)
263                 return rc;
264
265         if (val < 0 || val > INT_MAX)
266                 return -ERANGE;
267
268         mdt->mdt_identity_cache->uc_acquire_expire = val;
269
270         return count;
271 }
272 LUSTRE_RW_ATTR(identity_acquire_expire);
273
274 static ssize_t identity_upcall_show(struct kobject *kobj,
275                                     struct attribute *attr, char *buf)
276 {
277         struct obd_device *obd = container_of(kobj, struct obd_device,
278                                               obd_kset.kobj);
279         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
280         struct upcall_cache *hash = mdt->mdt_identity_cache;
281         int rc;
282
283         down_read(&hash->uc_upcall_rwsem);
284         rc = scnprintf(buf, PAGE_SIZE, "%s\n", hash->uc_upcall);
285         up_read(&hash->uc_upcall_rwsem);
286         return rc;
287 }
288
289 static ssize_t identity_upcall_store(struct kobject *kobj,
290                                      struct attribute *attr,
291                                      const char *buffer, size_t count)
292 {
293         struct obd_device *obd = container_of(kobj, struct obd_device,
294                                               obd_kset.kobj);
295         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
296         struct upcall_cache *hash = mdt->mdt_identity_cache;
297
298         if (count >= UC_CACHE_UPCALL_MAXPATH) {
299                 CERROR("%s: identity upcall too long\n", mdt_obd_name(mdt));
300                 return -EINVAL;
301         }
302
303         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
304         down_write(&hash->uc_upcall_rwsem);
305         sscanf(buffer, "%s", hash->uc_upcall);
306         up_write(&hash->uc_upcall_rwsem);
307
308         if (strcmp(hash->uc_name, mdt_obd_name(mdt)) != 0)
309                 CWARN("%s: write to upcall name %s\n",
310                       mdt_obd_name(mdt), hash->uc_upcall);
311
312         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
313                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
314                       "cause unexpected \"EACCESS\"\n", mdt_obd_name(mdt));
315
316         CDEBUG(D_CONFIG, "%s: identity upcall set to %s\n", mdt_obd_name(mdt),
317                hash->uc_upcall);
318         RETURN(count);
319 }
320 LUSTRE_RW_ATTR(identity_upcall);
321
322 static ssize_t identity_flush_store(struct kobject *kobj,
323                                     struct attribute *attr,
324                                     const char *buffer, size_t count)
325 {
326         struct obd_device *obd = container_of(kobj, struct obd_device,
327                                               obd_kset.kobj);
328         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
329         int uid;
330         int rc;
331
332         rc = kstrtoint(buffer, 0, &uid);
333         if (rc)
334                 return rc;
335
336         mdt_flush_identity(mdt->mdt_identity_cache, uid);
337         return count;
338 }
339 LUSTRE_WO_ATTR(identity_flush);
340
341 static ssize_t
342 lprocfs_identity_info_seq_write(struct file *file, const char __user *buffer,
343                                 size_t count, void *data)
344 {
345         struct seq_file   *m = file->private_data;
346         struct obd_device *obd = m->private;
347         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
348         struct identity_downcall_data *param;
349         int size = sizeof(*param), rc, checked = 0;
350
351 again:
352         if (count < size) {
353                 CERROR("%s: invalid data count = %lu, size = %d\n",
354                        mdt_obd_name(mdt), (unsigned long) count, size);
355                 return -EINVAL;
356         }
357
358         OBD_ALLOC(param, size);
359         if (param == NULL)
360                 return -ENOMEM;
361
362         if (copy_from_user(param, buffer, size)) {
363                 CERROR("%s: bad identity data\n", mdt_obd_name(mdt));
364                 GOTO(out, rc = -EFAULT);
365         }
366
367         if (checked == 0) {
368                 checked = 1;
369                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
370                         CERROR("%s: MDS identity downcall bad params\n",
371                                mdt_obd_name(mdt));
372                         GOTO(out, rc = -EINVAL);
373                 }
374
375                 if (param->idd_nperms > N_PERMS_MAX) {
376                         CERROR("%s: perm count %d more than maximum %d\n",
377                                mdt_obd_name(mdt), param->idd_nperms,
378                                N_PERMS_MAX);
379                         GOTO(out, rc = -EINVAL);
380                 }
381
382                 if (param->idd_ngroups > NGROUPS_MAX) {
383                         CERROR("%s: group count %d more than maximum %d\n",
384                                mdt_obd_name(mdt), param->idd_ngroups,
385                                NGROUPS_MAX);
386                         GOTO(out, rc = -EINVAL);
387                 }
388
389                 if (param->idd_ngroups) {
390                         rc = param->idd_ngroups; /* save idd_ngroups */
391                         OBD_FREE(param, size);
392                         size = offsetof(struct identity_downcall_data,
393                                         idd_groups[rc]);
394                         goto again;
395                 }
396         }
397
398         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
399                                    param->idd_uid, param);
400
401 out:
402         if (param != NULL)
403                 OBD_FREE(param, size);
404
405         return rc ? rc : count;
406 }
407 LPROC_SEQ_FOPS_WR_ONLY(mdt, identity_info);
408
409 static int mdt_site_stats_seq_show(struct seq_file *m, void *data)
410 {
411         struct obd_device *obd = m->private;
412         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
413
414         return lu_site_stats_seq_print(mdt_lu_site(mdt), m);
415 }
416 LPROC_SEQ_FOPS_RO(mdt_site_stats);
417
418 #define BUFLEN (UUID_MAX + 4)
419
420 static ssize_t
421 lprocfs_mds_evict_client_seq_write(struct file *file, const char __user *buf,
422                                    size_t count, loff_t *off)
423 {
424         struct seq_file   *m = file->private_data;
425         struct obd_device *obd = m->private;
426         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
427         char *kbuf;
428         char *tmpbuf;
429         int rc = 0;
430
431         OBD_ALLOC(kbuf, BUFLEN);
432         if (kbuf == NULL)
433                 return -ENOMEM;
434
435         /*
436          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
437          * bytes into kbuf, to ensure that the string is NUL-terminated.
438          * UUID_MAX should include a trailing NUL already.
439          */
440         if (copy_from_user(kbuf, buf, min_t(unsigned long, BUFLEN - 1, count)))
441                 GOTO(out, rc = -EFAULT);
442         tmpbuf = skip_spaces(kbuf);
443         tmpbuf = strsep(&tmpbuf, " \t\n\f\v\r");
444
445         if (strncmp(tmpbuf, "nid:", 4) != 0) {
446                 count = lprocfs_evict_client_seq_write(file, buf, count, off);
447                 goto out;
448         }
449
450         if (mdt->mdt_opts.mo_evict_tgt_nids) {
451                 rc = obd_set_info_async(NULL, mdt->mdt_child_exp,
452                                         sizeof(KEY_EVICT_BY_NID),
453                                         KEY_EVICT_BY_NID,
454                                         strlen(tmpbuf + 4) + 1,
455                                         tmpbuf + 4, NULL);
456                 if (rc)
457                         CERROR("Failed to evict nid %s from OSTs: rc %d\n",
458                                tmpbuf + 4, rc);
459         }
460
461         /* See the comments in function lprocfs_wr_evict_client()
462          * in ptlrpc/lproc_ptlrpc.c for details. - jay */
463         class_incref(obd, __func__, current);
464         obd_export_evict_by_nid(obd, tmpbuf + 4);
465         class_decref(obd, __func__, current);
466
467
468 out:
469         OBD_FREE(kbuf, BUFLEN);
470         return rc < 0 ? rc : count;
471 }
472
473 #undef BUFLEN
474
475 static ssize_t evict_tgt_nids_show(struct kobject *kobj,
476                                    struct attribute *attr, char *buf)
477 {
478         struct obd_device *obd = container_of(kobj, struct obd_device,
479                                               obd_kset.kobj);
480         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
481
482         return scnprintf(buf, PAGE_SIZE, "%u\n",
483                          mdt->mdt_opts.mo_evict_tgt_nids);
484 }
485
486 static ssize_t evict_tgt_nids_store(struct kobject *kobj,
487                                     struct attribute *attr, const char *buffer,
488                                     size_t count)
489 {
490         struct obd_device *obd = container_of(kobj, struct obd_device,
491                                               obd_kset.kobj);
492         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
493         bool val;
494         int rc;
495
496         rc = kstrtobool(buffer, &val);
497         if (rc)
498                 return rc;
499
500         mdt->mdt_opts.mo_evict_tgt_nids = val;
501         return count;
502 }
503 LUSTRE_RW_ATTR(evict_tgt_nids);
504
505 static ssize_t commit_on_sharing_show(struct kobject *kobj,
506                                       struct attribute *attr, char *buf)
507 {
508         struct obd_device *obd = container_of(kobj, struct obd_device,
509                                               obd_kset.kobj);
510         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
511
512         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt_cos_is_enabled(mdt));
513 }
514
515 static ssize_t commit_on_sharing_store(struct kobject *kobj,
516                                        struct attribute *attr,
517                                        const char *buffer, size_t count)
518 {
519         struct obd_device *obd = container_of(kobj, struct obd_device,
520                                               obd_kset.kobj);
521         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
522         bool val;
523         int rc;
524
525         rc = kstrtobool(buffer, &val);
526         if (rc)
527                 return rc;
528
529         mdt_enable_cos(mdt, val);
530         return count;
531 }
532 LUSTRE_RW_ATTR(commit_on_sharing);
533
534 static ssize_t local_recovery_show(struct kobject *kobj,
535                                       struct attribute *attr, char *buf)
536 {
537         struct obd_device *obd = container_of(kobj, struct obd_device,
538                                               obd_kset.kobj);
539
540         return scnprintf(buf, PAGE_SIZE, "%u\n",
541                          obd->u.obt.obt_lut->lut_local_recovery);
542 }
543
544 static ssize_t local_recovery_store(struct kobject *kobj,
545                                        struct attribute *attr,
546                                        const char *buffer, size_t count)
547 {
548         struct obd_device *obd = container_of(kobj, struct obd_device,
549                                               obd_kset.kobj);
550         bool val;
551         int rc;
552
553         rc = kstrtobool(buffer, &val);
554         if (rc)
555                 return rc;
556
557         obd->u.obt.obt_lut->lut_local_recovery = !!val;
558         return count;
559 }
560 LUSTRE_RW_ATTR(local_recovery);
561
562 static int mdt_root_squash_seq_show(struct seq_file *m, void *data)
563 {
564         struct obd_device *obd = m->private;
565         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
566         struct root_squash_info *squash = &mdt->mdt_squash;
567
568         seq_printf(m, "%u:%u\n", squash->rsi_uid,
569                    squash->rsi_gid);
570         return 0;
571 }
572
573 static ssize_t
574 mdt_root_squash_seq_write(struct file *file, const char __user *buffer,
575                           size_t count, loff_t *off)
576 {
577         struct seq_file   *m = file->private_data;
578         struct obd_device *obd = m->private;
579         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
580         struct root_squash_info *squash = &mdt->mdt_squash;
581
582         return lprocfs_wr_root_squash(buffer, count, squash,
583                                       mdt_obd_name(mdt));
584 }
585 LPROC_SEQ_FOPS(mdt_root_squash);
586
587 static int mdt_nosquash_nids_seq_show(struct seq_file *m, void *data)
588 {
589         struct obd_device *obd = m->private;
590         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
591         struct root_squash_info *squash = &mdt->mdt_squash;
592         int len = 0;
593
594         spin_lock(&squash->rsi_lock);
595         if (!list_empty(&squash->rsi_nosquash_nids)) {
596                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
597                                         &squash->rsi_nosquash_nids);
598                 m->count += len;
599                 seq_putc(m, '\n');
600         } else
601                 seq_puts(m, "NONE\n");
602         spin_unlock(&squash->rsi_lock);
603
604         return 0;
605 }
606
607 static ssize_t
608 mdt_nosquash_nids_seq_write(struct file *file, const char __user *buffer,
609                             size_t count, loff_t *off)
610 {
611         struct seq_file   *m = file->private_data;
612         struct obd_device *obd = m->private;
613         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
614         struct root_squash_info *squash = &mdt->mdt_squash;
615
616         return lprocfs_wr_nosquash_nids(buffer, count, squash,
617                                         mdt_obd_name(mdt));
618 }
619 LPROC_SEQ_FOPS(mdt_nosquash_nids);
620
621 static ssize_t enable_remote_dir_show(struct kobject *kobj,
622                                       struct attribute *attr, char *buf)
623 {
624         struct obd_device *obd = container_of(kobj, struct obd_device,
625                                               obd_kset.kobj);
626         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
627
628         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_remote_dir);
629 }
630
631 static ssize_t enable_remote_dir_store(struct kobject *kobj,
632                                        struct attribute *attr,
633                                        const char *buffer, size_t count)
634 {
635         struct obd_device *obd = container_of(kobj, struct obd_device,
636                                               obd_kset.kobj);
637         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
638         bool val;
639         int rc;
640
641         rc = kstrtobool(buffer, &val);
642         if (rc)
643                 return rc;
644
645         mdt->mdt_enable_remote_dir = val;
646         return count;
647 }
648 LUSTRE_RW_ATTR(enable_remote_dir);
649
650 static ssize_t enable_remote_dir_gid_show(struct kobject *kobj,
651                                           struct attribute *attr, char *buf)
652 {
653         struct obd_device *obd = container_of(kobj, struct obd_device,
654                                               obd_kset.kobj);
655         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
656
657         return scnprintf(buf, PAGE_SIZE, "%d\n",
658                          (int)mdt->mdt_enable_remote_dir_gid);
659 }
660
661 static ssize_t enable_remote_dir_gid_store(struct kobject *kobj,
662                                            struct attribute *attr,
663                                            const char *buffer, size_t count)
664 {
665         struct obd_device *obd = container_of(kobj, struct obd_device,
666                                               obd_kset.kobj);
667         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
668         int val;
669         int rc;
670
671         rc = kstrtoint(buffer, 0, &val);
672         if (rc)
673                 return rc;
674
675         mdt->mdt_enable_remote_dir_gid = val;
676         return count;
677 }
678 LUSTRE_RW_ATTR(enable_remote_dir_gid);
679
680 static ssize_t enable_chprojid_gid_show(struct kobject *kobj,
681                                         struct attribute *attr, char *buf)
682 {
683         struct obd_device *obd = container_of(kobj, struct obd_device,
684                                               obd_kset.kobj);
685         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
686
687         return scnprintf(buf, PAGE_SIZE, "%d\n",
688                          (int)mdt->mdt_enable_chprojid_gid);
689 }
690
691 static ssize_t enable_chprojid_gid_store(struct kobject *kobj,
692                                          struct attribute *attr,
693                                          const char *buffer, size_t count)
694 {
695         struct obd_device *obd = container_of(kobj, struct obd_device,
696                                               obd_kset.kobj);
697         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
698         int val;
699         int rc;
700
701         rc = kstrtoint(buffer, 0, &val);
702         if (rc)
703                 return rc;
704
705         mdt->mdt_enable_chprojid_gid = val;
706         return count;
707 }
708 LUSTRE_RW_ATTR(enable_chprojid_gid);
709
710 static ssize_t enable_striped_dir_show(struct kobject *kobj,
711                                        struct attribute *attr, char *buf)
712 {
713         struct obd_device *obd = container_of(kobj, struct obd_device,
714                                               obd_kset.kobj);
715         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
716
717         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_striped_dir);
718 }
719
720 static ssize_t enable_striped_dir_store(struct kobject *kobj,
721                                         struct attribute *attr,
722                                         const char *buffer, size_t count)
723 {
724         struct obd_device *obd = container_of(kobj, struct obd_device,
725                                               obd_kset.kobj);
726         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
727         bool val;
728         int rc;
729
730         rc = kstrtobool(buffer, &val);
731         if (rc)
732                 return rc;
733
734         mdt->mdt_enable_striped_dir = val;
735         return count;
736 }
737 LUSTRE_RW_ATTR(enable_striped_dir);
738
739 static ssize_t enable_dir_migration_show(struct kobject *kobj,
740                                          struct attribute *attr, char *buf)
741 {
742         struct obd_device *obd = container_of(kobj, struct obd_device,
743                                               obd_kset.kobj);
744         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
745
746         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_dir_migration);
747 }
748
749 static ssize_t enable_dir_migration_store(struct kobject *kobj,
750                                           struct attribute *attr,
751                                           const char *buffer, size_t count)
752 {
753         struct obd_device *obd = container_of(kobj, struct obd_device,
754                                               obd_kset.kobj);
755         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
756         bool val;
757         int rc;
758
759         rc = kstrtobool(buffer, &val);
760         if (rc)
761                 return rc;
762
763         mdt->mdt_enable_dir_migration = val;
764         return count;
765 }
766 LUSTRE_RW_ATTR(enable_dir_migration);
767
768 static ssize_t enable_dir_restripe_show(struct kobject *kobj,
769                                         struct attribute *attr, char *buf)
770 {
771         struct obd_device *obd = container_of(kobj, struct obd_device,
772                                               obd_kset.kobj);
773         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
774
775         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_enable_dir_restripe);
776 }
777
778 static ssize_t enable_dir_restripe_store(struct kobject *kobj,
779                                          struct attribute *attr,
780                                          const char *buffer, size_t count)
781 {
782         struct obd_device *obd = container_of(kobj, struct obd_device,
783                                               obd_kset.kobj);
784         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
785         bool val;
786         int rc;
787
788         rc = kstrtobool(buffer, &val);
789         if (rc)
790                 return rc;
791
792         mdt->mdt_enable_dir_restripe = val;
793         return count;
794 }
795 LUSTRE_RW_ATTR(enable_dir_restripe);
796
797 static ssize_t enable_dir_auto_split_show(struct kobject *kobj,
798                                           struct attribute *attr, char *buf)
799 {
800         struct obd_device *obd = container_of(kobj, struct obd_device,
801                                               obd_kset.kobj);
802         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
803
804         return scnprintf(buf, PAGE_SIZE, "%u\n",
805                          mdt->mdt_enable_dir_auto_split);
806 }
807
808 static ssize_t enable_dir_auto_split_store(struct kobject *kobj,
809                                            struct attribute *attr,
810                                            const char *buffer, size_t count)
811 {
812         struct obd_device *obd = container_of(kobj, struct obd_device,
813                                               obd_kset.kobj);
814         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
815         bool val;
816         int rc;
817
818         rc = kstrtobool(buffer, &val);
819         if (rc)
820                 return rc;
821
822         mdt->mdt_enable_dir_auto_split = val;
823         return count;
824 }
825 LUSTRE_RW_ATTR(enable_dir_auto_split);
826
827 /**
828  * Show MDT async commit count.
829  *
830  * @m           seq_file handle
831  * @data        unused for single entry
832  *
833  * Return:      0 on success
834  *              negative value on error
835  */
836 static ssize_t async_commit_count_show(struct kobject *kobj,
837                                        struct attribute *attr, char *buf)
838 {
839         struct obd_device *obd = container_of(kobj, struct obd_device,
840                                               obd_kset.kobj);
841         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
842
843         return scnprintf(buf, PAGE_SIZE, "%d\n",
844                          atomic_read(&mdt->mdt_async_commit_count));
845 }
846
847 static ssize_t async_commit_count_store(struct kobject *kobj,
848                                         struct attribute *attr,
849                                         const char *buffer, size_t count)
850 {
851         struct obd_device *obd = container_of(kobj, struct obd_device,
852                                               obd_kset.kobj);
853         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
854         int val;
855         int rc;
856
857         rc = kstrtoint(buffer, 10, &val);
858         if (rc)
859                 return rc;
860
861         atomic_set(&mdt->mdt_async_commit_count, val);
862
863         return count;
864 }
865 LUSTRE_RW_ATTR(async_commit_count);
866
867 /**
868  * Show MDT sync count.
869  *
870  * \param[in] m         seq_file handle
871  * \param[in] data      unused for single entry
872  *
873  * \retval              0 on success
874  * \retval              negative value on error
875  */
876 static ssize_t sync_count_show(struct kobject *kobj, struct attribute *attr,
877                                char *buf)
878 {
879         struct obd_device *obd = container_of(kobj, struct obd_device,
880                                               obd_kset.kobj);
881         struct lu_target *tgt = obd->u.obt.obt_lut;
882
883         return scnprintf(buf, PAGE_SIZE, "%d\n",
884                          atomic_read(&tgt->lut_sync_count));
885 }
886
887 static ssize_t sync_count_store(struct kobject *kobj, struct attribute *attr,
888                                 const char *buffer, size_t count)
889 {
890         struct obd_device *obd = container_of(kobj, struct obd_device,
891                                               obd_kset.kobj);
892         struct lu_target *tgt = obd->u.obt.obt_lut;
893         int val;
894         int rc;
895
896         rc = kstrtoint(buffer, 0, &val);
897         if (rc)
898                 return rc;
899
900         atomic_set(&tgt->lut_sync_count, val);
901
902         return count;
903 }
904 LUSTRE_RW_ATTR(sync_count);
905
906 static const char *dom_open_lock_modes[NUM_DOM_LOCK_ON_OPEN_MODES] = {
907         [NO_DOM_LOCK_ON_OPEN] = "never",
908         [TRYLOCK_DOM_ON_OPEN] = "trylock",
909         [ALWAYS_DOM_LOCK_ON_OPEN] = "always",
910 };
911
912 /* This must be longer than the longest string above */
913 #define DOM_LOCK_MODES_MAXLEN 16
914
915 /**
916  * Show MDT policy for data prefetch on open for DoM files..
917  *
918  * \param[in] m         seq_file handle
919  * \param[in] data      unused
920  *
921  * \retval              0 on success
922  * \retval              negative value on error
923  */
924 static ssize_t dom_lock_show(struct kobject *kobj, struct attribute *attr,
925                              char *buf)
926 {
927         struct obd_device *obd = container_of(kobj, struct obd_device,
928                                               obd_kset.kobj);
929         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
930
931         return scnprintf(buf, PAGE_SIZE, "%s\n",
932                          dom_open_lock_modes[mdt->mdt_opts.mo_dom_lock]);
933 }
934
935 /**
936  * Change MDT policy for data prefetch on open for DoM files.
937  *
938  * This variable defines how DOM lock is taken at open enqueue.
939  * There are three possible modes:
940  * 1) never - never take DoM lock on open. DoM lock will be taken as separate
941  *    IO lock with own enqueue.
942  * 2) trylock - DoM lock will be taken only if non-blocked.
943  * 3) always - DoM lock will be taken always even if it is blocking lock.
944  *
945  * If dom_read_open is enabled too then DoM lock is taken in PR mode and
946  * is paired with LAYOUT lock when possible.
947  *
948  * \param[in] file      proc file
949  * \param[in] buffer    string which represents policy
950  * \param[in] count     \a buffer length
951  * \param[in] off       unused for single entry
952  *
953  * \retval              \a count on success
954  * \retval              negative number on error
955  */
956 static ssize_t dom_lock_store(struct kobject *kobj, struct attribute *attr,
957                               const char *buffer, size_t count)
958 {
959         struct obd_device *obd = container_of(kobj, struct obd_device,
960                                               obd_kset.kobj);
961         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
962         int val = -1;
963         int i, rc;
964
965         if (count == 0 || count >= DOM_LOCK_MODES_MAXLEN)
966                 return -EINVAL;
967
968         for (i = 0 ; i < NUM_DOM_LOCK_ON_OPEN_MODES; i++) {
969                 /* buffer might have '\n' but using strlen() avoids it */
970                 if (strncmp(buffer, dom_open_lock_modes[i],
971                             strlen(dom_open_lock_modes[i])) == 0) {
972                         val = i;
973                         break;
974                 }
975         }
976
977         /* Legacy numeric codes */
978         if (val == -1) {
979                 rc = kstrtoint(buffer, 0, &val);
980                 if (rc)
981                         return rc;
982         }
983
984         if (val == ALWAYS_DOM_LOCK_ON_OPEN)
985                 val = TRYLOCK_DOM_ON_OPEN;
986
987         if (val < 0 || val >= NUM_DOM_LOCK_ON_OPEN_MODES)
988                 return -EINVAL;
989
990         mdt->mdt_opts.mo_dom_lock = val;
991         return count;
992 }
993 LUSTRE_RW_ATTR(dom_lock);
994
995 /**
996  * Show MDT policy for data prefetch on open for DoM files..
997  *
998  * \param[in] m         seq_file handle
999  * \param[in] data      unused
1000  *
1001  * \retval              0 on success
1002  * \retval              negative value on error
1003  */
1004 static ssize_t dom_read_open_show(struct kobject *kobj,
1005                                   struct attribute *attr, char *buf)
1006 {
1007         struct obd_device *obd = container_of(kobj, struct obd_device,
1008                                               obd_kset.kobj);
1009         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1010
1011         return scnprintf(buf, PAGE_SIZE, "%u\n",
1012                          !!mdt->mdt_opts.mo_dom_read_open);
1013 }
1014
1015 /**
1016  * Modify MDT policy for data prefetch on open for DoM files.
1017  *
1018  * If enabled then Data-on-MDT file data may be read during open and
1019  * returned back in reply. It works only with mo_dom_lock enabled.
1020  *
1021  * \param[in] file      proc file
1022  * \param[in] buffer    string which represents policy
1023  * \param[in] count     \a buffer length
1024  * \param[in] off       unused for single entry
1025  *
1026  * \retval              \a count on success
1027  * \retval              negative number on error
1028  */
1029 static ssize_t dom_read_open_store(struct kobject *kobj,
1030                                    struct attribute *attr, const char *buffer,
1031                                    size_t count)
1032 {
1033         struct obd_device *obd = container_of(kobj, struct obd_device,
1034                                               obd_kset.kobj);
1035         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1036         bool val;
1037         int rc;
1038
1039         rc = kstrtobool(buffer, &val);
1040         if (rc)
1041                 return rc;
1042
1043         mdt->mdt_opts.mo_dom_read_open = !!val;
1044         return count;
1045 }
1046 LUSTRE_RW_ATTR(dom_read_open);
1047
1048 static ssize_t migrate_hsm_allowed_show(struct kobject *kobj,
1049                                         struct attribute *attr, char *buf)
1050 {
1051         struct obd_device *obd = container_of(kobj, struct obd_device,
1052                                               obd_kset.kobj);
1053         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1054
1055         return scnprintf(buf, PAGE_SIZE, "%u\n",
1056                          mdt->mdt_opts.mo_migrate_hsm_allowed);
1057 }
1058
1059 static ssize_t migrate_hsm_allowed_store(struct kobject *kobj,
1060                                          struct attribute *attr,
1061                                          const char *buffer, size_t count)
1062 {
1063         struct obd_device *obd = container_of(kobj, struct obd_device,
1064                                               obd_kset.kobj);
1065         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1066         bool val;
1067         int rc;
1068
1069         rc = kstrtobool(buffer, &val);
1070         if (rc)
1071                 return rc;
1072
1073         mdt->mdt_opts.mo_migrate_hsm_allowed = val;
1074         return count;
1075 }
1076 LUSTRE_RW_ATTR(migrate_hsm_allowed);
1077
1078 static ssize_t readonly_show(struct kobject *kobj, struct attribute *attr,
1079                              char *buf)
1080 {
1081         struct obd_device *obd = container_of(kobj, struct obd_device,
1082                                               obd_kset.kobj);
1083         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1084
1085         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_readonly);
1086 }
1087
1088 static ssize_t readonly_store(struct kobject *kobj, struct attribute *attr,
1089                               const char *buffer, size_t count)
1090 {
1091         struct obd_device *obd = container_of(kobj, struct obd_device,
1092                                               obd_kset.kobj);
1093         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1094         bool val;
1095         int rc;
1096
1097         rc = kstrtobool(buffer, &val);
1098         if (rc)
1099                 return rc;
1100
1101         mdt->mdt_readonly = val;
1102         return count;
1103 }
1104 LUSTRE_RW_ATTR(readonly);
1105
1106 static ssize_t enable_remote_rename_show(struct kobject *kobj,
1107                                          struct attribute *attr,
1108                                          char *buf)
1109 {
1110         struct obd_device *obd = container_of(kobj, struct obd_device,
1111                                               obd_kset.kobj);
1112         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1113
1114         return scnprintf(buf, PAGE_SIZE, "%u\n",
1115                          mdt->mdt_enable_remote_rename);
1116 }
1117
1118 static ssize_t enable_remote_rename_store(struct kobject *kobj,
1119                                           struct attribute *attr,
1120                                           const char *buffer, size_t count)
1121 {
1122         struct obd_device *obd = container_of(kobj, struct obd_device,
1123                                               obd_kset.kobj);
1124         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1125         bool val;
1126         int rc;
1127
1128         rc = kstrtobool(buffer, &val);
1129         if (rc)
1130                 return rc;
1131
1132         mdt->mdt_enable_remote_rename = val;
1133         return count;
1134 }
1135 LUSTRE_RW_ATTR(enable_remote_rename);
1136
1137 static ssize_t dir_split_count_show(struct kobject *kobj,
1138                                      struct attribute *attr,
1139                                      char *buf)
1140 {
1141         struct obd_device *obd = container_of(kobj, struct obd_device,
1142                                               obd_kset.kobj);
1143         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1144
1145         return scnprintf(buf, PAGE_SIZE, "%llu\n",
1146                          mdt->mdt_restriper.mdr_dir_split_count);
1147 }
1148
1149 static ssize_t dir_split_count_store(struct kobject *kobj,
1150                                       struct attribute *attr,
1151                                       const char *buffer, size_t count)
1152 {
1153         struct obd_device *obd = container_of(kobj, struct obd_device,
1154                                               obd_kset.kobj);
1155         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1156         s64 val;
1157         int rc;
1158
1159         rc = sysfs_memparse(buffer, count, &val, "B");
1160         if (rc < 0)
1161                 return rc;
1162
1163         if (val < 0)
1164                 return -ERANGE;
1165
1166         mdt->mdt_restriper.mdr_dir_split_count = val;
1167
1168         return count;
1169 }
1170 LUSTRE_RW_ATTR(dir_split_count);
1171
1172 static ssize_t dir_split_delta_show(struct kobject *kobj,
1173                                     struct attribute *attr,
1174                                     char *buf)
1175 {
1176         struct obd_device *obd = container_of(kobj, struct obd_device,
1177                                               obd_kset.kobj);
1178         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1179
1180         return scnprintf(buf, PAGE_SIZE, "%u\n",
1181                          mdt->mdt_restriper.mdr_dir_split_delta);
1182 }
1183
1184 static ssize_t dir_split_delta_store(struct kobject *kobj,
1185                                      struct attribute *attr,
1186                                      const char *buffer, size_t count)
1187 {
1188         struct obd_device *obd = container_of(kobj, struct obd_device,
1189                                               obd_kset.kobj);
1190         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1191         u32 val;
1192         int rc;
1193
1194         rc = kstrtouint(buffer, 0, &val);
1195         if (rc)
1196                 return rc;
1197
1198         mdt->mdt_restriper.mdr_dir_split_delta = val;
1199
1200         return count;
1201 }
1202 LUSTRE_RW_ATTR(dir_split_delta);
1203
1204 static ssize_t dir_restripe_nsonly_show(struct kobject *kobj,
1205                                         struct attribute *attr, char *buf)
1206 {
1207         struct obd_device *obd = container_of(kobj, struct obd_device,
1208                                               obd_kset.kobj);
1209         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1210
1211         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_dir_restripe_nsonly);
1212 }
1213
1214 static ssize_t dir_restripe_nsonly_store(struct kobject *kobj,
1215                                          struct attribute *attr,
1216                                          const char *buffer, size_t count)
1217 {
1218         struct obd_device *obd = container_of(kobj, struct obd_device,
1219                                               obd_kset.kobj);
1220         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1221         bool val;
1222         int rc;
1223
1224         rc = kstrtobool(buffer, &val);
1225         if (rc)
1226                 return rc;
1227
1228         mdt->mdt_dir_restripe_nsonly = val;
1229         return count;
1230 }
1231 LUSTRE_RW_ATTR(dir_restripe_nsonly);
1232
1233 static ssize_t enable_remote_subdir_mount_show(struct kobject *kobj,
1234                                                struct attribute *attr,
1235                                                char *buf)
1236 {
1237         struct obd_device *obd = container_of(kobj, struct obd_device,
1238                                               obd_kset.kobj);
1239         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1240
1241         return scnprintf(buf, PAGE_SIZE, "%u\n",
1242                          mdt->mdt_enable_remote_subdir_mount);
1243 }
1244
1245 static ssize_t enable_remote_subdir_mount_store(struct kobject *kobj,
1246                                                 struct attribute *attr,
1247                                                 const char *buffer,
1248                                                 size_t count)
1249 {
1250         struct obd_device *obd = container_of(kobj, struct obd_device,
1251                                               obd_kset.kobj);
1252         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1253         bool val;
1254         int rc;
1255
1256         rc = kstrtobool(buffer, &val);
1257         if (rc)
1258                 return rc;
1259
1260         mdt->mdt_enable_remote_subdir_mount = val;
1261         return count;
1262 }
1263 LUSTRE_RW_ATTR(enable_remote_subdir_mount);
1264
1265 /**
1266  * Show if the OFD enforces T10PI checksum.
1267  *
1268  * \param[in] m         seq_file handle
1269  * \param[in] data      unused for single entry
1270  *
1271  * \retval              0 on success
1272  * \retval              negative value on error
1273  */
1274 static ssize_t checksum_t10pi_enforce_show(struct kobject *kobj,
1275                                            struct attribute *attr,
1276                                            char *buf)
1277 {
1278         struct obd_device *obd = container_of(kobj, struct obd_device,
1279                                               obd_kset.kobj);
1280         struct lu_target *lut = obd->u.obt.obt_lut;
1281
1282         return scnprintf(buf, PAGE_SIZE, "%u\n", lut->lut_cksum_t10pi_enforce);
1283 }
1284
1285 /**
1286  * Force specific T10PI checksum modes to be enabled
1287  *
1288  * If T10PI *is* supported in hardware, allow only the supported T10PI type
1289  * to be used. If T10PI is *not* supported by the OSD, setting the enforce
1290  * parameter forces all T10PI types to be enabled (even if slower) for
1291  * testing.
1292  *
1293  * The final determination of which algorithm to be used depends whether
1294  * the client supports T10PI or not, and is handled at client connect time.
1295  *
1296  * \param[in] file      proc file
1297  * \param[in] buffer    string which represents mode
1298  *                      1: set T10PI checksums enforced
1299  *                      0: unset T10PI checksums enforced
1300  * \param[in] count     \a buffer length
1301  * \param[in] off       unused for single entry
1302  *
1303  * \retval              \a count on success
1304  * \retval              negative number on error
1305  */
1306 static ssize_t checksum_t10pi_enforce_store(struct kobject *kobj,
1307                                             struct attribute *attr,
1308                                             const char *buffer, size_t count)
1309 {
1310         struct obd_device *obd = container_of(kobj, struct obd_device,
1311                                               obd_kset.kobj);
1312         struct lu_target *lut = obd->u.obt.obt_lut;
1313         bool enforce;
1314         int rc;
1315
1316         rc = kstrtobool(buffer, &enforce);
1317         if (rc)
1318                 return rc;
1319
1320         spin_lock(&lut->lut_flags_lock);
1321         lut->lut_cksum_t10pi_enforce = enforce;
1322         spin_unlock(&lut->lut_flags_lock);
1323         return count;
1324 }
1325 LUSTRE_RW_ATTR(checksum_t10pi_enforce);
1326
1327 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
1328 LPROC_SEQ_FOPS_WR_ONLY(mdt, mds_evict_client);
1329 LPROC_SEQ_FOPS_RW_TYPE(mdt, checksum_dump);
1330 LUSTRE_RW_ATTR(job_cleanup_interval);
1331 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
1332 LUSTRE_RW_ATTR(hsm_control);
1333
1334 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
1335 LUSTRE_RW_ATTR(recovery_time_hard);
1336 LUSTRE_RW_ATTR(recovery_time_soft);
1337 LUSTRE_RW_ATTR(ir_factor);
1338
1339 LUSTRE_RO_ATTR(tot_dirty);
1340 LUSTRE_RO_ATTR(tot_granted);
1341 LUSTRE_RO_ATTR(tot_pending);
1342 LUSTRE_RW_ATTR(grant_compat_disable);
1343 LUSTRE_RO_ATTR(instance);
1344
1345 LUSTRE_RO_ATTR(num_exports);
1346
1347 static struct attribute *mdt_attrs[] = {
1348         &lustre_attr_tot_dirty.attr,
1349         &lustre_attr_tot_granted.attr,
1350         &lustre_attr_tot_pending.attr,
1351         &lustre_attr_grant_compat_disable.attr,
1352         &lustre_attr_instance.attr,
1353         &lustre_attr_recovery_time_hard.attr,
1354         &lustre_attr_recovery_time_soft.attr,
1355         &lustre_attr_ir_factor.attr,
1356         &lustre_attr_num_exports.attr,
1357         &lustre_attr_identity_expire.attr,
1358         &lustre_attr_identity_acquire_expire.attr,
1359         &lustre_attr_identity_upcall.attr,
1360         &lustre_attr_identity_flush.attr,
1361         &lustre_attr_evict_tgt_nids.attr,
1362         &lustre_attr_enable_remote_dir.attr,
1363         &lustre_attr_enable_remote_dir_gid.attr,
1364         &lustre_attr_enable_chprojid_gid.attr,
1365         &lustre_attr_enable_striped_dir.attr,
1366         &lustre_attr_enable_dir_migration.attr,
1367         &lustre_attr_enable_dir_restripe.attr,
1368         &lustre_attr_enable_dir_auto_split.attr,
1369         &lustre_attr_enable_remote_rename.attr,
1370         &lustre_attr_commit_on_sharing.attr,
1371         &lustre_attr_local_recovery.attr,
1372         &lustre_attr_async_commit_count.attr,
1373         &lustre_attr_sync_count.attr,
1374         &lustre_attr_dom_lock.attr,
1375         &lustre_attr_dom_read_open.attr,
1376         &lustre_attr_migrate_hsm_allowed.attr,
1377         &lustre_attr_hsm_control.attr,
1378         &lustre_attr_job_cleanup_interval.attr,
1379         &lustre_attr_readonly.attr,
1380         &lustre_attr_dir_split_count.attr,
1381         &lustre_attr_dir_split_delta.attr,
1382         &lustre_attr_dir_restripe_nsonly.attr,
1383         &lustre_attr_checksum_t10pi_enforce.attr,
1384         &lustre_attr_enable_remote_subdir_mount.attr,
1385         NULL,
1386 };
1387
1388 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
1389         { .name =       "recovery_status",
1390           .fops =       &mdt_recovery_status_fops               },
1391         { .name =       "identity_info",
1392           .fops =       &mdt_identity_info_fops                 },
1393         { .name =       "site_stats",
1394           .fops =       &mdt_site_stats_fops                    },
1395         { .name =       "evict_client",
1396           .fops =       &mdt_mds_evict_client_fops              },
1397         { .name =       "checksum_dump",
1398           .fops =       &mdt_checksum_dump_fops                 },
1399         { .name =       "hash_stats",
1400           .fops =       &mdt_hash_fops                          },
1401         { .name =       "root_squash",
1402           .fops =       &mdt_root_squash_fops                   },
1403         { .name =       "nosquash_nids",
1404           .fops =       &mdt_nosquash_nids_fops                 },
1405         { NULL }
1406 };
1407
1408 static int
1409 lprocfs_mdt_print_open_files(struct obd_export *exp, void *v)
1410 {
1411         struct seq_file         *seq = v;
1412
1413         if (exp->exp_lock_hash != NULL) {
1414                 struct mdt_export_data  *med = &exp->exp_mdt_data;
1415                 struct mdt_file_data    *mfd;
1416
1417                 spin_lock(&med->med_open_lock);
1418                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1419                         seq_printf(seq, DFID"\n",
1420                                    PFID(mdt_object_fid(mfd->mfd_object)));
1421                 }
1422                 spin_unlock(&med->med_open_lock);
1423         }
1424
1425         return 0;
1426 }
1427
1428 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
1429 {
1430         struct nid_stat *stats = seq->private;
1431
1432         return obd_nid_export_for_each(stats->nid_obd, stats->nid,
1433                                        lprocfs_mdt_print_open_files, seq);
1434 }
1435
1436 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
1437 {
1438         struct seq_file         *seq;
1439         int                     rc;
1440
1441         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
1442         if (rc != 0)
1443                 return rc;
1444
1445         seq = file->private_data;
1446         seq->private = PDE_DATA(inode);
1447
1448         return 0;
1449 }
1450
1451 void mdt_counter_incr(struct ptlrpc_request *req, int opcode, long amount)
1452 {
1453         struct obd_export *exp = req->rq_export;
1454
1455         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
1456                 lprocfs_counter_add(exp->exp_obd->obd_md_stats,
1457                                     opcode + LPROC_MD_LAST_OPC, amount);
1458         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1459                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats, opcode,
1460                                     amount);
1461         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
1462             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1463                 lprocfs_job_stats_log(exp->exp_obd,
1464                                       lustre_msg_get_jobid(req->rq_reqmsg),
1465                                       opcode, amount);
1466 }
1467
1468 static const char * const mdt_stats[] = {
1469         [LPROC_MDT_OPEN]                = "open",
1470         [LPROC_MDT_CLOSE]               = "close",
1471         [LPROC_MDT_MKNOD]               = "mknod",
1472         [LPROC_MDT_LINK]                = "link",
1473         [LPROC_MDT_UNLINK]              = "unlink",
1474         [LPROC_MDT_MKDIR]               = "mkdir",
1475         [LPROC_MDT_RMDIR]               = "rmdir",
1476         [LPROC_MDT_RENAME]              = "rename",
1477         [LPROC_MDT_GETATTR]             = "getattr",
1478         [LPROC_MDT_SETATTR]             = "setattr",
1479         [LPROC_MDT_GETXATTR]            = "getxattr",
1480         [LPROC_MDT_SETXATTR]            = "setxattr",
1481         [LPROC_MDT_STATFS]              = "statfs",
1482         [LPROC_MDT_SYNC]                = "sync",
1483         [LPROC_MDT_SAMEDIR_RENAME]      = "samedir_rename",
1484         [LPROC_MDT_CROSSDIR_RENAME]     = "crossdir_rename",
1485         [LPROC_MDT_IO_READ]             = "read_bytes",
1486         [LPROC_MDT_IO_WRITE]            = "write_bytes",
1487         [LPROC_MDT_IO_PUNCH]            = "punch",
1488         [LPROC_MDT_MIGRATE]             = "migrate",
1489         [LPROC_MDT_FALLOCATE]           = "fallocate",
1490 };
1491
1492 void mdt_stats_counter_init(struct lprocfs_stats *stats, unsigned int offset)
1493 {
1494         int array_size = ARRAY_SIZE(mdt_stats);
1495         int oidx; /* obd_md_stats index */
1496         int midx; /* mdt_stats index */
1497
1498         LASSERT(stats && stats->ls_num >= offset + array_size);
1499
1500         for (midx = 0; midx < array_size; midx++) {
1501                 oidx = midx + offset;
1502                 if (midx == LPROC_MDT_IO_READ || midx == LPROC_MDT_IO_WRITE)
1503                         lprocfs_counter_init(stats, oidx,
1504                                              LPROCFS_TYPE_BYTES_FULL,
1505                                              mdt_stats[midx], "bytes");
1506                 else
1507                         lprocfs_counter_init(stats, oidx,
1508                                              LPROCFS_TYPE_LATENCY,
1509                                              mdt_stats[midx], "usecs");
1510         }
1511 }
1512
1513 int mdt_tunables_init(struct mdt_device *mdt, const char *name)
1514 {
1515         struct obd_device *obd = mdt2obd_dev(mdt);
1516         int rc;
1517
1518         ENTRY;
1519         LASSERT(name != NULL);
1520
1521         obd->obd_ktype.default_attrs = mdt_attrs;
1522         obd->obd_vars = lprocfs_mdt_obd_vars;
1523         rc = lprocfs_obd_setup(obd, true);
1524         if (rc) {
1525                 CERROR("%s: cannot create proc entries: rc = %d\n",
1526                        mdt_obd_name(mdt), rc);
1527                 return rc;
1528         }
1529
1530         rc = tgt_tunables_init(&mdt->mdt_lut);
1531         if (rc) {
1532                 CERROR("%s: failed to init target tunables: rc = %d\n",
1533                        mdt_obd_name(mdt), rc);
1534                 return rc;
1535         }
1536
1537         rc = hsm_cdt_tunables_init(mdt);
1538         if (rc) {
1539                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
1540                        mdt_obd_name(mdt), rc);
1541                 return rc;
1542         }
1543
1544         obd->obd_proc_exports_entry = proc_mkdir("exports",
1545                                                  obd->obd_proc_entry);
1546         if (obd->obd_proc_exports_entry)
1547                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
1548                                    obd, &mdt_nid_stats_clear_fops);
1549
1550         rc = lprocfs_alloc_md_stats(obd, ARRAY_SIZE(mdt_stats));
1551         if (rc)
1552                 return rc;
1553
1554         /* add additional MDT md_stats after the default ones */
1555         mdt_stats_counter_init(obd->obd_md_stats, LPROC_MD_LAST_OPC);
1556         rc = lprocfs_job_stats_init(obd, ARRAY_SIZE(mdt_stats),
1557                                     mdt_stats_counter_init);
1558
1559         rc = lproc_mdt_attach_rename_seqstat(mdt);
1560         if (rc)
1561                 CERROR("%s: MDT can not create rename stats rc = %d\n",
1562                        mdt_obd_name(mdt), rc);
1563
1564         RETURN(rc);
1565 }
1566
1567 void mdt_tunables_fini(struct mdt_device *mdt)
1568 {
1569         struct obd_device *obd = mdt2obd_dev(mdt);
1570
1571         if (obd->obd_proc_exports_entry != NULL) {
1572                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
1573                 obd->obd_proc_exports_entry = NULL;
1574         }
1575
1576         lprocfs_free_per_client_stats(obd);
1577         /* hsm_cdt_tunables is disabled earlier than this to avoid
1578          * coordinator restart.
1579          */
1580         hsm_cdt_tunables_fini(mdt);
1581         tgt_tunables_fini(&mdt->mdt_lut);
1582         lprocfs_obd_cleanup(obd);
1583         lprocfs_free_md_stats(obd);
1584         lprocfs_free_obd_stats(obd);
1585         lprocfs_job_stats_fini(obd);
1586 }