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