Whamcloud - gitweb
LU-334 llite: Add LPROC_LL_OSC_{READ,WRITE}.
[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 fsfilt_fid {
62         __u32 ino;
63         __u32 gen;
64 };
65
66 struct lustre_dquot;
67 struct fsfilt_operations {
68         cfs_list_t fs_list;
69         cfs_module_t *fs_owner;
70         char   *fs_type;
71         char   *(* fs_getlabel)(struct super_block *sb);
72         int     (* fs_setlabel)(struct super_block *sb, char *label);
73         char   *(* fs_uuid)(struct super_block *sb);
74         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
75                              int logs);
76         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
77                                  int niocount, struct niobuf_local *nb,
78                                  void *desc_private, int logs);
79         int     (* fs_extend)(struct inode *inode, unsigned nblocks, void *h);
80         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
81         int     (* fs_commit_async)(struct inode *inode, void *handle,
82                                         void **wait_handle);
83         int     (* fs_commit_wait)(struct inode *inode, void *handle);
84         int     (* fs_setattr)(struct dentry *dentry, void *handle,
85                                struct iattr *iattr, int do_trunc);
86         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
87                                  unsigned int cmd, unsigned long arg);
88         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
89                               int size, const char *name);
90         int     (* fs_get_md)(struct inode *inode, void *md, int size,
91                               const char *name);
92         /*
93          * this method is needed to make IO operation fsfilt nature depend.
94          *
95          * This operation maybe synchronous or asynchronous.
96          *
97          * Return convention: positive number of bytes written (synchronously)
98          * on success. Negative errno value on failure. Zero if asynchronous
99          * IO was submitted successfully.
100          *
101          */
102         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
103         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
104                                 loff_t *offset);
105         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
106                                       void *handle, fsfilt_cb_t cb_func,
107                                       void *cb_data);
108         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
109         int     (* fs_sync)(struct super_block *sb);
110         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
111                                        int pages, unsigned long *blocks,
112                                        int *created, int create,
113                                        cfs_semaphore_t *sem);
114         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
115                                     int force_sync);
116         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
117         int     (* fs_setup)(struct super_block *sb);
118         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
119         int     (* fs_quotacheck)(struct super_block *sb,
120                                   struct obd_quotactl *oqctl);
121         __u64   (* fs_get_version) (struct inode *inode);
122         __u64   (* fs_set_version) (struct inode *inode, __u64 new_version);
123         int     (* fs_quotactl)(struct super_block *sb,
124                                 struct obd_quotactl *oqctl);
125         int     (* fs_quotainfo)(struct lustre_quota_info *lqi, int type,
126                                  int cmd);
127         int     (* fs_qids)(struct file *file, struct inode *inode, int type,
128                             cfs_list_t *list);
129         int     (* fs_get_mblk)(struct super_block *sb, int *count,
130                                 struct inode *inode, int frags);
131         int     (* fs_dquot)(struct lustre_dquot *dquot, int cmd);
132         lvfs_sbdev_type (* fs_journal_sbdev)(struct super_block *sb);
133         struct dentry  *(* fs_fid2dentry)(struct vfsmount *mnt,
134                                           struct fsfilt_fid *fid,
135                                           int ignore_gen);
136 };
137
138 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
139 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
140 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
141 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
142
143 static inline char *fsfilt_get_label(struct obd_device *obd,
144                                      struct super_block *sb)
145 {
146         if (obd->obd_fsops->fs_getlabel == NULL)
147                 return NULL;
148         if (obd->obd_fsops->fs_getlabel(sb)[0] == '\0')
149                 return NULL;
150
151         return obd->obd_fsops->fs_getlabel(sb);
152 }
153
154 static inline int fsfilt_set_label(struct obd_device *obd,
155                                    struct super_block *sb, char *label)
156 {
157         if (obd->obd_fsops->fs_setlabel == NULL)
158                 return -ENOSYS;
159         return (obd->obd_fsops->fs_setlabel(sb, label));
160 }
161
162 static inline __u8 *fsfilt_uuid(struct obd_device *obd, struct super_block *sb)
163 {
164         if (obd->obd_fsops->fs_uuid == NULL)
165                 return NULL;
166
167         return obd->obd_fsops->fs_uuid(sb);
168 }
169
170 static inline lvfs_sbdev_type fsfilt_journal_sbdev(struct obd_device *obd,
171                                                    struct super_block *sb)
172 {
173         if (obd && obd->obd_fsops && obd->obd_fsops->fs_journal_sbdev)
174                 return obd->obd_fsops->fs_journal_sbdev(sb);
175         return (lvfs_sbdev_type)0;
176 }
177
178 #define FSFILT_OP_UNLINK                1
179 #define FSFILT_OP_RMDIR                 2
180 #define FSFILT_OP_RENAME                3
181 #define FSFILT_OP_CREATE                4
182 #define FSFILT_OP_MKDIR                 5
183 #define FSFILT_OP_SYMLINK               6
184 #define FSFILT_OP_MKNOD                 7
185 #define FSFILT_OP_SETATTR               8
186 #define FSFILT_OP_LINK                  9
187 #define FSFILT_OP_CANCEL_UNLINK         10
188 #define FSFILT_OP_NOOP                  15
189 #define FSFILT_OP_UNLINK_PARTIAL_CHILD  21
190 #define FSFILT_OP_UNLINK_PARTIAL_PARENT 22
191 #define FSFILT_OP_CREATE_PARTIAL_CHILD  23
192
193 #define __fsfilt_check_slow(obd, start, msg)                              \
194 do {                                                                      \
195         if (cfs_time_before(jiffies, start + 15 * CFS_HZ))                \
196                 break;                                                    \
197         else if (cfs_time_before(jiffies, start + 30 * CFS_HZ))           \
198                 CDEBUG(D_VFSTRACE, "%s: slow %s %lus\n", obd->obd_name,   \
199                        msg, (jiffies-start) / CFS_HZ);                    \
200         else if (cfs_time_before(jiffies, start + DISK_TIMEOUT * CFS_HZ)) \
201                 CWARN("%s: slow %s %lus\n", obd->obd_name, msg,           \
202                       (jiffies - start) / CFS_HZ);                        \
203         else                                                              \
204                 CERROR("%s: slow %s %lus\n", obd->obd_name, msg,          \
205                        (jiffies - start) / CFS_HZ);                       \
206 } while (0)
207
208 #define fsfilt_check_slow(obd, start, msg)              \
209 do {                                                    \
210         __fsfilt_check_slow(obd, start, msg);           \
211         start = jiffies;                                \
212 } while (0)
213
214 static inline void *fsfilt_start_log(struct obd_device *obd,
215                                      struct inode *inode, int op,
216                                      struct obd_trans_info *oti, int logs)
217 {
218         unsigned long now = jiffies;
219         void *parent_handle = oti ? oti->oti_handle : NULL;
220         void *handle;
221
222         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
223         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
224
225         if (oti != NULL) {
226                 if (parent_handle == NULL) {
227                         oti->oti_handle = handle;
228                 } else if (handle != parent_handle) {
229                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
230                                parent_handle, handle, oti);
231                         LBUG();
232                 }
233         }
234         fsfilt_check_slow(obd, now, "journal start");
235         return handle;
236 }
237
238 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
239                                  int op, struct obd_trans_info *oti)
240 {
241         return fsfilt_start_log(obd, inode, op, oti, 0);
242 }
243
244 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
245                                          struct fsfilt_objinfo *fso,
246                                          int niocount, struct niobuf_local *nb,
247                                          struct obd_trans_info *oti, int logs)
248 {
249         unsigned long now = jiffies;
250         void *parent_handle = oti ? oti->oti_handle : NULL;
251         void *handle;
252
253         handle = obd->obd_fsops->fs_brw_start(objcount, fso, niocount, nb,
254                                               parent_handle, logs);
255         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
256
257         if (oti != NULL) {
258                 if (parent_handle == NULL) {
259                         oti->oti_handle = handle;
260                 } else if (handle != parent_handle) {
261                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
262                                parent_handle, handle, oti);
263                         LBUG();
264                 }
265         }
266         fsfilt_check_slow(obd, now, "journal start");
267
268         return handle;
269 }
270
271 static inline void *fsfilt_brw_start(struct obd_device *obd, int objcount,
272                                      struct fsfilt_objinfo *fso, int niocount,
273                                      struct niobuf_local *nb,
274                                      struct obd_trans_info *oti)
275 {
276         return fsfilt_brw_start_log(obd, objcount, fso, niocount, nb, oti, 0);
277 }
278
279 static inline int fsfilt_extend(struct obd_device *obd, struct inode *inode,
280                                 unsigned int nblocks, void *handle)
281 {
282         unsigned long now = jiffies;
283         int rc = obd->obd_fsops->fs_extend(inode, nblocks, handle);
284         CDEBUG(D_INFO, "extending handle %p with %u blocks\n", handle, nblocks);
285
286         fsfilt_check_slow(obd, now, "journal extend");
287
288         return rc;
289 }
290
291 static inline int fsfilt_commit(struct obd_device *obd, struct inode *inode,
292                                 void *handle, int force_sync)
293 {
294         unsigned long now = jiffies;
295         int rc = obd->obd_fsops->fs_commit(inode, handle, force_sync);
296         CDEBUG(D_INFO, "committing handle %p\n", handle);
297
298         fsfilt_check_slow(obd, now, "journal start");
299
300         return rc;
301 }
302
303 static inline int fsfilt_commit_async(struct obd_device *obd,
304                                       struct inode *inode, void *handle,
305                                       void **wait_handle)
306 {
307         unsigned long now = jiffies;
308         int rc = obd->obd_fsops->fs_commit_async(inode, handle, wait_handle);
309
310         CDEBUG(D_INFO, "committing handle %p (async)\n", *wait_handle);
311         fsfilt_check_slow(obd, now, "journal start");
312
313         return rc;
314 }
315
316 static inline int fsfilt_commit_wait(struct obd_device *obd,
317                                      struct inode *inode, void *handle)
318 {
319         unsigned long now = jiffies;
320         int rc = obd->obd_fsops->fs_commit_wait(inode, handle);
321         CDEBUG(D_INFO, "waiting for completion %p\n", handle);
322         fsfilt_check_slow(obd, now, "journal start");
323         return rc;
324 }
325
326 static inline int fsfilt_setattr(struct obd_device *obd, struct dentry *dentry,
327                                  void *handle, struct iattr *iattr,int do_trunc)
328 {
329         unsigned long now = jiffies;
330         int rc;
331         rc = obd->obd_fsops->fs_setattr(dentry, handle, iattr, do_trunc);
332         fsfilt_check_slow(obd, now, "setattr");
333         return rc;
334 }
335
336 static inline int fsfilt_iocontrol(struct obd_device *obd, struct dentry *dentry,
337                                    unsigned int cmd, unsigned long arg)
338 {
339         struct file *dummy_file = NULL;
340         int ret;
341
342         OBD_ALLOC_PTR(dummy_file);
343         if (!dummy_file)
344                 return(-ENOMEM);
345
346         dummy_file->f_dentry = dentry;
347         dummy_file->f_vfsmnt = obd->u.obt.obt_vfsmnt;
348
349         ret = obd->obd_fsops->fs_iocontrol(dentry->d_inode, dummy_file, cmd,
350                                            arg);
351
352         OBD_FREE_PTR(dummy_file);
353         return ret;
354 }
355
356 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
357                                 void *handle, void *md, int size,
358                                 const char *name)
359 {
360         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
361 }
362
363 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
364                                 void *md, int size, const char *name)
365 {
366         return obd->obd_fsops->fs_get_md(inode, md, size, name);
367 }
368
369 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
370                                   struct inode *inode, void *bio)
371 {
372         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
373
374         if (rw == OBD_BRW_READ)
375                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
376         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
377 }
378
379 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
380                                       struct file *file, char *buf,
381                                       size_t count, loff_t *offset)
382 {
383         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
384 }
385
386 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
387                                         void *handle, fsfilt_cb_t cb_func,
388                                         void *cb_data)
389 {
390         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
391                                                  handle, cb_func, cb_data);
392 }
393
394 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
395 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
396                                 __u64 max_age)
397 {
398         int rc = 0;
399
400         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
401                 obd->obd_osfs_age, max_age);
402         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
403                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
404                 if (rc == 0) /* N.B. statfs can't really fail */
405                         obd->obd_osfs_age = cfs_time_current_64();
406         } else {
407                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
408         }
409
410         return rc;
411 }
412
413 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
414 {
415         return obd->obd_fsops->fs_sync(sb);
416 }
417
418 static inline int fsfilt_quotacheck(struct obd_device *obd,
419                                     struct super_block *sb,
420                                     struct obd_quotactl *oqctl)
421 {
422         if (obd->obd_fsops->fs_quotacheck)
423                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
424         return -ENOTSUPP;
425 }
426
427 static inline int fsfilt_quotactl(struct obd_device *obd,
428                                   struct super_block *sb,
429                                   struct obd_quotactl *oqctl)
430 {
431         if (obd->obd_fsops->fs_quotactl)
432                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
433         return -ENOTSUPP;
434 }
435
436 static inline int fsfilt_quotainfo(struct obd_device *obd,
437                                    struct lustre_quota_info *lqi,
438                                    int type, int cmd)
439 {
440         if (obd->obd_fsops->fs_quotainfo)
441                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
442         return -ENOTSUPP;
443 }
444
445 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
446                               struct inode *inode, int type,
447                               cfs_list_t *list)
448 {
449         if (obd->obd_fsops->fs_qids)
450                 return obd->obd_fsops->fs_qids(file, inode, type, list);
451         return -ENOTSUPP;
452 }
453
454 static inline int fsfilt_dquot(struct obd_device *obd,
455                                struct lustre_dquot *dquot, int cmd)
456 {
457         if (obd->obd_fsops->fs_dquot)
458                 return obd->obd_fsops->fs_dquot(dquot, cmd);
459         return -ENOTSUPP;
460 }
461
462 static inline int fsfilt_get_mblk(struct obd_device *obd,
463                                   struct super_block *sb, int *count,
464                                   struct inode *inode, int frags)
465 {
466         if (obd->obd_fsops->fs_get_mblk)
467                 return obd->obd_fsops->fs_get_mblk(sb, count, inode, frags);
468         return -ENOTSUPP;
469 }
470
471 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
472                                          struct inode *inode,
473                                          struct page **page, int pages,
474                                          unsigned long *blocks, int *created,
475                                          int create, cfs_semaphore_t *sem)
476 {
477         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
478                                                   created, create, sem);
479 }
480
481 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
482                                      void *buf, loff_t size, loff_t *offs)
483 {
484         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
485 }
486
487 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
488                                       void *buf, loff_t size, loff_t *offs,
489                                       int force_sync)
490 {
491         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
492 }
493
494 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
495 {
496         if (obd->obd_fsops->fs_setup)
497                 return obd->obd_fsops->fs_setup(fs);
498         return 0;
499 }
500
501 static inline __u64 fsfilt_set_version(struct obd_device *obd,
502                                       struct inode *inode, __u64 new_version)
503 {
504         if (obd->obd_fsops->fs_set_version)
505                 return obd->obd_fsops->fs_set_version(inode, new_version);
506         return -EOPNOTSUPP;
507 }
508
509 static inline __u64 fsfilt_get_version(struct obd_device *obd,
510                                        struct inode *inode)
511 {
512         if (obd->obd_fsops->fs_get_version)
513                 return obd->obd_fsops->fs_get_version(inode);
514         return -EOPNOTSUPP;
515 }
516
517 static inline struct dentry *fsfilt_fid2dentry(struct obd_device *obd,
518                                                struct vfsmount *mnt,
519                                                struct fsfilt_fid *fid,
520                                                int ignore_gen)
521 {
522         if (obd->obd_fsops->fs_fid2dentry)
523                 return obd->obd_fsops->fs_fid2dentry(mnt, fid, ignore_gen);
524         return ERR_PTR(-EOPNOTSUPP);
525 }
526
527 #endif /* __KERNEL__ */
528
529 #endif