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 00:07:10.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 */
829 + memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
830 + sizeof(EXT3_I(src)->i_data));
831 + memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
833 + ext3_discard_prealloc(src);
835 + dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
836 + src->i_size = EXT3_I(src)->i_disksize = 0;
838 + dst->i_blocks = src->i_blocks;
840 + /* Check EA blocks here to modify i_blocks correctly */
841 + if(ext3_has_ea (src)) {
842 + src->i_blocks += bpib;
843 + if( ! ext3_has_ea (dst) )
844 + if( dst->i_blocks >= bpib )
845 + dst->i_blocks -= bpib;
847 + if( ext3_has_ea (dst))
848 + dst->i_blocks += bpib;
851 + snap_debug("migrate data from ino %lu to ino %lu\n",
852 + src->i_ino, dst->i_ino);
853 + ext3_mark_inode_dirty(handle, src);
854 + ext3_mark_inode_dirty(handle, dst);
857 + return SNAP_ERROR(err);
861 + * ext3_get_indirect - get a specific indirect inode from a primary inode
862 + * @primary: primary (direct) inode
863 + * @table: table of @slot + 1 indices in reverse chronological order
864 + * @slot: starting slot number to check for indirect inode number
866 + * We locate an indirect inode from a primary inode using the redirection
867 + * table stored in the primary inode. Because the desired inode may actually
868 + * be in a "newer" slot number than the supplied slot, we are given a table
869 + * of indices in chronological order to search for the correct inode number.
870 + * We walk table from @slot to 0 looking for a non-zero inode to load.
872 + * To only load a specific index (and fail if it does not exist), you can
873 + * pass @table = NULL, and the index number in @slot. If @slot == 0, the
874 + * primary inode data is returned.
876 + * We return a pointer to an inode, or an error. If the indirect inode for
877 + * the given index does not exist, NULL is returned.
879 +static struct inode *ext3_get_indirect(struct inode *primary, int *table,
882 + char buf[EXT3_MAX_SNAP_DATA];
883 + struct snap_ea *snaps;
885 + struct inode *inode = NULL;
886 + int err = 0, index = 0;
888 + if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
891 + snap_debug("ino %lu, table %p, slot %d\n", primary->i_ino, table,slot);
893 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
894 + buf, EXT3_MAX_SNAP_DATA);
895 + if (err == -ENODATA) {
897 + } else if (err < 0) {
898 + snap_debug(" attribute read error\n");
901 + snaps = (struct snap_ea *)buf;
903 + /* if table is NULL and there is a slot */
904 + if( !table && slot ) {
906 + ino = le32_to_cpu ( snaps->ino[index] );
907 + if(ino) inode = iget(primary->i_sb, ino);
910 + /* if table is not NULL */
911 + while ( !inode && slot > 0) {
912 + index = table[slot];
913 + ino = le32_to_cpu ( snaps->ino[index] );
915 + snap_debug("snap inode at slot %d is %lu\n", slot, ino);
920 + inode = iget(primary->i_sb, ino);
923 + if( slot == 0 && table ) {
924 + snap_debug("redirector not found, using primary\n");
925 + inode = iget(primary->i_sb, primary->i_ino);
931 +/* get the indirect ino at index of the primary inode
932 + * return value: postive: indirect ino number
933 + * negative or 0: error
935 +static ino_t ext3_get_indirect_ino(struct inode *primary, int index)
937 + char buf[EXT3_MAX_SNAP_DATA];
938 + struct snap_ea *snaps;
942 + if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
945 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
946 + buf, EXT3_MAX_SNAP_DATA);
947 + if (err == -ENOATTR) {
950 + } else if (err < 0) {
951 + snap_err(EXT3_SNAP_ATTR " attribute read error\n");
956 + snaps = (struct snap_ea *)buf;
957 + ino = le32_to_cpu (snaps->ino[index]);
958 + snap_debug("snap ino for %ld at index %d is %lu\n",
959 + primary->i_ino, index, ino);
963 +/* ext3_copy_block - copy one data block from inode @src to @dst.
964 + No lock here. User should do the lock.
965 + User should check the return value to see if the result is correct.
967 + 1: The block has been copied successfully
968 + 0: No block is copied, usually this is because src has no such blk
972 +static int ext3_copy_block (struct inode *dst, struct inode *src, int blk)
974 + struct buffer_head *bh_dst = NULL, *bh_src = NULL;
976 + handle_t *handle = NULL;
979 + snap_debug("copy blk %d from %lu to %lu \n", blk, src->i_ino, dst->i_ino);
981 + * ext3_getblk() require handle!=NULL
983 + if (S_ISREG(src->i_mode))
986 + handle = ext3_journal_start(dst, SNAP_COPYBLOCK_TRANS_BLOCKS);
990 + bh_src = ext3_bread(handle, src, blk, 0, &err);
992 + snap_err("error for src blk %d, error %d\n", blk, err);
995 + bh_dst = ext3_getblk(handle, dst, blk, 1, &err);
997 + snap_err("error for dst blk %d, error %d\n", blk, err);
1001 + snap_debug("copy block %lu to %lu (%ld bytes)\n",
1002 + bh_src->b_blocknr, bh_dst->b_blocknr,
1003 + src->i_sb->s_blocksize);
1005 + ext3_journal_get_write_access(handle, bh_dst);
1007 + memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
1009 + ext3_journal_dirty_metadata(handle, bh_dst);
1012 + if (bh_src) brelse(bh_src);
1013 + if (bh_dst) brelse(bh_dst);
1015 + ext3_journal_stop(handle, dst);
1019 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1021 + * add one inode to superblock's snap_orphan chain
1022 + * only add on-disk data for simplicity
1024 +static void add_snap_orphan(handle_t *handle, struct inode *pri, struct inode *ind)
1026 + struct ext3_sb_info *sb = &pri->i_sb->u.ext3_sb;
1027 + struct ext3_iloc iloc;
1029 + if( ext3_get_inode_loc(ind, &iloc) ){
1030 + snap_debug("--- get ind loc fail\n");
1035 + snap_debug("add new ind inode %lu into orphan list,"
1036 + " primary %lu, last orphan %u\n",
1037 + ind->i_ino, pri->i_ino,
1038 + sb->s_es->s_last_snap_orphan);
1039 + lock_super(pri->i_sb);
1040 + iloc.raw_inode->i_next_snap_orphan = sb->s_es->s_last_snap_orphan;
1041 + iloc.raw_inode->i_snap_primary = pri->i_ino;
1042 + ext3_mark_inode_dirty(handle, ind);
1044 + ext3_journal_get_write_access(handle, sb->s_sbh);
1045 + sb->s_es->s_last_snap_orphan = ind->i_ino;
1046 + pri->i_sb->s_dirt = 1;
1047 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1048 + unlock_super(pri->i_sb);
1053 + * counterpart of add_snap_orphan
1055 +static void remove_snap_orphan(handle_t *handle, struct inode *ind)
1057 + struct ext3_sb_info *sb = &ind->i_sb->u.ext3_sb;
1058 + struct inode *pre = NULL, *inode = NULL;
1059 + struct ext3_iloc iloc, pre_iloc;
1062 + lock_super(ind->i_sb);
1063 + for(ino = sb->s_es->s_last_snap_orphan; ino; ){
1064 + snap_debug("found an orphan, ino=%lu\n", ino);
1065 + inode = iget( ind->i_sb, ino );
1067 + snap_debug("iget %lu fail\n", ino);
1070 + if( ext3_get_inode_loc(inode, &iloc) ){
1071 + snap_debug("get_inode_loc %lu fail\n", ino);
1074 + if( ino == ind->i_ino ){
1076 + snap_debug("found at head of orphan chain\n");
1077 + ext3_journal_get_write_access(handle, sb->s_sbh);
1078 + sb->s_es->s_last_snap_orphan =
1079 + iloc.raw_inode->i_next_snap_orphan;
1080 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1081 + snap_debug("set new last orphan: %u\n",
1082 + sb->s_es->s_last_snap_orphan);
1086 + snap_debug("found in middle of orphan chain\n");
1087 + if( ext3_get_inode_loc(pre, &pre_iloc) ){
1088 + snap_err("get pre_inode loc %lu fail\n", pre->i_ino);
1091 + pre_iloc.raw_inode->i_next_snap_orphan =
1092 + iloc.raw_inode->i_next_snap_orphan;
1093 + ext3_mark_inode_dirty(handle, pre);
1094 + brelse(pre_iloc.bh);
1100 + ino = iloc.raw_inode->i_next_snap_orphan;
1105 + unlock_super(ind->i_sb);
1110 + * FIXME: how about crashs again during recovery?
1112 +void snap_orphan_cleanup(struct super_block *sb)
1114 + ino_t ind_ino, pri_ino;
1115 + struct inode *ind = NULL, *pri = NULL;
1116 + struct ext3_iloc ind_iloc;
1118 + if( (ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan) == 0 ){
1119 + snap_debug("snap_orphan_cleanup: nothing to do\n");
1123 + snap_debug("------ begin cleanup snap orphans ------\n");
1125 + ind = iget( sb, ind_ino );
1127 + snap_err("snap_orphan_cleanup: get "
1128 + "ind %lu fail\n", ind_ino);
1132 + if( ext3_get_inode_loc(ind, &ind_iloc) ){
1133 + snap_err("snap_orphan_cleanup: get "
1134 + "iloc %lu fail\n", ind_ino);
1139 + ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan =
1140 + ind_iloc.raw_inode->i_next_snap_orphan;
1141 + pri_ino = ind_iloc.raw_inode->i_snap_primary;
1143 + pri = iget( sb, pri_ino );
1145 + snap_err("snap_orphan_cleanup: get primary "
1146 + "%lu fail\n", pri_ino);
1149 + restore_snap_inode(pri, ind);
1150 + }while( ind_ino );
1151 + snap_debug("------ end cleanup snap orphans ------\n");
1153 + sb->u.ext3_sb.s_es->s_last_snap_orphan = 0;
1158 + * reserse operation of set_indirect()
1159 + * we should determine whether we had put pri into primary inode chain,
1160 + * if not, don't touch it
1162 +static void unset_indirect(handle_t *handle, struct inode *pri, struct inode *ind)
1164 + char buf[EXT3_MAX_SNAP_DATA];
1165 + struct snap_ea *snaps;
1166 + int err, alone=1, index, found;
1168 + snap_debug("pri %lu, ind %lu\n", pri->i_ino, ind->i_ino);
1169 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
1170 + EXT3_MAX_SNAP_DATA);
1172 + if( err == -ENOATTR ){
1173 + snap_debug("primary inode has not EA\n");
1176 + snap_debug("get EA error on primary inode,"
1177 + "returned value %d\n", err);
1182 + /* find ind's item in the ea */
1183 + snaps = (struct snap_ea*)buf;
1184 + for(index=EXT3_MAX_SNAPS-1, found=-1; index>=0; index--) {
1185 + if( snaps->ino[index] == ind->i_ino )
1187 + else if( snaps->ino[index] )
1192 + snap_debug("remove from primary inode's EA\n");
1193 + snaps->ino[found] = 0;
1194 + snaps->parent_ino[found] = 0;
1195 + ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1196 + buf, EXT3_MAX_SNAP_DATA, 0);
1198 + snap_debug("delete from primary inodes chain\n");
1199 + lock_list(pri->i_sb);
1200 + delete_cowed_ino_from_list(handle, pri);
1201 + unlock_list(pri->i_sb);
1204 + snap_debug("didn't found ind in pri's EA, do nothing\n");
1213 + * restore all data in @ind to @pri after free data blocks of @pri.
1214 + * then release @ind
1216 +static void restore_snap_inode(struct inode *pri, struct inode *ind)
1219 + struct inode *tmp;
1221 + snap_debug("restore from indirect %lu to primary %lu\n",
1222 + ind->i_ino, pri->i_ino);
1224 + handle = ext3_journal_start(pri, SNAP_RESTOREORPHAN_TRANS_BLOCKS);
1228 + /* first: taken from pri's ea, or from fs-wide primary inode chain */
1229 + unset_indirect(handle, pri, ind);
1231 + /* second: throw out half-copied data in pri */
1232 + if( pri->i_blocks ){
1233 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1235 + snap_debug("ext3_new_inode error\n");
1239 + ext3_migrate_data(handle, tmp, pri);
1240 + snap_debug("freeing half-copied %lu blocks\n", tmp->i_blocks );
1245 + /* third: restore ind inode to pri inode */
1246 + snap_debug("restore %lu blocks to primary inode %lu\n",
1247 + ind->i_blocks, pri->i_ino);
1248 + ext3_migrate_data(handle, pri, ind);
1250 + /* final: delete ind inode */
1256 + ext3_journal_stop(handle, pri);
1259 +static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
1260 + struct inode *src, int *has_orphan)
1262 + unsigned long blocks, blk, cur_blks;
1263 + int low_credits, save_ref;
1265 + blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
1266 + src->i_sb->s_blocksize_bits;
1267 + low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
1269 + snap_debug("%lu blocks need to be copied,"
1270 + "low credits limit %d\n", blocks, low_credits);
1271 + for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
1272 + if (!ext3_bmap(src->i_mapping, blk))
1274 + if(handle->h_buffer_credits <= low_credits) {
1275 + int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
1276 + if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
1277 + needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
1278 + if (journal_extend(handle, needed)) {
1279 + snap_debug("create_indirect:fail to extend "
1280 + "journal, restart trans\n");
1282 + if(!*has_orphan) {
1283 + snap_debug("add orphan ino %lu nlink %d to orphan list \n",
1284 + dst->i_ino, dst->i_nlink);
1285 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1286 + add_snap_orphan(handle, dst, src);
1288 + ext3_orphan_add(handle, dst);
1292 + dst->u.ext3_i.i_disksize =
1293 + blk * dst->i_sb->s_blocksize;
1294 + dst->i_blocks = cur_blks;
1295 + dst->i_mtime = CURRENT_TIME;
1296 + ext3_mark_inode_dirty(handle, dst);
1299 + * We can be sure the last handle was stoped
1300 + * ONLY if the handle's reference count is 1
1302 + save_ref = handle->h_ref;
1303 + handle->h_ref = 1;
1304 + if( ext3_journal_stop(handle, dst) ){
1305 + snap_err("fail to stop journal\n");
1310 + handle = ext3_journal_start(dst,
1311 + low_credits + needed);
1313 + snap_err("fail to restart handle\n");
1316 + handle->h_ref = save_ref;
1319 + if (ext3_copy_block( dst, src, blk) < 0 )
1321 + cur_blks += dst->i_sb->s_blocksize / 512;
1323 + dst->i_size = dst->u.ext3_i.i_disksize = src->i_size;
1328 +static int ext3_set_generation(struct inode *inode, unsigned long gen)
1333 + handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
1335 + err = ext3_xattr_set(handle, inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1336 + (char*)&gen, sizeof(int), 0);
1338 + snap_err("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
1342 + ext3_journal_stop(handle, inode);
1346 +static int ext3_get_generation(struct inode *inode)
1350 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1351 + (char*)&gen, sizeof(gen));
1353 + if (err == -ENODATA) {
1356 + snap_err("can not get generation from %lu \n", inode->i_ino);
1363 + * ext3_create_indirect - copy data, attributes from primary to new indir inode
1364 + * @pri: primary (source) inode
1365 + * @index: index in snapshot table where indirect inode should be stored
1366 + * @delete: flag that the primary inode is being deleted
1368 + * We copy all of the data blocks from the @*src inode to the @*dst inode, as
1369 + * well as copying the attributes from @*src to @*dst. If @delete == 1, then
1370 + * the primary inode will only be a redirector and will appear deleted.
1372 + * FIXME do we move EAs, only non-snap EAs, what?
1373 + * FIXME we could do readpage/writepage, but we would have to handle block
1374 + * allocation then, and it ruins sparse files for 1k/2k filesystems,
1375 + * at the expense of doing a memcpy.
1378 +static struct inode *ext3_create_indirect(
1379 + struct inode *pri,
1385 + struct inode *ind;
1386 + handle_t *handle = NULL;
1388 + int has_orphan = 0;
1390 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1391 + printk( KERN_EMERG "TRY TO COW JOUNRAL\n");
1394 + snap_debug("creating indirect inode for %lu at index %d, %s pri\n",
1395 + pri->i_ino, index, del ? "deleting" : "preserve");
1397 + ind = ext3_get_indirect(pri, NULL, index);
1401 + handle = ext3_journal_start(pri, SNAP_CREATEIND_TRANS_BLOCKS);
1404 + /* XXX ? We should pass an err argument to get_indirect and precisely
1405 + * detect the errors, for some errors, we should exit right away.
1408 + /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect,
1409 + * we just free the primary data blocks and mark this inode delete
1411 + if((del) && ind && !IS_ERR(ind)) {
1412 + struct inode *tmp;
1413 + /* for directory, we don't free the data blocks,
1414 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1416 + snap_debug("del==SNAP_DEL_PRI_WITH_IND && ind\n");
1417 + if(!S_ISDIR(pri->i_mode)) {
1418 + /*Here delete the data of that pri inode.
1419 + * FIXME later, should throw the blocks of
1420 + * primary inode directly
1422 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1424 + down(&tmp->i_sem);
1425 + ext3_migrate_data(handle, tmp, pri);
1431 + snap_err("ext3_new_inode error\n");
1436 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1437 + ext3_mark_inode_dirty(handle, pri);
1442 + if (ind && !IS_ERR(ind)) {
1443 + snap_debug("existing indirect ino %lu for %lu: index %d\n",
1444 + ind->i_ino, pri->i_ino, index);
1448 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
1449 + ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1455 + snap_debug("got new inode %lu\n", ind->i_ino);
1456 + ind->i_rdev = pri->i_rdev;
1457 + ind->i_op = pri->i_op;
1458 + ext3_set_generation(ind, (unsigned long)gen);
1459 + /* If we are deleting the primary inode, we want to ensure that it is
1460 + * written to disk with a non-zero link count, otherwise the next iget
1461 + * and iput will mark the inode as free (which we don't want, we want
1462 + * it to stay a redirector). We fix this in ext3_destroy_indirect()
1463 + * when the last indirect inode is removed.
1465 + * We then do what ext3_delete_inode() does so that the metadata will
1466 + * appear the same as a deleted inode, and we can detect it later.
1469 + snap_debug("deleting primary inode\n");
1471 + down(&ind->i_sem);
1472 + err = ext3_migrate_data(handle, ind, pri);
1476 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1480 + /* XXX for directory, we copy the block back
1481 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1483 + if( S_ISDIR(pri->i_mode)) {
1484 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1491 + pri->u.ext3_i.i_flags |= EXT3_DEL_FL;
1492 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1493 + if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
1494 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1495 + //pri->u.ext3_i.i_generation++;
1496 + ext3_mark_inode_dirty(handle, pri);
1497 + ext3_mark_inode_dirty(handle, ind);
1500 + down(&ind->i_sem);
1501 + err = ext3_migrate_data(handle, ind, pri);
1505 + /* for regular files we do blocklevel COW's maybe */
1506 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
1507 + && S_ISREG(pri->i_mode)) {
1509 + snap_debug("ino %lu, do block cow\n",pri->i_ino);
1510 + /* because after migrate_data , pri->i_size is 0 */
1511 + pri->i_size = ind->i_size;
1514 + int bpib = pri->i_sb->s_blocksize >> 9;
1515 + snap_debug("ino %lu, do file cow\n", pri->i_ino);
1517 + /* XXX: can we do this better?
1518 + * If it's a fast symlink, we should copy i_data back!
1519 + * The criteria to determine a fast symlink is:
1520 + * 1) it's a link and its i_blocks is 0
1521 + * 2) it's a link and its i_blocks is bpib ( the case
1522 + * it has been cowed and has ea )
1524 + if( S_ISLNK(ind->i_mode) &&
1525 + ((ind->i_blocks == 0) || (ext3_has_ea(ind) && ind->i_blocks == bpib))) {
1526 + snap_debug("ino %lu is fast symlink\n", pri->i_ino);
1527 + memcpy(EXT3_I(pri)->i_data, EXT3_I(ind)->i_data,
1528 + sizeof(EXT3_I(ind)->i_data));
1529 + pri->i_size = ind->i_size;
1532 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1537 + /* set cow flag for ind */
1538 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1539 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
1541 + ext3_mark_inode_dirty(handle, pri);
1542 + ext3_mark_inode_dirty(handle, ind);
1544 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1551 + if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
1552 + EXT3_FEATURE_COMPAT_SNAPFS)) {
1553 + lock_super(pri->i_sb);
1554 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
1555 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat |=
1556 + cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
1557 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
1558 + pri->i_sb->s_dirt = 1;
1559 + unlock_super(pri->i_sb);
1562 + snap_debug("del %lu nlink %d from orphan list\n",
1563 + ind->i_ino, ind->i_nlink);
1564 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1565 + remove_snap_orphan(handle, ind);
1567 + ext3_orphan_del(handle, ind);
1570 + ext3_journal_stop(handle, pri);
1581 + snap_debug("del %lu nlink %d from orphan list\n",
1582 + ind->i_ino, ind->i_nlink);
1583 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1584 + remove_snap_orphan(handle, ind);
1586 + ext3_orphan_del(handle, ind);
1590 + ext3_journal_stop(handle, pri);
1592 + snap_err("exiting with error %d\n", err);
1597 +/* The following functions are used by destroy_indirect */
1598 +#define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
1599 +#define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
1601 +static inline int block_bmap (struct buffer_head * bh, int nr)
1607 + tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
1612 +static inline int block_setbmap (handle_t *handle, struct buffer_head * bh, int nr, int physical)
1617 + ext3_journal_get_write_access(handle, bh);
1618 + ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
1619 + ext3_journal_dirty_metadata(handle, bh);
1624 +static int ext3_migrate_block (handle_t *handle, struct inode * dst, struct inode *src, int block)
1626 + int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
1627 + int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
1628 + int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
1629 + unsigned long blksz = src->i_sb->s_blocksize;
1630 + kdev_t ddev = dst->i_dev;
1631 + kdev_t sdev = src->i_dev;
1635 + ext3_warning (src->i_sb, "ext3_migrate_block", "block < 0");
1638 + if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
1639 + (1 << (addr_per_block_bits * 2)) +
1640 + ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
1641 + ext3_warning (src->i_sb, "ext3_migrate_block", "block > big");
1644 + /* EXT3_NDIR_BLOCK */
1645 + if (block < EXT3_NDIR_BLOCKS) {
1646 + if( inode_bmap(dst, block) ) return 0;
1648 + if( (physical = inode_bmap(src, block)) ) {
1649 + inode_setbmap (dst, block, physical);
1650 + inode_setbmap (src, block, 0);
1657 + /* EXT3_IND_BLOCK */
1658 + block -= EXT3_NDIR_BLOCKS;
1659 + if (block < addr_per_block) {
1660 + i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1663 + physical = inode_bmap(src, EXT3_IND_BLOCK);
1665 + inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1666 + inode_setbmap (src, EXT3_IND_BLOCK, 0);
1672 + if( block_bmap (bread (ddev, i1_d, blksz), block ))
1675 + i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1676 + if( !i1_s) return 0;
1678 + physical = block_bmap ( bread (sdev, i1_s, blksz), block );
1681 + block_setbmap(handle, bread(ddev, i1_d, blksz),block,physical);
1682 + block_setbmap(handle, bread(sdev, i1_s, blksz), block, 0);
1688 + /* EXT3_DIND_BLOCK */
1689 + block -= addr_per_block;
1690 + if (block < (1 << (addr_per_block_bits * 2))) {
1691 + i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1692 + i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1694 + if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1695 + inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1696 + inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1702 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1703 + block >> addr_per_block_bits);
1707 + if( !i1_s) return 0;
1709 + physical = block_bmap (bread (sdev, i1_s, blksz),
1710 + block >> addr_per_block_bits);
1712 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1713 + block >> addr_per_block_bits, physical);
1714 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1715 + block >> addr_per_block_bits, 0);
1721 + physical = block_bmap (bread (ddev, i2_d,
1723 + block & (addr_per_block - 1));
1727 + i2_s = block_bmap (bread (sdev, i1_s,
1729 + block >> addr_per_block_bits);
1730 + if(!i2_s) return 0;
1732 + physical = block_bmap(bread (sdev, i2_s,
1734 + block & (addr_per_block - 1));
1736 + block_setbmap(handle, bread (ddev, i2_d, blksz),
1737 + block & (addr_per_block - 1), physical);
1738 + block_setbmap(handle, bread (sdev, i2_s, blksz),
1739 + block & (addr_per_block - 1), 0);
1747 + /* EXT3_TIND_BLOCK */
1748 + block -= (1 << (addr_per_block_bits * 2));
1749 + i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1750 + i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1752 + if( (physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1753 + inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1757 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1758 + block >> (addr_per_block_bits * 2));
1760 + if(i1_s) i2_s = block_bmap (bread (sdev, i1_s, blksz),
1761 + block >> (addr_per_block_bits * 2));
1765 + if( !i1_s) return 0;
1767 + physical = block_bmap (bread (sdev, i1_s, blksz),
1768 + block >> (addr_per_block_bits * 2));
1770 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1771 + block >> (addr_per_block_bits * 2), physical);
1772 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1773 + block >> (addr_per_block_bits * 2), 0);
1779 + i3_d = block_bmap (bread (ddev, i2_d, blksz),
1780 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1781 + if( i2_s) i3_s = block_bmap (bread (sdev, i2_s, blksz),
1782 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1785 + if (!i2_s) return 0;
1786 + physical = block_bmap (bread (sdev, i2_s, blksz),
1787 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1789 + block_setbmap (handle, bread (ddev, i2_d, blksz),
1790 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1792 + block_setbmap (handle, bread (sdev, i2_s, blksz),
1793 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1800 + physical = block_bmap (bread (ddev, i3_d, blksz),
1801 + block & (addr_per_block - 1)) ;
1802 + if(physical) return 0;
1804 + if(!i3_s) return 0;
1805 + physical = block_bmap (bread (sdev, i3_s, blksz),
1806 + block & (addr_per_block - 1)) ;
1808 + block_setbmap (handle, bread (ddev, i3_d, blksz),
1809 + block & (addr_per_block - 1), physical);
1810 + block_setbmap (handle, bread (sdev, i3_s, blksz),
1811 + block & (addr_per_block - 1), 0);
1819 +/* Generate i_blocks from blocks for an inode .
1820 + * We also calculate EA block here.
1822 +static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1824 + /* 512 byte disk blocks per inode block */
1825 + int bpib = inode->i_sb->s_blocksize >> 9;
1826 + int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1827 + unsigned long i_blocks = 0;
1830 + int meta_blocks = 0;
1832 + if( !inode ) return 0;
1834 + if( blocks < 0 ) {
1835 + /* re-calculate blocks here */
1836 + blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1837 + >> inode->i_sb->s_blocksize_bits;
1840 + /* calculate data blocks */
1841 + for(i = 0; i < blocks; i++ ) {
1842 + if(ext3_bmap(inode->i_mapping, i))
1845 + /* calculate meta blocks */
1846 + blocks -= EXT3_NDIR_BLOCKS;
1847 + if( blocks > 0 ) {
1849 + blocks -= addr_per_block;
1851 + if( blocks > 0 ) meta_blocks++;
1853 + while( (blocks > 0) && (i < addr_per_block) ) {
1855 + blocks -= addr_per_block;
1858 + if ( blocks > 0 ) meta_blocks += 2;
1861 + while( blocks > 0) {
1863 + blocks -= addr_per_block;
1865 + if(i >= addr_per_block ) {
1869 + if( j >= addr_per_block) {
1874 + /* calculate EA blocks */
1875 + if( ext3_has_ea (inode) ) meta_blocks++;
1877 + i_blocks += meta_blocks * bpib;
1878 + snap_debug("ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1883 + * ext3_destroy_indirect - delete an indirect inode from the table
1884 + * @pri: primary inode
1885 + * @ind: indirect inode
1886 + * @index: index of inode that should be deleted
1888 + * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1889 + * is NULL, we use the inode at @index.
1891 +static int ext3_destroy_indirect(struct inode *pri, int index,
1892 + struct inode *next_ind)
1894 + char buf[EXT3_MAX_SNAP_DATA];
1895 + struct snap_ea *snaps;
1896 + struct inode *ind;
1900 + handle_t *handle=NULL;
1903 + if (index < 0 || index > EXT3_MAX_SNAPS)
1906 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1907 + snap_err("TRY TO DESTROY JOURNAL'S IND\n");
1911 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1912 + buf, EXT3_MAX_SNAP_DATA);
1914 + snap_err("inode %lu attribute read error\n", pri->i_ino);
1918 + snaps = (struct snap_ea *)buf;
1919 + if ( !snaps->ino[index] ) {
1920 + snap_err("for pri ino %lu, index %d, redirect ino is 0\n",
1921 + pri->i_ino, index);
1925 + snap_debug("for pri ino %lu, reading inode %lu at index %d\n",
1926 + pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1928 + ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1930 + if ( !ind || IS_ERR(ind) || is_bad_inode(ind))
1933 + snap_debug("iget ind %lu, ref count = %d\n",
1934 + ind->i_ino, atomic_read(&ind->i_count));
1936 + handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
1941 + /* if it's block level cow, first copy the blocks back */
1942 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1943 + S_ISREG(pri->i_mode)) {
1948 + down(&ind->i_sem);
1950 + double_down(&next_ind->i_sem, &ind->i_sem);
1952 + blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1)
1953 + >> next_ind->i_sb->s_blocksize_bits;
1954 +#define FAST_MIGRATE_BLOCK
1955 +#ifdef FAST_MIGRATE_BLOCK
1956 + snap_debug("migrate block back from ino %lu to %lu\n",
1957 + ind->i_ino, next_ind->i_ino);
1959 + for(i = 0; i < blocks; i++) {
1960 + if( ext3_bmap(next_ind->i_mapping, i) )
1962 + if( !ext3_bmap(ind->i_mapping, i) )
1964 + ext3_migrate_block(handle, next_ind, ind, i) ;
1966 + /* Now re-compute the i_blocks */
1967 + /* XXX shall we take care of ind here? probably not */
1968 + next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1969 + ext3_mark_inode_dirty(handle, next_ind);
1972 + for (i = 0; i < blocks; i++) {
1973 + if (ext3_bmap(next_ind->i_mapping, i))
1975 + if (ext3_copy_block(next_ind, ind, i ) < 0)
1978 + ext3_mark_inode_dirty(handle, next_ind);
1980 + if (next_ind == pri)
1983 + double_up(&next_ind->i_sem, &ind->i_sem);
1987 + snap_debug("delete indirect ino %lu\n", ind->i_ino);
1988 + snap_debug("iput ind %lu, ref count = %d\n",
1989 + ind->i_ino, atomic_read(&ind->i_count));
1993 + snaps->ino[index] = cpu_to_le32(0);
1994 + for (i = 0; i < EXT3_MAX_SNAPS; i++)
1995 + save += snaps->ino[i];
1998 + lock_list(pri->i_sb);
1999 + delete_cowed_ino_from_list(handle, pri);
2000 + unlock_list(pri->i_sb);
2003 + /* if there are no cowed inode left, then remove snapfs feature */
2004 + if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2006 + lock_super(pri->i_sb);
2008 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
2009 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2010 + EXT3_FEATURE_COMPAT_SNAPFS)) {
2011 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2012 + cpu_to_le32(~EXT3_FEATURE_COMPAT_SNAPFS);
2014 + /* clean up block level cow feature */
2015 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2016 + EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2017 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2018 + cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2020 + /* XXX clean the extended attribute feature,
2021 + * this is not safe, find a better way
2023 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2024 + EXT3_FEATURE_COMPAT_EXT_ATTR)) {
2025 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2026 + cpu_to_le32(~EXT3_FEATURE_COMPAT_EXT_ATTR);
2029 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
2030 + pri->i_sb->s_dirt = 1;
2031 + unlock_super(pri->i_sb);
2035 + * If we are deleting the last indirect inode, and the primary inode
2036 + * has already been deleted, then mark the primary for deletion also.
2037 + * Otherwise, if we are deleting the last indirect inode remove the
2038 + * snaptable from the inode. XXX
2040 + if (!save && pri->u.ext3_i.i_dtime) {
2041 + snap_debug("deleting primary %lu\n", pri->i_ino);
2043 + /* reset err to 0 now */
2046 + snap_debug("%s redirector table\n",
2047 + save ? "saving" : "deleting");
2048 + /* XXX: since set ea will modify i_ctime of pri,
2049 + so save/restore i_ctime. Need this necessary ? */
2050 + ctime = pri->i_ctime;
2051 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2052 + save ? buf : NULL, EXT3_MAX_SNAP_DATA, 0);
2053 + pri->i_ctime = ctime;
2054 + ext3_mark_inode_dirty(handle, pri);
2056 + ext3_journal_stop(handle, pri);
2060 +/* restore a primary inode with the indirect inode at index */
2061 +static int ext3_restore_indirect(struct inode *pri, int index)
2063 + struct inode *ind;
2064 + struct inode *tmp;
2066 + handle_t *handle = NULL;
2068 + if (index < 0 || index > EXT3_MAX_SNAPS)
2071 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2072 + printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2075 + snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2077 + ind = ext3_get_indirect(pri, NULL, index);
2082 + snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2084 + handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
2087 + /* first destroy all the data blocks in primary inode */
2088 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
2089 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
2091 + double_down(&pri->i_sem, &tmp->i_sem);
2092 + ext3_migrate_data(handle, tmp, pri);
2093 + double_up(&pri->i_sem, &tmp->i_sem);
2099 + snap_err("restore_indirect, new_inode err\n");
2101 + double_down(&pri->i_sem, &ind->i_sem);
2102 + ext3_migrate_data(handle, pri, ind);
2103 + /* clear the cow flag for pri because ind has it */
2104 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
2105 + ext3_mark_inode_dirty(handle, pri);
2106 + double_up(&pri->i_sem, &ind->i_sem);
2109 +// ext3_destroy_indirect(pri, index);
2111 + ext3_journal_stop(handle, pri);
2117 + * ext3_snap_iterate - iterate through all of the inodes
2118 + * @sb: filesystem superblock
2119 + * @repeat: pointer to function called on each valid inode
2120 + * @start: inode to start iterating at
2121 + * @priv: private data to the caller/repeat function
2123 + * If @start is NULL, then we do not return an inode pointer. If @*start is
2124 + * NULL, then we start at the beginning of the filesystem, and iterate over
2125 + * all of the inodes in the system. If @*start is non-NULL, then we start
2126 + * iterating at this inode.
2128 + * We call the repeat function for each inode that is in use. The repeat
2129 + * function must check if this is a redirector (with is_redirector) if it
2130 + * only wants to operate on redirector inodes. If there is an error or
2131 + * the repeat function returns non-zero, we return the last inode operated
2132 + * on in the @*start parameter. This allows the caller to restart the
2133 + * iteration at this inode if desired, by returning a positive value.
2134 + * Negative return values indicate an error.
2136 + * NOTE we cannot simply traverse the existing filesystem tree from the root
2137 + * inode, as there may be disconnected trees from deleted files/dirs
2139 + * FIXME If there was a list of inodes with EAs, we could simply walk the list
2140 + * intead of reading every inode. This is an internal implementation issue.
2143 +static int ext3_iterate_all(struct super_block *sb,
2144 + int (*repeat)(struct inode *inode, void *priv),
2145 + struct inode **start, void *priv)
2147 + struct inode *tmp = NULL;
2149 + ino_t istart, ibase;
2155 + *start = iget(sb, EXT3_ROOT_INO);
2160 + if (is_bad_inode(*start)) {
2165 + if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
2166 + snap_debug("invalid starting inode %ld\n",(*start)->i_ino);
2170 + if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2171 + if ((err = (*repeat)(*start, priv) != 0))
2174 + *start = iget(sb, EXT3_FIRST_INO(sb));
2179 + if (is_bad_inode(*start)) {
2185 + gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
2186 + istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
2187 + ibase = gstart * EXT3_INODES_PER_GROUP(sb);
2188 + for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
2189 + gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
2190 + struct ext3_group_desc * gdp;
2195 + gdp = ext3_get_group_desc (sb, gnum, NULL);
2196 + if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
2197 + EXT3_INODES_PER_GROUP(sb))
2200 + bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2201 + if (bitmap_nr < 0)
2204 + bitmap = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr]->b_data;
2205 + for (ibyte = istart >> 3;
2206 + ibyte < EXT3_INODES_PER_GROUP(sb) >> 3;
2212 + if (!bitmap[ibyte])
2215 + /* FIXME need to verify if bit endianness will
2216 + * work properly here for all architectures.
2218 + for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2219 + ino_t ino = ibase + (ibyte << 3) + i;
2221 + if ((bitmap[ibyte] & bit) == 0)
2224 + if (ino < (*start)->i_ino)
2227 + *start = iget(sb, ino);
2232 + if (is_bad_inode(*start)) {
2237 + if ((err = (*repeat)(*start, priv)) != 0)
2250 +static int ext3_iterate(struct super_block *sb,
2251 + int (*repeat)(struct inode *inode, void *priv),
2252 + struct inode **start, void *priv, int flag)
2255 + case SNAP_ITERATE_ALL_INODE:
2256 + return ext3_iterate_all (sb, repeat, start, priv);
2258 + case SNAP_ITERATE_COWED_INODE:
2259 + return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2266 +static int find_snap_meta_index(
2267 + struct table_snap_meta_data *snap_meta,
2272 + /* table max length is null*/
2273 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2274 + /*compare name Max name Length 15*/
2275 + if (snap_meta->array[i].name[0]){
2276 + if(!strncmp(snap_meta->array[i].name, name, strlen(name)))
2280 + return -1; /* can not find */
2283 +int set_snap_meta_index(
2284 + struct table_snap_meta_data *snap_meta,
2290 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2291 + /*compare name Max name Length 15*/
2292 + if (! snap_meta->array[i].name[0]){
2293 + strcpy(snap_meta->array[i].name, name);
2294 + snap_meta->count ++;
2295 + snap_meta->array[i].start = i * TABLE_ITEM_SIZE + 1;
2296 + snap_meta->array[i].len = size;
2300 + return -1; /* can not find */
2303 +static int ext3_get_meta_attr(struct super_block *sb,
2304 + char* name, char* buf,
2308 + struct inode *inode;
2309 + struct buffer_head *bh = NULL;
2310 + struct table_snap_meta_data *s_attr;
2311 + unsigned long map_len = 0, left_size;
2312 + int i, error = 0, index = 0;
2314 + ino = SB_SNAPTABLE_INO(sb);
2316 + snap_err("No table file \n");
2319 + inode = iget(sb, ino);
2320 + if(!inode || is_bad_inode(inode)){
2321 + snap_err("unable to get table ino %lu\n", ino);
2325 + /*read the table from the table inode*/
2326 + bh = ext3_bread(NULL, inode, 0, 0, &error);
2328 + snap_err("read table ino %lu, error %d\n", ino, error);
2332 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2333 + index = find_snap_meta_index(s_attr, name);
2335 + snap_debug("not exit %s meta attr of table ino %lu \n",
2336 + name, inode->i_ino);
2340 + if (!buf || *size < s_attr->array[index].len) {
2341 + /*return the size of this meta attr */
2342 + error = s_attr->array[index].len;
2345 + map_len = (s_attr->array[index].len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2346 + left_size = *size;
2347 + for(i = 0; i < map_len; i++) {
2348 + struct buffer_head *array_bh = NULL;
2350 + array_bh = ext3_bread(NULL, inode,
2351 + s_attr->array[index].start + i,
2354 + snap_err("ino %lu read snap attr offset %d error %d \n",
2355 + inode->i_ino, (s_attr->array[index].start + i),
2359 + if (left_size >= sb->s_blocksize) {
2360 + memcpy(buf, array_bh->b_data, sb->s_blocksize);
2362 + memcpy(buf, array_bh->b_data, left_size);
2363 + left_size -= sb->s_blocksize;
2366 + *size = s_attr->array[index].len;
2373 +static int ext3_set_meta_attr(struct super_block *sb, char* name,
2374 + char* buf, int size)
2376 + struct inode *inode = NULL;
2377 + handle_t *handle = NULL;
2378 + struct buffer_head *bh = NULL;
2379 + struct table_snap_meta_data *s_attr = NULL;
2380 + unsigned long ino;
2381 + int i, index = 0, error = 0;
2382 + unsigned long new_len = 0, left_size;
2384 + ino = SB_SNAPTABLE_INO(sb);
2386 + if (ino == 0 && !buf) {
2387 + snap_debug("no table ino \n");
2391 + handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2396 + /*create table inode update table ino*/
2397 + inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
2401 + ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
2402 + SB_SNAPTABLE_INO(sb) = inode->i_ino;
2403 + ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
2408 + inode = iget(sb, ino);
2409 + if (!inode || !inode->i_nlink || is_bad_inode(inode)) {
2410 + snap_err("unable to get table ino %lu\n", ino);
2415 + /*read the table from the table inode,
2416 + * If can not find the block just create it*/
2417 + bh = ext3_bread(handle, inode, 0, 1, &error);
2419 + snap_err("read table ino %lu, error %d\n", ino, error);
2423 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2424 + index = find_snap_meta_index(s_attr, name);
2425 + if (index < 0 && !buf) {
2426 + snap_debug("%s meta attr of table ino %lu do not exist\n",
2427 + name, inode->i_ino);
2433 + snap_debug("delete the meta attr %s in the table ino %lu",
2434 + name, inode->i_ino);
2435 + /*Here we only delete the entry of the attr
2436 + *FIXME, should we also delete the block of
2439 + ext3_journal_get_write_access(handle, bh);
2440 + memset(s_attr->array[index].name, 0, TABLE_ITEM_NAME_SIZE);
2441 + s_attr->array[index].len = 0;
2443 + ext3_journal_dirty_metadata(handle, bh);
2447 + new_len = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2448 + /*find the place to put this attr in that index*/
2449 + ext3_journal_get_write_access(handle, bh);
2451 + index = set_snap_meta_index(s_attr, name, size);
2453 + snap_err("table full of ino %lu \n", inode->i_ino);
2459 + s_attr->array[index].len = size;
2460 + journal_dirty_metadata(handle, bh);
2462 + /*put this attr to the snap table*/
2464 + for(i = 0; i < new_len; i++) {
2465 + struct buffer_head *array_bh = NULL;
2467 + array_bh = ext3_bread(handle, inode,
2468 + s_attr->array[index].start + i, 1, &error);
2470 + snap_err("inode %lu Can not get the block of attr %s\n",
2471 + inode->i_ino, name);
2476 + ext3_journal_get_write_access(handle, array_bh);
2477 + if (left_size > inode->i_sb->s_blocksize)
2478 + memcpy(array_bh->b_data, buf, inode->i_sb->s_blocksize);
2480 + memcpy(array_bh->b_data, buf, left_size);
2481 + ext3_journal_dirty_metadata(handle, array_bh);
2482 + left_size -= inode->i_sb->s_blocksize;
2487 + ext3_journal_stop(handle, sb->s_root->d_inode);
2492 +struct snapshot_operations ext3_snap_operations = {
2493 + ops_version: SNAP_VERSION(2,0,2),
2494 + is_redirector: is_redirector,
2495 + is_indirect: is_indirect,
2496 + create_indirect: ext3_create_indirect,
2497 + get_indirect: ext3_get_indirect,
2498 + get_indirect_ino: ext3_get_indirect_ino,
2499 + destroy_indirect: ext3_destroy_indirect,
2500 + restore_indirect: ext3_restore_indirect,
2501 + iterate: ext3_iterate,
2502 + copy_block: ext3_copy_block,
2503 + set_indirect: ext3_set_indirect,
2504 + snap_feature: ext3_snap_feature,
2505 + get_generation: ext3_get_generation,
2506 + set_generation: ext3_set_generation,
2507 + get_meta_attr: ext3_get_meta_attr,
2508 + set_meta_attr: ext3_set_meta_attr,
2511 +EXPORT_SYMBOL(ext3_snap_operations);
2512 +#ifdef SNAP_PROFILE
2513 +EXPORT_SYMBOL(prof_snapdel);
2516 +#ifdef SNAP_DEBUG_IOC
2518 +static int print_inode(struct inode *pri, void *index_val)
2522 + struct snap_ea *snaps;
2523 + char buf[EXT3_MAX_SNAP_DATA];
2524 + int index = *(int *)index_val;
2526 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2527 + buf, EXT3_MAX_SNAP_DATA);
2529 + if (err == -ENODATA) {
2530 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
2532 + else if (err < 0) {
2533 + snap_err("got err %d when reading attributes\n", err);
2537 + snaps = (struct snap_ea *) buf;
2539 + if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2540 + snap_debug("no redirected ino for primary inode %lu\n",
2544 + snap_debug("primary inode %lu , redirected ino=%d\n",
2545 + primary->i_ino,le32_to_cpu(snaps->ino[index]));
2551 +int snap_print(struct super_block *sb, int index)
2553 + ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2557 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2559 + int index = *(int *)index_val;
2561 + printk("delete_inode for index %d\n",index);
2562 + rc = ext3_destroy_indirect(primary,index, NULL);
2564 + printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",
2565 + primary->i_ino, index, rc);
2569 +int ext3_snap_delete(struct super_block *sb, int index)
2571 + ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index,
2572 + SNAP_ITERATE_COWED_INODE);
2584 Index: linux-2.4.20-8/fs/ext3/Makefile
2585 ===================================================================
2586 --- linux-2.4.20-8.orig/fs/ext3/Makefile 2004-01-19 22:06:25.000000000 +0800
2587 +++ linux-2.4.20-8/fs/ext3/Makefile 2004-01-19 22:06:25.000000000 +0800
2590 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2591 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2593 + xattr_trusted.o snap.o
2594 obj-m := $(O_TARGET)
2596 export-objs += xattr.o
2597 Index: linux-2.4.20-8/fs/ext3/inode.c
2598 ===================================================================
2599 --- linux-2.4.20-8.orig/fs/ext3/inode.c 2004-01-19 22:06:24.000000000 +0800
2600 +++ linux-2.4.20-8/fs/ext3/inode.c 2004-01-26 01:12:48.000000000 +0800
2601 @@ -1191,7 +1191,7 @@
2602 * So, if we see any bmap calls here on a modified, data-journaled file,
2603 * take extra steps to flush any blocks which might be in the cache.
2605 -static int ext3_bmap(struct address_space *mapping, long block)
2606 +int ext3_bmap(struct address_space *mapping, long block)
2608 struct inode *inode = mapping->host;
2610 @@ -1403,7 +1403,7 @@
2611 * This required during truncate. We need to physically zero the tail end
2612 * of that block so it doesn't yield old data if the file is later grown.
2614 -static int ext3_block_truncate_page(handle_t *handle,
2615 +int ext3_block_truncate_page(handle_t *handle,
2616 struct address_space *mapping, loff_t from)
2618 unsigned long index = from >> PAGE_CACHE_SHIFT;
2619 Index: linux-2.4.20-8/fs/ext3/ialloc.c
2620 ===================================================================
2621 --- linux-2.4.20-8.orig/fs/ext3/ialloc.c 2004-01-19 22:06:24.000000000 +0800
2622 +++ linux-2.4.20-8/fs/ext3/ialloc.c 2004-01-19 22:06:25.000000000 +0800
2623 @@ -160,6 +160,13 @@
2627 +/* Export load_inode_bitmap*/
2628 +int ext3_load_inode_bitmap (struct super_block * sb,
2629 + unsigned int block_group)
2631 + return load_inode_bitmap(sb, block_group);
2635 * NOTE! When we get the inode, we're the only people
2636 * that have access to it, and as such there are no
2637 Index: linux-2.4.20-8/fs/ext3/super.c
2638 ===================================================================
2639 --- linux-2.4.20-8.orig/fs/ext3/super.c 2004-01-19 22:06:24.000000000 +0800
2640 +++ linux-2.4.20-8/fs/ext3/super.c 2004-01-19 22:06:25.000000000 +0800
2641 @@ -1324,6 +1324,13 @@
2642 sbi->s_mount_state = le16_to_cpu(es->s_state);
2643 sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
2644 sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
2645 +#define EXT3_SNAP_FS
2646 +#ifdef EXT3_SNAP_FS
2647 + init_MUTEX(&(sbi->s_snap_list_sem));
2648 + sbi->s_snaptable_ino = le32_to_cpu(es->s_snaptable_ino);
2649 + sbi->s_first_cowed_pri_ino = le32_to_cpu(es->s_first_cowed_pri_ino);
2650 + sbi->s_last_cowed_pri_ino = le32_to_cpu(es->s_last_cowed_pri_ino);
2652 for (i=0; i < 4; i++)
2653 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2654 sbi->s_def_hash_version = es->s_def_hash_version;
2655 Index: linux-2.4.20-8/fs/ext3/ext3-exports.c
2656 ===================================================================
2657 --- linux-2.4.20-8.orig/fs/ext3/ext3-exports.c 2004-01-19 22:06:19.000000000 +0800
2658 +++ linux-2.4.20-8/fs/ext3/ext3-exports.c 2004-01-26 01:13:53.000000000 +0800
2660 EXPORT_SYMBOL(ext3_xattr_set);
2661 EXPORT_SYMBOL(ext3_prep_san_write);
2662 EXPORT_SYMBOL(ext3_map_inode_page);
2663 +EXPORT_SYMBOL(ext3_orphan_add);
2664 +EXPORT_SYMBOL(ext3_orphan_del);
2665 +EXPORT_SYMBOL(ext3_block_truncate_page)
2667 EXPORT_SYMBOL(ext3_abort);
2668 EXPORT_SYMBOL(ext3_decode_error);
2669 Index: linux-2.4.20-8/include/linux/snap.h
2670 ===================================================================
2671 --- linux-2.4.20-8.orig/include/linux/snap.h 2003-01-30 18:24:37.000000000 +0800
2672 +++ linux-2.4.20-8/include/linux/snap.h 2004-01-19 22:11:26.000000000 +0800
2675 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
2676 + * started by Andreas Dilger <adilger@turbolinux.com>
2677 + * Peter Braam <braam@mountainviewdata.com>
2678 + * Harrison Xing <harrisonx@mountainviewdata.com>
2680 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2681 + * Eric Mei <Ericm@clusterfs.com>
2682 + * Wang Di <wangdi@clusterfs.com>
2684 + * Rewriten 2003 by Wang Di <wangdi@clusterfs.com>
2685 + * Eric Mei <ericm@clusterfs.com>
2687 + * Functions for implementing snapshots in the ext3 filesystem. They are
2688 + * intended to hide the internals of the filesystem from the caller in
2689 + * such a way that the caller doesn't need to know about inode numbers,
2690 + * how the redirectors are implemented or stored, etc. It may not do that
2691 + * all yet, but it tries.
2693 + * The snapshot inode redirection is stored in the primary/direct inode as
2694 + * an extended attribute $snap, in the form of little-endian u32 inode
2699 +#ifndef _LINUX_SNAP_H
2700 +#define _LINUX_SNAP_H
2702 +#include <linux/fs.h>
2704 +/* maximum number of snapshots available for users */
2705 +#define MAX_SNAPS 20
2707 +/* snap extended attributes definition */
2708 +#define SNAP_ATTR "@snap"
2713 + ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2714 + ino_t parent_ino[MAX_SNAPS+1];
2716 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
2718 +/* for compatibility with old 128 max snapshots */
2719 +#define MAX_SNAP128_DATA (sizeof(struct snap_ea) - (sizeof(ino_t) * 128 * 2))
2720 +#define ZERO_SNAP_ATTR_TOP(buf) \
2722 + struct snap_ea *p = (struct snap_ea*)buf; \
2723 + memset(&p->ino[129], 0, sizeof(ino_t)*128); \
2724 + memset(&p->parent_ino[129], 0, sizeof(ino_t)*128); \
2727 +/* snap new ea definition , for logging of new inode */
2728 +#define SNAP_NEW_INO_ATTR "@snap_new"
2729 +struct snap_new_ea{
2730 + ino_t prev_ino; /* reserved. save the inode to a linked list */
2732 + int new_index; /* indicate for which index this is a new inode */
2734 +#define NULL_NEW_INDEX -1 /* null new index, to clear the snap_new_ea */
2736 +/* ea to identiry a indirect inode's infomation */
2737 +#define SNAP_INDIRECT_INFO_ATTR "@snap_indirect_inode_info"
2738 +struct snap_indirect_info {
2739 + __u32 index; /* which index belongs to */
2740 + __u32 reserved[3]; /* reserved */
2744 +/* snapfs meta data stored in extended attributes of root ino */
2745 +#define DISK_SNAP_META_ATTR "@disk_snap_meta_attr"
2746 +struct disk_snap_meta_data {
2747 + ino_t snap_first_cowed_ino;
2748 + ino_t snap_table_ino;
2749 + __u32 snap_feature_compat;
2751 +/*snapfs quota info */
2753 +#define SNAP_USR_QUOTA 0
2754 +#define SNAP_GRP_QUOTA 1
2755 +#define DISK_SNAP_QUOTA_INFO "@disk_snap_quota_info"
2756 +struct quota_info_len {
2757 + int uid_len; /*uid quota info length */
2758 + int gid_len; /*gid quota info length */
2761 + * Check if the EA @name is Snap EA or not.
2762 + * Snap EA includes the SNAP_ATTR, SNAP_NEW_INO_ATTR and DISK_SNAP_META_ATTR
2765 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) || \
2766 + (!strcmp((name), DISK_SNAP_META_ATTR)))
2770 +/* file system features */
2771 +#define SNAP_FEATURE_COMPAT_SNAPFS 0x0010
2772 +#define SNAP_FEATURE_COMPAT_BLOCKCOW 0x0020
2774 +/* constants for snap_feature operations */
2775 +#define SNAP_CLEAR_FEATURE 0x0
2776 +#define SNAP_SET_FEATURE 0x1
2777 +#define SNAP_HAS_FEATURE 0x2
2779 +/* snap flags for inode, within 1 byte range, each occupy 1 bit */
2780 +#define SNAP_INO_MAGIC 0x88 /* magic for snap inode */
2781 +#define SNAP_COW_FLAG 0x01 /* snap redirected inode */
2782 +#define SNAP_DEL_FLAG 0x02 /* snap deleted inode */
2783 +#define SNAP_TABLE_FLAG 0x04 /* snap table inode */
2784 +#define SNAP_PRI_FLAG 0x08 /* primary inode */
2786 +/* no snapfs attributes for get_indirect_ino */
2787 +#define ENOSNAPATTR 320
2789 +/* constants used by iterator */
2790 +#define SNAP_ITERATE_ALL_INODE 0x0
2791 +#define SNAP_ITERATE_COWED_INODE 0x1
2793 +/* constants used by create_indirect */
2794 +#define SNAP_CREATE_IND_NORMAL 0x0
2795 +#define SNAP_CREATE_IND_DEL_PRI 0x1
2797 +/* the data structure represent in the xfs_dinode.pad
2798 + offset 0: magic (1 byte)
2799 + offset 1: flag (1 byte)
2800 + offset 2: gen (4 bytes)
2803 +#define SIZEOF_MAGIC 1
2804 +#define SIZEOF_FLAG 1
2805 +#define SIZEOF_GENERATION 4
2807 +#define MAGIC_OFFSET 0
2808 +#define FLAG_OFFSET 1
2809 +#define GENERATION_OFFSET 2
2811 +#define SNAP_GET_DINODE_MAGIC(dinode) \
2812 + (((__u8*)(dinode)->di_pad)[MAGIC_OFFSET])
2813 +#define SNAP_SET_DINODE_MAGIC(dinode) \
2814 + ((__u8*)(dinode)->di_pad)[MAGIC_OFFSET] = (SNAP_INO_MAGIC)
2815 +#define SNAP_GET_DINODE_FLAG(dinode) \
2816 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET])
2817 +#define SNAP_SET_DINODE_FLAG(dinode, flag) \
2818 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] |= (flag))
2819 +#define SNAP_CLEAR_DINODE_FLAG(dinode, flag) \
2820 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] &= ~(flag))
2821 +#define SNAP_GET_DINODE_GEN(dinode) \
2822 + (le32_to_cpu(*(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET])))
2823 +#define SNAP_SET_DINODE_GEN(dinode, gen) \
2824 + *(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET]) = cpu_to_le32(gen)
2827 +/* header of saving snaptable */
2829 + unsigned int size; /* buffer size passed by */
2830 + char data[0]; /* followed by actual data */
2833 +/* header of on-disk table data */
2834 +struct disk_snap_table_header {
2840 +/* table magic and version constant */
2841 +#define SNAP_TABLE_MAGIC 0xB3A2957F
2842 +#define SNAP_TABLE_VERSION 1
2845 +#define SNAPTABLE_BLOCKS(sb,size) \
2846 + (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
2847 + >> sb->s_blocksize_bits)+1)
2850 +#define SNAP_VERSION(a,b,c) \
2851 + (((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF))
2852 +#define SNAP_VERSION_MAJOR(v) \
2853 + ((v >> 16) & 0xFF)
2854 +#define SNAP_VERSION_MINOR(v) \
2856 +#define SNAP_VERSION_REL(v) \
2859 +/* for snap meta attr table */
2860 +#define TABLE_ITEM_COUNT 200
2861 +#define TABLE_ITEM_SIZE 1000
2862 +#define TABLE_ITEM_NAME_SIZE 16
2864 +/*snap table array */
2865 +struct snap_meta_array {
2866 + char name[TABLE_ITEM_NAME_SIZE];
2867 + int start; /* where is the start of the array */
2868 + int len; /* the len of the array */
2870 +/* snap table structure for record the information */
2871 +struct table_snap_meta_data {
2873 + struct snap_meta_array array[TABLE_ITEM_COUNT];
2878 +#define SNAP_PROFILE
2880 +#undef SNAP_PROFILE
2883 +#ifdef SNAP_PROFILE
2884 +struct profile_snapdel_stat
2886 + unsigned long total_tick; /* total time */
2887 + unsigned long inodes; /* primary inodes */
2889 + unsigned long yield_count; /* for yeild cpu */
2890 + unsigned long yield_tick;
2891 + unsigned long yield_max_tick;
2893 + unsigned long getea_count; /* for get ea */
2894 + unsigned long getea_tick;
2895 + unsigned long getea_max_tick;
2897 + unsigned long setea_count; /* for set ea */
2898 + unsigned long setea_tick;
2899 + unsigned long setea_max_tick;
2901 + unsigned long converge_count; /* for converge */
2902 + unsigned long converge_tick;
2903 + unsigned long converge_max_tick;
2908 +/* snapshot operations */
2909 +struct snapshot_operations {
2910 + unsigned int ops_version;
2911 + int (*is_redirector) (struct inode *inode);
2912 + int (*is_indirect) (struct inode *inode);
2913 + struct inode * (*create_indirect) (struct inode *pri, int index,
2914 + unsigned int gen, ino_t parent_ino,
2916 + struct inode * (*get_indirect) (struct inode *pri, int *table,int slot);
2917 + ino_t (*get_indirect_ino) (struct inode *pri, int index);
2918 + int (*destroy_indirect) (struct inode *pri, int index,
2919 + struct inode *next_ind);
2920 + int (*restore_indirect) (struct inode *pri, int index);
2921 + int (*iterate) (struct super_block *sb,
2922 + int (*repeat)(struct inode *inode, void *priv),
2923 + struct inode **start, void *priv, int flag);
2924 + int (*copy_block) ( struct inode *dst, struct inode *src, int blk);
2925 + int (*has_block) (struct inode *dst, int blk);
2926 + int (*set_indirect) (struct inode *pri, int index,
2927 + ino_t ind_ino, ino_t parent_ino );
2928 + int (*snap_feature) (struct super_block *sb, int feature, int op);
2929 + int (*get_generation) (struct inode *pri);
2930 + int (*set_generation) (struct inode *pri, unsigned long new_gen);
2931 + int (*has_del_flag) (struct inode *inode);
2932 + int (*clear_del_flag) (struct inode *inode);
2933 + int (*set_meta_attr)(struct super_block *sb, char *name,
2934 + char *buf, int size);
2935 + int (*get_meta_attr)(struct super_block *sb, char *name,
2936 + char *buf, int *size);
2940 Index: linux-2.4.20-8/include/linux/ext3_fs.h
2941 ===================================================================
2942 --- linux-2.4.20-8.orig/include/linux/ext3_fs.h 2004-01-19 22:06:24.000000000 +0800
2943 +++ linux-2.4.20-8/include/linux/ext3_fs.h 2004-01-19 22:11:15.000000000 +0800
2944 @@ -183,7 +183,13 @@
2945 #define EXT3_INDEX_FL 0x00001000 /* hash-indexed directory */
2946 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2947 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2948 -#define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2949 +/* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
2950 +#define EXT3_COW_FL 0x00008000 /* inode is snapshot cow */
2951 +#define EXT3_DEL_FL 0x00010000 /* inode is deleting in snapshot */
2952 +#define EXT3_SNAP_TABLE_FLAG 0x00020000 /* snap table inode */
2953 +/* FIXME For debugging will be removed later*/
2954 +#define EXT3_SNAP_PRI_FLAG 0x00040000 /* primary inode */
2957 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2958 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2959 @@ -205,10 +211,25 @@
2960 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2961 #define EXT3_IOC_GETVERSION_OLD _IOR('v', 1, long)
2962 #define EXT3_IOC_SETVERSION_OLD _IOW('v', 2, long)
2963 +/* the following are for temporary test */
2964 +/* snapfs ioctls */
2965 +#define EXT3_IOC_CREATE_INDIR _IOW('v', 3, long)
2966 +#define EXT3_IOC_GET_INDIR _IOW('v', 4, long)
2967 +#define EXT3_IOC_DESTROY_INDIR _IOW('v', 5, long)
2968 +#define EXT3_IOC_IS_REDIR _IOW('v', 6, long)
2969 +#define EXT3_IOC_RESTORE_INDIR _IOW('v', 7, long)
2971 +#define EXT3_IOC_SNAP_SETFILECOW _IOW('v', 10, long)
2973 +/* XXX: the following are for temporary test, can be removed later */
2974 +#define EXT3_IOC_SNAP_PRINT _IOW('v', 11, long)
2975 +#define EXT3_IOC_SNAP_DELETE _IOW('v', 12, long)
2976 +#define EXT3_IOC_SNAP_RESTORE _IOW('v', 13, long)
2979 #ifdef CONFIG_JBD_DEBUG
2980 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2984 * Structure of an inode on the disk
2986 @@ -429,7 +450,15 @@
2987 __u8 s_def_hash_version; /* Default hash version to use */
2988 __u8 s_reserved_char_pad;
2989 __u16 s_reserved_word_pad;
2990 - __u32 s_reserved[192]; /* Padding to the end of the block */
2991 + __u32 s_default_mount_opts;
2992 + __u32 s_first_meta_bg; /* First metablock group */
2993 + __u32 s_mkfs_time; /* When the filesystem was created */
2995 + __u32 s_first_cowed_pri_ino; /* For snapfs,the first cowed primary inode */
2996 + __u32 s_last_cowed_pri_ino; /* last cowed ino in memory */
2997 + __u32 s_snaptable_ino; /* snaptable ino in memory */
2998 + __u32 s_last_snap_orphan; /* SnapFS: start of cowing indirect inode */
2999 + __u32 s_reserved[186]; /* Padding to the end of the block,originally 204 */
3004 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
3005 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
3007 +#define EXT3_FEATURE_COMPAT_SNAPFS 0x0010
3008 +#define EXT3_FEATURE_COMPAT_BLOCKCOW 0x0020
3010 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
3011 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
3012 EXT3_FEATURE_INCOMPAT_RECOVER)
3013 Index: linux-2.4.20-8/include/linux/ext3_fs_sb.h
3014 ===================================================================
3015 --- linux-2.4.20-8.orig/include/linux/ext3_fs_sb.h 2004-01-19 22:06:18.000000000 +0800
3016 +++ linux-2.4.20-8/include/linux/ext3_fs_sb.h 2004-01-19 22:10:06.000000000 +0800
3018 wait_queue_head_t s_delete_thread_queue;
3019 wait_queue_head_t s_delete_waiter_queue;
3021 +#define EXT3_SNAP_FS
3022 +#ifdef EXT3_SNAP_FS
3023 + struct semaphore s_snap_list_sem;
3024 + unsigned long s_first_cowed_pri_ino;/* For snapfs,the first cowed primary inode */
3025 + unsigned long s_last_cowed_pri_ino; /* last cowed ino in memory */
3026 + unsigned long s_snaptable_ino; /* snaptable ino in memory */
3030 #endif /* _LINUX_EXT3_FS_SB */
3031 Index: linux-2.4.20-8/include/linux/ext3_jbd.h
3032 ===================================================================
3033 --- linux-2.4.20-8.orig/include/linux/ext3_jbd.h 2004-01-19 22:06:15.000000000 +0800
3034 +++ linux-2.4.20-8/include/linux/ext3_jbd.h 2004-01-19 22:11:15.000000000 +0800
3037 #define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
3039 +/*snapshot transaction blocks*/
3041 +#define EXT3_EA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3042 +#define EXT3_SETMETA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3043 +#define EXT3_NEWINODE_TRANS_BLOCKS 10
3044 +#define SNAP_INSERTLIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 1)
3045 +#define SNAP_DELETELIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 2)
3046 +#define SNAP_COPYBLOCK_TRANS_BLOCKS (EXT3_DATA_TRANS_BLOCKS)
3047 +#define SNAP_MIGRATEDATA_TRANS_BLOCKS 2
3048 +#define SNAP_SETIND_TRANS_BLOCKS (SNAP_INSERTLIST_TRANS_BLOCKS + 1)
3049 +#define SNAP_ADDORPHAN_TRANS_BLOCKS 2
3050 +#define SNAP_REMOVEORPHAN_TRANS_BLOCKS 1
3051 +#define SNAP_RESTOREORPHAN_TRANS_BLOCKS (EXT3_EA_TRANS_BLOCKS + \
3052 + SNAP_DELETELIST_TRANS_BLOCKS + \
3053 + EXT3_NEWINODE_TRANS_BLOCKS + \
3054 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS)
3055 +#define SNAP_BIGCOPY_TRANS_BLOCKS (2 * EXT3_DATA_TRANS_BLOCKS)
3056 +#define SNAP_CREATEIND_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3057 + SNAP_MIGRATEDATA_TRANS_BLOCKS + \
3058 + SNAP_SETIND_TRANS_BLOCKS + \
3059 + SNAP_BIGCOPY_TRANS_BLOCKS + 3)
3060 +#define SNAP_MIGRATEBLK_TRANS_BLOCKS 2
3061 +#define SNAP_DESTROY_TRANS_BLOCKS (SNAP_DELETELIST_TRANS_BLOCKS + \
3062 + EXT3_EA_TRANS_BLOCKS + 2)
3063 +#define SNAP_RESTORE_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3064 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS + 1)
3067 ext3_mark_iloc_dirty(handle_t *handle,
3068 struct inode *inode,
3071 fs/ext3/Makefile | 2
3072 fs/ext3/ext3-exports.c | 3
3073 fs/ext3/ialloc.c | 7
3075 fs/ext3/snap.c | 2577 +++++++++++++++++++++++++++++++++++++++++++++
3077 include/linux/ext3_fs.h | 38
3078 include/linux/ext3_fs_sb.h | 7
3079 include/linux/ext3_jbd.h | 27
3080 include/linux/snap.h | 266 ++++
3081 10 files changed, 2932 insertions(+), 6 deletions(-)