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