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