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