Whamcloud - gitweb
LU-1302 llog: llog test update and fixes
[fs/lustre-release.git] / lustre / mdd / mdd_lproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mdd/mdd_lproc.c
35  *
36  * Lustre Metadata Server (mdd) routines
37  *
38  * Author: Wang Di <wangdi@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lprocfs_status.h>
47 #include <lu_time.h>
48 #include <libcfs/libcfs_string.h>
49
50 #include "mdd_internal.h"
51
52 static const char *mdd_counter_names[LPROC_MDD_NR] = {
53 };
54
55 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
56 {
57         struct lprocfs_static_vars lvars;
58         struct lu_device    *ld = &mdd->mdd_md_dev.md_lu_dev;
59         struct obd_type     *type;
60         int                  rc;
61         ENTRY;
62
63         type = ld->ld_type->ldt_obd_type;
64
65         LASSERT(name != NULL);
66         LASSERT(type != NULL);
67
68         /* Find the type procroot and add the proc entry for this device */
69         lprocfs_mdd_init_vars(&lvars);
70         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
71                                                lvars.obd_vars, mdd);
72         if (IS_ERR(mdd->mdd_proc_entry)) {
73                 rc = PTR_ERR(mdd->mdd_proc_entry);
74                 CERROR("Error %d setting up lprocfs for %s\n",
75                        rc, name);
76                 mdd->mdd_proc_entry = NULL;
77                 GOTO(out, rc);
78         }
79
80         rc = lu_time_init(&mdd->mdd_stats,
81                           mdd->mdd_proc_entry,
82                           mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
83
84         EXIT;
85 out:
86         if (rc)
87                mdd_procfs_fini(mdd);
88         return rc;
89 }
90
91 int mdd_procfs_fini(struct mdd_device *mdd)
92 {
93         if (mdd->mdd_stats)
94                 lu_time_fini(&mdd->mdd_stats);
95
96         if (mdd->mdd_proc_entry) {
97                  lprocfs_remove(&mdd->mdd_proc_entry);
98                  mdd->mdd_proc_entry = NULL;
99         }
100         RETURN(0);
101 }
102
103 void mdd_lprocfs_time_start(const struct lu_env *env)
104 {
105         lu_lprocfs_time_start(env);
106 }
107
108 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
109                           int idx)
110 {
111         lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
112 }
113
114 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
115                                  unsigned long count, void *data)
116 {
117         struct mdd_device *mdd = data;
118         char kernbuf[20], *end;
119         unsigned long diff = 0;
120
121         if (count > (sizeof(kernbuf) - 1))
122                 return -EINVAL;
123
124         if (cfs_copy_from_user(kernbuf, buffer, count))
125                 return -EFAULT;
126
127         kernbuf[count] = '\0';
128
129         diff = simple_strtoul(kernbuf, &end, 0);
130         if (kernbuf == end)
131                 return -EINVAL;
132
133         mdd->mdd_atime_diff = diff;
134         return count;
135 }
136
137 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
138                                  int count, int *eof, void *data)
139 {
140         struct mdd_device *mdd = data;
141
142         *eof = 1;
143         return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
144 }
145
146
147 /**** changelogs ****/
148 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
149                                      int count, int *eof, void *data)
150 {
151         struct mdd_device *mdd = data;
152         int i = 0, rc = 0;
153
154         *eof = 1;
155         while (i < CL_LAST) {
156                 if (mdd->mdd_cl.mc_mask & (1 << i))
157                         rc += snprintf(page + rc, count - rc, "%s ",
158                                        changelog_type2str(i));
159                 i++;
160         }
161         return rc;
162 }
163
164 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
165                                      unsigned long count, void *data)
166 {
167         struct mdd_device *mdd = data;
168         char *kernbuf;
169         int rc;
170         ENTRY;
171
172         if (count >= CFS_PAGE_SIZE)
173                 RETURN(-EINVAL);
174         OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
175         if (kernbuf == NULL)
176                 RETURN(-ENOMEM);
177         if (cfs_copy_from_user(kernbuf, buffer, count))
178                 GOTO(out, rc = -EFAULT);
179         kernbuf[count] = 0;
180
181         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
182                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
183         if (rc == 0)
184                 rc = count;
185 out:
186         OBD_FREE(kernbuf, CFS_PAGE_SIZE);
187         return rc;
188 }
189
190 struct cucb_data {
191         char *page;
192         int count;
193         int idx;
194 };
195
196 static int lprocfs_changelog_users_cb(const struct lu_env *env,
197                                       struct llog_handle *llh,
198                                       struct llog_rec_hdr *hdr, void *data)
199 {
200         struct llog_changelog_user_rec *rec;
201         struct cucb_data *cucb = (struct cucb_data *)data;
202
203         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
204
205         rec = (struct llog_changelog_user_rec *)hdr;
206
207         cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
208                               CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
209                               rec->cur_id, rec->cur_endrec);
210         if (cucb->idx >= cucb->count)
211                 return -ENOSPC;
212
213         return 0;
214 }
215
216 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
217                                       int count, int *eof, void *data)
218 {
219         struct mdd_device *mdd = data;
220         struct llog_ctxt *ctxt;
221         struct cucb_data cucb;
222         __u64 cur;
223
224         *eof = 1;
225
226         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
227         if (ctxt == NULL)
228                 return -ENXIO;
229         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
230
231         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
232         cur = mdd->mdd_cl.mc_index;
233         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
234
235         cucb.count = count;
236         cucb.page = page;
237         cucb.idx = 0;
238
239         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
240                               "current index: "LPU64"\n", cur);
241
242         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
243                               "%-5s %s\n", "ID", "index");
244
245         llog_cat_process(NULL, ctxt->loc_handle, lprocfs_changelog_users_cb,
246                          &cucb, 0, 0);
247
248         llog_ctxt_put(ctxt);
249         return cucb.idx;
250 }
251
252 #ifdef HAVE_QUOTA_SUPPORT
253 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
254                                      int count, int *eof, void *data)
255 {
256         struct mdd_device *mdd = data;
257         return lprocfs_quota_rd_type(page, start, off, count, eof,
258                                      mdd->mdd_obd_dev);
259 }
260
261 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
262                                      unsigned long count, void *data)
263 {
264         struct mdd_device *mdd = data;
265         return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
266 }
267 #endif
268
269 static int lprocfs_rd_sync_perm(char *page, char **start, off_t off,
270                                 int count, int *eof, void *data)
271 {
272         struct mdd_device *mdd = data;
273
274         LASSERT(mdd != NULL);
275         return snprintf(page, count, "%d\n", mdd->mdd_sync_permission);
276 }
277
278 static int lprocfs_wr_sync_perm(struct file *file, const char *buffer,
279                                 unsigned long count, void *data)
280 {
281         struct mdd_device *mdd = data;
282         int val, rc;
283
284         LASSERT(mdd != NULL);
285         rc = lprocfs_write_helper(buffer, count, &val);
286         if (rc)
287                 return rc;
288
289         mdd->mdd_sync_permission = !!val;
290         return count;
291 }
292
293 static int lprocfs_rd_lfsck_speed_limit(char *page, char **start, off_t off,
294                                         int count, int *eof, void *data)
295 {
296         struct mdd_device *mdd = data;
297
298         LASSERT(mdd != NULL);
299         *eof = 1;
300         return snprintf(page, count, "%u\n", mdd->mdd_lfsck.ml_speed_limit);
301 }
302
303 static int lprocfs_wr_lfsck_speed_limit(struct file *file, const char *buffer,
304                                         unsigned long count, void *data)
305 {
306         struct mdd_device *mdd = data;
307         struct md_lfsck *lfsck;
308         __u32 val;
309         int rc;
310
311         LASSERT(mdd != NULL);
312         rc = lprocfs_write_helper(buffer, count, &val);
313         if (rc)
314                 return rc;
315
316         lfsck = &mdd->mdd_lfsck;
317         if (val != lfsck->ml_speed_limit)
318                 mdd_lfsck_set_speed(lfsck, val);
319         return count;
320 }
321
322 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
323         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
324         { "changelog_mask",  lprocfs_rd_changelog_mask,
325                              lprocfs_wr_changelog_mask, 0 },
326         { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
327 #ifdef HAVE_QUOTA_SUPPORT
328         { "quota_type",      mdd_lprocfs_quota_rd_type,
329                              mdd_lprocfs_quota_wr_type, 0 },
330 #endif
331         { "sync_permission", lprocfs_rd_sync_perm, lprocfs_wr_sync_perm, 0 },
332         { "lfsck_speed_limit", lprocfs_rd_lfsck_speed_limit,
333                                lprocfs_wr_lfsck_speed_limit, 0 },
334         { 0 }
335 };
336
337 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
338         { "num_refs",   lprocfs_rd_numrefs, 0, 0 },
339         { 0 }
340 };
341
342 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
343 {
344         lvars->module_vars  = lprocfs_mdd_module_vars;
345         lvars->obd_vars     = lprocfs_mdd_obd_vars;
346 }
347