Whamcloud - gitweb
some fix in delete snapshot
[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-19 23:24:39.000000000 +0800
6 @@ -0,0 +1,2583 @@
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_FIRST_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 +               snap_err("TRY TO DESTROY JOURNAL'S IND\n");
1902 +               return -EINVAL;
1903 +       }
1904 +
1905 +       err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1906 +                            buf, EXT3_MAX_SNAP_DATA);
1907 +       if (err < 0) {
1908 +               snap_err("inode %lu attribute read error\n", pri->i_ino);
1909 +               return err;
1910 +       }
1911 +       
1912 +       snaps = (struct snap_ea *)buf;
1913 +       if ( !snaps->ino[index] ) {
1914 +               snap_err("for pri ino %lu, index %d, redirect ino is 0\n",
1915 +                               pri->i_ino, index);     
1916 +               return -EINVAL;
1917 +       }
1918 +
1919 +       snap_debug("for pri ino %lu, reading inode %lu at index %d\n", 
1920 +               pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1921 +
1922 +       ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1923 +
1924 +       if ( !ind || IS_ERR(ind) || is_bad_inode(ind)) 
1925 +               return  -EINVAL;
1926 +
1927 +       snap_debug("iget ind %lu, ref count = %d\n", 
1928 +                  ind->i_ino, atomic_read(&ind->i_count));
1929 +
1930 +       handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
1931 +       if (!handle) {
1932 +               iput(ind);
1933 +               return -EINVAL;
1934 +       }
1935 +       /* if it's block level cow, first copy the blocks back */       
1936 +       if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1937 +                       S_ISREG(pri->i_mode)) {
1938 +
1939 +               int blocks;
1940 +               if( !next_ind ) next_ind = pri;
1941 +               blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1) 
1942 +                               >> next_ind->i_sb->s_blocksize_bits;
1943 +
1944 +#define FAST_MIGRATE_BLOCK
1945 +#ifdef FAST_MIGRATE_BLOCK
1946 +               snap_debug("migrate block back from ino %lu to %lu\n",
1947 +                               ind->i_ino, next_ind->i_ino);
1948 +
1949 +               snap_double_lock(next_ind, ind);
1950 +               for(i = 0; i < blocks; i++) {
1951 +                       if( ext3_bmap(next_ind->i_mapping, i) ) 
1952 +                               continue;
1953 +                       if( !ext3_bmap(ind->i_mapping, i) ) 
1954 +                               continue;
1955 +                       ext3_migrate_block(handle, next_ind, ind, i) ;
1956 +               }
1957 +               /* Now re-compute the i_blocks */
1958 +               /* XXX shall we take care of ind here? probably not */
1959 +               next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1960 +               ext3_mark_inode_dirty(handle, next_ind);
1961 +
1962 +               snap_double_unlock(next_ind, ind);
1963 +
1964 +#if 0
1965 +               snap_double_lock(pri, ind);
1966 +
1967 +               for(i = 0; i < blocks; i++) { 
1968 +                       if( ext3_bmap(pri, i) ) continue;
1969 +                       if( !ext3_bmap(ind, i) ) continue;
1970 +                       ext3_migrate_block( pri, ind, i) ;
1971 +               }
1972 +               /* Now re-compute the i_blocks */
1973 +               /* XXX shall we take care of ind here? probably not */
1974 +               pri->i_blocks = calculate_i_blocks( pri, blocks);
1975 +               mark_inode_dirty(pri);
1976 +
1977 +               double_unlock(pri, ind);
1978 +#endif
1979 +#else
1980 +               snap_double_lock(next_ind, ind);
1981 +               for (i = 0; i < blocks; i++) {
1982 +                       if (ext3_bmap(next_ind->i_mapping, i))  
1983 +                               continue;
1984 +                       if (ext3_copy_block(next_ind, ind, i ) < 0)     
1985 +                               break;
1986 +               }
1987 +               ext3_mark_inode_dirty(handle, next_ind);
1988 +               double_unlock(next_ind, ind);
1989 +#endif 
1990 +       }
1991 +
1992 +       snap_debug("delete indirect ino %lu\n", ind->i_ino);
1993 +       snap_debug("iput ind %lu, ref count = %d\n", 
1994 +                   ind->i_ino, atomic_read(&ind->i_count));
1995 +       ind->i_nlink = 0;
1996 +       iput (ind);
1997 +
1998 +       snaps->ino[index] = cpu_to_le32(0);
1999 +       for (i = 0; i < EXT3_MAX_SNAPS; i++)
2000 +               save += snaps->ino[i];
2001 +
2002 +       if(!save) {     
2003 +               lock_list(pri->i_sb);
2004 +               delete_cowed_ino_from_list(handle, pri);
2005 +               unlock_list(pri->i_sb);
2006 +       }
2007 +
2008 +       /* if there are no cowed inode left, then remove snapfs feature */
2009 +       if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2010 +
2011 +               lock_super(pri->i_sb);
2012 +
2013 +               ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
2014 +               if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2015 +                                     EXT3_FEATURE_COMPAT_SNAPFS)) {
2016 +                       pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2017 +                               cpu_to_le32(~EXT3_FEATURE_COMPAT_SNAPFS);
2018 +               }
2019 +               /* clean up block level cow feature */
2020 +               if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2021 +                       EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2022 +                               pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2023 +                                       cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2024 +                 }
2025 +               /* XXX clean the extended attribute feature,
2026 +                * this is not safe, find a better way
2027 +                */
2028 +                if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2029 +                       EXT3_FEATURE_COMPAT_EXT_ATTR)) {
2030 +                               pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2031 +                                       cpu_to_le32(~EXT3_FEATURE_COMPAT_EXT_ATTR);
2032 +                }
2033 +
2034 +               ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
2035 +               pri->i_sb->s_dirt = 1;
2036 +               unlock_super(pri->i_sb);
2037 +        }
2038 +
2039 +       /*
2040 +        * If we are deleting the last indirect inode, and the primary inode
2041 +        * has already been deleted, then mark the primary for deletion also.
2042 +        * Otherwise, if we are deleting the last indirect inode remove the
2043 +        * snaptable from the inode.    XXX
2044 +        */
2045 +       if (!save && pri->u.ext3_i.i_dtime) {
2046 +               snap_debug("deleting primary %lu\n", pri->i_ino);
2047 +               pri->i_nlink = 0;
2048 +               /* reset err to 0 now */
2049 +               err = 0;
2050 +       } else {
2051 +               snap_debug("%s redirector table\n",
2052 +                               save ? "saving" : "deleting");
2053 +               /* XXX: since set ea will modify i_ctime of pri, 
2054 +                       so save/restore i_ctime. Need this necessary ? */
2055 +               ctime = pri->i_ctime;   
2056 +               err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2057 +                                          save ? buf : NULL, EXT3_MAX_SNAP_DATA, 0);
2058 +               pri->i_ctime = ctime;
2059 +               ext3_mark_inode_dirty(handle, pri);
2060 +       }
2061 +       ext3_journal_stop(handle, pri);
2062 +       return err;
2063 +}
2064 +
2065 +/* restore a primary inode with the indirect inode at index */
2066 +static int ext3_restore_indirect(struct inode *pri, int index)
2067 +{
2068 +       struct inode *ind;
2069 +       struct inode *tmp;
2070 +       int err = 0;
2071 +       handle_t *handle = NULL;
2072 +
2073 +       if (index < 0 || index > EXT3_MAX_SNAPS)
2074 +               return -EINVAL;
2075 +
2076 +       if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2077 +               printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2078 +               return -EINVAL;
2079 +       }
2080 +       snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2081 +
2082 +       ind = ext3_get_indirect(pri, NULL, index);
2083 +
2084 +       if ( !ind ) 
2085 +               return -EINVAL;
2086 +
2087 +       snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2088 +
2089 +       handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
2090 +       if( !handle )
2091 +               return -EINVAL;
2092 +       /* first destroy all the data blocks in primary inode */
2093 +       /* XXX: check this, ext3_new_inode, the first arg should be "dir" */ 
2094 +       tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
2095 +       if(tmp) {
2096 +               snap_double_lock(pri, tmp);
2097 +               ext3_migrate_data(handle, tmp, pri);
2098 +               snap_double_unlock(pri, tmp);
2099 +
2100 +               tmp->i_nlink = 0;
2101 +               iput(tmp);      
2102 +       }
2103 +       else    
2104 +               snap_err("restore_indirect, new_inode err\n");
2105 +
2106 +       snap_double_lock(pri, ind);
2107 +       ext3_migrate_data(handle, pri, ind);
2108 +       /* clear the cow flag for pri because ind has it */
2109 +       pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
2110 +       ext3_mark_inode_dirty(handle, pri);
2111 +       snap_double_unlock(pri, ind);
2112 +
2113 +       iput(ind);
2114 +
2115 +//     ext3_destroy_indirect(pri, index);
2116 +
2117 +       ext3_journal_stop(handle, pri);
2118 +       return err;
2119 +}
2120 +
2121 +
2122 +/**
2123 + * ext3_snap_iterate - iterate through all of the inodes
2124 + * @sb: filesystem superblock
2125 + * @repeat: pointer to function called on each valid inode
2126 + * @start: inode to start iterating at
2127 + * @priv: private data to the caller/repeat function
2128 + *
2129 + * If @start is NULL, then we do not return an inode pointer.  If @*start is
2130 + * NULL, then we start at the beginning of the filesystem, and iterate over
2131 + * all of the inodes in the system.  If @*start is non-NULL, then we start
2132 + * iterating at this inode.
2133 + *
2134 + * We call the repeat function for each inode that is in use.  The repeat
2135 + * function must check if this is a redirector (with is_redirector) if it
2136 + * only wants to operate on redirector inodes.  If there is an error or
2137 + * the repeat function returns non-zero, we return the last inode operated
2138 + * on in the @*start parameter.  This allows the caller to restart the
2139 + * iteration at this inode if desired, by returning a positive value.
2140 + * Negative return values indicate an error.
2141 + *
2142 + * NOTE we cannot simply traverse the existing filesystem tree from the root
2143 + *      inode, as there may be disconnected trees from deleted files/dirs
2144 + *
2145 + * FIXME If there was a list of inodes with EAs, we could simply walk the list
2146 + * intead of reading every inode.  This is an internal implementation issue.
2147 + */
2148 +
2149 +static int ext3_iterate_all(struct super_block *sb,
2150 +                       int (*repeat)(struct inode *inode, void *priv),
2151 +                       struct inode **start, void *priv)
2152 +{
2153 +       struct inode *tmp = NULL;
2154 +       int gstart, gnum;
2155 +       ino_t istart, ibase;
2156 +       int err = 0;
2157 +
2158 +       if (!start)
2159 +               start = &tmp;
2160 +       if (!*start) {
2161 +               *start = iget(sb, EXT3_ROOT_INO);
2162 +               if (!*start) {
2163 +                       err = -ENOMEM;
2164 +                       goto exit;
2165 +               }
2166 +               if (is_bad_inode(*start)) {
2167 +                       err = -EIO;
2168 +                       goto exit;
2169 +               }
2170 +       }
2171 +       if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
2172 +               snap_debug("invalid starting inode %ld\n",(*start)->i_ino);
2173 +               err = -EINVAL;
2174 +               goto exit;
2175 +       }
2176 +       if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2177 +               if ((err = (*repeat)(*start, priv) != 0))
2178 +                       goto exit;
2179 +               iput(*start);
2180 +               *start = iget(sb, EXT3_FIRST_INO(sb));
2181 +               if (!*start) {
2182 +                       err = -ENOMEM;
2183 +                       goto exit;
2184 +               }
2185 +               if (is_bad_inode(*start)) {
2186 +                       err = -EIO;
2187 +                       goto exit;
2188 +               }
2189 +       }
2190 +
2191 +       gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
2192 +       istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
2193 +       ibase = gstart * EXT3_INODES_PER_GROUP(sb);
2194 +       for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
2195 +            gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
2196 +               struct ext3_group_desc * gdp;
2197 +               int bitmap_nr;
2198 +               char *bitmap;
2199 +               int ibyte;
2200 +
2201 +               gdp = ext3_get_group_desc (sb, gnum, NULL);
2202 +               if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
2203 +                   EXT3_INODES_PER_GROUP(sb))
2204 +                       continue;
2205 +
2206 +               bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2207 +               if (bitmap_nr < 0)
2208 +                       continue;
2209 +
2210 +               bitmap = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr]->b_data;
2211 +               for (ibyte = istart >> 3;
2212 +                    ibyte < EXT3_INODES_PER_GROUP(sb) >> 3;
2213 +                    ibyte++)
2214 +               {
2215 +                       int i;
2216 +                       int bit;
2217 +
2218 +                       if (!bitmap[ibyte])
2219 +                               continue;
2220 +
2221 +                       /* FIXME need to verify if bit endianness will
2222 +                        *       work properly here for all architectures.
2223 +                        */
2224 +                       for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2225 +                               ino_t ino = ibase + (ibyte << 3) + i;
2226 +
2227 +                               if ((bitmap[ibyte] & bit) == 0)
2228 +                                       continue;
2229 +                               if (*start) {
2230 +                                       if (ino < (*start)->i_ino)
2231 +                                               continue;
2232 +                               } else {
2233 +                                       *start = iget(sb, ino);
2234 +                                       if (!*start) {
2235 +                                               err = -ENOMEM;
2236 +                                               goto exit;
2237 +                                       }
2238 +                                       if (is_bad_inode(*start)) {
2239 +                                               err = -EIO;
2240 +                                               goto exit;
2241 +                                       }
2242 +                               }
2243 +                               if ((err = (*repeat)(*start, priv)) != 0)
2244 +                                       goto exit;
2245 +                               iput(*start);
2246 +                               *start = NULL;
2247 +                       }
2248 +               }
2249 +               istart = 0;
2250 +       }
2251 +exit:
2252 +       iput(tmp);
2253 +       return err;
2254 +}
2255 +
2256 +static int ext3_iterate(struct super_block *sb,
2257 +                       int (*repeat)(struct inode *inode, void *priv),
2258 +                       struct inode **start, void *priv, int flag)
2259 +{
2260 +       switch(flag) {
2261 +               case SNAP_ITERATE_ALL_INODE:
2262 +                       return ext3_iterate_all (sb, repeat, start, priv);
2263 +
2264 +               case SNAP_ITERATE_COWED_INODE:
2265 +                       return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2266 +
2267 +               default:
2268 +                       return -EINVAL;
2269 +       }
2270 +}
2271 +
2272 +static int find_snap_meta_index(
2273 +       struct table_snap_meta_data *snap_meta,
2274 +       char                        *name)
2275 +{
2276 +       int i;
2277 +
2278 +       /* table max length is null*/
2279 +       for( i = 0; i < TABLE_ITEM_COUNT; i++){
2280 +               /*compare name Max name Length 15*/
2281 +               if (snap_meta->array[i].name[0]){
2282 +                       if(!strncmp(snap_meta->array[i].name, name, strlen(name)))
2283 +                               return i;
2284 +               }
2285 +       }
2286 +       return -1; /* can not find */
2287 +}
2288 +
2289 +int set_snap_meta_index(
2290 +       struct table_snap_meta_data *snap_meta,
2291 +       char                        *name,
2292 +       int                          size)
2293 +{
2294 +       int i;
2295 +
2296 +       for( i = 0; i < TABLE_ITEM_COUNT; i++){
2297 +               /*compare name Max name Length 15*/
2298 +               if (! snap_meta->array[i].name[0]){
2299 +                       strcpy(snap_meta->array[i].name, name);
2300 +                       snap_meta->count ++;
2301 +                       snap_meta->array[i].start = i * TABLE_ITEM_SIZE + 1;
2302 +                       snap_meta->array[i].len   = size;
2303 +                       return i;
2304 +               }
2305 +       }
2306 +       return -1; /* can not find */
2307 +}
2308 +
2309 +static int ext3_get_meta_attr(struct super_block *sb, 
2310 +                             char* name, char* buf, 
2311 +                             int *size)
2312 +{
2313 +        ino_t                          ino;
2314 +        struct inode                   *inode;
2315 +       struct buffer_head              *bh = NULL;
2316 +       struct table_snap_meta_data     *s_attr;
2317 +       unsigned long                   map_len = 0,  left_size;
2318 +        int                            i, error = 0, index = 0;
2319 +       
2320 +       ino = SB_SNAPTABLE_INO(sb);     
2321 +       if (ino == 0){
2322 +               snap_err("No table file \n");
2323 +               return  -ENODATA;
2324 +       } 
2325 +       inode = iget(sb, ino);
2326 +        if(!inode || is_bad_inode(inode)){
2327 +                snap_err("unable to get table ino %lu\n", ino);
2328 +                error = -ENOENT;
2329 +                       goto out_iput; 
2330 +       }
2331 +       /*read the table from the table inode*/
2332 +       bh = ext3_bread(NULL, inode, 0, 0, &error);
2333 +       if (!bh) {
2334 +               snap_err("read table ino %lu, error %d\n", ino, error);
2335 +               error = -ENODATA;
2336 +               goto out_iput;
2337 +       }
2338 +       s_attr = (struct table_snap_meta_data *)(bh->b_data);
2339 +       index = find_snap_meta_index(s_attr, name);
2340 +       if (index < 0) {
2341 +               snap_debug("not exit %s meta attr of table ino %lu \n", 
2342 +                                       name, inode->i_ino);
2343 +               error = 0;
2344 +               goto out_iput;
2345 +       }
2346 +       if (!buf || *size < s_attr->array[index].len) {
2347 +               /*return the size of this meta attr */
2348 +               error = s_attr->array[index].len;               
2349 +               goto out_iput;  
2350 +       }
2351 +       map_len = (s_attr->array[index].len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;     
2352 +       left_size = *size;
2353 +       for(i = 0; i < map_len; i++) {
2354 +               struct buffer_head *array_bh = NULL;
2355 +
2356 +               array_bh = ext3_bread(NULL, inode, 
2357 +                                     s_attr->array[index].start + i,
2358 +                                     0, &error);
2359 +               if (!array_bh) {
2360 +                       snap_err("ino %lu read snap attr offset %d error %d \n",
2361 +                                 inode->i_ino, (s_attr->array[index].start + i),
2362 +                                 error);
2363 +                       goto out_iput;
2364 +               }
2365 +               if (left_size >= sb->s_blocksize) {
2366 +                       memcpy(buf, array_bh->b_data, sb->s_blocksize);
2367 +               }else
2368 +                       memcpy(buf, array_bh->b_data, left_size);
2369 +               left_size -= sb->s_blocksize;
2370 +               brelse(array_bh);
2371 +       }
2372 +       *size = s_attr->array[index].len;
2373 +out_iput:
2374 +       brelse(bh);
2375 +       iput(inode);
2376 +       return error;
2377 +} 
2378 +
2379 +static int ext3_set_meta_attr(struct super_block *sb, char* name, 
2380 +                             char* buf, int size)
2381 +{
2382 +        struct inode                   *inode = NULL;
2383 +        handle_t                       *handle = NULL;
2384 +       struct  buffer_head             *bh = NULL;
2385 +       struct table_snap_meta_data     *s_attr = NULL;
2386 +       unsigned long                   ino;
2387 +        int                            i, index = 0, error = 0;
2388 +       unsigned long                   new_len = 0, left_size; 
2389 +               
2390 +       ino = SB_SNAPTABLE_INO(sb);
2391 +      
2392 +       if (ino == 0 && !buf) {
2393 +               snap_debug("no table ino \n");
2394 +               return 0;
2395 +       }
2396 +       
2397 +       handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2398 +       if(!handle)
2399 +               return -EINVAL;
2400 +
2401 +       if (ino == 0) {
2402 +               /*create table inode update table ino*/
2403 +               inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
2404 +               if (!inode)
2405 +                       return  -EINVAL;
2406 +               lock_super(sb);
2407 +               ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
2408 +               SB_SNAPTABLE_INO(sb) = inode->i_ino;
2409 +               ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
2410 +               sb->s_dirt = 1;
2411 +               unlock_super(sb);
2412 +
2413 +       } else {
2414 +               inode = iget(sb, ino);
2415 +               if (!inode || !inode->i_nlink || is_bad_inode(inode)) {
2416 +                       snap_err("unable to get table ino %lu\n", ino);
2417 +                       error = -ENOENT;
2418 +                       goto exit;
2419 +               }
2420 +       }
2421 +       /*read the table from the table inode,
2422 +        * If can not find the block just create it*/
2423 +       bh = ext3_bread(handle, inode, 0, 1, &error);
2424 +       if (!bh) {
2425 +               snap_err("read table ino %lu, error %d\n", ino, error);
2426 +               error = -ENODATA;
2427 +               goto exit;
2428 +       }
2429 +       s_attr = (struct table_snap_meta_data *)(bh->b_data);
2430 +       index = find_snap_meta_index(s_attr, name);
2431 +       if (index < 0 && !buf) {        
2432 +               snap_debug("%s meta attr of table ino %lu do not exist\n", 
2433 +                           name, inode->i_ino);
2434 +                error = 0;
2435 +               brelse(bh);
2436 +               goto exit;
2437 +       }
2438 +       if (!buf) {
2439 +               snap_debug("delete the meta attr %s in the table ino %lu",
2440 +                          name, inode->i_ino);
2441 +               /*Here we only delete the entry of the attr
2442 +                *FIXME, should we also delete the block of 
2443 +                * this attr
2444 +                */
2445 +               ext3_journal_get_write_access(handle, bh);
2446 +               memset(s_attr->array[index].name, 0, TABLE_ITEM_NAME_SIZE);
2447 +               s_attr->array[index].len = 0;
2448 +               s_attr->count --;
2449 +               ext3_journal_dirty_metadata(handle, bh);
2450 +               brelse(bh);
2451 +               goto exit;
2452 +       }
2453 +       new_len = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2454 +       /*find the place to put this attr in that index*/
2455 +       ext3_journal_get_write_access(handle, bh);
2456 +       if (index < 0){
2457 +               index = set_snap_meta_index(s_attr, name, size);
2458 +               if (index < 0){
2459 +                       snap_err("table full of ino %lu \n", inode->i_ino);
2460 +                       error = index;
2461 +                       brelse(bh);
2462 +                       goto exit;
2463 +               }
2464 +       }
2465 +       s_attr->array[index].len = size;
2466 +       journal_dirty_metadata(handle, bh);
2467 +       brelse(bh);
2468 +       /*put this attr to the snap table*/
2469 +       left_size = size;
2470 +       for(i = 0; i < new_len; i++) {
2471 +               struct buffer_head *array_bh = NULL;
2472 +               
2473 +               array_bh = ext3_bread(handle, inode, 
2474 +                                     s_attr->array[index].start + i, 1, &error);
2475 +               if (!array_bh) {
2476 +                       snap_err("inode %lu Can not get the block of attr %s\n",  
2477 +                                 inode->i_ino, name);
2478 +                       error = -ENOSPC;
2479 +                       brelse(array_bh);
2480 +                       goto exit;
2481 +               }
2482 +               ext3_journal_get_write_access(handle, array_bh);
2483 +               if (left_size > inode->i_sb->s_blocksize)       
2484 +                       memcpy(array_bh->b_data, buf, inode->i_sb->s_blocksize);
2485 +               else
2486 +                       memcpy(array_bh->b_data, buf, left_size);
2487 +               ext3_journal_dirty_metadata(handle, array_bh);
2488 +               left_size -= inode->i_sb->s_blocksize;
2489 +               brelse(array_bh);
2490 +       }
2491 +exit:
2492 +       if (handle)
2493 +               ext3_journal_stop(handle, sb->s_root->d_inode); 
2494 +       iput(inode);
2495 +       return error;
2496 +}
2497 +
2498 +struct snapshot_operations ext3_snap_operations = {
2499 +       ops_version:            SNAP_VERSION(2,0,2),
2500 +       is_redirector:          is_redirector,
2501 +       is_indirect:            is_indirect,
2502 +       create_indirect:        ext3_create_indirect,
2503 +       get_indirect:           ext3_get_indirect,
2504 +        get_indirect_ino:      ext3_get_indirect_ino,
2505 +       destroy_indirect:       ext3_destroy_indirect,
2506 +       restore_indirect:       ext3_restore_indirect,
2507 +       iterate:                ext3_iterate,
2508 +       copy_block:             ext3_copy_block,
2509 +       set_indirect:           ext3_set_indirect,
2510 +       snap_feature:           ext3_snap_feature,
2511 +       get_generation:         ext3_get_generation,
2512 +       set_generation:         ext3_set_generation,
2513 +       get_meta_attr:          ext3_get_meta_attr,
2514 +       set_meta_attr:          ext3_set_meta_attr,                             
2515 +};
2516 +
2517 +EXPORT_SYMBOL(ext3_snap_operations);
2518 +#ifdef SNAP_PROFILE
2519 +EXPORT_SYMBOL(prof_snapdel);
2520 +#endif
2521 +
2522 +#ifdef SNAP_DEBUG_IOC
2523 +
2524 +static int print_inode(struct inode *pri, void *index_val)
2525 +{
2526 +
2527 +       int err=0;
2528 +       struct snap_ea *snaps;
2529 +       char buf[EXT3_MAX_SNAP_DATA];
2530 +       int index = *(int *)index_val;
2531 +
2532 +       err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2533 +                                               buf, EXT3_MAX_SNAP_DATA);
2534 +       
2535 +       if (err == -ENODATA) {
2536 +               memset(buf, 0, EXT3_MAX_SNAP_DATA);
2537 +       } 
2538 +       else if (err < 0) {
2539 +               snap_err("got err %d when reading attributes\n", err);
2540 +               goto err_exit;
2541 +       }
2542 +
2543 +       snaps = (struct snap_ea *) buf;
2544 +
2545 +       if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2546 +               snap_debug("no redirected ino for primary inode %lu\n",
2547 +                               primary->i_ino);
2548 +       }
2549 +       else {
2550 +               snap_debug("primary inode %lu , redirected ino=%d\n",
2551 +                               primary->i_ino,le32_to_cpu(snaps->ino[index]));
2552 +       }
2553 +err_exit:
2554 +       return err;
2555 +}
2556 +
2557 +int snap_print(struct super_block *sb, int index)
2558 +{
2559 +       ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2560 +       return 0;
2561 +}
2562 +
2563 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2564 +{
2565 +       int index = *(int *)index_val;
2566 +       int rc = 0;
2567 +       printk("delete_inode for index %d\n",index);
2568 +       rc = ext3_destroy_indirect(primary,index, NULL);
2569 +       if(rc != 0)     
2570 +               printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",                        
2571 +                                               primary->i_ino, index, rc);
2572 +       return 0;
2573 +}
2574 +
2575 +int ext3_snap_delete(struct super_block *sb, int index)
2576 +{
2577 +       ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index, 
2578 +                                       SNAP_ITERATE_COWED_INODE);
2579 +       return 0;
2580 +}
2581 +#endif
2582 +
2583 +
2584 +
2585 +
2586 +
2587 +
2588 +
2589 +
2590 Index: linux-2.4.20-8/fs/ext3/Makefile
2591 ===================================================================
2592 --- linux-2.4.20-8.orig/fs/ext3/Makefile        2004-01-05 10:54:03.000000000 +0800
2593 +++ linux-2.4.20-8/fs/ext3/Makefile     2004-01-13 00:10:14.000000000 +0800
2594 @@ -13,7 +13,7 @@
2595  
2596  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2597                 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2598 -               xattr_trusted.o
2599 +               xattr_trusted.o snap.o
2600  obj-m    := $(O_TARGET)
2601  
2602  export-objs += xattr.o
2603 Index: linux-2.4.20-8/fs/ext3/inode.c
2604 ===================================================================
2605 --- linux-2.4.20-8.orig/fs/ext3/inode.c 2004-01-05 10:54:03.000000000 +0800
2606 +++ linux-2.4.20-8/fs/ext3/inode.c      2004-01-18 01:48:19.000000000 +0800
2607 @@ -1191,7 +1191,7 @@
2608   * So, if we see any bmap calls here on a modified, data-journaled file,
2609   * take extra steps to flush any blocks which might be in the cache. 
2610   */
2611 -static int ext3_bmap(struct address_space *mapping, long block)
2612 +int ext3_bmap(struct address_space *mapping, long block)
2613  {
2614         struct inode *inode = mapping->host;
2615         journal_t *journal;
2616 Index: linux-2.4.20-8/fs/ext3/ialloc.c
2617 ===================================================================
2618 --- linux-2.4.20-8.orig/fs/ext3/ialloc.c        2004-01-05 10:54:03.000000000 +0800
2619 +++ linux-2.4.20-8/fs/ext3/ialloc.c     2004-01-16 20:48:29.000000000 +0800
2620 @@ -160,6 +160,13 @@
2621         return retval;
2622  }
2623  
2624 +/* Export load_inode_bitmap*/
2625 +int ext3_load_inode_bitmap (struct super_block * sb,
2626 +                           unsigned int block_group)
2627 +{
2628 +       return load_inode_bitmap(sb, block_group);
2629 +}
2630 +
2631  /*
2632   * NOTE! When we get the inode, we're the only people
2633   * that have access to it, and as such there are no
2634 Index: linux-2.4.20-8/fs/ext3/super.c
2635 ===================================================================
2636 --- linux-2.4.20-8.orig/fs/ext3/super.c 2004-01-05 10:54:03.000000000 +0800
2637 +++ linux-2.4.20-8/fs/ext3/super.c      2004-01-18 01:40:10.000000000 +0800
2638 @@ -1324,6 +1324,13 @@
2639         sbi->s_mount_state = le16_to_cpu(es->s_state);
2640         sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2641         sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2642 +#define EXT3_SNAP_FS
2643 +#ifdef EXT3_SNAP_FS
2644 +       init_MUTEX(&(sbi->s_snap_list_sem));
2645 +       sbi->s_snaptable_ino = le32_to_cpu(es->s_snaptable_ino);
2646 +       sbi->s_first_cowed_pri_ino = le32_to_cpu(es->s_first_cowed_pri_ino);
2647 +       sbi->s_last_cowed_pri_ino = le32_to_cpu(es->s_last_cowed_pri_ino);
2648 +#endif 
2649         for (i=0; i < 4; i++)
2650                 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2651         sbi->s_def_hash_version = es->s_def_hash_version;
2652 Index: linux-2.4.20-8/include/linux/snap.h
2653 ===================================================================
2654 --- linux-2.4.20-8.orig/include/linux/snap.h    2003-01-30 18:24:37.000000000 +0800
2655 +++ linux-2.4.20-8/include/linux/snap.h 2004-01-13 00:10:14.000000000 +0800
2656 @@ -0,0 +1,266 @@
2657 +/*
2658 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
2659 + * started by Andreas Dilger <adilger@turbolinux.com>
2660 + *            Peter Braam <braam@mountainviewdata.com>
2661 + *            Harrison Xing <harrisonx@mountainviewdata.com>
2662 + * 
2663 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2664 + *                   Eric Mei    <Ericm@clusterfs.com>
2665 + *                   Wang Di     <wangdi@clusterfs.com>
2666 + * 
2667 + * Rewriten   2003  by Wang Di  <wangdi@clusterfs.com>
2668 + *                    Eric Mei <ericm@clusterfs.com>
2669 + *
2670 + * Functions for implementing snapshots in the ext3 filesystem.  They are
2671 + * intended to hide the internals of the filesystem from the caller in
2672 + * such a way that the caller doesn't need to know about inode numbers,
2673 + * how the redirectors are implemented or stored, etc.  It may not do that
2674 + * all yet, but it tries.
2675 + *
2676 + * The snapshot inode redirection is stored in the primary/direct inode as
2677 + * an extended attribute $snap, in the form of little-endian u32 inode
2678 + * numbers. 
2679 + *   
2680 + */
2681 +
2682 +#ifndef _LINUX_SNAP_H
2683 +#define _LINUX_SNAP_H
2684 +
2685 +#include <linux/fs.h>
2686 +
2687 +/* maximum number of snapshots available for users */
2688 +#define MAX_SNAPS      20      
2689 +
2690 +/* snap extended attributes definition */
2691 +#define SNAP_ATTR      "@snap"
2692 +struct snap_ea{                        
2693 +       int   generation;
2694 +       ino_t prev_ino;
2695 +       ino_t next_ino;
2696 +       ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2697 +       ino_t parent_ino[MAX_SNAPS+1];
2698 +};
2699 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
2700 +#if 0
2701 +/* for compatibility with old 128 max snapshots */
2702 +#define MAX_SNAP128_DATA (sizeof(struct snap_ea) - (sizeof(ino_t) * 128 * 2))
2703 +#define ZERO_SNAP_ATTR_TOP(buf)                                                \
2704 +       do {                                                            \
2705 +               struct snap_ea *p = (struct snap_ea*)buf;               \
2706 +               memset(&p->ino[129], 0, sizeof(ino_t)*128);             \
2707 +               memset(&p->parent_ino[129], 0, sizeof(ino_t)*128);      \
2708 +       } while(0)
2709 +
2710 +/* snap new ea definition , for logging of new inode */
2711 +#define SNAP_NEW_INO_ATTR      "@snap_new"
2712 +struct snap_new_ea{                    
2713 +       ino_t prev_ino; /* reserved. save the inode to a linked list */
2714 +       ino_t next_ino;
2715 +       int new_index;  /* indicate for which index this is a new inode */
2716 +};
2717 +#define NULL_NEW_INDEX -1      /* null new index, to clear the snap_new_ea */
2718 +
2719 +/* ea to identiry a indirect inode's infomation */
2720 +#define SNAP_INDIRECT_INFO_ATTR        "@snap_indirect_inode_info"
2721 +struct snap_indirect_info {
2722 +       __u32 index; /* which index belongs to */
2723 +       __u32 reserved[3]; /* reserved */
2724 +};
2725 +#endif
2726 +
2727 +/* snapfs meta data stored in extended attributes of root ino */
2728 +#define DISK_SNAP_META_ATTR    "@disk_snap_meta_attr"
2729 +struct disk_snap_meta_data {
2730 +       ino_t snap_first_cowed_ino;
2731 +       ino_t snap_table_ino;
2732 +       __u32 snap_feature_compat;
2733 +};
2734 +/*snapfs quota info */
2735 +
2736 +#define SNAP_USR_QUOTA                 0
2737 +#define SNAP_GRP_QUOTA         1
2738 +#define DISK_SNAP_QUOTA_INFO   "@disk_snap_quota_info"
2739 +struct quota_info_len {
2740 +       int     uid_len;     /*uid quota info length */
2741 +       int     gid_len;     /*gid quota info length */
2742 +};
2743 +/*
2744 + * Check if the EA @name is Snap EA or not.
2745 + * Snap EA includes the SNAP_ATTR, SNAP_NEW_INO_ATTR and DISK_SNAP_META_ATTR
2746 + */
2747 +
2748 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) ||             \
2749 +                          (!strcmp((name), DISK_SNAP_META_ATTR)))
2750 +
2751 +
2752 +
2753 +/* file system features */
2754 +#define SNAP_FEATURE_COMPAT_SNAPFS              0x0010
2755 +#define SNAP_FEATURE_COMPAT_BLOCKCOW            0x0020
2756 +
2757 +/* constants for snap_feature operations */
2758 +#define SNAP_CLEAR_FEATURE     0x0
2759 +#define SNAP_SET_FEATURE       0x1
2760 +#define SNAP_HAS_FEATURE       0x2
2761 +
2762 +/* snap flags for inode, within 1 byte range, each occupy 1 bit */
2763 +#define SNAP_INO_MAGIC 0x88            /* magic for snap inode */
2764 +#define SNAP_COW_FLAG  0x01            /* snap redirected inode */
2765 +#define SNAP_DEL_FLAG  0x02            /* snap deleted inode */
2766 +#define SNAP_TABLE_FLAG        0x04            /* snap table inode */
2767 +#define SNAP_PRI_FLAG  0x08            /* primary inode */
2768 +
2769 +/* no snapfs attributes for get_indirect_ino */
2770 +#define ENOSNAPATTR    320
2771 +
2772 +/* constants used by iterator */
2773 +#define SNAP_ITERATE_ALL_INODE          0x0
2774 +#define SNAP_ITERATE_COWED_INODE        0x1
2775 +
2776 +/* constants used by create_indirect */
2777 +#define SNAP_CREATE_IND_NORMAL         0x0
2778 +#define        SNAP_CREATE_IND_DEL_PRI         0x1
2779 +
2780 +/* the data structure represent in the xfs_dinode.pad
2781 +       offset  0:      magic   (1 byte)
2782 +       offset  1:      flag    (1 byte)
2783 +       offset  2:      gen     (4 bytes)
2784 +       offset  6:      unused
2785 + */
2786 +#define SIZEOF_MAGIC           1
2787 +#define SIZEOF_FLAG            1
2788 +#define SIZEOF_GENERATION      4
2789 +
2790 +#define MAGIC_OFFSET           0
2791 +#define FLAG_OFFSET            1
2792 +#define GENERATION_OFFSET      2
2793 +
2794 +#define SNAP_GET_DINODE_MAGIC(dinode)  \
2795 +               (((__u8*)(dinode)->di_pad)[MAGIC_OFFSET])
2796 +#define SNAP_SET_DINODE_MAGIC(dinode)  \
2797 +               ((__u8*)(dinode)->di_pad)[MAGIC_OFFSET] = (SNAP_INO_MAGIC)
2798 +#define SNAP_GET_DINODE_FLAG(dinode)   \
2799 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET])
2800 +#define SNAP_SET_DINODE_FLAG(dinode, flag)     \
2801 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] |= (flag))
2802 +#define SNAP_CLEAR_DINODE_FLAG(dinode, flag)   \
2803 +               (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] &= ~(flag))
2804 +#define SNAP_GET_DINODE_GEN(dinode)    \
2805 +               (le32_to_cpu(*(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET])))
2806 +#define SNAP_SET_DINODE_GEN(dinode, gen)       \
2807 +               *(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET]) = cpu_to_le32(gen)
2808 +
2809 +#if 0
2810 +/* header of saving snaptable */
2811 +struct raw_data {
2812 +       unsigned int size;      /* buffer size passed by */
2813 +       char data[0];           /* followed by actual data */
2814 +};
2815 +
2816 +/* header of on-disk table data */
2817 +struct disk_snap_table_header {
2818 +       __u32   magic;
2819 +       __u32   version;
2820 +       __u32   datasize;
2821 +};
2822 +
2823 +/* table magic and version constant */
2824 +#define SNAP_TABLE_MAGIC       0xB3A2957F
2825 +#define SNAP_TABLE_VERSION     1
2826 +
2827 +
2828 +#define SNAPTABLE_BLOCKS(sb,size)      \
2829 +               (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
2830 +               >> sb->s_blocksize_bits)+1)
2831 +#endif
2832 +
2833 +#define SNAP_VERSION(a,b,c)            \
2834 +               (((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF))
2835 +#define SNAP_VERSION_MAJOR(v)          \
2836 +               ((v >> 16) & 0xFF)
2837 +#define SNAP_VERSION_MINOR(v)          \
2838 +               ((v >> 8) & 0xFF)
2839 +#define SNAP_VERSION_REL(v)            \
2840 +               (v & 0xFF)
2841 +
2842 +/* for snap meta attr table */
2843 +#define TABLE_ITEM_COUNT       200
2844 +#define TABLE_ITEM_SIZE                1000
2845 +#define TABLE_ITEM_NAME_SIZE   16
2846 +
2847 +/*snap table array */
2848 +struct snap_meta_array {
2849 +       char    name[TABLE_ITEM_NAME_SIZE];
2850 +       int     start;  /* where is the start of the array */
2851 +       int     len;  /* the len of the array */
2852 +}; 
2853 +/* snap table structure for record the information */
2854 +struct table_snap_meta_data {
2855 +       int                     count;
2856 +       struct snap_meta_array  array[TABLE_ITEM_COUNT]; 
2857 +};
2858 +
2859 +
2860 +#if 0
2861 +#define SNAP_PROFILE
2862 +#else
2863 +#undef SNAP_PROFILE
2864 +#endif
2865 +
2866 +#ifdef SNAP_PROFILE
2867 +struct profile_snapdel_stat
2868 +{
2869 +       unsigned long total_tick;               /* total time */
2870 +       unsigned long inodes;                   /* primary inodes */
2871 +
2872 +       unsigned long yield_count;              /* for yeild cpu */
2873 +       unsigned long yield_tick;
2874 +       unsigned long yield_max_tick;
2875 +
2876 +       unsigned long getea_count;              /* for get ea */
2877 +       unsigned long getea_tick;
2878 +       unsigned long getea_max_tick;
2879 +
2880 +       unsigned long setea_count;              /* for set ea */
2881 +       unsigned long setea_tick;
2882 +       unsigned long setea_max_tick;
2883 +
2884 +       unsigned long converge_count;           /* for converge */
2885 +       unsigned long converge_tick;
2886 +       unsigned long converge_max_tick;
2887 +};
2888 +
2889 +#endif
2890 +
2891 +/* snapshot operations */
2892 +struct snapshot_operations {
2893 +       unsigned int ops_version;
2894 +        int (*is_redirector) (struct inode *inode);
2895 +       int (*is_indirect) (struct inode *inode);
2896 +        struct inode * (*create_indirect) (struct inode *pri, int index,
2897 +                                          unsigned int gen, ino_t parent_ino,
2898 +                                          int del);
2899 +        struct inode * (*get_indirect) (struct inode *pri, int *table,int slot);
2900 +        ino_t (*get_indirect_ino) (struct inode *pri, int index);
2901 +        int (*destroy_indirect) (struct inode *pri, int index, 
2902 +                               struct inode *next_ind);
2903 +        int (*restore_indirect) (struct inode *pri, int index);
2904 +        int (*iterate) (struct super_block *sb,
2905 +                        int (*repeat)(struct inode *inode, void *priv),
2906 +                        struct inode **start, void *priv, int flag);
2907 +        int (*copy_block) ( struct inode *dst, struct inode *src, int blk);
2908 +       int (*has_block) (struct inode *dst, int blk);
2909 +       int (*set_indirect) (struct inode *pri, int index, 
2910 +                            ino_t ind_ino, ino_t parent_ino );
2911 +       int (*snap_feature) (struct super_block *sb, int feature, int op);
2912 +       int (*get_generation) (struct inode *pri);
2913 +       int (*set_generation) (struct inode *pri, unsigned long new_gen);
2914 +       int (*has_del_flag) (struct inode *inode);
2915 +       int (*clear_del_flag) (struct inode *inode);
2916 +       int (*set_meta_attr)(struct super_block *sb, char *name,
2917 +                               char *buf, int size);
2918 +       int (*get_meta_attr)(struct super_block *sb, char *name,
2919 +                               char *buf, int *size);
2920 +};
2921 +
2922 +#endif
2923 Index: linux-2.4.20-8/include/linux/ext3_fs.h
2924 ===================================================================
2925 --- linux-2.4.20-8.orig/include/linux/ext3_fs.h 2004-01-05 10:54:03.000000000 +0800
2926 +++ linux-2.4.20-8/include/linux/ext3_fs.h      2004-01-16 20:45:09.000000000 +0800
2927 @@ -183,7 +183,13 @@
2928  #define EXT3_INDEX_FL                  0x00001000 /* hash-indexed directory */
2929  #define EXT3_IMAGIC_FL                 0x00002000 /* AFS directory */
2930  #define EXT3_JOURNAL_DATA_FL           0x00004000 /* file data should be journaled */
2931 -#define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
2932 +/* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
2933 +#define EXT3_COW_FL                    0x00008000 /* inode is snapshot cow */
2934 +#define EXT3_DEL_FL                    0x00010000 /* inode is deleting in snapshot */
2935 +#define EXT3_SNAP_TABLE_FLAG                   0x00020000 /* snap table inode */
2936 +/* FIXME For debugging will be removed later*/
2937 +#define EXT3_SNAP_PRI_FLAG             0x00040000 /* primary inode */
2938 +
2939  
2940  #define EXT3_FL_USER_VISIBLE           0x00005FFF /* User visible flags */
2941  #define EXT3_FL_USER_MODIFIABLE                0x000000FF /* User modifiable flags */
2942 @@ -205,10 +211,25 @@
2943  /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2944  #define        EXT3_IOC_GETVERSION_OLD         _IOR('v', 1, long)
2945  #define        EXT3_IOC_SETVERSION_OLD         _IOW('v', 2, long)
2946 +/* the following are for temporary test */
2947 +/* snapfs ioctls */
2948 +#define EXT3_IOC_CREATE_INDIR           _IOW('v', 3, long)
2949 +#define EXT3_IOC_GET_INDIR              _IOW('v', 4, long)
2950 +#define EXT3_IOC_DESTROY_INDIR          _IOW('v', 5, long)
2951 +#define EXT3_IOC_IS_REDIR               _IOW('v', 6, long)
2952 +#define EXT3_IOC_RESTORE_INDIR          _IOW('v', 7, long)
2953 +
2954 +#define EXT3_IOC_SNAP_SETFILECOW       _IOW('v', 10, long)
2955 +
2956 +/* XXX: the following are for temporary test, can be removed later */
2957 +#define EXT3_IOC_SNAP_PRINT             _IOW('v', 11, long)
2958 +#define EXT3_IOC_SNAP_DELETE            _IOW('v', 12, long)
2959 +#define EXT3_IOC_SNAP_RESTORE           _IOW('v', 13, long)
2960 +
2961 +
2962  #ifdef CONFIG_JBD_DEBUG
2963  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
2964  #endif
2965 -
2966  /*
2967   * Structure of an inode on the disk
2968   */
2969 @@ -429,7 +450,15 @@
2970         __u8    s_def_hash_version;     /* Default hash version to use */
2971         __u8    s_reserved_char_pad;
2972         __u16   s_reserved_word_pad;
2973 -       __u32   s_reserved[192];        /* Padding to the end of the block */
2974 +       __u32   s_default_mount_opts;
2975 +        __u32   s_first_meta_bg;        /* First metablock group */
2976 +       __u32   s_mkfs_time;            /* When the filesystem was created */
2977 +       /* for snapfs */
2978 +       __u32   s_first_cowed_pri_ino;  /* For snapfs,the first cowed primary inode */
2979 +       __u32   s_last_cowed_pri_ino;     /* last cowed ino in memory */
2980 +       __u32   s_snaptable_ino;        /* snaptable ino in memory */
2981 +       __u32   s_last_snap_orphan;     /* SnapFS: start of cowing indirect inode */
2982 +       __u32   s_reserved[186];        /* Padding to the end of the block,originally 204 */
2983  };
2984  
2985  #ifdef __KERNEL__
2986 @@ -503,6 +532,9 @@
2987  #define EXT3_FEATURE_INCOMPAT_RECOVER          0x0004 /* Needs recovery */
2988  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
2989  
2990 +#define EXT3_FEATURE_COMPAT_SNAPFS             0x0010
2991 +#define EXT3_FEATURE_COMPAT_BLOCKCOW           0x0020
2992 +
2993  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
2994  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2995                                          EXT3_FEATURE_INCOMPAT_RECOVER)
2996 Index: linux-2.4.20-8/include/linux/ext3_fs_sb.h
2997 ===================================================================
2998 --- linux-2.4.20-8.orig/include/linux/ext3_fs_sb.h      2004-01-05 10:54:00.000000000 +0800
2999 +++ linux-2.4.20-8/include/linux/ext3_fs_sb.h   2004-01-13 00:10:14.000000000 +0800
3000 @@ -86,6 +86,13 @@
3001         wait_queue_head_t s_delete_thread_queue;
3002         wait_queue_head_t s_delete_waiter_queue;
3003  #endif
3004 +#define EXT3_SNAP_FS
3005 +#ifdef EXT3_SNAP_FS
3006 +       struct semaphore s_snap_list_sem;
3007 +       unsigned long   s_first_cowed_pri_ino;/* For snapfs,the first cowed primary inode */
3008 +       unsigned long   s_last_cowed_pri_ino;     /* last cowed ino in memory */
3009 +       unsigned long   s_snaptable_ino;      /* snaptable ino in memory */
3010 +#endif
3011  };
3012  
3013  #endif /* _LINUX_EXT3_FS_SB */
3014 Index: linux-2.4.20-8/include/linux/ext3_jbd.h
3015 ===================================================================
3016 --- linux-2.4.20-8.orig/include/linux/ext3_jbd.h        2004-01-05 10:53:59.000000000 +0800
3017 +++ linux-2.4.20-8/include/linux/ext3_jbd.h     2004-01-13 00:10:14.000000000 +0800
3018 @@ -71,6 +71,33 @@
3019  
3020  #define EXT3_INDEX_EXTRA_TRANS_BLOCKS  8
3021  
3022 +/*snapshot transaction blocks*/
3023 +
3024 +#define EXT3_EA_TRANS_BLOCKS           EXT3_DATA_TRANS_BLOCKS
3025 +#define EXT3_SETMETA_TRANS_BLOCKS      EXT3_DATA_TRANS_BLOCKS
3026 +#define EXT3_NEWINODE_TRANS_BLOCKS     10
3027 +#define SNAP_INSERTLIST_TRANS_BLOCKS   (2 * EXT3_EA_TRANS_BLOCKS + 1)
3028 +#define SNAP_DELETELIST_TRANS_BLOCKS   (2 * EXT3_EA_TRANS_BLOCKS + 2)
3029 +#define SNAP_COPYBLOCK_TRANS_BLOCKS    (EXT3_DATA_TRANS_BLOCKS)
3030 +#define SNAP_MIGRATEDATA_TRANS_BLOCKS  2
3031 +#define SNAP_SETIND_TRANS_BLOCKS       (SNAP_INSERTLIST_TRANS_BLOCKS + 1)
3032 +#define SNAP_ADDORPHAN_TRANS_BLOCKS    2
3033 +#define SNAP_REMOVEORPHAN_TRANS_BLOCKS 1
3034 +#define SNAP_RESTOREORPHAN_TRANS_BLOCKS        (EXT3_EA_TRANS_BLOCKS + \
3035 +                                        SNAP_DELETELIST_TRANS_BLOCKS + \
3036 +                                        EXT3_NEWINODE_TRANS_BLOCKS + \
3037 +                                        2 * SNAP_MIGRATEDATA_TRANS_BLOCKS)
3038 +#define SNAP_BIGCOPY_TRANS_BLOCKS      (2 * EXT3_DATA_TRANS_BLOCKS)
3039 +#define SNAP_CREATEIND_TRANS_BLOCKS    (EXT3_NEWINODE_TRANS_BLOCKS + \
3040 +                                        SNAP_MIGRATEDATA_TRANS_BLOCKS + \
3041 +                                        SNAP_SETIND_TRANS_BLOCKS + \
3042 +                                        SNAP_BIGCOPY_TRANS_BLOCKS + 3)
3043 +#define SNAP_MIGRATEBLK_TRANS_BLOCKS   2
3044 +#define SNAP_DESTROY_TRANS_BLOCKS      (SNAP_DELETELIST_TRANS_BLOCKS + \
3045 +                                        EXT3_EA_TRANS_BLOCKS + 2)
3046 +#define SNAP_RESTORE_TRANS_BLOCKS      (EXT3_NEWINODE_TRANS_BLOCKS + \
3047 +                                        2 * SNAP_MIGRATEDATA_TRANS_BLOCKS + 1)
3048 +
3049  int
3050  ext3_mark_iloc_dirty(handle_t *handle, 
3051                      struct inode *inode,
3052
3053 %diffstat
3054  fs/ext3/Makefile           |    2 
3055  fs/ext3/ialloc.c           |    7 
3056  fs/ext3/inode.c            |    2 
3057  fs/ext3/snap.c             | 2583 +++++++++++++++++++++++++++++++++++++++++++++
3058  fs/ext3/super.c            |    7 
3059  include/linux/ext3_fs.h    |   38 
3060  include/linux/ext3_fs_sb.h |    7 
3061  include/linux/ext3_jbd.h   |   27 
3062  include/linux/snap.h       |  266 ++++
3063  9 files changed, 2934 insertions(+), 5 deletions(-)
3064