Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / lustre / mdd / mdd_lproc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #include <linux/poll.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
55 #include <lu_time.h>
56 #include <lustre_log.h>
57 #include <lustre/lustre_idl.h>
58 #include <libcfs/libcfs_string.h>
59
60 #include "mdd_internal.h"
61
62 static const char *mdd_counter_names[LPROC_MDD_NR] = {
63 };
64
65 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
66 {
67         struct lprocfs_static_vars lvars;
68         struct lu_device    *ld = &mdd->mdd_md_dev.md_lu_dev;
69         struct obd_type     *type;
70         int                  rc;
71         ENTRY;
72
73         type = ld->ld_type->ldt_obd_type;
74
75         LASSERT(name != NULL);
76         LASSERT(type != NULL);
77
78         /* Find the type procroot and add the proc entry for this device */
79         lprocfs_mdd_init_vars(&lvars);
80         mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
81                                                lvars.obd_vars, mdd);
82         if (IS_ERR(mdd->mdd_proc_entry)) {
83                 rc = PTR_ERR(mdd->mdd_proc_entry);
84                 CERROR("Error %d setting up lprocfs for %s\n",
85                        rc, name);
86                 mdd->mdd_proc_entry = NULL;
87                 GOTO(out, rc);
88         }
89
90         rc = lu_time_init(&mdd->mdd_stats,
91                           mdd->mdd_proc_entry,
92                           mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
93
94         EXIT;
95 out:
96         if (rc)
97                mdd_procfs_fini(mdd);
98         return rc;
99 }
100
101 int mdd_procfs_fini(struct mdd_device *mdd)
102 {
103         if (mdd->mdd_stats)
104                 lu_time_fini(&mdd->mdd_stats);
105
106         if (mdd->mdd_proc_entry) {
107                  lprocfs_remove(&mdd->mdd_proc_entry);
108                  mdd->mdd_proc_entry = NULL;
109         }
110         RETURN(0);
111 }
112
113 void mdd_lprocfs_time_start(const struct lu_env *env)
114 {
115         lu_lprocfs_time_start(env);
116 }
117
118 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
119                           int idx)
120 {
121         lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
122 }
123
124 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
125                                  unsigned long count, void *data)
126 {
127         struct mdd_device *mdd = data;
128         char kernbuf[20], *end;
129         unsigned long diff = 0;
130
131         if (count > (sizeof(kernbuf) - 1))
132                 return -EINVAL;
133
134         if (cfs_copy_from_user(kernbuf, buffer, count))
135                 return -EFAULT;
136
137         kernbuf[count] = '\0';
138
139         diff = simple_strtoul(kernbuf, &end, 0);
140         if (kernbuf == end)
141                 return -EINVAL;
142
143         mdd->mdd_atime_diff = diff;
144         return count;
145 }
146
147 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
148                                  int count, int *eof, void *data)
149 {
150         struct mdd_device *mdd = data;
151
152         *eof = 1;
153         return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
154 }
155
156
157 /**** changelogs ****/
158 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
159                                      int count, int *eof, void *data)
160 {
161         struct mdd_device *mdd = data;
162         int i = 0, rc = 0;
163
164         *eof = 1;
165         while (i < CL_LAST) {
166                 if (mdd->mdd_cl.mc_mask & (1 << i))
167                         rc += snprintf(page + rc, count - rc, "%s ",
168                                        changelog_type2str(i));
169                 i++;
170         }
171         return rc;
172 }
173
174 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
175                                      unsigned long count, void *data)
176 {
177         struct mdd_device *mdd = data;
178         char *kernbuf;
179         int rc;
180         ENTRY;
181
182         if (count >= CFS_PAGE_SIZE)
183                 RETURN(-EINVAL);
184         OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
185         if (kernbuf == NULL)
186                 RETURN(-ENOMEM);
187         if (cfs_copy_from_user(kernbuf, buffer, count))
188                 GOTO(out, rc = -EFAULT);
189         kernbuf[count] = 0;
190
191         rc = libcfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
192                              CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
193         if (rc == 0)
194                 rc = count;
195 out:
196         OBD_FREE(kernbuf, CFS_PAGE_SIZE);
197         return rc;
198 }
199
200 struct cucb_data {
201         char *page;
202         int count;
203         int idx;
204 };
205
206 static int lprocfs_changelog_users_cb(struct llog_handle *llh,
207                                       struct llog_rec_hdr *hdr, void *data)
208 {
209         struct llog_changelog_user_rec *rec;
210         struct cucb_data *cucb = (struct cucb_data *)data;
211
212         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
213
214         rec = (struct llog_changelog_user_rec *)hdr;
215
216         cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
217                               CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
218                               rec->cur_id, rec->cur_endrec);
219         if (cucb->idx >= cucb->count)
220                 return -ENOSPC;
221
222         return 0;
223 }
224
225 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
226                                       int count, int *eof, void *data)
227 {
228         struct mdd_device *mdd = data;
229         struct llog_ctxt *ctxt;
230         struct cucb_data cucb;
231         __u64 cur;
232
233         *eof = 1;
234
235         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
236         if (ctxt == NULL)
237                 return -ENXIO;
238         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
239
240         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
241         cur = mdd->mdd_cl.mc_index;
242         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
243
244         cucb.count = count;
245         cucb.page = page;
246         cucb.idx = 0;
247
248         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
249                               "current index: "LPU64"\n", cur);
250
251         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
252                               "%-5s %s\n", "ID", "index");
253
254         llog_cat_process(ctxt->loc_handle, lprocfs_changelog_users_cb,
255                          &cucb, 0, 0);
256
257         llog_ctxt_put(ctxt);
258         return cucb.idx;
259 }
260
261 #ifdef HAVE_QUOTA_SUPPORT
262 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
263                                      int count, int *eof, void *data)
264 {
265         struct mdd_device *mdd = data;
266         return lprocfs_quota_rd_type(page, start, off, count, eof,
267                                      mdd->mdd_obd_dev);
268 }
269
270 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
271                                      unsigned long count, void *data)
272 {
273         struct mdd_device *mdd = data;
274         return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
275 }
276 #endif
277
278 static int lprocfs_rd_sync_perm(char *page, char **start, off_t off,
279                                 int count, int *eof, void *data)
280 {
281         struct mdd_device *mdd = data;
282
283         LASSERT(mdd != NULL);
284         return snprintf(page, count, "%d\n", mdd->mdd_sync_permission);
285 }
286
287 static int lprocfs_wr_sync_perm(struct file *file, const char *buffer,
288                                 unsigned long count, void *data)
289 {
290         struct mdd_device *mdd = data;
291         int val, rc;
292
293         LASSERT(mdd != NULL);
294         rc = lprocfs_write_helper(buffer, count, &val);
295         if (rc)
296                 return rc;
297
298         mdd->mdd_sync_permission = !!val;
299         return count;
300 }
301
302 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
303         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
304         { "changelog_mask",  lprocfs_rd_changelog_mask,
305                              lprocfs_wr_changelog_mask, 0 },
306         { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
307 #ifdef HAVE_QUOTA_SUPPORT
308         { "quota_type",      mdd_lprocfs_quota_rd_type,
309                              mdd_lprocfs_quota_wr_type, 0 },
310 #endif
311         { "sync_permission", lprocfs_rd_sync_perm, lprocfs_wr_sync_perm, 0 },
312         { 0 }
313 };
314
315 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
316         { "num_refs",   lprocfs_rd_numrefs, 0, 0 },
317         { 0 }
318 };
319
320 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
321 {
322         lvars->module_vars  = lprocfs_mdd_module_vars;
323         lvars->obd_vars     = lprocfs_mdd_obd_vars;
324 }
325