Whamcloud - gitweb
6e043bfe4639085e94359787694a98a95fbf72fd
[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  2008 Sun Microsystems, Inc. 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 (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 DECLARE_CHANGELOG_NAMES;
159
160 const char *changelog_bit2str(int bit)
161 {
162         if (bit < CL_LAST)
163                 return changelog_str[bit];
164         return NULL;
165 }
166
167 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
168                                      int count, int *eof, void *data)
169 {
170         struct mdd_device *mdd = data;
171         int i = 0, rc = 0;
172
173         *eof = 1;
174         while (i < CL_LAST) {
175                 if (mdd->mdd_cl.mc_mask & (1 << i))
176                         rc += snprintf(page + rc, count - rc, "%s ",
177                                        changelog_str[i]);
178                 i++;
179         }
180         return rc;
181 }
182
183 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
184                                      unsigned long count, void *data)
185 {
186         struct mdd_device *mdd = data;
187         char *kernbuf;
188         int rc;
189         ENTRY;
190
191         if (count >= CFS_PAGE_SIZE)
192                 RETURN(-EINVAL);
193         OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
194         if (kernbuf == NULL)
195                 RETURN(-ENOMEM);
196         if (copy_from_user(kernbuf, buffer, count))
197                 GOTO(out, rc = -EFAULT);
198         kernbuf[count] = 0;
199
200         rc = libcfs_str2mask(kernbuf, changelog_bit2str, &mdd->mdd_cl.mc_mask,
201                              CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
202         if (rc == 0)
203                 rc = count;
204 out:
205         OBD_FREE(kernbuf, CFS_PAGE_SIZE);
206         return rc;
207 }
208
209 struct cucb_data {
210         char *page;
211         int count;
212         int idx;
213 };
214
215 static int lprocfs_changelog_users_cb(struct llog_handle *llh,
216                                       struct llog_rec_hdr *hdr, void *data)
217 {
218         struct llog_changelog_user_rec *rec;
219         struct cucb_data *cucb = (struct cucb_data *)data;
220
221         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
222
223         rec = (struct llog_changelog_user_rec *)hdr;
224
225         cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
226                               CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
227                               rec->cur_id, rec->cur_endrec);
228         if (cucb->idx >= cucb->count)
229                 return -ENOSPC;
230
231         return 0;
232 }
233
234 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
235                                       int count, int *eof, void *data)
236 {
237         struct mdd_device *mdd = data;
238         struct llog_ctxt *ctxt;
239         struct cucb_data cucb;
240         __u64 cur;
241
242         *eof = 1;
243
244         ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
245         if (ctxt == NULL)
246                 return -ENXIO;
247         LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
248
249         spin_lock(&mdd->mdd_cl.mc_lock);
250         cur = mdd->mdd_cl.mc_index;
251         spin_unlock(&mdd->mdd_cl.mc_lock);
252
253         cucb.count = count;
254         cucb.page = page;
255         cucb.idx = 0;
256
257         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
258                               "current index: "LPU64"\n", cur);
259
260         cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
261                               "%-5s %s\n", "ID", "index");
262
263         llog_cat_process(ctxt->loc_handle, lprocfs_changelog_users_cb,
264                          &cucb, 0, 0);
265
266         llog_ctxt_put(ctxt);
267         return cucb.idx;
268 }
269
270 /* non-seq version for direct calling by class_process_proc_param */
271 static int mdd_changelog_write(struct file *file, const char *buffer,
272                                unsigned long count, void *data)
273 {
274         struct mdd_device *mdd = (struct mdd_device *)data;
275         char kernbuf[32];
276         char *end;
277         int rc;
278
279         if (count > (sizeof(kernbuf) - 1))
280                 goto out_usage;
281
282         count = min_t(unsigned long, count, sizeof(kernbuf));
283         if (copy_from_user(kernbuf, buffer, count))
284                 return -EFAULT;
285
286         kernbuf[count] = '\0';
287         /* strip trailing newline from "echo blah" */
288         if (kernbuf[count - 1] == '\n')
289                 kernbuf[count - 1] = '\0';
290
291         if (strcmp(kernbuf, "on") == 0) {
292                 LCONSOLE_INFO("changelog on\n");
293                 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
294                         CERROR("Changelogs cannot be enabled due to error "
295                                "condition.\n");
296                 } else {
297                         spin_lock(&mdd->mdd_cl.mc_lock);
298                         mdd->mdd_cl.mc_flags |= CLM_ON;
299                         spin_unlock(&mdd->mdd_cl.mc_lock);
300                         rc = mdd_changelog_write_header(mdd, CLM_START);
301                         if (rc)
302                               return rc;
303                 }
304         } else if (strcmp(kernbuf, "off") == 0) {
305                 LCONSOLE_INFO("changelog off\n");
306                 rc = mdd_changelog_write_header(mdd, CLM_FINI);
307                 if (rc)
308                       return rc;
309                 spin_lock(&mdd->mdd_cl.mc_lock);
310                 mdd->mdd_cl.mc_flags &= ~CLM_ON;
311                 spin_unlock(&mdd->mdd_cl.mc_lock);
312         } else {
313                 /* purge to an index */
314                 long long unsigned endrec;
315
316                 endrec = (long long)simple_strtoull(kernbuf, &end, 0);
317                 if (end == kernbuf)
318                         goto out_usage;
319
320                 LCONSOLE_INFO("changelog purge to %llu\n", endrec);
321
322                 rc = mdd_changelog_llog_cancel(mdd, endrec);
323                 if (rc < 0)
324                         return rc;
325         }
326
327         return count;
328
329 out_usage:
330         CWARN("changelog write usage: [on|off] | <purge_idx (0=all)>\n");
331         return -EINVAL;
332 }
333
334 static ssize_t mdd_changelog_seq_write(struct file *file, const char *buffer,
335                                        size_t count, loff_t *off)
336 {
337         struct seq_file *seq = file->private_data;
338         struct changelog_seq_iter *csi = seq->private;
339         struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
340
341         return mdd_changelog_write(file, buffer, count, mdd);
342 }
343
344 static int mdd_changelog_done(struct changelog_seq_iter *csi)
345 {
346         struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
347         int done = 0;
348
349         spin_lock(&mdd->mdd_cl.mc_lock);
350         done = (csi->csi_endrec >= mdd->mdd_cl.mc_index);
351         spin_unlock(&mdd->mdd_cl.mc_lock);
352         return done;
353 }
354
355 /* handle nonblocking */
356 static ssize_t mdd_changelog_seq_read(struct file *file, char __user *buf,
357                                       size_t count, loff_t *ppos)
358 {
359         struct seq_file *seq = (struct seq_file *)file->private_data;
360         struct changelog_seq_iter *csi = seq->private;
361         int rc;
362         ENTRY;
363
364         if ((file->f_flags & O_NONBLOCK) && mdd_changelog_done(csi))
365                 RETURN(-EAGAIN);
366
367         csi->csi_done = 0;
368         rc = seq_read(file, buf, count, ppos);
369         RETURN(rc);
370 }
371
372 /* handle nonblocking */
373 static unsigned int mdd_changelog_seq_poll(struct file *file, poll_table *wait)
374 {
375         struct seq_file *seq = (struct seq_file *)file->private_data;
376         struct changelog_seq_iter *csi = seq->private;
377         struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
378         ENTRY;
379
380         csi->csi_done = 0;
381         poll_wait(file, &mdd->mdd_cl.mc_waitq, wait);
382         if (!mdd_changelog_done(csi))
383                 RETURN(POLLIN | POLLRDNORM);
384
385         RETURN(0);
386 }
387
388 static int mdd_changelog_seq_open(struct inode *inode, struct file *file)
389 {
390         struct changelog_seq_iter *csi;
391         struct obd_device *obd;
392         int rc;
393         ENTRY;
394
395         rc = changelog_seq_open(inode, file, &csi);
396         if (rc)
397                 RETURN(rc);
398
399         /* The proc file is set up with mdd in data, not obd */
400         obd = mdd2obd_dev((struct mdd_device *)csi->csi_dev);
401         csi->csi_ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
402         if (csi->csi_ctxt == NULL) {
403                 changelog_seq_release(inode, file);
404                 RETURN(-ENOENT);
405         }
406         /* The handle is set up in llog_obd_origin_setup */
407         csi->csi_llh = csi->csi_ctxt->loc_handle;
408         RETURN(rc);
409 }
410
411 static int mdd_changelog_seq_release(struct inode *inode, struct file *file)
412 {
413         struct seq_file *seq = file->private_data;
414         struct changelog_seq_iter *csi = seq->private;
415
416         if (csi && csi->csi_ctxt)
417                 llog_ctxt_put(csi->csi_ctxt);
418
419         return (changelog_seq_release(inode, file));
420 }
421
422 /* mdd changelog proc can handle nonblocking ops and writing to purge recs */
423 struct file_operations mdd_changelog_fops = {
424         .owner   = THIS_MODULE,
425         .open    = mdd_changelog_seq_open,
426         .read    = mdd_changelog_seq_read,
427         .write   = mdd_changelog_seq_write,
428         .llseek  = changelog_seq_lseek,
429         .poll    = mdd_changelog_seq_poll,
430         .release = mdd_changelog_seq_release,
431 };
432
433 #ifdef HAVE_QUOTA_SUPPORT
434 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
435                                      int count, int *eof, void *data)
436 {
437         struct mdd_device *mdd = data;
438         return lprocfs_quota_rd_type(page, start, off, count, eof,
439                                      mdd->mdd_obd_dev);
440 }
441
442 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
443                                      unsigned long count, void *data)
444 {
445         struct mdd_device *mdd = data;
446         return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
447 }
448 #endif
449
450 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
451         { "atime_diff",      lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
452         { "changelog_mask",  lprocfs_rd_changelog_mask,
453                              lprocfs_wr_changelog_mask, 0 },
454         { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
455         { "changelog", 0, mdd_changelog_write, 0, &mdd_changelog_fops, 0600 },
456 #ifdef HAVE_QUOTA_SUPPORT
457         { "quota_type",      mdd_lprocfs_quota_rd_type,
458                              mdd_lprocfs_quota_wr_type, 0 },
459 #endif
460         { 0 }
461 };
462
463 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
464         { "num_refs",   lprocfs_rd_numrefs, 0, 0 },
465         { 0 }
466 };
467
468 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
469 {
470         lvars->module_vars  = lprocfs_mdd_module_vars;
471         lvars->obd_vars     = lprocfs_mdd_obd_vars;
472 }
473