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