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