Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[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  2008 Sun Microsystems, Inc. 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 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
62
63 struct lustre_dquot;
64 struct fsfilt_operations {
65         struct list_head fs_list;
66         struct module *fs_owner;
67         char   *fs_type;
68         char   *(* fs_getlabel)(struct super_block *sb);
69         int     (* fs_setlabel)(struct super_block *sb, char *label);
70         char   *(* fs_uuid)(struct super_block *sb);
71         void   *(* fs_start)(struct inode *inode, int op, void *desc_private,
72                              int logs);
73         void   *(* fs_brw_start)(int objcount, struct fsfilt_objinfo *fso,
74                                  int niocount, struct niobuf_local *nb,
75                                  void *desc_private, int logs);
76         int     (* fs_extend)(struct inode *inode, unsigned nblocks, void *h);
77         int     (* fs_commit)(struct inode *inode, void *handle,int force_sync);
78         int     (* fs_commit_async)(struct inode *inode, void *handle,
79                                         void **wait_handle);
80         int     (* fs_commit_wait)(struct inode *inode, void *handle);
81         int     (* fs_setattr)(struct dentry *dentry, void *handle,
82                                struct iattr *iattr, int do_trunc);
83         int     (* fs_iocontrol)(struct inode *inode, struct file *file,
84                                  unsigned int cmd, unsigned long arg);
85         int     (* fs_set_md)(struct inode *inode, void *handle, void *md,
86                               int size, const char *name);
87         int     (* fs_get_md)(struct inode *inode, void *md, int size,
88                               const char *name);
89         /*
90          * this method is needed to make IO operation fsfilt nature depend.
91          *
92          * This operation maybe synchronous or asynchronous.
93          *
94          * Return convention: positive number of bytes written (synchronously)
95          * on success. Negative errno value on failure. Zero if asynchronous
96          * IO was submitted successfully.
97          *
98          */
99         int     (* fs_send_bio)(int rw, struct inode *inode,struct kiobuf *bio);
100         ssize_t (* fs_readpage)(struct file *file, char *buf, size_t count,
101                                 loff_t *offset);
102         int     (* fs_add_journal_cb)(struct obd_device *obd, __u64 last_rcvd,
103                                       void *handle, fsfilt_cb_t cb_func,
104                                       void *cb_data);
105         int     (* fs_statfs)(struct super_block *sb, struct obd_statfs *osfs);
106         int     (* fs_sync)(struct super_block *sb);
107         int     (* fs_map_inode_pages)(struct inode *inode, struct page **page,
108                                        int pages, unsigned long *blocks,
109                                        int *created, int create,
110                                        struct semaphore *sem);
111         int     (* fs_write_record)(struct file *, void *, int size, loff_t *,
112                                     int force_sync);
113         int     (* fs_read_record)(struct file *, void *, int size, loff_t *);
114         int     (* fs_setup)(struct super_block *sb);
115         int     (* fs_get_op_len)(int, struct fsfilt_objinfo *, int);
116         int     (* fs_quotacheck)(struct super_block *sb,
117                                   struct obd_quotactl *oqctl);
118         __u64   (* fs_get_version) (struct inode *inode);
119         __u64   (* fs_set_version) (struct inode *inode, __u64 new_version);
120         int     (* fs_quotactl)(struct super_block *sb,
121                                 struct obd_quotactl *oqctl);
122         int     (* fs_quotainfo)(struct lustre_quota_info *lqi, int type,
123                                  int cmd);
124         int     (* fs_qids)(struct file *file, struct inode *inode, int type,
125                             struct list_head *list);
126         int     (* fs_dquot)(struct lustre_dquot *dquot, int cmd);
127         int     (* fs_get_mblk)(struct super_block *sb, int *count,
128                                 struct inode *inode, int frags);
129         lvfs_sbdev_type (* fs_journal_sbdev)(struct super_block *sb);
130 };
131
132 extern int fsfilt_register_ops(struct fsfilt_operations *fs_ops);
133 extern void fsfilt_unregister_ops(struct fsfilt_operations *fs_ops);
134 extern struct fsfilt_operations *fsfilt_get_ops(const char *type);
135 extern void fsfilt_put_ops(struct fsfilt_operations *fs_ops);
136
137 static inline char *fsfilt_get_label(struct obd_device *obd,
138                                      struct super_block *sb)
139 {
140         if (obd->obd_fsops->fs_getlabel == NULL)
141                 return NULL;
142         if (obd->obd_fsops->fs_getlabel(sb)[0] == '\0')
143                 return NULL;
144
145         return obd->obd_fsops->fs_getlabel(sb);
146 }
147
148 static inline int fsfilt_set_label(struct obd_device *obd,
149                                    struct super_block *sb, char *label)
150 {
151         if (obd->obd_fsops->fs_setlabel == NULL)
152                 return -ENOSYS;
153         return (obd->obd_fsops->fs_setlabel(sb, label));
154 }
155
156 static inline __u8 *fsfilt_uuid(struct obd_device *obd, struct super_block *sb)
157 {
158         if (obd->obd_fsops->fs_uuid == NULL)
159                 return NULL;
160
161         return obd->obd_fsops->fs_uuid(sb);
162 }
163
164 static inline lvfs_sbdev_type fsfilt_journal_sbdev(struct obd_device *obd,
165                                                    struct super_block *sb)
166 {
167         if (obd && obd->obd_fsops && obd->obd_fsops->fs_journal_sbdev)
168                 return obd->obd_fsops->fs_journal_sbdev(sb);
169         return (lvfs_sbdev_type)0;
170 }
171
172 #define FSFILT_OP_UNLINK         1
173 #define FSFILT_OP_RMDIR          2
174 #define FSFILT_OP_RENAME         3
175 #define FSFILT_OP_CREATE         4
176 #define FSFILT_OP_MKDIR          5
177 #define FSFILT_OP_SYMLINK        6
178 #define FSFILT_OP_MKNOD          7
179 #define FSFILT_OP_SETATTR        8
180 #define FSFILT_OP_LINK           9
181 #define FSFILT_OP_CANCEL_UNLINK 10
182 #define FSFILT_OP_JOIN          11
183 #define FSFILT_OP_NOOP          15
184
185 #define __fsfilt_check_slow(obd, start, msg)                            \
186 do {                                                                    \
187         if (time_before(jiffies, start + 15 * HZ))                      \
188                 break;                                                  \
189         else if (time_before(jiffies, start + 30 * HZ))                 \
190                 CDEBUG(D_VFSTRACE, "%s: slow %s %lus\n", obd->obd_name, \
191                        msg, (jiffies-start) / HZ);                      \
192         else if (time_before(jiffies, start + DISK_TIMEOUT * HZ))       \
193                 CWARN("%s: slow %s %lus\n", obd->obd_name, msg,         \
194                       (jiffies - start) / HZ);                          \
195         else                                                            \
196                 CERROR("%s: slow %s %lus\n", obd->obd_name, msg,        \
197                        (jiffies - start) / 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         if (obd->obd_fail)
215                 return ERR_PTR(-EROFS);
216
217         handle = obd->obd_fsops->fs_start(inode, op, parent_handle, logs);
218         CDEBUG(D_INFO, "started handle %p (%p)\n", handle, parent_handle);
219
220         if (oti != NULL) {
221                 if (parent_handle == NULL) {
222                         oti->oti_handle = handle;
223                 } else if (handle != parent_handle) {
224                         CERROR("mismatch: parent %p, handle %p, oti %p\n",
225                                parent_handle, handle, oti);
226                         LBUG();
227                 }
228         }
229         fsfilt_check_slow(obd, now, "journal start");
230         return handle;
231 }
232
233 static inline void *fsfilt_start(struct obd_device *obd, struct inode *inode,
234                                  int op, struct obd_trans_info *oti)
235 {
236         return fsfilt_start_log(obd, inode, op, oti, 0);
237 }
238
239 static inline void *fsfilt_brw_start_log(struct obd_device *obd, int objcount,
240                                          struct fsfilt_objinfo *fso,
241                                          int niocount, struct niobuf_local *nb,
242                                          struct obd_trans_info *oti, int logs)
243 {
244         unsigned long now = jiffies;
245         void *parent_handle = oti ? oti->oti_handle : NULL;
246         void *handle;
247
248         if (obd->obd_fail)
249                 return ERR_PTR(-EROFS);
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 inode *inode,
335                                    struct file *file, unsigned int cmd,
336                                    unsigned long arg)
337 {
338         return obd->obd_fsops->fs_iocontrol(inode, file, cmd, arg);
339 }
340
341 static inline int fsfilt_set_md(struct obd_device *obd, struct inode *inode,
342                                 void *handle, void *md, int size,
343                                 const char *name)
344 {
345         return obd->obd_fsops->fs_set_md(inode, handle, md, size, name);
346 }
347
348 static inline int fsfilt_get_md(struct obd_device *obd, struct inode *inode,
349                                 void *md, int size, const char *name)
350 {
351         return obd->obd_fsops->fs_get_md(inode, md, size, name);
352 }
353
354 static inline int fsfilt_send_bio(int rw, struct obd_device *obd,
355                                   struct inode *inode, void *bio)
356 {
357         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
358
359         if (rw == OBD_BRW_READ)
360                 return obd->obd_fsops->fs_send_bio(READ, inode, bio);
361         return obd->obd_fsops->fs_send_bio(WRITE, inode, bio);
362 }
363
364 static inline ssize_t fsfilt_readpage(struct obd_device *obd,
365                                       struct file *file, char *buf,
366                                       size_t count, loff_t *offset)
367 {
368         return obd->obd_fsops->fs_readpage(file, buf, count, offset);
369 }
370
371 static inline int fsfilt_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
372                                         void *handle, fsfilt_cb_t cb_func,
373                                         void *cb_data)
374 {
375         return obd->obd_fsops->fs_add_journal_cb(obd, last_rcvd,
376                                                  handle, cb_func, cb_data);
377 }
378
379 /* very similar to obd_statfs(), but caller already holds obd_osfs_lock */
380 static inline int fsfilt_statfs(struct obd_device *obd, struct super_block *sb,
381                                 __u64 max_age)
382 {
383         int rc = 0;
384
385         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
386                 obd->obd_osfs_age, max_age);
387         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
388                 rc = obd->obd_fsops->fs_statfs(sb, &obd->obd_osfs);
389                 if (rc == 0) /* N.B. statfs can't really fail */
390                         obd->obd_osfs_age = cfs_time_current_64();
391         } else {
392                 CDEBUG(D_SUPER, "using cached obd_statfs data\n");
393         }
394
395         return rc;
396 }
397
398 static inline int fsfilt_sync(struct obd_device *obd, struct super_block *sb)
399 {
400         return obd->obd_fsops->fs_sync(sb);
401 }
402
403 static inline int fsfilt_quotacheck(struct obd_device *obd,
404                                     struct super_block *sb,
405                                     struct obd_quotactl *oqctl)
406 {
407         if (obd->obd_fsops->fs_quotacheck)
408                 return obd->obd_fsops->fs_quotacheck(sb, oqctl);
409         return -ENOTSUPP;
410 }
411
412 static inline int fsfilt_quotactl(struct obd_device *obd,
413                                   struct super_block *sb,
414                                   struct obd_quotactl *oqctl)
415 {
416         if (obd->obd_fsops->fs_quotactl)
417                 return obd->obd_fsops->fs_quotactl(sb, oqctl);
418         return -ENOTSUPP;
419 }
420
421 static inline int fsfilt_quotainfo(struct obd_device *obd,
422                                    struct lustre_quota_info *lqi,
423                                    int type, int cmd)
424 {
425         if (obd->obd_fsops->fs_quotainfo)
426                 return obd->obd_fsops->fs_quotainfo(lqi, type, cmd);
427         return -ENOTSUPP;
428 }
429
430 static inline int fsfilt_qids(struct obd_device *obd, struct file *file,
431                               struct inode *inode, int type,
432                               struct list_head *list)
433 {
434         if (obd->obd_fsops->fs_qids)
435                 return obd->obd_fsops->fs_qids(file, inode, type, list);
436         return -ENOTSUPP;
437 }
438
439 static inline int fsfilt_dquot(struct obd_device *obd,
440                                struct lustre_dquot *dquot, int cmd)
441 {
442         if (obd->obd_fsops->fs_dquot)
443                 return obd->obd_fsops->fs_dquot(dquot, cmd);
444         return -ENOTSUPP;
445 }
446
447 static inline int fsfilt_get_mblk(struct obd_device *obd,
448                                   struct super_block *sb, int *count,
449                                   struct inode *inode, int frags)
450 {
451         if (obd->obd_fsops->fs_get_mblk)
452                 return obd->obd_fsops->fs_get_mblk(sb, count, inode, frags);
453         return -ENOTSUPP;
454 }
455
456 static inline int fsfilt_map_inode_pages(struct obd_device *obd,
457                                          struct inode *inode,
458                                          struct page **page, int pages,
459                                          unsigned long *blocks, int *created,
460                                          int create, struct semaphore *sem)
461 {
462         return obd->obd_fsops->fs_map_inode_pages(inode, page, pages, blocks,
463                                                   created, create, sem);
464 }
465
466 static inline int fsfilt_read_record(struct obd_device *obd, struct file *file,
467                                      void *buf, loff_t size, loff_t *offs)
468 {
469         return obd->obd_fsops->fs_read_record(file, buf, size, offs);
470 }
471
472 static inline int fsfilt_write_record(struct obd_device *obd, struct file *file,
473                                       void *buf, loff_t size, loff_t *offs,
474                                       int force_sync)
475 {
476         return obd->obd_fsops->fs_write_record(file, buf, size,offs,force_sync);
477 }
478
479 static inline int fsfilt_setup(struct obd_device *obd, struct super_block *fs)
480 {
481         if (obd->obd_fsops->fs_setup)
482                 return obd->obd_fsops->fs_setup(fs);
483         return 0;
484 }
485
486 static inline __u64 fsfilt_set_version(struct obd_device *obd,
487                                       struct inode *inode, __u64 new_version)
488 {
489         if (obd->obd_fsops->fs_set_version)
490                 return obd->obd_fsops->fs_set_version(inode, new_version);
491         return -EOPNOTSUPP;
492 }
493
494 static inline __u64 fsfilt_get_version(struct obd_device *obd,
495                                        struct inode *inode)
496 {
497         if (obd->obd_fsops->fs_set_version)
498                 return obd->obd_fsops->fs_get_version(inode);
499         return -EOPNOTSUPP;
500 }
501
502 #endif /* __KERNEL__ */
503
504 #endif