Whamcloud - gitweb
b=19706 porting patch for master.
[fs/lustre-release.git] / libcfs / include / libcfs / params_tree.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46
47 #include <libcfs/libcfs.h>
48
49 #undef LPROCFS
50 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
51 # define LPROCFS
52 #endif
53
54 #ifdef LPROCFS
55 typedef struct file                             cfs_param_file_t;
56 typedef struct inode                            cfs_inode_t;
57 typedef struct proc_inode                       cfs_proc_inode_t;
58 typedef struct seq_file                         cfs_seq_file_t;
59 typedef struct seq_operations                   cfs_seq_ops_t;
60 typedef struct file_operations                  cfs_param_file_ops_t;
61 typedef cfs_module_t                           *cfs_param_module_t;
62 typedef struct proc_dir_entry                   cfs_param_dentry_t;
63 typedef struct poll_table_struct                cfs_poll_table_t;
64 #define CFS_PARAM_MODULE                        THIS_MODULE
65 #define CFS_PDE(value)                          PDE(value)
66 #define cfs_file_private(file)                  (file->private_data)
67 #define cfs_dentry_data(dentry)                 (dentry->data)
68 #define cfs_proc_inode_pde(proc_inode)          (proc_inode->pde)
69 #define cfs_proc_inode(proc_inode)              (proc_inode->vfs_inode)
70 #define cfs_seq_read_common                     seq_read
71 #define cfs_seq_lseek_common                    seq_lseek
72 #define cfs_seq_private(seq)                    (seq->private)
73 #define cfs_seq_printf(seq, format, ...)        seq_printf(seq, format,  \
74                                                            ## __VA_ARGS__)
75 #define cfs_seq_release(inode, file)            seq_release(inode, file)
76 #define cfs_seq_puts(seq, s)                    seq_puts(seq, s)
77 #define cfs_seq_putc(seq, s)                    seq_putc(seq, s)
78 #define cfs_seq_read(file, buf, count, ppos, rc) (rc = seq_read(file, buf, \
79                                                             count, ppos))
80 #define cfs_seq_open(file, ops, rc)             (rc = seq_open(file, ops))
81
82 /* in lprocfs_stat.c, to protect the private data for proc entries */
83 extern cfs_rw_semaphore_t       _lprocfs_lock;
84
85 /* to begin from 2.6.23, Linux defines self file_operations (proc_reg_file_ops)
86  * in procfs, the proc file_operation defined by Lustre (lprocfs_generic_fops)
87  * will be wrapped into the new defined proc_reg_file_ops, which instroduces
88  * user count in proc_dir_entrey(pde_users) to protect the proc entry from
89  * being deleted. then the protection lock (_lprocfs_lock) defined by Lustre
90  * isn't necessary anymore for lprocfs_generic_fops(e.g. lprocfs_fops_read).
91  * see bug19706 for detailed information.
92  */
93 #ifndef HAVE_PROCFS_USERS
94
95 #define LPROCFS_ENTRY()                 \
96 do {                                    \
97         cfs_down_read(&_lprocfs_lock);  \
98 } while(0)
99
100 #define LPROCFS_EXIT()                  \
101 do {                                    \
102         cfs_up_read(&_lprocfs_lock);    \
103 } while(0)
104
105 #else
106 #define LPROCFS_ENTRY() do{ }while(0)
107 #define LPROCFS_EXIT()  do{ }while(0)
108 #endif
109
110 #ifdef HAVE_PROCFS_DELETED
111
112 #ifdef HAVE_PROCFS_USERS
113 #error proc_dir_entry->deleted is conflicted with proc_dir_entry->pde_users
114 #endif
115
116 static inline
117 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
118 {
119         LPROCFS_ENTRY();
120         if ((dp)->deleted) {
121                 LPROCFS_EXIT();
122                 return -ENODEV;
123         }
124         return 0;
125 }
126 #elif defined(HAVE_PROCFS_USERS) /* !HAVE_PROCFS_DELETED*/
127 static inline
128 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
129 {
130         int deleted = 0;
131         spin_lock(&(dp)->pde_unload_lock);
132         if (dp->proc_fops == NULL)
133                 deleted = 1;
134         spin_unlock(&(dp)->pde_unload_lock);
135         if (deleted)
136                 return -ENODEV;
137         return 0;
138 }
139 #else /* !HAVE_PROCFS_DELETED*/
140 static inline
141 int LPROCFS_ENTRY_AND_CHECK(struct proc_dir_entry *dp)
142 {
143         LPROCFS_ENTRY();
144         return 0;
145 }
146 #endif /* HAVE_PROCFS_DELETED */
147 #define LPROCFS_SRCH_ENTRY()            \
148 do {                                    \
149         down_read(&_lprocfs_lock);      \
150 } while(0)
151
152 #define LPROCFS_SRCH_EXIT()             \
153 do {                                    \
154         up_read(&_lprocfs_lock);        \
155 } while(0)
156
157 #define LPROCFS_WRITE_ENTRY()           \
158 do {                                    \
159         cfs_down_write(&_lprocfs_lock); \
160 } while(0)
161
162 #define LPROCFS_WRITE_EXIT()            \
163 do {                                    \
164         cfs_up_write(&_lprocfs_lock);   \
165 } while(0)
166 #else /* !LPROCFS */
167
168 typedef struct cfs_params_file {
169         void           *param_private;
170         loff_t          param_pos;
171         unsigned int    param_flags;
172 } cfs_param_file_t;
173
174 typedef struct cfs_param_inode {
175         void    *param_private;
176 } cfs_inode_t;
177
178 typedef struct cfs_param_dentry {
179         void *param_data;
180 } cfs_param_dentry_t;
181
182 typedef struct cfs_proc_inode {
183         cfs_param_dentry_t *param_pde;
184         cfs_inode_t         param_inode;
185 } cfs_proc_inode_t;
186
187 struct cfs_seq_operations;
188 typedef struct cfs_seq_file {
189         char                      *buf;
190         size_t                     size;
191         size_t                     from;
192         size_t                     count;
193         loff_t                     index;
194         loff_t                     version;
195         cfs_mutex_t                lock;
196         struct cfs_seq_operations *op;
197         void                      *private;
198 } cfs_seq_file_t;
199
200 typedef struct cfs_seq_operations {
201         void *(*start) (cfs_seq_file_t *m, loff_t *pos);
202         void  (*stop) (cfs_seq_file_t *m, void *v);
203         void *(*next) (cfs_seq_file_t *m, void *v, loff_t *pos);
204         int   (*show) (cfs_seq_file_t *m, void *v);
205 } cfs_seq_ops_t;
206
207 typedef void *cfs_param_module_t;
208 typedef void *cfs_poll_table_t;
209
210 typedef struct cfs_param_file_ops {
211         cfs_param_module_t owner;
212         int (*open) (cfs_inode_t *, cfs_file_t *);
213         loff_t (*llseek)(cfs_file_t *, loff_t, int);
214         int (*release) (cfs_inode_t *, cfs_param_file_t *);
215         unsigned int (*poll) (cfs_file_t *, cfs_poll_table_t *);
216         ssize_t (*write) (cfs_file_t *, const char *, size_t, loff_t *);
217         ssize_t (*read)(cfs_file_t *, char *, size_t, loff_t *);
218 } cfs_param_file_ops_t;
219 typedef cfs_param_file_ops_t *cfs_lproc_filep_t;
220
221 static inline cfs_proc_inode_t *FAKE_PROC_I(const cfs_inode_t *inode)
222 {
223         return container_of(inode, cfs_proc_inode_t, param_inode);
224 }
225
226 static inline cfs_param_dentry_t *FAKE_PDE(cfs_inode_t *inode)
227 {
228         return FAKE_PROC_I(inode)->param_pde;
229 }
230
231 #define CFS_PARAM_MODULE                        NULL
232 #define CFS_PDE(value)                          FAKE_PDE(value)
233 #define cfs_file_private(file)                  (file->param_private)
234 #define cfs_dentry_data(dentry)                 (dentry->param_data)
235 #define cfs_proc_inode(proc_inode)              (proc_inode->param_inode)
236 #define cfs_proc_inode_pde(proc_inode)          (proc_inode->param_pde)
237 #define cfs_seq_read_common                     NULL
238 #define cfs_seq_lseek_common                    NULL
239 #define cfs_seq_private(seq)                    (seq->private)
240 #define cfs_seq_read(file, buf, count, ppos, rc) do {} while(0)
241 #define cfs_seq_open(file, ops, rc)                     \
242 do {                                                    \
243          cfs_seq_file_t *p = cfs_file_private(file);    \
244          if (!p) {                                      \
245                 LIBCFS_ALLOC(p, sizeof(*p));            \
246                 if (!p) {                               \
247                         rc = -ENOMEM;                   \
248                         break;                          \
249                 }                                       \
250                 cfs_file_private(file) = p;             \
251         }                                               \
252         memset(p, 0, sizeof(*p));                       \
253         p->op = ops;                                    \
254         rc = 0;                                         \
255 } while(0)
256
257 #define LPROCFS_ENTRY()             do {} while(0)
258 #define LPROCFS_EXIT()              do {} while(0)
259 static inline
260 int LPROCFS_ENTRY_AND_CHECK(cfs_param_dentry_t *dp)
261 {
262         LPROCFS_ENTRY();
263         return 0;
264 }
265 #define LPROCFS_WRITE_ENTRY()       do {} while(0)
266 #define LPROCFS_WRITE_EXIT()        do {} while(0)
267
268 #endif /* LPROCFS */
269
270 /* XXX: params_tree APIs */
271
272 #endif  /* __PARAMS_TREE_H__ */