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