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