Whamcloud - gitweb
b291ffdbdc76c1ea5e0cf53ce9b723c6d233ee38
[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 __user *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         seq_printf(m, "%lu\n", mdd->mdd_atime_diff);
82         return 0;
83 }
84 LPROC_SEQ_FOPS(mdd_atime_diff);
85
86 /**** changelogs ****/
87 static int mdd_changelog_mask_seq_show(struct seq_file *m, void *data)
88 {
89         struct mdd_device *mdd = m->private;
90         int i = 0;
91
92         while (i < CL_LAST) {
93                 if (mdd->mdd_cl.mc_mask & (1 << i))
94                         seq_printf(m, "%s ", changelog_type2str(i));
95                 i++;
96         }
97         return 0;
98 }
99
100 static ssize_t
101 mdd_changelog_mask_seq_write(struct file *file, const char __user *buffer,
102                              size_t count, loff_t *off)
103 {
104         struct seq_file *m = file->private_data;
105         struct mdd_device *mdd = m->private;
106         char *kernbuf;
107         int rc;
108         ENTRY;
109
110         if (count >= PAGE_SIZE)
111                 RETURN(-EINVAL);
112         OBD_ALLOC(kernbuf, PAGE_SIZE);
113         if (kernbuf == NULL)
114                 RETURN(-ENOMEM);
115         if (copy_from_user(kernbuf, buffer, count))
116                 GOTO(out, rc = -EFAULT);
117         kernbuf[count] = 0;
118
119         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
120                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
121         if (rc == 0)
122                 rc = count;
123 out:
124         OBD_FREE(kernbuf, PAGE_SIZE);
125         return rc;
126 }
127 LPROC_SEQ_FOPS(mdd_changelog_mask);
128
129 static int lprocfs_changelog_users_cb(const struct lu_env *env,
130                                       struct llog_handle *llh,
131                                       struct llog_rec_hdr *hdr, void *data)
132 {
133         struct llog_changelog_user_rec *rec;
134         struct seq_file *m = data;
135
136         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
137
138         rec = (struct llog_changelog_user_rec *)hdr;
139
140         seq_printf(m, CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
141                    rec->cur_id, rec->cur_endrec);
142         return 0;
143 }
144
145 static int mdd_changelog_users_seq_show(struct seq_file *m, void *data)
146 {
147         struct lu_env            env;
148         struct mdd_device       *mdd = m->private;
149         struct llog_ctxt        *ctxt;
150         __u64                    cur;
151         int                      rc;
152
153         ctxt = llog_get_context(mdd2obd_dev(mdd),
154                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
155         if (ctxt == NULL)
156                 return -ENXIO;
157         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
158
159         rc = lu_env_init(&env, LCT_LOCAL);
160         if (rc) {
161                 llog_ctxt_put(ctxt);
162                 return rc;
163         }
164
165         spin_lock(&mdd->mdd_cl.mc_lock);
166         cur = mdd->mdd_cl.mc_index;
167         spin_unlock(&mdd->mdd_cl.mc_lock);
168
169         seq_printf(m, "current index: "LPU64"\n", cur);
170         seq_printf(m, "%-5s %s\n", "ID", "index");
171
172         llog_cat_process(&env, ctxt->loc_handle, lprocfs_changelog_users_cb,
173                          m, 0, 0);
174
175         lu_env_fini(&env);
176         llog_ctxt_put(ctxt);
177         return 0;
178 }
179 LPROC_SEQ_FOPS_RO(mdd_changelog_users);
180
181 static int mdd_changelog_size_ctxt(const struct lu_env *env,
182                                    struct mdd_device *mdd,
183                                    int index, __u64 *val)
184 {
185         struct llog_ctxt        *ctxt;
186
187         ctxt = llog_get_context(mdd2obd_dev(mdd),
188                                 index);
189         if (ctxt == NULL)
190                 return -ENXIO;
191
192         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
193                 CERROR("%s: ChangeLog has wrong flags: rc = %d\n",
194                        ctxt->loc_obd->obd_name, -EINVAL);
195                 llog_ctxt_put(ctxt);
196                 return -EINVAL;
197         }
198
199         *val += llog_cat_size(env, ctxt->loc_handle);
200
201         llog_ctxt_put(ctxt);
202
203         return 0;
204 }
205
206 static int mdd_changelog_size_seq_show(struct seq_file *m, void *data)
207 {
208         struct lu_env            env;
209         struct mdd_device       *mdd = m->private;
210         __u64                    tmp = 0;
211         int                      rc;
212
213         rc = lu_env_init(&env, LCT_LOCAL);
214         if (rc)
215                 return rc;
216
217         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_ORIG_CTXT, &tmp);
218         if (rc) {
219                 lu_env_fini(&env);
220                 return rc;
221         }
222
223         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_USER_ORIG_CTXT,
224                                      &tmp);
225
226         seq_printf(m, LPU64"\n", tmp);
227         lu_env_fini(&env);
228         return rc;
229 }
230 LPROC_SEQ_FOPS_RO(mdd_changelog_size);
231
232 static int mdd_sync_perm_seq_show(struct seq_file *m, void *data)
233 {
234         struct mdd_device *mdd = m->private;
235
236         LASSERT(mdd != NULL);
237         seq_printf(m, "%d\n", mdd->mdd_sync_permission);
238         return 0;
239 }
240
241 static ssize_t
242 mdd_sync_perm_seq_write(struct file *file, const char __user *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         int rc;
248         __s64 val;
249
250         LASSERT(mdd != NULL);
251         rc = lprocfs_str_to_s64(buffer, count, &val);
252         if (rc)
253                 return rc;
254
255         mdd->mdd_sync_permission = !!val;
256
257         return count;
258 }
259 LPROC_SEQ_FOPS(mdd_sync_perm);
260
261 static int mdd_lfsck_speed_limit_seq_show(struct seq_file *m, void *data)
262 {
263         struct mdd_device *mdd = m->private;
264
265         LASSERT(mdd != NULL);
266         return lfsck_get_speed(m, mdd->mdd_bottom);
267 }
268
269 static ssize_t
270 mdd_lfsck_speed_limit_seq_write(struct file *file, const char __user *buffer,
271                                 size_t count, loff_t *off)
272 {
273         struct seq_file *m = file->private_data;
274         struct mdd_device *mdd = m->private;
275         __s64 val;
276         int rc;
277
278         LASSERT(mdd != NULL);
279         rc = lprocfs_str_to_s64(buffer, count, &val);
280         if (rc != 0)
281                 return rc;
282         if (val < 0 || val > INT_MAX)
283                 return -ERANGE;
284
285         rc = lfsck_set_speed(mdd->mdd_bottom, val);
286         return rc != 0 ? rc : count;
287 }
288 LPROC_SEQ_FOPS(mdd_lfsck_speed_limit);
289
290 static int mdd_lfsck_async_windows_seq_show(struct seq_file *m, void *data)
291 {
292         struct mdd_device *mdd = m->private;
293
294         LASSERT(mdd != NULL);
295         return lfsck_get_windows(m, mdd->mdd_bottom);
296 }
297
298 static ssize_t
299 mdd_lfsck_async_windows_seq_write(struct file *file, const char __user *buffer,
300                                   size_t count, loff_t *off)
301 {
302         struct seq_file   *m = file->private_data;
303         struct mdd_device *mdd = m->private;
304         __s64              val;
305         int                rc;
306
307         LASSERT(mdd != NULL);
308         rc = lprocfs_str_to_s64(buffer, count, &val);
309         if (rc)
310                 return rc;
311         if (val < 0 || val > INT_MAX)
312                 return -ERANGE;
313
314         rc = lfsck_set_windows(mdd->mdd_bottom, val);
315
316         return rc != 0 ? rc : count;
317 }
318 LPROC_SEQ_FOPS(mdd_lfsck_async_windows);
319
320 static int mdd_lfsck_namespace_seq_show(struct seq_file *m, void *data)
321 {
322         struct mdd_device *mdd = m->private;
323
324         LASSERT(mdd != NULL);
325
326         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_NAMESPACE);
327 }
328 LPROC_SEQ_FOPS_RO(mdd_lfsck_namespace);
329
330 static int mdd_lfsck_layout_seq_show(struct seq_file *m, void *data)
331 {
332         struct mdd_device *mdd = m->private;
333
334         LASSERT(mdd != NULL);
335
336         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_LAYOUT);
337 }
338 LPROC_SEQ_FOPS_RO(mdd_lfsck_layout);
339
340 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
341         { .name =       "atime_diff",
342           .fops =       &mdd_atime_diff_fops            },
343         { .name =       "changelog_mask",
344           .fops =       &mdd_changelog_mask_fops        },
345         { .name =       "changelog_users",
346           .fops =       &mdd_changelog_users_fops       },
347         { .name =       "changelog_size",
348           .fops =       &mdd_changelog_size_fops        },
349         { .name =       "sync_permission",
350           .fops =       &mdd_sync_perm_fops             },
351         { .name =       "lfsck_speed_limit",
352           .fops =       &mdd_lfsck_speed_limit_fops     },
353         { .name =       "lfsck_async_windows",
354           .fops =       &mdd_lfsck_async_windows_fops   },
355         { .name =       "lfsck_namespace",
356           .fops =       &mdd_lfsck_namespace_fops       },
357         { .name =       "lfsck_layout",
358           .fops =       &mdd_lfsck_layout_fops          },
359         { NULL }
360 };
361
362 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
363 {
364         struct obd_device *obd = mdd2obd_dev(mdd);
365         struct obd_type   *type;
366         int                rc;
367         ENTRY;
368
369         /* at the moment there is no linkage between lu_type
370          * and obd_type, so we lookup obd_type this way */
371         type = class_search_type(LUSTRE_MDD_NAME);
372
373         LASSERT(name != NULL);
374         LASSERT(type != NULL);
375         LASSERT(obd  != NULL);
376
377         /* Find the type procroot and add the proc entry for this device */
378         obd->obd_vars = lprocfs_mdd_obd_vars;
379         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
380                                                obd->obd_vars, mdd);
381         if (IS_ERR(mdd->mdd_proc_entry)) {
382                 rc = PTR_ERR(mdd->mdd_proc_entry);
383                 CERROR("Error %d setting up lprocfs for %s\n",
384                        rc, name);
385                 mdd->mdd_proc_entry = NULL;
386                 GOTO(out, rc);
387         }
388         rc = 0;
389         EXIT;
390 out:
391         if (rc)
392                 mdd_procfs_fini(mdd);
393         return rc;
394 }
395
396 void mdd_procfs_fini(struct mdd_device *mdd)
397 {
398         if (mdd->mdd_proc_entry)
399                 lprocfs_remove(&mdd->mdd_proc_entry);
400 }