Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_lproc.c
33  *
34  * Lustre Metadata Server (mdd) routines
35  *
36  * Author: Wang Di <wangdi@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lprocfs_status.h>
45 #include <libcfs/libcfs_string.h>
46 #include "mdd_internal.h"
47
48 static ssize_t
49 mdd_atime_diff_seq_write(struct file *file, const char __user *buffer,
50                          size_t count, loff_t *off)
51 {
52         struct seq_file *m = file->private_data;
53         struct mdd_device *mdd = m->private;
54         char kernbuf[20], *end;
55         unsigned long diff = 0;
56
57         if (count > (sizeof(kernbuf) - 1))
58                 return -EINVAL;
59
60         if (copy_from_user(kernbuf, buffer, count))
61                 return -EFAULT;
62
63         kernbuf[count] = '\0';
64
65         diff = simple_strtoul(kernbuf, &end, 0);
66         if (kernbuf == end)
67                 return -EINVAL;
68
69         mdd->mdd_atime_diff = diff;
70         return count;
71 }
72
73 static int mdd_atime_diff_seq_show(struct seq_file *m, void *data)
74 {
75         struct mdd_device *mdd = m->private;
76
77         seq_printf(m, "%lu\n", mdd->mdd_atime_diff);
78         return 0;
79 }
80 LPROC_SEQ_FOPS(mdd_atime_diff);
81
82 /**** changelogs ****/
83 static int mdd_changelog_mask_seq_show(struct seq_file *m, void *data)
84 {
85         struct mdd_device *mdd = m->private;
86         int i = 0;
87
88         while (i < CL_LAST) {
89                 if (mdd->mdd_cl.mc_mask & (1 << i))
90                         seq_printf(m, "%s ", changelog_type2str(i));
91                 i++;
92         }
93         return 0;
94 }
95
96 static ssize_t
97 mdd_changelog_mask_seq_write(struct file *file, const char __user *buffer,
98                              size_t count, loff_t *off)
99 {
100         struct seq_file *m = file->private_data;
101         struct mdd_device *mdd = m->private;
102         char *kernbuf;
103         int rc;
104         ENTRY;
105
106         if (count >= PAGE_SIZE)
107                 RETURN(-EINVAL);
108         OBD_ALLOC(kernbuf, PAGE_SIZE);
109         if (kernbuf == NULL)
110                 RETURN(-ENOMEM);
111         if (copy_from_user(kernbuf, buffer, count))
112                 GOTO(out, rc = -EFAULT);
113         kernbuf[count] = 0;
114
115         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
116                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
117         if (rc == 0)
118                 rc = count;
119 out:
120         OBD_FREE(kernbuf, PAGE_SIZE);
121         return rc;
122 }
123 LPROC_SEQ_FOPS(mdd_changelog_mask);
124
125 static int lprocfs_changelog_users_cb(const struct lu_env *env,
126                                       struct llog_handle *llh,
127                                       struct llog_rec_hdr *hdr, void *data)
128 {
129         struct llog_changelog_user_rec *rec;
130         struct seq_file *m = data;
131
132         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
133
134         rec = (struct llog_changelog_user_rec *)hdr;
135
136         seq_printf(m, CHANGELOG_USER_PREFIX"%-3d %llu\n",
137                    rec->cur_id, rec->cur_endrec);
138         return 0;
139 }
140
141 static int mdd_changelog_users_seq_show(struct seq_file *m, void *data)
142 {
143         struct lu_env            env;
144         struct mdd_device       *mdd = m->private;
145         struct llog_ctxt        *ctxt;
146         __u64                    cur;
147         int                      rc;
148
149         ctxt = llog_get_context(mdd2obd_dev(mdd),
150                                 LLOG_CHANGELOG_USER_ORIG_CTXT);
151         if (ctxt == NULL)
152                 return -ENXIO;
153         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
154
155         rc = lu_env_init(&env, LCT_LOCAL);
156         if (rc) {
157                 llog_ctxt_put(ctxt);
158                 return rc;
159         }
160
161         spin_lock(&mdd->mdd_cl.mc_lock);
162         cur = mdd->mdd_cl.mc_index;
163         spin_unlock(&mdd->mdd_cl.mc_lock);
164
165         seq_printf(m, "current index: %llu\n", cur);
166         seq_printf(m, "%-5s %s\n", "ID", "index");
167
168         llog_cat_process(&env, ctxt->loc_handle, lprocfs_changelog_users_cb,
169                          m, 0, 0);
170
171         lu_env_fini(&env);
172         llog_ctxt_put(ctxt);
173         return 0;
174 }
175 LPROC_SEQ_FOPS_RO(mdd_changelog_users);
176
177 static int mdd_changelog_size_ctxt(const struct lu_env *env,
178                                    struct mdd_device *mdd,
179                                    int index, __u64 *val)
180 {
181         struct llog_ctxt        *ctxt;
182
183         ctxt = llog_get_context(mdd2obd_dev(mdd),
184                                 index);
185         if (ctxt == NULL)
186                 return -ENXIO;
187
188         if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
189                 CERROR("%s: ChangeLog has wrong flags: rc = %d\n",
190                        ctxt->loc_obd->obd_name, -EINVAL);
191                 llog_ctxt_put(ctxt);
192                 return -EINVAL;
193         }
194
195         *val += llog_cat_size(env, ctxt->loc_handle);
196
197         llog_ctxt_put(ctxt);
198
199         return 0;
200 }
201
202 static int mdd_changelog_size_seq_show(struct seq_file *m, void *data)
203 {
204         struct lu_env            env;
205         struct mdd_device       *mdd = m->private;
206         __u64                    tmp = 0;
207         int                      rc;
208
209         rc = lu_env_init(&env, LCT_LOCAL);
210         if (rc)
211                 return rc;
212
213         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_ORIG_CTXT, &tmp);
214         if (rc) {
215                 lu_env_fini(&env);
216                 return rc;
217         }
218
219         rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_USER_ORIG_CTXT,
220                                      &tmp);
221
222         seq_printf(m, "%llu\n", tmp);
223         lu_env_fini(&env);
224         return rc;
225 }
226 LPROC_SEQ_FOPS_RO(mdd_changelog_size);
227
228 static int mdd_sync_perm_seq_show(struct seq_file *m, void *data)
229 {
230         struct mdd_device *mdd = m->private;
231
232         LASSERT(mdd != NULL);
233         seq_printf(m, "%d\n", mdd->mdd_sync_permission);
234         return 0;
235 }
236
237 static ssize_t
238 mdd_sync_perm_seq_write(struct file *file, const char __user *buffer,
239                         size_t count, loff_t *off)
240 {
241         struct seq_file *m = file->private_data;
242         struct mdd_device *mdd = m->private;
243         int rc;
244         __s64 val;
245
246         LASSERT(mdd != NULL);
247         rc = lprocfs_str_to_s64(buffer, count, &val);
248         if (rc)
249                 return rc;
250
251         mdd->mdd_sync_permission = !!val;
252
253         return count;
254 }
255 LPROC_SEQ_FOPS(mdd_sync_perm);
256
257 static int mdd_lfsck_speed_limit_seq_show(struct seq_file *m, void *data)
258 {
259         struct mdd_device *mdd = m->private;
260
261         LASSERT(mdd != NULL);
262         return lfsck_get_speed(m, mdd->mdd_bottom);
263 }
264
265 static ssize_t
266 mdd_lfsck_speed_limit_seq_write(struct file *file, const char __user *buffer,
267                                 size_t count, loff_t *off)
268 {
269         struct seq_file *m = file->private_data;
270         struct mdd_device *mdd = m->private;
271         __s64 val;
272         int rc;
273
274         LASSERT(mdd != NULL);
275         rc = lprocfs_str_to_s64(buffer, count, &val);
276         if (rc != 0)
277                 return rc;
278         if (val < 0 || val > INT_MAX)
279                 return -ERANGE;
280
281         rc = lfsck_set_speed(mdd->mdd_bottom, val);
282         return rc != 0 ? rc : count;
283 }
284 LPROC_SEQ_FOPS(mdd_lfsck_speed_limit);
285
286 static int mdd_lfsck_async_windows_seq_show(struct seq_file *m, void *data)
287 {
288         struct mdd_device *mdd = m->private;
289
290         LASSERT(mdd != NULL);
291         return lfsck_get_windows(m, mdd->mdd_bottom);
292 }
293
294 static ssize_t
295 mdd_lfsck_async_windows_seq_write(struct file *file, const char __user *buffer,
296                                   size_t count, loff_t *off)
297 {
298         struct seq_file   *m = file->private_data;
299         struct mdd_device *mdd = m->private;
300         __s64              val;
301         int                rc;
302
303         LASSERT(mdd != NULL);
304         rc = lprocfs_str_to_s64(buffer, count, &val);
305         if (rc)
306                 return rc;
307         if (val < 0 || val > INT_MAX)
308                 return -ERANGE;
309
310         rc = lfsck_set_windows(mdd->mdd_bottom, val);
311
312         return rc != 0 ? rc : count;
313 }
314 LPROC_SEQ_FOPS(mdd_lfsck_async_windows);
315
316 static int mdd_lfsck_namespace_seq_show(struct seq_file *m, void *data)
317 {
318         struct mdd_device *mdd = m->private;
319
320         LASSERT(mdd != NULL);
321
322         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_NAMESPACE);
323 }
324 LPROC_SEQ_FOPS_RO(mdd_lfsck_namespace);
325
326 static int mdd_lfsck_layout_seq_show(struct seq_file *m, void *data)
327 {
328         struct mdd_device *mdd = m->private;
329
330         LASSERT(mdd != NULL);
331
332         return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_LAYOUT);
333 }
334 LPROC_SEQ_FOPS_RO(mdd_lfsck_layout);
335
336 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
337         { .name =       "atime_diff",
338           .fops =       &mdd_atime_diff_fops            },
339         { .name =       "changelog_mask",
340           .fops =       &mdd_changelog_mask_fops        },
341         { .name =       "changelog_users",
342           .fops =       &mdd_changelog_users_fops       },
343         { .name =       "changelog_size",
344           .fops =       &mdd_changelog_size_fops        },
345         { .name =       "sync_permission",
346           .fops =       &mdd_sync_perm_fops             },
347         { .name =       "lfsck_speed_limit",
348           .fops =       &mdd_lfsck_speed_limit_fops     },
349         { .name =       "lfsck_async_windows",
350           .fops =       &mdd_lfsck_async_windows_fops   },
351         { .name =       "lfsck_namespace",
352           .fops =       &mdd_lfsck_namespace_fops       },
353         { .name =       "lfsck_layout",
354           .fops =       &mdd_lfsck_layout_fops          },
355         { NULL }
356 };
357
358 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
359 {
360         struct obd_device *obd = mdd2obd_dev(mdd);
361         struct obd_type   *type;
362         int                rc;
363         ENTRY;
364
365         /* at the moment there is no linkage between lu_type
366          * and obd_type, so we lookup obd_type this way */
367         type = class_search_type(LUSTRE_MDD_NAME);
368
369         LASSERT(name != NULL);
370         LASSERT(type != NULL);
371         LASSERT(obd  != NULL);
372
373         /* Find the type procroot and add the proc entry for this device */
374         obd->obd_vars = lprocfs_mdd_obd_vars;
375         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
376                                                obd->obd_vars, mdd);
377         if (IS_ERR(mdd->mdd_proc_entry)) {
378                 rc = PTR_ERR(mdd->mdd_proc_entry);
379                 CERROR("Error %d setting up lprocfs for %s\n",
380                        rc, name);
381                 mdd->mdd_proc_entry = NULL;
382                 GOTO(out, rc);
383         }
384         rc = 0;
385         EXIT;
386 out:
387         if (rc)
388                 mdd_procfs_fini(mdd);
389         return rc;
390 }
391
392 void mdd_procfs_fini(struct mdd_device *mdd)
393 {
394         if (mdd->mdd_proc_entry)
395                 lprocfs_remove(&mdd->mdd_proc_entry);
396 }