Whamcloud - gitweb
LU-2034 changelog: redo changelog using OSD llog
[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 obd_type     *type;
59         int                  rc;
60         ENTRY;
61
62         /* at the moment there is no linkage between lu_type
63          * and obd_type, so we lookup obd_type this way */
64         type = class_search_type(LUSTRE_MDD_NAME);
65
66         LASSERT(name != NULL);
67         LASSERT(type != NULL);
68
69         /* Find the type procroot and add the proc entry for this device */
70         lprocfs_mdd_init_vars(&lvars);
71         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
72                                                lvars.obd_vars, mdd);
73         if (IS_ERR(mdd->mdd_proc_entry)) {
74                 rc = PTR_ERR(mdd->mdd_proc_entry);
75                 CERROR("Error %d setting up lprocfs for %s\n",
76                        rc, name);
77                 mdd->mdd_proc_entry = NULL;
78                 GOTO(out, rc);
79         }
80
81         rc = lu_time_init(&mdd->mdd_stats,
82                           mdd->mdd_proc_entry,
83                           mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
84
85         EXIT;
86 out:
87         if (rc)
88                mdd_procfs_fini(mdd);
89         return rc;
90 }
91
92 int mdd_procfs_fini(struct mdd_device *mdd)
93 {
94         if (mdd->mdd_stats)
95                 lu_time_fini(&mdd->mdd_stats);
96
97         if (mdd->mdd_proc_entry) {
98                  lprocfs_remove(&mdd->mdd_proc_entry);
99                  mdd->mdd_proc_entry = NULL;
100         }
101         RETURN(0);
102 }
103
104 void mdd_lprocfs_time_start(const struct lu_env *env)
105 {
106         lu_lprocfs_time_start(env);
107 }
108
109 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
110                           int idx)
111 {
112         lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
113 }
114
115 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
116                                  unsigned long count, void *data)
117 {
118         struct mdd_device *mdd = data;
119         char kernbuf[20], *end;
120         unsigned long diff = 0;
121
122         if (count > (sizeof(kernbuf) - 1))
123                 return -EINVAL;
124
125         if (cfs_copy_from_user(kernbuf, buffer, count))
126                 return -EFAULT;
127
128         kernbuf[count] = '\0';
129
130         diff = simple_strtoul(kernbuf, &end, 0);
131         if (kernbuf == end)
132                 return -EINVAL;
133
134         mdd->mdd_atime_diff = diff;
135         return count;
136 }
137
138 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
139                                  int count, int *eof, void *data)
140 {
141         struct mdd_device *mdd = data;
142
143         *eof = 1;
144         return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
145 }
146
147
148 /**** changelogs ****/
149 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
150                                      int count, int *eof, void *data)
151 {
152         struct mdd_device *mdd = data;
153         int i = 0, rc = 0;
154
155         *eof = 1;
156         while (i < CL_LAST) {
157                 if (mdd->mdd_cl.mc_mask & (1 << i))
158                         rc += snprintf(page + rc, count - rc, "%s ",
159                                        changelog_type2str(i));
160                 i++;
161         }
162         return rc;
163 }
164
165 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
166                                      unsigned long count, void *data)
167 {
168         struct mdd_device *mdd = data;
169         char *kernbuf;
170         int rc;
171         ENTRY;
172
173         if (count >= CFS_PAGE_SIZE)
174                 RETURN(-EINVAL);
175         OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
176         if (kernbuf == NULL)
177                 RETURN(-ENOMEM);
178         if (cfs_copy_from_user(kernbuf, buffer, count))
179                 GOTO(out, rc = -EFAULT);
180         kernbuf[count] = 0;
181
182         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
183                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
184         if (rc == 0)
185                 rc = count;
186 out:
187         OBD_FREE(kernbuf, CFS_PAGE_SIZE);
188         return rc;
189 }
190
191 struct cucb_data {
192         char *page;
193         int count;
194         int idx;
195 };
196
197 static int lprocfs_changelog_users_cb(const struct lu_env *env,
198                                       struct llog_handle *llh,
199                                       struct llog_rec_hdr *hdr, void *data)
200 {
201         struct llog_changelog_user_rec *rec;
202         struct cucb_data *cucb = (struct cucb_data *)data;
203
204         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
205
206         rec = (struct llog_changelog_user_rec *)hdr;
207
208         cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
209                               CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
210                               rec->cur_id, rec->cur_endrec);
211         if (cucb->idx >= cucb->count)
212                 return -ENOSPC;
213
214         return 0;
215 }
216
217 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
218                                       int count, int *eof, void *data)
219 {
220         struct lu_env            env;
221         struct mdd_device       *mdd = data;
222         struct llog_ctxt        *ctxt;
223         struct cucb_data         cucb;
224         __u64                    cur;
225         int                      rc;
226
227         *eof = 1;
228
229         ctxt = llog_get_context(mdd2obd_dev(mdd),
230                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
231         if (ctxt == NULL)
232                 return -ENXIO;
233         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
234
235         rc = lu_env_init(&env, LCT_LOCAL);
236         if (rc) {
237                 llog_ctxt_put(ctxt);
238                 return rc;
239         }
240
241         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
242         cur = mdd->mdd_cl.mc_index;
243         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
244
245         cucb.count = count;
246         cucb.page = page;
247         cucb.idx = 0;
248
249         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
250                               "current index: "LPU64"\n", cur);
251
252         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
253                               "%-5s %s\n", "ID", "index");
254
255         llog_cat_process(&env, ctxt->loc_handle, lprocfs_changelog_users_cb,
256                          &cucb, 0, 0);
257
258         lu_env_fini(&env);
259         llog_ctxt_put(ctxt);
260         return cucb.idx;
261 }
262
263 static int lprocfs_rd_sync_perm(char *page, char **start, off_t off,
264                                 int count, int *eof, void *data)
265 {
266         struct mdd_device *mdd = data;
267
268         LASSERT(mdd != NULL);
269         return snprintf(page, count, "%d\n", mdd->mdd_sync_permission);
270 }
271
272 static int lprocfs_wr_sync_perm(struct file *file, const char *buffer,
273                                 unsigned long count, void *data)
274 {
275         struct mdd_device *mdd = data;
276         int val, rc;
277
278         LASSERT(mdd != NULL);
279         rc = lprocfs_write_helper(buffer, count, &val);
280         if (rc)
281                 return rc;
282
283         mdd->mdd_sync_permission = !!val;
284         return count;
285 }
286
287 static int lprocfs_rd_lfsck_speed_limit(char *page, char **start, off_t off,
288                                         int count, int *eof, void *data)
289 {
290         struct mdd_device *mdd = data;
291
292         LASSERT(mdd != NULL);
293         *eof = 1;
294         return snprintf(page, count, "%u\n", mdd->mdd_lfsck.ml_speed_limit);
295 }
296
297 static int lprocfs_wr_lfsck_speed_limit(struct file *file, const char *buffer,
298                                         unsigned long count, void *data)
299 {
300         struct mdd_device *mdd = data;
301         struct md_lfsck *lfsck;
302         __u32 val;
303         int rc;
304
305         LASSERT(mdd != NULL);
306         rc = lprocfs_write_helper(buffer, count, &val);
307         if (rc)
308                 return rc;
309
310         lfsck = &mdd->mdd_lfsck;
311         if (val != lfsck->ml_speed_limit)
312                 mdd_lfsck_set_speed(lfsck, val);
313         return count;
314 }
315
316 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
317         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
318         { "changelog_mask",  lprocfs_rd_changelog_mask,
319                              lprocfs_wr_changelog_mask, 0 },
320         { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
321         { "sync_permission", lprocfs_rd_sync_perm, lprocfs_wr_sync_perm, 0 },
322         { "lfsck_speed_limit", lprocfs_rd_lfsck_speed_limit,
323                                lprocfs_wr_lfsck_speed_limit, 0 },
324         { 0 }
325 };
326
327 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
328         { "num_refs",   lprocfs_rd_numrefs, 0, 0 },
329         { 0 }
330 };
331
332 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
333 {
334         lvars->module_vars  = lprocfs_mdd_module_vars;
335         lvars->obd_vars     = lprocfs_mdd_obd_vars;
336 }
337