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