Whamcloud - gitweb
56e956d893a724acda6a5c75ea973e69b72e2161
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * API and structure definitions for params_tree.
35  *
36  * Author: LiuYing <emoly.liu@oracle.com>
37  */
38 #ifndef __PARAMS_TREE_H__
39 #define __PARAMS_TREE_H__
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 #include <libcfs/libcfs.h>
46
47 #undef LPROCFS
48 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
49 # define LPROCFS
50 #endif
51
52 #ifdef LPROCFS
53 typedef struct file                             cfs_param_file_t;
54 typedef struct inode                            cfs_inode_t;
55 typedef struct proc_inode                       cfs_proc_inode_t;
56 typedef struct seq_file                         cfs_seq_file_t;
57 typedef struct seq_operations                   cfs_seq_ops_t;
58 typedef struct file_operations                  cfs_param_file_ops_t;
59 typedef cfs_module_t                           *cfs_param_module_t;
60 typedef struct proc_dir_entry                   cfs_param_dentry_t;
61 typedef struct poll_table_struct                cfs_poll_table_t;
62 #define CFS_PARAM_MODULE                        THIS_MODULE
63 #define CFS_PDE(value)                          PDE(value)
64 #define cfs_file_private(file)                  (file->private_data)
65 #define cfs_dentry_data(dentry)                 (dentry->data)
66 #define cfs_proc_inode_pde(proc_inode)          (proc_inode->pde)
67 #define cfs_proc_inode(proc_inode)              (proc_inode->vfs_inode)
68 #define cfs_seq_read_common                     seq_read
69 #define cfs_seq_lseek_common                    seq_lseek
70 #define cfs_seq_private(seq)                    (seq->private)
71 #define cfs_seq_printf(seq, format, ...)        seq_printf(seq, format,  \
72                                                            ## __VA_ARGS__)
73 #define cfs_seq_release(inode, file)            seq_release(inode, file)
74 #define cfs_seq_puts(seq, s)                    seq_puts(seq, s)
75 #define cfs_seq_putc(seq, s)                    seq_putc(seq, s)
76 #define cfs_seq_read(file, buf, count, ppos, rc) (rc = seq_read(file, buf, \
77                                                             count, ppos))
78 #define cfs_seq_open(file, ops, rc)             (rc = seq_open(file, ops))
79
80 /* in lprocfs_stat.c, to protect the private data for proc entries */
81 extern cfs_rw_semaphore_t       _lprocfs_lock;
82
83 /* to begin from 2.6.23, Linux defines self file_operations (proc_reg_file_ops)
84  * in procfs, the proc file_operation defined by Lustre (lprocfs_generic_fops)
85  * will be wrapped into the new defined proc_reg_file_ops, which instroduces
86  * user count in proc_dir_entrey(pde_users) to protect the proc entry from
87  * being deleted. then the protection lock (_lprocfs_lock) defined by Lustre
88  * isn't necessary anymore for lprocfs_generic_fops(e.g. lprocfs_fops_read).
89  * see bug19706 for detailed information.
90  */
91 #ifndef HAVE_PROCFS_USERS
92
93 #define LPROCFS_ENTRY()                 \
94 do {                                    \
95         cfs_down_read(&_lprocfs_lock);  \
96 } while(0)
97
98 #define LPROCFS_EXIT()                  \
99 do {                                    \
100         cfs_up_read(&_lprocfs_lock);    \
101 } while(0)
102
103 #else
104 #define LPROCFS_ENTRY() do{ }while(0)
105 #define LPROCFS_EXIT()  do{ }while(0)
106 #endif
107
108 #ifdef HAVE_PROCFS_DELETED
109
110 #ifdef HAVE_PROCFS_USERS
111 #error proc_dir_entry->deleted is conflicted with proc_dir_entry->pde_users
112 #endif
113
114 static inline
115 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
116 {
117         LPROCFS_ENTRY();
118         if ((dp)->deleted) {
119                 LPROCFS_EXIT();
120                 return -ENODEV;
121         }
122         return 0;
123 }
124 #elif defined(HAVE_PROCFS_USERS) /* !HAVE_PROCFS_DELETED*/
125 static inline
126 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
127 {
128         int deleted = 0;
129         spin_lock(&(dp)->pde_unload_lock);
130         if (dp->proc_fops == NULL)
131                 deleted = 1;
132         spin_unlock(&(dp)->pde_unload_lock);
133         if (deleted)
134                 return -ENODEV;
135         return 0;
136 }
137 #else /* !HAVE_PROCFS_DELETED*/
138 static inline
139 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
140 {
141         LPROCFS_ENTRY();
142         return 0;
143 }
144 #endif /* HAVE_PROCFS_DELETED */
145 #define LPROCFS_SRCH_ENTRY()            \
146 do {                                    \
147         down_read(&_lprocfs_lock);      \
148 } while(0)
149
150 #define LPROCFS_SRCH_EXIT()             \
151 do {                                    \
152         up_read(&_lprocfs_lock);        \
153 } while(0)
154
155 #define LPROCFS_WRITE_ENTRY()           \
156 do {                                    \
157         cfs_down_write(&_lprocfs_lock); \
158 } while(0)
159
160 #define LPROCFS_WRITE_EXIT()            \
161 do {                                    \
162         cfs_up_write(&_lprocfs_lock);   \
163 } while(0)
164 #else /* !LPROCFS */
165
166 typedef struct cfs_params_file {
167         void           *param_private;
168         loff_t          param_pos;
169         unsigned int    param_flags;
170 } cfs_param_file_t;
171
172 typedef struct cfs_param_inode {
173         void    *param_private;
174 } cfs_inode_t;
175
176 typedef struct cfs_param_dentry {
177         void *param_data;
178 } cfs_param_dentry_t;
179
180 typedef struct cfs_proc_inode {
181         cfs_param_dentry_t *param_pde;
182         cfs_inode_t         param_inode;
183 } cfs_proc_inode_t;
184
185 struct cfs_seq_operations;
186 typedef struct cfs_seq_file {
187         char                      *buf;
188         size_t                     size;
189         size_t                     from;
190         size_t                     count;
191         loff_t                     index;
192         loff_t                     version;
193         cfs_mutex_t                lock;
194         struct cfs_seq_operations *op;
195         void                      *private;
196 } cfs_seq_file_t;
197
198 typedef struct cfs_seq_operations {
199         void *(*start) (cfs_seq_file_t *m, loff_t *pos);
200         void  (*stop) (cfs_seq_file_t *m, void *v);
201         void *(*next) (cfs_seq_file_t *m, void *v, loff_t *pos);
202         int   (*show) (cfs_seq_file_t *m, void *v);
203 } cfs_seq_ops_t;
204
205 typedef void *cfs_param_module_t;
206 typedef void *cfs_poll_table_t;
207
208 typedef struct cfs_param_file_ops {
209         cfs_param_module_t owner;
210         int (*open) (cfs_inode_t *, cfs_file_t *);
211         loff_t (*llseek)(cfs_file_t *, loff_t, int);
212         int (*release) (cfs_inode_t *, cfs_param_file_t *);
213         unsigned int (*poll) (cfs_file_t *, cfs_poll_table_t *);
214         ssize_t (*write) (cfs_file_t *, const char *, size_t, loff_t *);
215         ssize_t (*read)(cfs_file_t *, char *, size_t, loff_t *);
216 } cfs_param_file_ops_t;
217 typedef cfs_param_file_ops_t *cfs_lproc_filep_t;
218
219 static inline cfs_proc_inode_t *FAKE_PROC_I(const cfs_inode_t *inode)
220 {
221         return container_of(inode, cfs_proc_inode_t, param_inode);
222 }
223
224 static inline cfs_param_dentry_t *FAKE_PDE(cfs_inode_t *inode)
225 {
226         return FAKE_PROC_I(inode)->param_pde;
227 }
228
229 #define CFS_PARAM_MODULE                        NULL
230 #define CFS_PDE(value)                          FAKE_PDE(value)
231 #define cfs_file_private(file)                  (file->param_private)
232 #define cfs_dentry_data(dentry)                 (dentry->param_data)
233 #define cfs_proc_inode(proc_inode)              (proc_inode->param_inode)
234 #define cfs_proc_inode_pde(proc_inode)          (proc_inode->param_pde)
235 #define cfs_seq_read_common                     NULL
236 #define cfs_seq_lseek_common                    NULL
237 #define cfs_seq_private(seq)                    (seq->private)
238 #define cfs_seq_read(file, buf, count, ppos, rc) do {} while(0)
239 #define cfs_seq_open(file, ops, rc)                     \
240 do {                                                    \
241          cfs_seq_file_t *p = cfs_file_private(file);    \
242          if (!p) {                                      \
243                 LIBCFS_ALLOC(p, sizeof(*p));            \
244                 if (!p) {                               \
245                         rc = -ENOMEM;                   \
246                         break;                          \
247                 }                                       \
248                 cfs_file_private(file) = p;             \
249         }                                               \
250         memset(p, 0, sizeof(*p));                       \
251         p->op = ops;                                    \
252         rc = 0;                                         \
253 } while(0)
254
255 #define LPROCFS_ENTRY()             do {} while(0)
256 #define LPROCFS_EXIT()              do {} while(0)
257 static inline
258 int LPROCFS_ENTRY_AND_CHECK(cfs_param_dentry_t *dp)
259 {
260         LPROCFS_ENTRY();
261         return 0;
262 }
263 #define LPROCFS_WRITE_ENTRY()       do {} while(0)
264 #define LPROCFS_WRITE_EXIT()        do {} while(0)
265
266 #endif /* LPROCFS */
267
268 /* XXX: params_tree APIs */
269
270 #endif  /* __PARAMS_TREE_H__ */