Whamcloud - gitweb
f91f83d8bfe5bae71b3e8d0f8aec0752c8f4aa34
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/linux/lustre_fsfilt.h
37  *
38  * Filesystem interface helper.
39  */
40
41 #ifndef _LINUX_LUSTRE_FSFILT_H
42 #define _LINUX_LUSTRE_FSFILT_H
43
44 #ifndef _LUSTRE_FSFILT_H
45 #error Do not #include this file directly. #include <lustre_fsfilt.h> instead
46 #endif
47
48 #ifdef __KERNEL__
49
50 #include <obd.h>
51 #include <obd_class.h>
52
53 typedef void (*fsfilt_cb_t)(struct obd_device *obd, __u64 last_rcvd,
54                             void *data, int error);
55
56 struct fsfilt_objinfo {
57         struct dentry *fso_dentry;
58         int fso_bufcnt;
59 };
60
61 struct lustre_dquot;
62 struct fsfilt_operations {
63         cfs_list_t fs_list;
64         cfs_module_t *fs_owner;
65         char   *fs_type;
66         char   *(* fs_getlabel)(struct super_block *sb);
67         int     (* fs_setlabel)(struct super_block *sb, char *label);
68         char   *(* fs_uuid)(struct super_block *sb);
69         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
70                              int logs);
71         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
72                                  int niocount, struct niobuf_local *nb,
73                                  void *desc_private, int logs);
74         int     (* fs_extend)(struct inode *inode, unsigned nblocks, void *h);
75         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
76         int     (* fs_commit_async)(struct inode *inode, void *handle,
77                                         void **wait_handle);
78         int     (* fs_commit_wait)(struct inode *inode, void *handle);
79         int     (* fs_setattr)(struct dentry *dentry, void *handle,
80                                struct iattr *iattr, int do_trunc);
81         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
82                                  unsigned int cmd, unsigned long arg);
83         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
84                               int size, const char *name);
85         int     (* fs_get_md)(struct inode *inode, void *md, int size,
86                               const char *name);
87         /*
88          * this method is needed to make IO operation fsfilt nature depend.
89          *
90          * This operation maybe synchronous or asynchronous.
91          *
92          * Return convention: positive number of bytes written (synchronously)
93          * on success. Negative errno value on failure. Zero if asynchronous
94          * IO was submitted successfully.
95          *
96          */
97         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
98         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
99                                 loff_t *offset);
100         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
101                                       void *handle, fsfilt_cb_t cb_func,
102                                       void *cb_data);
103         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
104         int     (* fs_sync)(struct super_block *sb);
105         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
106                                        int pages, unsigned long *blocks,
107                                        int *created, int create,
108                                        cfs_semaphore_t *sem);
109         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
110                                     int force_sync);
111         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
112         int     (* fs_setup)(struct super_block *sb);
113         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
114         int     (* fs_quotacheck)(struct super_block *sb,
115                                   struct obd_quotactl *oqctl);
116         __u64   (* fs_get_version) (struct inode *inode);
117         __u64   (* fs_set_version) (struct inode *inode, __u64 new_version);
118         int     (* fs_quotactl)(struct super_block *sb,
119                                 struct obd_quotactl *oqctl);
120         int     (* fs_quotainfo)(struct lustre_quota_info *lqi, int type,
121                                  int cmd);
122         int     (* fs_qids)(struct file *file, struct inode *inode, int type,
123                             cfs_list_t *list);
124         int     (* fs_get_mblk)(struct super_block *sb, int *count,
125                                 struct inode *inode, int frags);
126         int     (* fs_dquot)(struct lustre_dquot *dquot, int cmd);
127         lvfs_sbdev_type (* fs_journal_sbdev)(struct super_block *sb);
128 };
129
130 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
131 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
132 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
133 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
134
135 static inline char *fsfilt_get_label(struct obd_device *obd,
136                                      struct super_block *sb)
137 {
138         if (obd->obd_fsops->fs_getlabel == NULL)
139                 return NULL;
140         if (obd->obd_fsops->fs_getlabel(sb)[0] == '\0')
141                 return NULL;
142
143         return obd->obd_fsops->fs_getlabel(sb);
144 }
145
146 static inline int fsfilt_set_label(struct obd_device *obd,
147                                    struct super_block *sb, char *label)
148 {
149         if (obd->obd_fsops->fs_setlabel == NULL)
150                 return -ENOSYS;
151         return (obd->obd_fsops->fs_setlabel(sb, label));
152 }
153
154 static inline __u8 *fsfilt_uuid(struct obd_device *obd, struct super_block *sb)
155 {
156         if (obd->obd_fsops->fs_uuid == NULL)
157                 return NULL;
158
159         return obd->obd_fsops->fs_uuid(sb);
160 }
161
162 static inline lvfs_sbdev_type fsfilt_journal_sbdev(struct obd_device *obd,
163                                                    struct super_block *sb)
164 {
165         if (obd && obd->obd_fsops && obd->obd_fsops->fs_journal_sbdev)
166                 return obd->obd_fsops->fs_journal_sbdev(sb);
167         return (lvfs_sbdev_type)0;
168 }
169
170 #define FSFILT_OP_UNLINK                1
171 #define FSFILT_OP_RMDIR                 2
172 #define FSFILT_OP_RENAME                3
173 #define FSFILT_OP_CREATE                4
174 #define FSFILT_OP_MKDIR                 5
175 #define FSFILT_OP_SYMLINK               6
176 #define FSFILT_OP_MKNOD                 7
177 #define FSFILT_OP_SETATTR               8
178 #define FSFILT_OP_LINK                  9
179 #define FSFILT_OP_CANCEL_UNLINK         10
180 #define FSFILT_OP_NOOP                  15
181 #define FSFILT_OP_UNLINK_PARTIAL_CHILD  21
182 #define FSFILT_OP_UNLINK_PARTIAL_PARENT 22
183 #define FSFILT_OP_CREATE_PARTIAL_CHILD  23
184
185 #define __fsfilt_check_slow(obd, start, msg)                              \
186 do {                                                                      \
187         if (cfs_time_before(jiffies, start + 15 * CFS_HZ))                \
188                 break;                                                    \
189         else if (cfs_time_before(jiffies, start + 30 * CFS_HZ))           \
190                 CDEBUG(D_VFSTRACE, "%s: slow %s %lus\n", obd->obd_name,   \
191                        msg, (jiffies-start) / CFS_HZ);                    \
192         else if (cfs_time_before(jiffies, start + DISK_TIMEOUT * CFS_HZ)) \
193                 CWARN("%s: slow %s %lus\n", obd->obd_name, msg,           \
194                       (jiffies - start) / CFS_HZ);                        \
195         else                                                              \
196                 CERROR("%s: slow %s %lus\n", obd->obd_name, msg,          \
197                        (jiffies - start) / CFS_HZ);                       \
198 } while (0)
199
200 #define fsfilt_check_slow(obd, start, msg)              \
201 do {                                                    \
202         __fsfilt_check_slow(obd, start, msg);           \
203         start = jiffies;                                \
204 } while (0)
205
206 static inline void *fsfilt_start_log(struct obd_device *obd,
207                                      struct inode *inode, int op,
208                                      struct obd_trans_info *oti, int logs)
209 {
210         unsigned long now = jiffies;
211         void *parent_handle = oti ? oti->oti_handle : NULL;
212         void *handle;
213
214         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
215         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
216
217         if (oti != NULL) {
218                 if (parent_handle == NULL) {
219                         oti->oti_handle = handle;
220                 } else if (handle != parent_handle) {
221                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
222                                parent_handle, handle, oti);
223                         LBUG();
224                 }
225         }
226         fsfilt_check_slow(obd, now, "journal start");
227         return handle;
228 }
229
230 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
231                                  int op, struct obd_trans_info *oti)
232 {
233         return fsfilt_start_log(obd, inode, op, oti, 0);
234 }
235
236 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
237                                          struct fsfilt_objinfo *fso,
238                                          int niocount, struct niobuf_local *nb,
239                                          struct obd_trans_info *oti, int logs)
240 {
241         unsigned long now = jiffies;
242         void *parent_handle = oti ? oti->oti_handle : NULL;
243         void *handle;
244
245         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
246                                               parent_handle, logs);
247         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
248
249         if (oti != NULL) {
250                 if (parent_handle == NULL) {
251                         oti->oti_handle = handle;
252                 } else if (handle != parent_handle) {
253                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
254                                parent_handle, handle, oti);
255                         LBUG();
256                 }
257         }
258         fsfilt_check_slow(obd, now, "journal start");
259
260         return handle;
261 }
262
263 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
264                                      struct fsfilt_objinfo *fso, int niocount,
265                                      struct niobuf_local *nb,
266                                      struct obd_trans_info *oti)
267 {
268         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
269 }
270
271 static inline int fsfilt_extend(struct obd_device *obd, struct inode *inode,
272                                 unsigned int nblocks, void *handle)
273 {
274         unsigned long now = jiffies;
275         int rc = obd->obd_fsops->fs_extend(inode, nblocks, handle);
276         CDEBUG(D_INFO, "extending handle %p with %u blocks\n", handle, nblocks);
277
278         fsfilt_check_slow(obd, now, "journal extend");
279
280         return rc;
281 }
282
283 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
284                                 void *handle, int force_sync)
285 {
286         unsigned long now = jiffies;
287         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
288         CDEBUG(D_INFO, "committing handle %p\n", handle);
289
290         fsfilt_check_slow(obd, now, "journal start");
291
292         return rc;
293 }
294
295 static inline int fsfilt_commit_async(struct obd_device *obd,
296                                       struct inode *inode, void *handle,
297                                       void **wait_handle)
298 {
299         unsigned long now = jiffies;
300         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
301
302         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
303         fsfilt_check_slow(obd, now, "journal start");
304
305         return rc;
306 }
307
308 static inline int fsfilt_commit_wait(struct obd_device *obd,
309                                      struct inode *inode, void *handle)
310 {
311         unsigned long now = jiffies;
312         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
313         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
314         fsfilt_check_slow(obd, now, "journal start");
315         return rc;
316 }
317
318 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
319                                  void *handle, struct iattr *iattr,int do_trunc)
320 {
321         unsigned long now = jiffies;
322         int rc;
323         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
324         fsfilt_check_slow(obd, now, "setattr");
325         return rc;
326 }
327
328 static inline int fsfilt_iocontrol(struct obd_device *obd, struct dentry *dentry,
329                                    unsigned int cmd, unsigned long arg)
330 {
331         struct file *dummy_file = NULL;
332         int ret;
333
334         OBD_ALLOC_PTR(dummy_file);
335         if (!dummy_file)
336                 return(-ENOMEM);
337
338         dummy_file->f_dentry = dentry;
339         dummy_file->f_vfsmnt = obd->u.obt.obt_vfsmnt;
340
341         ret = obd->obd_fsops->fs_iocontrol(dentry->d_inode, dummy_file, cmd,
342                                            arg);
343
344         OBD_FREE_PTR(dummy_file);
345         return ret;
346 }
347
348 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
349                                 void *handle, void *md, int size,
350                                 const char *name)
351 {
352         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
353 }
354
355 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
356                                 void *md, int size, const char *name)
357 {
358         return obd->obd_fsops->fs_get_md(inode, md, size, name);
359 }
360
361 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
362                                   struct inode *inode, void *bio)
363 {
364         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
365
366         if (rw == OBD_BRW_READ)
367                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
368         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
369 }
370
371 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
372                                       struct file *file, char *buf,
373                                       size_t count, loff_t *offset)
374 {
375         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
376 }
377
378 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
379                                         void *handle, fsfilt_cb_t cb_func,
380                                         void *cb_data)
381 {
382         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
383                                                  handle, cb_func, cb_data);
384 }
385
386 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
387 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
388                                 __u64 max_age)
389 {
390         int rc = 0;
391
392         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
393                 obd->obd_osfs_age, max_age);
394         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
395                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
396                 if (rc == 0) /* N.B. statfs can't really fail */
397                         obd->obd_osfs_age = cfs_time_current_64();
398         } else {
399                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
400         }
401
402         return rc;
403 }
404
405 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
406 {
407         return obd->obd_fsops->fs_sync(sb);
408 }
409
410 static inline int fsfilt_quotacheck(struct obd_device *obd,
411                                     struct super_block *sb,
412                                     struct obd_quotactl *oqctl)
413 {
414         if (obd->obd_fsops->fs_quotacheck)
415                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
416         return -ENOTSUPP;
417 }
418
419 static inline int fsfilt_quotactl(struct obd_device *obd,
420                                   struct super_block *sb,
421                                   struct obd_quotactl *oqctl)
422 {
423         if (obd->obd_fsops->fs_quotactl)
424                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
425         return -ENOTSUPP;
426 }
427
428 static inline int fsfilt_quotainfo(struct obd_device *obd,
429                                    struct lustre_quota_info *lqi,
430                                    int type, int cmd)
431 {
432         if (obd->obd_fsops->fs_quotainfo)
433                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
434         return -ENOTSUPP;
435 }
436
437 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
438                               struct inode *inode, int type,
439                               cfs_list_t *list)
440 {
441         if (obd->obd_fsops->fs_qids)
442                 return obd->obd_fsops->fs_qids(file, inode, type, list);
443         return -ENOTSUPP;
444 }
445
446 static inline int fsfilt_dquot(struct obd_device *obd,
447                                struct lustre_dquot *dquot, int cmd)
448 {
449         if (obd->obd_fsops->fs_dquot)
450                 return obd->obd_fsops->fs_dquot(dquot, cmd);
451         return -ENOTSUPP;
452 }
453
454 static inline int fsfilt_get_mblk(struct obd_device *obd,
455                                   struct super_block *sb, int *count,
456                                   struct inode *inode, int frags)
457 {
458         if (obd->obd_fsops->fs_get_mblk)
459                 return obd->obd_fsops->fs_get_mblk(sb, count, inode, frags);
460         return -ENOTSUPP;
461 }
462
463 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
464                                          struct inode *inode,
465                                          struct page **page, int pages,
466                                          unsigned long *blocks, int *created,
467                                          int create, cfs_semaphore_t *sem)
468 {
469         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
470                                                   created, create, sem);
471 }
472
473 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
474                                      void *buf, loff_t size, loff_t *offs)
475 {
476         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
477 }
478
479 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
480                                       void *buf, loff_t size, loff_t *offs,
481                                       int force_sync)
482 {
483         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
484 }
485
486 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
487 {
488         if (obd->obd_fsops->fs_setup)
489                 return obd->obd_fsops->fs_setup(fs);
490         return 0;
491 }
492
493 static inline __u64 fsfilt_set_version(struct obd_device *obd,
494                                       struct inode *inode, __u64 new_version)
495 {
496         if (obd->obd_fsops->fs_set_version)
497                 return obd->obd_fsops->fs_set_version(inode, new_version);
498         return -EOPNOTSUPP;
499 }
500
501 static inline __u64 fsfilt_get_version(struct obd_device *obd,
502                                        struct inode *inode)
503 {
504         if (obd->obd_fsops->fs_get_version)
505                 return obd->obd_fsops->fs_get_version(inode);
506         return -EOPNOTSUPP;
507 }
508
509 #endif /* __KERNEL__ */
510
511 #endif