Whamcloud - gitweb
b=16098
[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 [sun.com URL with a
20  * copy of GPLv2].
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
70 static const char *mdt_proc_names[LPROC_MDT_NR] = {
71 };
72
73 int mdt_procfs_init(struct mdt_device *mdt, const char *name)
74 {
75         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
76         int result;
77         ENTRY;
78
79         LASSERT(name != NULL);
80         mdt->mdt_proc_entry = ld->ld_obd->obd_proc_entry;
81         LASSERT(mdt->mdt_proc_entry != NULL);
82
83         result = lu_time_init(&mdt->mdt_stats, mdt->mdt_proc_entry,
84                               mdt_proc_names, ARRAY_SIZE(mdt_proc_names));
85         if (result == 0)
86                 result = lu_time_named_init(&ld->ld_site->ls_time_stats,
87                                             "site_time", mdt->mdt_proc_entry,
88                                             lu_time_names,
89                                             ARRAY_SIZE(lu_time_names));
90         RETURN(result);
91 }
92
93 int mdt_procfs_fini(struct mdt_device *mdt)
94 {
95         struct lu_device *ld = &mdt->mdt_md_dev.md_lu_dev;
96         lu_time_fini(&ld->ld_site->ls_time_stats);
97         lu_time_fini(&mdt->mdt_stats);
98         mdt->mdt_proc_entry = NULL;
99         RETURN(0);
100 }
101
102 void mdt_time_start(const struct mdt_thread_info *info)
103 {
104         lu_lprocfs_time_start(info->mti_env);
105 }
106
107 void mdt_time_end(const struct mdt_thread_info *info, int idx)
108 {
109         lu_lprocfs_time_end(info->mti_env, info->mti_mdt->mdt_stats, idx);
110 }
111
112 static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
113                                       int count, int *eof, void *data)
114 {
115         struct obd_device *obd = data;
116         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
117
118         *eof = 1;
119         return snprintf(page, count, "%lu\n",
120                         mdt->mdt_identity_cache->uc_entry_expire / HZ);
121 }
122
123 static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
124                                       unsigned long count, void *data)
125 {
126         struct obd_device *obd = data;
127         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
128         int rc, val;
129
130         rc = lprocfs_write_helper(buffer, count, &val);
131         if (rc)
132                 return rc;
133
134         mdt->mdt_identity_cache->uc_entry_expire = val * HZ;
135         return count;
136 }
137
138 static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
139                                               off_t off, int count, int *eof,
140                                               void *data)
141 {
142         struct obd_device *obd = data;
143         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
144
145         *eof = 1;
146         return snprintf(page, count, "%lu\n",
147                         mdt->mdt_identity_cache->uc_acquire_expire / HZ);
148 }
149
150 static int lprocfs_wr_identity_acquire_expire(struct file *file,
151                                               const char *buffer,
152                                               unsigned long count,
153                                               void *data)
154 {
155         struct obd_device *obd = data;
156         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
157         int rc, val;
158
159         rc = lprocfs_write_helper(buffer, count, &val);
160         if (rc)
161                 return rc;
162
163         mdt->mdt_identity_cache->uc_acquire_expire = val * HZ;
164         return count;
165 }
166
167 static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
168                                       int count, int *eof, void *data)
169 {
170         struct obd_device *obd = data;
171         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
172         struct upcall_cache *hash = mdt->mdt_identity_cache;
173         int len;
174
175         *eof = 1;
176         read_lock(&hash->uc_upcall_rwlock);
177         len = snprintf(page, count, "%s\n", hash->uc_upcall);
178         read_unlock(&hash->uc_upcall_rwlock);
179         return len;
180 }
181
182 static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
183                                       unsigned long count, void *data)
184 {
185         struct obd_device *obd = data;
186         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
187         struct upcall_cache *hash = mdt->mdt_identity_cache;
188         char kernbuf[UC_CACHE_UPCALL_MAXPATH] = { '\0' };
189
190         if (count >= UC_CACHE_UPCALL_MAXPATH) {
191                 CERROR("%s: identity upcall too long\n", obd->obd_name);
192                 return -EINVAL;
193         }
194
195         if (copy_from_user(kernbuf, buffer,
196                            min(count, UC_CACHE_UPCALL_MAXPATH - 1)))
197                 return -EFAULT;
198
199         /* Remove any extraneous bits from the upcall (e.g. linefeeds) */
200         write_lock(&hash->uc_upcall_rwlock);
201         sscanf(kernbuf, "%s", hash->uc_upcall);
202         write_unlock(&hash->uc_upcall_rwlock);
203
204         if (strcmp(hash->uc_name, obd->obd_name) != 0)
205                 CWARN("%s: write to upcall name %s\n",
206                       obd->obd_name, hash->uc_upcall);
207
208         if (strcmp(hash->uc_upcall, "NONE") == 0 && mdt->mdt_opts.mo_acl)
209                 CWARN("%s: disable \"identity_upcall\" with ACL enabled maybe "
210                       "cause unexpected \"EACCESS\"\n", obd->obd_name);
211
212         CWARN("%s: identity upcall set to %s\n", obd->obd_name, hash->uc_upcall);
213         return count;
214 }
215
216 static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
217                                      unsigned long count, void *data)
218 {
219         struct obd_device *obd = data;
220         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
221         int rc, uid;
222
223         rc = lprocfs_write_helper(buffer, count, &uid);
224         if (rc)
225                 return rc;
226
227         mdt_flush_identity(mdt->mdt_identity_cache, uid);
228         return count;
229 }
230
231 static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
232                                     unsigned long count, void *data)
233 {
234         struct obd_device *obd = data;
235         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
236         struct identity_downcall_data sparam, *param = &sparam;
237         int size = 0, rc = 0;
238
239         if (count < sizeof(*param)) {
240                 CERROR("%s: invalid data size %lu\n", obd->obd_name, count);
241                 return count;
242         }
243
244         if (copy_from_user(&sparam, buffer, sizeof(sparam))) {
245                 CERROR("%s: bad identity data\n", obd->obd_name);
246                 GOTO(out, rc = -EFAULT);
247         }
248
249         if (sparam.idd_magic != IDENTITY_DOWNCALL_MAGIC) {
250                 CERROR("%s: MDS identity downcall bad params\n", obd->obd_name);
251                 GOTO(out, rc = -EINVAL);
252         }
253
254         if (sparam.idd_nperms > N_PERMS_MAX) {
255                 CERROR("%s: perm count %d more than maximum %d\n",
256                        obd->obd_name, sparam.idd_nperms, N_PERMS_MAX);
257                 GOTO(out, rc = -EINVAL);
258         }
259
260         if (sparam.idd_ngroups > NGROUPS_MAX) {
261                 CERROR("%s: group count %d more than maximum %d\n",
262                        obd->obd_name, sparam.idd_ngroups, NGROUPS_MAX);
263                 GOTO(out, rc = -EINVAL);
264         }
265
266         if (sparam.idd_ngroups) {
267                 size = offsetof(struct identity_downcall_data,
268                                 idd_groups[sparam.idd_ngroups]);
269                 OBD_ALLOC(param, size);
270                 if (!param) {
271                         CERROR("%s: fail to alloc %d bytes for uid %u"
272                                " with %d groups\n", obd->obd_name, size,
273                                sparam.idd_uid, sparam.idd_ngroups);
274                         param = &sparam;
275                         param->idd_ngroups = 0;
276                 } else if (copy_from_user(param, buffer, size)) {
277                         CERROR("%s: uid %u bad supplementary group data\n",
278                                obd->obd_name, sparam.idd_uid);
279                         OBD_FREE(param, size);
280                         param = &sparam;
281                         param->idd_ngroups = 0;
282                 }
283         }
284
285         rc = upcall_cache_downcall(mdt->mdt_identity_cache, param->idd_err,
286                                    param->idd_uid, param);
287
288 out:
289         if (param && (param != &sparam))
290                 OBD_FREE(param, size);
291
292         return rc ?: count;
293 }
294
295 /* for debug only */
296 static int lprocfs_rd_capa(char *page, char **start, off_t off,
297                            int count, int *eof, void *data)
298 {
299         struct obd_device *obd = data;
300         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
301
302         return snprintf(page, count, "capability on: %s %s\n",
303                         mdt->mdt_opts.mo_oss_capa ? "oss" : "",
304                         mdt->mdt_opts.mo_mds_capa ? "mds" : "");
305 }
306
307 static int lprocfs_wr_capa(struct file *file, const char *buffer,
308                            unsigned long count, void *data)
309 {
310         struct obd_device *obd = data;
311         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
312         int val, rc;
313
314         rc = lprocfs_write_helper(buffer, count, &val);
315         if (rc)
316                 return rc;
317
318         if (val < 0 || val > 3) {
319                 CERROR("invalid capability mode, only 0/2/3 is accepted.\n"
320                        " 0:  disable fid capability\n"
321                        " 2:  enable MDS fid capability\n"
322                        " 3:  enable both MDS and OSS fid capability\n");
323                 return -EINVAL;
324         }
325
326         /* OSS fid capability needs enable both MDS and OSS fid capability on
327          * MDS */
328         if (val == 1) {
329                 CERROR("can't enable OSS fid capability only, you should use "
330                        "'3' to enable both MDS and OSS fid capability.\n");
331                 return -EINVAL;
332         }
333
334         mdt->mdt_opts.mo_oss_capa = (val & 0x1);
335         mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
336         mdt->mdt_capa_conf = 1;
337         LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
338                       obd->obd_name,
339                       mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
340         LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
341                       obd->obd_name,
342                       mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
343         return count;
344 }
345
346 static int lprocfs_rd_capa_count(char *page, char **start, off_t off,
347                                  int count, int *eof, void *data)
348 {
349         return snprintf(page, count, "%d %d\n",
350                         capa_count[CAPA_SITE_CLIENT],
351                         capa_count[CAPA_SITE_SERVER]);
352 }
353
354 static int lprocfs_rd_site_stats(char *page, char **start, off_t off,
355                                  int count, int *eof, void *data)
356 {
357         struct obd_device *obd = data;
358         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
359         struct lu_site    *s   = mdt->mdt_md_dev.md_lu_dev.ld_site;
360         int i;
361         int populated;
362
363         /*
364          * How many hash buckets are not-empty? Don't bother with locks: it's
365          * an estimation anyway.
366          */
367         for (i = 0, populated = 0; i < s->ls_hash_size; i++)
368                 populated += !hlist_empty(&s->ls_hash[i]);
369
370         return snprintf(page, count, "%d %d %d/%d %d %d %d %d %d %d\n",
371                         s->ls_total,
372                         s->ls_busy,
373                         populated,
374                         s->ls_hash_size,
375                         s->ls_stats.s_created,
376                         s->ls_stats.s_cache_hit,
377                         s->ls_stats.s_cache_miss,
378                         s->ls_stats.s_cache_check,
379                         s->ls_stats.s_cache_race,
380                         s->ls_stats.s_lru_purged);
381 }
382
383 static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
384                                    int count, int *eof, void *data)
385 {
386         struct obd_device *obd = data;
387         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
388
389         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
390 }
391
392 static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
393                                    unsigned long count, void *data)
394 {
395         struct obd_device *obd = data;
396         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
397         int val, rc;
398
399         rc = lprocfs_write_helper(buffer, count, &val);
400         if (rc)
401                 return rc;
402
403         mdt->mdt_capa_timeout = (unsigned long)val;
404         mdt->mdt_capa_conf = 1;
405         return count;
406 }
407
408 static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
409                                  int *eof, void *data)
410 {
411         struct obd_device *obd = data;
412         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
413
414         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
415 }
416
417 static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
418                                  unsigned long count, void *data)
419 {
420         struct obd_device *obd = data;
421         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
422         int val, rc;
423
424         rc = lprocfs_write_helper(buffer, count, &val);
425         if (rc)
426                 return rc;
427
428         mdt->mdt_ck_timeout = (unsigned long)val;
429         mdt->mdt_capa_conf = 1;
430         return count;
431 }
432
433 static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
434                                        unsigned long count, void *data)
435 {
436         char tmpbuf[sizeof(struct obd_uuid)];
437
438         sscanf(buffer, "%40s", tmpbuf);
439
440         if (strncmp(tmpbuf, "nid:", 4) != 0)
441                 return lprocfs_wr_evict_client(file, buffer, count, data);
442
443         CERROR("NOT implement evict client by nid %s\n", tmpbuf);
444
445         return count;
446 }
447
448 static struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
449         { "uuid",                       lprocfs_rd_uuid,                 0, 0 },
450         { "recovery_status",            lprocfs_obd_rd_recovery_status,  0, 0 },
451         { "num_exports",                lprocfs_rd_num_exports,          0, 0 },
452         { "identity_expire",            lprocfs_rd_identity_expire,
453                                         lprocfs_wr_identity_expire,         0 },
454         { "identity_acquire_expire",    lprocfs_rd_identity_acquire_expire,
455                                         lprocfs_wr_identity_acquire_expire, 0 },
456         { "identity_upcall",            lprocfs_rd_identity_upcall,
457                                         lprocfs_wr_identity_upcall,         0 },
458         { "identity_flush",             0, lprocfs_wr_identity_flush,       0 },
459         { "identity_info",              0, lprocfs_wr_identity_info,        0 },
460         { "capa",                       lprocfs_rd_capa,
461                                         lprocfs_wr_capa,                    0 },
462         { "capa_timeout",               lprocfs_rd_capa_timeout,
463                                         lprocfs_wr_capa_timeout,            0 },
464         { "capa_key_timeout",           lprocfs_rd_ck_timeout,
465                                         lprocfs_wr_ck_timeout,              0 },
466         { "capa_count",                 lprocfs_rd_capa_count,           0, 0 },
467         { "site_stats",                 lprocfs_rd_site_stats,           0, 0 },
468         { "evict_client",               0, lprocfs_mdt_wr_evict_client,     0 },
469         { 0 }
470 };
471
472 static struct lprocfs_vars lprocfs_mdt_module_vars[] = {
473         { "num_refs",                   lprocfs_rd_numrefs,              0, 0 },
474         { 0 }
475 };
476
477 void lprocfs_mdt_init_vars(struct lprocfs_static_vars *lvars)
478 {
479     lvars->module_vars  = lprocfs_mdt_module_vars;
480     lvars->obd_vars     = lprocfs_mdt_obd_vars;
481 }