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_snap.h>
38 #include <linux/lustre_smfs.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 *cache_dir = NULL;
54 struct dentry *cache_dentry = NULL;
55 struct dentry *cache_parent = NULL;
61 cache_dir = I2CI(dir);
65 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
70 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, PRE_HOOK, rc,
72 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
73 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
75 if (!cache_dentry || !cache_parent)
76 GOTO(exit, rc = -ENOMEM);
78 pre_smfs_inode(dir, cache_dir);
79 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
80 if (cache_dir && cache_dir->i_op->create)
81 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
84 if (cache_dir && cache_dir->i_op->create)
85 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
91 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
92 &I2SMI(dir)->smi_flags);
94 GOTO(exit, rc = -ENOMEM);
96 d_instantiate(dentry, inode);
97 sm_set_inode_ops(cache_dentry->d_inode, inode);
98 post_smfs_inode(dir, cache_dir);
99 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, POST_HOOK, rc,
103 post_smfs_dentry(cache_dentry);
104 post_smfs_dentry(cache_parent);
105 smfs_trans_commit(dir, handle, 0);
109 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
110 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
112 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
113 struct nameidata *nd)
116 struct inode *cache_dir;
117 struct inode *cache_inode;
118 struct inode *inode = NULL;
119 struct dentry *cache_dentry = NULL;
120 struct dentry *cache_parent = NULL;
121 struct dentry *rc = NULL;
127 if (!(cache_dir = I2CI(dir)))
128 RETURN(ERR_PTR(-ENOENT));
130 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, PRE_HOOK, rc2,
133 RETURN(ERR_PTR(rc2));
135 /* preparing artificial backing fs dentries. */
136 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
137 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
139 if (!cache_dentry || !cache_parent)
140 GOTO(exit, rc = ERR_PTR(-ENOMEM));
142 if (!cache_dir && cache_dir->i_op->lookup)
143 GOTO(exit, rc = ERR_PTR(-ENOENT));
145 /* perform lookup in backing fs. */
146 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
147 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
149 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
152 if (rc && IS_ERR(rc))
155 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
156 if (IS_ERR(cache_inode)) {
157 dentry->d_inode = cache_inode;
158 GOTO(exit, rc = NULL);
160 inode = iget4(dir->i_sb, cache_inode->i_ino, NULL,
161 &I2SMI(dir)->smi_flags);
167 d_add(dentry, inode);
170 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, POST_HOOK, rc2,
175 post_smfs_dentry(cache_dentry);
176 post_smfs_dentry(cache_parent);
177 smfs_trans_commit(dir, handle, 0);
181 static int smfs_link(struct dentry * old_dentry,
182 struct inode * dir, struct dentry *dentry)
184 struct inode *cache_old_inode = NULL;
185 struct inode *cache_dir = I2CI(dir);
186 struct inode *inode = NULL;
187 struct dentry *cache_dentry = NULL;
188 struct dentry *cache_old_dentry = NULL;
189 struct dentry *cache_parent = NULL;
193 inode = old_dentry->d_inode;
195 cache_old_inode = I2CI(inode);
197 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
202 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle, PRE_HOOK, rc,
205 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
206 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
208 if (!cache_parent || !cache_dentry)
209 GOTO(exit, rc = -ENOMEM);
211 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
213 if (!cache_old_dentry)
214 GOTO(exit, rc = -ENOMEM);
216 pre_smfs_inode(dir, cache_dir);
217 pre_smfs_inode(inode, cache_old_dentry->d_inode);
219 if (cache_dir->i_op->link)
220 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
225 atomic_inc(&inode->i_count);
226 post_smfs_inode(inode, cache_old_dentry->d_inode);
227 d_instantiate(dentry, inode);
228 post_smfs_inode(dir, cache_dir);
230 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle, POST_HOOK,
235 post_smfs_dentry(cache_dentry);
236 post_smfs_dentry(cache_parent);
237 post_smfs_dentry(cache_old_dentry);
238 smfs_trans_commit(dir, handle, 0);
242 static int smfs_unlink(struct inode * dir,
243 struct dentry *dentry)
245 struct inode *cache_dir = I2CI(dir);
246 struct inode *cache_inode = I2CI(dentry->d_inode);
247 struct dentry *cache_dentry;
248 struct dentry *cache_parent;
253 if (!cache_dir || !cache_inode)
256 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
260 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
263 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
264 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
266 if (!cache_parent || !cache_dentry)
267 GOTO(exit, rc = -ENOMEM);
270 pre_smfs_inode(dir, cache_dir);
271 pre_smfs_inode(dentry->d_inode, cache_inode);
272 if (cache_dir->i_op->unlink)
273 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
274 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
275 post_smfs_inode(dir, cache_dir);
278 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
281 post_smfs_dentry(cache_dentry);
282 post_smfs_dentry(cache_parent);
283 smfs_trans_commit(dir, handle, 0);
287 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
290 struct inode *cache_dir = I2CI(dir);
291 struct inode *inode = NULL;
292 struct dentry *cache_dentry;
293 struct dentry *cache_parent;
300 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
305 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
308 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
309 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
311 if (!cache_parent || !cache_dentry)
312 GOTO(exit, rc = -ENOMEM);
314 pre_smfs_inode(dir, cache_dir);
315 if (cache_dir->i_op->symlink)
316 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
318 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
319 &I2SMI(dir)->smi_flags);
320 post_smfs_inode(dir, cache_dir);
322 d_instantiate(dentry, inode);
326 tgt_len = strlen(symname) + 1;
328 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
329 POST_HOOK, rc, exit);
332 post_smfs_dentry(cache_dentry);
333 post_smfs_dentry(cache_parent);
334 smfs_trans_commit(dir, handle, 0);
338 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
341 struct inode *cache_dir = I2CI(dir);
342 struct inode *inode = NULL;
343 struct dentry *cache_dentry;
344 struct dentry *cache_parent;
351 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
357 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
360 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
361 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
363 if (!cache_parent || !cache_dentry)
364 GOTO(exit, rc = -ENOMEM);
366 pre_smfs_inode(dir, cache_dir);
368 if (cache_dir->i_op->mkdir)
369 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
374 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
375 &I2SMI(dir)->smi_flags);
377 GOTO(exit, rc = -ENOENT);
379 d_instantiate(dentry, inode);
380 post_smfs_inode(dir, cache_dir);
382 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, POST_HOOK, rc,
386 post_smfs_dentry(cache_dentry);
387 post_smfs_dentry(cache_parent);
388 smfs_trans_commit(dir, handle, 0);
392 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
394 struct inode *cache_dir = I2CI(dir);
395 struct inode *cache_inode = I2CI(dentry->d_inode);
396 struct dentry *cache_dentry = NULL;
397 struct dentry *cache_parent = NULL;
399 int rc = 0, mode = S_IFDIR;
404 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
405 if (IS_ERR(handle) ) {
406 CERROR("smfs_do_mkdir: no space for transaction\n");
412 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
415 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
416 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
418 if (!cache_parent || !cache_dentry)
419 GOTO(exit, rc = -ENOMEM);
421 pre_smfs_inode(dir, cache_dir);
422 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
423 if (cache_dir->i_op->rmdir)
424 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
426 post_smfs_inode(dir, cache_dir);
427 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
430 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
433 post_smfs_dentry(cache_dentry);
434 post_smfs_dentry(cache_parent);
435 smfs_trans_commit(dir, handle, 0);
439 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
440 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
443 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
444 int mode, dev_t rdev)
447 struct inode *cache_dir = I2CI(dir);
448 struct inode *inode = NULL;
449 struct dentry *cache_dentry = NULL;
450 struct dentry *cache_parent = NULL;
457 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
458 if (IS_ERR(handle)) {
459 CERROR("smfs_do_mkdir: no space for transaction\n");
464 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
467 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
468 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
469 if (!cache_parent || !cache_dentry)
470 GOTO(exit, rc = -ENOMEM);
472 pre_smfs_inode(dir, cache_dir);
473 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
475 if (!cache_dir->i_op->mknod)
478 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
482 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
483 &I2SMI(dir)->smi_flags);
484 d_instantiate(dentry, inode);
486 pre_smfs_inode(dir, cache_dir);
487 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
489 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
494 post_smfs_dentry(cache_dentry);
495 post_smfs_dentry(cache_parent);
496 smfs_trans_commit(dir, handle, 0);
500 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
501 struct inode * new_dir,struct dentry *new_dentry)
503 struct inode *cache_old_dir = I2CI(old_dir);
504 struct inode *cache_new_dir = I2CI(new_dir);
505 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
507 struct inode *cache_new_inode = new_dentry->d_inode ?
508 I2CI(new_dentry->d_inode) : NULL;
510 struct dentry *cache_old_dentry = NULL;
511 struct dentry *cache_new_dentry = NULL;
512 struct dentry *cache_new_parent = NULL;
513 struct dentry *cache_old_parent = NULL;
517 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
520 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
521 if (IS_ERR(handle)) {
522 CERROR("smfs_do_mkdir: no space for transaction\n");
528 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
531 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
532 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
534 if (!cache_old_parent || !cache_old_dentry)
535 GOTO(exit, rc = -ENOMEM);
537 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
538 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
540 if (!cache_new_parent || !cache_new_dentry)
541 GOTO(exit, rc = -ENOMEM);
543 pre_smfs_inode(old_dir, cache_old_dir);
544 pre_smfs_inode(new_dir, cache_new_dir);
546 if (cache_old_dir->i_op->rename)
547 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
548 cache_new_dir, cache_new_dentry);
550 post_smfs_inode(old_dir, cache_old_dir);
551 post_smfs_inode(new_dir, cache_new_dir);
553 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
554 POST_HOOK, rc, exit);
556 if (new_dentry->d_inode)
557 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
560 post_smfs_dentry(cache_old_dentry);
561 post_smfs_dentry(cache_old_parent);
562 post_smfs_dentry(cache_new_dentry);
563 post_smfs_dentry(cache_new_parent);
564 smfs_trans_commit(old_dir, handle, 0);
568 struct inode_operations smfs_dir_iops = {
571 link: smfs_link, /* BKL held */
572 unlink: smfs_unlink, /* BKL held */
573 symlink: smfs_symlink, /* BKL held */
574 mkdir: smfs_mkdir, /* BKL held */
575 rmdir: smfs_rmdir, /* BKL held */
576 mknod: smfs_mknod, /* BKL held */
577 rename: smfs_rename, /* BKL held */
578 setxattr: smfs_setxattr, /* BKL held */
579 getxattr: smfs_getxattr, /* BKL held */
580 listxattr: smfs_listxattr, /* BKL held */
581 removexattr: smfs_removexattr, /* BKL held */
584 static ssize_t smfs_read_dir(struct file *filp, char *buf,
585 size_t size, loff_t *ppos)
587 struct dentry *dentry = filp->f_dentry;
588 struct inode *cache_inode = NULL;
589 struct smfs_file_info *sfi = NULL;
594 cache_inode = I2CI(dentry->d_inode);
600 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
602 if (ppos != &(filp->f_pos))
603 cache_ppos = &tmp_ppos;
605 cache_ppos = &sfi->c_file->f_pos;
608 if (cache_inode->i_fop->read)
609 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
613 duplicate_file(filp, sfi->c_file);
617 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
619 struct dentry *dentry = filp->f_dentry;
620 struct inode *cache_inode = NULL;
621 struct smfs_file_info *sfi = NULL;
624 cache_inode = I2CI(dentry->d_inode);
629 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
631 if (cache_inode->i_fop->readdir)
632 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
634 duplicate_file(filp, sfi->c_file);
638 struct file_operations smfs_dir_fops = {
640 readdir: smfs_readdir, /* BKL held */
641 ioctl: smfs_ioctl, /* BKL held */
642 fsync: smfs_fsync, /* BKL held */
644 release: smfs_release,