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