Whamcloud - gitweb
- vmlist needs to be exported for modules support
[fs/lustre-release.git] / lustre / kernel_patches / patches / snapfs_core-2.4.20.patch
1 %patch
2 Index: linux-2.4.20-8/fs/ext3/snap.c
3 ===================================================================
4 --- linux-2.4.20-8.orig/fs/ext3/snap.c  2003-01-30 18:24:37.000000000 +0800
5 +++ linux-2.4.20-8/fs/ext3/snap.c       2004-01-27 00:07:10.000000000 +0800
6 @@ -0,0 +1,2577 @@
7 +/* fs/ext3/snap.c
8 + *
9 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
10 + * started by Andreas Dilger <adilger@turbolinux.com>
11 + *            Peter Braam <braam@mountainviewdata.com>
12 + *            Harrison Xing <harrisonx@mountainviewdata.com>
13 + *           Eric Mei    <Ericm@clusterfs.com>
14 + * 
15 + * port to 2.4 by Wang Di  <wangdi@clusterfs.com>
16 + *                Eric Mei <ericm@clusterfs.com>
17 + * 
18 + * Functions for implementing snapshots in the ext3 filesystem.  They are
19 + * intended to hide the internals of the filesystem from the caller in
20 + * such a way that the caller doesn't need to know about inode numbers,
21 + * how the redirectors are implemented or stored, etc.  It may not do that
22 + * all yet, but it tries.
23 + *
24 + * The snapshot inode redirection is stored in the primary/direct inode as
25 + * an extended attribute $snap, in the form of little-endian u32 inode
26 + * numbers. 
27 + *   
28 + */
29
30 +#define EXPORT_SYMTAB
31 +#include <linux/module.h>
32 +
33 +#include <linux/sched.h>
34 +#include <linux/jbd.h>
35 +#include <linux/mm.h>
36 +#include <linux/slab.h>
37 +#include <linux/locks.h>
38 +#include <linux/snap.h>
39 +#include <linux/ext3_jbd.h>
40 +#include <linux/ext3_fs.h>
41 +#include <linux/ext3_xattr.h>
42 +
43 +#define EXT3_SNAP_ATTR "@snap"
44 +#define EXT3_SNAP_GENERATION_ATTR "@snap_generation"
45 +#define EXT3_MAX_SNAPS 20
46 +#define EXT3_MAX_SNAP_DATA (sizeof(struct snap_ea))
47 +#define EXT3_SNAP_INDEX EXT3_XATTR_INDEX_LUSTRE
48 +
49 +#define EXT3_SNAP_DEBUG
50 +#ifdef EXT3_SNAP_DEBUG
51 +       #define snap_debug(f, a...)                             \
52 +       do {                                                    \
53 +               printk (KERN_INFO "SNAP DEBUG: (%s, %d): %s: ", \
54 +                       __FILE__, __LINE__, __FUNCTION__);      \
55 +               printk (f, ## a);                               \
56 +       } while (0)
57 +
58 +       #define snap_err(f, a...)                               \
59 +       do {                                                    \
60 +               printk (KERN_ERR "SNAP ERROR: (%s, %d): %s: ",  \
61 +                       __FILE__, __LINE__, __FUNCTION__);      \
62 +               printk (f, ## a);                               \
63 +       } while (0)
64 +
65 +#else
66 +       #define snap_debug(f, a...)             do {} while (0)
67 +       #define snap_err(f, a...)                               \
68 +       do {                                                    \
69 +               printk (KERN_ERR "SNAP ERROR: (%s, %d): ",      \
70 +                       __FILE__, __LINE__);                    \
71 +               printk (f, ## a);                               \
72 +       } while (0)
73 +
74 +#endif
75 +
76 +#ifdef EXT3_SNAP_DEBUG
77 +       #define ALLOC(ptr, cast, size)                                  \
78 +       do {                                                            \
79 +               ptr = (cast)kmalloc((size_t) size, GFP_KERNEL);         \
80 +               if (ptr == 0) {                                         \
81 +                       printk(KERN_ERR "kmalloc returns 0 at %s:%d\n", \
82 +                              __FILE__, __LINE__);                     \
83 +               } else {                                                \
84 +                       snap_kmem += size;                              \
85 +                       printk(KERN_INFO "snap_alloc %d, kmem %ld\n",   \
86 +                               (size_t)size, snap_kmem);               \
87 +               }                                                       \
88 +       } while (0)
89 +
90 +       #define FREE(ptr,size)                                          \
91 +       do {                                                            \
92 +               kfree((ptr));                                           \
93 +               snap_kmem -= size;                                      \
94 +               printk(KERN_INFO "snap_free %d, kmem %ld\n",            \
95 +                       (size_t)size, snap_kmem);                       \
96 +       } while (0)
97 +
98 +#else
99 +       #define ALLOC(ptr, cast, size)                                  \
100 +       do {                                                            \
101 +               ptr = (cast)kmalloc((size_t) size, GFP_KERNEL);         \
102 +       } while (0)
103 +
104 +       #define FREE(ptr,size)                                          \
105 +       do {                                                            \
106 +               kfree((ptr));                                           \
107 +       } while (0)
108 +
109 +#endif /* EXT3_SNAP_DEBUG */
110 +
111 +#ifdef EXT3_SNAP_DEBUG
112 +       /* modestr: convert inode mode to string . debug function */
113 +       static char * modestr ( umode_t mode )
114 +       {
115 +               if( S_ISREG(mode) )
116 +                       return "file";
117 +               else if(S_ISDIR(mode))
118 +                       return "dir";
119 +               else if(S_ISLNK(mode))
120 +                       return "link";
121 +               else if(S_ISCHR(mode))
122 +                       return "char";
123 +               else if(S_ISBLK(mode))
124 +                       return "block";
125 +               else if(S_ISFIFO(mode))
126 +                       return "fifo";
127 +               else if(S_ISSOCK(mode))
128 +                       return "sock";
129 +               else
130 +                       return "non-known";
131 +       }
132 +#define DEBUG_INODE(inode)                                      \
133 +       if(inode && !IS_ERR(inode)) {                                                   \
134 +               snap_debug("%s ino %lu, i_nlink %u, i_count %d, i_mode %u, i_size %lld, i_blocks %lu\n", \
135 +               modestr(inode->i_mode), inode->i_ino, inode->i_nlink,       \
136 +               atomic_read(&inode->i_count), inode->i_mode, inode->i_size, \
137 +               inode->i_blocks); }
138 +#else
139 +       #define modestr(mode)           do {} while (0)
140 +       #define DEBUG_INODE(inode)      
141 +
142 +#endif /* EXT3_SNAP_DEBUG */
143 +/* do file cow on: dir, symlink, regular but fs has filecow flag */
144 +
145 +#define IS_FILECOW_TYPE(inode)         \
146 +       (S_ISDIR(inode->i_mode) ||      \
147 +        S_ISLNK(inode->i_mode) ||      \
148 +        (S_ISREG(inode->i_mode) &&     \
149 +        !SNAP_HAS_COMPAT_FEATURE(inode->i_sb, SNAP_FEATURE_COMPAT_BLOCKCOW)))
150 +
151 +#define SNAP_ERROR(err)  ((err) < 0 ? (err) : (-(err)))
152 +/* SNAP_ERROR(err): Make sure we return negative errors for Linux ( return positive errors) */
153 +
154 +#ifdef DEBUG
155 +#ifdef __KERNEL__
156 +# ifdef  __ia64__
157 +#  define CDEBUG_STACK (THREAD_SIZE -                                      \
158 +                        ((unsigned long)__builtin_dwarf_cfa() &            \
159 +                         (THREAD_SIZE - 1)))
160 +# else
161 +#  define CDEBUG_STACK (THREAD_SIZE -                                      \
162 +                        ((unsigned long)__builtin_frame_address(0) &       \
163 +                         (THREAD_SIZE - 1)))
164 +# endif
165 +
166 +#define snap_debug_msg(file, fn, line, stack, format, a...)                 \
167 +    printf("(%s:%s,l. %d %d %lu): " format, file, fn, line,                  \
168 +           getpid() , stack, ## a);
169 +#endif
170 +
171 +#define CDEBUG(mask, format, a...)                                            \
172 +do {                                                                          \
173 +        CHECK_STACK(CDEBUG_STACK);                                            \
174 +        if (!(mask) || ((mask) & (D_ERROR | D_EMERG)))                       \
175 +                snap_debug_msg(__FILE__, __FUNCTION__, __LINE__,           \
176 +                               CDEBUG_STACK, format, ## a);                \
177 +} while (0)
178 +
179 +#define CWARN (format, a...) CDEBUG(D_WARNING, format, ## a)
180 +#define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
181 +#define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
182 +
183 +#define RETURN(rc)                                                      \
184 +do {                                                                    \
185 +        typeof(rc) RETURN__ret = (rc);                                  \
186 +        CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
187 +               (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
188 +        return RETURN__ret;                                             \
189 +} while (0)
190 +
191 +#define ENTRY                                                           \
192 +do {                                                                    \
193 +        CDEBUG(D_TRACE, "Process entered\n");                           \
194 +} while (0)
195 +
196 +#define EXIT                                                            \
197 +do {                                                                    \
198 +        CDEBUG(D_TRACE, "Process leaving\n");                           \
199 +} while(0)
200 +#else
201 +#define CDEBUG(mask, format, a...)      do { } while (0)
202 +#define CWARN(format, a...)             do { } while (0)
203 +#define CERROR(format, a...)            printk("<3>" format, ## a)
204 +#define CEMERG(format, a...)            printk("<0>" format, ## a)
205 +#define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
206 +#define RETURN(rc)                      return (rc)
207 +#define ENTRY                           do { } while (0)
208 +#define EXIT                            do { } while (0)
209 +#endif /*DEBUG*/
210 +
211 +#define SNAP_ATTR_BUF_CNT 10
212 +
213 +#define SB_LAST_COWED_INO(sb)  (EXT3_SB(sb)->s_es->s_last_cowed_pri_ino) 
214 +#define SB_FIRST_COWED_INO(sb) (EXT3_SB(sb)->s_es->s_first_cowed_pri_ino)
215 +#define SB_SNAPTABLE_INO(sb)   (EXT3_SB(sb)->s_es->s_snaptable_ino)
216 +#define SB_SNAP_LIST_SEM(sb)   (EXT3_SB(sb)->s_snap_list_sem)
217 +#define SB_FEATURE_COMPAT(sb)  (EXT3_SB(sb)->s_es->s_feature_compat)
218 +
219 +#define        SNAP_HAS_COMPAT_FEATURE(sb,mask)        \
220 +       (SB_FEATURE_COMPAT(sb) & cpu_to_le32(mask))
221 +
222 +/* NOTE: these macros are close dependant on the structure of snap ea */
223 +#define SNAP_CNT_FROM_SIZE(size)       ((((size)-sizeof(ino_t)*2)/2)/sizeof(ino_t))
224 +#define SNAP_EA_SIZE_FROM_INDEX(index) (sizeof(ino_t)*2 + 2*sizeof(ino_t)*((index)+1))
225 +
226 +#define SNAP_EA_INO_BLOCK_SIZE(size)   (((size)-sizeof(ino_t)*2)/2)
227 +#define SNAP_EA_PARENT_OFFSET(size)    (sizeof(ino_t)*2 + SNAP_EA_INO_BLOCK_SIZE((size)))
228 +/*SET FLAGS*/
229 +extern int ext3_bmap(struct address_space *mapping, long block);
230 +extern int ext3_load_inode_bitmap (struct super_block * sb, unsigned int block_group);
231 +extern int ext3_block_truncate_page(handle_t *handle, struct address_space *mapping, 
232 +                                   loff_t from);
233 +/* helper functions to manipulate field 'parent' in snap_ea */
234 +static inline int
235 +set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
236 +{
237 +       char * p = (char*) pea;
238 +       int offset;
239 +
240 +       offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
241 +       offset += sizeof(ino_t) * index;
242 +       *(ino_t*)(p+offset) = val;
243 +
244 +       return 0;
245 +}
246 +/* ext3_iterate_cowed_inode:
247 + *     iterate all the cowed inode with the same index and 
248 + *  run the associate function @repeat
249 + *
250 + *  For @repeat, if it returns non-zero value, it will exit the iterator
251 + *
252 + *  return value:      0 or positive:  success
253 + *                     negative:       failure
254 + *  additional: if the return value is positive, it must be the return value
255 + *             of function @repeat.
256 + */
257 +
258 +static int ext3_iterate_cowed_inode(
259 +               struct super_block *sb,
260 +               int (*repeat)(struct inode *inode, void *priv),
261 +               struct inode **start,
262 +               void *priv)
263 +{      
264 +       struct inode *list_inode = NULL;
265 +       char buf[EXT3_MAX_SNAP_DATA];   
266 +       struct snap_ea *snaps;
267 +       int  err = 0;
268 +
269 +       if (SB_FIRST_COWED_INO(sb) == 0) {
270 +               snap_debug("no cowed inode in the list\n"); 
271 +               return 0;
272 +       }
273 +
274 +       /* get head inode in the list */
275 +       if (start != NULL && *start != NULL && (*start)->i_ino)
276 +               list_inode = iget(sb, (*start)->i_ino);
277 +       else
278 +               list_inode = iget (sb, le32_to_cpu( SB_FIRST_COWED_INO(sb) ));
279 +
280 +       /* loop for all inode in list */
281 +       while (list_inode) {
282 +               if (!list_inode->i_nlink || is_bad_inode(list_inode)) {
283 +                       snap_err("inode %p, ino %lu, mode %o, nlink %d\n",
284 +                                       list_inode,
285 +                                       list_inode->i_ino,
286 +                                       list_inode->i_mode,
287 +                                       list_inode->i_nlink);
288 +                       err = -EIO;
289 +                       goto err_iput;
290 +               }
291 +
292 +               err = ext3_xattr_get(list_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
293 +                                                       buf, EXT3_MAX_SNAP_DATA);
294 +               if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
295 +                       snap_err("inode %lu, error %d\n", list_inode->i_ino, err);
296 +                       goto err_iput;
297 +               }
298 +
299 +               if ((err = (*repeat)(list_inode, priv)) != 0)
300 +                       goto err_iput;
301 +
302 +               iput (list_inode);
303 +
304 +               snaps = (struct snap_ea *) buf;
305 +               if (le32_to_cpu (snaps->next_ino) != 0) {
306 +                       list_inode = iget(sb, le32_to_cpu(snaps->next_ino));
307 +               }
308 +               else {
309 +                       snap_debug ("cowed inode list end, exit\n");
310 +                       goto err_free;
311 +               }
312 +       }
313 +err_iput:
314 +       if (list_inode) 
315 +               iput(list_inode);
316 +err_free:
317 +       return err; 
318 +}
319 +static int get_cowed_ino(struct inode *pri, void *param)
320 +{
321 +       ino_t *find = param;
322 +        (*find) = pri->i_ino;
323 +        return 0;
324 +}
325 +
326 +/* Return 0 for error. */
327 +static int get_cowed_ino_end (struct inode *inode)
328 +{
329 +        int rc;
330 +        ino_t ino = 0;
331 +
332 +        rc = ext3_iterate_cowed_inode(inode->i_sb, &get_cowed_ino, &inode, &ino);
333 +
334 +       if (rc < 0)
335 +                return 0;
336 +        else
337 +                return ino;
338 +}
339 +
340 +/* find the end of the primary inode, iterate if needed
341 + * return 0 if any error found */
342 +static inline ino_t find_last_cowed_ino(struct super_block *sb)
343 +{
344 +       struct inode *inode = NULL;
345 +       ino_t first, last = 0;
346 +
347 +        last = le32_to_cpu(SB_LAST_COWED_INO(sb));
348 +       if (last)
349 +               return last;
350 +
351 +       first = le32_to_cpu(SB_FIRST_COWED_INO(sb));
352 +
353 +       if (!first) {
354 +               snap_err("first cowed inode is NULL\n");
355 +               goto exit;
356 +       }
357 +
358 +       inode = iget(sb, first);
359 +       if (inode) {
360 +               if (is_bad_inode(inode)) {
361 +                       snap_err("bad inode %lu\n", first);
362 +                       goto exit;
363 +               }
364 +
365 +               last = get_cowed_ino_end(inode);
366 +       }
367 +exit:
368 +       if (inode)
369 +               iput(inode);
370 +       return last;
371 +}
372 +
373 +/* Insert the primary inode to the cowed inode list 
374 + * Append it to the list end
375 + * 
376 + * @pri: inode to insert
377 + * @buf_pri: the valid ea buf for @pri inode ( excluding the next_ino field) , 
378 + * it's used to write the ea for @pri inode
379 + * 
380 + * To avoid list broken in abnormal case, it will first write the ea for @pri
381 + * inode, and then write ea for the list end inode. Thus list broken is 
382 + * avoid even if there are errors when writting ea.    
383 + */
384 +static int insert_cowed_ino_to_list (handle_t *handle, struct inode *pri, char *buf_pri)
385 +{
386 +       char buf[EXT3_MAX_SNAP_DATA];
387 +       struct snap_ea *snaps;
388 +       struct snap_ea *snaps_pri;
389 +       struct inode *last_inode = NULL;
390 +       struct ext3_sb_info *sbi = EXT3_SB(pri->i_sb);
391 +       int err = 0; 
392 +       
393 +       snaps_pri = (struct snap_ea *)buf_pri;
394 +
395 +       if (!SB_FIRST_COWED_INO(pri->i_sb)) {
396 +               /* we set the next_ino and write ea for pri inode */
397 +               snaps_pri->next_ino = cpu_to_le32(0);
398 +               snaps_pri->prev_ino = cpu_to_le32(0);
399 +
400 +               err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
401 +                                          buf_pri, EXT3_MAX_SNAP_DATA, 0);
402 +               if (err < 0) {
403 +                       snap_err("ino %lu, set_ext_attr err %d\n", pri->i_ino, err);
404 +                       return err;
405 +               }
406 +               lock_super(pri->i_sb);
407 +               ext3_journal_get_write_access(handle, sbi->s_sbh);
408 +               sbi->s_es->s_first_cowed_pri_ino = cpu_to_le32(pri->i_ino);
409 +               SB_FIRST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
410 +               pri->i_sb->s_dirt = 1;
411 +               ext3_journal_dirty_metadata(handle, sbi->s_sbh);
412 +               unlock_super(pri->i_sb);
413 +               EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG; 
414 +               return err;
415 +       }
416 +
417 +       if (!SB_LAST_COWED_INO(pri->i_sb)){
418 +               SB_LAST_COWED_INO(pri->i_sb) = find_last_cowed_ino(pri->i_sb);
419 +               if (!SB_LAST_COWED_INO(pri->i_sb) ){
420 +                       snap_err("error, last cowed inode is NULL\n");
421 +                       return (-EINVAL);
422 +               }
423 +       }
424 +        
425 +       last_inode = iget(pri->i_sb, SB_LAST_COWED_INO(pri->i_sb));
426 +       if (!last_inode || is_bad_inode(last_inode)) {
427 +               iput(last_inode);
428 +               return -EINVAL;
429 +       }
430 +       err = ext3_xattr_get(last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
431 +                              buf, EXT3_MAX_SNAP_DATA);
432 +        if (err == -ENODATA) {
433 +               snap_debug("no existing attributes - zeroing\n");
434 +               memset(buf, 0, EXT3_MAX_SNAP_DATA);
435 +        } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
436 +               snap_debug("got err %d when reading attributes\n", err);
437 +              goto exit;
438 +       }
439 +       /*set primary inode EA*/
440 +       snaps_pri->next_ino = 0;
441 +        snaps_pri->prev_ino = cpu_to_le32(last_inode->i_ino);
442 +
443 +       err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
444 +                                      buf_pri, EXT3_MAX_SNAP_DATA, 0);
445 +       if (err < 0) {
446 +                snap_debug("set attributes error for inode %lu\n",
447 +                               (ulong)pri->i_ino);
448 +               goto exit;
449 +       }
450 +
451 +       /*set last inode EA*/
452 +       snaps = (struct snap_ea *) buf;
453 +       snaps->next_ino = cpu_to_le32(pri->i_ino);
454 +        err = ext3_xattr_set(handle, last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
455 +                                             buf, EXT3_MAX_SNAP_DATA, 0);
456 +        if(err < 0){
457 +                snap_debug("set attributes error for inode %lu\n",
458 +                               (ulong)last_inode->i_ino);
459 +               goto exit;
460 +       }
461 +       
462 +       EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG; 
463 +       
464 +       /* we update the new cowed ino list end in memory */ 
465 +       SB_LAST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
466 +        snap_debug("cowed_inode_list_end %lu, append ino=%lu\n",
467 +                   last_inode->i_ino, pri->i_ino);
468 +exit:
469 +       if (last_inode)
470 +               iput(last_inode);
471 +
472 +       return err;
473 +}
474 +
475 +/* delelte the ino from cowed inode list */
476 +static int delete_cowed_ino_from_list (handle_t *handle, struct inode *inode)
477 +{
478 +       ino_t prev_ino = 0, next_ino = 0;
479 +       struct inode *prev_inode = NULL;
480 +       struct inode *next_inode = NULL;
481 +       struct snap_ea *snaps;
482 +       char buf[EXT3_MAX_SNAP_DATA];
483 +       int err = 0;
484 +
485 +       err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
486 +                                           buf, EXT3_MAX_SNAP_DATA);
487 +       if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
488 +               snap_err("get attr inode %lu, error %d\n", inode->i_ino, err);
489 +               goto err_exit;
490 +       }
491 +
492 +       snaps = (struct snap_ea *) buf;
493 +       next_ino = le32_to_cpu(snaps->next_ino);
494 +        prev_ino = le32_to_cpu(snaps->prev_ino);
495 +
496 +       /* if this is the first cowed ino */
497 +       if (inode->i_ino == le32_to_cpu(SB_FIRST_COWED_INO(inode->i_sb))) {
498 +               SB_FIRST_COWED_INO(inode->i_sb) = cpu_to_le32(next_ino); 
499 +               EXT3_I(inode)->i_flags &= ~EXT3_SNAP_PRI_FLAG;
500 +               if (next_ino == 0)      
501 +                        SB_LAST_COWED_INO(inode->i_sb) = 0;
502 +       } else {
503 +               if (!prev_ino)  
504 +                       goto err_exit;
505 +
506 +               /* find previous inode and read its ea */
507 +               prev_inode = iget(inode->i_sb, prev_ino);
508 +                if (!prev_inode || is_bad_inode(prev_inode)) 
509 +                        goto err_exit;
510 +                            
511 +               err = ext3_xattr_get(prev_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
512 +                                                  buf, EXT3_MAX_SNAP_DATA);
513 +               if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
514 +                       snap_err("get attr inode %lu, error %d\n", prev_inode->i_ino, err);
515 +                       goto err_exit;
516 +               }
517 +               
518 +               /* make the previous inode point to the next inode,
519 +                * but ignore errors because at current version we
520 +                * didn't use the previous pionter */
521 +               snaps = (struct snap_ea *) buf;
522 +               snaps->next_ino = cpu_to_le32(next_ino);
523 +
524 +               snap_debug("delete ino %lu from list\n", inode->i_ino);
525 +
526 +               err = ext3_xattr_set(handle, prev_inode, EXT3_SNAP_INDEX, 
527 +                                    EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
528 +               if (err < 0) {
529 +                       snap_err("err %d setting ea for ino %lu\n", err, prev_inode->i_ino);
530 +                       goto err_exit;
531 +               }
532 +
533 +                if (next_ino == 0) {
534 +                        SB_LAST_COWED_INO(inode->i_sb) = prev_ino;
535 +                        goto err_exit;
536 +                }
537 +
538 +               /* make the next inode point to the previous one */
539 +               next_inode = iget(inode->i_sb, next_ino);
540 +                if (!next_inode || is_bad_inode(next_inode))       
541 +                        goto err_exit;
542 +
543 +               err = ext3_xattr_get(next_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
544 +                                                  buf, EXT3_MAX_SNAP_DATA);
545 +               if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
546 +                       snap_err("set attr inode %lu, error %d\n", next_inode->i_ino, err);
547 +                       goto err_exit;
548 +               }
549 +               snaps = ( struct snap_ea *) buf;
550 +               snaps->prev_ino = cpu_to_le32(prev_ino);
551 +
552 +               err = ext3_xattr_set(handle, next_inode, EXT3_SNAP_INDEX, 
553 +                                       EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
554 +               if (err < 0) {
555 +                       snap_err("err %d setting attributes for ino %lu\n",
556 +                                     err, next_inode->i_ino);
557 +               }
558 +       }
559 +err_exit:
560 +       iput(prev_inode);
561 +       iput(next_inode);
562 +       return err;
563 +}
564 +
565 +static inline void lock_list(struct super_block *sb)
566 +{
567 +       down(&SB_SNAP_LIST_SEM(sb));
568 +}
569 +
570 +static inline void unlock_list(struct super_block *sb)
571 +{
572 +       up(&SB_SNAP_LIST_SEM(sb));
573 +}
574 +
575 +static int ext3_snap_feature (struct super_block *sb, int feature, int op) {
576 +
577 +       int rc = -EINVAL;
578 +       handle_t *handle;
579 +       switch (op) {
580 +               case SNAP_SET_FEATURE:
581 +                       handle = ext3_journal_start(sb->s_root->d_inode, 1);
582 +                       lock_super(sb);
583 +                       ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
584 +                       SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
585 +                       sb->s_dirt = 1;
586 +                       ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
587 +                       unlock_super(sb);
588 +                       ext3_journal_stop(handle, sb->s_root->d_inode); 
589 +                       break;
590 +               case SNAP_CLEAR_FEATURE:
591 +                       handle = ext3_journal_start(sb->s_root->d_inode, 1);
592 +                       lock_super(sb);
593 +                       ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
594 +                       SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature); 
595 +                       ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
596 +                       sb->s_dirt = 1;
597 +                       unlock_super(sb);
598 +                       ext3_journal_stop(handle, sb->s_root->d_inode); 
599 +                       break;
600 +               case SNAP_HAS_FEATURE:
601 +                       /*FIXME should lock super or not*/
602 +                       rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
603 +                       break;
604 +               default:
605 +                       break;
606 +       }
607 +       return rc;
608 +}
609 +
610 +#ifdef _DEVICE_FAIL_TEST
611 +/*FIXME later*/
612 +extern int loop_discard_io(kdev_t dev, long arg);
613 +/*
614 + * modify failpos to let loop fail at certain point
615 + * let pos=0 mean no fail point
616 + */
617 +static int failpos = 0;
618 +#define loopfail(pos)  \
619 +       do{                                                                     \
620 +               if( pos == failpos ){                                           \
621 +                       int i;                                                  \
622 +                       printk(KERN_EMERG "SNAP; hit fail point %d\n", failpos);\
623 +                       for( i=0; i<15; i++ )                                   \
624 +                               loop_discard_io( MKDEV(7,i), 1 );               \
625 +               }                                                               \
626 +       }while(0)
627 +#else
628 +#define loopfail(pos) do{}while(0)
629 +#endif
630 +
631 +/* Save the indirect inode in the snapshot table of the primary inode. */
632 +static int ext3_set_indirect(struct inode *pri, int index, ino_t ind_ino, ino_t parent_ino )
633 +{
634 +       char buf[EXT3_MAX_SNAP_DATA];
635 +       struct snap_ea *snaps;
636 +       int err = 0, inlist = 1;
637 +       int ea_size;
638 +       handle_t *handle = NULL;
639 +       
640 +       snap_debug("(ino %lu, parent %lu): saving ind %lu to index %d\n", 
641 +                       pri->i_ino, parent_ino, ind_ino, index);
642 +
643 +       if (index < 0 || index > MAX_SNAPS || !pri)
644 +               return -EINVAL;
645 +       /* need lock the list before get_attr() to avoid race */
646 +       lock_list(pri->i_sb);
647 +       /* read ea at first */
648 +       err = ext3_xattr_get(pri, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
649 +                                         buf, EXT3_MAX_SNAP_DATA);
650 +       if (err == -ENODATA || err == -ENOATTR) {
651 +               snap_debug("no extended attributes - zeroing\n");
652 +               memset(buf, 0, EXT3_MAX_SNAP_DATA);
653 +               /* XXX
654 +                * To judge a inode in list, we only see if it has snap ea.
655 +                * So take care of snap ea of primary inodes very carefully.
656 +                * Is it right in snapfs EXT3, check it later?
657 +                */
658 +               inlist = 0; 
659 +       } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
660 +               goto out_unlock;
661 +       }
662 +       
663 +       handle = ext3_journal_start(pri, SNAP_SETIND_TRANS_BLOCKS);
664 +       if(!handle) {
665 +               err = PTR_ERR(handle);
666 +               goto out_unlock;
667 +       }
668 +       
669 +       snaps = (struct snap_ea *)buf;
670 +       snaps->ino[index] = cpu_to_le32 (ind_ino);
671 +       ea_size = EXT3_MAX_SNAP_DATA;
672 +
673 +       set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
674 +
675 +       snap_debug("saving attributes\n");
676 +
677 +       if (inlist) {
678 +               err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
679 +                                    buf, EXT3_MAX_SNAP_DATA, 0);
680 +       }
681 +       else {
682 +               /* This will also write the ea for the pri inode, like above */
683 +               err = insert_cowed_ino_to_list(handle, pri, buf);
684 +       }
685 +       ext3_mark_inode_dirty(handle, pri);
686 +       ext3_journal_stop(handle, pri);
687 +out_unlock:
688 +       unlock_list(pri->i_sb);
689 +       return err;
690 +}
691 +
692 +/*
693 + * is_redirector - determines if a primary inode is a redirector
694 + * @inode: primary inode to test
695 + *
696 + * Returns 1 if the inode is a redirector, 0 otherwise.
697 + */
698 +static int is_redirector(struct inode *inode)
699 +{
700 +       int is_redirector = 0;
701 +       int rc;
702 +
703 +       rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
704 +                                         NULL, 0);
705 +        if (rc > 0 && rc <= MAX_SNAP_DATA)
706 +                is_redirector = 1;
707 +        snap_debug("inode %lu %s redirector\n", inode->i_ino, 
708 +                       is_redirector ? "is" : "isn't");
709 +        return is_redirector;
710 +}
711 +
712 +/*if it's indirect inode or not */
713 +static int is_indirect(struct inode *inode)
714 +{
715 +       if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
716 +               return 1;
717 +       else
718 +               return 0;
719 +}
720 +/*
721 + * Copy inode metadata from one inode to another, excluding blocks and size.
722 + * FIXME do we copy EA data - ACLs and such (excluding snapshot data)?
723 + */
724 +static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
725 +{
726 +       int size;
727 +       
728 +       dst->i_mode = src->i_mode;
729 +       dst->i_nlink = src->i_nlink;
730 +       dst->i_uid = src->i_uid;
731 +       dst->i_gid = src->i_gid;
732 +       dst->i_atime = src->i_atime;
733 +       dst->i_mtime = src->i_mtime;
734 +       dst->i_ctime = src->i_ctime;
735 +//     dst->i_version = src->i_version;
736 +       dst->i_attr_flags = src->i_attr_flags;
737 +       dst->i_generation = src->i_generation;
738 +       dst->u.ext3_i.i_dtime = src->u.ext3_i.i_dtime;
739 +       dst->u.ext3_i.i_flags = src->u.ext3_i.i_flags | EXT3_COW_FL;
740 +#ifdef EXT3_FRAGMENTS
741 +       dst->u.ext3_i.i_faddr = src->u.ext3_i.i_faddr;
742 +       dst->u.ext3_i.i_frag_no = src->u.ext3_i.i_frag_no;
743 +       dst->u.ext3_i.i_frag_size = src->u.ext3_i.i_frag_size;
744 +#endif
745 +       if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
746 +               char names[size];
747 +               char *name;
748 +               int namelen;
749 +
750 +               if (ext3_xattr_list(src, names, 0) < 0)
751 +                       return;
752 +               /*
753 +                * the list of attribute names are stored as NUL terminated
754 +                * strings, with a double NUL string at the end.
755 +                */
756 +               name = names;
757 +               while ((namelen = strlen(name))) {
758 +                       int attrlen;
759 +                       char *buf;
760 +                       
761 +                       /* don't copy snap data */
762 +                       if (!strcmp(name, EXT3_SNAP_ATTR)) {
763 +                               snap_debug("skipping %s item\n", name);
764 +                               continue;
765 +                       }
766 +                       snap_debug("copying %s item\n", name);
767 +                       attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX, 
768 +                                                     EXT3_SNAP_ATTR, NULL, 0);
769 +                       if (attrlen < 0)
770 +                               continue;
771 +                       if ((buf = kmalloc(attrlen, GFP_ATOMIC)) == NULL)
772 +                               break;
773 +                       if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
774 +                                               EXT3_SNAP_ATTR, buf, attrlen) < 0)
775 +                               continue;       
776 +                       if (ext3_xattr_set(handle, dst, EXT3_SNAP_INDEX,
777 +                                               EXT3_SNAP_ATTR, buf, attrlen, 0) < 0)
778 +                               break;
779 +                       kfree(buf);
780 +                       name += namelen + 1; /* skip name and trailing NUL */
781 +               }
782 +       }
783 +}
784 +
785 +static inline int ext3_has_ea(struct inode *inode)
786 +{
787 +       return (EXT3_I(inode)->i_file_acl != 0);
788 +}
789 +/* XXX This function has a very bad effect to 
790 + * the performance of filesystem,
791 + * will find another way to fix it 
792 + */
793 +static void fs_flushinval_pages(handle_t *handle, struct inode* inode)
794 +{
795 +       if (inode->i_blocks > 0 && inode->i_mapping) { 
796 +               fsync_inode_data_buffers(inode);
797 +        //     ext3_block_truncate_page(handle, inode->i_mapping, inode->i_size);
798 +               truncate_inode_pages(inode->i_mapping, 0);
799 +       }
800 +}
801 +
802 +/* ext3_migrate_data2:
803 + *  MOVE all the data blocks from inode src to inode dst as well as
804 + *  COPY all attributes(meta data) from inode src to inode dst.
805 + *  For extended attributes(EA), we COPY all the EAs but skip the Snap EA from src to dst.
806 + *  If the dst has Snap EA, then we CAN'T overwrite it. We CAN'T copy the src Snap EA.
807 + *  XXX for EA, can we change it to MOVE all the EAs(exclude Snap EA) to dst and copy it back to src ?
808 + *  This is for LAN free backup later.
809 + */
810 +
811 +static int ext3_migrate_data (handle_t *handle, struct inode *dst, struct inode *src)
812 +{
813 +       unsigned long err = 0;
814 +       /* 512 byte disk blocks per inode block */
815 +       int bpib = src->i_sb->s_blocksize >> 9;
816 +       
817 +       if((!dst) || (!src)) 
818 +               return -EINVAL;
819 +       
820 +       if (dst->i_ino == src->i_ino)
821 +               return 0;
822 +
823 +       fs_flushinval_pages(handle, src);
824 +       
825 +       ext3_copy_meta(handle, dst, src);
826 +
827 +       snap_debug("migrating data blocks from %lu to %lu\n", src->i_ino, dst->i_ino);
828 +       /* Can't check blocks in case of EAs */
829 +        memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
830 +                             sizeof(EXT3_I(src)->i_data));
831 +        memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
832 +
833 +       ext3_discard_prealloc(src);
834 +
835 +       dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
836 +        src->i_size = EXT3_I(src)->i_disksize = 0;
837 +
838 +       dst->i_blocks = src->i_blocks;
839 +        src->i_blocks = 0;
840 +        /*  Check EA blocks here to modify i_blocks correctly */
841 +        if(ext3_has_ea (src)) {
842 +               src->i_blocks += bpib;
843 +               if( ! ext3_has_ea (dst) )
844 +                       if( dst->i_blocks >= bpib )
845 +                               dst->i_blocks -= bpib;
846 +       } else {
847 +               if( ext3_has_ea (dst))
848 +                       dst->i_blocks += bpib;
849 +       }
850 +       
851 +       snap_debug("migrate data from ino %lu to ino %lu\n", 
852 +               src->i_ino, dst->i_ino);        
853 +        ext3_mark_inode_dirty(handle, src);
854 +        ext3_mark_inode_dirty(handle, dst);
855 +
856 +
857 +       return SNAP_ERROR(err);
858 +}
859 +
860 +/**
861 + * ext3_get_indirect - get a specific indirect inode from a primary inode
862 + * @primary: primary (direct) inode
863 + * @table: table of @slot + 1 indices in reverse chronological order
864 + * @slot: starting slot number to check for indirect inode number
865 + *
866 + * We locate an indirect inode from a primary inode using the redirection
867 + * table stored in the primary inode.  Because the desired inode may actually
868 + * be in a "newer" slot number than the supplied slot, we are given a table
869 + * of indices in chronological order to search for the correct inode number.
870 + * We walk table from @slot to 0 looking for a non-zero inode to load.
871 + *
872 + * To only load a specific index (and fail if it does not exist), you can
873 + * pass @table = NULL, and the index number in @slot.  If @slot == 0, the
874 + * primary inode data is returned.
875 + *
876 + * We return a pointer to an inode, or an error.  If the indirect inode for
877 + * the given index does not exist, NULL is returned.
878 + */
879 +static struct inode *ext3_get_indirect(struct inode *primary, int *table,
880 +                                      int slot)
881 +{
882 +       char buf[EXT3_MAX_SNAP_DATA];
883 +       struct snap_ea *snaps;
884 +       ino_t ino;
885 +       struct inode *inode = NULL;
886 +       int err = 0, index = 0;
887 +
888 +       if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
889 +               return NULL;
890 +        
891 +       snap_debug("ino %lu, table %p, slot %d\n", primary->i_ino, table,slot);
892 +       
893 +       err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, 
894 +                               buf, EXT3_MAX_SNAP_DATA); 
895 +       if (err == -ENODATA) {
896 +               slot = 0;
897 +       } else if (err < 0) {
898 +               snap_debug(" attribute read error\n");
899 +               return NULL;
900 +       }
901 +       snaps = (struct snap_ea *)buf;
902 +
903 +       /* if table is NULL and there is a slot */
904 +       if( !table && slot ) {
905 +               index = slot;
906 +               ino = le32_to_cpu ( snaps->ino[index] );
907 +               if(ino) inode = iget(primary->i_sb, ino);
908 +               goto err_free;
909 +       }
910 +       /* if table is not NULL */
911 +       while ( !inode && slot > 0) {
912 +               index = table[slot];
913 +               ino = le32_to_cpu ( snaps->ino[index] );
914 +
915 +               snap_debug("snap inode at slot %d is %lu\n", slot, ino);
916 +               if (!ino) {
917 +                       --slot;
918 +                       continue;
919 +               }
920 +               inode = iget(primary->i_sb, ino);
921 +               goto err_free;
922 +       }
923 +       if( slot == 0 && table ) {
924 +               snap_debug("redirector not found, using primary\n");
925 +               inode = iget(primary->i_sb, primary->i_ino);
926 +       }
927 +err_free:
928 +       return inode;
929 +}
930 +
931 +/* get the indirect ino at index of the primary inode 
932 + * return value:       postive:        indirect ino number
933 + *                     negative or 0:  error
934 + */
935 +static ino_t ext3_get_indirect_ino(struct inode *primary, int index)
936 +{
937 +        char buf[EXT3_MAX_SNAP_DATA];
938 +        struct snap_ea *snaps;
939 +        ino_t ino = 0;
940 +        int err;
941 +
942 +        if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
943 +                return 0;
944 +
945 +       err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, 
946 +                               buf, EXT3_MAX_SNAP_DATA); 
947 +        if (err == -ENOATTR) {
948 +                ino = -ENOATTR;
949 +               goto err_free;
950 +        } else if (err < 0) {
951 +                snap_err(EXT3_SNAP_ATTR " attribute read error\n");
952 +                ino = -EINVAL;
953 +               goto err_free;
954 +        }
955 +
956 +        snaps = (struct snap_ea *)buf;
957 +        ino = le32_to_cpu (snaps->ino[index]);
958 +        snap_debug("snap ino for %ld at index %d is %lu\n",
959 +                        primary->i_ino, index, ino);
960 +err_free:
961 +        return ino;
962 +}
963 +/* ext3_copy_block - copy one data block from inode @src to @dst.
964 +   No lock here.  User should do the lock.
965 +   User should check the return value to see if the result is correct.
966 +   Return value:
967 +   1:    The block has been copied successfully
968 +   0:    No block is copied, usually this is because src has no such blk
969 +  -1:    Error
970 +*/
971 +
972 +static int ext3_copy_block (struct inode *dst, struct inode *src, int blk) 
973 +{
974 +       struct buffer_head *bh_dst = NULL, *bh_src = NULL;
975 +       int err = 0;
976 +       handle_t *handle = NULL;
977 +
978 +       
979 +       snap_debug("copy blk %d from %lu to %lu \n", blk, src->i_ino, dst->i_ino);
980 +       /* 
981 +        * ext3_getblk() require handle!=NULL
982 +        */
983 +       if (S_ISREG(src->i_mode))
984 +               return 0;
985 +
986 +       handle = ext3_journal_start(dst, SNAP_COPYBLOCK_TRANS_BLOCKS);
987 +       if( !handle )
988 +               return -1;
989 +
990 +       bh_src = ext3_bread(handle, src, blk, 0, &err);
991 +       if (!bh_src) {
992 +               snap_err("error for src blk %d, error %d\n", blk, err);
993 +               goto exit_relese;
994 +       }
995 +       bh_dst = ext3_getblk(handle, dst, blk, 1, &err);
996 +       if (!bh_dst) {
997 +               snap_err("error for dst blk %d, error %d\n", blk, err);
998 +               err = -ENOSPC;
999 +               goto exit_relese;
1000 +       }
1001 +       snap_debug("copy block %lu to %lu (%ld bytes)\n",
1002 +                  bh_src->b_blocknr, bh_dst->b_blocknr, 
1003 +                  src->i_sb->s_blocksize);
1004 +
1005 +       ext3_journal_get_write_access(handle, bh_dst);
1006 +
1007 +       memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
1008 +
1009 +       ext3_journal_dirty_metadata(handle, bh_dst);
1010 +       err = 1;
1011 +exit_relese:
1012 +       if (bh_src) brelse(bh_src);
1013 +       if (bh_dst) brelse(bh_dst);
1014 +       if (handle)     
1015 +               ext3_journal_stop(handle, dst);
1016 +       return err;
1017 +}
1018 +
1019 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1020 +/*
1021 + * add one inode to superblock's snap_orphan chain
1022 + * only add on-disk data for simplicity
1023 + */
1024 +static void add_snap_orphan(handle_t *handle, struct inode *pri, struct inode *ind)
1025 +{
1026 +       struct ext3_sb_info *sb = &pri->i_sb->u.ext3_sb;
1027 +       struct ext3_iloc iloc;
1028 +       
1029 +       if( ext3_get_inode_loc(ind, &iloc) ){
1030 +               snap_debug("--- get ind loc fail\n");
1031 +               brelse(iloc.bh);
1032 +               return;
1033 +       }
1034 +
1035 +       snap_debug("add new ind inode %lu into orphan list,"
1036 +                       " primary %lu, last orphan %u\n",
1037 +                       ind->i_ino, pri->i_ino, 
1038 +                       sb->s_es->s_last_snap_orphan);
1039 +       lock_super(pri->i_sb);
1040 +       iloc.raw_inode->i_next_snap_orphan = sb->s_es->s_last_snap_orphan;
1041 +       iloc.raw_inode->i_snap_primary = pri->i_ino;
1042 +       ext3_mark_inode_dirty(handle, ind);
1043 +
1044 +       ext3_journal_get_write_access(handle, sb->s_sbh);
1045 +       sb->s_es->s_last_snap_orphan = ind->i_ino;
1046 +       pri->i_sb->s_dirt = 1;
1047 +       ext3_journal_dirty_metadata(handle, sb->s_sbh);
1048 +       unlock_super(pri->i_sb);
1049 +       brelse(iloc.bh);
1050 +}
1051 +
1052 +/*
1053 + * counterpart of add_snap_orphan
1054 + */
1055 +static void remove_snap_orphan(handle_t *handle, struct inode *ind)
1056 +{
1057 +       struct ext3_sb_info *sb = &ind->i_sb->u.ext3_sb;
1058 +       struct inode *pre = NULL, *inode = NULL;
1059 +       struct ext3_iloc iloc, pre_iloc;
1060 +       ino_t ino;
1061 +
1062 +       lock_super(ind->i_sb);
1063 +       for(ino = sb->s_es->s_last_snap_orphan; ino; ){
1064 +               snap_debug("found an orphan, ino=%lu\n", ino);
1065 +               inode = iget( ind->i_sb, ino );
1066 +               if( !inode ){
1067 +                       snap_debug("iget %lu fail\n", ino);
1068 +                       break;
1069 +               }
1070 +               if( ext3_get_inode_loc(inode, &iloc) ){
1071 +                       snap_debug("get_inode_loc %lu fail\n", ino);
1072 +                       break;
1073 +               }
1074 +               if( ino == ind->i_ino ){
1075 +                       if( !pre ){
1076 +                               snap_debug("found at head of orphan chain\n");
1077 +                               ext3_journal_get_write_access(handle, sb->s_sbh);
1078 +                               sb->s_es->s_last_snap_orphan =
1079 +                                       iloc.raw_inode->i_next_snap_orphan;
1080 +                               ext3_journal_dirty_metadata(handle, sb->s_sbh);
1081 +                               snap_debug("set new last orphan: %u\n",
1082 +                                               sb->s_es->s_last_snap_orphan);
1083 +                               break;
1084 +                       }
1085 +                       else {
1086 +                               snap_debug("found in middle of orphan chain\n");
1087 +                               if( ext3_get_inode_loc(pre, &pre_iloc) ){
1088 +                                       snap_err("get pre_inode loc %lu fail\n", pre->i_ino);
1089 +                                       break;
1090 +                               }
1091 +                               pre_iloc.raw_inode->i_next_snap_orphan =
1092 +                                       iloc.raw_inode->i_next_snap_orphan;
1093 +                               ext3_mark_inode_dirty(handle, pre);
1094 +                               brelse(pre_iloc.bh);
1095 +                               break;
1096 +                       }
1097 +               }
1098 +               iput(pre);
1099 +               pre = inode;
1100 +               ino = iloc.raw_inode->i_next_snap_orphan;
1101 +               brelse(iloc.bh);
1102 +       }
1103 +       iput(pre);
1104 +       iput(inode);
1105 +       unlock_super(ind->i_sb);
1106 +       brelse(iloc.bh);
1107 +}
1108 +
1109 +/*
1110 + * FIXME: how about crashs again during recovery?
1111 + */
1112 +void snap_orphan_cleanup(struct super_block *sb)
1113 +{
1114 +       ino_t ind_ino, pri_ino;
1115 +       struct inode *ind = NULL, *pri = NULL;
1116 +       struct ext3_iloc ind_iloc;
1117 +
1118 +       if( (ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan) == 0 ){
1119 +               snap_debug("snap_orphan_cleanup: nothing to do\n");
1120 +               return;
1121 +       }
1122 +
1123 +       snap_debug("------ begin cleanup snap orphans ------\n");
1124 +       do{
1125 +               ind = iget( sb, ind_ino );
1126 +               if( !ind ){
1127 +                       snap_err("snap_orphan_cleanup: get "
1128 +                                       "ind %lu fail\n", ind_ino);
1129 +                       break;
1130 +               }
1131 +
1132 +               if( ext3_get_inode_loc(ind, &ind_iloc) ){
1133 +                       snap_err("snap_orphan_cleanup: get "
1134 +                                       "iloc %lu fail\n", ind_ino);
1135 +                       iput( ind );
1136 +                       break;
1137 +               }
1138 +
1139 +               ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan = 
1140 +                       ind_iloc.raw_inode->i_next_snap_orphan;
1141 +               pri_ino = ind_iloc.raw_inode->i_snap_primary;
1142 +
1143 +               pri = iget( sb, pri_ino );
1144 +               if( !pri ){
1145 +                       snap_err("snap_orphan_cleanup: get primary "
1146 +                                       "%lu fail\n", pri_ino);
1147 +                       iput( ind );
1148 +               }else 
1149 +                       restore_snap_inode(pri, ind);
1150 +       }while( ind_ino );
1151 +       snap_debug("------ end cleanup snap orphans ------\n");
1152 +
1153 +       sb->u.ext3_sb.s_es->s_last_snap_orphan = 0;
1154 +       sb->s_dirt = 1;
1155 +}
1156 +#endif
1157 +/*
1158 + * reserse operation of set_indirect()
1159 + * we should determine whether we had put pri into primary inode chain,
1160 + * if not, don't touch it
1161 + */
1162 +static void unset_indirect(handle_t *handle, struct inode *pri, struct inode *ind)
1163 +{
1164 +       char buf[EXT3_MAX_SNAP_DATA];
1165 +       struct snap_ea *snaps;
1166 +       int err, alone=1, index, found;
1167 +
1168 +       snap_debug("pri %lu, ind %lu\n", pri->i_ino, ind->i_ino);
1169 +       err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
1170 +                               EXT3_MAX_SNAP_DATA);
1171 +       if ( err < 0 ) {
1172 +               if( err == -ENOATTR ){
1173 +                       snap_debug("primary inode has not EA\n");
1174 +               }
1175 +               else{
1176 +                       snap_debug("get EA error on primary inode,"
1177 +                                       "returned value %d\n", err);
1178 +               }
1179 +               goto exit;
1180 +       }
1181 +
1182 +       /* find ind's item in the ea */
1183 +       snaps = (struct snap_ea*)buf;
1184 +       for(index=EXT3_MAX_SNAPS-1, found=-1; index>=0; index--) {
1185 +               if( snaps->ino[index] == ind->i_ino )
1186 +                       found = index;
1187 +               else if( snaps->ino[index] )
1188 +                       alone = 0;
1189 +       }
1190 +
1191 +       if(found >= 0) {
1192 +               snap_debug("remove from primary inode's EA\n");
1193 +               snaps->ino[found] = 0;
1194 +               snaps->parent_ino[found] = 0;
1195 +               ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1196 +                              buf, EXT3_MAX_SNAP_DATA, 0);
1197 +               if(alone) {
1198 +                       snap_debug("delete from primary inodes chain\n");
1199 +                       lock_list(pri->i_sb);
1200 +                       delete_cowed_ino_from_list(handle, pri);
1201 +                       unlock_list(pri->i_sb);
1202 +               }
1203 +       }else{
1204 +               snap_debug("didn't found ind in pri's EA, do nothing\n");
1205 +       }
1206 +
1207 +exit:
1208 +       return;
1209 +}
1210 +
1211 +
1212 +/*
1213 + * restore all data in @ind to @pri after free data blocks of @pri.
1214 + * then release @ind
1215 + */
1216 +static void restore_snap_inode(struct inode *pri, struct inode *ind)
1217 +{
1218 +       handle_t *handle;
1219 +       struct inode *tmp;
1220 +
1221 +       snap_debug("restore from indirect %lu to primary %lu\n",
1222 +                       ind->i_ino, pri->i_ino);
1223 +
1224 +       handle = ext3_journal_start(pri, SNAP_RESTOREORPHAN_TRANS_BLOCKS);
1225 +       if( !handle )
1226 +               return;
1227 +
1228 +       /* first: taken from pri's ea, or from fs-wide primary inode chain */
1229 +       unset_indirect(handle, pri, ind);
1230 +
1231 +       /* second: throw out half-copied data in pri */
1232 +       if( pri->i_blocks ){
1233 +               tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1234 +               if( !tmp ){
1235 +                       snap_debug("ext3_new_inode error\n");
1236 +                       goto exit;
1237 +               }
1238 +
1239 +               ext3_migrate_data(handle, tmp, pri);
1240 +               snap_debug("freeing half-copied %lu blocks\n", tmp->i_blocks );
1241 +               tmp->i_nlink = 0;
1242 +               iput( tmp );
1243 +       }
1244 +
1245 +       /* third: restore ind inode to pri inode */
1246 +       snap_debug("restore %lu blocks to primary inode %lu\n",
1247 +                       ind->i_blocks, pri->i_ino);
1248 +       ext3_migrate_data(handle, pri, ind);
1249 +
1250 +       /* final: delete ind inode */
1251 +       ind->i_nlink = 0;
1252 +       iput( ind );
1253 +       iput( pri );
1254 +
1255 +exit:
1256 +       ext3_journal_stop(handle, pri);
1257 +}
1258 +
1259 +static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
1260 +                               struct inode *src, int *has_orphan)
1261 +{
1262 +       unsigned long blocks, blk, cur_blks;
1263 +       int low_credits, save_ref;
1264 +
1265 +       blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
1266 +                       src->i_sb->s_blocksize_bits;
1267 +       low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
1268 +
1269 +       snap_debug("%lu blocks need to be copied,"
1270 +                       "low credits limit %d\n", blocks, low_credits);
1271 +       for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
1272 +               if (!ext3_bmap(src->i_mapping, blk))
1273 +                       continue;
1274 +               if(handle->h_buffer_credits <= low_credits) {
1275 +                       int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
1276 +                       if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
1277 +                               needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
1278 +                       if (journal_extend(handle, needed)) {
1279 +                               snap_debug("create_indirect:fail to extend "
1280 +                                               "journal, restart trans\n");
1281 +                               loopfail( 3 );
1282 +                               if(!*has_orphan) {
1283 +                                       snap_debug("add orphan ino %lu nlink %d to orphan list \n", 
1284 +                                                  dst->i_ino, dst->i_nlink); 
1285 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1286 +                                       add_snap_orphan(handle, dst, src);
1287 +#else
1288 +                                       ext3_orphan_add(handle, dst);
1289 +#endif
1290 +                                       *has_orphan = 1;
1291 +                               }
1292 +                               dst->u.ext3_i.i_disksize =
1293 +                                       blk * dst->i_sb->s_blocksize;
1294 +                               dst->i_blocks = cur_blks;
1295 +                               dst->i_mtime = CURRENT_TIME;
1296 +                               ext3_mark_inode_dirty(handle, dst);
1297 +
1298 +                               /*
1299 +                                * We can be sure the last handle was stoped
1300 +                                * ONLY if the handle's reference count is 1
1301 +                                */
1302 +                               save_ref = handle->h_ref;
1303 +                               handle->h_ref = 1;
1304 +                               if( ext3_journal_stop(handle, dst) ){
1305 +                                       snap_err("fail to stop journal\n");
1306 +                                       handle = NULL;
1307 +                                       break;
1308 +                               }
1309 +                               loopfail ( 4 );
1310 +                               handle = ext3_journal_start(dst,
1311 +                                               low_credits + needed);
1312 +                               if( !handle ){
1313 +                                       snap_err("fail to restart handle\n");
1314 +                                       break;
1315 +                               }
1316 +                               handle->h_ref = save_ref;
1317 +                       }
1318 +               }
1319 +               if (ext3_copy_block( dst, src, blk) < 0 )
1320 +                       break;
1321 +               cur_blks += dst->i_sb->s_blocksize / 512;
1322 +       }
1323 +       dst->i_size = dst->u.ext3_i.i_disksize = src->i_size;
1324 +
1325 +       return handle;
1326 +}
1327 +
1328 +static int ext3_set_generation(struct inode *inode, unsigned long gen)
1329 +{
1330 +       handle_t *handle;
1331 +       int err;
1332 +
1333 +       handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
1334 +
1335 +       err = ext3_xattr_set(handle, inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1336 +                            (char*)&gen, sizeof(int), 0);
1337 +       if (err < 0) {
1338 +               snap_err("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
1339 +               return err;
1340 +       }
1341 +       
1342 +       ext3_journal_stop(handle, inode);
1343 +       return 0;
1344 +}
1345 +
1346 +static int ext3_get_generation(struct inode *inode)
1347 +{
1348 +       int err, gen;
1349 +
1350 +       err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1351 +                            (char*)&gen, sizeof(gen));
1352 +       if (err < 0) {
1353 +               if (err == -ENODATA) {
1354 +                       return 0;
1355 +               } else {
1356 +                       snap_err("can not get generation from %lu \n", inode->i_ino);
1357 +                       return err;
1358 +               }
1359 +       }
1360 +       return gen;
1361 +}
1362 +/**
1363 + * ext3_create_indirect - copy data, attributes from primary to new indir inode
1364 + * @pri: primary (source) inode
1365 + * @index: index in snapshot table where indirect inode should be stored
1366 + * @delete: flag that the primary inode is being deleted
1367 + *
1368 + * We copy all of the data blocks from the @*src inode to the @*dst inode, as
1369 + * well as copying the attributes from @*src to @*dst.  If @delete == 1, then
1370 + * the primary inode will only be a redirector and will appear deleted.
1371 + *
1372 + * FIXME do we move EAs, only non-snap EAs, what?
1373 + * FIXME we could do readpage/writepage, but we would have to handle block
1374 + *       allocation then, and it ruins sparse files for 1k/2k filesystems,
1375 + *       at the expense of doing a memcpy.
1376 + */
1377 +
1378 +static struct inode *ext3_create_indirect(
1379 +                       struct inode *pri, 
1380 +                       int index,
1381 +                       unsigned int gen,    
1382 +                       ino_t parent_ino,
1383 +                       int del)
1384 +{
1385 +       struct inode *ind;
1386 +       handle_t *handle = NULL;
1387 +       int err = 0;
1388 +       int has_orphan = 0;
1389 +
1390 +       if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1391 +               printk( KERN_EMERG "TRY TO COW JOUNRAL\n");
1392 +               return NULL;
1393 +       }
1394 +       snap_debug("creating indirect inode for %lu at index %d, %s pri\n",
1395 +                       pri->i_ino, index, del ? "deleting" : "preserve");
1396 +
1397 +       ind = ext3_get_indirect(pri, NULL, index);
1398 +
1399 +       loopfail( 1 );
1400 +
1401 +       handle = ext3_journal_start(pri, SNAP_CREATEIND_TRANS_BLOCKS);
1402 +       if( !handle )
1403 +               return NULL;
1404 +       /* XXX ? We should pass an err argument to get_indirect and precisely
1405 +        * detect the errors, for some errors, we should exit right away.
1406 +        */
1407 +
1408 +       /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect, 
1409 +        * we just free the primary data blocks and mark this inode delete
1410 +        */
1411 +       if((del) && ind && !IS_ERR(ind)) {
1412 +               struct inode *tmp;
1413 +               /* for directory, we don't free the data blocks, 
1414 +                * or ext3_rmdir will report errors "bad dir, no data blocks" 
1415 +                */
1416 +               snap_debug("del==SNAP_DEL_PRI_WITH_IND && ind\n");
1417 +               if(!S_ISDIR(pri->i_mode)) {     
1418 +                       /*Here delete the data of that pri inode.
1419 +                        * FIXME later, should throw the blocks of 
1420 +                        * primary inode directly
1421 +                        */
1422 +                       tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1423 +                       if(tmp) {
1424 +                               down(&tmp->i_sem);
1425 +                               ext3_migrate_data(handle, tmp, pri);
1426 +                               up(&tmp->i_sem);
1427 +                               tmp->i_nlink = 0;
1428 +                               iput(tmp);      
1429 +                       }
1430 +                       else 
1431 +                               snap_err("ext3_new_inode error\n");
1432 +
1433 +                       pri->i_nlink = 1;
1434 +               }
1435 +
1436 +               pri->u.ext3_i.i_dtime = CURRENT_TIME;
1437 +               ext3_mark_inode_dirty(handle, pri);
1438 +               err = 0;
1439 +               goto exit;
1440 +       }
1441 +
1442 +       if (ind && !IS_ERR(ind)) {
1443 +               snap_debug("existing indirect ino %lu for %lu: index %d\n",
1444 +                         ind->i_ino, pri->i_ino, index);
1445 +               err = 0;
1446 +               goto exit;
1447 +       }
1448 +       /* XXX: check this, ext3_new_inode, the first arg should be "dir" */ 
1449 +       ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1450 +       if (!ind)
1451 +               goto exit;
1452 +
1453 +       loopfail( 2 );
1454 +
1455 +       snap_debug("got new inode %lu\n", ind->i_ino);
1456 +       ind->i_rdev = pri->i_rdev;
1457 +       ind->i_op = pri->i_op;
1458 +       ext3_set_generation(ind, (unsigned long)gen);
1459 +       /* If we are deleting the primary inode, we want to ensure that it is
1460 +        * written to disk with a non-zero link count, otherwise the next iget
1461 +        * and iput will mark the inode as free (which we don't want, we want
1462 +        * it to stay a redirector).  We fix this in ext3_destroy_indirect()
1463 +        * when the last indirect inode is removed.
1464 +        *
1465 +        * We then do what ext3_delete_inode() does so that the metadata will
1466 +        * appear the same as a deleted inode, and we can detect it later.
1467 +        */
1468 +       if (del) {
1469 +               snap_debug("deleting primary inode\n");
1470 +               
1471 +               down(&ind->i_sem);
1472 +               err = ext3_migrate_data(handle, ind, pri);
1473 +               if (err)
1474 +                       goto exit_unlock;
1475 +
1476 +               err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1477 +               if (err)
1478 +                       goto exit_unlock;
1479 +
1480 +               /* XXX for directory, we copy the block back 
1481 +                * or ext3_rmdir will report errors "bad dir, no data blocks" 
1482 +                */
1483 +               if( S_ISDIR(pri->i_mode)) {
1484 +                       handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1485 +                       if(!handle) {
1486 +                               err = -EINVAL;
1487 +                               goto exit_unlock;
1488 +                       }
1489 +               }
1490 +
1491 +               pri->u.ext3_i.i_flags |= EXT3_DEL_FL;
1492 +               ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1493 +               if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
1494 +               pri->u.ext3_i.i_dtime = CURRENT_TIME;
1495 +               //pri->u.ext3_i.i_generation++;
1496 +               ext3_mark_inode_dirty(handle, pri);
1497 +               ext3_mark_inode_dirty(handle, ind);
1498 +               up(&ind->i_sem);
1499 +       } else {
1500 +               down(&ind->i_sem);
1501 +               err = ext3_migrate_data(handle, ind, pri);
1502 +               if (err)
1503 +                       goto exit_unlock;
1504 +
1505 +               /* for regular files we do blocklevel COW's maybe */
1506 +               if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
1507 +                       && S_ISREG(pri->i_mode)) {
1508 +
1509 +                       snap_debug("ino %lu, do block cow\n",pri->i_ino);
1510 +                       /* because after migrate_data , pri->i_size is 0 */
1511 +                       pri->i_size = ind->i_size;
1512 +               }
1513 +               else {
1514 +                       int bpib = pri->i_sb->s_blocksize >> 9;
1515 +                       snap_debug("ino %lu, do file cow\n", pri->i_ino);
1516 +
1517 +                       /* XXX: can we do this better? 
1518 +                        * If it's a fast symlink, we should copy i_data back!
1519 +                        * The criteria to determine a fast symlink is:
1520 +                        * 1) it's a link and its i_blocks is 0
1521 +                        * 2) it's a link and its i_blocks is bpib ( the case 
1522 +                        *    it has been cowed and has ea )
1523 +                        */
1524 +                        if( S_ISLNK(ind->i_mode) &&
1525 +                          ((ind->i_blocks == 0) || (ext3_has_ea(ind) && ind->i_blocks == bpib))) {
1526 +                               snap_debug("ino %lu is fast symlink\n", pri->i_ino);
1527 +                               memcpy(EXT3_I(pri)->i_data, EXT3_I(ind)->i_data,
1528 +                                      sizeof(EXT3_I(ind)->i_data));
1529 +                               pri->i_size = ind->i_size;
1530 +                       }
1531 +                       else {
1532 +                               handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1533 +                               if (!handle)
1534 +                                       goto exit_unlock;
1535 +                       }
1536 +               }
1537 +               /* set cow flag for ind */
1538 +               ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1539 +               pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
1540 +
1541 +               ext3_mark_inode_dirty(handle, pri);
1542 +               ext3_mark_inode_dirty(handle, ind);
1543 +
1544 +               err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1545 +               if (err)
1546 +                       goto exit_unlock;
1547 +
1548 +               up(&ind->i_sem);
1549 +       }
1550 +
1551 +       if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
1552 +                                    EXT3_FEATURE_COMPAT_SNAPFS)) {
1553 +               lock_super(pri->i_sb);
1554 +               ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
1555 +               pri->i_sb->u.ext3_sb.s_es->s_feature_compat |=
1556 +                       cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
1557 +               ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
1558 +               pri->i_sb->s_dirt = 1;
1559 +               unlock_super(pri->i_sb);
1560 +       }
1561 +       if (has_orphan) {
1562 +               snap_debug("del %lu nlink %d from orphan list\n", 
1563 +                          ind->i_ino, ind->i_nlink);
1564 +#ifdef EXT3_ENABLE_SNAP_ORPHAN 
1565 +               remove_snap_orphan(handle, ind);
1566 +#else
1567 +               ext3_orphan_del(handle, ind);
1568 +#endif
1569 +       }
1570 +       ext3_journal_stop(handle, pri);
1571 +
1572 +       loopfail( 5 );
1573 +
1574 +       return ind;
1575 +
1576 +exit_unlock:
1577 +       up(&ind->i_sem);
1578 +       ind->i_nlink = 0;
1579 +exit:
1580 +       if (has_orphan) {
1581 +               snap_debug("del %lu nlink %d from orphan list\n", 
1582 +                          ind->i_ino, ind->i_nlink);
1583 +#ifdef EXT3_ENABLE_SNAP_ORPHAN 
1584 +               remove_snap_orphan(handle, ind);
1585 +#else
1586 +               ext3_orphan_del(handle, ind);
1587 +#endif
1588 +       }
1589 +       iput(ind);
1590 +       ext3_journal_stop(handle, pri);
1591 +       if (err)
1592 +               snap_err("exiting with error %d\n", err);
1593 +       return NULL;
1594 +}
1595 +
1596 +
1597 +/* The following functions are used by destroy_indirect */
1598 +#define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
1599 +#define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
1600 +
1601 +static inline int block_bmap (struct buffer_head * bh, int nr)
1602 +{
1603 +        int tmp;
1604 +
1605 +        if (!bh)
1606 +                return 0;
1607 +        tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
1608 +        brelse (bh);
1609 +        return tmp;
1610 +}
1611 +
1612 +static inline int block_setbmap (handle_t *handle, struct buffer_head * bh, int nr, int physical)
1613 +{
1614 +
1615 +       if (!bh)
1616 +               return 0;
1617 +       ext3_journal_get_write_access(handle, bh);
1618 +       ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
1619 +       ext3_journal_dirty_metadata(handle, bh);
1620 +       brelse (bh);
1621 +       return 1;
1622 +}
1623 +
1624 +static int ext3_migrate_block (handle_t *handle, struct inode * dst, struct inode *src, int block)
1625 +{
1626 +       int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
1627 +       int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
1628 +       int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
1629 +       unsigned long blksz = src->i_sb->s_blocksize;
1630 +       kdev_t ddev = dst->i_dev;
1631 +       kdev_t sdev = src->i_dev;
1632 +       int physical = 0;
1633 +
1634 +       if (block < 0) {
1635 +               ext3_warning (src->i_sb, "ext3_migrate_block", "block < 0");
1636 +               return 0;
1637 +       }
1638 +       if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
1639 +               (1 << (addr_per_block_bits * 2)) +
1640 +               ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
1641 +               ext3_warning (src->i_sb, "ext3_migrate_block", "block > big");
1642 +               return 0;
1643 +       }
1644 +       /* EXT3_NDIR_BLOCK */
1645 +       if (block < EXT3_NDIR_BLOCKS) {
1646 +               if( inode_bmap(dst, block) )    return 0;
1647 +               else {
1648 +                       if( (physical = inode_bmap(src, block)) ) {
1649 +                               inode_setbmap (dst, block, physical);
1650 +                               inode_setbmap (src, block, 0);
1651 +                               return 1;
1652 +                       }
1653 +                       else 
1654 +                               return 0;
1655 +               }
1656 +       }
1657 +       /* EXT3_IND_BLOCK */
1658 +       block -= EXT3_NDIR_BLOCKS;
1659 +       if (block < addr_per_block) {
1660 +               i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1661 +
1662 +               if (!i1_d) {
1663 +                       physical = inode_bmap(src, EXT3_IND_BLOCK);
1664 +                       if( physical ) {
1665 +                               inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1666 +                               inode_setbmap (src, EXT3_IND_BLOCK, 0);
1667 +                               return 1;
1668 +                       }
1669 +                       else 
1670 +                               return 0;
1671 +               }
1672 +               if( block_bmap (bread (ddev, i1_d, blksz), block )) 
1673 +                       return 0;
1674 +
1675 +               i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1676 +               if( !i1_s)      return 0;
1677 +
1678 +               physical = block_bmap ( bread (sdev, i1_s, blksz), block );
1679 +
1680 +               if( physical) {
1681 +                       block_setbmap(handle, bread(ddev, i1_d, blksz),block,physical); 
1682 +                       block_setbmap(handle, bread(sdev, i1_s, blksz), block, 0);
1683 +                       return 1; 
1684 +               }
1685 +               else 
1686 +                       return 0;
1687 +       }
1688 +       /* EXT3_DIND_BLOCK */
1689 +       block -= addr_per_block;
1690 +       if (block < (1 << (addr_per_block_bits * 2))) {
1691 +               i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1692 +               i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1693 +               if (!i1_d) {
1694 +                       if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1695 +                               inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1696 +                               inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1697 +                               return 1;
1698 +                       }
1699 +                       else 
1700 +                               return 0;
1701 +               }
1702 +               i2_d = block_bmap (bread (ddev, i1_d, blksz),
1703 +                               block >> addr_per_block_bits);
1704 +
1705 +               if (!i2_d) {
1706 +                       
1707 +                       if( !i1_s)      return 0;
1708 +
1709 +                       physical = block_bmap (bread (sdev, i1_s, blksz),
1710 +                               block >> addr_per_block_bits);
1711 +                       if( physical) {
1712 +                               block_setbmap (handle, bread (ddev, i1_d, blksz), 
1713 +                                       block >> addr_per_block_bits, physical);
1714 +                               block_setbmap (handle, bread (sdev, i1_s, blksz), 
1715 +                                       block >> addr_per_block_bits, 0);
1716 +                               return 1;
1717 +                       }
1718 +                       else
1719 +                               return 0;
1720 +               }
1721 +               physical = block_bmap (bread (ddev, i2_d,
1722 +                                         blksz),
1723 +                                  block & (addr_per_block - 1));
1724 +               if(physical) 
1725 +                               return 0;
1726 +               else {
1727 +                       i2_s =  block_bmap (bread (sdev, i1_s,
1728 +                                      blksz),
1729 +                               block >> addr_per_block_bits);
1730 +                       if(!i2_s)       return 0;
1731 +       
1732 +                       physical = block_bmap(bread (sdev, i2_s,
1733 +                                         blksz),
1734 +                                  block & (addr_per_block - 1));
1735 +                       if(physical) {
1736 +                               block_setbmap(handle, bread (ddev, i2_d, blksz),
1737 +                                  block & (addr_per_block - 1), physical);
1738 +                               block_setbmap(handle, bread (sdev, i2_s, blksz),
1739 +                                  block & (addr_per_block - 1), 0);
1740 +                               return 1;
1741 +                       }
1742 +                       else 
1743 +                               return 0;
1744 +               }
1745 +               
1746 +       }
1747 +       /* EXT3_TIND_BLOCK */
1748 +       block -= (1 << (addr_per_block_bits * 2));
1749 +       i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1750 +       i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1751 +       if (!i1_d) {
1752 +               if( (physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1753 +                               inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1754 +               else 
1755 +                       return 0;
1756 +       }
1757 +       i2_d = block_bmap (bread (ddev, i1_d, blksz),
1758 +                       block >> (addr_per_block_bits * 2));
1759 +
1760 +       if(i1_s) i2_s = block_bmap (bread (sdev, i1_s, blksz),
1761 +                       block >> (addr_per_block_bits * 2));
1762 +
1763 +       if (!i2_d) {
1764 +
1765 +               if( !i1_s)      return 0;
1766 +               
1767 +               physical = block_bmap (bread (sdev, i1_s, blksz),
1768 +                       block >> (addr_per_block_bits * 2));
1769 +               if(physical) {
1770 +                       block_setbmap (handle, bread (ddev, i1_d, blksz),
1771 +                               block >> (addr_per_block_bits * 2), physical);
1772 +                       block_setbmap (handle, bread (sdev, i1_s, blksz),
1773 +                               block >> (addr_per_block_bits * 2), 0);
1774 +                       return 1;
1775 +               }
1776 +               else
1777 +                       return 0;
1778 +       }
1779 +       i3_d = block_bmap (bread (ddev, i2_d, blksz),
1780 +                       (block >> addr_per_block_bits) & (addr_per_block - 1));
1781 +       if( i2_s) i3_s = block_bmap (bread (sdev, i2_s, blksz),
1782 +                       (block >> addr_per_block_bits) & (addr_per_block - 1));
1783 +       
1784 +       if (!i3_d) {
1785 +               if (!i2_s)      return 0;       
1786 +               physical = block_bmap (bread (sdev, i2_s, blksz),
1787 +                       (block >> addr_per_block_bits) & (addr_per_block - 1));
1788 +               if( physical) {
1789 +                       block_setbmap (handle, bread (ddev, i2_d, blksz),
1790 +                       (block >> addr_per_block_bits) & (addr_per_block - 1), 
1791 +                       physical);
1792 +                       block_setbmap (handle, bread (sdev, i2_s, blksz),
1793 +                       (block >> addr_per_block_bits) & (addr_per_block - 1),
1794 +                       0);
1795 +                       return 1;
1796 +               }
1797 +               else
1798 +                       return 0;
1799 +       }
1800 +       physical = block_bmap (bread (ddev, i3_d, blksz),
1801 +                          block & (addr_per_block - 1)) ;
1802 +       if(physical) return 0;
1803 +       else {
1804 +               if(!i3_s)       return 0;       
1805 +               physical =  block_bmap (bread (sdev, i3_s, blksz),
1806 +                          block & (addr_per_block - 1)) ;
1807 +               if( physical) {
1808 +                       block_setbmap (handle, bread (ddev, i3_d, blksz),
1809 +                          block & (addr_per_block - 1), physical); 
1810 +                       block_setbmap (handle, bread (sdev, i3_s, blksz),
1811 +                          block & (addr_per_block - 1), 0); 
1812 +                       return 1;
1813 +               }
1814 +               else
1815 +                       return 0; 
1816 +       }
1817 +}
1818 +
1819 +/* Generate i_blocks from blocks for an inode .
1820 + * We also calculate EA block here.
1821 + */
1822 +static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1823 +{
1824 +       /* 512 byte disk blocks per inode block */
1825 +       int bpib = inode->i_sb->s_blocksize >> 9;
1826 +       int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1827 +       unsigned long i_blocks = 0;
1828 +       int i=0;
1829 +       int j=0;
1830 +       int meta_blocks = 0;
1831 +
1832 +       if( !inode )    return 0;
1833 +
1834 +       if( blocks < 0 ) {
1835 +               /* re-calculate blocks here */  
1836 +               blocks = (inode->i_size + inode->i_sb->s_blocksize-1) 
1837 +                       >> inode->i_sb->s_blocksize_bits;
1838 +       }
1839 +
1840 +       /* calculate data blocks */
1841 +       for(i = 0; i < blocks; i++ ) {
1842 +               if(ext3_bmap(inode->i_mapping, i))  
1843 +                       i_blocks += bpib;
1844 +       }
1845 +       /* calculate meta blocks */
1846 +       blocks -= EXT3_NDIR_BLOCKS;
1847 +       if( blocks > 0 ) {
1848 +               meta_blocks++;
1849 +               blocks -= addr_per_block;
1850 +       }
1851 +       if( blocks > 0 ) meta_blocks++;
1852 +       i=0;
1853 +       while( (blocks > 0) && (i < addr_per_block) ) {
1854 +               meta_blocks++;
1855 +               blocks -= addr_per_block;
1856 +               i++;
1857 +       }
1858 +       if ( blocks > 0 ) meta_blocks += 2;
1859 +       i=0;
1860 +       j=0;
1861 +       while( blocks > 0) {
1862 +               meta_blocks++;
1863 +               blocks -= addr_per_block;
1864 +               i++;
1865 +               if(i >= addr_per_block  ) {
1866 +                       i=0;
1867 +                       j++;
1868 +               }
1869 +               if( j >= addr_per_block) {
1870 +                       j=0;
1871 +                       meta_blocks++;
1872 +               }
1873 +       }
1874 +       /* calculate EA blocks */
1875 +       if( ext3_has_ea (inode) )       meta_blocks++;
1876 +
1877 +       i_blocks += meta_blocks * bpib;
1878 +       snap_debug("ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1879 +       return i_blocks;
1880 +}
1881 +
1882 +/**
1883 + * ext3_destroy_indirect - delete an indirect inode from the table
1884 + * @pri: primary inode
1885 + * @ind: indirect inode
1886 + * @index: index of inode that should be deleted
1887 + *
1888 + * We delete the @*ind inode, and remove it from the snapshot table.  If @*ind
1889 + * is NULL, we use the inode at @index.
1890 + */
1891 +static int ext3_destroy_indirect(struct inode *pri, int index, 
1892 +                               struct inode *next_ind)
1893 +{
1894 +       char buf[EXT3_MAX_SNAP_DATA];
1895 +       struct snap_ea *snaps;
1896 +       struct inode *ind;
1897 +       int save = 0;
1898 +       int i=0;
1899 +       int err = 0;
1900 +       handle_t *handle=NULL;
1901 +       time_t ctime;
1902 +
1903 +       if (index < 0 || index > EXT3_MAX_SNAPS)
1904 +               return 0;
1905 +
1906 +       if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1907 +               snap_err("TRY TO DESTROY JOURNAL'S IND\n");
1908 +               return -EINVAL;
1909 +       }
1910 +
1911 +       err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1912 +                            buf, EXT3_MAX_SNAP_DATA);
1913 +       if (err < 0) {
1914 +               snap_err("inode %lu attribute read error\n", pri->i_ino);
1915 +               return err;
1916 +       }
1917 +       
1918 +       snaps = (struct snap_ea *)buf;
1919 +       if ( !snaps->ino[index] ) {
1920 +               snap_err("for pri ino %lu, index %d, redirect ino is 0\n",
1921 +                               pri->i_ino, index);     
1922 +               return -EINVAL;
1923 +       }
1924 +
1925 +       snap_debug("for pri ino %lu, reading inode %lu at index %d\n", 
1926 +               pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1927 +
1928 +       ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1929 +
1930 +       if ( !ind || IS_ERR(ind) || is_bad_inode(ind)) 
1931 +               return  -EINVAL;
1932 +
1933 +       snap_debug("iget ind %lu, ref count = %d\n", 
1934 +                  ind->i_ino, atomic_read(&ind->i_count));
1935 +
1936 +       handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
1937 +       if (!handle) {
1938 +               iput(ind);
1939 +               return -EINVAL;
1940 +       }
1941 +       /* if it's block level cow, first copy the blocks back */       
1942 +       if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1943 +           S_ISREG(pri->i_mode)) {
1944 +
1945 +               int blocks;
1946 +               if (!next_ind) {        
1947 +                       next_ind = pri;
1948 +                       down(&ind->i_sem);
1949 +               } else {
1950 +                       double_down(&next_ind->i_sem, &ind->i_sem);
1951 +               }
1952 +               blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1) 
1953 +                               >> next_ind->i_sb->s_blocksize_bits;
1954 +#define FAST_MIGRATE_BLOCK
1955 +#ifdef FAST_MIGRATE_BLOCK
1956 +               snap_debug("migrate block back from ino %lu to %lu\n",
1957 +                               ind->i_ino, next_ind->i_ino);
1958 +
1959 +               for(i = 0; i < blocks; i++) {
1960 +                       if( ext3_bmap(next_ind->i_mapping, i) ) 
1961 +                               continue;
1962 +                       if( !ext3_bmap(ind->i_mapping, i) ) 
1963 +                               continue;
1964 +                       ext3_migrate_block(handle, next_ind, ind, i) ;
1965 +               }
1966 +               /* Now re-compute the i_blocks */
1967 +               /* XXX shall we take care of ind here? probably not */
1968 +               next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1969 +               ext3_mark_inode_dirty(handle, next_ind);
1970 +
1971 +#else
1972 +               for (i = 0; i < blocks; i++) {
1973 +                       if (ext3_bmap(next_ind->i_mapping, i))  
1974 +                               continue;
1975 +                       if (ext3_copy_block(next_ind, ind, i ) < 0)     
1976 +                               break;
1977 +               }
1978 +               ext3_mark_inode_dirty(handle, next_ind);
1979 +#endif 
1980 +               if (next_ind == pri) 
1981 +                       up(&ind->i_sem);
1982 +               else 
1983 +                       double_up(&next_ind->i_sem, &ind->i_sem);
1984 +
1985 +       }
1986 +       
1987 +       snap_debug("delete indirect ino %lu\n", ind->i_ino);
1988 +       snap_debug("iput ind %lu, ref count = %d\n", 
1989 +                   ind->i_ino, atomic_read(&ind->i_count));
1990 +       ind->i_nlink = 0;
1991 +       iput (ind);
1992 +
1993 +       snaps->ino[index] = cpu_to_le32(0);
1994 +       for (i = 0; i < EXT3_MAX_SNAPS; i++)
1995 +               save += snaps->ino[i];
1996 +
1997 +       if(!save) {     
1998 +               lock_list(pri->i_sb);
1999 +               delete_cowed_ino_from_list(handle, pri);
2000 +               unlock_list(pri->i_sb);
2001 +       }
2002 +
2003 +       /* if there are no cowed inode left, then remove snapfs feature */
2004 +       if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2005 +
2006 +               lock_super(pri->i_sb);
2007 +
2008 +               ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
2009 +               if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2010 +                                     EXT3_FEATURE_COMPAT_SNAPFS)) {
2011 +                       pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2012 +                               cpu_to_le32(~EXT3_FEATURE_COMPAT_SNAPFS);
2013 +               }
2014 +               /* clean up block level cow feature */
2015 +               if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2016 +                       EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2017 +                               pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2018 +                                       cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2019 +                 }
2020 +               /* XXX clean the extended attribute feature,
2021 +                * this is not safe, find a better way
2022 +                */
2023 +                if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2024 +                       EXT3_FEATURE_COMPAT_EXT_ATTR)) {
2025 +                               pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2026 +                                       cpu_to_le32(~EXT3_FEATURE_COMPAT_EXT_ATTR);
2027 +                }
2028 +
2029 +               ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
2030 +               pri->i_sb->s_dirt = 1;
2031 +               unlock_super(pri->i_sb);
2032 +        }
2033 +
2034 +       /*
2035 +        * If we are deleting the last indirect inode, and the primary inode
2036 +        * has already been deleted, then mark the primary for deletion also.
2037 +        * Otherwise, if we are deleting the last indirect inode remove the
2038 +        * snaptable from the inode.    XXX
2039 +        */
2040 +       if (!save && pri->u.ext3_i.i_dtime) {
2041 +               snap_debug("deleting primary %lu\n", pri->i_ino);
2042 +               pri->i_nlink = 0;
2043 +               /* reset err to 0 now */
2044 +               err = 0;
2045 +       } else {
2046 +               snap_debug("%s redirector table\n",
2047 +                               save ? "saving" : "deleting");
2048 +               /* XXX: since set ea will modify i_ctime of pri, 
2049 +                       so save/restore i_ctime. Need this necessary ? */
2050 +               ctime = pri->i_ctime;   
2051 +               err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2052 +                                          save ? buf : NULL, EXT3_MAX_SNAP_DATA, 0);
2053 +               pri->i_ctime = ctime;
2054 +               ext3_mark_inode_dirty(handle, pri);
2055 +       }
2056 +       ext3_journal_stop(handle, pri);
2057 +       return err;
2058 +}
2059 +
2060 +/* restore a primary inode with the indirect inode at index */
2061 +static int ext3_restore_indirect(struct inode *pri, int index)
2062 +{
2063 +       struct inode *ind;
2064 +       struct inode *tmp;
2065 +       int err = 0;
2066 +       handle_t *handle = NULL;
2067 +
2068 +       if (index < 0 || index > EXT3_MAX_SNAPS)
2069 +               return -EINVAL;
2070 +
2071 +       if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2072 +               printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2073 +               return -EINVAL;
2074 +       }
2075 +       snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2076 +
2077 +       ind = ext3_get_indirect(pri, NULL, index);
2078 +
2079 +       if ( !ind ) 
2080 +               return -EINVAL;
2081 +
2082 +       snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2083 +
2084 +       handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
2085 +       if( !handle )
2086 +               return -EINVAL;
2087 +       /* first destroy all the data blocks in primary inode */
2088 +       /* XXX: check this, ext3_new_inode, the first arg should be "dir" */ 
2089 +       tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
2090 +       if(tmp) {
2091 +               double_down(&pri->i_sem, &tmp->i_sem);
2092 +               ext3_migrate_data(handle, tmp, pri);
2093 +               double_up(&pri->i_sem, &tmp->i_sem);
2094 +
2095 +               tmp->i_nlink = 0;
2096 +               iput(tmp);      
2097 +       }
2098 +       else    
2099 +               snap_err("restore_indirect, new_inode err\n");
2100 +       
2101 +       double_down(&pri->i_sem, &ind->i_sem);
2102 +       ext3_migrate_data(handle, pri, ind);
2103 +       /* clear the cow flag for pri because ind has it */
2104 +       pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
2105 +       ext3_mark_inode_dirty(handle, pri);
2106 +       double_up(&pri->i_sem, &ind->i_sem);
2107 +       iput(ind);
2108 +
2109 +//     ext3_destroy_indirect(pri, index);
2110 +
2111 +       ext3_journal_stop(handle, pri);
2112 +       return err;
2113 +}
2114 +
2115 +
2116 +/**
2117 + * ext3_snap_iterate - iterate through all of the inodes
2118 + * @sb: filesystem superblock
2119 + * @repeat: pointer to function called on each valid inode
2120 + * @start: inode to start iterating at
2121 + * @priv: private data to the caller/repeat function
2122 + *
2123 + * If @start is NULL, then we do not return an inode pointer.  If @*start is
2124 + * NULL, then we start at the beginning of the filesystem, and iterate over
2125 + * all of the inodes in the system.  If @*start is non-NULL, then we start
2126 + * iterating at this inode.
2127 + *
2128 + * We call the repeat function for each inode that is in use.  The repeat
2129 + * function must check if this is a redirector (with is_redirector) if it
2130 + * only wants to operate on redirector inodes.  If there is an error or
2131 + * the repeat function returns non-zero, we return the last inode operated
2132 + * on in the @*start parameter.  This allows the caller to restart the
2133 + * iteration at this inode if desired, by returning a positive value.
2134 + * Negative return values indicate an error.
2135 + *
2136 + * NOTE we cannot simply traverse the existing filesystem tree from the root
2137 + *      inode, as there may be disconnected trees from deleted files/dirs
2138 + *
2139 + * FIXME If there was a list of inodes with EAs, we could simply walk the list
2140 + * intead of reading every inode.  This is an internal implementation issue.
2141 + */
2142 +
2143 +static int ext3_iterate_all(struct super_block *sb,
2144 +                       int (*repeat)(struct inode *inode, void *priv),
2145 +                       struct inode **start, void *priv)
2146 +{
2147 +       struct inode *tmp = NULL;
2148 +       int gstart, gnum;
2149 +       ino_t istart, ibase;
2150 +       int err = 0;
2151 +
2152 +       if (!start)
2153 +               start = &tmp;
2154 +       if (!*start) {
2155 +               *start = iget(sb, EXT3_ROOT_INO);
2156 +               if (!*start) {
2157 +                       err = -ENOMEM;
2158 +                       goto exit;
2159 +               }
2160 +               if (is_bad_inode(*start)) {
2161 +                       err = -EIO;
2162 +                       goto exit;
2163 +               }
2164 +       }
2165 +       if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
2166 +               snap_debug("invalid starting inode %ld\n",(*start)->i_ino);
2167 +               err = -EINVAL;
2168 +               goto exit;
2169 +       }
2170 +       if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2171 +               if ((err = (*repeat)(*start, priv) != 0))
2172 +                       goto exit;
2173 +               iput(*start);
2174 +               *start = iget(sb, EXT3_FIRST_INO(sb));
2175 +               if (!*start) {
2176 +                       err = -ENOMEM;
2177 +                       goto exit;
2178 +               }
2179 +               if (is_bad_inode(*start)) {
2180 +                       err = -EIO;
2181 +                       goto exit;
2182 +               }
2183 +       }
2184 +
2185 +       gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
2186 +       istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
2187 +       ibase = gstart * EXT3_INODES_PER_GROUP(sb);
2188 +       for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
2189 +            gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
2190 +               struct ext3_group_desc * gdp;
2191 +               int bitmap_nr;
2192 +               char *bitmap;
2193 +               int ibyte;
2194 +
2195 +               gdp = ext3_get_group_desc (sb, gnum, NULL);
2196 +               if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
2197 +                   EXT3_INODES_PER_GROUP(sb))
2198 +                       continue;
2199 +
2200 +               bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2201 +               if (bitmap_nr < 0)
2202 +                       continue;
2203 +
2204 +               bitmap = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr]->b_data;
2205 +               for (ibyte = istart >> 3;
2206 +                    ibyte < EXT3_INODES_PER_GROUP(sb) >> 3;
2207 +                    ibyte++)
2208 +               {
2209 +                       int i;
2210 +                       int bit;
2211 +
2212 +                       if (!bitmap[ibyte])
2213 +                               continue;
2214 +
2215 +                       /* FIXME need to verify if bit endianness will
2216 +                        *       work properly here for all architectures.
2217 +                        */
2218 +                       for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2219 +                               ino_t ino = ibase + (ibyte << 3) + i;
2220 +
2221 +                               if ((bitmap[ibyte] & bit) == 0)
2222 +                                       continue;
2223 +                               if (*start) {
2224 +                                       if (ino < (*start)->i_ino)
2225 +                                               continue;
2226 +                               } else {
2227 +                                       *start = iget(sb, ino);
2228 +                                       if (!*start) {
2229 +                                               err = -ENOMEM;
2230 +                                               goto exit;
2231 +                                       }
2232 +                                       if (is_bad_inode(*start)) {
2233 +                                               err = -EIO;
2234 +                                               goto exit;
2235 +                                       }
2236 +                               }
2237 +                               if ((err = (*repeat)(*start, priv)) != 0)
2238 +                                       goto exit;
2239 +                               iput(*start);
2240 +                               *start = NULL;
2241 +                       }
2242 +               }
2243 +               istart = 0;
2244 +       }
2245 +exit:
2246 +       iput(tmp);
2247 +       return err;
2248 +}
2249 +
2250 +static int ext3_iterate(struct super_block *sb,
2251 +                       int (*repeat)(struct inode *inode, void *priv),
2252 +                       struct inode **start, void *priv, int flag)
2253 +{
2254 +       switch(flag) {
2255 +               case SNAP_ITERATE_ALL_INODE:
2256 +                       return ext3_iterate_all (sb, repeat, start, priv);
2257 +
2258 +               case SNAP_ITERATE_COWED_INODE:
2259 +                       return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2260 +
2261 +               default:
2262 +                       return -EINVAL;
2263 +       }
2264 +}
2265 +
2266 +static int find_snap_meta_index(
2267 +       struct table_snap_meta_data *snap_meta,
2268 +       char                        *name)
2269 +{
2270 +       int i;
2271 +
2272 +       /* table max length is null*/
2273 +       for( i = 0; i < TABLE_ITEM_COUNT; i++){
2274 +               /*compare name Max name Length 15*/
2275 +               if (snap_meta->array[i].name[0]){
2276 +                       if(!strncmp(snap_meta->array[i].name, name, strlen(name)))
2277 +                               return i;
2278 +               }
2279 +       }
2280 +       return -1; /* can not find */
2281 +}
2282 +
2283 +int set_snap_meta_index(
2284 +       struct table_snap_meta_data *snap_meta,
2285 +       char                        *name,
2286 +       int                          size)
2287 +{
2288 +       int i;
2289 +
2290 +       for( i = 0; i < TABLE_ITEM_COUNT; i++){
2291 +               /*compare name Max name Length 15*/
2292 +               if (! snap_meta->array[i].name[0]){
2293 +                       strcpy(snap_meta->array[i].name, name);
2294 +                       snap_meta->count ++;
2295 +                       snap_meta->array[i].start = i * TABLE_ITEM_SIZE + 1;
2296 +                       snap_meta->array[i].len   = size;
2297 +                       return i;
2298 +               }
2299 +       }
2300 +       return -1; /* can not find */
2301 +}
2302 +
2303 +static int ext3_get_meta_attr(struct super_block *sb, 
2304 +                             char* name, char* buf, 
2305 +                             int *size)
2306 +{
2307 +        ino_t                          ino;
2308 +        struct inode                   *inode;
2309 +       struct buffer_head              *bh = NULL;
2310 +       struct table_snap_meta_data     *s_attr;
2311 +       unsigned long                   map_len = 0,  left_size;
2312 +        int                            i, error = 0, index = 0;
2313 +       
2314 +       ino = SB_SNAPTABLE_INO(sb);     
2315 +       if (ino == 0){
2316 +               snap_err("No table file \n");
2317 +               return  -ENODATA;
2318 +       } 
2319 +       inode = iget(sb, ino);
2320 +        if(!inode || is_bad_inode(inode)){
2321 +                snap_err("unable to get table ino %lu\n", ino);
2322 +                error = -ENOENT;
2323 +                       goto out_iput; 
2324 +       }
2325 +       /*read the table from the table inode*/
2326 +       bh = ext3_bread(NULL, inode, 0, 0, &error);
2327 +       if (!bh) {
2328 +               snap_err("read table ino %lu, error %d\n", ino, error);
2329 +               error = -ENODATA;
2330 +               goto out_iput;
2331 +       }
2332 +       s_attr = (struct table_snap_meta_data *)(bh->b_data);
2333 +       index = find_snap_meta_index(s_attr, name);
2334 +       if (index < 0) {
2335 +               snap_debug("not exit %s meta attr of table ino %lu \n", 
2336 +                                       name, inode->i_ino);
2337 +               error = 0;
2338 +               goto out_iput;
2339 +       }
2340 +       if (!buf || *size < s_attr->array[index].len) {
2341 +               /*return the size of this meta attr */
2342 +               error = s_attr->array[index].len;               
2343 +               goto out_iput;  
2344 +       }
2345 +       map_len = (s_attr->array[index].len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;     
2346 +       left_size = *size;
2347 +       for(i = 0; i < map_len; i++) {
2348 +               struct buffer_head *array_bh = NULL;
2349 +
2350 +               array_bh = ext3_bread(NULL, inode, 
2351 +                                     s_attr->array[index].start + i,
2352 +                                     0, &error);
2353 +               if (!array_bh) {
2354 +                       snap_err("ino %lu read snap attr offset %d error %d \n",
2355 +                                 inode->i_ino, (s_attr->array[index].start + i),
2356 +                                 error);
2357 +                       goto out_iput;
2358 +               }
2359 +               if (left_size >= sb->s_blocksize) {
2360 +                       memcpy(buf, array_bh->b_data, sb->s_blocksize);
2361 +               }else
2362 +                       memcpy(buf, array_bh->b_data, left_size);
2363 +               left_size -= sb->s_blocksize;
2364 +               brelse(array_bh);
2365 +       }
2366 +       *size = s_attr->array[index].len;
2367 +out_iput:
2368 +       brelse(bh);
2369 +       iput(inode);
2370 +       return error;
2371 +} 
2372 +
2373 +static int ext3_set_meta_attr(struct super_block *sb, char* name, 
2374 +                             char* buf, int size)
2375 +{
2376 +        struct inode                   *inode = NULL;
2377 +        handle_t                       *handle = NULL;
2378 +       struct  buffer_head             *bh = NULL;
2379 +       struct table_snap_meta_data     *s_attr = NULL;
2380 +       unsigned long                   ino;
2381 +        int                            i, index = 0, error = 0;
2382 +       unsigned long                   new_len = 0, left_size; 
2383 +               
2384 +       ino = SB_SNAPTABLE_INO(sb);
2385 +      
2386 +       if (ino == 0 && !buf) {
2387 +               snap_debug("no table ino \n");
2388 +               return 0;
2389 +       }
2390 +       
2391 +       handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2392 +       if(!handle)
2393 +               return -EINVAL;
2394 +
2395 +       if (ino == 0) {
2396 +               /*create table inode update table ino*/
2397 +               inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
2398 +               if (!inode)
2399 +                       return  -EINVAL;
2400 +               lock_super(sb);
2401 +               ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
2402 +               SB_SNAPTABLE_INO(sb) = inode->i_ino;
2403 +               ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
2404 +               sb->s_dirt = 1;
2405 +               unlock_super(sb);
2406 +
2407 +       } else {
2408 +               inode = iget(sb, ino);
2409 +               if (!inode || !inode->i_nlink || is_bad_inode(inode)) {
2410 +                       snap_err("unable to get table ino %lu\n", ino);
2411 +                       error = -ENOENT;
2412 +                       goto exit;
2413 +               }
2414 +       }
2415 +       /*read the table from the table inode,
2416 +        * If can not find the block just create it*/
2417 +       bh = ext3_bread(handle, inode, 0, 1, &error);
2418 +       if (!bh) {
2419 +               snap_err("read table ino %lu, error %d\n", ino, error);
2420 +               error = -ENODATA;
2421 +               goto exit;
2422 +       }
2423 +       s_attr = (struct table_snap_meta_data *)(bh->b_data);
2424 +       index = find_snap_meta_index(s_attr, name);
2425 +       if (index < 0 && !buf) {        
2426 +               snap_debug("%s meta attr of table ino %lu do not exist\n", 
2427 +                           name, inode->i_ino);
2428 +                error = 0;
2429 +               brelse(bh);
2430 +               goto exit;
2431 +       }
2432 +       if (!buf) {
2433 +               snap_debug("delete the meta attr %s in the table ino %lu",
2434 +                          name, inode->i_ino);
2435 +               /*Here we only delete the entry of the attr
2436 +                *FIXME, should we also delete the block of 
2437 +                * this attr
2438 +                */
2439 +               ext3_journal_get_write_access(handle, bh);
2440 +               memset(s_attr->array[index].name, 0, TABLE_ITEM_NAME_SIZE);
2441 +               s_attr->array[index].len = 0;
2442 +               s_attr->count --;
2443 +               ext3_journal_dirty_metadata(handle, bh);
2444 +               brelse(bh);
2445 +               goto exit;
2446 +       }
2447 +       new_len = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2448 +       /*find the place to put this attr in that index*/
2449 +       ext3_journal_get_write_access(handle, bh);
2450 +       if (index < 0){
2451 +               index = set_snap_meta_index(s_attr, name, size);
2452 +               if (index < 0){
2453 +                       snap_err("table full of ino %lu \n", inode->i_ino);
2454 +                       error = index;
2455 +                       brelse(bh);
2456 +                       goto exit;
2457 +               }
2458 +       }
2459 +       s_attr->array[index].len = size;
2460 +       journal_dirty_metadata(handle, bh);
2461 +       brelse(bh);
2462 +       /*put this attr to the snap table*/
2463 +       left_size = size;
2464 +       for(i = 0; i < new_len; i++) {
2465 +               struct buffer_head *array_bh = NULL;
2466 +               
2467 +               array_bh = ext3_bread(handle, inode, 
2468 +                                     s_attr->array[index].start + i, 1, &error);
2469 +               if (!array_bh) {
2470 +                       snap_err("inode %lu Can not get the block of attr %s\n",  
2471 +                                 inode->i_ino, name);
2472 +                       error = -ENOSPC;
2473 +                       brelse(array_bh);
2474 +                       goto exit;
2475 +               }
2476 +               ext3_journal_get_write_access(handle, array_bh);
2477 +               if (left_size > inode->i_sb->s_blocksize)       
2478 +                       memcpy(array_bh->b_data, buf, inode->i_sb->s_blocksize);
2479 +               else
2480 +                       memcpy(array_bh->b_data, buf, left_size);
2481 +               ext3_journal_dirty_metadata(handle, array_bh);
2482 +               left_size -= inode->i_sb->s_blocksize;
2483 +               brelse(array_bh);
2484 +       }
2485 +exit:
2486 +       if (handle)
2487 +               ext3_journal_stop(handle, sb->s_root->d_inode); 
2488 +       iput(inode);
2489 +       return error;
2490 +}
2491 +
2492 +struct snapshot_operations ext3_snap_operations = {
2493 +       ops_version:            SNAP_VERSION(2,0,2),
2494 +       is_redirector:          is_redirector,
2495 +       is_indirect:            is_indirect,
2496 +       create_indirect:        ext3_create_indirect,
2497 +       get_indirect:           ext3_get_indirect,
2498 +        get_indirect_ino:      ext3_get_indirect_ino,
2499 +       destroy_indirect:       ext3_destroy_indirect,
2500 +       restore_indirect:       ext3_restore_indirect,
2501 +       iterate:                ext3_iterate,
2502 +       copy_block:             ext3_copy_block,
2503 +       set_indirect:           ext3_set_indirect,
2504 +       snap_feature:           ext3_snap_feature,
2505 +       get_generation:         ext3_get_generation,
2506 +       set_generation:         ext3_set_generation,
2507 +       get_meta_attr:          ext3_get_meta_attr,
2508 +       set_meta_attr:          ext3_set_meta_attr,                             
2509 +};
2510 +
2511 +EXPORT_SYMBOL(ext3_snap_operations);
2512 +#ifdef SNAP_PROFILE
2513 +EXPORT_SYMBOL(prof_snapdel);
2514 +#endif
2515 +
2516 +#ifdef SNAP_DEBUG_IOC
2517 +
2518 +static int print_inode(struct inode *pri, void *index_val)
2519 +{
2520 +
2521 +       int err=0;
2522 +       struct snap_ea *snaps;
2523 +       char buf[EXT3_MAX_SNAP_DATA];
2524 +       int index = *(int *)index_val;
2525 +
2526 +       err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2527 +                                               buf, EXT3_MAX_SNAP_DATA);
2528 +       
2529 +       if (err == -ENODATA) {
2530 +               memset(buf, 0, EXT3_MAX_SNAP_DATA);
2531 +       } 
2532 +       else if (err < 0) {
2533 +               snap_err("got err %d when reading attributes\n", err);
2534 +               goto err_exit;
2535 +       }
2536 +
2537 +       snaps = (struct snap_ea *) buf;
2538 +
2539 +       if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2540 +               snap_debug("no redirected ino for primary inode %lu\n",
2541 +                               primary->i_ino);
2542 +       }
2543 +       else {
2544 +               snap_debug("primary inode %lu , redirected ino=%d\n",
2545 +                               primary->i_ino,le32_to_cpu(snaps->ino[index]));
2546 +       }
2547 +err_exit:
2548 +       return err;
2549 +}
2550 +
2551 +int snap_print(struct super_block *sb, int index)
2552 +{
2553 +       ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2554 +       return 0;
2555 +}
2556 +
2557 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2558 +{
2559 +       int index = *(int *)index_val;
2560 +       int rc = 0;
2561 +       printk("delete_inode for index %d\n",index);
2562 +       rc = ext3_destroy_indirect(primary,index, NULL);
2563 +       if(rc != 0)     
2564 +               printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",                        
2565 +                                               primary->i_ino, index, rc);
2566 +       return 0;
2567 +}
2568 +
2569 +int ext3_snap_delete(struct super_block *sb, int index)
2570 +{
2571 +       ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index, 
2572 +                                       SNAP_ITERATE_COWED_INODE);
2573 +       return 0;
2574 +}
2575 +#endif
2576 +
2577 +
2578 +
2579 +
2580 +
2581 +
2582 +
2583 +
2584 Index: linux-2.4.20-8/fs/ext3/Makefile
2585 ===================================================================
2586 --- linux-2.4.20-8.orig/fs/ext3/Makefile        2004-01-19 22:06:25.000000000 +0800
2587 +++ linux-2.4.20-8/fs/ext3/Makefile     2004-01-19 22:06:25.000000000 +0800
2588 @@ -13,7 +13,7 @@
2589  
2590  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2591                 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2592 -               xattr_trusted.o
2593 +               xattr_trusted.o snap.o
2594  obj-m    := $(O_TARGET)
2595  
2596  export-objs += xattr.o
2597 Index: linux-2.4.20-8/fs/ext3/inode.c
2598 ===================================================================
2599 --- linux-2.4.20-8.orig/fs/ext3/inode.c 2004-01-19 22:06:24.000000000 +0800
2600 +++ linux-2.4.20-8/fs/ext3/inode.c      2004-01-26 01:12:48.000000000 +0800
2601 @@ -1191,7 +1191,7 @@
2602   * So, if we see any bmap calls here on a modified, data-journaled file,
2603   * take extra steps to flush any blocks which might be in the cache. 
2604   */
2605 -static int ext3_bmap(struct address_space *mapping, long block)
2606 +int ext3_bmap(struct address_space *mapping, long block)
2607  {
2608         struct inode *inode = mapping->host;
2609         journal_t *journal;
2610 @@ -1403,7 +1403,7 @@
2611   * This required during truncate. We need to physically zero the tail end
2612   * of that block so it doesn't yield old data if the file is later grown.
2613   */
2614 -static int ext3_block_truncate_page(handle_t *handle,
2615 +int ext3_block_truncate_page(handle_t *handle,
2616                 struct address_space *mapping, loff_t from)
2617  {
2618         unsigned long index = from >> PAGE_CACHE_SHIFT;
2619 Index: linux-2.4.20-8/fs/ext3/ialloc.c
2620 ===================================================================
2621 --- linux-2.4.20-8.orig/fs/ext3/ialloc.c        2004-01-19 22:06:24.000000000 +0800
2622 +++ linux-2.4.20-8/fs/ext3/ialloc.c     2004-01-19 22:06:25.000000000 +0800
2623 @@ -160,6 +160,13 @@
2624         return retval;
2625  }
2626  
2627 +/* Export load_inode_bitmap*/
2628 +int ext3_load_inode_bitmap (struct super_block * sb,
2629 +                           unsigned int block_group)
2630 +{
2631 +       return load_inode_bitmap(sb, block_group);
2632 +}
2633 +
2634  /*
2635   * NOTE! When we get the inode, we're the only people
2636   * that have access to it, and as such there are no
2637 Index: linux-2.4.20-8/fs/ext3/super.c
2638 ===================================================================
2639 --- linux-2.4.20-8.orig/fs/ext3/super.c 2004-01-19 22:06:24.000000000 +0800
2640 +++ linux-2.4.20-8/fs/ext3/super.c      2004-01-19 22:06:25.000000000 +0800
2641 @@ -1324,6 +1324,13 @@
2642         sbi->s_mount_state = le16_to_cpu(es->s_state);
2643         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2644         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2645 +#define EXT3_SNAP_FS
2646 +#ifdef EXT3_SNAP_FS
2647 +       init_MUTEX(&(sbi->s_snap_list_sem));
2648 +       sbi->s_snaptable_ino = le32_to_cpu(es->s_snaptable_ino);
2649 +       sbi->s_first_cowed_pri_ino = le32_to_cpu(es->s_first_cowed_pri_ino);
2650 +       sbi->s_last_cowed_pri_ino = le32_to_cpu(es->s_last_cowed_pri_ino);
2651 +#endif 
2652         for (i=0; i < 4; i++)
2653                 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2654         sbi->s_def_hash_version = es->s_def_hash_version;
2655 Index: linux-2.4.20-8/fs/ext3/ext3-exports.c
2656 ===================================================================
2657 --- linux-2.4.20-8.orig/fs/ext3/ext3-exports.c  2004-01-19 22:06:19.000000000 +0800
2658 +++ linux-2.4.20-8/fs/ext3/ext3-exports.c       2004-01-26 01:13:53.000000000 +0800
2659 @@ -21,6 +21,9 @@
2660  EXPORT_SYMBOL(ext3_xattr_set);
2661  EXPORT_SYMBOL(ext3_prep_san_write);
2662  EXPORT_SYMBOL(ext3_map_inode_page);
2663 +EXPORT_SYMBOL(ext3_orphan_add);
2664 +EXPORT_SYMBOL(ext3_orphan_del);
2665 +EXPORT_SYMBOL(ext3_block_truncate_page)
2666  
2667  EXPORT_SYMBOL(ext3_abort);
2668  EXPORT_SYMBOL(ext3_decode_error);
2669 Index: linux-2.4.20-8/include/linux/snap.h
2670 ===================================================================
2671 --- linux-2.4.20-8.orig/include/linux/snap.h    2003-01-30 18:24:37.000000000 +0800
2672 +++ linux-2.4.20-8/include/linux/snap.h 2004-01-19 22:11:26.000000000 +0800
2673 @@ -0,0 +1,266 @@
2674 +/*
2675 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
2676 + * started by Andreas Dilger <adilger@turbolinux.com>
2677 + *            Peter Braam <braam@mountainviewdata.com>
2678 + *            Harrison Xing <harrisonx@mountainviewdata.com>
2679 + * 
2680 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2681 + *                   Eric Mei    <Ericm@clusterfs.com>
2682 + *                   Wang Di     <wangdi@clusterfs.com>
2683 + * 
2684 + * Rewriten   2003  by Wang Di  <wangdi@clusterfs.com>
2685 + *                    Eric Mei <ericm@clusterfs.com>
2686 + *
2687 + * Functions for implementing snapshots in the ext3 filesystem.  They are
2688 + * intended to hide the internals of the filesystem from the caller in
2689 + * such a way that the caller doesn't need to know about inode numbers,
2690 + * how the redirectors are implemented or stored, etc.  It may not do that
2691 + * all yet, but it tries.
2692 + *
2693 + * The snapshot inode redirection is stored in the primary/direct inode as
2694 + * an extended attribute $snap, in the form of little-endian u32 inode
2695 + * numbers. 
2696 + *   
2697 + */
2698 +
2699 +#ifndef _LINUX_SNAP_H
2700 +#define _LINUX_SNAP_H
2701 +
2702 +#include <linux/fs.h>
2703 +
2704 +/* maximum number of snapshots available for users */
2705 +#define MAX_SNAPS      20      
2706 +
2707 +/* snap extended attributes definition */
2708 +#define SNAP_ATTR      "@snap"
2709 +struct snap_ea{                        
2710 +       int   generation;
2711 +       ino_t prev_ino;
2712 +       ino_t next_ino;
2713 +       ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2714 +       ino_t parent_ino[MAX_SNAPS+1];
2715 +};
2716 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
2717 +#if 0
2718 +/* for compatibility with old 128 max snapshots */
2719 +#define MAX_SNAP128_DATA (sizeof(struct snap_ea) - (sizeof(ino_t) * 128 * 2))
2720 +#define ZERO_SNAP_ATTR_TOP(buf)                                                \
2721 +       do {                                                            \
2722 +               struct snap_ea *p = (struct snap_ea*)buf;               \
2723 +               memset(&p->ino[129], 0, sizeof(ino_t)*128);             \
2724 +               memset(&p->parent_ino[129], 0, sizeof(ino_t)*128);      \
2725 +       } while(0)
2726 +
2727 +/* snap new ea definition , for logging of new inode */
2728 +#define SNAP_NEW_INO_ATTR      "@snap_new"
2729 +struct snap_new_ea{                    
2730 +       ino_t prev_ino; /* reserved. save the inode to a linked list */
2731 +       ino_t next_ino;
2732 +       int new_index;  /* indicate for which index this is a new inode */
2733 +};
2734 +#define NULL_NEW_INDEX -1      /* null new index, to clear the snap_new_ea */
2735 +
2736 +/* ea to identiry a indirect inode's infomation */
2737 +#define SNAP_INDIRECT_INFO_ATTR        "@snap_indirect_inode_info"
2738 +struct snap_indirect_info {
2739 +       __u32 index; /* which index belongs to */
2740 +       __u32 reserved[3]; /* reserved */
2741 +};
2742 +#endif
2743 +
2744 +/* snapfs meta data stored in extended attributes of root ino */
2745 +#define DISK_SNAP_META_ATTR    "@disk_snap_meta_attr"
2746 +struct disk_snap_meta_data {
2747 +       ino_t snap_first_cowed_ino;
2748 +       ino_t snap_table_ino;
2749 +       __u32 snap_feature_compat;
2750 +};
2751 +/*snapfs quota info */
2752 +
2753 +#define SNAP_USR_QUOTA                 0
2754 +#define SNAP_GRP_QUOTA         1
2755 +#define DISK_SNAP_QUOTA_INFO   "@disk_snap_quota_info"
2756 +struct quota_info_len {
2757 +       int     uid_len;     /*uid quota info length */
2758 +       int     gid_len;     /*gid quota info length */
2759 +};
2760 +/*
2761 + * Check if the EA @name is Snap EA or not.
2762 + * Snap EA includes the SNAP_ATTR, SNAP_NEW_INO_ATTR and DISK_SNAP_META_ATTR
2763 + */
2764 +
2765 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) ||             \
2766 +                          (!strcmp((name), DISK_SNAP_META_ATTR)))
2767 +
2768 +
2769 +
2770 +/* file system features */
2771 +#define SNAP_FEATURE_COMPAT_SNAPFS              0x0010
2772 +#define SNAP_FEATURE_COMPAT_BLOCKCOW            0x0020
2773 +
2774 +/* constants for snap_feature operations */
2775 +#define SNAP_CLEAR_FEATURE     0x0
2776 +#define SNAP_SET_FEATURE       0x1
2777 +#define SNAP_HAS_FEATURE       0x2
2778 +
2779 +/* snap flags for inode, within 1 byte range, each occupy 1 bit */
2780 +#define SNAP_INO_MAGIC 0x88            /* magic for snap inode */
2781 +#define SNAP_COW_FLAG  0x01            /* snap redirected inode */
2782 +#define SNAP_DEL_FLAG  0x02            /* snap deleted inode */
2783 +#define SNAP_TABLE_FLAG        0x04            /* snap table inode */
2784 +#define SNAP_PRI_FLAG  0x08            /* primary inode */
2785 +
2786 +/* no snapfs attributes for get_indirect_ino */
2787 +#define ENOSNAPATTR    320
2788 +
2789 +/* constants used by iterator */
2790 +#define SNAP_ITERATE_ALL_INODE          0x0
2791 +#define SNAP_ITERATE_COWED_INODE        0x1
2792 +
2793 +/* constants used by create_indirect */
2794 +#define SNAP_CREATE_IND_NORMAL         0x0
2795 +#define        SNAP_CREATE_IND_DEL_PRI         0x1
2796 +
2797 +/* the data structure represent in the xfs_dinode.pad
2798 +       offset  0:      magic   (1 byte)
2799 +       offset  1:      flag    (1 byte)
2800 +       offset  2:      gen     (4 bytes)
2801 +       offset  6:      unused
2802 + */
2803 +#define SIZEOF_MAGIC           1
2804 +#define SIZEOF_FLAG            1
2805 +#define SIZEOF_GENERATION      4
2806 +
2807 +#define MAGIC_OFFSET           0
2808 +#define FLAG_OFFSET            1
2809 +#define GENERATION_OFFSET      2
2810 +
2811 +#define SNAP_GET_DINODE_MAGIC(dinode)  \
2812 +               (((__u8*)(dinode)->di_pad)[MAGIC_OFFSET])
2813 +#define SNAP_SET_DINODE_MAGIC(dinode)  \
2814 +               ((__u8*)(dinode)->di_pad)[MAGIC_OFFSET] = (SNAP_INO_MAGIC)
2815 +#define SNAP_GET_DINODE_FLAG(dinode)   \
2816 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET])
2817 +#define SNAP_SET_DINODE_FLAG(dinode, flag)     \
2818 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] |= (flag))
2819 +#define SNAP_CLEAR_DINODE_FLAG(dinode, flag)   \
2820 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] &= ~(flag))
2821 +#define SNAP_GET_DINODE_GEN(dinode)    \
2822 +               (le32_to_cpu(*(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET])))
2823 +#define SNAP_SET_DINODE_GEN(dinode, gen)       \
2824 +               *(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET]) = cpu_to_le32(gen)
2825 +
2826 +#if 0
2827 +/* header of saving snaptable */
2828 +struct raw_data {
2829 +       unsigned int size;      /* buffer size passed by */
2830 +       char data[0];           /* followed by actual data */
2831 +};
2832 +
2833 +/* header of on-disk table data */
2834 +struct disk_snap_table_header {
2835 +       __u32   magic;
2836 +       __u32   version;
2837 +       __u32   datasize;
2838 +};
2839 +
2840 +/* table magic and version constant */
2841 +#define SNAP_TABLE_MAGIC       0xB3A2957F
2842 +#define SNAP_TABLE_VERSION     1
2843 +
2844 +
2845 +#define SNAPTABLE_BLOCKS(sb,size)      \
2846 +               (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
2847 +               >> sb->s_blocksize_bits)+1)
2848 +#endif
2849 +
2850 +#define SNAP_VERSION(a,b,c)            \
2851 +               (((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF))
2852 +#define SNAP_VERSION_MAJOR(v)          \
2853 +               ((v >> 16) & 0xFF)
2854 +#define SNAP_VERSION_MINOR(v)          \
2855 +               ((v >> 8) & 0xFF)
2856 +#define SNAP_VERSION_REL(v)            \
2857 +               (v & 0xFF)
2858 +
2859 +/* for snap meta attr table */
2860 +#define TABLE_ITEM_COUNT       200
2861 +#define TABLE_ITEM_SIZE                1000
2862 +#define TABLE_ITEM_NAME_SIZE   16
2863 +
2864 +/*snap table array */
2865 +struct snap_meta_array {
2866 +       char    name[TABLE_ITEM_NAME_SIZE];
2867 +       int     start;  /* where is the start of the array */
2868 +       int     len;  /* the len of the array */
2869 +}; 
2870 +/* snap table structure for record the information */
2871 +struct table_snap_meta_data {
2872 +       int                     count;
2873 +       struct snap_meta_array  array[TABLE_ITEM_COUNT]; 
2874 +};
2875 +
2876 +
2877 +#if 0
2878 +#define SNAP_PROFILE
2879 +#else
2880 +#undef SNAP_PROFILE
2881 +#endif
2882 +
2883 +#ifdef SNAP_PROFILE
2884 +struct profile_snapdel_stat
2885 +{
2886 +       unsigned long total_tick;               /* total time */
2887 +       unsigned long inodes;                   /* primary inodes */
2888 +
2889 +       unsigned long yield_count;              /* for yeild cpu */
2890 +       unsigned long yield_tick;
2891 +       unsigned long yield_max_tick;
2892 +
2893 +       unsigned long getea_count;              /* for get ea */
2894 +       unsigned long getea_tick;
2895 +       unsigned long getea_max_tick;
2896 +
2897 +       unsigned long setea_count;              /* for set ea */
2898 +       unsigned long setea_tick;
2899 +       unsigned long setea_max_tick;
2900 +
2901 +       unsigned long converge_count;           /* for converge */
2902 +       unsigned long converge_tick;
2903 +       unsigned long converge_max_tick;
2904 +};
2905 +
2906 +#endif
2907 +
2908 +/* snapshot operations */
2909 +struct snapshot_operations {
2910 +       unsigned int ops_version;
2911 +        int (*is_redirector) (struct inode *inode);
2912 +       int (*is_indirect) (struct inode *inode);
2913 +        struct inode * (*create_indirect) (struct inode *pri, int index,
2914 +                                          unsigned int gen, ino_t parent_ino,
2915 +                                          int del);
2916 +        struct inode * (*get_indirect) (struct inode *pri, int *table,int slot);
2917 +        ino_t (*get_indirect_ino) (struct inode *pri, int index);
2918 +        int (*destroy_indirect) (struct inode *pri, int index, 
2919 +                               struct inode *next_ind);
2920 +        int (*restore_indirect) (struct inode *pri, int index);
2921 +        int (*iterate) (struct super_block *sb,
2922 +                        int (*repeat)(struct inode *inode, void *priv),
2923 +                        struct inode **start, void *priv, int flag);
2924 +        int (*copy_block) ( struct inode *dst, struct inode *src, int blk);
2925 +       int (*has_block) (struct inode *dst, int blk);
2926 +       int (*set_indirect) (struct inode *pri, int index, 
2927 +                            ino_t ind_ino, ino_t parent_ino );
2928 +       int (*snap_feature) (struct super_block *sb, int feature, int op);
2929 +       int (*get_generation) (struct inode *pri);
2930 +       int (*set_generation) (struct inode *pri, unsigned long new_gen);
2931 +       int (*has_del_flag) (struct inode *inode);
2932 +       int (*clear_del_flag) (struct inode *inode);
2933 +       int (*set_meta_attr)(struct super_block *sb, char *name,
2934 +                               char *buf, int size);
2935 +       int (*get_meta_attr)(struct super_block *sb, char *name,
2936 +                               char *buf, int *size);
2937 +};
2938 +
2939 +#endif
2940 Index: linux-2.4.20-8/include/linux/ext3_fs.h
2941 ===================================================================
2942 --- linux-2.4.20-8.orig/include/linux/ext3_fs.h 2004-01-19 22:06:24.000000000 +0800
2943 +++ linux-2.4.20-8/include/linux/ext3_fs.h      2004-01-19 22:11:15.000000000 +0800
2944 @@ -183,7 +183,13 @@
2945  #define EXT3_INDEX_FL                  0x00001000 /* hash-indexed directory */
2946  #define EXT3_IMAGIC_FL                 0x00002000 /* AFS directory */
2947  #define EXT3_JOURNAL_DATA_FL           0x00004000 /* file data should be journaled */
2948 -#define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
2949 +/* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
2950 +#define EXT3_COW_FL                    0x00008000 /* inode is snapshot cow */
2951 +#define EXT3_DEL_FL                    0x00010000 /* inode is deleting in snapshot */
2952 +#define EXT3_SNAP_TABLE_FLAG                   0x00020000 /* snap table inode */
2953 +/* FIXME For debugging will be removed later*/
2954 +#define EXT3_SNAP_PRI_FLAG             0x00040000 /* primary inode */
2955 +
2956  
2957  #define EXT3_FL_USER_VISIBLE           0x00005FFF /* User visible flags */
2958  #define EXT3_FL_USER_MODIFIABLE                0x000000FF /* User modifiable flags */
2959 @@ -205,10 +211,25 @@
2960  /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2961  #define        EXT3_IOC_GETVERSION_OLD         _IOR('v', 1, long)
2962  #define        EXT3_IOC_SETVERSION_OLD         _IOW('v', 2, long)
2963 +/* the following are for temporary test */
2964 +/* snapfs ioctls */
2965 +#define EXT3_IOC_CREATE_INDIR           _IOW('v', 3, long)
2966 +#define EXT3_IOC_GET_INDIR              _IOW('v', 4, long)
2967 +#define EXT3_IOC_DESTROY_INDIR          _IOW('v', 5, long)
2968 +#define EXT3_IOC_IS_REDIR               _IOW('v', 6, long)
2969 +#define EXT3_IOC_RESTORE_INDIR          _IOW('v', 7, long)
2970 +
2971 +#define EXT3_IOC_SNAP_SETFILECOW       _IOW('v', 10, long)
2972 +
2973 +/* XXX: the following are for temporary test, can be removed later */
2974 +#define EXT3_IOC_SNAP_PRINT             _IOW('v', 11, long)
2975 +#define EXT3_IOC_SNAP_DELETE            _IOW('v', 12, long)
2976 +#define EXT3_IOC_SNAP_RESTORE           _IOW('v', 13, long)
2977 +
2978 +
2979  #ifdef CONFIG_JBD_DEBUG
2980  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
2981  #endif
2982 -
2983  /*
2984   * Structure of an inode on the disk
2985   */
2986 @@ -429,7 +450,15 @@
2987         __u8    s_def_hash_version;     /* Default hash version to use */
2988         __u8    s_reserved_char_pad;
2989         __u16   s_reserved_word_pad;
2990 -       __u32   s_reserved[192];        /* Padding to the end of the block */
2991 +       __u32   s_default_mount_opts;
2992 +        __u32   s_first_meta_bg;        /* First metablock group */
2993 +       __u32   s_mkfs_time;            /* When the filesystem was created */
2994 +       /* for snapfs */
2995 +       __u32   s_first_cowed_pri_ino;  /* For snapfs,the first cowed primary inode */
2996 +       __u32   s_last_cowed_pri_ino;     /* last cowed ino in memory */
2997 +       __u32   s_snaptable_ino;        /* snaptable ino in memory */
2998 +       __u32   s_last_snap_orphan;     /* SnapFS: start of cowing indirect inode */
2999 +       __u32   s_reserved[186];        /* Padding to the end of the block,originally 204 */
3000  };
3001  
3002  #ifdef __KERNEL__
3003 @@ -503,6 +532,9 @@
3004  #define EXT3_FEATURE_INCOMPAT_RECOVER          0x0004 /* Needs recovery */
3005  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
3006  
3007 +#define EXT3_FEATURE_COMPAT_SNAPFS             0x0010
3008 +#define EXT3_FEATURE_COMPAT_BLOCKCOW           0x0020
3009 +
3010  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
3011  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
3012                                          EXT3_FEATURE_INCOMPAT_RECOVER)
3013 Index: linux-2.4.20-8/include/linux/ext3_fs_sb.h
3014 ===================================================================
3015 --- linux-2.4.20-8.orig/include/linux/ext3_fs_sb.h      2004-01-19 22:06:18.000000000 +0800
3016 +++ linux-2.4.20-8/include/linux/ext3_fs_sb.h   2004-01-19 22:10:06.000000000 +0800
3017 @@ -86,6 +86,13 @@
3018         wait_queue_head_t s_delete_thread_queue;
3019         wait_queue_head_t s_delete_waiter_queue;
3020  #endif
3021 +#define EXT3_SNAP_FS
3022 +#ifdef EXT3_SNAP_FS
3023 +       struct semaphore s_snap_list_sem;
3024 +       unsigned long   s_first_cowed_pri_ino;/* For snapfs,the first cowed primary inode */
3025 +       unsigned long   s_last_cowed_pri_ino;     /* last cowed ino in memory */
3026 +       unsigned long   s_snaptable_ino;      /* snaptable ino in memory */
3027 +#endif
3028  };
3029  
3030  #endif /* _LINUX_EXT3_FS_SB */
3031 Index: linux-2.4.20-8/include/linux/ext3_jbd.h
3032 ===================================================================
3033 --- linux-2.4.20-8.orig/include/linux/ext3_jbd.h        2004-01-19 22:06:15.000000000 +0800
3034 +++ linux-2.4.20-8/include/linux/ext3_jbd.h     2004-01-19 22:11:15.000000000 +0800
3035 @@ -71,6 +71,33 @@
3036  
3037  #define EXT3_INDEX_EXTRA_TRANS_BLOCKS  8
3038  
3039 +/*snapshot transaction blocks*/
3040 +
3041 +#define EXT3_EA_TRANS_BLOCKS           EXT3_DATA_TRANS_BLOCKS
3042 +#define EXT3_SETMETA_TRANS_BLOCKS      EXT3_DATA_TRANS_BLOCKS
3043 +#define EXT3_NEWINODE_TRANS_BLOCKS     10
3044 +#define SNAP_INSERTLIST_TRANS_BLOCKS   (2 * EXT3_EA_TRANS_BLOCKS + 1)
3045 +#define SNAP_DELETELIST_TRANS_BLOCKS   (2 * EXT3_EA_TRANS_BLOCKS + 2)
3046 +#define SNAP_COPYBLOCK_TRANS_BLOCKS    (EXT3_DATA_TRANS_BLOCKS)
3047 +#define SNAP_MIGRATEDATA_TRANS_BLOCKS  2
3048 +#define SNAP_SETIND_TRANS_BLOCKS       (SNAP_INSERTLIST_TRANS_BLOCKS + 1)
3049 +#define SNAP_ADDORPHAN_TRANS_BLOCKS    2
3050 +#define SNAP_REMOVEORPHAN_TRANS_BLOCKS 1
3051 +#define SNAP_RESTOREORPHAN_TRANS_BLOCKS        (EXT3_EA_TRANS_BLOCKS + \
3052 +                                        SNAP_DELETELIST_TRANS_BLOCKS + \
3053 +                                        EXT3_NEWINODE_TRANS_BLOCKS + \
3054 +                                        2 * SNAP_MIGRATEDATA_TRANS_BLOCKS)
3055 +#define SNAP_BIGCOPY_TRANS_BLOCKS      (2 * EXT3_DATA_TRANS_BLOCKS)
3056 +#define SNAP_CREATEIND_TRANS_BLOCKS    (EXT3_NEWINODE_TRANS_BLOCKS + \
3057 +                                        SNAP_MIGRATEDATA_TRANS_BLOCKS + \
3058 +                                        SNAP_SETIND_TRANS_BLOCKS + \
3059 +                                        SNAP_BIGCOPY_TRANS_BLOCKS + 3)
3060 +#define SNAP_MIGRATEBLK_TRANS_BLOCKS   2
3061 +#define SNAP_DESTROY_TRANS_BLOCKS      (SNAP_DELETELIST_TRANS_BLOCKS + \
3062 +                                        EXT3_EA_TRANS_BLOCKS + 2)
3063 +#define SNAP_RESTORE_TRANS_BLOCKS      (EXT3_NEWINODE_TRANS_BLOCKS + \
3064 +                                        2 * SNAP_MIGRATEDATA_TRANS_BLOCKS + 1)
3065 +
3066  int
3067  ext3_mark_iloc_dirty(handle_t *handle, 
3068                      struct inode *inode,
3069
3070 %diffstat
3071  fs/ext3/Makefile           |    2 
3072  fs/ext3/ext3-exports.c     |    3 
3073  fs/ext3/ialloc.c           |    7 
3074  fs/ext3/inode.c            |    4 
3075  fs/ext3/snap.c             | 2577 +++++++++++++++++++++++++++++++++++++++++++++
3076  fs/ext3/super.c            |    7 
3077  include/linux/ext3_fs.h    |   38 
3078  include/linux/ext3_fs_sb.h |    7 
3079  include/linux/ext3_jbd.h   |   27 
3080  include/linux/snap.h       |  266 ++++
3081  10 files changed, 2932 insertions(+), 6 deletions(-)
3082