Whamcloud - gitweb
LU-5275 libcfs: cleanup the proc hash and cfs wrappers
[fs/lustre-release.git] / libcfs / include / libcfs / params_tree.h
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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 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  * API and structure definitions for params_tree.
37  *
38  * Author: LiuYing <emoly.liu@oracle.com>
39  */
40 #ifndef __PARAMS_TREE_H__
41 #define __PARAMS_TREE_H__
42
43 #include <libcfs/libcfs.h>
44
45 #undef LPROCFS
46 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
47 # define LPROCFS
48 #endif
49
50 #ifdef LPROCFS
51 #ifndef HAVE_ONLY_PROCFS_SEQ
52 /* in lprocfs_stat.c, to protect the private data for proc entries */
53 extern struct rw_semaphore              _lprocfs_lock;
54
55 static inline
56 int LPROCFS_ENTRY_CHECK(struct proc_dir_entry *dp)
57 {
58         int deleted = 0;
59
60         spin_lock(&(dp)->pde_unload_lock);
61         if (dp->proc_fops == NULL)
62                 deleted = 1;
63         spin_unlock(&(dp)->pde_unload_lock);
64         if (deleted)
65                 return -ENODEV;
66         return 0;
67 }
68 #define LPROCFS_SRCH_ENTRY()            \
69 do {                                    \
70         down_read(&_lprocfs_lock);      \
71 } while(0)
72
73 #define LPROCFS_SRCH_EXIT()             \
74 do {                                    \
75         up_read(&_lprocfs_lock);        \
76 } while(0)
77
78 #define LPROCFS_WRITE_ENTRY()           \
79 do {                                    \
80         down_write(&_lprocfs_lock);     \
81 } while(0)
82
83 #define LPROCFS_WRITE_EXIT()            \
84 do {                                    \
85         up_write(&_lprocfs_lock);       \
86 } while(0)
87
88 #define PDE_DATA(inode)         PDE(inode)->data
89
90 #else /* New proc api */
91
92 static inline struct proc_dir_entry *PDE(struct inode *inode)
93 {
94         return NULL;
95 }
96
97 static inline
98 int LPROCFS_ENTRY_CHECK(struct proc_dir_entry *dp)
99 {
100         return 0;
101 }
102
103 #define LPROCFS_WRITE_ENTRY() do {} while(0)
104 #define LPROCFS_WRITE_EXIT()  do {} while(0)
105
106 #endif
107
108 #else /* !LPROCFS */
109
110 struct file {
111         void            *param_private;
112         loff_t          param_pos;
113         unsigned int    param_flags;
114 };
115
116 struct inode {
117         void            *param_private;
118 };
119
120 struct poll_table_struct {
121         void            *pad;
122 };
123
124 struct proc_dir_entry {
125         void            *param_data;
126 };
127
128 struct proc_inode {
129         struct proc_dir_entry   *param_pde;
130         struct inode            param_inode;
131 };
132
133 struct seq_operations;
134 struct seq_file {
135         char                            *buf;
136         size_t                          size;
137         size_t                          from;
138         size_t                          count;
139         loff_t                          index;
140         loff_t                          version;
141         struct mutex                    lock;
142         const struct seq_operations     *op;
143         void                            *private;
144 };
145
146 struct seq_operations {
147         void *(*start) (struct seq_file *m, loff_t *pos);
148         void  (*stop) (struct seq_file *m, void *v);
149         void *(*next) (struct seq_file *m, void *v, loff_t *pos);
150         int   (*show) (struct seq_file *m, void *v);
151 };
152
153 #define seq_lseek       NULL
154
155 static inline int seq_read(char *buf, size_t count, loff_t *ppos)
156 {
157         return 0;
158 }
159
160 static inline int
161 seq_open(struct file *file, const struct seq_operations *fops)
162 {
163         struct seq_file *p = file->param_private;
164
165         if (p == NULL) {
166                 LIBCFS_ALLOC(p, sizeof(*p));
167                 if (p == NULL)
168                         return -ENOMEM;
169                 file->param_private = p;
170         } else {
171                 memset(p, 0, sizeof(*p));
172         }
173         p->op = fops;
174         return 0;
175 }
176
177 static inline struct proc_inode *FAKE_PROC_I(const struct inode *inode)
178 {
179         return container_of(inode, struct proc_inode, param_inode);
180 }
181
182 static inline struct proc_dir_entry *PDE(struct inode *inode)
183 {
184         return FAKE_PROC_I(inode)->param_pde;
185 }
186
187 static inline
188 int LPROCFS_ENTRY_CHECK(struct proc_dir_entry *dp)
189 {
190         return 0;
191 }
192 #define LPROCFS_WRITE_ENTRY()       do {} while(0)
193 #define LPROCFS_WRITE_EXIT()        do {} while(0)
194
195 int seq_printf(struct seq_file *, const char *, ...)
196         __attribute__ ((format (printf,2,3)));
197
198 #endif /* LPROCFS */
199
200 /* XXX: params_tree APIs */
201
202 #endif  /* __PARAMS_TREE_H__ */