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