Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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 (msi) /* parallel rename type */
182                 mdt_counter_incr(req, msi, ktime_delta);
183
184         if (src == tgt) {
185                 mdt_counter_incr(req, LPROC_MDT_RENAME_SAMEDIR, ktime_delta);
186                 lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_SAMEDIR_SIZE],
187                                       (unsigned int)ma->ma_attr.la_size);
188                 return;
189         }
190
191         mdt_counter_incr(req, LPROC_MDT_RENAME_CROSSDIR, ktime_delta);
192         lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_CROSSDIR_SRC_SIZE],
193                               (unsigned int)ma->ma_attr.la_size);
194
195         ma->ma_need = MA_INODE;
196         ma->ma_valid = 0;
197         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
198         if (rc) {
199                 CERROR("%s: "DFID" attr_get, rc = %d\n",
200                        mdt_obd_name(mdt), PFID(mdt_object_fid(tgt)), rc);
201                 return;
202         }
203
204         lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_CROSSDIR_TGT_SIZE],
205                               (unsigned int)ma->ma_attr.la_size);
206 }
207
208 static ssize_t identity_expire_show(struct kobject *kobj,
209                                     struct attribute *attr, char *buf)
210 {
211         struct obd_device *obd = container_of(kobj, struct obd_device,
212                                               obd_kset.kobj);
213         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
214
215         return scnprintf(buf, PAGE_SIZE, "%lld\n",
216                          mdt->mdt_identity_cache->uc_entry_expire);
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         time64_t val;
227         int rc;
228
229         rc = kstrtoll(buffer, 10, &val);
230         if (rc)
231                 return rc;
232
233         if (val < 0)
234                 return -ERANGE;
235
236         mdt->mdt_identity_cache->uc_entry_expire = val;
237
238         return count;
239 }
240 LUSTRE_RW_ATTR(identity_expire);
241
242 static ssize_t identity_acquire_expire_show(struct kobject *kobj,
243                                             struct attribute *attr, char *buf)
244 {
245         struct obd_device *obd = container_of(kobj, struct obd_device,
246                                               obd_kset.kobj);
247         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
248
249         return scnprintf(buf, PAGE_SIZE, "%lld\n",
250                          mdt->mdt_identity_cache->uc_acquire_expire);
251 }
252
253 static ssize_t identity_acquire_expire_store(struct kobject *kobj,
254                                              struct attribute *attr,
255                                              const char *buffer, size_t count)
256 {
257         struct obd_device *obd = container_of(kobj, struct obd_device,
258                                               obd_kset.kobj);
259         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
260         time64_t val;
261         int rc;
262
263         rc = kstrtoll(buffer, 0, &val);
264         if (rc)
265                 return rc;
266
267         if (val < 0 || val > INT_MAX)
268                 return -ERANGE;
269
270         mdt->mdt_identity_cache->uc_acquire_expire = val;
271
272         return count;
273 }
274 LUSTRE_RW_ATTR(identity_acquire_expire);
275
276 static ssize_t identity_upcall_show(struct kobject *kobj,
277                                     struct attribute *attr, char *buf)
278 {
279         struct obd_device *obd = container_of(kobj, struct obd_device,
280                                               obd_kset.kobj);
281         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
282         struct upcall_cache *hash = mdt->mdt_identity_cache;
283         int rc;
284
285         down_read(&hash->uc_upcall_rwsem);
286         rc = scnprintf(buf, PAGE_SIZE, "%s\n", hash->uc_upcall);
287         up_read(&hash->uc_upcall_rwsem);
288         return rc;
289 }
290
291 static ssize_t identity_upcall_store(struct kobject *kobj,
292                                      struct attribute *attr,
293                                      const char *buffer, size_t count)
294 {
295         struct obd_device *obd = container_of(kobj, struct obd_device,
296                                               obd_kset.kobj);
297         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
298         struct upcall_cache *hash = mdt->mdt_identity_cache;
299         int rc;
300
301         rc = upcall_cache_set_upcall(hash, buffer, count, false);
302         if (rc) {
303                 CERROR("%s: incorrect identity upcall %.*s. Valid values for mdt.%s.identity_upcall are NONE, or an executable pathname: rc = %d\n",
304                        mdt_obd_name(mdt), (int)count, buffer,
305                        mdt_obd_name(mdt), rc);
306                 return rc;
307         }
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_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 commit_on_sharing_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", mdt_cos_is_enabled(mdt));
484 }
485
486 static ssize_t commit_on_sharing_store(struct kobject *kobj,
487                                        struct attribute *attr,
488                                        const char *buffer, size_t count)
489 {
490         struct obd_device *obd = container_of(kobj, struct obd_device,
491                                               obd_kset.kobj);
492         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
493         bool val;
494         int rc;
495
496         rc = kstrtobool(buffer, &val);
497         if (rc)
498                 return rc;
499
500         mdt_enable_cos(mdt, val);
501         return count;
502 }
503 LUSTRE_RW_ATTR(commit_on_sharing);
504
505 static ssize_t local_recovery_show(struct kobject *kobj,
506                                       struct attribute *attr, char *buf)
507 {
508         struct obd_device *obd = container_of(kobj, struct obd_device,
509                                               obd_kset.kobj);
510
511         return scnprintf(buf, PAGE_SIZE, "%u\n",
512                          obd2obt(obd)->obt_lut->lut_local_recovery);
513 }
514
515 static ssize_t local_recovery_store(struct kobject *kobj,
516                                        struct attribute *attr,
517                                        const char *buffer, size_t count)
518 {
519         struct obd_device *obd = container_of(kobj, struct obd_device,
520                                               obd_kset.kobj);
521         bool val;
522         int rc;
523
524         rc = kstrtobool(buffer, &val);
525         if (rc)
526                 return rc;
527
528         obd2obt(obd)->obt_lut->lut_local_recovery = !!val;
529         return count;
530 }
531 LUSTRE_RW_ATTR(local_recovery);
532
533 static int mdt_root_squash_seq_show(struct seq_file *m, void *data)
534 {
535         struct obd_device *obd = m->private;
536         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
537         struct root_squash_info *squash = &mdt->mdt_squash;
538
539         seq_printf(m, "%u:%u\n", squash->rsi_uid,
540                    squash->rsi_gid);
541         return 0;
542 }
543
544 static ssize_t
545 mdt_root_squash_seq_write(struct file *file, const char __user *buffer,
546                           size_t count, loff_t *off)
547 {
548         struct seq_file   *m = file->private_data;
549         struct obd_device *obd = m->private;
550         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
551         struct root_squash_info *squash = &mdt->mdt_squash;
552
553         return lprocfs_wr_root_squash(buffer, count, squash,
554                                       mdt_obd_name(mdt));
555 }
556 LPROC_SEQ_FOPS(mdt_root_squash);
557
558 static int mdt_nosquash_nids_seq_show(struct seq_file *m, void *data)
559 {
560         struct obd_device *obd = m->private;
561         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
562         struct root_squash_info *squash = &mdt->mdt_squash;
563         int len = 0;
564
565         spin_lock(&squash->rsi_lock);
566         if (!list_empty(&squash->rsi_nosquash_nids)) {
567                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
568                                         &squash->rsi_nosquash_nids);
569                 m->count += len;
570                 seq_putc(m, '\n');
571         } else
572                 seq_puts(m, "NONE\n");
573         spin_unlock(&squash->rsi_lock);
574
575         return 0;
576 }
577
578 static ssize_t
579 mdt_nosquash_nids_seq_write(struct file *file, const char __user *buffer,
580                             size_t count, loff_t *off)
581 {
582         struct seq_file   *m = file->private_data;
583         struct obd_device *obd = m->private;
584         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
585         struct root_squash_info *squash = &mdt->mdt_squash;
586
587         return lprocfs_wr_nosquash_nids(buffer, count, squash,
588                                         mdt_obd_name(mdt));
589 }
590 LPROC_SEQ_FOPS(mdt_nosquash_nids);
591
592 static ssize_t enable_cap_mask_show(struct kobject *kobj,
593                                     struct attribute *attr, char *buf)
594 {
595         struct obd_device *obd = container_of(kobj, struct obd_device,
596                                               obd_kset.kobj);
597         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
598         u64 cap;
599
600         BUILD_BUG_ON(_KERNEL_CAP_T_SIZE != sizeof(u64));
601
602 #ifdef CAP_FOR_EACH_U32 /* kernels before v6.2-13111-gf122a08b197d */
603         cap = ((u64)mdt->mdt_enable_cap_mask.cap[1] << 32) |
604                mdt->mdt_enable_cap_mask.cap[0];
605 #else
606         cap = mdt->mdt_enable_cap_mask.val;
607 #endif
608         return scnprintf(buf, PAGE_SIZE, "%#0llx\n", cap);
609 }
610
611 static ssize_t enable_cap_mask_store(struct kobject *kobj,
612                                      struct attribute *attr,
613                                      const char *buffer, size_t count)
614 {
615         struct obd_device *obd = container_of(kobj, struct obd_device,
616                                               obd_kset.kobj);
617         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
618         unsigned long long val;
619         int rc;
620
621         rc = kstrtoull(buffer, 0, &val);
622         if (rc)
623                 /* should also accept symbolic names via cfs_str2mask() */
624                 return rc;
625
626 #ifdef CAP_FOR_EACH_U32
627         mdt->mdt_enable_cap_mask.cap[0] = val &
628                 (CAP_FS_MASK_B0 | CAP_TO_MASK(CAP_SYS_RESOURCE) |
629                  CAP_TO_MASK(CAP_LINUX_IMMUTABLE));
630         mdt->mdt_enable_cap_mask.cap[1] = (val >> 32) & CAP_FS_MASK_B1;
631 #else
632         mdt->mdt_enable_cap_mask.val = val &
633                 (CAP_FS_MASK | BIT_ULL(CAP_SYS_RESOURCE) |
634                  BIT_ULL(CAP_LINUX_IMMUTABLE));
635 #endif
636
637         return count;
638 }
639 LUSTRE_RW_ATTR(enable_cap_mask);
640
641 static ssize_t enable_remote_dir_gid_show(struct kobject *kobj,
642                                           struct attribute *attr, char *buf)
643 {
644         struct obd_device *obd = container_of(kobj, struct obd_device,
645                                               obd_kset.kobj);
646         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
647
648         return scnprintf(buf, PAGE_SIZE, "%d\n",
649                          (int)mdt->mdt_enable_remote_dir_gid);
650 }
651
652 static ssize_t enable_remote_dir_gid_store(struct kobject *kobj,
653                                            struct attribute *attr,
654                                            const char *buffer, size_t count)
655 {
656         struct obd_device *obd = container_of(kobj, struct obd_device,
657                                               obd_kset.kobj);
658         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
659         int val;
660         int rc;
661
662         rc = kstrtoint(buffer, 0, &val);
663         if (rc)
664                 return rc;
665
666         mdt->mdt_enable_remote_dir_gid = val;
667         return count;
668 }
669 LUSTRE_RW_ATTR(enable_remote_dir_gid);
670
671 static ssize_t enable_chprojid_gid_show(struct kobject *kobj,
672                                         struct attribute *attr, char *buf)
673 {
674         struct obd_device *obd = container_of(kobj, struct obd_device,
675                                               obd_kset.kobj);
676         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
677
678         return scnprintf(buf, PAGE_SIZE, "%d\n",
679                          (int)mdt->mdt_enable_chprojid_gid);
680 }
681
682 static ssize_t enable_chprojid_gid_store(struct kobject *kobj,
683                                          struct attribute *attr,
684                                          const char *buffer, size_t count)
685 {
686         struct obd_device *obd = container_of(kobj, struct obd_device,
687                                               obd_kset.kobj);
688         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
689         int val;
690         int rc;
691
692         rc = kstrtoint(buffer, 0, &val);
693         if (rc)
694                 return rc;
695
696         mdt->mdt_enable_chprojid_gid = val;
697         return count;
698 }
699 LUSTRE_RW_ATTR(enable_chprojid_gid);
700
701 #define MDT_BOOL_RW_ATTR(name)                                          \
702 static ssize_t name##_show(struct kobject *kobj, struct attribute *attr,\
703                            char *buf)                                   \
704 {                                                                       \
705         struct obd_device *obd = container_of(kobj, struct obd_device,  \
706                                               obd_kset.kobj);           \
707         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);              \
708         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_##name);      \
709 }                                                                       \
710 static ssize_t name##_store(struct kobject *kobj, struct attribute *attr,\
711                             const char *buffer, size_t count)           \
712 {                                                                       \
713         struct obd_device *obd = container_of(kobj, struct obd_device,  \
714                                               obd_kset.kobj);           \
715         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);              \
716         bool val;                                                       \
717         int rc;                                                         \
718         rc = kstrtobool(buffer, &val);                                  \
719         if (rc)                                                         \
720                 return rc;                                              \
721         mdt->mdt_##name = val;                                          \
722         return count;                                                   \
723 }                                                                       \
724 LUSTRE_RW_ATTR(name)
725
726 MDT_BOOL_RW_ATTR(readonly);
727 MDT_BOOL_RW_ATTR(evict_tgt_nids);
728 MDT_BOOL_RW_ATTR(dom_read_open);
729 MDT_BOOL_RW_ATTR(enable_remote_dir);
730 MDT_BOOL_RW_ATTR(enable_remote_rename);
731 MDT_BOOL_RW_ATTR(enable_parallel_rename_dir);
732 MDT_BOOL_RW_ATTR(enable_parallel_rename_file);
733 MDT_BOOL_RW_ATTR(enable_parallel_rename_crossdir);
734 MDT_BOOL_RW_ATTR(enable_striped_dir);
735 MDT_BOOL_RW_ATTR(enable_dir_migration);
736 MDT_BOOL_RW_ATTR(enable_dir_restripe);
737 MDT_BOOL_RW_ATTR(enable_dir_auto_split);
738 MDT_BOOL_RW_ATTR(dir_restripe_nsonly);
739 MDT_BOOL_RW_ATTR(migrate_hsm_allowed);
740 MDT_BOOL_RW_ATTR(enable_strict_som);
741 MDT_BOOL_RW_ATTR(enable_dmv_implicit_inherit);
742 MDT_BOOL_RW_ATTR(enable_dmv_xattr);
743
744 /**
745  * Show if the MDT is in no create mode.
746  *
747  * This means MDT has been adminstratively disabled to prevent it
748  * from creating any new directories on the MDT, though existing files
749  * and directories can still be read, written, and unlinked.
750  *
751  * \retval              number of bytes written
752  */
753 static ssize_t no_create_show(struct kobject *kobj, struct attribute *attr,
754                               char *buf)
755 {
756         struct obd_device *obd = container_of(kobj, struct obd_device,
757                                               obd_kset.kobj);
758         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
759
760         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_lut.lut_no_create);
761 }
762
763 /**
764  * Set MDT to no create mode.
765  *
766  * This is used to interface to userspace administrative tools to
767  * disable new directory creation on the MDT.
768  *
769  * \param[in] count     \a buffer length
770  *
771  * \retval              \a count on success
772  * \retval              negative number on error
773  */
774 static ssize_t no_create_store(struct kobject *kobj, struct attribute *attr,
775                                const char *buffer, size_t count)
776 {
777         struct obd_device *obd = container_of(kobj, struct obd_device,
778                                               obd_kset.kobj);
779         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
780         bool val;
781         int rc;
782
783         rc = kstrtobool(buffer, &val);
784         if (rc)
785                 return rc;
786
787         mdt->mdt_lut.lut_no_create = val;
788
789         return count;
790 }
791 LUSTRE_RW_ATTR(no_create);
792
793 /**
794  * Show MDT async commit count.
795  *
796  * @m           seq_file handle
797  * @data        unused for single entry
798  *
799  * Return:      0 on success
800  *              negative value on error
801  */
802 static ssize_t async_commit_count_show(struct kobject *kobj,
803                                        struct attribute *attr, char *buf)
804 {
805         struct obd_device *obd = container_of(kobj, struct obd_device,
806                                               obd_kset.kobj);
807         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
808
809         return scnprintf(buf, PAGE_SIZE, "%d\n",
810                          atomic_read(&mdt->mdt_async_commit_count));
811 }
812
813 static ssize_t async_commit_count_store(struct kobject *kobj,
814                                         struct attribute *attr,
815                                         const char *buffer, size_t count)
816 {
817         struct obd_device *obd = container_of(kobj, struct obd_device,
818                                               obd_kset.kobj);
819         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
820         int val;
821         int rc;
822
823         rc = kstrtoint(buffer, 10, &val);
824         if (rc)
825                 return rc;
826
827         atomic_set(&mdt->mdt_async_commit_count, val);
828
829         return count;
830 }
831 LUSTRE_RW_ATTR(async_commit_count);
832
833 /**
834  * Show MDT sync count.
835  *
836  * \param[in] m         seq_file handle
837  * \param[in] data      unused for single entry
838  *
839  * \retval              0 on success
840  * \retval              negative value on error
841  */
842 static ssize_t sync_count_show(struct kobject *kobj, struct attribute *attr,
843                                char *buf)
844 {
845         struct obd_device *obd = container_of(kobj, struct obd_device,
846                                               obd_kset.kobj);
847         struct lu_target *tgt = obd2obt(obd)->obt_lut;
848
849         return scnprintf(buf, PAGE_SIZE, "%d\n",
850                          atomic_read(&tgt->lut_sync_count));
851 }
852
853 static ssize_t sync_count_store(struct kobject *kobj, struct attribute *attr,
854                                 const char *buffer, size_t count)
855 {
856         struct obd_device *obd = container_of(kobj, struct obd_device,
857                                               obd_kset.kobj);
858         struct lu_target *tgt = obd2obt(obd)->obt_lut;
859         int val;
860         int rc;
861
862         rc = kstrtoint(buffer, 0, &val);
863         if (rc)
864                 return rc;
865
866         atomic_set(&tgt->lut_sync_count, val);
867
868         return count;
869 }
870 LUSTRE_RW_ATTR(sync_count);
871
872 static const char *dom_open_lock_modes[NUM_DOM_LOCK_ON_OPEN_MODES] = {
873         [NO_DOM_LOCK_ON_OPEN] = "never",
874         [TRYLOCK_DOM_ON_OPEN] = "trylock",
875         [ALWAYS_DOM_LOCK_ON_OPEN] = "always",
876 };
877
878 /* This must be longer than the longest string above */
879 #define DOM_LOCK_MODES_MAXLEN 16
880
881 /**
882  * Show MDT policy for data prefetch on open for DoM files..
883  *
884  * \param[in] m         seq_file handle
885  * \param[in] data      unused
886  *
887  * \retval              0 on success
888  * \retval              negative value on error
889  */
890 static ssize_t dom_lock_show(struct kobject *kobj, struct attribute *attr,
891                              char *buf)
892 {
893         struct obd_device *obd = container_of(kobj, struct obd_device,
894                                               obd_kset.kobj);
895         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
896
897         return scnprintf(buf, PAGE_SIZE, "%s\n",
898                          dom_open_lock_modes[mdt->mdt_opts.mo_dom_lock]);
899 }
900
901 /**
902  * Change MDT policy for data prefetch on open for DoM files.
903  *
904  * This variable defines how DOM lock is taken at open enqueue.
905  * There are three possible modes:
906  * 1) never - never take DoM lock on open. DoM lock will be taken as separate
907  *    IO lock with own enqueue.
908  * 2) trylock - DoM lock will be taken only if non-blocked.
909  * 3) always - DoM lock will be taken always even if it is blocking lock.
910  *
911  * If dom_read_open is enabled too then DoM lock is taken in PR mode and
912  * is paired with LAYOUT lock when possible.
913  *
914  * \param[in] file      proc file
915  * \param[in] buffer    string which represents policy
916  * \param[in] count     \a buffer length
917  * \param[in] off       unused for single entry
918  *
919  * \retval              \a count on success
920  * \retval              negative number on error
921  */
922 static ssize_t dom_lock_store(struct kobject *kobj, struct attribute *attr,
923                               const char *buffer, size_t count)
924 {
925         struct obd_device *obd = container_of(kobj, struct obd_device,
926                                               obd_kset.kobj);
927         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
928         int val = -1;
929         int i, rc;
930
931         if (count == 0 || count >= DOM_LOCK_MODES_MAXLEN)
932                 return -EINVAL;
933
934         for (i = 0 ; i < NUM_DOM_LOCK_ON_OPEN_MODES; i++) {
935                 /* buffer might have '\n' but using strlen() avoids it */
936                 if (strncmp(buffer, dom_open_lock_modes[i],
937                             strlen(dom_open_lock_modes[i])) == 0) {
938                         val = i;
939                         break;
940                 }
941         }
942
943         /* Legacy numeric codes */
944         if (val == -1) {
945                 rc = kstrtoint(buffer, 0, &val);
946                 if (rc)
947                         return rc;
948         }
949
950         if (val == ALWAYS_DOM_LOCK_ON_OPEN)
951                 val = TRYLOCK_DOM_ON_OPEN;
952
953         if (val < 0 || val >= NUM_DOM_LOCK_ON_OPEN_MODES)
954                 return -EINVAL;
955
956         mdt->mdt_opts.mo_dom_lock = val;
957         return count;
958 }
959 LUSTRE_RW_ATTR(dom_lock);
960
961 static ssize_t dir_split_count_show(struct kobject *kobj,
962                                      struct attribute *attr,
963                                      char *buf)
964 {
965         struct obd_device *obd = container_of(kobj, struct obd_device,
966                                               obd_kset.kobj);
967         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
968
969         return scnprintf(buf, PAGE_SIZE, "%llu\n",
970                          mdt->mdt_restriper.mdr_dir_split_count);
971 }
972
973 static ssize_t dir_split_count_store(struct kobject *kobj,
974                                       struct attribute *attr,
975                                       const char *buffer, size_t count)
976 {
977         struct obd_device *obd = container_of(kobj, struct obd_device,
978                                               obd_kset.kobj);
979         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
980         s64 val;
981         int rc;
982
983         rc = sysfs_memparse(buffer, count, &val, "B");
984         if (rc < 0)
985                 return rc;
986
987         if (val < 0)
988                 return -ERANGE;
989
990         mdt->mdt_restriper.mdr_dir_split_count = val;
991
992         return count;
993 }
994 LUSTRE_RW_ATTR(dir_split_count);
995
996 static ssize_t dir_split_delta_show(struct kobject *kobj,
997                                     struct attribute *attr,
998                                     char *buf)
999 {
1000         struct obd_device *obd = container_of(kobj, struct obd_device,
1001                                               obd_kset.kobj);
1002         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1003
1004         return scnprintf(buf, PAGE_SIZE, "%u\n",
1005                          mdt->mdt_restriper.mdr_dir_split_delta);
1006 }
1007
1008 static ssize_t dir_split_delta_store(struct kobject *kobj,
1009                                      struct attribute *attr,
1010                                      const char *buffer, size_t count)
1011 {
1012         struct obd_device *obd = container_of(kobj, struct obd_device,
1013                                               obd_kset.kobj);
1014         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1015         u32 val;
1016         int rc;
1017
1018         rc = kstrtouint(buffer, 0, &val);
1019         if (rc)
1020                 return rc;
1021
1022         mdt->mdt_restriper.mdr_dir_split_delta = val;
1023
1024         return count;
1025 }
1026 LUSTRE_RW_ATTR(dir_split_delta);
1027
1028 static ssize_t enable_remote_subdir_mount_show(struct kobject *kobj,
1029                                                struct attribute *attr,
1030                                                char *buf)
1031 {
1032         return scnprintf(buf, PAGE_SIZE, "%u\n", 1);
1033 }
1034
1035 static ssize_t enable_remote_subdir_mount_store(struct kobject *kobj,
1036                                                 struct attribute *attr,
1037                                                 const char *buffer,
1038                                                 size_t count)
1039 {
1040         LCONSOLE_WARN("enable_remote_subdir_mount is deprecated, it's always enabled.\n");
1041         return count;
1042 }
1043 LUSTRE_RW_ATTR(enable_remote_subdir_mount);
1044
1045 /**
1046  * Show if the OFD enforces T10PI checksum.
1047  *
1048  * \param[in] m         seq_file handle
1049  * \param[in] data      unused for single entry
1050  *
1051  * \retval              0 on success
1052  * \retval              negative value on error
1053  */
1054 static ssize_t checksum_t10pi_enforce_show(struct kobject *kobj,
1055                                            struct attribute *attr,
1056                                            char *buf)
1057 {
1058         struct obd_device *obd = container_of(kobj, struct obd_device,
1059                                               obd_kset.kobj);
1060         struct lu_target *lut = obd2obt(obd)->obt_lut;
1061
1062         return scnprintf(buf, PAGE_SIZE, "%u\n", lut->lut_cksum_t10pi_enforce);
1063 }
1064
1065 /**
1066  * Force specific T10PI checksum modes to be enabled
1067  *
1068  * If T10PI *is* supported in hardware, allow only the supported T10PI type
1069  * to be used. If T10PI is *not* supported by the OSD, setting the enforce
1070  * parameter forces all T10PI types to be enabled (even if slower) for
1071  * testing.
1072  *
1073  * The final determination of which algorithm to be used depends whether
1074  * the client supports T10PI or not, and is handled at client connect time.
1075  *
1076  * \param[in] file      proc file
1077  * \param[in] buffer    string which represents mode
1078  *                      1: set T10PI checksums enforced
1079  *                      0: unset T10PI checksums enforced
1080  * \param[in] count     \a buffer length
1081  * \param[in] off       unused for single entry
1082  *
1083  * \retval              \a count on success
1084  * \retval              negative number on error
1085  */
1086 static ssize_t checksum_t10pi_enforce_store(struct kobject *kobj,
1087                                             struct attribute *attr,
1088                                             const char *buffer, size_t count)
1089 {
1090         struct obd_device *obd = container_of(kobj, struct obd_device,
1091                                               obd_kset.kobj);
1092         struct lu_target *lut = obd2obt(obd)->obt_lut;
1093         bool enforce;
1094         int rc;
1095
1096         rc = kstrtobool(buffer, &enforce);
1097         if (rc)
1098                 return rc;
1099
1100         spin_lock(&lut->lut_flags_lock);
1101         lut->lut_cksum_t10pi_enforce = enforce;
1102         spin_unlock(&lut->lut_flags_lock);
1103         return count;
1104 }
1105 LUSTRE_RW_ATTR(checksum_t10pi_enforce);
1106
1107 /**
1108  * Show MDT Maximum modify RPCs in flight.
1109  *
1110  * @m           seq_file handle
1111  * @data        unused for single entry
1112  *
1113  * Return:      value on success or negative number on error
1114  */
1115 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
1116                                        struct attribute *attr, char *buf)
1117 {
1118         struct obd_device *obd = container_of(kobj, struct obd_device,
1119                                               obd_kset.kobj);
1120         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1121
1122         return scnprintf(buf, PAGE_SIZE, "%u\n",
1123                          mdt->mdt_max_mod_rpcs_in_flight);
1124 }
1125
1126 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
1127                                         struct attribute *attr,
1128                                         const char *buffer, size_t count)
1129 {
1130         struct obd_device *obd = container_of(kobj, struct obd_device,
1131                                               obd_kset.kobj);
1132         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1133         unsigned int val;
1134         int rc;
1135
1136         rc = kstrtouint(buffer, 0, &val);
1137         if (rc)
1138                 return rc;
1139
1140         if (val < 1 || val > OBD_MAX_RIF_MAX)
1141                 return -ERANGE;
1142
1143         if (mdt_max_mod_rpcs_changed(mdt)) {
1144                 CWARN("%s: deprecated 'max_mod_rpcs_in_flight' module parameter has also been modified\n",
1145                                 obd->obd_name);
1146                 max_mod_rpcs_per_client = val;
1147         }
1148         mdt->mdt_max_mod_rpcs_in_flight = val;
1149
1150         return count;
1151 }
1152 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
1153
1154 /*
1155  * mdt_checksum_type(server) proc handling
1156  */
1157 DECLARE_CKSUM_NAME;
1158
1159 static int mdt_checksum_type_seq_show(struct seq_file *m, void *data)
1160 {
1161         struct obd_device *obd = m->private;
1162         struct lu_target *lut;
1163         enum cksum_types pref;
1164         int i;
1165
1166         if (!obd)
1167                 return 0;
1168
1169         lut = obd2obt(obd)->obt_lut;
1170         /* select fastest checksum type on the server */
1171         pref = obd_cksum_type_select(obd->obd_name,
1172                                      lut->lut_cksum_types_supported,
1173                                      lut->lut_dt_conf.ddp_t10_cksum_type);
1174
1175         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
1176                 if ((BIT(i) & lut->lut_cksum_types_supported) == 0)
1177                         continue;
1178
1179                 if (pref == BIT(i))
1180                         seq_printf(m, "[%s] ", cksum_name[i]);
1181                 else
1182                         seq_printf(m, "%s ", cksum_name[i]);
1183         }
1184         seq_puts(m, "\n");
1185
1186         return 0;
1187 }
1188
1189 ssize_t job_xattr_show(struct kobject *kobj, struct attribute *attr, char *buf)
1190 {
1191         struct obd_device *obd = container_of(kobj, struct obd_device,
1192                                               obd_kset.kobj);
1193         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1194
1195         if (mdt->mdt_job_xattr[0] == '\0')
1196                 return scnprintf(buf, PAGE_SIZE, "NONE\n");
1197
1198         return scnprintf(buf, PAGE_SIZE, "%s\n", mdt->mdt_job_xattr);
1199 }
1200
1201 /**
1202  * Read in a name for the jobid xattr and validate it.
1203  * The only valid names are "trusted.job" or "user.*" where the name portion
1204  * is <= 7 bytes in the user namespace. Only alphanumeric characters are
1205  * allowed, aside from the namespace separator '.'.
1206  *
1207  * "none" is a valid value to turn this feature off.
1208  *
1209  * @return -EINVAL if the name is invalid, else count
1210  */
1211 ssize_t job_xattr_store(struct kobject *kobj, struct attribute *attr,
1212                         const char *buffer, size_t count)
1213 {
1214         struct obd_device *obd = container_of(kobj, struct obd_device,
1215                                               obd_kset.kobj);
1216         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1217         char name[XATTR_JOB_MAX_LEN] = { 0 };
1218         char *p;
1219
1220
1221         /* writing "none" turns this off by leaving the name empty */
1222         if (!strncmp(buffer, "none", 4) ||
1223             !strncmp(buffer, "NONE", 4)) {
1224                 memset(mdt->mdt_job_xattr, 0, sizeof(mdt->mdt_job_xattr));
1225                 return count;
1226         }
1227
1228         /* account for stripping \n before rejecting name for being too long */
1229         if (count > XATTR_JOB_MAX_LEN - 1 &&
1230             buffer[XATTR_JOB_MAX_LEN - 1] != '\n')
1231                 return -EINVAL;
1232
1233         strncpy(name, buffer, XATTR_JOB_MAX_LEN - 1);
1234
1235         /* reject if not in namespace.name format */
1236         p = strchr(name, '.');
1237         if (p == NULL)
1238                 return -EINVAL;
1239
1240         p++;
1241         for (; *p != '\0'; p++) {
1242                 /*
1243                  * if there are any non-alphanumeric characters, the name is
1244                  * invalid unless it's a newline, in which case overwrite it
1245                  * with '\0' and that's the end of the name.
1246                  */
1247                 if (!isalnum(*p)) {
1248                         if (*p != '\n')
1249                                 return -EINVAL;
1250                         *p = '\0';
1251                 }
1252         }
1253
1254         /* trusted.job is only valid name in trusted namespace */
1255         if (!strncmp(name, "trusted.job", 12)) {
1256                 strncpy(mdt->mdt_job_xattr, name, XATTR_JOB_MAX_LEN);
1257                 return count;
1258         }
1259
1260         /* only other valid namespace is user */
1261         if (strncmp(name, XATTR_USER_PREFIX, sizeof(XATTR_USER_PREFIX) - 1))
1262                 return -EINVAL;
1263
1264         /* ensure that a name was specified */
1265         if (name[sizeof(XATTR_USER_PREFIX) - 1] == '\0')
1266                 return -EINVAL;
1267
1268         strncpy(mdt->mdt_job_xattr, name, XATTR_JOB_MAX_LEN);
1269
1270         return count;
1271 }
1272
1273 LPROC_SEQ_FOPS_RO(mdt_checksum_type);
1274
1275 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
1276 LPROC_SEQ_FOPS_WR_ONLY(mdt, mds_evict_client);
1277 LPROC_SEQ_FOPS_RW_TYPE(mdt, checksum_dump);
1278 LUSTRE_RW_ATTR(job_cleanup_interval);
1279 LUSTRE_RW_ATTR(job_xattr);
1280 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
1281 LUSTRE_RW_ATTR(hsm_control);
1282
1283 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
1284 LUSTRE_RW_ATTR(recovery_time_hard);
1285 LUSTRE_RW_ATTR(recovery_time_soft);
1286 LUSTRE_RW_ATTR(ir_factor);
1287
1288 LUSTRE_RO_ATTR(tot_dirty);
1289 LUSTRE_RO_ATTR(tot_granted);
1290 LUSTRE_RO_ATTR(tot_pending);
1291 LUSTRE_RW_ATTR(grant_compat_disable);
1292 LUSTRE_RO_ATTR(instance);
1293
1294 LUSTRE_RO_ATTR(num_exports);
1295 LUSTRE_RW_ATTR(grant_check_threshold);
1296 LUSTRE_RO_ATTR(eviction_count);
1297
1298 /* per-device at parameters */
1299 LUSTRE_OBD_UINT_PARAM_ATTR(at_min);
1300 LUSTRE_OBD_UINT_PARAM_ATTR(at_max);
1301 LUSTRE_OBD_UINT_PARAM_ATTR(at_history);
1302
1303 static struct attribute *mdt_attrs[] = {
1304         &lustre_attr_tot_dirty.attr,
1305         &lustre_attr_tot_granted.attr,
1306         &lustre_attr_tot_pending.attr,
1307         &lustre_attr_grant_compat_disable.attr,
1308         &lustre_attr_instance.attr,
1309         &lustre_attr_recovery_time_hard.attr,
1310         &lustre_attr_recovery_time_soft.attr,
1311         &lustre_attr_ir_factor.attr,
1312         &lustre_attr_num_exports.attr,
1313         &lustre_attr_grant_check_threshold.attr,
1314         &lustre_attr_eviction_count.attr,
1315         &lustre_attr_identity_expire.attr,
1316         &lustre_attr_identity_acquire_expire.attr,
1317         &lustre_attr_identity_upcall.attr,
1318         &lustre_attr_identity_flush.attr,
1319         &lustre_attr_evict_tgt_nids.attr,
1320         &lustre_attr_enable_cap_mask.attr,
1321         &lustre_attr_enable_chprojid_gid.attr,
1322         &lustre_attr_enable_dir_migration.attr,
1323         &lustre_attr_enable_dir_restripe.attr,
1324         &lustre_attr_enable_dir_auto_split.attr,
1325         &lustre_attr_enable_parallel_rename_dir.attr,
1326         &lustre_attr_enable_parallel_rename_file.attr,
1327         &lustre_attr_enable_parallel_rename_crossdir.attr,
1328         &lustre_attr_enable_remote_dir.attr,
1329         &lustre_attr_enable_remote_dir_gid.attr,
1330         &lustre_attr_enable_remote_rename.attr,
1331         &lustre_attr_enable_striped_dir.attr,
1332         &lustre_attr_commit_on_sharing.attr,
1333         &lustre_attr_local_recovery.attr,
1334         &lustre_attr_no_create.attr,
1335         &lustre_attr_async_commit_count.attr,
1336         &lustre_attr_sync_count.attr,
1337         &lustre_attr_dom_lock.attr,
1338         &lustre_attr_dom_read_open.attr,
1339         &lustre_attr_enable_strict_som.attr,
1340         &lustre_attr_migrate_hsm_allowed.attr,
1341         &lustre_attr_hsm_control.attr,
1342         &lustre_attr_job_cleanup_interval.attr,
1343         &lustre_attr_job_xattr.attr,
1344         &lustre_attr_readonly.attr,
1345         &lustre_attr_dir_split_count.attr,
1346         &lustre_attr_dir_split_delta.attr,
1347         &lustre_attr_dir_restripe_nsonly.attr,
1348         &lustre_attr_checksum_t10pi_enforce.attr,
1349         &lustre_attr_enable_remote_subdir_mount.attr,
1350         &lustre_attr_max_mod_rpcs_in_flight.attr,
1351         &lustre_attr_enable_dmv_implicit_inherit.attr,
1352         &lustre_attr_at_min.attr,
1353         &lustre_attr_at_max.attr,
1354         &lustre_attr_at_history.attr,
1355         &lustre_attr_enable_dmv_xattr.attr,
1356         NULL,
1357 };
1358
1359 KOBJ_ATTRIBUTE_GROUPS(mdt); /* creates mdt_groups from mdt_attrs */
1360
1361 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
1362         { .name =       "recovery_status",
1363           .fops =       &mdt_recovery_status_fops               },
1364         { .name =       "identity_info",
1365           .fops =       &mdt_identity_info_fops                 },
1366         { .name =       "site_stats",
1367           .fops =       &mdt_site_stats_fops                    },
1368         { .name =       "evict_client",
1369           .fops =       &mdt_mds_evict_client_fops              },
1370         { .name =       "checksum_dump",
1371           .fops =       &mdt_checksum_dump_fops                 },
1372         { .name =       "hash_stats",
1373           .fops =       &mdt_hash_fops                          },
1374         { .name =       "root_squash",
1375           .fops =       &mdt_root_squash_fops                   },
1376         { .name =       "nosquash_nids",
1377           .fops =       &mdt_nosquash_nids_fops                 },
1378         { .name =       "checksum_type",
1379           .fops =       &mdt_checksum_type_fops         },
1380         { NULL }
1381 };
1382
1383 LDEBUGFS_SEQ_FOPS_RO_TYPE(mdt, recovery_stale_clients);
1384
1385 static struct ldebugfs_vars ldebugfs_mdt_obd_vars[] = {
1386         { .name =       "recovery_stale_clients",
1387           .fops =       &mdt_recovery_stale_clients_fops        },
1388         { NULL }
1389 };
1390
1391 LDEBUGFS_SEQ_FOPS_RO_TYPE(mdt, srpc_serverctx);
1392
1393 static struct ldebugfs_vars ldebugfs_mdt_gss_vars[] = {
1394         { .name =       "srpc_serverctx",
1395           .fops =       &mdt_srpc_serverctx_fops        },
1396         { NULL }
1397 };
1398
1399 static int
1400 lprocfs_mdt_print_open_files(struct obd_export *exp, void *v)
1401 {
1402         struct seq_file         *seq = v;
1403
1404         if (exp->exp_lock_hash != NULL) {
1405                 struct mdt_export_data  *med = &exp->exp_mdt_data;
1406                 struct mdt_file_data    *mfd;
1407
1408                 spin_lock(&med->med_open_lock);
1409                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1410                         seq_printf(seq, DFID"\n",
1411                                    PFID(mdt_object_fid(mfd->mfd_object)));
1412                 }
1413                 spin_unlock(&med->med_open_lock);
1414         }
1415
1416         return 0;
1417 }
1418
1419 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
1420 {
1421         struct nid_stat *stats = seq->private;
1422
1423         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
1424                                        lprocfs_mdt_print_open_files, seq);
1425 }
1426
1427 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
1428 {
1429         struct seq_file         *seq;
1430         int                     rc;
1431
1432         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
1433         if (rc != 0)
1434                 return rc;
1435
1436         seq = file->private_data;
1437         seq->private = pde_data(inode);
1438
1439         return 0;
1440 }
1441
1442 void mdt_counter_incr(struct ptlrpc_request *req, int opcode, long amount)
1443 {
1444         struct obd_export *exp = req->rq_export;
1445
1446         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
1447                 lprocfs_counter_add(exp->exp_obd->obd_md_stats,
1448                                     opcode + LPROC_MD_LAST_OPC, amount);
1449         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1450                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats, opcode,
1451                                     amount);
1452         if (exp->exp_obd && obd2obt(exp->exp_obd)->obt_jobstats.ojs_hash &&
1453             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1454                 lprocfs_job_stats_log(exp->exp_obd,
1455                                       lustre_msg_get_jobid(req->rq_reqmsg),
1456                                       opcode, amount);
1457 }
1458
1459 static const char * const mdt_stats[] = {
1460         [LPROC_MDT_OPEN]                = "open",
1461         [LPROC_MDT_CLOSE]               = "close",
1462         [LPROC_MDT_MKNOD]               = "mknod",
1463         [LPROC_MDT_LINK]                = "link",
1464         [LPROC_MDT_UNLINK]              = "unlink",
1465         [LPROC_MDT_MKDIR]               = "mkdir",
1466         [LPROC_MDT_RMDIR]               = "rmdir",
1467         [LPROC_MDT_RENAME]              = "rename",
1468         [LPROC_MDT_GETATTR]             = "getattr",
1469         [LPROC_MDT_SETATTR]             = "setattr",
1470         [LPROC_MDT_GETXATTR]            = "getxattr",
1471         [LPROC_MDT_SETXATTR]            = "setxattr",
1472         [LPROC_MDT_STATFS]              = "statfs",
1473         [LPROC_MDT_SYNC]                = "sync",
1474         [LPROC_MDT_RENAME_SAMEDIR]      = "samedir_rename",
1475         [LPROC_MDT_RENAME_PAR_FILE]     = "parallel_rename_file",
1476         [LPROC_MDT_RENAME_PAR_DIR]      = "parallel_rename_dir",
1477         [LPROC_MDT_RENAME_CROSSDIR]     = "crossdir_rename",
1478         [LPROC_MDT_IO_READ_BYTES]       = "read_bytes",
1479         [LPROC_MDT_IO_WRITE_BYTES]      = "write_bytes",
1480         [LPROC_MDT_IO_READ]             = "read",
1481         [LPROC_MDT_IO_WRITE]            = "write",
1482         [LPROC_MDT_IO_PUNCH]            = "punch",
1483         [LPROC_MDT_MIGRATE]             = "migrate",
1484         [LPROC_MDT_FALLOCATE]           = "fallocate",
1485 };
1486
1487 void mdt_stats_counter_init(struct lprocfs_stats *stats, unsigned int offset,
1488                             enum lprocfs_counter_config cntr_umask)
1489 {
1490         int array_size = ARRAY_SIZE(mdt_stats);
1491         int oidx; /* obd_md_stats index */
1492         int midx; /* mdt_stats index */
1493
1494         LASSERT(stats && stats->ls_num >= offset + array_size);
1495
1496         for (midx = 0; midx < array_size; midx++) {
1497                 oidx = midx + offset;
1498                 if (midx == LPROC_MDT_IO_READ_BYTES ||
1499                     midx == LPROC_MDT_IO_WRITE_BYTES)
1500                         lprocfs_counter_init(stats, oidx,
1501                                              LPROCFS_TYPE_BYTES_FULL_HISTOGRAM &
1502                                              (~cntr_umask),
1503                                              mdt_stats[midx]);
1504                 else
1505                         lprocfs_counter_init(stats, oidx,
1506                                              LPROCFS_TYPE_LATENCY &
1507                                              (~cntr_umask),
1508                                              mdt_stats[midx]);
1509         }
1510 }
1511
1512 int mdt_tunables_init(struct mdt_device *mdt, const char *name)
1513 {
1514         struct obd_device *obd = mdt2obd_dev(mdt);
1515         int rc;
1516
1517         ENTRY;
1518         LASSERT(name != NULL);
1519
1520         obd->obd_ktype.default_groups = KOBJ_ATTR_GROUPS(mdt);
1521         obd->obd_vars = lprocfs_mdt_obd_vars;
1522         rc = lprocfs_obd_setup(obd, true);
1523         if (rc) {
1524                 CERROR("%s: cannot create proc entries: rc = %d\n",
1525                        mdt_obd_name(mdt), rc);
1526                 return rc;
1527         }
1528         ldebugfs_add_vars(obd->obd_debugfs_entry, ldebugfs_mdt_obd_vars, obd);
1529
1530         rc = tgt_tunables_init(&mdt->mdt_lut);
1531         if (rc) {
1532                 CERROR("%s: failed to init target tunables: rc = %d\n",
1533                        mdt_obd_name(mdt), rc);
1534                 return rc;
1535         }
1536
1537         rc = hsm_cdt_tunables_init(mdt);
1538         if (rc) {
1539                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
1540                        mdt_obd_name(mdt), rc);
1541                 return rc;
1542         }
1543
1544         obd->obd_debugfs_gss_dir = debugfs_create_dir("gss",
1545                                                       obd->obd_debugfs_entry);
1546         if (obd->obd_debugfs_gss_dir)
1547                 ldebugfs_add_vars(obd->obd_debugfs_gss_dir,
1548                                   ldebugfs_mdt_gss_vars, obd);
1549
1550         obd->obd_proc_exports_entry = proc_mkdir("exports",
1551                                                  obd->obd_proc_entry);
1552         if (obd->obd_proc_exports_entry)
1553                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
1554                                    obd, &mdt_nid_stats_clear_fops);
1555
1556         rc = lprocfs_alloc_md_stats(obd, ARRAY_SIZE(mdt_stats));
1557         if (rc)
1558                 return rc;
1559
1560         /* add additional MDT md_stats after the default ones */
1561         mdt_stats_counter_init(obd->obd_md_stats, LPROC_MD_LAST_OPC,
1562                                LPROCFS_CNTR_HISTOGRAM);
1563         rc = lprocfs_job_stats_init(obd, ARRAY_SIZE(mdt_stats),
1564                                     mdt_stats_counter_init);
1565
1566         rc = lproc_mdt_attach_rename_seqstat(mdt);
1567         if (rc)
1568                 CERROR("%s: MDT can not create rename stats rc = %d\n",
1569                        mdt_obd_name(mdt), rc);
1570
1571         RETURN(rc);
1572 }
1573
1574 void mdt_tunables_fini(struct mdt_device *mdt)
1575 {
1576         struct obd_device *obd = mdt2obd_dev(mdt);
1577
1578         if (obd->obd_proc_exports_entry != NULL) {
1579                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
1580                 obd->obd_proc_exports_entry = NULL;
1581         }
1582
1583         lprocfs_free_per_client_stats(obd);
1584         /* hsm_cdt_tunables is disabled earlier than this to avoid
1585          * coordinator restart.
1586          */
1587         hsm_cdt_tunables_fini(mdt);
1588         tgt_tunables_fini(&mdt->mdt_lut);
1589         lprocfs_obd_cleanup(obd);
1590         lprocfs_free_md_stats(obd);
1591         lprocfs_free_obd_stats(obd);
1592         lprocfs_job_stats_fini(obd);
1593 }