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