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 <linux/kp30.h>
47 #include <linux/lustre_fsfilt.h>
48 #include <linux/obd.h>
49 #include <linux/obd_class.h>
50 #include <linux/lustre_smfs.h>
51 #include <linux/lustre_snap.h>
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_JOURNAL_START(sb, handle, blocks, rc) \
85 journal = EXT3_SB(sb)->s_journal; \
87 handle = journal_start(journal, blocks); \
89 if(IS_ERR(handle)) { \
90 CERROR("can't start transaction\n"); \
91 rc = PTR_ERR(handle); \
97 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
98 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
103 double_down(&i1->i_sem, &i2->i_sem);
105 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
110 double_up(&i1->i_sem, &i2->i_sem);
113 static inline void double_lock_inode(struct inode *i1, struct inode *i2)
115 struct semaphore *s1 = &i1->i_sem;
116 struct semaphore *s2 = &i2->i_sem;
119 if ((unsigned long) s1 < (unsigned long) s2) {
120 struct semaphore *tmp = s2;
128 static inline void double_unlock_inode(struct inode *i1, struct inode *i2)
130 struct semaphore *s1 = &i1->i_sem;
131 struct semaphore *s2 = &i2->i_sem;
140 /* helper functions to manipulate field 'parent' in snap_ea */
142 set_parent_ino(struct snap_ea *pea, int size, int index, ino_t val)
144 char * p = (char*) pea;
147 offset = sizeof(ino_t)*2 + (size - sizeof(ino_t)*2)/2;
148 offset += sizeof(ino_t) * index;
149 *(ino_t*)(p+offset) = val;
154 * fsfilt_ext3_get_indirect - get a specific indirect inode from a primary inode
155 * @primary: primary (direct) inode
156 * @table: table of @slot + 1 indices in reverse chronological order
157 * @slot: starting slot number to check for indirect inode number
159 * We locate an indirect inode from a primary inode using the redirection
160 * table stored in the primary inode. Because the desired inode may actually
161 * be in a "newer" slot number than the supplied slot, we are given a table
162 * of indices in chronological order to search for the correct inode number.
163 * We walk table from @slot to 0 looking for a non-zero inode to load.
165 * To only load a specific index (and fail if it does not exist), you can
166 * pass @table = NULL, and the index number in @slot. If @slot == 0, the
167 * primary inode data is returned.
169 * We return a pointer to an inode, or an error. If the indirect inode for
170 * the given index does not exist, NULL is returned.
172 static struct inode *fsfilt_ext3_get_indirect(struct inode *primary, int *table,
175 char buf[EXT3_MAX_SNAP_DATA];
176 struct snap_ea *snaps;
178 struct inode *inode = NULL;
183 if (slot < 0 || slot > EXT3_MAX_SNAPS || !primary)
186 CDEBUG(D_INODE, "ino %lu, table %p, slot %d\n", primary->i_ino, table,
188 rc = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR, buf,
190 if (rc == -ENODATA) {
193 CERROR("attribute read rc=%d \n", rc);
196 snaps = (struct snap_ea *)buf;
198 /* if table is NULL and there is a slot */
199 if( !table && slot >= 0) {
200 ino = le32_to_cpu(snaps->ino[slot]);
202 inode = iget(primary->i_sb, ino);
205 /* if table is not NULL */
206 while (!inode && slot >= 0 ) {
207 ino = le32_to_cpu(snaps->ino[slot]);
209 CDEBUG(D_INODE, "snap inode at slot %d is %lu\n", slot, ino);
214 inode = iget(primary->i_sb, ino);
217 if(slot == -1 && table) {
218 CDEBUG(D_INODE, "redirector not found, using primary\n");
219 inode = iget(primary->i_sb, primary->i_ino);
225 /* Save the indirect inode in the snapshot table of the primary inode. */
226 static int fsfilt_ext3_set_indirect(struct inode *pri, int index, ino_t ind_ino,
229 char buf[EXT3_MAX_SNAP_DATA];
230 struct snap_ea *snaps;
231 int rc = 0, inlist = 1;
233 handle_t *handle = NULL;
236 CDEBUG(D_INODE, "(ino %lu, parent %lu): saving ind %lu to index %d\n",
237 pri->i_ino, parent_ino, ind_ino, index);
239 if (index < 0 || index > MAX_SNAPS || !pri)
241 /* need lock the list before get_attr() to avoid race */
242 /* read ea at first */
243 rc = ext3_xattr_get(pri, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
244 buf, EXT3_MAX_SNAP_DATA);
245 if (rc == -ENODATA || rc == -ENODATA) {
246 CDEBUG(D_INODE, "no extended attributes - zeroing\n");
247 memset(buf, 0, EXT3_MAX_SNAP_DATA);
249 * To judge a inode in list, we only see if it has snap ea.
250 * So take care of snap ea of primary inodes very carefully.
251 * Is it right in snapfs EXT3, check it later?
255 } else if (rc < 0 || rc > EXT3_MAX_SNAP_DATA) {
256 GOTO(out_unlock, rc);
258 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_SETIND_TRANS_BLOCKS, rc);
260 GOTO(out_unlock, rc = PTR_ERR(handle));
262 snaps = (struct snap_ea *)buf;
263 snaps->ino[index] = cpu_to_le32 (ind_ino);
264 ea_size = EXT3_MAX_SNAP_DATA;
266 set_parent_ino(snaps, ea_size, index, cpu_to_le32(parent_ino));
268 rc = ext3_xattr_set_handle(handle, pri, EXT3_SNAP_INDEX,EXT3_SNAP_ATTR,
269 buf, EXT3_MAX_SNAP_DATA, 0);
270 ext3_mark_inode_dirty(handle, pri);
271 journal_stop(handle);
276 static int ext3_set_generation(struct inode *inode, unsigned long gen)
282 EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS, err);
286 err = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
287 EXT3_SNAP_GENERATION, (char*)&gen,
290 CERROR("ino %lu, set_ext_attr err %d\n", inode->i_ino, err);
294 journal_stop(handle);
299 * Copy inode metadata from one inode to another, excluding blocks and size.
300 * FIXME do we copy EA data - ACLs and such (excluding snapshot data)?
302 static void ext3_copy_meta(handle_t *handle, struct inode *dst, struct inode *src)
306 dst->i_mode = src->i_mode;
307 dst->i_nlink = src->i_nlink;
308 dst->i_uid = src->i_uid;
309 dst->i_gid = src->i_gid;
310 dst->i_atime = src->i_atime;
311 dst->i_mtime = src->i_mtime;
312 dst->i_ctime = src->i_ctime;
313 // dst->i_version = src->i_version;
315 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
316 dst->i_attr_flags = src->i_attr_flags;
318 dst->i_generation = src->i_generation;
319 EXT3_I(dst)->i_dtime = EXT3_I(src)->i_dtime;
320 EXT3_I(dst)->i_flags = EXT3_I(src)->i_flags | EXT3_COW_FL;
321 #ifdef EXT3_FRAGMENTS
322 EXT3_I(dst)->i_faddr = EXT3_I(src)->i_faddr;
323 EXT3_I(dst)->i_frag_no = EXT3_I(src)->i_frag_no;
324 EXT3_I(dst)->i_frag_size = EXT3_I(src)->i_frag_size;
326 if ((size = ext3_xattr_list(src, NULL, 0)) > 0) {
331 if (ext3_xattr_list(src, names, 0) < 0)
334 * the list of attribute names are stored as NUL terminated
335 * strings, with a double NUL string at the end.
338 while ((namelen = strlen(name))) {
342 /* don't copy snap data */
343 if (!strcmp(name, EXT3_SNAP_ATTR)) {
344 CDEBUG(D_INFO, "skipping %s item\n", name);
347 CDEBUG(D_INODE, "copying %s item\n", name);
348 attrlen = ext3_xattr_get(src, EXT3_SNAP_INDEX,
349 EXT3_SNAP_ATTR, NULL, 0);
352 OBD_ALLOC(buf, attrlen);
358 if (ext3_xattr_get(src, EXT3_SNAP_INDEX,
359 EXT3_SNAP_ATTR, buf, attrlen) < 0)
361 if (ext3_xattr_set_handle(handle, dst, EXT3_SNAP_INDEX,
362 EXT3_SNAP_ATTR, buf, attrlen,
365 OBD_FREE(buf, attrlen);
366 name += namelen + 1; /* skip name and trailing NUL */
370 static int ext3_copy_reg_block(struct inode *dst, struct inode *src, int blk)
372 struct page *src_page, *dst_page;
373 loff_t offset = blk << src->i_sb->s_blocksize_bits;
374 unsigned long index = offset >> PAGE_CACHE_SHIFT;
378 /*read the src page*/
379 src_page = grab_cache_page(src->i_mapping, index);
380 if (src_page == NULL)
383 if (!PageUptodate(src_page)) {
384 rc = src->i_mapping->a_ops->readpage(NULL, src_page);
386 page_cache_release(src_page);
393 dst_page = grab_cache_page(dst->i_mapping, index);
394 if (dst_page == NULL)
395 GOTO(src_page_unlock, rc = -ENOMEM);
398 rc = dst->i_mapping->a_ops->prepare_write(NULL, dst_page, 0,
399 PAGE_CACHE_SIZE - 1);
401 GOTO(dst_page_unlock, rc = -EFAULT);
402 memcpy(page_address(dst_page), page_address(src_page), PAGE_CACHE_SIZE);
404 flush_dcache_page(dst_page);
406 rc = dst->i_mapping->a_ops->commit_write(NULL, dst_page, 0,
407 PAGE_CACHE_SIZE - 1);
412 unlock_page(dst_page);
413 page_cache_release(dst_page);
416 page_cache_release(src_page);
419 static int ext3_copy_dir_block(struct inode *dst, struct inode *src, int blk)
421 struct buffer_head *bh_dst = NULL, *bh_src = NULL;
423 handle_t *handle = NULL;
426 EXT3_JOURNAL_START(dst->i_sb, handle, SNAP_COPYBLOCK_TRANS_BLOCKS, rc);
430 bh_src = ext3_bread(handle, src, blk, 0, &rc);
432 CERROR("rcor for src blk %d, rcor %d\n", blk, rc);
433 GOTO(exit_relese, rc);
435 bh_dst = ext3_getblk(handle, dst, blk, 1, &rc);
437 CERROR("rcor for dst blk %d, rcor %d\n", blk, rc);
438 GOTO(exit_relese, rc);
440 CDEBUG(D_INODE, "copy block %lu to %lu (%ld bytes)\n",
441 bh_src->b_blocknr, bh_dst->b_blocknr, src->i_sb->s_blocksize);
443 ext3_journal_get_write_access(handle, bh_dst);
444 memcpy(bh_dst->b_data, bh_src->b_data, src->i_sb->s_blocksize);
445 ext3_journal_dirty_metadata(handle, bh_dst);
449 if (bh_src) brelse(bh_src);
450 if (bh_dst) brelse(bh_dst);
452 journal_stop(handle);
455 /* fsfilt_ext3_copy_block - copy one data block from inode @src to @dst.
456 No lock here. User should do the lock.
457 User should check the return value to see if the result is correct.
459 1: The block has been copied successfully
460 0: No block is copied, usually this is because src has no such blk
464 static int fsfilt_ext3_copy_block (struct inode *dst, struct inode *src, int blk)
468 CDEBUG(D_INODE, "copy blk %d from %lu to %lu \n", blk, src->i_ino,
471 * ext3_getblk() require handle!=NULL
473 if (S_ISREG(src->i_mode)) {
474 rc = ext3_copy_reg_block(dst, src, blk);
476 rc = ext3_copy_dir_block(dst, src, blk);
482 static inline int ext3_has_ea(struct inode *inode)
484 return (EXT3_I(inode)->i_file_acl != 0);
486 /* XXXThis function has a very bad effect to
487 * the performance of filesystem,
488 * will find another way to fix it
490 static void fs_flushinval_pages(handle_t *handle, struct inode* inode)
492 if (inode->i_blocks > 0 && inode->i_mapping) {
493 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
494 fsync_inode_data_buffers(inode);
496 truncate_inode_pages(inode->i_mapping, 0);
499 /* ext3_migrate_data:
500 * MOVE all the data blocks from inode src to inode dst as well as
501 * COPY all attributes(meta data) from inode src to inode dst.
502 * For extended attributes(EA), we COPY all the EAs but skip the Snap EA from
503 * src to dst. If the dst has Snap EA, then we CAN'T overwrite it. We CAN'T
504 * copy the src Snap EA. XXX for EA, can we change it to MOVE all the EAs
505 * (exclude Snap EA) to dst and copy it back to src ? This is for LAN free
508 static int ext3_migrate_data(handle_t *handle, struct inode *dst,
511 unsigned long err = 0;
512 /* 512 byte disk blocks per inode block */
513 int bpib = src->i_sb->s_blocksize >> 9;
520 if (dst->i_ino == src->i_ino)
523 fs_flushinval_pages(handle, src);
525 ext3_copy_meta(handle, dst, src);
527 CDEBUG(D_INODE, "migrating data blocks from %lu to %lu\n",
528 src->i_ino, dst->i_ino);
529 /* Can't check blocks in case of EAs */
531 memcpy(EXT3_I(dst)->i_data, EXT3_I(src)->i_data,
532 sizeof(EXT3_I(src)->i_data));
533 memset(EXT3_I(src)->i_data, 0, sizeof(EXT3_I(src)->i_data));
535 ext3_discard_prealloc(src);
537 dst->i_size = EXT3_I(dst)->i_disksize = EXT3_I(src)->i_disksize;
538 src->i_size = EXT3_I(src)->i_disksize = 0;
540 dst->i_blocks = src->i_blocks;
542 /* Check EA blocks here to modify i_blocks correctly */
543 if(ext3_has_ea (src)) {
544 src->i_blocks += bpib;
545 if( ! ext3_has_ea (dst) )
546 if( dst->i_blocks >= bpib )
547 dst->i_blocks -= bpib;
549 if( ext3_has_ea (dst))
550 dst->i_blocks += bpib;
553 CDEBUG(D_INODE, "migrate data from ino %lu to ino %lu\n", src->i_ino,
555 ext3_mark_inode_dirty(handle, src);
556 ext3_mark_inode_dirty(handle, dst);
560 static handle_t * ext3_copy_data(handle_t *handle, struct inode *dst,
561 struct inode *src, int *has_orphan)
563 unsigned long blocks, blk, cur_blks;
564 int low_credits, save_ref;
568 blocks =(src->i_size + src->i_sb->s_blocksize-1) >>
569 src->i_sb->s_blocksize_bits;
570 low_credits = handle->h_buffer_credits - SNAP_BIGCOPY_TRANS_BLOCKS;
572 CDEBUG(D_INODE, "%lu blocks need to be copied,low credits limit %d\n",
573 blocks, low_credits);
575 for (blk = 0, cur_blks= dst->i_blocks; blk < blocks; blk++) {
576 if (!ext3_bmap(src->i_mapping, blk))
578 if(handle->h_buffer_credits <= low_credits) {
579 int needed = (blocks - blk) * EXT3_DATA_TRANS_BLOCKS;
580 if (needed > 4 * SNAP_COPYBLOCK_TRANS_BLOCKS)
581 needed = 4 * SNAP_COPYBLOCK_TRANS_BLOCKS;
582 if (journal_extend(handle, needed)) {
583 CDEBUG(D_INFO, "create_indirect:fail to extend "
584 "journal, restart trans\n");
587 CDEBUG(D_INODE, "add orphan ino %lu"
588 "nlink %d to orphan list \n",
589 dst->i_ino, dst->i_nlink);
590 ext3_orphan_add(handle, dst);
593 EXT3_I(dst)->i_disksize =
594 blk * dst->i_sb->s_blocksize;
595 dst->i_blocks = cur_blks;
596 dst->i_mtime = CURRENT_TIME;
597 ext3_mark_inode_dirty(handle, dst);
599 * We can be sure the last handle was stoped
600 * ONLY if the handle's reference count is 1
602 save_ref = handle->h_ref;
604 if(journal_stop(handle) ){
605 CERROR("fail to stop journal\n");
609 EXT3_JOURNAL_START(dst->i_sb, handle,
610 low_credits + needed, err);
612 handle->h_ref = save_ref;
615 if (fsfilt_ext3_copy_block( dst, src, blk) < 0 )
617 cur_blks += dst->i_sb->s_blocksize / 512;
620 dst->i_size = EXT3_I(dst)->i_disksize = src->i_size;
623 /*Here delete the data of that pri inode
624 *FIXME later, should throw the blocks of
625 *primary inode directly
627 static int ext3_throw_inode_data(handle_t *handle, struct inode *inode)
629 struct inode *tmp = NULL;
631 tmp = ext3_new_inode(handle, inode, (int)inode->i_mode, 0);
633 CERROR("ext3_new_inode error\n");
636 double_lock_inode(inode, tmp);
637 ext3_migrate_data(handle, tmp, inode);
638 double_unlock_inode(inode, tmp);
644 * fsfilt_ext3_create_indirect - copy data, attributes from primary to new indir inode
645 * @pri: primary (source) inode
646 * @index: index in snapshot table where indirect inode should be stored
647 * @delete: flag that the primary inode is being deleted
649 * We copy all of the data blocks from the @*src inode to the @*dst inode, as
650 * well as copying the attributes from @*src to @*dst. If @delete == 1, then
651 * the primary inode will only be a redirector and will appear deleted.
653 * FIXME do we move EAs, only non-snap EAs, what?
654 * FIXME we could do readpage/writepage, but we would have to handle block
655 * allocation then, and it ruins sparse files for 1k/2k filesystems,
656 * at the expense of doing a memcpy.
658 static struct inode* fsfilt_ext3_create_indirect(struct inode *pri, int index,
660 struct inode* parent,
663 struct inode *ind = NULL;
664 handle_t *handle = NULL;
669 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
670 CERROR("TRY TO COW JOUNRAL\n");
671 RETURN(ERR_PTR(-EINVAL));
673 CDEBUG(D_INODE, "creating indirect inode for %lu at index %d, %s pri\n",
674 pri->i_ino, index, del ? "deleting" : "preserve");
676 ind = fsfilt_ext3_get_indirect(pri, NULL, index);
678 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_CREATEIND_TRANS_BLOCKS,
681 RETURN(ERR_PTR(err));
682 /* XXX ? We should pass an err argument to get_indirect and precisely
683 * detect the errors, for some errors, we should exit right away.
686 /* if the option is SNAP_DEL_PRI_WITH_IND and there is an indirect,
687 * we just free the primary data blocks and mark this inode delete
689 if((del) && ind && !IS_ERR(ind)) {
690 /* for directory, we don't free the data blocks,
691 * or ext3_rmdir will report errors "bad dir, no data blocks"
693 CDEBUG(D_INODE, "del==SNAP_DEL_PRI_WITH_IND && ind\n");
694 if(!S_ISDIR(pri->i_mode)) {
695 err = ext3_throw_inode_data(handle, pri);
700 EXT3_I(pri)->i_dtime = LTIME_S(CURRENT_TIME);
701 ext3_mark_inode_dirty(handle, pri);
705 if (ind && !IS_ERR(ind)) {
706 CDEBUG(D_INODE, "existing indirect ino %lu for %lu: index %d\n",
707 ind->i_ino, pri->i_ino, index);
712 /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
713 ind = ext3_new_inode(handle, pri, (int)pri->i_mode, 0);
717 CDEBUG(D_INODE, "got new inode %lu\n", ind->i_ino);
718 ind->i_rdev = pri->i_rdev;
719 ind->i_op = pri->i_op;
722 memcpy(ind->i_op, pri->i_op, sizeof(*pri->i_op));
723 memcpy(ind->i_fop, pri->i_fop, sizeof(*pri->i_fop));
724 memcpy(ind->i_mapping->a_ops, pri->i_mapping->a_ops,
725 sizeof(*pri->i_mapping->a_ops));
727 ext3_set_generation(ind, (unsigned long)gen);
728 /* If we are deleting the primary inode, we want to ensure that it is
729 * written to disk with a non-zero link count, otherwise the next iget
730 * and iput will mark the inode as free (which we don't want, we want
731 * it to stay a redirector). We fix this in ext3_destroy_indirect()
732 * when the last indirect inode is removed.
734 * We then do what ext3_delete_inode() does so that the metadata will
735 * appear the same as a deleted inode, and we can detect it later.
738 CDEBUG(D_INODE, "deleting primary inode\n");
741 err = ext3_migrate_data(handle, ind, pri);
743 GOTO(exit_unlock, err);
745 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
747 GOTO(exit_unlock, err);
749 /* XXX for directory, we copy the block back
750 * or ext3_rmdir will report errors "bad dir, no data blocks"
752 if( S_ISDIR(pri->i_mode)) {
753 handle = ext3_copy_data(handle, pri, ind, &has_orphan);
755 GOTO(exit_unlock, err= -EINVAL);
758 EXT3_I(pri)->i_flags |= EXT3_DEL_FL;
759 EXT3_I(ind)->i_flags |= EXT3_COW_FL;
760 if(S_ISREG(pri->i_mode)) pri->i_nlink = 1;
761 EXT3_I(pri)->i_dtime = LTIME_S(CURRENT_TIME);
762 //EXT3_I(pri)->i_generation++;
763 ext3_mark_inode_dirty(handle, pri);
764 ext3_mark_inode_dirty(handle, ind);
768 err = ext3_migrate_data(handle, ind, pri);
772 /* for regular files we do blocklevel COW's maybe */
773 if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW)
774 && S_ISREG(pri->i_mode)) {
776 CDEBUG(D_INODE, "ino %lu, do block cow\n", pri->i_ino);
777 /* because after migrate_data , pri->i_size is 0 */
778 pri->i_size = ind->i_size;
781 int bpib = pri->i_sb->s_blocksize >> 9;
782 CDEBUG(D_INODE, "ino %lu, do file cow\n", pri->i_ino);
784 /* XXX: can we do this better?
785 * If it's a fast symlink, we should copy i_data back!
786 * The criteria to determine a fast symlink is:
787 * 1) it's a link and its i_blocks is 0
788 * 2) it's a link and its i_blocks is bpib ( the case
789 * it has been cowed and has ea )
791 if( S_ISLNK(ind->i_mode) && ((ind->i_blocks == 0) ||
792 (ext3_has_ea(ind) && ind->i_blocks == bpib))) {
793 CDEBUG(D_INODE, "ino %lu is fast symlink\n", pri->i_ino);
794 memcpy(EXT3_I(pri)->i_data, EXT3_I(ind)->i_data,
795 sizeof(EXT3_I(ind)->i_data));
796 pri->i_size = ind->i_size;
799 handle = ext3_copy_data(handle, pri, ind, &has_orphan);
801 GOTO(exit_unlock, err);
804 /* set cow flag for ind */
805 EXT3_I(ind)->i_flags |= EXT3_COW_FL;
806 EXT3_I(pri)->i_flags &= ~EXT3_COW_FL;
808 ext3_mark_inode_dirty(handle, pri);
809 ext3_mark_inode_dirty(handle, ind);
811 err = fsfilt_ext3_set_indirect(pri, index, ind->i_ino, parent->i_ino);
813 GOTO(exit_unlock, err);
817 if (!EXT3_HAS_COMPAT_FEATURE(pri->i_sb,
818 EXT3_FEATURE_COMPAT_SNAPFS)) {
819 lock_super(pri->i_sb);
820 ext3_journal_get_write_access(handle, EXT3_SB(pri->i_sb)->s_sbh);
821 EXT3_SB(pri->i_sb)->s_es->s_feature_compat |=
822 cpu_to_le32(EXT3_FEATURE_COMPAT_SNAPFS);
823 ext3_journal_dirty_metadata(handle, EXT3_SB(pri->i_sb)->s_sbh);
824 pri->i_sb->s_dirt = 1;
825 unlock_super(pri->i_sb);
828 CDEBUG(D_INODE, "del %lu nlink %d from orphan list\n",
829 ind->i_ino, ind->i_nlink);
830 ext3_orphan_del(handle, ind);
832 journal_stop(handle);
841 CDEBUG(D_INODE, "del %lu nlink %d from orphan list\n",
842 ind->i_ino, ind->i_nlink);
843 ext3_orphan_del(handle, ind);
846 journal_stop(handle);
848 RETURN(ERR_PTR(err));
851 static int fsfilt_ext3_snap_feature (struct super_block *sb, int feature, int op) {
858 case SNAP_SET_FEATURE:
859 case SNAP_CLEAR_FEATURE:
860 EXT3_JOURNAL_START(sb, handle, 1, rc);
864 ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
865 if (op == SNAP_SET_FEATURE)
866 SB_FEATURE_COMPAT(sb) |= cpu_to_le32(feature);
868 SB_FEATURE_COMPAT(sb) &= ~cpu_to_le32(feature);
870 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
872 journal_stop(handle);
874 case SNAP_HAS_FEATURE:
875 /*FIXME should lock super or not*/
876 rc = SNAP_HAS_COMPAT_FEATURE(sb, feature);
884 * is_redirector - determines if a primary inode is a redirector
885 * @inode: primary inode to test
887 * Returns 1 if the inode is a redirector, 0 otherwise.
889 static int fsfilt_ext3_is_redirector(struct inode *inode)
891 int is_redirector = 0;
895 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX ,EXT3_SNAP_ATTR,
897 if (rc > 0 && rc <= MAX_SNAP_DATA)
899 CDEBUG(D_INODE, "inode %lu %s redirector\n", inode->i_ino,
900 is_redirector ? "is" : "isn't");
901 RETURN(is_redirector);
903 /*if it's indirect inode or not */
904 static int fsfilt_ext3_is_indirect(struct inode *inode)
906 if (EXT3_I(inode)->i_flags |= EXT3_COW_FL)
912 /* get the indirect ino at index of the primary inode
913 * return value: postive: indirect ino number
914 * negative or 0: error
916 static ino_t fsfilt_ext3_get_indirect_ino(struct super_block *sb,
917 ino_t primary_ino, int index)
919 char buf[EXT3_MAX_SNAP_DATA];
920 struct inode *primary = NULL;
921 struct snap_ea *snaps;
925 if (index < 0 || index > EXT3_MAX_SNAPS)
927 primary = iget(sb, primary_ino);
931 CERROR("attribute read error=%d", err);
932 GOTO (err_free, ino = err);
934 err = ext3_xattr_get(primary, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
935 buf, EXT3_MAX_SNAP_DATA);
936 if (err == -ENODATA) {
937 GOTO(err_free, ino = -ENODATA);
938 } else if (err < 0) {
939 CERROR(" attribute read error err=%d\n", err);
940 GOTO(err_free, ino = err);
942 snaps = (struct snap_ea *)buf;
943 ino = le32_to_cpu (snaps->ino[index]);
944 CDEBUG(D_INODE, "snap ino for %ld at index %d is %lu\n",
945 primary->i_ino, index, ino);
953 /* The following functions are used by destroy_indirect */
954 #define inode_bmap(inode, nr) (EXT3_I(inode)->i_data[(nr)])
955 #define inode_setbmap(inode, nr, physical) (EXT3_I(inode)->i_data[(nr)]=(physical))
956 static inline int block_bmap(struct buffer_head * bh, int nr)
962 tmp = le32_to_cpu(((u32 *) bh->b_data)[nr]);
967 static inline int block_setbmap(handle_t *handle, struct buffer_head * bh,
968 int nr, int physical)
973 ext3_journal_get_write_access(handle, bh);
974 ((u32 *) bh->b_data)[nr] = cpu_to_le32(physical);
975 ext3_journal_dirty_metadata(handle, bh);
980 static int ext3_migrate_block(handle_t *handle, struct inode * dst,
981 struct inode *src, int block)
983 int i1_d=0, i1_s=0, i2_d=0, i2_s=0, i3_d=0, i3_s=0;
984 int addr_per_block = EXT3_ADDR_PER_BLOCK(src->i_sb);
985 int addr_per_block_bits = EXT3_ADDR_PER_BLOCK_BITS(src->i_sb);
990 CWARN("ext3_migrate_block block < 0 %p \n", src->i_sb);
993 if (block >= EXT3_NDIR_BLOCKS + addr_per_block +
994 (1 << (addr_per_block_bits * 2)) +
995 ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
996 CWARN("ext3_migrate_block block > big %p \n", src->i_sb);
999 /* EXT3_NDIR_BLOCK */
1000 if (block < EXT3_NDIR_BLOCKS) {
1001 if(inode_bmap(dst, block))
1004 if( (physical = inode_bmap(src, block)) ) {
1005 inode_setbmap (dst, block, physical);
1006 inode_setbmap (src, block, 0);
1013 /* EXT3_IND_BLOCK */
1014 block -= EXT3_NDIR_BLOCKS;
1015 if (block < addr_per_block) {
1016 i1_d = inode_bmap (dst, EXT3_IND_BLOCK);
1018 physical = inode_bmap(src, EXT3_IND_BLOCK);
1020 inode_setbmap (dst, EXT3_IND_BLOCK, physical);
1021 inode_setbmap (src, EXT3_IND_BLOCK, 0);
1027 if(block_bmap(sb_bread(dst->i_sb, i1_d), block))
1030 i1_s = inode_bmap (src, EXT3_IND_BLOCK);
1031 if( !i1_s) RETURN(0);
1033 physical = block_bmap(sb_bread(src->i_sb, i1_s), block);
1036 block_setbmap(handle, sb_bread(dst->i_sb, i1_d),block,
1038 block_setbmap(handle, sb_bread(src->i_sb, i1_s),block,0);
1044 /* EXT3_DIND_BLOCK */
1045 block -= addr_per_block;
1046 if (block < (1 << (addr_per_block_bits * 2))) {
1047 i1_d = inode_bmap (dst, EXT3_DIND_BLOCK);
1048 i1_s = inode_bmap (src, EXT3_DIND_BLOCK);
1050 if( (physical = inode_bmap(src, EXT3_DIND_BLOCK)) ) {
1051 inode_setbmap (dst, EXT3_DIND_BLOCK, physical);
1052 inode_setbmap (src, EXT3_DIND_BLOCK, 0);
1058 i2_d = block_bmap (sb_bread (dst->i_sb, i1_d),
1059 block >> addr_per_block_bits);
1063 if(!i1_s) RETURN(0);
1065 physical = block_bmap(sb_bread (src->i_sb, i1_s),
1066 block >> addr_per_block_bits);
1068 block_setbmap(handle, sb_bread(dst->i_sb, i1_d),
1069 block >> addr_per_block_bits,
1071 block_setbmap(handle, sb_bread(src->i_sb, i1_s),
1072 block >> addr_per_block_bits, 0);
1078 physical = block_bmap(sb_bread(dst->i_sb, i2_d),
1079 block & (addr_per_block - 1));
1083 i2_s = block_bmap (sb_bread(src->i_sb, i1_s),
1084 block >> addr_per_block_bits);
1085 if(!i2_s) RETURN(0);
1087 physical = block_bmap(sb_bread(src->i_sb, i2_s),
1088 block & (addr_per_block - 1));
1090 block_setbmap(handle, sb_bread(dst->i_sb, i2_d),
1091 block & (addr_per_block - 1), physical);
1092 block_setbmap(handle, sb_bread(src->i_sb, i2_s),
1093 block & (addr_per_block - 1), 0);
1101 /* EXT3_TIND_BLOCK */
1102 block -= (1 << (addr_per_block_bits * 2));
1103 i1_d = inode_bmap (dst, EXT3_TIND_BLOCK);
1104 i1_s = inode_bmap (src, EXT3_TIND_BLOCK);
1106 if((physical = inode_bmap(src, EXT3_TIND_BLOCK)) )
1107 inode_setbmap (dst, EXT3_TIND_BLOCK, physical);
1111 i2_d = block_bmap(sb_bread (dst->i_sb, i1_d),
1112 block >> (addr_per_block_bits * 2));
1114 if(i1_s) i2_s = block_bmap(sb_bread(src->i_sb, i1_s),
1115 block >> (addr_per_block_bits * 2));
1118 if( !i1_s) RETURN(0);
1120 physical = block_bmap(sb_bread (src->i_sb, i1_s),
1121 block >> (addr_per_block_bits * 2));
1123 block_setbmap(handle, sb_bread (dst->i_sb, i1_d),
1124 block >> (addr_per_block_bits * 2), physical);
1125 block_setbmap(handle, sb_bread (src->i_sb, i1_s),
1126 block >> (addr_per_block_bits * 2), 0);
1132 i3_d = block_bmap (sb_bread (dst->i_sb, i2_d),
1133 (block >> addr_per_block_bits) & (addr_per_block - 1));
1134 if( i2_s) i3_s = block_bmap (sb_bread (src->i_sb, i2_s),
1135 (block >> addr_per_block_bits) & (addr_per_block - 1));
1138 if (!i2_s) RETURN(0);
1139 physical = block_bmap (sb_bread (src->i_sb, i2_s),
1140 (block >> addr_per_block_bits) & (addr_per_block - 1));
1142 block_setbmap (handle, sb_bread (dst->i_sb, i2_d),
1143 (block >> addr_per_block_bits) &
1144 (addr_per_block - 1), physical);
1145 block_setbmap (handle, sb_bread (src->i_sb, i2_s),
1146 (block >> addr_per_block_bits) &
1147 (addr_per_block - 1),0);
1153 physical = block_bmap (sb_bread (dst->i_sb, i3_d),
1154 block & (addr_per_block - 1)) ;
1160 physical = block_bmap(sb_bread(src->i_sb, i3_s),
1161 block & (addr_per_block - 1));
1163 block_setbmap (handle, sb_bread (dst->i_sb, i3_d),
1164 block & (addr_per_block - 1), physical);
1165 block_setbmap (handle, sb_bread (src->i_sb, i3_s),
1166 block & (addr_per_block - 1), 0);
1174 /* Generate i_blocks from blocks for an inode .
1175 * We also calculate EA block here.
1177 static unsigned long calculate_i_blocks(struct inode *inode, int blocks)
1179 /* 512 byte disk blocks per inode block */
1180 int bpib = inode->i_sb->s_blocksize >> 9;
1181 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1182 unsigned long i_blocks = 0;
1183 int i=0, j=0, meta_blocks = 0;
1189 /* re-calculate blocks here */
1190 blocks = (inode->i_size + inode->i_sb->s_blocksize-1)
1191 >> inode->i_sb->s_blocksize_bits;
1194 /* calculate data blocks */
1195 for(i = 0; i < blocks; i++) {
1196 if(ext3_bmap(inode->i_mapping, i))
1199 /* calculate meta blocks */
1200 blocks -= EXT3_NDIR_BLOCKS;
1203 blocks -= addr_per_block;
1205 if( blocks > 0 ) meta_blocks++;
1208 while( (blocks > 0) && (i < addr_per_block) ) {
1210 blocks -= addr_per_block;
1214 if ( blocks > 0 ) meta_blocks += 2;
1217 while( blocks > 0) {
1219 blocks -= addr_per_block;
1221 if(i >= addr_per_block ) {
1225 if( j >= addr_per_block) {
1230 /* calculate EA blocks */
1231 if(ext3_has_ea(inode))
1234 i_blocks += meta_blocks * bpib;
1235 CDEBUG(D_INODE, "ino %lu, get i_blocks %lu\n", inode->i_ino, i_blocks);
1241 * fsfilt_ext3_destroy_indirect - delete an indirect inode from the table
1242 * @pri: primary inode
1243 * @ind: indirect inode
1244 * @index: index of inode that should be deleted
1246 * We delete the @*ind inode, and remove it from the snapshot table. If @*ind
1247 * is NULL, we use the inode at @index.
1249 static int fsfilt_ext3_destroy_indirect(struct inode *pri, int index,
1250 struct inode *next_ind)
1252 char buf[EXT3_MAX_SNAP_DATA];
1253 struct snap_ea *snaps;
1255 int save = 0, i=0, err = 0;
1256 handle_t *handle=NULL;
1259 if (index < 0 || index > EXT3_MAX_SNAPS)
1262 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1263 CERROR("TRY TO DESTROY JOURNAL'S IND\n");
1267 err = ext3_xattr_get(pri, EXT3_SNAP_INDEX, EXT3_SNAP_ATTR,
1268 buf, EXT3_MAX_SNAP_DATA);
1270 CERROR("inode %lu attribute read error\n", pri->i_ino);
1274 snaps = (struct snap_ea *)buf;
1275 if ( !snaps->ino[index] ) {
1276 CERROR("for pri ino %lu, index %d, redirect ino is 0\n",
1281 CDEBUG(D_INODE, "for pri ino %lu, reading inode %lu at index %d\n",
1282 pri->i_ino, (ulong)le32_to_cpu(snaps->ino[index]), index);
1284 ind = iget(pri->i_sb, le32_to_cpu (snaps->ino[index]));
1286 if ( !ind || IS_ERR(ind) || is_bad_inode(ind))
1289 CDEBUG(D_INODE, "iget ind %lu, ref count = %d\n",
1290 ind->i_ino, atomic_read(&ind->i_count));
1292 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_DESTROY_TRANS_BLOCKS, err);
1297 /* if it's block level cow, first copy the blocks back */
1298 if (EXT3_HAS_COMPAT_FEATURE(pri->i_sb, EXT3_FEATURE_COMPAT_BLOCKCOW) &&
1299 S_ISREG(pri->i_mode)) {
1306 double_lock_inode(next_ind, ind);
1308 blocks = (next_ind->i_size + next_ind->i_sb->s_blocksize-1)
1309 >> next_ind->i_sb->s_blocksize_bits;
1311 CDEBUG(D_INODE, "migrate block back from ino %lu to %lu\n",
1312 ind->i_ino, next_ind->i_ino);
1314 for(i = 0; i < blocks; i++) {
1315 if( ext3_bmap(next_ind->i_mapping, i) )
1317 if( !ext3_bmap(ind->i_mapping, i) )
1319 ext3_migrate_block(handle, next_ind, ind, i) ;
1321 /* Now re-compute the i_blocks */
1322 /* XXX shall we take care of ind here? probably not */
1323 next_ind->i_blocks = calculate_i_blocks( next_ind, blocks);
1324 ext3_mark_inode_dirty(handle, next_ind);
1326 if (next_ind == pri)
1329 double_unlock_inode(next_ind, ind);
1332 CDEBUG(D_INODE, "delete indirect ino %lu\n", ind->i_ino);
1333 CDEBUG(D_INODE, "iput ind %lu, ref count = %d\n", ind->i_ino,
1334 atomic_read(&ind->i_count));
1339 snaps->ino[index] = cpu_to_le32(0);
1340 for (i = 0; i < EXT3_MAX_SNAPS; i++)
1341 save += snaps->ino[i];
1344 /*Should we remove snap feature here*/
1346 * If we are deleting the last indirect inode, and the primary inode
1347 * has already been deleted, then mark the primary for deletion also.
1348 * Otherwise, if we are deleting the last indirect inode remove the
1349 * snaptable from the inode. XXX
1351 if (!save && EXT3_I(pri)->i_dtime) {
1352 CDEBUG(D_INODE, "deleting primary %lu\n", pri->i_ino);
1354 /* reset err to 0 now */
1357 CDEBUG(D_INODE, "%s redirector table\n",
1358 save ? "saving" : "deleting");
1359 err = ext3_xattr_set_handle(handle, pri, EXT3_SNAP_INDEX,
1360 EXT3_SNAP_ATTR, save ? buf : NULL,
1361 EXT3_MAX_SNAP_DATA, 0);
1362 ext3_mark_inode_dirty(handle, pri);
1364 journal_stop(handle);
1369 /* restore a primary inode with the indirect inode at index */
1370 static int fsfilt_ext3_restore_indirect(struct inode *pri, int index)
1374 handle_t *handle = NULL;
1377 if (index < 0 || index > EXT3_MAX_SNAPS)
1380 if( pri == EXT3_SB(pri->i_sb)->s_journal_inode ){
1381 CERROR("TRY TO RESTORE JOURNAL\n");
1384 CDEBUG(D_INODE, "pri ino %lu, index %d\n", pri->i_ino, index);
1386 ind = fsfilt_ext3_get_indirect(pri, NULL, index);
1391 CDEBUG(D_INODE, "restore ino %lu to %lu\n", pri->i_ino, ind->i_ino);
1393 EXT3_JOURNAL_START(pri->i_sb, handle, SNAP_RESTORE_TRANS_BLOCKS, err);
1396 /* first destroy all the data blocks in primary inode */
1397 /* XXX: check this, ext3_new_inode, the first arg should be "dir" */
1398 err = ext3_throw_inode_data(handle, pri);
1400 CERROR("restore_indirect, new_inode err\n");
1403 double_lock_inode(pri, ind);
1404 ext3_migrate_data(handle, pri, ind);
1405 EXT3_I(pri)->i_flags &= ~EXT3_COW_FL;
1406 ext3_mark_inode_dirty(handle, pri);
1407 double_unlock_inode(pri, ind);
1410 //fsfilt_ext3_destroy_indirect(pri, index);
1411 journal_stop(handle);
1417 * ext3_snap_iterate - iterate through all of the inodes
1418 * @sb: filesystem superblock
1419 * @repeat: pointer to function called on each valid inode
1420 * @start: inode to start iterating at
1421 * @priv: private data to the caller/repeat function
1423 * If @start is NULL, then we do not return an inode pointer. If @*start is
1424 * NULL, then we start at the beginning of the filesystem, and iterate over
1425 * all of the inodes in the system. If @*start is non-NULL, then we start
1426 * iterating at this inode.
1428 * We call the repeat function for each inode that is in use. The repeat
1429 * function must check if this is a redirector (with is_redirector) if it
1430 * only wants to operate on redirector inodes. If there is an error or
1431 * the repeat function returns non-zero, we return the last inode operated
1432 * on in the @*start parameter. This allows the caller to restart the
1433 * iteration at this inode if desired, by returning a positive value.
1434 * Negative return values indicate an error.
1436 * NOTE we cannot simply traverse the existing filesystem tree from the root
1437 * inode, as there may be disconnected trees from deleted files/dirs
1439 * FIXME If there was a list of inodes with EAs, we could simply walk the list
1440 * intead of reading every inode. This is an internal implementation issue.
1443 static int ext3_iterate_all(struct super_block *sb,
1444 int (*repeat)(struct inode *inode,void *priv),
1445 struct inode **start, void *priv)
1447 struct inode *tmp = NULL;
1448 int gstart, gnum, err = 0;
1449 ino_t istart, ibase;
1455 *start = iget(sb, EXT3_ROOT_INO);
1457 GOTO(exit, err = -ENOMEM);
1459 if (is_bad_inode(*start))
1460 GOTO(exit, err = -EIO);
1462 if ((*start)->i_ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count)) {
1463 CERROR("invalid starting inode %ld\n",(*start)->i_ino);
1464 GOTO(exit, err = -EINVAL);
1466 if ((*start)->i_ino < EXT3_FIRST_INO(sb)) {
1467 if ((err = (*repeat)(*start, priv) != 0))
1470 *start = iget(sb, EXT3_FIRST_INO(sb));
1472 GOTO(exit, err = -ENOMEM);
1473 if (is_bad_inode(*start))
1474 GOTO(exit, err = -EIO);
1477 gstart = ((*start)->i_ino - 1) / EXT3_INODES_PER_GROUP(sb);
1478 istart = ((*start)->i_ino - 1) % EXT3_INODES_PER_GROUP(sb);
1479 ibase = gstart * EXT3_INODES_PER_GROUP(sb);
1480 for (gnum = gstart; gnum < EXT3_SB(sb)->s_groups_count;
1481 gnum++, ibase += EXT3_INODES_PER_GROUP(sb)) {
1482 struct buffer_head *bitmap_bh = NULL;
1483 struct ext3_group_desc * gdp;
1486 gdp = ext3_get_group_desc (sb, gnum, NULL);
1487 if (!gdp || le16_to_cpu(gdp->bg_free_inodes_count) ==
1488 EXT3_INODES_PER_GROUP(sb))
1490 bitmap_bh = read_inode_bitmap(sb, gnum);
1496 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1497 ino = find_next_bit((unsigned long *)bitmap_bh->b_data,
1498 EXT3_INODES_PER_GROUP(sb), ino);
1500 ino = find_next_bit((unsigned long *)bitmap_bh->b_data,
1501 EXT3_INODES_PER_GROUP(sb), ino);
1502 #warning"FIXME-WANGDI need to port find_next_bit to 2.4"
1504 if (ino < EXT3_INODES_PER_GROUP(sb)) {
1505 ino_t inum = ino + gnum * EXT3_INODES_PER_GROUP(sb) + 1;
1507 if (inum < (*start)->i_ino)
1510 *start = iget(sb, inum);
1512 GOTO(exit, err = -ENOMEM);
1513 if (is_bad_inode(*start))
1514 GOTO(exit, err = -EIO);
1516 if ((err = (*repeat)(*start, priv)) != 0)
1520 if (++ino < EXT3_INODES_PER_GROUP(sb))
1530 static int fsfilt_ext3_iterate(struct super_block *sb,
1531 int (*repeat)(struct inode *inode, void *priv),
1532 struct inode **start, void *priv, int flag)
1535 case SNAP_ITERATE_ALL_INODE:
1536 return ext3_iterate_all (sb, repeat, start, priv);
1542 static int fsfilt_ext3_get_snap_info(struct inode *inode, void *key,
1543 __u32 keylen, void *val,
1549 if (!vallen || !val) {
1550 CERROR("val and val_size is 0!\n");
1553 if (keylen >= strlen(MAX_SNAPTABLE_COUNT)
1554 && strcmp(key, MAX_SNAPTABLE_COUNT) == 0) {
1555 /*FIXME should get it from the EA_size*/
1556 *((__u32 *)val) = EXT3_MAX_SNAPS;
1557 *vallen = sizeof(int);
1559 } else if (keylen >= strlen(SNAPTABLE_INFO)
1560 && strcmp(key, SNAPTABLE_INFO) == 0) {
1561 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1562 EXT3_SNAPTABLE_EA, val, *vallen);
1564 } else if (keylen >= strlen(SNAP_GENERATION)
1565 && strcmp(key, SNAP_GENERATION) == 0) {
1567 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1568 EXT3_SNAP_GENERATION, (char *)val, *vallen);
1569 if (rc == -ENODATA) {
1570 *((__u32 *)val) = 0;
1571 *vallen = sizeof(int);
1579 } else if (keylen >= strlen(SNAP_COUNT) &&
1580 strcmp(key, SNAP_COUNT) == 0) {
1581 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1582 EXT3_SNAP_COUNT, val, *vallen);
1583 if (rc == -ENODATA) {
1584 *((__u32 *)val) = 0;
1585 *vallen = sizeof(int);
1593 } else if (keylen >= strlen(SNAP_ROOT_INO) &&
1594 (strcmp(key, SNAP_ROOT_INO) == 0)) {
1596 rc = ext3_xattr_get(inode, EXT3_SNAP_INDEX,
1597 EXT3_SNAP_ROOT_INO, val, *vallen);
1607 static int fsfilt_ext3_set_snap_info(struct inode *inode, void *key,
1608 __u32 keylen, void *val,
1614 if (!vallen || !val) {
1615 CERROR("val and val_size is 0!\n");
1619 if (keylen >= strlen(SNAPTABLE_INFO)
1620 && strcmp(key, SNAPTABLE_INFO) == 0) {
1622 EXT3_JOURNAL_START(inode->i_sb, handle, EXT3_XATTR_TRANS_BLOCKS,
1626 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1627 EXT3_SNAPTABLE_EA, val, *vallen, 0);
1628 journal_stop(handle);
1631 } else if (keylen >= strlen(SNAP_GENERATION)
1632 && strcmp(key, SNAP_GENERATION) == 0) {
1634 rc = ext3_set_generation(inode, *(int*)val);
1637 } else if (keylen >= strlen(SNAP_COUNT) &&
1638 (strcmp(key, SNAP_COUNT) == 0)) {
1640 EXT3_JOURNAL_START(inode->i_sb, handle,
1641 EXT3_XATTR_TRANS_BLOCKS, rc);
1644 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1645 EXT3_SNAP_COUNT, val, *vallen, 0);
1646 journal_stop(handle);
1649 } else if (keylen >= strlen(SNAP_ROOT_INO) &&
1650 (strcmp(key, SNAP_ROOT_INO) == 0)) {
1652 EXT3_JOURNAL_START(inode->i_sb, handle,
1653 EXT3_XATTR_TRANS_BLOCKS, rc);
1656 rc = ext3_xattr_set_handle(handle, inode, EXT3_SNAP_INDEX,
1657 EXT3_SNAP_ROOT_INO, val, *vallen, 0);
1658 journal_stop(handle);
1665 static int fsfilt_ext3_dir_ent_size(char *name)
1668 return EXT3_DIR_REC_LEN(strlen(name));
1673 static int fsfilt_ext3_set_dir_ent(struct super_block *sb, char *name,
1674 char *buf, int buf_off, int nlen, size_t count)
1678 if (buf_off == 0 && nlen == 0) {
1679 struct ext3_dir_entry_2 *de = (struct ext3_dir_entry_2 *)buf;
1680 LASSERT(count == PAGE_CACHE_SIZE);
1681 de->rec_len = count;
1685 struct ext3_dir_entry_2 *de, *de1;
1686 de = (struct ext3_dir_entry_2 *)(buf + buf_off - nlen);
1687 de1 = (struct ext3_dir_entry_2 *)(buf + buf_off);
1690 LASSERT(nlen == EXT3_DIR_REC_LEN_DE(de));
1692 rlen = le16_to_cpu(de->rec_len);
1693 de->rec_len = cpu_to_le16(nlen);
1695 de1->rec_len = cpu_to_le16(rlen - nlen);
1696 de1->name_len = strlen(name);
1697 memcpy (de1->name, name, de->name_len);
1698 nlen = EXT3_DIR_REC_LEN_DE(de1);
1703 struct fsfilt_operations fsfilt_ext3_snap_ops = {
1704 .fs_type = "ext3_snap",
1705 .fs_owner = THIS_MODULE,
1706 .fs_create_indirect = fsfilt_ext3_create_indirect,
1707 .fs_get_indirect = fsfilt_ext3_get_indirect,
1708 .fs_set_indirect = fsfilt_ext3_set_indirect,
1709 .fs_snap_feature = fsfilt_ext3_snap_feature,
1710 .fs_is_redirector = fsfilt_ext3_is_redirector,
1711 .fs_is_indirect = fsfilt_ext3_is_indirect,
1712 .fs_get_indirect_ino = fsfilt_ext3_get_indirect_ino,
1713 .fs_destroy_indirect = fsfilt_ext3_destroy_indirect,
1714 .fs_restore_indirect = fsfilt_ext3_restore_indirect,
1715 .fs_iterate = fsfilt_ext3_iterate,
1716 .fs_copy_block = fsfilt_ext3_copy_block,
1717 .fs_set_snap_info = fsfilt_ext3_set_snap_info,
1718 .fs_get_snap_info = fsfilt_ext3_get_snap_info,
1719 .fs_dir_ent_size = fsfilt_ext3_dir_ent_size,
1720 .fs_set_dir_ent = fsfilt_ext3_set_dir_ent,
1724 static int __init fsfilt_ext3_snap_init(void)
1728 rc = fsfilt_register_ops(&fsfilt_ext3_snap_ops);
1733 static void __exit fsfilt_ext3_snap_exit(void)
1736 fsfilt_unregister_ops(&fsfilt_ext3_snap_ops);
1739 module_init(fsfilt_ext3_snap_init);
1740 module_exit(fsfilt_ext3_snap_exit);
1742 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1743 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1744 MODULE_LICENSE("GPL");