Whamcloud - gitweb
add "blacklist" support into OSS capabilities.
[fs/lustre-release.git] / lustre / obdfilter / lproc_obdfilter.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002, 2003 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 #include <linux/lprocfs_status.h>
26 #include <linux/obd.h>
27 #include <linux/seq_file.h>
28 #include <linux/version.h>
29
30 #include "filter_internal.h"
31
32 #ifndef LPROCFS
33 static struct lprocfs_vars lprocfs_obd_vars[]  = { {0} };
34 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
35 #else
36
37 static int lprocfs_filter_rd_groups(char *page, char **start, off_t off,
38                                     int count, int *eof, void *data)
39 {
40         struct obd_device *obd = (struct obd_device *)data;
41         
42         *eof = 1;
43         return snprintf(page, count, "%u\n", obd->u.filter.fo_group_count);
44 }
45
46 static int lprocfs_filter_rd_tot_dirty(char *page, char **start, off_t off,
47                                        int count, int *eof, void *data)
48 {
49         struct obd_device *obd = (struct obd_device *)data;
50
51         LASSERT(obd != NULL);
52         *eof = 1;
53         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_dirty);
54 }
55
56 static int lprocfs_filter_rd_tot_granted(char *page, char **start, off_t off,
57                                          int count, int *eof, void *data)
58 {
59         struct obd_device *obd = (struct obd_device *)data;
60
61         LASSERT(obd != NULL);
62         *eof = 1;
63         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_granted);
64 }
65
66 static int lprocfs_filter_rd_tot_pending(char *page, char **start, off_t off,
67                                          int count, int *eof, void *data)
68 {
69         struct obd_device *obd = (struct obd_device *)data;
70
71         LASSERT(obd != NULL);
72         *eof = 1;
73         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_pending);
74 }
75
76 static int lprocfs_filter_rd_mntdev(char *page, char **start, off_t off,
77                                     int count, int *eof, void *data)
78 {
79         struct obd_device *obd = (struct obd_device *)data;
80
81         LASSERT(obd != NULL);
82         LASSERT(obd->u.filter.fo_vfsmnt->mnt_devname);
83         *eof = 1;
84         return snprintf(page, count, "%s\n",
85                         obd->u.filter.fo_vfsmnt->mnt_devname);
86 }
87
88 static int lprocfs_filter_rd_last_id(char *page, char **start, off_t off,
89                                      int count, int *eof, void *data)
90 {
91         struct obd_device *obd = data;
92         struct filter_obd *filter = &obd->u.filter;
93         int retval = 0, rc, i;
94
95         if (obd == NULL)
96                 return 0;
97
98         for (i = 1; i < filter->fo_group_count; i++) {
99                 rc = snprintf(page, count, LPU64"\n",filter_last_id(filter, i));
100                 if (rc < 0) {
101                         retval = rc;
102                         break;
103                 }
104                 page += rc;
105                 count -= rc;
106                 retval += rc;
107         }
108         return retval;
109 }
110
111 int lprocfs_filter_rd_readcache(char *page, char **start, off_t off, int count,
112                                 int *eof, void *data)
113 {
114         struct obd_device *obd = data;
115         int rc;
116
117         rc = snprintf(page, count, LPU64"\n",
118                       obd->u.filter.fo_readcache_max_filesize);
119         return rc;
120 }
121
122 int lprocfs_filter_wr_readcache(struct file *file, const char *buffer,
123                                 unsigned long count, void *data)
124 {
125         struct obd_device *obd = data;
126         __u64 val;
127         int rc;
128
129         rc = lprocfs_write_u64_helper(buffer, count, &val);
130         if (rc)
131                 return rc;
132
133         obd->u.filter.fo_readcache_max_filesize = val;
134         return count;
135 }
136
137 int lprocfs_filter_rd_capa_stat(char *page, char **start, off_t off, int count,
138                                 int *eof, void *data)
139 {
140         struct obd_device *obd = data;
141         int rc;
142
143         rc = snprintf(page, count, "%d\n",
144                       obd->u.filter.fo_capa_stat);
145         return rc;
146 }
147
148 int lprocfs_filter_wr_capa_stat(struct file *file, const char *buffer,
149                                 unsigned long count, void *data)
150 {
151         struct obd_device *obd = data;
152         int val;
153         int rc;
154
155         rc = lprocfs_write_helper(buffer, count, &val);
156         if (rc)
157                 return rc;
158
159         obd->u.filter.fo_capa_stat = val;
160         return count;
161 }
162
163 static
164 int lprocfs_filter_rd_blacklist(char *page, char **start, off_t off, int count,
165                                 int *eof, void *data)
166 {
167         int rc;
168
169         rc = blacklist_display(page, count);
170         *eof = 1;
171         return rc;
172 }
173
174 static
175 int lprocfs_filter_wr_blacklist(struct file *file, const char *buffer,
176                                 unsigned long count, void *data)
177 {
178         int add;
179         uid_t uid = -1;
180
181         if (count < 2)
182                 return count;
183         if (buffer[0] == '+')
184                 add = 1;
185         else if (buffer[0] == '-')
186                 add = 0;
187         else
188                 return count;
189
190         sscanf(buffer + 1, "%u", &uid);
191         if (add)
192                 blacklist_add(uid);
193         else
194                 blacklist_del(uid);
195         return count;
196 }
197
198 static struct lprocfs_vars lprocfs_obd_vars[] = {
199         { "uuid",         lprocfs_rd_uuid,          0, 0 },
200         { "blocksize",    lprocfs_rd_blksize,       0, 0 },
201         { "kbytestotal",  lprocfs_rd_kbytestotal,   0, 0 },
202         { "kbytesfree",   lprocfs_rd_kbytesfree,    0, 0 },
203         { "kbytesavail",  lprocfs_rd_kbytesavail,   0, 0 },
204         { "filestotal",   lprocfs_rd_filestotal,    0, 0 },
205         { "filesfree",    lprocfs_rd_filesfree,     0, 0 },
206         { "filegroups",   lprocfs_filter_rd_groups, 0, 0 },
207         { "fstype",       lprocfs_rd_fstype,        0, 0 },
208         { "mntdev",       lprocfs_filter_rd_mntdev, 0, 0 },
209         { "last_id",      lprocfs_filter_rd_last_id,0, 0 },
210         { "tot_dirty",    lprocfs_filter_rd_tot_dirty,   0, 0 },
211         { "tot_pending",  lprocfs_filter_rd_tot_pending, 0, 0 },
212         { "tot_granted",  lprocfs_filter_rd_tot_granted, 0, 0 },
213         { "num_exports",  lprocfs_rd_num_exports,   0, 0 },
214         { "readcache_max_filesize",
215                           lprocfs_filter_rd_readcache,
216                           lprocfs_filter_wr_readcache, 0 },
217         { "recovery_status", lprocfs_obd_rd_recovery_status, 0, 0 },
218         { "capa",         lprocfs_filter_rd_capa_stat,
219                           lprocfs_filter_wr_capa_stat, 0 },
220         { "blacklist",    lprocfs_filter_rd_blacklist,
221                           lprocfs_filter_wr_blacklist, 0 },
222         { 0 }
223 };
224
225 static struct lprocfs_vars lprocfs_module_vars[] = {
226         { "num_refs",     lprocfs_rd_numrefs,       0, 0 },
227         { 0 }
228 };
229
230 void filter_tally_write(struct filter_obd *filter, struct page **pages,
231                      int nr_pages, unsigned long *blocks, int blocks_per_page)
232 {
233         struct page *last_page = NULL;
234         unsigned long *last_block = NULL;
235         unsigned long discont_pages = 0;
236         unsigned long discont_blocks = 0;
237         int i;
238
239         if (nr_pages == 0)
240                 return;
241
242         lprocfs_oh_tally_log2(&filter->fo_w_pages, nr_pages);
243
244         while (nr_pages-- > 0) {
245                 if (last_page && (*pages)->index != (last_page->index + 1))
246                         discont_pages++;
247                 last_page = *pages;
248                 pages++;
249                 for (i = 0; i < blocks_per_page; i++) {
250                         if (last_block && *blocks != (*last_block + 1))
251                                 discont_blocks++;
252                         last_block = blocks++;
253                 }
254         }
255
256         lprocfs_oh_tally(&filter->fo_w_discont_pages, discont_pages);
257         lprocfs_oh_tally(&filter->fo_w_discont_blocks, discont_blocks);
258 }
259
260 void filter_tally_read(struct filter_obd *filter, struct niobuf_local *lnb,
261                        int niocount)
262 {
263         struct niobuf_local *end;
264         struct page *last_page = NULL;
265         unsigned long discont_pages = 0;
266         unsigned long discont_blocks = 0;
267
268         if (niocount == 0)
269                 return;
270
271         for (end = lnb + niocount; lnb < end && lnb->page; lnb++) {
272                 struct page *page = lnb->page;
273                 if (last_page) {
274                        if (page->index != (last_page->index + 1))
275                                 discont_pages++;
276                         /* XXX not so smart for now */
277 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
278                         if ((page->buffers && last_page->buffers) &&
279                             (page->buffers->b_blocknr !=
280                              (last_page->buffers->b_blocknr + 1)))
281                                 discont_blocks++;
282 #else
283 #warning "port on 2.6 -bzzz"
284 #endif
285                 }
286                 last_page = page;
287         }
288
289         lprocfs_oh_tally_log2(&filter->fo_r_pages, niocount);
290         lprocfs_oh_tally(&filter->fo_r_discont_pages, discont_pages);
291         lprocfs_oh_tally(&filter->fo_r_discont_blocks, discont_blocks);
292 }
293
294 #define pct(a,b) (b ? a * 100 / b : 0)
295
296 static int filter_brw_stats_seq_show(struct seq_file *seq, void *v)
297 {
298         struct timeval now;
299         struct obd_device *dev = seq->private;
300         struct filter_obd *filter = &dev->u.filter;
301         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
302         int i;
303
304         do_gettimeofday(&now);
305
306         /* this sampling races with updates */
307
308         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
309                    now.tv_sec, now.tv_usec);
310
311         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
312         seq_printf(seq, "pages per brw         brws   %% cum %% |");
313         seq_printf(seq, "       rpcs   %% cum %%\n");
314
315         read_tot = lprocfs_oh_sum(&filter->fo_r_pages);
316         write_tot = lprocfs_oh_sum(&filter->fo_w_pages);
317
318         read_cum = 0;
319         write_cum = 0;
320         for (i = 0; i < OBD_HIST_MAX; i++) {
321                 unsigned long r = filter->fo_r_pages.oh_buckets[i];
322                 unsigned long w = filter->fo_w_pages.oh_buckets[i];
323                 read_cum += r;
324                 write_cum += w;
325                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
326                                  1 << i, r, pct(r, read_tot),
327                                  pct(read_cum, read_tot), w,
328                                  pct(w, write_tot),
329                                  pct(write_cum, write_tot));
330                 if (read_cum == read_tot && write_cum == write_tot)
331                         break;
332         }
333
334         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
335         seq_printf(seq, "discont pages        rpcs   %% cum %% |");
336         seq_printf(seq, "       rpcs   %% cum %%\n");
337
338         read_tot = lprocfs_oh_sum(&filter->fo_r_discont_pages);
339         write_tot = lprocfs_oh_sum(&filter->fo_w_discont_pages);
340
341         read_cum = 0;
342         write_cum = 0;
343
344         for (i = 0; i < OBD_HIST_MAX; i++) {
345                 unsigned long r = filter->fo_r_discont_pages.oh_buckets[i];
346                 unsigned long w = filter->fo_w_discont_pages.oh_buckets[i];
347                 read_cum += r;
348                 write_cum += w;
349                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
350                                  i, r, pct(r, read_tot),
351                                  pct(read_cum, read_tot), w,
352                                  pct(w, write_tot),
353                                  pct(write_cum, write_tot));
354                 if (read_cum == read_tot && write_cum == write_tot)
355                         break;
356         }
357
358         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
359         seq_printf(seq, "discont blocks        rpcs   %% cum %% |");
360         seq_printf(seq, "       rpcs   %% cum %%\n");
361
362         read_tot = lprocfs_oh_sum(&filter->fo_r_discont_blocks);
363         write_tot = lprocfs_oh_sum(&filter->fo_w_discont_blocks);
364
365         read_cum = 0;
366         write_cum = 0;
367         for (i = 0; i < OBD_HIST_MAX; i++) {
368                 unsigned long r = filter->fo_r_discont_blocks.oh_buckets[i];
369                 unsigned long w = filter->fo_w_discont_blocks.oh_buckets[i];
370                 read_cum += r;
371                 write_cum += w;
372                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
373                                  i, r, pct(r, read_tot),
374                                  pct(read_cum, read_tot), w,
375                                  pct(w, write_tot),
376                                  pct(write_cum, write_tot));
377                 if (read_cum == read_tot && write_cum == write_tot)
378                         break;
379         }
380
381         return 0;
382 }
383 #undef pct
384
385 static void *filter_brw_stats_seq_start(struct seq_file *p, loff_t *pos)
386 {
387         if (*pos == 0)
388                 return (void *)1;
389         return NULL;
390 }
391 static void *filter_brw_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
392 {
393         ++*pos;
394         return NULL;
395 }
396 static void filter_brw_stats_seq_stop(struct seq_file *p, void *v)
397 {
398 }
399 struct seq_operations filter_brw_stats_seq_sops = {
400         .start = filter_brw_stats_seq_start,
401         .stop = filter_brw_stats_seq_stop,
402         .next = filter_brw_stats_seq_next,
403         .show = filter_brw_stats_seq_show,
404 };
405
406 static int filter_brw_stats_seq_open(struct inode *inode, struct file *file)
407 {
408         struct proc_dir_entry *dp = PDE(inode);
409         struct seq_file *seq;
410         int rc;
411
412         rc = seq_open(file, &filter_brw_stats_seq_sops);
413         if (rc)
414                 return rc;
415         seq = file->private_data;
416         seq->private = dp->data;
417         return 0;
418 }
419
420 static ssize_t filter_brw_stats_seq_write(struct file *file, const char *buf,
421                                        size_t len, loff_t *off)
422 {
423         struct seq_file *seq = file->private_data;
424         struct obd_device *dev = seq->private;
425         struct filter_obd *filter = &dev->u.filter;
426
427         lprocfs_oh_clear(&filter->fo_r_pages);
428         lprocfs_oh_clear(&filter->fo_w_pages);
429         lprocfs_oh_clear(&filter->fo_r_discont_pages);
430         lprocfs_oh_clear(&filter->fo_w_discont_pages);
431         lprocfs_oh_clear(&filter->fo_r_discont_blocks);
432         lprocfs_oh_clear(&filter->fo_w_discont_blocks);
433
434         return len;
435 }
436
437 struct file_operations filter_brw_stats_fops = {
438         .owner   = THIS_MODULE,
439         .open    = filter_brw_stats_seq_open,
440         .read    = seq_read,
441         .write   = filter_brw_stats_seq_write,
442         .llseek  = seq_lseek,
443         .release = seq_release,
444 };
445
446 int lproc_filter_attach_seqstat(struct obd_device *dev)
447 {
448         return lprocfs_obd_seq_create(dev, "brw_stats", 0444,
449                                       &filter_brw_stats_fops, dev);
450 }
451
452
453
454 #endif /* LPROCFS */
455 LPROCFS_INIT_VARS(filter, lprocfs_module_vars, lprocfs_obd_vars)