Whamcloud - gitweb
Modify changelog to use LustreNetLink instead of seq_file
[fs/lustre-release.git] / lustre / mdc / lproc_mdc.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 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/version.h>
39 #include <linux/vfs.h>
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <lustre_log.h>
43
44 #ifdef LPROCFS
45
46 static int mdc_rd_max_rpcs_in_flight(char *page, char **start, off_t off,
47                                      int count, int *eof, void *data)
48 {
49         struct obd_device *dev = data;
50         struct client_obd *cli = &dev->u.cli;
51         int rc;
52
53         client_obd_list_lock(&cli->cl_loi_list_lock);
54         rc = snprintf(page, count, "%u\n", cli->cl_max_rpcs_in_flight);
55         client_obd_list_unlock(&cli->cl_loi_list_lock);
56         return rc;
57 }
58
59 static int mdc_wr_max_rpcs_in_flight(struct file *file, const char *buffer,
60                                      unsigned long count, void *data)
61 {
62         struct obd_device *dev = data;
63         struct client_obd *cli = &dev->u.cli;
64         int val, rc;
65
66         rc = lprocfs_write_helper(buffer, count, &val);
67         if (rc)
68                 return rc;
69
70         if (val < 1 || val > MDC_MAX_RIF_MAX)
71                 return -ERANGE;
72
73         client_obd_list_lock(&cli->cl_loi_list_lock);
74         cli->cl_max_rpcs_in_flight = val;
75         client_obd_list_unlock(&cli->cl_loi_list_lock);
76
77         return count;
78 }
79
80 static struct lnl_hdr *changelog_lnl_alloc(int len, int flags)
81 {
82         struct lnl_hdr *lh;
83
84         OBD_ALLOC(lh, len);
85         if (lh == NULL)
86                 RETURN(NULL);
87
88         lh->lnl_magic = LNL_MAGIC;
89         lh->lnl_transport = LNL_TRANSPORT_CHANGELOG;
90         lh->lnl_flags = flags;
91         lh->lnl_msgtype = CL_RECORD;
92         lh->lnl_msglen = len;
93         return lh;
94 }
95
96 #define D_CHANGELOG 0
97
98 static int changelog_show_cb(struct llog_handle *llh, struct llog_rec_hdr *hdr,
99                              void *data)
100 {
101         struct changelog_show *cs = data;
102         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
103         struct lnl_hdr *lh;
104         int len, rc;
105         ENTRY;
106
107         if ((rec->cr_hdr.lrh_type != CHANGELOG_REC) ||
108             (rec->cr.cr_type >= CL_LAST)) {
109                 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
110                        rec->cr.cr_type);
111                 RETURN(-EINVAL);
112         }
113
114         if (rec->cr.cr_index < cs->cs_startrec) {
115                 /* Skip entries earlier than what we are interested in */
116                 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
117                        rec->cr.cr_index, cs->cs_startrec);
118                 RETURN(0);
119         }
120
121         CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
122                " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
123                changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
124                rec->cr.cr_flags & CLF_FLAGMASK,
125                PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
126                rec->cr.cr_namelen, rec->cr.cr_name);
127
128         len = sizeof(*lh) + sizeof(rec->cr) + rec->cr.cr_namelen;
129
130         /* Set up the netlink message */
131         lh = changelog_lnl_alloc(len, cs->cs_flags);
132         if (lh == NULL)
133                 RETURN(-ENOMEM);
134         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
135
136         rc = libcfs_klnl_msg_put(cs->cs_pid, 0, lh);
137         CDEBUG(D_CHANGELOG, "nlmsg pid %d len %d rc %d\n", cs->cs_pid, len, rc);
138
139         OBD_FREE(lh, len);
140
141         RETURN(rc);
142 }
143
144 static int lproc_mdc_wr_changelog(struct file *file, const char *buffer,
145                                   unsigned long count, void *data)
146 {
147         struct obd_device *obd = data;
148         struct llog_ctxt *ctxt;
149         struct llog_handle *llh;
150         struct lnl_hdr *lnlh;
151         struct changelog_show cs = {};
152         int rc;
153
154         CDEBUG(D_CHANGELOG, "file pid %d\n", file->f_owner.pid);
155
156         if (count != sizeof(cs))
157                 return -EINVAL;
158
159         if (copy_from_user(&cs, buffer, sizeof(cs)))
160                 return -EFAULT;
161
162         CDEBUG(D_CHANGELOG, "changelog to pid=%d(%d) start "LPU64"\n",
163                cs.cs_pid, file->f_owner.pid, cs.cs_startrec);
164
165         /* Set up the remote catalog handle */
166         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
167         if (ctxt == NULL)
168                 RETURN(-ENOENT);
169         rc = llog_create(ctxt, &llh, NULL, CHANGELOG_CATALOG);
170         if (rc) {
171                 CERROR("llog_create() failed %d\n", rc);
172                 GOTO(out, rc);
173         }
174         rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
175         if (rc) {
176                 CERROR("llog_init_handle failed %d\n", rc);
177                 GOTO(out, rc);
178         }
179
180         rc = llog_cat_process(llh, changelog_show_cb, &cs, 0, 0);
181
182         /* Send EOF */
183         if ((lnlh = changelog_lnl_alloc(sizeof(*lnlh), cs.cs_flags))) {
184                 lnlh->lnl_msgtype = CL_EOF;
185                 libcfs_klnl_msg_put(cs.cs_pid, 0, lnlh);
186                 OBD_FREE(lnlh, sizeof(*lnlh));
187         }
188
189 out:
190         if (llh)
191                 llog_cat_put(llh);
192         if (ctxt)
193                 llog_ctxt_put(ctxt);
194         if (rc < 0)
195                 return rc;
196         return count;
197 }
198
199 /* temporary for testing */
200 static int mdc_wr_netlink(struct file *file, const char *buffer,
201                           unsigned long count, void *data)
202 {
203         struct obd_device *obd = data;
204         struct lnl_hdr *lh;
205         struct hsm_action_list *hal;
206         struct hsm_action_item *hai;
207         int len;
208         int pid, rc;
209
210         rc = lprocfs_write_helper(buffer, count, &pid);
211         if (rc)
212                 return rc;
213
214         if (pid < 0)
215                 return -ERANGE;
216         CWARN("message to pid %d\n", pid);
217
218         len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
219                 /* for mockup below */ 2 * size_round(sizeof(*hai));
220
221         OBD_ALLOC(lh, len);
222
223         lh->lnl_magic = LNL_MAGIC;
224         lh->lnl_transport = LNL_TRANSPORT_HSM;
225         lh->lnl_msgtype = HMT_ACTION_LIST;
226         lh->lnl_msglen = len;
227
228         hal = (struct hsm_action_list *)(lh + 1);
229         hal->hal_version = HAL_VERSION;
230         hal->hal_archive_num = 1;
231         obd_uuid2fsname(hal->hal_fsname, obd->obd_name, MTI_NAME_MAXLEN);
232
233         /* mock up an action list */
234         hal->hal_count = 2;
235         hai = hai_zero(hal);
236         hai->hai_action = HSMA_ARCHIVE;
237         hai->hai_fid.f_oid = 5;
238         hai->hai_len = sizeof(*hai);
239         hai = hai_next(hai);
240         hai->hai_action = HSMA_RESTORE;
241         hai->hai_fid.f_oid = 10;
242         hai->hai_len = sizeof(*hai);
243
244         /* This works for either broadcast or unicast to a single pid */
245         rc = libcfs_klnl_msg_put(pid, pid == 0 ? LNL_GRP_HSM : 0, lh);
246
247         OBD_FREE(lh, len);
248         if (rc < 0)
249                 return rc;
250         return count;
251 }
252
253 static struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
254         { "uuid",            lprocfs_rd_uuid,        0, 0 },
255         { "ping",            0, lprocfs_wr_ping,     0, 0, 0222 },
256         { "connect_flags",   lprocfs_rd_connect_flags, 0, 0 },
257         { "blocksize",       lprocfs_rd_blksize,     0, 0 },
258         { "kbytestotal",     lprocfs_rd_kbytestotal, 0, 0 },
259         { "kbytesfree",      lprocfs_rd_kbytesfree,  0, 0 },
260         { "kbytesavail",     lprocfs_rd_kbytesavail, 0, 0 },
261         { "filestotal",      lprocfs_rd_filestotal,  0, 0 },
262         { "filesfree",       lprocfs_rd_filesfree,   0, 0 },
263         /*{ "filegroups",      lprocfs_rd_filegroups,  0, 0 },*/
264         { "mds_server_uuid", lprocfs_rd_server_uuid, 0, 0 },
265         { "mds_conn_uuid",   lprocfs_rd_conn_uuid,   0, 0 },
266         { "max_rpcs_in_flight", mdc_rd_max_rpcs_in_flight,
267                                 mdc_wr_max_rpcs_in_flight, 0 },
268         { "quota_resend_count",  lprocfs_rd_quota_resend_count,
269                                  lprocfs_wr_quota_resend_count, 0},
270         { "timeouts",        lprocfs_rd_timeouts,    0, 0 },
271         { "import",          lprocfs_rd_import,      0, 0 },
272         { "state",           lprocfs_rd_state,       0, 0 },
273         { "changelog_trigger",0,lproc_mdc_wr_changelog, 0 },
274         { "hsm_nl",          0, mdc_wr_netlink,      0, 0, 0222 },
275         { 0 }
276 };
277
278 static struct lprocfs_vars lprocfs_mdc_module_vars[] = {
279         { "num_refs",        lprocfs_rd_numrefs,     0, 0 },
280         { 0 }
281 };
282
283 void lprocfs_mdc_init_vars(struct lprocfs_static_vars *lvars)
284 {
285     lvars->module_vars  = lprocfs_mdc_module_vars;
286     lvars->obd_vars     = lprocfs_mdc_obd_vars;
287 }
288 #endif /* LPROCFS */