Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[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 obd_device *, struct super_block *);
92         
93         int     (* fs_post_setup)(struct obd_device *obd, struct vfsmount *mnt);
94         int     (* fs_post_cleanup)(struct obd_device *obd, struct vfsmount *mnt);
95         int     (* fs_get_reint_log_ctxt)(struct super_block *sb, 
96                                           struct llog_ctxt **ctxt);
97         int     (* fs_set_kml_flags)(struct inode *inode);
98         int     (* fs_set_ost_flags)(struct super_block *sb);
99         int     (* fs_set_mds_flags)(struct super_block *sb);
100
101         int     (* fs_set_xattr)(struct inode *inode, void *handle, char *name,
102                                  void *buffer, int buffer_size);
103         int     (* fs_get_xattr)(struct inode *inode, char *name,
104                                  void *buffer, int buffer_size); 
105         
106         int     (* fs_init_extents_ea)(struct inode *inode); 
107         int     (* fs_insert_extents_ea)(struct inode *inode, unsigned long from, 
108                                          unsigned long num); 
109         int     (* fs_write_extents)(struct dentry *dentry, 
110                                      unsigned long offset, unsigned long blks);
111         int     (* fs_remove_extents_ea)(struct inode *inode, unsigned long from, 
112                                          unsigned long num); 
113         int     (* fs_get_ino_write_extents)(struct super_block *sb, ino_t ino, 
114                                              char **pbuf, int *size);
115         int     (* fs_free_write_extents)(struct super_block *sb, ino_t ino, 
116                                           char *pbuf, int size);
117         int     (* fs_get_inode_write_extents)(struct inode *inode, char **pbuf, 
118                                                int *size);
119         int     (* fs_get_write_extents_num)(struct inode *inode, int* size);
120
121         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
122         int     (* fs_add_dir_entry)(struct obd_device *, struct dentry *,
123                                      char *, int, unsigned long, unsigned long,
124                                      unsigned);
125         int     (* fs_del_dir_entry)(struct obd_device *, struct dentry *);
126 };
127
128 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
129 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
130 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
131 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
132
133 #define FSFILT_OP_UNLINK         1
134 #define FSFILT_OP_RMDIR          2
135 #define FSFILT_OP_RENAME         3
136 #define FSFILT_OP_CREATE         4
137 #define FSFILT_OP_MKDIR          5
138 #define FSFILT_OP_SYMLINK        6
139 #define FSFILT_OP_MKNOD          7
140 #define FSFILT_OP_SETATTR        8
141 #define FSFILT_OP_LINK           9
142 #define FSFILT_OP_CANCEL_UNLINK 10
143 #define FSFILT_OP_NOOP          15
144
145 /* XXX BUG 3188 -- must return to one set of opcodes */
146 #define KML_UNLINK              0x11
147 #define KML_RMDIR               0x12
148 #define KML_RENAME              0x13
149 #define KML_CREATE              0x14
150 #define KML_MKDIR               0x15
151 #define KML_SYMLINK             0x16
152 #define KML_MKNOD               0x17
153 #define KML_LINK                0x19
154
155 #define CACHE_UNLINK            0x21
156 #define CACHE_RMDIR             0x22
157 #define CACHE_RENAME            0x23
158 #define CACHE_CREATE            0x24
159 #define CACHE_MKDIR             0x25
160 #define CACHE_SYMLINK           0x26
161 #define CACHE_MKNOD             0x27
162 #define CACHE_LINK              0x29
163 #define CACHE_NOOP              0x2f
164
165 #define KML_CACHE_UNLINK        0x31
166 #define KML_CACHE_RMDIR         0x32
167 #define KML_CACHE_RENAME        0x33
168 #define KML_CACHE_CREATE        0x34
169 #define KML_CACHE_MKDIR         0x35
170 #define KML_CACHE_SYMLINK       0x36
171 #define KML_CACHE_MKNOD         0x37
172 #define KML_CACHE_LINK          0x39
173 #define KML_CACHE_NOOP          0x3f
174
175 static inline void *
176 fsfilt_start_ops(struct fsfilt_operations *ops, struct inode *inode,
177                  int op, struct obd_trans_info *oti, int logs)
178 {
179         unsigned long now = jiffies;
180         void *parent_handle = oti ? oti->oti_handle : NULL;
181         void *handle = ops->fs_start(inode, op, parent_handle, logs);
182         CDEBUG(D_HA, "started handle %p (%p)\n", handle, parent_handle);
183
184         if (oti != NULL) {
185                 if (parent_handle == NULL) {
186                         oti->oti_handle = handle;
187                 } else if (handle != parent_handle) {
188                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
189                                parent_handle, handle, oti);
190                         LBUG();
191                 }
192         }
193         if (time_after(jiffies, now + 15 * HZ))
194                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
195         return handle;
196 }
197
198 static inline void *
199 fsfilt_start_log(struct obd_device *obd, struct inode *inode,
200                  int op, struct obd_trans_info *oti, int logs)
201 {
202         return fsfilt_start_ops(obd->obd_fsops, inode, op, oti, logs);
203 }
204
205 static inline void *
206 fsfilt_start(struct obd_device *obd, struct inode *inode,
207              int op, struct obd_trans_info *oti)
208 {
209         return fsfilt_start_ops(obd->obd_fsops, inode, op, oti, 0);
210 }
211
212 static inline void *
213 llog_fsfilt_start(struct llog_ctxt *ctxt, struct inode *inode,
214                   int op, struct obd_trans_info *oti)
215 {
216         return fsfilt_start_ops(ctxt->loc_fsops, inode, op, oti, 1);
217 }
218
219 static inline int
220 fsfilt_commit_ops(struct fsfilt_operations *ops, struct inode *inode,
221                   void *handle, int force_sync)
222 {
223         unsigned long now = jiffies;
224         int rc = ops->fs_commit(inode, handle, force_sync);
225         CDEBUG(D_HA, "committing handle %p\n", handle);
226
227         if (time_after(jiffies, now + 15 * HZ))
228                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
229
230         return rc;
231 }
232
233 static inline int
234 fsfilt_commit(struct obd_device *obd, struct inode *inode,
235               void *handle, int force_sync)
236 {
237         return fsfilt_commit_ops(obd->obd_fsops, inode, handle, force_sync);
238 }
239
240 static inline int
241 llog_fsfilt_commit(struct llog_ctxt *ctxt, struct inode *inode,
242                    void *handle, int force_sync)
243 {
244         return fsfilt_commit_ops(ctxt->loc_fsops, inode, handle, force_sync);
245 }
246
247 static inline void *
248 fsfilt_brw_start_log(struct obd_device *obd, int objcount,
249                      struct fsfilt_objinfo *fso, int niocount,
250                      struct niobuf_local *nb, struct obd_trans_info *oti,
251                      int logs)
252 {
253         unsigned long now = jiffies;
254         void *parent_handle = oti ? oti->oti_handle : NULL;
255         void *handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
256                                                     parent_handle, logs);
257         CDEBUG(D_HA, "started handle %p (%p)\n", handle, parent_handle);
258
259         if (oti != NULL) {
260                 if (parent_handle == NULL) {
261                         oti->oti_handle = handle;
262                 } else if (handle != parent_handle) {
263                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
264                                parent_handle, handle, oti);
265                         LBUG();
266                 }
267         }
268         if (time_after(jiffies, now + 15 * HZ))
269                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
270
271         return handle;
272 }
273
274 static inline void *
275 fsfilt_brw_start(struct obd_device *obd, int objcount,
276                  struct fsfilt_objinfo *fso, int niocount,
277                  struct niobuf_local *nb, struct obd_trans_info *oti)
278 {
279         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
280 }
281
282 static inline int
283 fsfilt_commit_async(struct obd_device *obd, struct inode *inode,
284                     void *handle, void **wait_handle)
285 {
286         unsigned long now = jiffies;
287         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
288
289         CDEBUG(D_HA, "committing handle %p (async)\n", *wait_handle);
290         if (time_after(jiffies, now + 15 * HZ))
291                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
292
293         return rc;
294 }
295
296 static inline int
297 fsfilt_commit_wait(struct obd_device *obd, struct inode *inode, void *handle)
298 {
299         unsigned long now = jiffies;
300         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
301         CDEBUG(D_HA, "waiting for completion %p\n", handle);
302         if (time_after(jiffies, now + 15 * HZ))
303                 CERROR("long journal start time %lus\n", (jiffies - now) / HZ);
304         return rc;
305 }
306
307 static inline int
308 fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
309                void *handle, struct iattr *iattr, int do_trunc)
310 {
311         unsigned long now = jiffies;
312         int rc;
313         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
314         if (time_after(jiffies, now + 15 * HZ))
315                 CERROR("long setattr time %lus\n", (jiffies - now) / HZ);
316         return rc;
317 }
318
319 static inline int
320 fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
321                  struct file *file, unsigned int cmd,
322                  unsigned long arg)
323 {
324         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
325 }
326
327 static inline int fsfilt_setup(struct obd_device *obd,
328                                struct super_block *fs)
329 {
330         if (obd->obd_fsops->fs_setup)
331                 return obd->obd_fsops->fs_setup(obd, fs);
332         return 0;
333 }
334
335 static inline int
336 fsfilt_set_md(struct obd_device *obd, struct inode *inode,
337               void *handle, void *md, int size)
338 {
339         return obd->obd_fsops->fs_set_md(inode, handle, md, size);
340 }
341
342 static inline int
343 fsfilt_get_md(struct obd_device *obd, struct inode *inode,
344               void *md, int size)
345 {
346         return obd->obd_fsops->fs_get_md(inode, md, size);
347 }
348
349 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
350 static inline int
351 fsfilt_send_bio(struct obd_device *obd, struct inode *inode,
352                 struct bio *bio)
353 #else
354 static inline int
355 fsfilt_send_bio(struct obd_device *obd, struct inode *inode,
356                 struct kiobuf *bio)
357 #endif
358 {
359         return obd->obd_fsops->fs_send_bio(inode, bio);
360 }
361
362 static inline int
363 fsfilt_putpage(struct obd_device *obd, struct inode *inode,
364                struct page *page)
365 {
366         int rc = 0;
367         struct filter_obd *filter;
368         unsigned long now = jiffies;
369
370         LASSERT(obd != NULL);
371         LASSERT(inode != NULL);
372         LASSERT(page != NULL);
373
374         filter = &obd->u.filter;
375
376         if (!obd->obd_fsops->fs_putpage)
377                 return -ENOSYS;
378
379         CDEBUG(D_INFO, "putpage %lx\n", page->index);
380
381         rc = obd->obd_fsops->fs_putpage(inode, page);
382
383         if (time_after(jiffies, now + 15 * HZ))
384                 CERROR("long putpage time %lus\n", (jiffies - now) / HZ);
385
386         return rc;
387 }
388
389 static inline struct page *
390 fsfilt_getpage(struct obd_device *obd, struct inode *inode,
391                unsigned long index)
392 {
393         struct page *page;
394         unsigned long now = jiffies;
395
396         LASSERT(obd != NULL);
397         LASSERT(inode != NULL);
398
399         if (!obd->obd_fsops->fs_getpage)
400                 return ERR_PTR(-ENOSYS);
401
402         CDEBUG(D_INFO, "getpage %lx\n", index);
403
404         page = obd->obd_fsops->fs_getpage(inode, index);
405
406         if (time_after(jiffies, now + 15 * HZ))
407                 CERROR("long getpage time %lus\n", (jiffies - now) / HZ);
408
409         return page;
410 }
411
412 static inline ssize_t
413 fsfilt_readpage(struct obd_device *obd, struct file *file, char *buf,
414                 size_t count, loff_t *offset)
415 {
416         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
417 }
418
419 static inline int
420 fsfilt_add_journal_cb(struct obd_device *obd, struct super_block *sb,
421                       __u64 last_rcvd, void *handle, fsfilt_cb_t cb_func,
422                       void *cb_data)
423 {
424         return obd->obd_fsops->fs_add_journal_cb(obd, sb, last_rcvd, handle,
425                                                  cb_func, cb_data);
426 }
427
428 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
429 static inline int
430 fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
431               unsigned long max_age)
432 {
433         int rc = 0;
434
435         CDEBUG(D_SUPER, "osfs %lu, max_age %lu\n", obd->obd_osfs_age, max_age);
436         if (time_before(obd->obd_osfs_age, max_age)) {
437                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
438                 if (rc == 0) /* N.B. statfs can't really fail */
439                         obd->obd_osfs_age = jiffies;
440         } else {
441                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
442         }
443
444         return rc;
445 }
446
447 static inline int
448 fsfilt_sync(struct obd_device *obd, struct super_block *sb)
449 {
450         return obd->obd_fsops->fs_sync(sb);
451 }
452
453 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
454                                          struct inode *inode,
455                                          struct page **page, int pages,
456                                          unsigned long *blocks, int *created,
457                                          int create, struct semaphore *sem)
458 {
459         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
460                                                   created, create, sem);
461 }
462
463 static inline int 
464 fsfilt_write_extents(struct obd_device *obd, struct dentry *dentry, 
465                      unsigned long offset, unsigned long blks)
466 {
467         if (obd->obd_fsops->fs_write_extents)
468                 return obd->obd_fsops->fs_write_extents(dentry, 
469                                                         offset, blks);
470         return 0;
471 }
472
473 static inline int
474 fs_prep_san_write(struct obd_device *obd, struct inode *inode,
475                   long *blocks, int nblocks, loff_t newsize)
476 {
477         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
478                                                  nblocks, newsize);
479 }
480
481 static inline int
482 fsfilt_read_record(struct obd_device *obd, struct file *file,
483                    void *buf, loff_t size, loff_t *offs)
484 {
485         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
486 }
487
488 static inline int 
489 llog_fsfilt_read_record(struct llog_ctxt *ctxt, struct file *file, 
490                         void *buf, loff_t size, loff_t *offs)
491 {
492         return ctxt->loc_fsops->fs_read_record(file, buf, size, offs);
493 }
494
495 static inline int
496 fsfilt_write_record(struct obd_device *obd, struct file *file,
497                     void *buf, loff_t size, loff_t *offs, int force_sync)
498 {
499         return obd->obd_fsops->fs_write_record(file, buf, size, offs,
500                                                force_sync);
501 }
502
503 static inline int
504 llog_fsfilt_write_record(struct llog_ctxt *ctxt, struct file *file,
505                          void *buf, loff_t size, loff_t *offs,
506                          int force_sync)
507 {
508         return ctxt->loc_fsops->fs_write_record(file, buf, size, offs,
509                                                 force_sync);
510 }
511
512 static inline int 
513 fsfilt_set_kml_flags(struct obd_device *obd, struct inode *inode)
514 {
515         if (obd->obd_fsops->fs_set_kml_flags)
516                 return obd->obd_fsops->fs_set_kml_flags(inode);
517         return 0;
518 }
519
520 static inline int 
521 fsfilt_post_setup(struct obd_device *obd)
522 {
523         if (obd->obd_fsops->fs_post_setup)
524                 return obd->obd_fsops->fs_post_setup(obd, 
525                                 obd->obd_lvfs_ctxt.pwdmnt);
526         return 0;
527 }
528
529 static inline int 
530 fsfilt_post_cleanup(struct obd_device *obd)
531 {
532         if (obd->obd_fsops->fs_post_cleanup)
533                 return obd->obd_fsops->fs_post_cleanup(obd, 
534                                 obd->obd_lvfs_ctxt.pwdmnt);
535         return 0;
536 }
537
538 static inline int 
539 fsfilt_get_ino_write_extents(struct obd_device *obd, 
540                              struct super_block *sb, 
541                              int ino, char **buf, int *size)
542 {
543         if (obd->obd_fsops->fs_get_ino_write_extents)
544                 return obd->obd_fsops->fs_get_ino_write_extents(sb, ino, 
545                                                                 buf, size);
546         return 0;
547 }
548
549 static inline int 
550 fsfilt_free_write_extents(struct obd_device *obd, 
551                           struct super_block *sb, 
552                           int ino, char *buf, int size)
553 {
554         if (obd->obd_fsops->fs_free_write_extents)
555                 return obd->obd_fsops->fs_free_write_extents(sb, ino, 
556                                                              buf, size);
557         return 0;
558 }
559
560 static inline int 
561 fsfilt_get_reint_log_ctxt(struct obd_device *obd,
562                           struct super_block *sb, 
563                           struct llog_ctxt **ctxt)
564 {
565         if (obd->obd_fsops->fs_get_reint_log_ctxt)
566                 return obd->obd_fsops->fs_get_reint_log_ctxt(sb, ctxt);
567         return 0;
568 }
569
570 static inline int 
571 fsfilt_set_ost_flags(struct obd_device *obd, struct super_block *sb) 
572 {
573         if (obd->obd_fsops->fs_set_ost_flags)
574                 return obd->obd_fsops->fs_set_ost_flags(sb);
575         return 0;
576 }
577
578 static inline int 
579 fsfilt_set_mds_flags(struct obd_device *obd, struct super_block *sb) 
580 {
581         if (obd->obd_fsops->fs_set_mds_flags)
582                 return obd->obd_fsops->fs_set_mds_flags(sb);
583         return 0;
584 }
585
586 static inline int 
587 fsfilt_add_dir_entry(struct obd_device *obd, struct dentry *dir,
588                      char *name, int namelen, unsigned long ino,
589                      unsigned long generation, unsigned mds)
590 {
591         LASSERT(obd->obd_fsops->fs_add_dir_entry);
592         return obd->obd_fsops->fs_add_dir_entry(obd, dir, name,
593                                                 namelen, ino, generation, mds);
594 }
595
596 static inline int 
597 fsfilt_del_dir_entry(struct obd_device *obd, struct dentry *dentry)
598 {
599         LASSERT(obd->obd_fsops->fs_del_dir_entry);
600         return obd->obd_fsops->fs_del_dir_entry(obd, dentry);
601 }
602
603 #endif /* __KERNEL__ */
604
605 #endif