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