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/lustre_idl.h>
13 #include <linux/smp_lock.h>
15 #include "smfs_internal.h"
18 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
20 void prepare_parent_dentry(struct dentry *dentry, struct inode *inode)
22 atomic_set(&dentry->d_count, 1);
23 dentry->d_vfs_flags = 0;
25 dentry->d_inode = inode;
27 dentry->d_fsdata = NULL;
28 dentry->d_mounted = 0;
29 INIT_LIST_HEAD(&dentry->d_hash);
30 INIT_LIST_HEAD(&dentry->d_lru);
31 INIT_LIST_HEAD(&dentry->d_subdirs);
32 INIT_LIST_HEAD(&dentry->d_alias);
35 void d_unalloc(struct dentry *dentry)
38 list_del(&dentry->d_hash);
39 INIT_LIST_HEAD(&dentry->d_hash);
40 dput(dentry); /* this will free the dentry memory */
43 static int smfs_create(struct inode *dir,
44 struct dentry *dentry,
47 struct inode *cache_dir;
48 struct inode *cache_inode = NULL, *inode;
50 struct dentry *cache_dentry = NULL;
55 cache_dir = I2CI(dir);
59 prepare_parent_dentry(&parent, cache_dir);
60 cache_dentry = d_alloc(&parent, &dentry->d_name);
66 if (cache_dir && cache_dir->i_op->create)
67 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
72 cache_inode = igrab(cache_dentry->d_inode);
74 inode = iget(dir->i_sb, cache_inode->i_ino);
77 GOTO(exit, rc = -ENOMEM);
79 d_instantiate(dentry, inode);
81 sm_set_inode_ops(cache_inode, inode);
83 d_unalloc(cache_dentry);
87 static struct dentry *smfs_lookup(struct inode *dir,
88 struct dentry *dentry)
90 struct inode *cache_dir;
91 struct inode *cache_inode = NULL, *inode;
93 struct dentry *cache_dentry = NULL;
94 struct dentry *rc = NULL;
98 cache_dir = I2CI(dir);
100 RETURN(ERR_PTR(-ENOENT));
101 prepare_parent_dentry(&parent, cache_dir);
102 cache_dentry = d_alloc(&parent, &dentry->d_name);
104 if(cache_dir && cache_dir->i_op->lookup)
105 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
107 if (rc || !cache_dentry->d_inode ||
108 is_bad_inode(cache_dentry->d_inode) ||
109 IS_ERR(cache_dentry->d_inode)) {
113 cache_inode = igrab(cache_dentry->d_inode);
115 inode = iget(dir->i_sb, cache_inode->i_ino);
117 d_add(dentry, inode);
119 d_unalloc(cache_dentry);
123 static int smfs_lookup_raw(struct inode *dir, const char *name,
124 int len, ino_t *data)
126 struct inode *cache_dir;
129 cache_dir = I2CI(dir);
134 if (cache_dir->i_op->lookup_raw)
135 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
140 static int smfs_link(struct dentry * old_dentry,
141 struct inode * dir, struct dentry *dentry)
143 struct inode *cache_old_inode = NULL;
144 struct inode *cache_dir = I2CI(dir);
145 struct inode *inode = NULL;
146 struct dentry *cache_dentry;
147 struct dentry *cache_old_dentry;
148 struct dentry parent;
149 struct dentry parent_old;
152 inode = old_dentry->d_inode;
154 cache_old_inode = I2CI(inode);
156 if (!cache_old_inode || !cache_dir)
159 prepare_parent_dentry(&parent, cache_dir);
160 cache_dentry = d_alloc(&parent, &dentry->d_name);
162 prepare_parent_dentry(&parent_old, cache_dir);
163 cache_old_dentry = d_alloc(&parent_old, &dentry->d_name);
164 d_add(cache_old_dentry, cache_old_inode);
166 if (cache_dir->i_op->link)
167 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
172 atomic_inc(&inode->i_count);
173 post_smfs_inode(inode, cache_old_dentry->d_inode);
174 d_instantiate(dentry, inode);
177 if (cache_dentry->d_inode)
178 igrab(cache_dentry->d_inode);
179 if (cache_old_dentry->d_inode)
180 igrab(cache_old_dentry->d_inode);
182 d_unalloc(cache_dentry);
183 d_unalloc(cache_old_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;
194 struct dentry parent;
197 if (!cache_dir || !cache_inode)
200 prepare_parent_dentry(&parent, cache_dir);
201 cache_dentry = d_alloc(&parent, &dentry->d_name);
202 d_add(cache_dentry, cache_inode);
204 if (cache_dir->i_op->unlink)
205 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
208 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
209 post_smfs_inode(dir, cache_dir);
211 igrab(cache_dentry->d_inode);
213 d_unalloc(cache_dentry);
217 static int smfs_symlink (struct inode * dir,
218 struct dentry *dentry,
219 const char * symname)
221 struct inode *cache_dir = I2CI(dir);
222 struct inode *cache_inode = NULL;
223 struct inode *inode = NULL;
224 struct dentry *cache_dentry;
225 struct dentry parent;
231 prepare_parent_dentry(&parent, cache_dir);
232 cache_dentry = d_alloc(&parent, &dentry->d_name);
234 if (cache_dir->i_op->symlink)
235 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
237 cache_inode = igrab(cache_dentry->d_inode);
239 inode = iget(dir->i_sb, cache_inode->i_ino);
242 d_instantiate(dentry, inode);
246 d_unalloc(cache_dentry);
251 static int smfs_mkdir(struct inode * dir,
252 struct dentry * dentry,
255 struct inode *cache_dir = I2CI(dir);
256 struct inode *cache_inode = NULL;
257 struct inode *inode = NULL;
258 struct dentry *cache_dentry;
259 struct dentry parent;
266 handle = smfs_trans_start(cache_dir, KML_OPCODE_MKDIR);
267 if (IS_ERR(handle) ) {
268 CERROR("smfs_do_mkdir: no space for transaction\n");
272 prepare_parent_dentry(&parent, cache_dir);
273 cache_dentry = d_alloc(&parent, &dentry->d_name);
276 if (cache_dir->i_op->mkdir)
277 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
279 cache_inode = igrab(cache_dentry->d_inode);
281 inode = iget(dir->i_sb, cache_inode->i_ino);
284 GOTO(exit, rc = -ENOENT);
286 d_instantiate(dentry, inode);
288 if (smfs_do_kml(dir)) {
289 rc = post_kml_mkdir(dir, dentry);
292 post_smfs_inode(dir, cache_dir);
295 smfs_trans_commit(handle);
296 d_unalloc(cache_dentry);
300 static int smfs_rmdir(struct inode * dir,
301 struct dentry *dentry)
303 struct inode *cache_dir = I2CI(dir);
304 struct inode *cache_inode = I2CI(dentry->d_inode);
305 struct dentry *cache_dentry;
306 struct dentry parent;
312 prepare_parent_dentry(&parent, cache_dir);
313 cache_dentry = d_alloc(&parent, &dentry->d_name);
314 d_add(cache_dentry, cache_inode);
318 if (cache_dir->i_op->rmdir)
319 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
321 post_smfs_inode(dir, cache_dir);
322 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
323 d_unalloc(cache_dentry);
327 static int smfs_mknod(struct inode * dir, struct dentry *dentry,
330 struct inode *cache_dir = I2CI(dir);
331 struct inode *inode = NULL;
332 struct inode *cache_inode = NULL;
333 struct dentry *cache_dentry;
334 struct dentry parent;
340 prepare_parent_dentry(&parent, cache_dir);
341 cache_dentry = d_alloc(&parent, &dentry->d_name);
343 if (cache_dir->i_op->mknod)
344 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
346 if (cache_dentry->d_inode)
347 cache_inode = igrab(cache_dentry->d_inode);
351 inode = iget(dir->i_sb, cache_inode->i_ino);
352 d_instantiate(dentry, inode);
353 post_smfs_inode(dir, cache_dir);
354 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
356 d_unalloc(cache_dentry);
359 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
360 struct inode * new_dir,struct dentry *new_dentry)
362 struct inode *cache_old_dir = I2CI(old_dir);
363 struct inode *cache_new_dir = I2CI(new_dir);
364 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
365 struct dentry *cache_old_dentry;
366 struct dentry *cache_new_dentry;
367 struct dentry parent_new;
368 struct dentry parent_old;
371 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
374 prepare_parent_dentry(&parent_old, cache_old_dir);
375 cache_old_dentry = d_alloc(&parent_old, &old_dentry->d_name);
376 d_add(cache_old_dentry, cache_old_inode);
377 igrab(cache_old_inode);
379 prepare_parent_dentry(&parent_new, cache_new_dir);
380 cache_new_dentry = d_alloc(&parent_new, &new_dentry->d_name);
382 if (cache_old_dir->i_op->rename)
383 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
384 cache_new_dir, cache_new_dentry);
386 post_smfs_inode(old_dir, cache_old_dir) ;
387 post_smfs_inode(new_dir, cache_new_dir);
388 if (cache_new_dentry->d_inode) {
389 igrab(cache_new_dentry->d_inode);
391 d_unalloc(cache_old_dentry);
392 d_unalloc(cache_new_dentry);
396 struct inode_operations smfs_dir_iops = {
399 lookup_raw: smfs_lookup_raw, /* BKL held */
400 link: smfs_link, /* BKL held */
401 unlink: smfs_unlink, /* BKL held */
402 symlink: smfs_symlink, /* BKL held */
403 mkdir: smfs_mkdir, /* BKL held */
404 rmdir: smfs_rmdir, /* BKL held */
405 mknod: smfs_mknod, /* BKL held */
406 rename: smfs_rename, /* BKL held */
407 setxattr: smfs_setxattr, /* BKL held */
408 getxattr: smfs_getxattr, /* BKL held */
409 listxattr: smfs_listxattr, /* BKL held */
410 removexattr: smfs_removexattr, /* BKL held */
413 static ssize_t smfs_read_dir(struct file *filp, char *buf,
414 size_t size, loff_t *ppos)
416 struct dentry *dentry = filp->f_dentry;
417 struct inode *cache_inode = NULL;
418 struct file open_file;
419 struct dentry open_dentry;
422 cache_inode = I2CI(dentry->d_inode);
427 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
428 &open_file, &open_dentry);
430 if (cache_inode->i_fop->read)
431 rc = cache_inode->i_fop->read(&open_file, buf, size, ppos);
433 smfs_update_file(filp, &open_file);
437 static int smfs_readdir(struct file * filp,
441 struct dentry *dentry = filp->f_dentry;
442 struct inode *cache_inode = NULL;
443 struct file open_file;
444 struct dentry open_dentry;
447 cache_inode = I2CI(dentry->d_inode);
452 smfs_prepare_cachefile(dentry->d_inode, filp, cache_inode,
453 &open_file, &open_dentry);
455 if (cache_inode->i_fop->readdir)
456 rc = cache_inode->i_fop->readdir(&open_file, dirent, filldir);
458 smfs_update_file(filp, &open_file);
462 struct file_operations smfs_dir_fops = {
464 readdir: smfs_readdir, /* BKL held */
465 ioctl: smfs_ioctl, /* BKL held */
466 fsync: smfs_fsync, /* BKL held */