Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[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-2004 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/lustre_log.h>
32 #include <linux/obd_class.h>
33
34 typedef void (*fsfilt_cb_t)(struct obd_device *obd, __u64 last_rcvd,
35                             void *data, int error);
36
37 struct fsfilt_objinfo {
38         struct dentry *fso_dentry;
39         int fso_bufcnt;
40 };
41
42 struct fsfilt_operations {
43         struct list_head fs_list;
44         struct module *fs_owner;
45         char   *fs_type;
46         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
47                              int logs);
48         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
49                                  int niocount, struct niobuf_local *nb,
50                                  void *desc_private, int logs);
51         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
52         int     (* fs_commit_async)(struct inode *inode, void *handle,
53                                         void **wait_handle);
54         int     (* fs_commit_wait)(struct inode *inode, void *handle);
55         int     (* fs_setattr)(struct dentry *dentry, void *handle,
56                                struct iattr *iattr, int do_trunc);
57         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
58                                  unsigned int cmd, unsigned long arg);
59         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
60                               int size);
61         int     (* fs_get_md)(struct inode *inode, void *md, int size);
62
63         /* this method is needed to make IO operation fsfilt nature depend. */
64 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
65         int     (* fs_send_bio)(struct inode *inode, struct bio *bio);
66 #else
67         int     (* fs_send_bio)(struct inode *inode, struct kiobuf *bio);
68 #endif
69
70         /* methods for getting page from backing fs and putting page there
71          * during IO. Used on OST. */
72         int (* fs_putpage)(struct inode *inode, struct page *page);
73         struct page *(* fs_getpage)(struct inode *inode, long int index);
74
75         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
76                                 loff_t *offset);
77         int     (* fs_add_journal_cb)(struct obd_device *obd, struct super_block *sb,
78                                       __u64 last_rcvd, void *handle, fsfilt_cb_t cb_func,
79                                       void *cb_data);
80         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
81         int     (* fs_sync)(struct super_block *sb);
82         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
83                                        int pages, unsigned long *blocks,
84                                        int *created, int create,
85                                        struct semaphore *sem);
86         int     (* fs_prep_san_write)(struct inode *inode, long *blocks,
87                                       int nblocks, loff_t newsize);
88         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
89                                     int force_sync);
90         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
91         int     (* fs_setup)(struct super_block *sb);
92
93         int     (* fs_set_xattr)(struct inode *inode, void *handle, char *name,
94                                  void *buffer, int buffer_size);
95         int     (* fs_get_xattr)(struct inode *inode, char *name,
96                                  void *buffer, int buffer_size);
97
98         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
99 };
100
101 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
102 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
103 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
104 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
105
106 #define FSFILT_OP_UNLINK         1
107 #define FSFILT_OP_RMDIR          2
108 #define FSFILT_OP_RENAME         3
109 #define FSFILT_OP_CREATE         4
110 #define FSFILT_OP_MKDIR          5
111 #define FSFILT_OP_SYMLINK        6
112 #define FSFILT_OP_MKNOD          7
113 #define FSFILT_OP_SETATTR        8
114 #define FSFILT_OP_LINK           9
115 #define FSFILT_OP_CANCEL_UNLINK 10
116 #define FSFILT_OP_NOOP          15
117
118 /* XXX BUG 3188 -- must return to one set of opcodes */
119 #define KML_UNLINK              0x11
120 #define KML_RMDIR               0x12
121 #define KML_RENAME              0x13
122 #define KML_CREATE              0x14
123 #define KML_MKDIR               0x15
124 #define KML_SYMLINK             0x16
125 #define KML_MKNOD               0x17
126 #define KML_LINK                0x19
127
128 #define CACHE_UNLINK            0x21
129 #define CACHE_RMDIR             0x22
130 #define CACHE_RENAME            0x23
131 #define CACHE_CREATE            0x24
132 #define CACHE_MKDIR             0x25
133 #define CACHE_SYMLINK           0x26
134 #define CACHE_MKNOD             0x27
135 #define CACHE_LINK              0x29
136 #define CACHE_NOOP              0x2f
137
138 #define KML_CACHE_UNLINK        0x31
139 #define KML_CACHE_RMDIR         0x32
140 #define KML_CACHE_RENAME        0x33
141 #define KML_CACHE_CREATE        0x34
142 #define KML_CACHE_MKDIR         0x35
143 #define KML_CACHE_SYMLINK       0x36
144 #define KML_CACHE_MKNOD         0x37
145 #define KML_CACHE_LINK          0x39
146 #define KML_CACHE_NOOP          0x3f
147
148 static inline void *
149 fsfilt_start_ops(struct fsfilt_operations *ops, struct inode *inode,
150                  int op, struct obd_trans_info *oti, int logs)
151 {
152         unsigned long now = jiffies;
153         void *parent_handle = oti ? oti->oti_handle : NULL;
154         void *handle = ops->fs_start(inode, op, parent_handle, logs);
155         CDEBUG(D_HA, "started handle %p (%p)\n", handle, parent_handle);
156
157         if (oti != NULL) {
158                 if (parent_handle == NULL) {
159                         oti->oti_handle = handle;
160                 } else if (handle != parent_handle) {
161                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
162                                parent_handle, handle, oti);
163                         LBUG();
164                 }
165         }
166         if (time_after(jiffies, now + 15 * HZ))
167                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
168         return handle;
169 }
170
171 static inline void *
172 fsfilt_start_log(struct obd_device *obd, struct inode *inode,
173                  int op, struct obd_trans_info *oti, int logs)
174 {
175         return fsfilt_start_ops(obd->obd_fsops, inode, op, oti, logs);
176 }
177
178 static inline void *
179 fsfilt_start(struct obd_device *obd, struct inode *inode,
180              int op, struct obd_trans_info *oti)
181 {
182         return fsfilt_start_ops(obd->obd_fsops, inode, op, oti, 0);
183 }
184
185 static inline void *
186 llog_fsfilt_start(struct llog_ctxt *ctxt, struct inode *inode,
187                   int op, struct obd_trans_info *oti)
188 {
189         return fsfilt_start_ops(ctxt->loc_fsops, inode, op, oti, 1);
190 }
191
192 static inline int
193 fsfilt_commit_ops(struct fsfilt_operations *ops, struct inode *inode,
194                   void *handle, int force_sync)
195 {
196         unsigned long now = jiffies;
197         int rc = ops->fs_commit(inode, handle, force_sync);
198         CDEBUG(D_HA, "committing handle %p\n", handle);
199
200         if (time_after(jiffies, now + 15 * HZ))
201                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
202
203         return rc;
204 }
205
206 static inline int
207 fsfilt_commit(struct obd_device *obd, struct inode *inode,
208               void *handle, int force_sync)
209 {
210         return fsfilt_commit_ops(obd->obd_fsops, inode, handle, force_sync);
211 }
212
213 static inline int
214 llog_fsfilt_commit(struct llog_ctxt *ctxt, struct inode *inode,
215                    void *handle, int force_sync)
216 {
217         return fsfilt_commit_ops(ctxt->loc_fsops, inode, handle, force_sync);
218 }
219
220 static inline void *
221 fsfilt_brw_start_log(struct obd_device *obd, int objcount,
222                      struct fsfilt_objinfo *fso, int niocount,
223                      struct niobuf_local *nb, struct obd_trans_info *oti,
224                      int logs)
225 {
226         unsigned long now = jiffies;
227         void *parent_handle = oti ? oti->oti_handle : NULL;
228         void *handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
229                                                     parent_handle, logs);
230         CDEBUG(D_HA, "started handle %p (%p)\n", handle, parent_handle);
231
232         if (oti != NULL) {
233                 if (parent_handle == NULL) {
234                         oti->oti_handle = handle;
235                 } else if (handle != parent_handle) {
236                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
237                                parent_handle, handle, oti);
238                         LBUG();
239                 }
240         }
241         if (time_after(jiffies, now + 15 * HZ))
242                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
243
244         return handle;
245 }
246
247 static inline void *
248 fsfilt_brw_start(struct obd_device *obd, int objcount,
249                  struct fsfilt_objinfo *fso, int niocount,
250                  struct niobuf_local *nb, struct obd_trans_info *oti)
251 {
252         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
253 }
254
255 static inline int
256 fsfilt_commit_async(struct obd_device *obd, struct inode *inode,
257                     void *handle, void **wait_handle)
258 {
259         unsigned long now = jiffies;
260         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
261
262         CDEBUG(D_HA, "committing handle %p (async)\n", *wait_handle);
263         if (time_after(jiffies, now + 15 * HZ))
264                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
265
266         return rc;
267 }
268
269 static inline int
270 fsfilt_commit_wait(struct obd_device *obd, struct inode *inode, void *handle)
271 {
272         unsigned long now = jiffies;
273         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
274         CDEBUG(D_HA, "waiting for completion %p\n", handle);
275         if (time_after(jiffies, now + 15 * HZ))
276                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
277         return rc;
278 }
279
280 static inline int
281 fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
282                void *handle, struct iattr *iattr, int do_trunc)
283 {
284         unsigned long now = jiffies;
285         int rc;
286         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
287         if (time_after(jiffies, now + 15 * HZ))
288                 CERROR("long setattr time %lus\n", (jiffies - now) / HZ);
289         return rc;
290 }
291
292 static inline int
293 fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
294                  struct file *file, unsigned int cmd,
295                  unsigned long arg)
296 {
297         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
298 }
299
300 static inline int fsfilt_setup(struct obd_device *obd,
301                                struct super_block *fs)
302 {
303         if (obd->obd_fsops->fs_setup)
304                 return obd->obd_fsops->fs_setup(fs);
305
306         return 0;
307 }
308
309 static inline int
310 fsfilt_set_md(struct obd_device *obd, struct inode *inode,
311               void *handle, void *md, int size)
312 {
313         return obd->obd_fsops->fs_set_md(inode, handle, md, size);
314 }
315
316 static inline int
317 fsfilt_get_md(struct obd_device *obd, struct inode *inode,
318               void *md, int size)
319 {
320         return obd->obd_fsops->fs_get_md(inode, md, size);
321 }
322
323 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
324 static inline int
325 fsfilt_send_bio(struct obd_device *obd, struct inode *inode,
326                 struct bio *bio)
327 #else
328 static inline int
329 fsfilt_send_bio(struct obd_device *obd, struct inode *inode,
330                 struct kiobuf *bio)
331 #endif
332 {
333         return obd->obd_fsops->fs_send_bio(inode, bio);
334 }
335
336 static inline int
337 fsfilt_putpage(struct obd_device *obd, struct inode *inode,
338                struct page *page)
339 {
340         int rc = 0;
341         struct filter_obd *filter;
342         unsigned long now = jiffies;
343
344         LASSERT(obd != NULL);
345         LASSERT(inode != NULL);
346         LASSERT(page != NULL);
347
348         filter = &obd->u.filter;
349
350         if (!obd->obd_fsops->fs_putpage)
351                 return -ENOSYS;
352
353         CDEBUG(D_INFO, "putpage %lx\n", page->index);
354
355         rc = obd->obd_fsops->fs_putpage(inode, page);
356
357         if (time_after(jiffies, now + 15 * HZ))
358                 CERROR("long putpage time %lus\n", (jiffies - now) / HZ);
359
360         return rc;
361 }
362
363 static inline struct page *
364 fsfilt_getpage(struct obd_device *obd, struct inode *inode,
365                unsigned long index)
366 {
367         struct page *page;
368         unsigned long now = jiffies;
369
370         LASSERT(obd != NULL);
371         LASSERT(inode != NULL);
372
373         if (!obd->obd_fsops->fs_getpage)
374                 return ERR_PTR(-ENOSYS);
375
376         CDEBUG(D_INFO, "getpage %lx\n", index);
377
378         page = obd->obd_fsops->fs_getpage(inode, index);
379
380         if (time_after(jiffies, now + 15 * HZ))
381                 CERROR("long getpage time %lus\n", (jiffies - now) / HZ);
382
383         return page;
384 }
385
386 static inline ssize_t
387 fsfilt_readpage(struct obd_device *obd, struct file *file, char *buf,
388                 size_t count, loff_t *offset)
389 {
390         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
391 }
392
393 static inline int
394 fsfilt_add_journal_cb(struct obd_device *obd, struct super_block *sb,
395                       __u64 last_rcvd, void *handle, fsfilt_cb_t cb_func,
396                       void *cb_data)
397 {
398         return obd->obd_fsops->fs_add_journal_cb(obd, sb, last_rcvd, handle,
399                                                  cb_func, cb_data);
400 }
401
402 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
403 static inline int
404 fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
405               unsigned long max_age)
406 {
407         int rc = 0;
408
409         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
410         if (time_before(obd->obd_osfs_age, max_age)) {
411                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
412                 if (rc == 0) /* N.B. statfs can't really fail */
413                         obd->obd_osfs_age = jiffies;
414         } else {
415                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
416         }
417
418         return rc;
419 }
420
421 static inline int
422 fsfilt_sync(struct obd_device *obd, struct super_block *sb)
423 {
424         return obd->obd_fsops->fs_sync(sb);
425 }
426
427 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
428                                          struct inode *inode,
429                                          struct page **page, int pages,
430                                          unsigned long *blocks, int *created,
431                                          int create, struct semaphore *sem)
432 {
433         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
434                                                   created, create, sem);
435 }
436
437 static inline int
438 fs_prep_san_write(struct obd_device *obd, struct inode *inode,
439                   long *blocks, int nblocks, loff_t newsize)
440 {
441         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
442                                                  nblocks, newsize);
443 }
444
445 static inline int
446 fsfilt_read_record(struct obd_device *obd, struct file *file,
447                    void *buf, loff_t size, loff_t *offs)
448 {
449         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
450 }
451
452 static inline int
453 fsfilt_write_record(struct obd_device *obd, struct file *file,
454                     void *buf, loff_t size, loff_t *offs, int force_sync)
455 {
456         return obd->obd_fsops->fs_write_record(file, buf, size, offs,
457                                                force_sync);
458 }
459
460 static inline int
461 llog_fsfilt_write_record(struct llog_ctxt *ctxt, struct file *file,
462                          void *buf, loff_t size, loff_t *offs,
463                          int force_sync)
464 {
465         return ctxt->loc_fsops->fs_write_record(file, buf, size, offs,
466                                                 force_sync);
467 }
468
469 static inline int
470 llog_fsfilt_read_record(struct llog_ctxt *ctxt, struct file *file,
471                         void *buf, loff_t size, loff_t *offs)
472 {
473         return ctxt->loc_fsops->fs_read_record(file, buf, size, offs);
474 }
475
476 #endif /* __KERNEL__ */
477
478 #endif