2 Index: linux-2.4.20-8/fs/ext3/snap.c
3 ===================================================================
4 --- linux-2.4.20-8.orig/fs/ext3/snap.c 2003-01-30 18:24:37.000000000 +0800
5 +++ linux-2.4.20-8/fs/ext3/snap.c 2004-01-19 23:24:39.000000000 +0800
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>
15 + * port to 2.4 by Wang Di <wangdi@clusterfs.com>
16 + * Eric Mei <ericm@clusterfs.com>
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.
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
30 +#define EXPORT_SYMTAB
31 +#include <linux/module.h>
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>
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
49 +#ifdef EXT3_SNAP_DEBUG
50 + #define snap_debug(f, a...) \
52 + printk (KERN_INFO "SNAP DEBUG: (%s, %d): %s: ", \
53 + __FILE__, __LINE__, __FUNCTION__); \
57 + #define snap_err(f, a...) \
59 + printk (KERN_ERR "SNAP ERROR: (%s, %d): %s: ", \
60 + __FILE__, __LINE__, __FUNCTION__); \
65 + #define snap_debug(f, a...) do {} while (0)
66 + #define snap_err(f, a...) \
68 + printk (KERN_ERR "SNAP ERROR: (%s, %d): ", \
69 + __FILE__, __LINE__); \
75 +#ifdef EXT3_SNAP_DEBUG
76 + #define ALLOC(ptr, cast, size) \
78 + ptr = (cast)kmalloc((size_t) size, GFP_KERNEL); \
80 + printk(KERN_ERR "kmalloc returns 0 at %s:%d\n", \
81 + __FILE__, __LINE__); \
83 + snap_kmem += size; \
84 + printk(KERN_INFO "snap_alloc %d, kmem %ld\n", \
85 + (size_t)size, snap_kmem); \
89 + #define FREE(ptr,size) \
92 + snap_kmem -= size; \
93 + printk(KERN_INFO "snap_free %d, kmem %ld\n", \
94 + (size_t)size, snap_kmem); \
98 + #define ALLOC(ptr, cast, size) \
100 + ptr = (cast)kmalloc((size_t) size, GFP_KERNEL); \
103 + #define FREE(ptr,size) \
108 +#endif /* EXT3_SNAP_DEBUG */
110 +#ifdef EXT3_SNAP_DEBUG
111 + /* modestr: convert inode mode to string . debug function */
112 + static char * modestr ( umode_t mode )
114 + if( S_ISREG(mode) )
116 + else if(S_ISDIR(mode))
118 + else if(S_ISLNK(mode))
120 + else if(S_ISCHR(mode))
122 + else if(S_ISBLK(mode))
124 + else if(S_ISFIFO(mode))
126 + else if(S_ISSOCK(mode))
129 + return "non-known";
131 +#define DEBUG_INODE(inode) \
132 + if(inode && !IS_ERR(inode)) { \
133 + snap_debug("%s ino %lu, i_nlink %u, i_count %d, i_mode %u, i_size %lld, i_blocks %lu\n", \
134 + modestr(inode->i_mode), inode->i_ino, inode->i_nlink, \
135 + atomic_read(&inode->i_count), inode->i_mode, inode->i_size, \
136 + inode->i_blocks); }
138 + #define modestr(mode) do {} while (0)
139 + #define DEBUG_INODE(inode)
141 +#endif /* EXT3_SNAP_DEBUG */
142 +/* do file cow on: dir, symlink, regular but fs has filecow flag */
144 +#define IS_FILECOW_TYPE(inode) \
145 + (S_ISDIR(inode->i_mode) || \
146 + S_ISLNK(inode->i_mode) || \
147 + (S_ISREG(inode->i_mode) && \
148 + !SNAP_HAS_COMPAT_FEATURE(inode->i_sb, SNAP_FEATURE_COMPAT_BLOCKCOW)))
150 +#define SNAP_ERROR(err) ((err) < 0 ? (err) : (-(err)))
151 +/* SNAP_ERROR(err): Make sure we return negative errors for Linux ( return positive errors) */
156 +# define CDEBUG_STACK (THREAD_SIZE - \
157 + ((unsigned long)__builtin_dwarf_cfa() & \
158 + (THREAD_SIZE - 1)))
160 +# define CDEBUG_STACK (THREAD_SIZE - \
161 + ((unsigned long)__builtin_frame_address(0) & \
162 + (THREAD_SIZE - 1)))
165 +#define snap_debug_msg(file, fn, line, stack, format, a...) \
166 + printf("(%s:%s,l. %d %d %lu): " format, file, fn, line, \
167 + getpid() , stack, ## a);
170 +#define CDEBUG(mask, format, a...) \
172 + CHECK_STACK(CDEBUG_STACK); \
173 + if (!(mask) || ((mask) & (D_ERROR | D_EMERG))) \
174 + snap_debug_msg(__FILE__, __FUNCTION__, __LINE__, \
175 + CDEBUG_STACK, format, ## a); \
178 +#define CWARN (format, a...) CDEBUG(D_WARNING, format, ## a)
179 +#define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
180 +#define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
182 +#define RETURN(rc) \
184 + typeof(rc) RETURN__ret = (rc); \
185 + CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n", \
186 + (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
187 + return RETURN__ret; \
192 + CDEBUG(D_TRACE, "Process entered\n"); \
197 + CDEBUG(D_TRACE, "Process leaving\n"); \
200 +#define CDEBUG(mask, format, a...) do { } while (0)
201 +#define CWARN(format, a...) do { } while (0)
202 +#define CERROR(format, a...) printk("<3>" format, ## a)
203 +#define CEMERG(format, a...) printk("<0>" format, ## a)
204 +#define GOTO(label, rc) do { (void)(rc); goto label; } while (0)
205 +#define RETURN(rc) return (rc)
206 +#define ENTRY do { } while (0)
207 +#define EXIT do { } while (0)
210 +#define SNAP_ATTR_BUF_CNT 10
212 +#define SB_LAST_COWED_INO(sb) (EXT3_SB(sb)->s_es->s_last_cowed_pri_ino)
213 +#define SB_FIRST_COWED_INO(sb) (EXT3_SB(sb)->s_es->s_first_cowed_pri_ino)
214 +#define SB_SNAPTABLE_INO(sb) (EXT3_SB(sb)->s_es->s_snaptable_ino)
215 +#define SB_SNAP_LIST_SEM(sb) (EXT3_SB(sb)->s_snap_list_sem)
216 +#define SB_FEATURE_COMPAT(sb) (EXT3_SB(sb)->s_es->s_feature_compat)
218 +#define SNAP_HAS_COMPAT_FEATURE(sb,mask) \
219 + (SB_FEATURE_COMPAT(sb) & cpu_to_le32(mask))
221 +/* NOTE: these macros are close dependant on the structure of snap ea */
222 +#define SNAP_CNT_FROM_SIZE(size) ((((size)-sizeof(ino_t)*2)/2)/sizeof(ino_t))
223 +#define SNAP_EA_SIZE_FROM_INDEX(index) (sizeof(ino_t)*2 + 2*sizeof(ino_t)*((index)+1))
225 +#define SNAP_EA_INO_BLOCK_SIZE(size) (((size)-sizeof(ino_t)*2)/2)
226 +#define SNAP_EA_PARENT_OFFSET(size) (sizeof(ino_t)*2 + SNAP_EA_INO_BLOCK_SIZE((size)))
228 +extern int ext3_bmap(struct address_space *mapping, long block);
229 +extern int ext3_load_inode_bitmap (struct super_block * sb, unsigned int block_group);
230 +/* helper functions to manipulate field 'parent' in snap_ea */
233 +set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
235 + char * p = (char*) pea;
238 + offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
239 + offset += sizeof(ino_t) * index;
240 + *(ino_t*)(p+offset) = val;
245 +get_parent_ino(struct snap_ea *pea, int size, int index)
247 + char * p = (char*)pea;
250 + offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
251 + offset += sizeof(ino_t) * index;
252 + return *(ino_t*)(p+offset);
254 +static inline void snap_double_lock(struct inode *i1, struct inode *i2)
256 + double_down(&i1->i_sem, &i2->i_sem);
259 +static inline void snap_double_unlock(struct inode *i1, struct inode *i2)
261 + double_up(&i1->i_sem, &i2->i_sem);
264 +/* ext3_iterate_cowed_inode:
265 + * iterate all the cowed inode with the same index and
266 + * run the associate function @repeat
268 + * For @repeat, if it returns non-zero value, it will exit the iterator
270 + * return value: 0 or positive: success
271 + * negative: failure
272 + * additional: if the return value is positive, it must be the return value
273 + * of function @repeat.
276 +static int ext3_iterate_cowed_inode(
277 + struct super_block *sb,
278 + int (*repeat)(struct inode *inode, void *priv),
279 + struct inode **start,
282 + struct inode *list_inode = NULL;
283 + char buf[EXT3_MAX_SNAP_DATA];
284 + struct snap_ea *snaps;
287 + if (SB_FIRST_COWED_INO(sb) == 0) {
288 + snap_debug("no cowed inode in the list\n");
292 + /* get head inode in the list */
293 + if (start != NULL && *start != NULL && (*start)->i_ino)
294 + list_inode = iget(sb, (*start)->i_ino);
296 + list_inode = iget (sb, le32_to_cpu( SB_FIRST_COWED_INO(sb) ));
298 + /* loop for all inode in list */
299 + while (list_inode) {
300 + if (!list_inode->i_nlink || is_bad_inode(list_inode)) {
301 + snap_err("inode %p, ino %lu, mode %o, nlink %d\n",
304 + list_inode->i_mode,
305 + list_inode->i_nlink);
310 + err = ext3_xattr_get(list_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
311 + buf, EXT3_MAX_SNAP_DATA);
312 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
313 + snap_err("inode %lu, error %d\n", list_inode->i_ino, err);
317 + if ((err = (*repeat)(list_inode, priv)) != 0)
322 + snaps = (struct snap_ea *) buf;
323 + if (le32_to_cpu (snaps->next_ino) != 0) {
324 + list_inode = iget(sb, le32_to_cpu(snaps->next_ino));
327 + snap_debug ("cowed inode list end, exit\n");
337 +static int get_cowed_ino(struct inode *pri, void *param)
339 + ino_t *find = param;
340 + (*find) = pri->i_ino;
344 +/* Return 0 for error. */
345 +static int get_cowed_ino_end (struct inode *inode)
350 + rc = ext3_iterate_cowed_inode(inode->i_sb, &get_cowed_ino, &inode, &ino);
358 +/* find the end of the primary inode, iterate if needed
359 + * return 0 if any error found */
360 +static inline ino_t find_last_cowed_ino(struct super_block *sb)
362 + struct inode *inode = NULL;
363 + ino_t first, last = 0;
365 + last = le32_to_cpu(SB_LAST_COWED_INO(sb));
369 + first = le32_to_cpu(SB_FIRST_COWED_INO(sb));
372 + snap_err("first cowed inode is NULL\n");
376 + inode = iget(sb, first);
378 + if (is_bad_inode(inode)) {
379 + snap_err("bad inode %lu\n", first);
383 + last = get_cowed_ino_end(inode);
391 +/* Insert the primary inode to the cowed inode list
392 + * Append it to the list end
394 + * @pri: inode to insert
395 + * @buf_pri: the valid ea buf for @pri inode ( excluding the next_ino field) ,
396 + * it's used to write the ea for @pri inode
398 + * To avoid list broken in abnormal case, it will first write the ea for @pri
399 + * inode, and then write ea for the list end inode. Thus list broken is
400 + * avoid even if there are errors when writting ea.
402 +static int insert_cowed_ino_to_list (handle_t *handle, struct inode *pri, char *buf_pri)
404 + char buf[EXT3_MAX_SNAP_DATA];
405 + struct snap_ea *snaps;
406 + struct snap_ea *snaps_pri;
407 + struct inode *last_inode = NULL;
408 + struct ext3_sb_info *sbi = EXT3_SB(pri->i_sb);
411 + snaps_pri = (struct snap_ea *)buf_pri;
413 + if (!SB_FIRST_COWED_INO(pri->i_sb)) {
414 + /* we set the next_ino and write ea for pri inode */
415 + snaps_pri->next_ino = cpu_to_le32(0);
416 + snaps_pri->prev_ino = cpu_to_le32(0);
418 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
419 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
421 + snap_err("ino %lu, set_ext_attr err %d\n", pri->i_ino, err);
424 + lock_super(pri->i_sb);
425 + ext3_journal_get_write_access(handle, sbi->s_sbh);
426 + sbi->s_es->s_first_cowed_pri_ino = cpu_to_le32(pri->i_ino);
427 + SB_FIRST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
428 + pri->i_sb->s_dirt = 1;
429 + ext3_journal_dirty_metadata(handle, sbi->s_sbh);
430 + unlock_super(pri->i_sb);
431 + EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG;
435 + if (!SB_LAST_COWED_INO(pri->i_sb)){
436 + SB_LAST_COWED_INO(pri->i_sb) = find_last_cowed_ino(pri->i_sb);
437 + if (!SB_LAST_COWED_INO(pri->i_sb) ){
438 + snap_err("error, last cowed inode is NULL\n");
443 + last_inode = iget(pri->i_sb, SB_LAST_COWED_INO(pri->i_sb));
444 + if (!last_inode || is_bad_inode(last_inode)) {
448 + err = ext3_xattr_get(last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
449 + buf, EXT3_MAX_SNAP_DATA);
450 + if (err == -ENODATA) {
451 + snap_debug("no existing attributes - zeroing\n");
452 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
453 + } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
454 + snap_debug("got err %d when reading attributes\n", err);
457 + /*set primary inode EA*/
458 + snaps_pri->next_ino = 0;
459 + snaps_pri->prev_ino = cpu_to_le32(last_inode->i_ino);
461 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
462 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
464 + snap_debug("set attributes error for inode %lu\n",
465 + (ulong)pri->i_ino);
469 + /*set last inode EA*/
470 + snaps = (struct snap_ea *) buf;
471 + snaps->next_ino = cpu_to_le32(pri->i_ino);
472 + err = ext3_xattr_set(handle, last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
473 + buf, EXT3_MAX_SNAP_DATA, 0);
475 + snap_debug("set attributes error for inode %lu\n",
476 + (ulong)last_inode->i_ino);
480 + EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG;
482 + /* we update the new cowed ino list end in memory */
483 + SB_LAST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
484 + snap_debug("cowed_inode_list_end %lu, append ino=%lu\n",
485 + last_inode->i_ino, pri->i_ino);
493 +/* delelte the ino from cowed inode list */
494 +static int delete_cowed_ino_from_list (handle_t *handle, struct inode *inode)
496 + ino_t prev_ino = 0, next_ino = 0;
497 + struct inode *prev_inode = NULL;
498 + struct inode *next_inode = NULL;
499 + struct snap_ea *snaps;
500 + char buf[EXT3_MAX_SNAP_DATA];
503 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
504 + buf, EXT3_MAX_SNAP_DATA);
505 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
506 + snap_err("get attr inode %lu, error %d\n", inode->i_ino, err);
510 + snaps = (struct snap_ea *) buf;
511 + next_ino = le32_to_cpu(snaps->next_ino);
512 + prev_ino = le32_to_cpu(snaps->prev_ino);
514 + /* if this is the first cowed ino */
515 + if (inode->i_ino == le32_to_cpu(SB_FIRST_COWED_INO(inode->i_sb))) {
516 + SB_FIRST_COWED_INO(inode->i_sb) = cpu_to_le32(next_ino);
517 + EXT3_I(inode)->i_flags &= ~EXT3_SNAP_PRI_FLAG;
523 + /* find previous inode and read its ea */
524 + prev_inode = iget(inode->i_sb, prev_ino);
525 + if (!prev_inode || is_bad_inode(prev_inode))
528 + err = ext3_xattr_get(prev_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
529 + buf, EXT3_MAX_SNAP_DATA);
530 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
531 + snap_err("get attr inode %lu, error %d\n", prev_inode->i_ino, err);
535 + /* make the previous inode point to the next inode,
536 + * but ignore errors because at current version we
537 + * didn't use the previous pionter */
538 + snaps = (struct snap_ea *) buf;
539 + snaps->next_ino = cpu_to_le32(next_ino);
541 + snap_debug("delete ino %lu from list\n", inode->i_ino);
543 + err = ext3_xattr_set(handle, prev_inode, EXT3_SNAP_INDEX,
544 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
546 + snap_err("err %d setting ea for ino %lu\n", err, prev_inode->i_ino);
550 + if (next_ino == 0) {
551 + SB_LAST_COWED_INO(inode->i_sb) = prev_ino;
555 + /* make the next inode point to the previous one */
556 + next_inode = iget(inode->i_sb, next_ino);
557 + if (!next_inode || is_bad_inode(next_inode))
560 + err = ext3_xattr_get(next_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
561 + buf, EXT3_MAX_SNAP_DATA);
562 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
563 + snap_err("set attr inode %lu, error %d\n", next_inode->i_ino, err);
566 + snaps = ( struct snap_ea *) buf;
567 + snaps->prev_ino = cpu_to_le32(prev_ino);
569 + err = ext3_xattr_set(handle, next_inode, EXT3_SNAP_INDEX,
570 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
572 + snap_err("err %d setting attributes for ino %lu\n",
573 + err, next_inode->i_ino);
582 +static inline void lock_list(struct super_block *sb)
584 + down(&SB_SNAP_LIST_SEM(sb));
587 +static inline void unlock_list(struct super_block *sb)
589 + up(&SB_SNAP_LIST_SEM(sb));
592 +static int ext3_snap_feature (struct super_block *sb, int feature, int op) {
597 + case SNAP_SET_FEATURE:
598 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
600 + ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
601 + SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
603 + ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
605 + ext3_journal_stop(handle, sb->s_root->d_inode);
607 + case SNAP_CLEAR_FEATURE:
608 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
610 + ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
611 + SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature);
612 + ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
615 + ext3_journal_stop(handle, sb->s_root->d_inode);
617 + case SNAP_HAS_FEATURE:
618 + /*FIXME should lock super or not*/
619 + rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
627 +#ifdef _DEVICE_FAIL_TEST
629 +extern int loop_discard_io(kdev_t dev, long arg);
631 + * modify failpos to let loop fail at certain point
632 + * let pos=0 mean no fail point
634 +static int failpos = 0;
635 +#define loopfail(pos) \
637 + if( pos == failpos ){ \
639 + printk(KERN_EMERG "SNAP; hit fail point %d\n", failpos);\
640 + for( i=0; i<15; i++ ) \
641 + loop_discard_io( MKDEV(7,i), 1 ); \
645 +#define loopfail(pos) do{}while(0)
648 +/* Save the indirect inode in the snapshot table of the primary inode. */
649 +static int ext3_set_indirect(struct inode *pri, int index, ino_t ind_ino, ino_t parent_ino )
651 + char buf[EXT3_MAX_SNAP_DATA];
652 + struct snap_ea *snaps;
653 + int err = 0, inlist = 1;
655 + handle_t *handle = NULL;
657 + snap_debug("(ino %lu, parent %lu): saving ind %lu to index %d\n",
658 + pri->i_ino, parent_ino, ind_ino, index);
660 + if (index < 0 || index > MAX_SNAPS || !pri)
662 + /* need lock the list before get_attr() to avoid race */
663 + lock_list(pri->i_sb);
664 + /* read ea at first */
665 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
666 + buf, EXT3_MAX_SNAP_DATA);
667 + if (err == -ENODATA || err == -ENOATTR) {
668 + snap_debug("no extended attributes - zeroing\n");
669 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
671 + * To judge a inode in list, we only see if it has snap ea.
672 + * So take care of snap ea of primary inodes very carefully.
673 + * Is it right in snapfs EXT3, check it later?
676 + // ea_size = SNAP_EA_SIZE_FROM_INDEX(index);
677 + } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
681 + handle = ext3_journal_start(pri, SNAP_SETIND_TRANS_BLOCKS);
683 + err = PTR_ERR(handle);
687 + snaps = (struct snap_ea *)buf;
688 + snaps->ino[index] = cpu_to_le32 (ind_ino);
689 + ea_size = EXT3_MAX_SNAP_DATA;
691 + set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
693 + snap_debug("saving attributes\n");
696 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
697 + buf, EXT3_MAX_SNAP_DATA, 0);
700 + /* This will also write the ea for the pri inode, like above */
701 + err = insert_cowed_ino_to_list(handle, pri, buf);
703 + ext3_mark_inode_dirty(handle, pri);
704 + ext3_journal_stop(handle, pri);
706 + unlock_list(pri->i_sb);
711 + * is_redirector - determines if a primary inode is a redirector
712 + * @inode: primary inode to test
714 + * Returns 1 if the inode is a redirector, 0 otherwise.
716 +static int is_redirector(struct inode *inode)
718 + int is_redirector = 0;
721 + rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
723 + if (rc > 0 && rc <= MAX_SNAP_DATA)
725 + snap_debug("inode %lu %s redirector\n", inode->i_ino,
726 + is_redirector ? "is" : "isn't");
727 + return is_redirector;
730 +/*if it's indirect inode or not */
731 +static int is_indirect(struct inode *inode)
733 + if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
739 + * Copy inode metadata from one inode to another, excluding blocks and size.
740 + * FIXME do we copy EA data - ACLs and such (excluding snapshot data)?
742 +static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
746 + dst->i_mode = src->i_mode;
747 + dst->i_nlink = src->i_nlink;
748 + dst->i_uid = src->i_uid;
749 + dst->i_gid = src->i_gid;
750 + dst->i_atime = src->i_atime;
751 + dst->i_mtime = src->i_mtime;
752 + dst->i_ctime = src->i_ctime;
753 +// dst->i_version = src->i_version;
754 + dst->i_attr_flags = src->i_attr_flags;
755 + dst->i_generation = src->i_generation;
756 + dst->u.ext3_i.i_dtime = src->u.ext3_i.i_dtime;
757 + dst->u.ext3_i.i_flags = src->u.ext3_i.i_flags | EXT3_COW_FL;
758 +#ifdef EXT3_FRAGMENTS
759 + dst->u.ext3_i.i_faddr = src->u.ext3_i.i_faddr;
760 + dst->u.ext3_i.i_frag_no = src->u.ext3_i.i_frag_no;
761 + dst->u.ext3_i.i_frag_size = src->u.ext3_i.i_frag_size;
763 + if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
768 + if (ext3_xattr_list(src, names, 0) < 0)
771 + * the list of attribute names are stored as NUL terminated
772 + * strings, with a double NUL string at the end.
775 + while ((namelen = strlen(name))) {
779 + /* don't copy snap data */
780 + if (!strcmp(name, EXT3_SNAP_ATTR)) {
781 + snap_debug("skipping %s item\n", name);
784 + snap_debug("copying %s item\n", name);
785 + attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX,
786 + EXT3_SNAP_ATTR, NULL, 0);
789 + if ((buf = kmalloc(attrlen, GFP_ATOMIC)) == NULL)
791 + if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
792 + EXT3_SNAP_ATTR, buf, attrlen) < 0)
794 + if (ext3_xattr_set(handle, dst, EXT3_SNAP_INDEX,
795 + EXT3_SNAP_ATTR, buf, attrlen, 0) < 0)
798 + name += namelen + 1; /* skip name and trailing NUL */
803 +static inline int ext3_has_ea(struct inode *inode)
805 + return (EXT3_I(inode)->i_file_acl != 0);
808 +/* ext3_migrate_data:
809 + * MOVE all the data blocks from inode src to inode dst as well as
810 + * COPY all attributes(meta data) from inode src to inode dst.
811 + * For extended attributes(EA), we COPY all the EAs but skip the Snap EA from src to dst.
812 + * If the dst has Snap EA, then we CAN'T overwrite it. We CAN'T copy the src Snap EA.
813 + * XXX for EA, can we change it to MOVE all the EAs(exclude Snap EA) to dst and copy it back to src ?
814 + * This is for LAN free backup later.
817 +static int ext3_migrate_data (handle_t *handle, struct inode *dst, struct inode *src)
819 + unsigned long err = 0;
820 + /* 512 byte disk blocks per inode block */
821 + int bpib = src->i_sb->s_blocksize >> 9;
823 + if((!dst) || (!src))
826 + if (dst->i_ino == src->i_ino)
829 + ext3_copy_meta(handle, dst, src);
831 + snap_debug("migrating data blocks from %lu to %lu\n", src->i_ino, dst->i_ino);
832 + /* Can't check blocks in case of EAs */
833 + memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
834 + sizeof(EXT3_I(src)->i_data));
835 + memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
837 + ext3_discard_prealloc(src);
839 + dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
840 + src->i_size = EXT3_I(src)->i_disksize = 0;
842 + dst->i_blocks = src->i_blocks;
844 + /* Check EA blocks here to modify i_blocks correctly */
845 + if(ext3_has_ea (src)) {
846 + src->i_blocks += bpib;
847 + if( ! ext3_has_ea (dst) )
848 + if( dst->i_blocks >= bpib )
849 + dst->i_blocks -= bpib;
851 + if( ext3_has_ea (dst))
852 + dst->i_blocks += bpib;
855 + snap_debug("migrate data from ino %lu to ino %lu\n",
856 + src->i_ino, dst->i_ino);
857 + ext3_mark_inode_dirty(handle, src);
858 + ext3_mark_inode_dirty(handle, dst);
860 + truncate_inode_pages(src->i_mapping, 0);
862 + return SNAP_ERROR(err);
866 + * ext3_get_indirect - get a specific indirect inode from a primary inode
867 + * @primary: primary (direct) inode
868 + * @table: table of @slot + 1 indices in reverse chronological order
869 + * @slot: starting slot number to check for indirect inode number
871 + * We locate an indirect inode from a primary inode using the redirection
872 + * table stored in the primary inode. Because the desired inode may actually
873 + * be in a "newer" slot number than the supplied slot, we are given a table
874 + * of indices in chronological order to search for the correct inode number.
875 + * We walk table from @slot to 0 looking for a non-zero inode to load.
877 + * To only load a specific index (and fail if it does not exist), you can
878 + * pass @table = NULL, and the index number in @slot. If @slot == 0, the
879 + * primary inode data is returned.
881 + * We return a pointer to an inode, or an error. If the indirect inode for
882 + * the given index does not exist, NULL is returned.
884 +static struct inode *ext3_get_indirect(struct inode *primary, int *table,
887 + char buf[EXT3_MAX_SNAP_DATA];
888 + struct snap_ea *snaps;
890 + struct inode *inode = NULL;
891 + int err = 0, index = 0;
893 + if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
896 + snap_debug("ino %lu, table %p, slot %d\n", primary->i_ino, table,slot);
898 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
899 + buf, EXT3_MAX_SNAP_DATA);
900 + if (err == -ENODATA) {
902 + } else if (err < 0) {
903 + snap_debug(" attribute read error\n");
906 + snaps = (struct snap_ea *)buf;
908 + /* if table is NULL and there is a slot */
909 + if( !table && slot ) {
911 + ino = le32_to_cpu ( snaps->ino[index] );
912 + if(ino) inode = iget(primary->i_sb, ino);
915 + /* if table is not NULL */
916 + while ( !inode && slot > 0) {
917 + index = table[slot];
918 + ino = le32_to_cpu ( snaps->ino[index] );
920 + snap_debug("snap inode at slot %d is %lu\n", slot, ino);
925 + inode = iget(primary->i_sb, ino);
928 + if( slot == 0 && table ) {
929 + snap_debug("redirector not found, using primary\n");
930 + inode = iget(primary->i_sb, primary->i_ino);
936 +/* get the indirect ino at index of the primary inode
937 + * return value: postive: indirect ino number
938 + * negative or 0: error
940 +static ino_t ext3_get_indirect_ino(struct inode *primary, int index)
942 + char buf[EXT3_MAX_SNAP_DATA];
943 + struct snap_ea *snaps;
947 + if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
950 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
951 + buf, EXT3_MAX_SNAP_DATA);
952 + if (err == -ENOATTR) {
955 + } else if (err < 0) {
956 + snap_err(EXT3_SNAP_ATTR " attribute read error\n");
961 + snaps = (struct snap_ea *)buf;
962 + ino = le32_to_cpu (snaps->ino[index]);
963 + snap_debug("snap ino for %ld at index %d is %lu\n",
964 + primary->i_ino, index, ino);
968 +/* ext3_copy_block - copy one data block from inode @src to @dst.
969 + No lock here. User should do the lock.
970 + User should check the return value to see if the result is correct.
972 + 1: The block has been copied successfully
973 + 0: No block is copied, usually this is because src has no such blk
977 +static int ext3_copy_block (struct inode *dst, struct inode *src, int blk)
979 + struct buffer_head *bh_dst = NULL, *bh_src = NULL;
981 + handle_t *handle = NULL;
984 + snap_debug("copy blk %d from %lu to %lu \n", blk, src->i_ino, dst->i_ino);
986 + * ext3_getblk() require handle!=NULL
988 + if (S_ISREG(src->i_mode))
991 + handle = ext3_journal_start(dst, SNAP_COPYBLOCK_TRANS_BLOCKS);
995 + bh_src = ext3_bread(handle, src, blk, 0, &err);
997 + snap_err("error for src blk %d, error %d\n", blk, err);
1000 + bh_dst = ext3_getblk(handle, dst, blk, 1, &err);
1002 + snap_err("error for dst blk %d, error %d\n", blk, err);
1006 + snap_debug("copy block %lu to %lu (%ld bytes)\n",
1007 + bh_src->b_blocknr, bh_dst->b_blocknr,
1008 + src->i_sb->s_blocksize);
1010 + ext3_journal_get_write_access(handle, bh_dst);
1012 + memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
1014 + ext3_journal_dirty_metadata(handle, bh_dst);
1017 + if (bh_src) brelse(bh_src);
1018 + if (bh_dst) brelse(bh_dst);
1020 + ext3_journal_stop(handle, dst);
1024 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1026 + * add one inode to superblock's snap_orphan chain
1027 + * only add on-disk data for simplicity
1029 +static void add_snap_orphan(handle_t *handle, struct inode *pri, struct inode *ind)
1031 + struct ext3_sb_info *sb = &pri->i_sb->u.ext3_sb;
1032 + struct ext3_iloc iloc;
1034 + if( ext3_get_inode_loc(ind, &iloc) ){
1035 + snap_debug("--- get ind loc fail\n");
1040 + snap_debug("add new ind inode %lu into orphan list,"
1041 + " primary %lu, last orphan %u\n",
1042 + ind->i_ino, pri->i_ino,
1043 + sb->s_es->s_last_snap_orphan);
1044 + lock_super(pri->i_sb);
1045 + iloc.raw_inode->i_next_snap_orphan = sb->s_es->s_last_snap_orphan;
1046 + iloc.raw_inode->i_snap_primary = pri->i_ino;
1047 + ext3_mark_inode_dirty(handle, ind);
1049 + ext3_journal_get_write_access(handle, sb->s_sbh);
1050 + sb->s_es->s_last_snap_orphan = ind->i_ino;
1051 + pri->i_sb->s_dirt = 1;
1052 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1053 + unlock_super(pri->i_sb);
1058 + * counterpart of add_snap_orphan
1060 +static void remove_snap_orphan(handle_t *handle, struct inode *ind)
1062 + struct ext3_sb_info *sb = &ind->i_sb->u.ext3_sb;
1063 + struct inode *pre = NULL, *inode = NULL;
1064 + struct ext3_iloc iloc, pre_iloc;
1067 + lock_super(ind->i_sb);
1068 + for(ino = sb->s_es->s_last_snap_orphan; ino; ){
1069 + snap_debug("found an orphan, ino=%lu\n", ino);
1070 + inode = iget( ind->i_sb, ino );
1072 + snap_debug("iget %lu fail\n", ino);
1075 + if( ext3_get_inode_loc(inode, &iloc) ){
1076 + snap_debug("get_inode_loc %lu fail\n", ino);
1079 + if( ino == ind->i_ino ){
1081 + snap_debug("found at head of orphan chain\n");
1082 + ext3_journal_get_write_access(handle, sb->s_sbh);
1083 + sb->s_es->s_last_snap_orphan =
1084 + iloc.raw_inode->i_next_snap_orphan;
1085 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1086 + snap_debug("set new last orphan: %u\n",
1087 + sb->s_es->s_last_snap_orphan);
1091 + snap_debug("found in middle of orphan chain\n");
1092 + if( ext3_get_inode_loc(pre, &pre_iloc) ){
1093 + snap_err("get pre_inode loc %lu fail\n", pre->i_ino);
1096 + pre_iloc.raw_inode->i_next_snap_orphan =
1097 + iloc.raw_inode->i_next_snap_orphan;
1098 + ext3_mark_inode_dirty(handle, pre);
1099 + brelse(pre_iloc.bh);
1105 + ino = iloc.raw_inode->i_next_snap_orphan;
1110 + unlock_super(ind->i_sb);
1115 + * FIXME: how about crashs again during recovery?
1117 +void snap_orphan_cleanup(struct super_block *sb)
1119 + ino_t ind_ino, pri_ino;
1120 + struct inode *ind = NULL, *pri = NULL;
1121 + struct ext3_iloc ind_iloc;
1123 + if( (ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan) == 0 ){
1124 + snap_debug("snap_orphan_cleanup: nothing to do\n");
1128 + snap_debug("------ begin cleanup snap orphans ------\n");
1130 + ind = iget( sb, ind_ino );
1132 + snap_err("snap_orphan_cleanup: get "
1133 + "ind %lu fail\n", ind_ino);
1137 + if( ext3_get_inode_loc(ind, &ind_iloc) ){
1138 + snap_err("snap_orphan_cleanup: get "
1139 + "iloc %lu fail\n", ind_ino);
1144 + ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan =
1145 + ind_iloc.raw_inode->i_next_snap_orphan;
1146 + pri_ino = ind_iloc.raw_inode->i_snap_primary;
1148 + pri = iget( sb, pri_ino );
1150 + snap_err("snap_orphan_cleanup: get primary "
1151 + "%lu fail\n", pri_ino);
1154 + restore_snap_inode(pri, ind);
1155 + }while( ind_ino );
1156 + snap_debug("------ end cleanup snap orphans ------\n");
1158 + sb->u.ext3_sb.s_es->s_last_snap_orphan = 0;
1163 + * reserse operation of set_indirect()
1164 + * we should determine whether we had put pri into primary inode chain,
1165 + * if not, don't touch it
1167 +static void unset_indirect(handle_t *handle, struct inode *pri, struct inode *ind)
1169 + char buf[EXT3_MAX_SNAP_DATA];
1170 + struct snap_ea *snaps;
1171 + int err, alone=1, index, found;
1173 + snap_debug("pri %lu, ind %lu\n", pri->i_ino, ind->i_ino);
1174 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
1175 + EXT3_MAX_SNAP_DATA);
1177 + if( err == -ENOATTR ){
1178 + snap_debug("primary inode has not EA\n");
1181 + snap_debug("get EA error on primary inode,"
1182 + "returned value %d\n", err);
1187 + /* find ind's item in the ea */
1188 + snaps = (struct snap_ea*)buf;
1189 + for(index=EXT3_MAX_SNAPS-1, found=-1; index>=0; index--) {
1190 + if( snaps->ino[index] == ind->i_ino )
1192 + else if( snaps->ino[index] )
1197 + snap_debug("remove from primary inode's EA\n");
1198 + snaps->ino[found] = 0;
1199 + snaps->parent_ino[found] = 0;
1200 + ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1201 + buf, EXT3_MAX_SNAP_DATA, 0);
1203 + snap_debug("delete from primary inodes chain\n");
1204 + lock_list(pri->i_sb);
1205 + delete_cowed_ino_from_list(handle, pri);
1206 + unlock_list(pri->i_sb);
1209 + snap_debug("didn't found ind in pri's EA, do nothing\n");
1218 + * restore all data in @ind to @pri after free data blocks of @pri.
1219 + * then release @ind
1221 +static void restore_snap_inode(struct inode *pri, struct inode *ind)
1224 + struct inode *tmp;
1226 + snap_debug("restore from indirect %lu to primary %lu\n",
1227 + ind->i_ino, pri->i_ino);
1229 + handle = ext3_journal_start(pri, SNAP_RESTOREORPHAN_TRANS_BLOCKS);
1233 + /* first: taken from pri's ea, or from fs-wide primary inode chain */
1234 + unset_indirect(handle, pri, ind);
1236 + /* second: throw out half-copied data in pri */
1237 + if( pri->i_blocks ){
1238 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1240 + snap_debug("ext3_new_inode error\n");
1244 + ext3_migrate_data(handle, tmp, pri);
1245 + snap_debug("freeing half-copied %lu blocks\n", tmp->i_blocks );
1250 + /* third: restore ind inode to pri inode */
1251 + snap_debug("restore %lu blocks to primary inode %lu\n",
1252 + ind->i_blocks, pri->i_ino);
1253 + ext3_migrate_data(handle, pri, ind);
1255 + /* final: delete ind inode */
1261 + ext3_journal_stop(handle, pri);
1267 +static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
1268 + struct inode *src, int *has_orphan)
1270 + unsigned long blocks, blk, cur_blks;
1271 + int low_credits, save_ref;
1273 + blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
1274 + src->i_sb->s_blocksize_bits;
1275 + low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
1277 + snap_debug("%lu blocks need to be copied,"
1278 + "low credits limit %d\n", blocks, low_credits);
1279 + for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
1280 + if (!ext3_bmap(src->i_mapping, blk))
1282 + if(handle->h_buffer_credits <= low_credits) {
1283 + int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
1284 + if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
1285 + needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
1286 + if (journal_extend(handle, needed)) {
1287 + snap_debug("create_indirect:fail to extend "
1288 + "journal, restart trans\n");
1290 + if( !*has_orphan ){
1291 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1292 + add_snap_orphan(handle, dst, src);
1294 + ext3_orphan_add(handle, dst);
1298 + dst->u.ext3_i.i_disksize =
1299 + blk * dst->i_sb->s_blocksize;
1300 + dst->i_blocks = cur_blks;
1301 + dst->i_mtime = CURRENT_TIME;
1302 + ext3_mark_inode_dirty(handle, dst);
1305 + * We can be sure the last handle was stoped
1306 + * ONLY if the handle's reference count is 1
1308 + save_ref = handle->h_ref;
1309 + handle->h_ref = 1;
1310 + if( ext3_journal_stop(handle, dst) ){
1311 + snap_err("fail to stop journal\n");
1316 + handle = ext3_journal_start(dst,
1317 + low_credits + needed);
1319 + snap_err("fail to restart handle\n");
1322 + handle->h_ref = save_ref;
1325 + if (ext3_copy_block( dst, src, blk) < 0 )
1327 + cur_blks += dst->i_sb->s_blocksize / 512;
1329 + dst->i_size = dst->u.ext3_i.i_disksize = src->i_size;
1334 +static int ext3_set_generation(struct inode *inode, unsigned long gen)
1339 + handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
1341 + err = ext3_xattr_set(handle, inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1342 + (char*)&gen, sizeof(int), 0);
1344 + snap_err("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
1348 + ext3_journal_stop(handle, inode);
1352 +static int ext3_get_generation(struct inode *inode)
1356 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1357 + (char*)&gen, sizeof(gen));
1359 + if (err == -ENODATA) {
1362 + snap_err("can not get generation from %lu \n", inode->i_ino);
1369 + * ext3_create_indirect - copy data, attributes from primary to new indir inode
1370 + * @pri: primary (source) inode
1371 + * @index: index in snapshot table where indirect inode should be stored
1372 + * @delete: flag that the primary inode is being deleted
1374 + * We copy all of the data blocks from the @*src inode to the @*dst inode, as
1375 + * well as copying the attributes from @*src to @*dst. If @delete == 1, then
1376 + * the primary inode will only be a redirector and will appear deleted.
1378 + * FIXME do we move EAs, only non-snap EAs, what?
1379 + * FIXME we could do readpage/writepage, but we would have to handle block
1380 + * allocation then, and it ruins sparse files for 1k/2k filesystems,
1381 + * at the expense of doing a memcpy.
1384 +static struct inode *ext3_create_indirect(
1385 + struct inode *pri,
1391 + struct inode *ind;
1392 + handle_t *handle = NULL;
1394 + int has_orphan = 0;
1396 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1397 + printk( KERN_EMERG "TRY TO COW JOUNRAL\n");
1400 + snap_debug("creating indirect inode for %lu at index %d, %s pri\n",
1401 + pri->i_ino, index, del ? "deleting" : "preserve");
1403 + ind = ext3_get_indirect(pri, NULL, index);
1407 + handle = ext3_journal_start(pri, SNAP_CREATEIND_TRANS_BLOCKS);
1410 + /* XXX ? We should pass an err argument to get_indirect and precisely
1411 + * detect the errors, for some errors, we should exit right away.
1414 + /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect,
1415 + * we just free the primary data blocks and mark this inode delete
1417 + if((del) && ind && !IS_ERR(ind)) {
1418 + struct inode *tmp;
1419 + /* for directory, we don't free the data blocks,
1420 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1422 + snap_debug("del==SNAP_DEL_PRI_WITH_IND && ind\n");
1423 + if(!S_ISDIR(pri->i_mode)) {
1424 + /*Here delete the data of that pri inode.
1425 + * FIXME later, should throw the blocks of
1426 + * primary inode directly
1428 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1430 + down(&tmp->i_sem);
1431 + ext3_migrate_data(handle, tmp, pri);
1437 + snap_err("ext3_new_inode error\n");
1442 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1443 + ext3_mark_inode_dirty(handle, pri);
1448 + if (ind && !IS_ERR(ind)) {
1449 + snap_debug("existing indirect ino %lu for %lu: index %d\n",
1450 + ind->i_ino, pri->i_ino, index);
1454 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
1455 + ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1461 + snap_debug("got new inode %lu\n", ind->i_ino);
1462 + ind->i_rdev = pri->i_rdev;
1463 + ind->i_op = pri->i_op;
1464 + ext3_set_generation(ind, (unsigned long)gen);
1465 + /* If we are deleting the primary inode, we want to ensure that it is
1466 + * written to disk with a non-zero link count, otherwise the next iget
1467 + * and iput will mark the inode as free (which we don't want, we want
1468 + * it to stay a redirector). We fix this in ext3_destroy_indirect()
1469 + * when the last indirect inode is removed.
1471 + * We then do what ext3_delete_inode() does so that the metadata will
1472 + * appear the same as a deleted inode, and we can detect it later.
1475 + snap_debug("deleting primary inode\n");
1477 + down(&ind->i_sem);
1478 + err = ext3_migrate_data(handle, ind, pri);
1482 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1486 + /* XXX for directory, we copy the block back
1487 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1489 + if( S_ISDIR(pri->i_mode)) {
1490 + handle = ext3_copy_data( handle, pri, ind, &has_orphan );
1495 + pri->u.ext3_i.i_flags |= EXT3_DEL_FL;
1496 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1497 + if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
1498 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1499 + //pri->u.ext3_i.i_generation++;
1500 + ext3_mark_inode_dirty(handle, pri);
1501 + ext3_mark_inode_dirty(handle, ind);
1504 + down(&ind->i_sem);
1505 + err = ext3_migrate_data(handle, ind, pri);
1509 + /* for regular files we do blocklevel COW's maybe */
1510 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
1511 + && S_ISREG(pri->i_mode)) {
1513 + snap_debug("ino %lu, do block cow\n",pri->i_ino);
1514 + /* because after migrate_data , pri->i_size is 0 */
1515 + pri->i_size = ind->i_size;
1518 + int bpib = pri->i_sb->s_blocksize >> 9;
1519 + snap_debug("ino %lu, do file cow\n", pri->i_ino);
1521 + /* XXX: can we do this better?
1522 + * If it's a fast symlink, we should copy i_data back!
1523 + * The criteria to determine a fast symlink is:
1524 + * 1) it's a link and its i_blocks is 0
1525 + * 2) it's a link and its i_blocks is bpib ( the case
1526 + * it has been cowed and has ea )
1528 + if( S_ISLNK(ind->i_mode) &&
1529 + (( ind->i_blocks == 0) || (ext3_has_ea(ind) && ind->i_blocks == bpib )) ){
1530 + snap_debug("ino %lu is fast symlink\n",
1532 + memcpy(EXT3_I(pri)->i_data,
1533 + EXT3_I(ind)->i_data,
1534 + sizeof(EXT3_I(ind)->i_data));
1535 + pri->i_size = ind->i_size;
1538 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1543 + /* set cow flag for ind */
1544 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1545 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
1547 + ext3_mark_inode_dirty(handle, pri);
1548 + ext3_mark_inode_dirty(handle, ind);
1550 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1557 + if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
1558 + EXT3_FEATURE_COMPAT_SNAPFS)) {
1559 + lock_super(pri->i_sb);
1560 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
1561 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat |=
1562 + cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
1563 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
1564 + pri->i_sb->s_dirt = 1;
1565 + unlock_super(pri->i_sb);
1568 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1569 + remove_snap_orphan(handle, ind);
1571 + ext3_orphan_del(handle, ind);
1574 + ext3_journal_stop(handle, pri);
1585 + ext3_journal_stop(handle, pri);
1586 + snap_debug("exiting with error %d\n", err);
1591 +/* The following functions are used by destroy_indirect */
1592 +#define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
1593 +#define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
1595 +static inline int block_bmap (struct buffer_head * bh, int nr)
1601 + tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
1606 +static inline int block_setbmap (handle_t *handle, struct buffer_head * bh, int nr, int physical)
1611 + ext3_journal_get_write_access(handle, bh);
1612 + ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
1613 + ext3_journal_dirty_metadata(handle, bh);
1618 +static int ext3_migrate_block (handle_t *handle, struct inode * dst, struct inode *src, int block)
1620 + int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
1621 + int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
1622 + int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
1623 + unsigned long blksz = src->i_sb->s_blocksize;
1624 + kdev_t ddev = dst->i_dev;
1625 + kdev_t sdev = src->i_dev;
1629 + ext3_warning (src->i_sb, "ext3_migrate_block", "block < 0");
1632 + if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
1633 + (1 << (addr_per_block_bits * 2)) +
1634 + ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
1635 + ext3_warning (src->i_sb, "ext3_migrate_block", "block > big");
1638 + /* EXT3_NDIR_BLOCK */
1639 + if (block < EXT3_NDIR_BLOCKS) {
1640 + if( inode_bmap(dst, block) ) return 0;
1642 + if( (physical = inode_bmap(src, block)) ) {
1643 + inode_setbmap (dst, block, physical);
1644 + inode_setbmap (src, block, 0);
1651 + /* EXT3_IND_BLOCK */
1652 + block -= EXT3_NDIR_BLOCKS;
1653 + if (block < addr_per_block) {
1654 + i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1657 + physical = inode_bmap(src, EXT3_IND_BLOCK);
1659 + inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1660 + inode_setbmap (src, EXT3_IND_BLOCK, 0);
1666 + if( block_bmap (bread (ddev, i1_d, blksz), block ))
1669 + i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1670 + if( !i1_s) return 0;
1672 + physical = block_bmap ( bread (sdev, i1_s, blksz), block );
1675 + block_setbmap(handle, bread(ddev, i1_d, blksz),block,physical);
1676 + block_setbmap(handle, bread(sdev, i1_s, blksz), block, 0);
1682 + /* EXT3_DIND_BLOCK */
1683 + block -= addr_per_block;
1684 + if (block < (1 << (addr_per_block_bits * 2))) {
1685 + i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1686 + i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1688 + if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1689 + inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1690 + inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1696 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1697 + block >> addr_per_block_bits);
1701 + if( !i1_s) return 0;
1703 + physical = block_bmap (bread (sdev, i1_s, blksz),
1704 + block >> addr_per_block_bits);
1706 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1707 + block >> addr_per_block_bits, physical);
1708 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1709 + block >> addr_per_block_bits, 0);
1715 + physical = block_bmap (bread (ddev, i2_d,
1717 + block & (addr_per_block - 1));
1721 + i2_s = block_bmap (bread (sdev, i1_s,
1723 + block >> addr_per_block_bits);
1724 + if(!i2_s) return 0;
1726 + physical = block_bmap(bread (sdev, i2_s,
1728 + block & (addr_per_block - 1));
1730 + block_setbmap(handle, bread (ddev, i2_d, blksz),
1731 + block & (addr_per_block - 1), physical);
1732 + block_setbmap(handle, bread (sdev, i2_s, blksz),
1733 + block & (addr_per_block - 1), 0);
1741 + /* EXT3_TIND_BLOCK */
1742 + block -= (1 << (addr_per_block_bits * 2));
1743 + i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1744 + i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1746 + if( (physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1747 + inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1751 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1752 + block >> (addr_per_block_bits * 2));
1754 + if(i1_s) i2_s = block_bmap (bread (sdev, i1_s, blksz),
1755 + block >> (addr_per_block_bits * 2));
1759 + if( !i1_s) return 0;
1761 + physical = block_bmap (bread (sdev, i1_s, blksz),
1762 + block >> (addr_per_block_bits * 2));
1764 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1765 + block >> (addr_per_block_bits * 2), physical);
1766 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1767 + block >> (addr_per_block_bits * 2), 0);
1773 + i3_d = block_bmap (bread (ddev, i2_d, blksz),
1774 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1775 + if( i2_s) i3_s = block_bmap (bread (sdev, i2_s, blksz),
1776 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1779 + if (!i2_s) return 0;
1780 + physical = block_bmap (bread (sdev, i2_s, blksz),
1781 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1783 + block_setbmap (handle, bread (ddev, i2_d, blksz),
1784 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1786 + block_setbmap (handle, bread (sdev, i2_s, blksz),
1787 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1794 + physical = block_bmap (bread (ddev, i3_d, blksz),
1795 + block & (addr_per_block - 1)) ;
1796 + if(physical) return 0;
1798 + if(!i3_s) return 0;
1799 + physical = block_bmap (bread (sdev, i3_s, blksz),
1800 + block & (addr_per_block - 1)) ;
1802 + block_setbmap (handle, bread (ddev, i3_d, blksz),
1803 + block & (addr_per_block - 1), physical);
1804 + block_setbmap (handle, bread (sdev, i3_s, blksz),
1805 + block & (addr_per_block - 1), 0);
1813 +/* Generate i_blocks from blocks for an inode .
1814 + * We also calculate EA block here.
1816 +static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1818 + /* 512 byte disk blocks per inode block */
1819 + int bpib = inode->i_sb->s_blocksize >> 9;
1820 + int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1821 + unsigned long i_blocks = 0;
1824 + int meta_blocks = 0;
1826 + if( !inode ) return 0;
1828 + if( blocks < 0 ) {
1829 + /* re-calculate blocks here */
1830 + blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1831 + >> inode->i_sb->s_blocksize_bits;
1834 + /* calculate data blocks */
1835 + for(i = 0; i < blocks; i++ ) {
1836 + if(ext3_bmap(inode->i_mapping, i))
1839 + /* calculate meta blocks */
1840 + blocks -= EXT3_NDIR_BLOCKS;
1841 + if( blocks > 0 ) {
1843 + blocks -= addr_per_block;
1845 + if( blocks > 0 ) meta_blocks++;
1847 + while( (blocks > 0) && (i < addr_per_block) ) {
1849 + blocks -= addr_per_block;
1852 + if ( blocks > 0 ) meta_blocks += 2;
1855 + while( blocks > 0) {
1857 + blocks -= addr_per_block;
1859 + if(i >= addr_per_block ) {
1863 + if( j >= addr_per_block) {
1868 + /* calculate EA blocks */
1869 + if( ext3_has_ea (inode) ) meta_blocks++;
1871 + i_blocks += meta_blocks * bpib;
1872 + snap_debug("ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1877 + * ext3_destroy_indirect - delete an indirect inode from the table
1878 + * @pri: primary inode
1879 + * @ind: indirect inode
1880 + * @index: index of inode that should be deleted
1882 + * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1883 + * is NULL, we use the inode at @index.
1885 +static int ext3_destroy_indirect(struct inode *pri, int index,
1886 + struct inode *next_ind)
1888 + char buf[EXT3_MAX_SNAP_DATA];
1889 + struct snap_ea *snaps;
1890 + struct inode *ind;
1894 + handle_t *handle=NULL;
1897 + if (index < 0 || index > EXT3_MAX_SNAPS)
1900 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1901 + snap_err("TRY TO DESTROY JOURNAL'S IND\n");
1905 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1906 + buf, EXT3_MAX_SNAP_DATA);
1908 + snap_err("inode %lu attribute read error\n", pri->i_ino);
1912 + snaps = (struct snap_ea *)buf;
1913 + if ( !snaps->ino[index] ) {
1914 + snap_err("for pri ino %lu, index %d, redirect ino is 0\n",
1915 + pri->i_ino, index);
1919 + snap_debug("for pri ino %lu, reading inode %lu at index %d\n",
1920 + pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1922 + ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1924 + if ( !ind || IS_ERR(ind) || is_bad_inode(ind))
1927 + snap_debug("iget ind %lu, ref count = %d\n",
1928 + ind->i_ino, atomic_read(&ind->i_count));
1930 + handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
1935 + /* if it's block level cow, first copy the blocks back */
1936 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1937 + S_ISREG(pri->i_mode)) {
1940 + if( !next_ind ) next_ind = pri;
1941 + blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1)
1942 + >> next_ind->i_sb->s_blocksize_bits;
1944 +#define FAST_MIGRATE_BLOCK
1945 +#ifdef FAST_MIGRATE_BLOCK
1946 + snap_debug("migrate block back from ino %lu to %lu\n",
1947 + ind->i_ino, next_ind->i_ino);
1949 + snap_double_lock(next_ind, ind);
1950 + for(i = 0; i < blocks; i++) {
1951 + if( ext3_bmap(next_ind->i_mapping, i) )
1953 + if( !ext3_bmap(ind->i_mapping, i) )
1955 + ext3_migrate_block(handle, next_ind, ind, i) ;
1957 + /* Now re-compute the i_blocks */
1958 + /* XXX shall we take care of ind here? probably not */
1959 + next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1960 + ext3_mark_inode_dirty(handle, next_ind);
1962 + snap_double_unlock(next_ind, ind);
1965 + snap_double_lock(pri, ind);
1967 + for(i = 0; i < blocks; i++) {
1968 + if( ext3_bmap(pri, i) ) continue;
1969 + if( !ext3_bmap(ind, i) ) continue;
1970 + ext3_migrate_block( pri, ind, i) ;
1972 + /* Now re-compute the i_blocks */
1973 + /* XXX shall we take care of ind here? probably not */
1974 + pri->i_blocks = calculate_i_blocks( pri, blocks);
1975 + mark_inode_dirty(pri);
1977 + double_unlock(pri, ind);
1980 + snap_double_lock(next_ind, ind);
1981 + for (i = 0; i < blocks; i++) {
1982 + if (ext3_bmap(next_ind->i_mapping, i))
1984 + if (ext3_copy_block(next_ind, ind, i ) < 0)
1987 + ext3_mark_inode_dirty(handle, next_ind);
1988 + double_unlock(next_ind, ind);
1992 + snap_debug("delete indirect ino %lu\n", ind->i_ino);
1993 + snap_debug("iput ind %lu, ref count = %d\n",
1994 + ind->i_ino, atomic_read(&ind->i_count));
1998 + snaps->ino[index] = cpu_to_le32(0);
1999 + for (i = 0; i < EXT3_MAX_SNAPS; i++)
2000 + save += snaps->ino[i];
2003 + lock_list(pri->i_sb);
2004 + delete_cowed_ino_from_list(handle, pri);
2005 + unlock_list(pri->i_sb);
2008 + /* if there are no cowed inode left, then remove snapfs feature */
2009 + if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2011 + lock_super(pri->i_sb);
2013 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
2014 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2015 + EXT3_FEATURE_COMPAT_SNAPFS)) {
2016 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2017 + cpu_to_le32(~EXT3_FEATURE_COMPAT_SNAPFS);
2019 + /* clean up block level cow feature */
2020 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2021 + EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2022 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2023 + cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2025 + /* XXX clean the extended attribute feature,
2026 + * this is not safe, find a better way
2028 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2029 + EXT3_FEATURE_COMPAT_EXT_ATTR)) {
2030 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2031 + cpu_to_le32(~EXT3_FEATURE_COMPAT_EXT_ATTR);
2034 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
2035 + pri->i_sb->s_dirt = 1;
2036 + unlock_super(pri->i_sb);
2040 + * If we are deleting the last indirect inode, and the primary inode
2041 + * has already been deleted, then mark the primary for deletion also.
2042 + * Otherwise, if we are deleting the last indirect inode remove the
2043 + * snaptable from the inode. XXX
2045 + if (!save && pri->u.ext3_i.i_dtime) {
2046 + snap_debug("deleting primary %lu\n", pri->i_ino);
2048 + /* reset err to 0 now */
2051 + snap_debug("%s redirector table\n",
2052 + save ? "saving" : "deleting");
2053 + /* XXX: since set ea will modify i_ctime of pri,
2054 + so save/restore i_ctime. Need this necessary ? */
2055 + ctime = pri->i_ctime;
2056 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2057 + save ? buf : NULL, EXT3_MAX_SNAP_DATA, 0);
2058 + pri->i_ctime = ctime;
2059 + ext3_mark_inode_dirty(handle, pri);
2061 + ext3_journal_stop(handle, pri);
2065 +/* restore a primary inode with the indirect inode at index */
2066 +static int ext3_restore_indirect(struct inode *pri, int index)
2068 + struct inode *ind;
2069 + struct inode *tmp;
2071 + handle_t *handle = NULL;
2073 + if (index < 0 || index > EXT3_MAX_SNAPS)
2076 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2077 + printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2080 + snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2082 + ind = ext3_get_indirect(pri, NULL, index);
2087 + snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2089 + handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
2092 + /* first destroy all the data blocks in primary inode */
2093 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
2094 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
2096 + snap_double_lock(pri, tmp);
2097 + ext3_migrate_data(handle, tmp, pri);
2098 + snap_double_unlock(pri, tmp);
2104 + snap_err("restore_indirect, new_inode err\n");
2106 + snap_double_lock(pri, ind);
2107 + ext3_migrate_data(handle, pri, ind);
2108 + /* clear the cow flag for pri because ind has it */
2109 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
2110 + ext3_mark_inode_dirty(handle, pri);
2111 + snap_double_unlock(pri, ind);
2115 +// ext3_destroy_indirect(pri, index);
2117 + ext3_journal_stop(handle, pri);
2123 + * ext3_snap_iterate - iterate through all of the inodes
2124 + * @sb: filesystem superblock
2125 + * @repeat: pointer to function called on each valid inode
2126 + * @start: inode to start iterating at
2127 + * @priv: private data to the caller/repeat function
2129 + * If @start is NULL, then we do not return an inode pointer. If @*start is
2130 + * NULL, then we start at the beginning of the filesystem, and iterate over
2131 + * all of the inodes in the system. If @*start is non-NULL, then we start
2132 + * iterating at this inode.
2134 + * We call the repeat function for each inode that is in use. The repeat
2135 + * function must check if this is a redirector (with is_redirector) if it
2136 + * only wants to operate on redirector inodes. If there is an error or
2137 + * the repeat function returns non-zero, we return the last inode operated
2138 + * on in the @*start parameter. This allows the caller to restart the
2139 + * iteration at this inode if desired, by returning a positive value.
2140 + * Negative return values indicate an error.
2142 + * NOTE we cannot simply traverse the existing filesystem tree from the root
2143 + * inode, as there may be disconnected trees from deleted files/dirs
2145 + * FIXME If there was a list of inodes with EAs, we could simply walk the list
2146 + * intead of reading every inode. This is an internal implementation issue.
2149 +static int ext3_iterate_all(struct super_block *sb,
2150 + int (*repeat)(struct inode *inode, void *priv),
2151 + struct inode **start, void *priv)
2153 + struct inode *tmp = NULL;
2155 + ino_t istart, ibase;
2161 + *start = iget(sb, EXT3_ROOT_INO);
2166 + if (is_bad_inode(*start)) {
2171 + if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
2172 + snap_debug("invalid starting inode %ld\n",(*start)->i_ino);
2176 + if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2177 + if ((err = (*repeat)(*start, priv) != 0))
2180 + *start = iget(sb, EXT3_FIRST_INO(sb));
2185 + if (is_bad_inode(*start)) {
2191 + gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
2192 + istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
2193 + ibase = gstart * EXT3_INODES_PER_GROUP(sb);
2194 + for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
2195 + gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
2196 + struct ext3_group_desc * gdp;
2201 + gdp = ext3_get_group_desc (sb, gnum, NULL);
2202 + if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
2203 + EXT3_INODES_PER_GROUP(sb))
2206 + bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2207 + if (bitmap_nr < 0)
2210 + bitmap = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr]->b_data;
2211 + for (ibyte = istart >> 3;
2212 + ibyte < EXT3_INODES_PER_GROUP(sb) >> 3;
2218 + if (!bitmap[ibyte])
2221 + /* FIXME need to verify if bit endianness will
2222 + * work properly here for all architectures.
2224 + for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2225 + ino_t ino = ibase + (ibyte << 3) + i;
2227 + if ((bitmap[ibyte] & bit) == 0)
2230 + if (ino < (*start)->i_ino)
2233 + *start = iget(sb, ino);
2238 + if (is_bad_inode(*start)) {
2243 + if ((err = (*repeat)(*start, priv)) != 0)
2256 +static int ext3_iterate(struct super_block *sb,
2257 + int (*repeat)(struct inode *inode, void *priv),
2258 + struct inode **start, void *priv, int flag)
2261 + case SNAP_ITERATE_ALL_INODE:
2262 + return ext3_iterate_all (sb, repeat, start, priv);
2264 + case SNAP_ITERATE_COWED_INODE:
2265 + return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2272 +static int find_snap_meta_index(
2273 + struct table_snap_meta_data *snap_meta,
2278 + /* table max length is null*/
2279 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2280 + /*compare name Max name Length 15*/
2281 + if (snap_meta->array[i].name[0]){
2282 + if(!strncmp(snap_meta->array[i].name, name, strlen(name)))
2286 + return -1; /* can not find */
2289 +int set_snap_meta_index(
2290 + struct table_snap_meta_data *snap_meta,
2296 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2297 + /*compare name Max name Length 15*/
2298 + if (! snap_meta->array[i].name[0]){
2299 + strcpy(snap_meta->array[i].name, name);
2300 + snap_meta->count ++;
2301 + snap_meta->array[i].start = i * TABLE_ITEM_SIZE + 1;
2302 + snap_meta->array[i].len = size;
2306 + return -1; /* can not find */
2309 +static int ext3_get_meta_attr(struct super_block *sb,
2310 + char* name, char* buf,
2314 + struct inode *inode;
2315 + struct buffer_head *bh = NULL;
2316 + struct table_snap_meta_data *s_attr;
2317 + unsigned long map_len = 0, left_size;
2318 + int i, error = 0, index = 0;
2320 + ino = SB_SNAPTABLE_INO(sb);
2322 + snap_err("No table file \n");
2325 + inode = iget(sb, ino);
2326 + if(!inode || is_bad_inode(inode)){
2327 + snap_err("unable to get table ino %lu\n", ino);
2331 + /*read the table from the table inode*/
2332 + bh = ext3_bread(NULL, inode, 0, 0, &error);
2334 + snap_err("read table ino %lu, error %d\n", ino, error);
2338 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2339 + index = find_snap_meta_index(s_attr, name);
2341 + snap_debug("not exit %s meta attr of table ino %lu \n",
2342 + name, inode->i_ino);
2346 + if (!buf || *size < s_attr->array[index].len) {
2347 + /*return the size of this meta attr */
2348 + error = s_attr->array[index].len;
2351 + map_len = (s_attr->array[index].len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2352 + left_size = *size;
2353 + for(i = 0; i < map_len; i++) {
2354 + struct buffer_head *array_bh = NULL;
2356 + array_bh = ext3_bread(NULL, inode,
2357 + s_attr->array[index].start + i,
2360 + snap_err("ino %lu read snap attr offset %d error %d \n",
2361 + inode->i_ino, (s_attr->array[index].start + i),
2365 + if (left_size >= sb->s_blocksize) {
2366 + memcpy(buf, array_bh->b_data, sb->s_blocksize);
2368 + memcpy(buf, array_bh->b_data, left_size);
2369 + left_size -= sb->s_blocksize;
2372 + *size = s_attr->array[index].len;
2379 +static int ext3_set_meta_attr(struct super_block *sb, char* name,
2380 + char* buf, int size)
2382 + struct inode *inode = NULL;
2383 + handle_t *handle = NULL;
2384 + struct buffer_head *bh = NULL;
2385 + struct table_snap_meta_data *s_attr = NULL;
2386 + unsigned long ino;
2387 + int i, index = 0, error = 0;
2388 + unsigned long new_len = 0, left_size;
2390 + ino = SB_SNAPTABLE_INO(sb);
2392 + if (ino == 0 && !buf) {
2393 + snap_debug("no table ino \n");
2397 + handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2402 + /*create table inode update table ino*/
2403 + inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
2407 + ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
2408 + SB_SNAPTABLE_INO(sb) = inode->i_ino;
2409 + ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
2414 + inode = iget(sb, ino);
2415 + if (!inode || !inode->i_nlink || is_bad_inode(inode)) {
2416 + snap_err("unable to get table ino %lu\n", ino);
2421 + /*read the table from the table inode,
2422 + * If can not find the block just create it*/
2423 + bh = ext3_bread(handle, inode, 0, 1, &error);
2425 + snap_err("read table ino %lu, error %d\n", ino, error);
2429 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2430 + index = find_snap_meta_index(s_attr, name);
2431 + if (index < 0 && !buf) {
2432 + snap_debug("%s meta attr of table ino %lu do not exist\n",
2433 + name, inode->i_ino);
2439 + snap_debug("delete the meta attr %s in the table ino %lu",
2440 + name, inode->i_ino);
2441 + /*Here we only delete the entry of the attr
2442 + *FIXME, should we also delete the block of
2445 + ext3_journal_get_write_access(handle, bh);
2446 + memset(s_attr->array[index].name, 0, TABLE_ITEM_NAME_SIZE);
2447 + s_attr->array[index].len = 0;
2449 + ext3_journal_dirty_metadata(handle, bh);
2453 + new_len = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2454 + /*find the place to put this attr in that index*/
2455 + ext3_journal_get_write_access(handle, bh);
2457 + index = set_snap_meta_index(s_attr, name, size);
2459 + snap_err("table full of ino %lu \n", inode->i_ino);
2465 + s_attr->array[index].len = size;
2466 + journal_dirty_metadata(handle, bh);
2468 + /*put this attr to the snap table*/
2470 + for(i = 0; i < new_len; i++) {
2471 + struct buffer_head *array_bh = NULL;
2473 + array_bh = ext3_bread(handle, inode,
2474 + s_attr->array[index].start + i, 1, &error);
2476 + snap_err("inode %lu Can not get the block of attr %s\n",
2477 + inode->i_ino, name);
2482 + ext3_journal_get_write_access(handle, array_bh);
2483 + if (left_size > inode->i_sb->s_blocksize)
2484 + memcpy(array_bh->b_data, buf, inode->i_sb->s_blocksize);
2486 + memcpy(array_bh->b_data, buf, left_size);
2487 + ext3_journal_dirty_metadata(handle, array_bh);
2488 + left_size -= inode->i_sb->s_blocksize;
2493 + ext3_journal_stop(handle, sb->s_root->d_inode);
2498 +struct snapshot_operations ext3_snap_operations = {
2499 + ops_version: SNAP_VERSION(2,0,2),
2500 + is_redirector: is_redirector,
2501 + is_indirect: is_indirect,
2502 + create_indirect: ext3_create_indirect,
2503 + get_indirect: ext3_get_indirect,
2504 + get_indirect_ino: ext3_get_indirect_ino,
2505 + destroy_indirect: ext3_destroy_indirect,
2506 + restore_indirect: ext3_restore_indirect,
2507 + iterate: ext3_iterate,
2508 + copy_block: ext3_copy_block,
2509 + set_indirect: ext3_set_indirect,
2510 + snap_feature: ext3_snap_feature,
2511 + get_generation: ext3_get_generation,
2512 + set_generation: ext3_set_generation,
2513 + get_meta_attr: ext3_get_meta_attr,
2514 + set_meta_attr: ext3_set_meta_attr,
2517 +EXPORT_SYMBOL(ext3_snap_operations);
2518 +#ifdef SNAP_PROFILE
2519 +EXPORT_SYMBOL(prof_snapdel);
2522 +#ifdef SNAP_DEBUG_IOC
2524 +static int print_inode(struct inode *pri, void *index_val)
2528 + struct snap_ea *snaps;
2529 + char buf[EXT3_MAX_SNAP_DATA];
2530 + int index = *(int *)index_val;
2532 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2533 + buf, EXT3_MAX_SNAP_DATA);
2535 + if (err == -ENODATA) {
2536 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
2538 + else if (err < 0) {
2539 + snap_err("got err %d when reading attributes\n", err);
2543 + snaps = (struct snap_ea *) buf;
2545 + if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2546 + snap_debug("no redirected ino for primary inode %lu\n",
2550 + snap_debug("primary inode %lu , redirected ino=%d\n",
2551 + primary->i_ino,le32_to_cpu(snaps->ino[index]));
2557 +int snap_print(struct super_block *sb, int index)
2559 + ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2563 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2565 + int index = *(int *)index_val;
2567 + printk("delete_inode for index %d\n",index);
2568 + rc = ext3_destroy_indirect(primary,index, NULL);
2570 + printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",
2571 + primary->i_ino, index, rc);
2575 +int ext3_snap_delete(struct super_block *sb, int index)
2577 + ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index,
2578 + SNAP_ITERATE_COWED_INODE);
2590 Index: linux-2.4.20-8/fs/ext3/Makefile
2591 ===================================================================
2592 --- linux-2.4.20-8.orig/fs/ext3/Makefile 2004-01-05 10:54:03.000000000 +0800
2593 +++ linux-2.4.20-8/fs/ext3/Makefile 2004-01-13 00:10:14.000000000 +0800
2596 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2597 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2599 + xattr_trusted.o snap.o
2600 obj-m := $(O_TARGET)
2602 export-objs += xattr.o
2603 Index: linux-2.4.20-8/fs/ext3/inode.c
2604 ===================================================================
2605 --- linux-2.4.20-8.orig/fs/ext3/inode.c 2004-01-05 10:54:03.000000000 +0800
2606 +++ linux-2.4.20-8/fs/ext3/inode.c 2004-01-18 01:48:19.000000000 +0800
2607 @@ -1191,7 +1191,7 @@
2608 * So, if we see any bmap calls here on a modified, data-journaled file,
2609 * take extra steps to flush any blocks which might be in the cache.
2611 -static int ext3_bmap(struct address_space *mapping, long block)
2612 +int ext3_bmap(struct address_space *mapping, long block)
2614 struct inode *inode = mapping->host;
2616 Index: linux-2.4.20-8/fs/ext3/ialloc.c
2617 ===================================================================
2618 --- linux-2.4.20-8.orig/fs/ext3/ialloc.c 2004-01-05 10:54:03.000000000 +0800
2619 +++ linux-2.4.20-8/fs/ext3/ialloc.c 2004-01-16 20:48:29.000000000 +0800
2620 @@ -160,6 +160,13 @@
2624 +/* Export load_inode_bitmap*/
2625 +int ext3_load_inode_bitmap (struct super_block * sb,
2626 + unsigned int block_group)
2628 + return load_inode_bitmap(sb, block_group);
2632 * NOTE! When we get the inode, we're the only people
2633 * that have access to it, and as such there are no
2634 Index: linux-2.4.20-8/fs/ext3/super.c
2635 ===================================================================
2636 --- linux-2.4.20-8.orig/fs/ext3/super.c 2004-01-05 10:54:03.000000000 +0800
2637 +++ linux-2.4.20-8/fs/ext3/super.c 2004-01-18 01:40:10.000000000 +0800
2638 @@ -1324,6 +1324,13 @@
2639 sbi->s_mount_state = le16_to_cpu(es->s_state);
2640 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2641 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2642 +#define EXT3_SNAP_FS
2643 +#ifdef EXT3_SNAP_FS
2644 + init_MUTEX(&(sbi->s_snap_list_sem));
2645 + sbi->s_snaptable_ino = le32_to_cpu(es->s_snaptable_ino);
2646 + sbi->s_first_cowed_pri_ino = le32_to_cpu(es->s_first_cowed_pri_ino);
2647 + sbi->s_last_cowed_pri_ino = le32_to_cpu(es->s_last_cowed_pri_ino);
2649 for (i=0; i < 4; i++)
2650 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2651 sbi->s_def_hash_version = es->s_def_hash_version;
2652 Index: linux-2.4.20-8/include/linux/snap.h
2653 ===================================================================
2654 --- linux-2.4.20-8.orig/include/linux/snap.h 2003-01-30 18:24:37.000000000 +0800
2655 +++ linux-2.4.20-8/include/linux/snap.h 2004-01-13 00:10:14.000000000 +0800
2658 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
2659 + * started by Andreas Dilger <adilger@turbolinux.com>
2660 + * Peter Braam <braam@mountainviewdata.com>
2661 + * Harrison Xing <harrisonx@mountainviewdata.com>
2663 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2664 + * Eric Mei <Ericm@clusterfs.com>
2665 + * Wang Di <wangdi@clusterfs.com>
2667 + * Rewriten 2003 by Wang Di <wangdi@clusterfs.com>
2668 + * Eric Mei <ericm@clusterfs.com>
2670 + * Functions for implementing snapshots in the ext3 filesystem. They are
2671 + * intended to hide the internals of the filesystem from the caller in
2672 + * such a way that the caller doesn't need to know about inode numbers,
2673 + * how the redirectors are implemented or stored, etc. It may not do that
2674 + * all yet, but it tries.
2676 + * The snapshot inode redirection is stored in the primary/direct inode as
2677 + * an extended attribute $snap, in the form of little-endian u32 inode
2682 +#ifndef _LINUX_SNAP_H
2683 +#define _LINUX_SNAP_H
2685 +#include <linux/fs.h>
2687 +/* maximum number of snapshots available for users */
2688 +#define MAX_SNAPS 20
2690 +/* snap extended attributes definition */
2691 +#define SNAP_ATTR "@snap"
2696 + ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2697 + ino_t parent_ino[MAX_SNAPS+1];
2699 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
2701 +/* for compatibility with old 128 max snapshots */
2702 +#define MAX_SNAP128_DATA (sizeof(struct snap_ea) - (sizeof(ino_t) * 128 * 2))
2703 +#define ZERO_SNAP_ATTR_TOP(buf) \
2705 + struct snap_ea *p = (struct snap_ea*)buf; \
2706 + memset(&p->ino[129], 0, sizeof(ino_t)*128); \
2707 + memset(&p->parent_ino[129], 0, sizeof(ino_t)*128); \
2710 +/* snap new ea definition , for logging of new inode */
2711 +#define SNAP_NEW_INO_ATTR "@snap_new"
2712 +struct snap_new_ea{
2713 + ino_t prev_ino; /* reserved. save the inode to a linked list */
2715 + int new_index; /* indicate for which index this is a new inode */
2717 +#define NULL_NEW_INDEX -1 /* null new index, to clear the snap_new_ea */
2719 +/* ea to identiry a indirect inode's infomation */
2720 +#define SNAP_INDIRECT_INFO_ATTR "@snap_indirect_inode_info"
2721 +struct snap_indirect_info {
2722 + __u32 index; /* which index belongs to */
2723 + __u32 reserved[3]; /* reserved */
2727 +/* snapfs meta data stored in extended attributes of root ino */
2728 +#define DISK_SNAP_META_ATTR "@disk_snap_meta_attr"
2729 +struct disk_snap_meta_data {
2730 + ino_t snap_first_cowed_ino;
2731 + ino_t snap_table_ino;
2732 + __u32 snap_feature_compat;
2734 +/*snapfs quota info */
2736 +#define SNAP_USR_QUOTA 0
2737 +#define SNAP_GRP_QUOTA 1
2738 +#define DISK_SNAP_QUOTA_INFO "@disk_snap_quota_info"
2739 +struct quota_info_len {
2740 + int uid_len; /*uid quota info length */
2741 + int gid_len; /*gid quota info length */
2744 + * Check if the EA @name is Snap EA or not.
2745 + * Snap EA includes the SNAP_ATTR, SNAP_NEW_INO_ATTR and DISK_SNAP_META_ATTR
2748 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) || \
2749 + (!strcmp((name), DISK_SNAP_META_ATTR)))
2753 +/* file system features */
2754 +#define SNAP_FEATURE_COMPAT_SNAPFS 0x0010
2755 +#define SNAP_FEATURE_COMPAT_BLOCKCOW 0x0020
2757 +/* constants for snap_feature operations */
2758 +#define SNAP_CLEAR_FEATURE 0x0
2759 +#define SNAP_SET_FEATURE 0x1
2760 +#define SNAP_HAS_FEATURE 0x2
2762 +/* snap flags for inode, within 1 byte range, each occupy 1 bit */
2763 +#define SNAP_INO_MAGIC 0x88 /* magic for snap inode */
2764 +#define SNAP_COW_FLAG 0x01 /* snap redirected inode */
2765 +#define SNAP_DEL_FLAG 0x02 /* snap deleted inode */
2766 +#define SNAP_TABLE_FLAG 0x04 /* snap table inode */
2767 +#define SNAP_PRI_FLAG 0x08 /* primary inode */
2769 +/* no snapfs attributes for get_indirect_ino */
2770 +#define ENOSNAPATTR 320
2772 +/* constants used by iterator */
2773 +#define SNAP_ITERATE_ALL_INODE 0x0
2774 +#define SNAP_ITERATE_COWED_INODE 0x1
2776 +/* constants used by create_indirect */
2777 +#define SNAP_CREATE_IND_NORMAL 0x0
2778 +#define SNAP_CREATE_IND_DEL_PRI 0x1
2780 +/* the data structure represent in the xfs_dinode.pad
2781 + offset 0: magic (1 byte)
2782 + offset 1: flag (1 byte)
2783 + offset 2: gen (4 bytes)
2786 +#define SIZEOF_MAGIC 1
2787 +#define SIZEOF_FLAG 1
2788 +#define SIZEOF_GENERATION 4
2790 +#define MAGIC_OFFSET 0
2791 +#define FLAG_OFFSET 1
2792 +#define GENERATION_OFFSET 2
2794 +#define SNAP_GET_DINODE_MAGIC(dinode) \
2795 + (((__u8*)(dinode)->di_pad)[MAGIC_OFFSET])
2796 +#define SNAP_SET_DINODE_MAGIC(dinode) \
2797 + ((__u8*)(dinode)->di_pad)[MAGIC_OFFSET] = (SNAP_INO_MAGIC)
2798 +#define SNAP_GET_DINODE_FLAG(dinode) \
2799 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET])
2800 +#define SNAP_SET_DINODE_FLAG(dinode, flag) \
2801 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] |= (flag))
2802 +#define SNAP_CLEAR_DINODE_FLAG(dinode, flag) \
2803 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] &= ~(flag))
2804 +#define SNAP_GET_DINODE_GEN(dinode) \
2805 + (le32_to_cpu(*(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET])))
2806 +#define SNAP_SET_DINODE_GEN(dinode, gen) \
2807 + *(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET]) = cpu_to_le32(gen)
2810 +/* header of saving snaptable */
2812 + unsigned int size; /* buffer size passed by */
2813 + char data[0]; /* followed by actual data */
2816 +/* header of on-disk table data */
2817 +struct disk_snap_table_header {
2823 +/* table magic and version constant */
2824 +#define SNAP_TABLE_MAGIC 0xB3A2957F
2825 +#define SNAP_TABLE_VERSION 1
2828 +#define SNAPTABLE_BLOCKS(sb,size) \
2829 + (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
2830 + >> sb->s_blocksize_bits)+1)
2833 +#define SNAP_VERSION(a,b,c) \
2834 + (((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF))
2835 +#define SNAP_VERSION_MAJOR(v) \
2836 + ((v >> 16) & 0xFF)
2837 +#define SNAP_VERSION_MINOR(v) \
2839 +#define SNAP_VERSION_REL(v) \
2842 +/* for snap meta attr table */
2843 +#define TABLE_ITEM_COUNT 200
2844 +#define TABLE_ITEM_SIZE 1000
2845 +#define TABLE_ITEM_NAME_SIZE 16
2847 +/*snap table array */
2848 +struct snap_meta_array {
2849 + char name[TABLE_ITEM_NAME_SIZE];
2850 + int start; /* where is the start of the array */
2851 + int len; /* the len of the array */
2853 +/* snap table structure for record the information */
2854 +struct table_snap_meta_data {
2856 + struct snap_meta_array array[TABLE_ITEM_COUNT];
2861 +#define SNAP_PROFILE
2863 +#undef SNAP_PROFILE
2866 +#ifdef SNAP_PROFILE
2867 +struct profile_snapdel_stat
2869 + unsigned long total_tick; /* total time */
2870 + unsigned long inodes; /* primary inodes */
2872 + unsigned long yield_count; /* for yeild cpu */
2873 + unsigned long yield_tick;
2874 + unsigned long yield_max_tick;
2876 + unsigned long getea_count; /* for get ea */
2877 + unsigned long getea_tick;
2878 + unsigned long getea_max_tick;
2880 + unsigned long setea_count; /* for set ea */
2881 + unsigned long setea_tick;
2882 + unsigned long setea_max_tick;
2884 + unsigned long converge_count; /* for converge */
2885 + unsigned long converge_tick;
2886 + unsigned long converge_max_tick;
2891 +/* snapshot operations */
2892 +struct snapshot_operations {
2893 + unsigned int ops_version;
2894 + int (*is_redirector) (struct inode *inode);
2895 + int (*is_indirect) (struct inode *inode);
2896 + struct inode * (*create_indirect) (struct inode *pri, int index,
2897 + unsigned int gen, ino_t parent_ino,
2899 + struct inode * (*get_indirect) (struct inode *pri, int *table,int slot);
2900 + ino_t (*get_indirect_ino) (struct inode *pri, int index);
2901 + int (*destroy_indirect) (struct inode *pri, int index,
2902 + struct inode *next_ind);
2903 + int (*restore_indirect) (struct inode *pri, int index);
2904 + int (*iterate) (struct super_block *sb,
2905 + int (*repeat)(struct inode *inode, void *priv),
2906 + struct inode **start, void *priv, int flag);
2907 + int (*copy_block) ( struct inode *dst, struct inode *src, int blk);
2908 + int (*has_block) (struct inode *dst, int blk);
2909 + int (*set_indirect) (struct inode *pri, int index,
2910 + ino_t ind_ino, ino_t parent_ino );
2911 + int (*snap_feature) (struct super_block *sb, int feature, int op);
2912 + int (*get_generation) (struct inode *pri);
2913 + int (*set_generation) (struct inode *pri, unsigned long new_gen);
2914 + int (*has_del_flag) (struct inode *inode);
2915 + int (*clear_del_flag) (struct inode *inode);
2916 + int (*set_meta_attr)(struct super_block *sb, char *name,
2917 + char *buf, int size);
2918 + int (*get_meta_attr)(struct super_block *sb, char *name,
2919 + char *buf, int *size);
2923 Index: linux-2.4.20-8/include/linux/ext3_fs.h
2924 ===================================================================
2925 --- linux-2.4.20-8.orig/include/linux/ext3_fs.h 2004-01-05 10:54:03.000000000 +0800
2926 +++ linux-2.4.20-8/include/linux/ext3_fs.h 2004-01-16 20:45:09.000000000 +0800
2927 @@ -183,7 +183,13 @@
2928 #define EXT3_INDEX_FL 0x00001000 /* hash-indexed directory */
2929 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2930 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2931 -#define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2932 +/* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
2933 +#define EXT3_COW_FL 0x00008000 /* inode is snapshot cow */
2934 +#define EXT3_DEL_FL 0x00010000 /* inode is deleting in snapshot */
2935 +#define EXT3_SNAP_TABLE_FLAG 0x00020000 /* snap table inode */
2936 +/* FIXME For debugging will be removed later*/
2937 +#define EXT3_SNAP_PRI_FLAG 0x00040000 /* primary inode */
2940 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2941 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2942 @@ -205,10 +211,25 @@
2943 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2944 #define EXT3_IOC_GETVERSION_OLD _IOR('v', 1, long)
2945 #define EXT3_IOC_SETVERSION_OLD _IOW('v', 2, long)
2946 +/* the following are for temporary test */
2947 +/* snapfs ioctls */
2948 +#define EXT3_IOC_CREATE_INDIR _IOW('v', 3, long)
2949 +#define EXT3_IOC_GET_INDIR _IOW('v', 4, long)
2950 +#define EXT3_IOC_DESTROY_INDIR _IOW('v', 5, long)
2951 +#define EXT3_IOC_IS_REDIR _IOW('v', 6, long)
2952 +#define EXT3_IOC_RESTORE_INDIR _IOW('v', 7, long)
2954 +#define EXT3_IOC_SNAP_SETFILECOW _IOW('v', 10, long)
2956 +/* XXX: the following are for temporary test, can be removed later */
2957 +#define EXT3_IOC_SNAP_PRINT _IOW('v', 11, long)
2958 +#define EXT3_IOC_SNAP_DELETE _IOW('v', 12, long)
2959 +#define EXT3_IOC_SNAP_RESTORE _IOW('v', 13, long)
2962 #ifdef CONFIG_JBD_DEBUG
2963 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2967 * Structure of an inode on the disk
2969 @@ -429,7 +450,15 @@
2970 __u8 s_def_hash_version; /* Default hash version to use */
2971 __u8 s_reserved_char_pad;
2972 __u16 s_reserved_word_pad;
2973 - __u32 s_reserved[192]; /* Padding to the end of the block */
2974 + __u32 s_default_mount_opts;
2975 + __u32 s_first_meta_bg; /* First metablock group */
2976 + __u32 s_mkfs_time; /* When the filesystem was created */
2978 + __u32 s_first_cowed_pri_ino; /* For snapfs,the first cowed primary inode */
2979 + __u32 s_last_cowed_pri_ino; /* last cowed ino in memory */
2980 + __u32 s_snaptable_ino; /* snaptable ino in memory */
2981 + __u32 s_last_snap_orphan; /* SnapFS: start of cowing indirect inode */
2982 + __u32 s_reserved[186]; /* Padding to the end of the block,originally 204 */
2987 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2988 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2990 +#define EXT3_FEATURE_COMPAT_SNAPFS 0x0010
2991 +#define EXT3_FEATURE_COMPAT_BLOCKCOW 0x0020
2993 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2994 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2995 EXT3_FEATURE_INCOMPAT_RECOVER)
2996 Index: linux-2.4.20-8/include/linux/ext3_fs_sb.h
2997 ===================================================================
2998 --- linux-2.4.20-8.orig/include/linux/ext3_fs_sb.h 2004-01-05 10:54:00.000000000 +0800
2999 +++ linux-2.4.20-8/include/linux/ext3_fs_sb.h 2004-01-13 00:10:14.000000000 +0800
3001 wait_queue_head_t s_delete_thread_queue;
3002 wait_queue_head_t s_delete_waiter_queue;
3004 +#define EXT3_SNAP_FS
3005 +#ifdef EXT3_SNAP_FS
3006 + struct semaphore s_snap_list_sem;
3007 + unsigned long s_first_cowed_pri_ino;/* For snapfs,the first cowed primary inode */
3008 + unsigned long s_last_cowed_pri_ino; /* last cowed ino in memory */
3009 + unsigned long s_snaptable_ino; /* snaptable ino in memory */
3013 #endif /* _LINUX_EXT3_FS_SB */
3014 Index: linux-2.4.20-8/include/linux/ext3_jbd.h
3015 ===================================================================
3016 --- linux-2.4.20-8.orig/include/linux/ext3_jbd.h 2004-01-05 10:53:59.000000000 +0800
3017 +++ linux-2.4.20-8/include/linux/ext3_jbd.h 2004-01-13 00:10:14.000000000 +0800
3020 #define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
3022 +/*snapshot transaction blocks*/
3024 +#define EXT3_EA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3025 +#define EXT3_SETMETA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3026 +#define EXT3_NEWINODE_TRANS_BLOCKS 10
3027 +#define SNAP_INSERTLIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 1)
3028 +#define SNAP_DELETELIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 2)
3029 +#define SNAP_COPYBLOCK_TRANS_BLOCKS (EXT3_DATA_TRANS_BLOCKS)
3030 +#define SNAP_MIGRATEDATA_TRANS_BLOCKS 2
3031 +#define SNAP_SETIND_TRANS_BLOCKS (SNAP_INSERTLIST_TRANS_BLOCKS + 1)
3032 +#define SNAP_ADDORPHAN_TRANS_BLOCKS 2
3033 +#define SNAP_REMOVEORPHAN_TRANS_BLOCKS 1
3034 +#define SNAP_RESTOREORPHAN_TRANS_BLOCKS (EXT3_EA_TRANS_BLOCKS + \
3035 + SNAP_DELETELIST_TRANS_BLOCKS + \
3036 + EXT3_NEWINODE_TRANS_BLOCKS + \
3037 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS)
3038 +#define SNAP_BIGCOPY_TRANS_BLOCKS (2 * EXT3_DATA_TRANS_BLOCKS)
3039 +#define SNAP_CREATEIND_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3040 + SNAP_MIGRATEDATA_TRANS_BLOCKS + \
3041 + SNAP_SETIND_TRANS_BLOCKS + \
3042 + SNAP_BIGCOPY_TRANS_BLOCKS + 3)
3043 +#define SNAP_MIGRATEBLK_TRANS_BLOCKS 2
3044 +#define SNAP_DESTROY_TRANS_BLOCKS (SNAP_DELETELIST_TRANS_BLOCKS + \
3045 + EXT3_EA_TRANS_BLOCKS + 2)
3046 +#define SNAP_RESTORE_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3047 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS + 1)
3050 ext3_mark_iloc_dirty(handle_t *handle,
3051 struct inode *inode,
3054 fs/ext3/Makefile | 2
3055 fs/ext3/ialloc.c | 7
3057 fs/ext3/snap.c | 2583 +++++++++++++++++++++++++++++++++++++++++++++
3059 include/linux/ext3_fs.h | 38
3060 include/linux/ext3_fs_sb.h | 7
3061 include/linux/ext3_jbd.h | 27
3062 include/linux/snap.h | 266 ++++
3063 9 files changed, 2934 insertions(+), 5 deletions(-)