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-13 00:11:40.000000000 +0800
9 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
10 + * started by Andreas Dilger <adilger@turbolinux.com>
11 + * Peter Braam <braam@mountainviewdata.com>
12 + * Harrison Xing <harrisonx@mountainviewdata.com>
13 + * Eric Mei <Ericm@clusterfs.com>
15 + * port to 2.4 by Wang Di <wangdi@clusterfs.com>
16 + * Eric Mei <ericm@clusterfs.com>
18 + * Functions for implementing snapshots in the ext3 filesystem. They are
19 + * intended to hide the internals of the filesystem from the caller in
20 + * such a way that the caller doesn't need to know about inode numbers,
21 + * how the redirectors are implemented or stored, etc. It may not do that
22 + * all yet, but it tries.
24 + * The snapshot inode redirection is stored in the primary/direct inode as
25 + * an extended attribute $snap, in the form of little-endian u32 inode
30 +#define EXPORT_SYMTAB
31 +#include <linux/module.h>
33 +#include <linux/sched.h>
34 +#include <linux/jbd.h>
35 +#include <linux/mm.h>
36 +#include <linux/slab.h>
37 +#include <linux/locks.h>
38 +#include <linux/snap.h>
39 +#include <linux/ext3_jbd.h>
40 +#include <linux/ext3_fs.h>
41 +#include <linux/ext3_xattr.h>
43 +#define EXT3_SNAP_ATTR "@snap"
44 +#define EXT3_SNAP_GENERATION_ATTR "@snap_generation"
45 +#define EXT3_MAX_SNAPS 20
46 +#define EXT3_MAX_SNAP_DATA (sizeof(struct snap_ea))
47 +#define EXT3_SNAP_INDEX EXT3_XATTR_INDEX_LUSTRE
49 +#ifdef EXT3_SNAP_DEBUG
50 + static long snap_kmem = 0;
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 +/* helper functions to manipulate field 'parent' in snap_ea */
234 +set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
236 + char * p = (char*) pea;
239 + offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
240 + offset += sizeof(ino_t) * index;
241 + *(ino_t*)(p+offset) = val;
246 +get_parent_ino(struct snap_ea *pea, int size, int index)
248 + char * p = (char*)pea;
251 + offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
252 + offset += sizeof(ino_t) * index;
253 + return *(ino_t*)(p+offset);
255 +static inline void snap_double_lock(struct inode *i1, struct inode *i2)
257 + double_down(&i1->i_sem, &i2->i_sem);
260 +static inline void snap_double_unlock(struct inode *i1, struct inode *i2)
262 + double_up(&i1->i_sem, &i2->i_sem);
265 +/* ext3_iterate_cowed_inode:
266 + * iterate all the cowed inode with the same index and
267 + * run the associate function @repeat
269 + * For @repeat, if it returns non-zero value, it will exit the iterator
271 + * return value: 0 or positive: success
272 + * negative: failure
273 + * additional: if the return value is positive, it must be the return value
274 + * of function @repeat.
277 +static int ext3_iterate_cowed_inode(
278 + struct super_block *sb,
279 + int (*repeat)(struct inode *inode, void *priv),
280 + struct inode **start,
283 + struct inode *list_inode = NULL;
284 + char buf[EXT3_MAX_SNAP_DATA];
285 + struct snap_ea *snaps;
288 + if (SB_FIRST_COWED_INO(sb) == 0) {
289 + snap_debug("no cowed inode in the list\n");
293 + /* get head inode in the list */
294 + if (start != NULL && *start != NULL && (*start)->i_ino)
295 + list_inode = iget(sb, (*start)->i_ino);
297 + list_inode = iget (sb, le32_to_cpu( SB_FIRST_COWED_INO(sb) ));
299 + /* loop for all inode in list */
300 + while (list_inode) {
301 + if (!list_inode->i_nlink || is_bad_inode(list_inode)) {
302 + snap_err("inode %p, ino %lu, mode %o, nlink %d\n",
305 + list_inode->i_mode,
306 + list_inode->i_nlink);
311 + err = ext3_xattr_get(list_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
312 + buf, EXT3_MAX_SNAP_DATA);
313 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
314 + snap_err("inode %lu, error %d\n", list_inode->i_ino, err);
318 + if ((err = (*repeat)(list_inode, priv)) != 0)
323 + snaps = (struct snap_ea *) buf;
324 + if (le32_to_cpu (snaps->next_ino) != 0) {
325 + list_inode = iget(sb, le32_to_cpu(snaps->next_ino));
328 + snap_debug ("cowed inode list end, exit\n");
338 +static int get_cowed_ino(struct inode *pri, void *param)
340 + ino_t *find = param;
341 + (*find) = pri->i_ino;
345 +/* Return 0 for error. */
346 +static int get_cowed_ino_end (struct inode *inode)
351 + rc = ext3_iterate_cowed_inode(inode->i_sb, &get_cowed_ino, &inode, &ino);
359 +/* find the end of the primary inode, iterate if needed
360 + * return 0 if any error found */
361 +static inline ino_t find_last_cowed_ino(struct super_block *sb)
363 + struct inode *inode = NULL;
364 + ino_t first, last = 0;
366 + last = le32_to_cpu(SB_LAST_COWED_INO(sb));
370 + first = le32_to_cpu(SB_FIRST_COWED_INO(sb));
373 + snap_err("first cowed inode is NULL\n");
377 + inode = iget(sb, first);
379 + if (is_bad_inode(inode)) {
380 + snap_err("bad inode %lu\n", first);
384 + last = get_cowed_ino_end(inode);
392 +/* Insert the primary inode to the cowed inode list
393 + * Append it to the list end
395 + * @pri: inode to insert
396 + * @buf_pri: the valid ea buf for @pri inode ( excluding the next_ino field) ,
397 + * it's used to write the ea for @pri inode
399 + * To avoid list broken in abnormal case, it will first write the ea for @pri
400 + * inode, and then write ea for the list end inode. Thus list broken is
401 + * avoid even if there are errors when writting ea.
403 +static int insert_cowed_ino_to_list (handle_t *handle, struct inode *pri, char *buf_pri)
405 + char buf[EXT3_MAX_SNAP_DATA];
406 + struct snap_ea *snaps;
407 + struct snap_ea *snaps_pri;
408 + struct inode *last_inode = NULL;
409 + struct ext3_sb_info *sbi = EXT3_SB(pri->i_sb);
412 + snaps_pri = (struct snap_ea *)buf_pri;
414 + if (!SB_FIRST_COWED_INO(pri->i_sb)) {
415 + /* we set the next_ino and write ea for pri inode */
416 + snaps_pri->next_ino = cpu_to_le32(0);
417 + snaps_pri->prev_ino = cpu_to_le32(0);
419 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
420 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
422 + snap_err("ino %lu, set_ext_attr err %d\n", pri->i_ino, err);
425 + lock_super(pri->i_sb);
426 + ext3_journal_get_write_access(handle, sbi->s_sbh);
427 + sbi->s_es->s_first_cowed_pri_ino = cpu_to_le32(pri->i_ino);
428 + SB_LAST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
429 + pri->i_sb->s_dirt = 1;
430 + ext3_journal_dirty_metadata(handle, sbi->s_sbh);
431 + unlock_super(pri->i_sb);
432 + EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG;
436 + if (!SB_LAST_COWED_INO(pri->i_sb)){
437 + SB_LAST_COWED_INO(pri->i_sb) = find_last_cowed_ino(pri->i_sb);
438 + if (!SB_LAST_COWED_INO(pri->i_sb) ){
439 + snap_err("error, last cowed inode is NULL\n");
444 + last_inode = iget(pri->i_sb, SB_LAST_COWED_INO(pri->i_sb));
445 + if (!last_inode || is_bad_inode(last_inode)) {
449 + err = ext3_xattr_get(last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
450 + buf, EXT3_MAX_SNAP_DATA);
451 + if (err == -ENODATA) {
452 + snap_debug("no existing attributes - zeroing\n");
453 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
454 + } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
455 + snap_debug("got err %d when reading attributes\n", err);
458 + /*set primary inode EA*/
459 + snaps_pri->next_ino = 0;
460 + snaps_pri->prev_ino = cpu_to_le32(last_inode->i_ino);
462 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
463 + buf_pri, EXT3_MAX_SNAP_DATA, 0);
465 + snap_debug("set attributes error for inode %lu\n",
466 + (ulong)pri->i_ino);
470 + /*set last inode EA*/
471 + snaps = (struct snap_ea *) buf;
472 + snaps->next_ino = cpu_to_le32(pri->i_ino);
473 + err = ext3_xattr_set(handle, last_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
474 + buf, EXT3_MAX_SNAP_DATA, 0);
476 + snap_debug("set attributes error for inode %lu\n",
477 + (ulong)last_inode->i_ino);
481 + EXT3_I(pri)->i_flags |= EXT3_SNAP_PRI_FLAG;
483 + /* we update the new cowed ino list end in memory */
484 + SB_LAST_COWED_INO(pri->i_sb) = cpu_to_le32(pri->i_ino);
485 + snap_debug("cowed_inode_list_end %lu, append ino=%d\n",
486 + last_inode->i_ino, le32_to_cpu(snaps->ino[index]));
494 +/* delelte the ino from cowed inode list */
495 +static int delete_cowed_ino_from_list (handle_t *handle, struct inode *inode)
497 + ino_t prev_ino = 0, next_ino = 0;
498 + struct inode *prev_inode = NULL;
499 + struct inode *next_inode = NULL;
500 + struct snap_ea *snaps;
501 + char buf[EXT3_MAX_SNAP_DATA];
504 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
505 + buf, EXT3_MAX_SNAP_DATA);
506 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
507 + snap_err("get attr inode %lu, error %d\n", inode->i_ino, err);
511 + snaps = (struct snap_ea *) buf;
512 + next_ino = le32_to_cpu(snaps->next_ino);
513 + prev_ino = le32_to_cpu(snaps->prev_ino);
515 + /* if this is the first cowed ino */
516 + if (inode->i_ino == le32_to_cpu(SB_FIRST_COWED_INO(inode->i_sb))) {
517 + SB_FIRST_COWED_INO(inode->i_sb) = cpu_to_le32(next_ino);
518 + EXT3_I(inode)->i_flags &= ~EXT3_SNAP_PRI_FLAG;
524 + /* find previous inode and read its ea */
525 + prev_inode = iget(inode->i_sb, prev_ino);
526 + if (!prev_inode || is_bad_inode(prev_inode))
529 + err = ext3_xattr_get(prev_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
530 + buf, EXT3_MAX_SNAP_DATA);
531 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
532 + snap_err("get attr inode %lu, error %d\n", prev_inode->i_ino, err);
536 + /* make the previous inode point to the next inode,
537 + * but ignore errors because at current version we
538 + * didn't use the previous pionter */
539 + snaps = (struct snap_ea *) buf;
540 + snaps->next_ino = cpu_to_le32(next_ino);
542 + snap_debug("delete ino %lu from list\n", inode->i_ino);
544 + err = ext3_xattr_set(handle, prev_inode, EXT3_SNAP_INDEX,
545 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
547 + snap_err("err %d setting ea for ino %lu\n", err, prev_inode->i_ino);
551 + if (next_ino == 0) {
552 + SB_LAST_COWED_INO(inode->i_sb) = prev_ino;
556 + /* make the next inode point to the previous one */
557 + next_inode = iget(inode->i_sb, next_ino);
558 + if (!next_inode || is_bad_inode(next_inode))
561 + err = ext3_xattr_get(next_inode, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
562 + buf, EXT3_MAX_SNAP_DATA);
563 + if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
564 + snap_err("set attr inode %lu, error %d\n", next_inode->i_ino, err);
567 + snaps = ( struct snap_ea *) buf;
568 + snaps->prev_ino = cpu_to_le32(prev_ino);
570 + err = ext3_xattr_set(handle, next_inode, EXT3_SNAP_INDEX,
571 + EXT3_SNAP_ATTR, buf, EXT3_MAX_SNAP_DATA, 0);
573 + snap_err("err %d setting attributes for ino %lu\n",
574 + err, next_inode->i_ino);
583 +static inline void lock_list(struct super_block *sb)
585 + down(&SB_SNAP_LIST_SEM(sb));
588 +static inline void unlock_list(struct super_block *sb)
590 + up(&SB_SNAP_LIST_SEM(sb));
593 +static int ext3_snap_feature (struct super_block *sb, int feature, int op) {
598 + case SNAP_SET_FEATURE:
599 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
601 + ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
602 + SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
604 + ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
606 + ext3_journal_stop(handle, sb->s_root->d_inode);
608 + case SNAP_CLEAR_FEATURE:
609 + handle = ext3_journal_start(sb->s_root->d_inode, 1);
611 + ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
612 + SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature);
613 + ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
616 + ext3_journal_stop(handle, sb->s_root->d_inode);
618 + case SNAP_HAS_FEATURE:
619 + /*FIXME should lock super or not*/
620 + rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
628 +#ifdef _DEVICE_FAIL_TEST
630 +extern int loop_discard_io(kdev_t dev, long arg);
632 + * modify failpos to let loop fail at certain point
633 + * let pos=0 mean no fail point
635 +static int failpos = 0;
636 +#define loopfail(pos) \
638 + if( pos == failpos ){ \
640 + printk(KERN_EMERG "SNAP; hit fail point %d\n", failpos);\
641 + for( i=0; i<15; i++ ) \
642 + loop_discard_io( MKDEV(7,i), 1 ); \
646 +#define loopfail(pos) do{}while(0)
649 +/* Save the indirect inode in the snapshot table of the primary inode. */
650 +static int ext3_set_indirect(struct inode *pri, int index, ino_t ind_ino, ino_t parent_ino )
652 + char buf[EXT3_MAX_SNAP_DATA];
653 + struct snap_ea *snaps;
654 + int err = 0, inlist = 1;
656 + handle_t *handle = NULL;
658 + snap_debug("(ino %lu, parent %lu): saving ind %lu to index %d\n",
659 + pri->i_ino, parent_ino, ind_ino, index);
661 + if (index < 0 || index > MAX_SNAPS || !pri)
663 + /* need lock the list before get_attr() to avoid race */
664 + lock_list(pri->i_sb);
665 + /* read ea at first */
666 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
667 + buf, EXT3_MAX_SNAP_DATA);
668 + if (err == -ENODATA || err == -ENOATTR) {
669 + snap_debug("no extended attributes - zeroing\n");
670 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
672 + * To judge a inode in list, we only see if it has snap ea.
673 + * So take care of snap ea of primary inodes very carefully.
674 + * Is it right in snapfs EXT3, check it later?
677 + // ea_size = SNAP_EA_SIZE_FROM_INDEX(index);
678 + } else if (err < 0 || err > EXT3_MAX_SNAP_DATA) {
682 + handle = ext3_journal_start(pri, SNAP_SETIND_TRANS_BLOCKS);
684 + err = PTR_ERR(handle);
688 + snaps = (struct snap_ea *)buf;
689 + snaps->ino[index] = cpu_to_le32 (ind_ino);
690 + ea_size = EXT3_MAX_SNAP_DATA;
692 + set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
694 + snap_debug("saving attributes\n");
697 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
698 + buf, EXT3_MAX_SNAP_DATA, 0);
701 + /* This will also write the ea for the pri inode, like above */
702 + err = insert_cowed_ino_to_list(handle, pri, buf);
704 + ext3_mark_inode_dirty(handle, pri);
705 + ext3_journal_stop(handle, pri);
707 + unlock_list(pri->i_sb);
712 + * is_redirector - determines if a primary inode is a redirector
713 + * @inode: primary inode to test
715 + * Returns 1 if the inode is a redirector, 0 otherwise.
717 +static int is_redirector(struct inode *inode)
719 + int is_redirector = 0;
722 + rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
724 + if (rc > 0 && rc <= MAX_SNAP_DATA)
726 + snap_debug("inode %lu %s redirector\n", inode->i_ino,
727 + is_redirector ? "is" : "isn't");
728 + return is_redirector;
731 +/*if it's indirect inode or not */
732 +static int is_indirect(struct inode *inode)
734 + if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
740 + * Copy inode metadata from one inode to another, excluding blocks and size.
741 + * FIXME do we copy EA data - ACLs and such (excluding snapshot data)?
743 +static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
747 + dst->i_mode = src->i_mode;
748 + dst->i_nlink = src->i_nlink;
749 + dst->i_uid = src->i_uid;
750 + dst->i_gid = src->i_gid;
751 + dst->i_atime = src->i_atime;
752 + dst->i_mtime = src->i_mtime;
753 + dst->i_ctime = src->i_ctime;
754 +// dst->i_version = src->i_version;
755 + dst->i_attr_flags = src->i_attr_flags;
756 + dst->i_generation = src->i_generation;
757 + dst->u.ext3_i.i_dtime = src->u.ext3_i.i_dtime;
758 + dst->u.ext3_i.i_flags = src->u.ext3_i.i_flags | EXT3_COW_FL;
759 +#ifdef EXT3_FRAGMENTS
760 + dst->u.ext3_i.i_faddr = src->u.ext3_i.i_faddr;
761 + dst->u.ext3_i.i_frag_no = src->u.ext3_i.i_frag_no;
762 + dst->u.ext3_i.i_frag_size = src->u.ext3_i.i_frag_size;
764 + if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
769 + if (ext3_xattr_list(src, names, 0) < 0)
772 + * the list of attribute names are stored as NUL terminated
773 + * strings, with a double NUL string at the end.
776 + while ((namelen = strlen(name))) {
780 + /* don't copy snap data */
781 + if (!strcmp(name, EXT3_SNAP_ATTR)) {
782 + snap_debug("skipping %s item\n", name);
785 + snap_debug("copying %s item\n", name);
786 + attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX,
787 + EXT3_SNAP_ATTR, NULL, 0);
790 + if ((buf = kmalloc(attrlen, GFP_ATOMIC)) == NULL)
792 + if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
793 + EXT3_SNAP_ATTR, buf, attrlen) < 0)
795 + if (ext3_xattr_set(handle, dst, EXT3_SNAP_INDEX,
796 + EXT3_SNAP_ATTR, buf, attrlen, 0) < 0)
799 + name += namelen + 1; /* skip name and trailing NUL */
804 +static inline int ext3_has_ea(struct inode *inode)
806 + return (EXT3_I(inode)->i_file_acl != 0);
809 +/* ext3_migrate_data:
810 + * MOVE all the data blocks from inode src to inode dst as well as
811 + * COPY all attributes(meta data) from inode src to inode dst.
812 + * For extended attributes(EA), we COPY all the EAs but skip the Snap EA from src to dst.
813 + * If the dst has Snap EA, then we CAN'T overwrite it. We CAN'T copy the src Snap EA.
814 + * XXX for EA, can we change it to MOVE all the EAs(exclude Snap EA) to dst and copy it back to src ?
815 + * This is for LAN free backup later.
818 +static int ext3_migrate_data (handle_t *handle, struct inode *dst, struct inode *src)
820 + unsigned long err = 0;
821 + /* 512 byte disk blocks per inode block */
822 + int bpib = src->i_sb->s_blocksize >> 9;
824 + if((!dst) || (!src))
827 + if (dst->i_ino == src->i_ino)
830 + ext3_copy_meta(handle, dst, src);
832 + snap_debug("migrating %ld data blocks from %lu to %lu\n",
833 + blocks, src->i_ino, dst->i_ino);
834 + /* Can't check blocks in case of EAs */
835 + memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
836 + sizeof(EXT3_I(src)->i_data));
837 + memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
839 + ext3_discard_prealloc(src);
841 + dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
842 + src->i_size = EXT3_I(src)->i_disksize = 0;
844 + dst->i_blocks = src->i_blocks;
846 + /* Check EA blocks here to modify i_blocks correctly */
847 + if(ext3_has_ea (src)) {
848 + src->i_blocks += bpib;
849 + if( ! ext3_has_ea (dst) )
850 + if( dst->i_blocks >= bpib )
851 + dst->i_blocks -= bpib;
853 + if( ext3_has_ea (dst))
854 + dst->i_blocks += bpib;
857 + snap_debug("migrate data from ino %lu to ino %lu\n",
858 + src->i_ino, dst->i_ino);
859 + ext3_mark_inode_dirty(handle, src);
860 + ext3_mark_inode_dirty(handle, dst);
862 + return SNAP_ERROR(err);
866 + * ext3_get_indirect - get a specific indirect inode from a primary inode
867 + * @primary: primary (direct) inode
868 + * @table: table of @slot + 1 indices in reverse chronological order
869 + * @slot: starting slot number to check for indirect inode number
871 + * We locate an indirect inode from a primary inode using the redirection
872 + * table stored in the primary inode. Because the desired inode may actually
873 + * be in a "newer" slot number than the supplied slot, we are given a table
874 + * of indices in chronological order to search for the correct inode number.
875 + * We walk table from @slot to 0 looking for a non-zero inode to load.
877 + * To only load a specific index (and fail if it does not exist), you can
878 + * pass @table = NULL, and the index number in @slot. If @slot == 0, the
879 + * primary inode data is returned.
881 + * We return a pointer to an inode, or an error. If the indirect inode for
882 + * the given index does not exist, NULL is returned.
884 +static struct inode *ext3_get_indirect(struct inode *primary, int *table,
887 + char buf[EXT3_MAX_SNAP_DATA];
888 + struct snap_ea *snaps;
890 + struct inode *inode = NULL;
891 + int err = 0, index = 0;
893 + if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
896 + snap_debug("ino %lu, table %p, slot %d\n", primary->i_ino, table,slot);
898 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
899 + buf, EXT3_MAX_SNAP_DATA);
900 + if (err == -ENODATA) {
902 + } else if (err < 0) {
903 + snap_debug(" attribute read error\n");
906 + snaps = (struct snap_ea *)buf;
908 + /* if table is NULL and there is a slot */
909 + if( !table && slot ) {
911 + ino = le32_to_cpu ( snaps->ino[index] );
912 + if(ino) inode = iget(primary->i_sb, ino);
915 + /* if table is not NULL */
916 + while ( !inode && slot > 0) {
917 + index = table[slot];
918 + ino = le32_to_cpu ( snaps->ino[index] );
920 + snap_debug("snap inode at slot %d is %lu\n", slot, ino);
925 + inode = iget(primary->i_sb, ino);
928 + if( slot == 0 && table ) {
929 + snap_debug("redirector not found, using primary\n");
930 + inode = iget(primary->i_sb, primary->i_ino);
936 +/* get the indirect ino at index of the primary inode
937 + * return value: postive: indirect ino number
938 + * negative or 0: error
940 +static ino_t ext3_get_indirect_ino(struct inode *primary, int index)
942 + char buf[EXT3_MAX_SNAP_DATA];
943 + struct snap_ea *snaps;
947 + if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
950 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
951 + buf, EXT3_MAX_SNAP_DATA);
952 + if (err == -ENOATTR) {
955 + } else if (err < 0) {
956 + snap_err(EXT3_SNAP_ATTR " attribute read error\n");
961 + snaps = (struct snap_ea *)buf;
962 + ino = le32_to_cpu (snaps->ino[index]);
963 + snap_debug("snap ino for %ld at index %d is %lu\n",
964 + primary->i_ino, index, ino);
968 +/* ext3_copy_block - copy one data block from inode @src to @dst.
969 + No lock here. User should do the lock.
970 + User should check the return value to see if the result is correct.
972 + 1: The block has been copied successfully
973 + 0: No block is copied, usually this is because src has no such blk
977 +static int ext3_copy_block (struct inode *dst, struct inode *src, int blk)
979 + struct buffer_head *bh_dst, *bh_src;
982 + handle_t *handle = NULL;
984 + if (!ext3_bmap(src->i_mapping, blk))
988 + * ext3_getblk() require handle!=NULL
990 + journal_data = !S_ISREG(src->i_mode);
992 + handle = ext3_journal_start(dst, SNAP_COPYBLOCK_TRANS_BLOCKS);
996 + bh_src = ext3_bread(handle, src, blk, 0, &err);
998 + snap_err("error for src blk %d, error %d\n", blk, err);
1002 + bh_dst = ext3_getblk(handle, dst, blk, 1, &err);
1004 + snap_err("error for dst blk %d, error %d\n", blk, err);
1006 + goto exit_rels_src;
1008 + snap_debug("copy block %lu to %lu (%ld bytes)\n",
1009 + bh_src->b_blocknr, bh_dst->b_blocknr,
1010 + src->i_sb->s_blocksize);
1013 + ext3_journal_get_write_access(handle, bh_dst);
1016 + memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
1019 + ext3_journal_dirty_metadata(handle, bh_dst);
1021 + mark_buffer_dirty(bh_dst);
1022 + if (IS_SYNC(src)) {
1023 + ll_rw_block (WRITE, 1, &bh_dst);
1024 + wait_on_buffer (bh_dst);
1031 + ext3_journal_stop(handle, dst);
1035 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1037 + * add one inode to superblock's snap_orphan chain
1038 + * only add on-disk data for simplicity
1040 +static void add_snap_orphan(handle_t *handle, struct inode *pri, struct inode *ind)
1042 + struct ext3_sb_info *sb = &pri->i_sb->u.ext3_sb;
1043 + struct ext3_iloc iloc;
1045 + if( ext3_get_inode_loc(ind, &iloc) ){
1046 + snap_debug("--- get ind loc fail\n");
1051 + snap_debug("add new ind inode %lu into orphan list,"
1052 + " primary %lu, last orphan %u\n",
1053 + ind->i_ino, pri->i_ino,
1054 + sb->s_es->s_last_snap_orphan);
1055 + lock_super(pri->i_sb);
1056 + iloc.raw_inode->i_next_snap_orphan = sb->s_es->s_last_snap_orphan;
1057 + iloc.raw_inode->i_snap_primary = pri->i_ino;
1058 + ext3_mark_inode_dirty(handle, ind);
1060 + ext3_journal_get_write_access(handle, sb->s_sbh);
1061 + sb->s_es->s_last_snap_orphan = ind->i_ino;
1062 + pri->i_sb->s_dirt = 1;
1063 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1064 + unlock_super(pri->i_sb);
1069 + * counterpart of add_snap_orphan
1071 +static void remove_snap_orphan(handle_t *handle, struct inode *ind)
1073 + struct ext3_sb_info *sb = &ind->i_sb->u.ext3_sb;
1074 + struct inode *pre = NULL, *inode = NULL;
1075 + struct ext3_iloc iloc, pre_iloc;
1078 + lock_super(ind->i_sb);
1079 + for(ino = sb->s_es->s_last_snap_orphan; ino; ){
1080 + snap_debug("found an orphan, ino=%lu\n", ino);
1081 + inode = iget( ind->i_sb, ino );
1083 + snap_debug("iget %lu fail\n", ino);
1086 + if( ext3_get_inode_loc(inode, &iloc) ){
1087 + snap_debug("get_inode_loc %lu fail\n", ino);
1090 + if( ino == ind->i_ino ){
1092 + snap_debug("found at head of orphan chain\n");
1093 + ext3_journal_get_write_access(handle, sb->s_sbh);
1094 + sb->s_es->s_last_snap_orphan =
1095 + iloc.raw_inode->i_next_snap_orphan;
1096 + ext3_journal_dirty_metadata(handle, sb->s_sbh);
1097 + snap_debug("set new last orphan: %u\n",
1098 + sb->s_es->s_last_snap_orphan);
1102 + snap_debug("found in middle of orphan chain\n");
1103 + if( ext3_get_inode_loc(pre, &pre_iloc) ){
1104 + snap_err("get pre_inode loc %lu fail\n", pre->i_ino);
1107 + pre_iloc.raw_inode->i_next_snap_orphan =
1108 + iloc.raw_inode->i_next_snap_orphan;
1109 + ext3_mark_inode_dirty(handle, pre);
1110 + brelse(pre_iloc.bh);
1116 + ino = iloc.raw_inode->i_next_snap_orphan;
1121 + unlock_super(ind->i_sb);
1126 + * FIXME: how about crashs again during recovery?
1128 +void snap_orphan_cleanup(struct super_block *sb)
1130 + ino_t ind_ino, pri_ino;
1131 + struct inode *ind = NULL, *pri = NULL;
1132 + struct ext3_iloc ind_iloc;
1134 + if( (ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan) == 0 ){
1135 + snap_debug("snap_orphan_cleanup: nothing to do\n");
1139 + snap_debug("------ begin cleanup snap orphans ------\n");
1141 + ind = iget( sb, ind_ino );
1143 + snap_err("snap_orphan_cleanup: get "
1144 + "ind %lu fail\n", ind_ino);
1148 + if( ext3_get_inode_loc(ind, &ind_iloc) ){
1149 + snap_err("snap_orphan_cleanup: get "
1150 + "iloc %lu fail\n", ind_ino);
1155 + ind_ino = sb->u.ext3_sb.s_es->s_last_snap_orphan =
1156 + ind_iloc.raw_inode->i_next_snap_orphan;
1157 + pri_ino = ind_iloc.raw_inode->i_snap_primary;
1159 + pri = iget( sb, pri_ino );
1161 + snap_err("snap_orphan_cleanup: get primary "
1162 + "%lu fail\n", pri_ino);
1165 + restore_snap_inode(pri, ind);
1166 + }while( ind_ino );
1167 + snap_debug("------ end cleanup snap orphans ------\n");
1169 + sb->u.ext3_sb.s_es->s_last_snap_orphan = 0;
1174 + * reserse operation of set_indirect()
1175 + * we should determine whether we had put pri into primary inode chain,
1176 + * if not, don't touch it
1178 +static void unset_indirect(handle_t *handle, struct inode *pri, struct inode *ind)
1180 + char buf[EXT3_MAX_SNAP_DATA];
1181 + struct snap_ea *snaps;
1182 + int err, alone=1, index, found;
1184 + snap_debug("pri %lu, ind %lu\n", pri->i_ino, ind->i_ino);
1185 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
1186 + EXT3_MAX_SNAP_DATA);
1188 + if( err == -ENOATTR ){
1189 + snap_debug("primary inode has not EA\n");
1192 + snap_debug("get EA error on primary inode,"
1193 + "returned value %d\n", err);
1198 + /* find ind's item in the ea */
1199 + snaps = (struct snap_ea*)buf;
1200 + for(index=EXT3_MAX_SNAPS-1, found=-1; index>=0; index--) {
1201 + if( snaps->ino[index] == ind->i_ino )
1203 + else if( snaps->ino[index] )
1208 + snap_debug("remove from primary inode's EA\n");
1209 + snaps->ino[found] = 0;
1210 + snaps->parent_ino[found] = 0;
1211 + ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1212 + buf, EXT3_MAX_SNAP_DATA, 0);
1214 + snap_debug("delete from primary inodes chain\n");
1215 + lock_list(pri->i_sb);
1216 + delete_cowed_ino_from_list(handle, pri);
1217 + unlock_list(pri->i_sb);
1220 + snap_debug("didn't found ind in pri's EA, do nothing\n");
1229 + * restore all data in @ind to @pri after free data blocks of @pri.
1230 + * then release @ind
1232 +static void restore_snap_inode(struct inode *pri, struct inode *ind)
1235 + struct inode *tmp;
1237 + snap_debug("restore from indirect %lu to primary %lu\n",
1238 + ind->i_ino, pri->i_ino);
1240 + handle = ext3_journal_start(pri, SNAP_RESTOREORPHAN_TRANS_BLOCKS);
1244 + /* first: taken from pri's ea, or from fs-wide primary inode chain */
1245 + unset_indirect(handle, pri, ind);
1247 + /* second: throw out half-copied data in pri */
1248 + if( pri->i_blocks ){
1249 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1251 + snap_debug("ext3_new_inode error\n");
1255 + ext3_migrate_data(handle, tmp, pri);
1256 + snap_debug("freeing half-copied %lu blocks\n", tmp->i_blocks );
1261 + /* third: restore ind inode to pri inode */
1262 + snap_debug("restore %lu blocks to primary inode %lu\n",
1263 + ind->i_blocks, pri->i_ino);
1264 + ext3_migrate_data(handle, pri, ind);
1266 + /* final: delete ind inode */
1272 + ext3_journal_stop(handle, pri);
1278 +static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
1279 + struct inode *src, int *has_orphan)
1281 + unsigned long blocks, blk, cur_blks;
1282 + int low_credits, save_ref;
1284 + blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
1285 + src->i_sb->s_blocksize_bits;
1286 + low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
1288 + snap_debug("%lu blocks need to be copied,"
1289 + "low credits limit %d\n", blocks, low_credits);
1290 + for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
1291 + if (!ext3_bmap(src->i_mapping, blk))
1293 + if(handle->h_buffer_credits <= low_credits) {
1294 + int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
1295 + if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
1296 + needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
1297 + if (journal_extend(handle, needed)) {
1298 + snap_debug("create_indirect:fail to extend "
1299 + "journal, restart trans\n");
1301 + if( !*has_orphan ){
1302 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1303 + add_snap_orphan(handle, dst, src);
1305 + ext3_orphan_add(handle, dst);
1309 + dst->u.ext3_i.i_disksize =
1310 + blk * dst->i_sb->s_blocksize;
1311 + dst->i_blocks = cur_blks;
1312 + dst->i_mtime = CURRENT_TIME;
1313 + ext3_mark_inode_dirty(handle, dst);
1316 + * We can be sure the last handle was stoped
1317 + * ONLY if the handle's reference count is 1
1319 + save_ref = handle->h_ref;
1320 + handle->h_ref = 1;
1321 + if( ext3_journal_stop(handle, dst) ){
1322 + snap_err("fail to stop journal\n");
1327 + handle = ext3_journal_start(dst,
1328 + low_credits + needed);
1330 + snap_err("fail to restart handle\n");
1333 + handle->h_ref = save_ref;
1336 + if (ext3_copy_block( dst, src, blk) < 0 )
1338 + cur_blks += dst->i_sb->s_blocksize / 512;
1340 + dst->i_size = dst->u.ext3_i.i_disksize = src->i_size;
1345 +static int ext3_set_generation(struct inode *inode, unsigned long gen)
1350 + handle = ext3_journal_start(inode, EXT3_XATTR_TRANS_BLOCKS);
1352 + err = ext3_xattr_set(handle, inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1353 + (char*)&gen, sizeof(int), 0);
1355 + snap_err("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
1359 + ext3_journal_stop(handle, inode);
1363 +static int ext3_get_generation(struct inode *inode)
1367 + err = ext3_xattr_get(inode, EXT3_SNAP_INDEX, EXT3_SNAP_GENERATION_ATTR,
1368 + (char*)&gen, sizeof(gen));
1370 + if (err == -ENODATA) {
1373 + snap_err("can not get generation from %lu \n", inode->i_ino);
1380 + * ext3_create_indirect - copy data, attributes from primary to new indir inode
1381 + * @pri: primary (source) inode
1382 + * @index: index in snapshot table where indirect inode should be stored
1383 + * @delete: flag that the primary inode is being deleted
1385 + * We copy all of the data blocks from the @*src inode to the @*dst inode, as
1386 + * well as copying the attributes from @*src to @*dst. If @delete == 1, then
1387 + * the primary inode will only be a redirector and will appear deleted.
1389 + * FIXME do we move EAs, only non-snap EAs, what?
1390 + * FIXME we could do readpage/writepage, but we would have to handle block
1391 + * allocation then, and it ruins sparse files for 1k/2k filesystems,
1392 + * at the expense of doing a memcpy.
1395 +static struct inode *ext3_create_indirect(
1396 + struct inode *pri,
1402 + struct inode *ind;
1403 + handle_t *handle = NULL;
1405 + int has_orphan = 0;
1407 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1408 + printk( KERN_EMERG "TRY TO COW JOUNRAL\n");
1411 + snap_debug("creating indirect inode for %lu at index %d, %s pri\n",
1412 + pri->i_ino, index, del ? "deleting" : "preserve");
1414 + ind = ext3_get_indirect(pri, NULL, index);
1418 + handle = ext3_journal_start(pri, SNAP_CREATEIND_TRANS_BLOCKS);
1421 + /* XXX ? We should pass an err argument to get_indirect and precisely
1422 + * detect the errors, for some errors, we should exit right away.
1425 + /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect,
1426 + * we just free the primary data blocks and mark this inode delete
1428 + if((del) && ind && !IS_ERR(ind)) {
1429 + struct inode *tmp;
1430 + /* for directory, we don't free the data blocks,
1431 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1433 + snap_debug("del==SNAP_DEL_PRI_WITH_IND && ind\n");
1434 + if(!S_ISDIR(pri->i_mode)) {
1435 + /*Here delete the data of that pri inode.
1436 + * FIXME later, should throw the blocks of
1437 + * primary inode directly
1439 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1441 + down(&tmp->i_sem);
1442 + ext3_migrate_data(handle, tmp, pri);
1448 + snap_err("ext3_new_inode error\n");
1453 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1454 + ext3_mark_inode_dirty(handle, pri);
1459 + if (ind && !IS_ERR(ind)) {
1460 + snap_err("existing indirect ino %lu for %lu: index %d\n",
1461 + ind->i_ino, pri->i_ino, index);
1465 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
1466 + ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
1472 + snap_debug("got new inode %lu\n", ind->i_ino);
1473 + ind->i_rdev = pri->i_rdev;
1474 + ind->i_op = pri->i_op;
1475 + ext3_set_generation(ind, (unsigned long)gen);
1476 + /* If we are deleting the primary inode, we want to ensure that it is
1477 + * written to disk with a non-zero link count, otherwise the next iget
1478 + * and iput will mark the inode as free (which we don't want, we want
1479 + * it to stay a redirector). We fix this in ext3_destroy_indirect()
1480 + * when the last indirect inode is removed.
1482 + * We then do what ext3_delete_inode() does so that the metadata will
1483 + * appear the same as a deleted inode, and we can detect it later.
1486 + snap_debug("deleting primary inode\n");
1488 + down(&ind->i_sem);
1489 + err = ext3_migrate_data(handle, ind, pri);
1493 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1497 + /* XXX for directory, we copy the block back
1498 + * or ext3_rmdir will report errors "bad dir, no data blocks"
1500 + if( S_ISDIR(pri->i_mode)) {
1501 + handle = ext3_copy_data( handle, pri, ind, &has_orphan );
1506 + pri->u.ext3_i.i_flags |= EXT3_DEL_FL;
1507 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1508 + if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
1509 + pri->u.ext3_i.i_dtime = CURRENT_TIME;
1510 + //pri->u.ext3_i.i_generation++;
1511 + ext3_mark_inode_dirty(handle, pri);
1512 + ext3_mark_inode_dirty(handle, ind);
1515 + down(&ind->i_sem);
1516 + err = ext3_migrate_data(handle, ind, pri);
1520 + /* for regular files we do blocklevel COW's maybe */
1521 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
1522 + && S_ISREG(pri->i_mode)) {
1524 + snap_debug("ino %lu, do block cow\n",pri->i_ino);
1525 + /* because after migrate_data , pri->i_size is 0 */
1526 + pri->i_size = ind->i_size;
1529 + int bpib = pri->i_sb->s_blocksize >> 9;
1530 + snap_debug("ino %lu, do file cow\n", pri->i_ino);
1532 + /* XXX: can we do this better?
1533 + * If it's a fast symlink, we should copy i_data back!
1534 + * The criteria to determine a fast symlink is:
1535 + * 1) it's a link and its i_blocks is 0
1536 + * 2) it's a link and its i_blocks is bpib ( the case
1537 + * it has been cowed and has ea )
1539 + if( S_ISLNK(ind->i_mode) &&
1540 + (( ind->i_blocks == 0) || (ext3_has_ea(ind) && ind->i_blocks == bpib )) ){
1541 + snap_debug("ino %lu is fast symlink\n",
1543 + memcpy(EXT3_I(pri)->i_data,
1544 + EXT3_I(ind)->i_data,
1545 + sizeof(EXT3_I(ind)->i_data));
1546 + pri->i_size = ind->i_size;
1549 + handle = ext3_copy_data(handle, pri, ind, &has_orphan);
1554 + /* set cow flag for ind */
1555 + ind->u.ext3_i.i_flags |= EXT3_COW_FL;
1556 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
1558 + ext3_mark_inode_dirty(handle, pri);
1559 + ext3_mark_inode_dirty(handle, ind);
1561 + err = ext3_set_indirect(pri, index, ind->i_ino, parent_ino);
1568 + if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
1569 + EXT3_FEATURE_COMPAT_SNAPFS)) {
1570 + lock_super(pri->i_sb);
1571 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
1572 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat |=
1573 + cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
1574 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
1575 + pri->i_sb->s_dirt = 1;
1576 + unlock_super(pri->i_sb);
1579 +#ifdef EXT3_ENABLE_SNAP_ORPHAN
1580 + remove_snap_orphan(handle, ind);
1582 + ext3_orphan_del(handle, ind);
1585 + ext3_journal_stop(handle, pri);
1596 + ext3_journal_stop(handle, pri);
1597 + snap_debug("exiting with error %d\n", err);
1602 +/* The following functions are used by destroy_indirect */
1603 +#define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
1604 +#define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
1606 +static inline int block_bmap (struct buffer_head * bh, int nr)
1612 + tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
1617 +static inline int block_setbmap (handle_t *handle, struct buffer_head * bh, int nr, int physical)
1622 + ext3_journal_get_write_access(handle, bh);
1623 + ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
1624 + ext3_journal_dirty_metadata(handle, bh);
1629 +static int ext3_migrate_block (handle_t *handle, struct inode * dst, struct inode *src, int block)
1631 + int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
1632 + int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
1633 + int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
1634 + unsigned long blksz = src->i_sb->s_blocksize;
1635 + kdev_t ddev = dst->i_dev;
1636 + kdev_t sdev = src->i_dev;
1640 + ext3_warning (src->i_sb, "ext3_migrate_block", "block < 0");
1643 + if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
1644 + (1 << (addr_per_block_bits * 2)) +
1645 + ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
1646 + ext3_warning (src->i_sb, "ext3_migrate_block", "block > big");
1649 + /* EXT3_NDIR_BLOCK */
1650 + if (block < EXT3_NDIR_BLOCKS) {
1651 + if( inode_bmap(dst, block) ) return 0;
1653 + if( (physical = inode_bmap(src, block)) ) {
1654 + inode_setbmap (dst, block, physical);
1655 + inode_setbmap (src, block, 0);
1662 + /* EXT3_IND_BLOCK */
1663 + block -= EXT3_NDIR_BLOCKS;
1664 + if (block < addr_per_block) {
1665 + i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1668 + physical = inode_bmap(src, EXT3_IND_BLOCK);
1670 + inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1671 + inode_setbmap (src, EXT3_IND_BLOCK, 0);
1677 + if( block_bmap (bread (ddev, i1_d, blksz), block ))
1680 + i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1681 + if( !i1_s) return 0;
1683 + physical = block_bmap ( bread (sdev, i1_s, blksz), block );
1686 + block_setbmap(handle, bread(ddev, i1_d, blksz),block,physical);
1687 + block_setbmap(handle, bread(sdev, i1_s, blksz), block, 0);
1693 + /* EXT3_DIND_BLOCK */
1694 + block -= addr_per_block;
1695 + if (block < (1 << (addr_per_block_bits * 2))) {
1696 + i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1697 + i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1699 + if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1700 + inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1701 + inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1707 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1708 + block >> addr_per_block_bits);
1712 + if( !i1_s) return 0;
1714 + physical = block_bmap (bread (sdev, i1_s, blksz),
1715 + block >> addr_per_block_bits);
1717 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1718 + block >> addr_per_block_bits, physical);
1719 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1720 + block >> addr_per_block_bits, 0);
1726 + physical = block_bmap (bread (ddev, i2_d,
1728 + block & (addr_per_block - 1));
1732 + i2_s = block_bmap (bread (sdev, i1_s,
1734 + block >> addr_per_block_bits);
1735 + if(!i2_s) return 0;
1737 + physical = block_bmap(bread (sdev, i2_s,
1739 + block & (addr_per_block - 1));
1741 + block_setbmap(handle, bread (ddev, i2_d, blksz),
1742 + block & (addr_per_block - 1), physical);
1743 + block_setbmap(handle, bread (sdev, i2_s, blksz),
1744 + block & (addr_per_block - 1), 0);
1752 + /* EXT3_TIND_BLOCK */
1753 + block -= (1 << (addr_per_block_bits * 2));
1754 + i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1755 + i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1757 + if( (physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1758 + inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1762 + i2_d = block_bmap (bread (ddev, i1_d, blksz),
1763 + block >> (addr_per_block_bits * 2));
1765 + if(i1_s) i2_s = block_bmap (bread (sdev, i1_s, blksz),
1766 + block >> (addr_per_block_bits * 2));
1770 + if( !i1_s) return 0;
1772 + physical = block_bmap (bread (sdev, i1_s, blksz),
1773 + block >> (addr_per_block_bits * 2));
1775 + block_setbmap (handle, bread (ddev, i1_d, blksz),
1776 + block >> (addr_per_block_bits * 2), physical);
1777 + block_setbmap (handle, bread (sdev, i1_s, blksz),
1778 + block >> (addr_per_block_bits * 2), 0);
1784 + i3_d = block_bmap (bread (ddev, i2_d, blksz),
1785 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1786 + if( i2_s) i3_s = block_bmap (bread (sdev, i2_s, blksz),
1787 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1790 + if (!i2_s) return 0;
1791 + physical = block_bmap (bread (sdev, i2_s, blksz),
1792 + (block >> addr_per_block_bits) & (addr_per_block - 1));
1794 + block_setbmap (handle, bread (ddev, i2_d, blksz),
1795 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1797 + block_setbmap (handle, bread (sdev, i2_s, blksz),
1798 + (block >> addr_per_block_bits) & (addr_per_block - 1),
1805 + physical = block_bmap (bread (ddev, i3_d, blksz),
1806 + block & (addr_per_block - 1)) ;
1807 + if(physical) return 0;
1809 + if(!i3_s) return 0;
1810 + physical = block_bmap (bread (sdev, i3_s, blksz),
1811 + block & (addr_per_block - 1)) ;
1813 + block_setbmap (handle, bread (ddev, i3_d, blksz),
1814 + block & (addr_per_block - 1), physical);
1815 + block_setbmap (handle, bread (sdev, i3_s, blksz),
1816 + block & (addr_per_block - 1), 0);
1824 +/* Generate i_blocks from blocks for an inode .
1825 + * We also calculate EA block here.
1827 +static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1829 + /* 512 byte disk blocks per inode block */
1830 + int bpib = inode->i_sb->s_blocksize >> 9;
1831 + int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1832 + unsigned long i_blocks = 0;
1835 + int meta_blocks = 0;
1837 + if( !inode ) return 0;
1839 + if( blocks < 0 ) {
1840 + /* re-calculate blocks here */
1841 + blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1842 + >> inode->i_sb->s_blocksize_bits;
1845 + /* calculate data blocks */
1846 + for(i = 0; i < blocks; i++ ) {
1847 + if(ext3_bmap(inode->i_mapping, i))
1850 + /* calculate meta blocks */
1851 + blocks -= EXT3_NDIR_BLOCKS;
1852 + if( blocks > 0 ) {
1854 + blocks -= addr_per_block;
1856 + if( blocks > 0 ) meta_blocks++;
1858 + while( (blocks > 0) && (i < addr_per_block) ) {
1860 + blocks -= addr_per_block;
1863 + if ( blocks > 0 ) meta_blocks += 2;
1866 + while( blocks > 0) {
1868 + blocks -= addr_per_block;
1870 + if(i >= addr_per_block ) {
1874 + if( j >= addr_per_block) {
1879 + /* calculate EA blocks */
1880 + if( ext3_has_ea (inode) ) meta_blocks++;
1882 + i_blocks += meta_blocks * bpib;
1883 + snap_debug("ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1888 + * ext3_destroy_indirect - delete an indirect inode from the table
1889 + * @pri: primary inode
1890 + * @ind: indirect inode
1891 + * @index: index of inode that should be deleted
1893 + * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1894 + * is NULL, we use the inode at @index.
1896 +static int ext3_destroy_indirect(struct inode *pri, int index,
1897 + struct inode *next_ind)
1899 + char buf[EXT3_MAX_SNAP_DATA];
1900 + struct snap_ea *snaps;
1901 + struct inode *ind;
1905 + handle_t *handle=NULL;
1908 + if (index < 0 || index > EXT3_MAX_SNAPS)
1911 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
1912 + printk( KERN_EMERG "TRY TO DESTROY JOURNAL'S IND\n");
1916 + handle = ext3_journal_start(pri, SNAP_DESTROY_TRANS_BLOCKS);
1920 + err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1921 + buf, EXT3_MAX_SNAP_DATA);
1923 + if (err == -ENODATA)
1924 + snap_err("inode %lu is not a redirector\n", pri->i_ino);
1926 + snap_err(EXT3_SNAP_ATTR " attribute read error\n");
1930 + snaps = (struct snap_ea *)buf;
1931 + if ( !snaps->ino[index] ) {
1932 + snap_err("for pri ino %lu, index %d, redirect ino is 0\n",
1933 + pri->i_ino, index);
1938 + snap_debug("for pri ino %lu, reading inode %lu at index %d\n",
1939 + pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1941 + ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]) );
1942 + snap_debug("iget ind %lu, ref count = %d\n", ind->i_ino, ind->i_count);
1944 + if ( !ind || IS_ERR(ind) || is_bad_inode(ind) ) {
1949 + /* if it's block level cow, first copy the blocks back */
1950 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1951 + S_ISREG(pri->i_mode)) {
1954 + if( !next_ind ) next_ind = pri;
1955 + blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1)
1956 + >> next_ind->i_sb->s_blocksize_bits;
1958 +#define FAST_MIGRATE_BLOCK
1959 +#ifdef FAST_MIGRATE_BLOCK
1960 + snap_debug("migrate block back from ino %lu to %lu\n",
1961 + ind->i_ino, next_ind->i_ino);
1963 + snap_double_lock(next_ind, ind);
1964 + for(i = 0; i < blocks; i++) {
1965 + if( ext3_bmap(next_ind->i_mapping, i) )
1967 + if( !ext3_bmap(ind->i_mapping, i) )
1969 + ext3_migrate_block(handle, next_ind, ind, i) ;
1971 + /* Now re-compute the i_blocks */
1972 + /* XXX shall we take care of ind here? probably not */
1973 + next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1974 + ext3_mark_inode_dirty(handle, next_ind);
1976 + snap_double_unlock(next_ind, ind);
1979 + snap_double_lock(pri, ind);
1981 + for(i = 0; i < blocks; i++) {
1982 + if( ext3_bmap(pri, i) ) continue;
1983 + if( !ext3_bmap(ind, i) ) continue;
1984 + ext3_migrate_block( pri, ind, i) ;
1986 + /* Now re-compute the i_blocks */
1987 + /* XXX shall we take care of ind here? probably not */
1988 + pri->i_blocks = calculate_i_blocks( pri, blocks);
1989 + mark_inode_dirty(pri);
1991 + double_unlock(pri, ind);
1994 + snap_double_lock(next_ind, ind);
1995 + for (i = 0; i < blocks; i++) {
1996 + if (ext3_bmap (next_ind->i_mapping, i) )
1998 + if (ext3_copy_block (next_ind, ind, i ) < 0) break;
2000 + ext3_mark_inode_dirty(handle, next_ind);
2001 + double_unlock(next_ind, ind);
2006 + snap_debug("delete indirect ino %lu\n", ind->i_ino);
2007 + snap_debug("iput ind %lu, ref count = %d\n", ind->i_ino, ind->i_count);
2011 + snaps->ino[index] = cpu_to_le32(0);
2012 + for (i = 0; i < EXT3_MAX_SNAPS; i++)
2013 + save += snaps->ino[i];
2016 + lock_list(pri->i_sb);
2017 + delete_cowed_ino_from_list(handle, pri);
2018 + unlock_list(pri->i_sb);
2021 + /* if there are no cowed inode left, then remove snapfs feature */
2022 + if(!SB_FIRST_COWED_INO(pri->i_sb)) {
2024 + lock_super(pri->i_sb);
2026 + ext3_journal_get_write_access(handle, pri->i_sb->u.ext3_sb.s_sbh);
2027 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2028 + EXT3_FEATURE_COMPAT_SNAPFS)) {
2029 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2030 + cpu_to_le32(~EXT3_FEATURE_COMPAT_SNAPFS);
2032 + /* clean up block level cow feature */
2033 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2034 + EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2035 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2036 + cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2038 + /* XXX clean the extended attribute feature,
2039 + * this is not safe, find a better way
2041 + if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
2042 + EXT3_FEATURE_COMPAT_EXT_ATTR)) {
2043 + pri->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2044 + cpu_to_le32(~EXT3_FEATURE_COMPAT_EXT_ATTR);
2047 + ext3_journal_dirty_metadata(handle, pri->i_sb->u.ext3_sb.s_sbh);
2048 + pri->i_sb->s_dirt = 1;
2049 + unlock_super(pri->i_sb);
2053 + * If we are deleting the last indirect inode, and the primary inode
2054 + * has already been deleted, then mark the primary for deletion also.
2055 + * Otherwise, if we are deleting the last indirect inode remove the
2056 + * snaptable from the inode. XXX
2058 + if (!save && pri->u.ext3_i.i_dtime) {
2059 + snap_debug("deleting primary %lu\n", pri->i_ino);
2061 + /* reset err to 0 now */
2064 + snap_debug("%s redirector table\n",
2065 + save ? "saving" : "deleting");
2066 + /* XXX: since set ea will modify i_ctime of pri,
2067 + so save/restore i_ctime. Need this necessary ? */
2068 + ctime = pri->i_ctime;
2069 + err = ext3_xattr_set(handle, pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2070 + save ? buf : NULL, EXT3_MAX_SNAP_DATA, 0);
2071 + pri->i_ctime = ctime;
2072 + ext3_mark_inode_dirty(handle, pri);
2075 + ext3_journal_stop(handle, pri);
2079 +/* restore a primary inode with the indirect inode at index */
2080 +static int ext3_restore_indirect(struct inode *pri, int index)
2082 + struct inode *ind;
2083 + struct inode *tmp;
2085 + handle_t *handle = NULL;
2087 + if (index < 0 || index > EXT3_MAX_SNAPS)
2090 + if( pri == pri->i_sb->u.ext3_sb.s_journal_inode ){
2091 + printk( KERN_EMERG "TRY TO RESTORE JOURNAL\n");
2094 + snap_debug("pri ino %lu, index %d\n", pri->i_ino, index);
2096 + ind = ext3_get_indirect(pri, NULL, index);
2101 + snap_debug("restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
2103 + handle = ext3_journal_start(pri, SNAP_RESTORE_TRANS_BLOCKS);
2106 + /* first destroy all the data blocks in primary inode */
2107 + /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
2108 + tmp = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
2110 + snap_double_lock(pri, tmp);
2111 + ext3_migrate_data(handle, tmp, pri);
2112 + snap_double_unlock(pri, tmp);
2118 + snap_err("restore_indirect, new_inode err\n");
2120 + snap_double_lock(pri, ind);
2121 + ext3_migrate_data(handle, pri, ind);
2122 + /* clear the cow flag for pri because ind has it */
2123 + pri->u.ext3_i.i_flags &= ~EXT3_COW_FL;
2124 + ext3_mark_inode_dirty(handle, pri);
2125 + snap_double_unlock(pri, ind);
2129 +// ext3_destroy_indirect(pri, index);
2131 + ext3_journal_stop(handle, pri);
2137 + * ext3_snap_iterate - iterate through all of the inodes
2138 + * @sb: filesystem superblock
2139 + * @repeat: pointer to function called on each valid inode
2140 + * @start: inode to start iterating at
2141 + * @priv: private data to the caller/repeat function
2143 + * If @start is NULL, then we do not return an inode pointer. If @*start is
2144 + * NULL, then we start at the beginning of the filesystem, and iterate over
2145 + * all of the inodes in the system. If @*start is non-NULL, then we start
2146 + * iterating at this inode.
2148 + * We call the repeat function for each inode that is in use. The repeat
2149 + * function must check if this is a redirector (with is_redirector) if it
2150 + * only wants to operate on redirector inodes. If there is an error or
2151 + * the repeat function returns non-zero, we return the last inode operated
2152 + * on in the @*start parameter. This allows the caller to restart the
2153 + * iteration at this inode if desired, by returning a positive value.
2154 + * Negative return values indicate an error.
2156 + * NOTE we cannot simply traverse the existing filesystem tree from the root
2157 + * inode, as there may be disconnected trees from deleted files/dirs
2159 + * FIXME If there was a list of inodes with EAs, we could simply walk the list
2160 + * intead of reading every inode. This is an internal implementation issue.
2163 +static int ext3_iterate_all(struct super_block *sb,
2164 + int (*repeat)(struct inode *inode, void *priv),
2165 + struct inode **start, void *priv)
2167 + struct inode *tmp = NULL;
2169 + ino_t istart, ibase;
2175 + *start = iget(sb, EXT3_ROOT_INO);
2180 + if (is_bad_inode(*start)) {
2185 + if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
2186 + snap_debug("invalid starting inode %ld\n",(*start)->i_ino);
2190 + if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
2191 + if ((err = (*repeat)(*start, priv) != 0))
2194 + *start = iget(sb, EXT3_FIRST_INO(sb));
2199 + if (is_bad_inode(*start)) {
2205 + gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
2206 + istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
2207 + ibase = gstart * EXT3_INODES_PER_GROUP(sb);
2208 + for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
2209 + gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
2210 + struct ext3_group_desc * gdp;
2215 + gdp = ext3_get_group_desc (sb, gnum, NULL);
2216 + if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
2217 + EXT3_INODES_PER_GROUP(sb))
2220 + bitmap_nr = ext3_load_inode_bitmap(sb, gnum);
2221 + if (bitmap_nr < 0)
2224 + bitmap = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr]->b_data;
2225 + for (ibyte = istart >> 3;
2226 + ibyte < EXT3_INODES_PER_GROUP(sb) >> 3;
2232 + if (!bitmap[ibyte])
2235 + /* FIXME need to verify if bit endianness will
2236 + * work properly here for all architectures.
2238 + for (i = 1, bit = 1; i <= 8; i++, bit <<= 1) {
2239 + ino_t ino = ibase + (ibyte << 3) + i;
2241 + if ((bitmap[ibyte] & bit) == 0)
2244 + if (ino < (*start)->i_ino)
2247 + *start = iget(sb, ino);
2252 + if (is_bad_inode(*start)) {
2257 + if ((err = (*repeat)(*start, priv)) != 0)
2270 +static int ext3_iterate(struct super_block *sb,
2271 + int (*repeat)(struct inode *inode, void *priv),
2272 + struct inode **start, void *priv, int flag)
2275 + case SNAP_ITERATE_ALL_INODE:
2276 + return ext3_iterate_all (sb, repeat, start, priv);
2278 + case SNAP_ITERATE_COWED_INODE:
2279 + return ext3_iterate_cowed_inode (sb, repeat, start,priv);
2287 +static int find_snap_meta_index(
2288 + struct table_snap_meta_data *snap_meta,
2293 + /* table max length is null*/
2294 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2295 + /*compare name Max name Length 15*/
2296 + if (snap_meta->array[i].name[0]){
2297 + if(strcmp(snap_meta->array[i].name, name))
2302 + return -1; /* can not find */
2305 +int set_snap_meta_index(
2306 + struct table_snap_meta_data *snap_meta,
2312 + for( i = 0; i < TABLE_ITEM_COUNT; i++){
2313 + /*compare name Max name Length 15*/
2314 + if (! snap_meta->array[i].name[0]){
2315 + strcpy(snap_meta->array[i].name, name);
2316 + snap_meta->count ++;
2317 + snap_meta->array[i].start = i * TABLE_ITEM_SIZE + 1;
2318 + snap_meta->array[i].len = size;
2322 + return -1; /* can not find */
2325 +static int ext3_get_meta_attr(struct super_block *sb,
2326 + char* name, char* buf,
2330 + struct inode *inode;
2331 + struct buffer_head *bh = NULL;
2332 + struct table_snap_meta_data *s_attr;
2333 + unsigned long map_len = 0, index = 0, left_size;
2336 + ino = SB_SNAPTABLE_INO(sb);
2338 + snap_err("No table file \n");
2341 + inode = iget(sb, ino);
2342 + if(!inode || is_bad_inode(inode)){
2343 + snap_err("unable to get table ino %lu\n", ino);
2347 + /*read the table from the table inode*/
2348 + bh = ext3_bread(NULL, inode, 0, 0, &error);
2350 + snap_err("read table ino %lu, error %d\n", ino, error);
2354 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2355 + index = find_snap_meta_index(s_attr, name);
2357 + snap_debug("not exit %s meta attr of table ino %llu \n",
2362 + if (!buf || *size < s_attr->array[index].len) {
2363 + /*return the size of this meta attr */
2364 + error = s_attr->array[index].len;
2367 + map_len = (s_attr->array[index].len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2368 + left_size = *size;
2369 + for(i = 0; i < map_len; i++) {
2370 + struct buffer_head *array_bh = NULL;
2372 + array_bh = ext3_bread(NULL, inode,
2373 + s_attr->array[index].start + i,
2376 + snap_err("ino %lu read snap attr offset %d error %d \n",
2377 + inode->i_ino, (s_attr->array[index].start + i),
2381 + if (left_size >= sb->s_blocksize) {
2382 + memcpy(buf, array_bh->b_data, sb->s_blocksize);
2384 + memcpy(buf, array_bh->b_data, left_size);
2385 + left_size -= sb->s_blocksize;
2388 + *size = s_attr->array[index].len;
2395 +static int ext3_set_meta_attr(struct super_block *sb, char* name,
2396 + char* buf, int size)
2398 + struct inode *inode = NULL;
2399 + handle_t *handle = NULL;
2400 + struct buffer_head *bh = NULL;
2401 + struct table_snap_meta_data *s_attr = NULL;
2402 + unsigned long ino;
2403 + int i, index = 0, error = 0;
2404 + unsigned long new_len = 0, left_size;
2406 + ino = SB_SNAPTABLE_INO(sb);
2408 + if (ino == 0 && !buf) {
2409 + snap_debug("no table ino \n");
2413 + handle = ext3_journal_start(sb->s_root->d_inode, 2*EXT3_SETMETA_TRANS_BLOCKS);
2418 + /*create table inode update table ino*/
2419 + inode = ext3_new_inode(handle, sb->s_root->d_inode, (int)S_IFREG, 0);
2423 + ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
2424 + SB_SNAPTABLE_INO(sb) = inode->i_ino;
2425 + ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
2430 + inode = iget(sb, ino);
2431 + if (!inode || !inode->i_nlink || is_bad_inode(inode)) {
2432 + snap_err("unable to get table ino %lu\n", ino);
2437 + /*read the table from the table inode,
2438 + * If can not find the block just create it*/
2439 + bh = ext3_bread(handle, inode, 0, 1, &error);
2441 + snap_err("read table ino %lu, error %d\n", ino, error);
2445 + s_attr = (struct table_snap_meta_data *)(bh->b_data);
2446 + index = find_snap_meta_index(s_attr, name);
2447 + if (index < 0 && !buf) {
2448 + snap_debug("%s meta attr of table ino %llu do not exist\n",
2449 + name, inode->i_ino);
2454 + snap_debug("delete the meta attr %s in the table ino %s",
2455 + *name, inode->ino);
2456 + /*Here we only delete the entry of the attr
2457 + *FIXME, should we also delete the block of
2460 + ext3_journal_get_write_access(handle, bh);
2461 + memset(s_attr->array[index].name, 0, TABLE_ITEM_NAME_SIZE);
2462 + s_attr->array[index].len = 0;
2464 + ext3_journal_dirty_metadata(handle, bh);
2467 + new_len = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
2468 + /*find the place to put this attr in that index*/
2469 + ext3_journal_get_write_access(handle, bh);
2471 + index = set_snap_meta_index(s_attr, name, size);
2473 + snap_err("table full of ino %lu \n", inode->i_ino);
2478 + s_attr->array[index].len = size;
2479 + journal_dirty_metadata(handle, bh);
2480 + /*put this attr to the snap table*/
2482 + for(i = 0; i < new_len; i++) {
2483 + struct buffer_head *array_bh = NULL;
2485 + array_bh = ext3_bread(handle, inode,
2486 + s_attr->array[index].start + i, 1, &error);
2488 + snap_err("inode %lu Can not get the block of attr %s\n",
2489 + inode->i_ino, name);
2493 + ext3_journal_get_write_access(handle, array_bh);
2494 + if (left_size > inode->i_sb->s_blocksize)
2495 + memcpy(array_bh->b_data, buf, inode->i_sb->s_blocksize);
2497 + memcpy(array_bh->b_data, buf, left_size);
2498 + ext3_journal_dirty_metadata(handle, array_bh);
2499 + left_size -= inode->i_sb->s_blocksize;
2502 + ext3_journal_stop(handle, sb->s_root->d_inode);
2509 +static int ext3_get_meta_attr(struct super_block *sb,
2510 + char* name, char* buf,
2513 + struct inode *root_inode = sb->s_root->d_inode;
2520 + err = ext3_xattr_get(root_inode, EXT3_SNAP_INDEX, name,
2522 + if (err == -ERANGE || !buf){
2523 + /*get the size of the buf*/
2524 + *size = ext3_xattr_get(root_inode, EXT3_SNAP_INDEX, name,
2531 + * set the meta info of the snap system
2533 + * buf == NULL delete "name" meta attr
2534 + * != NULL set "name" meta attr
2540 +static int ext3_set_meta_attr(struct super_block *sb, char* name,
2541 + char* buf, int size)
2543 + struct inode *root_inode = sb->s_root->d_inode;
2544 + handle_t *handle = NULL;
2548 + handle = ext3_journal_start(root_inode, EXT3_XATTR_TRANS_BLOCKS);
2552 + error = ext3_xattr_set(handle, root_inode, EXT3_SNAP_INDEX, name, buf, size, 0);
2554 + ext3_journal_stop(handle, root_inode);
2560 +struct snapshot_operations ext3_snap_operations = {
2561 + ops_version: SNAP_VERSION(2,0,2),
2562 + is_redirector: is_redirector,
2563 + is_indirect: is_indirect,
2564 + create_indirect: ext3_create_indirect,
2565 + get_indirect: ext3_get_indirect,
2566 + get_indirect_ino: ext3_get_indirect_ino,
2567 + destroy_indirect: ext3_destroy_indirect,
2568 + restore_indirect: ext3_restore_indirect,
2569 + iterate: ext3_iterate,
2570 + copy_block: ext3_copy_block,
2571 + set_indirect: ext3_set_indirect,
2572 + snap_feature: ext3_snap_feature,
2573 + get_generation: ext3_get_generation,
2574 + set_generation: ext3_set_generation,
2575 + get_meta_attr: ext3_get_meta_attr,
2576 + set_meta_attr: ext3_set_meta_attr,
2579 +EXPORT_SYMBOL(ext3_snap_operations);
2580 +#ifdef SNAP_PROFILE
2581 +EXPORT_SYMBOL(prof_snapdel);
2584 +#ifdef SNAP_DEBUG_IOC
2586 +static int print_inode(struct inode *pri, void *index_val)
2590 + struct snap_ea *snaps;
2591 + char buf[EXT3_MAX_SNAP_DATA];
2592 + int index = *(int *)index_val;
2594 + err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
2595 + buf, EXT3_MAX_SNAP_DATA);
2597 + if (err == -ENODATA) {
2598 + memset(buf, 0, EXT3_MAX_SNAP_DATA);
2600 + else if (err < 0) {
2601 + snap_err("got err %d when reading attributes\n", err);
2605 + snaps = (struct snap_ea *) buf;
2607 + if( le32_to_cpu(snaps->ino[index]) == 0 ) {
2608 + snap_debug("no redirected ino for primary inode %lu\n",
2612 + snap_debug("primary inode %lu , redirected ino=%d\n",
2613 + primary->i_ino,le32_to_cpu(snaps->ino[index]));
2619 +int snap_print(struct super_block *sb, int index)
2621 + ext3_iterate_cowed_inode(sb, &print_inode, NULL, &index);
2625 +static int ext3_snap_destroy_inode(struct inode *primary,void *index_val)
2627 + int index = *(int *)index_val;
2629 + printk("delete_inode for index %d\n",index);
2630 + rc = ext3_destroy_indirect(primary,index, NULL);
2632 + printk("ERROR:ext3_destroy_indirect(ino %lu,index %d),ret %d\n",
2633 + primary->i_ino, index, rc);
2637 +int ext3_snap_delete(struct super_block *sb, int index)
2639 + ext3_iterate(sb, &ext3_snap_destroy_inode, NULL, &index,
2640 + SNAP_ITERATE_COWED_INODE);
2652 Index: linux-2.4.20-8/fs/ext3/ioctl.c
2653 ===================================================================
2654 --- linux-2.4.20-8.orig/fs/ext3/ioctl.c 2004-01-05 10:54:00.000000000 +0800
2655 +++ linux-2.4.20-8/fs/ext3/ioctl.c 2004-01-13 00:10:14.000000000 +0800
2657 #include <linux/ext3_jbd.h>
2658 #include <linux/sched.h>
2659 #include <asm/uaccess.h>
2660 +#include <linux/locks.h>
2662 +#include <linux/snap.h>
2663 +extern struct snapshot_operations ext3_snap_operations;
2664 +extern int ext3_snap_print(struct super_block *sb, int index);
2665 +extern int ext3_snap_delete(struct super_block *sb, int index);
2668 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
2669 @@ -189,6 +195,103 @@
2674 + case EXT3_IOC_SNAP_SETFILECOW: {
2675 + printk(KERN_INFO "set file cow on dev %x\n",inode->i_dev);
2677 + /* clear block cow feature*/
2678 + if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
2679 + EXT3_FEATURE_COMPAT_BLOCKCOW)) {
2680 + handle_t *handle = ext3_journal_start(inode, 1);
2682 + if (EXT3_HAS_COMPAT_FEATURE(inode->i_sb,
2683 + EXT3_FEATURE_COMPAT_SNAPFS)) {
2684 + printk(KERN_INFO "can't change cow level while snapfs feature exist");
2687 + lock_super(inode->i_sb);
2688 + journal_get_write_access(handle, EXT3_SB(inode->i_sb)->s_sbh);
2689 + inode->i_sb->u.ext3_sb.s_es->s_feature_compat &=
2690 + cpu_to_le32(~EXT3_FEATURE_COMPAT_BLOCKCOW);
2691 + inode->i_sb->s_dirt = 1;
2692 + journal_dirty_metadata(handle, EXT3_SB(inode->i_sb)->s_sbh);
2693 + unlock_super(inode->i_sb);
2694 + ext3_journal_stop(handle, inode);
2698 + case EXT3_IOC_CREATE_INDIR: {
2699 + struct inode *ind;
2700 + printk(KERN_INFO "create indirect on inode %lu\n",inode->i_ino);
2701 + ind = ext3_snap_operations.create_indirect(inode, 0, 1, 0);
2702 + if (!ind || IS_ERR(ind))
2703 + return PTR_ERR(ind);
2704 + printk(KERN_INFO "got indirect inode %lu\n",ind->i_ino);
2705 + put_user(ind->i_ino,(int *) arg);
2709 + case EXT3_IOC_GET_INDIR: {
2710 + struct inode *ind;
2712 + if (get_user(index, (int *) arg))
2715 + printk(KERN_INFO "get indirect on inode %lu, index %d\n",
2716 + inode->i_ino, index);
2717 + ind = ext3_snap_operations.get_indirect(inode, NULL, index);
2718 + if (!ind || IS_ERR(ind)) {
2719 + put_user(0,(int *) arg);
2720 + return PTR_ERR(ind);
2722 + printk(KERN_INFO "got indirect inode %lu for index %d\n",
2723 + ind->i_ino, index);
2724 + put_user(ind->i_ino,(int *) arg);
2728 + case EXT3_IOC_IS_REDIR: {
2729 + int is_redirector = 0;
2730 + printk(KERN_INFO "checking if inode %lu is redirector via\n",
2732 + is_redirector = ext3_snap_operations.is_redirector(inode);
2733 + printk(KERN_INFO "redirector: %s\n",is_redirector ? "yes":"no");
2734 + put_user(is_redirector,(int *) arg);
2738 + case EXT3_IOC_RESTORE_INDIR: {
2739 + printk(KERN_INFO "restore indirect on inode %lu\n",inode->i_ino);
2740 + return ext3_snap_operations.restore_indirect(inode, 1);
2742 + case EXT3_IOC_SNAP_PRINT: {
2744 + if (get_user(index, (int *) arg))
2746 + printk(KERN_INFO "print snap for index %d\n",index);
2748 + return ext3_snap_print(inode->i_sb, 1);
2750 + case EXT3_IOC_SNAP_DELETE: {
2752 + if (get_user(index, (int *) arg))
2755 + // XXX: debug code , always set index = 1
2756 + if(index !=1) index=1;
2757 + printk(KERN_INFO "delete all cowed inode for index %d\n",index);
2758 + return ext3_snap_delete(inode->i_sb, index);
2761 + case EXT3_IOC_DESTROY_INDIR: {
2763 + if (get_user(index, (int *) arg))
2765 + printk(KERN_INFO "destroy indirect on ino %lu, index %d\n",
2766 + inode->i_ino, index);
2767 + return ext3_snap_operations.destroy_indirect(inode, index, NULL);
2773 Index: linux-2.4.20-8/fs/ext3/Makefile
2774 ===================================================================
2775 --- linux-2.4.20-8.orig/fs/ext3/Makefile 2004-01-05 10:54:03.000000000 +0800
2776 +++ linux-2.4.20-8/fs/ext3/Makefile 2004-01-13 00:10:14.000000000 +0800
2779 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2780 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2782 + xattr_trusted.o snap.o
2783 obj-m := $(O_TARGET)
2785 export-objs += xattr.o
2786 Index: linux-2.4.20-8/fs/ext3/inode.c
2787 ===================================================================
2788 --- linux-2.4.20-8.orig/fs/ext3/inode.c 2004-01-05 10:54:03.000000000 +0800
2789 +++ linux-2.4.20-8/fs/ext3/inode.c 2004-01-13 00:10:14.000000000 +0800
2790 @@ -1191,7 +1191,7 @@
2791 * So, if we see any bmap calls here on a modified, data-journaled file,
2792 * take extra steps to flush any blocks which might be in the cache.
2794 -static int ext3_bmap(struct address_space *mapping, long block)
2795 +int ext3_bmap(struct address_space *mapping, long block)
2797 struct inode *inode = mapping->host;
2799 Index: linux-2.4.20-8/fs/ext3/ialloc.c
2800 ===================================================================
2801 --- linux-2.4.20-8.orig/fs/ext3/ialloc.c 2004-01-05 10:54:03.000000000 +0800
2802 +++ linux-2.4.20-8/fs/ext3/ialloc.c 2004-01-13 00:10:14.000000000 +0800
2804 sb->u.ext3_sb.s_inode_bitmap[bitmap_nr] = bh;
2809 * load_inode_bitmap loads the inode bitmap for a blocks group
2811 @@ -160,6 +159,13 @@
2815 +/* Export load_inode_bitmap*/
2816 +int ext3_load_inode_bitmap (struct super_block * sb,
2817 + unsigned int block_group)
2819 + return load_inode_bitmap(sb, block_group);
2823 * NOTE! When we get the inode, we're the only people
2824 * that have access to it, and as such there are no
2825 Index: linux-2.4.20-8/include/linux/snap.h
2826 ===================================================================
2827 --- linux-2.4.20-8.orig/include/linux/snap.h 2003-01-30 18:24:37.000000000 +0800
2828 +++ linux-2.4.20-8/include/linux/snap.h 2004-01-13 00:10:14.000000000 +0800
2831 + * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
2832 + * started by Andreas Dilger <adilger@turbolinux.com>
2833 + * Peter Braam <braam@mountainviewdata.com>
2834 + * Harrison Xing <harrisonx@mountainviewdata.com>
2836 + * Redesigned 2003 by Peter Braam <braam@clusterfs.com>
2837 + * Eric Mei <Ericm@clusterfs.com>
2838 + * Wang Di <wangdi@clusterfs.com>
2840 + * Rewriten 2003 by Wang Di <wangdi@clusterfs.com>
2841 + * Eric Mei <ericm@clusterfs.com>
2843 + * Functions for implementing snapshots in the ext3 filesystem. They are
2844 + * intended to hide the internals of the filesystem from the caller in
2845 + * such a way that the caller doesn't need to know about inode numbers,
2846 + * how the redirectors are implemented or stored, etc. It may not do that
2847 + * all yet, but it tries.
2849 + * The snapshot inode redirection is stored in the primary/direct inode as
2850 + * an extended attribute $snap, in the form of little-endian u32 inode
2855 +#ifndef _LINUX_SNAP_H
2856 +#define _LINUX_SNAP_H
2858 +#include <linux/fs.h>
2860 +/* maximum number of snapshots available for users */
2861 +#define MAX_SNAPS 20
2863 +/* snap extended attributes definition */
2864 +#define SNAP_ATTR "@snap"
2869 + ino_t ino[MAX_SNAPS+1]; /* including current snapshot */
2870 + ino_t parent_ino[MAX_SNAPS+1];
2872 +#define MAX_SNAP_DATA (sizeof(struct snap_ea))
2874 +/* for compatibility with old 128 max snapshots */
2875 +#define MAX_SNAP128_DATA (sizeof(struct snap_ea) - (sizeof(ino_t) * 128 * 2))
2876 +#define ZERO_SNAP_ATTR_TOP(buf) \
2878 + struct snap_ea *p = (struct snap_ea*)buf; \
2879 + memset(&p->ino[129], 0, sizeof(ino_t)*128); \
2880 + memset(&p->parent_ino[129], 0, sizeof(ino_t)*128); \
2883 +/* snap new ea definition , for logging of new inode */
2884 +#define SNAP_NEW_INO_ATTR "@snap_new"
2885 +struct snap_new_ea{
2886 + ino_t prev_ino; /* reserved. save the inode to a linked list */
2888 + int new_index; /* indicate for which index this is a new inode */
2890 +#define NULL_NEW_INDEX -1 /* null new index, to clear the snap_new_ea */
2892 +/* ea to identiry a indirect inode's infomation */
2893 +#define SNAP_INDIRECT_INFO_ATTR "@snap_indirect_inode_info"
2894 +struct snap_indirect_info {
2895 + __u32 index; /* which index belongs to */
2896 + __u32 reserved[3]; /* reserved */
2900 +/* snapfs meta data stored in extended attributes of root ino */
2901 +#define DISK_SNAP_META_ATTR "@disk_snap_meta_attr"
2902 +struct disk_snap_meta_data {
2903 + ino_t snap_first_cowed_ino;
2904 + ino_t snap_table_ino;
2905 + __u32 snap_feature_compat;
2907 +/*snapfs quota info */
2909 +#define SNAP_USR_QUOTA 0
2910 +#define SNAP_GRP_QUOTA 1
2911 +#define DISK_SNAP_QUOTA_INFO "@disk_snap_quota_info"
2912 +struct quota_info_len {
2913 + int uid_len; /*uid quota info length */
2914 + int gid_len; /*gid quota info length */
2917 + * Check if the EA @name is Snap EA or not.
2918 + * Snap EA includes the SNAP_ATTR, SNAP_NEW_INO_ATTR and DISK_SNAP_META_ATTR
2921 +#define IS_SNAP_EA(name) ( (!strcmp((name), SNAP_ATTR)) || \
2922 + (!strcmp((name), DISK_SNAP_META_ATTR)))
2926 +/* file system features */
2927 +#define SNAP_FEATURE_COMPAT_SNAPFS 0x0010
2928 +#define SNAP_FEATURE_COMPAT_BLOCKCOW 0x0020
2930 +/* constants for snap_feature operations */
2931 +#define SNAP_CLEAR_FEATURE 0x0
2932 +#define SNAP_SET_FEATURE 0x1
2933 +#define SNAP_HAS_FEATURE 0x2
2935 +/* snap flags for inode, within 1 byte range, each occupy 1 bit */
2936 +#define SNAP_INO_MAGIC 0x88 /* magic for snap inode */
2937 +#define SNAP_COW_FLAG 0x01 /* snap redirected inode */
2938 +#define SNAP_DEL_FLAG 0x02 /* snap deleted inode */
2939 +#define SNAP_TABLE_FLAG 0x04 /* snap table inode */
2940 +#define SNAP_PRI_FLAG 0x08 /* primary inode */
2942 +/* no snapfs attributes for get_indirect_ino */
2943 +#define ENOSNAPATTR 320
2945 +/* constants used by iterator */
2946 +#define SNAP_ITERATE_ALL_INODE 0x0
2947 +#define SNAP_ITERATE_COWED_INODE 0x1
2949 +/* constants used by create_indirect */
2950 +#define SNAP_CREATE_IND_NORMAL 0x0
2951 +#define SNAP_CREATE_IND_DEL_PRI 0x1
2953 +/* the data structure represent in the xfs_dinode.pad
2954 + offset 0: magic (1 byte)
2955 + offset 1: flag (1 byte)
2956 + offset 2: gen (4 bytes)
2959 +#define SIZEOF_MAGIC 1
2960 +#define SIZEOF_FLAG 1
2961 +#define SIZEOF_GENERATION 4
2963 +#define MAGIC_OFFSET 0
2964 +#define FLAG_OFFSET 1
2965 +#define GENERATION_OFFSET 2
2967 +#define SNAP_GET_DINODE_MAGIC(dinode) \
2968 + (((__u8*)(dinode)->di_pad)[MAGIC_OFFSET])
2969 +#define SNAP_SET_DINODE_MAGIC(dinode) \
2970 + ((__u8*)(dinode)->di_pad)[MAGIC_OFFSET] = (SNAP_INO_MAGIC)
2971 +#define SNAP_GET_DINODE_FLAG(dinode) \
2972 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET])
2973 +#define SNAP_SET_DINODE_FLAG(dinode, flag) \
2974 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] |= (flag))
2975 +#define SNAP_CLEAR_DINODE_FLAG(dinode, flag) \
2976 + (((__u8*)(dinode)->di_pad)[FLAG_OFFSET] &= ~(flag))
2977 +#define SNAP_GET_DINODE_GEN(dinode) \
2978 + (le32_to_cpu(*(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET])))
2979 +#define SNAP_SET_DINODE_GEN(dinode, gen) \
2980 + *(__u32*)(&((__u8*)(dinode)->di_pad)[GENERATION_OFFSET]) = cpu_to_le32(gen)
2983 +/* header of saving snaptable */
2985 + unsigned int size; /* buffer size passed by */
2986 + char data[0]; /* followed by actual data */
2989 +/* header of on-disk table data */
2990 +struct disk_snap_table_header {
2996 +/* table magic and version constant */
2997 +#define SNAP_TABLE_MAGIC 0xB3A2957F
2998 +#define SNAP_TABLE_VERSION 1
3001 +#define SNAPTABLE_BLOCKS(sb,size) \
3002 + (((size-sizeof(__u32)+sizeof(struct disk_snap_table_header)) \
3003 + >> sb->s_blocksize_bits)+1)
3006 +#define SNAP_VERSION(a,b,c) \
3007 + (((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF))
3008 +#define SNAP_VERSION_MAJOR(v) \
3009 + ((v >> 16) & 0xFF)
3010 +#define SNAP_VERSION_MINOR(v) \
3012 +#define SNAP_VERSION_REL(v) \
3015 +/* for snap meta attr table */
3016 +#define TABLE_ITEM_COUNT 200
3017 +#define TABLE_ITEM_SIZE 1000
3018 +#define TABLE_ITEM_NAME_SIZE 16
3020 +/*snap table array */
3021 +struct snap_meta_array {
3022 + char name[TABLE_ITEM_NAME_SIZE];
3023 + int start; /* where is the start of the array */
3024 + int len; /* the len of the array */
3026 +/* snap table structure for record the information */
3027 +struct table_snap_meta_data {
3029 + struct snap_meta_array array[TABLE_ITEM_COUNT];
3034 +#define SNAP_PROFILE
3036 +#undef SNAP_PROFILE
3039 +#ifdef SNAP_PROFILE
3040 +struct profile_snapdel_stat
3042 + unsigned long total_tick; /* total time */
3043 + unsigned long inodes; /* primary inodes */
3045 + unsigned long yield_count; /* for yeild cpu */
3046 + unsigned long yield_tick;
3047 + unsigned long yield_max_tick;
3049 + unsigned long getea_count; /* for get ea */
3050 + unsigned long getea_tick;
3051 + unsigned long getea_max_tick;
3053 + unsigned long setea_count; /* for set ea */
3054 + unsigned long setea_tick;
3055 + unsigned long setea_max_tick;
3057 + unsigned long converge_count; /* for converge */
3058 + unsigned long converge_tick;
3059 + unsigned long converge_max_tick;
3064 +/* snapshot operations */
3065 +struct snapshot_operations {
3066 + unsigned int ops_version;
3067 + int (*is_redirector) (struct inode *inode);
3068 + int (*is_indirect) (struct inode *inode);
3069 + struct inode * (*create_indirect) (struct inode *pri, int index,
3070 + unsigned int gen, ino_t parent_ino,
3072 + struct inode * (*get_indirect) (struct inode *pri, int *table,int slot);
3073 + ino_t (*get_indirect_ino) (struct inode *pri, int index);
3074 + int (*destroy_indirect) (struct inode *pri, int index,
3075 + struct inode *next_ind);
3076 + int (*restore_indirect) (struct inode *pri, int index);
3077 + int (*iterate) (struct super_block *sb,
3078 + int (*repeat)(struct inode *inode, void *priv),
3079 + struct inode **start, void *priv, int flag);
3080 + int (*copy_block) ( struct inode *dst, struct inode *src, int blk);
3081 + int (*has_block) (struct inode *dst, int blk);
3082 + int (*set_indirect) (struct inode *pri, int index,
3083 + ino_t ind_ino, ino_t parent_ino );
3084 + int (*snap_feature) (struct super_block *sb, int feature, int op);
3085 + int (*get_generation) (struct inode *pri);
3086 + int (*set_generation) (struct inode *pri, unsigned long new_gen);
3087 + int (*has_del_flag) (struct inode *inode);
3088 + int (*clear_del_flag) (struct inode *inode);
3089 + int (*set_meta_attr)(struct super_block *sb, char *name,
3090 + char *buf, int size);
3091 + int (*get_meta_attr)(struct super_block *sb, char *name,
3092 + char *buf, int *size);
3096 Index: linux-2.4.20-8/include/linux/ext3_fs.h
3097 ===================================================================
3098 --- linux-2.4.20-8.orig/include/linux/ext3_fs.h 2004-01-05 10:54:03.000000000 +0800
3099 +++ linux-2.4.20-8/include/linux/ext3_fs.h 2004-01-13 00:10:14.000000000 +0800
3100 @@ -183,7 +183,13 @@
3101 #define EXT3_INDEX_FL 0x00001000 /* hash-indexed directory */
3102 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
3103 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
3104 -#define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
3105 +/* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
3106 +#define EXT3_COW_FL 0x00008000 /* inode is snapshot cow */
3107 +#define EXT3_DEL_FL 0x00010000 /* inode is deleting in snapshot */
3108 +#define EXT3_SNAP_TABLE_FLAG 0x00020000 /* snap table inode */
3109 +/* FIXME For debugging will be removed later*/
3110 +#define EXT3_SNAP_PRI_FLAG 0x00040000 /* primary inode */
3113 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
3114 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
3115 @@ -205,10 +211,25 @@
3116 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
3117 #define EXT3_IOC_GETVERSION_OLD _IOR('v', 1, long)
3118 #define EXT3_IOC_SETVERSION_OLD _IOW('v', 2, long)
3119 +/* the following are for temporary test */
3120 +/* snapfs ioctls */
3121 +#define EXT3_IOC_CREATE_INDIR _IOW('v', 3, long)
3122 +#define EXT3_IOC_GET_INDIR _IOW('v', 4, long)
3123 +#define EXT3_IOC_DESTROY_INDIR _IOW('v', 5, long)
3124 +#define EXT3_IOC_IS_REDIR _IOW('v', 6, long)
3125 +#define EXT3_IOC_RESTORE_INDIR _IOW('v', 7, long)
3127 +#define EXT3_IOC_SNAP_SETFILECOW _IOW('v', 10, long)
3129 +/* XXX: the following are for temporary test, can be removed later */
3130 +#define EXT3_IOC_SNAP_PRINT _IOW('v', 11, long)
3131 +#define EXT3_IOC_SNAP_DELETE _IOW('v', 12, long)
3132 +#define EXT3_IOC_SNAP_RESTORE _IOW('v', 13, long)
3135 #ifdef CONFIG_JBD_DEBUG
3136 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
3140 * Structure of an inode on the disk
3142 @@ -429,7 +450,15 @@
3143 __u8 s_def_hash_version; /* Default hash version to use */
3144 __u8 s_reserved_char_pad;
3145 __u16 s_reserved_word_pad;
3146 - __u32 s_reserved[192]; /* Padding to the end of the block */
3148 + __u32 s_default_mount_opts;
3149 + __u32 s_first_meta_bg; /* First metablock group */
3150 + __u32 s_mkfs_time; /* When the filesystem was created */
3151 + __u32 s_first_cowed_pri_ino; /* For snapfs,the first cowed primary inode */
3152 + __u32 s_last_cowed_pri_ino; /* last cowed ino in memory */
3153 + __u32 s_snaptable_ino; /* snaptable ino in memory */
3154 + __u32 s_last_snap_orphan; /* SnapFS: start of cowing indirect inode */
3155 + __u32 s_reserved[185]; /* Padding to the end of the block,originally 204 */
3160 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
3161 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
3163 +#define EXT3_FEATURE_COMPAT_SNAPFS 0x0010
3164 +#define EXT3_FEATURE_COMPAT_BLOCKCOW 0x0020
3166 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
3167 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
3168 EXT3_FEATURE_INCOMPAT_RECOVER)
3169 Index: linux-2.4.20-8/include/linux/ext3_fs_sb.h
3170 ===================================================================
3171 --- linux-2.4.20-8.orig/include/linux/ext3_fs_sb.h 2004-01-05 10:54:00.000000000 +0800
3172 +++ linux-2.4.20-8/include/linux/ext3_fs_sb.h 2004-01-13 00:10:14.000000000 +0800
3174 wait_queue_head_t s_delete_thread_queue;
3175 wait_queue_head_t s_delete_waiter_queue;
3177 +#define EXT3_SNAP_FS
3178 +#ifdef EXT3_SNAP_FS
3179 + struct semaphore s_snap_list_sem;
3180 + unsigned long s_first_cowed_pri_ino;/* For snapfs,the first cowed primary inode */
3181 + unsigned long s_last_cowed_pri_ino; /* last cowed ino in memory */
3182 + unsigned long s_snaptable_ino; /* snaptable ino in memory */
3186 #endif /* _LINUX_EXT3_FS_SB */
3187 Index: linux-2.4.20-8/include/linux/ext3_jbd.h
3188 ===================================================================
3189 --- linux-2.4.20-8.orig/include/linux/ext3_jbd.h 2004-01-05 10:53:59.000000000 +0800
3190 +++ linux-2.4.20-8/include/linux/ext3_jbd.h 2004-01-13 00:10:14.000000000 +0800
3193 #define EXT3_INDEX_EXTRA_TRANS_BLOCKS 8
3195 +/*snapshot transaction blocks*/
3197 +#define EXT3_EA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3198 +#define EXT3_SETMETA_TRANS_BLOCKS EXT3_DATA_TRANS_BLOCKS
3199 +#define EXT3_NEWINODE_TRANS_BLOCKS 10
3200 +#define SNAP_INSERTLIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 1)
3201 +#define SNAP_DELETELIST_TRANS_BLOCKS (2 * EXT3_EA_TRANS_BLOCKS + 2)
3202 +#define SNAP_COPYBLOCK_TRANS_BLOCKS (EXT3_DATA_TRANS_BLOCKS)
3203 +#define SNAP_MIGRATEDATA_TRANS_BLOCKS 2
3204 +#define SNAP_SETIND_TRANS_BLOCKS (SNAP_INSERTLIST_TRANS_BLOCKS + 1)
3205 +#define SNAP_ADDORPHAN_TRANS_BLOCKS 2
3206 +#define SNAP_REMOVEORPHAN_TRANS_BLOCKS 1
3207 +#define SNAP_RESTOREORPHAN_TRANS_BLOCKS (EXT3_EA_TRANS_BLOCKS + \
3208 + SNAP_DELETELIST_TRANS_BLOCKS + \
3209 + EXT3_NEWINODE_TRANS_BLOCKS + \
3210 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS)
3211 +#define SNAP_BIGCOPY_TRANS_BLOCKS (2 * EXT3_DATA_TRANS_BLOCKS)
3212 +#define SNAP_CREATEIND_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3213 + SNAP_MIGRATEDATA_TRANS_BLOCKS + \
3214 + SNAP_SETIND_TRANS_BLOCKS + \
3215 + SNAP_BIGCOPY_TRANS_BLOCKS + 3)
3216 +#define SNAP_MIGRATEBLK_TRANS_BLOCKS 2
3217 +#define SNAP_DESTROY_TRANS_BLOCKS (SNAP_DELETELIST_TRANS_BLOCKS + \
3218 + EXT3_EA_TRANS_BLOCKS + 2)
3219 +#define SNAP_RESTORE_TRANS_BLOCKS (EXT3_NEWINODE_TRANS_BLOCKS + \
3220 + 2 * SNAP_MIGRATEDATA_TRANS_BLOCKS + 1)
3223 ext3_mark_iloc_dirty(handle_t *handle,
3224 struct inode *inode,
3227 fs/ext3/Makefile | 2
3228 fs/ext3/ialloc.c | 8
3230 fs/ext3/ioctl.c | 103 +
3231 fs/ext3/snap.c | 2645 +++++++++++++++++++++++++++++++++++++++++++++
3232 include/linux/ext3_fs.h | 38
3233 include/linux/ext3_fs_sb.h | 7
3234 include/linux/ext3_jbd.h | 27
3235 include/linux/snap.h | 266 ++++
3236 9 files changed, 3092 insertions(+), 6 deletions(-)