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