Whamcloud - gitweb
b=4834
[fs/lustre-release.git] / lustre / include / linux / lustre_fsfilt.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Filesystem interface helper.
22  *
23  */
24
25 #ifndef _LUSTRE_FSFILT_H
26 #define _LUSTRE_FSFILT_H
27
28 #ifdef __KERNEL__
29
30 #include <linux/obd.h>
31 #include <linux/obd_class.h>
32
33 typedef void (*fsfilt_cb_t)(struct obd_device *obd, __u64 last_rcvd,
34                             void *data, int error);
35
36 struct fsfilt_objinfo {
37         struct dentry *fso_dentry;
38         int fso_bufcnt;
39 };
40
41 struct fsfilt_operations {
42         struct list_head fs_list;
43         struct module *fs_owner;
44         char   *fs_type;
45         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
46                              int logs);
47         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
48                                  int niocount, struct niobuf_local *nb,
49                                  void *desc_private, int logs);
50         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
51         int     (* fs_commit_async)(struct inode *inode, void *handle,
52                                         void **wait_handle);
53         int     (* fs_commit_wait)(struct inode *inode, void *handle);
54         int     (* fs_setattr)(struct dentry *dentry, void *handle,
55                                struct iattr *iattr, int do_trunc);
56         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
57                                  unsigned int cmd, unsigned long arg);
58         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
59                               int size);
60         int     (* fs_get_md)(struct inode *inode, void *md, int size);
61         /* this method is needed to make IO operation fsfilt nature depend. */
62         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
63         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
64                                 loff_t *offset);
65         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
66                                       void *handle, fsfilt_cb_t cb_func,
67                                       void *cb_data);
68         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
69         int     (* fs_sync)(struct super_block *sb);
70         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
71                                        int pages, unsigned long *blocks,
72                                        int *created, int create,
73                                        struct semaphore *sem);
74         int     (* fs_prep_san_write)(struct inode *inode, long *blocks,
75                                       int nblocks, loff_t newsize);
76         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
77                                     int force_sync);
78         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
79         int     (* fs_setup)(struct super_block *sb);
80         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
81 };
82
83 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
84 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
85 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
86 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
87
88 #define FSFILT_OP_UNLINK         1
89 #define FSFILT_OP_RMDIR          2
90 #define FSFILT_OP_RENAME         3
91 #define FSFILT_OP_CREATE         4
92 #define FSFILT_OP_MKDIR          5
93 #define FSFILT_OP_SYMLINK        6
94 #define FSFILT_OP_MKNOD          7
95 #define FSFILT_OP_SETATTR        8
96 #define FSFILT_OP_LINK           9
97 #define FSFILT_OP_CANCEL_UNLINK 10
98
99 #define fsfilt_check_slow(start, timeout, msg)                          \
100 do {                                                                    \
101         if (time_before(jiffies, start + 15 * HZ))                      \
102                 break;                                                  \
103         else if (time_before(jiffies, start + timeout / 2 * HZ))        \
104                 CWARN("slow %s %lus\n", msg, (jiffies - start) / HZ);   \
105         else                                                            \
106                 CERROR("slow %s %lus\n", msg, (jiffies - start) / HZ);  \
107 } while (0)
108
109 static inline void *fsfilt_start_log(struct obd_device *obd,
110                                      struct inode *inode, int op,
111                                      struct obd_trans_info *oti, int logs)
112 {
113         unsigned long now = jiffies;
114         void *parent_handle = oti ? oti->oti_handle : NULL;
115         void *handle;
116         
117         if (obd->obd_fail)
118                 return ERR_PTR(-EROFS);
119
120         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
121         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
122
123         if (oti != NULL) {
124                 if (parent_handle == NULL) {
125                         oti->oti_handle = handle;
126                 } else if (handle != parent_handle) {
127                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
128                                parent_handle, handle, oti);
129                         LBUG();
130                 }
131         }
132         fsfilt_check_slow(now, obd_timeout, "journal start");
133         return handle;
134 }
135
136 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
137                                  int op, struct obd_trans_info *oti)
138 {
139         return fsfilt_start_log(obd, inode, op, oti, 0);
140 }
141
142 static inline void *fsfilt_brw_start_log(struct obd_device *obd,
143                                          int objcount,
144                                          struct fsfilt_objinfo *fso,
145                                          int niocount, struct niobuf_local *nb,
146                                          struct obd_trans_info *oti, int logs)
147 {
148         unsigned long now = jiffies;
149         void *parent_handle = oti ? oti->oti_handle : NULL;
150         void *handle;
151
152         if (obd->obd_fail)
153                 return ERR_PTR(-EROFS);
154
155         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
156                                               parent_handle, logs);
157         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
158
159         if (oti != NULL) {
160                 if (parent_handle == NULL) {
161                         oti->oti_handle = handle;
162                 } else if (handle != parent_handle) {
163                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
164                                parent_handle, handle, oti);
165                         LBUG();
166                 }
167         }
168         fsfilt_check_slow(now, obd_timeout, "journal start");
169
170         return handle;
171 }
172
173 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
174                                      struct fsfilt_objinfo *fso, int niocount,
175                                      struct niobuf_local *nb,
176                                      struct obd_trans_info *oti)
177 {
178         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
179 }
180
181 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
182                                 void *handle, int force_sync)
183 {
184         unsigned long now = jiffies;
185         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
186         CDEBUG(D_INFO, "committing handle %p\n", handle);
187
188         fsfilt_check_slow(now, obd_timeout, "journal start");
189
190         return rc;
191 }
192
193 static inline int fsfilt_commit_async(struct obd_device *obd,
194                                       struct inode *inode, void *handle,
195                                       void **wait_handle)
196 {
197         unsigned long now = jiffies;
198         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
199
200         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
201         fsfilt_check_slow(now, obd_timeout, "journal start");
202
203         return rc;
204 }
205
206 static inline int fsfilt_commit_wait(struct obd_device *obd,
207                                      struct inode *inode, void *handle)
208 {
209         unsigned long now = jiffies;
210         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
211         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
212         fsfilt_check_slow(now, obd_timeout, "journal start");
213         return rc;
214 }
215
216 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
217                                  void *handle, struct iattr *iattr,int do_trunc)
218 {
219         unsigned long now = jiffies;
220         int rc;
221         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
222         fsfilt_check_slow(now, obd_timeout, "setattr");
223         return rc;
224 }
225
226 static inline int fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
227                                    struct file *file, unsigned int cmd,
228                                    unsigned long arg)
229 {
230         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
231 }
232
233 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
234                                 void *handle, void *md, int size)
235 {
236         return obd->obd_fsops->fs_set_md(inode, handle, md, size);
237 }
238
239 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
240                                 void *md, int size)
241 {
242         return obd->obd_fsops->fs_get_md(inode, md, size);
243 }
244
245 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
246                                   struct inode *inode, void *bio)
247 {
248         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
249         
250         if (rw == OBD_BRW_READ)
251                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
252         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
253 }
254
255 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
256                                       struct file *file, char *buf,
257                                       size_t count, loff_t *offset)
258 {
259         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
260 }
261
262 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
263                                         void *handle, fsfilt_cb_t cb_func,
264                                         void *cb_data)
265 {
266         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
267                                                  handle, cb_func, cb_data);
268 }
269
270 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
271 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
272                                 unsigned long max_age)
273 {
274         int rc = 0;
275
276         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
277         if (time_before(obd->obd_osfs_age, max_age)) {
278                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
279                 if (rc == 0) /* N.B. statfs can't really fail */
280                         obd->obd_osfs_age = jiffies;
281         } else {
282                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
283         }
284
285         return rc;
286 }
287
288 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
289 {
290         return obd->obd_fsops->fs_sync(sb);
291 }
292
293 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
294                                          struct inode *inode,
295                                          struct page **page, int pages,
296                                          unsigned long *blocks, int *created,
297                                          int create, struct semaphore *sem)
298 {
299         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
300                                                   created, create, sem);
301 }
302
303 static inline int fs_prep_san_write(struct obd_device *obd,
304                                     struct inode *inode,
305                                     long *blocks,
306                                     int nblocks,
307                                     loff_t newsize)
308 {
309         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
310                                                  nblocks, newsize);
311 }
312
313 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
314                                      void *buf, loff_t size, loff_t *offs)
315 {
316         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
317 }
318
319 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
320                                       void *buf, loff_t size, loff_t *offs,
321                                       int force_sync)
322 {
323         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
324 }
325
326 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
327 {
328         if (obd->obd_fsops->fs_setup)
329                 return obd->obd_fsops->fs_setup(fs);
330         return 0;
331 }
332
333 #endif /* __KERNEL__ */
334
335 #endif