Whamcloud - gitweb
LU-1187 fld: fix fldb proc after moving range lookup to fld.
[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) 2011, 2012, Whamcloud, Inc.
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 <obd.h>
54 #include <obd_class.h>
55 #include <dt_object.h>
56 #include <md_object.h>
57 #include <obd_support.h>
58 #include <lustre_req_layout.h>
59 #include <lustre_fld.h>
60 #include <lustre_fid.h>
61 #include "fld_internal.h"
62
63 #ifdef LPROCFS
64 static int
65 fld_proc_read_targets(char *page, char **start, off_t off,
66                       int count, int *eof, void *data)
67 {
68         struct lu_client_fld *fld = (struct lu_client_fld *)data;
69         struct lu_fld_target *target;
70         int total = 0, rc;
71         ENTRY;
72
73         LASSERT(fld != NULL);
74
75         spin_lock(&fld->lcf_lock);
76         cfs_list_for_each_entry(target,
77                                 &fld->lcf_targets, ft_chain)
78         {
79                 rc = snprintf(page, count, "%s\n",
80                               fld_target_name(target));
81                 page += rc;
82                 count -= rc;
83                 total += rc;
84                 if (count == 0)
85                         break;
86         }
87         spin_unlock(&fld->lcf_lock);
88         RETURN(total);
89 }
90
91 static int
92 fld_proc_read_hash(char *page, char **start, off_t off,
93                    int count, int *eof, void *data)
94 {
95         struct lu_client_fld *fld = (struct lu_client_fld *)data;
96         int rc;
97         ENTRY;
98
99         LASSERT(fld != NULL);
100
101         spin_lock(&fld->lcf_lock);
102         rc = snprintf(page, count, "%s\n", fld->lcf_hash->fh_name);
103         spin_unlock(&fld->lcf_lock);
104
105         RETURN(rc);
106 }
107
108 static int
109 fld_proc_write_hash(struct file *file, const char *buffer,
110                     unsigned long count, void *data)
111 {
112         struct lu_client_fld *fld = (struct lu_client_fld *)data;
113         struct lu_fld_hash *hash = NULL;
114         int i;
115         ENTRY;
116
117         LASSERT(fld != NULL);
118
119         for (i = 0; fld_hash[i].fh_name != NULL; i++) {
120                 if (count != strlen(fld_hash[i].fh_name))
121                         continue;
122
123                 if (!strncmp(fld_hash[i].fh_name, buffer, count)) {
124                         hash = &fld_hash[i];
125                         break;
126                 }
127         }
128
129         if (hash != NULL) {
130                 spin_lock(&fld->lcf_lock);
131                 fld->lcf_hash = hash;
132                 spin_unlock(&fld->lcf_lock);
133
134                 CDEBUG(D_INFO, "%s: Changed hash to \"%s\"\n",
135                        fld->lcf_name, hash->fh_name);
136         }
137
138         RETURN(count);
139 }
140
141 static int
142 fld_proc_write_cache_flush(struct file *file, const char *buffer,
143                            unsigned long count, void *data)
144 {
145         struct lu_client_fld *fld = (struct lu_client_fld *)data;
146         ENTRY;
147
148         LASSERT(fld != NULL);
149
150         fld_cache_flush(fld->lcf_cache);
151
152         CDEBUG(D_INFO, "%s: Lookup cache is flushed\n", fld->lcf_name);
153
154         RETURN(count);
155 }
156
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         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         fld = param->fsp_fld;
193         obj = fld->lsf_obj;
194         LASSERT(obj != NULL);
195         iops = &obj->do_index_ops->dio_it;
196
197         iops->put(&param->fsp_env, param->fsp_it);
198         return;
199 }
200
201 static void *fldb_seq_next(struct seq_file *p, void *v, loff_t *pos)
202 {
203         struct fld_seq_param    *param = p->private;
204         struct lu_server_fld    *fld;
205         struct dt_object        *obj;
206         const struct dt_it_ops  *iops;
207         int                     rc;
208
209         if (param == NULL || param->fsp_stop)
210                 return NULL;
211
212         fld = param->fsp_fld;
213         obj = fld->lsf_obj;
214         LASSERT(obj != NULL);
215         iops = &obj->do_index_ops->dio_it;
216
217         rc = iops->next(&param->fsp_env, param->fsp_it);
218         if (rc > 0) {
219                 param->fsp_stop = 1;
220                 return NULL;
221         }
222
223         *pos = be64_to_cpu(*(__u64 *)iops->key(&param->fsp_env, param->fsp_it));
224         return param;
225 }
226
227 static int fldb_seq_show(struct seq_file *p, void *v)
228 {
229         struct fld_seq_param    *param = p->private;
230         struct lu_server_fld    *fld;
231         struct dt_object        *obj;
232         const struct dt_it_ops  *iops;
233         struct fld_thread_info  *info;
234         struct lu_seq_range     *fld_rec;
235         int                     rc;
236
237         if (param == NULL || param->fsp_stop)
238                 return 0;
239
240         fld = param->fsp_fld;
241         obj = fld->lsf_obj;
242         LASSERT(obj != NULL);
243         iops = &obj->do_index_ops->dio_it;
244
245         info = lu_context_key_get(&param->fsp_env.le_ctx,
246                                   &fld_thread_key);
247         fld_rec = &info->fti_rec;
248         rc = iops->rec(&param->fsp_env, param->fsp_it,
249                        (struct dt_rec *)fld_rec, 0);
250         if (rc != 0) {
251                 CERROR("%s:read record error: rc %d\n",
252                        fld->lsf_name, rc);
253         } else if (fld_rec->lsr_start != 0) {
254                 range_be_to_cpu(fld_rec, fld_rec);
255                 rc = seq_printf(p, DRANGE"\n", PRANGE(fld_rec));
256         }
257
258         return rc;
259 }
260
261 struct seq_operations fldb_sops = {
262         .start = fldb_seq_start,
263         .stop = fldb_seq_stop,
264         .next = fldb_seq_next,
265         .show = fldb_seq_show,
266 };
267
268 static int fldb_seq_open(struct inode *inode, struct file *file)
269 {
270         struct proc_dir_entry   *dp = PDE(inode);
271         struct seq_file         *seq;
272         struct lu_server_fld    *fld = (struct lu_server_fld *)dp->data;
273         struct dt_object        *obj;
274         const struct dt_it_ops  *iops;
275         struct fld_seq_param    *param = NULL;
276         int                     env_init = 0;
277         int                     rc;
278
279         LPROCFS_ENTRY_AND_CHECK(dp);
280         rc = seq_open(file, &fldb_sops);
281         if (rc)
282                 GOTO(out, rc);
283
284         obj = fld->lsf_obj;
285         if (obj == NULL) {
286                 seq = file->private_data;
287                 seq->private = NULL;
288                 return 0;
289         }
290
291         OBD_ALLOC_PTR(param);
292         if (param == NULL)
293                 GOTO(out, rc = -ENOMEM);
294
295         rc = lu_env_init(&param->fsp_env, LCT_MD_THREAD);
296         if (rc != 0)
297                 GOTO(out, rc);
298
299         env_init = 1;
300         iops = &obj->do_index_ops->dio_it;
301         param->fsp_it = iops->init(&param->fsp_env, obj, 0, NULL);
302         if (IS_ERR(param->fsp_it))
303                 GOTO(out, rc = PTR_ERR(param->fsp_it));
304
305         param->fsp_fld = fld;
306         param->fsp_stop = 0;
307
308         seq = file->private_data;
309         seq->private = param;
310 out:
311         if (rc != 0) {
312                 if (env_init == 1)
313                         lu_env_fini(&param->fsp_env);
314                 if (param != NULL)
315                         OBD_FREE_PTR(param);
316                 LPROCFS_EXIT();
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(obj != NULL);
342         LASSERT(param->fsp_it != NULL);
343         iops->fini(&param->fsp_env, param->fsp_it);
344         lu_env_fini(&param->fsp_env);
345         OBD_FREE_PTR(param);
346         lprocfs_seq_release(inode, file);
347
348         return 0;
349 }
350
351 struct lprocfs_vars fld_server_proc_list[] = {
352         { NULL }};
353
354 struct lprocfs_vars fld_client_proc_list[] = {
355         { "targets",     fld_proc_read_targets, NULL, NULL },
356         { "hash",        fld_proc_read_hash, fld_proc_write_hash, NULL },
357         { "cache_flush", NULL, fld_proc_write_cache_flush, NULL },
358         { NULL }};
359
360 struct file_operations fld_proc_seq_fops = {
361         .owner   = THIS_MODULE,
362         .open    = fldb_seq_open,
363         .read    = seq_read,
364         .release = fldb_seq_release,
365 };
366
367 #endif