1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_SM
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/unistd.h>
31 #include <linux/smp_lock.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_support.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_fsfilt.h>
37 #include <linux/lustre_smfs.h>
38 #include <linux/lustre_snap.h>
39 #include <linux/security.h>
40 #include "smfs_internal.h"
42 //#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 static int smfs_create(struct inode *dir, struct dentry *dentry,
48 static int smfs_create(struct inode *dir, struct dentry *dentry,
49 int mode, struct nameidata *nd)
52 struct inode *inode = NULL;
53 struct inode *parent = I2CI(dentry->d_parent->d_inode);
54 struct inode *cache_dir = I2CI(dir);
55 struct dentry *cache_dentry = NULL;
56 struct dentry *cache_parent = NULL;
58 struct hook_msg msg = {
66 LASSERT(cache_dir->i_op->create);
69 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
70 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
71 if (!cache_dentry || !cache_parent) {
76 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
82 SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
84 pre_smfs_inode(dir, cache_dir);
86 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
87 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
89 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
92 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
95 d_instantiate(dentry, inode);
101 SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc);
103 post_smfs_inode(dir, cache_dir);
104 smfs_trans_commit(dir, handle, 0);
108 post_smfs_dentry(cache_dentry);
109 post_smfs_dentry(cache_parent);
113 static struct dentry * iopen_connect_dentry(struct dentry * dentry,
114 struct inode *inode, int rehash)
116 struct dentry *tmp, *goal = NULL;
117 struct list_head *lp;
119 /* verify this dentry is really new */
120 LASSERT(dentry->d_inode == NULL);
121 LASSERT(list_empty(&dentry->d_alias)); /* d_instantiate */
123 LASSERT(d_unhashed(dentry)); /* d_rehash */
124 LASSERT(list_empty(&dentry->d_subdirs));
126 spin_lock(&dcache_lock);
130 /* preferrably return a connected dentry */
131 list_for_each(lp, &inode->i_dentry) {
132 tmp = list_entry(lp, struct dentry, d_alias);
133 if (tmp->d_flags & DCACHE_DISCONNECTED) {
134 LASSERT(tmp->d_alias.next == &inode->i_dentry);
135 LASSERT(tmp->d_alias.prev == &inode->i_dentry);
145 /* Move the goal to the de hash queue */
146 goal->d_flags &= ~ DCACHE_DISCONNECTED;
147 security_d_instantiate(goal, inode);
149 __d_move(goal, dentry);
150 spin_unlock(&dcache_lock);
155 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
157 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
158 dentry->d_inode = inode;
162 spin_unlock(&dcache_lock);
168 static int smfs_do_lookup (struct inode * dir,
169 struct dentry * dentry,
170 struct nameidata *nd,
171 struct inode **inode)
173 struct inode *cache_dir = I2CI(dir);
174 struct inode *parent = I2CI(dentry->d_parent->d_inode);
175 struct dentry *cache_dentry = NULL;
176 struct dentry *cache_parent = NULL;
177 struct dentry *rdentry = NULL, *tmp = NULL;
179 struct hook_msg msg = {
185 cache_dir = I2CI(dir);
189 LASSERT(cache_dir->i_op->lookup);
191 /* preparing artificial backing fs dentries. */
192 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
193 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
194 if (!cache_dentry || !cache_parent) {
199 SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
201 /* perform lookup in backing fs. */
202 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
204 if (IS_ERR(rdentry)) {
205 rc = PTR_ERR(rdentry);
214 SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
217 //copy fields if DCACHE_CROSS_REF
218 smfs_update_dentry(dentry, tmp);
221 *inode = smfs_get_inode(dir->i_sb, tmp->d_inode->i_ino,
233 post_smfs_dentry(cache_dentry);
234 post_smfs_dentry(cache_parent);
239 static struct dentry * smfs_iopen_lookup(struct inode * dir,
240 struct dentry *dentry,
241 struct nameidata *nd)
243 struct dentry * alternate = NULL;
244 struct inode *inode = NULL;
248 rc = smfs_do_lookup(dir, dentry, nd, &inode);
253 /* preferrably return a connected dentry */
254 spin_lock(&dcache_lock);
255 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
256 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
259 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
260 dget_locked(alternate);
261 spin_lock(&alternate->d_lock);
262 alternate->d_flags |= DCACHE_REFERENCED;
263 spin_unlock(&alternate->d_lock);
265 spin_unlock(&dcache_lock);
269 dentry->d_flags |= DCACHE_DISCONNECTED;
271 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
272 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
273 dentry->d_inode = inode;
275 __d_rehash(dentry); /* d_rehash */
276 spin_unlock(&dcache_lock);
281 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
282 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
284 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
285 struct nameidata *nd)
288 struct dentry * rdentry = NULL;
289 struct inode * inode = NULL;
292 rc = smfs_do_lookup(dir, dentry, nd, &inode);
296 //lmv stuff. Special dentry that has no inode.
297 if (dentry->d_flags & DCACHE_CROSS_REF) {
301 //TODO: should flags be checked and copied before?
302 rdentry = iopen_connect_dentry(dentry, inode, 1);
307 static int smfs_link(struct dentry *old_dentry,
308 struct inode *dir, struct dentry *dentry)
310 struct inode *parent = I2CI(dentry->d_parent->d_inode);
311 struct inode *cache_dir = I2CI(dir);
312 struct inode *old_inode = old_dentry->d_inode;
313 struct inode *cache_old_inode = I2CI(old_inode);
314 struct dentry *cache_old_dentry = NULL;
315 struct dentry *cache_dentry = NULL;
316 struct dentry *cache_parent = NULL;
319 struct hook_link_msg msg = {
320 .dentry = old_dentry,
329 if (!cache_old_inode)
332 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
333 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
334 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
335 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
340 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
341 if (IS_ERR(handle)) {
346 pre_smfs_inode(dir, cache_dir);
347 pre_smfs_inode(old_inode, cache_old_inode);
350 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
352 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
354 atomic_inc(&old_inode->i_count);
355 dput(iopen_connect_dentry(dentry, old_inode, 0));
358 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
360 post_smfs_inode(old_inode, cache_old_inode);
361 post_smfs_inode(dir, cache_dir);
363 smfs_trans_commit(dir, handle, 0);
367 post_smfs_dentry(cache_dentry);
368 post_smfs_dentry(cache_parent);
369 post_smfs_dentry(cache_old_dentry);
374 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
376 struct inode *cache_dir = I2CI(dir);
377 struct inode *cache_inode = I2CI(dentry->d_inode);
378 struct inode *parent = I2CI(dentry->d_parent->d_inode);
379 struct dentry *cache_dentry = NULL;
380 struct dentry *cache_parent = NULL;
384 struct hook_unlink_msg msg = {
386 .mode = dentry->d_inode->i_mode
392 LASSERT(cache_inode);
393 LASSERT(cache_dir->i_op->unlink);
396 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
397 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
398 if (!cache_dentry || !cache_parent) {
404 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
405 if (IS_ERR(handle)) {
410 pre_smfs_inode(dir, cache_dir);
411 pre_smfs_inode(dentry->d_inode, cache_inode);
413 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
415 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
417 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
419 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
420 post_smfs_inode(dir, cache_dir);
423 smfs_trans_commit(dir, handle, 0);
425 post_smfs_dentry(cache_dentry);
426 post_smfs_dentry(cache_parent);
430 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
433 struct inode *cache_dir = I2CI(dir);
434 struct inode *inode = NULL;
435 struct inode *parent = I2CI(dentry->d_parent->d_inode);
436 struct dentry *cache_dentry = NULL;
437 struct dentry *cache_parent = NULL;
440 struct hook_symlink_msg msg = {
442 .tgt_len = strlen(symname) + 1,
443 .symname = (char*)symname
449 LASSERT(cache_dir->i_op->symlink);
452 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
453 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
454 if (!cache_parent || !cache_dentry) {
459 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
460 if (IS_ERR(handle)) {
466 pre_smfs_inode(dir, cache_dir);
468 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
470 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
472 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
475 //smfs_update_dentry(dentry, cache_dentry);
476 d_instantiate(dentry, inode);
482 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
484 post_smfs_inode(dir, cache_dir);
485 smfs_trans_commit(dir, handle, 0);
489 post_smfs_dentry(cache_dentry);
490 post_smfs_dentry(cache_parent);
494 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
496 struct inode *cache_dir = I2CI(dir);
497 struct inode *parent = I2CI(dentry->d_parent->d_inode);
498 struct inode *inode = NULL;
499 struct dentry *cache_dentry = NULL;
500 struct dentry *cache_parent = NULL;
503 struct hook_msg msg = {
512 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
513 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
514 if (!cache_parent || !cache_dentry) {
519 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
520 if (IS_ERR(handle)) {
525 pre_smfs_inode(dir, cache_dir);
526 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
528 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
530 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
533 //smsf_update_dentry(dentry, cache_dentry);
534 d_instantiate(dentry, inode);
540 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
541 post_smfs_inode(dir, cache_dir);
542 smfs_trans_commit(dir, handle, 0);
545 post_smfs_dentry(cache_dentry);
546 post_smfs_dentry(cache_parent);
550 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
552 struct inode *cache_dir = I2CI(dir);
553 struct inode *cache_inode = I2CI(dentry->d_inode);
554 struct inode *parent = I2CI(dentry->d_parent->d_inode);
555 struct dentry *cache_dentry = NULL;
556 struct dentry *cache_parent = NULL;
559 struct hook_unlink_msg msg = {
567 LASSERT(cache_dir->i_op->rmdir);
570 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
571 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
572 if (!cache_parent || !cache_dentry) {
577 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
578 if (IS_ERR(handle) ) {
583 pre_smfs_inode(dir, cache_dir);
584 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
586 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
588 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
590 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
592 post_smfs_inode(dir, cache_dir);
593 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
594 //like vfs_rmdir is doing with inode
596 cache_dentry->d_inode->i_flags |= S_DEAD;
598 smfs_trans_commit(dir, handle, 0);
601 post_smfs_dentry(cache_dentry);
602 post_smfs_dentry(cache_parent);
606 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
607 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
610 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
611 int mode, dev_t rdev)
614 struct inode *cache_dir = I2CI(dir);
615 struct inode *inode = NULL;
616 struct inode *parent = I2CI(dentry->d_parent->d_inode);
617 struct dentry *cache_dentry = NULL;
618 struct dentry *cache_parent = NULL;
621 struct hook_msg msg = {
629 LASSERT(cache_dir->i_op->mknod);
631 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
632 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
633 if (!cache_parent || !cache_dentry) {
638 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
639 if (IS_ERR(handle)) {
644 pre_smfs_inode(dir, cache_dir);
646 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
648 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
650 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
653 //smsf_update_dentry(dentry, cache_dentry);
654 d_instantiate(dentry, inode);
660 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
662 post_smfs_inode(dir, cache_dir);
664 smfs_trans_commit(dir, handle, 0);
667 post_smfs_dentry(cache_dentry);
668 post_smfs_dentry(cache_parent);
672 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
673 struct inode *new_dir,struct dentry *new_dentry)
675 struct inode *cache_old_dir = I2CI(old_dir);
676 struct inode *cache_new_dir = I2CI(new_dir);
677 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
678 struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
679 struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
680 struct inode *cache_new_inode = NULL;
681 struct dentry *cache_old_dentry = NULL;
682 struct dentry *cache_new_dentry = NULL;
683 struct dentry *cache_new_parent = NULL;
684 struct dentry *cache_old_parent = NULL;
687 struct hook_rename_msg msg = {
688 .dentry = old_dentry,
690 .new_dentry = new_dentry
695 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
698 if (new_dentry->d_inode) {
699 cache_new_inode = I2CI(new_dentry->d_inode);
700 if (!cache_new_inode)
704 cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
705 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
707 if (!cache_old_parent || !cache_old_dentry) {
712 cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
713 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
715 if (!cache_new_parent || !cache_new_dentry) {
720 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
721 if (IS_ERR(handle)) {
726 pre_smfs_inode(old_dir, cache_old_dir);
727 pre_smfs_inode(new_dir, cache_new_dir);
728 if (new_dentry->d_inode)
729 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
731 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
733 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
734 cache_new_dir, cache_new_dentry);
736 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
738 post_smfs_inode(old_dir, cache_old_dir);
739 post_smfs_inode(new_dir, cache_new_dir);
740 if (new_dentry->d_inode)
741 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
743 smfs_trans_commit(old_dir, handle, 0);
746 post_smfs_dentry(cache_old_dentry);
747 post_smfs_dentry(cache_old_parent);
748 post_smfs_dentry(cache_new_dentry);
749 post_smfs_dentry(cache_new_parent);
753 struct inode_operations smfs_dir_iops = {
756 link: smfs_link, /* BKL held */
757 unlink: smfs_unlink, /* BKL held */
758 symlink: smfs_symlink, /* BKL held */
759 mkdir: smfs_mkdir, /* BKL held */
760 rmdir: smfs_rmdir, /* BKL held */
761 mknod: smfs_mknod, /* BKL held */
762 rename: smfs_rename, /* BKL held */
763 setxattr: smfs_setxattr, /* BKL held */
764 getxattr: smfs_getxattr, /* BKL held */
765 listxattr: smfs_listxattr, /* BKL held */
766 removexattr: smfs_removexattr, /* BKL held */
769 struct inode_operations smfs_iopen_iops = {
770 lookup: smfs_iopen_lookup,
773 static ssize_t smfs_read_dir(struct file *filp, char *buf,
774 size_t size, loff_t *ppos)
776 struct dentry *dentry = filp->f_dentry;
777 struct inode *cache_inode = NULL;
778 struct smfs_file_info *sfi = NULL;
780 loff_t *cache_ppos = NULL;
785 cache_inode = I2CI(dentry->d_inode);
787 if (!cache_inode || !cache_inode->i_fop->read)
791 if (sfi->magic != SMFS_FILE_MAGIC)
794 if (ppos != &(filp->f_pos))
795 cache_ppos = &tmp_ppos;
797 cache_ppos = &sfi->c_file->f_pos;
801 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
807 duplicate_file(filp, sfi->c_file);
812 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
814 struct dentry *dentry = filp->f_dentry;
815 struct inode *cache_inode = NULL;
816 struct smfs_file_info *sfi = NULL;
818 struct hook_readdir_msg msg = {
827 cache_inode = I2CI(dentry->d_inode);
828 if (!cache_inode || !cache_inode->i_fop->readdir)
832 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
834 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
836 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
838 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
839 duplicate_file(filp, sfi->c_file);
844 struct file_operations smfs_dir_fops = {
845 .read = smfs_read_dir,
846 .readdir = smfs_readdir, /* BKL held */
847 .ioctl = smfs_ioctl, /* BKL held */
848 .fsync = smfs_fsync, /* BKL held */
850 .release = smfs_release,
853 struct file_operations smfs_iopen_fops = {
854 .read = smfs_read_dir,