Whamcloud - gitweb
b=10719
[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 _LINUX_LUSTRE_FSFILT_H
26 #define _LINUX_LUSTRE_FSFILT_H
27
28 #ifndef _LUSTRE_FSFILT_H
29 #error Do not #include this file directly. #include <lustre_fsfilt.h> instead
30 #endif
31
32 #ifdef __KERNEL__
33
34 #include <obd.h>
35 #include <obd_class.h>
36
37 typedef void (*fsfilt_cb_t)(struct obd_device *obd, __u64 last_rcvd,
38                             void *data, int error);
39
40 struct fsfilt_objinfo {
41         struct dentry *fso_dentry;
42         int fso_bufcnt;
43 };
44
45 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
46
47 struct lustre_dquot;
48 struct fsfilt_operations {
49         struct list_head fs_list;
50         struct module *fs_owner;
51         char   *fs_type;
52         char   *(* fs_getlabel)(struct super_block *sb);
53         int     (* fs_setlabel)(struct super_block *sb, char *label);
54         char   *(* fs_uuid)(struct super_block *sb);
55         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
56                              int logs);
57         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
58                                  int niocount, struct niobuf_local *nb,
59                                  void *desc_private, int logs);
60         int     (* fs_extend)(struct inode *inode, unsigned nblocks, void *h);
61         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
62         int     (* fs_commit_async)(struct inode *inode, void *handle,
63                                         void **wait_handle);
64         int     (* fs_commit_wait)(struct inode *inode, void *handle);
65         int     (* fs_setattr)(struct dentry *dentry, void *handle,
66                                struct iattr *iattr, int do_trunc);
67         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
68                                  unsigned int cmd, unsigned long arg);
69         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
70                               int size, const char *name);
71         int     (* fs_get_md)(struct inode *inode, void *md, int size,
72                               const char *name);
73         /*
74          * this method is needed to make IO operation fsfilt nature depend.
75          *
76          * This operation maybe synchronous or asynchronous.
77          *
78          * Return convention: positive number of bytes written (synchronously)
79          * on success. Negative errno value on failure. Zero if asynchronous
80          * IO was submitted successfully.
81          *
82          */
83         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
84         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
85                                 loff_t *offset);
86         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
87                                       void *handle, fsfilt_cb_t cb_func,
88                                       void *cb_data);
89         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
90         int     (* fs_sync)(struct super_block *sb);
91         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
92                                        int pages, unsigned long *blocks,
93                                        int *created, int create,
94                                        struct semaphore *sem);
95         int     (* fs_prep_san_write)(struct inode *inode, long *blocks,
96                                       int nblocks, loff_t newsize);
97         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
98                                     int force_sync);
99         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
100         int     (* fs_setup)(struct super_block *sb);
101         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
102         int     (* fs_quotacheck)(struct super_block *sb,
103                                   struct obd_quotactl *oqctl);
104         int     (* fs_quotactl)(struct super_block *sb,
105                                 struct obd_quotactl *oqctl);
106         int     (* fs_quotainfo)(struct lustre_quota_info *lqi, int type,
107                                  int cmd);
108         int     (* fs_qids)(struct file *file, struct inode *inode, int type,
109                             struct list_head *list);
110         int     (* fs_dquot)(struct lustre_dquot *dquot, int cmd);
111         lvfs_sbdev_type (* fs_journal_sbdev)(struct super_block *sb);
112 };
113
114 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
115 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
116 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
117 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
118
119 static inline char *fsfilt_get_label(struct obd_device *obd,
120                                      struct super_block *sb)
121 {
122         if (obd->obd_fsops->fs_getlabel == NULL)
123                 return NULL;
124         if (obd->obd_fsops->fs_getlabel(sb)[0] == '\0')
125                 return NULL;
126
127         return obd->obd_fsops->fs_getlabel(sb);
128 }
129
130 static inline int fsfilt_set_label(struct obd_device *obd,
131                                    struct super_block *sb, char *label)
132 {
133         if (obd->obd_fsops->fs_setlabel == NULL)
134                 return -ENOSYS;
135         return (obd->obd_fsops->fs_setlabel(sb, label));
136 }
137
138 static inline __u8 *fsfilt_uuid(struct obd_device *obd, struct super_block *sb)
139 {
140         if (obd->obd_fsops->fs_uuid == NULL)
141                 return NULL;
142
143         return obd->obd_fsops->fs_uuid(sb);
144 }
145
146 static inline lvfs_sbdev_type fsfilt_journal_sbdev(struct obd_device *obd,
147                                                    struct super_block *sb)
148 {
149         if (obd && obd->obd_fsops && obd->obd_fsops->fs_journal_sbdev)
150                 return obd->obd_fsops->fs_journal_sbdev(sb);
151         return (lvfs_sbdev_type)0;
152 }
153
154 #define FSFILT_OP_UNLINK         1
155 #define FSFILT_OP_RMDIR          2
156 #define FSFILT_OP_RENAME         3
157 #define FSFILT_OP_CREATE         4
158 #define FSFILT_OP_MKDIR          5
159 #define FSFILT_OP_SYMLINK        6
160 #define FSFILT_OP_MKNOD          7
161 #define FSFILT_OP_SETATTR        8
162 #define FSFILT_OP_LINK           9
163 #define FSFILT_OP_CANCEL_UNLINK 10
164 #define FSFILT_OP_JOIN          11
165 #define FSFILT_OP_NOOP          15
166
167 #define fsfilt_check_slow(obd, start, timeout, msg)                     \
168 do {                                                                    \
169         if (time_before(jiffies, start + 15 * HZ))                      \
170                 break;                                                  \
171         else if (time_before(jiffies, start + 30 * HZ))                 \
172                 CDEBUG(D_VFSTRACE, "%s: slow %s %lus\n", obd->obd_name, \
173                        msg, (jiffies-start) / HZ);                      \
174         else if (time_before(jiffies, start + timeout / 2 * HZ))        \
175                 CWARN("%s: slow %s %lus\n", obd->obd_name, msg,         \
176                       (jiffies - start) / HZ);                          \
177         else                                                            \
178                 CERROR("%s: slow %s %lus\n", obd->obd_name, msg,        \
179                        (jiffies - start) / HZ);                         \
180 } while (0)
181
182 static inline void *fsfilt_start_log(struct obd_device *obd,
183                                      struct inode *inode, int op,
184                                      struct obd_trans_info *oti, int logs)
185 {
186         unsigned long now = jiffies;
187         void *parent_handle = oti ? oti->oti_handle : NULL;
188         void *handle;
189
190         if (obd->obd_fail)
191                 return ERR_PTR(-EROFS);
192
193         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
194         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
195
196         if (oti != NULL) {
197                 if (parent_handle == NULL) {
198                         oti->oti_handle = handle;
199                 } else if (handle != parent_handle) {
200                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
201                                parent_handle, handle, oti);
202                         LBUG();
203                 }
204         }
205         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
206         return handle;
207 }
208
209 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
210                                  int op, struct obd_trans_info *oti)
211 {
212         return fsfilt_start_log(obd, inode, op, oti, 0);
213 }
214
215 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
216                                          struct fsfilt_objinfo *fso,
217                                          int niocount, struct niobuf_local *nb,
218                                          struct obd_trans_info *oti, int logs)
219 {
220         unsigned long now = jiffies;
221         void *parent_handle = oti ? oti->oti_handle : NULL;
222         void *handle;
223
224         if (obd->obd_fail)
225                 return ERR_PTR(-EROFS);
226
227         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
228                                               parent_handle, logs);
229         CDEBUG(D_INFO, "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         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
241
242         return handle;
243 }
244
245 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
246                                      struct fsfilt_objinfo *fso, int niocount,
247                                      struct niobuf_local *nb,
248                                      struct obd_trans_info *oti)
249 {
250         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
251 }
252
253 static inline int fsfilt_extend(struct obd_device *obd, struct inode *inode,
254                                 unsigned int nblocks, void *handle)
255 {
256         unsigned long now = jiffies;
257         int rc = obd->obd_fsops->fs_extend(inode, nblocks, handle);
258         CDEBUG(D_INFO, "extending handle %p with %u blocks\n", handle, nblocks);
259
260         fsfilt_check_slow(obd, now, obd_timeout, "journal extend");
261
262         return rc;
263 }
264
265 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
266                                 void *handle, int force_sync)
267 {
268         unsigned long now = jiffies;
269         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
270         CDEBUG(D_INFO, "committing handle %p\n", handle);
271
272         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
273
274         return rc;
275 }
276
277 static inline int fsfilt_commit_async(struct obd_device *obd,
278                                       struct inode *inode, void *handle,
279                                       void **wait_handle)
280 {
281         unsigned long now = jiffies;
282         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
283
284         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
285         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
286
287         return rc;
288 }
289
290 static inline int fsfilt_commit_wait(struct obd_device *obd,
291                                      struct inode *inode, void *handle)
292 {
293         unsigned long now = jiffies;
294         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
295         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
296         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
297         return rc;
298 }
299
300 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
301                                  void *handle, struct iattr *iattr,int do_trunc)
302 {
303         unsigned long now = jiffies;
304         int rc;
305         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
306         fsfilt_check_slow(obd, now, obd_timeout, "setattr");
307         return rc;
308 }
309
310 static inline int fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
311                                    struct file *file, unsigned int cmd,
312                                    unsigned long arg)
313 {
314         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
315 }
316
317 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
318                                 void *handle, void *md, int size,
319                                 const char *name)
320 {
321         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
322 }
323
324 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
325                                 void *md, int size, const char *name)
326 {
327         return obd->obd_fsops->fs_get_md(inode, md, size, name);
328 }
329
330 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
331                                   struct inode *inode, void *bio)
332 {
333         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
334
335         if (rw == OBD_BRW_READ)
336                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
337         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
338 }
339
340 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
341                                       struct file *file, char *buf,
342                                       size_t count, loff_t *offset)
343 {
344         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
345 }
346
347 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
348                                         void *handle, fsfilt_cb_t cb_func,
349                                         void *cb_data)
350 {
351         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
352                                                  handle, cb_func, cb_data);
353 }
354
355 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
356 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
357                                 __u64 max_age)
358 {
359         int rc = 0;
360
361         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
362                 obd->obd_osfs_age, max_age);
363         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
364                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
365                 if (rc == 0) /* N.B. statfs can't really fail */
366                         obd->obd_osfs_age = cfs_time_current_64();
367         } else {
368                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
369         }
370
371         return rc;
372 }
373
374 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
375 {
376         return obd->obd_fsops->fs_sync(sb);
377 }
378
379 static inline int fsfilt_quotacheck(struct obd_device *obd,
380                                     struct super_block *sb,
381                                     struct obd_quotactl *oqctl)
382 {
383         if (obd->obd_fsops->fs_quotacheck)
384                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
385         return -ENOTSUPP;
386 }
387
388 static inline int fsfilt_quotactl(struct obd_device *obd,
389                                   struct super_block *sb,
390                                   struct obd_quotactl *oqctl)
391 {
392         if (obd->obd_fsops->fs_quotactl)
393                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
394         return -ENOTSUPP;
395 }
396
397 static inline int fsfilt_quotainfo(struct obd_device *obd,
398                                    struct lustre_quota_info *lqi,
399                                    int type, int cmd)
400 {
401         if (obd->obd_fsops->fs_quotainfo)
402                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
403         return -ENOTSUPP;
404 }
405
406 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
407                               struct inode *inode, int type, 
408                               struct list_head *list)
409 {
410         if (obd->obd_fsops->fs_qids)
411                 return obd->obd_fsops->fs_qids(file, inode, type, list);
412         return -ENOTSUPP;
413 }
414
415 static inline int fsfilt_dquot(struct obd_device *obd,
416                                struct lustre_dquot *dquot, int cmd)
417 {
418         if (obd->obd_fsops->fs_dquot)
419                 return obd->obd_fsops->fs_dquot(dquot, cmd);
420         return -ENOTSUPP;
421 }
422
423 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
424                                          struct inode *inode,
425                                          struct page **page, int pages,
426                                          unsigned long *blocks, int *created,
427                                          int create, struct semaphore *sem)
428 {
429         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
430                                                   created, create, sem);
431 }
432
433 static inline int fs_prep_san_write(struct obd_device *obd, struct inode *inode,
434                                     long *blocks, int nblocks, loff_t newsize)
435 {
436         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
437                                                  nblocks, newsize);
438 }
439
440 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
441                                      void *buf, loff_t size, loff_t *offs)
442 {
443         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
444 }
445
446 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
447                                       void *buf, loff_t size, loff_t *offs,
448                                       int force_sync)
449 {
450         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
451 }
452
453 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
454 {
455         if (obd->obd_fsops->fs_setup)
456                 return obd->obd_fsops->fs_setup(fs);
457         return 0;
458 }
459
460 #endif /* __KERNEL__ */
461
462 #endif