1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre filesystem abstraction routines
6 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
7 * Author: Andreas Dilger <adilger@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 #define DEBUG_SUBSYSTEM S_FILTER
26 #include <linux/init.h>
27 #include <linux/module.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>
43 #include <ext3/xattr.h>
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>
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 */
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"
65 #define SB_FEATURE_COMPAT(sb) (EXT3_SB(sb)->s_es->s_feature_compat)
67 #define SNAP_HAS_COMPAT_FEATURE(sb,mask) \
68 (SB_FEATURE_COMPAT(sb) & cpu_to_le32(mask))
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"
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))
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)))
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
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)
108 #define EXT3_JOURNAL_START(sb, handle, blocks, rc) \
110 journal_t *journal; \
111 journal = EXT3_SB(sb)->s_journal; \
113 handle = journal_start(journal, blocks); \
115 if(IS_ERR(handle)) { \
116 CERROR("can't start transaction\n"); \
117 rc = PTR_ERR(handle); \
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
124 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
129 double_down(&i1->i_sem, &i2->i_sem);
131 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
136 double_up(&i1->i_sem, &i2->i_sem);
139 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
141 struct semaphore *s1 = &i1->i_sem;
142 struct semaphore *s2 = &i2->i_sem;
145 if ((unsigned long) s1 < (unsigned long) s2) {
146 struct semaphore *tmp = s2;
154 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
156 struct semaphore *s1 = &i1->i_sem;
157 struct semaphore *s2 = &i2->i_sem;
166 /* helper functions to manipulate field 'parent' in snap_ea */
168 set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
170 char * p = (char*) pea;
173 offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
174 offset += sizeof(ino_t) * index;
175 *(ino_t*)(p+offset) = val;
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
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.
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.
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.
198 static struct inode *fsfilt_ext3_get_indirect(struct inode *primary, int *table,
201 char buf[EXT3_MAX_SNAP_DATA];
202 struct snap_ea *snaps;
204 struct inode *inode = NULL;
209 if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
212 CDEBUG(D_INODE, "ino %lu, table %p, slot %d\n", primary->i_ino, table,
214 rc = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
216 if (rc == -ENODATA) {
219 CERROR("attribute read rc=%d \n", rc);
222 snaps = (struct snap_ea *)buf;
224 /* if table is NULL and there is a slot */
225 if( !table && slot >= 0) {
226 ino = le32_to_cpu(snaps->ino[slot]);
228 inode = iget(primary->i_sb, ino);
231 /* if table is not NULL */
232 while (!inode && slot >= 0 ) {
233 ino = le32_to_cpu(snaps->ino[slot]);
235 CDEBUG(D_INODE, "snap inode at slot %d is %lu\n", slot, ino);
240 inode = iget(primary->i_sb, ino);
243 if(slot == -1 && table) {
244 CDEBUG(D_INODE, "redirector not found, using primary\n");
245 inode = iget(primary->i_sb, primary->i_ino);
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,
255 char buf[EXT3_MAX_SNAP_DATA];
256 struct snap_ea *snaps;
257 int rc = 0, inlist = 1;
259 handle_t *handle = NULL;
262 CDEBUG(D_INODE, "(ino %lu, parent %lu): saving ind %lu to index %d\n",
263 pri->i_ino, parent_ino, ind_ino, index);
265 if (index < 0 || index > MAX_SNAPS || !pri)
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);
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?
281 } else if (rc < 0 || rc > EXT3_MAX_SNAP_DATA) {
282 GOTO(out_unlock, rc);
284 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_SETIND_TRANS_BLOCKS, rc);
286 GOTO(out_unlock, rc = PTR_ERR(handle));
288 snaps = (struct snap_ea *)buf;
289 snaps->ino[index] = cpu_to_le32 (ind_ino);
290 ea_size = EXT3_MAX_SNAP_DATA;
292 set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
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);
302 static int ext3_set_generation(struct inode *inode, unsigned long gen)
308 EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS, err);
312 err = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
313 EXT3_SNAP_GENERATION, (char*)&gen,
316 CERROR("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
320 journal_stop(handle);
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)?
328 static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
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;
341 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
342 dst->i_attr_flags = src->i_attr_flags;
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;
352 if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
357 if (ext3_xattr_list(src, names, 0) < 0)
360 * the list of attribute names are stored as NUL terminated
361 * strings, with a double NUL string at the end.
364 while ((namelen = strlen(name))) {
368 /* don't copy snap data */
369 if (!strcmp(name, EXT3_SNAP_ATTR)) {
370 CDEBUG(D_INFO, "skipping %s item\n", name);
373 CDEBUG(D_INODE, "copying %s item\n", name);
374 attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX,
375 EXT3_SNAP_ATTR, NULL, 0);
378 OBD_ALLOC(buf, attrlen);
383 if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
384 EXT3_SNAP_ATTR, buf, attrlen) < 0)
386 if (ext3_xattr_set_handle(handle, dst, EXT3_SNAP_INDEX,
387 EXT3_SNAP_ATTR, buf, attrlen,
390 OBD_FREE(buf, attrlen);
391 name += namelen + 1; /* skip name and trailing NUL */
395 static int ext3_copy_reg_block(struct inode *dst, struct inode *src, int blk)
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;
403 /*read the src page*/
404 src_page = grab_cache_page(src->i_mapping, index);
405 if (src_page == NULL)
408 if (!PageUptodate(src_page)) {
409 rc = src->i_mapping->a_ops->readpage(NULL, src_page);
411 page_cache_release(src_page);
418 dst_page = grab_cache_page(dst->i_mapping, index);
419 if (dst_page == NULL)
420 GOTO(src_page_unlock, rc = -ENOMEM);
423 rc = dst->i_mapping->a_ops->prepare_write(NULL, dst_page, 0,
424 PAGE_CACHE_SIZE - 1);
426 GOTO(dst_page_unlock, rc = -EFAULT);
427 memcpy(page_address(dst_page), page_address(src_page), PAGE_CACHE_SIZE);
429 flush_dcache_page(dst_page);
431 rc = dst->i_mapping->a_ops->commit_write(NULL, dst_page, 0,
432 PAGE_CACHE_SIZE - 1);
437 unlock_page(dst_page);
438 page_cache_release(dst_page);
441 page_cache_release(src_page);
444 static int ext3_copy_dir_block(struct inode *dst, struct inode *src, int blk)
446 struct buffer_head *bh_dst = NULL, *bh_src = NULL;
448 handle_t *handle = NULL;
451 EXT3_JOURNAL_START(dst->i_sb, handle, SNAP_COPYBLOCK_TRANS_BLOCKS, rc);
455 bh_src = ext3_bread(handle, src, blk, 0, &rc);
457 CERROR("rcor for src blk %d, rcor %d\n", blk, rc);
458 GOTO(exit_relese, rc);
460 bh_dst = ext3_getblk(handle, dst, blk, 1, &rc);
462 CERROR("rcor for dst blk %d, rcor %d\n", blk, rc);
463 GOTO(exit_relese, rc);
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);
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);
474 if (bh_src) brelse(bh_src);
475 if (bh_dst) brelse(bh_dst);
477 journal_stop(handle);
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.
484 1: The block has been copied successfully
485 0: No block is copied, usually this is because src has no such blk
489 static int fsfilt_ext3_copy_block (struct inode *dst, struct inode *src, int blk)
493 CDEBUG(D_INODE, "copy blk %d from %lu to %lu \n", blk, src->i_ino,
496 * ext3_getblk() require handle!=NULL
498 if (S_ISREG(src->i_mode)) {
499 rc = ext3_copy_reg_block(dst, src, blk);
501 rc = ext3_copy_dir_block(dst, src, blk);
507 static inline int ext3_has_ea(struct inode *inode)
509 return (EXT3_I(inode)->i_file_acl != 0);
511 /* XXXThis function has a very bad effect to
512 * the performance of filesystem,
513 * will find another way to fix it
515 static void fs_flushinval_pages(handle_t *handle, struct inode* inode)
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);
521 truncate_inode_pages(inode->i_mapping, 0);
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
533 static int ext3_migrate_data(handle_t *handle, struct inode *dst,
536 unsigned long err = 0;
537 /* 512 byte disk blocks per inode block */
538 int bpib = src->i_sb->s_blocksize >> 9;
545 if (dst->i_ino == src->i_ino)
548 fs_flushinval_pages(handle, src);
550 ext3_copy_meta(handle, dst, src);
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 */
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));
560 ext3_discard_prealloc(src);
562 dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
563 src->i_size = EXT3_I(src)->i_disksize = 0;
565 dst->i_blocks = src->i_blocks;
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;
574 if( ext3_has_ea (dst))
575 dst->i_blocks += bpib;
578 CDEBUG(D_INODE, "migrate data from ino %lu to ino %lu\n", src->i_ino,
580 ext3_mark_inode_dirty(handle, src);
581 ext3_mark_inode_dirty(handle, dst);
585 static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
586 struct inode *src, int *has_orphan)
588 unsigned long blocks, blk, cur_blks;
589 int low_credits, save_ref;
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;
597 CDEBUG(D_INODE, "%lu blocks need to be copied,low credits limit %d\n",
598 blocks, low_credits);
600 for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
601 if (!ext3_bmap(src->i_mapping, blk))
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");
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);
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);
624 * We can be sure the last handle was stoped
625 * ONLY if the handle's reference count is 1
627 save_ref = handle->h_ref;
629 if(journal_stop(handle) ){
630 CERROR("fail to stop journal\n");
634 EXT3_JOURNAL_START(dst->i_sb, handle,
635 low_credits + needed, err);
637 handle->h_ref = save_ref;
640 if (fsfilt_ext3_copy_block( dst, src, blk) < 0 )
642 cur_blks += dst->i_sb->s_blocksize / 512;
645 dst->i_size = EXT3_I(dst)->i_disksize = src->i_size;
648 /*Here delete the data of that pri inode
649 *FIXME later, should throw the blocks of
650 *primary inode directly
652 static int ext3_throw_inode_data(handle_t *handle, struct inode *inode)
654 struct inode *tmp = NULL;
656 tmp = ext3_new_inode(handle, inode, (int)inode->i_mode, 0);
658 CERROR("ext3_new_inode error\n");
661 double_lock_inode(inode, tmp);
662 ext3_migrate_data(handle, tmp, inode);
663 double_unlock_inode(inode, tmp);
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
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.
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.
683 static struct inode* fsfilt_ext3_create_indirect(struct inode *pri, int index,
685 struct inode* parent,
688 struct inode *ind = NULL;
689 handle_t *handle = NULL;
694 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
695 CERROR("TRY TO COW JOUNRAL\n");
696 RETURN(ERR_PTR(-EINVAL));
698 CDEBUG(D_INODE, "creating indirect inode for %lu at index %d, %s pri\n",
699 pri->i_ino, index, del ? "deleting" : "preserve");
701 ind = fsfilt_ext3_get_indirect(pri, NULL, index);
703 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_CREATEIND_TRANS_BLOCKS,
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.
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
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"
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);
725 EXT3_I(pri)->i_dtime = LTIME_S(CURRENT_TIME);
726 ext3_mark_inode_dirty(handle, pri);
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);
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);
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;
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));
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.
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.
763 CDEBUG(D_INODE, "deleting primary inode\n");
766 err = ext3_migrate_data(handle, ind, pri);
768 GOTO(exit_unlock, err);
770 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
772 GOTO(exit_unlock, err);
774 /* XXX for directory, we copy the block back
775 * or ext3_rmdir will report errors "bad dir, no data blocks"
777 if( S_ISDIR(pri->i_mode)) {
778 handle = ext3_copy_data(handle, pri, ind, &has_orphan);
780 GOTO(exit_unlock, err= -EINVAL);
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);
793 err = ext3_migrate_data(handle, ind, pri);
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)) {
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;
806 int bpib = pri->i_sb->s_blocksize >> 9;
807 CDEBUG(D_INODE, "ino %lu, do file cow\n", pri->i_ino);
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 )
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;
824 handle = ext3_copy_data(handle, pri, ind, &has_orphan);
826 GOTO(exit_unlock, err);
829 /* set cow flag for ind */
830 EXT3_I(ind)->i_flags |= EXT3_COW_FL;
831 EXT3_I(pri)->i_flags &= ~EXT3_COW_FL;
833 ext3_mark_inode_dirty(handle, pri);
834 ext3_mark_inode_dirty(handle, ind);
836 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
838 GOTO(exit_unlock, err);
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);
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);
857 journal_stop(handle);
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);
871 journal_stop(handle);
873 RETURN(ERR_PTR(err));
876 static int fsfilt_ext3_snap_feature (struct super_block *sb, int feature, int op) {
883 case SNAP_SET_FEATURE:
884 case SNAP_CLEAR_FEATURE:
885 EXT3_JOURNAL_START(sb, handle, 1, rc);
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);
893 SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature);
895 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
897 journal_stop(handle);
899 case SNAP_HAS_FEATURE:
900 /*FIXME should lock super or not*/
901 rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
909 * is_redirector - determines if a primary inode is a redirector
910 * @inode: primary inode to test
912 * Returns 1 if the inode is a redirector, 0 otherwise.
914 static int fsfilt_ext3_is_redirector(struct inode *inode)
916 int is_redirector = 0;
920 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
922 if (rc > 0 && rc <= MAX_SNAP_DATA)
924 CDEBUG(D_INODE, "inode %lu %s redirector\n", inode->i_ino,
925 is_redirector ? "is" : "isn't");
926 RETURN(is_redirector);
928 /*if it's indirect inode or not */
929 static int fsfilt_ext3_is_indirect(struct inode *inode)
931 if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
937 /* get the indirect ino at index of the primary inode
938 * return value: postive: indirect ino number
939 * negative or 0: error
941 static ino_t fsfilt_ext3_get_indirect_ino(struct super_block *sb,
942 ino_t primary_ino, int index)
944 char buf[EXT3_MAX_SNAP_DATA];
945 struct inode *primary = NULL;
946 struct snap_ea *snaps;
950 if (index < 0 || index > EXT3_MAX_SNAPS)
952 primary = iget(sb, primary_ino);
956 CERROR("attribute read error=%d", err);
957 GOTO (err_free, ino = err);
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);
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);
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)
987 tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
992 static inline int block_setbmap(handle_t *handle, struct buffer_head * bh,
993 int nr, int physical)
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);
1005 static int ext3_migrate_block(handle_t *handle, struct inode * dst,
1006 struct inode *src, int block)
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);
1015 CWARN("ext3_migrate_block block < 0 %p \n", src->i_sb);
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);
1024 /* EXT3_NDIR_BLOCK */
1025 if (block < EXT3_NDIR_BLOCKS) {
1026 if(inode_bmap(dst, block))
1029 if( (physical = inode_bmap(src, block)) ) {
1030 inode_setbmap (dst, block, physical);
1031 inode_setbmap (src, block, 0);
1038 /* EXT3_IND_BLOCK */
1039 block -= EXT3_NDIR_BLOCKS;
1040 if (block < addr_per_block) {
1041 i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1043 physical = inode_bmap(src, EXT3_IND_BLOCK);
1045 inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1046 inode_setbmap (src, EXT3_IND_BLOCK, 0);
1052 if(block_bmap(sb_bread(dst->i_sb, i1_d), block))
1055 i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1056 if( !i1_s) RETURN(0);
1058 physical = block_bmap(sb_bread(src->i_sb, i1_s), block);
1061 block_setbmap(handle, sb_bread(dst->i_sb, i1_d),block,
1063 block_setbmap(handle, sb_bread(src->i_sb, i1_s),block,0);
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);
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);
1083 i2_d = block_bmap (sb_bread (dst->i_sb, i1_d),
1084 block >> addr_per_block_bits);
1088 if(!i1_s) RETURN(0);
1090 physical = block_bmap(sb_bread (src->i_sb, i1_s),
1091 block >> addr_per_block_bits);
1093 block_setbmap(handle, sb_bread(dst->i_sb, i1_d),
1094 block >> addr_per_block_bits,
1096 block_setbmap(handle, sb_bread(src->i_sb, i1_s),
1097 block >> addr_per_block_bits, 0);
1103 physical = block_bmap(sb_bread(dst->i_sb, i2_d),
1104 block & (addr_per_block - 1));
1108 i2_s = block_bmap (sb_bread(src->i_sb, i1_s),
1109 block >> addr_per_block_bits);
1110 if(!i2_s) RETURN(0);
1112 physical = block_bmap(sb_bread(src->i_sb, i2_s),
1113 block & (addr_per_block - 1));
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);
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);
1131 if((physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1132 inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1136 i2_d = block_bmap(sb_bread (dst->i_sb, i1_d),
1137 block >> (addr_per_block_bits * 2));
1139 if(i1_s) i2_s = block_bmap(sb_bread(src->i_sb, i1_s),
1140 block >> (addr_per_block_bits * 2));
1143 if( !i1_s) RETURN(0);
1145 physical = block_bmap(sb_bread (src->i_sb, i1_s),
1146 block >> (addr_per_block_bits * 2));
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);
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));
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));
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);
1178 physical = block_bmap (sb_bread (dst->i_sb, i3_d),
1179 block & (addr_per_block - 1)) ;
1185 physical = block_bmap(sb_bread(src->i_sb, i3_s),
1186 block & (addr_per_block - 1));
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);
1199 /* Generate i_blocks from blocks for an inode .
1200 * We also calculate EA block here.
1202 static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
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;
1214 /* re-calculate blocks here */
1215 blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1216 >> inode->i_sb->s_blocksize_bits;
1219 /* calculate data blocks */
1220 for(i = 0; i < blocks; i++) {
1221 if(ext3_bmap(inode->i_mapping, i))
1224 /* calculate meta blocks */
1225 blocks -= EXT3_NDIR_BLOCKS;
1228 blocks -= addr_per_block;
1230 if( blocks > 0 ) meta_blocks++;
1233 while( (blocks > 0) && (i < addr_per_block) ) {
1235 blocks -= addr_per_block;
1239 if ( blocks > 0 ) meta_blocks += 2;
1242 while( blocks > 0) {
1244 blocks -= addr_per_block;
1246 if(i >= addr_per_block ) {
1250 if( j >= addr_per_block) {
1255 /* calculate EA blocks */
1256 if(ext3_has_ea(inode))
1259 i_blocks += meta_blocks * bpib;
1260 CDEBUG(D_INODE, "ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
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
1271 * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1272 * is NULL, we use the inode at @index.
1274 static int fsfilt_ext3_destroy_indirect(struct inode *pri, int index,
1275 struct inode *next_ind)
1277 char buf[EXT3_MAX_SNAP_DATA];
1278 struct snap_ea *snaps;
1280 int save = 0, i=0, err = 0;
1281 handle_t *handle=NULL;
1284 if (index < 0 || index > EXT3_MAX_SNAPS)
1287 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1288 CERROR("TRY TO DESTROY JOURNAL'S IND\n");
1292 err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1293 buf, EXT3_MAX_SNAP_DATA);
1295 CERROR("inode %lu attribute read error\n", pri->i_ino);
1299 snaps = (struct snap_ea *)buf;
1300 if ( !snaps->ino[index] ) {
1301 CERROR("for pri ino %lu, index %d, redirect ino is 0\n",
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);
1309 ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]));
1311 if ( !ind || IS_ERR(ind) || is_bad_inode(ind))
1314 CDEBUG(D_INODE, "iget ind %lu, ref count = %d\n",
1315 ind->i_ino, atomic_read(&ind->i_count));
1317 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_DESTROY_TRANS_BLOCKS, err);
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)) {
1331 double_lock_inode(next_ind, ind);
1333 blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1)
1334 >> next_ind->i_sb->s_blocksize_bits;
1336 CDEBUG(D_INODE, "migrate block back from ino %lu to %lu\n",
1337 ind->i_ino, next_ind->i_ino);
1339 for(i = 0; i < blocks; i++) {
1340 if( ext3_bmap(next_ind->i_mapping, i) )
1342 if( !ext3_bmap(ind->i_mapping, i) )
1344 ext3_migrate_block(handle, next_ind, ind, i) ;
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);
1351 if (next_ind == pri)
1354 double_unlock_inode(next_ind, ind);
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));
1364 snaps->ino[index] = cpu_to_le32(0);
1365 for (i = 0; i < EXT3_MAX_SNAPS; i++)
1366 save += snaps->ino[i];
1369 /*Should we remove snap feature here*/
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
1376 if (!save && EXT3_I(pri)->i_dtime) {
1377 CDEBUG(D_INODE, "deleting primary %lu\n", pri->i_ino);
1379 /* reset err to 0 now */
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);
1389 journal_stop(handle);
1394 /* restore a primary inode with the indirect inode at index */
1395 static int fsfilt_ext3_restore_indirect(struct inode *pri, int index)
1399 handle_t *handle = NULL;
1402 if (index < 0 || index > EXT3_MAX_SNAPS)
1405 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1406 CERROR("TRY TO RESTORE JOURNAL\n");
1409 CDEBUG(D_INODE, "pri ino %lu, index %d\n", pri->i_ino, index);
1411 ind = fsfilt_ext3_get_indirect(pri, NULL, index);
1416 CDEBUG(D_INODE, "restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
1418 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_RESTORE_TRANS_BLOCKS, 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);
1425 CERROR("restore_indirect, new_inode err\n");
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);
1435 //fsfilt_ext3_destroy_indirect(pri, index);
1436 journal_stop(handle);
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
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.
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.
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
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.
1468 static int ext3_iterate_all(struct super_block *sb,
1469 int (*repeat)(struct inode *inode,void *priv),
1470 struct inode **start, void *priv)
1472 struct inode *tmp = NULL;
1473 int gstart, gnum, err = 0;
1474 ino_t istart, ibase;
1480 *start = iget(sb, EXT3_ROOT_INO);
1482 GOTO(exit, err = -ENOMEM);
1484 if (is_bad_inode(*start))
1485 GOTO(exit, err = -EIO);
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);
1491 if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
1492 if ((err = (*repeat)(*start, priv) != 0))
1495 *start = iget(sb, EXT3_FIRST_INO(sb));
1497 GOTO(exit, err = -ENOMEM);
1498 if (is_bad_inode(*start))
1499 GOTO(exit, err = -EIO);
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;
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))
1515 bitmap_bh = read_inode_bitmap(sb, gnum);
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);
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"
1529 if (ino < EXT3_INODES_PER_GROUP(sb)) {
1530 ino_t inum = ino + gnum * EXT3_INODES_PER_GROUP(sb) + 1;
1532 if (inum < (*start)->i_ino)
1535 *start = iget(sb, inum);
1537 GOTO(exit, err = -ENOMEM);
1538 if (is_bad_inode(*start))
1539 GOTO(exit, err = -EIO);
1541 if ((err = (*repeat)(*start, priv)) != 0)
1545 if (++ino < EXT3_INODES_PER_GROUP(sb))
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)
1560 case SNAP_ITERATE_ALL_INODE:
1561 return ext3_iterate_all (sb, repeat, start, priv);
1567 static int fsfilt_ext3_get_snap_info(struct inode *inode, void *key,
1568 __u32 keylen, void *val,
1574 if (!vallen || !val) {
1575 CERROR("val and val_size is 0!\n");
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);
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);
1589 } else if (keylen >= strlen(SNAP_GENERATION)
1590 && strcmp(key, SNAP_GENERATION) == 0) {
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);
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);
1618 } else if (keylen >= strlen(SNAP_ROOT_INO) &&
1619 (strcmp(key, SNAP_ROOT_INO) == 0)) {
1621 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1622 EXT3_SNAP_ROOT_INO, val, *vallen);
1632 static int fsfilt_ext3_set_snap_info(struct inode *inode, void *key,
1633 __u32 keylen, void *val,
1639 if (!vallen || !val) {
1640 CERROR("val and val_size is 0!\n");
1644 if (keylen >= strlen(SNAPTABLE_INFO)
1645 && strcmp(key, SNAPTABLE_INFO) == 0) {
1647 EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS,
1651 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1652 EXT3_SNAPTABLE_EA, val, *vallen, 0);
1653 journal_stop(handle);
1656 } else if (keylen >= strlen(SNAP_GENERATION)
1657 && strcmp(key, SNAP_GENERATION) == 0) {
1659 rc = ext3_set_generation(inode, *(int*)val);
1662 } else if (keylen >= strlen(SNAP_COUNT) &&
1663 (strcmp(key, SNAP_COUNT) == 0)) {
1665 EXT3_JOURNAL_START(inode->i_sb, handle,
1666 EXT3_XATTR_TRANS_BLOCKS, rc);
1669 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1670 EXT3_SNAP_COUNT, val, *vallen, 0);
1671 journal_stop(handle);
1674 } else if (keylen >= strlen(SNAP_ROOT_INO) &&
1675 (strcmp(key, SNAP_ROOT_INO) == 0)) {
1677 EXT3_JOURNAL_START(inode->i_sb, handle,
1678 EXT3_XATTR_TRANS_BLOCKS, rc);
1681 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1682 EXT3_SNAP_ROOT_INO, val, *vallen, 0);
1683 journal_stop(handle);
1690 static int fsfilt_ext3_dir_ent_size(char *name)
1693 return EXT3_DIR_REC_LEN(strlen(name));
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)
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;
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);
1715 rlen = le16_to_cpu(de->rec_len);
1716 de->rec_len = cpu_to_le16(nlen);
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));
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,
1748 static int __init fsfilt_ext3_snap_init(void)
1752 rc = fsfilt_register_ops(&fsfilt_ext3_snap_ops);
1757 static void __exit fsfilt_ext3_snap_exit(void)
1760 fsfilt_unregister_ops(&fsfilt_ext3_snap_ops);
1763 module_init(fsfilt_ext3_snap_init);
1764 module_exit(fsfilt_ext3_snap_exit);
1766 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1767 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1768 MODULE_LICENSE("GPL");