Whamcloud - gitweb
update snapfs 1. fix bug in write table_count in write snap_table 2. fixbug in repla...
[fs/lustre-release.git] / lustre / snapfs / snapfs_internal.h
1 #ifndef __LINUX_SNAPFS_H
2 #define __LINUX_SNAPFS_H
3 /* maximum number of snapshot tables we maintain in the kernel */
4 #define SNAP_MAX                32      
5 #define SNAP_MAX_TABLES         32      
6 #define SNAP_MAX_NAMELEN        64
7
8 /* ioctls for manipulating snapshots 40 - 60 */
9 #define IOC_SNAP_TYPE                   'f'
10 #define IOC_SNAP_MIN_NR                 41
11
12 #define IOC_SNAP_SETTABLE               _IOWR('f', 41, long)
13 #define IOC_SNAP_PRINTTABLE             _IOWR('f', 42, long)
14 #define IOC_SNAP_GETINDEXFROMNAME       _IOWR('f', 43, long)
15 #define IOC_SNAP_GET_NEXT_INO           _IOWR('f', 44, long)
16 #define IOC_SNAP_GET_INO_INFO           _IOWR('f', 45, long)
17
18 #define IOC_SNAP_ADD                    _IOWR('f', 46, long)
19 #define IOC_SNAP_DELETE                 _IOWR('f', 47, long)
20 #define IOC_SNAP_RESTORE                _IOWR('f', 48, long)
21 #define IOC_SNAP_DEBUG                  _IOWR('f', 49, long)
22 #define IOC_SNAP_DEVFAIL                _IOWR('f', 50, long)
23 #define IOC_SNAP_SHOW_DOTSNAP           _IOWR('f', 51, long)
24
25 #define IOC_SNAP_MAX_NR                 51 
26
27 struct snap {
28         time_t          time;
29         unsigned int    index;
30         unsigned int    gen;
31         unsigned int    flags;
32         char    name[SNAP_MAX_NAMELEN];
33 };
34
35
36 /*FIXME, use ioc_data temporary, will use obd_ioc_data later*/
37 struct ioc_data {
38         unsigned int ioc_inlen;
39         char         *ioc_inbuf;
40         char         ioc_bulk[0];
41 };
42
43 /* snap ioctl data for table fiddling */
44 struct ioc_snap_tbl_data {
45         int             no;             /* which table */
46         unsigned long   dev;
47         unsigned int    count;          /* how many snaps */
48         struct snap     snaps[0];       /* sorted times! */
49 };
50 struct option {
51         char *opt;
52         char *value;
53         struct list_head list;
54 };
55 /* we have just a single snapshot control device
56    it contains a list of all the snap_current info's
57 */
58 #define SNAPDEV_NAME "/dev/snapconf"
59 #define SNAP_PSDEV_MINOR 240
60 #define SNAP_PSDEV_MAJOR 10
61
62 #define SNAP_TABLE_OUTBUF_LEN   1020
63
64 #ifdef __KERNEL__
65
66 #if 0
67 #include <linux/lustre_lib.h>
68 #else
69 #include "snapfs_support.h" 
70 #endif
71 /* What we use to point to IDs in the obdmd data for snapshots.  If we use
72  * obd_id (8 bytes) instead of ino_t (4 bytes), we halve the number of
73  * available snapshot slots (14 in 56 bytes vs. 7 in 56 bytes until we
74  * increase the size of OBD_OBDMDSZ).
75  */
76 typedef ino_t   snap_id;
77
78
79 /* maximum number of snapshots per device 
80    must fit in "o_obdmd" area of struct obdo */
81 //#define OBD_OBDMDSZ  54
82 //#define SNAP_MAX ((OBD_OBDMDSZ - sizeof(uint32_t))/sizeof(snap_id))
83
84
85
86 /* if time is 0 this designates the "current" snapshot, i.e.
87    the head of the tree 
88 */
89
90 /* sysctl.c */
91 extern int init_snapfs_proc_sys(void);
92 extern void cleanup_spapfs_proc_sys(void);
93 extern int snap_print_entry;
94 extern int snap_debug_level;
95 extern int snap_inodes;
96 extern long snap_kmemory;
97 extern int snap_stack;
98
99 /* snap cache information: this morally equals the superblock of a
100  snap_current_fs.  However, that superblock is the one of the "cache"
101  device holding the inodes, hence we store this info in the hash of
102  mountpoints hanging of our control device. 
103 */
104 struct snap_cache {
105         struct list_head cache_chain;
106
107         kdev_t cache_dev;
108         struct super_block *cache_sb; /* the _real_ device */
109
110         struct list_head cache_clone_list;
111         int cache_snap_tableno;
112
113         struct filter_fs *cache_filter;
114
115         char cache_type;
116         char cache_show_dotsnap;
117 };
118
119 /* this is the snap_clone_info for the sb of snap_clone_fs */
120 struct snap_clone_info {
121         struct snap_cache *clone_cache;
122         struct list_head clone_list_entry;
123         int clone_index;
124 };
125
126 /* 
127  * it is important that things like inode, super and file operations
128  * for intermezzo are not defined statically.  If methods are NULL
129  * the VFS takes special action based on that.  Given that different
130  * cache types have NULL ops at different slots, we must install opeation 
131  * talbes for InterMezzo with NULL's in the same spot
132  */
133
134 struct filter_ops {
135         /* operations on the file store */
136         struct super_operations filter_sops;
137
138         struct inode_operations filter_dir_iops;
139         struct inode_operations filter_file_iops;
140         struct inode_operations filter_sym_iops;
141
142         struct file_operations filter_dir_fops;
143         struct file_operations filter_file_fops;
144         struct file_operations filter_sym_fops;
145
146         struct address_space_operations filter_file_aops;
147         struct dentry_operations filter_dentry_ops;
148 };
149
150
151 struct cache_ops {
152         /* operations on the file store */
153         struct super_operations *cache_sops;
154
155         struct inode_operations *cache_dir_iops;
156         struct inode_operations *cache_file_iops;
157         struct inode_operations *cache_sym_iops;
158
159         struct file_operations *cache_dir_fops;
160         struct file_operations *cache_file_fops;
161         struct file_operations *cache_sym_fops;
162
163         struct address_space_operations *cache_file_aops;
164         struct dentry_operations *cache_dentry_ops;
165 };
166
167
168 #define SNAP_OP_NOOP            0
169 #define SNAP_OP_CREATE          1
170 #define SNAP_OP_MKDIR           2
171 #define SNAP_OP_UNLINK          3
172 #define SNAP_OP_RMDIR           4
173 #define SNAP_OP_CLOSE           5
174 #define SNAP_OP_SYMLINK         6
175 #define SNAP_OP_RENAME          7
176 #define SNAP_OP_SETATTR         8
177 #define SNAP_OP_LINK            9
178 #define SNAP_OP_OPEN            10
179 #define SNAP_OP_MKNOD           11
180 #define SNAP_OP_WRITE           12
181 #define SNAP_OP_RELEASE         13
182
183 struct journal_ops {
184         void *(*trans_start)(struct inode *, int op);
185         void (*trans_commit)(void *handle);
186 };
187
188 struct snap_control_device {
189         struct list_head snap_dev_list;
190 };
191
192 #define D_MAXLEN 1024
193
194 #define SNAPSHOT_UNUSED_FLAG            (1 << 0)
195 #define SNAPSHOT_GOOD_FLAG              (1 << 1)
196 #define SNAPSHOT_DELETING_FLAG          (1 << 2)
197 #define SNAPSHOT_BAD_FLAG               (1 << 3)        
198
199 struct snap_disk {
200         __u64   time;
201         __u32   gen;
202         __u32   index;
203         __u32   flags;
204         char    name[SNAP_MAX_NAMELEN];
205 };
206 /* snap ioctl data for attach: current always in first slot of this array */
207 struct snap_obd_data {
208         int          snap_dev;  /* which device contains the data */
209         unsigned int snap_index;/* which snapshot is ours */
210         unsigned int snap_table;/* which table do we use */
211 };
212 #define DISK_SNAPTABLE_ATTR     "Snaptable"
213 #define DISK_SNAP_TABLE_MAGIC   0x1976
214 struct snap_disk_table {
215         unsigned int            magic;
216         unsigned int            count;
217         unsigned int            generation;
218         struct  snap_disk       snap_items[SNAP_MAX];
219 };
220
221 /*Snap Table*/
222 struct snap_table {
223         struct semaphore    tbl_sema;
224         spinlock_t          tbl_lock;
225         unsigned int        tbl_count; /* how many snapshots exist in this table*/
226         unsigned int        generation;
227         struct snap         snap_items[SNAP_MAX]; 
228 };
229
230 struct snap_iterdata {
231         kdev_t dev;     /* snap current device number */ 
232         int index;
233         int tableno;
234         time_t time;
235 };
236
237 struct snap_ioc_data {
238         kdev_t dev;
239         char name[SNAP_MAX_NAMELEN];
240 };
241
242 struct snap_ino_list_data{
243         kdev_t dev;
244         ino_t ino;
245 };
246 struct filter_inode_info {
247         int flags;              /* the flags indicated inode type */
248         int generation;         /*the inode generation*/
249 };
250 /* dotsnap.c */
251 extern int currentfs_is_under_dotsnap(struct dentry *de);
252
253 /* cache.c */
254 inline void snap_free_cache(struct snap_cache *cache);
255 struct snap_cache *snap_find_cache(kdev_t dev);
256 typedef int (*snap_cache_cb_t)(struct snap_cache*, void *in, unsigned long *out);
257 int snap_cache_process(snap_cache_cb_t cb, void* in, unsigned long* out);
258
259 /* snaptable.c */
260 extern struct snap_table snap_tables[SNAP_MAX_TABLES];
261 void snap_last(struct snap_cache *info, struct snap *snap);
262 int snap_index2slot(struct snap_table *snap_table, int snap_index);
263 int snap_needs_cow(struct inode *);
264 int snapfs_read_snaptable(struct snap_cache *cache, int tableno);
265 /* snap.c */
266 int snap_is_redirector(struct inode *inode);
267 struct inode *snap_redirect(struct inode *inode, struct super_block *clone_sb);
268 int snap_do_cow(struct inode *inode, ino_t parent_ino, int del);
269
270 int snap_iterate(struct super_block *sb,
271                 int (*repeat)(struct inode *inode, void *priv),
272                 struct inode **start, void *priv, int flag);
273
274 struct inode *snap_get_indirect(struct inode *pri, int *table, int slot);
275 int snap_destroy_indirect(struct inode *pri, int index, struct inode *next_ind);
276 int snap_restore_indirect(struct inode *pri, int index );
277 int snap_migrate_data(struct inode *dst, struct inode *src);
278 int snap_set_indirect(struct inode *pri, ino_t ind_ino, 
279                         int index, ino_t parent_ino);
280
281 /* inode.c */
282 extern struct super_operations currentfs_super_ops;
283 void cleanup_filter_info_cache(void);
284 int init_filter_info_cache(void);
285 extern void init_filter_data(struct inode *inode, int flag);
286 extern void set_filter_ops(struct snap_cache *cache, struct inode *inode);
287 /* dir.c */
288 extern struct inode_operations currentfs_dir_iops;
289 extern struct file_operations currentfs_dir_fops;
290 extern struct address_space_operations currentfs_file_aops;
291
292 /* file.c */
293 extern struct inode_operations currentfs_file_iops;
294 extern struct file_operations currentfs_file_fops;
295
296 /* symlink.c */
297 extern struct inode_operations currentfs_sym_iops;
298 extern struct file_operations currentfs_sym_fops;
299
300 extern struct dentry_operations currentfs_dentry_ops;
301
302 /* options.c */
303 extern int init_option(char *data);
304 extern void cleanup_option(void);
305 extern int get_opt(struct option **opt, char **pos);
306
307 #define FILTER_DID_SUPER_OPS    0x1
308 #define FILTER_DID_INODE_OPS    0x2
309 #define FILTER_DID_FILE_OPS     0x4
310 #define FILTER_DID_DENTRY_OPS   0x8
311 #define FILTER_DID_DEV_OPS      0x10
312 #define FILTER_DID_SYMLINK_OPS  0x20
313 #define FILTER_DID_DIR_OPS      0x40
314 #define FILTER_DID_SNAPSHOT_OPS 0x80
315 #define FILTER_DID_JOURNAL_OPS  0x100
316
317 struct filter_fs {
318         int o_flags;
319         struct filter_ops o_fops;
320         struct cache_ops  o_caops;
321         struct journal_ops *o_trops;
322         struct snapshot_operations *o_snapops;
323 };
324
325 #define FILTER_FS_TYPES 3
326 #define FILTER_FS_EXT2 0
327 #define FILTER_FS_EXT3 1
328 #define FILTER_FS_REISER 2
329 extern struct filter_fs filter_oppar[FILTER_FS_TYPES];
330 struct filter_fs *filter_get_filter_fs(const char *cache_type);
331 inline struct super_operations *filter_c2usops(struct filter_fs *cache);
332 inline struct inode_operations *filter_c2ufiops(struct filter_fs *cache);
333 inline struct inode_operations *filter_c2udiops(struct filter_fs *cache);
334 inline struct inode_operations *filter_c2usiops(struct filter_fs *cache);
335 inline struct super_operations *filter_c2csops(struct filter_fs *cache);
336 inline struct inode_operations *filter_c2cfiops(struct filter_fs *cache);
337 inline struct inode_operations *filter_c2cdiops(struct filter_fs *cache);
338 inline struct inode_operations *filter_c2csiops(struct filter_fs *cache);
339 inline struct file_operations *filter_c2udfops(struct filter_fs *cache);
340 inline struct file_operations *filter_c2cffops(struct filter_fs *cache);
341 inline struct file_operations *filter_c2cdfops(struct filter_fs *cache);
342 inline struct file_operations *filter_c2csfops(struct filter_fs *cache);
343 inline struct file_operations *filter_c2uffops(struct filter_fs *cache);
344 inline struct file_operations *filter_c2usfops(struct filter_fs *cache);
345 inline struct dentry_operations *filter_c2cdops(struct filter_fs *cache);
346 inline struct dentry_operations *filter_c2udops(struct filter_fs *cache);
347 inline struct address_space_operations *filter_c2cfaops(struct filter_fs *cache);
348 inline struct address_space_operations *filter_c2ufaops(struct filter_fs *cache);
349 /* for snapfs */
350 inline struct snapshot_operations *filter_c2csnapops(struct filter_fs *cache);
351
352 void filter_setup_file_ops(struct filter_fs        *cache, 
353                            struct inode            *inode,
354                            struct inode_operations *filter_iops,
355                            struct file_operations  *filter_fops,
356                            struct address_space_operations *filter_aops);
357
358 void filter_setup_dir_ops(struct filter_fs *cache, 
359                           struct inode     *inode,
360                           struct inode_operations *filter_iops, 
361                           struct file_operations *filter_fops);
362
363 void filter_setup_symlink_ops(struct filter_fs *cache, 
364                               struct inode *inode,
365                               struct inode_operations *filter_iops, 
366                               struct file_operations *filter_fops);
367
368 void filter_setup_dentry_ops(struct filter_fs *cache,
369                              struct dentry_operations *cache_dop, 
370                              struct dentry_operations *filter_dop);
371 void filter_setup_super_ops(struct filter_fs *cache, 
372                             struct super_operations *cache_sops, 
373                             struct super_operations *filter_sops);
374 /* for snapfs */
375 void filter_setup_snapshot_ops(struct filter_fs *cache, 
376                                struct snapshot_operations *cache_snapops);
377 void filter_setup_journal_ops(struct filter_fs *cache, 
378                               struct journal_ops *cache_journal_ops);
379
380 static inline void* snap_trans_start(struct snap_cache *cache, 
381                                      struct inode *inode, int op)
382 {
383         if( cache->cache_filter->o_trops )
384                 return cache->cache_filter->o_trops->trans_start(inode, op);
385         return NULL;
386 };
387 static inline void snap_trans_commit(struct snap_cache *cache, void *handle)
388 {
389         if( cache->cache_filter->o_trops )
390                 cache->cache_filter->o_trops->trans_commit(handle);
391 };
392
393 static inline void snapfs_cpy_attrs(struct inode *dst, struct inode *src)
394 {
395         dst->i_mtime = src->i_mtime;
396         dst->i_ctime = src->i_ctime;
397         dst->i_atime = src->i_atime;
398         dst->i_size = src->i_size;
399         dst->i_blksize = src->i_blksize;
400         dst->i_blocks = src->i_blocks;
401         dst->i_generation = src->i_generation;
402         dst->i_uid = src->i_uid;
403         dst->i_gid = src->i_gid;
404         dst->i_mode = src->i_mode;
405 }
406 #ifdef SNAP_DEBUG
407 extern unsigned int snap_debug_failcode;
408 #ifdef CONFIG_LOOP_DISCARD
409 #define BLKDEV_FAIL(dev,fail) loop_discard_io(dev,fail)
410 #else
411 #define BLKDEV_FAIL(dev,fail) set_device_ro(dev, 1)
412 #endif
413
414 static inline void snap_debug_device_fail(dev_t dev, unsigned short opcode, unsigned short pos)
415 {
416         unsigned int failcode = (opcode<<16) | pos;
417
418         if( failcode == snap_debug_failcode && !is_read_only(dev)){
419                 printk(KERN_EMERG "set block device %s into fail mode\n", bdevname(dev));
420                 BLKDEV_FAIL(dev, 1);
421         }
422 }
423 #else
424 #define snap_debug_device_fail(args...) do{}while(0)
425 #endif
426
427 extern int snap_debug_level;
428 extern int snap_print_entry;
429
430 #endif /*_KERNEL_*/
431 #endif /* __LINUX_SNAPFS_H */