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