Whamcloud - gitweb
1)reorganize the smfs hook ops to make smfs walk a list of hooks ops in hook macro
[fs/lustre-release.git] / lustre / include / linux / lustre_smfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *   smfs data structures.
22  *   See also lustre_idl.h for wire formats of requests.
23  *
24  */
25
26 #ifndef __LUSTRE_SMFS_H
27 #define __LUSTRE_SMFS_H
28
29 struct snap_inode_info {
30         int sn_flags;           /* the flags indicated inode type */
31         int sn_gen;             /*the inode generation*/
32 };
33 struct smfs_inode_info {
34         struct inode *smi_inode;
35         __u32  smi_flags;
36         struct snap_inode_info sm_sninfo;
37 };
38
39 struct journal_operations {
40         void *(*tr_start)(struct inode *, int op);
41         void (*tr_commit)(void *handle);
42 };
43
44 struct sm_operations {
45         /* operations on the file store */
46         struct super_operations sm_sb_ops;
47         struct inode_operations sm_dir_iops;
48         struct inode_operations sm_file_iops;
49         struct inode_operations sm_sym_iops;
50         struct file_operations sm_dir_fops;
51         struct file_operations sm_file_fops;
52         struct file_operations sm_sym_fops;
53         struct dentry_operations sm_dentry_ops;
54         struct journal_operations sm_journal_ops;
55 };
56
57 /*smfs rec*/
58 typedef int (*smfs_pack_rec_func)(char *buffer, struct dentry *dentry,
59                                   struct inode *dir, void *data1,
60                                   void *data2, int op);
61 typedef enum {
62         PACK_NORMAL = 0,
63         PACK_OST = 1,
64         PACK_MDS = 2,
65         PACK_MAX = 3,
66 } pack_func_t;
67
68 struct mds_kml_pack_info {
69         int mpi_bufcount;
70         int mpi_size[4];
71         int mpi_total_size;
72 };
73 typedef int (*smfs_hook_func)(struct inode *inode, struct dentry *dentry,
74                              void *data1, void *data2, int op, void *handle);
75 struct smfs_hook_ops {
76         struct list_head smh_list;
77         char *           smh_name;
78         smfs_hook_func   smh_post_op;
79         smfs_hook_func   smh_pre_op;
80 };
81 struct smfs_super_info {
82         struct super_block       *smsi_sb;
83         struct vfsmount          *smsi_mnt;         /* mount the cache kern */
84         struct fsfilt_operations *sm_cache_fsfilt;  /* fsfilt operations */
85         struct fsfilt_operations *sm_fsfilt;        /* fsfilt operations */
86         struct sm_operations     *sm_ops;           /* cache ops */
87         struct lvfs_run_ctxt     *smsi_ctxt;
88         struct llog_ctxt         *smsi_rec_log;     /* smfs kml llog */
89         struct dentry            *smsi_logs_dir;
90         struct dentry            *smsi_objects_dir;
91         struct dentry            *smsi_delete_dir;  /* for delete inode dir */
92         char                     *smsi_cache_ftype; /* cache file system type */
93         char                     *smsi_ftype;       /* file system type */
94         struct obd_export        *smsi_exp;         /* file system obd exp */
95         struct snap_info         *smsi_snap_info;   /* snap table cow */
96         smfs_pack_rec_func       smsi_pack_rec[PACK_MAX]; /* sm_pack_rec type ops */
97         __u32                    smsi_flags;        /* flags */
98         __u32                    smsi_ops_check;
99         struct list_head         smsi_hook_list;
100 };
101
102
103 #define SMFS_FILE_TYPE         "smfs"
104 #define SMFS_FILE_MAGIC        0x19760218
105
106 struct smfs_file_info {
107         struct file        *c_file;
108         int                 magic;
109 };
110
111 struct smfs_proc_args {
112         struct super_block *sr_sb;
113         int                 sr_count;
114         int                 sr_flags;
115         void               *sr_data;
116 };
117 struct fs_extent{
118         __u32   e_block;        /* first logical block extent covers */
119         __u32   e_start;        /* first physical block extents lives */
120         __u32   e_num;          /* number of blocks covered by extent */
121 };
122
123 #define I2SMI(inode)  ((struct smfs_inode_info *) (&(inode->u.generic_ip)))
124 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
125 #define S2SMI(sb)   ((struct smfs_super_info *) (&(sb->u.generic_sbp)))
126 #define S2CSB(sb)   (((struct smfs_super_info *) (&(sb->u.generic_sbp)))->smsi_sb)
127 #else
128 #define S2SMI(sb)   ((struct smfs_super_info *) (sb->s_fs_info))
129 #define S2CSB(sb)   (((struct smfs_super_info *) (sb->s_fs_info))->smsi_sb)
130 #endif
131
132 #define I2CI(inode) (((struct smfs_inode_info*) (&(inode->u.generic_ip)))->smi_inode)
133
134 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
135 #define I2CSB(inode) ((struct smfs_super_info *) (&(inode->i_sb->u.generic_sbp)))
136 #else
137 #define I2CSB(inode) ((struct smfs_super_info *) (inode->i_sb->s_fs_info))
138 #endif
139
140 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
141 #define I2FOPS(inode) (((struct smfs_super_info *) \
142                         (&(inode->i_sb->u.generic_sbp)))->sm_cache_fsfilt)
143 #else
144 #define I2FOPS(inode) (((struct smfs_super_info *) \
145                         (inode->i_sb->s_fs_info))->sm_cache_fsfilt)
146 #endif
147
148 #define I2SNAPI(inode) (&(I2SMI(inode)->sm_sninfo))
149 #define I2SNAPCOPS(inode) ((S2SMI(inode->i_sb))->smsi_snap_info->snap_cache_fsfilt) 
150 #define I2SNAPOPS(inode) ((S2SMI(inode->i_sb))->smsi_snap_info->snap_fsfilt) 
151
152 #define S2SNAPI(sb) (S2SMI(sb)->smsi_snap_info)
153 #define F2SMFI(file) ((struct smfs_file_info *)((file->private_data)))
154 #define F2CF(file) (((struct smfs_file_info *) ((file->private_data)))->c_file)
155 #define SIZE2BLKS(size, inode) ((size + (I2CI(inode)->i_blksize)) >> (I2CI(inode)->i_blkbits))
156 #define OFF2BLKS(off, inode) (off >> (I2CI(inode)->i_blkbits))
157
158 #define SM_DO_REC               0x1
159 #define SM_INIT_REC             0x2
160 #define SM_CACHE_HOOK           0x4
161 #define SM_OVER_WRITE           0x8
162 #define SM_DIRTY_WRITE          0x10
163 #define SM_DO_COW               0x20
164 #define SM_DO_COWED             0x40
165
166 #define SMFS_DO_REC(smfs_info) (smfs_info->smsi_flags & SM_DO_REC)
167 #define SMFS_SET_REC(smfs_info) (smfs_info->smsi_flags |= SM_DO_REC)
168 #define SMFS_CLEAN_REC(smfs_info) (smfs_info->smsi_flags &= ~SM_DO_REC)
169
170 #define SMFS_INIT_REC(smfs_info) (smfs_info->smsi_flags & SM_INIT_REC)
171 #define SMFS_SET_INIT_REC(smfs_info) (smfs_info->smsi_flags |= SM_INIT_REC)
172 #define SMFS_CLEAN_INIT_REC(smfs_info) (smfs_info->smsi_flags &= ~SM_INIT_REC)
173
174 #define SMFS_SET_INODE_REC(inode) (I2SMI(inode)->smi_flags |= SM_DO_REC)
175 #define SMFS_DO_INODE_REC(inode) (I2SMI(inode)->smi_flags & SM_DO_REC)
176 #define SMFS_CLEAN_INODE_REC(inode) (I2SMI(inode)->smi_flags &= ~SM_DO_REC)
177
178 #define SMFS_CACHE_HOOK(smfs_info) (smfs_info->smsi_flags & SM_CACHE_HOOK)
179 #define SMFS_SET_CACHE_HOOK(smfs_info) (smfs_info->smsi_flags |= SM_CACHE_HOOK)
180 #define SMFS_CLEAN_CACHE_HOOK(smfs_info) (smfs_info->smsi_flags &= ~SM_CACHE_HOOK)
181
182 #define SMFS_INODE_CACHE_HOOK(inode) (I2SMI(inode)->smi_flags & SM_CACHE_HOOK)
183 #define SMFS_SET_INODE_CACHE_HOOK(inode) (I2SMI(inode)->smi_flags |= SM_CACHE_HOOK)
184 #define SMFS_CLEAN_INODE_CACHE_HOOK(inode) (I2SMI(inode)->smi_flags &= ~SM_CACHE_HOOK)
185
186 #define SMFS_INODE_OVER_WRITE(inode) (I2SMI(inode)->smi_flags & SM_OVER_WRITE)
187 #define SMFS_SET_INODE_OVER_WRITE(inode) (I2SMI(inode)->smi_flags |= SM_OVER_WRITE)
188 #define SMFS_CLEAN_INODE_OVER_WRITE(inode) (I2SMI(inode)->smi_flags &= ~SM_OVER_WRITE)
189
190 #define SMFS_INODE_DIRTY_WRITE(inode) (I2SMI(inode)->smi_flags & SM_DIRTY_WRITE)
191 #define SMFS_SET_INODE_DIRTY_WRITE(inode) (I2SMI(inode)->smi_flags |= SM_DIRTY_WRITE)
192 #define SMFS_CLEAN_INODE_DIRTY_WRITE(inode) (I2SMI(inode)->smi_flags &= ~SM_DIRTY_WRITE)
193
194 #define SMFS_DO_COW(smfs_info) (smfs_info->smsi_flags & SM_DO_COW)
195 #define SMFS_SET_COW(smfs_info) (smfs_info->smsi_flags |= SM_DO_COW)
196 #define SMFS_CLEAN_COW(smfs_info) (smfs_info->smsi_flags &= ~SM_DO_COW)
197
198 #define SMFS_SET_INODE_COW(inode) (I2SMI(inode)->smi_flags |= SM_DO_COW)
199 #define SMFS_DO_INODE_COW(inode) (I2SMI(inode)->smi_flags & SM_DO_COW)
200 #define SMFS_CLEAN_INODE_COW(inode) (I2SMI(inode)->smi_flags &= ~SM_DO_COW)
201
202 #define SMFS_SET_INODE_COWED(inode) (I2SMI(inode)->smi_flags |= SM_DO_COWED)
203 #define SMFS_DO_INODE_COWED(inode) (I2SMI(inode)->smi_flags & SM_DO_COWED)
204 #define SMFS_CLEAN_INODE_COWED(inode) (I2SMI(inode)->smi_flags &= ~SM_DO_COWED)
205
206
207 #define LVFS_SMFS_BACK_ATTR "lvfs_back_attr"
208
209
210 #define REC_COUNT_BIT       0
211 #define REC_COUNT_MASK      0x01 /*0001*/
212 #define REC_OP_BIT          1
213 #define REC_OP_MASK         0x06 /*0110*/
214 #define REC_WRITE_KML_BIT   3
215 #define REC_WRITE_KML_MASK  0x08 /*1000*/
216 #define REC_DEC_LINK_BIT    4
217 #define REC_DEC_LINK_MASK   0x10 /*10000* different with unlink*/
218 #define REC_GET_OID_BIT     5
219 #define REC_GET_OID_MASK    0x20 /*100000*/
220
221 #define REC_PACK_TYPE_BIT   6
222 #define REC_PACK_TYPE_MASK  0x1C0 /*111000000*/
223
224 #define SET_REC_COUNT_FLAGS(flag, count_flag) \
225                 (flag |= count_flag << REC_COUNT_BIT)
226 #define GET_REC_COUNT_FLAGS(flag) \
227                 ((flag & REC_COUNT_MASK) >> REC_COUNT_BIT)
228
229 #define SET_REC_OP_FLAGS(flag, op_flag) \
230                 (flag |= op_flag << REC_OP_BIT)
231 #define GET_REC_OP_FLAGS(flag) \
232                 ((flag & REC_OP_MASK) >> REC_OP_BIT)
233
234 #define SET_REC_WRITE_KML_FLAGS(flag, op_flag) \
235                 (flag |= op_flag << REC_OP_BIT)
236 #define GET_REC_WRITE_KML_FLAGS(flag) \
237                 ((flag & REC_WRITE_KML_MASK) >> REC_WRITE_KML_BIT)
238
239 #define SET_REC_DEC_LINK_FLAGS(flag, op_flag) \
240                 (flag |= op_flag << REC_DEC_LINK_BIT)
241 #define GET_REC_DEC_LINK_FLAGS(flag) \
242                 ((flag & REC_DEC_LINK_MASK) >> REC_DEC_LINK_BIT)
243
244 #define SET_REC_GET_ID_FLAGS(flag, op_flag) \
245                 (flag |= op_flag << REC_GET_OID_BIT)
246 #define GET_REC_GET_OID_FLAGS(flag) \
247                 ((flag & REC_GET_OID_MASK) >> REC_GET_OID_BIT)
248
249 #define SET_REC_PACK_TYPE_INDEX(flag, op_flag) \
250                 (flag |= op_flag << REC_PACK_TYPE_BIT)
251 #define GET_REC_PACK_TYPE_INDEX(flag) \
252                 ((flag & REC_PACK_TYPE_MASK) >> REC_PACK_TYPE_BIT)
253
254 #define SMFS_REC_ALL             0x1
255 #define SMFS_REC_BY_COUNT        0x0
256
257 #define SMFS_REINT_REC           0x1
258 #define SMFS_UNDO_REC            0x2
259
260 #define SMFS_WRITE_KML           0x1
261 #define SMFS_DEC_LINK            0x1
262 #define SMFS_GET_OID             0x1
263
264 #define SMFS_DO_REINT_REC(flag) \
265          (GET_REC_OP_FLAGS(flag) == SMFS_REINT_REC)
266 #define SMFS_DO_UNDO_REC(flag) \
267          (GET_REC_OP_FLAGS(flag) == SMFS_UNDO_REC)
268 #define SMFS_DO_REC_ALL(flag) \
269         (GET_REC_COUNT_FLAGS(flag) == SMFS_REC_ALL)
270 #define SMFS_DO_REC_BY_COUNT(flag) \
271         (GET_REC_COUNT_FLAGS(flag) == SMFS_REC_BY_COUNT)
272 #define SMFS_DO_WRITE_KML(flag) \
273         (GET_REC_WRITE_KML_FLAGS(flag) == SMFS_WRITE_KML)
274 #define SMFS_DO_DEC_LINK(flag) \
275         (GET_REC_DEC_LINK_FLAGS(flag) == SMFS_DEC_LINK)
276
277 #define SMFS_DO_GET_OID(flag) \
278         (GET_REC_GET_OID_FLAGS(flag) == SMFS_GET_OID)
279
280 /*DIRTY flags of write ops*/
281 #define REINT_EXTENTS_FLAGS         "replay_flags"
282 #define SMFS_DIRTY_WRITE        0x01
283 #define SMFS_OVER_WRITE         0x02
284
285
286 static inline void duplicate_inode(struct inode *dst_inode,
287                                    struct inode *src_inode)
288 {
289         dst_inode->i_mode = src_inode->i_mode;
290         dst_inode->i_uid = src_inode->i_uid;
291         dst_inode->i_gid = src_inode->i_gid;
292         dst_inode->i_nlink = src_inode->i_nlink;
293         dst_inode->i_size = src_inode->i_size;
294         dst_inode->i_atime = src_inode->i_atime;
295         dst_inode->i_ctime = src_inode->i_ctime;
296         dst_inode->i_mtime = src_inode->i_mtime;
297         dst_inode->i_blksize = src_inode->i_blksize;
298         dst_inode->i_version = src_inode->i_version;
299         dst_inode->i_state = src_inode->i_state;
300         dst_inode->i_generation = src_inode->i_generation;
301
302         /* This is to make creating special files working. */
303         dst_inode->i_rdev = src_inode->i_rdev;
304 }
305
306 static inline void post_smfs_inode(struct inode *inode,
307                                    struct inode *cache_inode)
308 {
309         if (inode && cache_inode) {
310                 duplicate_inode(inode, cache_inode);
311                 /*Here we must release the cache_inode,
312                  *Otherwise we will have no chance to
313                  *do it
314                  */
315                 cache_inode->i_state &=~I_LOCK;
316                 inode->i_blocks = cache_inode->i_blocks;
317         }
318 }
319
320 static inline void pre_smfs_inode(struct inode *inode,
321                                   struct inode *cache_inode)
322 {
323         if (inode && cache_inode)
324                 duplicate_inode(cache_inode, inode);
325 }
326
327 /* instantiate a file handle to the cache file */
328 static inline void duplicate_file(struct file *dst_file, struct file *src_file)
329 {
330         if (dst_file && src_file) {
331                 dst_file->f_pos = src_file->f_pos;
332                 dst_file->f_mode = src_file->f_mode;
333                 dst_file->f_flags = src_file->f_flags;
334                 dst_file->f_owner  = src_file->f_owner;
335                 dst_file->f_vfsmnt = src_file->f_vfsmnt;
336
337         #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
338                 dst_file->f_reada = src_file->f_reada;
339                 dst_file->f_ramax = src_file->f_ramax;
340                 dst_file->f_raend = src_file->f_raend;
341                 dst_file->f_ralen = src_file->f_ralen;
342                 dst_file->f_rawin = src_file->f_rawin;
343         #else
344                 dst_file->f_ra = src_file->f_ra;
345         #endif
346         }
347 }
348
349 static inline void duplicate_sb(struct super_block *dst_sb,
350                                 struct super_block *src_sb)
351 {
352         dst_sb->s_blocksize = src_sb->s_blocksize;
353         dst_sb->s_magic = src_sb->s_magic;
354         dst_sb->s_blocksize_bits = src_sb->s_blocksize_bits;
355         dst_sb->s_maxbytes = src_sb->s_maxbytes;
356         dst_sb->s_flags = src_sb->s_flags;
357 }
358
359 static inline void d_unalloc(struct dentry *dentry)
360 {
361 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
362         list_del(&dentry->d_hash);
363         INIT_LIST_HEAD(&dentry->d_hash);
364 #else
365         hlist_del_init(&dentry->d_hash);
366 #endif
367         dput(dentry); /* this will free the dentry memory */
368 }
369
370 static inline struct dentry *pre_smfs_dentry(struct dentry *parent_dentry,
371                                              struct inode *cache_inode,
372                                              struct dentry *dentry)
373 {
374         struct dentry *cache_dentry = NULL;
375
376         cache_dentry = d_alloc(parent_dentry, &dentry->d_name);
377         if (!cache_dentry)
378                 RETURN(NULL);
379         if (!parent_dentry)
380                 cache_dentry->d_parent = cache_dentry;
381         if (cache_inode)
382                 d_add(cache_dentry, cache_inode);
383         RETURN(cache_dentry);
384 }
385
386 static inline void post_smfs_dentry(struct dentry *cache_dentry)
387 {
388         if (!cache_dentry)
389                 return;
390         if (cache_dentry->d_inode)
391                 igrab(cache_dentry->d_inode);
392         d_unalloc(cache_dentry);
393 }
394
395 static inline int lookup_by_path(char *path, int flags, struct nameidata *nd)
396 {
397         struct dentry *dentry = NULL;
398         int rc = 0;
399
400 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
401         if (path_init(path, flags, nd)) {
402 #else
403         if (path_lookup(path, flags, nd)) {
404 #endif
405                 rc = path_walk(path, nd);
406                 if (rc)
407                         RETURN(rc);
408         } else {
409                 RETURN(-EINVAL);
410         }
411
412         dentry = nd->dentry;
413
414         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
415                 path_release(nd);
416                 RETURN(-ENODEV);
417         }
418         RETURN(rc);
419 }
420
421 /*FIXME there should be more conditions in this check*/
422 static inline int smfs_do_rec(struct inode *inode)
423 {
424         struct super_block *sb = inode->i_sb;
425         struct smfs_super_info *smfs_info = S2SMI(sb);
426
427         if (SMFS_DO_REC(smfs_info) && SMFS_INIT_REC(smfs_info) &&
428             SMFS_DO_INODE_REC(inode))
429                 return 1;
430         return 0;
431 }
432
433 static inline int smfs_cache_hook(struct inode *inode)
434 {
435         struct smfs_super_info  *smfs_info = I2CSB(inode);
436
437         if (SMFS_CACHE_HOOK(smfs_info) && SMFS_INIT_REC(smfs_info) &&
438             SMFS_INODE_CACHE_HOOK(inode))
439                 return 1;
440         else
441                 return 0;
442 }
443
444 static inline int smfs_do_cow(struct inode *inode)
445 {
446         struct super_block *sb = inode->i_sb;
447         struct smfs_super_info *smfs_info = S2SMI(sb);
448
449         if (SMFS_DO_COW(smfs_info) && SMFS_DO_INODE_COW(inode))
450                 return 1;
451         return 0;
452 }
453
454 /* XXX BUG 3188 -- must return to one set of opcodes */
455 #define SMFS_TRANS_OP(inode, op)                \
456 {                                               \
457         if (smfs_do_rec(inode))                 \
458                 op = op | 0x10;                 \
459         if (smfs_cache_hook(inode))             \
460                 op = op | 0x20;                 \
461 }
462
463 extern int smfs_start_rec(struct super_block *sb, struct vfsmount *mnt);
464 extern int smfs_stop_rec(struct super_block *sb);
465 extern int smfs_write_extents(struct inode *dir, struct dentry *dentry,
466                               unsigned long from, unsigned long num);
467 extern int smfs_rec_setattr(struct inode *dir, struct dentry *dentry,
468                             struct iattr *attr);
469 extern int smfs_rec_precreate(struct dentry *dentry, int *num, struct obdo *oa);
470 extern int smfs_rec_md(struct inode *inode, void * lmm, int lmm_size);
471 extern int smfs_rec_unpack(struct smfs_proc_args *args, char *record,
472                            char **pbuf, int *opcode);
473         
474
475 extern int smfs_post_setup(struct super_block *sb, struct vfsmount *mnt);
476 extern int smfs_post_cleanup(struct super_block *sb);
477 #endif /* _LUSTRE_SMFS_H */