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