Whamcloud - gitweb
land b_groups onto HEAD:
[fs/lustre-release.git] / lustre / mds / lproc_mds.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #define DEBUG_SUBSYSTEM S_CLASS
23
24 #include <linux/version.h>
25 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
26 #include <asm/statfs.h>
27 #endif
28 #include <linux/obd.h>
29 #include <linux/obd_class.h>
30 #include <linux/lprocfs_status.h>
31 #include "mds_internal.h"
32
33 #ifndef LPROCFS
34 struct lprocfs_vars lprocfs_mds_obd_vars[]  = { {0} };
35 struct lprocfs_vars lprocfs_mds_module_vars[] = { {0} };
36 struct lprocfs_vars lprocfs_mdt_obd_vars[] = { {0} };
37 struct lprocfs_vars lprocfs_mdt_module_vars[] = { {0} };
38
39 atomic_t * lprocfs_alloc_mds_counters()
40 {
41         return NULL;
42 }
43 void lprocfs_free_mds_counters(atomic_t *ptr)
44 {
45         return;
46 }
47
48 #else
49
50 struct ll_mdscounters_opcode {
51      __u32       opcode;
52      const char *opname;
53 } ll_mdscounters_opcode_table[MDS_LAST_OPC_COUNT] = {
54        { MDS_OPEN_COUNT,          "mds_open" },
55        { MDS_CREATE_COUNT,        "mds_create" },
56        { MDS_CLOSE_COUNT,         "mds_close" },
57        { MDS_LINK_COUNT,          "mds_link" },
58        { MDS_UNLINK_COUNT,        "mds_unlink" },
59        { MDS_GETATTR_COUNT,       "mds_getattr" },
60        { MDS_GETATTR_NAME_COUNT,  "mds_getattr_name" },
61        { MDS_SETATTR_COUNT,       "mds_setattr" },
62        { MDS_RENAME_COUNT,        "mds_rename" },
63        { MDS_STATFS_COUNT,        "mds_statfs" },
64 };
65
66 const char* ll_mds_count_opcode2str(__u32 opcode)
67 {
68         __u32 offset = opcode;
69
70         LASSERT(offset < MDS_LAST_OPC_COUNT);
71         LASSERT(ll_mdscounters_opcode_table[offset].opcode == opcode);
72         return ll_mdscounters_opcode_table[offset].opname;
73 }
74
75 struct lprocfs_stats * lprocfs_alloc_mds_counters()
76 {
77         struct lprocfs_stats *counters;
78         int i;
79
80         counters = lprocfs_alloc_stats(MDS_LAST_OPC_COUNT);
81
82         for (i = 0; i < MDS_LAST_OPC_COUNT; i ++) {         
83             lprocfs_counter_init(counters, i, 0, 
84                                  (char *)ll_mds_count_opcode2str(i), "reqs");
85         }
86         return counters;
87 }
88
89 void lprocfs_free_mds_counters(struct lprocfs_stats *ptr)
90 {
91         lprocfs_free_stats(ptr);
92 }
93
94 static int lprocfs_mds_rd_mntdev(char *page, char **start, off_t off, int count,
95                                  int *eof, void *data)
96 {
97         struct obd_device* obd = (struct obd_device *)data;
98
99         LASSERT(obd != NULL);
100         LASSERT(obd->u.mds.mds_vfsmnt->mnt_devname);
101         *eof = 1;
102
103         return snprintf(page, count, "%s\n",obd->u.mds.mds_vfsmnt->mnt_devname);
104 }
105
106 static int lprocfs_mds_rd_recovery_status(char *page, char **start, off_t off,
107                                           int count, int *eof, void *data)
108 {
109         struct obd_device *obd = data;
110         int len = 0, n,
111                 connected = obd->obd_connected_clients,
112                 max_recoverable = obd->obd_max_recoverable_clients,
113                 recoverable = obd->obd_recoverable_clients,
114                 completed = max_recoverable - recoverable,
115                 queue_len = obd->obd_requests_queued_for_recovery,
116                 replayed = obd->obd_replayed_requests;
117         __u64 next_transno = obd->obd_next_recovery_transno;
118
119         LASSERT(obd != NULL);
120         *eof = 1;
121
122         n = snprintf(page, count, "status: ");
123         page += n; len += n; count -= n;
124         if (obd->obd_max_recoverable_clients == 0) {
125                 n = snprintf(page, count, "INACTIVE\n");
126                 return len + n;
127         }
128
129         if (obd->obd_recoverable_clients == 0) {
130                 n = snprintf(page, count, "COMPLETE\n");
131                 page += n; len += n; count -= n;
132                 n = snprintf(page, count, "recovered_clients: %d\n",
133                              max_recoverable);
134                 page += n; len += n; count -= n;
135                 n = snprintf(page, count, "last_transno: "LPD64"\n",
136                              next_transno - 1);
137                 page += n; len += n; count -= n;
138                 n = snprintf(page, count, "replayed_requests: %d\n", replayed);
139                 return len + n;
140         }
141
142         /* sampled unlocked, but really... */
143         if (obd->obd_recovering == 0) {
144                 n = snprintf(page, count, "ABORTED\n");
145                 return len + n;
146         }
147
148         n = snprintf(page, count, "RECOVERING\n");
149         page += n; len += n; count -= n;
150         n = snprintf(page, count, "connected_clients: %d/%d\n",
151                      connected, max_recoverable);
152         page += n; len += n; count -= n;
153         n = snprintf(page, count, "completed_clients: %d/%d\n",
154                      completed, max_recoverable);
155         page += n; len += n; count -= n;
156         n = snprintf(page, count, "replayed_requests: %d/??\n", replayed);
157         page += n; len += n; count -= n;
158         n = snprintf(page, count, "queued_requests: %d\n", queue_len);
159         page += n; len += n; count -= n;
160         n = snprintf(page, count, "next_transno: "LPD64"\n", next_transno);
161         return len + n;
162 }
163
164 static int lprocfs_rd_mds_counters(char *page, char **start, off_t off,
165                                           int count, int *eof, void *data)
166 {
167         struct obd_device* obd = (struct obd_device *)data;
168         int len = 0, n, i, j;
169         struct lprocfs_counter  t, ret = { .lc_min = ~(__u64)0 };
170         struct lprocfs_stats *stats;
171         struct timeval now;
172
173         LASSERT(obd != NULL);
174         if (obd->u.mds.mds_counters == NULL)
175                 return 0;
176
177         do_gettimeofday(&now);
178
179         n = snprintf(page, count, "%-25s %lu.%lu secs.usecs\n",
180                                "snapshot_time", now.tv_sec, now.tv_usec);
181         page += n; len +=n; count -=n;
182
183         stats = obd->u.mds.mds_counters;
184
185         *eof = 1;
186         for (i = 0; i < MDS_LAST_OPC_COUNT; i ++) {
187                 ret.lc_count = 0; 
188                 for (j = 0; j < num_online_cpus(); j++) {
189                         struct lprocfs_counter *percpu_cntr =
190                                 &(stats->ls_percpu[j])->lp_cntr[i];
191                         int centry;
192                         do {
193                                 centry = 
194                                    atomic_read(&percpu_cntr->lc_cntl.la_entry); 
195                                 t.lc_count = percpu_cntr->lc_count;
196                         } while (centry != 
197                                  atomic_read(&percpu_cntr->lc_cntl.la_entry) &&
198                                  centry != 
199                                  atomic_read(&percpu_cntr->lc_cntl.la_exit));
200                         ret.lc_count += t.lc_count;
201                } 
202                 n = snprintf(page, count, "%-25s "LPU64" \n", 
203                                    ll_mds_count_opcode2str(i), ret.lc_count);
204                 page += n; len +=n; count -=n;
205         }
206         return (len);
207 }
208
209 static int lprocfs_mds_wr_evict_client(struct file *file, const char *buffer,
210                                        unsigned long count, void *data)
211 {
212         struct obd_device *obd = data;
213         struct obd_export *doomed_exp = NULL;
214         struct obd_uuid doomed;
215         struct list_head *p;
216         char tmpbuf[sizeof(doomed)];
217
218         sscanf(buffer, "%40s", tmpbuf);
219         obd_str2uuid(&doomed, tmpbuf);
220
221         spin_lock(&obd->obd_dev_lock);
222         list_for_each(p, &obd->obd_exports) {
223                 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
224                 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
225                         class_export_get(doomed_exp);
226                         break;
227                 }
228                 doomed_exp = NULL;
229         }
230         spin_unlock(&obd->obd_dev_lock);
231
232         if (doomed_exp == NULL) {
233                 CERROR("can't disconnect %s: no export found\n",
234                        doomed.uuid);
235         } else {
236                 CERROR("evicting %s at adminstrative request\n",
237                        doomed.uuid);
238                 ptlrpc_fail_export(doomed_exp);
239                 class_export_put(doomed_exp);
240         }
241         return count;
242 }
243
244 static int lprocfs_mds_wr_config_update(struct file *file, const char *buffer,
245                                         unsigned long count, void *data)
246 {
247         struct obd_device *obd = data;
248         ENTRY;
249
250         RETURN(mds_lov_update_config(obd, 0));
251 }
252
253 struct lprocfs_vars lprocfs_mds_obd_vars[] = {
254         { "uuid",         lprocfs_rd_uuid,        0, 0 },
255         { "blocksize",    lprocfs_rd_blksize,     0, 0 },
256         { "kbytestotal",  lprocfs_rd_kbytestotal, 0, 0 },
257         { "kbytesfree",   lprocfs_rd_kbytesfree,  0, 0 },
258         { "kbytesavail",  lprocfs_rd_kbytesavail, 0, 0 },
259         { "fstype",       lprocfs_rd_fstype,      0, 0 },
260         { "filestotal",   lprocfs_rd_filestotal,  0, 0 },
261         { "filesfree",    lprocfs_rd_filesfree,   0, 0 },
262         { "mntdev",       lprocfs_mds_rd_mntdev,  0, 0 },
263         { "recovery_status", lprocfs_mds_rd_recovery_status, 0, 0 },
264         { "evict_client", 0, lprocfs_mds_wr_evict_client, 0 },
265         { "config_update", 0, lprocfs_mds_wr_config_update, 0 },
266         { "num_exports",  lprocfs_rd_num_exports, 0, 0 },
267         { "counters", lprocfs_rd_mds_counters, 0, 0 },
268         { 0 }
269 };
270
271 /*
272  * group hash proc entries handler
273  */
274 static int lprocfs_wr_group_info(struct file *file, const char *buffer,
275                                  unsigned long count, void *data)
276 {
277         struct {
278                 int             err;
279                 uid_t           uid;
280                 uint32_t        ngroups;
281                 gid_t          *groups;
282         } param;
283         gid_t   gids_local[NGROUPS_SMALL];
284         gid_t  *gids = NULL;
285
286         if (count != sizeof(param)) {
287                 CERROR("invalid data size %lu\n", count);
288                 return count;
289         }
290         if (copy_from_user(&param, buffer, count)) {
291                 CERROR("broken downcall\n");
292                 return count;
293         }
294         if (param.ngroups > NGROUPS_MAX) {
295                 CERROR("%d groups?\n", param.ngroups);
296                 return count;
297         }
298
299         if (param.ngroups <= NGROUPS_SMALL)
300                 gids = gids_local;
301         else {
302                 OBD_ALLOC(gids, param.ngroups * sizeof(gid_t));
303                 if (!gids) {
304                         CERROR("fail to alloc memory for %d gids\n",
305                                 param.ngroups);
306                         return count;
307                 }
308         }
309         if (copy_from_user(gids, param.groups,
310                            param.ngroups * sizeof(gid_t))) {
311                 CERROR("broken downcall\n");
312                 goto out;
313         }
314
315         mds_handle_group_downcall(param.err, param.uid,
316                                   param.ngroups, gids);
317
318 out:
319         if (gids && gids != gids_local)
320                 OBD_FREE(gids, param.ngroups * sizeof(gid_t));
321         return count;
322 }
323 static int lprocfs_rd_expire(char *page, char **start, off_t off, int count,
324                              int *eof, void *data)
325 {
326         struct mds_grp_hash *hash = __mds_get_global_group_hash();
327
328         *eof = 1;
329         return snprintf(page, count, "%d\n", hash->gh_entry_expire);
330 }
331 static int lprocfs_wr_expire(struct file *file, const char *buffer,
332                              unsigned long count, void *data)
333 {
334         struct mds_grp_hash *hash = __mds_get_global_group_hash();
335         char buf[32];
336
337         if (copy_from_user(buf, buffer, min(count, 32UL)))
338                 return count;
339         buf[31] = 0;
340         sscanf(buf, "%d", &hash->gh_entry_expire);
341         return count;
342 }
343 static int lprocfs_rd_ac_expire(char *page, char **start, off_t off, int count,
344                                 int *eof, void *data)
345 {
346         struct mds_grp_hash *hash = __mds_get_global_group_hash();
347
348         *eof = 1;
349         return snprintf(page, count, "%d\n", hash->gh_acquire_expire);
350 }
351 static int lprocfs_wr_ac_expire(struct file *file, const char *buffer,
352                                 unsigned long count, void *data)
353 {
354         struct mds_grp_hash *hash = __mds_get_global_group_hash();
355         char buf[32];
356
357         if (copy_from_user(buf, buffer, min(count, 32UL)))
358                 return count;
359         buf[31] = 0;
360         sscanf(buf, "%d", &hash->gh_acquire_expire);
361         return count;
362 }
363 static int lprocfs_rd_hash_upcall(char *page, char **start, off_t off, int count,
364                                 int *eof, void *data)
365 {
366         struct mds_grp_hash *hash = __mds_get_global_group_hash();
367
368         *eof = 1;
369         return snprintf(page, count, "%s\n", hash->gh_upcall);
370 }
371 static int lprocfs_wr_hash_upcall(struct file *file, const char *buffer,
372                                   unsigned long count, void *data)
373 {
374         struct mds_grp_hash *hash = __mds_get_global_group_hash();
375
376         if (count < MDSGRP_UPCALL_MAXPATH) {
377                 sscanf(buffer, "%1024s", hash->gh_upcall);
378                 hash->gh_upcall[MDSGRP_UPCALL_MAXPATH-1] = 0;
379         }
380         return count;
381 }
382 static int lprocfs_wr_hash_flush(struct file *file, const char *buffer,
383                                   unsigned long count, void *data)
384 {
385         mds_group_hash_flush_idle();
386         return count;
387 }
388 static int lprocfs_rd_allow_setgroups(char *page, char **start, off_t off,
389                                       int count, int *eof, void *data)
390 {
391         struct mds_grp_hash *hash = __mds_get_global_group_hash();
392
393         *eof = 1;
394         return snprintf(page, count, "%d\n", hash->gh_allow_setgroups);
395 }
396 static int lprocfs_wr_allow_setgroups(struct file *file, const char *buffer,
397                                       unsigned long count, void *data)
398 {
399         struct mds_grp_hash *hash = __mds_get_global_group_hash();
400         char buf[8];
401         int val;
402
403         if (copy_from_user(buf, buffer, min(count, 8UL)))
404                 return count;
405         buf[7] = 0;
406         sscanf(buf, "%d", &val);
407         hash->gh_allow_setgroups = (val != 0);
408         return count;
409 }
410
411 struct lprocfs_vars lprocfs_mds_module_vars[] = {
412         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
413         { "grp_hash_expire_interval", lprocfs_rd_expire, lprocfs_wr_expire, 0},
414         { "grp_hash_acquire_expire", lprocfs_rd_ac_expire,
415                                      lprocfs_wr_ac_expire, 0},
416         { "grp_hash_upcall", lprocfs_rd_hash_upcall, lprocfs_wr_hash_upcall, 0},
417         { "grp_hash_flush", 0, lprocfs_wr_hash_flush, 0},
418         { "group_info", 0,  lprocfs_wr_group_info,   0 },
419         { "allow_setgroups", lprocfs_rd_allow_setgroups,
420                              lprocfs_wr_allow_setgroups, 0},
421         { 0 }
422 };
423
424 struct lprocfs_vars lprocfs_mdt_obd_vars[] = {
425         { "uuid",         lprocfs_rd_uuid,        0, 0 },
426         { 0 }
427 };
428
429 struct lprocfs_vars lprocfs_mdt_module_vars[] = {
430         { "num_refs",     lprocfs_rd_numrefs,     0, 0 },
431         { 0 }
432 };
433
434 #endif /* LPROCFS */
435
436 struct lprocfs_static_vars lprocfs_array_vars[] = { {lprocfs_mds_module_vars,
437                                                      lprocfs_mds_obd_vars},
438                                                     {lprocfs_mdt_module_vars,
439                                                      lprocfs_mdt_obd_vars}};
440
441 LPROCFS_INIT_MULTI_VARS(lprocfs_array_vars,
442                         (sizeof(lprocfs_array_vars) /
443                          sizeof(struct lprocfs_static_vars)))