Whamcloud - gitweb
e9163ccfc9d286e104a39d50bc73deeaf12141cd
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_lproc.c
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  * Author: Fan Yong <fanyong@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <linux/version.h>
45 #include <asm/statfs.h>
46
47 #include <linux/module.h>
48
49 /* LUSTRE_VERSION_CODE */
50 #include <lustre_ver.h>
51 /*
52  * struct OBD_{ALLOC,FREE}*()
53  * MDT_FAIL_CHECK
54  */
55 #include <obd_support.h>
56 /* struct obd_export */
57 #include <lustre_export.h>
58 /* struct obd_device */
59 #include <obd.h>
60 #include <obd_class.h>
61 #include <lustre_mds.h>
62 #include <lustre_mdt.h>
63 #include <lprocfs_status.h>
64 #include "mdt_internal.h"
65 #include <lnet/lib-lnet.h>
66
67 /**
68  * The rename stats output would be YAML formats, like
69  * rename_stats:
70  * - snapshot_time: 1234567890.123456
71  * - same_dir:
72  *     4kB: { samples: 1230, pct: 33, cum_pct: 45 }
73  *     8kB: { samples: 1242, pct: 33, cum_pct: 78 }
74  *     16kB: { samples: 132, pct: 3, cum_pct: 81 }
75  * - crossdir_src:
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  * - crossdir_tgt:
80  *     4kB: { samples: 123, pct: 33, cum_pct: 45 }
81  *     8kB: { samples: 124, pct: 33, cum_pct: 78 }
82  *     16kB: { samples: 12, pct: 3, cum_pct: 81 }
83  **/
84
85 #define pct(a, b) (b ? a * 100 / b : 0)
86
87 static void display_rename_stats(struct seq_file *seq, char *name,
88                                  struct obd_histogram *hist)
89 {
90         unsigned long tot, t, cum = 0;
91         int i;
92
93         tot = lprocfs_oh_sum(hist);
94         if (tot > 0)
95                 seq_printf(seq, "- %-15s\n", name);
96         /* dir size start from 4K, start i from 10(2^10) here */
97         for (i = 0; i < OBD_HIST_MAX; i++) {
98                 t = hist->oh_buckets[i];
99                 cum += t;
100                 if (cum == 0)
101                         continue;
102
103                 if (i < 10)
104                         seq_printf(seq, "%6s%d%s", " ", 1<< i, "bytes:");
105                 else if (i < 20)
106                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-10), "KB:");
107                 else
108                         seq_printf(seq, "%6s%d%s", " ", 1<<(i-20), "MB:");
109
110                 seq_printf(seq, " { sample: %3lu, pct: %3lu, cum_pct: %3lu }\n",
111                            t, pct(t, tot), pct(cum, tot));
112
113                 if (cum == tot)
114                         break;
115         }
116 }
117
118 static void rename_stats_show(struct seq_file *seq,
119                               struct rename_stats *rename_stats)
120 {
121         struct timeval now;
122
123         /* this sampling races with updates */
124         do_gettimeofday(&now);
125         seq_printf(seq, "rename_stats:\n");
126         seq_printf(seq, "- %-15s %lu.%lu\n", "snapshot_time:",
127                    now.tv_sec, now.tv_usec);
128
129         display_rename_stats(seq, "same_dir",
130                              &rename_stats->hist[RENAME_SAMEDIR_SIZE]);
131         display_rename_stats(seq, "crossdir_src",
132                              &rename_stats->hist[RENAME_CROSSDIR_SRC_SIZE]);
133         display_rename_stats(seq, "crossdir_tgt",
134                              &rename_stats->hist[RENAME_CROSSDIR_TGT_SIZE]);
135 }
136
137 #undef pct
138
139 static int mdt_rename_stats_seq_show(struct seq_file *seq, void *v)
140 {
141         struct mdt_device *mdt = seq->private;
142
143         rename_stats_show(seq, &mdt->mdt_rename_stats);
144
145         return 0;
146 }
147
148 static ssize_t mdt_rename_stats_seq_write(struct file *file, const char *buf,
149                                           size_t len, loff_t *off)
150 {
151         struct seq_file *seq = file->private_data;
152         struct mdt_device *mdt = seq->private;
153         int i;
154
155         for (i = 0; i < RENAME_LAST; i++)
156                 lprocfs_oh_clear(&mdt->mdt_rename_stats.hist[i]);
157
158         return len;
159 }
160
161 LPROC_SEQ_FOPS(mdt_rename_stats);
162
163 static int lproc_mdt_attach_rename_seqstat(struct mdt_device *mdt)
164 {
165         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
166         struct obd_device *obd = ld->ld_obd;
167         int i;
168
169         for (i = 0; i < RENAME_LAST; i++)
170                 spin_lock_init(&mdt->mdt_rename_stats.hist[i].oh_lock);
171
172         return lprocfs_obd_seq_create(obd, "rename_stats", 0644,
173                                       &mdt_rename_stats_fops, mdt);
174 }
175
176 void mdt_rename_counter_tally(struct mdt_thread_info *info,
177                               struct mdt_device *mdt,
178                               struct ptlrpc_request *req,
179                               struct mdt_object *src,
180                               struct mdt_object *tgt)
181 {
182         struct md_attr *ma = &info->mti_attr;
183         struct rename_stats *rstats = &mdt->mdt_rename_stats;
184         int rc;
185
186         ma->ma_need = MA_INODE;
187         ma->ma_valid = 0;
188         rc = mo_attr_get(info->mti_env, mdt_object_child(src), ma);
189         if (rc) {
190                 CERROR("%s: "DFID" attr_get, rc = %d\n",
191                        req->rq_export->exp_obd->obd_name,
192                        PFID(mdt_object_fid(src)), rc);
193                 return;
194         }
195
196         if (src == tgt) {
197                 mdt_counter_incr(req, LPROC_MDT_SAMEDIR_RENAME);
198                 lprocfs_oh_tally_log2(&rstats->hist[RENAME_SAMEDIR_SIZE],
199                                       (unsigned int)ma->ma_attr.la_size);
200                 return;
201         }
202
203         mdt_counter_incr(req, LPROC_MDT_CROSSDIR_RENAME);
204         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_SRC_SIZE],
205                               (unsigned int)ma->ma_attr.la_size);
206
207         ma->ma_need = MA_INODE;
208         ma->ma_valid = 0;
209         rc = mo_attr_get(info->mti_env, mdt_object_child(tgt), ma);
210         if (rc) {
211                 CERROR("%s: "DFID" attr_get, rc = %d\n",
212                        req->rq_export->exp_obd->obd_name,
213                        PFID(mdt_object_fid(tgt)), rc);
214                 return;
215         }
216
217         lprocfs_oh_tally_log2(&rstats->hist[RENAME_CROSSDIR_TGT_SIZE],
218                               (unsigned int)ma->ma_attr.la_size);
219 }
220
221 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
222 {
223         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
224         struct obd_device *obd = ld->ld_obd;
225         struct lprocfs_static_vars lvars;
226         int rc;
227         ENTRY;
228
229         LASSERT(name != NULL);
230
231         lprocfs_mdt_init_vars(&lvars);
232         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
233         if (rc) {
234                 CERROR("Can't init lprocfs, rc %d\n", rc);
235                 return rc;
236         }
237         ptlrpc_lprocfs_register_obd(obd);
238
239         obd->obd_proc_exports_entry = proc_mkdir("exports",
240                                                  obd->obd_proc_entry);
241         if (obd->obd_proc_exports_entry)
242                 lprocfs_add_simple(obd->obd_proc_exports_entry,
243                                    "clear", lprocfs_nid_stats_clear_read,
244                                    lprocfs_nid_stats_clear_write, obd, NULL);
245         rc = lprocfs_alloc_md_stats(obd, LPROC_MDT_LAST);
246         if (rc)
247                 return rc;
248         mdt_stats_counter_init(obd->md_stats);
249
250         rc = lprocfs_job_stats_init(obd, LPROC_MDT_LAST,
251                                     mdt_stats_counter_init);
252
253         rc = lproc_mdt_attach_rename_seqstat(mdt);
254         if (rc)
255                 CERROR("%s: MDT can not create rename stats rc = %d\n",
256                        obd->obd_name, rc);
257
258         RETURN(rc);
259 }
260
261 void mdt_procfs_fini(struct mdt_device *mdt)
262 {
263         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
264         struct obd_device *obd = ld->ld_obd;
265
266         if (obd->obd_proc_exports_entry) {
267                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
268                 obd->obd_proc_exports_entry = NULL;
269         }
270         lprocfs_free_per_client_stats(obd);
271         lprocfs_obd_cleanup(obd);
272         ptlrpc_lprocfs_unregister_obd(obd);
273         lprocfs_free_md_stats(obd);
274         lprocfs_free_obd_stats(obd);
275         lprocfs_job_stats_fini(obd);
276 }
277
278 static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
279                                       int count, int *eof, void *data)
280 {
281         struct obd_device *obd = data;
282         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
283
284         *eof = 1;
285         return snprintf(page, count, "%u\n",
286                         mdt->mdt_identity_cache->uc_entry_expire);
287 }
288
289 static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
290                                       unsigned long count, void *data)
291 {
292         struct obd_device *obd = data;
293         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
294         int rc, val;
295
296         rc = lprocfs_write_helper(buffer, count, &val);
297         if (rc)
298                 return rc;
299
300         mdt->mdt_identity_cache->uc_entry_expire = val;
301         return count;
302 }
303
304 static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
305                                               off_t off, int count, int *eof,
306                                               void *data)
307 {
308         struct obd_device *obd = data;
309         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
310
311         *eof = 1;
312         return snprintf(page, count, "%u\n",
313                         mdt->mdt_identity_cache->uc_acquire_expire);
314 }
315
316 static int lprocfs_wr_identity_acquire_expire(struct file *file,
317                                               const char *buffer,
318                                               unsigned long count,
319                                               void *data)
320 {
321         struct obd_device *obd = data;
322         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
323         int rc, val;
324
325         rc = lprocfs_write_helper(buffer, count, &val);
326         if (rc)
327                 return rc;
328
329         mdt->mdt_identity_cache->uc_acquire_expire = val;
330         return count;
331 }
332
333 static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
334                                       int count, int *eof, void *data)
335 {
336         struct obd_device *obd = data;
337         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
338         struct upcall_cache *hash = mdt->mdt_identity_cache;
339         int len;
340
341         *eof = 1;
342         read_lock(&hash->uc_upcall_rwlock);
343         len = snprintf(page, count, "%s\n", hash->uc_upcall);
344         read_unlock(&hash->uc_upcall_rwlock);
345         return len;
346 }
347
348 static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
349                                       unsigned long count, void *data)
350 {
351         struct obd_device *obd = data;
352         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
353         struct upcall_cache *hash = mdt->mdt_identity_cache;
354         int rc;
355         char *kernbuf;
356
357         if (count >= UC_CACHE_UPCALL_MAXPATH) {
358                 CERROR("%s: identity upcall too long\n", obd->obd_name);
359                 return -EINVAL;
360         }
361         OBD_ALLOC(kernbuf, count + 1);
362         if (kernbuf == NULL)
363                 GOTO(failed, rc = -ENOMEM);
364         if (cfs_copy_from_user(kernbuf, buffer, count))
365                 GOTO(failed, rc = -EFAULT);
366
367         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
368         write_lock(&hash->uc_upcall_rwlock);
369         sscanf(kernbuf, "%s", hash->uc_upcall);
370         write_unlock(&hash->uc_upcall_rwlock);
371
372         if (strcmp(hash->uc_name, obd->obd_name) != 0)
373                 CWARN("%s: write to upcall name %s\n",
374                       obd->obd_name, hash->uc_upcall);
375
376         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
377                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
378                       "cause unexpected \"EACCESS\"\n", obd->obd_name);
379
380         CWARN("%s: identity upcall set to %s\n", obd->obd_name, hash->uc_upcall);
381         OBD_FREE(kernbuf, count + 1);
382         RETURN(count);
383
384  failed:
385         if (kernbuf)
386                 OBD_FREE(kernbuf, count + 1);
387         RETURN(rc);
388 }
389
390 static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
391                                      unsigned long count, void *data)
392 {
393         struct obd_device *obd = data;
394         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
395         int rc, uid;
396
397         rc = lprocfs_write_helper(buffer, count, &uid);
398         if (rc)
399                 return rc;
400
401         mdt_flush_identity(mdt->mdt_identity_cache, uid);
402         return count;
403 }
404
405 static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
406                                     unsigned long count, void *data)
407 {
408         struct obd_device *obd = data;
409         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
410         struct identity_downcall_data *param;
411         int size = sizeof(*param), rc, checked = 0;
412
413 again:
414         if (count < size) {
415                 CERROR("%s: invalid data count = %lu, size = %d\n",
416                        obd->obd_name, count, size);
417                 return -EINVAL;
418         }
419
420         OBD_ALLOC(param, size);
421         if (param == NULL)
422                 return -ENOMEM;
423
424         if (cfs_copy_from_user(param, buffer, size)) {
425                 CERROR("%s: bad identity data\n", obd->obd_name);
426                 GOTO(out, rc = -EFAULT);
427         }
428
429         if (checked == 0) {
430                 checked = 1;
431                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
432                         CERROR("%s: MDS identity downcall bad params\n",
433                                obd->obd_name);
434                         GOTO(out, rc = -EINVAL);
435                 }
436
437                 if (param->idd_nperms > N_PERMS_MAX) {
438                         CERROR("%s: perm count %d more than maximum %d\n",
439                                obd->obd_name, param->idd_nperms, N_PERMS_MAX);
440                         GOTO(out, rc = -EINVAL);
441                 }
442
443                 if (param->idd_ngroups > NGROUPS_MAX) {
444                         CERROR("%s: group count %d more than maximum %d\n",
445                                obd->obd_name, param->idd_ngroups, NGROUPS_MAX);
446                         GOTO(out, rc = -EINVAL);
447                 }
448
449                 if (param->idd_ngroups) {
450                         rc = param->idd_ngroups; /* save idd_ngroups */
451                         OBD_FREE(param, size);
452                         size = offsetof(struct identity_downcall_data,
453                                         idd_groups[rc]);
454                         goto again;
455                 }
456         }
457
458         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
459                                    param->idd_uid, param);
460
461 out:
462         if (param != NULL)
463                 OBD_FREE(param, size);
464
465         return rc ? rc : count;
466 }
467
468 /* for debug only */
469 static int lprocfs_rd_capa(char *page, char **start, off_t off,
470                            int count, int *eof, void *data)
471 {
472         struct obd_device *obd = data;
473         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
474
475         return snprintf(page, count, "capability on: %s %s\n",
476                         mdt->mdt_opts.mo_oss_capa ? "oss" : "",
477                         mdt->mdt_opts.mo_mds_capa ? "mds" : "");
478 }
479
480 static int lprocfs_wr_capa(struct file *file, const char *buffer,
481                            unsigned long count, void *data)
482 {
483         struct obd_device *obd = data;
484         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
485         int val, rc;
486
487         rc = lprocfs_write_helper(buffer, count, &val);
488         if (rc)
489                 return rc;
490
491         if (val < 0 || val > 3) {
492                 CERROR("invalid capability mode, only 0/2/3 is accepted.\n"
493                        " 0:  disable fid capability\n"
494                        " 2:  enable MDS fid capability\n"
495                        " 3:  enable both MDS and OSS fid capability\n");
496                 return -EINVAL;
497         }
498
499         /* OSS fid capability needs enable both MDS and OSS fid capability on
500          * MDS */
501         if (val == 1) {
502                 CERROR("can't enable OSS fid capability only, you should use "
503                        "'3' to enable both MDS and OSS fid capability.\n");
504                 return -EINVAL;
505         }
506
507         mdt->mdt_opts.mo_oss_capa = (val & 0x1);
508         mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
509         mdt->mdt_capa_conf = 1;
510         LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
511                       obd->obd_name,
512                       mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
513         LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
514                       obd->obd_name,
515                       mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
516         return count;
517 }
518
519 static int lprocfs_rd_capa_count(char *page, char **start, off_t off,
520                                  int count, int *eof, void *data)
521 {
522         return snprintf(page, count, "%d %d\n",
523                         capa_count[CAPA_SITE_CLIENT],
524                         capa_count[CAPA_SITE_SERVER]);
525 }
526
527 static int lprocfs_rd_site_stats(char *page, char **start, off_t off,
528                                  int count, int *eof, void *data)
529 {
530         struct obd_device *obd = data;
531         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
532
533         return lu_site_stats_print(mdt_lu_site(mdt), page, count);
534 }
535
536 static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
537                                    int count, int *eof, void *data)
538 {
539         struct obd_device *obd = data;
540         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
541
542         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
543 }
544
545 static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
546                                    unsigned long count, void *data)
547 {
548         struct obd_device *obd = data;
549         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
550         int val, rc;
551
552         rc = lprocfs_write_helper(buffer, count, &val);
553         if (rc)
554                 return rc;
555
556         mdt->mdt_capa_timeout = (unsigned long)val;
557         mdt->mdt_capa_conf = 1;
558         return count;
559 }
560
561 static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
562                                  int *eof, void *data)
563 {
564         struct obd_device *obd = data;
565         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
566
567         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
568 }
569
570 static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
571                                  unsigned long count, void *data)
572 {
573         struct obd_device *obd = data;
574         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
575         int val, rc;
576
577         rc = lprocfs_write_helper(buffer, count, &val);
578         if (rc)
579                 return rc;
580
581         mdt->mdt_ck_timeout = (unsigned long)val;
582         mdt->mdt_capa_conf = 1;
583         return count;
584 }
585
586 #define BUFLEN (UUID_MAX + 4)
587
588 static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
589                                        unsigned long count, void *data)
590 {
591         char *kbuf;
592         char *tmpbuf;
593
594         OBD_ALLOC(kbuf, BUFLEN);
595         if (kbuf == NULL)
596                 return -ENOMEM;
597
598         /*
599          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
600          * bytes into kbuf, to ensure that the string is NUL-terminated.
601          * UUID_MAX should include a trailing NUL already.
602          */
603         if (cfs_copy_from_user(kbuf, buffer,
604                                min_t(unsigned long, BUFLEN - 1, count))) {
605                 count = -EFAULT;
606                 goto out;
607         }
608         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
609
610         if (strncmp(tmpbuf, "nid:", 4) != 0) {
611                 count = lprocfs_wr_evict_client(file, buffer, count, data);
612                 goto out;
613         }
614
615         CERROR("NOT implement evict client by nid %s\n", tmpbuf);
616
617 out:
618         OBD_FREE(kbuf, BUFLEN);
619         return count;
620 }
621
622 #undef BUFLEN
623
624 static int lprocfs_rd_sec_level(char *page, char **start, off_t off,
625                                 int count, int *eof, void *data)
626 {
627         struct obd_device *obd = data;
628         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
629
630         return snprintf(page, count, "%d\n", mdt->mdt_sec_level);
631 }
632
633 static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
634                                 unsigned long count, void *data)
635 {
636         struct obd_device *obd = data;
637         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
638         int val, rc;
639
640         rc = lprocfs_write_helper(buffer, count, &val);
641         if (rc)
642                 return rc;
643
644         if (val > LUSTRE_SEC_ALL || val < LUSTRE_SEC_NONE)
645                 return -EINVAL;
646
647         if (val == LUSTRE_SEC_SPECIFY) {
648                 CWARN("security level %d will be supported in future.\n",
649                       LUSTRE_SEC_SPECIFY);
650                 return -EINVAL;
651         }
652
653         mdt->mdt_sec_level = val;
654         return count;
655 }
656
657 static int lprocfs_rd_cos(char *page, char **start, off_t off,
658                               int count, int *eof, void *data)
659 {
660         struct obd_device *obd = data;
661         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
662
663         return snprintf(page, count, "%u\n", mdt_cos_is_enabled(mdt));
664 }
665
666 static int lprocfs_wr_cos(struct file *file, const char *buffer,
667                                   unsigned long count, void *data)
668 {
669         struct obd_device *obd = data;
670         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
671         int val, rc;
672
673         rc = lprocfs_write_helper(buffer, count, &val);
674         if (rc)
675                 return rc;
676         mdt_enable_cos(mdt, val);
677         return count;
678 }
679
680 static int lprocfs_rd_root_squash(char *page, char **start, off_t off,
681                                   int count, int *eof, void *data)
682 {
683         struct obd_device *obd = data;
684         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
685
686         return snprintf(page, count, "%u:%u\n", mdt->mdt_squash_uid,
687                         mdt->mdt_squash_gid);
688 }
689
690 static int safe_strtoul(const char *str, char **endp, unsigned long *res)
691 {
692         char n[24];
693
694         *res = simple_strtoul(str, endp, 0);
695         if (str == *endp)
696                 return 1;
697
698         sprintf(n, "%lu", *res);
699         if (strncmp(n, str, *endp - str))
700                 /* overflow */
701                 return 1;
702         return 0;
703 }
704
705 static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
706                                   unsigned long count, void *data)
707 {
708         struct obd_device *obd = data;
709         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
710         int rc;
711         char kernbuf[50], *tmp, *end, *errmsg;
712         unsigned long uid, gid;
713         int nouid, nogid;
714         ENTRY;
715
716         if (count >= sizeof(kernbuf)) {
717                 errmsg = "string too long";
718                 GOTO(failed, rc = -EINVAL);
719         }
720         if (cfs_copy_from_user(kernbuf, buffer, count)) {
721                 errmsg = "bad address";
722                 GOTO(failed, rc = -EFAULT);
723         }
724         kernbuf[count] = '\0';
725
726         nouid = nogid = 0;
727         if (safe_strtoul(buffer, &tmp, &uid)) {
728                 uid = mdt->mdt_squash_uid;
729                 nouid = 1;
730         }
731
732         /* skip ':' */
733         if (*tmp == ':') {
734                 tmp++;
735                 if (safe_strtoul(tmp, &end, &gid)) {
736                         gid = mdt->mdt_squash_gid;
737                         nogid = 1;
738                 }
739         } else {
740                 gid = mdt->mdt_squash_gid;
741                 nogid = 1;
742         }
743
744         mdt->mdt_squash_uid = uid;
745         mdt->mdt_squash_gid = gid;
746
747         if (nouid && nogid) {
748                 errmsg = "needs uid:gid format";
749                 GOTO(failed, rc = -EINVAL);
750         }
751
752         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
753                       obd->obd_name,
754                       mdt->mdt_squash_uid,  mdt->mdt_squash_gid);
755         RETURN(count);
756
757  failed:
758         CWARN("%s: failed to set root_squash to \"%s\", %s: rc %d\n",
759               obd->obd_name, buffer, errmsg, rc);
760         RETURN(rc);
761 }
762
763 static int lprocfs_rd_nosquash_nids(char *page, char **start, off_t off,
764                                     int count, int *eof, void *data)
765 {
766         struct obd_device *obd = data;
767         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
768
769         if (mdt->mdt_nosquash_str)
770                 return snprintf(page, count, "%s\n", mdt->mdt_nosquash_str);
771         return snprintf(page, count, "NONE\n");
772 }
773
774 static int lprocfs_wr_nosquash_nids(struct file *file, const char *buffer,
775                                     unsigned long count, void *data)
776 {
777         struct obd_device *obd = data;
778         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
779         int rc;
780         char *kernbuf, *errmsg;
781         cfs_list_t tmp;
782         ENTRY;
783
784         OBD_ALLOC(kernbuf, count + 1);
785         if (kernbuf == NULL) {
786                 errmsg = "no memory";
787                 GOTO(failed, rc = -ENOMEM);
788         }
789         if (cfs_copy_from_user(kernbuf, buffer, count)) {
790                 errmsg = "bad address";
791                 GOTO(failed, rc = -EFAULT);
792         }
793         kernbuf[count] = '\0';
794
795         if (!strcmp(kernbuf, "NONE") || !strcmp(kernbuf, "clear")) {
796                 /* empty string is special case */
797                 down_write(&mdt->mdt_squash_sem);
798                 if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
799                         cfs_free_nidlist(&mdt->mdt_nosquash_nids);
800                         OBD_FREE(mdt->mdt_nosquash_str,
801                                  mdt->mdt_nosquash_strlen);
802                         mdt->mdt_nosquash_str = NULL;
803                         mdt->mdt_nosquash_strlen = 0;
804                 }
805                 up_write(&mdt->mdt_squash_sem);
806                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n",
807                               obd->obd_name);
808                 OBD_FREE(kernbuf, count + 1);
809                 RETURN(count);
810         }
811
812         CFS_INIT_LIST_HEAD(&tmp);
813         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
814                 errmsg = "can't parse";
815                 GOTO(failed, rc = -EINVAL);
816         }
817
818         down_write(&mdt->mdt_squash_sem);
819         if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
820                 cfs_free_nidlist(&mdt->mdt_nosquash_nids);
821                 OBD_FREE(mdt->mdt_nosquash_str, mdt->mdt_nosquash_strlen);
822         }
823         mdt->mdt_nosquash_str = kernbuf;
824         mdt->mdt_nosquash_strlen = count + 1;
825         cfs_list_splice(&tmp, &mdt->mdt_nosquash_nids);
826
827         LCONSOLE_INFO("%s: nosquash_nids is set to %s\n",
828                       obd->obd_name, kernbuf);
829         up_write(&mdt->mdt_squash_sem);
830         RETURN(count);
831
832  failed:
833         CWARN("%s: failed to set nosquash_nids to \"%s\", %s: rc %d\n",
834               obd->obd_name, kernbuf, errmsg, rc);
835         if (kernbuf)
836                 OBD_FREE(kernbuf, count + 1);
837         RETURN(rc);
838 }
839
840 static int lprocfs_rd_mdt_som(char *page, char **start, off_t off,
841                               int count, int *eof, void *data)
842 {
843         struct obd_device *obd = data;
844         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
845
846         return snprintf(page, count, "%sabled\n",
847                         mdt->mdt_som_conf ? "en" : "dis");
848 }
849
850 static int lprocfs_wr_mdt_som(struct file *file, const char *buffer,
851                               unsigned long count, void *data)
852 {
853         struct obd_export *exp;
854         struct obd_device *obd = data;
855         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
856         char kernbuf[16];
857         unsigned long val = 0;
858
859         if (count > (sizeof(kernbuf) - 1))
860                 return -EINVAL;
861
862         if (cfs_copy_from_user(kernbuf, buffer, count))
863                 return -EFAULT;
864
865         kernbuf[count] = '\0';
866
867         if (!strcmp(kernbuf, "enabled"))
868                 val = 1;
869         else if (strcmp(kernbuf, "disabled"))
870                 return -EINVAL;
871
872         if (mdt->mdt_som_conf == val)
873                 return count;
874
875         if (!obd->obd_process_conf) {
876                 CERROR("Temporary SOM change is not supported, use lctl "
877                        "conf_param for permanent setting\n");
878                 return count;
879         }
880
881         /* 1 stands for self export. */
882         cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
883                 if (exp == obd->obd_self_export)
884                         continue;
885                 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS)
886                         continue;
887                 /* Some clients are already connected, skip the change */
888                 LCONSOLE_INFO("%s is already connected, SOM will be %s on "
889                               "the next mount\n", exp->exp_client_uuid.uuid,
890                               val ? "enabled" : "disabled");
891                 return count;
892         }
893
894         mdt->mdt_som_conf = val;
895         LCONSOLE_INFO("Enabling SOM\n");
896
897         return count;
898 }
899
900 /* Temporary; for testing purposes only */
901 static int lprocfs_mdt_wr_mdc(struct file *file, const char *buffer,
902                               unsigned long count, void *data)
903 {
904         struct obd_device *obd = data;
905         struct obd_export *exp = NULL;
906         struct obd_uuid   *uuid;
907         char              *kbuf;
908         char              *tmpbuf;
909
910         OBD_ALLOC(kbuf, UUID_MAX);
911         if (kbuf == NULL)
912                 return -ENOMEM;
913
914         /*
915          * OBD_ALLOC() will zero kbuf, but we only copy UUID_MAX - 1
916          * bytes into kbuf, to ensure that the string is NUL-terminated.
917          * UUID_MAX should include a trailing NUL already.
918          */
919         if (cfs_copy_from_user(kbuf, buffer,
920                                min_t(unsigned long, UUID_MAX - 1, count))) {
921                 count = -EFAULT;
922                 goto out;
923         }
924         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, UUID_MAX - 1, count));
925
926         OBD_ALLOC(uuid, UUID_MAX);
927         if (uuid == NULL) {
928                 count = -ENOMEM;
929                 goto out;
930         }
931
932         obd_str2uuid(uuid, tmpbuf);
933         exp = cfs_hash_lookup(obd->obd_uuid_hash, uuid);
934         if (exp == NULL) {
935                 CERROR("%s: no export %s found\n",
936                        obd->obd_name, obd_uuid2str(uuid));
937         } else {
938                 mdt_hsm_copytool_send(exp);
939                 class_export_put(exp);
940         }
941
942         OBD_FREE(uuid, UUID_MAX);
943 out:
944         OBD_FREE(kbuf, UUID_MAX);
945         return count;
946 }
947
948 static int lprocfs_rd_enable_remote_dir(char *page, char **start, off_t off,
949                                         int count, int *eof, void *data)
950 {
951         struct obd_device *obd = data;
952         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
953
954         return snprintf(page, count, "%u\n", mdt->mdt_enable_remote_dir);
955 }
956
957 static int lprocfs_wr_enable_remote_dir(struct file *file, const char *buffer,
958                                         unsigned long count, void *data)
959 {
960         struct obd_device *obd = data;
961         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
962         __u32 val;
963         int rc;
964
965         rc = lprocfs_write_helper(buffer, count, &val);
966         if (rc)
967                 return rc;
968
969         if (val < 0 || val > 1)
970                 return -ERANGE;
971
972         mdt->mdt_enable_remote_dir = val;
973         return count;
974 }
975
976 static int lprocfs_rd_enable_remote_dir_gid(char *page, char **start, off_t off,
977                                             int count, int *eof, void *data)
978 {
979         struct obd_device *obd = data;
980         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
981
982         return snprintf(page, count, "%d\n",
983                         (int)mdt->mdt_enable_remote_dir_gid);
984 }
985
986 static int lprocfs_wr_enable_remote_dir_gid(struct file *file,
987                                             const char *buffer,
988                                             unsigned long count, void *data)
989 {
990         struct obd_device *obd = data;
991         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
992         __u32 val;
993         int rc;
994
995         rc = lprocfs_write_helper(buffer, count, &val);
996         if (rc)
997                 return rc;
998
999         mdt->mdt_enable_remote_dir_gid = val;
1000         return count;
1001 }
1002
1003 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
1004         { "uuid",                       lprocfs_rd_uuid,                 0, 0 },
1005         { "recovery_status",            lprocfs_obd_rd_recovery_status,  0, 0 },
1006         { "num_exports",                lprocfs_rd_num_exports,          0, 0 },
1007         { "identity_expire",            lprocfs_rd_identity_expire,
1008                                         lprocfs_wr_identity_expire,         0 },
1009         { "identity_acquire_expire",    lprocfs_rd_identity_acquire_expire,
1010                                         lprocfs_wr_identity_acquire_expire, 0 },
1011         { "identity_upcall",            lprocfs_rd_identity_upcall,
1012                                         lprocfs_wr_identity_upcall,         0 },
1013         { "identity_flush",             0, lprocfs_wr_identity_flush,       0 },
1014         { "identity_info",              0, lprocfs_wr_identity_info,        0 },
1015         { "capa",                       lprocfs_rd_capa,
1016                                         lprocfs_wr_capa,                    0 },
1017         { "capa_timeout",               lprocfs_rd_capa_timeout,
1018                                         lprocfs_wr_capa_timeout,            0 },
1019         { "capa_key_timeout",           lprocfs_rd_ck_timeout,
1020                                         lprocfs_wr_ck_timeout,              0 },
1021         { "capa_count",                 lprocfs_rd_capa_count,           0, 0 },
1022         { "site_stats",                 lprocfs_rd_site_stats,           0, 0 },
1023         { "evict_client",               0, lprocfs_mdt_wr_evict_client,     0 },
1024         { "hash_stats",                 lprocfs_obd_rd_hash,    0, 0 },
1025         { "sec_level",                  lprocfs_rd_sec_level,
1026                                         lprocfs_wr_sec_level,               0 },
1027         { "commit_on_sharing",          lprocfs_rd_cos, lprocfs_wr_cos, 0 },
1028         { "root_squash",                lprocfs_rd_root_squash,
1029                                         lprocfs_wr_root_squash,             0 },
1030         { "nosquash_nids",              lprocfs_rd_nosquash_nids,
1031                                         lprocfs_wr_nosquash_nids,           0 },
1032         { "som",                        lprocfs_rd_mdt_som,
1033                                         lprocfs_wr_mdt_som, 0 },
1034         { "mdccomm",                    0, lprocfs_mdt_wr_mdc,              0 },
1035         { "instance",                   lprocfs_target_rd_instance,         0 },
1036         { "ir_factor",                  lprocfs_obd_rd_ir_factor,
1037                                         lprocfs_obd_wr_ir_factor,           0 },
1038         { "job_cleanup_interval",       lprocfs_rd_job_interval,
1039                                         lprocfs_wr_job_interval, 0 },
1040         { "enable_remote_dir",          lprocfs_rd_enable_remote_dir,
1041                                         lprocfs_wr_enable_remote_dir,       0},
1042         { "enable_remote_dir_gid",      lprocfs_rd_enable_remote_dir_gid,
1043                                         lprocfs_wr_enable_remote_dir_gid,   0},
1044         { 0 }
1045 };
1046
1047 static struct lprocfs_vars lprocfs_mdt_module_vars[] = {
1048         { "num_refs",                   lprocfs_rd_numrefs,              0, 0 },
1049         { 0 }
1050 };
1051
1052 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
1053 {
1054         lvars->module_vars  = lprocfs_mdt_module_vars;
1055         lvars->obd_vars     = lprocfs_mdt_obd_vars;
1056 }
1057
1058 struct lprocfs_vars lprocfs_mds_obd_vars[] = {
1059         { "uuid",        lprocfs_rd_uuid,       0, 0 },
1060         { 0 }
1061 };
1062
1063 struct lprocfs_vars lprocfs_mds_module_vars[] = {
1064         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
1065         { 0 }
1066 };
1067
1068 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
1069 {
1070         struct obd_export *exp = req->rq_export;
1071
1072         if (exp->exp_obd && exp->exp_obd->md_stats)
1073                 lprocfs_counter_incr(exp->exp_obd->md_stats, opcode);
1074         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1075                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
1076         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
1077             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1078                 lprocfs_job_stats_log(exp->exp_obd,
1079                                       lustre_msg_get_jobid(req->rq_reqmsg),
1080                                       opcode, 1);
1081 }
1082
1083 void mdt_stats_counter_init(struct lprocfs_stats *stats)
1084 {
1085         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
1086         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
1087         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
1088         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
1089         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
1090         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
1091         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
1092         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
1093         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
1094         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
1095         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
1096         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
1097         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
1098         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
1099         lprocfs_counter_init(stats, LPROC_MDT_SAMEDIR_RENAME, 0,
1100                              "samedir_rename", "reqs");
1101         lprocfs_counter_init(stats, LPROC_MDT_CROSSDIR_RENAME, 0,
1102                              "crossdir_rename", "reqs");
1103 }