Whamcloud - gitweb
LU-365 Update copyright for files modified by Whamcloud
[fs/lustre-release.git] / lustre / mdt / mdt_lproc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/mdt/mdt_lproc.c
40  *
41  * Author: Lai Siyao <lsy@clusterfs.com>
42  * Author: Fan Yong <fanyong@clusterfs.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_MDS
49
50 #include <linux/version.h>
51 #include <asm/statfs.h>
52
53 #include <linux/module.h>
54
55 /* LUSTRE_VERSION_CODE */
56 #include <lustre_ver.h>
57 /*
58  * struct OBD_{ALLOC,FREE}*()
59  * MDT_FAIL_CHECK
60  */
61 #include <obd_support.h>
62 /* struct obd_export */
63 #include <lustre_export.h>
64 /* struct obd_device */
65 #include <obd.h>
66 #include <obd_class.h>
67 #include <lustre_mds.h>
68 #include <lustre_mdt.h>
69 #include <lprocfs_status.h>
70 #include <lu_time.h>
71 #include "mdt_internal.h"
72 #include <lnet/lib-lnet.h>
73
74 enum {
75         LPROC_MDT_NR
76 };
77 static const char *mdt_proc_names[LPROC_MDT_NR] = {
78 };
79
80 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
81 {
82         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
83         struct obd_device *obd = ld->ld_obd;
84         struct lprocfs_static_vars lvars;
85         int rc;
86         ENTRY;
87
88         LASSERT(name != NULL);
89
90         lprocfs_mdt_init_vars(&lvars);
91         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
92         if (rc) {
93                 CERROR("Can't init lprocfs, rc %d\n", rc);
94                 return rc;
95         }
96         ptlrpc_lprocfs_register_obd(obd);
97
98         mdt->mdt_proc_entry = obd->obd_proc_entry;
99         LASSERT(mdt->mdt_proc_entry != NULL);
100
101         rc = lu_time_init(&mdt->mdt_stats, mdt->mdt_proc_entry,
102                           mdt_proc_names, ARRAY_SIZE(mdt_proc_names));
103         if (rc == 0)
104                 rc = lu_time_named_init(&ld->ld_site->ls_time_stats,
105                                         "site_time", mdt->mdt_proc_entry,
106                                          lu_time_names,
107                                          ARRAY_SIZE(lu_time_names));
108         if (rc)
109                 return rc;
110
111         obd->obd_proc_exports_entry = proc_mkdir("exports",
112                                                  obd->obd_proc_entry);
113         if (obd->obd_proc_exports_entry)
114                 lprocfs_add_simple(obd->obd_proc_exports_entry,
115                                    "clear", lprocfs_nid_stats_clear_read,
116                                    lprocfs_nid_stats_clear_write, obd, NULL);
117         rc = lprocfs_alloc_md_stats(obd, LPROC_MDT_LAST);
118         if (rc == 0)
119                 mdt_stats_counter_init(obd->md_stats);
120
121         RETURN(rc);
122 }
123
124 int mdt_procfs_fini(struct mdt_device *mdt)
125 {
126         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
127         struct obd_device *obd = ld->ld_obd;
128
129         if (mdt->mdt_proc_entry) {
130                 lu_time_fini(&ld->ld_site->ls_time_stats);
131                 lu_time_fini(&mdt->mdt_stats);
132                 mdt->mdt_proc_entry = NULL;
133         }
134         if (obd->obd_proc_exports_entry) {
135                 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
136                 obd->obd_proc_exports_entry = NULL;
137         }
138         ptlrpc_lprocfs_unregister_obd(obd);
139         lprocfs_free_md_stats(obd);
140         lprocfs_obd_cleanup(obd);
141
142         RETURN(0);
143 }
144
145 void mdt_time_start(const struct mdt_thread_info *info)
146 {
147         lu_lprocfs_time_start(info->mti_env);
148 }
149
150 void mdt_time_end(const struct mdt_thread_info *info, int idx)
151 {
152         lu_lprocfs_time_end(info->mti_env, info->mti_mdt->mdt_stats, idx);
153 }
154
155 static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
156                                       int count, int *eof, void *data)
157 {
158         struct obd_device *obd = data;
159         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
160
161         *eof = 1;
162         return snprintf(page, count, "%u\n",
163                         mdt->mdt_identity_cache->uc_entry_expire);
164 }
165
166 static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
167                                       unsigned long count, void *data)
168 {
169         struct obd_device *obd = data;
170         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
171         int rc, val;
172
173         rc = lprocfs_write_helper(buffer, count, &val);
174         if (rc)
175                 return rc;
176
177         mdt->mdt_identity_cache->uc_entry_expire = val;
178         return count;
179 }
180
181 static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
182                                               off_t off, int count, int *eof,
183                                               void *data)
184 {
185         struct obd_device *obd = data;
186         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
187
188         *eof = 1;
189         return snprintf(page, count, "%u\n",
190                         mdt->mdt_identity_cache->uc_acquire_expire);
191 }
192
193 static int lprocfs_wr_identity_acquire_expire(struct file *file,
194                                               const char *buffer,
195                                               unsigned long count,
196                                               void *data)
197 {
198         struct obd_device *obd = data;
199         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
200         int rc, val;
201
202         rc = lprocfs_write_helper(buffer, count, &val);
203         if (rc)
204                 return rc;
205
206         mdt->mdt_identity_cache->uc_acquire_expire = val;
207         return count;
208 }
209
210 static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
211                                       int count, int *eof, void *data)
212 {
213         struct obd_device *obd = data;
214         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
215         struct upcall_cache *hash = mdt->mdt_identity_cache;
216         int len;
217
218         *eof = 1;
219         cfs_read_lock(&hash->uc_upcall_rwlock);
220         len = snprintf(page, count, "%s\n", hash->uc_upcall);
221         cfs_read_unlock(&hash->uc_upcall_rwlock);
222         return len;
223 }
224
225 static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
226                                       unsigned long count, void *data)
227 {
228         struct obd_device *obd = data;
229         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
230         struct upcall_cache *hash = mdt->mdt_identity_cache;
231         int rc;
232         char *kernbuf;
233
234         if (count >= UC_CACHE_UPCALL_MAXPATH) {
235                 CERROR("%s: identity upcall too long\n", obd->obd_name);
236                 return -EINVAL;
237         }
238         OBD_ALLOC(kernbuf, count + 1);
239         if (kernbuf == NULL)
240                 GOTO(failed, rc = -ENOMEM);
241         if (cfs_copy_from_user(kernbuf, buffer, count))
242                 GOTO(failed, rc = -EFAULT);
243
244         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
245         cfs_write_lock(&hash->uc_upcall_rwlock);
246         sscanf(kernbuf, "%s", hash->uc_upcall);
247         cfs_write_unlock(&hash->uc_upcall_rwlock);
248
249         if (strcmp(hash->uc_name, obd->obd_name) != 0)
250                 CWARN("%s: write to upcall name %s\n",
251                       obd->obd_name, hash->uc_upcall);
252
253         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
254                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
255                       "cause unexpected \"EACCESS\"\n", obd->obd_name);
256
257         CWARN("%s: identity upcall set to %s\n", obd->obd_name, hash->uc_upcall);
258         OBD_FREE(kernbuf, count + 1);
259         RETURN(count);
260
261  failed:
262         if (kernbuf)
263                 OBD_FREE(kernbuf, count + 1);
264         RETURN(rc);
265 }
266
267 static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
268                                      unsigned long count, void *data)
269 {
270         struct obd_device *obd = data;
271         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
272         int rc, uid;
273
274         rc = lprocfs_write_helper(buffer, count, &uid);
275         if (rc)
276                 return rc;
277
278         mdt_flush_identity(mdt->mdt_identity_cache, uid);
279         return count;
280 }
281
282 static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
283                                     unsigned long count, void *data)
284 {
285         struct obd_device *obd = data;
286         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
287         struct identity_downcall_data *param;
288         int size = sizeof(*param), rc, checked = 0;
289
290 again:
291         if (count < size) {
292                 CERROR("%s: invalid data count = %lu, size = %d\n",
293                        obd->obd_name, count, size);
294                 return -EINVAL;
295         }
296
297         OBD_ALLOC(param, size);
298         if (param == NULL)
299                 return -ENOMEM;
300
301         if (cfs_copy_from_user(param, buffer, size)) {
302                 CERROR("%s: bad identity data\n", obd->obd_name);
303                 GOTO(out, rc = -EFAULT);
304         }
305
306         if (checked == 0) {
307                 checked = 1;
308                 if (param->idd_magic != IDENTITY_DOWNCALL_MAGIC) {
309                         CERROR("%s: MDS identity downcall bad params\n",
310                                obd->obd_name);
311                         GOTO(out, rc = -EINVAL);
312                 }
313
314                 if (param->idd_nperms > N_PERMS_MAX) {
315                         CERROR("%s: perm count %d more than maximum %d\n",
316                                obd->obd_name, param->idd_nperms, N_PERMS_MAX);
317                         GOTO(out, rc = -EINVAL);
318                 }
319
320                 if (param->idd_ngroups > NGROUPS_MAX) {
321                         CERROR("%s: group count %d more than maximum %d\n",
322                                obd->obd_name, param->idd_ngroups, NGROUPS_MAX);
323                         GOTO(out, rc = -EINVAL);
324                 }
325
326                 if (param->idd_ngroups) {
327                         rc = param->idd_ngroups; /* save idd_ngroups */
328                         OBD_FREE(param, size);
329                         size = offsetof(struct identity_downcall_data,
330                                         idd_groups[rc]);
331                         goto again;
332                 }
333         }
334
335         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
336                                    param->idd_uid, param);
337
338 out:
339         if (param != NULL)
340                 OBD_FREE(param, size);
341
342         return rc ? rc : count;
343 }
344
345 /* for debug only */
346 static int lprocfs_rd_capa(char *page, char **start, off_t off,
347                            int count, int *eof, void *data)
348 {
349         struct obd_device *obd = data;
350         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
351
352         return snprintf(page, count, "capability on: %s %s\n",
353                         mdt->mdt_opts.mo_oss_capa ? "oss" : "",
354                         mdt->mdt_opts.mo_mds_capa ? "mds" : "");
355 }
356
357 static int lprocfs_wr_capa(struct file *file, const char *buffer,
358                            unsigned long count, void *data)
359 {
360         struct obd_device *obd = data;
361         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
362         int val, rc;
363
364         rc = lprocfs_write_helper(buffer, count, &val);
365         if (rc)
366                 return rc;
367
368         if (val < 0 || val > 3) {
369                 CERROR("invalid capability mode, only 0/2/3 is accepted.\n"
370                        " 0:  disable fid capability\n"
371                        " 2:  enable MDS fid capability\n"
372                        " 3:  enable both MDS and OSS fid capability\n");
373                 return -EINVAL;
374         }
375
376         /* OSS fid capability needs enable both MDS and OSS fid capability on
377          * MDS */
378         if (val == 1) {
379                 CERROR("can't enable OSS fid capability only, you should use "
380                        "'3' to enable both MDS and OSS fid capability.\n");
381                 return -EINVAL;
382         }
383
384         mdt->mdt_opts.mo_oss_capa = (val & 0x1);
385         mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
386         mdt->mdt_capa_conf = 1;
387         LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
388                       obd->obd_name,
389                       mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
390         LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
391                       obd->obd_name,
392                       mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
393         return count;
394 }
395
396 static int lprocfs_rd_capa_count(char *page, char **start, off_t off,
397                                  int count, int *eof, void *data)
398 {
399         return snprintf(page, count, "%d %d\n",
400                         capa_count[CAPA_SITE_CLIENT],
401                         capa_count[CAPA_SITE_SERVER]);
402 }
403
404 static int lprocfs_rd_site_stats(char *page, char **start, off_t off,
405                                  int count, int *eof, void *data)
406 {
407         struct obd_device *obd = data;
408         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
409
410         return lu_site_stats_print(mdt_lu_site(mdt), page, count);
411 }
412
413 static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
414                                    int count, int *eof, void *data)
415 {
416         struct obd_device *obd = data;
417         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
418
419         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
420 }
421
422 static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
423                                    unsigned long count, void *data)
424 {
425         struct obd_device *obd = data;
426         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
427         int val, rc;
428
429         rc = lprocfs_write_helper(buffer, count, &val);
430         if (rc)
431                 return rc;
432
433         mdt->mdt_capa_timeout = (unsigned long)val;
434         mdt->mdt_capa_conf = 1;
435         return count;
436 }
437
438 static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
439                                  int *eof, void *data)
440 {
441         struct obd_device *obd = data;
442         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
443
444         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
445 }
446
447 static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
448                                  unsigned long count, void *data)
449 {
450         struct obd_device *obd = data;
451         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
452         int val, rc;
453
454         rc = lprocfs_write_helper(buffer, count, &val);
455         if (rc)
456                 return rc;
457
458         mdt->mdt_ck_timeout = (unsigned long)val;
459         mdt->mdt_capa_conf = 1;
460         return count;
461 }
462
463 static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
464                                        unsigned long count, void *data)
465 {
466         char tmpbuf[sizeof(struct obd_uuid)];
467
468         sscanf(buffer, "%40s", tmpbuf);
469
470         if (strncmp(tmpbuf, "nid:", 4) != 0)
471                 return lprocfs_wr_evict_client(file, buffer, count, data);
472
473         CERROR("NOT implement evict client by nid %s\n", tmpbuf);
474
475         return count;
476 }
477
478 static int lprocfs_rd_sec_level(char *page, char **start, off_t off,
479                                 int count, int *eof, void *data)
480 {
481         struct obd_device *obd = data;
482         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
483
484         return snprintf(page, count, "%d\n", mdt->mdt_sec_level);
485 }
486
487 static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
488                                 unsigned long count, void *data)
489 {
490         struct obd_device *obd = data;
491         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
492         int val, rc;
493
494         rc = lprocfs_write_helper(buffer, count, &val);
495         if (rc)
496                 return rc;
497
498         if (val > LUSTRE_SEC_ALL || val < LUSTRE_SEC_NONE)
499                 return -EINVAL;
500
501         if (val == LUSTRE_SEC_SPECIFY) {
502                 CWARN("security level %d will be supported in future.\n",
503                       LUSTRE_SEC_SPECIFY);
504                 return -EINVAL;
505         }
506
507         mdt->mdt_sec_level = val;
508         return count;
509 }
510
511 static int lprocfs_rd_cos(char *page, char **start, off_t off,
512                               int count, int *eof, void *data)
513 {
514         struct obd_device *obd = data;
515         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
516
517         return snprintf(page, count, "%u\n", mdt_cos_is_enabled(mdt));
518 }
519
520 static int lprocfs_wr_cos(struct file *file, const char *buffer,
521                                   unsigned long count, void *data)
522 {
523         struct obd_device *obd = data;
524         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
525         int val, rc;
526
527         rc = lprocfs_write_helper(buffer, count, &val);
528         if (rc)
529                 return rc;
530         mdt_enable_cos(mdt, val);
531         return count;
532 }
533
534 static int lprocfs_rd_root_squash(char *page, char **start, off_t off,
535                                   int count, int *eof, void *data)
536 {
537         struct obd_device *obd = data;
538         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
539         ENTRY;
540
541         return snprintf(page, count, "%u:%u\n", mdt->mdt_squash_uid,
542                         mdt->mdt_squash_gid);
543 }
544
545 static int safe_strtoul(const char *str, char **endp, unsigned long *res)
546 {
547         char n[24];
548
549         *res = simple_strtoul(str, endp, 0);
550         if (str == *endp)
551                 return 1;
552
553         sprintf(n, "%lu", *res);
554         if (strncmp(n, str, *endp - str))
555                 /* overflow */
556                 return 1;
557         return 0;
558 }
559
560 static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
561                                   unsigned long count, void *data)
562 {
563         struct obd_device *obd = data;
564         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
565         int rc;
566         char kernbuf[50], *tmp, *end, *errmsg;
567         unsigned long uid, gid;
568         int nouid, nogid;
569         ENTRY;
570
571         if (count >= sizeof(kernbuf)) {
572                 errmsg = "string too long";
573                 GOTO(failed, rc = -EINVAL);
574         }
575         if (cfs_copy_from_user(kernbuf, buffer, count)) {
576                 errmsg = "bad address";
577                 GOTO(failed, rc = -EFAULT);
578         }
579         kernbuf[count] = '\0';
580
581         nouid = nogid = 0;
582         if (safe_strtoul(buffer, &tmp, &uid)) {
583                 uid = mdt->mdt_squash_uid;
584                 nouid = 1;
585         }
586
587         /* skip ':' */
588         if (*tmp == ':') {
589                 tmp++;
590                 if (safe_strtoul(tmp, &end, &gid)) {
591                         gid = mdt->mdt_squash_gid;
592                         nogid = 1;
593                 }
594         } else {
595                 gid = mdt->mdt_squash_gid;
596                 nogid = 1;
597         }
598
599         mdt->mdt_squash_uid = uid;
600         mdt->mdt_squash_gid = gid;
601
602         if (nouid && nogid) {
603                 errmsg = "needs uid:gid format";
604                 GOTO(failed, rc = -EINVAL);
605         }
606
607         LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
608                       obd->obd_name,
609                       mdt->mdt_squash_uid,  mdt->mdt_squash_gid);
610         RETURN(count);
611
612  failed:
613         CWARN("%s: failed to set root_squash to \"%s\", %s: rc %d\n",
614               obd->obd_name, buffer, errmsg, rc);
615         RETURN(rc);
616 }
617
618 static int lprocfs_rd_nosquash_nids(char *page, char **start, off_t off,
619                                     int count, int *eof, void *data)
620 {
621         struct obd_device *obd = data;
622         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
623
624         if (mdt->mdt_nosquash_str)
625                 return snprintf(page, count, "%s\n", mdt->mdt_nosquash_str);
626         return snprintf(page, count, "NONE\n");
627 }
628
629 static int lprocfs_wr_nosquash_nids(struct file *file, const char *buffer,
630                                     unsigned long count, void *data)
631 {
632         struct obd_device *obd = data;
633         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
634         int rc;
635         char *kernbuf, *errmsg;
636         cfs_list_t tmp;
637         ENTRY;
638
639         OBD_ALLOC(kernbuf, count + 1);
640         if (kernbuf == NULL) {
641                 errmsg = "no memory";
642                 GOTO(failed, rc = -ENOMEM);
643         }
644         if (cfs_copy_from_user(kernbuf, buffer, count)) {
645                 errmsg = "bad address";
646                 GOTO(failed, rc = -EFAULT);
647         }
648         kernbuf[count] = '\0';
649
650         if (!strcmp(kernbuf, "NONE") || !strcmp(kernbuf, "clear")) {
651                 /* empty string is special case */
652                 cfs_down_write(&mdt->mdt_squash_sem);
653                 if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
654                         cfs_free_nidlist(&mdt->mdt_nosquash_nids);
655                         OBD_FREE(mdt->mdt_nosquash_str,
656                                  mdt->mdt_nosquash_strlen);
657                         mdt->mdt_nosquash_str = NULL;
658                         mdt->mdt_nosquash_strlen = 0;
659                 }
660                 cfs_up_write(&mdt->mdt_squash_sem);
661                 LCONSOLE_INFO("%s: nosquash_nids is cleared\n",
662                               obd->obd_name);
663                 OBD_FREE(kernbuf, count + 1);
664                 RETURN(count);
665         }
666
667         CFS_INIT_LIST_HEAD(&tmp);
668         if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
669                 errmsg = "can't parse";
670                 GOTO(failed, rc = -EINVAL);
671         }
672
673         cfs_down_write(&mdt->mdt_squash_sem);
674         if (!cfs_list_empty(&mdt->mdt_nosquash_nids)) {
675                 cfs_free_nidlist(&mdt->mdt_nosquash_nids);
676                 OBD_FREE(mdt->mdt_nosquash_str, mdt->mdt_nosquash_strlen);
677         }
678         mdt->mdt_nosquash_str = kernbuf;
679         mdt->mdt_nosquash_strlen = count + 1;
680         cfs_list_splice(&tmp, &mdt->mdt_nosquash_nids);
681
682         LCONSOLE_INFO("%s: nosquash_nids is set to %s\n",
683                       obd->obd_name, kernbuf);
684         cfs_up_write(&mdt->mdt_squash_sem);
685         RETURN(count);
686
687  failed:
688         CWARN("%s: failed to set nosquash_nids to \"%s\", %s: rc %d\n",
689               obd->obd_name, kernbuf, errmsg, rc);
690         if (kernbuf)
691                 OBD_FREE(kernbuf, count + 1);
692         RETURN(rc);
693 }
694
695 static int lprocfs_rd_mdt_som(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, "%sabled\n",
702                         mdt->mdt_som_conf ? "en" : "dis");
703 }
704
705 static int lprocfs_wr_mdt_som(struct file *file, const char *buffer,
706                               unsigned long count, void *data)
707 {
708         struct obd_export *exp;
709         struct obd_device *obd = data;
710         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
711         char kernbuf[16];
712         unsigned long val = 0;
713
714         if (count > (sizeof(kernbuf) - 1))
715                 return -EINVAL;
716
717         if (cfs_copy_from_user(kernbuf, buffer, count))
718                 return -EFAULT;
719
720         kernbuf[count] = '\0';
721
722         if (!strcmp(kernbuf, "enabled"))
723                 val = 1;
724         else if (strcmp(kernbuf, "disabled"))
725                 return -EINVAL;
726
727         if (mdt->mdt_som_conf == val)
728                 return count;
729
730         if (!obd->obd_process_conf) {
731                 CERROR("Temporary SOM change is not supported, use lctl "
732                        "conf_param for permanent setting\n");
733                 return count;
734         }
735
736         /* 1 stands for self export. */
737         cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
738                 if (exp == obd->obd_self_export)
739                         continue;
740                 if (exp->exp_connect_flags & OBD_CONNECT_MDS_MDS)
741                         continue;
742                 /* Some clients are already connected, skip the change */
743                 LCONSOLE_INFO("%s is already connected, SOM will be %s on "
744                               "the next mount\n", exp->exp_client_uuid.uuid,
745                               val ? "enabled" : "disabled");
746                 return count;
747         }
748
749         mdt->mdt_som_conf = val;
750         LCONSOLE_INFO("Enabling SOM\n");
751
752         return count;
753 }
754
755 /* Temporary; for testing purposes only */
756 static int lprocfs_mdt_wr_mdc(struct file *file, const char *buffer,
757                               unsigned long count, void *data)
758 {
759         struct obd_device *obd = data;
760         struct obd_export *exp = NULL;
761         struct obd_uuid uuid;
762         char tmpbuf[sizeof(struct obd_uuid)];
763
764         sscanf(buffer, "%40s", tmpbuf);
765
766         obd_str2uuid(&uuid, tmpbuf);
767         exp = cfs_hash_lookup(obd->obd_uuid_hash, &uuid);
768         if (exp == NULL) {
769                 CERROR("%s: no export %s found\n",
770                        obd->obd_name, obd_uuid2str(&uuid));
771         } else {
772                 mdt_hsm_copytool_send(exp);
773                 class_export_put(exp);
774         }
775
776         return count;
777 }
778
779 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
780         { "uuid",                       lprocfs_rd_uuid,                 0, 0 },
781         { "recovery_status",            lprocfs_obd_rd_recovery_status,  0, 0 },
782         { "num_exports",                lprocfs_rd_num_exports,          0, 0 },
783         { "identity_expire",            lprocfs_rd_identity_expire,
784                                         lprocfs_wr_identity_expire,         0 },
785         { "identity_acquire_expire",    lprocfs_rd_identity_acquire_expire,
786                                         lprocfs_wr_identity_acquire_expire, 0 },
787         { "identity_upcall",            lprocfs_rd_identity_upcall,
788                                         lprocfs_wr_identity_upcall,         0 },
789         { "identity_flush",             0, lprocfs_wr_identity_flush,       0 },
790         { "identity_info",              0, lprocfs_wr_identity_info,        0 },
791         { "capa",                       lprocfs_rd_capa,
792                                         lprocfs_wr_capa,                    0 },
793         { "capa_timeout",               lprocfs_rd_capa_timeout,
794                                         lprocfs_wr_capa_timeout,            0 },
795         { "capa_key_timeout",           lprocfs_rd_ck_timeout,
796                                         lprocfs_wr_ck_timeout,              0 },
797         { "capa_count",                 lprocfs_rd_capa_count,           0, 0 },
798         { "site_stats",                 lprocfs_rd_site_stats,           0, 0 },
799         { "evict_client",               0, lprocfs_mdt_wr_evict_client,     0 },
800         { "hash_stats",                 lprocfs_obd_rd_hash,    0, 0 },
801         { "sec_level",                  lprocfs_rd_sec_level,
802                                         lprocfs_wr_sec_level,               0 },
803         { "commit_on_sharing",          lprocfs_rd_cos, lprocfs_wr_cos, 0 },
804         { "root_squash",                lprocfs_rd_root_squash,
805                                         lprocfs_wr_root_squash,             0 },
806         { "nosquash_nids",              lprocfs_rd_nosquash_nids,
807                                         lprocfs_wr_nosquash_nids,           0 },
808         { "som",                        lprocfs_rd_mdt_som,
809                                         lprocfs_wr_mdt_som, 0 },
810         { "mdccomm",                    0, lprocfs_mdt_wr_mdc,              0 },
811         { 0 }
812 };
813
814 static struct lprocfs_vars lprocfs_mdt_module_vars[] = {
815         { "num_refs",                   lprocfs_rd_numrefs,              0, 0 },
816         { 0 }
817 };
818
819 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
820 {
821     lvars->module_vars  = lprocfs_mdt_module_vars;
822     lvars->obd_vars     = lprocfs_mdt_obd_vars;
823 }
824
825 void mdt_counter_incr(struct obd_export *exp, int opcode)
826 {
827         if (exp->exp_obd && exp->exp_obd->md_stats)
828                 lprocfs_counter_incr(exp->exp_obd->md_stats, opcode);
829         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
830                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
831
832 }
833
834 void mdt_stats_counter_init(struct lprocfs_stats *stats)
835 {
836         lprocfs_counter_init(stats, LPROC_MDT_OPEN, 0, "open", "reqs");
837         lprocfs_counter_init(stats, LPROC_MDT_CLOSE, 0, "close", "reqs");
838         lprocfs_counter_init(stats, LPROC_MDT_MKNOD, 0, "mknod", "reqs");
839         lprocfs_counter_init(stats, LPROC_MDT_LINK, 0, "link", "reqs");
840         lprocfs_counter_init(stats, LPROC_MDT_UNLINK, 0, "unlink", "reqs");
841         lprocfs_counter_init(stats, LPROC_MDT_MKDIR, 0, "mkdir", "reqs");
842         lprocfs_counter_init(stats, LPROC_MDT_RMDIR, 0, "rmdir", "reqs");
843         lprocfs_counter_init(stats, LPROC_MDT_RENAME, 0, "rename", "reqs");
844         lprocfs_counter_init(stats, LPROC_MDT_GETATTR, 0, "getattr", "reqs");
845         lprocfs_counter_init(stats, LPROC_MDT_SETATTR, 0, "setattr", "reqs");
846         lprocfs_counter_init(stats, LPROC_MDT_GETXATTR, 0, "getxattr", "reqs");
847         lprocfs_counter_init(stats, LPROC_MDT_SETXATTR, 0, "setxattr", "reqs");
848         lprocfs_counter_init(stats, LPROC_MDT_STATFS, 0, "statfs", "reqs");
849         lprocfs_counter_init(stats, LPROC_MDT_SYNC, 0, "sync", "reqs");
850 }