4 #define DEBUG_SUBSYSTEM S_SNAP
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/stat.h>
11 #include <linux/unistd.h>
12 #include <linux/jbd.h>
13 #include <linux/ext3_fs.h>
14 #include <linux/snap.h>
16 #include "snapfs_internal.h"
18 static ino_t get_parent_ino(struct inode * inode)
21 struct dentry * dentry;
23 if (list_empty(&inode->i_dentry)) {
24 CERROR("No dentry for ino %lu\n", inode->i_ino);
28 dentry = dget(list_entry(inode->i_dentry.next, struct dentry, d_alias));
30 if(dentry->d_parent->d_inode)
31 ino = dentry->d_parent->d_inode->i_ino;
38 static void d_unadd_iput(struct dentry *dentry)
40 list_del(&dentry->d_alias);
41 INIT_LIST_HEAD(&dentry->d_alias);
42 list_del(&dentry->d_hash);
43 INIT_LIST_HEAD(&dentry->d_hash);
44 iput(dentry->d_inode);
45 dentry->d_inode = NULL;
48 /* XXX check the return values */
49 static struct dentry *currentfs_lookup(struct inode * dir,struct dentry *dentry)
51 struct snap_cache *cache;
53 struct inode_operations *iops;
54 struct inode *cache_inode;
59 cache = snap_find_cache(dir->i_dev);
61 RETURN(ERR_PTR(-EINVAL));
64 if ( dentry->d_name.len == strlen(".snap") &&
65 (memcmp(dentry->d_name.name, ".snap", strlen(".snap")) == 0) ) {
69 /* Don't permit .snap in clonefs */
70 if( dentry->d_sb != cache->cache_sb )
71 RETURN(ERR_PTR(-ENOENT));
73 /* Don't permit .snap under .snap */
74 if( currentfs_is_under_dotsnap(dentry) )
75 RETURN(ERR_PTR(-ENOENT));
77 ino = 0xF0000000 | dir->i_ino;
78 snap = iget(dir->i_sb, ino);
79 CDEBUG(D_INODE, ".snap inode ino %ld, mode %o\n", snap->i_ino, snap->i_mode);
84 iops = filter_c2cdiops(cache->cache_filter);
85 if (!iops || !iops->lookup) {
86 RETURN(ERR_PTR(-EINVAL));
89 rc = iops->lookup(dir, dentry);
90 if ( rc || !dentry->d_inode) {
95 * If we are under dotsnap, we need save extra data into
96 * dentry->d_fsdata: For dir, we only need _this_ snapshot's index;
97 * For others, save primary ino, with it we could found index later
100 cache_inode = dentry->d_inode;
101 if ( (index = currentfs_is_under_dotsnap(dentry)) ) {
102 struct snapshot_operations *snapops;
103 struct inode *ind_inode;
104 ino_t pri_ino, ind_ino;
106 pri_ino = cache_inode->i_ino;
107 snapops = filter_c2csnapops(cache->cache_filter);
111 ind_ino = snapops->get_indirect_ino(cache_inode, index);
112 if( ind_ino <=0 && ind_ino != -ENOATTR )
114 else if( ind_ino != -ENOATTR ){
115 ind_inode = iget(cache_inode->i_sb, ind_ino);
119 list_del(&dentry->d_alias);
120 INIT_LIST_HEAD(&dentry->d_alias);
121 list_add(&dentry->d_alias, &ind_inode->i_dentry);
122 dentry->d_inode = ind_inode;
126 if( S_ISDIR(dentry->d_inode->i_mode) )
127 dentry->d_fsdata = (void*)index;
129 dentry->d_fsdata = (void*)pri_ino;
135 /* XXX: PJB these need to be set up again. See dcache.c */
136 printk("set up dentry ops\n");
137 CDEBUG(D_CACHE, "\n");
138 filter_setup_dentry_ops(cache->cache_filter,
139 dentry->d_op, ¤tfs_dentry_ops);
140 dentry->d_op = filter_c2udops(cache->cache_filter);
141 CDEBUG(D_CACHE, "\n");
145 d_unadd_iput(dentry);
146 RETURN(ERR_PTR(-EINVAL));
149 static int currentfs_create(struct inode *dir, struct dentry *dentry, int mode)
151 struct snap_cache *cache;
152 struct inode_operations *iops;
158 if (currentfs_is_under_dotsnap(dentry)) {
162 cache = snap_find_cache(dir->i_dev);
167 handle = snap_trans_start(cache, dir, SNAP_OP_CREATE);
169 if ( snap_needs_cow(dir) != -1 ) {
170 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
171 snap_debug_device_fail(dir->i_dev, SNAP_OP_CREATE, 1);
172 if ((snap_do_cow(dir, get_parent_ino(dir), 0))) {
173 CERROR("Do cow error\n");
178 iops = filter_c2cdiops(cache->cache_filter);
179 if (!iops || !iops->create) {
182 snap_debug_device_fail(dir->i_dev, SNAP_OP_CREATE, 2);
183 rc = iops->create(dir, dentry, mode);
185 /* XXX now set the correct snap_{file,dir,sym}_iops */
186 if (!dentry->d_inode) {
187 CERROR("Error in currentfs_create, dentry->d_inode is NULL\n");
190 set_filter_ops(cache, dentry->d_inode);
191 CDEBUG(D_INODE, "inode %lu, i_op %p\n", dentry->d_inode->i_ino, dentry->d_inode->i_op);
192 snap_debug_device_fail(dir->i_dev, SNAP_OP_CREATE, 3);
193 init_filter_data(dentry->d_inode, 0);
195 snap_trans_commit(cache, handle);
199 static int currentfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
201 struct snap_cache *cache;
203 struct inode_operations *iops;
208 if (currentfs_is_under_dotsnap(dentry)) {
212 cache = snap_find_cache(dir->i_dev);
217 handle = snap_trans_start(cache, dir, SNAP_OP_MKDIR);
219 if ( snap_needs_cow(dir) != -1 ) {
220 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
221 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKDIR, 1);
222 snap_do_cow(dir, get_parent_ino(dir), 0);
225 iops = filter_c2cdiops(cache->cache_filter);
226 if (!iops || !iops->mkdir) {
231 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKDIR, 2);
232 rc = iops->mkdir(dir, dentry, mode);
237 /* XXX now set the correct snap_{file,dir,sym}_iops */
238 if ( dentry->d_inode) {
239 dentry->d_inode->i_op = filter_c2udiops(cache->cache_filter);
240 CDEBUG(D_INODE, "inode %lu, i_op %p\n", dentry->d_inode->i_ino, dentry->d_inode->i_op);
242 CERROR("Error in currentfs_mkdir, dentry->d_inode is NULL\n");
245 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKDIR, 3);
248 snap_trans_commit(cache, handle);
252 static int currentfs_link (struct dentry * old_dentry, struct inode * dir,
253 struct dentry *dentry)
255 struct snap_cache *cache;
257 struct inode_operations *iops;
262 if (currentfs_is_under_dotsnap(dentry))
265 cache = snap_find_cache(dir->i_dev);
269 handle = snap_trans_start(cache, dir, SNAP_OP_LINK);
271 if ( snap_needs_cow(dir) != -1 ) {
272 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
273 snap_debug_device_fail(dir->i_dev, SNAP_OP_LINK, 1);
274 snap_do_cow(dir, get_parent_ino(dir), 0);
276 if ( snap_needs_cow(old_dentry->d_inode) != -1 ) {
277 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",old_dentry->d_inode->i_ino);
278 snap_debug_device_fail(dir->i_dev, SNAP_OP_LINK, 2);
279 snap_do_cow(old_dentry->d_inode, dir->i_ino, 0);
282 iops = filter_c2cdiops(cache->cache_filter);
284 if (!iops || !iops->link)
285 GOTO(exit, rc = -EINVAL);
287 snap_debug_device_fail(dir->i_dev, SNAP_OP_LINK, 2);
288 rc = iops->link(old_dentry,dir, dentry);
289 snap_debug_device_fail(dir->i_dev, SNAP_OP_LINK, 3);
291 snap_trans_commit(cache, handle);
295 static int currentfs_symlink(struct inode *dir, struct dentry *dentry,
296 const char * symname)
298 struct snap_cache *cache;
300 struct inode_operations *iops;
305 cache = snap_find_cache(dir->i_dev);
309 handle = snap_trans_start(cache, dir, SNAP_OP_SYMLINK);
311 if ( snap_needs_cow(dir) != -1 ) {
312 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
313 snap_debug_device_fail(dir->i_dev, SNAP_OP_SYMLINK, 1);
314 snap_do_cow(dir, get_parent_ino(dir), 0);
317 iops = filter_c2cdiops(cache->cache_filter);
318 if (!iops || !iops->symlink)
319 GOTO(exit, rc = -EINVAL);
321 snap_debug_device_fail(dir->i_dev, SNAP_OP_SYMLINK, 2);
322 rc = iops->symlink(dir, dentry, symname);
323 snap_debug_device_fail(dir->i_dev, SNAP_OP_SYMLINK, 3);
326 snap_trans_commit(cache, handle);
330 static int currentfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
333 struct snap_cache *cache;
335 struct inode_operations *iops;
340 if (currentfs_is_under_dotsnap(dentry)) {
344 cache = snap_find_cache(dir->i_dev);
349 handle = snap_trans_start(cache, dir, SNAP_OP_MKNOD);
351 if ( snap_needs_cow(dir) != -1 ) {
352 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
353 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKNOD, 1);
354 snap_do_cow(dir, get_parent_ino(dir), 0);
357 iops = filter_c2cdiops(cache->cache_filter);
358 if (!iops || !iops->mknod)
359 GOTO(exit, rc = -EINVAL);
361 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKNOD, 2);
362 rc = iops->mknod(dir, dentry, mode, rdev);
363 snap_debug_device_fail(dir->i_dev, SNAP_OP_MKNOD, 3);
365 /* XXX do we need to set the correct snap_{*}_iops */
368 snap_trans_commit(cache, handle);
372 static int currentfs_rmdir(struct inode *dir, struct dentry *dentry)
374 struct snap_cache *cache;
376 struct inode_operations *iops;
377 struct inode *inode = NULL;
378 // time_t i_ctime = 0;
383 // struct dentry_operations *save_dop = NULL;
388 if (currentfs_is_under_dotsnap(dentry)) {
392 cache = snap_find_cache(dir->i_dev);
397 handle = snap_trans_start(cache, dir, SNAP_OP_RMDIR);
399 if ( snap_needs_cow(dir) != -1 ) {
400 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
401 snap_debug_device_fail(dir->i_dev, SNAP_OP_RMDIR, 1);
402 snap_do_cow(dir, get_parent_ino(dir), 0);
405 iops = filter_c2cdiops(cache->cache_filter);
406 if (!iops || !iops->rmdir)
407 GOTO(exit, rc = -EINVAL);
409 /* XXX : there are two cases that we can't remove this inode from disk.
410 1. the inode needs to be cowed.
411 2. the inode is a redirector.
412 then we must keep this inode(dir) so that the inode
413 will not be deleted after rmdir, will only remove dentry
416 if (snap_needs_cow(dentry->d_inode) != -1 ||
417 snap_is_redirector(dentry->d_inode)) {
418 snap_debug_device_fail(dir->i_dev, SNAP_OP_RMDIR, 2);
419 snap_do_cow (dir, get_parent_ino(dir), SNAP_CREATE_IND_DEL_PRI);
424 printk("set up dentry ops, before %p\n",dentry->d_op);
425 save_dop = dentry->d_op;
427 filter_setup_dentry_ops(cache->cache_filter,
428 dentry->d_op, ¤tfs_dentry_ops);
429 dentry->d_op = filter_c2udops(cache->cache_filter);
431 printk("set up dentry ops, after %p\n",dentry->d_op);
437 if( keep_inode && dentry->d_inode ) {
438 ino = dentry->d_inode->i_ino;
439 // i_ctime = dentry->d_inode->i_ctime;
440 i_nlink = dentry->d_inode->i_nlink;
441 i_size = dentry->d_inode->i_size;
444 snap_debug_device_fail(dir->i_dev, SNAP_OP_RMDIR, 4);
445 rc = iops->rmdir(dir, dentry);
446 snap_debug_device_fail(dir->i_dev, SNAP_OP_RMDIR, 5);
448 /* XXX : check this */
451 dentry->d_op = save_dop;
452 printk("restore dentry ops, now at %p\n",dentry->d_op);
457 if( keep_inode && ino) {
458 inode = iget ( dir->i_sb, ino);
460 // inode->i_ctime = i_ctime;
461 inode->i_nlink = i_nlink;
462 inode->i_size = i_size;
463 mark_inode_dirty(inode);
465 #ifdef CONFIG_SNAPFS_EXT3
467 * In Ext3, rmdir() will put this inode into
468 * orphan list, we must remove it out. It's ugly!!
470 if( cache->cache_type == FILTER_FS_EXT3 )
471 ext3_orphan_del(handle, inode);
473 snap_debug_device_fail(dir->i_dev, SNAP_OP_RMDIR, 6);
477 snap_trans_commit(cache, handle);
482 static int currentfs_unlink(struct inode *dir, struct dentry *dentry)
484 struct inode *inode = dentry->d_inode;
485 struct snap_cache *cache;
487 struct inode_operations *iops;
492 if (currentfs_is_under_dotsnap(dentry)) {
496 cache = snap_find_cache(dir->i_dev);
501 handle = snap_trans_start(cache, dir, SNAP_OP_UNLINK);
503 if ( snap_needs_cow(dir) != -1 ) {
504 CDEBUG(D_INODE, "snap_needs_cow for ino %lu \n",dir->i_ino);
505 snap_debug_device_fail(dir->i_dev, SNAP_OP_UNLINK, 1);
506 snap_do_cow(dir, get_parent_ino(dir), 0);
509 iops = filter_c2cdiops(cache->cache_filter);
510 if (!iops || !iops->unlink) {
515 /* XXX : if nlink for this inode is 1, there are two cases that we
516 can't remove this inode from disk.
517 1. the inode needs to be cowed.
518 2. the inode is a redirector.
519 then we increament dentry->d_inode->i_nlink so that the inode
520 will not be deleted after unlink, will only remove dentry
523 if( snap_needs_cow (inode) != -1) {
524 /* call snap_do_cow with DEL_WITHOUT_IND option */
525 snap_debug_device_fail(dir->i_dev, SNAP_OP_UNLINK, 2);
526 snap_do_cow(inode, dir->i_ino, SNAP_CREATE_IND_DEL_PRI);
527 if( inode->i_nlink == 1 )
529 } else if (snap_is_redirector (inode) && inode->i_nlink == 1) {
530 /* call snap_do_cow with DEL_WITH_IND option
531 * just free the blocks of inode, not really delete it
533 snap_debug_device_fail(dir->i_dev, SNAP_OP_UNLINK, 3);
534 snap_do_cow (inode, dir->i_ino, SNAP_CREATE_IND_DEL_PRI);
538 snap_debug_device_fail(dir->i_dev, SNAP_OP_UNLINK, 4);
539 rc = iops->unlink(dir, dentry);
540 snap_debug_device_fail(dir->i_dev, SNAP_OP_UNLINK, 5);
543 snap_trans_commit(cache, handle);
547 static int currentfs_rename (struct inode * old_dir, struct dentry *old_dentry,
548 struct inode * new_dir, struct dentry *new_dentry)
550 struct snap_cache *cache;
552 struct inode_operations *iops;
557 if (currentfs_is_under_dotsnap(old_dentry) ||
558 currentfs_is_under_dotsnap(new_dentry)) {
562 cache = snap_find_cache(old_dir->i_dev);
567 handle = snap_trans_start(cache, old_dir, SNAP_OP_RENAME);
569 /* Always cow the old dir and old dentry->d_inode */
570 if ( snap_needs_cow(old_dir) != -1 ) {
571 CDEBUG(D_INODE, "rename: needs_cow for old_dir %lu\n",old_dir->i_ino);
572 snap_debug_device_fail(old_dir->i_dev, SNAP_OP_RENAME, 1);
573 snap_do_cow(old_dir, get_parent_ino(old_dir), 0);
575 if( snap_needs_cow (old_dentry->d_inode) != -1) {
576 CDEBUG(D_INODE, "rename: needs_cow for old_dentry, ino %lu\n",
577 old_dentry->d_inode->i_ino);
578 snap_debug_device_fail(old_dir->i_dev, SNAP_OP_RENAME, 2);
579 snap_do_cow(old_dentry->d_inode, old_dir->i_ino,0);
582 /* If it's not in the same dir, whether the new_dentry is NULL or not,
583 * we should cow the new_dir. Because rename will use the ino of
584 * old_dentry as the ino of the new_dentry in new_dir.
586 if(( old_dir != new_dir) ) {
587 if( snap_needs_cow(new_dir) !=-1 ){
588 CDEBUG(D_INODE, "rename:snap_needs_cow for new_dir %lu\n",
590 snap_debug_device_fail(old_dir->i_dev,SNAP_OP_RENAME,3);
591 snap_do_cow(new_dir, get_parent_ino(new_dir), 0);
596 if( ( old_dir != new_dir) && ( new_dentry->d_inode )) {
597 if(snap_needs_cow(new_dentry->d_inode) !=-1 ){
598 printk("rename:needs_cow for new_entry ,ino %lu\n",
599 new_dentry->d_inode->i_ino);
600 snap_debug_device_fail(old_dir->i_dev, SNAP_OP_RENAME, 4);
601 snap_do_cow (new_dentry->d_inode,
602 new_dentry->d_parent->d_inode->i_ino, 0);
606 /* The inode for the new_dentry will be freed for normal rename option.
607 * But we should keep this inode since we need to keep it available
608 * for the clone and for snap rollback
610 if( new_dentry->d_inode && new_dentry->d_inode->i_nlink == 1 ) {
611 if( snap_needs_cow (new_dentry->d_inode) != -1) {
612 /* call snap_do_cow with DEL_WITHOUT_IND option */
613 snap_debug_device_fail(old_dir->i_dev,SNAP_OP_RENAME,4);
614 snap_do_cow(new_dentry->d_inode, new_dir->i_ino,
615 SNAP_CREATE_IND_DEL_PRI);
616 new_dentry->d_inode->i_nlink++;
618 else if( snap_is_redirector (new_dentry->d_inode) ) {
619 /* call snap_do_cow with DEL_WITH_IND option
620 * just free the blocks of inode, not really delete it
622 snap_debug_device_fail(old_dir->i_dev,SNAP_OP_RENAME,4);
623 snap_do_cow (new_dentry->d_inode, new_dir->i_ino,
624 SNAP_CREATE_IND_DEL_PRI);
625 new_dentry->d_inode->i_nlink++;
629 iops = filter_c2cdiops(cache->cache_filter);
630 if (!iops || !iops->rename) {
635 snap_debug_device_fail(old_dir->i_dev, SNAP_OP_RENAME, 5);
636 rc = iops->rename(old_dir, old_dentry, new_dir, new_dentry);
637 snap_debug_device_fail(old_dir->i_dev, SNAP_OP_RENAME, 6);
640 snap_trans_commit(cache, handle);
644 static int currentfs_readdir(struct file *filp, void *dirent,
647 struct snap_cache *cache;
648 struct file_operations *fops;
652 if( !filp || !filp->f_dentry || !filp->f_dentry->d_inode ) {
656 cache = snap_find_cache(filp->f_dentry->d_inode->i_dev);
660 fops = filter_c2cdfops( cache->cache_filter );
666 * no action if we are under clonefs or .snap
668 if( cache->cache_show_dotsnap &&
669 (filp->f_dentry->d_sb == cache->cache_sb) &&
670 !currentfs_is_under_dotsnap(filp->f_dentry) ){
671 if( filp->f_pos == 0 ){
672 if( filldir(dirent, ".snap",
673 strlen(".snap")+1, filp->f_pos,
677 filp->f_pos += strlen(".snap")+1;
679 filp->f_pos -= strlen(".snap")+1;
680 rc = fops->readdir(filp, dirent, filldir);
681 filp->f_pos += strlen(".snap")+1;
683 rc = fops->readdir(filp, dirent, filldir);
688 struct file_operations currentfs_dir_fops = {
689 readdir: currentfs_readdir
692 struct inode_operations currentfs_dir_iops = {
693 create: currentfs_create,
694 mkdir: currentfs_mkdir,
695 link: currentfs_link,
696 symlink: currentfs_symlink,
697 mknod: currentfs_mknod,
698 rmdir: currentfs_rmdir,
699 unlink: currentfs_unlink,
700 rename: currentfs_rename,
701 lookup: currentfs_lookup