Whamcloud - gitweb
LU-13791 mdt: parameter to tune capabilities
[fs/lustre-release.git] / lustre / mdt / mdt_lproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mdt/mdt_lproc.c
32  *
33  * Author: Lai Siyao <lsy@clusterfs.com>
34  * Author: Fan Yong <fanyong@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include <linux/version.h>
40 #include <asm/statfs.h>
41
42 #include <linux/module.h>
43 #include <uapi/linux/lnet/nidstr.h>
44 /* LUSTRE_VERSION_CODE */
45 #include <uapi/linux/lustre/lustre_ver.h>
46 /*
47  * struct OBD_{ALLOC,FREE}*()
48  * MDT_FAIL_CHECK
49  */
50 #include <obd_support.h>
51 /* struct obd_export */
52 #include <lustre_export.h>
53 /* struct obd_device */
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_mds.h>
57 #include <lprocfs_status.h>
58 #include "mdt_internal.h"
59 #include <obd_cksum.h>
60
61 /**
62  * The rename stats output would be YAML formats, like
63  * rename_stats:
64  * - snapshot_time: 1234567890.123456789
65  * - start_time:    1234567880.987654321
66  * - elapsed_time:  9.135802468
67  * - same_dir:
68  *     4kB: { samples: 1230, pct: 33, cum_pct: 45 }
69  *     8kB: { samples: 1242, pct: 33, cum_pct: 78 }
70  *     16kB: { samples: 132, pct: 3, cum_pct: 81 }
71  * - crossdir_src:
72  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
73  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
74  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
75  * - crossdir_tgt:
76  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
77  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
78  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
79  **/
80
81 static void display_rename_stats(struct seq_file *seq, char *name,
82                                  struct obd_histogram *rs_hist)
83 {
84         unsigned long tot, t, cum = 0;
85         int i;
86
87         tot = lprocfs_oh_sum(rs_hist);
88         if (tot > 0)
89                 seq_printf(seq, "- %s:\n", name);
90
91         for (i = 0; i < OBD_HIST_MAX; i++) {
92                 t = rs_hist->oh_buckets[i];
93                 cum += t;
94                 if (cum == 0)
95                         continue;
96
97                 if (i < 10)
98                         seq_printf(seq, "%6s%d%s", " ", 1 << i, "bytes:");
99                 else if (i < 20)
100                         seq_printf(seq, "%6s%d%s", " ", 1 << (i - 10), "KB:");
101                 else
102                         seq_printf(seq, "%6s%d%s", " ", 1 << (i - 20), "MB:");
103
104                 seq_printf(seq, " { sample: %3lu, pct: %3u, cum_pct: %3u }\n",
105                            t, pct(t, tot), pct(cum, tot));
106
107                 if (cum == tot)
108                         break;
109         }
110 }
111
112 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
113 {
114         struct mdt_device *mdt = seq->private;
115         struct rename_stats *rename_stats = &mdt->mdt_rename_stats;
116
117         /* this sampling races with updates */
118         seq_puts(seq, "rename_stats:\n");
119         lprocfs_stats_header(seq, ktime_get_real(), rename_stats->rs_init, 15,
120                              ":", false, "- ");
121
122         display_rename_stats(seq, "same_dir",
123                              &rename_stats->rs_hist[RENAME_SAMEDIR_SIZE]);
124         display_rename_stats(seq, "crossdir_src",
125                              &rename_stats->rs_hist[RENAME_CROSSDIR_SRC_SIZE]);
126         display_rename_stats(seq, "crossdir_tgt",
127                              &rename_stats->rs_hist[RENAME_CROSSDIR_TGT_SIZE]);
128
129         return 0;
130 }
131
132 static ssize_t
133 mdt_rename_stats_seq_write(struct file *file, const char __user *buf,
134                            size_t len, loff_t *off)
135 {
136         struct seq_file *seq = file->private_data;
137         struct mdt_device *mdt = seq->private;
138         int i;
139
140         for (i = 0; i < RENAME_LAST; i++)
141                 lprocfs_oh_clear(&mdt->mdt_rename_stats.rs_hist[i]);
142         mdt->mdt_rename_stats.rs_init = ktime_get_real();
143
144         return len;
145 }
146 LPROC_SEQ_FOPS(mdt_rename_stats);
147
148 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
149 {
150         int i;
151
152         for (i = 0; i < RENAME_LAST; i++)
153                 spin_lock_init(&mdt->mdt_rename_stats.rs_hist[i].oh_lock);
154         mdt->mdt_rename_stats.rs_init = ktime_get_real();
155
156         return lprocfs_obd_seq_create(mdt2obd_dev(mdt), "rename_stats", 0644,
157                                       &mdt_rename_stats_fops, mdt);
158 }
159
160 void mdt_rename_counter_tally(struct mdt_thread_info *info,
161                               struct mdt_device *mdt,
162                               struct ptlrpc_request *req,
163                               struct mdt_object *src, struct mdt_object *tgt,
164                               enum mdt_stat_idx msi, s64 ktime_delta)
165 {
166         struct md_attr *ma = &info->mti_attr;
167         struct rename_stats *rstats = &mdt->mdt_rename_stats;
168         int rc;
169
170         mdt_counter_incr(req, LPROC_MDT_RENAME, ktime_delta);
171
172         ma->ma_need = MA_INODE;
173         ma->ma_valid = 0;
174         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
175         if (rc) {
176                 CERROR("%s: "DFID" attr_get, rc = %d\n",
177                        mdt_obd_name(mdt), PFID(mdt_object_fid(src)), rc);
178                 return;
179         }
180
181         if (src == tgt) {
182                 mdt_counter_incr(req, LPROC_MDT_RENAME_SAMEDIR, ktime_delta);
183                 if (msi) /* parallel rename type */
184                         mdt_counter_incr(req, msi, ktime_delta);
185                 lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_SAMEDIR_SIZE],
186                                       (unsigned int)ma->ma_attr.la_size);
187                 return;
188         }
189
190         mdt_counter_incr(req, LPROC_MDT_RENAME_CROSSDIR, ktime_delta);
191         lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_CROSSDIR_SRC_SIZE],
192                               (unsigned int)ma->ma_attr.la_size);
193
194         ma->ma_need = MA_INODE;
195         ma->ma_valid = 0;
196         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
197         if (rc) {
198                 CERROR("%s: "DFID" attr_get, rc = %d\n",
199                        mdt_obd_name(mdt), PFID(mdt_object_fid(tgt)), rc);
200                 return;
201         }
202
203         lprocfs_oh_tally_log2(&rstats->rs_hist[RENAME_CROSSDIR_TGT_SIZE],
204                               (unsigned int)ma->ma_attr.la_size);
205 }
206
207 static ssize_t identity_expire_show(struct kobject *kobj,
208                                     struct attribute *attr, char *buf)
209 {
210         struct obd_device *obd = container_of(kobj, struct obd_device,
211                                               obd_kset.kobj);
212         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
213
214         return scnprintf(buf, PAGE_SIZE, "%lld\n",
215                          mdt->mdt_identity_cache->uc_entry_expire);
216 }
217
218 static ssize_t identity_expire_store(struct kobject *kobj,
219                                      struct attribute *attr,
220                                      const char *buffer, size_t count)
221 {
222         struct obd_device *obd = container_of(kobj, struct obd_device,
223                                               obd_kset.kobj);
224         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
225         time64_t val;
226         int rc;
227
228         rc = kstrtoll(buffer, 10, &val);
229         if (rc)
230                 return rc;
231
232         if (val < 0)
233                 return -ERANGE;
234
235         mdt->mdt_identity_cache->uc_entry_expire = val;
236
237         return count;
238 }
239 LUSTRE_RW_ATTR(identity_expire);
240
241 static ssize_t identity_acquire_expire_show(struct kobject *kobj,
242                                             struct attribute *attr, char *buf)
243 {
244         struct obd_device *obd = container_of(kobj, struct obd_device,
245                                               obd_kset.kobj);
246         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
247
248         return scnprintf(buf, PAGE_SIZE, "%lld\n",
249                          mdt->mdt_identity_cache->uc_acquire_expire);
250 }
251
252 static ssize_t identity_acquire_expire_store(struct kobject *kobj,
253                                              struct attribute *attr,
254                                              const char *buffer, size_t count)
255 {
256         struct obd_device *obd = container_of(kobj, struct obd_device,
257                                               obd_kset.kobj);
258         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
259         time64_t val;
260         int rc;
261
262         rc = kstrtoll(buffer, 0, &val);
263         if (rc)
264                 return rc;
265
266         if (val < 0 || val > INT_MAX)
267                 return -ERANGE;
268
269         mdt->mdt_identity_cache->uc_acquire_expire = val;
270
271         return count;
272 }
273 LUSTRE_RW_ATTR(identity_acquire_expire);
274
275 static ssize_t identity_upcall_show(struct kobject *kobj,
276                                     struct attribute *attr, char *buf)
277 {
278         struct obd_device *obd = container_of(kobj, struct obd_device,
279                                               obd_kset.kobj);
280         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
281         struct upcall_cache *hash = mdt->mdt_identity_cache;
282         int rc;
283
284         down_read(&hash->uc_upcall_rwsem);
285         rc = scnprintf(buf, PAGE_SIZE, "%s\n", hash->uc_upcall);
286         up_read(&hash->uc_upcall_rwsem);
287         return rc;
288 }
289
290 static ssize_t identity_upcall_store(struct kobject *kobj,
291                                      struct attribute *attr,
292                                      const char *buffer, size_t count)
293 {
294         struct obd_device *obd = container_of(kobj, struct obd_device,
295                                               obd_kset.kobj);
296         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
297         struct upcall_cache *hash = mdt->mdt_identity_cache;
298
299         if (count >= UC_CACHE_UPCALL_MAXPATH) {
300                 CERROR("%s: identity upcall too long\n", mdt_obd_name(mdt));
301                 return -EINVAL;
302         }
303
304         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
305         down_write(&hash->uc_upcall_rwsem);
306         sscanf(buffer, "%s", hash->uc_upcall);
307         up_write(&hash->uc_upcall_rwsem);
308
309         if (strcmp(hash->uc_name, mdt_obd_name(mdt)) != 0)
310                 CWARN("%s: write to upcall name %s\n",
311                       mdt_obd_name(mdt), hash->uc_upcall);
312
313         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
314                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
315                       "cause unexpected \"EACCESS\"\n", mdt_obd_name(mdt));
316
317         CDEBUG(D_CONFIG, "%s: identity upcall set to %s\n", mdt_obd_name(mdt),
318                hash->uc_upcall);
319         RETURN(count);
320 }
321 LUSTRE_RW_ATTR(identity_upcall);
322
323 static ssize_t identity_flush_store(struct kobject *kobj,
324                                     struct attribute *attr,
325                                     const char *buffer, size_t count)
326 {
327         struct obd_device *obd = container_of(kobj, struct obd_device,
328                                               obd_kset.kobj);
329         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
330         int uid;
331         int rc;
332
333         rc = kstrtoint(buffer, 0, &uid);
334         if (rc)
335                 return rc;
336
337         mdt_flush_identity(mdt->mdt_identity_cache, uid);
338         return count;
339 }
340 LUSTRE_WO_ATTR(identity_flush);
341
342 static ssize_t
343 lprocfs_identity_info_seq_write(struct file *file, const char __user *buffer,
344                                 size_t count, void *data)
345 {
346         struct seq_file   *m = file->private_data;
347         struct obd_device *obd = m->private;
348         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
349         struct identity_downcall_data *param;
350         int size = sizeof(*param), rc, checked = 0;
351
352 again:
353         if (count < size) {
354                 CERROR("%s: invalid data count = %lu, size = %d\n",
355                        mdt_obd_name(mdt), (unsigned long) count, size);
356                 return -EINVAL;
357         }
358
359         OBD_ALLOC(param, size);
360         if (param == NULL)
361                 return -ENOMEM;
362
363         if (copy_from_user(param, buffer, size)) {
364                 CERROR("%s: bad identity data\n", mdt_obd_name(mdt));
365                 GOTO(out, rc = -EFAULT);
366         }
367
368         if (checked == 0) {
369                 checked = 1;
370                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
371                         CERROR("%s: MDS identity downcall bad params\n",
372                                mdt_obd_name(mdt));
373                         GOTO(out, rc = -EINVAL);
374                 }
375
376                 if (param->idd_nperms > N_PERMS_MAX) {
377                         CERROR("%s: perm count %d more than maximum %d\n",
378                                mdt_obd_name(mdt), param->idd_nperms,
379                                N_PERMS_MAX);
380                         GOTO(out, rc = -EINVAL);
381                 }
382
383                 if (param->idd_ngroups > NGROUPS_MAX) {
384                         CERROR("%s: group count %d more than maximum %d\n",
385                                mdt_obd_name(mdt), param->idd_ngroups,
386                                NGROUPS_MAX);
387                         GOTO(out, rc = -EINVAL);
388                 }
389
390                 if (param->idd_ngroups) {
391                         rc = param->idd_ngroups; /* save idd_ngroups */
392                         OBD_FREE(param, size);
393                         size = offsetof(struct identity_downcall_data,
394                                         idd_groups[rc]);
395                         goto again;
396                 }
397         }
398
399         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
400                                    param->idd_uid, param);
401
402 out:
403         if (param != NULL)
404                 OBD_FREE(param, size);
405
406         return rc ? rc : count;
407 }
408 LPROC_SEQ_FOPS_WR_ONLY(mdt, identity_info);
409
410 static int mdt_site_stats_seq_show(struct seq_file *m, void *data)
411 {
412         struct obd_device *obd = m->private;
413         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
414
415         return lu_site_stats_seq_print(mdt_lu_site(mdt), m);
416 }
417 LPROC_SEQ_FOPS_RO(mdt_site_stats);
418
419 #define BUFLEN (UUID_MAX + 4)
420
421 static ssize_t
422 lprocfs_mds_evict_client_seq_write(struct file *file, const char __user *buf,
423                                    size_t count, loff_t *off)
424 {
425         struct seq_file   *m = file->private_data;
426         struct obd_device *obd = m->private;
427         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
428         char *kbuf;
429         char *tmpbuf;
430         int rc = 0;
431
432         OBD_ALLOC(kbuf, BUFLEN);
433         if (kbuf == NULL)
434                 return -ENOMEM;
435
436         /*
437          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
438          * bytes into kbuf, to ensure that the string is NUL-terminated.
439          * UUID_MAX should include a trailing NUL already.
440          */
441         if (copy_from_user(kbuf, buf, min_t(unsigned long, BUFLEN - 1, count)))
442                 GOTO(out, rc = -EFAULT);
443         tmpbuf = skip_spaces(kbuf);
444         tmpbuf = strsep(&tmpbuf, " \t\n\f\v\r");
445
446         if (strncmp(tmpbuf, "nid:", 4) != 0) {
447                 count = lprocfs_evict_client_seq_write(file, buf, count, off);
448                 goto out;
449         }
450
451         if (mdt->mdt_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_striped_dir);
734 MDT_BOOL_RW_ATTR(enable_dir_migration);
735 MDT_BOOL_RW_ATTR(enable_dir_restripe);
736 MDT_BOOL_RW_ATTR(enable_dir_auto_split);
737 MDT_BOOL_RW_ATTR(dir_restripe_nsonly);
738 MDT_BOOL_RW_ATTR(migrate_hsm_allowed);
739 MDT_BOOL_RW_ATTR(enable_strict_som);
740 MDT_BOOL_RW_ATTR(enable_dmv_implicit_inherit);
741 MDT_BOOL_RW_ATTR(enable_dmv_xattr);
742
743 /**
744  * Show if the MDT is in no create mode.
745  *
746  * This means MDT has been adminstratively disabled to prevent it
747  * from creating any new directories on the MDT, though existing files
748  * and directories can still be read, written, and unlinked.
749  *
750  * \retval              number of bytes written
751  */
752 static ssize_t no_create_show(struct kobject *kobj, struct attribute *attr,
753                               char *buf)
754 {
755         struct obd_device *obd = container_of(kobj, struct obd_device,
756                                               obd_kset.kobj);
757         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
758
759         return scnprintf(buf, PAGE_SIZE, "%u\n", mdt->mdt_lut.lut_no_create);
760 }
761
762 /**
763  * Set MDT to no create mode.
764  *
765  * This is used to interface to userspace administrative tools to
766  * disable new directory creation on the MDT.
767  *
768  * \param[in] count     \a buffer length
769  *
770  * \retval              \a count on success
771  * \retval              negative number on error
772  */
773 static ssize_t no_create_store(struct kobject *kobj, struct attribute *attr,
774                                const char *buffer, size_t count)
775 {
776         struct obd_device *obd = container_of(kobj, struct obd_device,
777                                               obd_kset.kobj);
778         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
779         bool val;
780         int rc;
781
782         rc = kstrtobool(buffer, &val);
783         if (rc)
784                 return rc;
785
786         mdt->mdt_lut.lut_no_create = val;
787
788         return count;
789 }
790 LUSTRE_RW_ATTR(no_create);
791
792 /**
793  * Show MDT async commit count.
794  *
795  * @m           seq_file handle
796  * @data        unused for single entry
797  *
798  * Return:      0 on success
799  *              negative value on error
800  */
801 static ssize_t async_commit_count_show(struct kobject *kobj,
802                                        struct attribute *attr, char *buf)
803 {
804         struct obd_device *obd = container_of(kobj, struct obd_device,
805                                               obd_kset.kobj);
806         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
807
808         return scnprintf(buf, PAGE_SIZE, "%d\n",
809                          atomic_read(&mdt->mdt_async_commit_count));
810 }
811
812 static ssize_t async_commit_count_store(struct kobject *kobj,
813                                         struct attribute *attr,
814                                         const char *buffer, size_t count)
815 {
816         struct obd_device *obd = container_of(kobj, struct obd_device,
817                                               obd_kset.kobj);
818         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
819         int val;
820         int rc;
821
822         rc = kstrtoint(buffer, 10, &val);
823         if (rc)
824                 return rc;
825
826         atomic_set(&mdt->mdt_async_commit_count, val);
827
828         return count;
829 }
830 LUSTRE_RW_ATTR(async_commit_count);
831
832 /**
833  * Show MDT sync count.
834  *
835  * \param[in] m         seq_file handle
836  * \param[in] data      unused for single entry
837  *
838  * \retval              0 on success
839  * \retval              negative value on error
840  */
841 static ssize_t sync_count_show(struct kobject *kobj, struct attribute *attr,
842                                char *buf)
843 {
844         struct obd_device *obd = container_of(kobj, struct obd_device,
845                                               obd_kset.kobj);
846         struct lu_target *tgt = obd2obt(obd)->obt_lut;
847
848         return scnprintf(buf, PAGE_SIZE, "%d\n",
849                          atomic_read(&tgt->lut_sync_count));
850 }
851
852 static ssize_t sync_count_store(struct kobject *kobj, struct attribute *attr,
853                                 const char *buffer, size_t count)
854 {
855         struct obd_device *obd = container_of(kobj, struct obd_device,
856                                               obd_kset.kobj);
857         struct lu_target *tgt = obd2obt(obd)->obt_lut;
858         int val;
859         int rc;
860
861         rc = kstrtoint(buffer, 0, &val);
862         if (rc)
863                 return rc;
864
865         atomic_set(&tgt->lut_sync_count, val);
866
867         return count;
868 }
869 LUSTRE_RW_ATTR(sync_count);
870
871 static const char *dom_open_lock_modes[NUM_DOM_LOCK_ON_OPEN_MODES] = {
872         [NO_DOM_LOCK_ON_OPEN] = "never",
873         [TRYLOCK_DOM_ON_OPEN] = "trylock",
874         [ALWAYS_DOM_LOCK_ON_OPEN] = "always",
875 };
876
877 /* This must be longer than the longest string above */
878 #define DOM_LOCK_MODES_MAXLEN 16
879
880 /**
881  * Show MDT policy for data prefetch on open for DoM files..
882  *
883  * \param[in] m         seq_file handle
884  * \param[in] data      unused
885  *
886  * \retval              0 on success
887  * \retval              negative value on error
888  */
889 static ssize_t dom_lock_show(struct kobject *kobj, struct attribute *attr,
890                              char *buf)
891 {
892         struct obd_device *obd = container_of(kobj, struct obd_device,
893                                               obd_kset.kobj);
894         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
895
896         return scnprintf(buf, PAGE_SIZE, "%s\n",
897                          dom_open_lock_modes[mdt->mdt_opts.mo_dom_lock]);
898 }
899
900 /**
901  * Change MDT policy for data prefetch on open for DoM files.
902  *
903  * This variable defines how DOM lock is taken at open enqueue.
904  * There are three possible modes:
905  * 1) never - never take DoM lock on open. DoM lock will be taken as separate
906  *    IO lock with own enqueue.
907  * 2) trylock - DoM lock will be taken only if non-blocked.
908  * 3) always - DoM lock will be taken always even if it is blocking lock.
909  *
910  * If dom_read_open is enabled too then DoM lock is taken in PR mode and
911  * is paired with LAYOUT lock when possible.
912  *
913  * \param[in] file      proc file
914  * \param[in] buffer    string which represents policy
915  * \param[in] count     \a buffer length
916  * \param[in] off       unused for single entry
917  *
918  * \retval              \a count on success
919  * \retval              negative number on error
920  */
921 static ssize_t dom_lock_store(struct kobject *kobj, struct attribute *attr,
922                               const char *buffer, size_t count)
923 {
924         struct obd_device *obd = container_of(kobj, struct obd_device,
925                                               obd_kset.kobj);
926         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
927         int val = -1;
928         int i, rc;
929
930         if (count == 0 || count >= DOM_LOCK_MODES_MAXLEN)
931                 return -EINVAL;
932
933         for (i = 0 ; i < NUM_DOM_LOCK_ON_OPEN_MODES; i++) {
934                 /* buffer might have '\n' but using strlen() avoids it */
935                 if (strncmp(buffer, dom_open_lock_modes[i],
936                             strlen(dom_open_lock_modes[i])) == 0) {
937                         val = i;
938                         break;
939                 }
940         }
941
942         /* Legacy numeric codes */
943         if (val == -1) {
944                 rc = kstrtoint(buffer, 0, &val);
945                 if (rc)
946                         return rc;
947         }
948
949         if (val == ALWAYS_DOM_LOCK_ON_OPEN)
950                 val = TRYLOCK_DOM_ON_OPEN;
951
952         if (val < 0 || val >= NUM_DOM_LOCK_ON_OPEN_MODES)
953                 return -EINVAL;
954
955         mdt->mdt_opts.mo_dom_lock = val;
956         return count;
957 }
958 LUSTRE_RW_ATTR(dom_lock);
959
960 static ssize_t dir_split_count_show(struct kobject *kobj,
961                                      struct attribute *attr,
962                                      char *buf)
963 {
964         struct obd_device *obd = container_of(kobj, struct obd_device,
965                                               obd_kset.kobj);
966         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
967
968         return scnprintf(buf, PAGE_SIZE, "%llu\n",
969                          mdt->mdt_restriper.mdr_dir_split_count);
970 }
971
972 static ssize_t dir_split_count_store(struct kobject *kobj,
973                                       struct attribute *attr,
974                                       const char *buffer, size_t count)
975 {
976         struct obd_device *obd = container_of(kobj, struct obd_device,
977                                               obd_kset.kobj);
978         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
979         s64 val;
980         int rc;
981
982         rc = sysfs_memparse(buffer, count, &val, "B");
983         if (rc < 0)
984                 return rc;
985
986         if (val < 0)
987                 return -ERANGE;
988
989         mdt->mdt_restriper.mdr_dir_split_count = val;
990
991         return count;
992 }
993 LUSTRE_RW_ATTR(dir_split_count);
994
995 static ssize_t dir_split_delta_show(struct kobject *kobj,
996                                     struct attribute *attr,
997                                     char *buf)
998 {
999         struct obd_device *obd = container_of(kobj, struct obd_device,
1000                                               obd_kset.kobj);
1001         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1002
1003         return scnprintf(buf, PAGE_SIZE, "%u\n",
1004                          mdt->mdt_restriper.mdr_dir_split_delta);
1005 }
1006
1007 static ssize_t dir_split_delta_store(struct kobject *kobj,
1008                                      struct attribute *attr,
1009                                      const char *buffer, size_t count)
1010 {
1011         struct obd_device *obd = container_of(kobj, struct obd_device,
1012                                               obd_kset.kobj);
1013         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1014         u32 val;
1015         int rc;
1016
1017         rc = kstrtouint(buffer, 0, &val);
1018         if (rc)
1019                 return rc;
1020
1021         mdt->mdt_restriper.mdr_dir_split_delta = val;
1022
1023         return count;
1024 }
1025 LUSTRE_RW_ATTR(dir_split_delta);
1026
1027 static ssize_t enable_remote_subdir_mount_show(struct kobject *kobj,
1028                                                struct attribute *attr,
1029                                                char *buf)
1030 {
1031         return scnprintf(buf, PAGE_SIZE, "%u\n", 1);
1032 }
1033
1034 static ssize_t enable_remote_subdir_mount_store(struct kobject *kobj,
1035                                                 struct attribute *attr,
1036                                                 const char *buffer,
1037                                                 size_t count)
1038 {
1039         LCONSOLE_WARN("enable_remote_subdir_mount is deprecated, it's always enabled.\n");
1040         return count;
1041 }
1042 LUSTRE_RW_ATTR(enable_remote_subdir_mount);
1043
1044 /**
1045  * Show if the OFD enforces T10PI checksum.
1046  *
1047  * \param[in] m         seq_file handle
1048  * \param[in] data      unused for single entry
1049  *
1050  * \retval              0 on success
1051  * \retval              negative value on error
1052  */
1053 static ssize_t checksum_t10pi_enforce_show(struct kobject *kobj,
1054                                            struct attribute *attr,
1055                                            char *buf)
1056 {
1057         struct obd_device *obd = container_of(kobj, struct obd_device,
1058                                               obd_kset.kobj);
1059         struct lu_target *lut = obd2obt(obd)->obt_lut;
1060
1061         return scnprintf(buf, PAGE_SIZE, "%u\n", lut->lut_cksum_t10pi_enforce);
1062 }
1063
1064 /**
1065  * Force specific T10PI checksum modes to be enabled
1066  *
1067  * If T10PI *is* supported in hardware, allow only the supported T10PI type
1068  * to be used. If T10PI is *not* supported by the OSD, setting the enforce
1069  * parameter forces all T10PI types to be enabled (even if slower) for
1070  * testing.
1071  *
1072  * The final determination of which algorithm to be used depends whether
1073  * the client supports T10PI or not, and is handled at client connect time.
1074  *
1075  * \param[in] file      proc file
1076  * \param[in] buffer    string which represents mode
1077  *                      1: set T10PI checksums enforced
1078  *                      0: unset T10PI checksums enforced
1079  * \param[in] count     \a buffer length
1080  * \param[in] off       unused for single entry
1081  *
1082  * \retval              \a count on success
1083  * \retval              negative number on error
1084  */
1085 static ssize_t checksum_t10pi_enforce_store(struct kobject *kobj,
1086                                             struct attribute *attr,
1087                                             const char *buffer, size_t count)
1088 {
1089         struct obd_device *obd = container_of(kobj, struct obd_device,
1090                                               obd_kset.kobj);
1091         struct lu_target *lut = obd2obt(obd)->obt_lut;
1092         bool enforce;
1093         int rc;
1094
1095         rc = kstrtobool(buffer, &enforce);
1096         if (rc)
1097                 return rc;
1098
1099         spin_lock(&lut->lut_flags_lock);
1100         lut->lut_cksum_t10pi_enforce = enforce;
1101         spin_unlock(&lut->lut_flags_lock);
1102         return count;
1103 }
1104 LUSTRE_RW_ATTR(checksum_t10pi_enforce);
1105
1106 /**
1107  * Show MDT Maximum modify RPCs in flight.
1108  *
1109  * @m           seq_file handle
1110  * @data        unused for single entry
1111  *
1112  * Return:      value on success or negative number on error
1113  */
1114 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
1115                                        struct attribute *attr, char *buf)
1116 {
1117         struct obd_device *obd = container_of(kobj, struct obd_device,
1118                                               obd_kset.kobj);
1119         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1120
1121         return scnprintf(buf, PAGE_SIZE, "%u\n",
1122                          mdt->mdt_max_mod_rpcs_in_flight);
1123 }
1124
1125 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
1126                                         struct attribute *attr,
1127                                         const char *buffer, size_t count)
1128 {
1129         struct obd_device *obd = container_of(kobj, struct obd_device,
1130                                               obd_kset.kobj);
1131         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1132         unsigned int val;
1133         int rc;
1134
1135         rc = kstrtouint(buffer, 0, &val);
1136         if (rc)
1137                 return rc;
1138
1139         if (val < 1 || val > OBD_MAX_RIF_MAX)
1140                 return -ERANGE;
1141
1142         if (mdt_max_mod_rpcs_changed(mdt)) {
1143                 CWARN("%s: deprecated 'max_mod_rpcs_in_flight' module parameter has also been modified\n",
1144                                 obd->obd_name);
1145                 max_mod_rpcs_per_client = val;
1146         }
1147         mdt->mdt_max_mod_rpcs_in_flight = val;
1148
1149         return count;
1150 }
1151 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
1152
1153 /*
1154  * mdt_checksum_type(server) proc handling
1155  */
1156 DECLARE_CKSUM_NAME;
1157
1158 static int mdt_checksum_type_seq_show(struct seq_file *m, void *data)
1159 {
1160         struct obd_device *obd = m->private;
1161         struct lu_target *lut;
1162         enum cksum_types pref;
1163         int i;
1164
1165         if (!obd)
1166                 return 0;
1167
1168         lut = obd2obt(obd)->obt_lut;
1169         /* select fastest checksum type on the server */
1170         pref = obd_cksum_type_select(obd->obd_name,
1171                                      lut->lut_cksum_types_supported,
1172                                      lut->lut_dt_conf.ddp_t10_cksum_type);
1173
1174         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
1175                 if ((BIT(i) & lut->lut_cksum_types_supported) == 0)
1176                         continue;
1177
1178                 if (pref == BIT(i))
1179                         seq_printf(m, "[%s] ", cksum_name[i]);
1180                 else
1181                         seq_printf(m, "%s ", cksum_name[i]);
1182         }
1183         seq_puts(m, "\n");
1184
1185         return 0;
1186 }
1187
1188 ssize_t job_xattr_show(struct kobject *kobj, struct attribute *attr, char *buf)
1189 {
1190         struct obd_device *obd = container_of(kobj, struct obd_device,
1191                                               obd_kset.kobj);
1192         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1193
1194         if (mdt->mdt_job_xattr[0] == '\0')
1195                 return scnprintf(buf, PAGE_SIZE, "NONE\n");
1196
1197         return scnprintf(buf, PAGE_SIZE, "%s\n", mdt->mdt_job_xattr);
1198 }
1199
1200 /**
1201  * Read in a name for the jobid xattr and validate it.
1202  * The only valid names are "trusted.job" or "user.*" where the name portion
1203  * is <= 7 bytes in the user namespace. Only alphanumeric characters are
1204  * allowed, aside from the namespace separator '.'.
1205  *
1206  * "none" is a valid value to turn this feature off.
1207  *
1208  * @return -EINVAL if the name is invalid, else count
1209  */
1210 ssize_t job_xattr_store(struct kobject *kobj, struct attribute *attr,
1211                         const char *buffer, size_t count)
1212 {
1213         struct obd_device *obd = container_of(kobj, struct obd_device,
1214                                               obd_kset.kobj);
1215         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1216         char name[XATTR_JOB_MAX_LEN] = { 0 };
1217         char *p;
1218
1219
1220         /* writing "none" turns this off by leaving the name empty */
1221         if (!strncmp(buffer, "none", 4) ||
1222             !strncmp(buffer, "NONE", 4)) {
1223                 memset(mdt->mdt_job_xattr, 0, sizeof(mdt->mdt_job_xattr));
1224                 return count;
1225         }
1226
1227         /* account for stripping \n before rejecting name for being too long */
1228         if (count > XATTR_JOB_MAX_LEN - 1 &&
1229             buffer[XATTR_JOB_MAX_LEN - 1] != '\n')
1230                 return -EINVAL;
1231
1232         strncpy(name, buffer, XATTR_JOB_MAX_LEN - 1);
1233
1234         /* reject if not in namespace.name format */
1235         p = strchr(name, '.');
1236         if (p == NULL)
1237                 return -EINVAL;
1238
1239         p++;
1240         for (; *p != '\0'; p++) {
1241                 /*
1242                  * if there are any non-alphanumeric characters, the name is
1243                  * invalid unless it's a newline, in which case overwrite it
1244                  * with '\0' and that's the end of the name.
1245                  */
1246                 if (!isalnum(*p)) {
1247                         if (*p != '\n')
1248                                 return -EINVAL;
1249                         *p = '\0';
1250                 }
1251         }
1252
1253         /* trusted.job is only valid name in trusted namespace */
1254         if (!strncmp(name, "trusted.job", 12)) {
1255                 strncpy(mdt->mdt_job_xattr, name, XATTR_JOB_MAX_LEN);
1256                 return count;
1257         }
1258
1259         /* only other valid namespace is user */
1260         if (strncmp(name, XATTR_USER_PREFIX, sizeof(XATTR_USER_PREFIX) - 1))
1261                 return -EINVAL;
1262
1263         /* ensure that a name was specified */
1264         if (name[sizeof(XATTR_USER_PREFIX) - 1] == '\0')
1265                 return -EINVAL;
1266
1267         strncpy(mdt->mdt_job_xattr, name, XATTR_JOB_MAX_LEN);
1268
1269         return count;
1270 }
1271
1272 LPROC_SEQ_FOPS_RO(mdt_checksum_type);
1273
1274 LPROC_SEQ_FOPS_RO_TYPE(mdt, hash);
1275 LPROC_SEQ_FOPS_WR_ONLY(mdt, mds_evict_client);
1276 LPROC_SEQ_FOPS_RW_TYPE(mdt, checksum_dump);
1277 LUSTRE_RW_ATTR(job_cleanup_interval);
1278 LUSTRE_RW_ATTR(job_xattr);
1279 LPROC_SEQ_FOPS_RW_TYPE(mdt, nid_stats_clear);
1280 LUSTRE_RW_ATTR(hsm_control);
1281
1282 LPROC_SEQ_FOPS_RO_TYPE(mdt, recovery_status);
1283 LUSTRE_RW_ATTR(recovery_time_hard);
1284 LUSTRE_RW_ATTR(recovery_time_soft);
1285 LUSTRE_RW_ATTR(ir_factor);
1286
1287 LUSTRE_RO_ATTR(tot_dirty);
1288 LUSTRE_RO_ATTR(tot_granted);
1289 LUSTRE_RO_ATTR(tot_pending);
1290 LUSTRE_RW_ATTR(grant_compat_disable);
1291 LUSTRE_RO_ATTR(instance);
1292
1293 LUSTRE_RO_ATTR(num_exports);
1294 LUSTRE_RW_ATTR(grant_check_threshold);
1295 LUSTRE_RO_ATTR(eviction_count);
1296
1297 /* per-device at parameters */
1298 LUSTRE_OBD_UINT_PARAM_ATTR(at_min);
1299 LUSTRE_OBD_UINT_PARAM_ATTR(at_max);
1300 LUSTRE_OBD_UINT_PARAM_ATTR(at_history);
1301
1302 static struct attribute *mdt_attrs[] = {
1303         &lustre_attr_tot_dirty.attr,
1304         &lustre_attr_tot_granted.attr,
1305         &lustre_attr_tot_pending.attr,
1306         &lustre_attr_grant_compat_disable.attr,
1307         &lustre_attr_instance.attr,
1308         &lustre_attr_recovery_time_hard.attr,
1309         &lustre_attr_recovery_time_soft.attr,
1310         &lustre_attr_ir_factor.attr,
1311         &lustre_attr_num_exports.attr,
1312         &lustre_attr_grant_check_threshold.attr,
1313         &lustre_attr_eviction_count.attr,
1314         &lustre_attr_identity_expire.attr,
1315         &lustre_attr_identity_acquire_expire.attr,
1316         &lustre_attr_identity_upcall.attr,
1317         &lustre_attr_identity_flush.attr,
1318         &lustre_attr_evict_tgt_nids.attr,
1319         &lustre_attr_enable_cap_mask.attr,
1320         &lustre_attr_enable_chprojid_gid.attr,
1321         &lustre_attr_enable_dir_migration.attr,
1322         &lustre_attr_enable_dir_restripe.attr,
1323         &lustre_attr_enable_dir_auto_split.attr,
1324         &lustre_attr_enable_parallel_rename_dir.attr,
1325         &lustre_attr_enable_parallel_rename_file.attr,
1326         &lustre_attr_enable_remote_dir.attr,
1327         &lustre_attr_enable_remote_dir_gid.attr,
1328         &lustre_attr_enable_remote_rename.attr,
1329         &lustre_attr_enable_striped_dir.attr,
1330         &lustre_attr_commit_on_sharing.attr,
1331         &lustre_attr_local_recovery.attr,
1332         &lustre_attr_no_create.attr,
1333         &lustre_attr_async_commit_count.attr,
1334         &lustre_attr_sync_count.attr,
1335         &lustre_attr_dom_lock.attr,
1336         &lustre_attr_dom_read_open.attr,
1337         &lustre_attr_enable_strict_som.attr,
1338         &lustre_attr_migrate_hsm_allowed.attr,
1339         &lustre_attr_hsm_control.attr,
1340         &lustre_attr_job_cleanup_interval.attr,
1341         &lustre_attr_job_xattr.attr,
1342         &lustre_attr_readonly.attr,
1343         &lustre_attr_dir_split_count.attr,
1344         &lustre_attr_dir_split_delta.attr,
1345         &lustre_attr_dir_restripe_nsonly.attr,
1346         &lustre_attr_checksum_t10pi_enforce.attr,
1347         &lustre_attr_enable_remote_subdir_mount.attr,
1348         &lustre_attr_max_mod_rpcs_in_flight.attr,
1349         &lustre_attr_enable_dmv_implicit_inherit.attr,
1350         &lustre_attr_at_min.attr,
1351         &lustre_attr_at_max.attr,
1352         &lustre_attr_at_history.attr,
1353         &lustre_attr_enable_dmv_xattr.attr,
1354         NULL,
1355 };
1356
1357 KOBJ_ATTRIBUTE_GROUPS(mdt); /* creates mdt_groups from mdt_attrs */
1358
1359 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
1360         { .name =       "recovery_status",
1361           .fops =       &mdt_recovery_status_fops               },
1362         { .name =       "identity_info",
1363           .fops =       &mdt_identity_info_fops                 },
1364         { .name =       "site_stats",
1365           .fops =       &mdt_site_stats_fops                    },
1366         { .name =       "evict_client",
1367           .fops =       &mdt_mds_evict_client_fops              },
1368         { .name =       "checksum_dump",
1369           .fops =       &mdt_checksum_dump_fops                 },
1370         { .name =       "hash_stats",
1371           .fops =       &mdt_hash_fops                          },
1372         { .name =       "root_squash",
1373           .fops =       &mdt_root_squash_fops                   },
1374         { .name =       "nosquash_nids",
1375           .fops =       &mdt_nosquash_nids_fops                 },
1376         { .name =       "checksum_type",
1377           .fops =       &mdt_checksum_type_fops         },
1378         { NULL }
1379 };
1380
1381 LDEBUGFS_SEQ_FOPS_RO_TYPE(mdt, recovery_stale_clients);
1382
1383 static struct ldebugfs_vars ldebugfs_mdt_obd_vars[] = {
1384         { .name =       "recovery_stale_clients",
1385           .fops =       &mdt_recovery_stale_clients_fops        },
1386         { NULL }
1387 };
1388
1389 static int
1390 lprocfs_mdt_print_open_files(struct obd_export *exp, void *v)
1391 {
1392         struct seq_file         *seq = v;
1393
1394         if (exp->exp_lock_hash != NULL) {
1395                 struct mdt_export_data  *med = &exp->exp_mdt_data;
1396                 struct mdt_file_data    *mfd;
1397
1398                 spin_lock(&med->med_open_lock);
1399                 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1400                         seq_printf(seq, DFID"\n",
1401                                    PFID(mdt_object_fid(mfd->mfd_object)));
1402                 }
1403                 spin_unlock(&med->med_open_lock);
1404         }
1405
1406         return 0;
1407 }
1408
1409 static int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
1410 {
1411         struct nid_stat *stats = seq->private;
1412
1413         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
1414                                        lprocfs_mdt_print_open_files, seq);
1415 }
1416
1417 int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
1418 {
1419         struct seq_file         *seq;
1420         int                     rc;
1421
1422         rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
1423         if (rc != 0)
1424                 return rc;
1425
1426         seq = file->private_data;
1427         seq->private = pde_data(inode);
1428
1429         return 0;
1430 }
1431
1432 void mdt_counter_incr(struct ptlrpc_request *req, int opcode, long amount)
1433 {
1434         struct obd_export *exp = req->rq_export;
1435
1436         if (exp->exp_obd && exp->exp_obd->obd_md_stats)
1437                 lprocfs_counter_add(exp->exp_obd->obd_md_stats,
1438                                     opcode + LPROC_MD_LAST_OPC, amount);
1439         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1440                 lprocfs_counter_add(exp->exp_nid_stats->nid_stats, opcode,
1441                                     amount);
1442         if (exp->exp_obd && obd2obt(exp->exp_obd)->obt_jobstats.ojs_hash &&
1443             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1444                 lprocfs_job_stats_log(exp->exp_obd,
1445                                       lustre_msg_get_jobid(req->rq_reqmsg),
1446                                       opcode, amount);
1447 }
1448
1449 static const char * const mdt_stats[] = {
1450         [LPROC_MDT_OPEN]                = "open",
1451         [LPROC_MDT_CLOSE]               = "close",
1452         [LPROC_MDT_MKNOD]               = "mknod",
1453         [LPROC_MDT_LINK]                = "link",
1454         [LPROC_MDT_UNLINK]              = "unlink",
1455         [LPROC_MDT_MKDIR]               = "mkdir",
1456         [LPROC_MDT_RMDIR]               = "rmdir",
1457         [LPROC_MDT_RENAME]              = "rename",
1458         [LPROC_MDT_GETATTR]             = "getattr",
1459         [LPROC_MDT_SETATTR]             = "setattr",
1460         [LPROC_MDT_GETXATTR]            = "getxattr",
1461         [LPROC_MDT_SETXATTR]            = "setxattr",
1462         [LPROC_MDT_STATFS]              = "statfs",
1463         [LPROC_MDT_SYNC]                = "sync",
1464         [LPROC_MDT_RENAME_SAMEDIR]      = "samedir_rename",
1465         [LPROC_MDT_RENAME_PAR_FILE]     = "parallel_rename_file",
1466         [LPROC_MDT_RENAME_PAR_DIR]      = "parallel_rename_dir",
1467         [LPROC_MDT_RENAME_CROSSDIR]     = "crossdir_rename",
1468         [LPROC_MDT_IO_READ_BYTES]       = "read_bytes",
1469         [LPROC_MDT_IO_WRITE_BYTES]      = "write_bytes",
1470         [LPROC_MDT_IO_READ]             = "read",
1471         [LPROC_MDT_IO_WRITE]            = "write",
1472         [LPROC_MDT_IO_PUNCH]            = "punch",
1473         [LPROC_MDT_MIGRATE]             = "migrate",
1474         [LPROC_MDT_FALLOCATE]           = "fallocate",
1475 };
1476
1477 void mdt_stats_counter_init(struct lprocfs_stats *stats, unsigned int offset,
1478                             enum lprocfs_counter_config cntr_umask)
1479 {
1480         int array_size = ARRAY_SIZE(mdt_stats);
1481         int oidx; /* obd_md_stats index */
1482         int midx; /* mdt_stats index */
1483
1484         LASSERT(stats && stats->ls_num >= offset + array_size);
1485
1486         for (midx = 0; midx < array_size; midx++) {
1487                 oidx = midx + offset;
1488                 if (midx == LPROC_MDT_IO_READ_BYTES ||
1489                     midx == LPROC_MDT_IO_WRITE_BYTES)
1490                         lprocfs_counter_init(stats, oidx,
1491                                              LPROCFS_TYPE_BYTES_FULL_HISTOGRAM &
1492                                              (~cntr_umask),
1493                                              mdt_stats[midx]);
1494                 else
1495                         lprocfs_counter_init(stats, oidx,
1496                                              LPROCFS_TYPE_LATENCY &
1497                                              (~cntr_umask),
1498                                              mdt_stats[midx]);
1499         }
1500 }
1501
1502 int mdt_tunables_init(struct mdt_device *mdt, const char *name)
1503 {
1504         struct obd_device *obd = mdt2obd_dev(mdt);
1505         int rc;
1506
1507         ENTRY;
1508         LASSERT(name != NULL);
1509
1510         obd->obd_ktype.default_groups = KOBJ_ATTR_GROUPS(mdt);
1511         obd->obd_vars = lprocfs_mdt_obd_vars;
1512         rc = lprocfs_obd_setup(obd, true);
1513         if (rc) {
1514                 CERROR("%s: cannot create proc entries: rc = %d\n",
1515                        mdt_obd_name(mdt), rc);
1516                 return rc;
1517         }
1518         ldebugfs_add_vars(obd->obd_debugfs_entry, ldebugfs_mdt_obd_vars, obd);
1519
1520         rc = tgt_tunables_init(&mdt->mdt_lut);
1521         if (rc) {
1522                 CERROR("%s: failed to init target tunables: rc = %d\n",
1523                        mdt_obd_name(mdt), rc);
1524                 return rc;
1525         }
1526
1527         rc = hsm_cdt_tunables_init(mdt);
1528         if (rc) {
1529                 CERROR("%s: cannot create hsm proc entries: rc = %d\n",
1530                        mdt_obd_name(mdt), rc);
1531                 return rc;
1532         }
1533
1534         obd->obd_proc_exports_entry = proc_mkdir("exports",
1535                                                  obd->obd_proc_entry);
1536         if (obd->obd_proc_exports_entry)
1537                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
1538                                    obd, &mdt_nid_stats_clear_fops);
1539
1540         rc = lprocfs_alloc_md_stats(obd, ARRAY_SIZE(mdt_stats));
1541         if (rc)
1542                 return rc;
1543
1544         /* add additional MDT md_stats after the default ones */
1545         mdt_stats_counter_init(obd->obd_md_stats, LPROC_MD_LAST_OPC,
1546                                LPROCFS_CNTR_HISTOGRAM);
1547         rc = lprocfs_job_stats_init(obd, ARRAY_SIZE(mdt_stats),
1548                                     mdt_stats_counter_init);
1549
1550         rc = lproc_mdt_attach_rename_seqstat(mdt);
1551         if (rc)
1552                 CERROR("%s: MDT can not create rename stats rc = %d\n",
1553                        mdt_obd_name(mdt), rc);
1554
1555         RETURN(rc);
1556 }
1557
1558 void mdt_tunables_fini(struct mdt_device *mdt)
1559 {
1560         struct obd_device *obd = mdt2obd_dev(mdt);
1561
1562         if (obd->obd_proc_exports_entry != NULL) {
1563                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
1564                 obd->obd_proc_exports_entry = NULL;
1565         }
1566
1567         lprocfs_free_per_client_stats(obd);
1568         /* hsm_cdt_tunables is disabled earlier than this to avoid
1569          * coordinator restart.
1570          */
1571         hsm_cdt_tunables_fini(mdt);
1572         tgt_tunables_fini(&mdt->mdt_lut);
1573         lprocfs_obd_cleanup(obd);
1574         lprocfs_free_md_stats(obd);
1575         lprocfs_free_obd_stats(obd);
1576         lprocfs_job_stats_fini(obd);
1577 }