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