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>
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;
57 struct inode *cache_inode = NULL;
62 cache_dir = I2CI(dir);
63 LASSERT(cache_dir && cache_dir->i_op->create);
66 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
67 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
69 if (!cache_dentry || !cache_parent)
70 GOTO(exit, rc = -ENOMEM);
72 pre_smfs_inode(dir, cache_dir);
74 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
76 GOTO(exit, rc = -ENOSPC);
78 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle,
81 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
82 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
84 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
86 cache_inode = cache_dentry->d_inode;
91 SMFS_IGET(dir, cache_inode->i_ino, inode, rc, exit);
93 d_instantiate(dentry, inode);
95 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle,
98 post_smfs_inode(dir, cache_dir);
102 post_smfs_dentry(cache_dentry);
103 post_smfs_dentry(cache_parent);
104 smfs_trans_commit(dir, handle, 0);
108 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
109 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
111 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
112 struct nameidata *nd)
115 struct inode *cache_dir;
116 struct inode *cache_inode;
117 struct inode *inode = NULL;
118 struct dentry *cache_dentry = NULL;
119 struct dentry *cache_parent = NULL;
120 struct dentry *rdentry = NULL;
125 cache_dir = I2CI(dir);
126 if (!cache_dir || !cache_dir->i_op->lookup)
127 RETURN(ERR_PTR(-ENOENT));
129 /* preparing artificial backing fs dentries. */
130 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
131 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
133 if (!cache_dentry || !cache_parent)
134 RETURN (ERR_PTR(-ENOMEM));
136 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, NULL,
139 /* perform lookup in backing fs. */
140 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
141 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry);
143 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
145 cache_inode = cache_dentry->d_inode;
151 cache_inode = rdentry->d_inode;
157 SMFS_IGET(dir, cache_inode->i_ino, inode, rc, exit);
160 //rdentry = d_splice_alias(inode, dentry);
161 d_add(dentry, inode);
162 //CDEBUG(D_INODE, "dir %p (inode %p)\n", dir, cache_dir);
164 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, NULL, POST_HOOK, rc,
167 post_smfs_dentry(cache_dentry);
168 post_smfs_dentry(cache_parent);
172 static int smfs_link(struct dentry *old_dentry,
173 struct inode *dir, struct dentry *dentry)
175 struct inode *cache_old_inode = NULL;
176 struct inode *cache_dir = NULL;
177 struct inode *old_inode = NULL;
178 struct dentry *cache_dentry = NULL;
179 struct dentry *cache_old_dentry = NULL;
180 struct dentry *cache_parent = NULL;
186 cache_dir = I2CI(dir);
187 if (!cache_dir || !cache_dir->i_op->link)
188 GOTO(exit, rc = -ENOENT);
190 old_inode = old_dentry->d_inode;
191 cache_old_inode = I2CI(old_inode);
192 if (!cache_old_inode)
193 GOTO(exit, rc = -ENOENT);
195 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
196 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
197 if (!cache_parent || !cache_dentry)
198 GOTO(exit, rc = -ENOMEM);
200 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
202 if (!cache_old_dentry)
203 GOTO(exit, rc = -ENOMEM);
205 pre_smfs_inode(dir, cache_dir);
206 pre_smfs_inode(old_inode, cache_old_inode);
208 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
210 GOTO(exit, rc = -ENOSPC);
213 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle,
216 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
221 atomic_inc(&old_inode->i_count);
222 d_instantiate(dentry, old_inode);
224 post_smfs_inode(old_inode, cache_old_inode);
225 post_smfs_inode(dir, cache_dir);
227 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle,
228 POST_HOOK, rc, exit);
231 post_smfs_dentry(cache_dentry);
232 post_smfs_dentry(cache_parent);
233 post_smfs_dentry(cache_old_dentry);
234 smfs_trans_commit(dir, handle, 0);
238 static int smfs_unlink(struct inode * dir,
239 struct dentry *dentry)
241 struct inode *cache_dir = I2CI(dir);
242 struct inode *cache_inode = I2CI(dentry->d_inode);
243 struct dentry *cache_dentry;
244 struct dentry *cache_parent;
251 if (!cache_dir || !cache_inode || !cache_dir->i_op->unlink)
254 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
255 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
257 if (!cache_parent || !cache_dentry)
258 GOTO(exit, rc = -ENOMEM);
261 pre_smfs_inode(dir, cache_dir);
262 pre_smfs_inode(dentry->d_inode, cache_inode);
264 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
266 GOTO(exit, rc = -ENOSPC);
268 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
271 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
275 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
276 post_smfs_inode(dir, cache_dir);
279 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
282 smfs_trans_commit(dir, handle, 0);
283 post_smfs_dentry(cache_dentry);
284 post_smfs_dentry(cache_parent);
288 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
291 struct inode *cache_dir = I2CI(dir);
292 struct inode *inode = NULL;
293 struct dentry *cache_dentry;
294 struct dentry *cache_parent;
300 if (!cache_dir || !cache_dir->i_op->symlink)
303 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
304 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
306 if (!cache_parent || !cache_dentry)
307 GOTO(exit, rc = -ENOMEM);
309 pre_smfs_inode(dir, cache_dir);
311 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
313 GOTO(exit, rc = -ENOSPC);
316 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
319 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
323 SMFS_IGET(dir, cache_dentry->d_inode->i_ino, inode, rc, exit);
325 d_instantiate(dentry, inode);
326 post_smfs_inode(dir, cache_dir);
328 tgt_len = strlen(symname) + 1;
329 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
330 POST_HOOK, rc, exit);
334 smfs_trans_commit(dir, handle, 0);
335 post_smfs_dentry(cache_dentry);
336 post_smfs_dentry(cache_parent);
340 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
343 struct inode *cache_dir = I2CI(dir);
344 struct inode *inode = NULL;
345 struct dentry *cache_dentry;
346 struct dentry *cache_parent;
352 if (!cache_dir || !cache_dir->i_op->mkdir)
355 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
356 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
358 if (!cache_parent || !cache_dentry)
359 GOTO(exit, rc = -ENOMEM);
361 pre_smfs_inode(dir, cache_dir);
363 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
365 GOTO(exit, rc = -ENOSPC);
369 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
372 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
377 SMFS_IGET(dir, cache_dentry->d_inode->i_ino, inode, rc, exit);
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 smfs_trans_commit(dir, handle, 0);
387 post_smfs_dentry(cache_dentry);
388 post_smfs_dentry(cache_parent);
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;
403 if (!cache_dir || !cache_dir->i_op->rmdir)
406 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
407 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
409 if (!cache_parent || !cache_dentry)
410 GOTO(exit, rc = -ENOMEM);
412 pre_smfs_inode(dir, cache_dir);
413 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
415 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
416 if (IS_ERR(handle) ) {
417 CERROR("smfs_do_mkdir: no space for transaction\n");
418 GOTO(exit, rc = -ENOSPC);
423 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
426 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
430 post_smfs_inode(dir, cache_dir);
431 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
434 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
437 smfs_trans_commit(dir, handle, 0);
438 post_smfs_dentry(cache_dentry);
439 post_smfs_dentry(cache_parent);
443 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
444 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
447 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
448 int mode, dev_t rdev)
451 struct inode *cache_dir = I2CI(dir);
452 struct inode *inode = NULL;
453 struct dentry *cache_dentry = NULL;
454 struct dentry *cache_parent = NULL;
460 if (!cache_dir || !cache_dir->i_op->mknod)
463 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
464 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
466 if (!cache_parent || !cache_dentry)
467 GOTO(exit, rc = -ENOMEM);
469 pre_smfs_inode(dir, cache_dir);
470 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
472 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
473 if (IS_ERR(handle)) {
474 CERROR("smfs_do_mkdir: no space for transaction\n");
475 GOTO(exit, rc = -ENOSPC);
479 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
482 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
486 SMFS_IGET(dir, cache_dentry->d_inode->i_ino, inode, rc, exit);
488 d_instantiate(dentry, inode);
490 post_smfs_inode(dir, cache_dir);
491 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
493 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
498 smfs_trans_commit(dir, handle, 0);
499 post_smfs_dentry(cache_dentry);
500 post_smfs_dentry(cache_parent);
504 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
505 struct inode *new_dir,struct dentry *new_dentry)
507 struct inode *cache_old_dir = I2CI(old_dir);
508 struct inode *cache_new_dir = I2CI(new_dir);
509 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
511 struct inode *cache_new_inode = new_dentry->d_inode ?
512 I2CI(new_dentry->d_inode) : NULL;
514 struct dentry *cache_old_dentry = NULL;
515 struct dentry *cache_new_dentry = NULL;
516 struct dentry *cache_new_parent = NULL;
517 struct dentry *cache_old_parent = NULL;
523 if (!cache_old_dir || !cache_new_dir || !cache_old_inode
524 || !cache_old_dir->i_op->rename)
527 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
529 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
532 if (!cache_old_parent || !cache_old_dentry)
533 GOTO(exit, rc = -ENOMEM);
535 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
536 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
539 if (!cache_new_parent || !cache_new_dentry)
540 GOTO(exit, rc = -ENOMEM);
542 pre_smfs_inode(old_dir, cache_old_dir);
543 pre_smfs_inode(new_dir, cache_new_dir);
545 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
546 if (IS_ERR(handle)) {
547 CERROR("smfs_do_mkdir: no space for transaction\n");
548 GOTO(exit, rc = -ENOSPC);
552 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME,
553 handle, PRE_HOOK, rc, exit);
555 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
556 cache_new_dir, cache_new_dentry);
558 post_smfs_inode(old_dir, cache_old_dir);
559 post_smfs_inode(new_dir, cache_new_dir);
561 if (new_dentry->d_inode)
562 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
564 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
565 POST_HOOK, rc, exit);
570 smfs_trans_commit(old_dir, handle, 0);
571 post_smfs_dentry(cache_old_dentry);
572 post_smfs_dentry(cache_old_parent);
573 post_smfs_dentry(cache_new_dentry);
574 post_smfs_dentry(cache_new_parent);
578 struct inode_operations smfs_dir_iops = {
581 link: smfs_link, /* BKL held */
582 unlink: smfs_unlink, /* BKL held */
583 symlink: smfs_symlink, /* BKL held */
584 mkdir: smfs_mkdir, /* BKL held */
585 rmdir: smfs_rmdir, /* BKL held */
586 mknod: smfs_mknod, /* BKL held */
587 rename: smfs_rename, /* BKL held */
588 setxattr: smfs_setxattr, /* BKL held */
589 getxattr: smfs_getxattr, /* BKL held */
590 listxattr: smfs_listxattr, /* BKL held */
591 removexattr: smfs_removexattr, /* BKL held */
594 static ssize_t smfs_read_dir(struct file *filp, char *buf,
595 size_t size, loff_t *ppos)
597 struct dentry *dentry = filp->f_dentry;
598 struct inode *cache_inode = NULL;
599 struct smfs_file_info *sfi = NULL;
601 loff_t *cache_ppos = NULL;
606 cache_inode = I2CI(dentry->d_inode);
608 if (!cache_inode || !cache_inode->i_fop->read)
612 if (sfi->magic != SMFS_FILE_MAGIC)
615 if (ppos != &(filp->f_pos))
616 cache_ppos = &tmp_ppos;
618 cache_ppos = &sfi->c_file->f_pos;
622 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
628 duplicate_file(filp, sfi->c_file);
633 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
635 struct dentry *dentry = filp->f_dentry;
636 struct inode *cache_inode = NULL;
637 struct smfs_file_info *sfi = NULL;
642 cache_inode = I2CI(dentry->d_inode);
643 if (!cache_inode || !cache_inode->i_fop->readdir)
647 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
649 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
652 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
653 CDEBUG(D_INODE,"readdir rc=%u\n",rc);
657 duplicate_file(filp, sfi->c_file);
659 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
660 POST_HOOK, rc, exit);
668 struct file_operations smfs_dir_fops = {
669 .read = smfs_read_dir,
670 .readdir = smfs_readdir, /* BKL held */
671 .ioctl = smfs_ioctl, /* BKL held */
672 .fsync = smfs_fsync, /* BKL held */
674 .release = smfs_release,