Whamcloud - gitweb
LU-1431 ptlrpc: PTLRPC_BRW_MAX_SIZE usage cleanup.
[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 int 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         lprocfs_job_stats_fini(obd);
267
268         if (obd->obd_proc_exports_entry) {
269                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
270                 obd->obd_proc_exports_entry = NULL;
271         }
272         lprocfs_free_per_client_stats(obd);
273         lprocfs_obd_cleanup(obd);
274         ptlrpc_lprocfs_unregister_obd(obd);
275         lprocfs_free_md_stats(obd);
276         lprocfs_free_obd_stats(obd);
277
278         RETURN(0);
279 }
280
281 static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
282                                       int count, int *eof, void *data)
283 {
284         struct obd_device *obd = data;
285         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
286
287         *eof = 1;
288         return snprintf(page, count, "%u\n",
289                         mdt->mdt_identity_cache->uc_entry_expire);
290 }
291
292 static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
293                                       unsigned long count, void *data)
294 {
295         struct obd_device *obd = data;
296         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
297         int rc, val;
298
299         rc = lprocfs_write_helper(buffer, count, &val);
300         if (rc)
301                 return rc;
302
303         mdt->mdt_identity_cache->uc_entry_expire = val;
304         return count;
305 }
306
307 static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
308                                               off_t off, int count, int *eof,
309                                               void *data)
310 {
311         struct obd_device *obd = data;
312         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
313
314         *eof = 1;
315         return snprintf(page, count, "%u\n",
316                         mdt->mdt_identity_cache->uc_acquire_expire);
317 }
318
319 static int lprocfs_wr_identity_acquire_expire(struct file *file,
320                                               const char *buffer,
321                                               unsigned long count,
322                                               void *data)
323 {
324         struct obd_device *obd = data;
325         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
326         int rc, val;
327
328         rc = lprocfs_write_helper(buffer, count, &val);
329         if (rc)
330                 return rc;
331
332         mdt->mdt_identity_cache->uc_acquire_expire = val;
333         return count;
334 }
335
336 static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
337                                       int count, int *eof, void *data)
338 {
339         struct obd_device *obd = data;
340         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
341         struct upcall_cache *hash = mdt->mdt_identity_cache;
342         int len;
343
344         *eof = 1;
345         read_lock(&hash->uc_upcall_rwlock);
346         len = snprintf(page, count, "%s\n", hash->uc_upcall);
347         read_unlock(&hash->uc_upcall_rwlock);
348         return len;
349 }
350
351 static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
352                                       unsigned long count, void *data)
353 {
354         struct obd_device *obd = data;
355         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
356         struct upcall_cache *hash = mdt->mdt_identity_cache;
357         int rc;
358         char *kernbuf;
359
360         if (count >= UC_CACHE_UPCALL_MAXPATH) {
361                 CERROR("%s: identity upcall too long\n", obd->obd_name);
362                 return -EINVAL;
363         }
364         OBD_ALLOC(kernbuf, count + 1);
365         if (kernbuf == NULL)
366                 GOTO(failed, rc = -ENOMEM);
367         if (cfs_copy_from_user(kernbuf, buffer, count))
368                 GOTO(failed, rc = -EFAULT);
369
370         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
371         write_lock(&hash->uc_upcall_rwlock);
372         sscanf(kernbuf, "%s", hash->uc_upcall);
373         write_unlock(&hash->uc_upcall_rwlock);
374
375         if (strcmp(hash->uc_name, obd->obd_name) != 0)
376                 CWARN("%s: write to upcall name %s\n",
377                       obd->obd_name, hash->uc_upcall);
378
379         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
380                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
381                       "cause unexpected \"EACCESS\"\n", obd->obd_name);
382
383         CWARN("%s: identity upcall set to %s\n", obd->obd_name, hash->uc_upcall);
384         OBD_FREE(kernbuf, count + 1);
385         RETURN(count);
386
387  failed:
388         if (kernbuf)
389                 OBD_FREE(kernbuf, count + 1);
390         RETURN(rc);
391 }
392
393 static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
394                                      unsigned long count, void *data)
395 {
396         struct obd_device *obd = data;
397         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
398         int rc, uid;
399
400         rc = lprocfs_write_helper(buffer, count, &uid);
401         if (rc)
402                 return rc;
403
404         mdt_flush_identity(mdt->mdt_identity_cache, uid);
405         return count;
406 }
407
408 static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
409                                     unsigned long count, void *data)
410 {
411         struct obd_device *obd = data;
412         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
413         struct identity_downcall_data *param;
414         int size = sizeof(*param), rc, checked = 0;
415
416 again:
417         if (count < size) {
418                 CERROR("%s: invalid data count = %lu, size = %d\n",
419                        obd->obd_name, count, size);
420                 return -EINVAL;
421         }
422
423         OBD_ALLOC(param, size);
424         if (param == NULL)
425                 return -ENOMEM;
426
427         if (cfs_copy_from_user(param, buffer, size)) {
428                 CERROR("%s: bad identity data\n", obd->obd_name);
429                 GOTO(out, rc = -EFAULT);
430         }
431
432         if (checked == 0) {
433                 checked = 1;
434                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
435                         CERROR("%s: MDS identity downcall bad params\n",
436                                obd->obd_name);
437                         GOTO(out, rc = -EINVAL);
438                 }
439
440                 if (param->idd_nperms > N_PERMS_MAX) {
441                         CERROR("%s: perm count %d more than maximum %d\n",
442                                obd->obd_name, param->idd_nperms, N_PERMS_MAX);
443                         GOTO(out, rc = -EINVAL);
444                 }
445
446                 if (param->idd_ngroups > NGROUPS_MAX) {
447                         CERROR("%s: group count %d more than maximum %d\n",
448                                obd->obd_name, param->idd_ngroups, NGROUPS_MAX);
449                         GOTO(out, rc = -EINVAL);
450                 }
451
452                 if (param->idd_ngroups) {
453                         rc = param->idd_ngroups; /* save idd_ngroups */
454                         OBD_FREE(param, size);
455                         size = offsetof(struct identity_downcall_data,
456                                         idd_groups[rc]);
457                         goto again;
458                 }
459         }
460
461         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
462                                    param->idd_uid, param);
463
464 out:
465         if (param != NULL)
466                 OBD_FREE(param, size);
467
468         return rc ? rc : count;
469 }
470
471 /* for debug only */
472 static int lprocfs_rd_capa(char *page, char **start, off_t off,
473                            int count, int *eof, void *data)
474 {
475         struct obd_device *obd = data;
476         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
477
478         return snprintf(page, count, "capability on: %s %s\n",
479                         mdt->mdt_opts.mo_oss_capa ? "oss" : "",
480                         mdt->mdt_opts.mo_mds_capa ? "mds" : "");
481 }
482
483 static int lprocfs_wr_capa(struct file *file, const char *buffer,
484                            unsigned long count, void *data)
485 {
486         struct obd_device *obd = data;
487         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
488         int val, rc;
489
490         rc = lprocfs_write_helper(buffer, count, &val);
491         if (rc)
492                 return rc;
493
494         if (val < 0 || val > 3) {
495                 CERROR("invalid capability mode, only 0/2/3 is accepted.\n"
496                        " 0:  disable fid capability\n"
497                        " 2:  enable MDS fid capability\n"
498                        " 3:  enable both MDS and OSS fid capability\n");
499                 return -EINVAL;
500         }
501
502         /* OSS fid capability needs enable both MDS and OSS fid capability on
503          * MDS */
504         if (val == 1) {
505                 CERROR("can't enable OSS fid capability only, you should use "
506                        "'3' to enable both MDS and OSS fid capability.\n");
507                 return -EINVAL;
508         }
509
510         mdt->mdt_opts.mo_oss_capa = (val & 0x1);
511         mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
512         mdt->mdt_capa_conf = 1;
513         LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
514                       obd->obd_name,
515                       mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
516         LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
517                       obd->obd_name,
518                       mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
519         return count;
520 }
521
522 static int lprocfs_rd_capa_count(char *page, char **start, off_t off,
523                                  int count, int *eof, void *data)
524 {
525         return snprintf(page, count, "%d %d\n",
526                         capa_count[CAPA_SITE_CLIENT],
527                         capa_count[CAPA_SITE_SERVER]);
528 }
529
530 static int lprocfs_rd_site_stats(char *page, char **start, off_t off,
531                                  int count, int *eof, void *data)
532 {
533         struct obd_device *obd = data;
534         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
535
536         return lu_site_stats_print(mdt_lu_site(mdt), page, count);
537 }
538
539 static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
540                                    int count, int *eof, void *data)
541 {
542         struct obd_device *obd = data;
543         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
544
545         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
546 }
547
548 static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
549                                    unsigned long count, void *data)
550 {
551         struct obd_device *obd = data;
552         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
553         int val, rc;
554
555         rc = lprocfs_write_helper(buffer, count, &val);
556         if (rc)
557                 return rc;
558
559         mdt->mdt_capa_timeout = (unsigned long)val;
560         mdt->mdt_capa_conf = 1;
561         return count;
562 }
563
564 static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
565                                  int *eof, void *data)
566 {
567         struct obd_device *obd = data;
568         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
569
570         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
571 }
572
573 static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
574                                  unsigned long count, void *data)
575 {
576         struct obd_device *obd = data;
577         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
578         int val, rc;
579
580         rc = lprocfs_write_helper(buffer, count, &val);
581         if (rc)
582                 return rc;
583
584         mdt->mdt_ck_timeout = (unsigned long)val;
585         mdt->mdt_capa_conf = 1;
586         return count;
587 }
588
589 #define BUFLEN (UUID_MAX + 4)
590
591 static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
592                                        unsigned long count, void *data)
593 {
594         char *kbuf;
595         char *tmpbuf;
596
597         OBD_ALLOC(kbuf, BUFLEN);
598         if (kbuf == NULL)
599                 return -ENOMEM;
600
601         /*
602          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
603          * bytes into kbuf, to ensure that the string is NUL-terminated.
604          * UUID_MAX should include a trailing NUL already.
605          */
606         if (cfs_copy_from_user(kbuf, buffer,
607                                min_t(unsigned long, BUFLEN - 1, count))) {
608                 count = -EFAULT;
609                 goto out;
610         }
611         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
612
613         if (strncmp(tmpbuf, "nid:", 4) != 0) {
614                 count = lprocfs_wr_evict_client(file, buffer, count, data);
615                 goto out;
616         }
617
618         CERROR("NOT implement evict client by nid %s\n", tmpbuf);
619
620 out:
621         OBD_FREE(kbuf, BUFLEN);
622         return count;
623 }
624
625 #undef BUFLEN
626
627 static int lprocfs_rd_sec_level(char *page, char **start, off_t off,
628                                 int count, int *eof, void *data)
629 {
630         struct obd_device *obd = data;
631         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
632
633         return snprintf(page, count, "%d\n", mdt->mdt_sec_level);
634 }
635
636 static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
637                                 unsigned long count, void *data)
638 {
639         struct obd_device *obd = data;
640         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
641         int val, rc;
642
643         rc = lprocfs_write_helper(buffer, count, &val);
644         if (rc)
645                 return rc;
646
647         if (val > LUSTRE_SEC_ALL || val < LUSTRE_SEC_NONE)
648                 return -EINVAL;
649
650         if (val == LUSTRE_SEC_SPECIFY) {
651                 CWARN("security level %d will be supported in future.\n",
652                       LUSTRE_SEC_SPECIFY);
653                 return -EINVAL;
654         }
655
656         mdt->mdt_sec_level = val;
657         return count;
658 }
659
660 static int lprocfs_rd_cos(char *page, char **start, off_t off,
661                               int count, int *eof, void *data)
662 {
663         struct obd_device *obd = data;
664         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
665
666         return snprintf(page, count, "%u\n", mdt_cos_is_enabled(mdt));
667 }
668
669 static int lprocfs_wr_cos(struct file *file, const char *buffer,
670                                   unsigned long count, void *data)
671 {
672         struct obd_device *obd = data;
673         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
674         int val, rc;
675
676         rc = lprocfs_write_helper(buffer, count, &val);
677         if (rc)
678                 return rc;
679         mdt_enable_cos(mdt, val);
680         return count;
681 }
682
683 static int lprocfs_rd_root_squash(char *page, char **start, off_t off,
684                                   int count, int *eof, void *data)
685 {
686         struct obd_device *obd = data;
687         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
688
689         return snprintf(page, count, "%u:%u\n", mdt->mdt_squash_uid,
690                         mdt->mdt_squash_gid);
691 }
692
693 static int safe_strtoul(const char *str, char **endp, unsigned long *res)
694 {
695         char n[24];
696
697         *res = simple_strtoul(str, endp, 0);
698         if (str == *endp)
699                 return 1;
700
701         sprintf(n, "%lu", *res);
702         if (strncmp(n, str, *endp - str))
703                 /* overflow */
704                 return 1;
705         return 0;
706 }
707
708 static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
709                                   unsigned long count, void *data)
710 {
711         struct obd_device *obd = data;
712         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
713         int rc;
714         char kernbuf[50], *tmp, *end, *errmsg;
715         unsigned long uid, gid;
716         int nouid, nogid;
717         ENTRY;
718
719         if (count >= sizeof(kernbuf)) {
720                 errmsg = "string too long";
721                 GOTO(failed, rc = -EINVAL);
722         }
723         if (cfs_copy_from_user(kernbuf, buffer, count)) {
724                 errmsg = "bad address";
725                 GOTO(failed, rc = -EFAULT);
726         }
727         kernbuf[count] = '\0';
728
729         nouid = nogid = 0;
730         if (safe_strtoul(buffer, &tmp, &uid)) {
731                 uid = mdt->mdt_squash_uid;
732                 nouid = 1;
733         }
734
735         /* skip ':' */
736         if (*tmp == ':') {
737                 tmp++;
738                 if (safe_strtoul(tmp, &end, &gid)) {
739                         gid = mdt->mdt_squash_gid;
740                         nogid = 1;
741                 }
742         } else {
743                 gid = mdt->mdt_squash_gid;
744                 nogid = 1;
745         }
746
747         mdt->mdt_squash_uid = uid;
748         mdt->mdt_squash_gid = gid;
749
750         if (nouid && nogid) {
751                 errmsg = "needs uid:gid format";
752                 GOTO(failed, rc = -EINVAL);
753         }
754
755         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
756                       obd->obd_name,
757                       mdt->mdt_squash_uid,  mdt->mdt_squash_gid);
758         RETURN(count);
759
760  failed:
761         CWARN("%s: failed to set root_squash to \"%s\", %s: rc %d\n",
762               obd->obd_name, buffer, errmsg, rc);
763         RETURN(rc);
764 }
765
766 static int lprocfs_rd_nosquash_nids(char *page, char **start, off_t off,
767                                     int count, int *eof, void *data)
768 {
769         struct obd_device *obd = data;
770         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
771
772         if (mdt->mdt_nosquash_str)
773                 return snprintf(page, count, "%s\n", mdt->mdt_nosquash_str);
774         return snprintf(page, count, "NONE\n");
775 }
776
777 static int lprocfs_wr_nosquash_nids(struct file *file, const char *buffer,
778                                     unsigned long count, void *data)
779 {
780         struct obd_device *obd = data;
781         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
782         int rc;
783         char *kernbuf, *errmsg;
784         cfs_list_t tmp;
785         ENTRY;
786
787         OBD_ALLOC(kernbuf, count + 1);
788         if (kernbuf == NULL) {
789                 errmsg = "no memory";
790                 GOTO(failed, rc = -ENOMEM);
791         }
792         if (cfs_copy_from_user(kernbuf, buffer, count)) {
793                 errmsg = "bad address";
794                 GOTO(failed, rc = -EFAULT);
795         }
796         kernbuf[count] = '\0';
797
798         if (!strcmp(kernbuf, "NONE") || !strcmp(kernbuf, "clear")) {
799                 /* empty string is special case */
800                 down_write(&mdt->mdt_squash_sem);
801                 if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
802                         cfs_free_nidlist(&mdt->mdt_nosquash_nids);
803                         OBD_FREE(mdt->mdt_nosquash_str,
804                                  mdt->mdt_nosquash_strlen);
805                         mdt->mdt_nosquash_str = NULL;
806                         mdt->mdt_nosquash_strlen = 0;
807                 }
808                 up_write(&mdt->mdt_squash_sem);
809                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n",
810                               obd->obd_name);
811                 OBD_FREE(kernbuf, count + 1);
812                 RETURN(count);
813         }
814
815         CFS_INIT_LIST_HEAD(&tmp);
816         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
817                 errmsg = "can't parse";
818                 GOTO(failed, rc = -EINVAL);
819         }
820
821         down_write(&mdt->mdt_squash_sem);
822         if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
823                 cfs_free_nidlist(&mdt->mdt_nosquash_nids);
824                 OBD_FREE(mdt->mdt_nosquash_str, mdt->mdt_nosquash_strlen);
825         }
826         mdt->mdt_nosquash_str = kernbuf;
827         mdt->mdt_nosquash_strlen = count + 1;
828         cfs_list_splice(&tmp, &mdt->mdt_nosquash_nids);
829
830         LCONSOLE_INFO("%s: nosquash_nids is set to %s\n",
831                       obd->obd_name, kernbuf);
832         up_write(&mdt->mdt_squash_sem);
833         RETURN(count);
834
835  failed:
836         CWARN("%s: failed to set nosquash_nids to \"%s\", %s: rc %d\n",
837               obd->obd_name, kernbuf, errmsg, rc);
838         if (kernbuf)
839                 OBD_FREE(kernbuf, count + 1);
840         RETURN(rc);
841 }
842
843 static int lprocfs_rd_mdt_som(char *page, char **start, off_t off,
844                               int count, int *eof, void *data)
845 {
846         struct obd_device *obd = data;
847         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
848
849         return snprintf(page, count, "%sabled\n",
850                         mdt->mdt_som_conf ? "en" : "dis");
851 }
852
853 static int lprocfs_wr_mdt_som(struct file *file, const char *buffer,
854                               unsigned long count, void *data)
855 {
856         struct obd_export *exp;
857         struct obd_device *obd = data;
858         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
859         char kernbuf[16];
860         unsigned long val = 0;
861
862         if (count > (sizeof(kernbuf) - 1))
863                 return -EINVAL;
864
865         if (cfs_copy_from_user(kernbuf, buffer, count))
866                 return -EFAULT;
867
868         kernbuf[count] = '\0';
869
870         if (!strcmp(kernbuf, "enabled"))
871                 val = 1;
872         else if (strcmp(kernbuf, "disabled"))
873                 return -EINVAL;
874
875         if (mdt->mdt_som_conf == val)
876                 return count;
877
878         if (!obd->obd_process_conf) {
879                 CERROR("Temporary SOM change is not supported, use lctl "
880                        "conf_param for permanent setting\n");
881                 return count;
882         }
883
884         /* 1 stands for self export. */
885         cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
886                 if (exp == obd->obd_self_export)
887                         continue;
888                 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS)
889                         continue;
890                 /* Some clients are already connected, skip the change */
891                 LCONSOLE_INFO("%s is already connected, SOM will be %s on "
892                               "the next mount\n", exp->exp_client_uuid.uuid,
893                               val ? "enabled" : "disabled");
894                 return count;
895         }
896
897         mdt->mdt_som_conf = val;
898         LCONSOLE_INFO("Enabling SOM\n");
899
900         return count;
901 }
902
903 /* Temporary; for testing purposes only */
904 static int lprocfs_mdt_wr_mdc(struct file *file, const char *buffer,
905                               unsigned long count, void *data)
906 {
907         struct obd_device *obd = data;
908         struct obd_export *exp = NULL;
909         struct obd_uuid   *uuid;
910         char              *kbuf;
911         char              *tmpbuf;
912
913         OBD_ALLOC(kbuf, UUID_MAX);
914         if (kbuf == NULL)
915                 return -ENOMEM;
916
917         /*
918          * OBD_ALLOC() will zero kbuf, but we only copy UUID_MAX - 1
919          * bytes into kbuf, to ensure that the string is NUL-terminated.
920          * UUID_MAX should include a trailing NUL already.
921          */
922         if (cfs_copy_from_user(kbuf, buffer,
923                                min_t(unsigned long, UUID_MAX - 1, count))) {
924                 count = -EFAULT;
925                 goto out;
926         }
927         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, UUID_MAX - 1, count));
928
929         OBD_ALLOC(uuid, UUID_MAX);
930         if (uuid == NULL) {
931                 count = -ENOMEM;
932                 goto out;
933         }
934
935         obd_str2uuid(uuid, tmpbuf);
936         exp = cfs_hash_lookup(obd->obd_uuid_hash, uuid);
937         if (exp == NULL) {
938                 CERROR("%s: no export %s found\n",
939                        obd->obd_name, obd_uuid2str(uuid));
940         } else {
941                 mdt_hsm_copytool_send(exp);
942                 class_export_put(exp);
943         }
944
945         OBD_FREE(uuid, UUID_MAX);
946 out:
947         OBD_FREE(kbuf, UUID_MAX);
948         return count;
949 }
950
951 static int lprocfs_rd_enable_remote_dir(char *page, char **start, off_t off,
952                                         int count, int *eof, void *data)
953 {
954         struct obd_device *obd = data;
955         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
956
957         return snprintf(page, count, "%u\n", mdt->mdt_enable_remote_dir);
958 }
959
960 static int lprocfs_wr_enable_remote_dir(struct file *file, const char *buffer,
961                                         unsigned long count, void *data)
962 {
963         struct obd_device *obd = data;
964         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
965         __u32 val;
966         int rc;
967
968         rc = lprocfs_write_helper(buffer, count, &val);
969         if (rc)
970                 return rc;
971
972         if (val < 0 || val > 1)
973                 return -ERANGE;
974
975         mdt->mdt_enable_remote_dir = val;
976         return count;
977 }
978
979 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
980         { "uuid",                       lprocfs_rd_uuid,                 0, 0 },
981         { "recovery_status",            lprocfs_obd_rd_recovery_status,  0, 0 },
982         { "num_exports",                lprocfs_rd_num_exports,          0, 0 },
983         { "identity_expire",            lprocfs_rd_identity_expire,
984                                         lprocfs_wr_identity_expire,         0 },
985         { "identity_acquire_expire",    lprocfs_rd_identity_acquire_expire,
986                                         lprocfs_wr_identity_acquire_expire, 0 },
987         { "identity_upcall",            lprocfs_rd_identity_upcall,
988                                         lprocfs_wr_identity_upcall,         0 },
989         { "identity_flush",             0, lprocfs_wr_identity_flush,       0 },
990         { "identity_info",              0, lprocfs_wr_identity_info,        0 },
991         { "capa",                       lprocfs_rd_capa,
992                                         lprocfs_wr_capa,                    0 },
993         { "capa_timeout",               lprocfs_rd_capa_timeout,
994                                         lprocfs_wr_capa_timeout,            0 },
995         { "capa_key_timeout",           lprocfs_rd_ck_timeout,
996                                         lprocfs_wr_ck_timeout,              0 },
997         { "capa_count",                 lprocfs_rd_capa_count,           0, 0 },
998         { "site_stats",                 lprocfs_rd_site_stats,           0, 0 },
999         { "evict_client",               0, lprocfs_mdt_wr_evict_client,     0 },
1000         { "hash_stats",                 lprocfs_obd_rd_hash,    0, 0 },
1001         { "sec_level",                  lprocfs_rd_sec_level,
1002                                         lprocfs_wr_sec_level,               0 },
1003         { "commit_on_sharing",          lprocfs_rd_cos, lprocfs_wr_cos, 0 },
1004         { "root_squash",                lprocfs_rd_root_squash,
1005                                         lprocfs_wr_root_squash,             0 },
1006         { "nosquash_nids",              lprocfs_rd_nosquash_nids,
1007                                         lprocfs_wr_nosquash_nids,           0 },
1008         { "som",                        lprocfs_rd_mdt_som,
1009                                         lprocfs_wr_mdt_som, 0 },
1010         { "mdccomm",                    0, lprocfs_mdt_wr_mdc,              0 },
1011         { "instance",                   lprocfs_target_rd_instance,         0 },
1012         { "ir_factor",                  lprocfs_obd_rd_ir_factor,
1013                                         lprocfs_obd_wr_ir_factor,           0 },
1014         { "job_cleanup_interval",       lprocfs_rd_job_interval,
1015                                         lprocfs_wr_job_interval, 0 },
1016         { "enable_remote_dir",          lprocfs_rd_enable_remote_dir,
1017                                         lprocfs_wr_enable_remote_dir,       0},
1018         { 0 }
1019 };
1020
1021 static struct lprocfs_vars lprocfs_mdt_module_vars[] = {
1022         { "num_refs",                   lprocfs_rd_numrefs,              0, 0 },
1023         { 0 }
1024 };
1025
1026 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
1027 {
1028         lvars->module_vars  = lprocfs_mdt_module_vars;
1029         lvars->obd_vars     = lprocfs_mdt_obd_vars;
1030 }
1031
1032 struct lprocfs_vars lprocfs_mds_obd_vars[] = {
1033         { "uuid",        lprocfs_rd_uuid,       0, 0 },
1034         { 0 }
1035 };
1036
1037 struct lprocfs_vars lprocfs_mds_module_vars[] = {
1038         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
1039         { 0 }
1040 };
1041
1042 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
1043 {
1044         struct obd_export *exp = req->rq_export;
1045
1046         if (exp->exp_obd && exp->exp_obd->md_stats)
1047                 lprocfs_counter_incr(exp->exp_obd->md_stats, opcode);
1048         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
1049                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
1050         if (exp->exp_obd && exp->exp_obd->u.obt.obt_jobstats.ojs_hash &&
1051             (exp_connect_flags(exp) & OBD_CONNECT_JOBSTATS))
1052                 lprocfs_job_stats_log(exp->exp_obd,
1053                                       lustre_msg_get_jobid(req->rq_reqmsg),
1054                                       opcode, 1);
1055 }
1056
1057 void mdt_stats_counter_init(struct lprocfs_stats *stats)
1058 {
1059         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
1060         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
1061         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
1062         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
1063         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
1064         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
1065         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
1066         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
1067         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
1068         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
1069         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
1070         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
1071         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
1072         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
1073         lprocfs_counter_init(stats, LPROC_MDT_SAMEDIR_RENAME, 0,
1074                              "samedir_rename", "reqs");
1075         lprocfs_counter_init(stats, LPROC_MDT_CROSSDIR_RENAME, 0,
1076                              "crossdir_rename", "reqs");
1077 }