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>
13 #include "smfs_internal.h"
15 static void d_unalloc(struct dentry *dentry)
18 list_del(&dentry->d_hash);
19 INIT_LIST_HEAD(&dentry->d_hash);
23 static struct inode *sm_create_inode(struct super_block *sb,
24 struct inode *cache_inode)
28 inode = new_inode(sb);
30 /*FIXME there are still some
31 * other attributes need to
33 inode->i_ino = cache_inode->i_ino;
34 inode->i_mode = cache_inode->i_mode;
36 I2CI(inode) = cache_inode;
40 static void prepare_parent_dentry(struct dentry *dentry, struct inode *inode)
42 atomic_set(&dentry->d_count, 1);
43 dentry->d_vfs_flags = 0;
45 dentry->d_inode = inode;
47 dentry->d_fsdata = NULL;
48 dentry->d_mounted = 0;
49 INIT_LIST_HEAD(&dentry->d_hash);
50 INIT_LIST_HEAD(&dentry->d_lru);
51 INIT_LIST_HEAD(&dentry->d_subdirs);
52 INIT_LIST_HEAD(&dentry->d_alias);
55 static int smfs_create(struct inode *dir,
56 struct dentry *dentry,
59 struct inode *cache_dir;
60 struct inode *cache_inode, *inode;
62 struct dentry *cache_dentry;
67 cache_dir = I2CI(dir);
71 prepare_parent_dentry(&tmp, cache_dir);
72 cache_dentry = d_alloc(&tmp, &dentry->d_name);
77 if(cache_dir && cache_dir->i_op->create)
78 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
82 cache_inode = cache_dentry->d_inode;
84 // inode = sm_create_inode(dir->i_sb, cache_inode);
85 inode = iget(dir->i_sb, cache_inode->i_ino);
88 GOTO(exit, rc = -ENOMEM);
90 d_instantiate(dentry, inode);
92 sm_set_inode_ops(cache_inode, inode);
94 d_unalloc(cache_dentry);
98 static struct dentry *smfs_lookup(struct inode *dir,
99 struct dentry *dentry)
101 struct inode *cache_dir;
102 struct inode *cache_inode, *inode;
104 struct dentry *cache_dentry;
105 struct dentry *rc = NULL;
109 cache_dir = I2CI(dir);
111 RETURN(ERR_PTR(-ENOENT));
112 prepare_parent_dentry(&tmp, cache_dir);
113 cache_dentry = d_alloc(&tmp, &dentry->d_name);
116 RETURN(ERR_PTR(-ENOENT));
118 if(cache_dir && cache_dir->i_op->lookup)
119 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
121 if (rc || !cache_dentry->d_inode ||
122 is_bad_inode(cache_dentry->d_inode) ||
123 IS_ERR(cache_dentry->d_inode)) {
127 cache_inode = cache_dentry->d_inode;
129 inode = iget(dir->i_sb, cache_inode->i_ino);
131 d_add(dentry, inode);
133 d_unalloc(cache_dentry);
137 static int smfs_lookup_raw(struct inode *dir, const char *name,
138 int len, ino_t *data)
140 struct inode *cache_dir;
143 cache_dir = I2CI(dir);
148 if (cache_dir->i_op->lookup_raw)
149 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
154 static int smfs_link(struct dentry * old_dentry,
155 struct inode * dir, struct dentry *dentry)
157 struct inode *cache_old_inode = NULL;
158 struct inode *cache_dir = I2CI(dir);
159 struct inode *inode = NULL;
160 struct dentry *cache_dentry = NULL;
162 struct dentry tmp_old;
165 inode = old_dentry->d_inode;
167 cache_old_inode = I2CI(inode);
169 if (!cache_old_inode || !dir)
172 prepare_parent_dentry(&tmp_old, cache_old_inode);
174 prepare_parent_dentry(&tmp, cache_dir);
175 cache_dentry = d_alloc(&tmp, &dentry->d_name);
177 if (cache_dir->i_op->link)
178 rc = cache_dir->i_op->link(&tmp, cache_dir, cache_dentry);
181 d_instantiate(dentry, inode);
184 d_unalloc(cache_dentry);
188 static int smfs_unlink(struct inode * dir,
189 struct dentry *dentry)
191 struct inode *cache_dir = I2CI(dir);
192 struct inode *cache_inode = I2CI(dentry->d_inode);
193 struct dentry *cache_dentry = NULL;
197 if (!cache_dir || !cache_inode)
200 igrab(cache_dentry->d_inode);
202 prepare_parent_dentry(&tmp, cache_dir);
203 cache_dentry = d_alloc(&tmp, &dentry->d_name);
204 d_add(cache_dentry, cache_inode);
206 if (cache_dir->i_op->unlink)
207 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
210 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
211 duplicate_inode(cache_dir, dir);
213 d_unalloc(cache_dentry);
216 static int smfs_symlink (struct inode * dir,
217 struct dentry *dentry,
218 const char * symname)
220 struct inode *cache_dir = I2CI(dir);
221 struct inode *cache_inode = NULL;
222 struct inode *inode = NULL;
223 struct dentry *cache_dentry = NULL;
230 prepare_parent_dentry(&tmp, NULL);
231 cache_dentry = d_alloc(&tmp, &dentry->d_name);
233 if (cache_inode->i_op->symlink)
234 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
236 cache_inode = cache_dentry->d_inode;
238 inode = iget(dir->i_sb, cache_inode->i_ino);
241 d_instantiate(dentry, inode);
245 d_unalloc(cache_dentry);
248 static int smfs_mkdir(struct inode * dir,
249 struct dentry * dentry,
252 struct inode *cache_dir = I2CI(dir);
253 struct inode *cache_inode = NULL;
254 struct inode *inode = NULL;
255 struct dentry *cache_dentry = NULL;
262 prepare_parent_dentry(&tmp, NULL);
263 cache_dentry = d_alloc(&tmp, &dentry->d_name);
265 if (cache_dir->i_op->mkdir)
266 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
268 cache_inode = cache_dentry->d_inode;
270 inode = iget(dir->i_sb, cache_inode->i_ino);
273 GOTO(exit, rc = -ENOENT);
275 d_instantiate(dentry, inode);
276 duplicate_inode(cache_dir, dir);
278 d_unalloc(cache_dentry);
281 static int smfs_rmdir(struct inode * dir,
282 struct dentry *dentry)
284 struct inode *cache_dir = I2CI(dir);
285 struct dentry *cache_dentry = NULL;
292 prepare_parent_dentry(&tmp, NULL);
293 cache_dentry = d_alloc(&tmp, &dentry->d_name);
295 if (cache_dir->i_op->rmdir)
296 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
298 duplicate_inode(cache_dir, dir);
299 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
301 d_unalloc(cache_dentry);
305 static int smfs_mknod(struct inode * dir, struct dentry *dentry,
308 struct inode *cache_dir = I2CI(dir);
309 struct dentry *cache_dentry = NULL;
316 prepare_parent_dentry(&tmp, NULL);
317 cache_dentry = d_alloc(&tmp, &dentry->d_name);
319 if (cache_dir->i_op->mknod)
320 rc = cache_dir->i_op->mknod(cache_dir, dentry, mode, rdev);
322 duplicate_inode(cache_dir, dir);
323 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
325 d_unalloc(cache_dentry);
328 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
329 struct inode * new_dir,struct dentry *new_dentry)
331 struct inode *cache_old_dir = I2CI(old_dir);
332 struct inode *cache_new_dir = I2CI(new_dir);
333 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
334 struct inode *cache_new_inode = NULL;
335 struct inode *new_inode = NULL;
336 struct dentry *cache_old_dentry = NULL;
337 struct dentry *cache_new_dentry = NULL;
338 struct dentry tmp_new;
339 struct dentry tmp_old;
342 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
345 prepare_parent_dentry(&tmp_old, old_dir);
346 cache_old_dentry = d_alloc(&tmp_old, &old_dentry->d_name);
347 d_add(cache_old_dentry, cache_old_inode);
349 prepare_parent_dentry(&tmp_new, NULL);
350 cache_new_dentry = d_alloc(&tmp_new, &new_dentry->d_name);
352 if (cache_old_dir->i_op->rename)
353 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
354 cache_new_dir, cache_new_dentry);
356 cache_new_inode = cache_new_dentry->d_inode;
357 new_inode = iget(new_dir->i_sb, cache_new_inode->i_ino);
359 d_instantiate(new_dentry, new_inode);
361 duplicate_inode(cache_old_dir, old_dir);
362 duplicate_inode(cache_new_dir, new_dir);
364 d_unalloc(cache_old_dentry);
365 d_unalloc(cache_new_dentry);
370 struct inode_operations smfs_dir_iops = {
373 lookup_raw: smfs_lookup_raw, /* BKL held */
374 link: smfs_link, /* BKL held */
375 unlink: smfs_unlink, /* BKL held */
376 symlink: smfs_symlink, /* BKL held */
377 mkdir: smfs_mkdir, /* BKL held */
378 rmdir: smfs_rmdir, /* BKL held */
379 mknod: smfs_mknod, /* BKL held */
380 rename: smfs_rename, /* BKL held */
381 setxattr: smfs_setxattr, /* BKL held */
382 getxattr: smfs_getxattr, /* BKL held */
383 listxattr: smfs_listxattr, /* BKL held */
384 removexattr: smfs_removexattr, /* BKL held */
387 static ssize_t smfs_read_dir(struct file *filp, char *buf,
388 size_t size, loff_t *ppos)
390 struct dentry *dentry = filp->f_dentry;
391 struct inode *cache_inode = NULL;
392 struct file open_file;
393 struct dentry open_dentry;
396 cache_inode = I2CI(dentry->d_inode);
401 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
402 &open_file, &open_dentry);
404 if (cache_inode->i_fop->read)
405 rc = cache_inode->i_fop->read(&open_file, buf, size, ppos);
407 smfs_update_file(filp, &open_file);
411 static int smfs_readdir(struct file * filp,
415 struct dentry *dentry = filp->f_dentry;
416 struct inode *cache_inode = NULL;
417 struct file open_file;
418 struct dentry open_dentry;
421 cache_inode = I2CI(dentry->d_inode);
426 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
427 &open_file, &open_dentry);
429 if (cache_inode->i_fop->readdir)
430 rc = cache_inode->i_fop->readdir(&open_file, dirent, filldir);
432 smfs_update_file(filp, &open_file);
436 struct file_operations smfs_dir_fops = {
438 readdir: smfs_readdir, /* BKL held */
439 ioctl: smfs_ioctl, /* BKL held */
440 fsync: smfs_fsync, /* BKL held */