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