Whamcloud - gitweb
LU-3319 procfs: move FLD proc handling over to seq_file
[fs/lustre-release.git] / lustre / fld / lproc_fld.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  * Copyright (c) 2012, 2013, Intel Corporation.
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/fld/lproc_fld.c
37  *
38  * FLD (FIDs Location Database)
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  *      Di Wang <di.wang@whamcloud.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_FLD
45
46 #ifdef __KERNEL__
47 # include <libcfs/libcfs.h>
48 # include <linux/module.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 #endif
52
53 #include <dt_object.h>
54 #include <obd_support.h>
55 #include <lustre_fld.h>
56 #include <lustre_fid.h>
57 #include "fld_internal.h"
58 #include <md_object.h>
59
60 #ifdef LPROCFS
61 static int
62 fld_proc_targets_seq_show(struct seq_file *m, void *unused)
63 {
64         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
65         struct lu_fld_target *target;
66         ENTRY;
67
68         LASSERT(fld != NULL);
69
70         spin_lock(&fld->lcf_lock);
71         cfs_list_for_each_entry(target,
72                                 &fld->lcf_targets, ft_chain)
73                 seq_printf(m, "%s\n", fld_target_name(target));
74         spin_unlock(&fld->lcf_lock);
75         RETURN(0);
76 }
77
78 static int
79 fld_proc_hash_seq_show(struct seq_file *m, void *unused)
80 {
81         struct lu_client_fld *fld = (struct lu_client_fld *)m->private;
82         ENTRY;
83
84         LASSERT(fld != NULL);
85
86         spin_lock(&fld->lcf_lock);
87         seq_printf(m, "%s\n", fld->lcf_hash->fh_name);
88         spin_unlock(&fld->lcf_lock);
89
90         RETURN(0);
91 }
92
93 static ssize_t
94 fld_proc_hash_seq_write(struct file *file, const char *buffer,
95                         size_t count, loff_t *off)
96 {
97         struct lu_client_fld *fld = ((struct seq_file *)file->private_data)->private;
98         struct lu_fld_hash *hash = NULL;
99         int i;
100         ENTRY;
101
102         LASSERT(fld != NULL);
103
104         for (i = 0; fld_hash[i].fh_name != NULL; i++) {
105                 if (count != strlen(fld_hash[i].fh_name))
106                         continue;
107
108                 if (!strncmp(fld_hash[i].fh_name, buffer, count)) {
109                         hash = &fld_hash[i];
110                         break;
111                 }
112         }
113
114         if (hash != NULL) {
115                 spin_lock(&fld->lcf_lock);
116                 fld->lcf_hash = hash;
117                 spin_unlock(&fld->lcf_lock);
118
119                 CDEBUG(D_INFO, "%s: Changed hash to \"%s\"\n",
120                        fld->lcf_name, hash->fh_name);
121         }
122
123         RETURN(count);
124 }
125
126 static ssize_t
127 lprocfs_cache_flush_seq_write(struct file *file, const char *buffer,
128                                size_t count, loff_t *pos)
129 {
130         struct lu_client_fld *fld = ((struct seq_file *)file->private_data)->private;
131         ENTRY;
132
133         LASSERT(fld != NULL);
134
135         fld_cache_flush(fld->lcf_cache);
136
137         CDEBUG(D_INFO, "%s: Lookup cache is flushed\n", fld->lcf_name);
138
139         RETURN(count);
140 }
141
142 LPROC_SEQ_FOPS_RO(fld_proc_targets);
143 LPROC_SEQ_FOPS(fld_proc_hash);
144 LPROC_SEQ_FOPS_WO_TYPE(fld, cache_flush);
145
146 struct lprocfs_seq_vars fld_client_proc_list[] = {
147         { .name =       "targets",
148           .fops =       &fld_proc_targets_fops  },
149         { .name =       "hash",
150           .fops =       &fld_proc_hash_fops     },
151         { .name =       "cache_flush",
152           .fops =       &fld_cache_flush_fops   },
153         { NULL }
154 };
155
156 #ifdef HAVE_SERVER_SUPPORT
157 struct fld_seq_param {
158         struct lu_env           fsp_env;
159         struct dt_it            *fsp_it;
160         struct lu_server_fld    *fsp_fld;
161         unsigned int            fsp_stop:1;
162 };
163
164 static void *fldb_seq_start(struct seq_file *p, loff_t *pos)
165 {
166         struct fld_seq_param    *param = p->private;
167         struct lu_server_fld    *fld;
168         struct dt_object        *obj;
169         const struct dt_it_ops  *iops;
170
171         if (param == NULL || param->fsp_stop)
172                 return NULL;
173
174         fld = param->fsp_fld;
175         obj = fld->lsf_obj;
176         LASSERT(obj != NULL);
177         iops = &obj->do_index_ops->dio_it;
178
179         iops->load(&param->fsp_env, param->fsp_it, *pos);
180
181         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
182         return param;
183 }
184
185 static void fldb_seq_stop(struct seq_file *p, void *v)
186 {
187         struct fld_seq_param    *param = p->private;
188         const struct dt_it_ops  *iops;
189         struct lu_server_fld    *fld;
190         struct dt_object        *obj;
191
192         if (param == NULL)
193                 return;
194
195         fld = param->fsp_fld;
196         obj = fld->lsf_obj;
197         LASSERT(obj != NULL);
198         iops = &obj->do_index_ops->dio_it;
199
200         iops->put(&param->fsp_env, param->fsp_it);
201 }
202
203 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
204 {
205         struct fld_seq_param    *param = p->private;
206         struct lu_server_fld    *fld;
207         struct dt_object        *obj;
208         const struct dt_it_ops  *iops;
209         int                     rc;
210
211         if (param == NULL || param->fsp_stop)
212                 return NULL;
213
214         fld = param->fsp_fld;
215         obj = fld->lsf_obj;
216         LASSERT(obj != NULL);
217         iops = &obj->do_index_ops->dio_it;
218
219         rc = iops->next(&param->fsp_env, param->fsp_it);
220         if (rc > 0) {
221                 param->fsp_stop = 1;
222                 return NULL;
223         }
224
225         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
226         return param;
227 }
228
229 static int fldb_seq_show(struct seq_file *p, void *v)
230 {
231         struct fld_seq_param    *param = p->private;
232         struct lu_server_fld    *fld;
233         struct dt_object        *obj;
234         const struct dt_it_ops  *iops;
235         struct fld_thread_info  *info;
236         struct lu_seq_range     *fld_rec;
237         int                     rc;
238
239         if (param == NULL || param->fsp_stop)
240                 return 0;
241
242         fld = param->fsp_fld;
243         obj = fld->lsf_obj;
244         LASSERT(obj != NULL);
245         iops = &obj->do_index_ops->dio_it;
246
247         info = lu_context_key_get(&param->fsp_env.le_ctx,
248                                   &fld_thread_key);
249         fld_rec = &info->fti_rec;
250         rc = iops->rec(&param->fsp_env, param->fsp_it,
251                        (struct dt_rec *)fld_rec, 0);
252         if (rc != 0) {
253                 CERROR("%s:read record error: rc %d\n",
254                        fld->lsf_name, rc);
255         } else if (fld_rec->lsr_start != 0) {
256                 range_be_to_cpu(fld_rec, fld_rec);
257                 rc = seq_printf(p, DRANGE"\n", PRANGE(fld_rec));
258         }
259
260         return rc;
261 }
262
263 struct seq_operations fldb_sops = {
264         .start = fldb_seq_start,
265         .stop = fldb_seq_stop,
266         .next = fldb_seq_next,
267         .show = fldb_seq_show,
268 };
269
270 static int fldb_seq_open(struct inode *inode, struct file *file)
271 {
272         struct seq_file         *seq;
273         struct lu_server_fld    *fld = (struct lu_server_fld *)PDE_DATA(inode);
274         struct dt_object        *obj;
275         const struct dt_it_ops  *iops;
276         struct fld_seq_param    *param = NULL;
277         int                     env_init = 0;
278         int                     rc;
279
280         LPROCFS_ENTRY_CHECK(PDE(inode));
281         rc = seq_open(file, &fldb_sops);
282         if (rc)
283                 GOTO(out, rc);
284
285         obj = fld->lsf_obj;
286         if (obj == NULL) {
287                 seq = file->private_data;
288                 seq->private = NULL;
289                 return 0;
290         }
291
292         OBD_ALLOC_PTR(param);
293         if (param == NULL)
294                 GOTO(out, rc = -ENOMEM);
295
296         rc = lu_env_init(&param->fsp_env, LCT_MD_THREAD);
297         if (rc != 0)
298                 GOTO(out, rc);
299
300         env_init = 1;
301         iops = &obj->do_index_ops->dio_it;
302         param->fsp_it = iops->init(&param->fsp_env, obj, 0, NULL);
303         if (IS_ERR(param->fsp_it))
304                 GOTO(out, rc = PTR_ERR(param->fsp_it));
305
306         param->fsp_fld = fld;
307         param->fsp_stop = 0;
308
309         seq = file->private_data;
310         seq->private = param;
311 out:
312         if (rc != 0) {
313                 if (env_init == 1)
314                         lu_env_fini(&param->fsp_env);
315                 if (param != NULL)
316                         OBD_FREE_PTR(param);
317         }
318         return rc;
319 }
320
321 static int fldb_seq_release(struct inode *inode, struct file *file)
322 {
323         struct seq_file         *seq = file->private_data;
324         struct fld_seq_param    *param;
325         struct lu_server_fld    *fld;
326         struct dt_object        *obj;
327         const struct dt_it_ops  *iops;
328
329         param = seq->private;
330         if (param == NULL) {
331                 lprocfs_seq_release(inode, file);
332                 return 0;
333         }
334
335         fld = param->fsp_fld;
336         obj = fld->lsf_obj;
337         LASSERT(obj != NULL);
338         iops = &obj->do_index_ops->dio_it;
339
340         LASSERT(iops != NULL);
341         LASSERT(param->fsp_it != NULL);
342         iops->fini(&param->fsp_env, param->fsp_it);
343         lu_env_fini(&param->fsp_env);
344         OBD_FREE_PTR(param);
345         lprocfs_seq_release(inode, file);
346
347         return 0;
348 }
349
350 const struct file_operations fld_proc_seq_fops = {
351         .owner   = THIS_MODULE,
352         .open    = fldb_seq_open,
353         .read    = seq_read,
354         .release = fldb_seq_release,
355 };
356
357 struct lprocfs_seq_vars fld_server_proc_list[] = {
358         { NULL }
359 };
360
361 # endif /* HAVE_SERVER_SUPPORT */
362
363 #endif /* LPROCFS */