Whamcloud - gitweb
update .snap on smfs
[fs/lustre-release.git] / lustre / lvfs / fsfilt_snap_ext3.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Lustre filesystem abstraction routines
5  *
6  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
7  *   Author: Andreas Dilger <adilger@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #define DEBUG_SUBSYSTEM S_FILTER
25
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/fs.h>
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/ext3_fs.h>
34 #include <linux/ext3_jbd.h>
35 #include <linux/ext3_extents.h>
36 #include <linux/version.h>
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38 #include <linux/locks.h>
39 #include <linux/ext3_xattr.h>
40 #include <linux/module.h>
41 #include <linux/iobuf.h>
42 #else
43 #include <ext3/xattr.h>
44 #endif
45
46 #include <linux/kp30.h>
47 #include <linux/lustre_fsfilt.h>
48 #include <linux/obd.h>
49 #include <linux/obd_class.h>
50 #include <linux/lustre_smfs.h>
51 #include <linux/lustre_snap.h>
52
53 /* For snapfs in EXT3 flags --- FIXME will find other ways to store it*/
54 #define EXT3_COW_FL                     0x00100000 /* inode is snapshot cow */
55 #define EXT3_DEL_FL                     0x00200000 /* inode is deleting in snapshot */
56
57 #define EXT3_SNAP_ATTR "@snap"
58 #define EXT3_SNAP_GENERATION "@snap_generation"
59 #define EXT3_MAX_SNAPS 10
60 #define EXT3_MAX_SNAP_DATA (sizeof(struct snap_ea))
61 #define EXT3_SNAP_INDEX EXT3_XATTR_INDEX_LUSTRE
62 #define EXT3_SNAP_COUNT "@snapcount"
63
64
65 #define SB_FEATURE_COMPAT(sb)  (EXT3_SB(sb)->s_es->s_feature_compat)
66                                                                                                                                                                                                      
67 #define SNAP_HAS_COMPAT_FEATURE(sb,mask)        \
68         (SB_FEATURE_COMPAT(sb) & cpu_to_le32(mask))
69
70 #define EXT3_FEATURE_COMPAT_SNAPFS             0x0010
71 #define EXT3_FEATURE_COMPAT_BLOCKCOW           0x0020
72 /*snaptable info for EXT3*/
73 #define EXT3_SNAPTABLE_EA       "@snaptable"
74                                                                                                                                                                                                      
75 /* NOTE: these macros are close dependant on the structure of snap ea */
76 #define SNAP_CNT_FROM_SIZE(size)       ((((size)-sizeof(ino_t)*2)/2)/sizeof(ino_t))
77 #define SNAP_EA_SIZE_FROM_INDEX(index) (sizeof(ino_t)*2 + 2*sizeof(ino_t)*((index)+1))
78                                                                                                                                                                                                      
79 #define SNAP_EA_INO_BLOCK_SIZE(size)   (((size)-sizeof(ino_t)*2)/2)
80 #define SNAP_EA_PARENT_OFFSET(size)    (sizeof(ino_t)*2 + SNAP_EA_INO_BLOCK_SIZE((size)))
81
82 #define EXT3_JOURNAL_START(sb, handle, blocks, rc)              \
83 do {                                                            \
84         journal_t *journal;                                     \
85         journal = EXT3_SB(sb)->s_journal;                       \
86         lock_kernel();                                          \
87         handle = journal_start(journal, blocks);                \
88         unlock_kernel();                                        \
89         if(IS_ERR(handle)) {                                    \
90                 CERROR("can't start transaction\n");            \
91                 rc = PTR_ERR(handle);                           \
92         } else                                                  \
93                 rc = 0;                                         \
94 } while(0)
95
96
97 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
98 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
99 {
100         if (i1 == i2)
101                 down(&i1->i_sem);
102         else
103                 double_down(&i1->i_sem, &i2->i_sem);
104 }
105 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
106 {
107         if (i1 == i2)
108                 up(&i1->i_sem);
109         else 
110                 double_up(&i1->i_sem, &i2->i_sem);
111 }
112 #else
113 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
114 {
115        struct semaphore *s1 = &i1->i_sem;
116        struct semaphore *s2 = &i2->i_sem;
117
118        if (s1 != s2) {
119                if ((unsigned long) s1 < (unsigned long) s2) {
120                        struct semaphore *tmp = s2;
121                        s2 = s1; s1 = tmp;
122                }
123                down(s1);
124        }
125        down(s2);
126 }
127
128 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
129 {
130        struct semaphore *s1 = &i1->i_sem;
131        struct semaphore *s2 = &i2->i_sem;
132
133        up(s1);
134        if (s1 != s2)
135                up(s2);
136 }
137
138 #endif
139
140 /* helper functions to manipulate field 'parent' in snap_ea */
141 static inline int
142 set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
143 {
144        char * p = (char*) pea;
145        int offset;
146                                                                                                                                                                                                      
147        offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
148        offset += sizeof(ino_t) * index;
149        *(ino_t*)(p+offset) = val;
150                                                                                                                                                                                                      
151        return 0;
152 }
153 /**
154  * fsfilt_ext3_get_indirect - get a specific indirect inode from a primary inode
155  * @primary: primary (direct) inode
156  * @table: table of @slot + 1 indices in reverse chronological order
157  * @slot: starting slot number to check for indirect inode number
158  *
159  * We locate an indirect inode from a primary inode using the redirection
160  * table stored in the primary inode.  Because the desired inode may actually
161  * be in a "newer" slot number than the supplied slot, we are given a table
162  * of indices in chronological order to search for the correct inode number.
163  * We walk table from @slot to 0 looking for a non-zero inode to load.
164  *
165  * To only load a specific index (and fail if it does not exist), you can
166  * pass @table = NULL, and the index number in @slot.  If @slot == 0, the
167  * primary inode data is returned.
168  *
169  * We return a pointer to an inode, or an error.  If the indirect inode for
170  * the given index does not exist, NULL is returned.
171  */
172 static struct inode *fsfilt_ext3_get_indirect(struct inode *primary, int *table,
173                                               int slot)
174 {
175         char buf[EXT3_MAX_SNAP_DATA];
176         struct snap_ea *snaps;
177         ino_t ino;
178         struct inode *inode = NULL;
179         int rc = 0;
180
181         ENTRY;
182
183         if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
184                 RETURN(NULL);
185         
186         CDEBUG(D_INODE, "ino %lu, table %p, slot %d\n", primary->i_ino, table,
187                slot);
188         rc = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf, 
189                              EXT3_MAX_SNAP_DATA); 
190         if (rc == -ENODATA) {
191                 slot = -1;
192         } else if (rc < 0) {
193                 CERROR("attribute read rc=%d \n", rc);
194                 RETURN(NULL);
195         }
196         snaps = (struct snap_ea *)buf;
197
198         /* if table is NULL and there is a slot */
199         if( !table && slot >= 0) {
200                 ino = le32_to_cpu(snaps->ino[slot]);
201                 if(ino) 
202                         inode = iget(primary->i_sb, ino);
203                 GOTO(err_free, rc);
204         }
205         /* if table is not NULL */
206         while (!inode && slot >= 0 ) {
207                 ino = le32_to_cpu(snaps->ino[slot]);
208
209                 CDEBUG(D_INODE, "snap inode at slot %d is %lu\n", slot, ino);
210                 if (!ino) {
211                         --slot;
212                         continue;
213                 }
214                 inode = iget(primary->i_sb, ino);
215                 GOTO(err_free, rc);
216         }
217         if( slot == -1) {
218                 CDEBUG(D_INODE, "redirector not found, using primary\n");
219                 inode = iget(primary->i_sb, primary->i_ino);
220         }
221 err_free:
222         RETURN(inode);
223 }
224
225 /* Save the indirect inode in the snapshot table of the primary inode. */
226 static int fsfilt_ext3_set_indirect(struct inode *pri, int index, ino_t ind_ino, 
227                                     ino_t parent_ino )
228 {
229         char buf[EXT3_MAX_SNAP_DATA];
230         struct snap_ea *snaps;
231         int rc = 0, inlist = 1;
232         int ea_size;
233         handle_t *handle = NULL;
234         ENTRY;
235         
236         CDEBUG(D_INODE, "(ino %lu, parent %lu): saving ind %lu to index %d\n", 
237                pri->i_ino, parent_ino, ind_ino, index);
238
239         if (index < 0 || index > MAX_SNAPS || !pri)
240                 RETURN(-EINVAL);
241         /* need lock the list before get_attr() to avoid race */
242         /* read ea at first */
243         rc = ext3_xattr_get(pri, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
244                                           buf, EXT3_MAX_SNAP_DATA);
245         if (rc == -ENODATA || rc == -ENODATA) {
246                 CDEBUG(D_INODE, "no extended attributes - zeroing\n");
247                 memset(buf, 0, EXT3_MAX_SNAP_DATA);
248                 /* XXX
249                  * To judge a inode in list, we only see if it has snap ea.
250                  * So take care of snap ea of primary inodes very carefully.
251                  * Is it right in snapfs EXT3, check it later?
252                  */
253                 inlist = 0;
254                 rc = 0; 
255         } else if (rc < 0 || rc > EXT3_MAX_SNAP_DATA) {
256                 GOTO(out_unlock, rc);
257         }
258         EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_SETIND_TRANS_BLOCKS, rc); 
259         if(rc) 
260                 GOTO(out_unlock, rc = PTR_ERR(handle));
261         
262         snaps = (struct snap_ea *)buf;
263         snaps->ino[index] = cpu_to_le32 (ind_ino);
264         ea_size = EXT3_MAX_SNAP_DATA;
265
266         set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
267
268         rc = ext3_xattr_set_handle(handle, pri, EXT3_SNAP_INDEX,EXT3_SNAP_ATTR,
269                                     buf, EXT3_MAX_SNAP_DATA, 0);
270         ext3_mark_inode_dirty(handle, pri);
271         journal_stop(handle);
272 out_unlock:
273         RETURN(rc);
274 }
275
276 static int ext3_set_generation(struct inode *inode, unsigned long gen)
277 {
278         handle_t *handle;
279         int err = 0;
280         ENTRY;
281        
282         EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS, err);
283         if(err)
284                 RETURN(err);
285         
286         err = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX, 
287                                     EXT3_SNAP_GENERATION, (char*)&gen, 
288                                     sizeof(int), 0);
289         if (err < 0) {
290                 CERROR("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
291                 RETURN(err);
292         }
293         
294         journal_stop(handle);
295         RETURN(0);
296 }
297
298 /*
299  * Copy inode metadata from one inode to another, excluding blocks and size.
300  * FIXME do we copy EA data - ACLs and such (excluding snapshot data)?
301  */
302 static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
303 {
304         int size;
305         
306         dst->i_mode = src->i_mode;
307         dst->i_nlink = src->i_nlink;
308         dst->i_uid = src->i_uid;
309         dst->i_gid = src->i_gid;
310         dst->i_atime = src->i_atime;
311         dst->i_mtime = src->i_mtime;
312         dst->i_ctime = src->i_ctime;
313 //      dst->i_version = src->i_version;
314         
315 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
316         dst->i_attr_flags = src->i_attr_flags;
317 #endif
318         dst->i_generation = src->i_generation;
319         EXT3_I(dst)->i_dtime = EXT3_I(src)->i_dtime;
320         EXT3_I(dst)->i_flags = EXT3_I(src)->i_flags | EXT3_COW_FL;
321 #ifdef EXT3_FRAGMENTS
322         EXT3_I(dst)->i_faddr = EXT3_I(src)->i_faddr;
323         EXT3_I(dst)->i_frag_no = EXT3_I(src)->i_frag_no;
324         EXT3_I(dst)->i_frag_size = EXT3_I(src)->i_frag_size;
325 #endif
326         if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
327                 char names[size];
328                 char *name;
329                 int namelen;
330
331                 if (ext3_xattr_list(src, names, 0) < 0)
332                         return;
333                 /*
334                  * the list of attribute names are stored as NUL terminated
335                  * strings, with a double NUL string at the end.
336                  */
337                 name = names;
338                 while ((namelen = strlen(name))) {
339                         int attrlen;
340                         char *buf;
341                         
342                         /* don't copy snap data */
343                         if (!strcmp(name, EXT3_SNAP_ATTR)) {
344                                 CDEBUG(D_INFO, "skipping %s item\n", name);
345                                 continue;
346                         }
347                         CDEBUG(D_INODE, "copying %s item\n", name);
348                         attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX, 
349                                                  EXT3_SNAP_ATTR, NULL, 0);
350                         if (attrlen < 0)
351                                 continue;
352                         OBD_ALLOC(buf, attrlen);
353                                 break;
354                         if (!buf) {
355                                 CERROR("No MEM\n");
356                                 break;
357                         }
358                         if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
359                                            EXT3_SNAP_ATTR, buf, attrlen) < 0)
360                                 continue;       
361                         if (ext3_xattr_set_handle(handle, dst, EXT3_SNAP_INDEX,
362                                                   EXT3_SNAP_ATTR, buf, attrlen, 
363                                                   0) < 0)
364                                 break;
365                         OBD_FREE(buf, attrlen);
366                         name += namelen + 1; /* skip name and trailing NUL */
367                 }
368         }
369 }
370 static int ext3_copy_reg_block(struct inode *dst, struct inode *src, int blk)
371 {
372         struct page     *src_page, *dst_page; 
373         loff_t          offset = blk << src->i_sb->s_blocksize_bits;
374         unsigned long   index = offset >> PAGE_CACHE_SHIFT;
375         int             rc = 0;
376         ENTRY;
377         
378         /*read the src page*/
379         src_page = grab_cache_page(src->i_mapping, index);
380         if (src_page == NULL)
381                 RETURN(-ENOMEM);
382
383         if (!PageUptodate(src_page)) {
384                 rc = src->i_mapping->a_ops->readpage(NULL, src_page);
385                 if (rc < 0) {
386                         page_cache_release(src_page);
387                         RETURN(rc);
388                 }
389         }
390         kmap(src_page);
391         /*get dst page*/
392         
393         dst_page = grab_cache_page(dst->i_mapping, index);
394         if (dst_page == NULL)
395                 GOTO(src_page_unlock, rc = -ENOMEM);
396         kmap(dst_page);
397
398         rc = dst->i_mapping->a_ops->prepare_write(NULL, dst_page, 0, 
399                                                   PAGE_CACHE_SIZE - 1);
400         if (rc)
401                 GOTO(dst_page_unlock, rc = -EFAULT);
402         memcpy(page_address(dst_page), page_address(src_page), PAGE_CACHE_SIZE);
403         
404         flush_dcache_page(dst_page);
405         
406         rc = dst->i_mapping->a_ops->commit_write(NULL, dst_page, 0, 
407                                                  PAGE_CACHE_SIZE - 1);
408         if (!rc)
409                 rc = 1;
410 dst_page_unlock:
411         kunmap(dst_page);
412         unlock_page(dst_page);
413         page_cache_release(dst_page);
414 src_page_unlock:
415         kunmap(src_page);
416         page_cache_release(src_page);
417         RETURN(rc);
418 }
419 static int ext3_copy_dir_block(struct inode *dst, struct inode *src, int blk)
420 {
421         struct buffer_head *bh_dst = NULL, *bh_src = NULL;
422         int rc = 0;
423         handle_t *handle = NULL;
424         ENTRY;   
425
426         EXT3_JOURNAL_START(dst->i_sb, handle, SNAP_COPYBLOCK_TRANS_BLOCKS, rc);
427         if(rc)
428                 RETURN(rc);
429                                                                                                                                                                                                      
430         bh_src = ext3_bread(handle, src, blk, 0, &rc);
431         if (!bh_src) {
432                 CERROR("rcor for src blk %d, rcor %d\n", blk, rc);
433                 GOTO(exit_relese, rc);
434         }
435         bh_dst = ext3_getblk(handle, dst, blk, 1, &rc);
436         if (!bh_dst) {
437                 CERROR("rcor for dst blk %d, rcor %d\n", blk, rc);
438                 GOTO(exit_relese, rc);
439         }
440         CDEBUG(D_INODE, "copy block %lu to %lu (%ld bytes)\n",
441                bh_src->b_blocknr, bh_dst->b_blocknr, src->i_sb->s_blocksize);
442         
443         ext3_journal_get_write_access(handle, bh_dst);
444         memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
445         ext3_journal_dirty_metadata(handle, bh_dst);
446         rc = 1;
447
448 exit_relese:
449         if (bh_src) brelse(bh_src);
450         if (bh_dst) brelse(bh_dst);
451         if (handle)
452                 journal_stop(handle);
453         RETURN(rc);
454 }
455 /* fsfilt_ext3_copy_block - copy one data block from inode @src to @dst.
456    No lock here.  User should do the lock.
457    User should check the return value to see if the result is correct.
458    Return value:
459    1:    The block has been copied successfully
460    0:    No block is copied, usually this is because src has no such blk
461   -1:    Error
462 */
463                                                                                                                                                                                                      
464 static int fsfilt_ext3_copy_block (struct inode *dst, struct inode *src, int blk)
465 {
466         int rc = 0;
467         ENTRY;                                                                                                                                                                                             
468         CDEBUG(D_INODE, "copy blk %d from %lu to %lu \n", blk, src->i_ino, 
469                dst->i_ino);
470         /*
471          * ext3_getblk() require handle!=NULL
472          */
473         if (S_ISREG(src->i_mode)) { 
474                 rc = ext3_copy_reg_block(dst, src, blk);
475         } else {
476                 rc = ext3_copy_dir_block(dst, src, blk);
477         }
478
479         RETURN(rc);
480 }
481                                                                                                                                                                                              
482 static inline int ext3_has_ea(struct inode *inode)
483 {
484        return (EXT3_I(inode)->i_file_acl != 0);
485 }
486 /* XXXThis function has a very bad effect to
487  * the performance of filesystem,
488  * will find another way to fix it
489  */
490 static void fs_flushinval_pages(handle_t *handle, struct inode* inode)
491 {
492         if (inode->i_blocks > 0 && inode->i_mapping) {
493 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
494                 fsync_inode_data_buffers(inode);
495 #endif
496                 truncate_inode_pages(inode->i_mapping, 0);
497         }
498 }
499 /*  ext3_migrate_data:
500  *  MOVE all the data blocks from inode src to inode dst as well as
501  *  COPY all attributes(meta data) from inode src to inode dst.
502  *  For extended attributes(EA), we COPY all the EAs but skip the Snap EA from 
503  *  src to dst. If the dst has Snap EA, then we CAN'T overwrite it. We CAN'T 
504  *  copy the src Snap EA. XXX for EA, can we change it to MOVE all the EAs
505  *  (exclude Snap EA) to dst and copy it back to src ? This is for LAN free 
506  *  backup later.
507  */
508 static int ext3_migrate_data(handle_t *handle, struct inode *dst, 
509                              struct inode *src)
510 {
511         unsigned long err = 0;
512         /* 512 byte disk blocks per inode block */
513         int bpib = src->i_sb->s_blocksize >> 9;
514         ENTRY;
515         
516         
517         if((!dst) || (!src)) 
518                 RETURN(-EINVAL);
519         
520         if (dst->i_ino == src->i_ino)
521                 RETURN(0);
522
523         fs_flushinval_pages(handle, src);
524         
525         ext3_copy_meta(handle, dst, src);
526
527         CDEBUG(D_INODE, "migrating data blocks from %lu to %lu\n", 
528                src->i_ino, dst->i_ino);
529         /* Can't check blocks in case of EAs */
530        
531         memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
532                sizeof(EXT3_I(src)->i_data));
533         memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
534         
535         ext3_discard_prealloc(src);
536
537         dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
538         src->i_size = EXT3_I(src)->i_disksize = 0;
539
540         dst->i_blocks = src->i_blocks;
541         src->i_blocks = 0;
542         /*  Check EA blocks here to modify i_blocks correctly */
543         if(ext3_has_ea (src)) {
544                 src->i_blocks += bpib;
545                 if( ! ext3_has_ea (dst) )
546                         if( dst->i_blocks >= bpib )
547                                 dst->i_blocks -= bpib;
548         } else {
549                 if( ext3_has_ea (dst))
550                         dst->i_blocks += bpib;
551         }
552         
553         CDEBUG(D_INODE, "migrate data from ino %lu to ino %lu\n", src->i_ino, 
554                dst->i_ino);
555         ext3_mark_inode_dirty(handle, src);
556         ext3_mark_inode_dirty(handle, dst);
557         RETURN(err);
558 }
559
560 static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
561                                  struct inode *src, int *has_orphan)
562 {
563         unsigned long blocks, blk, cur_blks;
564         int low_credits, save_ref;
565         int err = 0;
566         ENTRY;
567
568         blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
569                  src->i_sb->s_blocksize_bits;
570         low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
571         
572         CDEBUG(D_INODE, "%lu blocks need to be copied,low credits limit %d\n", 
573                blocks, low_credits);
574
575         for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
576                 if (!ext3_bmap(src->i_mapping, blk))
577                         continue;
578                 if(handle->h_buffer_credits <= low_credits) {
579                         int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
580                         if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
581                                 needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
582                         if (journal_extend(handle, needed)) {
583                                 CDEBUG(D_INFO, "create_indirect:fail to extend "
584                                        "journal, restart trans\n");
585                                 
586                                 if(!*has_orphan) {
587                                         CDEBUG(D_INODE, "add orphan ino %lu" 
588                                                "nlink %d to orphan list \n",
589                                                 dst->i_ino, dst->i_nlink); 
590                                         ext3_orphan_add(handle, dst);
591                                         *has_orphan = 1;
592                                 }
593                                 EXT3_I(dst)->i_disksize =
594                                         blk * dst->i_sb->s_blocksize;
595                                 dst->i_blocks = cur_blks;
596                                 dst->i_mtime = CURRENT_TIME;
597                                 ext3_mark_inode_dirty(handle, dst);
598                                 /*
599                                  * We can be sure the last handle was stoped
600                                  * ONLY if the handle's reference count is 1
601                                  */
602                                 save_ref = handle->h_ref;
603                                 handle->h_ref = 1;
604                                 if(journal_stop(handle) ){
605                                         CERROR("fail to stop journal\n");
606                                         handle = NULL;
607                                         break;
608                                 }
609                                 EXT3_JOURNAL_START(dst->i_sb, handle, 
610                                                    low_credits + needed, err);
611                                 if(err) break;
612                                 handle->h_ref = save_ref;
613                         }
614                 }
615                 if (fsfilt_ext3_copy_block( dst, src, blk) < 0 )
616                         break;
617                 cur_blks += dst->i_sb->s_blocksize / 512;
618         }
619         
620         dst->i_size = EXT3_I(dst)->i_disksize = src->i_size;
621         RETURN(handle);
622 }
623 /*Here delete the data of that pri inode 
624  *FIXME later, should throw the blocks of 
625  *primary inode directly
626  */
627 static int ext3_throw_inode_data(handle_t *handle, struct inode *inode) 
628 {       
629         struct inode *tmp = NULL;
630         ENTRY;
631         tmp = ext3_new_inode(handle, inode, (int)inode->i_mode, 0);
632         if(tmp) { 
633                 CERROR("ext3_new_inode error\n");
634                 RETURN(-EIO);
635         }                
636         double_lock_inode(inode, tmp);
637         ext3_migrate_data(handle, tmp, inode);
638         double_unlock_inode(inode, tmp);
639         tmp->i_nlink = 0;
640         iput(tmp);      
641         RETURN(0);
642 }
643 /**
644  * fsfilt_ext3_create_indirect - copy data, attributes from primary to new indir inode
645  * @pri: primary (source) inode
646  * @index: index in snapshot table where indirect inode should be stored
647  * @delete: flag that the primary inode is being deleted
648  *
649  * We copy all of the data blocks from the @*src inode to the @*dst inode, as
650  * well as copying the attributes from @*src to @*dst.  If @delete == 1, then
651  * the primary inode will only be a redirector and will appear deleted.
652  *
653  * FIXME do we move EAs, only non-snap EAs, what?
654  * FIXME we could do readpage/writepage, but we would have to handle block
655  *       allocation then, and it ruins sparse files for 1k/2k filesystems,
656  *       at the expense of doing a memcpy.
657  */
658 static struct inode* fsfilt_ext3_create_indirect(struct inode *pri, int index, 
659                                                  unsigned int gen, 
660                                                  struct inode* parent,
661                                                  int del)
662 {
663         struct inode *ind = NULL;
664         handle_t *handle = NULL;
665         int err = 0;
666         int has_orphan = 0;
667         ENTRY;
668         
669         if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
670                 CERROR("TRY TO COW JOUNRAL\n");
671                 RETURN(ERR_PTR(-EINVAL));
672         }
673         CDEBUG(D_INODE, "creating indirect inode for %lu at index %d, %s pri\n",
674                pri->i_ino, index, del ? "deleting" : "preserve");
675
676         ind = fsfilt_ext3_get_indirect(pri, NULL, index);
677         
678         EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_CREATEIND_TRANS_BLOCKS,
679                            err);
680         if(err) 
681                 RETURN(ERR_PTR(err));
682         /* XXX ? We should pass an err argument to get_indirect and precisely
683          * detect the errors, for some errors, we should exit right away.
684          */
685
686         /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect, 
687          * we just free the primary data blocks and mark this inode delete
688          */
689         if((del) && ind && !IS_ERR(ind)) {
690                 /* for directory, we don't free the data blocks, 
691                  * or ext3_rmdir will report errors "bad dir, no data blocks" 
692                  */
693                 CDEBUG(D_INODE, "del==SNAP_DEL_PRI_WITH_IND && ind\n");
694                 if(!S_ISDIR(pri->i_mode)) {     
695                         err = ext3_throw_inode_data(handle, pri);
696                         if (err)
697                                 GOTO(exit, err);
698                         pri->i_nlink = 1;
699                 }
700                 EXT3_I(pri)->i_dtime = LTIME_S(CURRENT_TIME);
701                 ext3_mark_inode_dirty(handle, pri);
702                 GOTO(exit, err=0);
703         }
704
705         if (ind && !IS_ERR(ind)) {
706                 CDEBUG(D_INODE, "existing indirect ino %lu for %lu: index %d\n",
707                        ind->i_ino, pri->i_ino, index);
708         
709                 GOTO(exit, err=0);
710         }
711         
712         /* XXX: check this, ext3_new_inode, the first arg should be "dir" */ 
713         ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
714
715         if (IS_ERR(ind))
716                 GOTO(exit, err);
717         CDEBUG(D_INODE, "got new inode %lu\n", ind->i_ino);
718         ind->i_rdev = pri->i_rdev;
719         ind->i_op = pri->i_op;
720       
721         /*init ind ops*/ 
722         memcpy(ind->i_op, pri->i_op, sizeof(*pri->i_op));
723         memcpy(ind->i_fop, pri->i_fop, sizeof(*pri->i_fop));
724         memcpy(ind->i_mapping->a_ops, pri->i_mapping->a_ops, 
725                sizeof(*pri->i_mapping->a_ops));
726          
727         ext3_set_generation(ind, (unsigned long)gen);
728         /* If we are deleting the primary inode, we want to ensure that it is
729          * written to disk with a non-zero link count, otherwise the next iget
730          * and iput will mark the inode as free (which we don't want, we want
731          * it to stay a redirector).  We fix this in ext3_destroy_indirect()
732          * when the last indirect inode is removed.
733          *
734          * We then do what ext3_delete_inode() does so that the metadata will
735          * appear the same as a deleted inode, and we can detect it later.
736          */
737         if (del) {
738                 CDEBUG(D_INODE, "deleting primary inode\n");
739                 
740                 down(&ind->i_sem);
741                 err = ext3_migrate_data(handle, ind, pri);
742                 if (err)
743                         GOTO(exit_unlock, err);
744
745                 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
746                 if (err)
747                         GOTO(exit_unlock, err);
748
749                 /* XXX for directory, we copy the block back 
750                  * or ext3_rmdir will report errors "bad dir, no data blocks" 
751                  */
752                 if( S_ISDIR(pri->i_mode)) {
753                         handle = ext3_copy_data(handle, pri, ind, &has_orphan);
754                         if(!handle) 
755                                 GOTO(exit_unlock, err= -EINVAL);
756                 }
757
758                 EXT3_I(pri)->i_flags |= EXT3_DEL_FL;
759                 EXT3_I(ind)->i_flags |= EXT3_COW_FL;
760                 if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
761                 EXT3_I(pri)->i_dtime = LTIME_S(CURRENT_TIME);
762                 //EXT3_I(pri)->i_generation++;
763                 ext3_mark_inode_dirty(handle, pri);
764                 ext3_mark_inode_dirty(handle, ind);
765                 up(&ind->i_sem);
766         } else {
767                 down(&ind->i_sem);
768                 err = ext3_migrate_data(handle, ind, pri);
769                 if (err)
770                         goto exit_unlock;
771
772                 /* for regular files we do blocklevel COW's maybe */
773                 if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
774                     && S_ISREG(pri->i_mode)) {
775
776                         CDEBUG(D_INODE, "ino %lu, do block cow\n", pri->i_ino);
777                         /* because after migrate_data , pri->i_size is 0 */
778                         pri->i_size = ind->i_size;
779                 }
780                 else {
781                         int bpib = pri->i_sb->s_blocksize >> 9;
782                         CDEBUG(D_INODE, "ino %lu, do file cow\n", pri->i_ino);
783
784                         /* XXX: can we do this better? 
785                          * If it's a fast symlink, we should copy i_data back!
786                          * The criteria to determine a fast symlink is:
787                          * 1) it's a link and its i_blocks is 0
788                          * 2) it's a link and its i_blocks is bpib ( the case 
789                          *    it has been cowed and has ea )
790                          */
791                         if( S_ISLNK(ind->i_mode) && ((ind->i_blocks == 0) || 
792                             (ext3_has_ea(ind) && ind->i_blocks == bpib))) {
793                                 CDEBUG(D_INODE, "ino %lu is fast symlink\n", pri->i_ino);
794                                 memcpy(EXT3_I(pri)->i_data, EXT3_I(ind)->i_data,
795                                        sizeof(EXT3_I(ind)->i_data));
796                                 pri->i_size = ind->i_size;
797                         }
798                         else {
799                                 handle = ext3_copy_data(handle, pri, ind, &has_orphan);
800                                 if (!handle)
801                                         GOTO(exit_unlock, err);
802                         }
803                 }
804                 /* set cow flag for ind */
805                 EXT3_I(ind)->i_flags |= EXT3_COW_FL;
806                 EXT3_I(pri)->i_flags &= ~EXT3_COW_FL;
807
808                 ext3_mark_inode_dirty(handle, pri);
809                 ext3_mark_inode_dirty(handle, ind);
810
811                 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
812                 if (err)
813                         GOTO(exit_unlock, err);
814                 up(&ind->i_sem);
815         }
816
817         if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
818                                      EXT3_FEATURE_COMPAT_SNAPFS)) {
819                 lock_super(pri->i_sb);
820                 ext3_journal_get_write_access(handle, EXT3_SB(pri->i_sb)->s_sbh);
821                 EXT3_SB(pri->i_sb)->s_es->s_feature_compat |=
822                         cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
823                 ext3_journal_dirty_metadata(handle, EXT3_SB(pri->i_sb)->s_sbh);
824                 pri->i_sb->s_dirt = 1;
825                 unlock_super(pri->i_sb);
826         }
827         if (has_orphan) {
828                 CDEBUG(D_INODE, "del %lu nlink %d from orphan list\n", 
829                        ind->i_ino, ind->i_nlink);
830                 ext3_orphan_del(handle, ind);
831         }
832         journal_stop(handle);
833
834         RETURN(ind);
835
836 exit_unlock:
837         up(&ind->i_sem);
838         ind->i_nlink = 0;
839 exit:
840         if (has_orphan) {
841                 CDEBUG(D_INODE, "del %lu nlink %d from orphan list\n", 
842                        ind->i_ino, ind->i_nlink);
843                 ext3_orphan_del(handle, ind);
844         }
845         iput(ind);
846         journal_stop(handle);
847         
848         RETURN(ERR_PTR(err));
849 }
850
851 static int fsfilt_ext3_snap_feature (struct super_block *sb, int feature, int op) {
852                                                                                                                                                                                                      
853         int rc = -EINVAL;
854         handle_t *handle;
855         ENTRY;
856         
857         switch (op) {
858                 case SNAP_SET_FEATURE:
859                 case SNAP_CLEAR_FEATURE:
860                         EXT3_JOURNAL_START(sb, handle, 1, rc);
861                         if(rc)
862                                 RETURN(rc);
863                         lock_super(sb);
864                         ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
865                         if (op == SNAP_SET_FEATURE) 
866                                 SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
867                         else 
868                                 SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature);
869                         sb->s_dirt = 1;
870                         ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
871                         unlock_super(sb);
872                         journal_stop(handle);
873                         break;
874                 case SNAP_HAS_FEATURE:
875                         /*FIXME should lock super or not*/
876                         rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
877                         break;
878                 default:
879                         break;
880         }
881         RETURN(rc);
882 }
883 /*
884  * is_redirector - determines if a primary inode is a redirector
885  * @inode: primary inode to test
886  *
887  * Returns 1 if the inode is a redirector, 0 otherwise.
888  */
889 static int fsfilt_ext3_is_redirector(struct inode *inode)
890 {
891         int is_redirector = 0;
892         int rc;
893         ENTRY;
894                                                                                                                                                                                                      
895         rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
896                                           NULL, 0);
897         if (rc > 0 && rc <= MAX_SNAP_DATA)
898                 is_redirector = 1;
899         CDEBUG(D_INODE, "inode %lu %s redirector\n", inode->i_ino,
900                is_redirector ? "is" : "isn't");
901         RETURN(is_redirector);
902 }
903 /*if it's indirect inode or not */
904 static int fsfilt_ext3_is_indirect(struct inode *inode)
905 {
906         if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
907                 return 1;
908         else
909                 return 0;
910 }
911
912 /* get the indirect ino at index of the primary inode
913  * return value:        postive:        indirect ino number
914  *                      negative or 0:  error
915  */
916 static ino_t fsfilt_ext3_get_indirect_ino(struct super_block *sb, 
917                                           ino_t primary_ino, int index)
918 {
919         char buf[EXT3_MAX_SNAP_DATA];
920         struct inode *primary = NULL;
921         struct snap_ea *snaps;
922         ino_t ino = 0;
923         int err;
924         ENTRY;                                                                                                                                                                                             
925         if (index < 0 || index > EXT3_MAX_SNAPS || !primary)
926                 RETURN(0);
927         primary = iget(sb, primary_ino);   
928        
929         if (!primary) {
930                 err = -EIO;
931                 CERROR("attribute read error=%d", err);
932                 GOTO (err_free, ino = err); 
933         }                                                                                                                                                                                              
934         err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
935                              buf, EXT3_MAX_SNAP_DATA);
936         if (err == -ENODATA) {
937                 GOTO(err_free, ino = -ENODATA);
938         } else if (err < 0) {
939                 CERROR(" attribute read error err=%d\n", err);
940                 GOTO(err_free, ino = err);
941         }
942         snaps = (struct snap_ea *)buf;
943         ino = le32_to_cpu (snaps->ino[index]);
944         CDEBUG(D_INODE, "snap ino for %ld at index %d is %lu\n",
945                primary->i_ino, index, ino);
946 err_free:
947         if (primary)
948                 iput(primary); 
949         RETURN(ino);
950 }
951                                                                                                                                                                                                      
952
953 /* The following functions are used by destroy_indirect */
954 #define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
955 #define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
956 static inline int block_bmap(struct buffer_head * bh, int nr)
957 {
958         int tmp;
959                                                                                                                                                                                                      
960         if (!bh)
961                 return 0;
962         tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
963         brelse (bh);
964         return tmp;
965 }
966                                                                                                                                                                                                      
967 static inline int block_setbmap(handle_t *handle, struct buffer_head * bh, 
968                                  int nr, int physical)
969 {
970                                                                                                                                                                                                      
971         if (!bh)
972                 return 0;
973         ext3_journal_get_write_access(handle, bh);
974         ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
975         ext3_journal_dirty_metadata(handle, bh);
976         brelse (bh);
977         return 1;
978 }
979
980 static int ext3_migrate_block(handle_t *handle, struct inode * dst, 
981                               struct inode *src, int block)
982 {
983         int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
984         int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
985         int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
986         int physical = 0;
987         ENTRY;        
988
989         if (block < 0) {
990                 CWARN("ext3_migrate_block block < 0 %p \n", src->i_sb);
991                 RETURN(0);
992         }
993         if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
994                 (1 << (addr_per_block_bits * 2)) +
995                 ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
996                 CWARN("ext3_migrate_block block > big %p \n", src->i_sb);
997                 RETURN(0);
998         }
999         /* EXT3_NDIR_BLOCK */
1000         if (block < EXT3_NDIR_BLOCKS) {
1001                 if(inode_bmap(dst, block))      
1002                         RETURN(0);
1003                 else {
1004                         if( (physical = inode_bmap(src, block)) ) {
1005                                 inode_setbmap (dst, block, physical);
1006                                 inode_setbmap (src, block, 0);
1007                                 RETURN(1);
1008                         }
1009                         else 
1010                                 RETURN(0);
1011                 }
1012         }
1013         /* EXT3_IND_BLOCK */
1014         block -= EXT3_NDIR_BLOCKS;
1015         if (block < addr_per_block) {
1016                 i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1017                 if (!i1_d) {
1018                         physical = inode_bmap(src, EXT3_IND_BLOCK);
1019                         if( physical ) {
1020                                 inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1021                                 inode_setbmap (src, EXT3_IND_BLOCK, 0);
1022                                 RETURN(1);
1023                         }
1024                         else 
1025                                 RETURN(0);
1026                 }
1027                 if(block_bmap(sb_bread(dst->i_sb, i1_d), block)) 
1028                         RETURN(0);
1029
1030                 i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1031                 if( !i1_s)      RETURN(0);
1032
1033                 physical = block_bmap(sb_bread(src->i_sb, i1_s), block);
1034
1035                 if( physical) {
1036                         block_setbmap(handle, sb_bread(dst->i_sb, i1_d),block,
1037                                       physical); 
1038                         block_setbmap(handle, sb_bread(src->i_sb, i1_s),block,0);
1039                         RETURN(1); 
1040                 }
1041                 else 
1042                         RETURN(0);
1043         }
1044         /* EXT3_DIND_BLOCK */
1045         block -= addr_per_block;
1046         if (block < (1 << (addr_per_block_bits * 2))) {
1047                 i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1048                 i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1049                 if (!i1_d) {
1050                         if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1051                                 inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1052                                 inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1053                                 RETURN(1);
1054                         }
1055                         else 
1056                                 RETURN(0);
1057                 }
1058                 i2_d = block_bmap (sb_bread (dst->i_sb, i1_d),
1059                                 block >> addr_per_block_bits);
1060
1061                 if (!i2_d) {
1062                         
1063                         if(!i1_s)       RETURN(0);
1064
1065                         physical = block_bmap(sb_bread (src->i_sb, i1_s),
1066                                                block >> addr_per_block_bits);
1067                         if(physical) {
1068                                 block_setbmap(handle, sb_bread(dst->i_sb, i1_d), 
1069                                               block >> addr_per_block_bits, 
1070                                               physical);
1071                                 block_setbmap(handle, sb_bread(src->i_sb, i1_s), 
1072                                               block >> addr_per_block_bits, 0);
1073                                 RETURN(1);
1074                         }
1075                         else
1076                                 RETURN(0);
1077                 }
1078                 physical = block_bmap(sb_bread(dst->i_sb, i2_d),
1079                                       block & (addr_per_block - 1));
1080                 if(physical) 
1081                                 RETURN(0);
1082                 else {
1083                         i2_s =  block_bmap (sb_bread(src->i_sb, i1_s),
1084                                 block >> addr_per_block_bits);
1085                         if(!i2_s)       RETURN(0);
1086         
1087                         physical = block_bmap(sb_bread(src->i_sb, i2_s),
1088                                    block & (addr_per_block - 1));
1089                         if(physical) {
1090                                 block_setbmap(handle, sb_bread(dst->i_sb, i2_d),
1091                                    block & (addr_per_block - 1), physical);
1092                                 block_setbmap(handle, sb_bread(src->i_sb, i2_s),
1093                                    block & (addr_per_block - 1), 0);
1094                                 RETURN(1);
1095                         }
1096                         else 
1097                                 RETURN(0);
1098                 }
1099                 
1100         }
1101         /* EXT3_TIND_BLOCK */
1102         block -= (1 << (addr_per_block_bits * 2));
1103         i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1104         i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1105         if (!i1_d) {
1106                 if((physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1107                         inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1108                 else 
1109                         RETURN(0);
1110         }
1111         i2_d = block_bmap(sb_bread (dst->i_sb, i1_d),
1112                            block >> (addr_per_block_bits * 2));
1113
1114         if(i1_s) i2_s = block_bmap(sb_bread(src->i_sb, i1_s),
1115                                    block >> (addr_per_block_bits * 2));
1116
1117         if (!i2_d) {
1118                 if( !i1_s)      RETURN(0);
1119                 
1120                 physical = block_bmap(sb_bread (src->i_sb, i1_s),
1121                                        block >> (addr_per_block_bits * 2));
1122                 if(physical) {
1123                         block_setbmap(handle, sb_bread (dst->i_sb, i1_d),
1124                                       block >> (addr_per_block_bits * 2), physical);
1125                         block_setbmap(handle, sb_bread (src->i_sb, i1_s),
1126                                       block >> (addr_per_block_bits * 2), 0);
1127                         RETURN(1);
1128                 }
1129                 else
1130                         RETURN(0);
1131         }
1132         i3_d = block_bmap (sb_bread (dst->i_sb, i2_d),
1133                         (block >> addr_per_block_bits) & (addr_per_block - 1));
1134         if( i2_s) i3_s = block_bmap (sb_bread (src->i_sb, i2_s),
1135                         (block >> addr_per_block_bits) & (addr_per_block - 1));
1136         
1137         if (!i3_d) {
1138                 if (!i2_s)      RETURN(0);      
1139                 physical = block_bmap (sb_bread (src->i_sb, i2_s),
1140                         (block >> addr_per_block_bits) & (addr_per_block - 1));
1141                 if( physical) {
1142                         block_setbmap (handle, sb_bread (dst->i_sb, i2_d),
1143                                        (block >> addr_per_block_bits) & 
1144                                        (addr_per_block - 1), physical);
1145                         block_setbmap (handle, sb_bread (src->i_sb, i2_s),
1146                                        (block >> addr_per_block_bits) & 
1147                                        (addr_per_block - 1),0);
1148                         RETURN(1);
1149                 }
1150                 else
1151                         RETURN(0);
1152         }
1153         physical = block_bmap (sb_bread (dst->i_sb, i3_d),
1154                            block & (addr_per_block - 1)) ;
1155         if(physical)    
1156                 RETURN(0);
1157         else {
1158                 if(!i3_s)       
1159                         RETURN(0);      
1160                 physical = block_bmap(sb_bread(src->i_sb, i3_s),
1161                                       block & (addr_per_block - 1));
1162                 if(physical) {
1163                         block_setbmap (handle, sb_bread (dst->i_sb, i3_d),
1164                                        block & (addr_per_block - 1), physical);
1165                         block_setbmap (handle, sb_bread (src->i_sb, i3_s),
1166                                        block & (addr_per_block - 1), 0); 
1167                         RETURN(1);
1168                 }
1169                 else
1170                         RETURN(0); 
1171         }
1172 }
1173
1174 /* Generate i_blocks from blocks for an inode .
1175  * We also calculate EA block here.
1176  */
1177 static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1178 {
1179         /* 512 byte disk blocks per inode block */
1180         int bpib = inode->i_sb->s_blocksize >> 9;
1181         int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1182         unsigned long i_blocks = 0;
1183         int i=0, j=0, meta_blocks = 0;
1184         ENTRY;                                                                                                                                                                                                     
1185         if(!inode)    
1186                 RETURN(0);
1187         
1188         if( blocks < 0 ) {
1189                 /* re-calculate blocks here */
1190                 blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1191                           >> inode->i_sb->s_blocksize_bits;
1192         }
1193                                                                                                                                                                                                      
1194         /* calculate data blocks */
1195         for(i = 0; i < blocks; i++) {
1196                 if(ext3_bmap(inode->i_mapping, i))
1197                         i_blocks += bpib;
1198         }
1199         /* calculate meta blocks */
1200         blocks -= EXT3_NDIR_BLOCKS;
1201         if(blocks > 0) {
1202                 meta_blocks++;
1203                 blocks -= addr_per_block;
1204         }
1205         if( blocks > 0 ) meta_blocks++;
1206         i=0;
1207         
1208         while( (blocks > 0) && (i < addr_per_block) ) {
1209                 meta_blocks++;
1210                 blocks -= addr_per_block;
1211                 i++;
1212         }
1213         
1214         if ( blocks > 0 ) meta_blocks += 2;
1215         i=0; j=0;
1216         
1217         while( blocks > 0) {
1218                 meta_blocks++;
1219                 blocks -= addr_per_block;
1220                 i++;
1221                 if(i >= addr_per_block  ) {
1222                         i=0;
1223                         j++;
1224                 }
1225                 if( j >= addr_per_block) {
1226                         j=0;
1227                         meta_blocks++;
1228                 }
1229         }
1230         /* calculate EA blocks */
1231         if(ext3_has_ea(inode))       
1232                 meta_blocks++;
1233                                                                                                                                                                                                      
1234         i_blocks += meta_blocks * bpib;
1235         CDEBUG(D_INODE, "ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1236         
1237         RETURN(i_blocks);
1238 }
1239
1240 /**
1241  * fsfilt_ext3_destroy_indirect - delete an indirect inode from the table
1242  * @pri: primary inode
1243  * @ind: indirect inode
1244  * @index: index of inode that should be deleted
1245  *
1246  * We delete the @*ind inode, and remove it from the snapshot table.  If @*ind
1247  * is NULL, we use the inode at @index.
1248  */
1249 static int fsfilt_ext3_destroy_indirect(struct inode *pri, int index, 
1250                                         struct inode *next_ind)
1251 {
1252         char buf[EXT3_MAX_SNAP_DATA];
1253         struct snap_ea *snaps;
1254         struct inode *ind;
1255         int save = 0, i=0, err = 0;
1256         handle_t *handle=NULL;
1257         ENTRY;
1258
1259         if (index < 0 || index > EXT3_MAX_SNAPS)
1260                 RETURN(0);
1261
1262         if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1263                 CERROR("TRY TO DESTROY JOURNAL'S IND\n");
1264                 RETURN(-EINVAL);
1265         }
1266
1267         err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1268                              buf, EXT3_MAX_SNAP_DATA);
1269         if (err < 0) {
1270                 CERROR("inode %lu attribute read error\n", pri->i_ino);
1271                 RETURN(err);
1272         }
1273         
1274         snaps = (struct snap_ea *)buf;
1275         if ( !snaps->ino[index] ) {
1276                 CERROR("for pri ino %lu, index %d, redirect ino is 0\n",
1277                        pri->i_ino, index);      
1278                 RETURN(-EINVAL);
1279         }
1280
1281         CDEBUG(D_INODE, "for pri ino %lu, reading inode %lu at index %d\n", 
1282                pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1283
1284         ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]));
1285
1286         if ( !ind || IS_ERR(ind) || is_bad_inode(ind)) 
1287                 RETURN(-EINVAL);
1288
1289         CDEBUG(D_INODE, "iget ind %lu, ref count = %d\n", 
1290                ind->i_ino, atomic_read(&ind->i_count));
1291         
1292         EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_DESTROY_TRANS_BLOCKS, err);
1293         if (err) {
1294                 iput(ind);
1295                 RETURN(err);
1296         }
1297         /* if it's block level cow, first copy the blocks back */       
1298         if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1299             S_ISREG(pri->i_mode)) {
1300                 int blocks;
1301                 
1302                 if (!next_ind) {        
1303                         next_ind = pri;
1304                         down(&ind->i_sem);
1305                 } else {
1306                         double_lock_inode(next_ind, ind);
1307                 }
1308                 blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1) 
1309                           >> next_ind->i_sb->s_blocksize_bits;
1310
1311                 CDEBUG(D_INODE, "migrate block back from ino %lu to %lu\n",
1312                        ind->i_ino, next_ind->i_ino);
1313
1314                 for(i = 0; i < blocks; i++) {
1315                         if( ext3_bmap(next_ind->i_mapping, i) ) 
1316                                 continue;
1317                         if( !ext3_bmap(ind->i_mapping, i) ) 
1318                                 continue;
1319                         ext3_migrate_block(handle, next_ind, ind, i) ;
1320                 }
1321                 /* Now re-compute the i_blocks */
1322                 /* XXX shall we take care of ind here? probably not */
1323                 next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1324                 ext3_mark_inode_dirty(handle, next_ind);
1325
1326                 if (next_ind == pri) 
1327                         up(&ind->i_sem);
1328                 else 
1329                         double_unlock_inode(next_ind, ind);
1330         }
1331         
1332         CDEBUG(D_INODE, "delete indirect ino %lu\n", ind->i_ino);
1333         CDEBUG(D_INODE, "iput ind %lu, ref count = %d\n", ind->i_ino, 
1334                atomic_read(&ind->i_count));
1335         
1336         ind->i_nlink = 0;
1337         iput (ind);
1338
1339         snaps->ino[index] = cpu_to_le32(0);
1340         for (i = 0; i < EXT3_MAX_SNAPS; i++)
1341                 save += snaps->ino[i];
1342
1343
1344         /*Should we remove snap feature here*/
1345         /*
1346          * If we are deleting the last indirect inode, and the primary inode
1347          * has already been deleted, then mark the primary for deletion also.
1348          * Otherwise, if we are deleting the last indirect inode remove the
1349          * snaptable from the inode.    XXX
1350          */
1351         if (!save && EXT3_I(pri)->i_dtime) {
1352                 CDEBUG(D_INODE, "deleting primary %lu\n", pri->i_ino);
1353                 pri->i_nlink = 0;
1354                 /* reset err to 0 now */
1355                 err = 0;
1356         } else {
1357                 CDEBUG(D_INODE, "%s redirector table\n", 
1358                        save ? "saving" : "deleting");
1359                 err = ext3_xattr_set_handle(handle, pri, EXT3_SNAP_INDEX, 
1360                                             EXT3_SNAP_ATTR, save ? buf : NULL, 
1361                                             EXT3_MAX_SNAP_DATA, 0);
1362                 ext3_mark_inode_dirty(handle, pri);
1363         }
1364         journal_stop(handle);
1365         
1366         RETURN(err);
1367 }
1368
1369 /* restore a primary inode with the indirect inode at index */
1370 static int fsfilt_ext3_restore_indirect(struct inode *pri, int index)
1371 {
1372         struct inode *ind;
1373         int err = 0;
1374         handle_t *handle = NULL;
1375         ENTRY;
1376
1377         if (index < 0 || index > EXT3_MAX_SNAPS)
1378                 RETURN(-EINVAL);
1379
1380         if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1381                 CERROR("TRY TO RESTORE JOURNAL\n");
1382                 RETURN(-EINVAL);
1383         }
1384         CDEBUG(D_INODE, "pri ino %lu, index %d\n", pri->i_ino, index);
1385
1386         ind = fsfilt_ext3_get_indirect(pri, NULL, index);
1387
1388         if (!ind) 
1389                 RETURN(-EINVAL);
1390
1391         CDEBUG(D_INODE, "restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
1392
1393         EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_RESTORE_TRANS_BLOCKS, err); 
1394         if(err)
1395                 RETURN(err);
1396         /* first destroy all the data blocks in primary inode */
1397         /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
1398         err = ext3_throw_inode_data(handle, pri);
1399         if (err) {
1400                 CERROR("restore_indirect, new_inode err\n");
1401                 RETURN(err);
1402         }       
1403         double_lock_inode(pri, ind);
1404         ext3_migrate_data(handle, pri, ind);
1405         EXT3_I(pri)->i_flags &= ~EXT3_COW_FL;
1406         ext3_mark_inode_dirty(handle, pri);
1407         double_unlock_inode(pri, ind);
1408         iput(ind);
1409         
1410         //fsfilt_ext3_destroy_indirect(pri, index);
1411         journal_stop(handle);
1412         
1413         RETURN(err);
1414 }
1415
1416 /**
1417  * ext3_snap_iterate - iterate through all of the inodes
1418  * @sb: filesystem superblock
1419  * @repeat: pointer to function called on each valid inode
1420  * @start: inode to start iterating at
1421  * @priv: private data to the caller/repeat function
1422  *
1423  * If @start is NULL, then we do not return an inode pointer.  If @*start is
1424  * NULL, then we start at the beginning of the filesystem, and iterate over
1425  * all of the inodes in the system.  If @*start is non-NULL, then we start
1426  * iterating at this inode.
1427  *
1428  * We call the repeat function for each inode that is in use.  The repeat
1429  * function must check if this is a redirector (with is_redirector) if it
1430  * only wants to operate on redirector inodes.  If there is an error or
1431  * the repeat function returns non-zero, we return the last inode operated
1432  * on in the @*start parameter.  This allows the caller to restart the
1433  * iteration at this inode if desired, by returning a positive value.
1434  * Negative return values indicate an error.
1435  *
1436  * NOTE we cannot simply traverse the existing filesystem tree from the root
1437  *      inode, as there may be disconnected trees from deleted files/dirs
1438  *
1439  * FIXME If there was a list of inodes with EAs, we could simply walk the list
1440  * intead of reading every inode.  This is an internal implementation issue.
1441  */
1442
1443 static int ext3_iterate_all(struct super_block *sb,
1444                             int (*repeat)(struct inode *inode,void *priv),
1445                             struct inode **start, void *priv)
1446 {
1447         struct inode *tmp = NULL;
1448         int gstart, gnum, err = 0;
1449         ino_t istart, ibase;
1450         ENTRY;
1451
1452         if (!start)
1453                 start = &tmp;
1454         if (!*start) {
1455                 *start = iget(sb, EXT3_ROOT_INO);
1456                 if (!*start) 
1457                         GOTO(exit, err = -ENOMEM);
1458                 
1459                 if (is_bad_inode(*start)) 
1460                         GOTO(exit, err = -EIO);
1461         }
1462         if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
1463                 CERROR("invalid starting inode %ld\n",(*start)->i_ino);
1464                 GOTO(exit, err = -EINVAL); 
1465         }
1466         if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
1467                 if ((err = (*repeat)(*start, priv) != 0))
1468                         GOTO(exit, err);
1469                 iput(*start);
1470                 *start = iget(sb, EXT3_FIRST_INO(sb));
1471                 if (!*start)
1472                         GOTO(exit, err = -ENOMEM);
1473                 if (is_bad_inode(*start)) 
1474                         GOTO(exit, err = -EIO);
1475         }
1476
1477         gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
1478         istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
1479         ibase = gstart * EXT3_INODES_PER_GROUP(sb);
1480         for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
1481              gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
1482                 struct buffer_head *bitmap_bh = NULL;
1483                 struct ext3_group_desc * gdp;
1484                 ino_t  ino;
1485                 
1486                 gdp = ext3_get_group_desc (sb, gnum, NULL);
1487                 if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
1488                     EXT3_INODES_PER_GROUP(sb))
1489                         continue;
1490                 bitmap_bh = read_inode_bitmap(sb, gnum);
1491
1492                 if (!bitmap_bh)
1493                         continue;
1494                 ino = 0;
1495 repeat:
1496 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1497                 ino = find_next_bit((unsigned long *)bitmap_bh->b_data, 
1498                                     EXT3_INODES_PER_GROUP(sb), ino);
1499 #else
1500                 ino = find_next_bit((unsigned long *)bitmap_bh->b_data, 
1501                                     EXT3_INODES_PER_GROUP(sb), ino);
1502 #warning"FIXME-WANGDI need to port find_next_bit to 2.4" 
1503 #endif                
1504                 if (ino < EXT3_INODES_PER_GROUP(sb)) { 
1505                         ino_t inum = ino + gnum * EXT3_INODES_PER_GROUP(sb) + 1;
1506                         if (*start) {
1507                                 if (inum < (*start)->i_ino)
1508                                         continue;
1509                         } else {
1510                                 *start = iget(sb, inum);
1511                                 if (!*start) 
1512                                         GOTO(exit, err = -ENOMEM);
1513                                 if (is_bad_inode(*start)) 
1514                                         GOTO(exit, err = -EIO);
1515                         }
1516                         if ((err = (*repeat)(*start, priv)) != 0)
1517                                 GOTO(exit, err);
1518                         iput(*start);
1519                         *start = NULL;
1520                         if (++ino < EXT3_INODES_PER_GROUP(sb))
1521                                 goto repeat;
1522                 }
1523                 istart = 0;
1524         }
1525 exit:
1526         iput(tmp);
1527         RETURN(err);
1528 }
1529
1530 static int fsfilt_ext3_iterate(struct super_block *sb,
1531                                int (*repeat)(struct inode *inode, void *priv),
1532                                struct inode **start, void *priv, int flag)
1533 {
1534         switch(flag) {
1535                 case SNAP_ITERATE_ALL_INODE:
1536                         return ext3_iterate_all (sb, repeat, start, priv);
1537                 default:
1538                         return -EINVAL;
1539         }
1540 }
1541
1542 static int fsfilt_ext3_get_snap_info(struct inode *inode, void *key, 
1543                                      __u32 keylen, void *val, 
1544                                      __u32 *vallen) 
1545 {
1546         int rc = 0;
1547         ENTRY;
1548
1549         if (!vallen || !val) {
1550                 CERROR("val and val_size is 0!\n");
1551                 RETURN(-EFAULT);
1552         }
1553         if (keylen >= strlen(MAX_SNAPTABLE_COUNT) 
1554             && strcmp(key, MAX_SNAPTABLE_COUNT) == 0) {
1555                 /*FIXME should get it from the EA_size*/
1556                *((__u32 *)val) = EXT3_MAX_SNAPS; 
1557                *vallen = sizeof(int);
1558                RETURN(rc);
1559         } else if (keylen >= strlen(SNAPTABLE_INFO) 
1560                    && strcmp(key, SNAPTABLE_INFO) == 0) {
1561                 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX, 
1562                                     EXT3_SNAPTABLE_EA, val, *vallen); 
1563                 RETURN(rc);
1564         } else if (keylen >= strlen(SNAP_GENERATION) 
1565                    && strcmp(key, SNAP_GENERATION) == 0) {
1566                 
1567                 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1568                                     EXT3_SNAP_GENERATION, (char *)val, *vallen);
1569                 if (rc == -ENODATA) {
1570                         *((__u32 *)val) = 0; 
1571                         *vallen = sizeof(int);
1572                         rc = 0;
1573                 }
1574                 RETURN(rc);
1575         } else if (keylen >= strlen(SNAP_COUNT) && 
1576                    strcmp(key, SNAP_COUNT) == 0) {
1577                 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1578                                     EXT3_SNAP_COUNT, val, *vallen);
1579                 if (rc == -ENODATA) {
1580                         *((__u32 *)val) = 0; 
1581                         *vallen = sizeof(int);
1582                         rc = 0;
1583                 }
1584                 RETURN(rc);
1585         }
1586  
1587         RETURN(-EINVAL);
1588
1589
1590 static int fsfilt_ext3_set_snap_info(struct inode *inode, void *key, 
1591                                      __u32 keylen, void *val, 
1592                                      __u32 *vallen)
1593 {
1594         int rc = 0;
1595         ENTRY;
1596         
1597         if (!vallen || !val) {
1598                 CERROR("val and val_size is 0!\n");
1599                 RETURN(-EFAULT);
1600         }
1601
1602         if (keylen >= strlen(SNAPTABLE_INFO) 
1603             && strcmp(key, SNAPTABLE_INFO) == 0) {
1604                 handle_t *handle;
1605                 EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS,
1606                                     rc); 
1607                 if(rc)
1608                         RETURN(rc);
1609                 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX, 
1610                                            EXT3_SNAPTABLE_EA, val, *vallen, 0); 
1611                 journal_stop(handle);
1612                 
1613                 RETURN(rc);
1614         } else if (keylen >= strlen(SNAP_GENERATION) 
1615                    && strcmp(key, SNAP_GENERATION) == 0) {
1616                 LASSERT(inode);
1617                 rc = ext3_set_generation(inode, *(int*)val);
1618                 
1619                 RETURN(rc); 
1620         } else if (keylen >= strlen(SNAP_COUNT) && 
1621                    (strcmp(key, SNAP_COUNT) == 0)) {
1622                 handle_t *handle;
1623                 EXT3_JOURNAL_START(inode->i_sb, handle, 
1624                                    EXT3_XATTR_TRANS_BLOCKS, rc); 
1625                 if(rc)
1626                         RETURN(rc);
1627                 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX, 
1628                                            EXT3_SNAP_COUNT, val, *vallen, 0); 
1629                 journal_stop(handle);
1630                 
1631                 RETURN(rc);
1632         } else if (keylen >= strlen(SNAP_ROOT_INO) && 
1633                    (strcmp(key, SNAP_ROOT_INO) == 0)) {
1634         
1635
1636
1637
1638         }       
1639  
1640         RETURN(-EINVAL);
1641 }
1642 static int fsfilt_ext3_dir_ent_size(char *name)
1643 {
1644         if (name) {
1645                 return EXT3_DIR_REC_LEN(strlen(name));
1646         }
1647         return 0;
1648 }
1649
1650 static int fsfilt_ext3_set_dir_ent(struct super_block *sb, char *name, 
1651                                    char *buf, int buf_off, int nlen, size_t count)
1652 {
1653         int rc = 0; 
1654         ENTRY;
1655         if (buf_off == 0 && nlen == 0) {
1656                 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *)buf;  
1657                 LASSERT(count == PAGE_CACHE_SIZE);
1658                 de->rec_len = count;
1659                 de->inode = 0;
1660                 RETURN(rc);
1661         } else {
1662                 struct ext3_dir_entry_2 *de, *de1; 
1663                 de = (struct ext3_dir_entry_2 *)(buf + buf_off - nlen); 
1664                 de1 = (struct ext3_dir_entry_2 *)(buf + buf_off); 
1665                 int rlen, nlen;
1666  
1667                 LASSERT(nlen == EXT3_DIR_REC_LEN_DE(de));
1668                 
1669                 rlen = le16_to_cpu(de->rec_len);
1670                 de->rec_len = cpu_to_le16(nlen);
1671                 
1672                 de1->rec_len = cpu_to_le16(rlen - nlen);
1673                 de1->name_len = strlen(name);
1674                 memcpy (de1->name, name, de->name_len);
1675                 nlen = EXT3_DIR_REC_LEN_DE(de1); 
1676                 RETURN(nlen);
1677         }        
1678
1679 }
1680 struct fsfilt_operations fsfilt_ext3_snap_ops = {
1681         .fs_type                = "ext3_snap",
1682         .fs_owner               = THIS_MODULE,
1683         .fs_create_indirect     = fsfilt_ext3_create_indirect,
1684         .fs_get_indirect        = fsfilt_ext3_get_indirect,
1685         .fs_set_indirect        = fsfilt_ext3_set_indirect,
1686         .fs_snap_feature        = fsfilt_ext3_snap_feature,
1687         .fs_is_redirector       = fsfilt_ext3_is_redirector,
1688         .fs_is_indirect         = fsfilt_ext3_is_indirect,
1689         .fs_get_indirect_ino    = fsfilt_ext3_get_indirect_ino,
1690         .fs_destroy_indirect    = fsfilt_ext3_destroy_indirect,
1691         .fs_restore_indirect    = fsfilt_ext3_restore_indirect,
1692         .fs_iterate             = fsfilt_ext3_iterate,
1693         .fs_copy_block          = fsfilt_ext3_copy_block,
1694         .fs_set_snap_info       = fsfilt_ext3_set_snap_info,
1695         .fs_get_snap_info       = fsfilt_ext3_get_snap_info,
1696         .fs_dir_ent_size        = fsfilt_ext3_dir_ent_size,
1697         .fs_set_dir_ent         = fsfilt_ext3_set_dir_ent,
1698 };
1699
1700
1701 static int __init fsfilt_ext3_snap_init(void)
1702 {
1703         int rc;
1704
1705         rc = fsfilt_register_ops(&fsfilt_ext3_snap_ops);
1706
1707         return rc;
1708 }
1709
1710 static void __exit fsfilt_ext3_snap_exit(void)
1711 {
1712
1713         fsfilt_unregister_ops(&fsfilt_ext3_snap_ops);
1714 }
1715
1716 module_init(fsfilt_ext3_snap_init);
1717 module_exit(fsfilt_ext3_snap_exit);
1718
1719 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1720 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1721 MODULE_LICENSE("GPL");