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