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 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
17 static void prepare_parent_dentry(struct dentry *dentry, struct inode *inode)
19 atomic_set(&dentry->d_count, 1);
20 dentry->d_vfs_flags = 0;
22 dentry->d_inode = inode;
24 dentry->d_fsdata = NULL;
25 dentry->d_mounted = 0;
26 INIT_LIST_HEAD(&dentry->d_hash);
27 INIT_LIST_HEAD(&dentry->d_lru);
28 INIT_LIST_HEAD(&dentry->d_subdirs);
29 INIT_LIST_HEAD(&dentry->d_alias);
32 void d_unalloc(struct dentry *dentry)
35 list_del(&dentry->d_hash);
36 INIT_LIST_HEAD(&dentry->d_hash);
37 dput(dentry); /* this will free the dentry memory */
40 static int smfs_create(struct inode *dir,
41 struct dentry *dentry,
44 struct inode *cache_dir;
45 struct inode *cache_inode = NULL, *inode;
47 struct dentry *cache_dentry = NULL;
52 cache_dir = I2CI(dir);
56 prepare_parent_dentry(&parent, cache_dir);
57 cache_dentry = d_alloc(&parent, &dentry->d_name);
63 if (cache_dir && cache_dir->i_op->create)
64 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
69 cache_inode = igrab(cache_dentry->d_inode);
71 inode = iget(dir->i_sb, cache_inode->i_ino);
74 GOTO(exit, rc = -ENOMEM);
76 d_instantiate(dentry, inode);
78 sm_set_inode_ops(cache_inode, inode);
80 d_unalloc(cache_dentry);
84 static struct dentry *smfs_lookup(struct inode *dir,
85 struct dentry *dentry)
87 struct inode *cache_dir;
88 struct inode *cache_inode = NULL, *inode;
90 struct dentry *cache_dentry = NULL;
91 struct dentry *rc = NULL;
95 cache_dir = I2CI(dir);
97 RETURN(ERR_PTR(-ENOENT));
98 prepare_parent_dentry(&parent, cache_dir);
99 cache_dentry = d_alloc(&parent, &dentry->d_name);
101 if(cache_dir && cache_dir->i_op->lookup)
102 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
104 if (rc || !cache_dentry->d_inode ||
105 is_bad_inode(cache_dentry->d_inode) ||
106 IS_ERR(cache_dentry->d_inode)) {
110 cache_inode = igrab(cache_dentry->d_inode);
112 inode = iget(dir->i_sb, cache_inode->i_ino);
114 d_add(dentry, inode);
116 d_unalloc(cache_dentry);
120 static int smfs_lookup_raw(struct inode *dir, const char *name,
121 int len, ino_t *data)
123 struct inode *cache_dir;
126 cache_dir = I2CI(dir);
131 if (cache_dir->i_op->lookup_raw)
132 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
137 static int smfs_link(struct dentry * old_dentry,
138 struct inode * dir, struct dentry *dentry)
140 struct inode *cache_old_inode = NULL;
141 struct inode *cache_dir = I2CI(dir);
142 struct inode *inode = NULL;
143 struct dentry *cache_dentry;
144 struct dentry *cache_old_dentry;
145 struct dentry parent;
146 struct dentry parent_old;
149 inode = old_dentry->d_inode;
151 cache_old_inode = I2CI(inode);
153 if (!cache_old_inode || !cache_dir)
156 prepare_parent_dentry(&parent, cache_dir);
157 cache_dentry = d_alloc(&parent, &dentry->d_name);
159 prepare_parent_dentry(&parent_old, cache_dir);
160 cache_old_dentry = d_alloc(&parent_old, &dentry->d_name);
161 d_add(cache_old_dentry, cache_old_inode);
163 if (cache_dir->i_op->link)
164 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
169 atomic_inc(&inode->i_count);
170 duplicate_inode(cache_old_dentry->d_inode, inode);
171 d_instantiate(dentry, inode);
174 if (cache_dentry->d_inode)
175 igrab(cache_dentry->d_inode);
176 if (cache_old_dentry->d_inode)
177 igrab(cache_old_dentry->d_inode);
179 d_unalloc(cache_dentry);
180 d_unalloc(cache_old_dentry);
185 static int smfs_unlink(struct inode * dir,
186 struct dentry *dentry)
188 struct inode *cache_dir = I2CI(dir);
189 struct inode *cache_inode = I2CI(dentry->d_inode);
190 struct dentry *cache_dentry;
191 struct dentry parent;
194 if (!cache_dir || !cache_inode)
197 prepare_parent_dentry(&parent, cache_dir);
198 cache_dentry = d_alloc(&parent, &dentry->d_name);
199 d_add(cache_dentry, cache_inode);
201 if (cache_dir->i_op->unlink)
202 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
205 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
206 duplicate_inode(cache_dir, dir);
208 igrab(cache_dentry->d_inode);
210 d_unalloc(cache_dentry);
214 static int smfs_symlink (struct inode * dir,
215 struct dentry *dentry,
216 const char * symname)
218 struct inode *cache_dir = I2CI(dir);
219 struct inode *cache_inode = NULL;
220 struct inode *inode = NULL;
221 struct dentry *cache_dentry;
222 struct dentry parent;
228 prepare_parent_dentry(&parent, cache_dir);
229 cache_dentry = d_alloc(&parent, &dentry->d_name);
231 if (cache_dir->i_op->symlink)
232 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
234 cache_inode = igrab(cache_dentry->d_inode);
236 inode = iget(dir->i_sb, cache_inode->i_ino);
239 d_instantiate(dentry, inode);
243 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;
256 struct dentry parent;
262 prepare_parent_dentry(&parent, cache_dir);
263 cache_dentry = d_alloc(&parent, &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 = igrab(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);
282 static int smfs_rmdir(struct inode * dir,
283 struct dentry *dentry)
285 struct inode *cache_dir = I2CI(dir);
286 struct inode *cache_inode = I2CI(dentry->d_inode);
287 struct dentry *cache_dentry;
288 struct dentry parent;
294 prepare_parent_dentry(&parent, cache_dir);
295 cache_dentry = d_alloc(&parent, &dentry->d_name);
296 d_add(cache_dentry, cache_inode);
300 if (cache_dir->i_op->rmdir)
301 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
303 duplicate_inode(cache_dir, dir);
304 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
306 d_unalloc(cache_dentry);
310 static int smfs_mknod(struct inode * dir, struct dentry *dentry,
313 struct inode *cache_dir = I2CI(dir);
314 struct inode *inode = NULL;
315 struct inode *cache_inode = NULL;
316 struct dentry *cache_dentry;
317 struct dentry parent;
323 prepare_parent_dentry(&parent, cache_dir);
324 cache_dentry = d_alloc(&parent, &dentry->d_name);
326 if (cache_dir->i_op->mknod)
327 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
329 if (cache_dentry->d_inode)
330 cache_inode = igrab(cache_dentry->d_inode);
334 inode = iget(dir->i_sb, cache_inode->i_ino);
335 d_instantiate(dentry, inode);
336 duplicate_inode(cache_dir, dir);
337 duplicate_inode(cache_dentry->d_inode, dentry->d_inode);
339 d_unalloc(cache_dentry);
342 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
343 struct inode * new_dir,struct dentry *new_dentry)
345 struct inode *cache_old_dir = I2CI(old_dir);
346 struct inode *cache_new_dir = I2CI(new_dir);
347 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
348 struct dentry *cache_old_dentry;
349 struct dentry *cache_new_dentry;
350 struct dentry parent_new;
351 struct dentry parent_old;
354 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
357 prepare_parent_dentry(&parent_old, cache_old_dir);
358 cache_old_dentry = d_alloc(&parent_old, &old_dentry->d_name);
359 d_add(cache_old_dentry, cache_old_inode);
360 igrab(cache_old_inode);
362 prepare_parent_dentry(&parent_new, cache_new_dir);
363 cache_new_dentry = d_alloc(&parent_new, &new_dentry->d_name);
365 if (cache_old_dir->i_op->rename)
366 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
367 cache_new_dir, cache_new_dentry);
369 duplicate_inode(cache_old_dir, old_dir);
370 duplicate_inode(cache_new_dir, new_dir);
371 if (cache_new_dentry->d_inode) {
372 igrab(cache_new_dentry->d_inode);
374 d_unalloc(cache_old_dentry);
375 d_unalloc(cache_new_dentry);
379 struct inode_operations smfs_dir_iops = {
382 lookup_raw: smfs_lookup_raw, /* BKL held */
383 link: smfs_link, /* BKL held */
384 unlink: smfs_unlink, /* BKL held */
385 symlink: smfs_symlink, /* BKL held */
386 mkdir: smfs_mkdir, /* BKL held */
387 rmdir: smfs_rmdir, /* BKL held */
388 mknod: smfs_mknod, /* BKL held */
389 rename: smfs_rename, /* BKL held */
390 setxattr: smfs_setxattr, /* BKL held */
391 getxattr: smfs_getxattr, /* BKL held */
392 listxattr: smfs_listxattr, /* BKL held */
393 removexattr: smfs_removexattr, /* BKL held */
396 static ssize_t smfs_read_dir(struct file *filp, char *buf,
397 size_t size, loff_t *ppos)
399 struct dentry *dentry = filp->f_dentry;
400 struct inode *cache_inode = NULL;
401 struct file open_file;
402 struct dentry open_dentry;
405 cache_inode = I2CI(dentry->d_inode);
410 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
411 &open_file, &open_dentry);
413 if (cache_inode->i_fop->read)
414 rc = cache_inode->i_fop->read(&open_file, buf, size, ppos);
416 smfs_update_file(filp, &open_file);
420 static int smfs_readdir(struct file * filp,
424 struct dentry *dentry = filp->f_dentry;
425 struct inode *cache_inode = NULL;
426 struct file open_file;
427 struct dentry open_dentry;
430 cache_inode = I2CI(dentry->d_inode);
435 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
436 &open_file, &open_dentry);
438 if (cache_inode->i_fop->readdir)
439 rc = cache_inode->i_fop->readdir(&open_file, dirent, filldir);
441 smfs_update_file(filp, &open_file);
445 struct file_operations smfs_dir_fops = {
447 readdir: smfs_readdir, /* BKL held */
448 ioctl: smfs_ioctl, /* BKL held */
449 fsync: smfs_fsync, /* BKL held */