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