Whamcloud - gitweb
0bfbdf72138e1f7b7f04567462ff7a303efcd751
[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 typedef struct inode                            cfs_inode_t;
52 typedef struct file_operations                  cfs_param_file_ops_t;
53 typedef struct proc_dir_entry                   cfs_param_dentry_t;
54 typedef struct poll_table_struct                cfs_poll_table_t;
55 #define CFS_PARAM_MODULE                        THIS_MODULE
56 #define cfs_file_private(file)                  (file->private_data)
57
58 #ifndef HAVE_ONLY_PROCFS_SEQ
59 /* in lprocfs_stat.c, to protect the private data for proc entries */
60 extern struct rw_semaphore              _lprocfs_lock;
61
62 static inline
63 int LPROCFS_ENTRY_CHECK(struct proc_dir_entry *dp)
64 {
65         int deleted = 0;
66
67         spin_lock(&(dp)->pde_unload_lock);
68         if (dp->proc_fops == NULL)
69                 deleted = 1;
70         spin_unlock(&(dp)->pde_unload_lock);
71         if (deleted)
72                 return -ENODEV;
73         return 0;
74 }
75 #define LPROCFS_SRCH_ENTRY()            \
76 do {                                    \
77         down_read(&_lprocfs_lock);      \
78 } while(0)
79
80 #define LPROCFS_SRCH_EXIT()             \
81 do {                                    \
82         up_read(&_lprocfs_lock);        \
83 } while(0)
84
85 #define LPROCFS_WRITE_ENTRY()           \
86 do {                                    \
87         down_write(&_lprocfs_lock);     \
88 } while(0)
89
90 #define LPROCFS_WRITE_EXIT()            \
91 do {                                    \
92         up_write(&_lprocfs_lock);       \
93 } while(0)
94
95 #define PDE_DATA(inode)         PDE(inode)->data
96
97 #else /* New proc api */
98
99 static inline cfs_param_dentry_t* PDE(struct inode *inode)
100 {
101         return NULL;
102 }
103
104 static inline
105 int LPROCFS_ENTRY_CHECK(cfs_param_dentry_t *dp)
106 {
107         return 0;
108 }
109
110 #define LPROCFS_WRITE_ENTRY() do {} while(0)
111 #define LPROCFS_WRITE_EXIT()  do {} while(0)
112
113 #endif
114
115 #else /* !LPROCFS */
116
117 struct file {
118         void            *param_private;
119         loff_t          param_pos;
120         unsigned int    param_flags;
121 };
122
123 typedef struct cfs_param_inode {
124         void    *param_private;
125 } cfs_inode_t;
126
127 typedef struct cfs_param_dentry {
128         void *param_data;
129 } cfs_param_dentry_t;
130
131 typedef struct cfs_proc_inode {
132         cfs_param_dentry_t      *param_pde;
133         cfs_inode_t             param_inode;
134 } cfs_proc_inode_t;
135
136 struct seq_operations;
137 struct seq_file {
138         char                            *buf;
139         size_t                          size;
140         size_t                          from;
141         size_t                          count;
142         loff_t                          index;
143         loff_t                          version;
144         struct mutex                    lock;
145         const struct seq_operations     *op;
146         void                            *private;
147 };
148
149 struct seq_operations {
150         void *(*start) (struct seq_file *m, loff_t *pos);
151         void  (*stop) (struct seq_file *m, void *v);
152         void *(*next) (struct seq_file *m, void *v, loff_t *pos);
153         int   (*show) (struct seq_file *m, void *v);
154 };
155
156 typedef void *cfs_poll_table_t;
157
158 typedef struct cfs_param_file_ops {
159         struct module   *owner;
160         int (*open) (cfs_inode_t *, struct file *);
161         loff_t (*llseek)(struct file *, loff_t, int);
162         int (*release) (cfs_inode_t *, struct file *);
163         unsigned int (*poll) (struct file *, cfs_poll_table_t *);
164         ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
165         ssize_t (*read)(struct file *, char *, size_t, loff_t *);
166 } cfs_param_file_ops_t;
167 typedef cfs_param_file_ops_t *cfs_lproc_filep_t;
168
169 #define CFS_PARAM_MODULE        NULL
170 #define seq_lseek               NULL
171
172 static inline int seq_read(char *buf, size_t count, loff_t *ppos)
173 {
174         return 0;
175 }
176
177 static inline int
178 seq_open(struct file *file, const struct seq_operations *fops)
179 {
180         struct seq_file *p = file->param_private;
181
182         if (p == NULL) {
183                 LIBCFS_ALLOC(p, sizeof(*p));
184                 if (p == NULL)
185                         return -ENOMEM;
186                 file->param_private = p;
187         } else {
188                 memset(p, 0, sizeof(*p));
189         }
190         p->op = fops;
191         return 0;
192 }
193
194 static inline cfs_proc_inode_t *FAKE_PROC_I(const cfs_inode_t *inode)
195 {
196         return container_of(inode, cfs_proc_inode_t, param_inode);
197 }
198
199 static inline cfs_param_dentry_t *PDE(cfs_inode_t *inode)
200 {
201         return FAKE_PROC_I(inode)->param_pde;
202 }
203
204 static inline
205 int LPROCFS_ENTRY_CHECK(cfs_param_dentry_t *dp)
206 {
207         return 0;
208 }
209 #define LPROCFS_WRITE_ENTRY()       do {} while(0)
210 #define LPROCFS_WRITE_EXIT()        do {} while(0)
211
212 int seq_printf(struct seq_file *, const char *, ...)
213         __attribute__ ((format (printf,2,3)));
214
215 #endif /* LPROCFS */
216
217 /* XXX: params_tree APIs */
218
219 #endif  /* __PARAMS_TREE_H__ */