Whamcloud - gitweb
1557133e065f6c24b773be36748eb4575a5e7b69
[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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
63         int     (* fs_send_bio)(int rw, struct inode *inode, struct bio *bio);
64 #else
65         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
66 #endif
67         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
68                                 loff_t *offset);
69         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
70                                       void *handle, fsfilt_cb_t cb_func,
71                                       void *cb_data);
72         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
73         int     (* fs_sync)(struct super_block *sb);
74         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
75                                        int pages, unsigned long *blocks,
76                                        int *created, int create,
77                                        struct semaphore *sem);
78         int     (* fs_prep_san_write)(struct inode *inode, long *blocks,
79                                       int nblocks, loff_t newsize);
80         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
81                                     int force_sync);
82         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
83         int     (* fs_setup)(struct super_block *sb);
84         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
85 };
86
87 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
88 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
89 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
90 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
91
92 #define FSFILT_OP_UNLINK         1
93 #define FSFILT_OP_RMDIR          2
94 #define FSFILT_OP_RENAME         3
95 #define FSFILT_OP_CREATE         4
96 #define FSFILT_OP_MKDIR          5
97 #define FSFILT_OP_SYMLINK        6
98 #define FSFILT_OP_MKNOD          7
99 #define FSFILT_OP_SETATTR        8
100 #define FSFILT_OP_LINK           9
101 #define FSFILT_OP_CANCEL_UNLINK 10
102
103 #define fsfilt_check_slow(start, timeout, msg)                          \
104 do {                                                                    \
105         if (time_before(jiffies, start + 15 * HZ))                      \
106                 break;                                                  \
107         else if (time_before(jiffies, start + timeout / 2 * HZ))        \
108                 CWARN("slow %s %lus\n", msg, (jiffies - start) / HZ);   \
109         else                                                            \
110                 CERROR("slow %s %lus\n", msg, (jiffies - start) / HZ);  \
111 } while (0)
112
113 static inline void *fsfilt_start_log(struct obd_device *obd,
114                                      struct inode *inode, int op,
115                                      struct obd_trans_info *oti, int logs)
116 {
117         unsigned long now = jiffies;
118         void *parent_handle = oti ? oti->oti_handle : NULL;
119         void *handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
120         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
121
122         if (oti != NULL) {
123                 if (parent_handle == NULL) {
124                         oti->oti_handle = handle;
125                 } else if (handle != parent_handle) {
126                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
127                                parent_handle, handle, oti);
128                         LBUG();
129                 }
130         }
131         fsfilt_check_slow(now, obd_timeout, "journal start");
132         return handle;
133 }
134
135 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
136                                  int op, struct obd_trans_info *oti)
137 {
138         return fsfilt_start_log(obd, inode, op, oti, 0);
139 }
140
141 static inline void *fsfilt_brw_start_log(struct obd_device *obd,
142                                          int objcount,
143                                          struct fsfilt_objinfo *fso,
144                                          int niocount, struct niobuf_local *nb,
145                                          struct obd_trans_info *oti, int logs)
146 {
147         unsigned long now = jiffies;
148         void *parent_handle = oti ? oti->oti_handle : NULL;
149         void *handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
150                                                     parent_handle, logs);
151         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
152
153         if (oti != NULL) {
154                 if (parent_handle == NULL) {
155                         oti->oti_handle = handle;
156                 } else if (handle != parent_handle) {
157                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
158                                parent_handle, handle, oti);
159                         LBUG();
160                 }
161         }
162         fsfilt_check_slow(now, obd_timeout, "journal start");
163
164         return handle;
165 }
166
167 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
168                                      struct fsfilt_objinfo *fso, int niocount,
169                                      struct niobuf_local *nb,
170                                      struct obd_trans_info *oti)
171 {
172         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
173 }
174
175 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
176                                 void *handle, int force_sync)
177 {
178         unsigned long now = jiffies;
179         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
180         CDEBUG(D_INFO, "committing handle %p\n", handle);
181
182         fsfilt_check_slow(now, obd_timeout, "journal start");
183
184         return rc;
185 }
186
187 static inline int fsfilt_commit_async(struct obd_device *obd,
188                                       struct inode *inode, void *handle,
189                                       void **wait_handle)
190 {
191         unsigned long now = jiffies;
192         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
193
194         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
195         fsfilt_check_slow(now, obd_timeout, "journal start");
196
197         return rc;
198 }
199
200 static inline int fsfilt_commit_wait(struct obd_device *obd,
201                                      struct inode *inode, void *handle)
202 {
203         unsigned long now = jiffies;
204         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
205         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
206         fsfilt_check_slow(now, obd_timeout, "journal start");
207         return rc;
208 }
209
210 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
211                                  void *handle, struct iattr *iattr,int do_trunc)
212 {
213         unsigned long now = jiffies;
214         int rc;
215         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
216         fsfilt_check_slow(now, obd_timeout, "setattr");
217         return rc;
218 }
219
220 static inline int fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
221                                    struct file *file, unsigned int cmd,
222                                    unsigned long arg)
223 {
224         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
225 }
226
227 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
228                                 void *handle, void *md, int size)
229 {
230         return obd->obd_fsops->fs_set_md(inode, handle, md, size);
231 }
232
233 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
234                                 void *md, int size)
235 {
236         return obd->obd_fsops->fs_get_md(inode, md, size);
237 }
238
239 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
240                                   struct inode *inode, void *bio)
241 {
242         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
243
244         if (rw == OBD_BRW_READ)
245                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
246         else
247                 return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
248 }
249
250 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
251                                       struct file *file, char *buf,
252                                       size_t count, loff_t *offset)
253 {
254         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
255 }
256
257 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
258                                         void *handle, fsfilt_cb_t cb_func,
259                                         void *cb_data)
260 {
261         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
262                                                  handle, cb_func, cb_data);
263 }
264
265 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
266 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
267                                 unsigned long max_age)
268 {
269         int rc = 0;
270
271         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
272         if (time_before(obd->obd_osfs_age, max_age)) {
273                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
274                 if (rc == 0) /* N.B. statfs can't really fail */
275                         obd->obd_osfs_age = jiffies;
276         } else {
277                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
278         }
279
280         return rc;
281 }
282
283 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
284 {
285         return obd->obd_fsops->fs_sync(sb);
286 }
287
288 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
289                                          struct inode *inode,
290                                          struct page **page, int pages,
291                                          unsigned long *blocks, int *created,
292                                          int create, struct semaphore *sem)
293 {
294         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
295                                                   created, create, sem);
296 }
297
298 static inline int fs_prep_san_write(struct obd_device *obd,
299                                     struct inode *inode,
300                                     long *blocks,
301                                     int nblocks,
302                                     loff_t newsize)
303 {
304         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
305                                                  nblocks, newsize);
306 }
307
308 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
309                                      void *buf, loff_t size, loff_t *offs)
310 {
311         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
312 }
313
314 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
315                                       void *buf, loff_t size, loff_t *offs,
316                                       int force_sync)
317 {
318         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
319 }
320
321 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
322 {
323         if (obd->obd_fsops->fs_setup)
324                 return obd->obd_fsops->fs_setup(fs);
325         return 0;
326 }
327
328 #endif /* __KERNEL__ */
329
330 #endif