Whamcloud - gitweb
branch: 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 _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_write_record)(struct file *, void *, int size, loff_t *,
96                                     int force_sync);
97         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
98         int     (* fs_setup)(struct super_block *sb);
99         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
100         int     (* fs_quotactl)(struct super_block *sb,
101                                 struct obd_quotactl *oqctl);
102         int     (* fs_quotacheck)(struct super_block *sb,
103                                   struct obd_quotactl *oqctl);
104         __u64   (* fs_get_version) (struct inode *inode);
105         __u64   (* fs_set_version) (struct inode *inode, __u64 new_version);
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, 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 + DISK_TIMEOUT * 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 #define fsfilt_check_slow(obd, start, msg)              \
183 do {                                                    \
184         __fsfilt_check_slow(obd, start, msg);           \
185         start = jiffies;                                \
186 } while (0)
187
188 static inline void *fsfilt_start_log(struct obd_device *obd,
189                                      struct inode *inode, int op,
190                                      struct obd_trans_info *oti, int logs)
191 {
192         unsigned long now = jiffies;
193         void *parent_handle = oti ? oti->oti_handle : NULL;
194         void *handle;
195
196         if (obd->obd_fail)
197                 return ERR_PTR(-EROFS);
198
199         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
200         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
201
202         if (oti != NULL) {
203                 if (parent_handle == NULL) {
204                         oti->oti_handle = handle;
205                 } else if (handle != parent_handle) {
206                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
207                                parent_handle, handle, oti);
208                         LBUG();
209                 }
210         }
211         fsfilt_check_slow(obd, now, "journal start");
212         return handle;
213 }
214
215 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
216                                  int op, struct obd_trans_info *oti)
217 {
218         return fsfilt_start_log(obd, inode, op, oti, 0);
219 }
220
221 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
222                                          struct fsfilt_objinfo *fso,
223                                          int niocount, struct niobuf_local *nb,
224                                          struct obd_trans_info *oti, int logs)
225 {
226         unsigned long now = jiffies;
227         void *parent_handle = oti ? oti->oti_handle : NULL;
228         void *handle;
229
230         if (obd->obd_fail)
231                 return ERR_PTR(-EROFS);
232
233         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
234                                               parent_handle, logs);
235         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
236
237         if (oti != NULL) {
238                 if (parent_handle == NULL) {
239                         oti->oti_handle = handle;
240                 } else if (handle != parent_handle) {
241                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
242                                parent_handle, handle, oti);
243                         LBUG();
244                 }
245         }
246         fsfilt_check_slow(obd, now, "journal start");
247
248         return handle;
249 }
250
251 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
252                                      struct fsfilt_objinfo *fso, int niocount,
253                                      struct niobuf_local *nb,
254                                      struct obd_trans_info *oti)
255 {
256         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
257 }
258
259 static inline int fsfilt_extend(struct obd_device *obd, struct inode *inode,
260                                 unsigned int nblocks, void *handle)
261 {
262         unsigned long now = jiffies;
263         int rc = obd->obd_fsops->fs_extend(inode, nblocks, handle);
264         CDEBUG(D_INFO, "extending handle %p with %u blocks\n", handle, nblocks);
265
266         fsfilt_check_slow(obd, now, "journal extend");
267
268         return rc;
269 }
270
271 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
272                                 void *handle, int force_sync)
273 {
274         unsigned long now = jiffies;
275         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
276         CDEBUG(D_INFO, "committing handle %p\n", handle);
277
278         fsfilt_check_slow(obd, now, "journal start");
279
280         return rc;
281 }
282
283 static inline int fsfilt_commit_async(struct obd_device *obd,
284                                       struct inode *inode, void *handle,
285                                       void **wait_handle)
286 {
287         unsigned long now = jiffies;
288         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
289
290         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
291         fsfilt_check_slow(obd, now, "journal start");
292
293         return rc;
294 }
295
296 static inline int fsfilt_commit_wait(struct obd_device *obd,
297                                      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_INFO, "waiting for completion %p\n", handle);
302         fsfilt_check_slow(obd, now, "journal start");
303         return rc;
304 }
305
306 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
307                                  void *handle, struct iattr *iattr,int do_trunc)
308 {
309         unsigned long now = jiffies;
310         int rc;
311         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
312         fsfilt_check_slow(obd, now, "setattr");
313         return rc;
314 }
315
316 static inline int fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
317                                    struct file *file, unsigned int cmd,
318                                    unsigned long arg)
319 {
320         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
321 }
322
323 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
324                                 void *handle, void *md, int size,
325                                 const char *name)
326 {
327         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
328 }
329
330 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
331                                 void *md, int size, const char *name)
332 {
333         return obd->obd_fsops->fs_get_md(inode, md, size, name);
334 }
335
336 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
337                                   struct inode *inode, void *bio)
338 {
339         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
340
341         if (rw == OBD_BRW_READ)
342                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
343         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
344 }
345
346 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
347                                       struct file *file, char *buf,
348                                       size_t count, loff_t *offset)
349 {
350         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
351 }
352
353 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
354                                         void *handle, fsfilt_cb_t cb_func,
355                                         void *cb_data)
356 {
357         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
358                                                  handle, cb_func, cb_data);
359 }
360
361 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
362 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
363                                 __u64 max_age)
364 {
365         int rc = 0;
366
367         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
368                 obd->obd_osfs_age, max_age);
369         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
370                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
371                 if (rc == 0) /* N.B. statfs can't really fail */
372                         obd->obd_osfs_age = cfs_time_current_64();
373         } else {
374                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
375         }
376
377         return rc;
378 }
379
380 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
381 {
382         return obd->obd_fsops->fs_sync(sb);
383 }
384
385 static inline int fsfilt_quotacheck(struct obd_device *obd,
386                                     struct super_block *sb,
387                                     struct obd_quotactl *oqctl)
388 {
389         if (obd->obd_fsops->fs_quotacheck)
390                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
391         return -ENOTSUPP;
392 }
393
394 static inline int fsfilt_quotactl(struct obd_device *obd,
395                                   struct super_block *sb,
396                                   struct obd_quotactl *oqctl)
397 {
398         if (obd->obd_fsops->fs_quotactl)
399                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
400         return -ENOTSUPP;
401 }
402
403 static inline int fsfilt_quotainfo(struct obd_device *obd,
404                                    struct lustre_quota_info *lqi,
405                                    int type, int cmd)
406 {
407         if (obd->obd_fsops->fs_quotainfo)
408                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
409         return -ENOTSUPP;
410 }
411
412 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
413                               struct inode *inode, int type, 
414                               struct list_head *list)
415 {
416         if (obd->obd_fsops->fs_qids)
417                 return obd->obd_fsops->fs_qids(file, inode, type, list);
418         return -ENOTSUPP;
419 }
420
421 static inline int fsfilt_dquot(struct obd_device *obd,
422                                struct lustre_dquot *dquot, int cmd)
423 {
424         if (obd->obd_fsops->fs_dquot)
425                 return obd->obd_fsops->fs_dquot(dquot, cmd);
426         return -ENOTSUPP;
427 }
428
429 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
430                                          struct inode *inode,
431                                          struct page **page, int pages,
432                                          unsigned long *blocks, int *created,
433                                          int create, struct semaphore *sem)
434 {
435         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
436                                                   created, create, sem);
437 }
438
439 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
440                                      void *buf, loff_t size, loff_t *offs)
441 {
442         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
443 }
444
445 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
446                                       void *buf, loff_t size, loff_t *offs,
447                                       int force_sync)
448 {
449         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
450 }
451
452 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
453 {
454         if (obd->obd_fsops->fs_setup)
455                 return obd->obd_fsops->fs_setup(fs);
456         return 0;
457 }
458
459 static inline __u64 fsfilt_set_version(struct obd_device *obd,
460                                       struct inode *inode, __u64 new_version)
461 {
462         if (obd->obd_fsops->fs_set_version)
463                 return obd->obd_fsops->fs_set_version(inode, new_version);
464         return -EOPNOTSUPP;
465 }
466
467 static inline __u64 fsfilt_get_version(struct obd_device *obd,
468                                        struct inode *inode)
469 {
470         if (obd->obd_fsops->fs_set_version)
471                 return obd->obd_fsops->fs_get_version(inode);
472         return -EOPNOTSUPP;
473 }
474
475 #endif /* __KERNEL__ */
476
477 #endif