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
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 +#define EXT3_SNAP_DEBUG
50 +#ifdef EXT3_SNAP_DEBUG
51 + #define snap_debug(f, a...) \
53 + printk (KERN_INFO "SNAP DEBUG: (%s, %d): %s: ", \
54 + __FILE__, __LINE__, __FUNCTION__); \
58 + #define snap_err(f, a...) \
60 + printk (KERN_ERR "SNAP ERROR: (%s, %d): %s: ", \
61 + __FILE__, __LINE__, __FUNCTION__); \
66 + #define snap_debug(f, a...) do {} while (0)
67 + #define snap_err(f, a...) \
69 + printk (KERN_ERR "SNAP ERROR: (%s, %d): ", \
70 + __FILE__, __LINE__); \
76 +#ifdef EXT3_SNAP_DEBUG
77 + #define ALLOC(ptr, cast, size) \
79 + ptr = (cast)kmalloc((size_t) size, GFP_KERNEL); \
81 + printk(KERN_ERR "kmalloc returns 0 at %s:%d\n", \
82 + __FILE__, __LINE__); \
84 + snap_kmem += size; \
85 + printk(KERN_INFO "snap_alloc %d, kmem %ld\n", \
86 + (size_t)size, snap_kmem); \
90 + #define FREE(ptr,size) \
93 + snap_kmem -= size; \
94 + printk(KERN_INFO "snap_free %d, kmem %ld\n", \
95 + (size_t)size, snap_kmem); \
99 + #define ALLOC(ptr, cast, size) \
101 + ptr = (cast)kmalloc((size_t) size, GFP_KERNEL); \
104 + #define FREE(ptr,size) \
109 +#endif /* EXT3_SNAP_DEBUG */
111 +#ifdef EXT3_SNAP_DEBUG
112 + /* modestr: convert inode mode to string . debug function */
113 + static char * modestr ( umode_t mode )
115 + if( S_ISREG(mode) )
117 + else if(S_ISDIR(mode))
119 + else if(S_ISLNK(mode))
121 + else if(S_ISCHR(mode))
123 + else if(S_ISBLK(mode))
125 + else if(S_ISFIFO(mode))
127 + else if(S_ISSOCK(mode))
130 + return "non-known";
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); }
139 + #define modestr(mode) do {} while (0)
140 + #define DEBUG_INODE(inode)
142 +#endif /* EXT3_SNAP_DEBUG */
143 +/* do file cow on: dir, symlink, regular but fs has filecow flag */
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)))
151 +#define SNAP_ERROR(err) ((err) < 0 ? (err) : (-(err)))
152 +/* SNAP_ERROR(err): Make sure we return negative errors for Linux ( return positive errors) */
157 +# define CDEBUG_STACK (THREAD_SIZE - \
158 + ((unsigned long)__builtin_dwarf_cfa() & \
159 + (THREAD_SIZE - 1)))
161 +# define CDEBUG_STACK (THREAD_SIZE - \
162 + ((unsigned long)__builtin_frame_address(0) & \
163 + (THREAD_SIZE - 1)))
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);
171 +#define CDEBUG(mask, format, a...) \
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); \
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)
183 +#define RETURN(rc) \
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; \
193 + CDEBUG(D_TRACE, "Process entered\n"); \
198 + CDEBUG(D_TRACE, "Process leaving\n"); \
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)
211 +#define SNAP_ATTR_BUF_CNT 10
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)
219 +#define SNAP_HAS_COMPAT_FEATURE(sb,mask) \
220 + (SB_FEATURE_COMPAT(sb) & cpu_to_le32(mask))
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))
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)))
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,
233 +/* helper functions to manipulate field 'parent' in snap_ea */
235 +set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
237 + char * p = (char*) pea;
240 + offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
241 + offset += sizeof(ino_t) * index;
242 + *(ino_t*)(p+offset) = val;
246 +/* ext3_iterate_cowed_inode:
247 + * iterate all the cowed inode with the same index and
248 + * run the associate function @repeat
250 + * For @repeat, if it returns non-zero value, it will exit the iterator
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.
258 +static int ext3_iterate_cowed_inode(
259 + struct super_block *sb,
260 + int (*repeat)(struct inode *inode, void *priv),
261 + struct inode **start,
264 + struct inode *list_inode = NULL;
265 + char buf[EXT3_MAX_SNAP_DATA];
266 + struct snap_ea *snaps;
269 + if (SB_FIRST_COWED_INO(sb) == 0) {
270 + snap_debug("no cowed inode in the list\n");
274 + /* get head inode in the list */
275 + if (start != NULL && *start != NULL && (*start)->i_ino)
276 + list_inode = iget(sb, (*start)->i_ino);
278 + list_inode = iget (sb, le32_to_cpu( SB_FIRST_COWED_INO(sb) ));
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",
286 + list_inode->i_mode,
287 + list_inode->i_nlink);
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);
299 + if ((err = (*repeat)(list_inode, priv)) != 0)
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));
309 + snap_debug ("cowed inode list end, exit\n");
319 +static int get_cowed_ino(struct inode *pri, void *param)
321 + ino_t *find = param;
322 + (*find) = pri->i_ino;
326 +/* Return 0 for error. */
327 +static int get_cowed_ino_end (struct inode *inode)
332 + rc = ext3_iterate_cowed_inode(inode->i_sb, &get_cowed_ino, &inode, &ino);
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)
344 + struct inode *inode = NULL;
345 + ino_t first, last = 0;
347 + last = le32_to_cpu(SB_LAST_COWED_INO(sb));
351 + first = le32_to_cpu(SB_FIRST_COWED_INO(sb));
354 + snap_err("first cowed inode is NULL\n");
358 + inode = iget(sb, first);
360 + if (is_bad_inode(inode)) {
361 + snap_err("bad inode %lu\n", first);
365 + last = get_cowed_ino_end(inode);
373 +/* Insert the primary inode to the cowed inode list
374 + * Append it to the list end
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
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.
384 +static int insert_cowed_ino_to_list (handle_t *handle, struct inode *pri, char *buf_pri)
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);
393 + snaps_pri = (struct snap_ea *)buf_pri;
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);
400 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
401 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
403 + snap_err("ino %lu, set_ext_attr err %d\n", pri->i_ino, err);
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;
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");
425 + last_inode = iget(pri->i_sb, SB_LAST_COWED_INO(pri->i_sb));
426 + if (!last_inode || is_bad_inode(last_inode)) {
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);
439 + /*set primary inode EA*/
440 + snaps_pri->next_ino = 0;
441 + snaps_pri->prev_ino = cpu_to_le32(last_inode->i_ino);
443 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
444 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
446 + snap_debug("set attributes error for inode %lu\n",
447 + (ulong)pri->i_ino);
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);
457 + snap_debug("set attributes error for inode %lu\n",
458 + (ulong)last_inode->i_ino);
462 + EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG;
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);
475 +/* delelte the ino from cowed inode list */
476 +static int delete_cowed_ino_from_list (handle_t *handle, struct inode *inode)
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];
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);
492 + snaps = (struct snap_ea *) buf;
493 + next_ino = le32_to_cpu(snaps->next_ino);
494 + prev_ino = le32_to_cpu(snaps->prev_ino);
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;
501 + SB_LAST_COWED_INO(inode->i_sb) = 0;
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))
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);
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);
524 + snap_debug("delete ino %lu from list\n", inode->i_ino);
526 + err = ext3_xattr_set(handle, prev_inode, EXT3_SNAP_INDEX,
527 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
529 + snap_err("err %d setting ea for ino %lu\n", err, prev_inode->i_ino);
533 + if (next_ino == 0) {
534 + SB_LAST_COWED_INO(inode->i_sb) = prev_ino;
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))
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);
549 + snaps = ( struct snap_ea *) buf;
550 + snaps->prev_ino = cpu_to_le32(prev_ino);
552 + err = ext3_xattr_set(handle, next_inode, EXT3_SNAP_INDEX,
553 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
555 + snap_err("err %d setting attributes for ino %lu\n",
556 + err, next_inode->i_ino);
565 +static inline void lock_list(struct super_block *sb)
567 + down(&SB_SNAP_LIST_SEM(sb));
570 +static inline void unlock_list(struct super_block *sb)
572 + up(&SB_SNAP_LIST_SEM(sb));
575 +static int ext3_snap_feature (struct super_block *sb, int feature, int op) {
580 + case SNAP_SET_FEATURE:
581 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
583 + ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
584 + SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
586 + ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
588 + ext3_journal_stop(handle, sb->s_root->d_inode);
590 + case SNAP_CLEAR_FEATURE:
591 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
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);
598 + ext3_journal_stop(handle, sb->s_root->d_inode);
600 + case SNAP_HAS_FEATURE:
601 + /*FIXME should lock super or not*/
602 + rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
610 +#ifdef _DEVICE_FAIL_TEST
612 +extern int loop_discard_io(kdev_t dev, long arg);
614 + * modify failpos to let loop fail at certain point
615 + * let pos=0 mean no fail point
617 +static int failpos = 0;
618 +#define loopfail(pos) \
620 + if( pos == failpos ){ \
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 ); \
628 +#define loopfail(pos) do{}while(0)
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 )
634 + char buf[EXT3_MAX_SNAP_DATA];
635 + struct snap_ea *snaps;
636 + int err = 0, inlist = 1;
638 + handle_t *handle = NULL;
640 + snap_debug("(ino %lu, parent %lu): saving ind %lu to index %d\n",
641 + pri->i_ino, parent_ino, ind_ino, index);
643 + if (index < 0 || index > MAX_SNAPS || !pri)
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);
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?
659 + } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
663 + handle = ext3_journal_start(pri, SNAP_SETIND_TRANS_BLOCKS);
665 + err = PTR_ERR(handle);
669 + snaps = (struct snap_ea *)buf;
670 + snaps->ino[index] = cpu_to_le32 (ind_ino);
671 + ea_size = EXT3_MAX_SNAP_DATA;
673 + set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
675 + snap_debug("saving attributes\n");
678 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
679 + buf, EXT3_MAX_SNAP_DATA, 0);
682 + /* This will also write the ea for the pri inode, like above */
683 + err = insert_cowed_ino_to_list(handle, pri, buf);
685 + ext3_mark_inode_dirty(handle, pri);
686 + ext3_journal_stop(handle, pri);
688 + unlock_list(pri->i_sb);
693 + * is_redirector - determines if a primary inode is a redirector
694 + * @inode: primary inode to test
696 + * Returns 1 if the inode is a redirector, 0 otherwise.
698 +static int is_redirector(struct inode *inode)
700 + int is_redirector = 0;
703 + rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
705 + if (rc > 0 && rc <= MAX_SNAP_DATA)
707 + snap_debug("inode %lu %s redirector\n", inode->i_ino,
708 + is_redirector ? "is" : "isn't");
709 + return is_redirector;
712 +/*if it's indirect inode or not */
713 +static int is_indirect(struct inode *inode)
715 + if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
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)?
724 +static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
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;
745 + if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
750 + if (ext3_xattr_list(src, names, 0) < 0)
753 + * the list of attribute names are stored as NUL terminated
754 + * strings, with a double NUL string at the end.
757 + while ((namelen = strlen(name))) {
761 + /* don't copy snap data */
762 + if (!strcmp(name, EXT3_SNAP_ATTR)) {
763 + snap_debug("skipping %s item\n", name);
766 + snap_debug("copying %s item\n", name);
767 + attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX,
768 + EXT3_SNAP_ATTR, NULL, 0);
771 + if ((buf = kmalloc(attrlen, GFP_ATOMIC)) == NULL)
773 + if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
774 + EXT3_SNAP_ATTR, buf, attrlen) < 0)
776 + if (ext3_xattr_set(handle, dst, EXT3_SNAP_INDEX,
777 + EXT3_SNAP_ATTR, buf, attrlen, 0) < 0)
780 + name += namelen + 1; /* skip name and trailing NUL */
785 +static inline int ext3_has_ea(struct inode *inode)
787 + return (EXT3_I(inode)->i_file_acl != 0);
789 +/* XXX This function has a very bad effect to
790 + * the performance of filesystem,
791 + * will find another way to fix it
793 +static void fs_flushinval_pages(handle_t *handle, struct inode* inode)
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);
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.
811 +static int ext3_migrate_data (handle_t *handle, struct inode *dst, struct inode *src)
813 + unsigned long err = 0;
814 + /* 512 byte disk blocks per inode block */
815 + int bpib = src->i_sb->s_blocksize >> 9;
817 + if((!dst) || (!src))
820 + if (dst->i_ino == src->i_ino)
823 + fs_flushinval_pages(handle, src);
825 + ext3_copy_meta(handle, dst, src);
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 */
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));
834 + ext3_discard_prealloc(src);
836 + dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
837 + src->i_size = EXT3_I(src)->i_disksize = 0;
839 + dst->i_blocks = src->i_blocks;
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;
848 + if( ext3_has_ea (dst))
849 + dst->i_blocks += bpib;
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);
858 + return SNAP_ERROR(err);
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
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.
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.
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.
880 +static struct inode *ext3_get_indirect(struct inode *primary, int *table,
883 + char buf[EXT3_MAX_SNAP_DATA];
884 + struct snap_ea *snaps;
886 + struct inode *inode = NULL;
887 + int err = 0, index = 0;
889 + if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
892 + snap_debug("ino %lu, table %p, slot %d\n", primary->i_ino, table,slot);
894 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
895 + buf, EXT3_MAX_SNAP_DATA);
896 + if (err == -ENODATA) {
898 + } else if (err < 0) {
899 + snap_debug(" attribute read error\n");
902 + snaps = (struct snap_ea *)buf;
904 + /* if table is NULL and there is a slot */
905 + if( !table && slot ) {
907 + ino = le32_to_cpu ( snaps->ino[index] );
908 + if(ino) inode = iget(primary->i_sb, ino);
911 + /* if table is not NULL */
912 + while ( !inode && slot > 0) {
913 + index = table[slot];
914 + ino = le32_to_cpu ( snaps->ino[index] );
916 + snap_debug("snap inode at slot %d is %lu\n", slot, ino);
921 + inode = iget(primary->i_sb, ino);
924 + if( slot == 0 && table ) {
925 + snap_debug("redirector not found, using primary\n");
926 + inode = iget(primary->i_sb, primary->i_ino);
932 +/* get the indirect ino at index of the primary inode
933 + * return value: postive: indirect ino number
934 + * negative or 0: error
936 +static ino_t ext3_get_indirect_ino(struct inode *primary, int index)
938 + char buf[EXT3_MAX_SNAP_DATA];
939 + struct snap_ea *snaps;
943 + if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
946 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
947 + buf, EXT3_MAX_SNAP_DATA);
948 + if (err == -ENOATTR) {
951 + } else if (err < 0) {
952 + snap_err(EXT3_SNAP_ATTR " attribute read error\n");
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);
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.
968 + 1: The block has been copied successfully
969 + 0: No block is copied, usually this is because src has no such blk
973 +static int ext3_copy_block (struct inode *dst, struct inode *src, int blk)
975 + struct buffer_head *bh_dst = NULL, *bh_src = NULL;
977 + handle_t *handle = NULL;
980 + snap_debug("copy blk %d from %lu to %lu \n", blk, src->i_ino, dst->i_ino);
982 + * ext3_getblk() require handle!=NULL
984 + if (S_ISREG(src->i_mode))
987 + handle = ext3_journal_start(dst, SNAP_COPYBLOCK_TRANS_BLOCKS);
991 + bh_src = ext3_bread(handle, src, blk, 0, &err);
993 + snap_err("error for src blk %d, error %d\n", blk, err);
996 + bh_dst = ext3_getblk(handle, dst, blk, 1, &err);
998 + snap_err("error for dst blk %d, error %d\n", blk, err);
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);
1006 + ext3_journal_get_write_access(handle, bh_dst);
1008 + memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
1010 + ext3_journal_dirty_metadata(handle, bh_dst);
1013 + if (bh_src) brelse(bh_src);
1014 + if (bh_dst) brelse(bh_dst);
1016 + ext3_journal_stop(handle, dst);
1020 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1022 + * add one inode to superblock's snap_orphan chain
1023 + * only add on-disk data for simplicity
1025 +static void add_snap_orphan(handle_t *handle, struct inode *pri, struct inode *ind)
1027 + struct ext3_sb_info *sb = &pri->i_sb->u.ext3_sb;
1028 + struct ext3_iloc iloc;
1030 + if( ext3_get_inode_loc(ind, &iloc) ){
1031 + snap_debug("--- get ind loc fail\n");
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);
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);
1054 + * counterpart of add_snap_orphan
1056 +static void remove_snap_orphan(handle_t *handle, struct inode *ind)
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;
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 );
1068 + snap_debug("iget %lu fail\n", ino);
1071 + if( ext3_get_inode_loc(inode, &iloc) ){
1072 + snap_debug("get_inode_loc %lu fail\n", ino);
1075 + if( ino == ind->i_ino ){
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);
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);
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);
1101 + ino = iloc.raw_inode->i_next_snap_orphan;
1106 + unlock_super(ind->i_sb);
1111 + * FIXME: how about crashs again during recovery?
1113 +void snap_orphan_cleanup(struct super_block *sb)
1115 + ino_t ind_ino, pri_ino;
1116 + struct inode *ind = NULL, *pri = NULL;
1117 + struct ext3_iloc ind_iloc;
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");
1124 + snap_debug("------ begin cleanup snap orphans ------\n");
1126 + ind = iget( sb, ind_ino );
1128 + snap_err("snap_orphan_cleanup: get "
1129 + "ind %lu fail\n", ind_ino);
1133 + if( ext3_get_inode_loc(ind, &ind_iloc) ){
1134 + snap_err("snap_orphan_cleanup: get "
1135 + "iloc %lu fail\n", ind_ino);
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;
1144 + pri = iget( sb, pri_ino );
1146 + snap_err("snap_orphan_cleanup: get primary "
1147 + "%lu fail\n", pri_ino);
1150 + restore_snap_inode(pri, ind);
1151 + }while( ind_ino );
1152 + snap_debug("------ end cleanup snap orphans ------\n");
1154 + sb->u.ext3_sb.s_es->s_last_snap_orphan = 0;
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
1163 +static void unset_indirect(handle_t *handle, struct inode *pri, struct inode *ind)
1165 + char buf[EXT3_MAX_SNAP_DATA];
1166 + struct snap_ea *snaps;
1167 + int err, alone=1, index, found;
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);
1173 + if( err == -ENOATTR ){
1174 + snap_debug("primary inode has not EA\n");
1177 + snap_debug("get EA error on primary inode,"
1178 + "returned value %d\n", err);
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 )
1188 + else if( snaps->ino[index] )
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);
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);
1205 + snap_debug("didn't found ind in pri's EA, do nothing\n");
1214 + * restore all data in @ind to @pri after free data blocks of @pri.
1215 + * then release @ind
1217 +static void restore_snap_inode(struct inode *pri, struct inode *ind)
1220 + struct inode *tmp;
1222 + snap_debug("restore from indirect %lu to primary %lu\n",
1223 + ind->i_ino, pri->i_ino);
1225 + handle = ext3_journal_start(pri, SNAP_RESTOREORPHAN_TRANS_BLOCKS);
1229 + /* first: taken from pri's ea, or from fs-wide primary inode chain */
1230 + unset_indirect(handle, pri, ind);
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);
1236 + snap_debug("ext3_new_inode error\n");
1240 + ext3_migrate_data(handle, tmp, pri);
1241 + snap_debug("freeing half-copied %lu blocks\n", tmp->i_blocks );
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);
1251 + /* final: delete ind inode */
1257 + ext3_journal_stop(handle, pri);
1260 +static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
1261 + struct inode *src, int *has_orphan)
1263 + unsigned long blocks, blk, cur_blks;
1264 + int low_credits, save_ref;
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;
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))
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");
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);
1289 + ext3_orphan_add(handle, dst);
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);
1300 + * We can be sure the last handle was stoped
1301 + * ONLY if the handle's reference count is 1
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");
1311 + handle = ext3_journal_start(dst,
1312 + low_credits + needed);
1314 + snap_err("fail to restart handle\n");
1317 + handle->h_ref = save_ref;
1320 + if (ext3_copy_block( dst, src, blk) < 0 )
1322 + cur_blks += dst->i_sb->s_blocksize / 512;
1324 + dst->i_size = dst->u.ext3_i.i_disksize = src->i_size;
1329 +static int ext3_set_generation(struct inode *inode, unsigned long gen)
1334 + handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
1336 + err = ext3_xattr_set(handle, inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1337 + (char*)&gen, sizeof(int), 0);
1339 + snap_err("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
1343 + ext3_journal_stop(handle, inode);
1347 +static int ext3_get_generation(struct inode *inode)
1351 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1352 + (char*)&gen, sizeof(gen));
1354 + if (err == -ENODATA) {
1357 + snap_err("can not get generation from %lu \n", inode->i_ino);
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
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.
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.
1379 +static struct inode *ext3_create_indirect(
1380 + struct inode *pri,
1386 + struct inode *ind;
1387 + handle_t *handle = NULL;
1389 + int has_orphan = 0;
1391 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1392 + printk( KERN_EMERG "TRY TO COW JOUNRAL\n");
1395 + snap_debug("creating indirect inode for %lu at index %d, %s pri\n",
1396 + pri->i_ino, index, del ? "deleting" : "preserve");
1398 + ind = ext3_get_indirect(pri, NULL, index);
1402 + handle = ext3_journal_start(pri, SNAP_CREATEIND_TRANS_BLOCKS);
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.
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
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"
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
1423 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1425 + down(&tmp->i_sem);
1426 + ext3_migrate_data(handle, tmp, pri);
1432 + snap_err("ext3_new_inode error\n");
1437 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1438 + ext3_mark_inode_dirty(handle, pri);
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);
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);
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.
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.
1470 + snap_debug("deleting primary inode\n");
1472 + down(&ind->i_sem);
1473 + err = ext3_migrate_data(handle, ind, pri);
1477 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1481 + /* XXX for directory, we copy the block back
1482 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1484 + if( S_ISDIR(pri->i_mode)) {
1485 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
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);
1501 + down(&ind->i_sem);
1502 + err = ext3_migrate_data(handle, ind, pri);
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)) {
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;
1515 + int bpib = pri->i_sb->s_blocksize >> 9;
1516 + snap_debug("ino %lu, do file cow\n", pri->i_ino);
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 )
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;
1533 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
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;
1542 + ext3_mark_inode_dirty(handle, pri);
1543 + ext3_mark_inode_dirty(handle, ind);
1545 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
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);
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);
1568 + ext3_orphan_del(handle, ind);
1571 + ext3_journal_stop(handle, pri);
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);
1587 + ext3_orphan_del(handle, ind);
1591 + ext3_journal_stop(handle, pri);
1593 + snap_err("exiting with error %d\n", err);
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))
1602 +static inline int block_bmap (struct buffer_head * bh, int nr)
1608 + tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
1613 +static inline int block_setbmap (handle_t *handle, struct buffer_head * bh, int nr, int physical)
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);
1625 +static int ext3_migrate_block (handle_t *handle, struct inode * dst, struct inode *src, int block)
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;
1636 + ext3_warning (src->i_sb, "ext3_migrate_block", "block < 0");
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");
1645 + /* EXT3_NDIR_BLOCK */
1646 + if (block < EXT3_NDIR_BLOCKS) {
1647 + if( inode_bmap(dst, block) ) return 0;
1649 + if( (physical = inode_bmap(src, block)) ) {
1650 + inode_setbmap (dst, block, physical);
1651 + inode_setbmap (src, block, 0);
1658 + /* EXT3_IND_BLOCK */
1659 + block -= EXT3_NDIR_BLOCKS;
1660 + if (block < addr_per_block) {
1661 + i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1664 + physical = inode_bmap(src, EXT3_IND_BLOCK);
1666 + inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1667 + inode_setbmap (src, EXT3_IND_BLOCK, 0);
1673 + if( block_bmap (bread (ddev, i1_d, blksz), block ))
1676 + i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1677 + if( !i1_s) return 0;
1679 + physical = block_bmap ( bread (sdev, i1_s, blksz), block );
1682 + block_setbmap(handle, bread(ddev, i1_d, blksz),block,physical);
1683 + block_setbmap(handle, bread(sdev, i1_s, blksz), block, 0);
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);
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);
1703 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1704 + block >> addr_per_block_bits);
1708 + if( !i1_s) return 0;
1710 + physical = block_bmap (bread (sdev, i1_s, blksz),
1711 + block >> addr_per_block_bits);
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);
1722 + physical = block_bmap (bread (ddev, i2_d,
1724 + block & (addr_per_block - 1));
1728 + i2_s = block_bmap (bread (sdev, i1_s,
1730 + block >> addr_per_block_bits);
1731 + if(!i2_s) return 0;
1733 + physical = block_bmap(bread (sdev, i2_s,
1735 + block & (addr_per_block - 1));
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);
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);
1753 + if( (physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1754 + inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1758 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1759 + block >> (addr_per_block_bits * 2));
1761 + if(i1_s) i2_s = block_bmap (bread (sdev, i1_s, blksz),
1762 + block >> (addr_per_block_bits * 2));
1766 + if( !i1_s) return 0;
1768 + physical = block_bmap (bread (sdev, i1_s, blksz),
1769 + block >> (addr_per_block_bits * 2));
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);
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));
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));
1790 + block_setbmap (handle, bread (ddev, i2_d, blksz),
1791 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1793 + block_setbmap (handle, bread (sdev, i2_s, blksz),
1794 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1801 + physical = block_bmap (bread (ddev, i3_d, blksz),
1802 + block & (addr_per_block - 1)) ;
1803 + if(physical) return 0;
1805 + if(!i3_s) return 0;
1806 + physical = block_bmap (bread (sdev, i3_s, blksz),
1807 + block & (addr_per_block - 1)) ;
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);
1820 +/* Generate i_blocks from blocks for an inode .
1821 + * We also calculate EA block here.
1823 +static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
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;
1831 + int meta_blocks = 0;
1833 + if( !inode ) return 0;
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;
1841 + /* calculate data blocks */
1842 + for(i = 0; i < blocks; i++ ) {
1843 + if(ext3_bmap(inode->i_mapping, i))
1846 + /* calculate meta blocks */
1847 + blocks -= EXT3_NDIR_BLOCKS;
1848 + if( blocks > 0 ) {
1850 + blocks -= addr_per_block;
1852 + if( blocks > 0 ) meta_blocks++;
1854 + while( (blocks > 0) && (i < addr_per_block) ) {
1856 + blocks -= addr_per_block;
1859 + if ( blocks > 0 ) meta_blocks += 2;
1862 + while( blocks > 0) {
1864 + blocks -= addr_per_block;
1866 + if(i >= addr_per_block ) {
1870 + if( j >= addr_per_block) {
1875 + /* calculate EA blocks */
1876 + if( ext3_has_ea (inode) ) meta_blocks++;
1878 + i_blocks += meta_blocks * bpib;
1879 + snap_debug("ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
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
1889 + * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1890 + * is NULL, we use the inode at @index.
1892 +static int ext3_destroy_indirect(struct inode *pri, int index,
1893 + struct inode *next_ind)
1895 + char buf[EXT3_MAX_SNAP_DATA];
1896 + struct snap_ea *snaps;
1897 + struct inode *ind;
1901 + handle_t *handle=NULL;
1904 + if (index < 0 || index > EXT3_MAX_SNAPS)
1907 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1908 + snap_err("TRY TO DESTROY JOURNAL'S IND\n");
1912 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1913 + buf, EXT3_MAX_SNAP_DATA);
1915 + snap_err("inode %lu attribute read error\n", pri->i_ino);
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);
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);
1929 + ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1931 + if ( !ind || IS_ERR(ind) || is_bad_inode(ind))
1934 + snap_debug("iget ind %lu, ref count = %d\n",
1935 + ind->i_ino, atomic_read(&ind->i_count));
1937 + handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
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)) {
1949 + down(&ind->i_sem);
1951 + double_down(&next_ind->i_sem, &ind->i_sem);
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);
1960 + for(i = 0; i < blocks; i++) {
1961 + if( ext3_bmap(next_ind->i_mapping, i) )
1963 + if( !ext3_bmap(ind->i_mapping, i) )
1965 + ext3_migrate_block(handle, next_ind, ind, i) ;
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);
1973 + for (i = 0; i < blocks; i++) {
1974 + if (ext3_bmap(next_ind->i_mapping, i))
1976 + if (ext3_copy_block(next_ind, ind, i ) < 0)
1979 + ext3_mark_inode_dirty(handle, next_ind);
1981 + if (next_ind == pri)
1984 + double_up(&next_ind->i_sem, &ind->i_sem);
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));
1994 + snaps->ino[index] = cpu_to_le32(0);
1995 + for (i = 0; i < EXT3_MAX_SNAPS; i++)
1996 + save += snaps->ino[i];
1999 + lock_list(pri->i_sb);
2000 + delete_cowed_ino_from_list(handle, pri);
2001 + unlock_list(pri->i_sb);
2004 + /* if there are no cowed inode left, then remove snapfs feature */
2005 + if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2007 + lock_super(pri->i_sb);
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);
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);
2021 + /* XXX clean the extended attribute feature,
2022 + * this is not safe, find a better way
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);
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);
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
2041 + if (!save && pri->u.ext3_i.i_dtime) {
2042 + snap_debug("deleting primary %lu\n", pri->i_ino);
2044 + /* reset err to 0 now */
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);
2057 + ext3_journal_stop(handle, pri);
2061 +/* restore a primary inode with the indirect inode at index */
2062 +static int ext3_restore_indirect(struct inode *pri, int index)
2064 + struct inode *ind;
2065 + struct inode *tmp;
2067 + handle_t *handle = NULL;
2069 + if (index < 0 || index > EXT3_MAX_SNAPS)
2072 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2073 + printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2076 + snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2078 + ind = ext3_get_indirect(pri, NULL, index);
2083 + snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2085 + handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
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);
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);
2100 + snap_err("restore_indirect, new_inode err\n");
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);
2110 +// ext3_destroy_indirect(pri, index);
2112 + ext3_journal_stop(handle, pri);
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
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.
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.
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
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.
2144 +static int ext3_iterate_all(struct super_block *sb,
2145 + int (*repeat)(struct inode *inode, void *priv),
2146 + struct inode **start, void *priv)
2148 + struct inode *tmp = NULL;
2150 + ino_t istart, ibase;
2156 + *start = iget(sb, EXT3_ROOT_INO);
2161 + if (is_bad_inode(*start)) {
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);
2171 + if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2172 + if ((err = (*repeat)(*start, priv) != 0))
2175 + *start = iget(sb, EXT3_FIRST_INO(sb));
2180 + if (is_bad_inode(*start)) {
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;
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))
2201 + bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2202 + if (bitmap_nr < 0)
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;
2213 + if (!bitmap[ibyte])
2216 + /* FIXME need to verify if bit endianness will
2217 + * work properly here for all architectures.
2219 + for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2220 + ino_t ino = ibase + (ibyte << 3) + i;
2222 + if ((bitmap[ibyte] & bit) == 0)
2225 + if (ino < (*start)->i_ino)
2228 + *start = iget(sb, ino);
2233 + if (is_bad_inode(*start)) {
2238 + if ((err = (*repeat)(*start, priv)) != 0)
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)
2256 + case SNAP_ITERATE_ALL_INODE:
2257 + return ext3_iterate_all (sb, repeat, start, priv);
2259 + case SNAP_ITERATE_COWED_INODE:
2260 + return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2267 +static int find_snap_meta_index(
2268 + struct table_snap_meta_data *snap_meta,
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)))
2281 + return -1; /* can not find */
2284 +int set_snap_meta_index(
2285 + struct table_snap_meta_data *snap_meta,
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;
2301 + return -1; /* can not find */
2304 +static int ext3_get_meta_attr(struct super_block *sb,
2305 + char* name, char* buf,
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;
2315 + ino = SB_SNAPTABLE_INO(sb);
2317 + snap_err("No table file \n");
2320 + inode = iget(sb, ino);
2321 + if(!inode || is_bad_inode(inode)){
2322 + snap_err("unable to get table ino %lu\n", ino);
2326 + /*read the table from the table inode*/
2327 + bh = ext3_bread(NULL, inode, 0, 0, &error);
2329 + snap_err("read table ino %lu, error %d\n", ino, error);
2333 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2334 + index = find_snap_meta_index(s_attr, name);
2336 + snap_debug("not exit %s meta attr of table ino %lu \n",
2337 + name, inode->i_ino);
2341 + if (!buf || *size < s_attr->array[index].len) {
2342 + /*return the size of this meta attr */
2343 + error = s_attr->array[index].len;
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;
2351 + array_bh = ext3_bread(NULL, inode,
2352 + s_attr->array[index].start + i,
2355 + snap_err("ino %lu read snap attr offset %d error %d \n",
2356 + inode->i_ino, (s_attr->array[index].start + i),
2360 + if (left_size >= sb->s_blocksize) {
2361 + memcpy(buf, array_bh->b_data, sb->s_blocksize);
2363 + memcpy(buf, array_bh->b_data, left_size);
2364 + left_size -= sb->s_blocksize;
2367 + *size = s_attr->array[index].len;
2374 +static int ext3_set_meta_attr(struct super_block *sb, char* name,
2375 + char* buf, int size)
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;
2385 + ino = SB_SNAPTABLE_INO(sb);
2387 + if (ino == 0 && !buf) {
2388 + snap_debug("no table ino \n");
2392 + handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2397 + /*create table inode update table ino*/
2398 + inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
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);
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);
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);
2420 + snap_err("read table ino %lu, error %d\n", ino, error);
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);
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
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;
2444 + ext3_journal_dirty_metadata(handle, bh);
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);
2452 + index = set_snap_meta_index(s_attr, name, size);
2454 + snap_err("table full of ino %lu \n", inode->i_ino);
2460 + s_attr->array[index].len = size;
2461 + journal_dirty_metadata(handle, bh);
2463 + /*put this attr to the snap table*/
2465 + for(i = 0; i < new_len; i++) {
2466 + struct buffer_head *array_bh = NULL;
2468 + array_bh = ext3_bread(handle, inode,
2469 + s_attr->array[index].start + i, 1, &error);
2471 + snap_err("inode %lu Can not get the block of attr %s\n",
2472 + inode->i_ino, name);
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);
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;
2488 + ext3_journal_stop(handle, sb->s_root->d_inode);
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,
2512 +EXPORT_SYMBOL(ext3_snap_operations);
2513 +#ifdef SNAP_PROFILE
2514 +EXPORT_SYMBOL(prof_snapdel);
2517 +#ifdef SNAP_DEBUG_IOC
2519 +static int print_inode(struct inode *pri, void *index_val)
2523 + struct snap_ea *snaps;
2524 + char buf[EXT3_MAX_SNAP_DATA];
2525 + int index = *(int *)index_val;
2527 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2528 + buf, EXT3_MAX_SNAP_DATA);
2530 + if (err == -ENODATA) {
2531 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
2533 + else if (err < 0) {
2534 + snap_err("got err %d when reading attributes\n", err);
2538 + snaps = (struct snap_ea *) buf;
2540 + if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2541 + snap_debug("no redirected ino for primary inode %lu\n",
2545 + snap_debug("primary inode %lu , redirected ino=%d\n",
2546 + primary->i_ino,le32_to_cpu(snaps->ino[index]));
2552 +int snap_print(struct super_block *sb, int index)
2554 + ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2558 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2560 + int index = *(int *)index_val;
2562 + printk("delete_inode for index %d\n",index);
2563 + rc = ext3_destroy_indirect(primary,index, NULL);
2565 + printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",
2566 + primary->i_ino, index, rc);
2570 +int ext3_snap_delete(struct super_block *sb, int index)
2572 + ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index,
2573 + SNAP_ITERATE_COWED_INODE);
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
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)
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.
2606 -static int ext3_bmap(struct address_space *mapping, long block)
2607 +int ext3_bmap(struct address_space *mapping, long block)
2609 struct inode *inode = mapping->host;
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 @@
2619 +/* Export load_inode_bitmap*/
2620 +int ext3_load_inode_bitmap (struct super_block * sb,
2621 + unsigned int block_group)
2623 + return load_inode_bitmap(sb, block_group);
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);
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
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);
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
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>
2671 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2672 + * Eric Mei <Ericm@clusterfs.com>
2673 + * Wang Di <wangdi@clusterfs.com>
2675 + * Rewriten 2003 by Wang Di <wangdi@clusterfs.com>
2676 + * Eric Mei <ericm@clusterfs.com>
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.
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
2690 +#ifndef _LINUX_SNAP_H
2691 +#define _LINUX_SNAP_H
2693 +#include <linux/fs.h>
2695 +/* maximum number of snapshots available for users */
2696 +#define MAX_SNAPS 20
2698 +/* snap extended attributes definition */
2699 +#define SNAP_ATTR "@snap"
2704 + ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2705 + ino_t parent_ino[MAX_SNAPS+1];
2707 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
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) \
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); \
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 */
2723 + int new_index; /* indicate for which index this is a new inode */
2725 +#define NULL_NEW_INDEX -1 /* null new index, to clear the snap_new_ea */
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 */
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;
2742 +/*snapfs quota info */
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 */
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
2756 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) || \
2757 + (!strcmp((name), DISK_SNAP_META_ATTR)))
2761 +/* file system features */
2762 +#define SNAP_FEATURE_COMPAT_SNAPFS 0x0010
2763 +#define SNAP_FEATURE_COMPAT_BLOCKCOW 0x0020
2765 +/* constants for snap_feature operations */
2766 +#define SNAP_CLEAR_FEATURE 0x0
2767 +#define SNAP_SET_FEATURE 0x1
2768 +#define SNAP_HAS_FEATURE 0x2
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 */
2777 +/* no snapfs attributes for get_indirect_ino */
2778 +#define ENOSNAPATTR 320
2780 +/* constants used by iterator */
2781 +#define SNAP_ITERATE_ALL_INODE 0x0
2782 +#define SNAP_ITERATE_COWED_INODE 0x1
2784 +/* constants used by create_indirect */
2785 +#define SNAP_CREATE_IND_NORMAL 0x0
2786 +#define SNAP_CREATE_IND_DEL_PRI 0x1
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)
2794 +#define SIZEOF_MAGIC 1
2795 +#define SIZEOF_FLAG 1
2796 +#define SIZEOF_GENERATION 4
2798 +#define MAGIC_OFFSET 0
2799 +#define FLAG_OFFSET 1
2800 +#define GENERATION_OFFSET 2
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)
2818 +/* header of saving snaptable */
2820 + unsigned int size; /* buffer size passed by */
2821 + char data[0]; /* followed by actual data */
2824 +/* header of on-disk table data */
2825 +struct disk_snap_table_header {
2831 +/* table magic and version constant */
2832 +#define SNAP_TABLE_MAGIC 0xB3A2957F
2833 +#define SNAP_TABLE_VERSION 1
2836 +#define SNAPTABLE_BLOCKS(sb,size) \
2837 + (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
2838 + >> sb->s_blocksize_bits)+1)
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) \
2847 +#define SNAP_VERSION_REL(v) \
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
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 */
2861 +/* snap table structure for record the information */
2862 +struct table_snap_meta_data {
2864 + struct snap_meta_array array[TABLE_ITEM_COUNT];
2869 +#define SNAP_PROFILE
2871 +#undef SNAP_PROFILE
2874 +#ifdef SNAP_PROFILE
2875 +struct profile_snapdel_stat
2877 + unsigned long total_tick; /* total time */
2878 + unsigned long inodes; /* primary inodes */
2880 + unsigned long yield_count; /* for yeild cpu */
2881 + unsigned long yield_tick;
2882 + unsigned long yield_max_tick;
2884 + unsigned long getea_count; /* for get ea */
2885 + unsigned long getea_tick;
2886 + unsigned long getea_max_tick;
2888 + unsigned long setea_count; /* for set ea */
2889 + unsigned long setea_tick;
2890 + unsigned long setea_max_tick;
2892 + unsigned long converge_count; /* for converge */
2893 + unsigned long converge_tick;
2894 + unsigned long converge_max_tick;
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,
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);
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 */
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 */
2946 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2947 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
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)
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)
2962 +#define EXT3_IOC_SNAP_SETFILECOW _IOW('v', 10, long)
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)
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 */
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 */
2991 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2992 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2994 +#define EXT3_FEATURE_COMPAT_SNAPFS 0x0010
2995 +#define EXT3_FEATURE_COMPAT_BLOCKCOW 0x0020
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
3005 wait_queue_head_t s_delete_thread_queue;
3006 wait_queue_head_t s_delete_waiter_queue;
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 */
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
3024 #define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
3026 +/*snapshot transaction blocks*/
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)
3054 ext3_mark_iloc_dirty(handle_t *handle,
3055 struct inode *inode,
3058 fs/ext3/Makefile | 2
3059 fs/ext3/ext3-exports.c | 2
3060 fs/ext3/ialloc.c | 7
3062 fs/ext3/snap.c | 2578 +++++++++++++++++++++++++++++++++++++++++++++
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(-)