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