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