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