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