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 / inode.c
1 /*
2  *  fs/snap/snap.c
3  *
4  *  A snap shot file system.
5  *
6  */
7
8 #define DEBUG_SUBSYSTEM S_SNAP
9
10 #include <linux/kmod.h>
11 #include <linux/init.h>
12 #include <linux/fs.h>
13 #include <linux/slab.h>
14 #include <linux/jbd.h>
15 #include <linux/ext3_fs.h>
16 #include <linux/string.h>
17 #include <linux/snap.h>
18 #include "snapfs_internal.h" 
19
20
21 extern int currentfs_remount(struct super_block * sb, int *flags, char *data);
22
23 /* XXX PJB: this is exactly what we need to put things under 
24    filters - we don't want the ext2 methods hardcoded, we want them
25    in the filter (in read_super) and then call those methods. 
26    See how InterMezzo gets the journal operations .
27 */
28  
29 extern void currentfs_dotsnap_read_inode(struct snap_cache *, struct inode *);
30
31 static kmem_cache_t *filter_info_cache = NULL;
32
33 void cleanup_filter_info_cache()
34 {
35         kmem_cache_destroy(filter_info_cache);
36 }
37
38 int init_filter_info_cache()
39 {
40         filter_info_cache = kmem_cache_create("snapfs_filter_info",
41                                                sizeof(struct filter_inode_info), 
42                                             0, 0, NULL, NULL);
43         if (!filter_info_cache) {
44                 CERROR("unable to create snap_inode info cache\n");
45                 return -ENOMEM;
46         }
47         return 0;
48 }
49
50
51 void init_filter_data(struct inode *inode, 
52                              int flag)
53 {
54         struct filter_inode_info *i;
55         struct snap_cache *cache;
56         struct snapshot_operations *snapops; 
57
58         if (inode->i_filterdata || inode->i_ino & 0xF0000000)
59                 return;
60         cache = snap_find_cache(inode->i_dev);
61         if (!cache) {
62                 CERROR("currentfs_read_inode: cannot find cache\n");
63                 make_bad_inode(inode);
64                 return;
65         }
66         snapops = filter_c2csnapops(cache->cache_filter);
67         
68         inode->i_filterdata = (struct filter_inode_info *) \
69                               kmem_cache_alloc(filter_info_cache, SLAB_KERNEL);
70         i = inode->i_filterdata;
71         i -> generation = snapops->get_generation(inode);
72         i -> flags      = flag;
73 }
74
75 void set_filter_ops(struct snap_cache *cache, struct inode *inode)
76 {
77         /* XXX now set the correct snap_{file,dir,sym}_iops */
78         if (S_ISDIR(inode->i_mode)) { 
79                 inode->i_op = filter_c2udiops(cache->cache_filter);
80                 inode->i_fop = filter_c2udfops(cache->cache_filter);
81         } else if (S_ISREG(inode->i_mode)) {
82                 if ( !filter_c2cfiops(cache->cache_filter) ) {
83                         filter_setup_file_ops(cache->cache_filter, inode, 
84                                               &currentfs_file_iops, 
85                                               &currentfs_file_fops, 
86                                               &currentfs_file_aops);
87                 }
88                 CDEBUG(D_INODE, "inode %lu, i_op at %p\n", 
89                        inode->i_ino, inode->i_op);
90                 inode->i_fop = filter_c2uffops(cache->cache_filter);
91                 inode->i_op = filter_c2ufiops(cache->cache_filter);
92                 if (inode->i_mapping)
93                         inode->i_mapping->a_ops = filter_c2ufaops(cache->cache_filter);
94
95         }
96         else if (S_ISLNK(inode->i_mode)) {
97                 if ( !filter_c2csiops(cache->cache_filter) ) {
98                         filter_setup_symlink_ops(cache->cache_filter, inode,
99                                 &currentfs_sym_iops, &currentfs_sym_fops);
100                 }
101                 inode->i_op = filter_c2usiops(cache->cache_filter);
102                 inode->i_fop = filter_c2usfops(cache->cache_filter);
103                 CDEBUG(D_INODE, "inode %lu, i_op at %p\n", 
104                        inode->i_ino, inode->i_op);
105         }
106 }
107
108 /* Superblock operations. */
109 static void currentfs_read_inode(struct inode *inode)
110 {
111         struct snap_cache *cache;
112         ENTRY;
113
114         if( !inode ) 
115                 return;
116
117         CDEBUG(D_INODE, "read_inode ino %lu\n", inode->i_ino);
118
119         cache = snap_find_cache(inode->i_dev);
120         if (!cache) {
121                 CERROR("currentfs_read_inode: cannot find cache\n");
122                 make_bad_inode(inode);
123                 return;
124         }
125
126         if (inode->i_ino & 0xF0000000) { 
127                 currentfs_dotsnap_read_inode(cache, inode);
128                 return;
129         }
130
131         if(filter_c2csops(cache->cache_filter))
132                 filter_c2csops(cache->cache_filter)->read_inode(inode);
133
134         set_filter_ops(cache, inode);
135         /*init filter_data struct 
136          * FIXME flag should be set future*/
137         init_filter_data(inode, 0); 
138         return; 
139 }
140
141 static void currentfs_put_super(struct super_block *sb)
142 {
143
144         struct snap_cache *cache;
145         ENTRY;
146
147         CDEBUG(D_SUPER, "sb %lx, sb->u.generic_sbp: %lx\n",
148                 (ulong) sb, (ulong) sb->u.generic_sbp);
149         cache = snap_find_cache(sb->s_dev);
150
151         if (!cache) 
152                 GOTO(exit, 0);  
153         
154         /* handle COMPAT_FEATUREs */
155 #ifdef CONFIG_SNAPFS_EXT2
156         else if( cache->cache_type == FILTER_FS_EXT2 ){
157                 if( !EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_SNAPFS) ){
158                         sb->u.ext2_sb.s_feature_compat &=
159                                 ~EXT2_FEATURE_COMPAT_BLOCKCOW;
160                         sb->u.ext2_sb.s_es->s_feature_compat &=
161                                 cpu_to_le32(~EXT2_FEATURE_COMPAT_BLOCKCOW);
162                 }
163         }
164 #endif
165 #ifdef CONFIG_SNAPFS_EXT3
166         else if( cache->cache_type == FILTER_FS_EXT3 ){
167                 if( !EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_SNAPFS) ){
168                         sb->u.ext3_sb.s_es->s_feature_compat &=
169                                 cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
170                 }
171         }
172 #endif
173         /*
174          * If there is a saved 'put_super' function for the underlying
175          * fs then call it.
176          */
177         if (cache->cache_filter->o_caops.cache_sops->put_super) { 
178                 cache->cache_filter->o_caops.cache_sops->put_super(sb);
179         }
180         
181         if (!list_empty(&cache->cache_clone_list)) {
182                 CWARN("snap_put_super: clones exist!\n");
183         }
184
185         list_del(&cache->cache_chain);
186         snap_free_cache(cache);
187
188         CDEBUG(D_SUPER, "sb %lx, sb->u.generic_sbp: %lx\n",
189                 (ulong) sb, (ulong) sb->u.generic_sbp);
190 exit:
191         EXIT;
192         return;
193 }
194 static void currentfs_clear_inode(struct inode *inode)
195 {
196         struct snap_cache *cache;
197         struct super_operations *sops;
198         ENTRY;                                                                                                                                                                                                                       
199         cache = snap_find_cache(inode->i_dev);
200         if (!cache) {
201                 CDEBUG(D_INODE, "inode has invalid dev\n");
202                 return;
203         }
204         
205         if (inode->i_filterdata) {
206                 kmem_cache_free(filter_info_cache, inode->i_filterdata);
207                 inode->i_filterdata = NULL;
208         }
209
210         sops = filter_c2csops(cache->cache_filter);
211         if (sops && sops->clear_inode)
212                 sops->clear_inode(inode);
213 }
214
215 struct super_operations currentfs_super_ops = {
216         read_inode:     currentfs_read_inode,
217         put_super:      currentfs_put_super,
218         clear_inode:    currentfs_clear_inode,
219 };