Whamcloud - gitweb
9cdb99c50c6410e6f5ef3310de5e35382c69cd78
[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 };
112
113 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
114 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
115 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
116 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
117
118 static inline char *fsfilt_get_label(struct obd_device *obd,
119                                      struct super_block *sb)
120 {
121         if (obd->obd_fsops->fs_getlabel == NULL)
122                 return NULL;
123         if (obd->obd_fsops->fs_getlabel(sb)[0] == '\0')
124                 return NULL;
125
126         return obd->obd_fsops->fs_getlabel(sb);
127 }
128
129 static inline int fsfilt_set_label(struct obd_device *obd,
130                                    struct super_block *sb, char *label)
131 {
132         if (obd->obd_fsops->fs_setlabel == NULL)
133                 return -ENOSYS;
134         return (obd->obd_fsops->fs_setlabel(sb, label));
135 }
136
137 static inline __u8 *fsfilt_uuid(struct obd_device *obd, struct super_block *sb)
138 {
139         if (obd->obd_fsops->fs_uuid == NULL)
140                 return NULL;
141
142         return obd->obd_fsops->fs_uuid(sb);
143 }
144
145 #define FSFILT_OP_UNLINK         1
146 #define FSFILT_OP_RMDIR          2
147 #define FSFILT_OP_RENAME         3
148 #define FSFILT_OP_CREATE         4
149 #define FSFILT_OP_MKDIR          5
150 #define FSFILT_OP_SYMLINK        6
151 #define FSFILT_OP_MKNOD          7
152 #define FSFILT_OP_SETATTR        8
153 #define FSFILT_OP_LINK           9
154 #define FSFILT_OP_CANCEL_UNLINK 10
155 #define FSFILT_OP_JOIN          11
156 #define FSFILT_OP_NOOP          15
157
158 #define fsfilt_check_slow(obd, start, timeout, msg)                     \
159 do {                                                                    \
160         if (time_before(jiffies, start + 15 * HZ))                      \
161                 break;                                                  \
162         else if (time_before(jiffies, start + 30 * HZ))                 \
163                 CDEBUG(D_VFSTRACE, "%s: slow %s %lus\n", obd->obd_name, \
164                        msg, (jiffies-start) / HZ);                      \
165         else if (time_before(jiffies, start + timeout / 2 * HZ))        \
166                 CWARN("%s: slow %s %lus\n", obd->obd_name, msg,         \
167                       (jiffies - start) / HZ);                          \
168         else                                                            \
169                 CERROR("%s: slow %s %lus\n", obd->obd_name, msg,        \
170                        (jiffies - start) / HZ);                         \
171 } while (0)
172
173 static inline void *fsfilt_start_log(struct obd_device *obd,
174                                      struct inode *inode, int op,
175                                      struct obd_trans_info *oti, int logs)
176 {
177         unsigned long now = jiffies;
178         void *parent_handle = oti ? oti->oti_handle : NULL;
179         void *handle;
180
181         if (obd->obd_fail)
182                 return ERR_PTR(-EROFS);
183
184         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
185         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
186
187         if (oti != NULL) {
188                 if (parent_handle == NULL) {
189                         oti->oti_handle = handle;
190                 } else if (handle != parent_handle) {
191                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
192                                parent_handle, handle, oti);
193                         LBUG();
194                 }
195         }
196         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
197         return handle;
198 }
199
200 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
201                                  int op, struct obd_trans_info *oti)
202 {
203         return fsfilt_start_log(obd, inode, op, oti, 0);
204 }
205
206 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
207                                          struct fsfilt_objinfo *fso,
208                                          int niocount, struct niobuf_local *nb,
209                                          struct obd_trans_info *oti, int logs)
210 {
211         unsigned long now = jiffies;
212         void *parent_handle = oti ? oti->oti_handle : NULL;
213         void *handle;
214
215         if (obd->obd_fail)
216                 return ERR_PTR(-EROFS);
217
218         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
219                                               parent_handle, logs);
220         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
221
222         if (oti != NULL) {
223                 if (parent_handle == NULL) {
224                         oti->oti_handle = handle;
225                 } else if (handle != parent_handle) {
226                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
227                                parent_handle, handle, oti);
228                         LBUG();
229                 }
230         }
231         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
232
233         return handle;
234 }
235
236 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
237                                      struct fsfilt_objinfo *fso, int niocount,
238                                      struct niobuf_local *nb,
239                                      struct obd_trans_info *oti)
240 {
241         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
242 }
243
244 static inline int fsfilt_extend(struct obd_device *obd, struct inode *inode,
245                                 unsigned int nblocks, void *handle)
246 {
247         unsigned long now = jiffies;
248         int rc = obd->obd_fsops->fs_extend(inode, nblocks, handle);
249         CDEBUG(D_INFO, "extending handle %p with %u blocks\n", handle, nblocks);
250
251         fsfilt_check_slow(obd, now, obd_timeout, "journal extend");
252
253         return rc;
254 }
255
256 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
257                                 void *handle, int force_sync)
258 {
259         unsigned long now = jiffies;
260         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
261         CDEBUG(D_INFO, "committing handle %p\n", handle);
262
263         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
264
265         return rc;
266 }
267
268 static inline int fsfilt_commit_async(struct obd_device *obd,
269                                       struct inode *inode, void *handle,
270                                       void **wait_handle)
271 {
272         unsigned long now = jiffies;
273         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
274
275         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
276         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
277
278         return rc;
279 }
280
281 static inline int fsfilt_commit_wait(struct obd_device *obd,
282                                      struct inode *inode, void *handle)
283 {
284         unsigned long now = jiffies;
285         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
286         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
287         fsfilt_check_slow(obd, now, obd_timeout, "journal start");
288         return rc;
289 }
290
291 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
292                                  void *handle, struct iattr *iattr,int do_trunc)
293 {
294         unsigned long now = jiffies;
295         int rc;
296         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
297         fsfilt_check_slow(obd, now, obd_timeout, "setattr");
298         return rc;
299 }
300
301 static inline int fsfilt_iocontrol(struct obd_device *obd, struct inode *inode,
302                                    struct file *file, unsigned int cmd,
303                                    unsigned long arg)
304 {
305         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
306 }
307
308 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
309                                 void *handle, void *md, int size,
310                                 const char *name)
311 {
312         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
313 }
314
315 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
316                                 void *md, int size, const char *name)
317 {
318         return obd->obd_fsops->fs_get_md(inode, md, size, name);
319 }
320
321 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
322                                   struct inode *inode, void *bio)
323 {
324         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
325
326         if (rw == OBD_BRW_READ)
327                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
328         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
329 }
330
331 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
332                                       struct file *file, char *buf,
333                                       size_t count, loff_t *offset)
334 {
335         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
336 }
337
338 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
339                                         void *handle, fsfilt_cb_t cb_func,
340                                         void *cb_data)
341 {
342         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
343                                                  handle, cb_func, cb_data);
344 }
345
346 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
347 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
348                                 __u64 max_age)
349 {
350         int rc = 0;
351
352         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
353                 obd->obd_osfs_age, max_age);
354         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
355                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
356                 if (rc == 0) /* N.B. statfs can't really fail */
357                         obd->obd_osfs_age = cfs_time_current_64();
358         } else {
359                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
360         }
361
362         return rc;
363 }
364
365 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
366 {
367         return obd->obd_fsops->fs_sync(sb);
368 }
369
370 static inline int fsfilt_quotacheck(struct obd_device *obd,
371                                     struct super_block *sb,
372                                     struct obd_quotactl *oqctl)
373 {
374         if (obd->obd_fsops->fs_quotacheck)
375                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
376         return -ENOTSUPP;
377 }
378
379 static inline int fsfilt_quotactl(struct obd_device *obd,
380                                   struct super_block *sb,
381                                   struct obd_quotactl *oqctl)
382 {
383         if (obd->obd_fsops->fs_quotactl)
384                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
385         return -ENOTSUPP;
386 }
387
388 static inline int fsfilt_quotainfo(struct obd_device *obd,
389                                    struct lustre_quota_info *lqi,
390                                    int type, int cmd)
391 {
392         if (obd->obd_fsops->fs_quotainfo)
393                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
394         return -ENOTSUPP;
395 }
396
397 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
398                               struct inode *inode, int type, 
399                               struct list_head *list)
400 {
401         if (obd->obd_fsops->fs_qids)
402                 return obd->obd_fsops->fs_qids(file, inode, type, list);
403         return -ENOTSUPP;
404 }
405
406 static inline int fsfilt_dquot(struct obd_device *obd,
407                                struct lustre_dquot *dquot, int cmd)
408 {
409         if (obd->obd_fsops->fs_dquot)
410                 return obd->obd_fsops->fs_dquot(dquot, cmd);
411         return -ENOTSUPP;
412 }
413
414 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
415                                          struct inode *inode,
416                                          struct page **page, int pages,
417                                          unsigned long *blocks, int *created,
418                                          int create, struct semaphore *sem)
419 {
420         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
421                                                   created, create, sem);
422 }
423
424 static inline int fs_prep_san_write(struct obd_device *obd, struct inode *inode,
425                                     long *blocks, int nblocks, loff_t newsize)
426 {
427         return obd->obd_fsops->fs_prep_san_write(inode, blocks,
428                                                  nblocks, newsize);
429 }
430
431 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
432                                      void *buf, loff_t size, loff_t *offs)
433 {
434         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
435 }
436
437 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
438                                       void *buf, loff_t size, loff_t *offs,
439                                       int force_sync)
440 {
441         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
442 }
443
444 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
445 {
446         if (obd->obd_fsops->fs_setup)
447                 return obd->obd_fsops->fs_setup(fs);
448         return 0;
449 }
450
451 #endif /* __KERNEL__ */
452
453 #endif