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