Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mdd/mdd_lproc.c
35  *
36  * Lustre Metadata Server (mdd) routines
37  *
38  * Author: Wang Di <wangdi@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <linux/module.h>
47 #include <linux/poll.h>
48 #include <obd.h>
49 #include <obd_class.h>
50 #include <lustre_ver.h>
51 #include <obd_support.h>
52 #include <lprocfs_status.h>
53 #include <lu_time.h>
54 #include <lustre_log.h>
55 #include <lustre/lustre_idl.h>
56 #include <libcfs/libcfs_string.h>
57
58 #include "mdd_internal.h"
59
60 static const char *mdd_counter_names[LPROC_MDD_NR] = {
61 };
62
63 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
64 {
65         struct lprocfs_static_vars lvars;
66         struct lu_device    *ld = &mdd->mdd_md_dev.md_lu_dev;
67         struct obd_type     *type;
68         int                  rc;
69         ENTRY;
70
71         type = ld->ld_type->ldt_obd_type;
72
73         LASSERT(name != NULL);
74         LASSERT(type != NULL);
75
76         /* Find the type procroot and add the proc entry for this device */
77         lprocfs_mdd_init_vars(&lvars);
78         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
79                                                lvars.obd_vars, mdd);
80         if (IS_ERR(mdd->mdd_proc_entry)) {
81                 rc = PTR_ERR(mdd->mdd_proc_entry);
82                 CERROR("Error %d setting up lprocfs for %s\n",
83                        rc, name);
84                 mdd->mdd_proc_entry = NULL;
85                 GOTO(out, rc);
86         }
87
88         rc = lu_time_init(&mdd->mdd_stats,
89                           mdd->mdd_proc_entry,
90                           mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
91
92         EXIT;
93 out:
94         if (rc)
95                mdd_procfs_fini(mdd);
96         return rc;
97 }
98
99 int mdd_procfs_fini(struct mdd_device *mdd)
100 {
101         if (mdd->mdd_stats)
102                 lu_time_fini(&mdd->mdd_stats);
103
104         if (mdd->mdd_proc_entry) {
105                  lprocfs_remove(&mdd->mdd_proc_entry);
106                  mdd->mdd_proc_entry = NULL;
107         }
108         RETURN(0);
109 }
110
111 void mdd_lprocfs_time_start(const struct lu_env *env)
112 {
113         lu_lprocfs_time_start(env);
114 }
115
116 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
117                           int idx)
118 {
119         lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
120 }
121
122 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
123                                  unsigned long count, void *data)
124 {
125         struct mdd_device *mdd = data;
126         char kernbuf[20], *end;
127         unsigned long diff = 0;
128
129         if (count > (sizeof(kernbuf) - 1))
130                 return -EINVAL;
131
132         if (cfs_copy_from_user(kernbuf, buffer, count))
133                 return -EFAULT;
134
135         kernbuf[count] = '\0';
136
137         diff = simple_strtoul(kernbuf, &end, 0);
138         if (kernbuf == end)
139                 return -EINVAL;
140
141         mdd->mdd_atime_diff = diff;
142         return count;
143 }
144
145 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
146                                  int count, int *eof, void *data)
147 {
148         struct mdd_device *mdd = data;
149
150         *eof = 1;
151         return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
152 }
153
154
155 /**** changelogs ****/
156 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
157                                      int count, int *eof, void *data)
158 {
159         struct mdd_device *mdd = data;
160         int i = 0, rc = 0;
161
162         *eof = 1;
163         while (i < CL_LAST) {
164                 if (mdd->mdd_cl.mc_mask & (1 << i))
165                         rc += snprintf(page + rc, count - rc, "%s ",
166                                        changelog_type2str(i));
167                 i++;
168         }
169         return rc;
170 }
171
172 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
173                                      unsigned long count, void *data)
174 {
175         struct mdd_device *mdd = data;
176         char *kernbuf;
177         int rc;
178         ENTRY;
179
180         if (count >= CFS_PAGE_SIZE)
181                 RETURN(-EINVAL);
182         OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
183         if (kernbuf == NULL)
184                 RETURN(-ENOMEM);
185         if (cfs_copy_from_user(kernbuf, buffer, count))
186                 GOTO(out, rc = -EFAULT);
187         kernbuf[count] = 0;
188
189         rc = cfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
190                           CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
191         if (rc == 0)
192                 rc = count;
193 out:
194         OBD_FREE(kernbuf, CFS_PAGE_SIZE);
195         return rc;
196 }
197
198 struct cucb_data {
199         char *page;
200         int count;
201         int idx;
202 };
203
204 static int lprocfs_changelog_users_cb(struct llog_handle *llh,
205                                       struct llog_rec_hdr *hdr, void *data)
206 {
207         struct llog_changelog_user_rec *rec;
208         struct cucb_data *cucb = (struct cucb_data *)data;
209
210         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
211
212         rec = (struct llog_changelog_user_rec *)hdr;
213
214         cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
215                               CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
216                               rec->cur_id, rec->cur_endrec);
217         if (cucb->idx >= cucb->count)
218                 return -ENOSPC;
219
220         return 0;
221 }
222
223 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
224                                       int count, int *eof, void *data)
225 {
226         struct mdd_device *mdd = data;
227         struct llog_ctxt *ctxt;
228         struct cucb_data cucb;
229         __u64 cur;
230
231         *eof = 1;
232
233         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
234         if (ctxt == NULL)
235                 return -ENXIO;
236         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
237
238         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
239         cur = mdd->mdd_cl.mc_index;
240         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
241
242         cucb.count = count;
243         cucb.page = page;
244         cucb.idx = 0;
245
246         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
247                               "current index: "LPU64"\n", cur);
248
249         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
250                               "%-5s %s\n", "ID", "index");
251
252         llog_cat_process(ctxt->loc_handle, lprocfs_changelog_users_cb,
253                          &cucb, 0, 0);
254
255         llog_ctxt_put(ctxt);
256         return cucb.idx;
257 }
258
259 #ifdef HAVE_QUOTA_SUPPORT
260 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
261                                      int count, int *eof, void *data)
262 {
263         struct mdd_device *mdd = data;
264         return lprocfs_quota_rd_type(page, start, off, count, eof,
265                                      mdd->mdd_obd_dev);
266 }
267
268 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
269                                      unsigned long count, void *data)
270 {
271         struct mdd_device *mdd = data;
272         return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
273 }
274 #endif
275
276 static int lprocfs_rd_sync_perm(char *page, char **start, off_t off,
277                                 int count, int *eof, void *data)
278 {
279         struct mdd_device *mdd = data;
280
281         LASSERT(mdd != NULL);
282         return snprintf(page, count, "%d\n", mdd->mdd_sync_permission);
283 }
284
285 static int lprocfs_wr_sync_perm(struct file *file, const char *buffer,
286                                 unsigned long count, void *data)
287 {
288         struct mdd_device *mdd = data;
289         int val, rc;
290
291         LASSERT(mdd != NULL);
292         rc = lprocfs_write_helper(buffer, count, &val);
293         if (rc)
294                 return rc;
295
296         mdd->mdd_sync_permission = !!val;
297         return count;
298 }
299
300 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
301         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
302         { "changelog_mask",  lprocfs_rd_changelog_mask,
303                              lprocfs_wr_changelog_mask, 0 },
304         { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
305 #ifdef HAVE_QUOTA_SUPPORT
306         { "quota_type",      mdd_lprocfs_quota_rd_type,
307                              mdd_lprocfs_quota_wr_type, 0 },
308 #endif
309         { "sync_permission", lprocfs_rd_sync_perm, lprocfs_wr_sync_perm, 0 },
310         { 0 }
311 };
312
313 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
314         { "num_refs",   lprocfs_rd_numrefs, 0, 0 },
315         { 0 }
316 };
317
318 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
319 {
320         lvars->module_vars  = lprocfs_mdd_module_vars;
321         lvars->obd_vars     = lprocfs_mdd_obd_vars;
322 }
323